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

fypurchase.xml 9.1KB

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