Browse Source

Add unit selection to green coffees used for roasted coffees report

Neal Wilson 8 years ago
parent
commit
411c2f6bff
1 changed files with 24 additions and 6 deletions
  1. 24
    6
      config/Reports/greenforroasted.xml

+ 24
- 6
config/Reports/greenforroasted.xml View File

@@ -3,6 +3,8 @@
3 3
     <layout type="vertical">
4 4
         <layout type="horizontal">
5 5
             <daterange id="dates" initial="19" /><!-- Year to Date -->
6
+            <label>Weight Unit:</label>
7
+            <sqldrop id="unit" />
6 8
             <stretch />
7 9
         </layout>
8 10
         <webview id="report" />
@@ -32,10 +34,24 @@
32 34
             dateSelect.setLifetimeRange(lifetimeStartDate, lifetimeEndDate);
33 35
             dateQuery = dateQuery.invalidate();
34 36
             dateSelect.rangeUpdated.connect(refresh);
37
+            var unitBox = findChildObject(this, 'unit');
38
+            unitBox.addItem(TTR("greenforroasted", "Kg"));
39
+            unitBox.addItem(TTR("greenforroasted", "Lb"));
40
+            unitBox.currentIndex = QSettings.value("script/report_unit", 1);
41
+            unitBox['currentIndexChanged(int)'].connect(function() {
42
+                QSettings.setValue("script/report_unit", unitBox.currentIndex);
43
+                refresh();
44
+            });
35 45
             function refresh() {
36 46
                 var dateRange = dateSelect.currentRange();
37 47
                 var startDate = dateRange[0];
38 48
                 var endDate = dateRange[dateRange.length - 1];
49
+                var conversion = 1;
50
+                var unitText = TTR("greensales", "Lb");
51
+                if(unitBox.currentIndex == 0) {
52
+                    conversion = 2.2;
53
+                    unitText = TTR("greensales", "Kg");
54
+                }
39 55
                 var buffer = new QBuffer;
40 56
                 buffer.open(3);
41 57
                 var output = new XmlWriter(buffer);
@@ -50,11 +66,13 @@
50 66
                 output.writeTextElement("h1", TTR("greenforroasted", "Green Coffees Used for Roasted Coffees ") + startDate + "-" + endDate);
51 67
                 output.writeStartElement("ul");
52 68
                 var query = new QSqlQuery();
53
-                query.prepare("WITH q AS (SELECT roasted_id, unroasted_id, (SELECT name FROM items WHERE id = roasted_id) AS rname, generate_subscripts(unroasted_id, 1) AS s, sum(roasted_quantity) AS rq, min(time)::date, max(time)::date FROM roasting_log WHERE time >= :sd1 AND time < :ed1 ::date + interval '1 day' GROUP BY roasted_id, unroasted_id) SELECT q.*, (SELECT name FROM items WHERE id = unroasted_id[q.s]) || ' (' || unroasted_id[q.s] || ')' AS gname, (SELECT SUM(unroasted_quantity[q.s]) AS gq FROM roasting_log WHERE roasted_id = q.roasted_id AND unroasted_id = q.unroasted_id AND time >= :sd2 AND time < :ed2 ::date + interval '1 day') FROM q ORDER BY q.rname ASC, min ASC");
69
+                query.prepare("WITH q AS (SELECT roasted_id, unroasted_id, (SELECT name FROM items WHERE id = roasted_id) AS rname, generate_subscripts(unroasted_id, 1) AS s, sum(roasted_quantity)/:c1 AS rq, min(time)::date, max(time)::date FROM roasting_log WHERE time >= :sd1 AND time < :ed1 ::date + interval '1 day' GROUP BY roasted_id, unroasted_id) SELECT q.*, (SELECT name FROM items WHERE id = unroasted_id[q.s]) || ' (' || unroasted_id[q.s] || ')' AS gname, (SELECT SUM(unroasted_quantity[q.s])/:c2 AS gq FROM roasting_log WHERE roasted_id = q.roasted_id AND unroasted_id = q.unroasted_id AND time >= :sd2 AND time < :ed2 ::date + interval '1 day') FROM q ORDER BY q.rname ASC, min ASC");
54 70
                 query.bind(":sd1", startDate);
55 71
                 query.bind(":sd2", startDate);
56 72
                 query.bind(":ed1", endDate);
57 73
                 query.bind(":ed2", endDate);
74
+                query.bind(":c1", conversion);
75
+                query.bind(":c2", conversion);
58 76
                 query.exec();
59 77
                 var prevRid = -1;
60 78
                 var prevGid = "";
@@ -69,18 +87,18 @@
69 87
                         }
70 88
                         output.writeTextElement("li", query.value(2) + " (" + query.value(0) + ")");
71 89
                         output.writeStartElement("ul");
72
-                        output.writeTextElement("li", query.value(4) + " pounds roasted between " + query.value(5) + " and " + query.value(6) + " with:");
90
+                        output.writeTextElement("li", query.value(4) + " " + unitText + " roasted between " + query.value(5) + " and " + query.value(6) + " with:");
73 91
                         output.writeStartElement("ul");
74
-                        output.writeTextElement("li", query.value(8) + " pounds of " + query.value(7));
92
+                        output.writeTextElement("li", query.value(8) + " " + unitText + " of " + query.value(7));
75 93
                     }
76 94
                     else { // Same roasted item
77 95
                         if(query.value(1) != prevGid) { // New set of greens
78 96
                             output.writeEndElement(); //ul
79
-                            output.writeTextElement("li", query.value(4) + " pounds roasted between " + query.value(5) + " and " + query.value(6) + " with:");
97
+                            output.writeTextElement("li", query.value(4) + " " + unitText + " roasted between " + query.value(5) + " and " + query.value(6) + " with:");
80 98
                             output.writeStartElement("ul");
81
-                            output.writeTextElement("li", query.value(8) + " pounds of " + query.value(7));
99
+                            output.writeTextElement("li", query.value(8) + " " + unitText + " of " + query.value(7));
82 100
                         } else { // Same set of greens
83
-                            output.writeTextElement("li", query.value(8) + " pounds of " + query.value(7));
101
+                            output.writeTextElement("li", query.value(8) + " " + unitText + " of " + query.value(7));
84 102
                         }
85 103
                     }
86 104
                     prevRid = query.value(0);

Loading…
Cancel
Save