Typica is a free program for professional coffee roasters. https://typica.us
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

newbatch.xml 46KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. <window id="batchWindow">
  2. <menu name="File">
  3. <item id="print" shortcut="Ctrl+P">Print...</item>
  4. </menu>
  5. <menu name="Batch">
  6. <item id="new" shortcut="Ctrl+N">New Batch...</item>
  7. </menu>
  8. <layout type="vertical">
  9. <layout type="horizontal">
  10. <label>Scheduled Batch ID:</label>
  11. <line id="search" />
  12. <stretch />
  13. </layout>
  14. <tabbar id="tabs" />
  15. <layout type="stack" id="pages">
  16. <page>
  17. <layout type="vertical">
  18. <layout type="horizontal">
  19. <label>Filter:</label>
  20. <line id="filter" />
  21. <label>Value:</label>
  22. <line id="filtervalue" />
  23. <stretch />
  24. </layout>
  25. <sqlview id="batches" />
  26. </layout>
  27. </page>
  28. <page>
  29. <layout type="vertical">
  30. <splitter type="horizontal" id="splitter">
  31. <widget id="required">
  32. <layout type="vertical">
  33. <layout type="horizontal">
  34. <label>Machine:</label>
  35. <line id="machine" writable="false" />
  36. <label>Unit:</label>
  37. <sqldrop id="unit" />
  38. <stretch />
  39. </layout>
  40. <layout type="horizontal">
  41. <label>Roasted Coffee:</label>
  42. <sqldrop data="0" display="1" showdata="true" id="roasted">
  43. <null />
  44. <query>SELECT id, name FROM items WHERE category = 'Coffee: Roasted' AND id IN (SELECT item FROM current_items) ORDER BY name</query>
  45. </sqldrop>
  46. <stretch />
  47. </layout>
  48. <label>Green Coffee:</label>
  49. <sqltablearray columns="3" id="greens">
  50. <column name="Coffee" delegate="sql" showdata="true" null="true" nulltext="Delete" nulldata="delete" data="0" display="1">SELECT id, name FROM coffees WHERE quantity &lt;&gt; 0 ORDER BY name</column>
  51. <column name="Weight" delegate="numeric" />
  52. <column name="Remaining" />
  53. </sqltablearray>
  54. <layout type="horizontal">
  55. <label>Green Weight:</label>
  56. <line id="green" writable="false">0.0</line>
  57. </layout>
  58. <layout type="horizontal">
  59. <button name="Load Profile" type="push" id="load" />
  60. <button name="No Profile" type="push" id="noprofile" />
  61. </layout>
  62. <layout type="horizontal">
  63. <label>Time:</label>
  64. <line id="time" writable="false" />
  65. <label>Duration:</label>
  66. <line id="duration" writable="false" />
  67. </layout>
  68. <layout type="horizontal">
  69. <label>Roasted Weight:</label>
  70. <line id="roast" validator="numeric">0.0</line>
  71. <label>Weight Loss:</label>
  72. <line id="wloss" writable="false" />
  73. <button type="check" id="approval" name="Approved" />
  74. <stretch />
  75. </layout>
  76. <layout type="horizontal">
  77. <label>Annotation:</label>
  78. <textarea id="annotation" />
  79. </layout>
  80. <layout type="horizontal">
  81. <button name="Submit" id="submit" type="push" />
  82. <button name="Save log as target profile" type="check" id="target" />
  83. </layout>
  84. </layout>
  85. </widget>
  86. <widget>
  87. <layout type="vertical">
  88. <label>Connected Scales</label>
  89. <layout type="vertical" id="scales" />
  90. <label>Expected Weight Loss</label>
  91. <line id="lossspec" writable="false" />
  92. <label>Expected Roasted Weight</label>
  93. <layout type="horizontal">
  94. <label>Min:</label>
  95. <line id="minroastweight" writable="false" />
  96. </layout>
  97. <layout type="horizontal">
  98. <label>Expected:</label>
  99. <line id="expectedroastweight" writable="false" />
  100. </layout>
  101. <layout type="horizontal">
  102. <label>Max:</label>
  103. <line id="maxroastweight" writable="false" />
  104. </layout>
  105. <label>Degree of Roast</label>
  106. <label>Whole Bean:</label>
  107. <roastcoloredit id="wholecolor" />
  108. <label id="wholespec">-</label>
  109. <label>Ground:</label>
  110. <roastcoloredit id="groundcolor" />
  111. <label id="groundspec">-</label>
  112. <label>Specification Details</label>
  113. <textarea id="specnotes" />
  114. <layout type="horizontal">
  115. <label>File ID:</label>
  116. <line id="filenofield" writable="false" />
  117. </layout>
  118. <stretch />
  119. </layout>
  120. </widget>
  121. <widget id="tagcontainer" ignoreSizePolicy="true">
  122. <layout type="vertical">
  123. <webview id="batchTag" />
  124. <layout type="horizontal">
  125. <printerselector id="printerlist" />
  126. <button name="Print" id="printbutton" type="push" />
  127. </layout>
  128. </layout>
  129. </widget>
  130. </splitter>
  131. </layout>
  132. </page>
  133. </layout>
  134. </layout>
  135. <program>
  136. <![CDATA[
  137. var scheduledID = 0;
  138. var window = this;
  139. var splitter = findChildObject(this, 'splitter');
  140. window.aboutToClose.connect(function() {
  141. splitter.saveState("script/newbatch/splitter");
  142. });
  143. splitter.setCollapsible(0, false);
  144. splitter.restoreState("script/newbatch/splitter");
  145. var tabs = findChildObject(this, 'tabs');
  146. tabs.addTab(TTR("batchWindow", "Schedule"));
  147. tabs.addTab(TTR("batchWindow", "Batch"));
  148. var pages = findChildObject(this, 'pages');
  149. tabs.currentChanged.connect(function(index) {
  150. pages.setCurrentIndex(index);
  151. });
  152. var batches = findChildObject(this, 'batches');
  153. var filter = findChildObject(this, 'filter');
  154. var filtervalue = findChildObject(this, 'filtervalue');
  155. filter.setText(QSettings.value("script/newbatch/filter", ""));
  156. filtervalue.setText(QSettings.value("script/newbatch/filtervalue", ""));
  157. var updateSchedule = function() {
  158. var filterclause = "";
  159. if(filter.text.length > 0) {
  160. filterclause = " AND data->'filters' @> "
  161. filterclause += "'[{\"key\":\"" + filter.text + '"';
  162. if(filtervalue.text.length > 0) {
  163. filterclause += ',"value":"' + filtervalue.text + '"';
  164. }
  165. filterclause += "}]'";
  166. }
  167. var q = "SELECT id, (SELECT name FROM items WHERE id = (data#>>'{roasted}')::numeric), (data#>>'{green_weight}')::numeric FROM scheduled_roasts WHERE machine IS NULL" + filterclause;
  168. batches.setQuery(q);
  169. };
  170. filter.editingFinished.connect(function() {
  171. QSettings.setValue("script/newbatch/filter", filter.text);
  172. updateSchedule();
  173. });
  174. filtervalue.editingFinished.connect(function() {
  175. QSettings.setValue("script/newbatch/filtervalue", filtervalue.text);
  176. updateSchedule();
  177. });
  178. updateSchedule();
  179. if(batches.rows() > 0) {
  180. tabs.setCurrentIndex(0);
  181. } else {
  182. tabs.setCurrentIndex(1);
  183. }
  184. batches.setHeaderData(0, TTR("batchWindow", "ID"));
  185. batches.setHeaderData(1, TTR("batchWindow", "Roasted Coffee"));
  186. batches.setHeaderData(2, TTR("batchWindow", "Green Weight"));
  187. batches.alternatingRowColors = true;
  188. batches.selectionMode = 1;
  189. batches.selectionBehavior = 1;
  190. batches.editTreiggers = 0;
  191. var snotifier = Application.subscribe("scheduledroastschange");
  192. snotifier.notify.connect(function() {
  193. updateSchedule();
  194. });
  195. var unitBox = findChildObject(this, 'unit');
  196. unitBox.addItem("g");
  197. unitBox.addItem("Kg");
  198. unitBox.addItem("oz");
  199. unitBox.addItem("lb");
  200. unitBox.currentIndex = (QSettings.value("script/batch_unit", unitBox.findText("lb")));
  201. unitBox['currentIndexChanged(int)'].connect(function() {
  202. QSettings.setValue("script/batch_unit", unitBox.currentIndex);
  203. });
  204. var machine = findChildObject(this, "machine");
  205. machine.setText(selectedRoasterName + " (" + selectedRoasterID + ")");
  206. var newMenu = findChildObject(this, 'new');
  207. newMenu.triggered.connect(function() {
  208. var bwindow = createWindow("batchWindow");
  209. bwindow.windowTitle = TTR("batchWindow", "Typica - [*]New Batch");
  210. });
  211. var batch = this;
  212. var table = findChildObject(this, 'greens');
  213. var green = findChildObject(this, 'green');
  214. var model = table.model();
  215. var lossField = findChildObject(this, 'wloss');
  216. lossField.maximumWidth = 80;
  217. var roasted = findChildObject(this, 'roasted');
  218. var roastwt = findChildObject(this, 'roast');
  219. roastwt.maximumWidth = 80;
  220. var scalesLayout = findChildObject(this, 'scales');
  221. scalesLayout.spacing = 10;
  222. var batchTag = findChildObject(this, 'batchTag');
  223. if(navigationwindow.loggingWindow.scales.length > 0) {
  224. for(var i = 0; i < navigationwindow.loggingWindow.scales.length; i++) {
  225. var scale = navigationwindow.loggingWindow.scales[i];
  226. var label = new DragLabel();
  227. var weighButton = new QPushButton();
  228. weighButton.text = "Weigh";
  229. weighButton.clicked.connect(scale.weigh);
  230. label.updateMeasurement = function(m, u) {
  231. switch(unitBox.currentIndex) {
  232. case 0:
  233. this.text = Units.convertWeight(m, u, Units.Gram).toFixed(1);
  234. break;
  235. case 1:
  236. this.text = Units.convertWeight(m, u, Units.Kilogram).toFixed(4);
  237. break;
  238. case 2:
  239. this.text = Units.convertWeight(m, u, Units.Ounce).toFixed(3);
  240. break;
  241. case 3:
  242. this.text = Units.convertWeight(m, u, Units.Pound).toFixed(4);
  243. break;
  244. }
  245. };
  246. scalesLayout.addWidget(label);
  247. scalesLayout.addWidget(weighButton);
  248. scale.newMeasurement.connect(function(m, u) {
  249. label.updateMeasurement(m, u);
  250. });
  251. scale.weigh();
  252. unitBox['currentIndexChanged(int)'].connect(scale.weigh);
  253. }
  254. }
  255. var remainingStock = new Array();
  256. var query = new QSqlQuery();
  257. query.exec("SELECT id, quantity, (SELECT conversion FROM lb_bag_conversion WHERE item = id) FROM coffees WHERE quantity <> 0");
  258. while(query.next()) {
  259. remainingStock.push({id: query.value(0),
  260. quantity: query.value(1),
  261. conversion: query.value(2)});
  262. }
  263. query = query.invalidate();
  264. var convertToPounds = function(w, u) {
  265. switch(u) {
  266. case "g":
  267. return w * 0.0022;
  268. case "oz":
  269. return w * 0.0625;
  270. case "Kg":
  271. return w * 2.2;
  272. }
  273. return w;
  274. };
  275. var convertFromPounds = function(w, u) {
  276. switch(u) {
  277. case "g":
  278. return w / 0.0022;
  279. case "oz":
  280. return w / 0.0625;
  281. case "Kg":
  282. return w / 2.2;
  283. }
  284. return w;
  285. };
  286. var specnotes = findChildObject(this, 'specnotes');
  287. specnotes.readOnly = true;
  288. var lossspec = findChildObject(this, 'lossspec');
  289. var minfield = findChildObject(this, 'minroastweight');
  290. var midfield = findChildObject(this, 'expectedroastweight');
  291. var maxfield = findChildObject(this, 'maxroastweight');
  292. var minloss = 0;
  293. var maxloss = 0;
  294. var expectloss = 0;
  295. var updateGreenTable = function() {
  296. var deleteRow = -1;
  297. /* The combo box delegate updates user data before display data
  298. and this code is executed before the model update is fully
  299. complete. Rather than rely on this behavior continuing, we
  300. check that the display value has also been updated and defer
  301. row removal until both updates are complete.
  302. */
  303. while((deleteRow = table.findData("delete", 0)) > -1) {
  304. if(table.data(deleteRow, 0, 0) == "Delete") {
  305. table.removeRow(table.findData("delete", 0));
  306. } else {
  307. break;
  308. }
  309. }
  310. green.text = table.columnSum(1, 0);
  311. table.resizeColumnToContents(0);
  312. var gid = 0;
  313. var r = 0;
  314. while(gid >= 0)
  315. {
  316. gid = Number(table.data(r, 0, 32));
  317. if(isNaN(gid))
  318. {
  319. gid = -1;
  320. break;
  321. }
  322. var bagConversion = 1;
  323. for(var i = 0; i < remainingStock.length; i++)
  324. {
  325. if(gid == Number(remainingStock[i].id))
  326. {
  327. var displayValue = Number(remainingStock[i].quantity);
  328. bagConversion = Number(remainingStock[i].conversion);
  329. if(!isNaN(Number(table.data(r, 1, 0))))
  330. {
  331. var change = Number(table.data(r, 1, 0));
  332. switch(unitBox.currentIndex)
  333. {
  334. case 0:
  335. change = convertToPounds(change, "g");
  336. break;
  337. case 1:
  338. change = convertToPounds(change, "Kg");
  339. break;
  340. case 2:
  341. change = convertToPounds(change, "oz");
  342. break;
  343. }
  344. displayValue -= change;
  345. }
  346. var bagCount = (displayValue / bagConversion).toFixed(2);
  347. switch(unitBox.currentIndex)
  348. {
  349. case 0:
  350. displayValue = convertFromPounds(displayValue, "g");
  351. break;
  352. case 1:
  353. displayValue = convertFromPounds(displayValue, "Kg");
  354. break;
  355. case 2:
  356. displayValue = convertFromPounds(displayValue, "oz");
  357. break;
  358. }
  359. displayValue = "" + Number(displayValue).toFixed(3) + " (" + Number(bagCount).toFixed(3) + " bags)";
  360. if(table.data(r, 2, 0) != displayValue)
  361. {
  362. table.setData(r, 2, displayValue, 0);
  363. table.setData(r, 2, displayValue, 2);
  364. table.resizeColumnToContents(2);
  365. }
  366. }
  367. }
  368. r++;
  369. }
  370. if(parseFloat(green.text) > 0)
  371. {
  372. var expectedLossDesc = "";
  373. if(lossspec.text.length > 0) {
  374. if(minloss != expectloss) {
  375. minfield.text = (-(parseFloat(green.text)) * (maxloss - 1)).toFixed(2);
  376. maxfield.text = (-(parseFloat(green.text)) * (minloss - 1)).toFixed(2);
  377. }
  378. midfield.text = (-(parseFloat(green.text)) * (expectloss - 1)).toFixed(2);
  379. }
  380. if(parseFloat(roastwt.text) > 0)
  381. {
  382. lossField.text = (((parseFloat(green.text) - parseFloat(roastwt.text)) / parseFloat(green.text)) * 100).toFixed(2) + "%";
  383. }
  384. else
  385. {
  386. lossField.text = "100%";
  387. }
  388. }
  389. };
  390. model.dataChanged.connect(updateGreenTable);
  391. unitBox['currentIndexChanged(int)'].connect(updateGreenTable);
  392. roastwt.textChanged.connect(function() {
  393. if(parseFloat(green.text) > 0)
  394. {
  395. if(parseFloat(roastwt.text) > 0)
  396. {
  397. lossField.text = (((parseFloat(green.text) - parseFloat(roastwt.text)) / parseFloat(green.text)) * 100).toFixed(2) + "%";
  398. }
  399. else
  400. {
  401. lossField.text = "100%";
  402. }
  403. }
  404. });
  405. var profilebutton = findChildObject(this, 'load');
  406. profilebutton.setEnabled(false);
  407. var wholespec = findChildObject(this, 'wholespec');
  408. var groundspec = findChildObject(this, 'groundspec');
  409. roasted['currentIndexChanged(int)'].connect(function() {
  410. table.clear();
  411. var query = new QSqlQuery();
  412. var q = "SELECT EXISTS(SELECT 1 FROM item_files WHERE item = ";
  413. q = q + roasted.currentData();
  414. q = q + ")";
  415. query.exec(q);
  416. if(query.next())
  417. {
  418. if(query.value(0) == 'false')
  419. {
  420. profilebutton.setEnabled(false);
  421. }
  422. else
  423. {
  424. profilebutton.setEnabled(true);
  425. }
  426. }
  427. else
  428. {
  429. profilebutton.setEnabled(false);
  430. }
  431. var title = "Typica - [*]New Batch (";
  432. title = title + roasted.currentText;
  433. title = title + ")";
  434. batch.windowTitle = title;
  435. q = "SELECT unroasted_id FROM roasting_log WHERE roasted_id = ";
  436. q = q + roasted.currentData();
  437. q = q + " AND time = (SELECT max(time) FROM roasting_log WHERE roasted_id = ";
  438. q = q + roasted.currentData();
  439. q = q + ")";
  440. query.exec(q);
  441. if(query.next())
  442. {
  443. var unroasted_items = sqlToArray(query.value(0));
  444. var names = [];
  445. q = "SELECT name FROM items WHERE id = :id AND quantity <> 0";
  446. query.prepare(q);
  447. var allInStock = true;
  448. for(var i = 0; i < unroasted_items.length; i++)
  449. {
  450. query.bind("id", unroasted_items[i]);
  451. query.exec();
  452. if(query.next())
  453. {
  454. names[i] = query.value(0);
  455. }
  456. else
  457. {
  458. allInStock = false;
  459. }
  460. }
  461. if(allInStock)
  462. {
  463. for(var i = 0; i < unroasted_items.length; i++)
  464. {
  465. table.setData(i, 0, names[i], 0);
  466. table.setData(i, 0, unroasted_items[i], 32);
  467. }
  468. }
  469. }
  470. query.prepare("SELECT loss, tolerance, notes, spec FROM roasting_specification WHERE item = :id1 AND time = (SELECT max(time) FROM roasting_specification WHERE item = :id2)");
  471. query.bind(":id1", roasted.currentData());
  472. query.bind(":id2", roasted.currentData());
  473. query.exec();
  474. var lossSpecDescription = "";
  475. if(query.next()) {
  476. if(query.value(0).length > 0) {
  477. lossSpecDescription += (Number(query.value(0)) * 100).toFixed(2);
  478. minloss = Number(query.value(0));
  479. maxloss = Number(query.value(0));
  480. expectloss = Number(query.value(0));
  481. }
  482. if(query.value(1).length > 0) {
  483. lossSpecDescription += " +/- " + (Number(query.value(1)) * 100).toFixed(2);
  484. minloss -= Number(query.value(1));
  485. maxloss += Number(query.value(1));
  486. }
  487. if(lossSpecDescription.length > 0) {
  488. lossSpecDescription += "%";
  489. }
  490. lossspec.text = lossSpecDescription;
  491. specnotes.plainText = query.value(2);
  492. if(query.value(3).length > 0) {
  493. var spec = JSON.parse(query.value(3));
  494. var wholespectext = "";
  495. var groundspectext = "";
  496. if(spec.color.whole.expected != undefined) {
  497. wholespectext += spec.color.whole.expected;
  498. if(spec.color.whole.tolerance != undefined) {
  499. wholespectext += " +/- " + spec.color.whole.tolerance;
  500. }
  501. wholespec.text = wholespectext;
  502. } else {
  503. wholespec.text = "";
  504. }
  505. if(spec.color.ground.expected != undefined) {
  506. groundspectext += spec.color.ground.expected;
  507. if(spec.color.ground.tolerance != undefined) {
  508. groundspectext += " +/- " + spec.color.ground.tolerance;
  509. }
  510. groundspec.text = groundspectext;
  511. } else {
  512. groundspec.text = "";
  513. }
  514. } else {
  515. wholespec.text = "";
  516. groundspec.text = "";
  517. }
  518. } else {
  519. lossspec.text = "";
  520. specnotes.plainText = "";
  521. wholespec.text = "";
  522. groundspec.text = "";
  523. }
  524. query = query.invalidate();
  525. drawTag();
  526. });
  527. var validateCapacity = function() {
  528. if(checkCapacity == "true") {
  529. if(convertToPounds(parseFloat(green.text), unitBox.currentText) > poundsCapacity) {
  530. return false;
  531. }
  532. }
  533. return true;
  534. }
  535. var duration = findChildObject(this, 'duration');
  536. var timefield = findChildObject(this, 'time');
  537. var markTaken = function(id) {
  538. var query = new QSqlQuery();
  539. query.prepare("UPDATE scheduled_roasts SET machine = :mid WHERE id = :id");
  540. query.bind(":mid", selectedRoasterID);
  541. query.bind(":id", scheduledID);
  542. query.exec();
  543. query = query.invalidate();
  544. }
  545. profilebutton.clicked.connect(function() {
  546. var proceed = false;
  547. if(validateCapacity()) {
  548. proceed = true;
  549. } else {
  550. proceed = displayWarning(TTR("batchWindow", "Suspicious Input"),
  551. TTR("batchWindow", "Entered green coffee weight exceeds maximum batch size. Continue?"));
  552. }
  553. if((proceed == true) && (timefield.text.length != 0)) {
  554. proceed = displayWarning(TTR("batchWindow", "Batch Already Roasted"),
  555. TTR("batchWindow", "Roasting data already exists for this batch. Roasting another batch from this window will overwrite existing data. Are you sure you want to do this?"));
  556. }
  557. if(proceed) {
  558. doLoadProfile();
  559. }
  560. });
  561. var doLoadProfile = function() {
  562. if(scheduledID > 0) {
  563. markTaken();
  564. }
  565. batch.windowModified = true;
  566. currentBatchInfo = batch;
  567. query = new QSqlQuery();
  568. var q = "SELECT files FROM item_files WHERE item = :item AND time = (SELECT max(time) FROM item_files WHERE item = :again)";
  569. query.prepare(q);
  570. query.bind(":item", Number(roasted.currentData()));
  571. query.bind(":again", Number(roasted.currentData()));
  572. query.exec();
  573. var graph;
  574. var log;
  575. if(query.next())
  576. {
  577. var files = query.value(0);
  578. files = files.replace("{", "(");
  579. files = files.replace("}", ")");
  580. q = "SELECT file, name FROM files WHERE id IN ";
  581. q = q + files;
  582. q = q + " AND type = 'profile'";
  583. query.exec(q);
  584. if(query.next())
  585. {
  586. var targetseries = -1;
  587. var buffer = new QBuffer(query.value(0));
  588. var pname = query.value(1);
  589. var input = new XMLInput(buffer, 1);
  590. graph = findChildObject(navigationwindow.loggingWindow, 'graph');
  591. log = findChildObject(navigationwindow.loggingWindow, 'log');
  592. log.clear();
  593. graph.clear();
  594. input.newTemperatureColumn.connect(log.setHeaderData);
  595. input.newTemperatureColumn.connect(function(col, text) {
  596. if(text == navigationwindow.loggingWindow.targetcolumnname)
  597. {
  598. targetseries = col;
  599. }
  600. });
  601. input.newAnnotationColumn.connect(log.setHeaderData);
  602. input.measure.connect(graph.newMeasurement);
  603. input.measure.connect(log.newMeasurement);
  604. input.measure.connect(function(data, series) {
  605. if(series == targetseries)
  606. {
  607. targetDetector.newMeasurement(data);
  608. }
  609. });
  610. var lc;
  611. input.lastColumn.connect(function(c) {
  612. lc = c;
  613. QSettings.setValue("liveColumn", c + 1);
  614. navigationwindow.loggingWindow.postLoadColumnSetup(c)
  615. });
  616. input.annotation.connect(function(note, tcol, ncol) {
  617. for(var i = tcol; i < ncol; i++) {
  618. log.newAnnotation(note, i, ncol);
  619. }
  620. });
  621. }
  622. }
  623. query = query.invalidate();
  624. navigationwindow.loggingWindow.windowTitle = "Typica - [*]" + pname;
  625. navigationwindow.loggingWindow.raise();
  626. navigationwindow.loggingWindow.activateWindow();
  627. graph.updatesEnabled = false;
  628. log.updatesEnabled = false;
  629. input.input();
  630. log.updatesEnabled = true;
  631. graph.updatesEnabled = true;
  632. log.newAnnotation(TTR("batchWindow", "End"), 1, lc);
  633. }
  634. var noprofilebutton = findChildObject(this, 'noprofile');
  635. noprofilebutton.clicked.connect(function() {
  636. var proceed = false;
  637. if(validateCapacity()) {
  638. proceed = true;
  639. } else {
  640. proceed = displayWarning(TTR("batchWindow", "Suspicious Input"),
  641. TTR("batchWindow", "Entered green coffee weight exceeds maximum batch size. Continue?"));
  642. }
  643. if((proceed == true) && (timefield.text.length != 0)) {
  644. proceed = displayWarning(TTR("batchWindow", "Batch Already Roasted"),
  645. TTR("batchWindow", "Roasting data already exists for this batch. Roasting another batch from this window will overwrite existing data. Are you sure you want to do this?"));
  646. }
  647. if(proceed) {
  648. doNoProfile();
  649. }
  650. });
  651. var doNoProfile = function() {
  652. batch.windowModified = true;
  653. currentBatchInfo = batch;
  654. navigationwindow.loggingWindow.raise();
  655. navigationwindow.loggingWindow.activateWindow();
  656. if(scheduledID > 0) {
  657. markTaken();
  658. }
  659. }
  660. var submitbutton = findChildObject(this, 'submit');
  661. var notes = findChildObject(this, 'annotation');
  662. var approval = findChildObject(this, 'approval');
  663. approval.checked = true;
  664. var target = findChildObject(this, 'target');
  665. var greenCheck = function() {
  666. var itemArray = table.columnArray(0, 32).split("\\s*,\\s*");
  667. var weightArray = table.columnArray(1, 0).split("\\s*,\\s*");
  668. return (itemArray.length == weightArray.length) && (itemArray.length > 0);
  669. }
  670. var checkSubmitEnable = function () {
  671. if(roasted.currentIndex > 0) {
  672. if(timefield.text.length > 0) {
  673. if(duration.text.length > 0) {
  674. if(batch.tempData.length > 0) {
  675. if(green.text.length > 0) {
  676. if(greenCheck()) {
  677. return true;
  678. }
  679. }
  680. }
  681. }
  682. }
  683. }
  684. return false;
  685. }
  686. submitbutton.clicked.connect(function() {
  687. var proceed = false;
  688. if(validateCapacity()) {
  689. proceed = true;
  690. } else {
  691. proceed = displayWarning(TTR("batchWindow", "Suspicious Input"),
  692. TTR("batchWindow", "Entered green coffee weight exceeds maximum batch size. Continue?"));
  693. }
  694. if(proceed) {
  695. if(checkSubmitEnable()) {
  696. doSubmit();
  697. } else {
  698. displayError(TTR("batchWindow", "Incomplete Input"),
  699. TTR("batchWindow", "Some required information is not available. Please check inputs and try again."));
  700. }
  701. }
  702. });
  703. var filenofield = findChildObject(this, 'filenofield');
  704. this.endBatch = function() {
  705. var q = "INSERT INTO files (id, name, type, note, file) VALUES(default, :name, 'profile', NULL, :data) RETURNING id";
  706. var query = new QSqlQuery();
  707. query.prepare(q);
  708. query.bind(":name", timefield.text + " " + roasted.currentText);
  709. query.bindFileData(":data", batch.tempData);
  710. query.exec();
  711. query.next();
  712. filenofield.text = query.value(0);
  713. var file = new QFile(batch.tempData);
  714. file.remove();
  715. drawTag();
  716. }
  717. var wholecolor = findChildObject(this, 'wholecolor');
  718. var groundcolor = findChildObject(this, 'groundcolor');
  719. var doSubmit = function() {
  720. checkQuery = new QSqlQuery();
  721. checkQuery.exec("SELECT 1 FROM machine WHERE id = " + selectedRoasterID);
  722. if(!checkQuery.next())
  723. {
  724. checkQuery.prepare("INSERT INTO machine (id, name) VALUES(:id, :name)");
  725. checkQuery.bind(":id", selectedRoasterID);
  726. checkQuery.bind(":name", selectedRoasterName);
  727. checkQuery.exec();
  728. }
  729. checkQuery = checkQuery.invalidate();
  730. query = new QSqlQuery();
  731. var fileno = Number(filenofield.text);
  732. var q2 = "INSERT INTO roasting_log (time, unroasted_id, unroasted_quantity, unroasted_total_quantity, roasted_id, roasted_quantity, transaction_type, annotation, machine, duration, approval, humidity, barometric, indoor_air, outdoor_air, files, person, additional_data) VALUES(:time, ";
  733. q2 = q2 + table.columnArray(0, 32);
  734. q2 = q2 + ", ";
  735. for(var i = 0; table.data(i, 1, 0).value != ""; i++)
  736. {
  737. table.setData(i, 1, convertToPounds(parseFloat(table.data(i, 1, 0)), unitBox.currentText) ,32)
  738. }
  739. q2 = q2 + table.columnArray(1, 32);
  740. q2 = q2 + ", ";
  741. q2 = q2 + convertToPounds(parseFloat(green.text), unitBox.currentText);
  742. q2 = q2 + ", ";
  743. q2 = q2 + roasted.currentData();
  744. q2 = q2 + ", ";
  745. q2 = q2 + convertToPounds(parseFloat(roastwt.text), unitBox.currentText);
  746. q2 = q2 + ", 'ROAST', :annotation, ";
  747. q2 = q2 + selectedRoasterID;
  748. q2 = q2 + ", :duration, :approval, NULL, NULL, NULL, NULL, '{";
  749. q2 = q2 + fileno;
  750. q2 = q2 + "}', :user, :extradata) RETURNING time";
  751. query2 = new QSqlQuery();
  752. query2.prepare(q2);
  753. query2.bind(":time", timefield.text);
  754. query2.bind(":annotation", notes.plainText);
  755. query2.bind(":duration", duration.text);
  756. query2.bind(":approval", approval.checked);
  757. query2.bind(":user", Application.currentTypicaUser());
  758. var extradata = new Object;
  759. var colordata = new Object;
  760. if(wholecolor.value.length > 0) {
  761. colordata.whole = wholecolor.value;
  762. }
  763. if(groundcolor.value.length > 0) {
  764. colordata.ground = groundcolor.value;
  765. }
  766. if(colordata.whole || colordata.ground) {
  767. extradata.color = colordata;
  768. }
  769. query2.bind(":extradata", JSON.stringify(extradata));
  770. query2.exec();
  771. if(!query2.next()) {
  772. displayError(TTR("batchWindow", "Database Insert Failed"), TTR("batchWindow", "Failed to save batch to database. Please check inputs and connection and try again."));
  773. query2 = query2.invalidate();
  774. query = query.invalidate();
  775. return;
  776. }
  777. query2 = query2.invalidate();
  778. if(target.checked) {
  779. var q3 = "INSERT INTO item_files (time, item, files) VALUES(:time, :item, '{";
  780. q3 = q3 + fileno;
  781. q3 = q3 + "}')";
  782. query.prepare(q3);
  783. query.bind(":time", timefield.text);
  784. query.bind(":item", roasted.currentData());
  785. query.exec();
  786. }
  787. if(scheduledID > 0) {
  788. query.prepare("UPDATE scheduled_roasts SET machine = :mid, time = :time WHERE id = :id");
  789. query.bind(":mid", selectedRoasterID);
  790. query.bind(":time", timefield.text);
  791. query.bind(":id", scheduledID);
  792. query.exec();
  793. }
  794. query = query.invalidate();
  795. batch.windowModified = false;
  796. batch.close();
  797. }
  798. function drawTag() {
  799. var buffer = new QBuffer;
  800. buffer.open(3);
  801. var output = new XmlWriter(buffer);
  802. output.writeStartDocument("1.0");
  803. output.writeDTD('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg.dtd">');
  804. output.writeStartElement("html");
  805. output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
  806. output.writeStartElement("head");
  807. var styleFile = new QFile(QSettings.value("config") + "/Scripts/batchtag.css");
  808. styleFile.open(1);
  809. output.writeTextElement("style", styleFile.readToString());
  810. styleFile.close();
  811. styleFile = new QFile(QSettings.value("config") + "/Scripts/barcode.css");
  812. styleFile.open(1);
  813. output.writeTextElement("style", styleFile.readToString());
  814. styleFile.close();
  815. output.writeStartElement("script");
  816. scriptFile = new QFile(QSettings.value("config") + "/Scripts/qrcode.js");
  817. scriptFile.open(1);
  818. output.writeCDATA(scriptFile.readToString());
  819. scriptFile.close();
  820. output.writeEndElement();
  821. output.writeStartElement("script");
  822. scriptFile = new QFile(QSettings.value("config") + "/Scripts/barcode.js");
  823. scriptFile.open(1);
  824. output.writeCDATA(scriptFile.readToString());
  825. scriptFile.close();
  826. output.writeEndElement();
  827. output.writeEndElement();
  828. output.writeStartElement("body");
  829. output.writeStartElement("h1");
  830. output.writeCharacters(roasted.currentText);
  831. output.writeEndElement();
  832. output.writeTextElement("span", "Roasted at: " + timefield.text);
  833. output.writeTextElement("span", "On machine: " + machine.text);
  834. output.writeTextElement("span", "Batch file: " + filenofield.text);
  835. output.writeStartElement("div");
  836. output.writeAttribute("id", "barcode");
  837. output.writeAttribute("class", "barcode128h");
  838. output.writeAttribute("align", "center");
  839. output.writeEndElement();
  840. output.writeStartElement("script");
  841. var c128data = 'var strBarcodeHTML = code128("';
  842. c128data += filenofield.text;
  843. c128data += '", "C");'
  844. c128data += 'document.getElementById("barcode").innerHTML = strBarcodeHTML;';
  845. output.writeCDATA(c128data);
  846. output.writeEndElement();
  847. output.writeStartElement("div");
  848. output.writeAttribute("id", "container");
  849. output.writeEndElement();
  850. output.writeStartElement("script");
  851. var tag = {g: "Typica", m: Number(selectedRoasterID), v: 1};
  852. if(timefield.text.length > 0) {
  853. tag.t = timefield.text;
  854. }
  855. if(filenofield.text.length > 0) {
  856. tag.f = Number(filenofield.text);
  857. }
  858. var scriptData = 'var width = document.getElementById("container").offsetWidth;';
  859. scriptData += 'var qrcode = new QRCode({content: \'';
  860. scriptData += JSON.stringify(tag);
  861. scriptData += '\', width: width, height: width});';
  862. scriptData += 'var svg = qrcode.svg();';
  863. scriptData += 'document.getElementById("container").innerHTML = svg;';
  864. output.writeCDATA(scriptData);
  865. output.writeEndElement();
  866. output.writeEndElement();
  867. output.writeEndElement();
  868. output.writeEndDocument();
  869. batchTag.setContent(buffer);
  870. buffer.close();
  871. };
  872. drawTag();
  873. var printMenu = findChildObject(this, 'print');
  874. printMenu.triggered.connect(function() {
  875. batchTag.print();
  876. });
  877. var printers = findChildObject(this, 'printerlist');
  878. printers.currentIndex = printers.findText(QSettings.value("script/batchtagprinter"));
  879. printers['currentIndexChanged(int)'].connect(function() {
  880. QSettings.setValue("script/batchtagprinter", printers.currentText);
  881. });
  882. var printbutton = findChildObject(this, 'printbutton');
  883. printbutton.clicked.connect(function() {
  884. batchTag.print(printers.currentText);
  885. });
  886. var loadBatch = function(id) {
  887. var query = new QSqlQuery();
  888. query.prepare("SELECT data FROM scheduled_roasts WHERE id = :id");
  889. query.bind(":id", id);
  890. query.exec();
  891. if(query.next()) {
  892. scheduledID = id;
  893. tabs.setCurrentIndex(1);
  894. var d = JSON.parse(query.value(0));
  895. if(d.roasted) {
  896. roasted.currentIndex = roasted.findData(d.roasted);
  897. }
  898. if(d.greens) {
  899. query.prepare("SELECT name FROM items WHERE id = :id");
  900. for(var i = 0; i < d.greens.length; i++) {
  901. query.bind(":id", d.greens[i].id);
  902. query.exec();
  903. if(query.next()) {
  904. table.setData(i, 0, query.value(0), 0);
  905. table.setData(i, 0, d.greens[i].id, 32);
  906. switch(unitBox.currentIndex) {
  907. case 0:
  908. table.setData(i, 1, convertFromPounds(d.greens[i].weight, "g"), 0);
  909. break;
  910. case 1:
  911. table.setData(i, 1, convertFromPounds(d.greens[i].weight, "Kg"), 0);
  912. break;
  913. case 2:
  914. table.setData(i, 1, convertFromPounds(d.greens[i].weight, "oz"), 0);
  915. break;
  916. default:
  917. table.setData(i, 1, d.greens[i].weight, 0);
  918. break;
  919. }
  920. }
  921. }
  922. }
  923. } else {
  924. displayInfo(TTR("batchWindow", "Search Failed"),
  925. TTR("batchWindow", "Scheduled batch ID not found."));
  926. }
  927. query = query.invalidate();
  928. drawTag();
  929. };
  930. batches.selectEntry.connect(function(id) {
  931. loadBatch(id);
  932. });
  933. var search = findChildObject(this, 'search');
  934. search.returnPressed.connect(function() {
  935. if(search.text.length == 0) {
  936. return;
  937. }
  938. if(Number(search.text)) {
  939. loadBatch(Number(search.text));
  940. }
  941. search.text = "";
  942. });
  943. ]]>
  944. </program>
  945. </window>