Typica is a free program for professional coffee roasters. https://typica.us
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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);
  101. output.writeTextElement("td", query.value(2) / conversion);
  102. output.writeTextElement("td", query.value(3) / conversion);
  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);
  111. output.writeTextElement("td", query.value(1) / conversion);
  112. output.writeTextElement("td", query.value(2) / conversion);
  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. var maxy1 = 0;
  209. while(maxy1 < Math.max(curpounds[days - 1], prevpounds[days - 1]))
  210. {
  211. maxy1 += 100;
  212. }
  213. var pos = 0;
  214. while(pos <= maxy1)
  215. {
  216. output.writeStartElement("line");
  217. output.writeAttribute("x1", "35");
  218. output.writeAttribute("x2", "45");
  219. output.writeAttribute("y1", (450/maxy1)*pos);
  220. output.writeAttribute("y2", (450/maxy1)*pos);
  221. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  222. output.writeEndElement();
  223. pos += 100;
  224. }
  225. var n = Math.min.apply(Math, change);
  226. var m = Math.max.apply(Math, change);
  227. var miny2 = 0;
  228. var maxy2;
  229. if(n < 0)
  230. {
  231. while(miny2 > n)
  232. {
  233. miny2 -= 0.1;
  234. }
  235. maxy2 = miny2;
  236. }
  237. else
  238. {
  239. while(miny2 < n)
  240. {
  241. miny2 += 0.1;
  242. }
  243. miny2 -= 0.1;
  244. maxy2 = miny2;
  245. }
  246. while(maxy2 < m)
  247. {
  248. maxy2 += 0.1;
  249. }
  250. var range = maxy2 - miny2;
  251. pos = miny2;
  252. while(pos <= maxy2)
  253. {
  254. output.writeStartElement("line");
  255. output.writeAttribute("x1", "655");
  256. output.writeAttribute("x2", "665");
  257. output.writeAttribute("y1", (pos-miny2)*(4.5/(range*100))*10000);
  258. output.writeAttribute("y2", (pos-miny2)*(4.5/(range*100))*10000);
  259. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  260. output.writeEndElement();
  261. pos += 0.1;
  262. }
  263. pos = 0;
  264. while(pos < days)
  265. {
  266. output.writeStartElement("line");
  267. output.writeAttribute("x1", 50+((600/(days-1))*pos));
  268. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  269. output.writeAttribute("y1", "-5");
  270. output.writeAttribute("y2", "-15");
  271. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  272. output.writeEndElement();
  273. pos += 7;
  274. }
  275. output.writeStartElement("line");
  276. output.writeAttribute("x1", 50+((600/(days-1))*(days-1)));
  277. output.writeAttribute("x2", 50+((600/(days-1))*(days-1)));
  278. output.writeAttribute("y1", "-5");
  279. output.writeAttribute("y2", "-15");
  280. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  281. output.writeEndElement();
  282. pos = 1;
  283. while(pos < days)
  284. {
  285. output.writeStartElement("line");
  286. output.writeAttribute("x1", 50+((600/(days-1))*(pos - 1)));
  287. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  288. output.writeAttribute("y1", (prevpounds[pos-1]/maxy1)*450);
  289. output.writeAttribute("y2", (prevpounds[pos]/maxy1)*450);
  290. output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
  291. output.writeEndElement();
  292. output.writeStartElement("line");
  293. output.writeAttribute("x1", 50+((600/(days-1))*(pos - 1)));
  294. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  295. output.writeAttribute("y1", (curpounds[pos-1]/maxy1)*450);
  296. output.writeAttribute("y2", (curpounds[pos]/maxy1)*450);
  297. output.writeAttribute("style", "stroke:rgb(255,0,0);stroke-width:1;");
  298. output.writeEndElement();
  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", (change[pos-1]-miny2)*(4.5/(range*100))*10000);
  303. output.writeAttribute("y2", (change[pos]-miny2)*(4.5/(range*100))*10000);
  304. output.writeAttribute("style", "stroke:rgb(0,255,0);stroke-width:1;");
  305. output.writeEndElement();
  306. if(pos > (parseInt(avgField.text)-1))
  307. {
  308. output.writeStartElement("line");
  309. output.writeAttribute("x1", 50+((600/(days-1))*(pos-1)));
  310. output.writeAttribute("x2", 50+((600/(days-1))*pos));
  311. output.writeAttribute("y1", (avgchange[pos-1]-miny2)*(4.5/(range*100))*10000);
  312. output.writeAttribute("y2", (avgchange[pos]-miny2)*(4.5/(range*100))*10000);
  313. output.writeAttribute("style", "stroke:rgb(0,0,255);stroke-width:1;");
  314. output.writeEndElement();
  315. }
  316. pos++;
  317. }
  318. output.writeEndElement();
  319. i = 0;
  320. while(i <= maxy1)
  321. {
  322. output.writeStartElement("text");
  323. output.writeAttribute("x", "0");
  324. output.writeAttribute("y", -((450/maxy1)*i)+5);
  325. output.writeAttribute("font-size", "12");
  326. output.writeCharacters(i);
  327. output.writeEndElement();
  328. i += 100;
  329. }
  330. i = miny2;
  331. while(i <= maxy2)
  332. {
  333. output.writeStartElement("text");
  334. output.writeAttribute("x", "670");
  335. output.writeAttribute("y", -((i-miny2)*(4.5/(range*100))*10000)+5);
  336. output.writeAttribute("font-size", "12");
  337. output.writeCharacters(Number(i*100).toFixed(0)+"%");
  338. output.writeEndElement();
  339. i += 0.1;
  340. }
  341. i = 0;
  342. while(i <= days-1)
  343. {
  344. output.writeStartElement("text");
  345. output.writeAttribute("x", 45+((600/(days-1))*i));
  346. output.writeAttribute("y", "20");
  347. output.writeAttribute("font-size", "12");
  348. output.writeAttribute("transform", "rotate(90 "+(45+((600/(days-1))*i))+",20)");
  349. q = "SELECT ('"+dates[i]+"'::date - '1 day'::interval)::date";
  350. query.exec(q);
  351. query.next();
  352. var ds = query.value(0);
  353. var parts = ds.split("-");
  354. switch(parts[1])
  355. {
  356. case '01':
  357. ds = "January ";
  358. break;
  359. case '02':
  360. ds = "February ";
  361. break;
  362. case '03':
  363. ds = "March ";
  364. break;
  365. case '04':
  366. ds = "April ";
  367. break;
  368. case '05':
  369. ds = "May ";
  370. break;
  371. case '06':
  372. ds = "June ";
  373. break;
  374. case '07':
  375. ds = "July ";
  376. break;
  377. case '08':
  378. ds = "August ";
  379. break;
  380. case '09':
  381. ds = "September ";
  382. break;
  383. case '10':
  384. ds = "October ";
  385. break;
  386. case '11':
  387. ds = "November ";
  388. break;
  389. case '12':
  390. ds = "December ";
  391. break;
  392. }
  393. ds = ds + Number(parts[2]);
  394. output.writeCharacters(ds);
  395. output.writeEndElement();
  396. if(i == days-1)
  397. {
  398. break;
  399. }
  400. i += 7;
  401. if(i > days-1)
  402. {
  403. i = days - 1;
  404. }
  405. }
  406. output.writeStartElement("rect");
  407. output.writeAttribute("fill", "rgb(0,0,0)");
  408. output.writeAttribute("x", "45");
  409. output.writeAttribute("y", "110");
  410. output.writeAttribute("width", "24");
  411. output.writeAttribute("height", "12");
  412. output.writeEndElement();
  413. output.writeStartElement("text");
  414. output.writeAttribute("x", "75");
  415. output.writeAttribute("y", "120");
  416. output.writeAttribute("font-size", "12");
  417. output.writeCharacters("Previous Year Pounds");
  418. output.writeEndElement();
  419. output.writeStartElement("rect");
  420. output.writeAttribute("fill", "rgb(255,0,0)");
  421. output.writeAttribute("x", "195");
  422. output.writeAttribute("y", "110");
  423. output.writeAttribute("width", "24");
  424. output.writeAttribute("height", "12");
  425. output.writeEndElement();
  426. output.writeStartElement("text");
  427. output.writeAttribute("x", "225");
  428. output.writeAttribute("y", "120");
  429. output.writeAttribute("font-size", "12");
  430. output.writeCharacters("Current Year Pounds");
  431. output.writeEndElement();
  432. output.writeStartElement("rect");
  433. output.writeAttribute("fill", "rgb(0,255,0)");
  434. output.writeAttribute("x", "345");
  435. output.writeAttribute("y", "110");
  436. output.writeAttribute("width", "24");
  437. output.writeAttribute("height", "12");
  438. output.writeEndElement();
  439. output.writeStartElement("text");
  440. output.writeAttribute("x", "375");
  441. output.writeAttribute("y", "120");
  442. output.writeAttribute("font-size", "12");
  443. output.writeCharacters("% Change");
  444. output.writeEndElement();
  445. output.writeStartElement("rect");
  446. output.writeAttribute("fill", "rgb(0,0,255)");
  447. output.writeAttribute("x", "495");
  448. output.writeAttribute("y", "110");
  449. output.writeAttribute("width", "24");
  450. output.writeAttribute("height", "12");
  451. output.writeEndElement();
  452. output.writeStartElement("text");
  453. output.writeAttribute("x", "525");
  454. output.writeAttribute("y", "120");
  455. output.writeAttribute("font-size", "12");
  456. output.writeCharacters("Average % Change");
  457. output.writeEndElement();
  458. output.writeEndElement();
  459. output.writeEndElement();
  460. output.writeEndElement();
  461. output.writeEndElement();
  462. output.writeEndDocument();
  463. view.setContent(buffer);
  464. buffer.close();
  465. }
  466. refresh();
  467. startDateField.dateChanged.connect(function() {
  468. refresh();
  469. });
  470. endDateField.dateChanged.connect(function() {
  471. refresh();
  472. });
  473. avgField.editingFinished.connect(function() {
  474. refresh();
  475. });
  476. ]]>
  477. </program>
  478. </window>