Typica is a free program for professional coffee roasters. https://typica.us
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

fypurchase.xml 7.7KB

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