<window id="useandcostreport">
	<reporttitle>Production:->Average Use and Cost by Origin</reporttitle>
    <layout type="vertical">
        <layout type="horizontal">
            <daterange id="dates" initial="23" /><!-- Lifetime -->
            <label>Sort Order:</label>
            <sqldrop id="sort" />
            <label>Weight Unit:</label>
            <sqldrop id="unit" />
            <stretch />
        </layout>
        <webview id="report" />
    </layout>
    <menu name="File">
        <item id="print" shortcut="Ctrl+P">Print...</item>
    </menu>
    <program>
        <![CDATA[
            this.windowTitle = TTR("useandcostreport", "Typica - Average Use and Cost by Origin");
            var report = findChildObject(this, 'report');
            var printMenu = findChildObject(this, 'print');
            printMenu.triggered.connect(function() {
                report.print();
            });
            var sortBox = findChildObject(this, 'sort');
            sortBox.addItem(TTR("useandcostreport","Origin A-Z"));
            sortBox.addItem(TTR("useandcostreport", "Origin Z-A"));
            sortBox.addItem(TTR("useandcostreport", "Avg. Rate Ascending"));
            sortBox.addItem(TTR("useandcostreport", "Avg. Rate Descending"));
            sortBox.addItem(TTR("useandcostreport", "Avg. Cost Ascending"));
            sortBox.addItem(TTR("useandcostreport", "Avg. Cost Descending"));
            sortBox.currentIndex = QSettings.value("auco_sort", 0);
            var unitBox = findChildObject(this, 'unit');
            unitBox.addItem(TTR("useandcostreport", "Kg"));
            unitBox.addItem(TTR("useandcostreport", "Lb"));
            unitBox.currentIndex = QSettings.value("script/report_unit", 1);
            unitBox['currentIndexChanged(int)'].connect(function() {
                QSettings.setValue("script/report_unit", unitBox.currentIndex);
                refresh();
            });
            var dateSelect = findChildObject(this, 'dates');
            var dateQuery = new QSqlQuery;
            dateQuery.exec("SELECT time::date FROM transactions WHERE time = (SELECT min(time) FROM transactions) OR time = (SELECT max(time) FROM transactions) ORDER BY time ASC");
            dateQuery.next();
            var lifetimeStartDate = dateQuery.value(0);
            var lifetimeEndDate;
            if(dateQuery.next()) {
                lifetimeEndDate = dateQuery.value(0);
            } else {
                lifetimeEndDate = lifetimeStartDate;
            }
            dateSelect.setLifetimeRange(lifetimeStartDate, lifetimeEndDate);
            dateQuery = dateQuery.invalidate();
            dateSelect.rangeUpdated.connect(refresh);
            var rowData = new Array();
            var rowIndex;
            function refresh() {
                rowData.length = 0;
                var dateRange = dateSelect.currentRange();
                var startDate = dateRange[0];
                var endDate = dateRange[dateRange.length - 1];
                rowIndex = 0;
                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("useandcostreport", "Recent Use and Cost by Origin"));
                output.writeEndElement();
                output.writeStartElement("body");
                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 = '<tr><td /><td colspan="6"><table><tr><th>' +
                                    TTR("useandcostreport", "Id") + '</th><th>' +
                                    TTR("useandcostreport", "Name") + '</th><th>' +
                                    TTR("useandcostreport", "Rate") + '</th><th>' +
                                    TTR("useandcostreport", "Cost") + '</th><th>' +
                                    TTR("useandcostreport", "Inventory") + '</th><th>' +
                                    TTR("useandcostreport", "First Use") + '</th><th>' +
                                    TTR("useandcostreport", "Last Use") + '</th></tr>';
                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 += "<tr>";
                    details += '<td><a href="typica://script/i' + query.value(0) + '">' + query.value(0) + "</a></td>";
                    for(var i = 1; i < 7; i++) {
                        details += "<td>" + query.value(i) + "</td>";
                    }
                    details += "</tr>";
                }
                query = query.invalidate();
                details += "</table></td></tr>";
                element.appendOutside(details);
            });
        ]]>
    </program>
</window>
