Browse Source

Merge branch 'unitselect' into development

Neal Wilson 12 years ago
parent
commit
d815137f08
1 changed files with 72 additions and 25 deletions
  1. 72
    25
      config/Reports/chart.xml

+ 72
- 25
config/Reports/chart.xml View File

8
             <calendar id="enddate" />
8
             <calendar id="enddate" />
9
             <label>Days to Average</label>
9
             <label>Days to Average</label>
10
             <line validator="integer" id="days">7</line>
10
             <line validator="integer" id="days">7</line>
11
+			<label>Weight Unit:</label>
12
+			<sqldrop id="unit" />
11
             <stretch />
13
             <stretch />
12
         </layout>
14
         </layout>
13
         <webview id="report" />
15
         <webview id="report" />
27
                 view.print();
29
                 view.print();
28
             });
30
             });
29
             var avgField = findChildObject(this, 'days');
31
             var avgField = findChildObject(this, 'days');
32
+			var unitBox = findChildObject(this, 'unit');
33
+			unitBox.addItem("Kg");
34
+			unitBox.addItem("Lb");
35
+			unitBox.currentIndex = QSettings.value("script/report_unit", 1);
36
+			unitBox['currentIndexChanged(int)'].connect(function() {
37
+				QSettings.setValue("script/report_unit", unitBox.currentIndex);
38
+				refresh();
39
+			});
30
             function refresh() {
40
             function refresh() {
41
+				var conversion = 1;
42
+				if(unitBox.currentIndex == 0) {
43
+					conversion = 2.2;
44
+				}
31
                 var buffer = new QBuffer;
45
                 var buffer = new QBuffer;
32
                 buffer.open(3);
46
                 buffer.open(3);
33
                 var output = new XmlWriter(buffer);
47
                 var output = new XmlWriter(buffer);
48
                 output.writeStartElement("thead");
62
                 output.writeStartElement("thead");
49
                 output.writeStartElement("tr");
63
                 output.writeStartElement("tr");
50
                 output.writeTextElement("th", "Coffee");
64
                 output.writeTextElement("th", "Coffee");
51
-                output.writeTextElement("th", "Previous");
52
-                output.writeTextElement("th", "Current");
53
-                output.writeTextElement("th", "Change");
65
+				switch(unitBox.currentIndex) {
66
+					case 0:
67
+						output.writeTextElement("th", "Previous (Kg)");
68
+						output.writeTextElement("th", "Current (Kg)");
69
+						output.writeTextElement("th", "Change (Kg)");
70
+						break;
71
+					case 1:
72
+						output.writeTextElement("th", "Previous (Lb)");
73
+						output.writeTextElement("th", "Current (Lb)");
74
+						output.writeTextElement("th", "Change (Lb)");
75
+						break;
76
+				}
54
                 output.writeEndElement();
77
                 output.writeEndElement();
55
                 output.writeEndElement();
78
                 output.writeEndElement();
56
                 output.writeStartElement("tbody");
79
                 output.writeStartElement("tbody");
57
                 var query = new QSqlQuery();
80
                 var query = new QSqlQuery();
58
                 query.exec("START TRANSACTION");
81
                 query.exec("START TRANSACTION");
59
-                print(query.executedQuery());
60
                 var curStartDate = "'"+startDateField.year()+"-"+startDateField.month()+"-"+startDateField.day()+"'";
82
                 var curStartDate = "'"+startDateField.year()+"-"+startDateField.month()+"-"+startDateField.day()+"'";
61
                 query.exec("SELECT "+curStartDate+"::date - interval '1 year', '"+endDateField.year()+"-"+endDateField.month()+"-"+endDateField.day()+"'::date - interval '1 year' + interval '1 day', '"+endDateField.year()+"-"+endDateField.month()+"-"+endDateField.day()+"'::date + interval '1 day'");
83
                 query.exec("SELECT "+curStartDate+"::date - interval '1 year', '"+endDateField.year()+"-"+endDateField.month()+"-"+endDateField.day()+"'::date - interval '1 year' + interval '1 day', '"+endDateField.year()+"-"+endDateField.month()+"-"+endDateField.day()+"'::date + interval '1 day'");
62
-                print(query.executedQuery());
63
                 query.next();
84
                 query.next();
64
                 var curEndDate = "'"+query.value(2)+"'";
85
                 var curEndDate = "'"+query.value(2)+"'";
65
                 var prevStartDate = "'"+query.value(0)+"'";
86
                 var prevStartDate = "'"+query.value(0)+"'";
66
                 var prevEndDate = "'"+query.value(1)+"'";
87
                 var prevEndDate = "'"+query.value(1)+"'";
67
                 var q = "CREATE TEMPORARY TABLE previous ON COMMIT DROP AS SELECT roasted_id, sum(roasted_quantity) AS p FROM roasting_log WHERE time > "+prevStartDate+" AND time < "+prevEndDate+" AND roasted_id IS NOT NULL GROUP BY roasted_id";
88
                 var q = "CREATE TEMPORARY TABLE previous ON COMMIT DROP AS SELECT roasted_id, sum(roasted_quantity) AS p FROM roasting_log WHERE time > "+prevStartDate+" AND time < "+prevEndDate+" AND roasted_id IS NOT NULL GROUP BY roasted_id";
68
                 query.exec(q);
89
                 query.exec(q);
69
-                print(query.executedQuery());
70
                 q = "CREATE TEMPORARY TABLE current ON COMMIT DROP AS SELECT roasted_id, sum(roasted_quantity) AS c FROM roasting_log WHERE time > "+curStartDate+" AND time < "+curEndDate+" AND roasted_id IS NOT NULL GROUP BY roasted_id";
90
                 q = "CREATE TEMPORARY TABLE current ON COMMIT DROP AS SELECT roasted_id, sum(roasted_quantity) AS c FROM roasting_log WHERE time > "+curStartDate+" AND time < "+curEndDate+" AND roasted_id IS NOT NULL GROUP BY roasted_id";
71
                 query.exec(q);
91
                 query.exec(q);
72
-                print(query.executedQuery());
73
                 query.exec("INSERT INTO previous SELECT roasted_id, 0 FROM current WHERE roasted_id NOT IN (SELECT roasted_id FROM previous)");
92
                 query.exec("INSERT INTO previous SELECT roasted_id, 0 FROM current WHERE roasted_id NOT IN (SELECT roasted_id FROM previous)");
74
-                print(query.executedQuery());
75
                 query.exec("INSERT INTO current SELECT roasted_id, 0 FROM previous WHERE roasted_id NOT IN (SELECT roasted_id FROM current)");
93
                 query.exec("INSERT INTO current SELECT roasted_id, 0 FROM previous WHERE roasted_id NOT IN (SELECT roasted_id FROM current)");
76
-                print(query.executedQuery());
77
                 query.exec("CREATE TEMPORARY TABLE comp ON COMMIT DROP AS SELECT previous.roasted_id, p, c FROM previous LEFT OUTER JOIN current ON previous.roasted_id = current.roasted_id");
94
                 query.exec("CREATE TEMPORARY TABLE comp ON COMMIT DROP AS SELECT previous.roasted_id, p, c FROM previous LEFT OUTER JOIN current ON previous.roasted_id = current.roasted_id");
78
-                print(query.executedQuery());
79
                 query.exec("SELECT (SELECT name FROM items WHERE id = roasted_id) AS name, p, c, (c-p) FROM comp WHERE p > 0 OR c > 0 ORDER BY name");
95
                 query.exec("SELECT (SELECT name FROM items WHERE id = roasted_id) AS name, p, c, (c-p) FROM comp WHERE p > 0 OR c > 0 ORDER BY name");
80
-                print(query.executedQuery());
81
                 while(query.next())
96
                 while(query.next())
82
                 {
97
                 {
83
                     output.writeStartElement("tr");
98
                     output.writeStartElement("tr");
84
                     output.writeTextElement("td", query.value(0));
99
                     output.writeTextElement("td", query.value(0));
85
-                    output.writeTextElement("td", query.value(1));
86
-                    output.writeTextElement("td", query.value(2));
87
-                    output.writeTextElement("td", query.value(3));
100
+					output.writeTextElement("td", (query.value(1) / conversion).toFixed(2));
101
+					output.writeTextElement("td", (query.value(2) / conversion).toFixed(2));
102
+					output.writeTextElement("td", (query.value(3) / conversion).toFixed(2));
88
                     output.writeEndElement();
103
                     output.writeEndElement();
89
                 }
104
                 }
90
                 output.writeEndElement();
105
                 output.writeEndElement();
91
                 output.writeStartElement("tfoot");
106
                 output.writeStartElement("tfoot");
92
                 output.writeTextElement("th", "Totals");
107
                 output.writeTextElement("th", "Totals");
93
                 query.exec("SELECT sum(p), sum(c), sum(c-p) FROM comp");
108
                 query.exec("SELECT sum(p), sum(c), sum(c-p) FROM comp");
94
-                print(query.executedQuery());
95
                 query.next();
109
                 query.next();
96
-                output.writeTextElement("td", query.value(0));
97
-                output.writeTextElement("td", query.value(1));
98
-                output.writeTextElement("td", query.value(2));
110
+				output.writeTextElement("td", (query.value(0) / conversion).toFixed(2));
111
+				output.writeTextElement("td", (query.value(1) / conversion).toFixed(2));
112
+				output.writeTextElement("td", (query.value(2) / conversion).toFixed(2));
99
                 output.writeEndElement();
113
                 output.writeEndElement();
100
                 query.exec("ABORT");
114
                 query.exec("ABORT");
101
-                print(query.executedQuery());
102
                 output.writeEndElement();
115
                 output.writeEndElement();
103
                 output.writeStartElement("svg");
116
                 output.writeStartElement("svg");
104
                 output.writeAttribute("xmlns", "http://www.w3.org/2000/svg");
117
                 output.writeAttribute("xmlns", "http://www.w3.org/2000/svg");
192
                         avgchange[i] = sum / parseInt(avgField.text);
205
                         avgchange[i] = sum / parseInt(avgField.text);
193
                     }
206
                     }
