123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <window id="invoicereport">
- <reporttitle>Purchase:->Invoices</reporttitle>
- <layout type="vertical">
- <layout type="horizontal">
- <daterange id="dates" initial="23" /><!-- Lifetime -->
- <label>Vendor: </label>
- <sqldrop id="vendor" />
- <label>Search: </label>
- <line id="search" />
- <stretch />
- </layout>
- <webview id="report" />
- </layout>
- <menu name="File">
- <item id="print" shortcut="Ctrl+P">Print...</item>
- </menu>
- <menu name="Reports" type="reports" src="Reports" />
- <program>
- <![CDATA[
- var vendor = findChildObject(this, "vendor");
- vendor.addItem(TTR("invoicereport", "Any"));
- var query = new QSqlQuery();
- query.exec("SELECT DISTINCT vendor FROM invoices");
- while(query.next()) {
- vendor.addItem(query.value(0));
- }
- vendor['currentIndexChanged(int)'].connect(refresh);
- var dateSelect = findChildObject(this, 'dates');
- query.exec("SELECT time::date FROM invoices WHERE time = (SELECT min(time) FROM invoices) OR time = (SELECT max(time) FROM invoices) ORDER BY time ASC");
- query.next();
- var lifetimeStartDate = query.value(0);
- var lifetimeEndDate;
- if(query.next()) {
- lifetimeEndDate = query.value(0);
- } else {
- lifetimeEndDate = lifetimeStartDate;
- }
- dateSelect.setLifetimeRange(lifetimeStartDate, lifetimeEndDate);
- dateSelect.rangeUpdated.connect(refresh);
- query = query.invalidate();
- var search = findChildObject(this, "search");
- search.editingFinished.connect(refresh);
- var view = findChildObject(this, "report");
- view.scriptLinkClicked.connect(function(url) {
- var info = createWindow("invoiceinfo");
- info.setInvoiceID(url);
- });
- function refresh() {
- var dateRange = dateSelect.currentRange();
- var startDate = dateRange[0];
- var endDate = dateRange[dateRange.length - 1];
- var buffer = new QBuffer;
- buffer.open(3);
- var output = new XmlWriter(buffer);
- output.writeStartDocument("1.0");
- 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">');
- output.writeStartElement("html");
- output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
- output.writeStartElement("head");
- output.writeTextElement("title", TTR("invoicereport", "Invoices"));
- output.writeEndElement();
- output.writeStartElement("body");
- var cdt = new Date(Date.now());
- output.writeTextElement("p", cdt.toLocaleDateString(TTR("reports", "en-US")) + " " + cdt.toLocaleTimeString(TTR("reports", "en-US")));
- output.writeTextElement("h1", TTR("invoicereport", "Invoices ") + startDate + " - " + endDate);
- output.writeStartElement("table");
- output.writeAttribute("style", "page-break-after: auto; text-align: left");
- output.writeAttribute("rules", "groups");
- output.writeAttribute("cellpadding", "3px");
- output.writeStartElement("thead");
- output.writeStartElement("tr");
- output.writeTextElement("th", TTR("invoicereport", "Date"));
- output.writeTextElement("th", TTR("invoicereport", "Vendor"));
- output.writeTextElement("th", TTR("invoicereport", "Invoice"));
- output.writeTextElement("th", TTR("invoicereport", "Cost"));
- output.writeEndElement();
- output.writeEndElement();
- output.writeStartElement("tbody");
- var query = new QSqlQuery();
- var vendorclause = "";
- if(vendor.currentIndex > 0) {
- vendorclause = " AND vendor = :vendor";
- }
- var searchclause = "";
- if(search.text.length > 0)
- {
- searchclause = " AND id IN (SELECT invoice_id FROM invoice_items WHERE item_id IN (SELECT item FROM certifications WHERE certification ~* :p1 UNION SELECT id FROM coffees WHERE origin ~* :p2 UNION SELECT id FROM items WHERE name ~* :p3 UNION SELECT id FROM coffees WHERE reference ~* :p4 UNION SELECT id FROM coffees WHERE region ~* :p5 UNION SELECT id FROM coffees WHERE producer ~* :p6 UNION SELECT id FROM coffees WHERE grade ~* :p7 UNION SELECT id FROM coffees WHERE milling ~* :p8 UNION SELECT id FROM coffees WHERE drying ~* :p9 UNION SELECT id FROM decaf_coffees WHERE decaf_method ~* :p10) OR description ~* :p11 UNION SELECT id FROM invoices WHERE invoice ~* :p12 UNION SELECT id FROM invoices WHERE vendor ~* :p13)";
- }
- query.prepare("SELECT id, time::date, vendor, invoice, (SELECT sum(cost) FROM invoice_items WHERE invoice_id = id)::numeric(12,2) AS cost FROM invoices WHERE time >= :sd AND time < :ed::date + interval '1 day'" + vendorclause + searchclause + " ORDER BY time DESC");
- query.bind(":sd", startDate);
- query.bind(":ed", endDate);
- if(vendorclause.length > 0) {
- query.bind(":vendor", vendor.currentText);
- }
- if(searchclause.length > 0)
- {
- var pattern = ".*" + search.text + ".*";
- query.bind(":p1", pattern);
- query.bind(":p2", pattern);
- query.bind(":p3", pattern);
- query.bind(":p4", pattern);
- query.bind(":p5", pattern);
- query.bind(":p6", pattern);
- query.bind(":p7", pattern);
- query.bind(":p8", pattern);
- query.bind(":p9", pattern);
- query.bind(":p10", pattern);
- query.bind(":p11", pattern);
- query.bind(":p12", pattern);
- query.bind(":p13", pattern);
- }
- query.exec();
- var cost_sum = 0;
- while(query.next()) {
- output.writeStartElement("tr");
- output.writeStartElement("td");
- output.writeStartElement("a");
- output.writeAttribute("href", "typica://script/" + query.value(0));
- output.writeCharacters(query.value(1));
- output.writeEndElement();
- output.writeEndElement();
- for(var i = 2; i <= 4; i++) {
- output.writeTextElement("td", query.value(i));
- }
- output.writeEndElement();
- cost_sum += Number(query.value(4));
- }
- query = query.invalidate();
- output.writeEndElement();
- output.writeStartElement("tfoot");
- output.writeStartElement("tr");
- output.writeEmptyElement("td");
- output.writeEmptyElement("td");
- output.writeTextElement("th", TTR("invoicereport", "Total:"));
- output.writeTextElement("td", Number(cost_sum).toFixed(2));
- output.writeEndElement();
- output.writeEndElement();
- output.writeEndElement();
- output.writeEndElement();
- output.writeEndElement();
- output.writeEndDocument();
- view.setContent(buffer);
- buffer.close();
- }
- refresh();
- var notifier = Application.subscribe("invoiceschange");
- notifier.notify.connect(function() {
- refresh();
- });
- ]]>
- </program>
|