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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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, additional_data, spec 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, NULL::jsonb AS spec) SELECT time, (loss*100)::numeric(12,2) AS spec_loss, (tolerance*100)::numeric(12,2) AS spec_tolerance, notes, spec 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) rspec, 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.writeTextElement("th", TTR("batchreport", "Degree of Roast"));
  175. output.writeEndElement();
  176. output.writeStartElement("tr");
  177. output.writeAttribute("valign", "bottom");
  178. output.writeEmptyElement("td");
  179. output.writeStartElement("th");
  180. output.writeAttribute("colspan", "2");
  181. output.writeCharacters(TTR("batchreport", "Batch Notes"));
  182. output.writeEndElement();
  183. output.writeTextElement("th", TTR("batchreport", "Specification Notes"));
  184. output.writeEndElement();
  185. output.writeEndElement();
  186. output.writeStartElement("tbody");
  187. while(query.next()) {
  188. output.writeStartElement("tr");
  189. output.writeAttribute("valign", "top");
  190. output.writeStartElement("td");
  191. output.writeStartElement("a");
  192. output.writeAttribute("href", "typica://script/b/" + query.value(15));
  193. output.writeCharacters(query.value(0).replace("T", " "));
  194. output.writeEndElement();
  195. output.writeEndElement();
  196. output.writeStartElement("td");
  197. output.writeStartElement("a");
  198. output.writeAttribute("href", "typica://script/b/" + query.value(15));
  199. output.writeCharacters(query.value(1));
  200. output.writeEndElement();
  201. output.writeEndElement();
  202. output.writeTextElement("td", query.value(2));
  203. output.writeStartElement("td");
  204. output.writeStartElement("span");
  205. if(query.value(11) == "false") {
  206. output.writeAttribute("style", "color:#FF0000");
  207. }
  208. output.writeCharacters(query.value(3));
  209. output.writeEndElement();
  210. output.writeEndElement();
  211. output.writeEndElement();
  212. output.writeStartElement("tr");
  213. output.writeAttribute("valign", "top");
  214. output.writeEmptyElement("td");
  215. output.writeTextElement("td", query.value(4));
  216. output.writeTextElement("td", query.value(5));
  217. output.writeTextElement("td", query.value(6));
  218. output.writeEndElement();
  219. output.writeStartElement("tr");
  220. output.writeAttribute("valign", "top");
  221. output.writeEmptyElement("td");
  222. output.writeTextElement("td", query.value(7));
  223. output.writeTextElement("td", query.value(8));
  224. output.writeStartElement("td");
  225. output.writeStartElement("span");
  226. if(query.value(14) == "false" && query.value(12).length > 0) {
  227. output.writeAttribute("style", "color:#FF0000");
  228. } else if (query.value(14) === "true") {
  229. output.writeAttribute("style", "color:#00FF00");
  230. }
  231. output.writeAttribute("title", query.value(12));
  232. output.writeCharacters(query.value(9));
  233. output.writeEndElement();
  234. output.writeEndElement();
  235. output.writeEndElement();
  236. output.writeStartElement("tr");
  237. output.writeAttribute("valign", "top");
  238. output.writeEmptyElement("td");
  239. if(query.value(17).length > 0) {
  240. extradata = JSON.parse(query.value(17));
  241. if(extradata.color) {
  242. wcolor = "#000000";
  243. gcolor = "#000000";
  244. wtitle = "";
  245. gtitle = "";
  246. if(query.value(18).length > 0) {
  247. specdata = JSON.parse(query.value(18));
  248. if(extradata.color.whole && specdata.color.whole.expected) {
  249. wmin = Number(specdata.color.whole.expected);
  250. wmax = wmin;
  251. wtitle += specdata.color.whole.expected;
  252. if(specdata.color.whole.tolerance) {
  253. wmin -= Number(specdata.color.whole.tolerance);
  254. wmax += Number(specdata.color.whole.tolerance);
  255. wtitle += "±";
  256. wtitle += specdata.color.whole.tolerance;
  257. }
  258. if(Number(extradata.color.whole) < wmin || Number(extradata.color.whole) > wmax) {
  259. wcolor = "#FF0000";
  260. } else {
  261. wcolor = "#00FF00";
  262. }
  263. }
  264. if(extradata.color.ground && specdata.color.ground.expected) {
  265. gmin = Number(specdata.color.ground.expected);
  266. gmax = gmin;
  267. gtitle += specdata.color.ground.expected;
  268. if(specdata.color.ground.tolerance) {
  269. gmin -= Number(specdata.color.ground.tolerance);
  270. gmax += Number(specdata.color.ground.tolerance);
  271. gtitle += "±";
  272. gtitle += specdata.color.ground.tolerance;
  273. }
  274. if(Number(extradata.color.ground) < gmin || Number(extradata.color.ground) > gmax) {
  275. gcolor = "#FF0000";
  276. } else {
  277. gcolor = "#00FF00";
  278. }
  279. }
  280. }
  281. output.writeStartElement("td");
  282. if(extradata.color.whole) {
  283. output.writeStartElement("span");
  284. output.writeAttribute("style", "color:"+wcolor);
  285. output.writeAttribute("title", wtitle);
  286. output.writeCharacters(extradata.color.whole);
  287. output.writeEndElement();
  288. }
  289. output.writeStartElement("span");
  290. if(wcolor === gcolor) {
  291. output.writeAttribute("style", "color:"+wcolor);
  292. }
  293. output.writeCharacters("/");
  294. output.writeEndElement();
  295. if(extradata.color.ground) {
  296. output.writeStartElement("span");
  297. output.writeAttribute("style", "color:"+gcolor);
  298. output.writeAttribute("title", gtitle);
  299. output.writeCharacters(extradata.color.ground);
  300. output.writeEndElement();
  301. }
  302. output.writeEndElement();
  303. }
  304. }
  305. output.writeEndElement();
  306. output.writeStartElement("tr");
  307. output.writeAttribute("valign", "top");
  308. output.writeEmptyElement("td");
  309. output.writeStartElement("td");
  310. output.writeAttribute("colspan", "2");
  311. output.writeAttribute("style", "max-width: 400px");
  312. var noteArray = query.value(10).split("\n");
  313. for(var i = 0; i < noteArray.length; i++) {
  314. output.writeStartElement("p");
  315. output.writeAttribute("style", "margin-top: 0; margin-bottom: 0");
  316. output.writeCharacters(noteArray[i]);
  317. output.writeEndElement();
  318. }
  319. output.writeEndElement();
  320. output.writeStartElement("td");
  321. output.writeAttribute("style", "max-width: 400px");
  322. var specArray = query.value(13).split("\n");
  323. for(var i = 0; i < specArray.length; i++) {
  324. output.writeStartElement("p", specArray[i]);
  325. output.writeAttribute("style", "margin-top: 0; margin-bottom: 0");
  326. output.writeCharacters(specArray[i]);
  327. output.writeEndElement();
  328. }
  329. output.writeEndElement();
  330. output.writeEndElement();
  331. }
  332. output.writeEndElement();
  333. output.writeStartElement("tfoot");
  334. output.writeStartElement("tr");
  335. output.writeAttribute("valign", "bottom");
  336. output.writeTextElement("th", TTR("batchreport", "Total Batches"));
  337. output.writeTextElement("th", TTR("batchreport", "Total Duration"));
  338. output.writeTextElement("th", TTR("batchreport", "Total Green Weight"));
  339. output.writeTextElement("th", TTR("batchreport", "Total Roasted Weight"));
  340. output.writeEndElement();
  341. output.writeStartElement("tr");
  342. output.writeAttribute("valign", "top");
  343. 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;
  344. var query = new QSqlQuery();
  345. query.prepare(q);
  346. query.bind(":c1", conversion);
  347. query.bind(":c2", conversion);
  348. query.bind(":sd", startDate);
  349. query.bind(":ed", endDate);
  350. if(searchClause.length > 0) {
  351. var pattern = ".*" + search.text + ".*";
  352. query.bind(":p1", pattern);
  353. query.bind(":p2", pattern);
  354. query.bind(":p3", pattern);
  355. query.bind(":p4", pattern);
  356. query.bind(":p5", pattern);
  357. query.bind(":p6", pattern);
  358. }
  359. query.exec();
  360. query.next();
  361. for(var i = 0; i < 4; i++) {
  362. output.writeTextElement("td", query.value(i));
  363. }
  364. output.writeEndElement();
  365. output.writeEndElement();
  366. query = query.invalidate();
  367. output.writeEndElement();
  368. output.writeEndElement();
  369. output.writeEndDocument();
  370. view.setContent(buffer);
  371. buffer.close();
  372. };
  373. refresh();
  374. var notifier = Application.subscribe("roastinglogchange");
  375. notifier.notify.connect(function() {
  376. refresh();
  377. });
  378. ]]>
  379. </program>
  380. </window>