feat: recycle system
This commit is contained in:
@@ -7,16 +7,12 @@
|
||||
SUM(t.returned) AS returnedQuantity, SUM(t.received - t.returned) AS netQuantity
|
||||
FROM (
|
||||
SELECT e.recycle_item_id, e.recycle_name_snapshot AS recycle_name,
|
||||
e.recycle_unit_snapshot AS recycle_unit, e.recycle_quantity AS received, 0 AS returned
|
||||
FROM gao_recycle_exchange e
|
||||
WHERE e.store_id = #{storeId} AND e.occurred_at >= #{startAt} AND e.occurred_at < #{endAt}
|
||||
UNION ALL
|
||||
SELECT e.recycle_item_id, e.recycle_name_snapshot, e.recycle_unit_snapshot,
|
||||
0, e.recycle_quantity
|
||||
FROM gao_recycle_exchange_action a
|
||||
JOIN gao_recycle_exchange e ON e.id = a.exchange_id AND e.store_id = a.store_id
|
||||
WHERE a.store_id = #{storeId} AND a.action_type IN ('CANCEL', 'REVOKE')
|
||||
AND a.recycle_returned = 1 AND a.occurred_at >= #{startAt} AND a.occurred_at < #{endAt}
|
||||
e.recycle_unit_snapshot AS recycle_unit,
|
||||
CASE WHEN p.direction = 'IN' THEN p.quantity ELSE 0 END AS received,
|
||||
CASE WHEN p.direction = 'OUT' THEN p.quantity ELSE 0 END AS returned
|
||||
FROM gao_recycle_physical_record p
|
||||
JOIN gao_recycle_exchange e ON e.sales_order_id = p.order_id AND e.store_id = p.store_id
|
||||
WHERE p.store_id = #{storeId} AND p.occurred_at >= #{startAt} AND p.occurred_at < #{endAt}
|
||||
) t
|
||||
GROUP BY t.recycle_item_id, t.recycle_name, t.recycle_unit
|
||||
</select>
|
||||
@@ -25,19 +21,25 @@
|
||||
t.unit_name AS unit, SUM(t.created) AS createdQuantity,
|
||||
SUM(t.verified) AS verifiedQuantity, SUM(t.revoked) AS revokedQuantity
|
||||
FROM (
|
||||
SELECT e.sku_id_snapshot AS sku_id, e.sku_unit_id_snapshot AS sku_unit_id,
|
||||
e.product_name_snapshot AS product_name, e.unit_snapshot AS unit_name,
|
||||
e.exchange_quantity AS created, 0 AS verified, 0 AS revoked
|
||||
SELECT oi.sku_id, oi.sku_unit_id, oi.product_name_snapshot AS product_name,
|
||||
oi.unit_snapshot AS unit_name,
|
||||
oi.quantity AS created, 0 AS verified, 0 AS revoked
|
||||
FROM gao_recycle_exchange e
|
||||
JOIN gao_sales_order_item oi ON oi.sales_order_id = e.sales_order_id
|
||||
AND oi.item_type = 'RECYCLE_ENTITLEMENT' AND oi.deleted_at IS NULL
|
||||
WHERE e.store_id = #{storeId} AND e.occurred_at >= #{startAt} AND e.occurred_at < #{endAt}
|
||||
UNION ALL
|
||||
SELECT e.sku_id_snapshot, e.sku_unit_id_snapshot, e.product_name_snapshot, e.unit_snapshot,
|
||||
0, CASE WHEN a.action_type = 'VERIFY' THEN e.exchange_quantity ELSE 0 END,
|
||||
CASE WHEN a.action_type = 'REVOKE' THEN e.exchange_quantity ELSE 0 END
|
||||
FROM gao_recycle_exchange_action a
|
||||
JOIN gao_recycle_exchange e ON e.id = a.exchange_id AND e.store_id = a.store_id
|
||||
WHERE a.store_id = #{storeId} AND a.action_type IN ('VERIFY', 'REVOKE')
|
||||
AND a.occurred_at >= #{startAt} AND a.occurred_at < #{endAt}
|
||||
SELECT oi.sku_id, oi.sku_unit_id, oi.product_name_snapshot, oi.unit_snapshot,
|
||||
0, CASE WHEN f.source_fulfillment_id IS NULL THEN i.base_quantity / oi.base_quantity_per_unit_snapshot ELSE 0 END,
|
||||
CASE WHEN f.source_fulfillment_id IS NOT NULL THEN i.base_quantity / oi.base_quantity_per_unit_snapshot ELSE 0 END
|
||||
FROM gao_order_fulfillment f
|
||||
JOIN gao_recycle_exchange e ON e.sales_order_id = f.sales_order_id AND e.store_id = f.store_id
|
||||
JOIN gao_order_fulfillment_item i ON i.fulfillment_id = f.id
|
||||
JOIN gao_sales_order_item oi ON oi.id = i.source_order_item_id AND oi.sales_order_id = f.sales_order_id
|
||||
WHERE f.store_id = #{storeId}
|
||||
AND ((f.source_fulfillment_id IS NULL AND f.status IN ('VERIFIED', 'REVOKED'))
|
||||
OR (f.source_fulfillment_id IS NOT NULL AND f.status = 'VERIFIED'))
|
||||
AND f.delivered_at >= #{startAt} AND f.delivered_at < #{endAt}
|
||||
) t
|
||||
GROUP BY t.sku_id, t.sku_unit_id, t.product_name, t.unit_name
|
||||
</select>
|
||||
|
||||
Reference in New Issue
Block a user