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

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