Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

newbatch.xml 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. <window id="batchWindow">
  2. <menu name="Batch">
  3. <item id="new" shortcut="Ctrl+N">New Batch...</item>
  4. </menu>
  5. <layout type="horizontal">
  6. <layout type="vertical">
  7. <layout type="horizontal">
  8. <label>Machine:</label>
  9. <line id="machine" writable="false" />
  10. <label>Unit:</label>
  11. <sqldrop id="unit" />
  12. <stretch />
  13. </layout>
  14. <layout type="horizontal">
  15. <label>Roasted Coffee:</label>
  16. <sqldrop data="0" display="1" showdata="true" id="roasted">
  17. <null />
  18. <query>SELECT id, name FROM items WHERE category = 'Coffee: Roasted' AND id IN (SELECT item FROM current_items) ORDER BY name</query>
  19. </sqldrop>
  20. <stretch />
  21. </layout>
  22. <label>Green Coffee:</label>
  23. <sqltablearray columns="3" id="greens">
  24. <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>
  25. <column name="Weight" delegate="numeric" />
  26. <column name="Remaining" />
  27. </sqltablearray>
  28. <layout type="horizontal">
  29. <label>Green Weight:</label>
  30. <line id="green" writable="false">0.0</line>
  31. </layout>
  32. <layout type="horizontal">
  33. <button name="Load Profile" type="push" id="load" />
  34. <button name="No Profile" type="push" id="noprofile" />
  35. </layout>
  36. <layout type="horizontal">
  37. <label>Time:</label>
  38. <line id="time" writable="false" />
  39. <label>Duration:</label>
  40. <line id="duration" writable="false" />
  41. </layout>
  42. <layout type="horizontal">
  43. <label>Roasted Weight:</label>
  44. <line id="roast" validator="numeric">0.0</line>
  45. <label>Weight Loss:</label>
  46. <line id="wloss" writable="false" />
  47. <button type="check" id="approval" name="Approved" />
  48. </layout>
  49. <layout type="horizontal">
  50. <label>Annotation:</label>
  51. <textarea id="annotation" />
  52. </layout>
  53. <layout type="horizontal">
  54. <button name="Submit" id="submit" type="push" />
  55. <button name="Save log as target profile" type="check" id="target" />
  56. </layout>
  57. </layout>
  58. <layout type="vertical">
  59. <label>Connected Scales</label>
  60. <layout type="vertical" id="scales" />
  61. <label>Expected Weight Loss</label>
  62. <line id="lossspec" writable="false" />
  63. <label>Expected Roasted Weight</label>
  64. <layout type="horizontal">
  65. <label>Min:</label>
  66. <line id="minroastweight" writable="false" />
  67. </layout>
  68. <layout type="horizontal">
  69. <label>Expected:</label>
  70. <line id="expectedroastweight" writable="false" />
  71. </layout>
  72. <layout type="horizontal">
  73. <label>Max:</label>
  74. <line id="maxroastweight" writable="false" />
  75. </layout>
  76. <label>Specification Details</label>
  77. <textarea id="specnotes" />
  78. <stretch />
  79. </layout>
  80. </layout>
  81. <program>
  82. <![CDATA[
  83. var unitBox = findChildObject(this, 'unit');
  84. unitBox.addItem("g");
  85. unitBox.addItem("Kg");
  86. unitBox.addItem("oz");
  87. unitBox.addItem("lb");
  88. unitBox.currentIndex = (QSettings.value("script/batch_unit", unitBox.findText("lb")));
  89. unitBox['currentIndexChanged(int)'].connect(function() {
  90. QSettings.setValue("script/batch_unit", unitBox.currentIndex);
  91. });
  92. var machine = findChildObject(this, "machine");
  93. machine.setText(selectedRoasterName + " (" + selectedRoasterID + ")");
  94. var newMenu = findChildObject(this, 'new');
  95. newMenu.triggered.connect(function() {
  96. var bwindow = createWindow("batchWindow");
  97. bwindow.windowTitle = "Typica - [*]New Batch";
  98. });
  99. var batch = this;
  100. var table = findChildObject(this, 'greens');
  101. var green = findChildObject(this, 'green');
  102. var model = table.model();
  103. var lossField = findChildObject(this, 'wloss');
  104. lossField.maximumWidth = 80;
  105. var roasted = findChildObject(this, 'roasted');
  106. var roastwt = findChildObject(this, 'roast');
  107. roastwt.maximumWidth = 80;
  108. var scalesLayout = findChildObject(this, 'scales');
  109. scalesLayout.spacing = 10;
  110. if(navigationwindow.loggingWindow.scales.length > 0) {
  111. for(var i = 0; i < navigationwindow.loggingWindow.scales.length; i++) {
  112. var scale = navigationwindow.loggingWindow.scales[i];
  113. var label = new DragLabel();
  114. var weighButton = new QPushButton();
  115. weighButton.text = "Weigh";
  116. weighButton.clicked.connect(scale.weigh);
  117. label.updateMeasurement = function(m, u) {
  118. switch(unitBox.currentIndex) {
  119. case 0:
  120. this.text = Units.convertWeight(m, u, Units.Gram).toFixed(1);
  121. break;
  122. case 1:
  123. this.text = Units.convertWeight(m, u, Units.Kilogram).toFixed(4);
  124. break;
  125. case 2:
  126. this.text = Units.convertWeight(m, u, Units.Ounce).toFixed(3);
  127. break;
  128. case 3:
  129. this.text = Units.convertWeight(m, u, Units.Pound).toFixed(4);
  130. break;
  131. }
  132. };
  133. scalesLayout.addWidget(label);
  134. scalesLayout.addWidget(weighButton);
  135. scale.newMeasurement.connect(function(m, u) {
  136. label.updateMeasurement(m, u);
  137. });
  138. scale.weigh();
  139. unitBox['currentIndexChanged(int)'].connect(scale.weigh);
  140. }
  141. }
  142. var remainingStock = new Array();
  143. var query = new QSqlQuery();
  144. query.exec("SELECT id, quantity, (SELECT conversion FROM lb_bag_conversion WHERE item = id) FROM coffees WHERE quantity <> 0");
  145. while(query.next()) {
  146. remainingStock.push({id: query.value(0),
  147. quantity: query.value(1),
  148. conversion: query.value(2)});
  149. }
  150. query = query.invalidate();
  151. var convertToPounds = function(w, u) {
  152. switch(u) {
  153. case "g":
  154. return w * 0.0022;
  155. case "oz":
  156. return w * 0.0625;
  157. case "Kg":
  158. return w * 2.2;
  159. }
  160. return w;
  161. };
  162. var convertFromPounds = function(w, u) {
  163. switch(u) {
  164. case "g":
  165. return w / 0.0022;
  166. case "oz":
  167. return w / 0.0625;
  168. case "Kg":
  169. return w / 2.2;
  170. }
  171. return w;
  172. };
  173. var specnotes = findChildObject(this, 'specnotes');
  174. specnotes.readOnly = true;
  175. var lossspec = findChildObject(this, 'lossspec');
  176. var minfield = findChildObject(this, 'minroastweight');
  177. var midfield = findChildObject(this, 'expectedroastweight');
  178. var maxfield = findChildObject(this, 'maxroastweight');
  179. var minloss = 0;
  180. var maxloss = 0;
  181. var expectloss = 0;
  182. var updateGreenTable = function() {
  183. var deleteRow = -1;
  184. /* The combo box delegate updates user data before display data
  185. and this code is executed before the model update is fully
  186. complete. Rather than rely on this behavior continuing, we
  187. check that the display value has also been updated and defer
  188. row removal until both updates are complete.
  189. */
  190. while((deleteRow = table.findData("delete", 0)) > -1) {
  191. if(table.data(deleteRow, 0, 0) == "Delete") {
  192. table.removeRow(table.findData("delete", 0));
  193. } else {
  194. break;
  195. }
  196. }
  197. green.text = table.columnSum(1, 0);
  198. table.resizeColumnToContents(0);
  199. var gid = 0;
  200. var r = 0;
  201. while(gid >= 0)
  202. {
  203. gid = Number(table.data(r, 0, 32));
  204. if(isNaN(gid))
  205. {
  206. gid = -1;
  207. break;
  208. }
  209. var bagConversion = 1;
  210. for(var i = 0; i < remainingStock.length; i++)
  211. {
  212. if(gid == Number(remainingStock[i].id))
  213. {
  214. var displayValue = Number(remainingStock[i].quantity);
  215. bagConversion = Number(remainingStock[i].conversion);
  216. if(!isNaN(Number(table.data(r, 1, 0))))
  217. {
  218. var change = Number(table.data(r, 1, 0));
  219. switch(unitBox.currentIndex)
  220. {
  221. case 0:
  222. change = convertToPounds(change, "g");
  223. break;
  224. case 1:
  225. change = convertToPounds(change, "Kg");
  226. break;
  227. case 2:
  228. change = convertToPounds(change, "oz");
  229. break;
  230. }
  231. displayValue -= change;
  232. }
  233. var bagCount = (displayValue / bagConversion).toFixed(2);
  234. switch(unitBox.currentIndex)
  235. {
  236. case 0:
  237. displayValue = convertFromPounds(displayValue, "g");
  238. break;
  239. case 1:
  240. displayValue = convertFromPounds(displayValue, "Kg");
  241. break;
  242. case 2:
  243. displayValue = convertFromPounds(displayValue, "oz");
  244. break;
  245. }
  246. displayValue = "" + Number(displayValue).toFixed(3) + " (" + Number(bagCount).toFixed(3) + " bags)";
  247. if(table.data(r, 2, 0) != displayValue)
  248. {
  249. table.setData(r, 2, displayValue, 0);
  250. table.setData(r, 2, displayValue, 2);
  251. table.resizeColumnToContents(2);
  252. }
  253. }
  254. }
  255. r++;
  256. }
  257. if(parseFloat(green.text) > 0)
  258. {
  259. var expectedLossDesc = "";
  260. if(lossspec.text.length > 0) {
  261. if(minloss != expectloss) {
  262. minfield.text = (-(parseFloat(green.text)) * (maxloss - 1)).toFixed(2);
  263. maxfield.text = (-(parseFloat(green.text)) * (minloss - 1)).toFixed(2);
  264. }
  265. midfield.text = (-(parseFloat(green.text)) * (expectloss - 1)).toFixed(2);
  266. }
  267. if(parseFloat(roastwt.text) > 0)
  268. {
  269. lossField.text = (((parseFloat(green.text) - parseFloat(roastwt.text)) / parseFloat(green.text)) * 100).toFixed(2) + "%";
  270. }
  271. else
  272. {
  273. lossField.text = "100%";
  274. }
  275. }
  276. };
  277. model.dataChanged.connect(updateGreenTable);
  278. unitBox['currentIndexChanged(int)'].connect(updateGreenTable);
  279. roastwt.textChanged.connect(function() {
  280. if(parseFloat(green.text) > 0)
  281. {
  282. if(parseFloat(roastwt.text) > 0)
  283. {
  284. lossField.text = (((parseFloat(green.text) - parseFloat(roastwt.text)) / parseFloat(green.text)) * 100).toFixed(2) + "%";
  285. }
  286. else
  287. {
  288. lossField.text = "100%";
  289. }
  290. }
  291. });
  292. var profilebutton = findChildObject(this, 'load');
  293. profilebutton.setEnabled(false);
  294. roasted['currentIndexChanged(int)'].connect(function() {
  295. table.clear();
  296. var query = new QSqlQuery();
  297. var q = "SELECT EXISTS(SELECT 1 FROM item_files WHERE item = ";
  298. q = q + roasted.currentData();
  299. q = q + ")";
  300. query.exec(q);
  301. if(query.next())
  302. {
  303. if(query.value(0) == 'false')
  304. {
  305. profilebutton.setEnabled(false);
  306. }
  307. else
  308. {
  309. profilebutton.setEnabled(true);
  310. }
  311. }
  312. else
  313. {
  314. profilebutton.setEnabled(false);
  315. }
  316. var title = "Typica - [*]New Batch (";
  317. title = title + roasted.currentText;
  318. title = title + ")";
  319. batch.windowTitle = title;
  320. q = "SELECT unroasted_id FROM roasting_log WHERE roasted_id = ";
  321. q = q + roasted.currentData();
  322. q = q + " AND time = (SELECT max(time) FROM roasting_log WHERE roasted_id = ";
  323. q = q + roasted.currentData();
  324. q = q + ")";
  325. query.exec(q);
  326. if(query.next())
  327. {
  328. var unroasted_items = sqlToArray(query.value(0));
  329. var names = [];
  330. q = "SELECT name FROM items WHERE id = :id AND quantity <> 0";
  331. query.prepare(q);
  332. var allInStock = true;
  333. for(var i = 0; i < unroasted_items.length; i++)
  334. {
  335. query.bind("id", unroasted_items[i]);
  336. query.exec();
  337. if(query.next())
  338. {
  339. names[i] = query.value(0);
  340. }
  341. else
  342. {
  343. allInStock = false;
  344. }
  345. }
  346. if(allInStock)
  347. {
  348. for(var i = 0; i < unroasted_items.length; i++)
  349. {
  350. table.setData(i, 0, names[i], 0);
  351. table.setData(i, 0, unroasted_items[i], 32);
  352. }
  353. }
  354. }
  355. query.prepare("SELECT loss, tolerance, notes FROM roasting_specification WHERE item = :id1 AND time = (SELECT max(time) FROM roasting_specification WHERE item = :id2)");
  356. query.bind(":id1", roasted.currentData());
  357. query.bind(":id2", roasted.currentData());
  358. query.exec();
  359. var lossSpecDescription = "";
  360. if(query.next()) {
  361. if(query.value(0).length > 0) {
  362. lossSpecDescription += (Number(query.value(0)) * 100).toFixed(2);
  363. minloss = Number(query.value(0));
  364. maxloss = Number(query.value(0));
  365. expectloss = Number(query.value(0));
  366. }
  367. if(query.value(1).length > 0) {
  368. lossSpecDescription += " +/- " + (Number(query.value(1)) * 100).toFixed(2);
  369. minloss -= Number(query.value(1));
  370. maxloss += Number(query.value(1));
  371. }
  372. if(lossSpecDescription.length > 0) {
  373. lossSpecDescription += "%";
  374. }
  375. lossspec.text = lossSpecDescription;
  376. specnotes.plainText = query.value(2);
  377. } else {
  378. lossspec.text = "";
  379. specnotes.plainText = "";
  380. }
  381. roastestimate.text = "";
  382. query = query.invalidate();
  383. });
  384. var validateCapacity = function() {
  385. if(checkCapacity == "true") {
  386. if(convertToPounds(parseFloat(green.text), unitBox.currentText) > poundsCapacity) {
  387. return false;
  388. }
  389. }
  390. return true;
  391. }
  392. profilebutton.clicked.connect(function() {
  393. var proceed = false;
  394. if(validateCapacity()) {
  395. proceed = true;
  396. } else {
  397. proceed = displayWarning(TTR("batchWindow", "Suspicious Input"),
  398. TTR("batchWindow", "Entered green coffee weight exceeds maximum batch size. Continue?"));
  399. }
  400. if(proceed) {
  401. doLoadProfile();
  402. }
  403. });
  404. var doLoadProfile = function() {
  405. batch.windowModified = true;
  406. currentBatchInfo = batch;
  407. query = new QSqlQuery();
  408. var q = "SELECT files FROM item_files WHERE item = :item AND time = (SELECT max(time) FROM item_files WHERE item = :again)";
  409. query.prepare(q);
  410. query.bind(":item", Number(roasted.currentData()));
  411. query.bind(":again", Number(roasted.currentData()));
  412. query.exec();
  413. var graph;
  414. var log;
  415. if(query.next())
  416. {
  417. var files = query.value(0);
  418. files = files.replace("{", "(");
  419. files = files.replace("}", ")");
  420. q = "SELECT file, name FROM files WHERE id IN ";
  421. q = q + files;
  422. q = q + " AND type = 'profile'";
  423. query.exec(q);
  424. if(query.next())
  425. {
  426. var targetseries = -1;
  427. var buffer = new QBuffer(query.value(0));
  428. var pname = query.value(1);
  429. var input = new XMLInput(buffer, 1);
  430. graph = findChildObject(navigationwindow.loggingWindow, 'graph');
  431. log = findChildObject(navigationwindow.loggingWindow, 'log');
  432. log.clear();
  433. graph.clear();
  434. input.newTemperatureColumn.connect(log.setHeaderData);
  435. input.newTemperatureColumn.connect(function(col, text) {
  436. if(text == navigationwindow.loggingWindow.targetcolumnname)
  437. {
  438. targetseries = col;
  439. }
  440. });
  441. input.newAnnotationColumn.connect(log.setHeaderData);
  442. input.measure.connect(graph.newMeasurement);
  443. input.measure.connect(log.newMeasurement);
  444. input.measure.connect(function(data, series) {
  445. if(series == targetseries)
  446. {
  447. targetDetector.newMeasurement(data);
  448. }
  449. });
  450. var lc;
  451. input.lastColumn.connect(function(c) {
  452. lc = c;
  453. QSettings.setValue("liveColumn", c + 1);
  454. navigationwindow.loggingWindow.postLoadColumnSetup(c)
  455. });
  456. input.annotation.connect(function(note, tcol, ncol) {
  457. for(var i = tcol; i < ncol; i++) {
  458. log.newAnnotation(note, i, ncol);
  459. }
  460. });
  461. }
  462. }
  463. query = query.invalidate();
  464. navigationwindow.loggingWindow.windowTitle = "Typica - [*]" + pname;
  465. navigationwindow.loggingWindow.raise();
  466. navigationwindow.loggingWindow.activateWindow();
  467. graph.updatesEnabled = false;
  468. log.updatesEnabled = false;
  469. input.input();
  470. log.updatesEnabled = true;
  471. graph.updatesEnabled = true;
  472. log.newAnnotation(TTR("batchWindow", "End"), 1, lc);
  473. }
  474. var noprofilebutton = findChildObject(this, 'noprofile');
  475. noprofilebutton.clicked.connect(function() {
  476. var proceed = false;
  477. if(validateCapacity()) {
  478. proceed = true;
  479. } else {
  480. proceed = displayWarning(TTR("batchWindow", "Suspicious Input"),
  481. TTR("batchWindow", "Entered green coffee weight exceeds maximum batch size. Continue?"));
  482. }
  483. if(proceed) {
  484. doNoProfile();
  485. }
  486. });
  487. var doNoProfile = function() {
  488. batch.windowModified = true;
  489. currentBatchInfo = batch;
  490. navigationwindow.loggingWindow.raise();
  491. navigationwindow.loggingWindow.activateWindow();
  492. }
  493. var submitbutton = findChildObject(this, 'submit');
  494. var timefield = findChildObject(this, 'time');
  495. var notes = findChildObject(this, 'annotation');
  496. var duration = findChildObject(this, 'duration');
  497. var approval = findChildObject(this, 'approval');
  498. approval.checked = true;
  499. var target = findChildObject(this, 'target');
  500. var checkSubmitEnable = function () {
  501. if(roasted.currentIndex > 0) {
  502. if(timefield.text.length > 0) {
  503. if(duration.text.length > 0) {
  504. if(batch.tempData.length > 0) {
  505. if(green.text.length > 0) {
  506. return true;
  507. }
  508. }
  509. }
  510. }
  511. }
  512. return false;
  513. }
  514. submitbutton.clicked.connect(function() {
  515. var proceed = false;
  516. if(validateCapacity()) {
  517. proceed = true;
  518. } else {
  519. proceed = displayWarning(TTR("batchWindow", "Suspicious Input"),
  520. TTR("batchWindow", "Entered green coffee weight exceeds maximum batch size. Continue?"));
  521. }
  522. if(proceed) {
  523. if(checkSubmitEnable()) {
  524. doSubmit();
  525. } else {
  526. displayError(TTR("batchWindow", "Incomplete Input"),
  527. TTR("batchWindow", "Some required information is not available. Please check inputs and try again."));
  528. }
  529. }
  530. });
  531. var doSubmit = function() {
  532. checkQuery = new QSqlQuery();
  533. checkQuery.exec("SELECT 1 FROM machine WHERE id = " + selectedRoasterID);
  534. if(!checkQuery.next())
  535. {
  536. checkQuery.prepare("INSERT INTO machine (id, name) VALUES(:id, :name)");
  537. checkQuery.bind(":id", selectedRoasterID);
  538. checkQuery.bind(":name", selectedRoasterName);
  539. checkQuery.exec();
  540. }
  541. checkQuery = checkQuery.invalidate();
  542. var q = "INSERT INTO files (id, name, type, note, file) VALUES(default, :name, 'profile', NULL, :data) RETURNING id";
  543. query = new QSqlQuery();
  544. query.prepare(q);
  545. query.bind(":name", timefield.text + " " + roasted.currentText);
  546. query.bindFileData(":data", batch.tempData);
  547. query.exec();
  548. query.next();
  549. var fileno = query.value(0);
  550. var file = new QFile(batch.tempData);
  551. file.remove();
  552. 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) VALUES(:time, ";
  553. q2 = q2 + table.columnArray(0, 32);
  554. q2 = q2 + ", ";
  555. for(var i = 0; table.data(i, 1, 0).value != ""; i++)
  556. {
  557. table.setData(i, 1, convertToPounds(parseFloat(table.data(i, 1, 0)), unitBox.currentText) ,32)
  558. }
  559. q2 = q2 + table.columnArray(1, 32);
  560. q2 = q2 + ", ";
  561. q2 = q2 + convertToPounds(parseFloat(green.text), unitBox.currentText);
  562. q2 = q2 + ", ";
  563. q2 = q2 + roasted.currentData();
  564. q2 = q2 + ", ";
  565. q2 = q2 + convertToPounds(parseFloat(roastwt.text), unitBox.currentText);
  566. q2 = q2 + ", 'ROAST', :annotation, ";
  567. q2 = q2 + selectedRoasterID;
  568. q2 = q2 + ", :duration, :approval, NULL, NULL, NULL, NULL, '{";
  569. q2 = q2 + fileno;
  570. q2 = q2 + "}')";
  571. query2 = new QSqlQuery();
  572. query2.prepare(q2);
  573. query2.bind(":time", timefield.text);
  574. query2.bind(":annotation", notes.plainText);
  575. query2.bind(":duration", duration.text);
  576. query2.bind(":approval", approval.checked);
  577. query2.exec();
  578. query2 = query2.invalidate();
  579. if(target.checked) {
  580. var q3 = "INSERT INTO item_files (time, item, files) VALUES(:time, :item, '{";
  581. q3 = q3 + fileno;
  582. q3 = q3 + "}')";
  583. query.prepare(q3);
  584. query.bind(":time", timefield.text);
  585. query.bind(":item", roasted.currentData());
  586. query.exec();
  587. }
  588. query = query.invalidate();
  589. batch.windowModified = false;
  590. batch.close();
  591. }
  592. ]]>
  593. </program>
  594. </window>