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.

dailyproductiondetail.xml 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. <window id="dailyproduction">
  2. <reporttitle>Production:->Daily Production Report (Detailed)</reporttitle>
  3. <layout type="vertical">
  4. <layout type="horizontal">
  5. <label>Date:</label>
  6. <calendar id="reportdate" />
  7. <label>Weight Unit:</label>
  8. <sqldrop id="unit" />
  9. <label>Temperature Unit:</label>
  10. <sqldrop id="tempunit" />
  11. <stretch />
  12. </layout>
  13. <webview id="report" />
  14. </layout>
  15. <menu name="File">
  16. <item id="print" shortcut="Ctrl+P">Print</item>
  17. </menu>
  18. <program>
  19. <![CDATA[
  20. this.windowTitle = TTR("dailyproduction", "Typica - Daily Production Report");
  21. var dateField = findChildObject(this, 'reportdate');
  22. var view = findChildObject(this, 'report');
  23. var printMenu = findChildObject(this, 'print');
  24. printMenu.triggered.connect(function() {
  25. view.print();
  26. });
  27. var unitBox = findChildObject(this, 'unit');
  28. unitBox.addItem("Kg");
  29. unitBox.addItem("Lb");
  30. unitBox.currentIndex = QSettings.value("script/report_unit", 1);
  31. unitBox['currentIndexChanged(int)'].connect(function() {
  32. QSettings.setValue("script/report_unit", unitBox.currentIndex);
  33. refresh();
  34. });
  35. var tempUnit = findChildObject(this, 'tempunit');
  36. tempUnit.addItem("Celsius");
  37. tempUnit.addItem("Fahrenheit");
  38. tempUnit.currentIndex = QSettings.value("script/report_temperature", 1);
  39. tempUnit['currentIndexChanged(int)'].connect(function() {
  40. QSettings.setValue("script/report_temperature", tempUnit.currentIndex);
  41. refresh();
  42. });
  43. function refresh() {
  44. var buffer = new QBuffer;
  45. buffer.open(3);
  46. var output = new XmlWriter(buffer);
  47. output.writeStartDocument("1.0");
  48. 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">');
  49. output.writeStartElement("html");
  50. output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
  51. output.writeStartElement("head");
  52. output.writeTextElement("title", TTR("dailyproduction", "Daily Production Report"));
  53. output.writeEndElement();
  54. output.writeStartElement("body");
  55. var cdt = new Date(Date.now());
  56. output.writeTextElement("p", cdt.toLocaleDateString(TTR("reports", "en-US")) + " " + cdt.toLocaleTimeString(TTR("reports", "en-US")));
  57. var dateString = "" + dateField.year() + "-" + dateField.month() + "-" + dateField.day();
  58. output.writeTextElement("h1", TTR("dailyproduction", "Daily Production Report: ") + dateString);
  59. output.writeTextElement("h2", TTR("dailyproduction", "Batches Roasted"));
  60. var query = new QSqlQuery();
  61. var q = "SELECT time, machine, (SELECT name FROM machine WHERE id = machine), (SELECT name FROM items WHERE id = roasted_id), unroasted_id, unroasted_quantity, unroasted_total_quantity, roasted_id, roasted_quantity, annotation, duration, files, (SELECT loss FROM roasting_specification WHERE item = roasted_id AND time = (SELECT max(time) FROM roasting_specification WHERE time <= roasting_log.time AND item = roasted_id)), (SELECT tolerance FROM roasting_specification WHERE item = roasted_id AND time = (SELECT max(time) FROM roasting_specification WHERE time <= roasting_log.time AND item = roasted_id)), (SELECT notes FROM roasting_specification WHERE item = roasted_id AND time = (SELECT max(time) FROM roasting_specification WHERE time <= roasting_log.time AND item = roasted_id)), approval FROM roasting_log WHERE time > '" + dateString + "' AND time < ('" + dateString + "'::date + integer '1') ORDER BY time";
  62. query.exec(q);
  63. var times = new Array();
  64. var machines = new Array();
  65. output.writeStartElement("table");
  66. output.writeAttribute("rules", "groups");
  67. output.writeAttribute("cellpadding", "3px");
  68. output.writeStartElement("thead");
  69. output.writeStartElement("tr");
  70. output.writeAttribute("valign", "bottom");
  71. output.writeTextElement("th", TTR("dailyproduction", "Time"));
  72. output.writeTextElement("th", TTR("dailyproduction", "Machine"));
  73. output.writeTextElement("th", TTR("dailyproduction", "Batch ID"));
  74. switch(unitBox.currentIndex)
  75. {
  76. case 0:
  77. output.writeTextElement("th", TTR("dailyproduction", "Green Weights (Kg)"));
  78. break;
  79. case 1:
  80. output.writeTextElement("th", TTR("dailyproduction", "Green Weights (Lb)"));
  81. break;
  82. }
  83. output.writeTextElement("th", "Green Coffees");
  84. switch(unitBox.currentIndex)
  85. {
  86. case 0:
  87. output.writeTextElement("th", TTR("dailyproduction", "Roasted Weight (Kg)"));
  88. break;
  89. case 1:
  90. output.writeTextElement("th", TTR("dailyproduction", "Roasted Weight (Lb)"));
  91. break;
  92. }
  93. output.writeTextElement("th", TTR("dailyproduction", "Roasted Coffee"));
  94. output.writeTextElement("th", TTR("dailyproduction", "% Weight Loss"));
  95. output.writeTextElement("th", TTR("dailyproduction", "Duration"));
  96. output.writeEndElement();
  97. output.writeEndElement();
  98. output.writeStartElement("tbody");
  99. output.writeAttribute("valign", "top");
  100. while(query.next())
  101. {
  102. times.push(query.value(0));
  103. machines.push(query.value(1));
  104. output.writeStartElement("tr");
  105. output.writeTextElement("td", String(query.value(0)).split("T")[1]);
  106. output.writeTextElement("td", query.value(2));
  107. output.writeStartElement("td");
  108. output.writeStartElement("a");
  109. output.writeAttribute("href", "typica://script/p" + query.value(11).slice(1,-1));
  110. output.writeCDATA(query.value(11));
  111. output.writeEndElement();
  112. output.writeEndElement();
  113. var unroastedWeightsList = sqlToArray(query.value(5));
  114. output.writeStartElement("td");
  115. for(var i = 0; i < unroastedWeightsList.length; i++)
  116. {
  117. if(i != 0)
  118. {
  119. output.writeEmptyElement("br");
  120. }
  121. switch(unitBox.currentIndex)
  122. {
  123. case 0:
  124. output.writeCDATA((unroastedWeightsList[i]/2.2).toFixed(unroastedWeightsList[i].split('.').length > 1 ? unroastedWeightsList[i].split('.')[1].length : 0));
  125. break;
  126. case 1:
  127. output.writeCDATA(unroastedWeightsList[i]);
  128. break;
  129. }
  130. }
  131. if(unroastedWeightsList.length > 1)
  132. {
  133. output.writeCDATA("Total: ");
  134. switch(unitBox.currentIndex)
  135. {
  136. case 0:
  137. output.writeCDATA((query.value(6)/2.2).toFixed(query.value(6).split('.').length > 1 ? query.value(6).split('.')[1].length : 0));
  138. break;
  139. case 1:
  140. output.writeCDATA(query.value(6));
  141. break;
  142. }
  143. }
  144. output.writeEndElement();
  145. var unroastedList = sqlToArray(query.value(4));
  146. output.writeStartElement("td");
  147. var greensQuery = new QSqlQuery();
  148. greensQuery.prepare("SELECT name, reference FROM coffees WHERE id = :id");
  149. for(var i = 0; i < unroastedList.length; i++)
  150. {
  151. greensQuery.bind(":id", Number(unroastedList[i]));
  152. greensQuery.exec();
  153. if(i != 0)
  154. {
  155. output.writeEmptyElement("br");
  156. }
  157. greensQuery.next();
  158. output.writeCDATA(greensQuery.value(0) + " (" + unroastedList[i] + ")" + (greensQuery.value(1) == "" ? "" : " ref: " + greensQuery.value(1)));
  159. }
  160. greensQuery = greensQuery.invalidate();
  161. output.writeEndElement();
  162. switch(unitBox.currentIndex)
  163. {
  164. case 0:
  165. output.writeCDATA((query.value(8)/2.2).toFixed(query.value(8).split('.').length > 1 ? query.value(8).split('.')[1].length : 0));
  166. break;
  167. case 1:
  168. output.writeCDATA(query.value(8));
  169. break;
  170. }
  171. output.writeStartElement("td");
  172. output.writeStartElement("span");
  173. if(query.value(15) == "false") {
  174. output.writeAttribute("style", "color:#FF0000");
  175. }
  176. output.writeCharacters(query.value(3) + " (" + query.value(7) + ")");
  177. output.writeEndElement();
  178. output.writeEndElement();
  179. if(Number(query.value(6)) > 0) {
  180. var loss = (Number(query.value(6)) - Number(query.value(8)))/Number(query.value(6));
  181. var lossMin;
  182. var lossMax;
  183. var lossColor;
  184. var useLoss;
  185. if(query.value(12) > 0) {
  186. useLoss = true;
  187. lossMin = Number(query.value(12)) - Number(query.value(13));
  188. lossMax = Number(query.value(12)) + Number(query.value(13));
  189. if(loss >= lossMin && loss <= lossMax) {
  190. lossColor = '#00FF00';
  191. } else {
  192. lossColor = '#FF0000';
  193. }
  194. } else {
  195. lossColor = '#000000';
  196. useLoss = false;
  197. }
  198. loss *= 100;
  199. var lossSpec = Number(query.value(12)) * 100;
  200. var lossTol = Number(query.value(13)) * 100;
  201. output.writeStartElement("td");
  202. output.writeStartElement("span");
  203. output.writeAttribute("style", "color:"+lossColor);
  204. if(useLoss) {
  205. output.writeAttribute("title", lossSpec.toFixed(2) + "+/-" + lossTol.toFixed(2) + "%");
  206. }
  207. output.writeCharacters(loss.toFixed(2) + "%");
  208. output.writeEndElement();
  209. output.writeEndElement();
  210. } else {
  211. output.writeTextElement("td", "Undefined");
  212. }
  213. output.writeTextElement("td", query.value(10));
  214. output.writeEndElement();
  215. if(query.value(9) != "")
  216. {
  217. output.writeStartElement("tr");
  218. output.writeEmptyElement("td");
  219. output.writeStartElement("td");
  220. output.writeAttribute("colspan", "8");
  221. var noteArray = query.value(9).split("\n");
  222. for(var i = 0; i < noteArray.length; i++) {
  223. output.writeStartElement("p");
  224. output.writeAttribute("style", "margin-top: 0; margin-bottom: 0");
  225. output.writeCharacters(noteArray[i]);
  226. output.writeEndElement();
  227. }
  228. output.writeEndElement();
  229. output.writeEndElement();
  230. }
  231. var files = sqlToArray(query.value(11));
  232. var annotations = annotationFromRecord(files[0]);
  233. output.writeStartElement("tr");
  234. output.writeStartElement("td");
  235. output.writeAttribute("colspan", "5");
  236. output.writeTextElement("strong", TTR("dailyproduction", "Profile Summary"));
  237. var buffer2 = new QBuffer("<points>"+annotations+"</points>");
  238. buffer2.open(1);
  239. var colQuery = new XQuery;
  240. colQuery.bind("profile", buffer2);
  241. colQuery.setQuery('for $i in doc($profile)//tuple[1]/temperature/@series return (string($i), ";")');
  242. var result = colQuery.exec();
  243. buffer2.close();
  244. var seriesHeaders = new Array();
  245. seriesHeaders.push(TTR("dailyproduction", "Time"));
  246. var records = result.split(";");
  247. for(var i = 0; i < records.length - 1; i++) {
  248. seriesHeaders.push(records[i].replace(/^\s+|\s+$/g,""));
  249. }
  250. seriesHeaders.push(TTR("dailyproduction", "Note"));
  251. output.writeStartElement("table");
  252. output.writeStartElement("thead");
  253. output.writeStartElement("tr");
  254. for(var i = 0; i < seriesHeaders.length; i++) {
  255. if(i > 0 && i < seriesHeaders.length - 1) {
  256. switch(tempUnit.currentIndex)
  257. {
  258. case 0:
  259. output.writeTextElement("th", seriesHeaders[i] + " (°C)");
  260. break;
  261. case 1:
  262. output.writeTextElement("th", seriesHeaders[i] + " (°F)");
  263. break;
  264. }
  265. } else {
  266. output.writeTextElement("th", seriesHeaders[i]);
  267. }
  268. }
  269. output.writeEndElement();
  270. output.writeEndElement();
  271. buffer2.open(1);
  272. var rq = 'for $t in doc($profile) //tuple return (string($t/time), ";", ';
  273. for(var i = 0; i < seriesHeaders.length - 2; i++) {
  274. rq = rq + 'string($t/temperature[' + Number(i+1) + ']), ";", ';
  275. }
  276. rq = rq + 'string($t/annotation), "~")';
  277. colQuery.setQuery(rq);
  278. var annotationData = colQuery.exec();
  279. buffer2.close();
  280. output.writeStartElement("tbody");
  281. var annotationRecords = annotationData.split("~");
  282. for(var i = 0; i < annotationRecords.length - 1; i++) {
  283. output.writeStartElement("tr");
  284. var annotationRow = annotationRecords[i].split(";");
  285. for(var j = 0; j < annotationRow.length; j++) {
  286. output.writeStartElement("td");
  287. output.writeAttribute("style", "border-left: 1px solid #000000");
  288. if(j > 0) {
  289. output.writeAttribute("align", "center");
  290. }
  291. if(j > 0 && j < annotationRow.length - 1) {
  292. switch(tempUnit.currentIndex)
  293. {
  294. case 0:
  295. output.writeCharacters(((Number(annotationRow[j].replace(/^\s+|\s+$/g,""))-32)*5/9).toFixed(2));
  296. break;
  297. case 1:
  298. output.writeCharacters(Number(annotationRow[j].replace(/^\s+|\s+$/g,"")).toFixed(2));
  299. break;
  300. }
  301. }
  302. else {
  303. output.writeCharacters(annotationRow[j].replace(/^\s+|\s+$/g,""));
  304. }
  305. output.writeEndElement();
  306. }
  307. output.writeEndElement();
  308. }
  309. output.writeEndElement();
  310. output.writeEndElement();
  311. output.writeEndElement();
  312. output.writeStartElement("td");
  313. output.writeAttribute("colspan", "4");
  314. if(query.value(14)) {
  315. output.writeTextElement("strong", "Roast Specification Notes");
  316. var specArray = query.value(14).split("\n");
  317. for(var i = 0; i < specArray.length; i++) {
  318. output.writeStartElement("p");
  319. output.writeAttribute("style", "margin-top: 0; margin-bottom: 0");
  320. output.writeCharacters(specArray[i]);
  321. output.writeEndElement();
  322. }
  323. }
  324. output.writeEndElement();
  325. output.writeEndElement();
  326. }
  327. output.writeEndElement();
  328. output.writeStartElement("tfoot");
  329. output.writeStartElement("tr");
  330. output.writeEmptyElement("td");
  331. output.writeEmptyElement("td");
  332. output.writeStartElement("td");
  333. output.writeTextElement("strong", TTR("dailyproduction", "Totals:"));
  334. output.writeEndElement();
  335. q = "SELECT sum(unroasted_total_quantity), sum(roasted_quantity), sum(duration) FROM roasting_log WHERE time > '" + dateString + "' AND time < ('" + dateString + "'::date + integer '1')";
  336. query.exec(q);
  337. query.next();
  338. switch(unitBox.currentIndex)
  339. {
  340. case 0:
  341. output.writeTextElement("td", (query.value(0)/2.2).toFixed(query.value(0).split('.').length > 1 ? query.value(0).split('.')[1].length : 0));
  342. break;
  343. case 1:
  344. output.writeTextElement("td", query.value(0));
  345. break;
  346. }
  347. output.writeEmptyElement("td");
  348. switch(unitBox.currentIndex)
  349. {
  350. case 0:
  351. output.writeTextElement("td", (query.value(1)/2.2).toFixed(query.value(1).split('.').length > 1 ? query.value(1).split('.')[1].length : 0));
  352. break;
  353. case 1:
  354. output.writeTextElement("td", query.value(1));
  355. break;
  356. }
  357. output.writeEmptyElement("td");
  358. output.writeEmptyElement("td");
  359. output.writeTextElement("td", query.value(2));
  360. output.writeEndElement();
  361. output.writeEndElement(); //tfoot
  362. output.writeEndElement(); //table
  363. output.writeTextElement("h2", TTR("dailyproduction", "Inventory"));
  364. output.writeStartElement("table");
  365. output.writeAttribute("rules", "groups");
  366. output.writeAttribute("cellpadding", "3px");
  367. output.writeStartElement("thead");
  368. output.writeStartElement("tr");
  369. output.writeTextElement("th", TTR("dailyproduction", "Green Coffee"));
  370. output.writeTextElement("th", TTR("dailyproduction", "Starting Inventory"));
  371. output.writeTextElement("th", TTR("dailyproduction", "Ending Inventory"));
  372. output.writeTextElement("th", TTR("dailyproduction", "Change"));
  373. output.writeTextElement("th", TTR("dailyproduction", "Availability"));
  374. output.writeEndElement();
  375. output.writeEndElement();
  376. output.writeStartElement("tbody");
  377. q = "SELECT DISTINCT item, (SELECT name FROM items WHERE id = item) AS name, (SELECT out FROM coffee_history WHERE id = item) FROM all_transactions WHERE time >= '" + dateString + "' AND time < '" + dateString + "'::date + integer '1' ORDER BY name ASC";
  378. query.exec(q)
  379. var subQuery = new QSqlQuery();
  380. var qq;
  381. while(query.next())
  382. {
  383. var validRow = true;
  384. var td1 = query.value(1);
  385. var td2 = 0;
  386. var td3 = 0;
  387. var td4 = 0;
  388. var td5 = query.value(2);
  389. qq = "SELECT balance FROM item_history(" + query.value(0) + ") WHERE time = (SELECT max(time) FROM all_transactions WHERE time <= '" + dateString + "' AND item = " + query.value(0) + ") OR time = (SELECT max(time) FROM all_transactions WHERE time < '" + dateString + "'::date + integer '1' AND item = " + query.value(0) + ") ORDER BY time ASC";
  390. subQuery.exec(qq);
  391. var startValue = "0.0";
  392. var endValue = "0.0";
  393. if(subQuery.next())
  394. {
  395. switch(unitBox.currentIndex)
  396. {
  397. case 0:
  398. td2 = (subQuery.value(0)/2.2).toFixed(subQuery.value(0).split('.').length > 1 ? subQuery.value(0).split('.')[1].length : 0);
  399. break;
  400. case 1:
  401. td2 = subQuery.value(0);
  402. break;
  403. }
  404. startValue = subQuery.value(0);
  405. }
  406. else
  407. {
  408. /* This should never happen. */
  409. validRow = false;
  410. }
  411. if(subQuery.next())
  412. {
  413. switch(unitBox.currentIndex)
  414. {
  415. case 0:
  416. td3 = (subQuery.value(0)/2.2).toFixed(subQuery.value(0).split('.').length > 1 ? subQuery.value(0).split('.')[1].length : 0);
  417. break;
  418. case 1:
  419. td3 = subQuery.value(0);
  420. break;
  421. }
  422. endValue = subQuery.value(0);
  423. }
  424. else
  425. {
  426. /* If only one transaction exists for the current query
  427. this is a purchase transaction. Until something is
  428. done with the coffee, it should not appear in this
  429. section of the report. */
  430. endValue = startValue;
  431. validRow = false;
  432. }
  433. var startPrec = startValue.split('.').length > 1 ? startValue.split('.')[1].length : 0;
  434. var endPrec = endValue.split('.').length > 1 ? endValue.split('.')[1].length : 0;
  435. switch(unitBox.currentIndex)
  436. {
  437. case 0:
  438. td4 = (Number(endValue/2.2) - Number(startValue/2.2)).toFixed(Math.max(startPrec, endPrec));
  439. break;
  440. case 1:
  441. td4 = (Number(endValue) - Number(startValue)).toFixed(Math.max(startPrec, endPrec));
  442. break;
  443. }
  444. if(validRow) {
  445. output.writeStartElement("tr");
  446. output.writeTextElement("td", td1);
  447. output.writeTextElement("td", td2);
  448. output.writeTextElement("td", td3);
  449. output.writeTextElement("td", td4);
  450. output.writeTextElement("td", td5);
  451. output.writeEndElement();
  452. }
  453. }
  454. query = query.invalidate();
  455. subQuery = subQuery.invalidate();
  456. output.writeEndElement();
  457. output.writeStartElement("tfoot");
  458. output.writeEndElement();
  459. output.writeEndElement();//End of inventory table
  460. output.writeEndElement();
  461. output.writeEndElement();
  462. output.writeEndDocument();
  463. view.setContent(buffer);
  464. buffer.close();
  465. }
  466. refresh();
  467. dateField.dateChanged.connect(function() {
  468. refresh();
  469. });
  470. var notifier = Application.subscribe("roastinglogchange");
  471. notifier.notify.connect(function() {
  472. refresh();
  473. });
  474. view.scriptLinkClicked.connect(function(url) {
  475. var arg = url.slice(1, url.length);
  476. var details = createWindow("batchDetails");
  477. var fakeTable = new Object;
  478. fakeTable.holding = new Array(7);
  479. fakeTable.data = function(r, c) {
  480. return this.holding[c];
  481. };
  482. var query = new QSqlQuery();
  483. query.exec("SELECT time, machine, (SELECT name FROM items WHERE id = roasted_id) AS name, unroasted_total_quantity AS green, roasted_quantity AS roasted, ((unroasted_total_quantity - roasted_quantity) / unroasted_total_quantity * 100::numeric)::numeric(12,2) AS weight_loss, duration, annotation FROM roasting_log WHERE files = '{" + arg + "}'");
  484. query.next();
  485. for(var i = 0; i < 8; i++) {
  486. fakeTable.holding[i] = query.value(i);
  487. }
  488. query = query.invalidate();
  489. details.loadData(fakeTable, 0);
  490. });
  491. ]]>
  492. </program>
  493. </window>