Purchase:->Coffee Purchase Previous Years Comparison Print... '); output.writeStartElement("html"); output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml"); output.writeStartElement("head"); output.writeTextElement("title", "Coffee Purchase Previous Years Comparison"); output.writeEndElement(); output.writeStartElement("body"); output.writeTextElement("h1", "Coffee Purchase Previous Years Comparison"); 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.writeEmptyElement("th"); output.writeTextElement("th", "Sacks Purchased"); switch(unitBox.currentIndex) { case 0: output.writeTextElement("th", "Kilos Purchased"); break; case 1: output.writeTextElement("th", "Pounds Purchased"); break; } output.writeTextElement("th", "Cost"); output.writeEndElement(); //tr output.writeEndElement(); //thead output.writeStartElement("tbody"); var sacktotal = 0; var unittotal = 0; var costtotal = 0; var query = new QSqlQuery(); for(var i = startDateField.year(); i <= endDateField.year(); i++) { output.writeStartElement("tr"); output.writeAttribute("id", "y"+i); 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'"; query.prepare(q); query.bind(":conversion", unitBox.currentIndex == 0 ? 2.2 : 1); query.exec(); query.next(); output.writeStartElement("th"); output.writeStartElement("a"); output.writeAttribute("href", "typica://script/y" + i); output.writeCharacters(i); output.writeEndElement(); //a output.writeEndElement(); //th output.writeTextElement("td", query.value(0)); output.writeTextElement("td", query.value(1)); output.writeTextElement("td", Number(query.value(2)).toFixed(2)); sacktotal += Number(query.value(0)); unittotal += Number(query.value(1)); costtotal += Number(query.value(2)); output.writeEndElement(); //tr } query = query.invalidate(); output.writeEndElement(); //tbody output.writeStartElement("tfoot"); output.writeTextElement("th", "Totals:"); output.writeTextElement("td", sacktotal); output.writeTextElement("td", unittotal.toFixed(2)); output.writeTextElement("td", costtotal.toFixed(2)); output.writeEndElement(); //tfoot output.writeEndElement(); //table output.writeEndElement(); //body output.writeEndElement(); //html output.writeEndDocument(); view.setContent(buffer); buffer.close(); } refresh(); /* Update report as needed. */ startDateField.dateChanged.connect(function() { refresh(); }); endDateField.dateChanged.connect(function() { refresh(); }); /* Expand year data */ var expandedYears = new Array(); var expandYear = function(url) { if(expandedYears.indexOf(url) == -1) { expandedYears.push(url); var element = new WebElement(view.findFirstElement("#" + url)); var year = url.slice(1,url.length); var details = ''; 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'"; var query = new QSqlQuery(); query.exec(q); while(query.next()) { details += ''; details += ''; details += ''; details += ''; } query = query.invalidate(); details += '
IdInvoiceVendorCost
' + query.value(0) + '' + query.value(1) + '' + query.value(2) + '' + Number(query.value(3)).toFixed(2) + '
' element.appendOutside(details); } }; /* Open invoices */ var openInvoice = function(url) { var arg = url.slice(1, url.length); var info = createWindow("invoiceinfo"); info.setInvoiceID(arg); var query = new QSqlQuery(); query.exec("SELECT time, invoice, vendor FROM invoices WHERE id = " + arg); query.next(); var timefield = findChildObject(info, 'date'); timefield.text = query.value(0); var vendorfield = findChildObject(info, 'vendor'); vendorfield.text = query.value(2); var invoicefield = findChildObject(info, 'invoice'); invoicefield.text = query.value(1); var itemtable = findChildObject(info, 'itemtable'); 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"); query = query.invalidate(); }; /* Dispatch script link clicks */ view.scriptLinkClicked.connect(function(url) { var linkType = url[0]; switch(linkType) { case 'y': expandYear(url); break case 'i': openInvoice(url); break; } }); ]]>