<window id="productionreport">
	<reporttitle>Production:->Recent Average Coffee Production</reporttitle>
    <layout type="vertical">
        <layout type="horizontal">
            <label>Sort Order:</label>
            <sqldrop id="sort" />
            <label>Weight Unit:</label>
            <sqldrop id="unit" />
            <label>Batch Type: </label>
            <sqldrop id="batchtype" />
            <label>Days to Average: </label>
            <line id="days" />
            <label>Days to Scale: </label>
            <line id="scale" />
            <stretch />
        </layout>
        <webview id="report" />
    </layout>
    <menu name="File">
        <item id="print" shortcut="Ctrl+P">Print</item>
    </menu>
    <program>
        <![CDATA[
            this.windowTitle = "Typica - Recent Average Coffee Production";
            var report = findChildObject(this, 'report');
            var printMenu = findChildObject(this, 'print');
            printMenu.triggered.connect(function() {
                report.print();
            });
            var sortBox = findChildObject(this, 'sort');
            sortBox.addItem("Roasted Coffee A-Z");
            sortBox.addItem("Roasted Coffee Z-A");
            sortBox.addItem("Weekly Use Ascending");
            sortBox.addItem("Weekly Use Descending");
            sortBox.currentIndex = QSettings.value("rwacp_sort", 3);
            var unitBox = findChildObject(this, 'unit');
            unitBox.addItem("Kg");
            unitBox.addItem("Lb");
            unitBox.currentIndex = QSettings.value("script/report_unit", 1);
            var batchType = findChildObject(this, 'batchtype');
            batchType.addItem("Any");
            batchType.addItem("Production Roasts");
            batchType.addItem("Sample Roasts");
            batchType.currentIndex = QSettings.value("script/racpreport/batchtypefilter", 1);
            batchType['currentIndexChanged(int)'].connect(function() {
                QSettings.setValue("script/racpreport/batchtypefilter",
                                   batchType.currentIndex);
                refresh();
            });
            var daysBox = findChildObject(this, 'days');
            daysBox.text = QSettings.value("script/racpreport/days", "28");
            daysBox.editingFinished.connect(function() {
                if(Number(daysBox.text) < 1) {
                    daysBox.text = QSettings.value("script/racpreport/days");
                }
                QSettings.setValue("script/racpreport/days", daysBox.text);
                refresh();
            });
            var scaleBox = findChildObject(this, 'scale');
            scaleBox.text = QSettings.value("script/racpreport/scale", "7");
            scaleBox.editingFinished.connect(function() {
                if(Number(scaleBox.text) < 1) {
                    scaleBox.text = QSettings.value("script/racpreport/scale");
                }
                QSettings.setValue("script/racpreport/scale", scaleBox.text);
                refresh();
            });
            function refresh() {
                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", "Recent Average Coffee Production");
                output.writeEndElement();
                output.writeStartElement("body");
                output.writeTextElement("h1", "Recent Average Coffee Production");
                switch(unitBox.currentIndex)
                {
                    case 0:
                        output.writeTextElement("p", "This is a report of average coffee production per " + scaleBox.text + " days in kilograms over the past " + daysBox.text + " days.");
                        break;
                    case 1:
                        output.writeTextElement("p", "This is a report of average coffee production per " + scaleBox.text + " days in pounds over the past " + daysBox.text + " days.");
                        break;
                }
                output.writeStartElement("table");
                output.writeAttribute("rules", "groups");
                output.writeAttribute("cellpadding", "3px");
                output.writeStartElement("thead");
                output.writeStartElement("tr");
                output.writeTextElement("th", "Roasted Coffee");
                output.writeTextElement("th", "Weekly Use");
                output.writeEndElement();
                output.writeEndElement();
                output.writeStartElement("tbody");
                var batchClause = "";
                switch(batchType.currentIndex) {
                    case 1:
                        batchClause = " AND transaction_type = 'ROAST'";
                        break;
                    case 2:
                        batchClause = " AND transaction_type = 'SAMPLEROAST'";
                        break;
                }
                var q = "SELECT (SELECT name FROM items WHERE id = roasted_id) AS name, ((sum(roasted_quantity) / :scale) / :conversion)::numeric(18,2) AS weekly FROM roasting_log WHERE time > current_date - integer '" + daysBox.text + "' AND roasted_quantity > 0 " + batchClause + " GROUP BY roasted_id ORDER BY "
                switch(sortBox.currentIndex)
                {
                    case 0:
                        q += "name ASC";
                        break;
                    case 1:
                        q += "name DESC";
                        break;
                    case 2:
                        q += "weekly ASC";
                        break;
                    case 3:
                        q += "weekly DESC";
                        break;
                }
                var query = new QSqlQuery();
                query.prepare(q);
                switch(unitBox.currentIndex)
                {
                    case 0:
                        query.bind(":conversion", 2.2);
                        break;
                    case 1:
                        query.bind(":conversion", 1);
                        break;
                }
                query.bind(":scale", Number(daysBox.text)/Number(scaleBox.text));
                query.exec();
                while(query.next())
                {
                    output.writeStartElement("tr");
                    output.writeTextElement("td", query.value(0));
                    output.writeTextElement("td", query.value(1));
                    output.writeEndElement();
                }
                output.writeEndElement();
                output.writeStartElement("tfoot")
                output.writeTextElement("th", "Total");
                query.prepare("SELECT ((sum(roasted_quantity) / :scale) / :conversion)::numeric(18,2) FROM roasting_log WHERE time > current_date - integer '" + daysBox.text + "' AND roasted_quantity > 0 " + batchClause);
                switch(unitBox.currentIndex)
                {
                    case 0:
                        query.bind(":conversion", 2.2);
                        break;
                    case 1:
                        query.bind(":conversion", 1);
                        break;
                }
                query.bind(":scale", Number(daysBox.text)/Number(scaleBox.text));
                query.exec();
                query.next();
                output.writeTextElement("td", query.value(0));
                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("rwacp_sort", sortBox.currentIndex);
                refresh();
            });
			unitBox['currentIndexChanged(int)'].connect(function() {
				QSettings.setValue("script/report_unit", unitBox.currentIndex);
				refresh();
			});
        ]]>
    </program>
</window>
