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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <window id="greencost">
  2. <reporttitle>Production:->Cost of Green Coffee for Roasted Coffee</reporttitle> <layout type="vertical">
  3. <layout type="horizontal">
  4. <label>Weight Unit:</label>
  5. <sqldrop id="unit" />
  6. <stretch />
  7. </layout>
  8. <webview id="report" />
  9. </layout>
  10. <menu name="File">
  11. <item id="print" shortcut="Ctrl+P">Print</item>
  12. </menu>
  13. <program>
  14. <![CDATA[
  15. this.windowTitle = "Typica - Cost of Green Coffee for Roasted Coffee";
  16. var report = findChildObject(this, 'report');
  17. var printMenu = findChildObject(this, 'print');
  18. printMenu.triggered.connect(function() {
  19. report.print();
  20. });
  21. var unitBox = findChildObject(this, 'unit');
  22. unitBox.addItem("Kg");
  23. unitBox.addItem("Lb");
  24. unitBox.currentIndex = QSettings.value("script/report_unit", 1);
  25. unitBox['currentIndexChanged(int)'].connect(function() {
  26. QSettings.setValue("script/report_unit", unitBox.currentIndex);
  27. refresh();
  28. });
  29. function refresh() {
  30. var conversion = 1;
  31. if(unitBox.currentIndex == 0) {
  32. conversion = 2.2;
  33. }
  34. var buffer = new QBuffer;
  35. buffer.open(3);
  36. var output = new XmlWriter(buffer);
  37. output.writeStartDocument("1.0");
  38. 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">');
  39. output.writeStartElement("html");
  40. output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
  41. output.writeStartElement("head");
  42. output.writeTextElement("title", "Cost of Green Coffee for Roasted Coffee");
  43. output.writeEndElement();
  44. output.writeStartElement("body");
  45. var unit = (unitBox.currentIndex == 0 ? "Kg" : "Lb");
  46. output.writeTextElement("h1", "Cost of Green Coffee for Roasted Coffee");
  47. output.writeTextElement("p", "Cost of green coffee per " + unit + " of roasted coffee");
  48. var query = new QSqlQuery();
  49. query.exec("SELECT item, (SELECT name FROM items WHERE id = item) AS name FROM current_items ORDER BY name");
  50. var roastedItems = new Array();
  51. var roastedNames = new Array();
  52. while(query.next()) {
  53. roastedItems.push(query.value(0));
  54. roastedNames.push(query.value(1));
  55. }
  56. var recipes = new Array();
  57. var recipeQuantities = new Array();
  58. query.prepare("SELECT unroasted_id, unroasted_quantity FROM roasting_log WHERE roasted_id = :item AND time = (SELECT max(time) FROM roasting_log WHERE roasted_id = :item2)");
  59. for(var i = 0; i < roastedItems.length; i++) {
  60. query.bind("item", roastedItems[i]);
  61. query.bind("item2", roastedItems[i]);
  62. query.exec();
  63. if(query.next()) {
  64. recipes.push(query.value(0));
  65. recipeQuantities.push(query.value(1));
  66. } else {
  67. recipes.push("{-1}");
  68. recipeQuantities.push("{-1}");
  69. }
  70. }
  71. query.prepare("SELECT min(unroasted_total_quantity / roasted_quantity), max(unroasted_total_quantity / roasted_quantity), avg(unroasted_total_quantity / roasted_quantity) FROM roasting_log WHERE roasted_id = :ri AND unroasted_id = :gi AND unroasted_total_quantity > 0 AND roasted_quantity > 0 AND approval = true");
  72. var mins = new Array();
  73. var maxes = new Array();
  74. var means = new Array();
  75. for(var i = 0; i < roastedItems.length; i++) {
  76. if(recipes[i] == "{-1}") {
  77. mins.push("undefined");
  78. maxes.push("undefined");
  79. means.push("undefined");
  80. } else {
  81. query.bind(":ri", Number(roastedItems[i]));
  82. query.bind(":gi", recipes[i]);
  83. query.exec();
  84. if(query.next()) {
  85. mins.push(query.value(0));
  86. maxes.push(query.value(1));
  87. means.push(query.value(2));
  88. } else {
  89. print("Error 2");
  90. }
  91. }
  92. }
  93. var proportionalCosts = new Array();
  94. query.prepare("SELECT cost * :proportion * :conversion FROM purchase WHERE item = :id");
  95. for(var i = 0; i < roastedItems.length; i++) {
  96. if(recipes[i] == "{-1}") {
  97. proportionalCosts.push("undefined");
  98. } else {
  99. greens = sqlToArray(recipes[i]);
  100. weights = sqlToArray(recipeQuantities[i]);
  101. proportions = new Array();
  102. quantitySum = weights.reduce(function(p, c) {
  103. return Number(p) + Number(c);
  104. });
  105. for(var j = 0; j < weights.length; j++) {
  106. proportions.push(weights[j]/quantitySum);
  107. }
  108. partialSum = 0;
  109. for(var j = 0; j < greens.length; j++) {
  110. query.bind(":proportion", proportions[j]);
  111. query.bind(":id", greens[j]);
  112. query.bind(":conversion", conversion);
  113. query.exec();
  114. if(query.next()) {
  115. partialSum += Number(query.value(0));
  116. } else {
  117. print("Error 3");
  118. }
  119. }
  120. proportionalCosts.push(partialSum);
  121. }
  122. }
  123. query = query.invalidate();
  124. var minCosts = new Array();
  125. var maxCosts = new Array();
  126. var meanCosts = new Array();
  127. for(var i = 0; i < roastedItems.length; i++) {
  128. if(recipes[i] == "{-1}") {
  129. minCosts.push("undefined");
  130. maxCosts.push("undefined");
  131. meanCosts.push("undefined");
  132. } else {
  133. minCosts.push(proportionalCosts[i] * mins[i]);
  134. maxCosts.push(proportionalCosts[i] * maxes[i]);
  135. meanCosts.push(proportionalCosts[i] * means[i]);
  136. }
  137. }
  138. output.writeStartElement("table");
  139. output.writeAttribute("rules", "groups");
  140. output.writeAttribute("cellpadding", "3px");
  141. output.writeStartElement("thead");
  142. output.writeStartElement("tr");
  143. output.writeTextElement("th", "Coffee");
  144. output.writeTextElement("th", "Minimum Cost");
  145. output.writeTextElement("th", "Maximum Cost");
  146. output.writeTextElement("th", "Mean Cost");
  147. output.writeEndElement();
  148. output.writeEndElement();
  149. output.writeStartElement("tbody");
  150. for(var i = 0; i < roastedItems.length; i++) {
  151. output.writeStartElement("tr");
  152. output.writeTextElement("td", roastedNames[i]);
  153. output.writeTextElement("td", Number(minCosts[i]).toFixed(2));
  154. output.writeTextElement("td", Number(maxCosts[i]).toFixed(2));
  155. output.writeTextElement("td", Number(meanCosts[i]).toFixed(2));
  156. output.writeEndElement();
  157. }
  158. output.writeEndElement();
  159. output.writeEndElement();
  160. output.writeEndElement();
  161. output.writeEndElement();
  162. output.writeEndDocument();
  163. report.setContent(buffer);
  164. buffer.close();
  165. };
  166. refresh();
  167. ]]>
  168. </program>
  169. </window>