From 5a23817f4c44fb8d950a334702a22d8bc1f11c59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Faleiros?= Date: Fri, 28 Aug 2026 09:39:01 -0300 Subject: [PATCH] Show synchronized composition in production orders --- backend/services/productionOrderService.js | 92 +++++++++++++++++++++- 1 file changed, 88 insertions(+), 4 deletions(-) diff --git a/backend/services/productionOrderService.js b/backend/services/productionOrderService.js index 7f658e0..18119c9 100644 --- a/backend/services/productionOrderService.js +++ b/backend/services/productionOrderService.js @@ -560,8 +560,50 @@ const getOrderById = async (id, client = pool) => { ) ORDER BY component.id ) - FROM production_order_components component - WHERE component.production_order_id = po.id + FROM ( + SELECT + order_component.id, + order_component.component_tiny_id, + order_component.component_sku, + order_component.component_name, + order_component.quantity_per_unit, + order_component.total_quantity, + order_component.unit + FROM production_order_components order_component + WHERE order_component.production_order_id = po.id + + UNION ALL + + SELECT + -composition_component.id AS id, + composition_component.component_tiny_id, + composition_component.component_sku, + composition_component.component_name, + composition_component.quantity_per_unit, + composition_component.quantity_per_unit * po.quantity AS total_quantity, + composition_component.unit + FROM product_composition_components composition_component + JOIN product_compositions composition + ON composition.id = ( + SELECT matched_composition.id + FROM product_compositions matched_composition + WHERE matched_composition.source IN ('manual', 'tiny_olist_v3') + AND ( + matched_composition.finished_tiny_product_id = po.product_sku + OR matched_composition.finished_tiny_product_id = po.tiny_id + OR matched_composition.external_source_id = po.product_sku + OR matched_composition.external_source_id = po.tiny_id + OR matched_composition.finished_product_sku = UPPER(po.product_sku) + ) + ORDER BY CASE matched_composition.source WHEN 'manual' THEN 0 ELSE 1 END, matched_composition.id + LIMIT 1 + ) + WHERE NOT EXISTS ( + SELECT 1 + FROM production_order_components order_component + WHERE order_component.production_order_id = po.id + ) + ) component ), '[]'::json ) as components, @@ -700,8 +742,50 @@ const baseProductionOrderSelect = ` ) ORDER BY component.id ) - FROM production_order_components component - WHERE component.production_order_id = po.id + FROM ( + SELECT + order_component.id, + order_component.component_tiny_id, + order_component.component_sku, + order_component.component_name, + order_component.quantity_per_unit, + order_component.total_quantity, + order_component.unit + FROM production_order_components order_component + WHERE order_component.production_order_id = po.id + + UNION ALL + + SELECT + -composition_component.id AS id, + composition_component.component_tiny_id, + composition_component.component_sku, + composition_component.component_name, + composition_component.quantity_per_unit, + composition_component.quantity_per_unit * po.quantity AS total_quantity, + composition_component.unit + FROM product_composition_components composition_component + JOIN product_compositions composition + ON composition.id = ( + SELECT matched_composition.id + FROM product_compositions matched_composition + WHERE matched_composition.source IN ('manual', 'tiny_olist_v3') + AND ( + matched_composition.finished_tiny_product_id = po.product_sku + OR matched_composition.finished_tiny_product_id = po.tiny_id + OR matched_composition.external_source_id = po.product_sku + OR matched_composition.external_source_id = po.tiny_id + OR matched_composition.finished_product_sku = UPPER(po.product_sku) + ) + ORDER BY CASE matched_composition.source WHEN 'manual' THEN 0 ELSE 1 END, matched_composition.id + LIMIT 1 + ) + WHERE NOT EXISTS ( + SELECT 1 + FROM production_order_components order_component + WHERE order_component.production_order_id = po.id + ) + ) component ), '[]'::json ) as components,