Typica is a free program for professional coffee roasters. https://typica.us
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

fypurchase.xml 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <window id="fypurchase">
  2. <reporttitle>Purchase:->Coffee Purchase Previous Years Comparison</reporttitle>
  3. <layout type="vertical">
  4. <layout type="horizontal">
  5. <label>Start Date:</label>
  6. <calendar id="startdate" />
  7. <label>End Date:</label>
  8. <calendar id="enddate" />
  9. <label>Weight Unit:</label>
  10. <sqldrop id="unit" />
  11. <stretch />
  12. </layout>
  13. <webview id="report" />
  14. </layout>
  15. <menu name="File">
  16. <item id="print" shortcut="Ctrl+P">Print...</item>
  17. </menu>
  18. <program>
  19. <![CDATA[
  20. this.windowTitle = "Typica - Coffee Purchase Previous Years Comparison";
  21. /* Set starting year to the first year on record. */
  22. var startDateField = findChildObject(this, 'startdate');
  23. var query = new QSqlQuery();
  24. query.exec("SELECT EXTRACT(YEAR FROM time) FROM purchase WHERE time = (SELECT min(time) FROM purchase)");
  25. query.next();
  26. startDateField.setDate(query.value(0), 1, 1);
  27. query = query.invalidate();
  28. /* Set ending year to the current year. */
  29. var endDateField = findChildObject(this, 'enddate');
  30. endDateField.setDate(endDateField.year(), 12, 31);
  31. /* Enable printing */
  32. var view = findChildObject(this, 'report');
  33. var printMenu = findChildObject(this, 'print');
  34. printMenu.triggered.connect(function() {
  35. view.print();
  36. });
  37. /* Add units to unit selector and enable functionality */
  38. var unitBox = findChildObject(this, 'unit');
  39. unitBox.addItem("Kg");
  40. unitBox.addItem("Lb");
  41. unitBox.currentIndex = QSettings.value("script/report_unit", 1);
  42. unitBox['currentIndexChanged(int)'].connect(function() {
  43. QSettings.setValue("script/report_unit", unitBox.currentIndex);
  44. refresh();
  45. });
  46. /* Generate report */
  47. function refresh() {
  48. var buffer = new QBuffer;
  49. buffer.open(3);
  50. var output = new XmlWriter(buffer);
  51. output.writeStartDocument("1.0");
  52. 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">');
  53. output.writeStartElement("html");
  54. output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
  55. output.writeStartElement("head");
  56. output.writeTextElement("title", "Coffee Purchase Previous Years Comparison");
  57. output.writeEndElement();
  58. output.writeStartElement("body");
  59. output.writeTextElement("h1", "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", "Sacks Purchased");
  68. switch(unitBox.currentIndex) {
  69. case 0:
  70. output.writeTextElement("th", "Kilos Purchased");
  71. break;
  72. case 1:
  73. output.writeTextElement("th", "Pounds Purchased");
  74. break;
  75. }
  76. output.writeTextElement("th", "Cost");
  77. output.writeEndElement(); //tr
  78. output.writeEndElement(); //thead
  79. output.writeStartElement("tbody");
  80. var sacktotal = 0;
  81. var unittotal = 0;
  82. var costtotal = 0;
  83. var query = new QSqlQuery();
  84. for(var i = startDateField.year(); i <= endDateField.year(); 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. startDateField.dateChanged.connect(function() {
  125. refresh();
  126. });
  127. endDateField.dateChanged.connect(function() {
  128. refresh();
  129. });
  130. /* Expand year data */
  131. var expandedYears = new Array();
  132. var expandYear = function(url) {
  133. if(expandedYears.indexOf(url) == -1) {
  134. expandedYears.push(url);
  135. var element = new WebElement(view.findFirstElement("#" + url));
  136. var year = url.slice(1,url.length);
  137. var details = '<tr><td /><td colspan="3"><table><tr><th>Id</th><th>Invoice</th><th>Vendor</th><th>Cost</th></tr>';
  138. 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'";
  139. var query = new QSqlQuery();
  140. query.exec(q);
  141. while(query.next())
  142. {
  143. details += '<tr><td><a href="typica://script/i' + query.value(0) + '">' + query.value(0) + '</a></td>';
  144. details += '<td>' + query.value(1) + '</td>';
  145. details += '<td>' + query.value(2) + '</td>';
  146. details += '<td>' + Number(query.value(3)).toFixed(2) + '</td></tr>';
  147. }
  148. query = query.invalidate();
  149. details += '</table></td></tr>'
  150. element.appendOutside(details);
  151. }
  152. };
  153. /* Open invoices */
  154. var openInvoice = function(url) {
  155. var arg = url.slice(1, url.length);
  156. var info = createWindow("invoiceinfo");
  157. info.setInvoiceID(arg);
  158. var query = new QSqlQuery();
  159. query.exec("SELECT time, invoice, vendor FROM invoices WHERE id = " + arg);
  160. query.next();
  161. var timefield = findChildObject(info, 'date');
  162. timefield.text = query.value(0);
  163. var vendorfield = findChildObject(info, 'vendor');
  164. vendorfield.text = query.value(2);
  165. var invoicefield = findChildObject(info, 'invoice');
  166. invoicefield.text = query.value(1);
  167. var itemtable = findChildObject(info, 'itemtable');
  168. 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");
  169. query = query.invalidate();
  170. };
  171. /* Dispatch script link clicks */
  172. view.scriptLinkClicked.connect(function(url) {
  173. var linkType = url[0];
  174. switch(linkType) {
  175. case 'y':
  176. expandYear(url);
  177. break
  178. case 'i':
  179. openInvoice(url);
  180. break;
  181. }
  182. });
  183. ]]>
  184. </program>
  185. </window>