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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. var dateSelect = findChildObject(this, 'dates');
  24. var dateQuery = new QSqlQuery();
  25. 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");
  26. dateQuery.next();
  27. var lifetimeStartDate = dateQuery.value(0);
  28. var lifetimeEndDate;
  29. if(dateQuery.next()) {
  30. lifetimeEndDate = dateQuery.value(0);
  31. } else {
  32. lifetimeEndDate = lifetimeStartDate;
  33. }
  34. dateSelect.setLifetimeRange(lifetimeStartDate, lifetimeEndDate);
  35. dateQuery = dateQuery.invalidate();
  36. dateSelect.currentIndex = QSettings.value("script/history/dateIndex", 6);
  37. dateSelect.rangeUpdated.connect(function() {
  38. if(dateSelect.currentIndex != 24) {
  39. QSettings.setValue("script/history/dateIndex", dateSelect.currentIndex);
  40. }
  41. refresh();
  42. });
  43. var unitBox = findChildObject(this, 'unit');
  44. unitBox.addItem("Kg");
  45. unitBox.addItem("Lb");
  46. unitBox.currentIndex = QSettings.value("script/history_unit", 1);
  47. unitBox['currentIndexChanged(int)'].connect(function() {
  48. QSettings.setValue("script/history_unit", unitBox.currentIndex);
  49. refresh();
  50. });
  51. var batchType = findChildObject(this, 'batchtype');
  52. batchType.addItem("Any");
  53. batchType.addItem("Production Roasts");
  54. batchType.addItem("Sample Roasts");
  55. batchType.currentIndex = QSettings.value("script/history/batchtypefilter", 1);
  56. batchType['currentIndexChanged(int)'].connect(function() {
  57. QSettings.setValue("script/history/batchtypefilter", batchType.currentIndex);
  58. refresh();
  59. });
  60. var approval = findChildObject(this, 'approval');
  61. approval.addItem("Any");
  62. approval.addItem("Approved");
  63. approval.addItem("Not Approved");
  64. approval.currentIndex = QSettings.value("script/history/approvalfilter", 1);
  65. approval['currentIndexChanged(int)'].connect(function() {
  66. QSettings.setValue("script/history/approvalfilter", approval.currentIndex);
  67. refresh();
  68. });
  69. var search = findChildObject(this, 'search');
  70. search.editingFinished.connect(function() {
  71. refresh();
  72. });
  73. var view = findChildObject(this, 'report');
  74. view.scriptLinkClicked.connect(function(url) {
  75. var arg = decodeURI(url.slice(2, url.length));
  76. var key = arg.split("@");
  77. print("Handling click with key " + key);
  78. var details = createWindow("batchDetails");
  79. var fakeTable = new Object;
  80. fakeTable.holding = new Array(7);
  81. fakeTable.data = function(r, c) {
  82. return this.holding[c];
  83. };
  84. var conversion = 1;
  85. if(unitBox.currentIndex == 0) {
  86. conversion = 2.2;
  87. }
  88. var query = new QSqlQuery();
  89. 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";
  90. query.prepare(q);
  91. query.bind(":machine", key[0]);
  92. query.bind(":time", key[1]);
  93. query.exec();
  94. query.next();
  95. for(var i = 0; i < 8; i++) {
  96. print("Constructing row with " + query.value(i));
  97. fakeTable.holding[i] = query.value(i);
  98. }
  99. query = query.invalidate();
  100. details.loadData(fakeTable, 0);
  101. });
  102. var refresh = function() {
  103. var dateRange = dateSelect.currentRange();
  104. var startDate = dateRange[0];
  105. var endDate = dateRange[dateRange.length - 1];
  106. var conversion = 1;
  107. if(unitBox.currentIndex == 0) {
  108. conversion = 2.2;
  109. }
  110. var approvalClause = "";
  111. switch(approval.currentIndex) {
  112. case 1:
  113. approvalClause = " AND approval = true";
  114. break;
  115. case 2:
  116. approvalClause = " AND approval = false";
  117. break;
  118. }
  119. var batchClause = "";
  120. switch(batchType.currentIndex) {
  121. case 1:
  122. batchClause = " AND transaction_type = 'ROAST'";
  123. break;
  124. case 2:
  125. batchClause = " AND transaction_type = 'SAMPLEROAST'";
  126. break;
  127. }
  128. var searchClause = "";
  129. if(search.text.length > 0) {
  130. searchClause = " WHERE (person ~* :p1 OR rname ~* :p2 OR mname ~* :p3 OR greens ~* :p4 OR annotation ~* :p5 OR array_to_string(files, ',') ~* :p6)";
  131. }
  132. 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 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";
  133. var query = new QSqlQuery();
  134. query.prepare(q);
  135. query.bind(":c1", conversion);
  136. query.bind(":c2", conversion);
  137. query.bind(":sd", startDate);
  138. query.bind(":ed", endDate);
  139. if(searchClause.length > 0) {
  140. var pattern = ".*" + search.text + ".*";
  141. query.bind(":p1", pattern);
  142. query.bind(":p2", pattern);
  143. query.bind(":p3", pattern);
  144. query.bind(":p4", pattern);
  145. query.bind(":p5", pattern);
  146. query.bind(":p6", pattern);
  147. }
  148. query.exec();
  149. var buffer = new QBuffer;
  150. buffer.open(3);
  151. var output = new XmlWriter(buffer);
  152. output.writeStartDocument("1.0");
  153. 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">');
  154. output.writeStartElement("html");
  155. output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
  156. output.writeStartElement("head");
  157. output.writeTextElement("title", "Batch Log");
  158. output.writeEndElement();
  159. output.writeStartElement("body");
  160. output.writeStartElement("table");
  161. output.writeAttribute("style", "page-break-after: auto; text-align: left");
  162. output.writeAttribute("rules", "groups");
  163. output.writeAttribute("cellpadding", "3px");
  164. output.writeStartElement("thead");
  165. output.writeStartElement("tr");
  166. output.writeTextElement("th", "Time");
  167. output.writeTextElement("th", "File Numbers");
  168. output.writeTextElement("th", "Operator");
  169. output.writeTextElement("th", "Roasted Coffee");
  170. output.writeEndElement();
  171. output.writeStartElement("tr");
  172. output.writeEmptyElement("td");
  173. output.writeTextElement("th", "Duration");
  174. output.writeTextElement("th", "Machine");
  175. output.writeTextElement("th", "Green Coffees");
  176. output.writeEndElement();
  177. output.writeStartElement("tr");
  178. output.writeEmptyElement("td");
  179. output.writeTextElement("th", "Green Weight");
  180. output.writeTextElement("th", "Roasted Weight");
  181. output.writeTextElement("th", "% Weight Loss");
  182. output.writeEndElement();
  183. output.writeStartElement("tr");
  184. output.writeEmptyElement("td");
  185. output.writeStartElement("th");
  186. output.writeAttribute("colspan", "2");
  187. output.writeCharacters("Batch Notes");
  188. output.writeEndElement();
  189. output.writeTextElement("th", "Specification Notes");
  190. output.writeEndElement();
  191. output.writeEndElement();
  192. output.writeStartElement("tbody");
  193. while(query.next()) {
  194. output.writeStartElement("tr");
  195. output.writeStartElement("td");
  196. output.writeStartElement("a");
  197. output.writeAttribute("href", "typica://script/b/" + query.value(15));
  198. output.writeCharacters(query.value(0).replace("T", " "));
  199. output.writeEndElement();
  200. output.writeEndElement();
  201. output.writeStartElement("td");
  202. output.writeStartElement("a");
  203. output.writeAttribute("href", "typica://script/b/" + query.value(15));
  204. output.writeCharacters(query.value(1));
  205. output.writeEndElement();
  206. output.writeEndElement();
  207. output.writeTextElement("td", query.value(2));
  208. output.writeStartElement("td");
  209. output.writeStartElement("span");
  210. if(query.value(11) == "false") {
  211. output.writeAttribute("style", "color:#FF0000");
  212. }
  213. output.writeCharacters(query.value(3));
  214. output.writeEndElement();
  215. output.writeEndElement();
  216. output.writeEndElement();
  217. output.writeStartElement("tr");
  218. output.writeEmptyElement("td");
  219. output.writeTextElement("td", query.value(4));
  220. output.writeTextElement("td", query.value(5));
  221. output.writeTextElement("td", query.value(6));
  222. output.writeEndElement();
  223. output.writeStartElement("tr");
  224. output.writeEmptyElement("td");
  225. output.writeTextElement("td", query.value(7));
  226. output.writeTextElement("td", query.value(8));
  227. output.writeStartElement("td");
  228. output.writeStartElement("span");
  229. if(query.value(14) == "false" && query.value(12).length > 0) {
  230. output.writeAttribute("style", "color:#FF0000");
  231. } else if (query.value(14) === "true") {
  232. output.writeAttribute("style", "color:#00FF00");
  233. }
  234. output.writeAttribute("title", query.value(12));
  235. output.writeCharacters(query.value(9));
  236. output.writeEndElement();
  237. output.writeEndElement();
  238. output.writeEndElement();
  239. output.writeStartElement("tr");
  240. output.writeEmptyElement("td");
  241. output.writeStartElement("td");
  242. output.writeAttribute("colspan", "2");
  243. output.writeAttribute("style", "max-width: 400px");
  244. var noteArray = query.value(10).split("\n");
  245. for(var i = 0; i < noteArray.length; i++) {
  246. output.writeStartElement("p");
  247. output.writeAttribute("style", "margin-top: 0; margin-bottom: 0");
  248. output.writeCharacters(noteArray[i]);
  249. output.writeEndElement();
  250. }
  251. output.writeEndElement();
  252. output.writeStartElement("td");
  253. output.writeAttribute("style", "max-width: 400px");
  254. var specArray = query.value(13).split("\n");
  255. for(var i = 0; i < specArray.length; i++) {
  256. output.writeStartElement("p", specArray[i]);
  257. output.writeAttribute("style", "margin-top: 0; margin-bottom: 0");
  258. output.writeCharacters(specArray[i]);
  259. output.writeEndElement();
  260. }
  261. output.writeEndElement();
  262. output.writeEndElement();
  263. }
  264. query = query.invalidate();
  265. output.writeEndElement();
  266. output.writeEndElement();
  267. output.writeEndElement();
  268. output.writeEndDocument();
  269. view.setContent(buffer);
  270. buffer.close();
  271. };
  272. refresh();
  273. ]]>
  274. </program>
  275. </window>