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", "Recent Use and Cost by Origin"); output.writeEndElement(); output.writeStartElement("body"); output.writeTextElement("h1", "Average Use and Cost by Origin"); switch(unitBox.currentIndex) { case 0: output.writeTextElement("p", "This is a report of average rate of use in kilograms per day and cost of unroasted coffee."); break; case 1: output.writeTextElement("p", "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", "8"); output.writeCharacters("Regular Coffees"); output.writeEndElement(); output.writeEndElement(); output.writeStartElement("tr"); output.writeTextElement("th", "Origin"); output.writeTextElement("th", "Avg. Rate"); output.writeTextElement("th", "Avg. Cost"); output.writeTextElement("th", "Last Cost"); output.writeTextElement("th", "Last Purchase Date"); output.writeTextElement("th", "Bag Size (min)"); output.writeTextElement("th", "Bag Size (max)"); output.writeTextElement("th", "Bag Size (mean)"); 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("SELECT DISTINCT origin, (avg(rate)/:conversion)::numeric(10,2) AS rate, (SELECT avg(cost)*:conversion2 FROM purchase WHERE item IN (SELECT id FROM regular_coffees WHERE origin = coffee_history.origin))::numeric(10,2) AS cost, (SELECT avg(cost)*:conversion3 FROM purchase WHERE item IN (SELECT id FROM regular_coffees WHERE origin = coffee_history.origin) AND time = (SELECT max(time) FROM purchase WHERE item IN (SELECT id FROM regular_coffees WHERE origin = coffee_history.origin))), (SELECT max(time)::date FROM purchase WHERE item IN (SELECT id FROM regular_coffees WHERE origin = coffee_history.origin)), (SELECT min(conversion)/:conversion4 FROM lb_bag_conversion WHERE item IN (SELECT id FROM regular_coffees WHERE origin = coffee_history.origin))::numeric(10,2) AS minbag, (SELECT max(conversion)/:conversion5 FROM lb_bag_conversion WHERE item IN (SELECT id FROM regular_coffees WHERE origin = coffee_history.origin))::numeric(10,2) AS maxbag, (SELECT avg(conversion)/:conversion6 FROM lb_bag_conversion WHERE item IN (SELECT id FROM regular_coffees WHERE origin = coffee_history.origin))::numeric(10,2) AS meanbag FROM coffee_history WHERE id IN (SELECT id FROM regular_coffees) GROUP BY origin ORDER BY " + orderClause); query.bind(":conversion", conversion); query.bind(":conversion2", conversion); query.bind(":conversion3", conversion); query.bind(":conversion4", conversion); query.bind(":conversion5", conversion); query.bind(":conversion6", conversion); query.exec(); while(query.next()) { output.writeStartElement("tr"); output.writeTextElement("td", query.value(0)); 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.writeEndElement(); } output.writeStartElement("tr"); output.writeStartElement("th"); output.writeAttribute("colspan", "8"); output.writeCharacters("Decaffeinated Coffees"); output.writeEndElement(); output.writeEndElement(); query.prepare("SELECT DISTINCT origin, (avg(rate)/:conversion)::numeric(10,2) AS rate, (SELECT avg(cost)*:conversion2 FROM purchase WHERE item IN (SELECT id FROM decaf_coffees WHERE origin = coffee_history.origin))::numeric(10,2) AS cost, (SELECT avg(cost)*:conversion3 FROM purchase WHERE item IN (SELECT id FROM decaf_coffees WHERE origin = coffee_history.origin) AND time = (SELECT max(time) FROM purchase WHERE item IN (SELECT id FROM decaf_coffees WHERE origin = coffee_history.origin))), (SELECT max(time)::date FROM purchase WHERE item IN (SELECT id FROM decaf_coffees WHERE origin = coffee_history.origin)), (SELECT min(conversion)/:conversion4 FROM lb_bag_conversion WHERE item IN (SELECT id FROM decaf_coffees WHERE origin = coffee_history.origin))::numeric(10,2) AS minbag, (SELECT max(conversion)/:conversion5 FROM lb_bag_conversion WHERE item IN (SELECT id FROM decaf_coffees WHERE origin = coffee_history.origin))::numeric(10,2) AS maxbag, (SELECT avg(conversion)/:conversion6 FROM lb_bag_conversion WHERE item IN (SELECT id FROM decaf_coffees WHERE origin = coffee_history.origin))::numeric(10,2) AS meanbag FROM coffee_history WHERE id IN (SELECT id FROM decaf_coffees) GROUP BY origin ORDER BY " + orderClause); query.bind(":conversion", conversion); query.bind(":conversion2", conversion); query.bind(":conversion3", conversion); query.bind(":conversion4", conversion); query.bind(":conversion5", conversion); query.bind(":conversion6", conversion); query.exec(); while(query.next()) { output.writeStartElement("tr"); output.writeTextElement("td", query.value(0)); 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.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(); }); ]]>