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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. <window id="pytdprodcomp">
  2. <reporttitle>Production:->Previous Year Production Comparison</reporttitle>
  3. <layout type="vertical">
  4. <layout type="horizontal">
  5. <daterange id="dates" initial="19" /><!-- Current Year to Date -->
  6. <label>Days to Average</label>
  7. <line validator="integer" id="days">7</line>
  8. <label>Weight Unit:</label>
  9. <sqldrop id="unit" />
  10. <stretch />
  11. </layout>
  12. <webview id="report" />
  13. </layout>
  14. <menu name="File">
  15. <item id="print" shortcut="Ctrl+P">Print</item>
  16. </menu>
  17. <program>
  18. <![CDATA[
  19. this.windowTitle = "Typica - Previous Year Production Comparison";
  20. var dateSelect = findChildObject(this, 'dates');
  21. dateSelect.removeIndex(23); // Remove Lifetime range
  22. var view = findChildObject(this, 'report');
  23. var printMenu = findChildObject(this, 'print');
  24. printMenu.triggered.connect(function() {
  25. view.print();
  26. });
  27. var avgField = findChildObject(this, 'days');
  28. var unitBox = findChildObject(this, 'unit');
  29. unitBox.addItem("Kg");
  30. unitBox.addItem("Lb");
  31. unitBox.currentIndex = QSettings.value("script/report_unit", 1);
  32. unitBox['currentIndexChanged(int)'].connect(function() {
  33. QSettings.setValue("script/report_unit", unitBox.currentIndex);
  34. refresh();
  35. });
  36. function refresh() {
  37. var conversion = 1;
  38. if(unitBox.currentIndex == 0) {
  39. conversion = 2.2;
  40. }
  41. var buffer = new QBuffer;
  42. buffer.open(3);
  43. var output = new XmlWriter(buffer);
  44. output.writeStartDocument("1.0");
  45. 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">');
  46. output.writeStartElement("html");
  47. output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
  48. output.writeStartElement("head");
  49. output.writeTextElement("title", "Previous Year Production Comparison");
  50. output.writeEndElement();
  51. output.writeStartElement("body");
  52. output.writeTextElement("h1", "Previous Year Production Comparison");
  53. output.writeTextElement("p", "This report provides an itemized and overall comparison of roasted coffee production for the dates specified with those dates in the previous year. A chart of this data along with percent change and rolling average of the percent change is also produced.");
  54. output.writeStartElement("table");
  55. output.writeAttribute("style", "page-break-after:auto;");
  56. output.writeAttribute("rules", "groups");
  57. output.writeAttribute("cellpadding", "3px");
  58. output.writeStartElement("thead");
  59. output.writeStartElement("tr");
  60. output.writeTextElement("th", "Coffee");
  61. switch(unitBox.currentIndex) {
  62. case 0:
  63. output.writeTextElement("th", "Previous (Kg)");
  64. output.writeTextElement("th", "Current (Kg)");
  65. output.writeTextElement("th", "Change (Kg)");
  66. break;
  67. case 1:
  68. output.writeTextElement("th", "Previous (Lb)");
  69. output.writeTextElement("th", "Current (Lb)");
  70. output.writeTextElement("th", "Change (Lb)");
  71. break;
  72. }
  73. output.writeEndElement();
  74. output.writeEndElement();
  75. output.writeStartElement("tbody");
  76. var query = new QSqlQuery();
  77. query.exec("START TRANSACTION");
  78. var dateRange = dateSelect.currentRange();
  79. var curStartDate = "'"+dateRange[0]+"'";
  80. var curEndDate = "'"+dateRange[dateRange.length - 1]+"'";
  81. query.exec("SELECT "+curStartDate+"::date - interval '1 year', "+curEndDate+"::date - interval '1 year' + interval '1 day', "+curEndDate+"::date + interval '1 day'");
  82. query.next();
  83. curEndDate = "'"+query.value(2)+"'";
  84. var prevStartDate = "'"+query.value(0)+"'";
  85. var prevEndDate = "'"+query.value(1)+"'";
  86. var q = "CREATE TEMPORARY TABLE previous ON COMMIT DROP AS SELECT roasted_id, sum(roasted_quantity) AS p FROM roasting_log WHERE time > "+prevStartDate+" AND time < "+prevEndDate+" AND roasted_id IS NOT NULL GROUP BY roasted_id";
  87. query.exec(q);
  88. q = "CREATE TEMPORARY TABLE current ON COMMIT DROP AS SELECT roasted_id, sum(roasted_quantity) AS c FROM roasting_log WHERE time > "+curStartDate+" AND time < "+curEndDate+" AND roasted_id IS NOT NULL GROUP BY roasted_id";
  89. query.exec(q);
  90. query.exec("INSERT INTO previous SELECT roasted_id, 0 FROM current WHERE roasted_id NOT IN (SELECT roasted_id FROM previous)");
  91. query.exec("INSERT INTO current SELECT roasted_id, 0 FROM previous WHERE roasted_id NOT IN (SELECT roasted_id FROM current)");
  92. query.exec("CREATE TEMPORARY TABLE comp ON COMMIT DROP AS SELECT previous.roasted_id, p, c FROM previous LEFT OUTER JOIN current ON previous.roasted_id = current.roasted_id");
  93. query.exec("SELECT (SELECT name FROM items WHERE id = roasted_id) AS name, p, c, (c-p) FROM comp WHERE p > 0 OR c > 0 ORDER BY name");
  94. while(query.next())
  95. {
  96. output.writeStartElement("tr");
  97. output.writeTextElement("td", query.value(0));
  98. output.writeTextElement("td", (query.value(1) / conversion).toFixed(2));
  99. output.writeTextElement("td", (query.value(2) / conversion).toFixed(2));
  100. output.writeTextElement("td", (query.value(3) / conversion).toFixed(2));
  101. output.writeEndElement();
  102. }
  103. output.writeEndElement();
  104. output.writeStartElement("tfoot");
  105. output.writeTextElement("th", "Totals");
  106. query.exec("SELECT sum(p), sum(c), sum(c-p) FROM comp");
  107. query.next();
  108. output.writeTextElement("td", (query.value(0) / conversion).toFixed(2));
  109. output.writeTextElement("td", (query.value(1) / conversion).toFixed(2));
  110. output.writeTextElement("td", (query.value(2) / conversion).toFixed(2));
  111. output.writeEndElement();
  112. query.exec("ABORT");
  113. output.writeEndElement();
  114. output.writeStartElement("p");
  115. output.writeAttribute("style", "page-break-inside: avoid");
  116. output.writeStartElement("svg");
  117. output.writeAttribute("xmlns", "http://www.w3.org/2000/svg");
  118. output.writeAttribute("width", "7.5in");
  119. output.writeAttribute("height", "6.3in");
  120. output.writeStartElement("g");
  121. output.writeAttribute("transform", "translate(0,470)");
  122. output.writeStartElement("g");
  123. output.writeAttribute("transform", "scale(1,-1)");
  124. output.writeStartElement("line");
  125. output.writeAttribute("x1", "40");
  126. output.writeAttribute("x2", "40");
  127. output.writeAttribute("y1", "0");
  128. output.writeAttribute("y2", "450");
  129. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  130. output.writeEndElement();
  131. output.writeStartElement("line");
  132. output.writeAttribute("x1", "50");
  133. output.writeAttribute("x2", "650");
  134. output.writeAttribute("y1", "-10");
  135. output.writeAttribute("y2", "-10");
  136. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  137. output.writeEndElement();
  138. output.writeStartElement("line");
  139. output.writeAttribute("x1", "660");
  140. output.writeAttribute("x2", "660");
  141. output.writeAttribute("y1", "0");
  142. output.writeAttribute("y2", "450");
  143. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  144. output.writeEndElement();
  145. var q = "SELECT "+curEndDate+"::date - "+curStartDate+"::date";
  146. query.exec(q);
  147. query.next();
  148. var days = query.value(0);
  149. var dates = new Array();
  150. var curpounds = new Array();
  151. var prevpounds = new Array();
  152. var change = new Array();
  153. var avgchange = new Array();
  154. var i;
  155. for(i = 0; i < days; i++)
  156. {
  157. q = "SELECT "+curStartDate+"::date + "+(i+1);
  158. query.exec(q);
  159. query.next();
  160. dates[i] = query.value(0);
  161. q = "SELECT sum(roasted_quantity) FROM roasting_log WHERE time > "+curStartDate+" AND time < '"+dates[i]+"'";
  162. query.exec(q);
  163. if(query.next())
  164. {
  165. curpounds[i] = Number(query.value(0));
  166. }
  167. else
  168. {
  169. curpounds[i] = 0;
  170. }
  171. q = "SELECT sum(roasted_quantity) FROM roasting_log WHERE time > "+curStartDate+"::date - '1 year'::interval AND time < '"+dates[i]+"'::date - '1 year'::interval";
  172. query.exec(q);
  173. if(query.next())
  174. {
  175. prevpounds[i] = Number(query.value(0));
  176. }
  177. else
  178. {
  179. prevpounds[i] = 0;
  180. }
  181. if(curpounds[i] > 0)
  182. {
  183. change[i] = (curpounds[i] - prevpounds[i])/curpounds[i];
  184. }
  185. else
  186. {
  187. if(prevpounds[i] > 0)
  188. {
  189. change[i] = -1;
  190. }
  191. else
  192. {
  193. change[i] = 0;
  194. }
  195. }
  196. if(i > (parseInt(avgField.text)-2))
  197. {
  198. var sum = 0;
  199. var j;
  200. for(j = 0; j < parseInt(avgField.text); j++)
  201. {
  202. sum += change[i-j];
  203. }
  204. avgchange[i] = sum / parseInt(avgField.text);
  205. }
  206. }
  207. // Calculate the domain of the primary y axis.
  208. var maxy1 = 0;
  209. var increment = 100; // Starting increment is 100 Lb.
  210. if(unitBox.currentIndex == 0) {
  211. increment = 110; // Starting increment is 50 Kg if Kg is requested.
  212. }
  213. while(maxy1 < Math.max(curpounds[days - 1], prevpounds[days - 1]))
  214. {
  215. maxy1 += increment;
  216. }
  217. // Calculate the number of grid lines and loosen spacing if there are too many.
  218. var divisions = maxy1 / increment;
  219. while(divisions > 10) {
  220. increment *= 2;
  221. divisions = maxy1 / increment;
  222. }
  223. // Draw y axis grid lines.
  224. var pos = 0;
  225. while(pos <= maxy1)
  226. {
  227. output.writeStartElement("line");
  228. output.writeAttribute("x1", "35");
  229. output.writeAttribute("x2", "45");
  230. output.writeAttribute("y1", (450/maxy1)*pos);
  231. output.writeAttribute("y2", (450/maxy1)*pos);
  232. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  233. output.writeEndElement();
  234. pos += increment;
  235. }
  236. var n = Math.min.apply(Math, change);
  237. var m = Math.max.apply(Math, change);
  238. var miny2 = 0;
  239. var maxy2;
  240. if(n < 0)
  241. {
  242. while(miny2 > n)
  243. {
  244. miny2 -= 0.1;
  245. }
  246. maxy2 = miny2;
  247. }
  248. else
  249. {
  250. while(miny2 < n)
  251. {
  252. miny2 += 0.1;
  253. }
  254. miny2 -= 0.1;
  255. maxy2 = miny2;
  256. }
  257. while(maxy2 < m)
  258. {
  259. maxy2 += 0.1;
  260. }
  261. var range = maxy2 - miny2;
  262. pos = miny2;
  263. while(pos <= maxy2)
  264. {
  265. output.writeStartElement("line");
  266. output.writeAttribute("x1", "655");
  267. output.writeAttribute("x2", "665");
  268. output.writeAttribute("y1", (pos-miny2)*(4.5/(range*100))*10000);
  269. output.writeAttribute("y2", (pos-miny2)*(4.5/(range*100))*10000);
  270. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  271. output.writeEndElement();
  272. pos += 0.1;
  273. }
  274. pos = 0;
  275. while(pos < days)
  276. {
  277. output.writeStartElement("line");
  278. output.writeAttribute("x1", 50+((600/(days-1))*pos));
  279. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  280. output.writeAttribute("y1", "-5");
  281. output.writeAttribute("y2", "-15");
  282. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  283. output.writeEndElement();
  284. pos += 7;
  285. }
  286. output.writeStartElement("line");
  287. output.writeAttribute("x1", 50+((600/(days-1))*(days-1)));
  288. output.writeAttribute("x2", 50+((600/(days-1))*(days-1)));
  289. output.writeAttribute("y1", "-5");
  290. output.writeAttribute("y2", "-15");
  291. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  292. output.writeEndElement();
  293. pos = 1;
  294. while(pos < days)
  295. {
  296. output.writeStartElement("line");
  297. output.writeAttribute("x1", 50+((600/(days-1))*(pos - 1)));
  298. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  299. output.writeAttribute("y1", (prevpounds[pos-1]/maxy1)*450);
  300. output.writeAttribute("y2", (prevpounds[pos]/maxy1)*450);
  301. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  302. output.writeEndElement();
  303. output.writeStartElement("line");
  304. output.writeAttribute("x1", 50+((600/(days-1))*(pos - 1)));
  305. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  306. output.writeAttribute("y1", (curpounds[pos-1]/maxy1)*450);
  307. output.writeAttribute("y2", (curpounds[pos]/maxy1)*450);
  308. output.writeAttribute("style", "stroke:rgb(255,0,0);stroke-width:1;");
  309. output.writeEndElement();
  310. output.writeStartElement("line");
  311. output.writeAttribute("x1", 50+((600/(days-1))*(pos-1)));
  312. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  313. output.writeAttribute("y1", (change[pos-1]-miny2)*(4.5/(range*100))*10000);
  314. output.writeAttribute("y2", (change[pos]-miny2)*(4.5/(range*100))*10000);
  315. output.writeAttribute("style", "stroke:rgb(0,255,0);stroke-width:1;");
  316. output.writeEndElement();
  317. if(pos > (parseInt(avgField.text)-1))
  318. {
  319. output.writeStartElement("line");
  320. output.writeAttribute("x1", 50+((600/(days-1))*(pos-1)));
  321. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  322. output.writeAttribute("y1", (avgchange[pos-1]-miny2)*(4.5/(range*100))*10000);
  323. output.writeAttribute("y2", (avgchange[pos]-miny2)*(4.5/(range*100))*10000);
  324. output.writeAttribute("style", "stroke:rgb(0,0,255);stroke-width:1;");
  325. output.writeEndElement();
  326. }
  327. pos++;
  328. }
  329. output.writeEndElement();
  330. i = 0;
  331. while(i <= maxy1)
  332. {
  333. output.writeStartElement("text");
  334. output.writeAttribute("x", "0");
  335. output.writeAttribute("y", -((450/maxy1)*i)+5);
  336. output.writeAttribute("font-size", "12");
  337. switch(unitBox.currentIndex) {
  338. case 0:
  339. output.writeCharacters((i / 2.2).toFixed(0));
  340. break;
  341. case 1:
  342. default:
  343. output.writeCharacters(i);
  344. break;
  345. }
  346. output.writeEndElement();
  347. i += increment;
  348. }
  349. i = miny2;
  350. var stepSize = 0.1;
  351. while((maxy2 - miny2) / stepSize > 20)
  352. {
  353. stepSize += 0.05;
  354. }
  355. while(i <= maxy2)
  356. {
  357. output.writeStartElement("text");
  358. output.writeAttribute("x", "670");
  359. output.writeAttribute("y", -((i-miny2)*(4.5/(range*100))*10000)+5);
  360. output.writeAttribute("font-size", "12");
  361. output.writeCharacters(Number(i*100).toFixed(0)+"%");
  362. output.writeEndElement();
  363. i += stepSize;
  364. }
  365. i = 0;
  366. while(i <= days-1)
  367. {
  368. output.writeStartElement("text");
  369. output.writeAttribute("x", 45+((600/(days-1))*i));
  370. output.writeAttribute("y", "20");
  371. output.writeAttribute("font-size", "12");
  372. output.writeAttribute("transform", "rotate(90 "+(45+((600/(days-1))*i))+",20)");
  373. q = "SELECT ('"+dates[i]+"'::date - '1 day'::interval)::date";
  374. query.exec(q);
  375. query.next();
  376. var ds = query.value(0);
  377. var parts = ds.split("-");
  378. switch(parts[1])
  379. {
  380. case '01':
  381. ds = "January ";
  382. break;
  383. case '02':
  384. ds = "February ";
  385. break;
  386. case '03':
  387. ds = "March ";
  388. break;
  389. case '04':
  390. ds = "April ";
  391. break;
  392. case '05':
  393. ds = "May ";
  394. break;
  395. case '06':
  396. ds = "June ";
  397. break;
  398. case '07':
  399. ds = "July ";
  400. break;
  401. case '08':
  402. ds = "August ";
  403. break;
  404. case '09':
  405. ds = "September ";
  406. break;
  407. case '10':
  408. ds = "October ";
  409. break;
  410. case '11':
  411. ds = "November ";
  412. break;
  413. case '12':
  414. ds = "December ";
  415. break;
  416. }
  417. ds = ds + Number(parts[2]);
  418. output.writeCharacters(ds);
  419. output.writeEndElement();
  420. if(i == days-1)
  421. {
  422. break;
  423. }
  424. i += 7;
  425. if(i > days-1)
  426. {
  427. i = days - 1;
  428. }
  429. }
  430. query = query.invalidate();
  431. output.writeStartElement("rect");
  432. output.writeAttribute("fill", "rgb(0,0,0)");
  433. output.writeAttribute("x", "45");
  434. output.writeAttribute("y", "110");
  435. output.writeAttribute("width", "24");
  436. output.writeAttribute("height", "12");
  437. output.writeEndElement();
  438. output.writeStartElement("text");
  439. output.writeAttribute("x", "75");
  440. output.writeAttribute("y", "120");
  441. output.writeAttribute("font-size", "12");
  442. switch(unitBox.currentIndex) {
  443. case 0:
  444. output.writeCharacters("Previous Year Kg");
  445. break;
  446. case 1:
  447. output.writeCharacters("Previous Year Lb");
  448. break;
  449. }
  450. output.writeEndElement();
  451. output.writeStartElement("rect");
  452. output.writeAttribute("fill", "rgb(255,0,0)");
  453. output.writeAttribute("x", "195");
  454. output.writeAttribute("y", "110");
  455. output.writeAttribute("width", "24");
  456. output.writeAttribute("height", "12");
  457. output.writeEndElement();
  458. output.writeStartElement("text");
  459. output.writeAttribute("x", "225");
  460. output.writeAttribute("y", "120");
  461. output.writeAttribute("font-size", "12");
  462. switch(unitBox.currentIndex) {
  463. case 0:
  464. output.writeCharacters("Current Year Kg");
  465. break;
  466. case 1:
  467. output.writeCharacters("Current Year Lb");
  468. break;
  469. }
  470. output.writeEndElement();
  471. output.writeStartElement("rect");
  472. output.writeAttribute("fill", "rgb(0,255,0)");
  473. output.writeAttribute("x", "345");
  474. output.writeAttribute("y", "110");
  475. output.writeAttribute("width", "24");
  476. output.writeAttribute("height", "12");
  477. output.writeEndElement();
  478. output.writeStartElement("text");
  479. output.writeAttribute("x", "375");
  480. output.writeAttribute("y", "120");
  481. output.writeAttribute("font-size", "12");
  482. output.writeCharacters("% Change");
  483. output.writeEndElement();
  484. output.writeStartElement("rect");
  485. output.writeAttribute("fill", "rgb(0,0,255)");
  486. output.writeAttribute("x", "495");
  487. output.writeAttribute("y", "110");
  488. output.writeAttribute("width", "24");
  489. output.writeAttribute("height", "12");
  490. output.writeEndElement();
  491. output.writeStartElement("text");
  492. output.writeAttribute("x", "525");
  493. output.writeAttribute("y", "120");
  494. output.writeAttribute("font-size", "12");
  495. output.writeCharacters("Average % Change");
  496. output.writeEndElement();
  497. output.writeEndElement();
  498. output.writeEndElement();
  499. output.writeEndElement();
  500. output.writeEndElement();
  501. output.writeEndElement();
  502. output.writeEndDocument();
  503. view.setContent(buffer);
  504. buffer.close();
  505. }
  506. refresh();
  507. dateSelect.rangeUpdated.connect(function() {
  508. refresh();
  509. });
  510. avgField.editingFinished.connect(function() {
  511. refresh();
  512. });
  513. ]]>
  514. </program>
  515. </window>