194
                 }
207
                 }
208
+				// Calculate the domain of the primary y axis.
195
                 var maxy1 = 0;
209
                 var maxy1 = 0;
210
+				var increment = 100; // Starting increment is 100 Lb.
211
+				if(unitBox.currentIndex == 0) {
212
+					increment = 110; // Starting increment is 50 Kg if Kg is requested.
213
+				}
196
                 while(maxy1 < Math.max(curpounds[days - 1], prevpounds[days - 1]))
214
                 while(maxy1 < Math.max(curpounds[days - 1], prevpounds[days - 1]))
197
                 {
215
                 {
198
-                    maxy1 += 100;
216
+					maxy1 += increment;
199
                 }
217
                 }
218
+				// Calculate the number of grid lines and loosen spacing if there are too many.
219
+				var divisions = maxy1 / increment;
220
+				while(divisions > 10) {
221
+					increment *= 2;
222
+					divisions = maxy1 / increment;
223
+				}
224
+				// Draw y axis grid lines.
200
                 var pos = 0;
225
                 var pos = 0;
201
                 while(pos <= maxy1)
226
                 while(pos <= maxy1)
202
                 {
227
                 {
207
                     output.writeAttribute("y2", (450/maxy1)*pos);
232
                     output.writeAttribute("y2", (450/maxy1)*pos);
208
                     output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
233
                     output.writeAttribute("style", "stroke:rgb(0,0,0);stroke-width:1;");
209
                     output.writeEndElement();
234
                     output.writeEndElement();
210
-                    pos += 100;
235
+					pos += increment;
211
                 }
236
                 }
