diff --git a/back/boxtribute_server/business_logic/statistics/sql.py b/back/boxtribute_server/business_logic/statistics/sql.py index 2dbd6b821..5a244ed3e 100644 --- a/back/boxtribute_server/business_logic/statistics/sql.py +++ b/back/boxtribute_server/business_logic/statistics/sql.py @@ -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 @@ -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, @@ -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 @@ -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' @@ -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' @@ -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.' @@ -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 @@ -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, @@ -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 @@ -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, @@ -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, @@ -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, @@ -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 @@ -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, @@ -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 @@ -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, @@ -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 ; """ diff --git a/back/boxtribute_server/graph_ql/definitions/public/types.graphql b/back/boxtribute_server/graph_ql/definitions/public/types.graphql index 422fc433a..1586c62e0 100644 --- a/back/boxtribute_server/graph_ql/definitions/public/types.graphql +++ b/back/boxtribute_server/graph_ql/definitions/public/types.graphql @@ -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!] diff --git a/back/test/endpoint_tests/test_statistics.py b/back/test/endpoint_tests/test_statistics.py index f84376cbd..a9aeb1366 100644 --- a/back/test/endpoint_tests/test_statistics.py +++ b/back/test/endpoint_tests/test_statistics.py @@ -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 } } } }""" @@ -219,6 +219,7 @@ def test_query_moved_boxes( "categoryId": 1, "productName": "jackets", "sizeId": 2, + "measureName": None, "gender": "Women", "targetId": location_name, "organisationName": None, @@ -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, @@ -243,6 +245,7 @@ def test_query_moved_boxes( "categoryId": 12, "productName": "joggers", "sizeId": 1, + "measureName": None, "gender": "Boy", "targetId": location_name, "organisationName": None, @@ -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, @@ -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, @@ -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, diff --git a/front/src/types/generated/graphql.ts b/front/src/types/generated/graphql.ts index 475a527d1..cbd19ebe1 100755 --- a/front/src/types/generated/graphql.ts +++ b/front/src/types/generated/graphql.ts @@ -687,6 +687,7 @@ export type MovedBoxesResult = { categoryId: Scalars['Int']; gender: ProductGender; itemsCount: Scalars['Int']; + measureName?: Maybe; movedOn: Scalars['Date']; /** Shipment target organisation name; null for BoxState/OutgoingLocation target types */ organisationName?: Maybe;