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

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