212
                 var n = Math.min.apply(Math, change);
237
                 var n = Math.min.apply(Math, change);
213
                 var m = Math.max.apply(Math, change);
238
                 var m = Math.max.apply(Math, change);
310
                     output.writeAttribute("x", "0");
335
                     output.writeAttribute("x", "0");
311
                     output.writeAttribute("y", -((450/maxy1)*i)+5);
336
                     output.writeAttribute("y", -((450/maxy1)*i)+5);
312
                     output.writeAttribute("font-size", "12");
337
                     output.writeAttribute("font-size", "12");
313
-                    output.writeCharacters(i);
338
+					switch(unitBox.currentIndex) {
339
+						case 0:
340
+							output.writeCharacters((i / 2.2).toFixed(0));
341
+							break;
342
+						case 1:
343
+						default:
344
+							output.writeCharacters(i);
345
+							break;
346
+					}
314
                     output.writeEndElement();
347
                     output.writeEndElement();
315
-                    i += 100;
348
+                    i += increment;
316
                 }
349
                 }
317
                 i = miny2;
350
                 i = miny2;
318
                 while(i <= maxy2)
351
                 while(i <= maxy2)
401
                 output.writeAttribute("x", "75");
434
                 output.writeAttribute("x", "75");
402
                 output.writeAttribute("y", "120");
435
                 output.writeAttribute("y", "120");
403
                 output.writeAttribute("font-size", "12");
436
                 output.writeAttribute("font-size", "12");
404
-                output.writeCharacters("Previous Year Pounds");
437
+				switch(unitBox.currentIndex) {
438
+					case 0:
439
+						output.writeCharacters("Previous Year Kg");
440
+						break;
441
+					case 1:
442
+						output.writeCharacters("Previous Year Lb");
443
+						break;
444
+				}
405
                 output.writeEndElement();
445
                 output.writeEndElement();
406
                 output.writeStartElement("rect");
446
                 output.writeStartElement("rect");
407
                 output.writeAttribute("fill", "rgb(255,0,0)");
447
                 output.writeAttribute("fill", "rgb(255,0,0)");
414
                 output.writeAttribute("x", "225");
454
                 output.writeAttribute("x", "225");
415
                 output.writeAttribute("y", "120");
455
                 output.writeAttribute("y", "120");
416
                 output.writeAttribute("font-size", "12");
456
                 output.writeAttribute("font-size", "12");
417
-                output.writeCharacters("Current Year Pounds");
457
+				switch(unitBox.currentIndex) {
458
+					case 0:
459
+						output.writeCharacters("Current Year Kg");
460
+						break;
461
+					case 1:
462
+						output.writeCharacters("Current Year Lb");
463
+						break;
464
+				}
418
                 output.writeEndElement();
465
                 output.writeEndElement();
419
                 output.writeStartElement("rect");
466
                 output.writeStartElement("rect");
420
                 output.writeAttribute("fill", "rgb(0,255,0)");
467
                 output.writeAttribute("fill", "rgb(0,255,0)");

Loading…
Cancel
Save