Typica is a free program for professional coffee roasters. https://typica.us
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

fypurchase.xml 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. output.writeTextElement("h1", TTR("fypurchase", "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", TTR("fypurchase", "Sacks Purchased"));
  65. switch(unitBox.currentIndex) {
  66. case 0:
  67. output.writeTextElement("th", TTR("fypurchase", "Kg Purchased"));
  68. break;
  69. case 1:
  70. output.writeTextElement("th", TTR("fypurchase", "Lb Purchased"));
  71. break;
  72. }
  73. output.writeTextElement("th", TTR("fypurchase", "Cost"));
  74. switch(unitBox.currentIndex) {
  75. case 0:
  76. output.writeTextElement("th", TTR("fypurchase", "Cost per Kg"));
  77. break;
  78. case 1:
  79. output.writeTextElement("th", TTR("fypurchase", "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", TTR("fypurchase", "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>' + TTR("fypurchase", "Invoice") +
  144. '</th><th>' + TTR("fypurchase", "Vendor") +
  145. '</th><th>' + TTR("fypurchase", "Cost") +
  146. '</th></tr>';
  147. 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'";
  148. var query = new QSqlQuery();
  149. query.exec(q);
  150. while(query.next())
  151. {
  152. details += '<tr><td><a href="typica://script/i' + query.value(0) + '">' + query.value(0) + '</a></td>';
  153. details += '<td>' + query.value(1) + '</td>';
  154. details += '<td>' + query.value(2) + '</td>';
  155. details += '<td>' + Number(query.value(3)).toFixed(2) + '</td></tr>';
  156. }
  157. query = query.invalidate();
  158. details += '</table></td></tr>'
  159. element.appendOutside(details);
  160. }
  161. };
  162. /* Open invoices */
  163. var openInvoice = function(url) {
  164. var arg = url.slice(1, url.length);
  165. var info = createWindow("invoiceinfo");
  166. info.setInvoiceID(arg);
  167. var query = new QSqlQuery();
  168. query.exec("SELECT time, invoice, vendor FROM invoices WHERE id = " + arg);
  169. query.next();
  170. var timefield = findChildObject(info, 'date');
  171. timefield.text = query.value(0);
  172. var vendorfield = findChildObject(info, 'vendor');
  173. vendorfield.text = query.value(2);
  174. var invoicefield = findChildObject(info, 'invoice');
  175. invoicefield.text = query.value(1);
  176. var itemtable = findChildObject(info, 'itemtable');
  177. 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");
  178. query = query.invalidate();
  179. };
  180. /* Dispatch script link clicks */
  181. view.scriptLinkClicked.connect(function(url) {
  182. var linkType = url[0];
  183. switch(linkType) {
  184. case 'y':
  185. expandYear(url);
  186. break
  187. case 'i':
  188. openInvoice(url);
  189. break;
  190. }
  191. });
  192. ]]>
  193. </program>
  194. </window>