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.

historyreport.xml 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <window id="batchreport">
  2. <reporttitle>Production:->Batch Log</reporttitle>
  3. <layout type="vertical">
  4. <layout type="horizontal">
  5. <daterange id="dates" initial="6" /><!-- Last 7 Days -->
  6. <label>Batch Type: </label>
  7. <sqldrop id="batchtype" />
  8. <label>Approval: </label>
  9. <sqldrop id="approval" />
  10. <label>Search: </label>
  11. <line id="search" />
  12. <label>Weight Unit:</label>
  13. <sqldrop id="unit" />
  14. <stretch />
  15. </layout>
  16. <webview id="report" />
  17. </layout>
  18. <menu name="File">
  19. <item id="print" shortcut="Ctrl+P">Print...</item>
  20. </menu>
  21. <program>
  22. <![CDATA[
  23. this.setWindowTitle("Typica - Batch Log");
  24. var dateSelect = findChildObject(this, 'dates');
  25. var dateQuery = new QSqlQuery();
  26. dateQuery.exec("SELECT time::date FROM roasting_log WHERE time = (SELECT min(time) FROM roasting_log) OR time = (SELECT max(time) FROM roasting_log) ORDER BY time ASC");
  27. dateQuery.next();
  28. var lifetimeStartDate = dateQuery.value(0);
  29. var lifetimeEndDate;
  30. if(dateQuery.next()) {
  31. lifetimeEndDate = dateQuery.value(0);
  32. } else {
  33. lifetimeEndDate = lifetimeStartDate;
  34. }
  35. dateSelect.setLifetimeRange(lifetimeStartDate, lifetimeEndDate);
  36. dateQuery = dateQuery.invalidate();
  37. dateSelect.currentIndex = QSettings.value("script/history/dateIndex", 6);
  38. dateSelect.rangeUpdated.connect(function() {
  39. if(dateSelect.currentIndex != 24) {
  40. QSettings.setValue("script/history/dateIndex", dateSelect.currentIndex);
  41. }
  42. refresh();
  43. });
  44. var unitBox = findChildObject(this, 'unit');
  45. unitBox.addItem("Kg");
  46. unitBox.addItem("Lb");
  47. unitBox.currentIndex = QSettings.value("script/history_unit", 1);
  48. unitBox['currentIndexChanged(int)'].connect(function() {
  49. QSettings.setValue("script/history_unit", unitBox.currentIndex);
  50. refresh();
  51. });
  52. var batchType = findChildObject(this, 'batchtype');
  53. batchType.addItem("Any");
  54. batchType.addItem("Production Roasts");
  55. batchType.addItem("Sample Roasts");
  56. batchType.currentIndex = QSettings.value("script/history/batchtypefilter", 1);
  57. batchType['currentIndexChanged(int)'].connect(function() {
  58. QSettings.setValue("script/history/batchtypefilter", batchType.currentIndex);
  59. refresh();
  60. });
  61. var approval = findChildObject(this, 'approval');
  62. approval.addItem("Any");
  63. approval.addItem("Approved");
  64. approval.addItem("Not Approved");
  65. approval.currentIndex = QSettings.value("script/history/approvalfilter", 1);
  66. approval['currentIndexChanged(int)'].connect(function() {
  67. QSettings.setValue("script/history/approvalfilter", approval.currentIndex);
  68. refresh();
  69. });
  70. var search = findChildObject(this, 'search');
  71. search.editingFinished.connect(function() {
  72. refresh();
  73. });
  74. var view = findChildObject(this, 'report');
  75. view.scriptLinkClicked.connect(function(url) {
  76. var arg = decodeURI(url.slice(2, url.length));
  77. var key = arg.split("@");
  78. print("Handling click with key " + key);
  79. var details = createWindow("batchDetails");
  80. var fakeTable = new Object;
  81. fakeTable.holding = new Array(7);
  82. fakeTable.data = function(r, c) {
  83. return this.holding[c];
  84. };
  85. var conversion = 1;
  86. if(unitBox.currentIndex == 0) {
  87. conversion = 2.2;
  88. }
  89. var query = new QSqlQuery();
  90. var q = "SELECT to_char(time, 'Dy Mon DD YYYY HH24:MI:SS'), machine, (SELECT name FROM items WHERE id = roasted_id) AS name, unroasted_total_quantity / " + conversion + " AS green, roasted_quantity / " + conversion + " AS roasted, CASE WHEN unroasted_total_quantity = 0 THEN NULL ELSE ((unroasted_total_quantity - roasted_quantity) / unroasted_total_quantity * 100::numeric)::numeric(12,2) END AS weight_loss, duration, annotation FROM roasting_log WHERE machine = :machine AND time = :time";
  91. query.prepare(q);
  92. query.bind(":machine", key[0]);
  93. query.bind(":time", key[1]);
  94. query.exec();
  95. query.next();
  96. for(var i = 0; i < 8; i++) {
  97. print("Constructing row with " + query.value(i));
  98. fakeTable.holding[i] = query.value(i);
  99. }
  100. query = query.invalidate();
  101. details.loadData(fakeTable, 0);
  102. });
  103. var refresh = function() {
  104. var dateRange = dateSelect.currentRange();
  105. var startDate = dateRange[0];
  106. var endDate = dateRange[dateRange.length - 1];
  107. var conversion = 1;
  108. if(unitBox.currentIndex == 0) {
  109. conversion = 2.2;
  110. }
  111. var approvalClause = "";
  112. switch(approval.currentIndex) {
  113. case 1:
  114. approvalClause = " AND approval = true";
  115. break;
  116. case 2:
  117. approvalClause = " AND approval = false";
  118. break;
  119. }
  120. var batchClause = "";
  121. switch(batchType.currentIndex) {
  122. case 1:
  123. batchClause = " AND transaction_type = 'ROAST'";
  124. break;
  125. case 2:
  126. batchClause = " AND transaction_type = 'SAMPLEROAST'";
  127. break;
  128. }
  129. var searchClause = "";
  130. if(search.text.length > 0) {
  131. searchClause = " WHERE (person ~* :p1 OR rname ~* :p2 OR mname ~* :p3 OR greens ~* :p4 OR annotation ~* :p5 OR array_to_string(files, ',') ~* :p6)";
  132. }
  133. var q = "WITH qq AS (SELECT roasting_log.time, array_to_string(files, ','), person, (SELECT name || ' (' || id || ')' FROM items WHERE id = roasted_id) AS rname, duration, (SELECT name FROM machine WHERE id = machine) AS mname, array_to_string(ARRAY(SELECT name || ' (' || id || ')' FROM items WHERE id IN (SELECT unnest(unroasted_id))), ',') AS greens, (unroasted_total_quantity/:c1)::numeric(12,2), (roasted_quantity/:c2)::numeric(12,2), loss, annotation, approval, spec_loss::numeric(12,2) || '±' || spec_tolerance::numeric(12,2) AS lspec, notes, loss_match, machine || '@' || roasting_log.time AS link, files FROM roasting_log, LATERAL (SELECT CASE WHEN (unroasted_total_quantity = 0) THEN NULL ELSE (((unroasted_total_quantity - roasted_quantity)/unroasted_total_quantity)*100)::numeric(12,2) END AS loss) lc, LATERAL (WITH q AS (SELECT (SELECT min(time) - interval '10 years' FROM roasting_log) AS time, NULL::numeric AS loss, NULL::numeric AS tolerance, NULL::text AS notes) SELECT time, (loss*100)::numeric(12,2) AS spec_loss, (tolerance*100)::numeric(12,2) AS spec_tolerance, 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) UNION (SELECT * FROM q) ORDER BY time DESC LIMIT 1) spec, LATERAL (SELECT loss >= spec_loss - spec_tolerance AND loss <= spec_loss + spec_tolerance AS loss_match) m WHERE roasting_log.time >= :sd AND roasting_log.time < :ed::date + interval '1 day'" + approvalClause + batchClause + ") SELECT * FROM qq" + searchClause + " ORDER BY time DESC";
  134. var query = new QSqlQuery();
  135. query.prepare(q);
  136. query.bind(":c1", conversion);
  137. query.bind(":c2", conversion);
  138. query.bind(":sd", startDate);
  139. query.bind(":ed", endDate);
  140. if(searchClause.length > 0) {
  141. var pattern = ".*" + search.text + ".*";
  142. query.bind(":p1", pattern);
  143. query.bind(":p2", pattern);
  144. query.bind(":p3", pattern);
  145. query.bind(":p4", pattern);
  146. query.bind(":p5", pattern);
  147. query.bind(":p6", pattern);
  148. }
  149. query.exec();
  150. var buffer = new QBuffer;
  151. buffer.open(3);
  152. var output = new XmlWriter(buffer);
  153. output.writeStartDocument("1.0");
  154. 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">');
  155. output.writeStartElement("html");
  156. output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
  157. output.writeStartElement("head");
  158. output.writeTextElement("title", "Batch Log");
  159. output.writeEndElement();
  160. output.writeStartElement("body");
  161. output.writeStartElement("table");
  162. output.writeAttribute("style", "page-break-after: auto; text-align: left");
  163. output.writeAttribute("rules", "groups");
  164. output.writeAttribute("cellpadding", "3px");
  165. output.writeStartElement("thead");
  166. output.writeStartElement("tr");
  167. output.writeAttribute("valign", "bottom");
  168. output.writeTextElement("th", "Time");
  169. output.writeTextElement("th", "File Numbers");
  170. output.writeTextElement("th", "Operator");
  171. output.writeTextElement("th", "Roasted Coffee");
  172. output.writeEndElement();
  173. output.writeStartElement("tr");
  174. output.writeAttribute("valign", "bottom");
  175. output.writeEmptyElement("td");
  176. output.writeTextElement("th", "Duration");
  177. output.writeTextElement("th", "Machine");
  178. output.writeTextElement("th", "Green Coffees");
  179. output.writeEndElement();
  180. output.writeStartElement("tr");
  181. output.writeAttribute("valign", "bottom");
  182. output.writeEmptyElement("td");
  183. output.writeTextElement("th", "Green Weight");
  184. output.writeTextElement("th", "Roasted Weight");
  185. output.writeTextElement("th", "% Weight Loss");
  186. output.writeEndElement();
  187. output.writeStartElement("tr");
  188. output.writeAttribute("valign", "bottom");
  189. output.writeEmptyElement("td");
  190. output.writeStartElement("th");
  191. output.writeAttribute("colspan", "2");
  192. output.writeCharacters("Batch Notes");
  193. output.writeEndElement();
  194. output.writeTextElement("th", "Specification Notes");
  195. output.writeEndElement();
  196. output.writeEndElement();
  197. output.writeStartElement("tbody");
  198. while(query.next()) {
  199. output.writeStartElement("tr");
  200. output.writeAttribute("valign", "top");
  201. output.writeStartElement("td");
  202. output.writeStartElement("a");
  203. output.writeAttribute("href", "typica://script/b/" + query.value(15));
  204. output.writeCharacters(query.value(0).replace("T", " "));
  205. output.writeEndElement();
  206. output.writeEndElement();
  207. output.writeStartElement("td");
  208. output.writeStartElement("a");
  209. output.writeAttribute("href", "typica://script/b/" + query.value(15));
  210. output.writeCharacters(query.value(1));
  211. output.writeEndElement();
  212. output.writeEndElement();
  213. output.writeTextElement("td", query.value(2));
  214. output.writeStartElement("td");
  215. output.writeStartElement("span");
  216. if(query.value(11) == "false") {
  217. output.writeAttribute("style", "color:#FF0000");
  218. }
  219. output.writeCharacters(query.value(3));
  220. output.writeEndElement();
  221. output.writeEndElement();
  222. output.writeEndElement();
  223. output.writeStartElement("tr");
  224. output.writeAttribute("valign", "top");
  225. output.writeEmptyElement("td");
  226. output.writeTextElement("td", query.value(4));
  227. output.writeTextElement("td", query.value(5));
  228. output.writeTextElement("td", query.value(6));
  229. output.writeEndElement();
  230. output.writeStartElement("tr");
  231. output.writeAttribute("valign", "top");
  232. output.writeEmptyElement("td");
  233. output.writeTextElement("td", query.value(7));
  234. output.writeTextElement("td", query.value(8));
  235. output.writeStartElement("td");
  236. output.writeStartElement("span");
  237. if(query.value(14) == "false" && query.value(12).length > 0) {
  238. output.writeAttribute("style", "color:#FF0000");
  239. } else if (query.value(14) === "true") {
  240. output.writeAttribute("style", "color:#00FF00");
  241. }
  242. output.writeAttribute("title", query.value(12));
  243. output.writeCharacters(query.value(9));
  244. output.writeEndElement();
  245. output.writeEndElement();
  246. output.writeEndElement();
  247. output.writeStartElement("tr");
  248. output.writeAttribute("valign", "top");
  249. output.writeEmptyElement("td");
  250. output.writeStartElement("td");
  251. output.writeAttribute("colspan", "2");
  252. output.writeAttribute("style", "max-width: 400px");
  253. var noteArray = query.value(10).split("\n");
  254. for(var i = 0; i < noteArray.length; i++) {
  255. output.writeStartElement("p");
  256. output.writeAttribute("style", "margin-top: 0; margin-bottom: 0");
  257. output.writeCharacters(noteArray[i]);
  258. output.writeEndElement();
  259. }
  260. output.writeEndElement();
  261. output.writeStartElement("td");
  262. output.writeAttribute("style", "max-width: 400px");
  263. var specArray = query.value(13).split("\n");
  264. for(var i = 0; i < specArray.length; i++) {
  265. output.writeStartElement("p", specArray[i]);
  266. output.writeAttribute("style", "margin-top: 0; margin-bottom: 0");
  267. output.writeCharacters(specArray[i]);
  268. output.writeEndElement();
  269. }
  270. output.writeEndElement();
  271. output.writeEndElement();
  272. }
  273. output.writeEndElement();
  274. output.writeStartElement("tfoot");
  275. output.writeStartElement("tr");
  276. output.writeAttribute("valign", "bottom");
  277. output.writeTextElement("th", "Total Batches");
  278. output.writeTextElement("th", "Total Duration");
  279. output.writeTextElement("th", "Total Green Weight");
  280. output.writeTextElement("th", "Total Roasted Weight");
  281. output.writeEndElement();
  282. output.writeStartElement("tr");
  283. output.writeAttribute("valign", "top");
  284. q = "WITH qq AS (SELECT roasting_log.time, array_to_string(files, ','), person, (SELECT name || ' (' || id || ')' FROM items WHERE id = roasted_id) AS rname, duration, (SELECT name FROM machine WHERE id = machine) AS mname, array_to_string(ARRAY(SELECT name || ' (' || id || ')' FROM items WHERE id IN (SELECT unnest(unroasted_id))), ',') AS greens, (unroasted_total_quantity/:c1)::numeric(12,2) AS uq, (roasted_quantity/:c2)::numeric(12,2) AS rq, annotation, approval, files FROM roasting_log WHERE roasting_log.time >= :sd AND roasting_log.time < :ed::date + interval '1 day'" + approvalClause + batchClause + ") SELECT count(1), sum(duration), sum(uq), sum(rq) FROM qq" + searchClause;
  285. var query = new QSqlQuery();
  286. query.prepare(q);
  287. query.bind(":c1", conversion);
  288. query.bind(":c2", conversion);
  289. query.bind(":sd", startDate);
  290. query.bind(":ed", endDate);
  291. if(searchClause.length > 0) {
  292. var pattern = ".*" + search.text + ".*";
  293. query.bind(":p1", pattern);
  294. query.bind(":p2", pattern);
  295. query.bind(":p3", pattern);
  296. query.bind(":p4", pattern);
  297. query.bind(":p5", pattern);
  298. query.bind(":p6", pattern);
  299. }
  300. query.exec();
  301. query.next();
  302. for(var i = 0; i < 4; i++) {
  303. output.writeTextElement("td", query.value(i));
  304. }
  305. output.writeEndElement();
  306. output.writeEndElement();
  307. query = query.invalidate();
  308. output.writeEndElement();
  309. output.writeEndElement();
  310. output.writeEndDocument();
  311. view.setContent(buffer);
  312. buffer.close();
  313. };
  314. refresh();
  315. ]]>
  316. </program>
  317. </window>