Typica is a free program for professional coffee roasters. https://typica.us
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

chart.xml 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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("p");
  117. output.writeAttribute("style", "page-break-inside: avoid");
  118. output.writeStartElement("svg");
  119. output.writeAttribute("xmlns", "http://www.w3.org/2000/svg");
  120. output.writeAttribute("width", "7.5in");
  121. output.writeAttribute("height", "6.3in");
  122. output.writeStartElement("g");
  123. output.writeAttribute("transform", "translate(0,470)");
  124. output.writeStartElement("g");
  125. output.writeAttribute("transform", "scale(1,-1)");
  126. output.writeStartElement("line");
  127. output.writeAttribute("x1", "40");
  128. output.writeAttribute("x2", "40");
  129. output.writeAttribute("y1", "0");
  130. output.writeAttribute("y2", "450");
  131. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  132. output.writeEndElement();
  133. output.writeStartElement("line");
  134. output.writeAttribute("x1", "50");
  135. output.writeAttribute("x2", "650");
  136. output.writeAttribute("y1", "-10");
  137. output.writeAttribute("y2", "-10");
  138. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  139. output.writeEndElement();
  140. output.writeStartElement("line");
  141. output.writeAttribute("x1", "660");
  142. output.writeAttribute("x2", "660");
  143. output.writeAttribute("y1", "0");
  144. output.writeAttribute("y2", "450");
  145. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  146. output.writeEndElement();
  147. var query = new QSqlQuery();
  148. var q = "SELECT "+curEndDate+"::date - "+curStartDate+"::date";
  149. query.exec(q);
  150. query.next();
  151. var days = query.value(0);
  152. var dates = new Array();
  153. var curpounds = new Array();
  154. var prevpounds = new Array();
  155. var change = new Array();
  156. var avgchange = new Array();
  157. var i;
  158. for(i = 0; i < days; i++)
  159. {
  160. q = "SELECT "+curStartDate+"::date + "+(i+1);
  161. query.exec(q);
  162. query.next();
  163. dates[i] = query.value(0);
  164. q = "SELECT sum(roasted_quantity) FROM roasting_log WHERE time > "+curStartDate+" AND time < '"+dates[i]+"'";
  165. query.exec(q);
  166. if(query.next())
  167. {
  168. curpounds[i] = query.value(0);
  169. }
  170. else
  171. {
  172. curpounds[i] = 0;
  173. }
  174. q = "SELECT sum(roasted_quantity) FROM roasting_log WHERE time > "+curStartDate+"::date - '1 year'::interval AND time < '"+dates[i]+"'::date - '1 year'::interval";
  175. query.exec(q);
  176. if(query.next())
  177. {
  178. prevpounds[i] = query.value(0);
  179. }
  180. else
  181. {
  182. prevpounds[i] = 0;
  183. }
  184. if(curpounds[i] > 0)
  185. {
  186. change[i] = (curpounds[i] - prevpounds[i])/curpounds[i];
  187. }
  188. else
  189. {
  190. if(prevpounds[i] > 0)
  191. {
  192. change[i] = -1;
  193. }
  194. else
  195. {
  196. change[i] = 0;
  197. }
  198. }
  199. if(i > (parseInt(avgField.text)-2))
  200. {
  201. var sum = 0;
  202. var j;
  203. for(j = 0; j < parseInt(avgField.text); j++)
  204. {
  205. sum += change[i-j];
  206. }
  207. avgchange[i] = sum / parseInt(avgField.text);
  208. }
  209. }
  210. // Calculate the domain of the primary y axis.
  211. var maxy1 = 0;
  212. var increment = 100; // Starting increment is 100 Lb.
  213. if(unitBox.currentIndex == 0) {
  214. increment = 110; // Starting increment is 50 Kg if Kg is requested.
  215. }
  216. while(maxy1 < Math.max(curpounds[days - 1], prevpounds[days - 1]))
  217. {
  218. maxy1 += increment;
  219. }
  220. // Calculate the number of grid lines and loosen spacing if there are too many.
  221. var divisions = maxy1 / increment;
  222. while(divisions > 10) {
  223. increment *= 2;
  224. divisions = maxy1 / increment;
  225. }
  226. // Draw y axis grid lines.
  227. var pos = 0;
  228. while(pos <= maxy1)
  229. {
  230. output.writeStartElement("line");
  231. output.writeAttribute("x1", "35");
  232. output.writeAttribute("x2", "45");
  233. output.writeAttribute("y1", (450/maxy1)*pos);
  234. output.writeAttribute("y2", (450/maxy1)*pos);
  235. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  236. output.writeEndElement();
  237. pos += increment;
  238. }
  239. var n = Math.min.apply(Math, change);
  240. var m = Math.max.apply(Math, change);
  241. var miny2 = 0;
  242. var maxy2;
  243. if(n < 0)
  244. {
  245. while(miny2 > n)
  246. {
  247. miny2 -= 0.1;
  248. }
  249. maxy2 = miny2;
  250. }
  251. else
  252. {
  253. while(miny2 < n)
  254. {
  255. miny2 += 0.1;
  256. }
  257. miny2 -= 0.1;
  258. maxy2 = miny2;
  259. }
  260. while(maxy2 < m)
  261. {
  262. maxy2 += 0.1;
  263. }
  264. var range = maxy2 - miny2;
  265. pos = miny2;
  266. while(pos <= maxy2)
  267. {
  268. output.writeStartElement("line");
  269. output.writeAttribute("x1", "655");
  270. output.writeAttribute("x2", "665");
  271. output.writeAttribute("y1", (pos-miny2)*(4.5/(range*100))*10000);
  272. output.writeAttribute("y2", (pos-miny2)*(4.5/(range*100))*10000);
  273. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  274. output.writeEndElement();
  275. pos += 0.1;
  276. }
  277. pos = 0;
  278. while(pos < days)
  279. {
  280. output.writeStartElement("line");
  281. output.writeAttribute("x1", 50+((600/(days-1))*pos));
  282. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  283. output.writeAttribute("y1", "-5");
  284. output.writeAttribute("y2", "-15");
  285. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  286. output.writeEndElement();
  287. pos += 7;
  288. }
  289. output.writeStartElement("line");
  290. output.writeAttribute("x1", 50+((600/(days-1))*(days-1)));
  291. output.writeAttribute("x2", 50+((600/(days-1))*(days-1)));
  292. output.writeAttribute("y1", "-5");
  293. output.writeAttribute("y2", "-15");
  294. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  295. output.writeEndElement();
  296. pos = 1;
  297. while(pos < days)
  298. {
  299. output.writeStartElement("line");
  300. output.writeAttribute("x1", 50+((600/(days-1))*(pos - 1)));
  301. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  302. output.writeAttribute("y1", (prevpounds[pos-1]/maxy1)*450);
  303. output.writeAttribute("y2", (prevpounds[pos]/maxy1)*450);
  304. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  305. output.writeEndElement();
  306. output.writeStartElement("line");
  307. output.writeAttribute("x1", 50+((600/(days-1))*(pos - 1)));
  308. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  309. output.writeAttribute("y1", (curpounds[pos-1]/maxy1)*450);
  310. output.writeAttribute("y2", (curpounds[pos]/maxy1)*450);
  311. output.writeAttribute("style", "stroke:rgb(255,0,0);stroke-width:1;");
  312. output.writeEndElement();
  313. output.writeStartElement("line");
  314. output.writeAttribute("x1", 50+((600/(days-1))*(pos-1)));
  315. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  316. output.writeAttribute("y1", (change[pos-1]-miny2)*(4.5/(range*100))*10000);
  317. output.writeAttribute("y2", (change[pos]-miny2)*(4.5/(range*100))*10000);
  318. output.writeAttribute("style", "stroke:rgb(0,255,0);stroke-width:1;");
  319. output.writeEndElement();
  320. if(pos > (parseInt(avgField.text)-1))
  321. {
  322. output.writeStartElement("line");
  323. output.writeAttribute("x1", 50+((600/(days-1))*(pos-1)));
  324. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  325. output.writeAttribute("y1", (avgchange[pos-1]-miny2)*(4.5/(range*100))*10000);
  326. output.writeAttribute("y2", (avgchange[pos]-miny2)*(4.5/(range*100))*10000);
  327. output.writeAttribute("style", "stroke:rgb(0,0,255);stroke-width:1;");
  328. output.writeEndElement();
  329. }
  330. pos++;
  331. }
  332. output.writeEndElement();
  333. i = 0;
  334. while(i <= maxy1)
  335. {
  336. output.writeStartElement("text");
  337. output.writeAttribute("x", "0");
  338. output.writeAttribute("y", -((450/maxy1)*i)+5);
  339. output.writeAttribute("font-size", "12");
  340. switch(unitBox.currentIndex) {
  341. case 0:
  342. output.writeCharacters((i / 2.2).toFixed(0));
  343. break;
  344. case 1:
  345. default:
  346. output.writeCharacters(i);
  347. break;
  348. }
  349. output.writeEndElement();
  350. i += increment;
  351. }
  352. i = miny2;
  353. while(i <= maxy2)
  354. {
  355. output.writeStartElement("text");
  356. output.writeAttribute("x", "670");
  357. output.writeAttribute("y", -((i-miny2)*(4.5/(range*100))*10000)+5);
  358. output.writeAttribute("font-size", "12");
  359. output.writeCharacters(Number(i*100).toFixed(0)+"%");
  360. output.writeEndElement();
  361. i += 0.1;
  362. }
  363. i = 0;
  364. while(i <= days-1)
  365. {
  366. output.writeStartElement("text");
  367. output.writeAttribute("x", 45+((600/(days-1))*i));
  368. output.writeAttribute("y", "20");
  369. output.writeAttribute("font-size", "12");
  370. output.writeAttribute("transform", "rotate(90 "+(45+((600/(days-1))*i))+",20)");
  371. q = "SELECT ('"+dates[i]+"'::date - '1 day'::interval)::date";
  372. query.exec(q);
  373. query.next();
  374. var ds = query.value(0);
  375. var parts = ds.split("-");
  376. switch(parts[1])
  377. {
  378. case '01':
  379. ds = "January ";
  380. break;
  381. case '02':
  382. ds = "February ";
  383. break;
  384. case '03':
  385. ds = "March ";
  386. break;
  387. case '04':
  388. ds = "April ";
  389. break;
  390. case '05':
  391. ds = "May ";
  392. break;
  393. case '06':
  394. ds = "June ";
  395. break;
  396. case '07':
  397. ds = "July ";
  398. break;
  399. case '08':
  400. ds = "August ";
  401. break;
  402. case '09':
  403. ds = "September ";
  404. break;
  405. case '10':
  406. ds = "October ";
  407. break;
  408. case '11':
  409. ds = "November ";
  410. break;
  411. case '12':
  412. ds = "December ";
  413. break;
  414. }
  415. ds = ds + Number(parts[2]);
  416. output.writeCharacters(ds);
  417. output.writeEndElement();
  418. if(i == days-1)
  419. {
  420. break;
  421. }
  422. i += 7;
  423. if(i > days-1)
  424. {
  425. i = days - 1;
  426. }
  427. }
  428. output.writeStartElement("rect");
  429. output.writeAttribute("fill", "rgb(0,0,0)");
  430. output.writeAttribute("x", "45");
  431. output.writeAttribute("y", "110");
  432. output.writeAttribute("width", "24");
  433. output.writeAttribute("height", "12");
  434. output.writeEndElement();
  435. output.writeStartElement("text");
  436. output.writeAttribute("x", "75");
  437. output.writeAttribute("y", "120");
  438. output.writeAttribute("font-size", "12");
  439. switch(unitBox.currentIndex) {
  440. case 0:
  441. output.writeCharacters("Previous Year Kg");
  442. break;
  443. case 1:
  444. output.writeCharacters("Previous Year Lb");
  445. break;
  446. }
  447. output.writeEndElement();
  448. output.writeStartElement("rect");
  449. output.writeAttribute("fill", "rgb(255,0,0)");
  450. output.writeAttribute("x", "195");
  451. output.writeAttribute("y", "110");
  452. output.writeAttribute("width", "24");
  453. output.writeAttribute("height", "12");
  454. output.writeEndElement();
  455. output.writeStartElement("text");
  456. output.writeAttribute("x", "225");
  457. output.writeAttribute("y", "120");
  458. output.writeAttribute("font-size", "12");
  459. switch(unitBox.currentIndex) {
  460. case 0:
  461. output.writeCharacters("Current Year Kg");
  462. break;
  463. case 1:
  464. output.writeCharacters("Current Year Lb");
  465. break;
  466. }
  467. output.writeEndElement();
  468. output.writeStartElement("rect");
  469. output.writeAttribute("fill", "rgb(0,255,0)");
  470. output.writeAttribute("x", "345");
  471. output.writeAttribute("y", "110");
  472. output.writeAttribute("width", "24");
  473. output.writeAttribute("height", "12");
  474. output.writeEndElement();
  475. output.writeStartElement("text");
  476. output.writeAttribute("x", "375");
  477. output.writeAttribute("y", "120");
  478. output.writeAttribute("font-size", "12");
  479. output.writeCharacters("% Change");
  480. output.writeEndElement();
  481. output.writeStartElement("rect");
  482. output.writeAttribute("fill", "rgb(0,0,255)");
  483. output.writeAttribute("x", "495");
  484. output.writeAttribute("y", "110");
  485. output.writeAttribute("width", "24");
  486. output.writeAttribute("height", "12");
  487. output.writeEndElement();
  488. output.writeStartElement("text");
  489. output.writeAttribute("x", "525");
  490. output.writeAttribute("y", "120");
  491. output.writeAttribute("font-size", "12");
  492. output.writeCharacters("Average % Change");
  493. output.writeEndElement();
  494. output.writeEndElement();
  495. output.writeEndElement();
  496. output.writeEndElement();
  497. output.writeEndElement();
  498. output.writeEndElement();
  499. output.writeEndDocument();
  500. view.setContent(buffer);
  501. buffer.close();
  502. }
  503. refresh();
  504. startDateField.dateChanged.connect(function() {
  505. refresh();
  506. });
  507. endDateField.dateChanged.connect(function() {
  508. refresh();
  509. });
  510. avgField.editingFinished.connect(function() {
  511. refresh();
  512. });
  513. ]]>
  514. </program>
  515. </window>