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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <window id="useandcostreport">
  2. <reporttitle>Production:->Average Use and Cost by Origin</reporttitle>
  3. <layout type="vertical">
  4. <layout type="horizontal">
  5. <label>Sort Order:</label>
  6. <sqldrop id="sort" />
  7. <label>Weight Unit:</label>
  8. <sqldrop id="unit" />
  9. <stretch />
  10. </layout>
  11. <webview id="report" />
  12. </layout>
  13. <menu name="File">
  14. <item id="print" shortcut="Ctrl+P">Print</item>
  15. </menu>
  16. <program>
  17. <![CDATA[
  18. this.windowTitle = "Typica - Average Use and Cost by Origin";
  19. var report = findChildObject(this, 'report');
  20. var printMenu = findChildObject(this, 'print');
  21. printMenu.triggered.connect(function() {
  22. report.print();
  23. });
  24. var sortBox = findChildObject(this, 'sort');
  25. sortBox.addItem("Origin A-Z");
  26. sortBox.addItem("Origin Z-A");
  27. sortBox.addItem("Avg. Rate Ascending");
  28. sortBox.addItem("Avg. Rate Descending");
  29. sortBox.addItem("Avg. Cost Ascending");
  30. sortBox.addItem("Avg. Cost Descending");
  31. sortBox.currentIndex = QSettings.value("auco_sort", 0);
  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 buffer = new QBuffer;
  42. buffer.open(3);
  43. var output = new XmlWriter(buffer);
  44. output.writeStartDocument("1.0");
  45. 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">');
  46. output.writeStartElement("html");
  47. output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
  48. output.writeStartElement("head");
  49. output.writeTextElement("title", "Recent Use and Cost by Origin");
  50. output.writeEndElement();
  51. output.writeStartElement("body");
  52. output.writeTextElement("h1", "Average Use and Cost by Origin");
  53. switch(unitBox.currentIndex)
  54. {
  55. case 0:
  56. output.writeTextElement("p", "This is a report of average rate of use in kilograms per day and cost of unroasted coffee.");
  57. break;
  58. case 1:
  59. output.writeTextElement("p", "This is a report of average rate of use in pounds per day and cost of unroasted coffee.");
  60. break;
  61. }
  62. output.writeStartElement("table");
  63. output.writeAttribute("rules", "groups");
  64. output.writeAttribute("cellpadding", "3px");
  65. output.writeStartElement("thead");
  66. output.writeStartElement("tr");
  67. output.writeStartElement("th");
  68. output.writeAttribute("colspan", "8");
  69. output.writeCharacters("Regular Coffees");
  70. output.writeEndElement();
  71. output.writeEndElement();
  72. output.writeStartElement("tr");
  73. output.writeTextElement("th", "Origin");
  74. output.writeTextElement("th", "Avg. Rate");
  75. output.writeTextElement("th", "Avg. Cost");
  76. output.writeTextElement("th", "Last Cost");
  77. output.writeTextElement("th", "Last Purchase Date");
  78. output.writeTextElement("th", "Bag Size (min)");
  79. output.writeTextElement("th", "Bag Size (max)");
  80. output.writeTextElement("th", "Bag Size (mean)");
  81. output.writeEndElement();
  82. output.writeEndElement();
  83. output.writeStartElement("tbody");
  84. var query = new QSqlQuery();
  85. var conversion = 1;
  86. if(unitBox.currentIndex == 0) {
  87. conversion = 2.2;
  88. }
  89. var orderClause;
  90. switch(sortBox.currentIndex)
  91. {
  92. case 0:
  93. orderClause = "origin ASC";
  94. break;
  95. case 1:
  96. orderClause = "origin DESC";
  97. break;
  98. case 2:
  99. orderClause = "rate ASC";
  100. break;
  101. case 3:
  102. orderClause = "rate DESC";
  103. break;
  104. case 4:
  105. orderClause = "cost ASC";
  106. break;
  107. case 5:
  108. orderClause = "cost DESC";
  109. break;
  110. }
  111. query.prepare("SELECT DISTINCT origin, (avg(rate)/:conversion)::numeric(10,2) AS rate, (SELECT avg(cost)*:conversion2 FROM purchase WHERE item IN (SELECT id FROM regular_coffees WHERE origin = coffee_history.origin))::numeric(10,2) AS cost, (SELECT avg(cost)*:conversion3 FROM purchase WHERE item IN (SELECT id FROM regular_coffees WHERE origin = coffee_history.origin) AND time = (SELECT max(time) FROM purchase WHERE item IN (SELECT id FROM regular_coffees WHERE origin = coffee_history.origin))), (SELECT max(time)::date FROM purchase WHERE item IN (SELECT id FROM regular_coffees WHERE origin = coffee_history.origin)), (SELECT min(conversion)/:conversion4 FROM lb_bag_conversion WHERE item IN (SELECT id FROM regular_coffees WHERE origin = coffee_history.origin))::numeric(10,2) AS minbag, (SELECT max(conversion)/:conversion5 FROM lb_bag_conversion WHERE item IN (SELECT id FROM regular_coffees WHERE origin = coffee_history.origin))::numeric(10,2) AS maxbag, (SELECT avg(conversion)/:conversion6 FROM lb_bag_conversion WHERE item IN (SELECT id FROM regular_coffees WHERE origin = coffee_history.origin))::numeric(10,2) AS meanbag FROM coffee_history WHERE id IN (SELECT id FROM regular_coffees) GROUP BY origin ORDER BY " + orderClause);
  112. query.bind(":conversion", conversion);
  113. query.bind(":conversion2", conversion);
  114. query.bind(":conversion3", conversion);
  115. query.bind(":conversion4", conversion);
  116. query.bind(":conversion5", conversion);
  117. query.bind(":conversion6", conversion);
  118. query.exec();
  119. while(query.next())
  120. {
  121. output.writeStartElement("tr");
  122. output.writeTextElement("td", query.value(0));
  123. output.writeTextElement("td", query.value(1));
  124. output.writeTextElement("td", query.value(2));
  125. output.writeTextElement("td", query.value(3));
  126. output.writeTextElement("td", query.value(4));
  127. output.writeTextElement("td", query.value(5));
  128. output.writeTextElement("td", query.value(6));
  129. output.writeTextElement("td", query.value(7));
  130. output.writeEndElement();
  131. }
  132. output.writeStartElement("tr");
  133. output.writeStartElement("th");
  134. output.writeAttribute("colspan", "8");
  135. output.writeCharacters("Decaffeinated Coffees");
  136. output.writeEndElement();
  137. output.writeEndElement();
  138. query.prepare("SELECT DISTINCT origin, (avg(rate)/:conversion)::numeric(10,2) AS rate, (SELECT avg(cost)*:conversion2 FROM purchase WHERE item IN (SELECT id FROM decaf_coffees WHERE origin = coffee_history.origin))::numeric(10,2) AS cost, (SELECT avg(cost)*:conversion3 FROM purchase WHERE item IN (SELECT id FROM decaf_coffees WHERE origin = coffee_history.origin) AND time = (SELECT max(time) FROM purchase WHERE item IN (SELECT id FROM decaf_coffees WHERE origin = coffee_history.origin))), (SELECT max(time)::date FROM purchase WHERE item IN (SELECT id FROM decaf_coffees WHERE origin = coffee_history.origin)), (SELECT min(conversion)/:conversion4 FROM lb_bag_conversion WHERE item IN (SELECT id FROM decaf_coffees WHERE origin = coffee_history.origin))::numeric(10,2) AS minbag, (SELECT max(conversion)/:conversion5 FROM lb_bag_conversion WHERE item IN (SELECT id FROM decaf_coffees WHERE origin = coffee_history.origin))::numeric(10,2) AS maxbag, (SELECT avg(conversion)/:conversion6 FROM lb_bag_conversion WHERE item IN (SELECT id FROM decaf_coffees WHERE origin = coffee_history.origin))::numeric(10,2) AS meanbag FROM coffee_history WHERE id IN (SELECT id FROM decaf_coffees) GROUP BY origin ORDER BY " + orderClause);
  139. query.bind(":conversion", conversion);
  140. query.bind(":conversion2", conversion);
  141. query.bind(":conversion3", conversion);
  142. query.bind(":conversion4", conversion);
  143. query.bind(":conversion5", conversion);
  144. query.bind(":conversion6", conversion);
  145. query.exec();
  146. while(query.next())
  147. {
  148. output.writeStartElement("tr");
  149. output.writeTextElement("td", query.value(0));
  150. output.writeTextElement("td", query.value(1));
  151. output.writeTextElement("td", query.value(2));
  152. output.writeTextElement("td", query.value(3));
  153. output.writeTextElement("td", query.value(4));
  154. output.writeTextElement("td", query.value(5));
  155. output.writeTextElement("td", query.value(6));
  156. output.writeTextElement("td", query.value(7));
  157. output.writeEndElement();
  158. }
  159. query = query.invalidate();
  160. output.writeEndElement();
  161. output.writeEndElement();
  162. output.writeEndElement();
  163. output.writeEndElement();
  164. output.writeEndDocument();
  165. report.setContent(buffer);
  166. buffer.close();
  167. }
  168. refresh();
  169. sortBox['currentIndexChanged(int)'].connect(function() {
  170. QSettings.setValue("auco_sort", sortBox.currentIndex);
  171. refresh();
  172. });
  173. ]]>
  174. </program>
  175. </window>