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.

itemtransactions.xml 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <window id="item_transactions">
  2. <reporttitle>Inventory:->Item Transactions</reporttitle>
  3. <layout type="vertical">
  4. <layout type="horizontal">
  5. <label>Item:</label>
  6. <sqldrop id="item" data="0" display="1" showdata="true">
  7. <null />
  8. <query>SELECT id, name FROM items WHERE category = 'Coffee: Unroasted' ORDER BY name</query>
  9. </sqldrop>
  10. <label>Weight Unit:</label>
  11. <sqldrop id="unit" />
  12. <stretch />
  13. </layout>
  14. <webview id="report" />
  15. </layout>
  16. <menu name="File">
  17. <item id="print" shortcut="Ctrl+P">Print</item>
  18. </menu>
  19. <program>
  20. <![CDATA[
  21. this.windowTitle = "Typica - Item Transactions";
  22. var itemBox = findChildObject(this, 'item');
  23. var unitBox = findChildObject(this, 'unit');
  24. unitBox.addItem("Kg");
  25. unitBox.addItem("Lb");
  26. unitBox.currentIndex = QSettings.value("script/report_unit", 1);
  27. unitBox['currentIndexChanged(int)'].connect(function() {
  28. QSettings.setValue("script/report_unit", unitBox.currentIndex);
  29. refresh();
  30. });
  31. var view = findChildObject(this, 'report');
  32. var printMenu = findChildObject(this, 'print');
  33. printMenu.triggered.connect(function() {
  34. view.print();
  35. });
  36. itemBox['currentIndexChanged(int)'].connect(function() {
  37. refresh();
  38. });
  39. function refresh() {
  40. var buffer = new QBuffer;
  41. buffer.open(3);
  42. var output = new XmlWriter(buffer);
  43. output.writeStartDocument("1.0");
  44. 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">');
  45. output.writeStartElement("html");
  46. output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
  47. output.writeStartElement("head");
  48. output.writeTextElement("title", "Item Transactions");
  49. output.writeStartElement("script");
  50. var scriptFile = new QFile(QSettings.value("config") + "/Scripts/d3.min.js");
  51. scriptFile.open(1);
  52. output.writeCDATA(scriptFile.readToString());
  53. scriptFile.close();
  54. output.writeEndElement();
  55. output.writeStartElement("style");
  56. output.writeAttribute("type", "text/css");
  57. output.writeCDATA("tr.PURCHASE {background-color: #77FF77}");
  58. output.writeCDATA("tr.USE {background-color: #FFFFFF}");
  59. output.writeCDATA("tr.INVENTORY {background-color: #7777FF}");
  60. output.writeCDATA("tr.SALE {background-color: #FF77FF}");
  61. output.writeCDATA("tr.LOSS {background-color: #FF7777}");
  62. output.writeCDATA("tr.MAKE {background-color: #FFFF77}");
  63. output.writeEndElement(); // style
  64. output.writeEndElement();
  65. output.writeStartElement("body");
  66. output.writeTextElement("h1", "Item Transactions:");
  67. output.writeStartElement("table");
  68. output.writeStartElement("tr");
  69. output.writeStartElement("td");
  70. output.writeTextElement("strong", "Item: ")
  71. output.writeTextElement("span", itemBox.currentText);
  72. output.writeEndElement(); // td
  73. var query = new QSqlQuery();
  74. query.prepare("SELECT reference, category FROM items WHERE id = :item");
  75. query.bind(":item", itemBox.currentData());
  76. query.exec();
  77. if(query.next()) {
  78. output.writeStartElement("td");
  79. output.writeTextElement("strong", "Reference: ");
  80. output.writeTextElement("span", query.value(0));
  81. output.writeEndElement(); // td
  82. output.writeStartElement("td");
  83. output.writeTextElement("strong", "Category: ");
  84. output.writeTextElement("span", query.value(1));
  85. output.writeEndElement(); //td
  86. output.writeEndElement(); //tr
  87. query.prepare("SELECT origin, region, producer, grade, milling, drying FROM coffees WHERE id = :item");
  88. query.bind(":item", itemBox.currentData());
  89. query.exec();
  90. if(query.next()) {
  91. output.writeStartElement("tr");
  92. output.writeStartElement("td");
  93. output.writeTextElement("strong", "Origin: ");
  94. output.writeTextElement("span", query.value(0));
  95. output.writeEndElement(); // td
  96. output.writeStartElement("td");
  97. output.writeTextElement("strong", "Region: ");
  98. output.writeTextElement("span", query.value(1));
  99. output.writeEndElement(); // td
  100. output.writeStartElement("td");
  101. output.writeTextElement("strong", "Producer: ");
  102. output.writeTextElement("span", query.value(2));
  103. output.writeEndElement(); // td
  104. output.writeEndElement(); // tr
  105. output.writeStartElement("tr");
  106. output.writeStartElement("td");
  107. output.writeTextElement("strong", "Grade: ");
  108. output.writeTextElement("span", query.value(3));
  109. output.writeEndElement(); // td
  110. output.writeStartElement("td");
  111. output.writeTextElement("strong", "Milling: ");
  112. output.writeTextElement("span", query.value(4));
  113. output.writeEndElement(); // td
  114. output.writeStartElement("td");
  115. output.writeTextElement("strong", "Drying: ");
  116. output.writeTextElement("span", query.value(5));
  117. output.writeEndElement(); // td
  118. output.writeEndElement(); // tr
  119. query.prepare("SELECT decaf_method FROM decaf_coffees WHERE id = :item");
  120. query.bind(":item", itemBox.currentData());
  121. query.exec();
  122. if(query.next()) {
  123. output.writeStartElement("tr");
  124. output.writeStartElement("td");
  125. output.writeAttribute("colspan", "3");
  126. output.writeTextElement("strong", "Decaffeination Method: ");
  127. output.writeTextElement("span", query.value(0));
  128. output.writeEndElement(); // td
  129. output.writeEndElement(); // tr
  130. }
  131. }
  132. output.writeEndElement() // table
  133. output.writeStartElement("div");
  134. output.writeAttribute("id", "chart");
  135. output.writeEndElement();
  136. query.prepare("WITH q AS (SELECT roasted_id, unroasted_id, unroasted_quantity, unroasted_total_quantity, roasted_quantity, generate_subscripts(unroasted_quantity, 1) AS s FROM roasting_log) SELECT (SELECT name FROM items WHERE id = roasted_id) AS name, roasted_id, SUM(unroasted_quantity[s]) AS total, COUNT(unroasted_quantity[s]), SUM((unroasted_quantity[s]/unroasted_total_quantity)*roasted_quantity)::numeric(12,3) AS roast_proportion FROM q WHERE unroasted_id[s] = :item1 GROUP BY roasted_id UNION SELECT 'Green Sales', NULL, SUM(quantity), COUNT(1), NULL FROM sale WHERE item = :item2 UNION SELECT 'Inventory Adjustment', NULL, ((SELECT SUM(quantity) FROM purchase WHERE item = :item3) - (SELECT quantity FROM items WHERE id = :item4) - (SELECT SUM(quantity) FROM all_transactions WHERE type != 'PURCHASE' AND type != 'INVENTORY' AND item = :item5)), (SELECT COUNT(1) FROM inventory WHERE item = :item6), NULL UNION SELECT 'Loss', NULL, SUM(quantity), COUNT(1), NULL FROM loss WHERE item = :item7 UNION SELECT 'Current Inventory', NULL, (SELECT quantity FROM items WHERE id = :item8), NULL, NULL ORDER BY total DESC");
  137. query.bind(":item1", itemBox.currentData());
  138. query.bind(":item2", itemBox.currentData());
  139. query.bind(":item3", itemBox.currentData());
  140. query.bind(":item4", itemBox.currentData());
  141. query.bind(":item5", itemBox.currentData());
  142. query.bind(":item6", itemBox.currentData());
  143. query.bind(":item7", itemBox.currentData());
  144. query.bind(":item8", itemBox.currentData());
  145. query.exec();
  146. var chartData = "var data = [";
  147. var roastedCoffeeLines = "";
  148. var adjustmentLines = "";
  149. var currentInventoryLine = "";
  150. var conversion = 1;
  151. if(unitBox.currentIndex == 0) {
  152. conversion = 2.2;
  153. }
  154. while(query.next()) {
  155. if(Number(query.value(1)) > 0) {
  156. roastedCoffeeLines += "['" + query.value(0).replace(/\'/g, "\\x27") + "'," + query.value(2) / conversion + "," + query.value(3) + "," + query.value(4) / conversion + "],";
  157. } else if (query.value(0) == "Current Inventory") {
  158. currentInventoryLine = "['Current Inventory'," + query.value(2) / conversion + "," + query.value(3) + "," + query.value(4) / conversion + "]";
  159. } else {
  160. if(Number(query.value(3)) > 0) {
  161. adjustmentLines += "['" + query.value(0) + "'," + query.value(2) / conversion + "," + query.value(3) + "," + query.value(4) / conversion + "],";
  162. }
  163. }
  164. }
  165. chartData = chartData + roastedCoffeeLines + adjustmentLines + currentInventoryLine + "];";
  166. output.writeTextElement("script", chartData);
  167. output.writeStartElement("script");
  168. scriptFile = new QFile(QSettings.value("config") + "/Scripts/greenusechart.js");
  169. scriptFile.open(1);
  170. output.writeCDATA(scriptFile.readToString());
  171. scriptFile.close();
  172. output.writeEndElement();
  173. query.prepare("SELECT time::date, type, quantity / :c1, balance / :c2, (SELECT files FROM roasting_log WHERE roasting_log.time = item_history.time AND item = ANY(unroasted_id)), (SELECT invoice_id FROM invoice_items WHERE item = item_id AND item_history.type = 'PURCHASE'), (SELECT vendor || ' ' || invoice FROM invoices WHERE id = (SELECT invoice_id FROM invoice_items WHERE item = item_id AND item_history.type = 'PURCHASE')), (SELECT name FROM items WHERE id = (SELECT roasted_id FROM roasting_log WHERE roasting_log.time = item_history.time AND item = ANY(unroasted_id))) FROM item_history(:item)");
  174. switch(unitBox.currentIndex)
  175. {
  176. case 0:
  177. query.bind(":c1", 2.2);
  178. query.bind(":c2", 2.2);
  179. break;
  180. case 1:
  181. query.bind(":c1", 1);
  182. query.bind(":c2", 1);
  183. break;
  184. }
  185. query.bind(":item", itemBox.currentData());
  186. query.exec();
  187. output.writeStartElement("table");
  188. output.writeStartElement("tr");
  189. output.writeTextElement("th", "Date");
  190. output.writeTextElement("th", "Type");
  191. output.writeTextElement("th", "Quantity");
  192. output.writeTextElement("th", "Balance");
  193. output.writeTextElement("th", "Record");
  194. output.writeEndElement(); // tr
  195. var prev_balance = "0";
  196. var prev_prec = 0;
  197. var cur_prec = 0;
  198. while(query.next()) {
  199. output.writeStartElement("tr");
  200. output.writeAttribute("class", query.value(1));
  201. output.writeTextElement("td", query.value(0));
  202. output.writeTextElement("td", query.value(1));
  203. if(query.value(1) == "INVENTORY") {
  204. var split = prev_balance.split('.');
  205. if(split.length > 1) {
  206. prev_prec = split[1].length;
  207. } else {
  208. prev_prec = 0;
  209. }
  210. split = query.value(2).split('.');
  211. if(split.length > 1) {
  212. cur_prec = split[1].length;
  213. } else {
  214. cur_prec = 0;
  215. }
  216. var prec = prev_prec > cur_prec ? prev_prec : cur_prec;
  217. output.writeTextElement("td", (Number(query.value(2)) - Number(prev_balance)).toFixed(prec));
  218. } else {
  219. output.writeTextElement("td", query.value(2));
  220. }
  221. output.writeTextElement("td", query.value(3));
  222. prev_balance = query.value(3);
  223. if(query.value(1) == "PURCHASE") {
  224. output.writeStartElement("td");
  225. output.writeStartElement("a");
  226. output.writeAttribute("href", "typica://script/i" + query.value(5));
  227. output.writeCDATA(query.value(6) + " (" + query.value(5) + ")");
  228. output.writeEndElement();
  229. output.writeEndElement();
  230. } else if(query.value(1) == "USE") {
  231. output.writeStartElement("td");
  232. output.writeStartElement("a");
  233. output.writeAttribute("href", "typica://script/p" + query.value(4).slice(1,-1));
  234. output.writeCDATA(query.value(7) + " " + query.value(4));
  235. output.writeEndElement();
  236. output.writeEndElement();
  237. } else {
  238. output.writeTextElement("td", "");
  239. }
  240. output.writeEndElement(); // tr
  241. }
  242. output.writeEndElement(); // table
  243. /* Put the rest of the report here. No sense running queries if
  244. the item doesn't exist. */
  245. } else {
  246. /* Close tags if item data not found. */
  247. output.writeEndElement(); // tr
  248. output.writeEndElement(); // table
  249. }
  250. output.writeEndElement(); // body
  251. output.writeEndElement(); // html
  252. output.writeEndDocument();
  253. view.setContent(buffer);
  254. buffer.close();
  255. query = query.invalidate();
  256. }
  257. if(itemBox.currentData() > 0) {
  258. refresh();
  259. }
  260. /* Open invoices */
  261. var openInvoice = function(url) {
  262. var arg = url.slice(1, url.length);
  263. var info = createWindow("invoiceinfo");
  264. info.setInvoiceID(arg);
  265. var query = new QSqlQuery();
  266. query.exec("SELECT time, invoice, vendor FROM invoices WHERE id = " + arg);
  267. query.next();
  268. var timefield = findChildObject(info, 'date');
  269. timefield.text = query.value(0);
  270. var vendorfield = findChildObject(info, 'vendor');
  271. vendorfield.text = query.value(2);
  272. var invoicefield = findChildObject(info, 'invoice');
  273. invoicefield.text = query.value(1);
  274. var itemtable = findChildObject(info, 'itemtable');
  275. itemtable.setQuery("SELECT record_type, item_id, description, (SELECT reference FROM items WHERE id = item_id) AS reference, (SELECT cost FROM purchase WHERE item = item_id) AS unit_cost, (SELECT quantity FROM purchase WHERE item = item_id) AS quantity, ((SELECT quantity FROM purchase WHERE item = item_id)/(SELECT conversion FROM lb_bag_conversion WHERE item = item_id))::numeric(12,2) AS sacks, cost FROM invoice_items WHERE invoice_id = " + arg + " AND record_type = 'PURCHASE' UNION SELECT record_type, NULL, description, NULL, NULL, NULL, NULL, cost FROM invoice_items WHERE invoice_id = " + arg + " AND record_type = 'FEE' ORDER BY item_id");
  276. query = query.invalidate();
  277. };
  278. /* Open batch data */
  279. var openProfile = function(url) {
  280. var arg = url.slice(1, url.length);
  281. var details = createWindow("batchDetails");
  282. var fakeTable = new Object;
  283. fakeTable.holding = new Array(7);
  284. fakeTable.data = function(r, c) {
  285. return this.holding[c];
  286. };
  287. var query = new QSqlQuery();
  288. query.exec("SELECT time, machine, (SELECT name FROM items WHERE id = roasted_id) AS name, unroasted_total_quantity AS green, roasted_quantity AS roasted, ((unroasted_total_quantity - roasted_quantity) / unroasted_total_quantity * 100::numeric)::numeric(12,2) AS weight_loss, duration, annotation FROM roasting_log WHERE files = '{" + arg + "}'");
  289. query.next();
  290. for(var i = 0; i < 8; i++) {
  291. fakeTable.holding[i] = query.value(i);
  292. }
  293. query = query.invalidate();
  294. details.loadData(fakeTable, 0);
  295. };
  296. view.scriptLinkClicked.connect(function(url) {
  297. var linkType = url[0];
  298. switch(linkType) {
  299. case 'i':
  300. openInvoice(url);
  301. break;
  302. case 'p':
  303. openProfile(url);
  304. break;
  305. }
  306. });
  307. ]]>
  308. </program>
  309. </window>