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.

cogr.xml 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 = TTR("greencost", "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(TTR("greencost", "Kg"));
  23. unitBox.addItem(TTR("greencost", "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", TTR("greencost", "Cost of Green Coffee for Roasted Coffee"));
  43. output.writeEndElement();
  44. output.writeStartElement("body");
  45. var unit = (unitBox.currentIndex == 0 ? TTR("greencost", "Kg") :
  46. TTR("greencost", "Lb"));
  47. output.writeTextElement("h1", TTR("greencost", "Cost of Green Coffee for Roasted Coffee"));
  48. output.writeTextElement("p", TTR("greencost", "Cost of green coffee per ") +
  49. unit + TTR("greencost", " of roasted coffee"));
  50. var query = new QSqlQuery();
  51. query.exec("SELECT item, (SELECT name FROM items WHERE id = item) AS name FROM current_items ORDER BY name");
  52. var roastedItems = new Array();
  53. var roastedNames = new Array();
  54. while(query.next()) {
  55. roastedItems.push(query.value(0));
  56. roastedNames.push(query.value(1));
  57. }
  58. var recipes = new Array();
  59. var recipeQuantities = new Array();
  60. 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)");
  61. for(var i = 0; i < roastedItems.length; i++) {
  62. query.bind("item", roastedItems[i]);
  63. query.bind("item2", roastedItems[i]);
  64. query.exec();
  65. if(query.next()) {
  66. recipes.push(query.value(0));
  67. recipeQuantities.push(query.value(1));
  68. } else {
  69. recipes.push("{-1}");
  70. recipeQuantities.push("{-1}");
  71. }
  72. }
  73. 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");
  74. var mins = new Array();
  75. var maxes = new Array();
  76. var means = new Array();
  77. for(var i = 0; i < roastedItems.length; i++) {
  78. if(recipes[i] == "{-1}") {
  79. mins.push("undefined");
  80. maxes.push("undefined");
  81. means.push("undefined");
  82. } else {
  83. query.bind(":ri", Number(roastedItems[i]));
  84. query.bind(":gi", recipes[i]);
  85. query.exec();
  86. if(query.next()) {
  87. mins.push(query.value(0));
  88. maxes.push(query.value(1));
  89. means.push(query.value(2));
  90. } else {
  91. print("Error 2");
  92. }
  93. }
  94. }
  95. var proportionalCosts = new Array();
  96. query.prepare("SELECT cost * :proportion * :conversion FROM purchase WHERE item = :id");
  97. for(var i = 0; i < roastedItems.length; i++) {
  98. if(recipes[i] == "{-1}") {
  99. proportionalCosts.push("undefined");
  100. } else {
  101. greens = sqlToArray(recipes[i]);
  102. weights = sqlToArray(recipeQuantities[i]);
  103. proportions = new Array();
  104. quantitySum = weights.reduce(function(p, c) {
  105. return Number(p) + Number(c);
  106. });
  107. for(var j = 0; j < weights.length; j++) {
  108. proportions.push(weights[j]/quantitySum);
  109. }
  110. partialSum = 0;
  111. for(var j = 0; j < greens.length; j++) {
  112. query.bind(":proportion", proportions[j]);
  113. query.bind(":id", greens[j]);
  114. query.bind(":conversion", conversion);
  115. query.exec();
  116. if(query.next()) {
  117. partialSum += Number(query.value(0));
  118. } else {
  119. print("Error 3");
  120. }
  121. }
  122. proportionalCosts.push(partialSum);
  123. }
  124. }
  125. query = query.invalidate();
  126. var minCosts = new Array();
  127. var maxCosts = new Array();
  128. var meanCosts = new Array();
  129. for(var i = 0; i < roastedItems.length; i++) {
  130. if(recipes[i] == "{-1}") {
  131. minCosts.push("undefined");
  132. maxCosts.push("undefined");
  133. meanCosts.push("undefined");
  134. } else {
  135. minCosts.push(proportionalCosts[i] * mins[i]);
  136. maxCosts.push(proportionalCosts[i] * maxes[i]);
  137. meanCosts.push(proportionalCosts[i] * means[i]);
  138. }
  139. }
  140. output.writeStartElement("table");
  141. output.writeAttribute("rules", "groups");
  142. output.writeAttribute("cellpadding", "3px");
  143. output.writeStartElement("thead");
  144. output.writeStartElement("tr");
  145. output.writeTextElement("th", TTR("greencost", "Coffee"));
  146. output.writeTextElement("th", TTR("greencost", "Minimum Cost"));
  147. output.writeTextElement("th", TTR("greencost", "Maximum Cost"));
  148. output.writeTextElement("th", TTR("greencost", "Mean Cost"));
  149. output.writeEndElement();
  150. output.writeEndElement();
  151. output.writeStartElement("tbody");
  152. for(var i = 0; i < roastedItems.length; i++) {
  153. output.writeStartElement("tr");
  154. output.writeTextElement("td", roastedNames[i]);
  155. output.writeTextElement("td", Number(minCosts[i]).toFixed(2));
  156. output.writeTextElement("td", Number(maxCosts[i]).toFixed(2));
  157. output.writeTextElement("td", Number(meanCosts[i]).toFixed(2));
  158. output.writeEndElement();
  159. }
  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. ]]>
  170. </program>
  171. </window>