123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <window id="fypurchase">
- <reporttitle>Purchase:->Coffee Purchase Previous Years Comparison</reporttitle>
- <layout type="vertical">
- <layout type="horizontal">
- <label>Start Date:</label>
- <calendar id="startdate" />
- <label>End Date:</label>
- <calendar id="enddate" />
- <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 = "Typica - Coffee Purchase Previous Years Comparison";
- /* Set starting year to the first year on record. */
- var startDateField = findChildObject(this, 'startdate');
- var query = new QSqlQuery();
- query.exec("SELECT EXTRACT(YEAR FROM time) FROM purchase WHERE time = (SELECT min(time) FROM purchase)");
- query.next();
- startDateField.setDate(query.value(0), 1, 1);
- /* Set ending year to the current year. */
- var endDateField = findChildObject(this, 'enddate');
- endDateField.setDate(endDateField.year(), 12, 31);
- /* Enable printing */
- var view = findChildObject(this, 'report');
- var printMenu = findChildObject(this, 'print');
- printMenu.triggered.connect(function() {
- view.print();
- });
- /* Add units to unit selector and enable functionality */
- var unitBox = findChildObject(this, 'unit');
- unitBox.addItem("Kg");
- unitBox.addItem("Lb");
- unitBox.currentIndex = QSettings.value("script/report_unit", 1);
- unitBox['currentIndexChanged(int)'].connect(function() {
- QSettings.setValue("script/report_unit", unitBox.currentIndex);
- refresh();
- });
- /* Generate report */
- 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", "Coffee Purchase Previous Years Comparison");
- output.writeEndElement();
- output.writeStartElement("body");
- output.writeTextElement("h1", "Coffee Purchase Previous Years Comparison");
- output.writeStartElement("table");
- output.writeAttribute("style", "page-break-after: auto; text-align: left");
- output.writeAttribute("rules", "groups");
- output.writeAttribute("cellpadding", "3px");
- output.writeStartElement("thead");
- output.writeStartElement("tr");
- output.writeEmptyElement("th");
- output.writeTextElement("th", "Sacks Purchased");
- switch(unitBox.currentIndex) {
- case 0:
- output.writeTextElement("th", "Kilos Purchased");
- break;
- case 1:
- output.writeTextElement("th", "Pounds Purchased");
- break;
- }
- output.writeTextElement("th", "Cost");
- output.writeEndElement(); //tr
- output.writeEndElement(); //thead
- output.writeStartElement("tbody");
- var sacktotal = 0;
- var unittotal = 0;
- var costtotal = 0;
- for(var i = startDateField.year(); i <= endDateField.year(); i++)
- {
- output.writeStartElement("tr");
- output.writeAttribute("id", "y"+i);
- var q = "SELECT sum(quantity/(SELECT conversion FROM lb_bag_conversion WHERE item = purchase.item)), (sum(quantity) / :conversion)::numeric(12,2), sum(cost*quantity)::numeric(12,2) FROM purchase WHERE time >= '" + i + "-01-01' AND time < '" + (i+1) + "-01-01'";
- query.prepare(q);
- query.bind(":conversion", unitBox.currentIndex == 0 ? 2.2 : 1);
- query.exec();
- query.next();
- output.writeStartElement("th");
- output.writeCharacters(i);
- output.writeEndElement(); //th
- output.writeTextElement("td", query.value(0));
- output.writeTextElement("td", query.value(1));
- output.writeTextElement("td", Number(query.value(2)).toFixed(2));
- sacktotal += Number(query.value(0));
- unittotal += Number(query.value(1));
- costtotal += Number(query.value(2));
- output.writeEndElement(); //tr
- }
- output.writeEndElement(); //tbody
- output.writeStartElement("tfoot");
- output.writeTextElement("th", "Totals:");
- output.writeTextElement("td", sacktotal);
- output.writeTextElement("td", unittotal.toFixed(2));
- output.writeTextElement("td", costtotal.toFixed(2));
- output.writeEndElement(); //tfoot
- output.writeEndElement(); //table
- output.writeEndElement(); //body
- output.writeEndElement(); //html
- output.writeEndDocument();
- view.setContent(buffer);
- buffer.close();
- }
- refresh();
- /* Update report as needed. */
- startDateField.dateChanged.connect(function() {
- refresh();
- });
- endDateField.dateChanged.connect(function() {
- refresh();
- });
- ]]>
- </program>
- </window>
|