huminmin
3 天以前 6be17495cd29ba8b28af35da7ba171cbc0d61c21
src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
@@ -14,11 +14,14 @@
        T1.tax_rate,
        T1.tax_inclusive_unit_price,
        T1.tax_inclusive_total_price,
        T1.freight_unit_price,
        T1.total_freight,
        T1.tax_exclusive_total_price,
        T1.invoice_type,
        T1.type,
        T1.product_id,
        T1.product_model_id,
        T1.stock_type,
        T1.register,
        T1.register_date,
        T1.approve_status,
@@ -30,29 +33,120 @@
        pm.model as specification_model,
        pm.unit as unit,
        CASE
        WHEN (IFNULL(t2.qualitity, 0) - IFNULL(t2.locked_quantity, 0)) >0 THEN 1
        WHEN IFNULL(
            CASE
                WHEN T1.stock_type = 'waste' THEN t2.waste_available_quantity
                ELSE t2.qualified_available_quantity
            END, 0
        ) >= (IFNULL(T1.quantity, 0) - IFNULL(t3.shipped_quantity, 0)) THEN 1
        ELSE 0
        END as has_sufficient_stock,
        (IFNULL(T1.quantity, 0) - IFNULL(t3.shipped_quantity, 0)) as no_quantity,
        IFNULL(t5.pending_approval_quantity, 0) as pending_approval_quantity,
        CASE
         WHEN IFNULL(t3.shipped_quantity, 0) = 0 THEN '待发货'
         WHEN IFNULL(t3.shipped_quantity, 0) = 0 AND IFNULL(t5.pending_approval_quantity, 0) = 0 THEN '待发货'
         WHEN IFNULL(t3.shipped_quantity, 0) = 0 AND IFNULL(t5.pending_approval_quantity, 0) > 0 THEN '审批中'
         WHEN (IFNULL(T1.quantity, 0) - IFNULL(t3.shipped_quantity, 0)) > 0 THEN '部分发货'
        ELSE '已发货'
        END as shippingStatus
        END as shippingStatus,
        CASE
         WHEN T1.type != 2 THEN NULL
         WHEN IFNULL(t4.approved_stock_in_num, 0) <= 0 THEN '待入库'
         WHEN IFNULL(t4.approved_stock_in_num, 0) >= IFNULL(T1.quantity, 0) THEN '完全入库'
        ELSE '入库中'
        END AS stock_in_approval_status
        FROM
        sales_ledger_product T1
        LEFT JOIN (
        SELECT product_model_id, SUM(qualitity) as qualitity, SUM(locked_quantity) as locked_quantity
        FROM stock_inventory
        GROUP BY product_model_id
        SELECT
            t.product_model_id,
            SUM(t.qualified_available_quantity) as qualified_available_quantity,
            SUM(t.waste_available_quantity) as waste_available_quantity
        FROM (
            SELECT
                si.product_model_id,
                (IFNULL(si.qualitity, 0) - IFNULL(si.locked_quantity, 0) - IFNULL((
                    SELECT SUM(sor.stock_out_num)
                    FROM stock_out_record sor
                    WHERE sor.product_model_id = si.product_model_id
                      AND (
                        (si.batch_no IS NULL AND sor.batch_no IS NULL)
                        OR si.batch_no = sor.batch_no
                      )
                      AND sor.type = '0'
                      AND sor.approval_status IN (0, 3)
                ), 0)) as qualified_available_quantity,
                0 as waste_available_quantity
            FROM stock_inventory si
            UNION ALL
            SELECT
                su.product_model_id,
                0 as qualified_available_quantity,
                (IFNULL(su.qualitity, 0) - IFNULL(su.locked_quantity, 0) - IFNULL((
                    SELECT SUM(sor.stock_out_num)
                    FROM stock_out_record sor
                    WHERE sor.product_model_id = su.product_model_id
                      AND (
                        (su.batch_no IS NULL AND sor.batch_no IS NULL)
                        OR su.batch_no = sor.batch_no
                      )
                      AND sor.type = '1'
                      AND sor.approval_status IN (0, 3)
                ), 0)) as waste_available_quantity
            FROM stock_uninventory su
            WHERE COALESCE(su.type, 'unqualified') = 'waste'
        ) t
        GROUP BY t.product_model_id
        ) t2 ON T1.product_model_id = t2.product_model_id
        LEFT JOIN (
        SELECT sales_ledger_product_id, IFNULL(SUM(spd.quantity), 0) as shipped_quantity
        FROM shipping_info si
        LEFT JOIN shipping_product_detail spd ON si.id = spd.shipping_info_id
        where si.status != '审核拒绝'
        where si.status = '审核通过' OR si.status = '已发货'
        GROUP BY sales_ledger_product_id
        ) t3 ON t3.sales_ledger_product_id = T1.id
        LEFT JOIN (
        SELECT rel.sales_ledger_product_id,
               IFNULL(SUM(rel.stock_in_num), 0) AS approved_stock_in_num
        FROM (
            SELECT slp.id AS sales_ledger_product_id,
                   sir.stock_in_num
            FROM stock_in_record sir
            INNER JOIN sales_ledger_product slp
                ON slp.type = 2
                AND TRIM(sir.record_type) = '7'
                AND sir.record_id = slp.sales_ledger_id
                AND (
                    (sir.batch_no IS NOT NULL AND sir.batch_no LIKE CONCAT('%-', slp.id))
                    OR (sir.batch_no IS NULL AND sir.product_model_id = slp.product_model_id)
                )
            WHERE sir.approval_status = 1
            UNION ALL
            SELECT slp.id AS sales_ledger_product_id,
                   sir.stock_in_num
            FROM stock_in_record sir
            INNER JOIN quality_inspect qi
                ON TRIM(sir.record_type) = '10'
                AND sir.record_id = qi.id
            INNER JOIN sales_ledger_product slp
                ON slp.type = 2
                AND slp.sales_ledger_id = qi.purchase_ledger_id
                AND slp.product_model_id = qi.product_model_id
            WHERE sir.approval_status = 1
        ) rel
        GROUP BY rel.sales_ledger_product_id
        ) t4 ON t4.sales_ledger_product_id = T1.id
        LEFT JOIN (
        SELECT sales_ledger_product_id, IFNULL(SUM(spd.quantity), 0) as pending_approval_quantity
        FROM shipping_info si
        LEFT JOIN shipping_product_detail spd ON si.id = spd.shipping_info_id
        WHERE si.status IN ('待审核', '审核中')
        GROUP BY sales_ledger_product_id
        ) t5 ON t5.sales_ledger_product_id = T1.id
        left join product_model pm ON T1.product_model_id = pm.id
        left join product p ON pm.product_id = p.id
        <where>