Browse Source

Rotate coffee purchase report. Fixes #15

Neal Wilson 11 years ago
parent
commit
16385b94e1
1 changed files with 40 additions and 53 deletions
  1. 40
    53
      config/Reports/fypurchase.xml

+ 40
- 53
config/Reports/fypurchase.xml View File

13
 		<webview id="report" />
13
 		<webview id="report" />
14
 	</layout>
14
 	</layout>
15
 	<menu name="File">
15
 	<menu name="File">
16
-		<item id="print" shortcut="Ctrl+P">Print</item>
16
+		<item id="print" shortcut="Ctrl+P">Print...</item>
17
 	</menu>
17
 	</menu>
18
 	<program>
18
 	<program>
19
 		<![CDATA[
19
 		<![CDATA[
20
 			this.windowTitle = "Typica - Coffee Purchase Previous Years Comparison";
20
 			this.windowTitle = "Typica - Coffee Purchase Previous Years Comparison";
21
+			/* Set starting year to the first year on record. */
21
 			var startDateField = findChildObject(this, 'startdate');
22
 			var startDateField = findChildObject(this, 'startdate');
22
 			var query = new QSqlQuery();
23
 			var query = new QSqlQuery();
23
 			query.exec("SELECT EXTRACT(YEAR FROM time) FROM purchase WHERE time = (SELECT min(time) FROM purchase)");
24
 			query.exec("SELECT EXTRACT(YEAR FROM time) FROM purchase WHERE time = (SELECT min(time) FROM purchase)");
24
 			query.next();
25
 			query.next();
25
 			startDateField.setDate(query.value(0), 1, 1);
26
 			startDateField.setDate(query.value(0), 1, 1);
27
+			/* Set ending year to the current year. */
26
 			var endDateField = findChildObject(this, 'enddate');
28
 			var endDateField = findChildObject(this, 'enddate');
27
 			endDateField.setDate(endDateField.year(), 12, 31);
29
 			endDateField.setDate(endDateField.year(), 12, 31);
30
+			/* Enable printing */
28
 			var view = findChildObject(this, 'report');
31
 			var view = findChildObject(this, 'report');
29
 			var printMenu = findChildObject(this, 'print');
32
 			var printMenu = findChildObject(this, 'print');
30
 			printMenu.triggered.connect(function() {
33
 			printMenu.triggered.connect(function() {
31
 				view.print();
34
 				view.print();
32
 			});
35
 			});
36
+			/* Add units to unit selector and enable functionality */
33
 			var unitBox = findChildObject(this, 'unit');
37
 			var unitBox = findChildObject(this, 'unit');
34
 			unitBox.addItem("Kg");
38
 			unitBox.addItem("Kg");
35
 			unitBox.addItem("Lb");
39
 			unitBox.addItem("Lb");
38
 				QSettings.setValue("script/report_unit", unitBox.currentIndex);
42
 				QSettings.setValue("script/report_unit", unitBox.currentIndex);
39
 				refresh();
43
 				refresh();
40
 			});
44
 			});
45
+			/* Generate report */
41
 			function refresh() {
46
 			function refresh() {
42
 				var buffer = new QBuffer;
47
 				var buffer = new QBuffer;
43
 				buffer.open(3);
48
 				buffer.open(3);
52
 				output.writeStartElement("body");
57
 				output.writeStartElement("body");
53
 				output.writeTextElement("h1", "Coffee Purchase Previous Years Comparison");
58
 				output.writeTextElement("h1", "Coffee Purchase Previous Years Comparison");
54
 				output.writeStartElement("table");
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
 				output.writeStartElement("tr");
64
 				output.writeStartElement("tr");
65
+				output.writeEmptyElement("th");
69
 				output.writeTextElement("th", "Sacks Purchased");
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
 				switch(unitBox.currentIndex) {
67
 				switch(unitBox.currentIndex) {
84
 					case 0:
68
 					case 0:
85
 						output.writeTextElement("th", "Kilos Purchased");
69
 						output.writeTextElement("th", "Kilos Purchased");
88
 						output.writeTextElement("th", "Pounds Purchased");
72
 						output.writeTextElement("th", "Pounds Purchased");
89
 						break;
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
 				for(var i = startDateField.year(); i <= endDateField.year(); i++)
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
 					query.prepare(q);
87
 					query.prepare(q);
97
 					query.bind(":conversion", unitBox.currentIndex == 0 ? 2.2 : 1);
88
 					query.bind(":conversion", unitBox.currentIndex == 0 ? 2.2 : 1);
98
 					query.exec();
89
 					query.exec();
99
 					query.next();
90
 					query.next();
91
+					output.writeStartElement("th");
92
+					output.writeCharacters(i);
93
+					output.writeEndElement(); //th
100
 					output.writeTextElement("td", query.value(0));
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
 				output.writeStartElement("tfoot");
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
 				output.writeEndDocument();
112
 				output.writeEndDocument();
126
 				view.setContent(buffer);
113
 				view.setContent(buffer);
127
 				buffer.close();
114
 				buffer.close();
128
 			}
115
 			}
129
 			refresh();
116
 			refresh();
117
+			/* Update report as needed. */
130
 			startDateField.dateChanged.connect(function() {
118
 			startDateField.dateChanged.connect(function() {
131
 				refresh();
119
 				refresh();
132
 			});
120
 			});
136
 		]]>
124
 		]]>
137
 	</program>
125
 	</program>
138
 </window>
126
 </window>
139
-

Loading…
Cancel
Save