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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <window id="invchange">
  2. <reporttitle>Inventory:->Inventory Change Summary</reporttitle>
  3. <layout type="vertical">
  4. <layout type="horizontal">
  5. <daterange id="dates" initial="19" /><!-- Current Year to Date-->
  6. <label>Weight Unit:</label>
  7. <sqldrop id="unit" />
  8. <stretch />
  9. </layout>
  10. <webview id="report" />
  11. </layout>
  12. <menu name="File">
  13. <item id="print" shortcut="Ctrl+P">Print</item>
  14. </menu>
  15. <program>
  16. <![CDATA[
  17. this.windowTitle = TTR("invchange", "Typica - Inventory Change Summary");
  18. var dateSelect = findChildObject(this, 'dates');
  19. var dateQuery = new QSqlQuery();
  20. dateQuery.exec("SELECT time::date FROM transactions WHERE time = (SELECT min(time) FROM transactions) OR time = (SELECT max(time) FROM transactions) ORDER BY time ASC");
  21. dateQuery.next();
  22. var lifetimeStartDate = dateQuery.value(0);
  23. var lifetimeEndDate;
  24. if(dateQuery.next()) {
  25. lifetimeEndDate = dateQuery.value(0);
  26. } else {
  27. lifetimeEndDate = lifetimeStartDate;
  28. }
  29. dateSelect.setLifetimeRange(lifetimeStartDate, lifetimeEndDate);
  30. dateQuery = dateQuery.invalidate();
  31. var unitBox = findChildObject(this, 'unit');
  32. unitBox.addItem(TTR("invchange", "Kg"));
  33. unitBox.addItem(TTR("invchange", "Lb"));
  34. unitBox.currentIndex = QSettings.value("script/report_unit", 1);
  35. unitBox['currentIndexChanged(int)'].connect(function() {
  36. QSettings.setValue("script/report_unit", unitBox.currentIndex);
  37. refresh();
  38. });
  39. var view = findChildObject(this, 'report');
  40. var printMenu = findChildObject(this, 'print');
  41. printMenu.triggered.connect(function() {
  42. view.print();
  43. });
  44. function refresh() {
  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", TTR("invchange", "Inventory Change Summary"));
  54. output.writeEndElement();
  55. output.writeStartElement("body");
  56. var dateRange = dateSelect.currentRange();
  57. var startDate = dateRange[0];
  58. var endDate = dateRange[dateRange.length - 1];
  59. output.writeTextElement("h1", TTR("invchange", "Inventory Change Summary: ") + startDate + " – " + endDate);
  60. var conversion = 1;
  61. if(unitBox.currentIndex == 0) {
  62. conversion = 2.2;
  63. }
  64. var unitText = TTR("invchange", "Lb");
  65. if(unitBox.currentIndex == 0) {
  66. unitText = TTR("invchange", "Kg");
  67. }
  68. var query = new QSqlQuery();
  69. var q = "WITH q AS (SELECT id, name, reference, COALESCE((SELECT balance FROM item_history(id) WHERE time = (SELECT max(time) FROM item_history(id) WHERE time < :sd1)), 0)/:c1 AS starting_balance, COALESCE((SELECT sum(quantity) FROM purchase WHERE item = id AND time >= :sd2 AND time < :ed1 ::date + interval '1 day'), 0)/:c2 AS purchase, COALESCE((SELECT sum(quantity) FROM use WHERE item = id AND time >= :sd3 AND time < :ed2 ::date + interval '1 day'), 0)/:c3 AS use, COALESCE((SELECT sum(quantity) FROM sale WHERE item = id AND time >= :sd4 AND time < :ed3 ::date + interval '1 day'), 0)/:c4 AS sale, (SElECT balance FROM item_history(id) WHERE time = (SELECT max(time) FROM item_history(id) WHERE time < :ed4 ::date + interval '1 day'))/:c5 AS quantity, (SELECT sum(cost * quantity) / sum(quantity) FROM purchase WHERE item = id) AS unit_cost FROM coffees WHERE id IN (SELECT item FROM purchase WHERE time >= :sd6 AND time < :ed5 ::date + interval '1 day') OR id IN (SELECT id FROM items WHERE (SELECT balance FROM item_history(id) WHERE time = (SELECT max(time) FROM item_history(id) WHERE time < :ed6 ::date + interval '1 day')) > 0) OR id IN (SELECT DISTINCT item FROM all_transactions WHERE time > :sd7 AND time < :ed7 ::date + interval '1 day')) SELECT *, (starting_balance + purchase - use - sale - quantity) AS adjustment, starting_balance * unit_cost * :c8 AS starting_cost, purchase * unit_cost * :c9 AS purchase_cost, use * unit_cost * :c10 AS use_cost, sale * unit_cost * :c11 AS sale_cost, quantity * unit_cost * :c12 AS quantity_cost, (starting_balance + purchase - use - sale - quantity) * unit_cost * :c13 AS adjustment_cost, (SELECT sum(quantity)/:c6 FROM purchase WHERE item = id) AS total_purchase FROM q ORDER BY name";
  70. query.prepare(q);
  71. query.bind(":sd1", startDate);
  72. query.bind(":sd2", startDate);
  73. query.bind(":sd3", startDate);
  74. query.bind(":sd4", startDate);
  75. query.bind(":sd6", startDate);
  76. query.bind(":sd7", startDate);
  77. query.bind(":ed1", endDate);
  78. query.bind(":ed2", endDate);
  79. query.bind(":ed3", endDate);
  80. query.bind(":ed4", endDate);
  81. query.bind(":ed5", endDate);
  82. query.bind(":ed6", endDate);
  83. query.bind(":ed7", endDate);
  84. query.bind(":c1", conversion);
  85. query.bind(":c2", conversion);
  86. query.bind(":c3", conversion);
  87. query.bind(":c4", conversion);
  88. query.bind(":c5", conversion);
  89. query.bind(":c6", conversion);
  90. query.bind(":c8", conversion);
  91. query.bind(":c9", conversion);
  92. query.bind(":c10", conversion);
  93. query.bind(":c11", conversion);
  94. query.bind(":c12", conversion);
  95. query.bind(":c13", conversion);
  96. query.exec();
  97. output.writeStartElement("table");
  98. output.writeAttribute("rules", "groups");
  99. output.writeAttribute("cellpadding", "3px");
  100. output.writeStartElement("thead");
  101. output.writeStartElement("tr");
  102. output.writeTextElement("th", TTR("invchange", "ID")); // 0
  103. output.writeTextElement("th", TTR("invchange", "Coffee")); // 1
  104. output.writeTextElement("th", TTR("invchange", "Reference")); // 2
  105. output.writeTextElement("th", TTR("invchange", "Starting (") + unitText + ")"); // 3
  106. output.writeTextElement("th", TTR("invchange", "Cost")); // 10
  107. output.writeTextElement("th", TTR("invchange", "Purchase (") + unitText + ")"); // 4
  108. output.writeTextElement("th", TTR("invchange", "Cost")); // 11
  109. output.writeTextElement("th", TTR("invchange", "Use (") + unitText + ")"); // 5
  110. output.writeTextElement("th", TTR("invchange", "Cost")); // 12
  111. output.writeTextElement("th", TTR("invchange", "Sale (") + unitText + ")"); // 6
  112. output.writeTextElement("th", TTR("invchange", "Cost")); // 13
  113. output.writeTextElement("th", TTR("invchange", "Adjustment (") + unitText + ")"); // 9
  114. output.writeTextElement("th", TTR("invchange", "Cost")); // 15
  115. output.writeTextElement("th", TTR("invchange", "Ending (") + unitText + ")"); // 7
  116. output.writeTextElement("th", TTR("invchange", "Cost")); // 14
  117. output.writeEndElement();
  118. output.writeEndElement();
  119. output.writeStartElement("tbody");
  120. var sum3 = 0;
  121. var sum10 = 0;
  122. var sum4 = 0;
  123. var sum11 = 0;
  124. var sum5 = 0;
  125. var sum12 = 0;
  126. var sum6 = 0;
  127. var sum13 = 0;
  128. var sum9 = 0;
  129. var sum15 = 0;
  130. var sum7 = 0;
  131. var sum14 = 0;;
  132. while(query.next())
  133. {
  134. output.writeStartElement("tr");
  135. output.writeStartElement("td");
  136. output.writeStartElement("a");
  137. output.writeAttribute("href", "typica://script/" + query.value(0));
  138. output.writeTextElement("span", query.value(0)); //ID
  139. output.writeEndElement();
  140. output.writeEndElement();
  141. output.writeTextElement("td", query.value(1)); //Coffee
  142. output.writeTextElement("td", query.value(2)); //Reference
  143. output.writeStartElement("td"); //Starting Wt
  144. output.writeAttribute("title", (parseFloat(query.value(3))/parseFloat(query.value(16)) * 100).toFixed(0) + "%");
  145. output.writeCDATA(parseFloat(query.value(3)).toFixed(2));
  146. output.writeEndElement(); //End of Starting Wt.
  147. output.writeTextElement("td", parseFloat(query.value(10)).toFixed(2)); //Starting Cost
  148. output.writeStartElement("td"); //Purchase Wt
  149. output.writeAttribute("title", (parseFloat(query.value(4))/parseFloat(query.value(16)) * 100).toFixed(0) + "%");
  150. output.writeCDATA(parseFloat(query.value(4)).toFixed(2));
  151. output.writeEndElement(); //End of Purchase Wt
  152. output.writeTextElement("td", parseFloat(query.value(11)).toFixed(2)); //Purchase Cost
  153. output.writeStartElement("td"); //Use Wt
  154. output.writeAttribute("title", (parseFloat(query.value(5))/parseFloat(query.value(16)) * 100).toFixed(0) + "%");
  155. output.writeCDATA(parseFloat(query.value(5)).toFixed(2));
  156. output.writeEndElement(); //End of Use Wt
  157. output.writeTextElement("td", parseFloat(query.value(12)).toFixed(2)); //Use Cost
  158. output.writeStartElement("td"); //Sale Wt
  159. output.writeAttribute("title", (parseFloat(query.value(6))/parseFloat(query.value(16)) * 100).toFixed(0) + "%");
  160. output.writeCDATA(parseFloat(query.value(6)).toFixed(2));
  161. output.writeEndElement(); //End of Sale Wt
  162. output.writeTextElement("td", parseFloat(query.value(13)).toFixed(2)); //Sale Cost
  163. output.writeStartElement("td"); //Adjustment Wt
  164. output.writeAttribute("title", (parseFloat(query.value(9))/parseFloat(query.value(16)) * 100).toFixed(0) + "%");
  165. output.writeCDATA(parseFloat(query.value(9)).toFixed(2));
  166. output.writeEndElement(); //Adjustment Wt
  167. output.writeTextElement("td", parseFloat(query.value(15)).toFixed(2)); //Adjustment Cost
  168. output.writeStartElement("td"); //Ending Wt
  169. output.writeAttribute("title", (parseFloat(query.value(7))/parseFloat(query.value(16)) * 100).toFixed(0) + "%");
  170. output.writeCDATA(parseFloat(query.value(7)).toFixed(2));
  171. output.writeEndElement(); //End of Ending Wt
  172. output.writeTextElement("td", parseFloat(query.value(14)).toFixed(2)); //Ending Cost
  173. output.writeEndElement();
  174. sum3 += parseFloat(query.value(3));
  175. sum10 += parseFloat(query.value(10));
  176. sum4 += parseFloat(query.value(4));
  177. sum11 += parseFloat(query.value(11));
  178. sum5 += parseFloat(query.value(5));
  179. sum12 += parseFloat(query.value(12));
  180. sum6 += parseFloat(query.value(6));
  181. sum13 += parseFloat(query.value(13));
  182. sum9 += parseFloat(query.value(9));
  183. sum15 += parseFloat(query.value(15));
  184. sum7 += parseFloat(query.value(7));
  185. sum14 += parseFloat(query.value(14));
  186. }
  187. output.writeEndElement(); // tbody
  188. output.writeStartElement("tfoot");
  189. output.writeStartElement("tr");
  190. output.writeTextElement("td", "");
  191. output.writeTextElement("td", "");
  192. output.writeTextElement("th", TTR("invchange", "Total:"));
  193. output.writeTextElement("td", sum3.toFixed(2));
  194. output.writeTextElement("td", sum10.toFixed(2));
  195. output.writeTextElement("td", sum4.toFixed(2));
  196. output.writeTextElement("td", sum11.toFixed(2));
  197. output.writeTextElement("td", sum5.toFixed(2));
  198. output.writeTextElement("td", sum12.toFixed(2));
  199. output.writeTextElement("td", sum6.toFixed(2));
  200. output.writeTextElement("td", sum13.toFixed(2));
  201. output.writeTextElement("td", sum9.toFixed(2));
  202. output.writeTextElement("td", sum15.toFixed(2));
  203. output.writeTextElement("td", sum7.toFixed(2));
  204. output.writeTextElement("td", sum14.toFixed(2));
  205. output.writeEndElement(); // tr
  206. output.writeEndElement(); // tfoot
  207. output.writeEndElement();
  208. output.writeEndElement();
  209. output.writeEndElement();
  210. output.writeEndDocument();
  211. view.setContent(buffer);
  212. buffer.close();
  213. query = query.invalidate();
  214. }
  215. refresh();
  216. dateSelect.rangeUpdated.connect(function() {
  217. refresh();
  218. });
  219. view.scriptLinkClicked.connect(function(url) {
  220. var itemReport = createReport("itemtransactions.xml");
  221. var sIB = findChildObject(itemReport, 'item');
  222. sIB.currentIndex = sIB.findData(url);
  223. });
  224. ]]>
  225. </program>
  226. </window>