-- 原材料重量口径清理脚本
-- 目标：原材料业务只保留重量，不再保留数量口径。
-- 注意：成品、销售订单、发货、退货仍保留件数/PCS，不在本脚本处理范围内。

START TRANSACTION;

UPDATE md_material material
JOIN md_item item ON item.id = material.item_id
SET
    material.purchase_calc_mode = 'BY_WEIGHT',
    material.purchase_multiple_qty = 0
WHERE item.item_type = 'RAW_MATERIAL';

UPDATE md_bom_item bom_item
JOIN md_item item ON item.id = bom_item.material_item_id
SET
    bom_item.usage_type = 'WEIGHT',
    bom_item.qty_per_piece = 1
WHERE item.item_type = 'RAW_MATERIAL';

UPDATE mrp_material_demand
SET suggested_purchase_qty = 0;

UPDATE po_purchase_order_item po_item
JOIN md_item item ON item.id = po_item.material_item_id
SET
    po_item.order_qty = 0,
    po_item.received_qty = 0
WHERE item.item_type = 'RAW_MATERIAL';

UPDATE po_receipt_item receipt_item
JOIN md_item item ON item.id = receipt_item.material_item_id
SET
    receipt_item.received_qty = 0,
    receipt_item.accepted_qty = 0
WHERE item.item_type = 'RAW_MATERIAL';

UPDATE wh_stock_lot lot
JOIN md_item item ON item.id = lot.item_id
SET
    lot.inbound_qty = 0,
    lot.remaining_qty = 0,
    lot.locked_qty = 0
WHERE item.item_type = 'RAW_MATERIAL';

UPDATE wh_stock_balance balance
JOIN md_item item ON item.id = balance.item_id
SET
    balance.qty_on_hand = 0,
    balance.qty_available = 0,
    balance.qty_allocated = 0
WHERE item.item_type = 'RAW_MATERIAL';

UPDATE wh_inventory_txn txn
JOIN md_item item ON item.id = txn.item_id
SET txn.qty_change = 0
WHERE item.item_type = 'RAW_MATERIAL';

UPDATE pp_work_order_material material
JOIN md_item item ON item.id = material.material_item_id
SET
    material.required_qty = 0,
    material.issued_qty = 0,
    material.returned_qty = 0
WHERE item.item_type = 'RAW_MATERIAL';

UPDATE pp_work_order_material_issue issue_row
JOIN md_item item ON item.id = issue_row.material_item_id
SET issue_row.issued_qty = 0
WHERE item.item_type = 'RAW_MATERIAL';

UPDATE pp_work_order wo
JOIN (
    SELECT
        wom.work_order_id,
        FLOOR(
            COALESCE(NULLIF(wom.issued_weight_kg, 0), NULLIF(wom.required_weight_kg, 0), 0)
            / COALESCE(NULLIF(bi.gross_weight_per_piece_kg, 0), NULLIF(product.unit_weight_kg, 0))
        ) AS reference_qty
    FROM pp_work_order_material wom
    JOIN (
        SELECT work_order_id, MIN(id) AS first_material_row_id
        FROM pp_work_order_material
        GROUP BY work_order_id
    ) first_row ON first_row.first_material_row_id = wom.id
    JOIN pp_work_order wo_inner ON wo_inner.id = wom.work_order_id
    JOIN md_item product ON product.id = wo_inner.product_item_id
    JOIN md_bom_item bi ON bi.id = wom.bom_item_id
    JOIN md_item material ON material.id = wom.material_item_id
    WHERE material.item_type = 'RAW_MATERIAL'
) calc ON calc.work_order_id = wo.id
SET
    wo.planned_qty = calc.reference_qty,
    wo.released_qty = calc.reference_qty
WHERE calc.reference_qty > 0;

UPDATE pp_work_order_operation operation
JOIN pp_work_order wo ON wo.id = operation.work_order_id
SET operation.planned_qty = wo.planned_qty
WHERE wo.planned_qty > 0;

COMMIT;
