Browse Source

Unit selection for previous year production comparison report. Table done, graphic needs work.

Neal Wilson 11 years ago
parent
commit
8cc50e6896
1 changed files with 31 additions and 19 deletions
  1. 31
    19
      config/Reports/chart.xml

+ 31
- 19
config/Reports/chart.xml View File

@@ -8,6 +8,8 @@
8 8
             <calendar id="enddate" />
9 9
             <label>Days to Average</label>
10 10
             <line validator="integer" id="days">7</line>
11
+			<label>Weight Unit:</label>
12
+			<sqldrop id="unit" />
11 13
             <stretch />
12 14
         </layout>
13 15
         <webview id="report" />
@@ -27,7 +29,18 @@
27 29
                 view.print();
28 30
             });
29 31
             var avgField = findChildObject(this, 'days');
32
+			unitBox.addItem("Kg");
33
+			unitBox.addItem("Lb");
34
+			unitBox.currentIndex = QSettings.value("script/report_unit", 1);
35
+			unitBox['currentIndexChanged(int)'].connect(function() {
36
+				QSettings.setValue("script/report_unit", unitBox.currentIndex);
37
+				refresh();
38
+			});
30 39
             function refresh() {
40
+				var conversion = 1;
41
+				if(unitBox.currentIndex == 0) {
42
+					conversion = 2.2;
43
+				}
31 44
                 var buffer = new QBuffer;
32 45
                 buffer.open(3);
33 46
                 var output = new XmlWriter(buffer);
@@ -48,57 +61,56 @@
48 61
                 output.writeStartElement("thead");
49 62
                 output.writeStartElement("tr");
50 63
                 output.writeTextElement("th", "Coffee");
51
-                output.writeTextElement("th", "Previous");
52
-                output.writeTextElement("th", "Current");
53
-                output.writeTextElement("th", "Change");
64
+				switch(unitBox.currentIndex) {
65
+					case 0:
66
+						output.writeTextElement("th", "Previous (Kg)");
67
+						output.writeTextElement("th", "Current (Kg)");
68
+						output.writeTextElement("th", "Change (Kg)");
69
+						break;
70
+					case 1:
71
+						output.writeTextElement("th", "Previous (Lb)");
72
+						output.writeTextElement("th", "Current (Lb)");
73
+						output.writeTextElement("th", "Change (Lb)");
74
+						break;
75
+				}
54 76
                 output.writeEndElement();
55 77
                 output.writeEndElement();
56 78
                 output.writeStartElement("tbody");
57 79
                 var query = new QSqlQuery();
58 80
                 query.exec("START TRANSACTION");
59
-                print(query.executedQuery());
60 81
                 var curStartDate = "'"+startDateField.year()+"-"+startDateField.month()+"-"+startDateField.day()+"'";
61 82
                 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 83
                 query.next();
64 84
                 var curEndDate = "'"+query.value(2)+"'";
65 85
                 var prevStartDate = "'"+query.value(0)+"'";
66 86
                 var prevEndDate = "'"+query.value(1)+"'";
67 87
                 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 88
                 query.exec(q);
69
-                print(query.executedQuery());
70 89
                 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 90
                 query.exec(q);
72
-                print(query.executedQuery());
73 91
                 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 92
                 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 93
                 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 94
                 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 95
                 while(query.next())
82 96
                 {
83 97
                     output.writeStartElement("tr");
84 98
                     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));
99
+					output.writeTextElement("td", query.value(1) / conversion);
100
+					output.writeTextElement("td", query.value(2) / conversion);
101
+					output.writeTextElement("td", query.value(3) / conversion);
88 102
                     output.writeEndElement();
89 103
                 }
90 104
                 output.writeEndElement();
91 105
                 output.writeStartElement("tfoot");
92 106
                 output.writeTextElement("th", "Totals");
93 107
                 query.exec("SELECT sum(p), sum(c), sum(c-p) FROM comp");
94
-                print(query.executedQuery());
95 108
                 query.next();
96
-                output.writeTextElement("td", query.value(0));
97
-                output.writeTextElement("td", query.value(1));
98
-                output.writeTextElement("td", query.value(2));
109
+				output.writeTextElement("td", query.value(0) / conversion);
110
+				output.writeTextElement("td", query.value(1) / conversion);
111
+				output.writeTextElement("td", query.value(2) / conversion);
99 112
                 output.writeEndElement();
100 113
                 query.exec("ABORT");
101
-                print(query.executedQuery());
102 114
                 output.writeEndElement();
103 115
                 output.writeStartElement("svg");
104 116
                 output.writeAttribute("xmlns", "http://www.w3.org/2000/svg");

Loading…
Cancel
Save