SELECT
    '{{%%p_start_date}}' AS 实际开始日期,
    '{{%%p_end_date}}'   AS 实际结束日期,
    sos.id_storage          AS 仓库id,
	sos.storage_name        AS 仓库名称,
    sos.part_name           AS 材料名称,
    sos.custom_code         AS 材料编码,
    sos.supplier_code       AS 零件号,
    sos.category_root_name  AS 材料一级分类,
    sos.category_name       AS 材料二级分类,
	sos.part_brand          AS 品牌,
    /* ---------- 期初（begin_*：仅在开始日取值；>60天仅月初保留） ---------- */
    SUM(
        CASE
            WHEN DATEDIFF(CURRENT_DATE(), STR_TO_DATE('{{%%p_start_date}}', '%Y-%m-%d')) > 60
                 AND DAY(STR_TO_DATE(sos.bill_day, '%Y-%m-%d')) <> 1
            THEN 0
            WHEN sos.bill_day = '{{%%p_start_date}}'
            THEN sos.begin_number
            ELSE 0
        END
    ) AS 期初在库数量,

    SUM(
        CASE
            WHEN DATEDIFF(CURRENT_DATE(), STR_TO_DATE('{{%%p_start_date}}', '%Y-%m-%d')) > 60
                 AND DAY(STR_TO_DATE(sos.bill_day, '%Y-%m-%d')) <> 1
            THEN 0
            WHEN sos.bill_day = '{{%%p_start_date}}'
            THEN sos.begin_price
            ELSE 0
        END
    ) AS 期初在库成本,

    SUM(
        CASE
            WHEN DATEDIFF(CURRENT_DATE(), STR_TO_DATE('{{%%p_start_date}}', '%Y-%m-%d')) > 60
                 AND DAY(STR_TO_DATE(sos.bill_day, '%Y-%m-%d')) <> 1
            THEN 0
            WHEN sos.bill_day = '{{%%p_start_date}}'
            THEN sos.begin_price_no_tax
            ELSE 0
        END
    ) AS 期初在库成本除税,

    /* ---------- 入库数量 ---------- */
    SUM(sos.cgdin_number)   AS 采购入库数量,
    SUM(sos.cgtin_number)   AS 采购退货数量,
    SUM(sos.drdin_number)   AS 调入数量,
    SUM(sos.rkdin_number)   AS 手工入库数量,
    SUM(sos.ykrin_number)   AS 移库入库数量,
    SUM(sos.czrin_number)   AS 拆装入库数量,

    /* ---------- 入库成本（除税） ---------- */
    SUM(sos.cgdin_amount_no_tax)  AS 采购入库成本（除税）,
    SUM(sos.cgtin_amount_no_tax)  AS 采购退货成本（除税）,
    SUM(sos.drdin_amount_no_tax)  AS 调拨入库成本（除税）,
    SUM(sos.rkdin_amount_no_tax)  AS 手工入库成本（除税）,
    SUM(sos.ykrin_amount_no_tax)  AS 移库入库成本（除税）,
    SUM(sos.czrin_amount_no_tax)  AS 拆装入库成本（除税）,

    /* ---------- 出库/退料相关数量 ---------- */
    SUM(sos.pickout_number) AS 领料出库数量,
    SUM(sos.tin_number)     AS 退料入库数量,
    SUM(sos.dcdout_number)  AS 调拨出库数量,
    SUM(sos.ckdout_number)  AS 手工出库数量,
    SUM(sos.czcout_number)  AS 拆装出库数量,
    SUM(sos.ykcout_number)  AS 移库出库数量,

    /* ---------- 出库/退料相关成本（除税） ---------- */
    SUM(sos.pickout_amount_no_tax) AS 领料出库成本（除税）,
    SUM(sos.tin_amount_no_tax)     AS 退料入库成本（除税）,
    SUM(sos.dcdout_amount_no_tax)  AS 调拨出库成本（除税）,
    SUM(sos.ckdout_amount_no_tax)  AS 手工出库成本（除税）,
    SUM(sos.czcout_amount_no_tax)  AS 拆装出库成本（除税）,
    SUM(sos.ykcout_amount_no_tax)  AS 移库出库成本（除税）,

    /* ---------- 期末（end_*：仅在结束日取值；>60天仅月末保留） ---------- */
    SUM(
        CASE
            WHEN DATEDIFF(CURRENT_DATE(), STR_TO_DATE('{{%%p_end_date}}', '%Y-%m-%d')) > 60
                 AND DAY(STR_TO_DATE(sos.bill_day, '%Y-%m-%d')) <> DAY(LAST_DAY(STR_TO_DATE(sos.bill_day, '%Y-%m-%d')))
            THEN 0
            WHEN sos.bill_day = '{{%%p_end_date}}'
            THEN sos.end_number
            ELSE 0
        END
    ) AS 期末在库数量,

    SUM(
        CASE
            WHEN DATEDIFF(CURRENT_DATE(), STR_TO_DATE('{{%%p_end_date}}', '%Y-%m-%d')) > 60
                 AND DAY(STR_TO_DATE(sos.bill_day, '%Y-%m-%d')) <> DAY(LAST_DAY(STR_TO_DATE(sos.bill_day, '%Y-%m-%d')))
            THEN 0
            WHEN sos.bill_day = '{{%%p_end_date}}'
            THEN sos.end_price
            ELSE 0
        END
    ) AS 期末在库成本,

    SUM(
        CASE
            WHEN DATEDIFF(CURRENT_DATE(), STR_TO_DATE('{{%%p_end_date}}', '%Y-%m-%d')) > 60
                 AND DAY(STR_TO_DATE(sos.bill_day, '%Y-%m-%d')) <> DAY(LAST_DAY(STR_TO_DATE(sos.bill_day, '%Y-%m-%d')))
            THEN 0
            WHEN sos.bill_day = '{{%%p_end_date}}'
            THEN sos.end_price_no_tax
            ELSE 0
        END
    ) AS 期末在库成本除税

FROM rpt_customized_ndsd_storage_order_stat AS sos
WHERE 1=1
    /* ---------- 固定过滤条件（按你现有配置保留） ---------- */
    AND sos.id_own_group = '11240984669918396614'
    AND sos.id_own_org IN ('11240984669918400634')

    /* ---------- 日期区间过滤（bill_day 为文本 YYYY-MM-DD） ---------- */
    AND sos.bill_day >= '{{%%p_start_date}}'
    AND sos.bill_day <= '{{%%p_end_date}}'

GROUP BY
    sos.id_storage,
    sos.part_name,
    sos.custom_code,
    sos.supplier_code,
    sos.category_root_name,
    sos.category_name;