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

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