Skip to content

Commit

Permalink
Integrate measure products in MovedBoxes statistic
Browse files Browse the repository at this point in the history
  • Loading branch information
pylipp committed Sep 26, 2024
1 parent e7a98e5 commit 28e8c7a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
44 changes: 36 additions & 8 deletions back/boxtribute_server/business_logic/statistics/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
s.location_id,
s.size_id,
s.box_state_id,
s.display_unit_id,
s.measure_value,
s.product_id
FROM stock s
JOIN locations l ON s.location_id = l.id AND l.camp_id = %s
Expand All @@ -22,6 +24,8 @@
s.product_id AS stock_product_id,
s.size_id AS stock_size_id,
s.box_state_id AS stock_box_state_id,
s.display_unit_id AS stock_display_unit_id,
s.measure_value AS stock_measure_value,
h.record_id,
h.changes,
h.changedate,
Expand All @@ -48,6 +52,8 @@
h.to_int,
h.changes,
h.changedate,
h.stock_display_unit_id,
h.stock_measure_value,
IF(h.changes <> 'items',
-- The current change is NOT about number of items, hence the correct number of items
-- of the box at this time must be inferred
Expand Down Expand Up @@ -157,6 +163,8 @@
h.items AS number_of_items,
h.location_id,
h.product_id AS product,
h.stock_display_unit_id,
h.stock_measure_value,
h.size_id
FROM HistoryReconstruction h
WHERE h.changes = 'box_state_id'
Expand All @@ -169,6 +177,8 @@
h.items AS number_of_items,
h.location_id,
h.product_id AS product,
h.stock_display_unit_id,
h.stock_measure_value,
h.size_id
FROM HistoryReconstruction h
WHERE h.changes = 'Record deleted'
Expand All @@ -181,6 +191,8 @@
h.items AS number_of_items,
h.location_id,
h.product_id AS product,
h.stock_display_unit_id,
h.stock_measure_value,
h.size_id
FROM HistoryReconstruction h
WHERE h.changes = 'Box was undeleted.'
Expand All @@ -193,6 +205,8 @@
h.items AS number_of_items,
h.location_id,
h.product_id AS product,
h.stock_display_unit_id,
h.stock_measure_value,
h.size_id
FROM HistoryReconstruction h
WHERE h.changes = 'Record created' and h.box_state_id = 5
Expand All @@ -208,6 +222,7 @@
TRIM(LOWER(p.name)) AS product_name,
p.gender_id AS gender,
t.size_id,
CONCAT(ROUND(t.stock_measure_value * u.conversion_factor, 2), u.symbol) AS measure_name,
GROUP_CONCAT(DISTINCT tr.tag_id) AS tag_ids,
"Deleted" AS target_id,
NULL AS organisation_name,
Expand All @@ -217,8 +232,9 @@
FROM DeletedBoxes t
JOIN products p ON p.id = t.product
JOIN locations loc ON loc.id = t.location_id
LEFT OUTER JOIN units u ON u.id = t.stock_display_unit_id
LEFT OUTER JOIN tags_relations tr ON tr.object_id = t.box_id AND tr.object_type = "Stock" AND tr.deleted_on IS NULL
GROUP BY moved_on, p.category_id, p.name, p.gender_id, t.size_id, loc.label
GROUP BY moved_on, p.category_id, p.name, p.gender_id, t.size_id, loc.label, measure_name
UNION ALL
Expand All @@ -228,6 +244,7 @@
TRIM(LOWER(p.name)) AS product_name,
p.gender_id AS gender,
t.size_id,
CONCAT(ROUND(t.stock_measure_value * u.conversion_factor, 2), u.symbol) AS measure_name,
GROUP_CONCAT(DISTINCT tr.tag_id) AS tag_ids,
"Deleted" AS target_id,
NULL AS organisation_name,
Expand All @@ -237,18 +254,20 @@
FROM UndeletedBoxes t
JOIN products p ON p.id = t.product
JOIN locations loc ON loc.id = t.location_id
LEFT OUTER JOIN units u ON u.id = t.stock_display_unit_id
LEFT OUTER JOIN tags_relations tr ON tr.object_id = t.box_id AND tr.object_type = "Stock" AND tr.deleted_on IS NULL
GROUP BY moved_on, p.category_id, p.name, p.gender_id, t.size_id, loc.label
GROUP BY moved_on, p.category_id, p.name, p.gender_id, t.size_id, loc.label, measure_name
UNION ALL
-- Collect information about boxes created in donated state
select
SELECT
t.moved_on,
p.category_id,
TRIM(LOWER(p.name)) AS product_name,
p.gender_id AS gender,
t.size_id,
CONCAT(ROUND(t.stock_measure_value * u.conversion_factor, 2), u.symbol) AS measure_name,
GROUP_CONCAT(DISTINCT tr.tag_id) AS tag_ids,
loc.label AS target_id,
NULL AS organisation_name,
Expand All @@ -258,18 +277,20 @@
FROM CreatedDonatedBoxes t
JOIN products p ON p.id = t.product
JOIN locations loc ON loc.id = t.location_id
LEFT OUTER JOIN units u ON u.id = t.stock_display_unit_id
LEFT OUTER JOIN tags_relations tr ON tr.object_id = t.box_id AND tr.object_type = "Stock" AND tr.deleted_on IS NULL
GROUP BY moved_on, p.category_id, p.name, p.gender_id, t.size_id, loc.label
GROUP BY moved_on, p.category_id, p.name, p.gender_id, t.size_id, loc.label, measure_name
UNION ALL
-- Collect information about boxes being moved between states InStock and Donated
select
SELECT
t.moved_on,
p.category_id,
TRIM(LOWER(p.name)) AS product_name,
p.gender_id AS gender,
t.size_id,
CONCAT(ROUND(t.stock_measure_value * u.conversion_factor, 2), u.symbol) AS measure_name,
GROUP_CONCAT(DISTINCT tr.tag_id) AS tag_ids,
loc.label AS target_id,
NULL AS organisation_name,
Expand All @@ -291,10 +312,11 @@
FROM BoxStateChangeVersions t
JOIN products p ON p.id = t.product
JOIN locations loc ON loc.id = t.location_id
LEFT OUTER JOIN units u ON u.id = t.stock_display_unit_id
LEFT OUTER JOIN tags_relations tr ON tr.object_id = t.box_id AND tr.object_type = "Stock" AND tr.deleted_on IS NULL
WHERE (t.prev_box_state_id = 1 AND t.box_state_id = 5) OR
(t.prev_box_state_id = 5 AND t.box_state_id = 1)
GROUP BY moved_on, p.category_id, p.name, p.gender_id, t.size_id, loc.label
GROUP BY moved_on, p.category_id, p.name, p.gender_id, t.size_id, loc.label, measure_name
UNION ALL
Expand All @@ -306,6 +328,8 @@
TRIM(LOWER(p.name)) AS product_name,
p.gender_id AS gender,
d.source_size_id AS size_id,
-- neglect possible history of box's measure_value
CONCAT(ROUND(b.measure_value * u.conversion_factor, 2), u.symbol) AS measure_name,
GROUP_CONCAT(DISTINCT tr.tag_id) AS tag_ids,
c.name AS target_id,
o.label AS organisation_name,
Expand All @@ -324,8 +348,10 @@
JOIN camps c ON c.id = sh.target_base_id
JOIN organisations o on o.id = c.organisation_id
JOIN products p ON p.id = d.source_product_id
JOIN stock b ON b.id = d.box_id
LEFT OUTER JOIN units u ON u.id = b.display_unit_id
LEFT OUTER JOIN tags_relations tr ON tr.object_id = d.box_id AND tr.object_type = "Stock" AND tr.deleted_on IS NULL
GROUP BY moved_on, p.category_id, p.name, p.gender_id, d.source_size_id, c.name
GROUP BY moved_on, p.category_id, p.name, p.gender_id, d.source_size_id, c.name, measure_name
UNION ALL
Expand All @@ -338,6 +364,7 @@
TRIM(LOWER(p.name)) AS product_name,
p.gender_id AS gender,
b.size_id,
CONCAT(ROUND(b.measure_value * u.conversion_factor, 2), u.symbol) AS measure_name,
GROUP_CONCAT(DISTINCT tr.tag_id) AS tag_ids,
bs.label AS target_id,
NULL AS organisation_name,
Expand All @@ -356,7 +383,8 @@
h.to_int IN (2, 6) -- (Lost, Scrap)
JOIN products p ON p.id = b.product_id AND p.camp_id = %s
JOIN box_state bs on bs.id = h.to_int
LEFT OUTER JOIN units u ON u.id = b.display_unit_id
LEFT OUTER JOIN tags_relations tr ON tr.object_id = b.box_id AND tr.object_type = "Stock" AND tr.deleted_on IS NULL
GROUP BY moved_on, p.category_id, p.name, p.gender_id, b.size_id, bs.label
GROUP BY moved_on, p.category_id, p.name, p.gender_id, b.size_id, bs.label, measure_name
;
"""
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ type MovedBoxesResult {
productName: String!
gender: ProductGender!
sizeId: Int
measureName: String
" Shipment target organisation name; null for BoxState/OutgoingLocation target types "
organisationName: String
tagIds: [Int!]
Expand Down
8 changes: 7 additions & 1 deletion back/test/endpoint_tests/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def test_query_moved_boxes(
query = """query { movedBoxes(baseId: 1) {
facts {
movedOn targetId categoryId productName gender sizeId tagIds
organisationName boxesCount itemsCount
measureName organisationName boxesCount itemsCount
}
dimensions { target { id name type } }
} }"""
Expand All @@ -219,6 +219,7 @@ def test_query_moved_boxes(
"categoryId": 1,
"productName": "jackets",
"sizeId": 2,
"measureName": None,
"gender": "Women",
"targetId": location_name,
"organisationName": None,
Expand All @@ -231,6 +232,7 @@ def test_query_moved_boxes(
"categoryId": 1,
"productName": "indigestion tablets",
"sizeId": 1,
"measureName": None,
"gender": "Women",
"targetId": location_name,
"organisationName": None,
Expand All @@ -243,6 +245,7 @@ def test_query_moved_boxes(
"categoryId": 12,
"productName": "joggers",
"sizeId": 1,
"measureName": None,
"gender": "Boy",
"targetId": location_name,
"organisationName": None,
Expand All @@ -255,6 +258,7 @@ def test_query_moved_boxes(
"categoryId": 1,
"productName": "indigestion tablets",
"sizeId": 1,
"measureName": None,
"gender": "Women",
"targetId": base_name,
"organisationName": org_name,
Expand All @@ -267,6 +271,7 @@ def test_query_moved_boxes(
"categoryId": 1,
"productName": "new product",
"sizeId": 1,
"measureName": None,
"gender": "Women",
"targetId": base_name,
"organisationName": org_name,
Expand All @@ -279,6 +284,7 @@ def test_query_moved_boxes(
"categoryId": 1,
"productName": "indigestion tablets",
"sizeId": 1,
"measureName": None,
"gender": "Women",
"targetId": BoxState.Lost.name,
"organisationName": None,
Expand Down
1 change: 1 addition & 0 deletions front/src/types/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ export type MovedBoxesResult = {
categoryId: Scalars['Int'];
gender: ProductGender;
itemsCount: Scalars['Int'];
measureName?: Maybe<Scalars['String']>;
movedOn: Scalars['Date'];
/** Shipment target organisation name; null for BoxState/OutgoingLocation target types */
organisationName?: Maybe<Scalars['String']>;
Expand Down

0 comments on commit 28e8c7a

Please sign in to comment.