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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 = TTR("item_transactions", "Typica - Item Transactions");
  22. var itemBox = findChildObject(this, 'item');
  23. var unitBox = findChildObject(this, 'unit');
  24. unitBox.addItem(TTR("item_transactions", "Kg"));
  25. unitBox.addItem(TTR("item_transactions", "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", TTR("item_transactions", "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", TTR("item_transactions", "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", TTR("item_transactions", "Reference: "));
  80. output.writeTextElement("span", query.value(0));
  81. output.writeEndElement(); // td
  82. output.writeStartElement("td");
  83. output.writeTextElement("strong", TTR("item_transactions", "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", TTR("item_transactions", "Origin: "));
  94. output.writeTextElement("span", query.value(0));
  95. output.writeEndElement(); // td
  96. output.writeStartElement("td");
  97. output.writeTextElement("strong", TTR("item_transactions", "Region: "));
  98. output.writeTextElement("span", query.value(1));
  99. output.writeEndElement(); // td
  100. output.writeStartElement("td");
  101. output.writeTextElement("strong", TTR("item_transactions", "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", TTR("item_transactions", "Grade: "));
  108. output.writeTextElement("span", query.value(3));
  109. output.writeEndElement(); // td
  110. output.writeStartElement("td");
  111. output.writeTextElement("strong", TTR("item_transactions", "Milling: "));
  112. output.writeTextElement("span", query.value(4));
  113. output.writeEndElement(); // td
  114. output.writeStartElement("td");
  115. output.writeTextElement("strong", TTR("item_transactions", "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", TTR("item_transactions", "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", TTR("item_transactions", "Date"));
  190. output.writeTextElement("th", TTR("item_transactions", "Type"));
  191. output.writeTextElement("th", TTR("item_transactions", "Quantity"));
  192. output.writeTextElement("th", TTR("item_transactions", "Balance"));
  193. output.writeTextElement("th", TTR("item_transactions", "Record"));
  194. output.writeEndElement(); // tr
  195. var prev_balance = "0";
  196. var prev_prec = 0;
  197. var cur_prec = 0;
  198. var max_prec = 3;
  199. while(query.next()) {
  200. output.writeStartElement("tr");
  201. output.writeAttribute("class", query.value(1));
  202. output.writeTextElement("td", query.value(0));
  203. output.writeTextElement("td", query.value(1));
  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. var prec = (prec > max_prec ? max_prec : prec);
  218. if(query.value(1) == "INVENTORY") {
  219. output.writeTextElement("td", (Number(query.value(2)) - Number(prev_balance)).toFixed(prec));
  220. } else {
  221. output.writeTextElement("td", (Number(query.value(2)).toFixed(prec)));
  222. }
  223. output.writeTextElement("td", (Number(query.value(3)).toFixed(prec)));
  224. prev_balance = query.value(3);
  225. if(query.value(1) == "PURCHASE") {
  226. output.writeStartElement("td");
  227. output.writeStartElement("a");
  228. output.writeAttribute("href", "typica://script/i" + query.value(5));
  229. output.writeCDATA(query.value(6) + " (" + query.value(5) + ")");
  230. output.writeEndElement();
  231. output.writeEndElement();
  232. } else if(query.value(1) == "USE") {
  233. output.writeStartElement("td");
  234. output.writeStartElement("a");
  235. output.writeAttribute("href", "typica://script/p" + query.value(4).slice(1,-1));
  236. output.writeCDATA(query.value(7) + " " + query.value(4));
  237. output.writeEndElement();
  238. output.writeEndElement();
  239. } else if(query.value(1) == "LOSS") {
  240. output.writeTextElement("td", query.value(9));
  241. } else if(query.value(1) == "SALE") {
  242. output.writeTextElement("td", query.value(8));
  243. } else {
  244. output.writeTextElement("td", "");
  245. }
  246. output.writeEndElement(); // tr
  247. }
  248. output.writeEndElement(); // table
  249. /* Put the rest of the report here. No sense running queries if
  250. the item doesn't exist. */
  251. } else {
  252. /* Close tags if item data not found. */
  253. output.writeEndElement(); // tr
  254. output.writeEndElement(); // table
  255. }
  256. output.writeEndElement(); // body
  257. output.writeEndElement(); // html
  258. output.writeEndDocument();
  259. view.setContent(buffer);
  260. buffer.close();
  261. query = query.invalidate();
  262. }
  263. if(itemBox.currentData() > 0) {
  264. refresh();
  265. }
  266. /* Open invoices */
  267. var openInvoice = function(url) {
  268. var arg = url.slice(1, url.length);
  269. var info = createWindow("invoiceinfo");
  270. info.setInvoiceID(arg);
  271. var query = new QSqlQuery();
  272. query.exec("SELECT time, invoice, vendor FROM invoices WHERE id = " + arg);
  273. query.next();
  274. var timefield = findChildObject(info, 'date');
  275. timefield.text = query.value(0);
  276. var vendorfield = findChildObject(info, 'vendor');
  277. vendorfield.text = query.value(2);
  278. var invoicefield = findChildObject(info, 'invoice');
  279. invoicefield.text = query.value(1);
  280. var itemtable = findChildObject(info, 'itemtable');
  281. 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");
  282. query = query.invalidate();
  283. };
  284. /* Open batch data */
  285. var openProfile = function(url) {
  286. var arg = url.slice(1, url.length);
  287. var details = createWindow("batchDetails");
  288. var fakeTable = new Object;
  289. fakeTable.holding = new Array(7);
  290. fakeTable.data = function(r, c) {
  291. return this.holding[c];
  292. };
  293. var query = new QSqlQuery();
  294. 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 + "}'");
  295. query.next();
  296. for(var i = 0; i < 8; i++) {
  297. fakeTable.holding[i] = query.value(i);
  298. }
  299. query = query.invalidate();
  300. details.loadData(fakeTable, 0);
  301. };
  302. view.scriptLinkClicked.connect(function(url) {
  303. var linkType = url[0];
  304. switch(linkType) {
  305. case 'i':
  306. openInvoice(url);
  307. break;
  308. case 'p':
  309. openProfile(url);
  310. break;
  311. }
  312. });
  313. ]]>
  314. </program>
  315. </window>