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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 = TTR("fypurchase", "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(TTR("fypurchase", "Kg"));
  37. unitBox.addItem(TTR("fypurchase", "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", TTR("fypurchase", "Coffee Purchase Previous Years Comparison"));
  54. output.writeEndElement();
  55. output.writeStartElement("body");
  56. var cdt = new Date(Date.now());
  57. output.writeTextElement("p", cdt.toLocaleDateString(TTR("reports", "en-US")) + " " + cdt.toLocaleTimeString(TTR("reports", "en-US")));
  58. output.writeTextElement("h1", TTR("fypurchase", "Coffee Purchase Previous Years Comparison"));
  59. output.writeStartElement("table");
  60. output.writeAttribute("style", "page-break-after: auto; text-align: left");
  61. output.writeAttribute("rules", "groups");
  62. output.writeAttribute("cellpadding", "3px");
  63. output.writeStartElement("thead");
  64. output.writeStartElement("tr");
  65. output.writeEmptyElement("th");
  66. output.writeTextElement("th", TTR("fypurchase", "Sacks Purchased"));
  67. switch(unitBox.currentIndex) {
  68. case 0:
  69. output.writeTextElement("th", TTR("fypurchase", "Kg Purchased"));
  70. break;
  71. case 1:
  72. output.writeTextElement("th", TTR("fypurchase", "Lb Purchased"));
  73. break;
  74. }
  75. output.writeTextElement("th", TTR("fypurchase", "Cost"));
  76. switch(unitBox.currentIndex) {
  77. case 0:
  78. output.writeTextElement("th", TTR("fypurchase", "Cost per Kg"));
  79. break;
  80. case 1:
  81. output.writeTextElement("th", TTR("fypurchase", "Cost per Lb"));
  82. break;
  83. }
  84. output.writeEndElement(); //tr
  85. output.writeEndElement(); //thead
  86. output.writeStartElement("tbody");
  87. var sacktotal = 0;
  88. var unittotal = 0;
  89. var costtotal = 0;
  90. var query = new QSqlQuery();
  91. var dateRange = dateSelect.currentRange();
  92. var startYear = Number(dateRange[0].substr(0, 4));
  93. var endYear = Number(dateRange[dateRange.length - 1].substr(0, 4));
  94. for(var i = startYear; i <= endYear; i++)
  95. {
  96. output.writeStartElement("tr");
  97. output.writeAttribute("id", "y"+i);
  98. 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'";
  99. query.prepare(q);
  100. query.bind(":conversion", unitBox.currentIndex == 0 ? 2.2 : 1);
  101. query.exec();
  102. query.next();
  103. output.writeStartElement("th");
  104. output.writeStartElement("a");
  105. output.writeAttribute("href", "typica://script/y" + i);
  106. output.writeCharacters(i);
  107. output.writeEndElement(); //a
  108. output.writeEndElement(); //th
  109. output.writeTextElement("td", query.value(0));
  110. output.writeTextElement("td", query.value(1));
  111. output.writeTextElement("td", Number(query.value(2)).toFixed(2));
  112. sacktotal += Number(query.value(0));
  113. unittotal += Number(query.value(1));
  114. costtotal += Number(query.value(2));
  115. output.writeTextElement("td", (Number(query.value(2))/Number(query.value(1))).toFixed(2));
  116. output.writeEndElement(); //tr
  117. }
  118. query = query.invalidate();
  119. output.writeEndElement(); //tbody
  120. output.writeStartElement("tfoot");
  121. output.writeTextElement("th", TTR("fypurchase", "Totals:"));
  122. output.writeTextElement("td", sacktotal.toFixed(2));
  123. output.writeTextElement("td", unittotal.toFixed(2));
  124. output.writeTextElement("td", costtotal.toFixed(2));
  125. output.writeEndElement(); //tfoot
  126. output.writeEndElement(); //table
  127. output.writeEndElement(); //body
  128. output.writeEndElement(); //html
  129. output.writeEndDocument();
  130. view.setContent(buffer);
  131. buffer.close();
  132. }
  133. refresh();
  134. /* Update report as needed. */
  135. dateSelect.rangeUpdated.connect(function() {
  136. refresh();
  137. });
  138. /* Expand year data */
  139. var expandedYears = new Array();
  140. var expandYear = function(url) {
  141. if(expandedYears.indexOf(url) == -1) {
  142. expandedYears.push(url);
  143. var element = new WebElement(view.findFirstElement("#" + url));
  144. var year = url.slice(1,url.length);
  145. var details = '<tr><td /><td colspan="3"><table><tr><th>Id</th><th>' + TTR("fypurchase", "Invoice") +
  146. '</th><th>' + TTR("fypurchase", "Vendor") +
  147. '</th><th>' + TTR("fypurchase", "Cost") +
  148. '</th></tr>';
  149. 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'";
  150. var query = new QSqlQuery();
  151. query.exec(q);
  152. while(query.next())
  153. {
  154. details += '<tr><td><a href="typica://script/i' + query.value(0) + '">' + query.value(0) + '</a></td>';
  155. details += '<td>' + query.value(1) + '</td>';
  156. details += '<td>' + query.value(2) + '</td>';
  157. details += '<td>' + Number(query.value(3)).toFixed(2) + '</td></tr>';
  158. }
  159. query = query.invalidate();
  160. details += '</table></td></tr>'
  161. element.appendOutside(details);
  162. }
  163. };
  164. /* Open invoices */
  165. var openInvoice = function(url) {
  166. var arg = url.slice(1, url.length);
  167. var info = createWindow("invoiceinfo");
  168. info.setInvoiceID(arg);
  169. var query = new QSqlQuery();
  170. query.exec("SELECT time, invoice, vendor FROM invoices WHERE id = " + arg);
  171. query.next();
  172. var timefield = findChildObject(info, 'date');
  173. timefield.text = query.value(0);
  174. var vendorfield = findChildObject(info, 'vendor');
  175. vendorfield.text = query.value(2);
  176. var invoicefield = findChildObject(info, 'invoice');
  177. invoicefield.text = query.value(1);
  178. var itemtable = findChildObject(info, 'itemtable');
  179. 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");
  180. query = query.invalidate();
  181. };
  182. /* Dispatch script link clicks */
  183. view.scriptLinkClicked.connect(function(url) {
  184. var linkType = url[0];
  185. switch(linkType) {
  186. case 'y':
  187. expandYear(url);
  188. break
  189. case 'i':
  190. openInvoice(url);
  191. break;
  192. }
  193. });
  194. ]]>
  195. </program>
  196. </window>