-- 汇总输出（依赖 _common.sql 的 CTE 链）
-- 对 order_with_days 进行聚合，输出账期整体汇总指标
-- 列名与 detail.sql 保持一致，方便交叉验证（明细 SUM = 汇总值）

SELECT
  -- 订单统计
  COUNT(CASE WHEN order_date >= '{period_start}'::date
                  AND order_date <= '{period_end}'::date THEN 1 END) AS "订单数",
  COUNT(CASE WHEN order_date >= '{period_start}'::date
                  AND order_date <= '{period_end}'::date
                  AND amortization_start_date <= '{period_end}'::date THEN 1 END) AS "正式订单数",
  COUNT(CASE WHEN order_date >= '{period_start}'::date
                  AND order_date <= '{period_end}'::date
                  AND amortization_start_date > '{period_end}'::date THEN 1 END) AS "试用订单数",

  -- 冲销历史累计摊销金额
  ROUND(SUM(
    CASE WHEN is_current_period_refund = 1 AND is_ordered_in_period = 0
              AND is_trial_period_refund = 0 AND historical_amorted_days > 0
      THEN -(historical_amorted_days * original_daily_amort_amount) ELSE 0 END
  )::numeric, 2) AS "冲销历史累计摊销金额",

  -- 退后订单历史累计摊销金额
  ROUND(SUM(
    CASE
      WHEN is_ordered_in_period = 1 THEN 0
      WHEN is_pre_period_partial_refund = 1 AND historical_amorted_days > 0
        THEN historical_amorted_days * daily_amort_amount
      WHEN is_current_period_refund = 1 AND is_ordered_in_period = 0
           AND is_trial_period_refund = 0 AND historical_amorted_days > 0
        THEN historical_amorted_days * daily_amort_amount
      WHEN historical_amorted_days > 0
        THEN historical_amorted_days * original_daily_amort_amount
      ELSE 0 END
  )::numeric, 2) AS "退后订单历史累计摊销金额",

  -- 冲销原税款
  ROUND(SUM(
    CASE WHEN is_current_period_refund = 1 AND is_ordered_in_period = 0
              AND is_trial_period_refund = 0
      THEN -tax_amount ELSE 0 END
  )::numeric, 2) AS "冲销原税款",

  -- 冲销未确认收入
  ROUND(SUM(
    CASE WHEN is_current_period_refund = 1 AND is_ordered_in_period = 0
              AND is_trial_period_refund = 0
      THEN -(after_tax_amount - historical_amorted_days * original_daily_amort_amount) ELSE 0 END
  )::numeric, 2) AS "冲销未确认收入",

  -- 当期摊销金额
  ROUND(SUM(
    CASE
      WHEN is_pre_period_full_refund = 1 THEN 0
      WHEN is_trial_period_refund = 1 AND is_full_refund = 1 THEN 0
      WHEN is_trial_period_refund = 1 AND is_full_refund = 0 THEN
        CASE WHEN total_cycle_days > 0 AND period_amort_days > 0
          THEN daily_amort_amount * period_amort_days ELSE 0 END
      WHEN is_current_period_refund = 1 AND is_full_refund = 1 AND is_ordered_in_period = 1 THEN 0
      WHEN is_current_period_refund = 1 AND is_full_refund = 1 AND is_ordered_in_period = 0 THEN 0
      WHEN is_current_period_refund = 1 AND is_full_refund = 0 AND is_ordered_in_period = 1 THEN
        CASE WHEN total_cycle_days > 0 AND period_amort_days > 0
          THEN daily_amort_amount * period_amort_days ELSE 0 END
      WHEN is_current_period_refund = 1 AND is_full_refund = 0 AND is_ordered_in_period = 0 THEN
        CASE WHEN total_cycle_days > 0 AND period_amort_days > 0
          THEN daily_amort_amount * period_amort_days ELSE 0 END
      WHEN is_pre_period_partial_refund = 1 THEN
        CASE WHEN total_cycle_days > 0 AND period_amort_days > 0
          THEN daily_amort_amount * period_amort_days ELSE 0 END
      WHEN period_amort_days > 0 THEN
        CASE WHEN eff_end = amortization_end_date
          THEN after_tax_amount - original_daily_amort_amount * (original_cycle_days - 1)
               + original_daily_amort_amount * (period_amort_days - 1)
          ELSE original_daily_amort_amount * period_amort_days END
      ELSE 0 END
  )::numeric, 2) AS "当期摊销金额",

  -- 账期税额
  ROUND(SUM(
    CASE
      WHEN is_ordered_in_period = 1 AND DATE(amortization_start_date) <= '{period_end}'::date
           AND is_current_period_refund = 1 AND is_full_refund = 0
      THEN refunded_tax_amount
      WHEN is_ordered_in_period = 1 AND DATE(amortization_start_date) <= '{period_end}'::date
           AND (has_refund = 0 OR (is_current_period_refund = 0 AND is_full_refund = 0))
      THEN tax_amount ELSE 0 END
  )::numeric, 2) AS "账期税额",

  -- 未确认收入
  ROUND(SUM(
    CASE
      WHEN is_pre_period_full_refund = 1 THEN 0
      WHEN is_current_period_refund = 1 AND is_full_refund = 1 THEN 0
      WHEN is_current_period_refund = 1 AND is_full_refund = 0 THEN
        refunded_after_tax_amount - daily_amort_amount * refunded_amort_days_to_period_end
      WHEN is_pre_period_partial_refund = 1 THEN
        refunded_after_tax_amount - daily_amort_amount * total_amorted_days_to_period_end
      ELSE after_tax_amount - original_daily_amort_amount * total_amorted_days_to_period_end END
  )::numeric, 2) AS "未确认收入"

FROM order_with_days
WHERE is_pre_period_full_refund = 0
  AND NOT (is_trial_period_refund = 1 AND is_full_refund = 1);
