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.

chart.xml 23KB

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