|
@@ -49,8 +49,16 @@
|
49
|
49
|
var startDate = "" + startDateField.year() + "-" + startDateField.month() + "-" + startDateField.day();
|
50
|
50
|
var endDate = "" + endDateField.year() + "-" + endDateField.month() + "-" + endDateField.day();
|
51
|
51
|
output.writeTextElement("h1", "Inventory Change Summary: " + startDate + " – " + endDate);
|
|
52
|
+ var conversion = 1;
|
|
53
|
+ if(unitBox.currentIndex == 0) {
|
|
54
|
+ conversion = 2.2;
|
|
55
|
+ }
|
|
56
|
+ var unitText = "Lb";
|
|
57
|
+ if(unitBox.currentIndex == 0) {
|
|
58
|
+ unitText = "Kg";
|
|
59
|
+ }
|
52
|
60
|
var query = new QSqlQuery();
|
53
|
|
- var q = "WITH q AS (SELECT id, name, reference, COALESCE((SELECT balance FROM item_history(id) WHERE time = (SELECT max(time) FROM item_history(id) WHERE time < :sd1)), 0) AS starting_balance, COALESCE((SELECT sum(quantity) FROM purchase WHERE item = id AND time >= :sd2 AND time < :ed1 ::date + interval '1 day'), 0) AS purchase, COALESCE((SELECT sum(quantity) FROM use WHERE item = id AND time >= :sd3 AND time < :ed2 ::date + interval '1 day'), 0) AS use, COALESCE((SELECT sum(quantity) FROM sale WHERE item = id AND time >= :sd4 AND time < :ed3 ::date + interval '1 day'), 0) AS sale, (SElECT balance FROM item_history(id) WHERE time = (SELECT max(time) FROM item_history(id) WHERE time < :ed4 ::date + interval '1 day')) AS quantity, (SELECT sum(cost * quantity) / sum(quantity) FROM purchase WHERE item = id) AS unit_cost FROM coffees WHERE id IN (SELECT item FROM purchase WHERE time >= :sd6 AND time < :ed5 ::date + interval '1 day') OR id IN (SELECT id FROM items WHERE (SELECT balance FROM item_history(id) WHERE time = (SELECT max(time) FROM item_history(id) WHERE time < :ed6 ::date + interval '1 day')) > 0) OR id IN (SELECT DISTINCT item FROM all_transactions WHERE time > :sd7 AND time < :ed7 ::date + interval '1 day')) SELECT *, (starting_balance + purchase - use - sale - quantity) AS adjustment, starting_balance * unit_cost AS starting_cost, purchase * unit_cost AS purchase_cost, use * unit_cost AS use_cost, sale * unit_cost AS sale_cost, quantity * unit_cost AS quantity_cost, (starting_balance + purchase - use - sale - quantity) * unit_cost AS adjustment_cost FROM q ORDER BY name";
|
|
61
|
+ var q = "WITH q AS (SELECT id, name, reference, COALESCE((SELECT balance FROM item_history(id) WHERE time = (SELECT max(time) FROM item_history(id) WHERE time < :sd1)), 0)/:c1 AS starting_balance, COALESCE((SELECT sum(quantity) FROM purchase WHERE item = id AND time >= :sd2 AND time < :ed1 ::date + interval '1 day'), 0)/:c2 AS purchase, COALESCE((SELECT sum(quantity) FROM use WHERE item = id AND time >= :sd3 AND time < :ed2 ::date + interval '1 day'), 0)/:c3 AS use, COALESCE((SELECT sum(quantity) FROM sale WHERE item = id AND time >= :sd4 AND time < :ed3 ::date + interval '1 day'), 0)/:c4 AS sale, (SElECT balance FROM item_history(id) WHERE time = (SELECT max(time) FROM item_history(id) WHERE time < :ed4 ::date + interval '1 day'))/:c5 AS quantity, (SELECT sum(cost * quantity) / sum(quantity) FROM purchase WHERE item = id)*:c6 AS unit_cost FROM coffees WHERE id IN (SELECT item FROM purchase WHERE time >= :sd6 AND time < :ed5 ::date + interval '1 day') OR id IN (SELECT id FROM items WHERE (SELECT balance FROM item_history(id) WHERE time = (SELECT max(time) FROM item_history(id) WHERE time < :ed6 ::date + interval '1 day')) > 0) OR id IN (SELECT DISTINCT item FROM all_transactions WHERE time > :sd7 AND time < :ed7 ::date + interval '1 day')) SELECT *, (starting_balance + purchase - use - sale - quantity)/:c7 AS adjustment, starting_balance * unit_cost * :c8 AS starting_cost, purchase * unit_cost * :c9 AS purchase_cost, use * unit_cost * :c10 AS use_cost, sale * unit_cost * :c11 AS sale_cost, quantity * unit_cost * :c12 AS quantity_cost, (starting_balance + purchase - use - sale - quantity) * unit_cost * :c13 AS adjustment_cost FROM q ORDER BY name";
|
54
|
62
|
query.prepare(q);
|
55
|
63
|
query.bind(":sd1", startDate);
|
56
|
64
|
query.bind(":sd2", startDate);
|
|
@@ -65,6 +73,19 @@
|
65
|
73
|
query.bind(":ed5", endDate);
|
66
|
74
|
query.bind(":ed6", endDate);
|
67
|
75
|
query.bind(":ed7", endDate);
|
|
76
|
+ query.bind(":c1", conversion);
|
|
77
|
+ query.bind(":c2", conversion);
|
|
78
|
+ query.bind(":c3", conversion);
|
|
79
|
+ query.bind(":c4", conversion);
|
|
80
|
+ query.bind(":c5", conversion);
|
|
81
|
+ query.bind(":c6", conversion);
|
|
82
|
+ query.bind(":c7", conversion);
|
|
83
|
+ query.bind(":c8", conversion);
|
|
84
|
+ query.bind(":c9", conversion);
|
|
85
|
+ query.bind(":c10", conversion);
|
|
86
|
+ query.bind(":c11", conversion);
|
|
87
|
+ query.bind(":c12", conversion);
|
|
88
|
+ query.bind(":c13", conversion);
|
68
|
89
|
query.exec();
|
69
|
90
|
output.writeStartElement("table");
|
70
|
91
|
output.writeAttribute("rules", "groups");
|
|
@@ -74,17 +95,17 @@
|
74
|
95
|
output.writeTextElement("th", "ID"); // 0
|
75
|
96
|
output.writeTextElement("th", "Coffee"); // 1
|
76
|
97
|
output.writeTextElement("th", "Reference"); // 2
|
77
|
|
- output.writeTextElement("th", "Starting (Lb)"); // 3
|
|
98
|
+ output.writeTextElement("th", "Starting (" + unitText + ")"); // 3
|
78
|
99
|
output.writeTextElement("th", "Cost"); // 10
|
79
|
|
- output.writeTextElement("th", "Purchase (Lb)"); // 4
|
|
100
|
+ output.writeTextElement("th", "Purchase (" + unitText + ")"); // 4
|
80
|
101
|
output.writeTextElement("th", "Cost"); // 11
|
81
|
|
- output.writeTextElement("th", "Use (Lb)"); // 5
|
|
102
|
+ output.writeTextElement("th", "Use (" + unitText + ")"); // 5
|
82
|
103
|
output.writeTextElement("th", "Cost"); // 12
|
83
|
|
- output.writeTextElement("th", "Sale (Lb)"); // 6
|
|
104
|
+ output.writeTextElement("th", "Sale (" + unitText + ")"); // 6
|
84
|
105
|
output.writeTextElement("th", "Cost"); // 13
|
85
|
|
- output.writeTextElement("th", "Adjustment (Lb)"); // 9
|
|
106
|
+ output.writeTextElement("th", "Adjustment (" + unitText + ")"); // 9
|
86
|
107
|
output.writeTextElement("th", "Cost"); // 15
|
87
|
|
- output.writeTextElement("th", "Ending (Lb)"); // 7
|
|
108
|
+ output.writeTextElement("th", "Ending (" + unitText + ")"); // 7
|
88
|
109
|
output.writeTextElement("th", "Cost"); // 14
|
89
|
110
|
output.writeEndElement();
|
90
|
111
|
output.writeEndElement();
|