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

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