Sales:->Green Coffee Sales
');
output.writeStartElement("html");
output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
output.writeStartElement("head");
output.writeTextElement("title", TTR("greensales", "Green Coffee Sales"));
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")));
var dateRange = dateSelect.currentRange();
var startDate = dateRange[0];
var endDate = dateRange[dateRange.length - 1];
output.writeTextElement("h1", TTR("greensales", "Green Coffee Sales: ") + startDate + " - " + endDate);
var conversion = 1;
var unitText = TTR("greensales", "Lb");
if(unitBox.currentIndex == 0) {
conversion = 2.2;
unitText = TTR("greensales", "Kg");
}
var query = new QSqlQuery();
query.prepare("SELECT item, (SELECT name FROM coffees WHERE id = item) AS name, (SELECT origin FROM coffees WHERE id = item) AS origin, (SELECT reference FROM coffees WHERE id = item) AS reference, (SUM(quantity)/:conversion)::numeric(12,3) FROM sale WHERE time < :ed ::date + interval '1 day' AND time >= :sd GROUP BY item ORDER BY name ASC");
query.bind(":conversion", conversion);
query.bind(":ed", endDate);
query.bind(":sd", startDate);
query.exec();
output.writeStartElement("table");
output.writeAttribute("rules", "groups");
output.writeAttribute("cellpadding", "3px");
output.writeStartElement("thead");
output.writeStartElement("tr");
output.writeTextElement("th", TTR("greensales", "ID")); // 0
output.writeTextElement("th", TTR("greensales", "Coffee")); // 1
output.writeTextElement("th", TTR("greensales", "Origin")); // 2
output.writeTextElement("th", TTR("greensales", "Reference")); // 3
output.writeTextElement("th", TTR("greensales", "Quantity")); // 4
output.writeEndElement();
output.writeEndElement();
output.writeStartElement("tbody");
while(query.next()) {
output.writeStartElement("tr");
output.writeAttribute("id", "r"+query.value(0));
reportitems.push(query.value(0));
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.writeEndElement();
}
output.writeEndElement();
output.writeStartElement("tfoot");
query.prepare("SELECT (sum(quantity)/:conversion)::numeric(12,3) FROM sale WHERE time < :ed ::date + interval '1 day' AND time >= :sd");
query.bind(":conversion", conversion);
query.bind(":ed", endDate);
query.bind(":sd", startDate);
query.exec();
if(query.next()) {
output.writeStartElement("tr");
output.writeEmptyElement("td");
output.writeEmptyElement("td");
output.writeEmptyElement("td");
output.writeTextElement("th", TTR("greensales", "Total:"));
output.writeTextElement("td", query.value(0));
output.writeEndElement();
}
output.writeEndElement();
output.writeEndElement();
output.writeEndElement();
output.writeEndElement();
output.writeEndDocument();
view.setContent(buffer);
buffer.close();
query = query.invalidate();
}
refresh();
var notifier = Application.subscribe("salechange");
notifier.notify.connect(function() {
refresh();
});
dateSelect.rangeUpdated.connect(refresh);
]]>