Typica is a free program for professional coffee roasters. https://typica.us
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

newbatch.xml 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 = "" + displayValue + " (" + bagCount + " 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("Suspicious Input", "Entered green coffee weight exceeds maximum batch size. Continue?");
  398. }
  399. if(proceed) {
  400. doLoadProfile();
  401. }
  402. });
  403. var doLoadProfile = function() {
  404. batch.windowModified = true;
  405. currentBatchInfo = batch;
  406. query = new QSqlQuery();
  407. var q = "SELECT files FROM item_files WHERE item = :item AND time = (SELECT max(time) FROM item_files WHERE item = :again)";
  408. query.prepare(q);
  409. query.bind(":item", Number(roasted.currentData()));
  410. query.bind(":again", Number(roasted.currentData()));
  411. query.exec();
  412. var graph;
  413. var log;
  414. if(query.next())
  415. {
  416. var files = query.value(0);
  417. files = files.replace("{", "(");
  418. files = files.replace("}", ")");
  419. q = "SELECT file, name FROM files WHERE id IN ";
  420. q = q + files;
  421. q = q + " AND type = 'profile'";
  422. query.exec(q);
  423. if(query.next())
  424. {
  425. var targetseries = -1;
  426. var buffer = new QBuffer(query.value(0));
  427. var pname = query.value(1);
  428. var input = new XMLInput(buffer, 1);
  429. graph = findChildObject(navigationwindow.loggingWindow, 'graph');
  430. log = findChildObject(navigationwindow.loggingWindow, 'log');
  431. log.clear();
  432. graph.clear();
  433. input.newTemperatureColumn.connect(log.setHeaderData);
  434. input.newTemperatureColumn.connect(function(col, text) {
  435. if(text == navigationwindow.loggingWindow.targetcolumnname)
  436. {
  437. targetseries = col;
  438. }
  439. });
  440. input.newAnnotationColumn.connect(log.setHeaderData);
  441. input.measure.connect(graph.newMeasurement);
  442. input.measure.connect(log.newMeasurement);
  443. input.measure.connect(function(data, series) {
  444. if(series == targetseries)
  445. {
  446. targetDetector.newMeasurement(data);
  447. }
  448. });
  449. var lc;
  450. input.lastColumn.connect(function(c) {
  451. lc = c;
  452. QSettings.setValue("liveColumn", c + 1);
  453. navigationwindow.loggingWindow.postLoadColumnSetup(c)
  454. });
  455. input.annotation.connect(function(note, tcol, ncol) {
  456. for(var i = tcol; i < ncol; i++) {
  457. log.newAnnotation(note, i, ncol);
  458. }
  459. });
  460. }
  461. }
  462. query = query.invalidate();
  463. navigationwindow.loggingWindow.windowTitle = "Typica - [*]" + pname;
  464. navigationwindow.loggingWindow.raise();
  465. navigationwindow.loggingWindow.activateWindow();
  466. graph.updatesEnabled = false;
  467. log.updatesEnabled = false;
  468. input.input();
  469. log.updatesEnabled = true;
  470. graph.updatesEnabled = true;
  471. log.newAnnotation("End", 1, lc);
  472. }
  473. var noprofilebutton = findChildObject(this, 'noprofile');
  474. noprofilebutton.clicked.connect(function() {
  475. batch.windowModified = true;
  476. currentBatchInfo = batch;
  477. navigationwindow.loggingWindow.raise();
  478. navigationwindow.loggingWindow.activateWindow();
  479. });
  480. var submitbutton = findChildObject(this, 'submit');
  481. var timefield = findChildObject(this, 'time');
  482. var notes = findChildObject(this, 'annotation');
  483. var duration = findChildObject(this, 'duration');
  484. var approval = findChildObject(this, 'approval');
  485. approval.checked = true;
  486. var target = findChildObject(this, 'target');
  487. submitbutton.clicked.connect(function() {
  488. checkQuery = new QSqlQuery();
  489. checkQuery.exec("SELECT 1 FROM machine WHERE id = " + selectedRoasterID);
  490. if(!checkQuery.next())
  491. {
  492. checkQuery.prepare("INSERT INTO machine (id, name) VALUES(:id, :name)");
  493. checkQuery.bind(":id", selectedRoasterID);
  494. checkQuery.bind(":name", selectedRoasterName);
  495. checkQuery.exec();
  496. }
  497. checkQuery = checkQuery.invalidate();
  498. var q = "INSERT INTO files (id, name, type, note, file) VALUES(default, :name, 'profile', NULL, :data) RETURNING id";
  499. query = new QSqlQuery();
  500. query.prepare(q);
  501. query.bind(":name", timefield.text + " " + roasted.currentText);
  502. query.bindFileData(":data", batch.tempData);
  503. query.exec();
  504. query.next();
  505. var fileno = query.value(0);
  506. var file = new QFile(batch.tempData);
  507. file.remove();
  508. 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, ";
  509. q2 = q2 + table.columnArray(0, 32);
  510. q2 = q2 + ", ";
  511. for(var i = 0; table.data(i, 1, 0).value != ""; i++)
  512. {
  513. table.setData(i, 1, convertToPounds(parseFloat(table.data(i, 1, 0)), unitBox.currentText) ,32)
  514. }
  515. q2 = q2 + table.columnArray(1, 32);
  516. q2 = q2 + ", ";
  517. q2 = q2 + convertToPounds(parseFloat(green.text), unitBox.currentText);
  518. q2 = q2 + ", ";
  519. q2 = q2 + roasted.currentData();
  520. q2 = q2 + ", ";
  521. q2 = q2 + convertToPounds(parseFloat(roastwt.text), unitBox.currentText);
  522. q2 = q2 + ", 'ROAST', :annotation, ";
  523. q2 = q2 + selectedRoasterID;
  524. q2 = q2 + ", :duration, :approval, NULL, NULL, NULL, NULL, '{";
  525. q2 = q2 + fileno;
  526. q2 = q2 + "}')";
  527. query2 = new QSqlQuery();
  528. query2.prepare(q2);
  529. query2.bind(":time", timefield.text);
  530. query2.bind(":annotation", notes.plainText);
  531. query2.bind(":duration", duration.text);
  532. query2.bind(":approval", approval.checked);
  533. query2.exec();
  534. query2 = query2.invalidate();
  535. if(target.checked) {
  536. var q3 = "INSERT INTO item_files (time, item, files) VALUES(:time, :item, '{";
  537. q3 = q3 + fileno;
  538. q3 = q3 + "}')";
  539. query.prepare(q3);
  540. query.bind(":time", timefield.text);
  541. query.bind(":item", roasted.currentData());
  542. query.exec();
  543. }
  544. query = query.invalidate();
  545. batch.windowModified = false;
  546. batch.close();
  547. });
  548. ]]>
  549. </program>
  550. </window>