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.

monthcompare.xml 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <window id="pytdprodcomp">
  2. <reporttitle>Production:->Previous Year Production Comparison By Month</reporttitle>
  3. <layout type="vertical">
  4. <layout type="horizontal">
  5. <label>Weight Unit:</label>
  6. <sqldrop id="unit" />
  7. <stretch />
  8. </layout>
  9. <webview id="report" />
  10. </layout>
  11. <menu name="File">
  12. <item id="print" shortcut="Ctrl+P">Print</item>
  13. </menu>
  14. <program>
  15. <![CDATA[
  16. this.windowTitle = "Typica - Previous Year Production Comparison By Month";
  17. var view = findChildObject(this, 'report');
  18. var printMenu = findChildObject(this, 'print');
  19. printMenu.triggered.connect(function() {
  20. view.print();
  21. });
  22. var unitBox = findChildObject(this, 'unit');
  23. unitBox.addItem("Kg");
  24. unitBox.addItem("Lb");
  25. unitBox.currentIndex = QSettings.value("script/report_unit", 1);
  26. unitBox['currentIndexChanged(int)'].connect(function() {
  27. QSettings.setValue("script/report_unit", unitBox.currentIndex);
  28. refresh();
  29. });
  30. function refresh() {
  31. var buffer = new QBuffer;
  32. buffer.open(3);
  33. var output = new XmlWriter(buffer);
  34. output.writeStartDocument("1.0");
  35. 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">');
  36. output.writeStartElement("html");
  37. output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
  38. output.writeStartElement("head");
  39. output.writeTextElement("title", "Previous Year Production Comparison By Month");
  40. output.writeEndElement();
  41. output.writeStartElement("body");
  42. output.writeTextElement("h1", "Previous Year Production Comparison By Month");
  43. switch(unitBox.currentIndex)
  44. {
  45. case 0:
  46. output.writeTextElement("p", "This report compares total roasted coffee production in kilograms with the previous year on a monthly basis.");
  47. break;
  48. case 1:
  49. output.writeTextElement("p", "This report compares total roasted coffee production in pounds with the previous year on a monthly basis.");
  50. break;
  51. }
  52. output.writeStartElement("table");
  53. output.writeAttribute("style", "page-break-after:auto;");
  54. output.writeAttribute("rules", "groups");
  55. output.writeAttribute("cellpadding", "3px");
  56. output.writeStartElement("thead");
  57. output.writeStartElement("tr");
  58. output.writeTextElement("th", "Month");
  59. var query = new QSqlQuery();
  60. query.exec("SELECT EXTRACT(YEAR FROM 'now'::date) AS current_year");
  61. query.next();
  62. var current_year = query.value(0);
  63. output.writeTextElement("th", current_year-1);
  64. output.writeTextElement("th", current_year);
  65. output.writeTextElement("th", "Change");
  66. output.writeEndElement();
  67. output.writeEndElement();
  68. output.writeStartElement("tbody");
  69. var month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  70. var current_data = new Array();
  71. var previous_data = new Array();
  72. for(var i = 0; i < 12; i++) {
  73. var q = "SELECT SUM(roasted_quantity) FROM roasting_log WHERE roasted_id IS NOT NULL AND time > '"
  74. q += current_year;
  75. q += "-";
  76. q += 1+i;
  77. q += "-01' AND time < '";
  78. if(i == 11) {
  79. q += 1+current_year;
  80. } else {
  81. q += current_year;
  82. }
  83. q += "-";
  84. if(i == 11) {
  85. q += "01"
  86. } else {
  87. q += 2+i;
  88. }
  89. q += "-01'";
  90. query.exec(q);
  91. query.next();
  92. current_data.push(query.value(0));
  93. }
  94. for(var i = 0; i < 12; i++) {
  95. var q = "SELECT SUM(roasted_quantity) FROM roasting_log WHERE roasted_id IS NOT NULL AND time > '"
  96. q += current_year-1;
  97. q += "-";
  98. q += 1+i;
  99. q += "-01' AND time < '";
  100. if(i == 11) {
  101. q += current_year;
  102. } else {
  103. q += current_year-1;
  104. }
  105. q += "-";
  106. if(i == 11) {
  107. q += "01"
  108. } else {
  109. q += 2+i;
  110. }
  111. q += "-01'";
  112. query.exec(q);
  113. query.next();
  114. previous_data.push(query.value(0));
  115. }
  116. query = query.invalidate();
  117. for(var i = 0; i < 12; i++) {
  118. output.writeStartElement("tr");
  119. output.writeTextElement("td", month_names[i]);
  120. switch(unitBox.currentIndex)
  121. {
  122. case 0:
  123. output.writeTextElement("td", Number(previous_data[i] / 2.2).toFixed(2));
  124. output.writeTextElement("td", Number(current_data[i] / 2.2).toFixed(2));
  125. output.writeTextElement("td", Number((current_data[i] - previous_data[i]) / 2.2).toFixed(2));
  126. break;
  127. case 1:
  128. output.writeTextElement("td", Number(previous_data[i]).toFixed(2));
  129. output.writeTextElement("td", Number(current_data[i]).toFixed(2));
  130. output.writeTextElement("td", Number(current_data[i]-previous_data[i]).toFixed(2));
  131. break;
  132. }
  133. output.writeEndElement();
  134. }
  135. output.writeEndElement();
  136. output.writeStartElement("tfoot");
  137. output.writeTextElement("th", "Totals");
  138. var current_total = current_data.reduce(function(a,b) {return Number(a) + Number(b);}, 0);
  139. var previous_total = previous_data.reduce(function(a,b) {return Number(a) + Number(b);}, 0);
  140. switch(unitBox.currentIndex)
  141. {
  142. case 0:
  143. output.writeTextElement("td", (previous_total/2.2).toFixed(2));
  144. output.writeTextElement("td", (current_total/2.2).toFixed(2));
  145. output.writeTextElement("td", ((current_total-previous_total)/2.2).toFixed(2));
  146. break;
  147. case 1:
  148. output.writeTextElement("td", previous_total.toFixed(2));
  149. output.writeTextElement("td", current_total.toFixed(2));
  150. output.writeTextElement("td", (current_total-previous_total).toFixed(2));
  151. break;
  152. }
  153. output.writeEndElement();
  154. output.writeEndElement();
  155. output.writeEndElement();
  156. output.writeEndDocument();
  157. view.setContent(buffer);
  158. buffer.close();
  159. }
  160. refresh();
  161. ]]>
  162. </program>
  163. </window>