|
@@ -50,20 +50,21 @@
|
50
|
50
|
var endDate = "" + endDateField.year() + "-" + endDateField.month() + "-" + endDateField.day();
|
51
|
51
|
output.writeTextElement("h1", "Inventory Change Summary: " + startDate + " – " + endDate);
|
52
|
52
|
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 AND time >= :sd5 AND time < :ed5 ::date + interval '1 day') AS unit_cost FROM coffees WHERE id IN (SELECT item FROM purchase WHERE time >= :sd6) OR ((SELECT balance FROM item_history(id) WHERE time = (SELECT max(time) FROM item_history(id) WHERE time < :ed6 ::date + interval '1 day')) > 0)) 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";
|
|
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";
|
54
|
54
|
query.prepare(q);
|
55
|
55
|
query.bind(":sd1", startDate);
|
56
|
56
|
query.bind(":sd2", startDate);
|
57
|
57
|
query.bind(":sd3", startDate);
|
58
|
58
|
query.bind(":sd4", startDate);
|
59
|
|
- query.bind(":sd5", startDate);
|
60
|
59
|
query.bind(":sd6", startDate);
|
|
60
|
+ query.bind(":sd7", startDate);
|
61
|
61
|
query.bind(":ed1", endDate);
|
62
|
62
|
query.bind(":ed2", endDate);
|
63
|
63
|
query.bind(":ed3", endDate);
|
64
|
64
|
query.bind(":ed4", endDate);
|
65
|
65
|
query.bind(":ed5", endDate);
|
66
|
66
|
query.bind(":ed6", endDate);
|
|
67
|
+ query.bind(":ed7", endDate);
|
67
|
68
|
query.exec();
|
68
|
69
|
output.writeStartElement("table");
|
69
|
70
|
output.writeAttribute("rules", "groups");
|