Production:->Average Use and Cost by Origin Print... '); output.writeStartElement("html"); output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml"); output.writeStartElement("head"); output.writeTextElement("title", TTR("useandcostreport", "Recent Use and Cost by Origin")); 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("useandcostreport", "Average Use and Cost by Origin ") + startDate + " - " + endDate); switch(unitBox.currentIndex) { case 0: output.writeTextElement("p", TTR("useandcostreport", "This is a report of average rate of use in kilograms per day and cost of unroasted coffee.")); break; case 1: output.writeTextElement("p", TTR("useandcostreport", "This is a report of average rate of use in pounds per day and cost of unroasted coffee.")); break; } output.writeStartElement("table"); output.writeAttribute("rules", "groups"); output.writeAttribute("cellpadding", "3px"); output.writeStartElement("thead"); output.writeStartElement("tr"); output.writeStartElement("th"); output.writeAttribute("colspan", "9"); output.writeCharacters(TTR("useandcostreport", "Regular Coffees")); output.writeEndElement(); output.writeEndElement(); output.writeStartElement("tr"); output.writeTextElement("th", TTR("useandcostreport", "Origin")); output.writeTextElement("th", TTR("useandcostreport", "Avg. Rate")); output.writeTextElement("th", TTR("useandcostreport", "Avg. Cost")); output.writeTextElement("th", TTR("useandcostreport", "Last Cost")); output.writeTextElement("th", TTR("useandcostreport", "Last Purchase Date")); output.writeTextElement("th", TTR("useandcostreport", "Bag Size (min)")); output.writeTextElement("th", TTR("useandcostreport", "Bag Size (max)")); output.writeTextElement("th", TTR("useandcostreport", "Bag Size (mean)")); output.writeTextElement("th", TTR("useandcostreport", "Purchases")); output.writeEndElement(); output.writeEndElement(); output.writeStartElement("tbody"); var query = new QSqlQuery(); var conversion = 1; if(unitBox.currentIndex == 0) { conversion = 2.2; } var orderClause; switch(sortBox.currentIndex) { case 0: orderClause = "origin ASC"; break; case 1: orderClause = "origin DESC"; break; case 2: orderClause = "rate ASC"; break; case 3: orderClause = "rate DESC"; break; case 4: orderClause = "cost ASC"; break; case 5: orderClause = "cost DESC"; break; } query.prepare("WITH q AS (SELECT id, origin, rate/:c1 AS rate, (SELECT cost*:c2 FROM purchase WHERE item = id) AS cost, (SELECT min(time) FROM purchase WHERE item = id) AS purchase_time, (SELECT conversion/:c3 FROM lb_bag_conversion WHERE item = id) AS bag_weight FROM coffee_history WHERE id IN (SELECT id FROM regular_coffees) AND id IN (SELECT item FROM transactions WHERE time >= :startDate AND time < :endDate::date + interval '1 day')) SELECT DISTINCT origin, avg(rate)::numeric(10,2) AS rate, avg(cost)::numeric(10,2) AS cost, (SELECT (cost*:c4)::numeric(10,2) FROM purchase WHERE item = max(q.id)) AS last_cost, max(purchase_time)::date AS last_purchase, min(bag_weight)::numeric(10,2) AS min_weight, max(bag_weight)::numeric(10,2) AS max_weight, avg(bag_weight)::numeric(10,2) AS mean_weight, count(1) AS n FROM q GROUP BY origin ORDER BY " + orderClause); query.bind(":c1", conversion); query.bind(":c2", conversion); query.bind(":c3", conversion); query.bind(":c4", conversion); query.bind(":startDate", startDate); query.bind(":endDate", endDate); query.exec(); while(query.next()) { output.writeStartElement("tr"); output.writeAttribute("id", "r" + rowIndex); output.writeStartElement("td"); output.writeStartElement("a"); output.writeAttribute("href", "typica://script/r" + rowIndex); rowIndex++; rowData.push(query.value(0)); output.writeCharacters(query.value(0)); output.writeEndElement(); output.writeEndElement(); output.writeTextElement("td", query.value(1)); output.writeTextElement("td", query.value(2)); output.writeTextElement("td", query.value(3)); output.writeTextElement("td", query.value(4)); output.writeTextElement("td", query.value(5)); output.writeTextElement("td", query.value(6)); output.writeTextElement("td", query.value(7)); output.writeTextElement("td", query.value(8)); output.writeEndElement(); } output.writeStartElement("tr"); output.writeStartElement("th"); output.writeAttribute("colspan", "9"); output.writeCharacters(TTR("useandcostreport", "Decaffeinated Coffees")); output.writeEndElement(); output.writeEndElement(); query.prepare("WITH q AS (SELECT id, origin, rate/:c1 AS rate, (SELECT cost*:c2 FROM purchase WHERE item = id) AS cost, (SELECT min(time) FROM purchase WHERE item = id) AS purchase_time, (SELECT conversion/:c3 FROM lb_bag_conversion WHERE item = id) AS bag_weight FROM coffee_history WHERE id IN (SELECT id FROM decaf_coffees) AND id IN (SELECT item FROM transactions WHERE time >= :startDate AND time < :endDate::date + interval '1 day')) SELECT DISTINCT origin, avg(rate)::numeric(10,2) AS rate, avg(cost)::numeric(10,2) AS cost, (SELECT (cost*:c4)::numeric(10,2) FROM purchase WHERE item = max(q.id)) AS last_cost, max(purchase_time)::date AS last_purchase, min(bag_weight)::numeric(10,2) AS min_weight, max(bag_weight)::numeric(10,2) AS max_weight, avg(bag_weight)::numeric(10,2) AS mean_weight, count(1) AS n FROM q GROUP BY origin ORDER BY " + orderClause); query.bind(":c1", conversion); query.bind(":c2", conversion); query.bind(":c3", conversion); query.bind(":c4", conversion); query.bind(":startDate", startDate); query.bind(":endDate", endDate); query.exec(); while(query.next()) { output.writeStartElement("tr"); output.writeAttribute("id", "d" + rowIndex); output.writeStartElement("td"); output.writeStartElement("a"); output.writeAttribute("href", "typica://script/d" + rowIndex); rowIndex++; rowData.push(query.value(0)); output.writeCharacters(query.value(0)); output.writeEndElement(); output.writeEndElement(); output.writeTextElement("td", query.value(1)); output.writeTextElement("td", query.value(2)); output.writeTextElement("td", query.value(3)); output.writeTextElement("td", query.value(4)); output.writeTextElement("td", query.value(5)); output.writeTextElement("td", query.value(6)); output.writeTextElement("td", query.value(7)); output.writeTextElement("td", query.value(8)); output.writeEndElement(); } query = query.invalidate(); output.writeEndElement(); output.writeEndElement(); output.writeEndElement(); output.writeEndElement(); output.writeEndDocument(); report.setContent(buffer); buffer.close(); } refresh(); sortBox['currentIndexChanged(int)'].connect(function() { QSettings.setValue("auco_sort", sortBox.currentIndex); refresh(); }); report.scriptLinkClicked.connect(function(url) { if(url[0] == 'i') { url = url.slice(1, url.length); var itemReport = createReport("itemtransactions.xml"); var sIB = findChildObject(itemReport, 'item'); sIB.currentIndex = sIB.findData(url); return; } var element = new WebElement(report.findFirstElement("#" + url)); var regular = url[0] == 'r'; var index = url.slice(1, url.length); var tableref; if(regular) { tableref = "regular_coffees"; } else { tableref = "decaf_coffees"; } var origin = rowData[Number(url.slice(1, url.length))]; var details = ''; var query = new QSqlQuery(); query.prepare("SELECT id, name, (rate/:conversion1)::numeric(12,3), (SELECT (cost*:conversion2)::numeric(12,2) FROM purchase WHERE item = id), (stock/:conversion3)::numeric(12,3), (SELECT min(time)::date FROM use WHERE item = id) AS first_use, (SELECT max(time)::date FROM use WHERE item = id) AS last_use FROM coffee_history WHERE origin = :origin AND id IN (SELECT id FROM " + tableref + ") AND id IN (SELECT item FROM transactions WHERE time >= :startDate AND time < :endDate::date + interval '1 day') ORDER BY first_use DESC"); var conversion = 1; if(unitBox.currentIndex == 0) { conversion = 2.2; } var dateRange = dateSelect.currentRange(); var startDate = dateRange[0]; var endDate = dateRange[dateRange.length - 1]; query.bind(":conversion1", conversion); query.bind(":conversion2", conversion); query.bind(":conversion3", conversion); query.bind(":origin", origin); query.bind(":startDate", startDate); query.bind(":endDate", endDate); query.exec(); while(query.next()) { details += ""; details += '"; for(var i = 1; i < 7; i++) { details += ""; } details += ""; } query = query.invalidate(); details += "
' + TTR("useandcostreport", "Id") + '' + TTR("useandcostreport", "Name") + '' + TTR("useandcostreport", "Rate") + '' + TTR("useandcostreport", "Cost") + '' + TTR("useandcostreport", "Inventory") + '' + TTR("useandcostreport", "First Use") + '' + TTR("useandcostreport", "Last Use") + '
' + query.value(0) + "" + query.value(i) + "
"; element.appendOutside(details); }); ]]>