|  | @@ -13,23 +13,27 @@
 | 
		
	
		
			
			| 13 | 13 |  		<webview id="report" />
 | 
		
	
		
			
			| 14 | 14 |  	</layout>
 | 
		
	
		
			
			| 15 | 15 |  	<menu name="File">
 | 
		
	
		
			
			| 16 |  | -		<item id="print" shortcut="Ctrl+P">Print</item>
 | 
		
	
		
			
			|  | 16 | +		<item id="print" shortcut="Ctrl+P">Print...</item>
 | 
		
	
		
			
			| 17 | 17 |  	</menu>
 | 
		
	
		
			
			| 18 | 18 |  	<program>
 | 
		
	
		
			
			| 19 | 19 |  		<![CDATA[
 | 
		
	
		
			
			| 20 | 20 |  			this.windowTitle = "Typica - Coffee Purchase Previous Years Comparison";
 | 
		
	
		
			
			|  | 21 | +			/* Set starting year to the first year on record. */
 | 
		
	
		
			
			| 21 | 22 |  			var startDateField = findChildObject(this, 'startdate');
 | 
		
	
		
			
			| 22 | 23 |  			var query = new QSqlQuery();
 | 
		
	
		
			
			| 23 | 24 |  			query.exec("SELECT EXTRACT(YEAR FROM time) FROM purchase WHERE time = (SELECT min(time) FROM purchase)");
 | 
		
	
		
			
			| 24 | 25 |  			query.next();
 | 
		
	
		
			
			| 25 | 26 |  			startDateField.setDate(query.value(0), 1, 1);
 | 
		
	
		
			
			|  | 27 | +			/* Set ending year to the current year. */
 | 
		
	
		
			
			| 26 | 28 |  			var endDateField = findChildObject(this, 'enddate');
 | 
		
	
		
			
			| 27 | 29 |  			endDateField.setDate(endDateField.year(), 12, 31);
 | 
		
	
		
			
			|  | 30 | +			/* Enable printing */
 | 
		
	
		
			
			| 28 | 31 |  			var view = findChildObject(this, 'report');
 | 
		
	
		
			
			| 29 | 32 |  			var printMenu = findChildObject(this, 'print');
 | 
		
	
		
			
			| 30 | 33 |  			printMenu.triggered.connect(function() {
 | 
		
	
		
			
			| 31 | 34 |  				view.print();
 | 
		
	
		
			
			| 32 | 35 |  			});
 | 
		
	
		
			
			|  | 36 | +			/* Add units to unit selector and enable functionality */
 | 
		
	
		
			
			| 33 | 37 |  			var unitBox = findChildObject(this, 'unit');
 | 
		
	
		
			
			| 34 | 38 |  			unitBox.addItem("Kg");
 | 
		
	
		
			
			| 35 | 39 |  			unitBox.addItem("Lb");
 | 
		
	
	
		
			
			|  | @@ -38,6 +42,7 @@
 | 
		
	
		
			
			| 38 | 42 |  				QSettings.setValue("script/report_unit", unitBox.currentIndex);
 | 
		
	
		
			
			| 39 | 43 |  				refresh();
 | 
		
	
		
			
			| 40 | 44 |  			});
 | 
		
	
		
			
			|  | 45 | +			/* Generate report */
 | 
		
	
		
			
			| 41 | 46 |  			function refresh() {
 | 
		
	
		
			
			| 42 | 47 |  				var buffer = new QBuffer;
 | 
		
	
		
			
			| 43 | 48 |  				buffer.open(3);
 | 
		
	
	
		
			
			|  | @@ -52,34 +57,13 @@
 | 
		
	
		
			
			| 52 | 57 |  				output.writeStartElement("body");
 | 
		
	
		
			
			| 53 | 58 |  				output.writeTextElement("h1", "Coffee Purchase Previous Years Comparison");
 | 
		
	
		
			
			| 54 | 59 |  				output.writeStartElement("table");
 | 
		
	
		
			
			| 55 |  | -				output.writeAttribute("style", "page-break-after:auto;");
 | 
		
	
		
			
			| 56 |  | -                output.writeAttribute("rules", "groups");
 | 
		
	
		
			
			| 57 |  | -                output.writeAttribute("cellpadding", "3px");
 | 
		
	
		
			
			| 58 |  | -                output.writeStartElement("thead");
 | 
		
	
		
			
			| 59 |  | -                output.writeStartElement("tr");
 | 
		
	
		
			
			| 60 |  | -				output.writeEmptyElement("th");
 | 
		
	
		
			
			| 61 |  | -				for(var i = startDateField.year(); i <= endDateField.year(); i++)
 | 
		
	
		
			
			| 62 |  | -				{
 | 
		
	
		
			
			| 63 |  | -					output.writeTextElement("th", i);
 | 
		
	
		
			
			| 64 |  | -				}
 | 
		
	
		
			
			| 65 |  | -				output.writeEndElement();
 | 
		
	
		
			
			| 66 |  | -				output.writeEndElement();
 | 
		
	
		
			
			| 67 |  | -				output.writeStartElement("tbody");
 | 
		
	
		
			
			|  | 60 | +				output.writeAttribute("style", "page-break-after: auto; text-align: left");
 | 
		
	
		
			
			|  | 61 | +				output.writeAttribute("rules", "groups");
 | 
		
	
		
			
			|  | 62 | +				output.writeAttribute("cellpadding", "3px");
 | 
		
	
		
			
			|  | 63 | +				output.writeStartElement("thead");
 | 
		
	
		
			
			| 68 | 64 |  				output.writeStartElement("tr");
 | 
		
	
		
			
			|  | 65 | +				output.writeEmptyElement("th");
 | 
		
	
		
			
			| 69 | 66 |  				output.writeTextElement("th", "Sacks Purchased");
 | 
		
	
		
			
			| 70 |  | -				var j = 0;
 | 
		
	
		
			
			| 71 |  | -				var sacks = new Array;
 | 
		
	
		
			
			| 72 |  | -				for(var i = startDateField.year(); i <= endDateField.year(); i++)
 | 
		
	
		
			
			| 73 |  | -				{
 | 
		
	
		
			
			| 74 |  | -					var q = "SELECT sum(quantity/(SELECT conversion FROM lb_bag_conversion WHERE item = purchase.item)) FROM purchase WHERE time >= '" + i + "-01-01' AND time < '" + (i+1) + "-01-01'";
 | 
		
	
		
			
			| 75 |  | -					query.exec(q);
 | 
		
	
		
			
			| 76 |  | -					query.next();
 | 
		
	
		
			
			| 77 |  | -					output.writeTextElement("td", query.value(0));
 | 
		
	
		
			
			| 78 |  | -					sacks[j] = query.value(0);
 | 
		
	
		
			
			| 79 |  | -					j++;
 | 
		
	
		
			
			| 80 |  | -				}
 | 
		
	
		
			
			| 81 |  | -				output.writeEndElement();
 | 
		
	
		
			
			| 82 |  | -				output.writeStartElement("tr");
 | 
		
	
		
			
			| 83 | 67 |  				switch(unitBox.currentIndex) {
 | 
		
	
		
			
			| 84 | 68 |  					case 0:
 | 
		
	
		
			
			| 85 | 69 |  						output.writeTextElement("th", "Kilos Purchased");
 | 
		
	
	
		
			
			|  | @@ -88,45 +72,49 @@
 | 
		
	
		
			
			| 88 | 72 |  						output.writeTextElement("th", "Pounds Purchased");
 | 
		
	
		
			
			| 89 | 73 |  						break;
 | 
		
	
		
			
			| 90 | 74 |  				}
 | 
		
	
		
			
			| 91 |  | -				j = 0;
 | 
		
	
		
			
			| 92 |  | -				var pounds = new Array;
 | 
		
	
		
			
			|  | 75 | +				output.writeTextElement("th", "Cost");
 | 
		
	
		
			
			|  | 76 | +				output.writeEndElement(); //tr
 | 
		
	
		
			
			|  | 77 | +				output.writeEndElement(); //thead
 | 
		
	
		
			
			|  | 78 | +				output.writeStartElement("tbody");
 | 
		
	
		
			
			|  | 79 | +				var sacktotal = 0;
 | 
		
	
		
			
			|  | 80 | +				var unittotal = 0;
 | 
		
	
		
			
			|  | 81 | +				var costtotal = 0;
 | 
		
	
		
			
			| 93 | 82 |  				for(var i = startDateField.year(); i <= endDateField.year(); i++)
 | 
		
	
		
			
			| 94 | 83 |  				{
 | 
		
	
		
			
			| 95 |  | -					var q = "SELECT (sum(quantity) / :conversion)::numeric(12,2) FROM purchase WHERE time >= '" + i + "-01-01' AND time < '" + (i+1) + "-01-01'";
 | 
		
	
		
			
			|  | 84 | +					output.writeStartElement("tr");
 | 
		
	
		
			
			|  | 85 | +					output.writeAttribute("id", "y"+i);
 | 
		
	
		
			
			|  | 86 | +					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'";
 | 
		
	
		
			
			| 96 | 87 |  					query.prepare(q);
 | 
		
	
		
			
			| 97 | 88 |  					query.bind(":conversion", unitBox.currentIndex == 0 ? 2.2 : 1);
 | 
		
	
		
			
			| 98 | 89 |  					query.exec();
 | 
		
	
		
			
			| 99 | 90 |  					query.next();
 | 
		
	
		
			
			|  | 91 | +					output.writeStartElement("th");
 | 
		
	
		
			
			|  | 92 | +					output.writeCharacters(i);
 | 
		
	
		
			
			|  | 93 | +					output.writeEndElement(); //th
 | 
		
	
		
			
			| 100 | 94 |  					output.writeTextElement("td", query.value(0));
 | 
		
	
		
			
			| 101 |  | -					pounds[j] = query.value(0);
 | 
		
	
		
			
			| 102 |  | -					j++;
 | 
		
	
		
			
			|  | 95 | +					output.writeTextElement("td", query.value(1));
 | 
		
	
		
			
			|  | 96 | +					output.writeTextElement("td", Number(query.value(2)).toFixed(2));
 | 
		
	
		
			
			|  | 97 | +					sacktotal += Number(query.value(0));
 | 
		
	
		
			
			|  | 98 | +					unittotal += Number(query.value(1));
 | 
		
	
		
			
			|  | 99 | +					costtotal += Number(query.value(2));
 | 
		
	
		
			
			|  | 100 | +					output.writeEndElement(); //tr
 | 
		
	
		
			
			| 103 | 101 |  				}
 | 
		
	
		
			
			| 104 |  | -				output.writeEndElement();
 | 
		
	
		
			
			| 105 |  | -				output.writeStartElement("tr");
 | 
		
	
		
			
			| 106 |  | -				output.writeTextElement("th", "Cost");
 | 
		
	
		
			
			| 107 |  | -				j = 0;
 | 
		
	
		
			
			| 108 |  | -				var cost = new Array;
 | 
		
	
		
			
			| 109 |  | -				for(var i = startDateField.year(); i <= endDateField.year(); i++)
 | 
		
	
		
			
			| 110 |  | -				{
 | 
		
	
		
			
			| 111 |  | -					var q = "SELECT sum(cost*quantity)::numeric(12,2) FROM purchase WHERE time >= '" + i + "-01-01' AND time < '" + (i+1) + "-01-01'";
 | 
		
	
		
			
			| 112 |  | -					query.exec(q);
 | 
		
	
		
			
			| 113 |  | -					query.next();
 | 
		
	
		
			
			| 114 |  | -					output.writeTextElement("td", query.value(0));
 | 
		
	
		
			
			| 115 |  | -					cost[j] = query.value(0);
 | 
		
	
		
			
			| 116 |  | -					j++;
 | 
		
	
		
			
			| 117 |  | -				}
 | 
		
	
		
			
			| 118 |  | -				output.writeEndElement();
 | 
		
	
		
			
			| 119 |  | -				output.writeEndElement();
 | 
		
	
		
			
			|  | 102 | +				output.writeEndElement(); //tbody
 | 
		
	
		
			
			| 120 | 103 |  				output.writeStartElement("tfoot");
 | 
		
	
		
			
			| 121 |  | -				output.writeEndElement();
 | 
		
	
		
			
			| 122 |  | -				output.writeEndElement();
 | 
		
	
		
			
			| 123 |  | -				output.writeEndElement();
 | 
		
	
		
			
			| 124 |  | -				output.writeEndElement();
 | 
		
	
		
			
			|  | 104 | +				output.writeTextElement("th", "Totals:");
 | 
		
	
		
			
			|  | 105 | +				output.writeTextElement("td", sacktotal);
 | 
		
	
		
			
			|  | 106 | +				output.writeTextElement("td", unittotal.toFixed(2));
 | 
		
	
		
			
			|  | 107 | +				output.writeTextElement("td", costtotal.toFixed(2));
 | 
		
	
		
			
			|  | 108 | +				output.writeEndElement(); //tfoot
 | 
		
	
		
			
			|  | 109 | +				output.writeEndElement(); //table
 | 
		
	
		
			
			|  | 110 | +				output.writeEndElement(); //body
 | 
		
	
		
			
			|  | 111 | +				output.writeEndElement(); //html
 | 
		
	
		
			
			| 125 | 112 |  				output.writeEndDocument();
 | 
		
	
		
			
			| 126 | 113 |  				view.setContent(buffer);
 | 
		
	
		
			
			| 127 | 114 |  				buffer.close();
 | 
		
	
		
			
			| 128 | 115 |  			}
 | 
		
	
		
			
			| 129 | 116 |  			refresh();
 | 
		
	
		
			
			|  | 117 | +			/* Update report as needed. */
 | 
		
	
		
			
			| 130 | 118 |  			startDateField.dateChanged.connect(function() {
 | 
		
	
		
			
			| 131 | 119 |  				refresh();
 | 
		
	
		
			
			| 132 | 120 |  			});
 | 
		
	
	
		
			
			|  | @@ -136,4 +124,3 @@
 | 
		
	
		
			
			| 136 | 124 |  		]]>
 | 
		
	
		
			
			| 137 | 125 |  	</program>
 | 
		
	
		
			
			| 138 | 126 |  </window>
 | 
		
	
		
			
			| 139 |  | -
 |