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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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))), customer, reason 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 if(query.value(1) == "LOSS") {
  238. output.writeTextElement("td", query.value(9));
  239. } else if(query.value(1) == "SALE") {
  240. output.writeTextElement("td", query.value(8));
  241. } else {
  242. output.writeTextElement("td", "");
  243. }
  244. output.writeEndElement(); // tr
  245. }
  246. output.writeEndElement(); // table
  247. /* Put the rest of the report here. No sense running queries if
  248. the item doesn't exist. */
  249. } else {
  250. /* Close tags if item data not found. */
  251. output.writeEndElement(); // tr
  252. output.writeEndElement(); // table
  253. }
  254. output.writeEndElement(); // body
  255. output.writeEndElement(); // html
  256. output.writeEndDocument();
  257. view.setContent(buffer);
  258. buffer.close();
  259. query = query.invalidate();
  260. }
  261. if(itemBox.currentData() > 0) {
  262. refresh();
  263. }
  264. /* Open invoices */
  265. var openInvoice = function(url) {
  266. var arg = url.slice(1, url.length);
  267. var info = createWindow("invoiceinfo");
  268. info.setInvoiceID(arg);
  269. var query = new QSqlQuery();
  270. query.exec("SELECT time, invoice, vendor FROM invoices WHERE id = " + arg);
  271. query.next();
  272. var timefield = findChildObject(info, 'date');
  273. timefield.text = query.value(0);
  274. var vendorfield = findChildObject(info, 'vendor');
  275. vendorfield.text = query.value(2);
  276. var invoicefield = findChildObject(info, 'invoice');
  277. invoicefield.text = query.value(1);
  278. var itemtable = findChildObject(info, 'itemtable');
  279. 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");
  280. query = query.invalidate();
  281. };
  282. /* Open batch data */
  283. var openProfile = function(url) {
  284. var arg = url.slice(1, url.length);
  285. var details = createWindow("batchDetails");
  286. var fakeTable = new Object;
  287. fakeTable.holding = new Array(7);
  288. fakeTable.data = function(r, c) {
  289. return this.holding[c];
  290. };
  291. var query = new QSqlQuery();
  292. 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 + "}'");
  293. query.next();
  294. for(var i = 0; i < 8; i++) {
  295. fakeTable.holding[i] = query.value(i);
  296. }
  297. query = query.invalidate();
  298. details.loadData(fakeTable, 0);
  299. };
  300. view.scriptLinkClicked.connect(function(url) {
  301. var linkType = url[0];
  302. switch(linkType) {
  303. case 'i':
  304. openInvoice(url);
  305. break;
  306. case 'p':
  307. openProfile(url);
  308. break;
  309. }
  310. });
  311. ]]>
  312. </program>
  313. </window>