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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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="2" 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" />
  26. </sqltablearray>
  27. <layout type="horizontal">
  28. <label>Green Weight:</label>
  29. <line id="green" writable="false">0.0</line>
  30. </layout>
  31. <layout type="horizontal">
  32. <button name="Load Profile" type="push" id="load" />
  33. <button name="No Profile" type="push" id="noprofile" />
  34. </layout>
  35. <layout type="horizontal">
  36. <label>Time:</label>
  37. <line id="time" writable="false" />
  38. <label>Duration:</label>
  39. <line id="duration" writable="false" />
  40. </layout>
  41. <layout type="horizontal">
  42. <label>Roasted Weight:</label>
  43. <line id="roast" validator="numeric" />
  44. <label>Weight Loss:</label>
  45. <line id="wloss" writable="false" />
  46. <button type="check" id="approval" name="Approved" />
  47. </layout>
  48. <layout type="horizontal">
  49. <label>Annotation:</label>
  50. <textarea id="annotation" />
  51. </layout>
  52. <layout type="horizontal">
  53. <button name="Submit" id="submit" type="push" />
  54. <button name="Save log as target profile" type="check" id="target" />
  55. </layout>
  56. </layout>
  57. <layout type="vertical">
  58. <label>Connected Scales</label>
  59. <layout type="vertical" id="scales" />
  60. <stretch />
  61. </layout>
  62. </layout>
  63. <program>
  64. <![CDATA[
  65. var unitBox = findChildObject(this, 'unit');
  66. unitBox.addItem("g");
  67. unitBox.addItem("Kg");
  68. unitBox.addItem("oz");
  69. unitBox.addItem("lb");
  70. unitBox.currentIndex = (QSettings.value("script/batch_unit", unitBox.findText("lb")));
  71. var machine = findChildObject(this, "machine");
  72. machine.setText(selectedRoasterName + " (" + selectedRoasterID + ")");
  73. var newMenu = findChildObject(this, 'new');
  74. newMenu.triggered.connect(function() {
  75. var bwindow = createWindow("batchWindow");
  76. bwindow.windowTitle = "Typica - [*]New Batch";
  77. });
  78. var batch = this;
  79. var table = findChildObject(this, 'greens');
  80. var green = findChildObject(this, 'green');
  81. var model = table.model();
  82. var lossField = findChildObject(this, 'wloss');
  83. lossField.maximumWidth = 80;
  84. var roasted = findChildObject(this, 'roasted');
  85. var roastwt = findChildObject(this, 'roast');
  86. roastwt.maximumWidth = 80;
  87. var scalesLayout = findChildObject(this, 'scales');
  88. scalesLayout.spacing = 10;
  89. if(navigationwindow.loggingWindow.scales.length > 0) {
  90. for(var i = 0; i < navigationwindow.loggingWindow.scales.length; i++) {
  91. var scale = navigationwindow.loggingWindow.scales[i];
  92. var label = new DragLabel();
  93. var weighButton = new QPushButton();
  94. weighButton.text = "Weigh";
  95. weighButton.clicked.connect(scale.weigh);
  96. label.updateMeasurement = function(m, u) {
  97. switch(unitBox.currentIndex) {
  98. case 0:
  99. this.text = Units.convertWeight(m, u, Units.Gram).toFixed(1);
  100. break;
  101. case 1:
  102. this.text = Units.convertWeight(m, u, Units.Kilogram).toFixed(4);
  103. break;
  104. case 2:
  105. this.text = Units.convertWeight(m, u, Units.Ounce).toFixed(3);
  106. break;
  107. case 3:
  108. this.text = Units.convertWeight(m, u, Units.Pound).toFixed(4);
  109. break;
  110. }
  111. };
  112. scalesLayout.addWidget(label);
  113. scalesLayout.addWidget(weighButton);
  114. scale.newMeasurement.connect(function(m, u) {
  115. label.updateMeasurement(m, u);
  116. });
  117. scale.weigh();
  118. unitBox['currentIndexChanged(int)'].connect(scale.weigh);
  119. }
  120. }
  121. model.dataChanged.connect(function() {
  122. var deleteRow = -1;
  123. /* The combo box delegate updates user data before display data
  124. and this code is executed before the model update is fully
  125. complete. Rather than rely on this behavior continuing, we
  126. check that the display value has also been updated and defer
  127. row removal until both updates are complete.
  128. */
  129. while((deleteRow = table.findData("delete", 0)) > -1) {
  130. if(table.data(deleteRow, 0, 0) == "Delete") {
  131. table.removeRow(table.findData("delete", 0));
  132. } else {
  133. break;
  134. }
  135. }
  136. green.text = table.columnSum(1, 0);
  137. table.resizeColumnToContents(0);
  138. if(parseFloat(green.text) > 0)
  139. {
  140. if(parseFloat(roastwt.text) > 0)
  141. {
  142. lossField.text = (((parseFloat(green.text) - parseFloat(roastwt.text)) / parseFloat(green.text)) * 100).toFixed(2) + "%";
  143. }
  144. else
  145. {
  146. lossField.text = "100%";
  147. }
  148. }
  149. });
  150. roastwt.textChanged.connect(function() {
  151. if(parseFloat(green.text) > 0)
  152. {
  153. if(parseFloat(roastwt.text) > 0)
  154. {
  155. lossField.text = (((parseFloat(green.text) - parseFloat(roastwt.text)) / parseFloat(green.text)) * 100).toFixed(2) + "%";
  156. }
  157. else
  158. {
  159. lossField.text = "100%";
  160. }
  161. }
  162. });
  163. var convertToPounds = function(w, u) {
  164. switch(u) {
  165. case "g":
  166. return w * 0.0022;
  167. case "oz":
  168. return w * 0.0625;
  169. case "Kg":
  170. return w * 2.2;
  171. }
  172. return w;
  173. };
  174. var profilebutton = findChildObject(this, 'load');
  175. profilebutton.setEnabled(false);
  176. roasted['currentIndexChanged(int)'].connect(function() {
  177. table.clear();
  178. var query = new QSqlQuery();
  179. var q = "SELECT EXISTS(SELECT 1 FROM item_files WHERE item = ";
  180. q = q + roasted.currentData();
  181. q = q + ")";
  182. query.exec(q);
  183. if(query.next())
  184. {
  185. if(query.value(0) == 'false')
  186. {
  187. profilebutton.setEnabled(false);
  188. }
  189. else
  190. {
  191. profilebutton.setEnabled(true);
  192. }
  193. }
  194. else
  195. {
  196. profilebutton.setEnabled(false);
  197. }
  198. var title = "Typica - [*]New Batch (";
  199. title = title + roasted.currentText;
  200. title = title + ")";
  201. batch.windowTitle = title;
  202. q = "SELECT unroasted_id FROM roasting_log WHERE roasted_id = ";
  203. q = q + roasted.currentData();
  204. q = q + " AND time = (SELECT max(time) FROM roasting_log WHERE roasted_id = ";
  205. q = q + roasted.currentData();
  206. q = q + ")";
  207. query.exec(q);
  208. if(query.next())
  209. {
  210. var unroasted_items = sqlToArray(query.value(0));
  211. var names = [];
  212. q = "SELECT name FROM items WHERE id = :id AND quantity <> 0";
  213. query.prepare(q);
  214. var allInStock = true;
  215. for(var i = 0; i < unroasted_items.length; i++)
  216. {
  217. query.bind("id", unroasted_items[i]);
  218. query.exec();
  219. if(query.next())
  220. {
  221. names[i] = query.value(0);
  222. }
  223. else
  224. {
  225. allInStock = false;
  226. }
  227. }
  228. if(allInStock)
  229. {
  230. for(var i = 0; i < unroasted_items.length; i++)
  231. {
  232. table.setData(i, 0, names[i], 0);
  233. table.setData(i, 0, unroasted_items[i], 32);
  234. }
  235. }
  236. }
  237. });
  238. profilebutton.clicked.connect(function() {
  239. batch.windowModified = true;
  240. currentBatchInfo = batch;
  241. query = new QSqlQuery();
  242. var q = "SELECT files FROM item_files WHERE item = :item AND time = (SELECT max(time) FROM item_files WHERE item = :again)";
  243. query.prepare(q);
  244. query.bind(":item", Number(roasted.currentData()));
  245. query.bind(":again", Number(roasted.currentData()));
  246. query.exec();
  247. var graph;
  248. var log;
  249. if(query.next())
  250. {
  251. var files = query.value(0);
  252. files = files.replace("{", "(");
  253. files = files.replace("}", ")");
  254. q = "SELECT file, name FROM files WHERE id IN ";
  255. q = q + files;
  256. q = q + " AND type = 'profile'";
  257. query.exec(q);
  258. if(query.next())
  259. {
  260. var targetseries = -1;
  261. var buffer = new QBuffer(query.value(0));
  262. var pname = query.value(1);
  263. var input = new XMLInput(buffer, 1);
  264. graph = findChildObject(navigationwindow.loggingWindow, 'graph');
  265. log = findChildObject(navigationwindow.loggingWindow, 'log');
  266. log.clear();
  267. graph.clear();
  268. input.newTemperatureColumn.connect(log.setHeaderData);
  269. input.newTemperatureColumn.connect(function(col, text) {
  270. if(text == navigationwindow.loggingWindow.targetcolumnname)
  271. {
  272. targetseries = col;
  273. }
  274. });
  275. input.newAnnotationColumn.connect(log.setHeaderData);
  276. input.measure.connect(graph.newMeasurement);
  277. input.measure.connect(log.newMeasurement);
  278. input.measure.connect(function(data, series) {
  279. if(series == targetseries)
  280. {
  281. targetDetector.newMeasurement(data);
  282. }
  283. });
  284. var lc;
  285. input.lastColumn.connect(function(c) {
  286. lc = c;
  287. QSettings.setValue("liveColumn", c + 1);
  288. navigationwindow.loggingWindow.postLoadColumnSetup(c)
  289. });
  290. input.annotation.connect(function(note, tcol, ncol) {
  291. for(var i = tcol; i < ncol; i++) {
  292. log.newAnnotation(note, i, ncol);
  293. }
  294. });
  295. }
  296. }
  297. query = query.invalidate();
  298. navigationwindow.loggingWindow.windowTitle = "Typica - [*]" + pname;
  299. navigationwindow.loggingWindow.raise();
  300. navigationwindow.loggingWindow.activateWindow();
  301. graph.updatesEnabled = false;
  302. log.updatesEnabled = false;
  303. input.input();
  304. log.updatesEnabled = true;
  305. graph.updatesEnabled = true;
  306. log.newAnnotation("End", 1, lc);
  307. });
  308. var noprofilebutton = findChildObject(this, 'noprofile');
  309. noprofilebutton.clicked.connect(function() {
  310. batch.windowModified = true;
  311. currentBatchInfo = batch;
  312. navigationwindow.loggingWindow.raise();
  313. navigationwindow.loggingWindow.activateWindow();
  314. });
  315. var submitbutton = findChildObject(this, 'submit');
  316. var timefield = findChildObject(this, 'time');
  317. var notes = findChildObject(this, 'annotation');
  318. var duration = findChildObject(this, 'duration');
  319. var approval = findChildObject(this, 'approval');
  320. approval.checked = true;
  321. var target = findChildObject(this, 'target');
  322. submitbutton.clicked.connect(function() {
  323. checkQuery = new QSqlQuery();
  324. checkQuery.exec("SELECT 1 FROM machine WHERE id = " + selectedRoasterID);
  325. if(!checkQuery.next())
  326. {
  327. checkQuery.prepare("INSERT INTO machine (id, name) VALUES(:id, :name)");
  328. checkQuery.bind(":id", selectedRoasterID);
  329. checkQuery.bind(":name", selectedRoasterName);
  330. checkQuery.exec();
  331. }
  332. checkQuery = checkQuery.invalidate();
  333. var q = "INSERT INTO files (id, name, type, note, file) VALUES(default, :name, 'profile', NULL, :data) RETURNING id";
  334. query = new QSqlQuery();
  335. query.prepare(q);
  336. query.bind(":name", timefield.text + " " + roasted.currentText);
  337. query.bindFileData(":data", batch.tempData);
  338. query.exec();
  339. query.next();
  340. var fileno = query.value(0);
  341. var file = new QFile(batch.tempData);
  342. file.remove();
  343. 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, ";
  344. q2 = q2 + table.columnArray(0, 32);
  345. q2 = q2 + ", ";
  346. for(var i = 0; table.data(i, 1, 0).value != ""; i++)
  347. {
  348. table.setData(i, 1, convertToPounds(parseFloat(table.data(i, 1, 0)), unitBox.currentText) ,32)
  349. }
  350. q2 = q2 + table.columnArray(1, 32);
  351. q2 = q2 + ", ";
  352. q2 = q2 + convertToPounds(parseFloat(green.text), unitBox.currentText);
  353. q2 = q2 + ", ";
  354. q2 = q2 + roasted.currentData();
  355. q2 = q2 + ", ";
  356. q2 = q2 + convertToPounds(parseFloat(roastwt.text), unitBox.currentText);
  357. q2 = q2 + ", 'ROAST', :annotation, ";
  358. q2 = q2 + selectedRoasterID;
  359. q2 = q2 + ", :duration, :approval, NULL, NULL, NULL, NULL, '{";
  360. q2 = q2 + fileno;
  361. q2 = q2 + "}')";
  362. query2 = new QSqlQuery();
  363. query2.prepare(q2);
  364. query2.bind(":time", timefield.text);
  365. query2.bind(":annotation", notes.plainText);
  366. query2.bind(":duration", duration.text);
  367. query2.bind(":approval", approval.checked);
  368. query2.exec();
  369. query2 = query2.invalidate();
  370. if(target.checked) {
  371. var q3 = "INSERT INTO item_files (time, item, files) VALUES(:time, :item, '{";
  372. q3 = q3 + fileno;
  373. q3 = q3 + "}')";
  374. query.prepare(q3);
  375. query.bind(":time", timefield.text);
  376. query.bind(":item", roasted.currentData());
  377. query.exec();
  378. }
  379. query = query.invalidate();
  380. batch.windowModified = false;
  381. batch.close();
  382. });
  383. ]]>
  384. </program>
  385. </window>