Typica is a free program for professional coffee roasters. https://typica.us
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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 q = "SELECT "+curEndDate+"::date - "+curStartDate+"::date";
  148. query.exec(q);
  149. query.next();
  150. var days = query.value(0);
  151. var dates = new Array();
  152. var curpounds = new Array();
  153. var prevpounds = new Array();
  154. var change = new Array();
  155. var avgchange = new Array();
  156. var i;
  157. for(i = 0; i < days; i++)
  158. {
  159. q = "SELECT "+curStartDate+"::date + "+(i+1);
  160. query.exec(q);
  161. query.next();
  162. dates[i] = query.value(0);
  163. q = "SELECT sum(roasted_quantity) FROM roasting_log WHERE time > "+curStartDate+" AND time < '"+dates[i]+"'";
  164. query.exec(q);
  165. if(query.next())
  166. {
  167. curpounds[i] = query.value(0);
  168. }
  169. else
  170. {
  171. curpounds[i] = 0;
  172. }
  173. q = "SELECT sum(roasted_quantity) FROM roasting_log WHERE time > "+curStartDate+"::date - '1 year'::interval AND time < '"+dates[i]+"'::date - '1 year'::interval";
  174. query.exec(q);
  175. if(query.next())
  176. {
  177. prevpounds[i] = query.value(0);
  178. }
  179. else
  180. {
  181. prevpounds[i] = 0;
  182. }
  183. if(curpounds[i] > 0)
  184. {
  185. change[i] = (curpounds[i] - prevpounds[i])/curpounds[i];
  186. }
  187. else
  188. {
  189. if(prevpounds[i] > 0)
  190. {
  191. change[i] = -1;
  192. }
  193. else
  194. {
  195. change[i] = 0;
  196. }
  197. }
  198. if(i > (parseInt(avgField.text)-2))
  199. {
  200. var sum = 0;
  201. var j;
  202. for(j = 0; j < parseInt(avgField.text); j++)
  203. {
  204. sum += change[i-j];
  205. }
  206. avgchange[i] = sum / parseInt(avgField.text);
  207. }
  208. }
  209. // Calculate the domain of the primary y axis.
  210. var maxy1 = 0;
  211. var increment = 100; // Starting increment is 100 Lb.
  212. if(unitBox.currentIndex == 0) {
  213. increment = 110; // Starting increment is 50 Kg if Kg is requested.
  214. }
  215. while(maxy1 < Math.max(curpounds[days - 1], prevpounds[days - 1]))
  216. {
  217. maxy1 += increment;
  218. }
  219. // Calculate the number of grid lines and loosen spacing if there are too many.
  220. var divisions = maxy1 / increment;
  221. while(divisions > 10) {
  222. increment *= 2;
  223. divisions = maxy1 / increment;
  224. }
  225. // Draw y axis grid lines.
  226. var pos = 0;
  227. while(pos <= maxy1)
  228. {
  229. output.writeStartElement("line");
  230. output.writeAttribute("x1", "35");
  231. output.writeAttribute("x2", "45");
  232. output.writeAttribute("y1", (450/maxy1)*pos);
  233. output.writeAttribute("y2", (450/maxy1)*pos);
  234. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  235. output.writeEndElement();
  236. pos += increment;
  237. }
  238. var n = Math.min.apply(Math, change);
  239. var m = Math.max.apply(Math, change);
  240. var miny2 = 0;
  241. var maxy2;
  242. if(n < 0)
  243. {
  244. while(miny2 > n)
  245. {
  246. miny2 -= 0.1;
  247. }
  248. maxy2 = miny2;
  249. }
  250. else
  251. {
  252. while(miny2 < n)
  253. {
  254. miny2 += 0.1;
  255. }
  256. miny2 -= 0.1;
  257. maxy2 = miny2;
  258. }
  259. while(maxy2 < m)
  260. {
  261. maxy2 += 0.1;
  262. }
  263. var range = maxy2 - miny2;
  264. pos = miny2;
  265. while(pos <= maxy2)
  266. {
  267. output.writeStartElement("line");
  268. output.writeAttribute("x1", "655");
  269. output.writeAttribute("x2", "665");
  270. output.writeAttribute("y1", (pos-miny2)*(4.5/(range*100))*10000);
  271. output.writeAttribute("y2", (pos-miny2)*(4.5/(range*100))*10000);
  272. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  273. output.writeEndElement();
  274. pos += 0.1;
  275. }
  276. pos = 0;
  277. while(pos < days)
  278. {
  279. output.writeStartElement("line");
  280. output.writeAttribute("x1", 50+((600/(days-1))*pos));
  281. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  282. output.writeAttribute("y1", "-5");
  283. output.writeAttribute("y2", "-15");
  284. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  285. output.writeEndElement();
  286. pos += 7;
  287. }
  288. output.writeStartElement("line");
  289. output.writeAttribute("x1", 50+((600/(days-1))*(days-1)));
  290. output.writeAttribute("x2", 50+((600/(days-1))*(days-1)));
  291. output.writeAttribute("y1", "-5");
  292. output.writeAttribute("y2", "-15");
  293. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  294. output.writeEndElement();
  295. pos = 1;
  296. while(pos < days)
  297. {
  298. output.writeStartElement("line");
  299. output.writeAttribute("x1", 50+((600/(days-1))*(pos - 1)));
  300. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  301. output.writeAttribute("y1", (prevpounds[pos-1]/maxy1)*450);
  302. output.writeAttribute("y2", (prevpounds[pos]/maxy1)*450);
  303. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  304. output.writeEndElement();
  305. output.writeStartElement("line");
  306. output.writeAttribute("x1", 50+((600/(days-1))*(pos - 1)));
  307. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  308. output.writeAttribute("y1", (curpounds[pos-1]/maxy1)*450);
  309. output.writeAttribute("y2", (curpounds[pos]/maxy1)*450);
  310. output.writeAttribute("style", "stroke:rgb(255,0,0);stroke-width:1;");
  311. output.writeEndElement();
  312. output.writeStartElement("line");
  313. output.writeAttribute("x1", 50+((600/(days-1))*(pos-1)));
  314. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  315. output.writeAttribute("y1", (change[pos-1]-miny2)*(4.5/(range*100))*10000);
  316. output.writeAttribute("y2", (change[pos]-miny2)*(4.5/(range*100))*10000);
  317. output.writeAttribute("style", "stroke:rgb(0,255,0);stroke-width:1;");
  318. output.writeEndElement();
  319. if(pos > (parseInt(avgField.text)-1))
  320. {
  321. output.writeStartElement("line");
  322. output.writeAttribute("x1", 50+((600/(days-1))*(pos-1)));
  323. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  324. output.writeAttribute("y1", (avgchange[pos-1]-miny2)*(4.5/(range*100))*10000);
  325. output.writeAttribute("y2", (avgchange[pos]-miny2)*(4.5/(range*100))*10000);
  326. output.writeAttribute("style", "stroke:rgb(0,0,255);stroke-width:1;");
  327. output.writeEndElement();
  328. }
  329. pos++;
  330. }
  331. output.writeEndElement();
  332. i = 0;
  333. while(i <= maxy1)
  334. {
  335. output.writeStartElement("text");
  336. output.writeAttribute("x", "0");
  337. output.writeAttribute("y", -((450/maxy1)*i)+5);
  338. output.writeAttribute("font-size", "12");
  339. switch(unitBox.currentIndex) {
  340. case 0:
  341. output.writeCharacters((i / 2.2).toFixed(0));
  342. break;
  343. case 1:
  344. default:
  345. output.writeCharacters(i);
  346. break;
  347. }
  348. output.writeEndElement();
  349. i += increment;
  350. }
  351. i = miny2;
  352. while(i <= maxy2)
  353. {
  354. output.writeStartElement("text");
  355. output.writeAttribute("x", "670");
  356. output.writeAttribute("y", -((i-miny2)*(4.5/(range*100))*10000)+5);
  357. output.writeAttribute("font-size", "12");
  358. output.writeCharacters(Number(i*100).toFixed(0)+"%");
  359. output.writeEndElement();
  360. i += 0.1;
  361. }
  362. i = 0;
  363. while(i <= days-1)
  364. {
  365. output.writeStartElement("text");
  366. output.writeAttribute("x", 45+((600/(days-1))*i));
  367. output.writeAttribute("y", "20");
  368. output.writeAttribute("font-size", "12");
  369. output.writeAttribute("transform", "rotate(90 "+(45+((600/(days-1))*i))+",20)");
  370. q = "SELECT ('"+dates[i]+"'::date - '1 day'::interval)::date";
  371. query.exec(q);
  372. query.next();
  373. var ds = query.value(0);
  374. var parts = ds.split("-");
  375. switch(parts[1])
  376. {
  377. case '01':
  378. ds = "January ";
  379. break;
  380. case '02':
  381. ds = "February ";
  382. break;
  383. case '03':
  384. ds = "March ";
  385. break;
  386. case '04':
  387. ds = "April ";
  388. break;
  389. case '05':
  390. ds = "May ";
  391. break;
  392. case '06':
  393. ds = "June ";
  394. break;
  395. case '07':
  396. ds = "July ";
  397. break;
  398. case '08':
  399. ds = "August ";
  400. break;
  401. case '09':
  402. ds = "September ";
  403. break;
  404. case '10':
  405. ds = "October ";
  406. break;
  407. case '11':
  408. ds = "November ";
  409. break;
  410. case '12':
  411. ds = "December ";
  412. break;
  413. }
  414. ds = ds + Number(parts[2]);
  415. output.writeCharacters(ds);
  416. output.writeEndElement();
  417. if(i == days-1)
  418. {
  419. break;
  420. }
  421. i += 7;
  422. if(i > days-1)
  423. {
  424. i = days - 1;
  425. }
  426. }
  427. query = query.invalidate();
  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>