Typica is a free program for professional coffee roasters. https://typica.us
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

fypurchase.xml 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. var notifier = Application.subscribe("purchasechange");
  135. notifier.notify.connect(function() {
  136. refresh();
  137. });
  138. /* Update report as needed. */
  139. dateSelect.rangeUpdated.connect(function() {
  140. refresh();
  141. });
  142. /* Expand year data */
  143. var expandedYears = new Array();
  144. var expandYear = function(url) {
  145. if(expandedYears.indexOf(url) == -1) {
  146. expandedYears.push(url);
  147. var element = new WebElement(view.findFirstElement("#" + url));
  148. var year = url.slice(1,url.length);
  149. var details = '<tr><td /><td colspan="3"><table><tr><th>Id</th><th>' + TTR("fypurchase", "Invoice") +
  150. '</th><th>' + TTR("fypurchase", "Vendor") +
  151. '</th><th>' + TTR("fypurchase", "Cost") +
  152. '</th></tr>';
  153. 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'";
  154. var query = new QSqlQuery();
  155. query.exec(q);
  156. while(query.next())
  157. {
  158. details += '<tr><td><a href="typica://script/i' + query.value(0) + '">' + query.value(0) + '</a></td>';
  159. details += '<td>' + query.value(1) + '</td>';
  160. details += '<td>' + query.value(2) + '</td>';
  161. details += '<td>' + Number(query.value(3)).toFixed(2) + '</td></tr>';
  162. }
  163. query = query.invalidate();
  164. details += '</table></td></tr>'
  165. element.appendOutside(details);
  166. }
  167. };
  168. /* Open invoices */
  169. var openInvoice = function(url) {
  170. var arg = url.slice(1, url.length);
  171. var info = createWindow("invoiceinfo");
  172. info.setInvoiceID(arg);
  173. var query = new QSqlQuery();
  174. query.exec("SELECT time, invoice, vendor FROM invoices WHERE id = " + arg);
  175. query.next();
  176. var timefield = findChildObject(info, 'date');
  177. timefield.text = query.value(0);
  178. var vendorfield = findChildObject(info, 'vendor');
  179. vendorfield.text = query.value(2);
  180. var invoicefield = findChildObject(info, 'invoice');
  181. invoicefield.text = query.value(1);
  182. var itemtable = findChildObject(info, 'itemtable');
  183. 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");
  184. query = query.invalidate();
  185. };
  186. /* Dispatch script link clicks */
  187. view.scriptLinkClicked.connect(function(url) {
  188. var linkType = url[0];
  189. switch(linkType) {
  190. case 'y':
  191. expandYear(url);
  192. break
  193. case 'i':
  194. openInvoice(url);
  195. break;
  196. }
  197. });
  198. ]]>
  199. </program>
  200. </window>