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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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(TTR("batchreport", "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(TTR("batchreport", "Kg"));
  46. unitBox.addItem(TTR("batchreport", "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(TTR("batchreport", "Any"));
  54. batchType.addItem(TTR("batchreport", "Production Roasts"));
  55. batchType.addItem(TTR("batchreport", "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(TTR("batchreport", "Any"));
  63. approval.addItem(TTR("batchreport", "Approved"));
  64. approval.addItem(TTR("batchreport", "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. var printMenu = findChildObject(this, 'print');
  76. printMenu.triggered.connect(function() {
  77. view.print();
  78. });
  79. view.scriptLinkClicked.connect(function(url) {
  80. var arg = decodeURI(url.slice(2, url.length));
  81. var key = arg.split("@");
  82. var details = createWindow("batchDetails");
  83. details.loadBatch(key[0], key[1]);
  84. });
  85. var refresh = function() {
  86. var dateRange = dateSelect.currentRange();
  87. var startDate = dateRange[0];
  88. var endDate = dateRange[dateRange.length - 1];
  89. var conversion = 1;
  90. if(unitBox.currentIndex == 0) {
  91. conversion = 2.2;
  92. }
  93. var approvalClause = "";
  94. switch(approval.currentIndex) {
  95. case 1:
  96. approvalClause = " AND approval = true";
  97. break;
  98. case 2:
  99. approvalClause = " AND approval = false";
  100. break;
  101. }
  102. var batchClause = "";
  103. switch(batchType.currentIndex) {
  104. case 1:
  105. batchClause = " AND transaction_type = 'ROAST'";
  106. break;
  107. case 2:
  108. batchClause = " AND transaction_type = 'SAMPLEROAST'";
  109. break;
  110. }
  111. var searchClause = "";
  112. if(search.text.length > 0) {
  113. searchClause = " WHERE (person ~* :p1 OR rname ~* :p2 OR mname ~* :p3 OR greens ~* :p4 OR annotation ~* :p5 OR array_to_string(files, ',') ~* :p6)";
  114. }
  115. 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";
  116. var query = new QSqlQuery();
  117. query.prepare(q);
  118. query.bind(":c1", conversion);
  119. query.bind(":c2", conversion);
  120. query.bind(":sd", startDate);
  121. query.bind(":ed", endDate);
  122. if(searchClause.length > 0) {
  123. var pattern = ".*" + search.text + ".*";
  124. query.bind(":p1", pattern);
  125. query.bind(":p2", pattern);
  126. query.bind(":p3", pattern);
  127. query.bind(":p4", pattern);
  128. query.bind(":p5", pattern);
  129. query.bind(":p6", pattern);
  130. }
  131. query.exec();
  132. var buffer = new QBuffer;
  133. buffer.open(3);
  134. var output = new XmlWriter(buffer);
  135. output.writeStartDocument("1.0");
  136. 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">');
  137. output.writeStartElement("html");
  138. output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
  139. output.writeStartElement("head");
  140. output.writeTextElement("title", TTR("batchreport", "Batch Log"));
  141. output.writeEndElement();
  142. output.writeStartElement("body");
  143. var cdt = new Date(Date.now());
  144. output.writeTextElement("p", cdt.toLocaleDateString(TTR("reports", "en-US")) + " " + cdt.toLocaleTimeString(TTR("reports", "en-US")));
  145. output.writeStartElement("table");
  146. output.writeAttribute("style", "page-break-after: auto; text-align: left");
  147. output.writeAttribute("rules", "groups");
  148. output.writeAttribute("cellpadding", "3px");
  149. output.writeStartElement("thead");
  150. output.writeStartElement("tr");
  151. output.writeAttribute("valign", "bottom");
  152. output.writeTextElement("th", TTR("batchreport", "Time"));
  153. output.writeTextElement("th", TTR("batchreport", "File Numbers"));
  154. output.writeTextElement("th", TTR("batchreport", "Operator"));
  155. output.writeTextElement("th", TTR("batchreport", "Roasted Coffee"));
  156. output.writeEndElement();
  157. output.writeStartElement("tr");
  158. output.writeAttribute("valign", "bottom");
  159. output.writeEmptyElement("td");
  160. output.writeTextElement("th", TTR("batchreport", "Duration"));
  161. output.writeTextElement("th", TTR("batchreport", "Machine"));
  162. output.writeTextElement("th", TTR("batchreport", "Green Coffees"));
  163. output.writeEndElement();
  164. output.writeStartElement("tr");
  165. output.writeAttribute("valign", "bottom");
  166. output.writeEmptyElement("td");
  167. output.writeTextElement("th", TTR("batchreport", "Green Weight"));
  168. output.writeTextElement("th", TTR("batchreport", "Roasted Weight"));
  169. output.writeTextElement("th", TTR("batchreport", "% Weight Loss"));
  170. output.writeEndElement();
  171. output.writeStartElement("tr");
  172. output.writeAttribute("valign", "bottom");
  173. output.writeEmptyElement("td");
  174. output.writeStartElement("th");
  175. output.writeAttribute("colspan", "2");
  176. output.writeCharacters(TTR("batchreport", "Batch Notes"));
  177. output.writeEndElement();
  178. output.writeTextElement("th", TTR("batchreport", "Specification Notes"));
  179. output.writeEndElement();
  180. output.writeEndElement();
  181. output.writeStartElement("tbody");
  182. while(query.next()) {
  183. output.writeStartElement("tr");
  184. output.writeAttribute("valign", "top");
  185. output.writeStartElement("td");
  186. output.writeStartElement("a");
  187. output.writeAttribute("href", "typica://script/b/" + query.value(15));
  188. output.writeCharacters(query.value(0).replace("T", " "));
  189. output.writeEndElement();
  190. output.writeEndElement();
  191. output.writeStartElement("td");
  192. output.writeStartElement("a");
  193. output.writeAttribute("href", "typica://script/b/" + query.value(15));
  194. output.writeCharacters(query.value(1));
  195. output.writeEndElement();
  196. output.writeEndElement();
  197. output.writeTextElement("td", query.value(2));
  198. output.writeStartElement("td");
  199. output.writeStartElement("span");
  200. if(query.value(11) == "false") {
  201. output.writeAttribute("style", "color:#FF0000");
  202. }
  203. output.writeCharacters(query.value(3));
  204. output.writeEndElement();
  205. output.writeEndElement();
  206. output.writeEndElement();
  207. output.writeStartElement("tr");
  208. output.writeAttribute("valign", "top");
  209. output.writeEmptyElement("td");
  210. output.writeTextElement("td", query.value(4));
  211. output.writeTextElement("td", query.value(5));
  212. output.writeTextElement("td", query.value(6));
  213. output.writeEndElement();
  214. output.writeStartElement("tr");
  215. output.writeAttribute("valign", "top");
  216. output.writeEmptyElement("td");
  217. output.writeTextElement("td", query.value(7));
  218. output.writeTextElement("td", query.value(8));
  219. output.writeStartElement("td");
  220. output.writeStartElement("span");
  221. if(query.value(14) == "false" && query.value(12).length > 0) {
  222. output.writeAttribute("style", "color:#FF0000");
  223. } else if (query.value(14) === "true") {
  224. output.writeAttribute("style", "color:#00FF00");
  225. }
  226. output.writeAttribute("title", query.value(12));
  227. output.writeCharacters(query.value(9));
  228. output.writeEndElement();
  229. output.writeEndElement();
  230. output.writeEndElement();
  231. output.writeStartElement("tr");
  232. output.writeAttribute("valign", "top");
  233. output.writeEmptyElement("td");
  234. output.writeStartElement("td");
  235. output.writeAttribute("colspan", "2");
  236. output.writeAttribute("style", "max-width: 400px");
  237. var noteArray = query.value(10).split("\n");
  238. for(var i = 0; i < noteArray.length; i++) {
  239. output.writeStartElement("p");
  240. output.writeAttribute("style", "margin-top: 0; margin-bottom: 0");
  241. output.writeCharacters(noteArray[i]);
  242. output.writeEndElement();
  243. }
  244. output.writeEndElement();
  245. output.writeStartElement("td");
  246. output.writeAttribute("style", "max-width: 400px");
  247. var specArray = query.value(13).split("\n");
  248. for(var i = 0; i < specArray.length; i++) {
  249. output.writeStartElement("p", specArray[i]);
  250. output.writeAttribute("style", "margin-top: 0; margin-bottom: 0");
  251. output.writeCharacters(specArray[i]);
  252. output.writeEndElement();
  253. }
  254. output.writeEndElement();
  255. output.writeEndElement();
  256. }
  257. output.writeEndElement();
  258. output.writeStartElement("tfoot");
  259. output.writeStartElement("tr");
  260. output.writeAttribute("valign", "bottom");
  261. output.writeTextElement("th", TTR("batchreport", "Total Batches"));
  262. output.writeTextElement("th", TTR("batchreport", "Total Duration"));
  263. output.writeTextElement("th", TTR("batchreport", "Total Green Weight"));
  264. output.writeTextElement("th", TTR("batchreport", "Total Roasted Weight"));
  265. output.writeEndElement();
  266. output.writeStartElement("tr");
  267. output.writeAttribute("valign", "top");
  268. 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;
  269. var query = new QSqlQuery();
  270. query.prepare(q);
  271. query.bind(":c1", conversion);
  272. query.bind(":c2", conversion);
  273. query.bind(":sd", startDate);
  274. query.bind(":ed", endDate);
  275. if(searchClause.length > 0) {
  276. var pattern = ".*" + search.text + ".*";
  277. query.bind(":p1", pattern);
  278. query.bind(":p2", pattern);
  279. query.bind(":p3", pattern);
  280. query.bind(":p4", pattern);
  281. query.bind(":p5", pattern);
  282. query.bind(":p6", pattern);
  283. }
  284. query.exec();
  285. query.next();
  286. for(var i = 0; i < 4; i++) {
  287. output.writeTextElement("td", query.value(i));
  288. }
  289. output.writeEndElement();
  290. output.writeEndElement();
  291. query = query.invalidate();
  292. output.writeEndElement();
  293. output.writeEndElement();
  294. output.writeEndDocument();
  295. view.setContent(buffer);
  296. buffer.close();
  297. };
  298. refresh();
  299. var notifier = Application.subscribe("roastinglogchange");
  300. notifier.notify.connect(function() {
  301. refresh();
  302. });
  303. ]]>
  304. </program>
  305. </window>