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.

fypurchase.xml 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <window id="fypurchase">
  2. <reporttitle>Purchase:->Coffee Purchase Previous Years Comparison</reporttitle>
  3. <layout type="vertical">
  4. <layout type="horizontal">
  5. <daterange id = "dates" initial="23" /><!--Lifetime-->
  6. <label>Weight Unit:</label>
  7. <sqldrop id="unit" />
  8. <stretch />
  9. </layout>
  10. <webview id="report" />
  11. </layout>
  12. <menu name="File">
  13. <item id="print" shortcut="Ctrl+P">Print...</item>
  14. </menu>
  15. <program>
  16. <![CDATA[
  17. this.windowTitle = "Typica - Coffee Purchase Previous Years Comparison";
  18. /* Set Lifetime range. */
  19. var dateSelect = findChildObject(this, 'dates');
  20. var query = new QSqlQuery();
  21. query.exec("SELECT concat(EXTRACT(YEAR FROM time::date), '-01-01') FROM purchase WHERE time = (SELECT min(time) FROM purchase) UNION SELECT concat(EXTRACT(YEAR FROM 'now'::date), '-12-31') ORDER BY concat ASC");
  22. query.next();
  23. var lifetimeStartDate = query.value(0);
  24. query.next();
  25. var lifetimeEndDate = query.value(0);
  26. dateSelect.setLifetimeRange(lifetimeStartDate, lifetimeEndDate);
  27. query = query.invalidate();
  28. /* Enable printing */
  29. var view = findChildObject(this, 'report');
  30. var printMenu = findChildObject(this, 'print');
  31. printMenu.triggered.connect(function() {
  32. view.print();
  33. });
  34. /* Add units to unit selector and enable functionality */
  35. var unitBox = findChildObject(this, 'unit');
  36. unitBox.addItem("Kg");
  37. unitBox.addItem("Lb");
  38. unitBox.currentIndex = QSettings.value("script/report_unit", 1);
  39. unitBox['currentIndexChanged(int)'].connect(function() {
  40. QSettings.setValue("script/report_unit", unitBox.currentIndex);
  41. refresh();
  42. });
  43. /* Generate report */
  44. function refresh() {
  45. var buffer = new QBuffer;
  46. buffer.open(3);
  47. var output = new XmlWriter(buffer);
  48. output.writeStartDocument("1.0");
  49. 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">');
  50. output.writeStartElement("html");
  51. output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
  52. output.writeStartElement("head");
  53. output.writeTextElement("title", "Coffee Purchase Previous Years Comparison");
  54. output.writeEndElement();
  55. output.writeStartElement("body");
  56. output.writeTextElement("h1", "Coffee Purchase Previous Years Comparison");
  57. output.writeStartElement("table");
  58. output.writeAttribute("style", "page-break-after: auto; text-align: left");
  59. output.writeAttribute("rules", "groups");
  60. output.writeAttribute("cellpadding", "3px");
  61. output.writeStartElement("thead");
  62. output.writeStartElement("tr");
  63. output.writeEmptyElement("th");
  64. output.writeTextElement("th", "Sacks Purchased");
  65. switch(unitBox.currentIndex) {
  66. case 0:
  67. output.writeTextElement("th", "Kg Purchased");
  68. break;
  69. case 1:
  70. output.writeTextElement("th", "Lb Purchased");
  71. break;
  72. }
  73. output.writeTextElement("th", "Cost");
  74. switch(unitBox.currentIndex) {
  75. case 0:
  76. output.writeTextElement("th", "Cost per Kg");
  77. break;
  78. case 1:
  79. output.writeTextElement("th", "Cost per Lb");
  80. break;
  81. }
  82. output.writeEndElement(); //tr
  83. output.writeEndElement(); //thead
  84. output.writeStartElement("tbody");
  85. var sacktotal = 0;
  86. var unittotal = 0;
  87. var costtotal = 0;
  88. var query = new QSqlQuery();
  89. var dateRange = dateSelect.currentRange();
  90. var startYear = Number(dateRange[0].substr(0, 4));
  91. var endYear = Number(dateRange[dateRange.length - 1].substr(0, 4));
  92. for(var i = startYear; i <= endYear; i++)
  93. {
  94. output.writeStartElement("tr");
  95. output.writeAttribute("id", "y"+i);
  96. var q = "SELECT sum(quantity/(SELECT conversion FROM lb_bag_conversion WHERE item = purchase.item)), (sum(quantity) / :conversion)::numeric(12,2), sum(cost*quantity)::numeric(12,2) FROM purchase WHERE time >= '" + i + "-01-01' AND time < '" + (i+1) + "-01-01'";
  97. query.prepare(q);
  98. query.bind(":conversion", unitBox.currentIndex == 0 ? 2.2 : 1);
  99. query.exec();
  100. query.next();
  101. output.writeStartElement("th");
  102. output.writeStartElement("a");
  103. output.writeAttribute("href", "typica://script/y" + i);
  104. output.writeCharacters(i);
  105. output.writeEndElement(); //a
  106. output.writeEndElement(); //th
  107. output.writeTextElement("td", query.value(0));
  108. output.writeTextElement("td", query.value(1));
  109. output.writeTextElement("td", Number(query.value(2)).toFixed(2));
  110. sacktotal += Number(query.value(0));
  111. unittotal += Number(query.value(1));
  112. costtotal += Number(query.value(2));
  113. output.writeTextElement("td", (Number(query.value(2))/Number(query.value(1))).toFixed(2));
  114. output.writeEndElement(); //tr
  115. }
  116. query = query.invalidate();
  117. output.writeEndElement(); //tbody
  118. output.writeStartElement("tfoot");
  119. output.writeTextElement("th", "Totals:");
  120. output.writeTextElement("td", sacktotal.toFixed(2));
  121. output.writeTextElement("td", unittotal.toFixed(2));
  122. output.writeTextElement("td", costtotal.toFixed(2));
  123. output.writeEndElement(); //tfoot
  124. output.writeEndElement(); //table
  125. output.writeEndElement(); //body
  126. output.writeEndElement(); //html
  127. output.writeEndDocument();
  128. view.setContent(buffer);
  129. buffer.close();
  130. }
  131. refresh();
  132. /* Update report as needed. */
  133. dateSelect.rangeUpdated.connect(function() {
  134. refresh();
  135. });
  136. /* Expand year data */
  137. var expandedYears = new Array();
  138. var expandYear = function(url) {
  139. if(expandedYears.indexOf(url) == -1) {
  140. expandedYears.push(url);
  141. var element = new WebElement(view.findFirstElement("#" + url));
  142. var year = url.slice(1,url.length);
  143. var details = '<tr><td /><td colspan="3"><table><tr><th>Id</th><th>Invoice</th><th>Vendor</th><th>Cost</th></tr>';
  144. q = "SELECT id, invoice, vendor, (SELECT sum(cost) FROM invoice_items WHERE invoice_id = id) FROM invoices WHERE time >= '" + year + "-01-01' AND time <= '" + year + "-12-31'";
  145. var query = new QSqlQuery();
  146. query.exec(q);
  147. while(query.next())
  148. {
  149. details += '<tr><td><a href="typica://script/i' + query.value(0) + '">' + query.value(0) + '</a></td>';
  150. details += '<td>' + query.value(1) + '</td>';
  151. details += '<td>' + query.value(2) + '</td>';
  152. details += '<td>' + Number(query.value(3)).toFixed(2) + '</td></tr>';
  153. }
  154. query = query.invalidate();
  155. details += '</table></td></tr>'
  156. element.appendOutside(details);
  157. }
  158. };
  159. /* Open invoices */
  160. var openInvoice = function(url) {
  161. var arg = url.slice(1, url.length);
  162. var info = createWindow("invoiceinfo");
  163. info.setInvoiceID(arg);
  164. var query = new QSqlQuery();
  165. query.exec("SELECT time, invoice, vendor FROM invoices WHERE id = " + arg);
  166. query.next();
  167. var timefield = findChildObject(info, 'date');
  168. timefield.text = query.value(0);
  169. var vendorfield = findChildObject(info, 'vendor');
  170. vendorfield.text = query.value(2);
  171. var invoicefield = findChildObject(info, 'invoice');
  172. invoicefield.text = query.value(1);
  173. var itemtable = findChildObject(info, 'itemtable');
  174. 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");
  175. query = query.invalidate();
  176. };
  177. /* Dispatch script link clicks */
  178. view.scriptLinkClicked.connect(function(url) {
  179. var linkType = url[0];
  180. switch(linkType) {
  181. case 'y':
  182. expandYear(url);
  183. break
  184. case 'i':
  185. openInvoice(url);
  186. break;
  187. }
  188. });
  189. ]]>
  190. </program>
  191. </window>