-- 订单明细输出（依赖 _common.sql 的 CTE 链）
-- 最终查询从 order_with_days 中选取所有字段，按场景分类展示每笔订单的均摊明细

SELECT
  order_no AS "订单号",
  order_date AS "下单日期",
  latest_refund_date AS "退费日期",
  CASE
    WHEN is_pre_period_full_refund = 1 THEN '账期前全额退费'
    WHEN is_pre_period_partial_refund = 1 THEN '账期前部分退费'
    WHEN is_trial_period_refund = 1 AND is_full_refund = 0 THEN '试用期内部分退费'
    WHEN is_current_period_refund = 1 AND is_full_refund = 1 AND is_ordered_in_period = 1 THEN '账期内下单全额退费'
    WHEN is_current_period_refund = 1 AND is_full_refund = 1 AND is_ordered_in_period = 0 THEN '历史下单账期内全额退费'
    WHEN is_current_period_refund = 1 AND is_full_refund = 0 AND is_ordered_in_period = 1 THEN '账期内下单部分退费'
    WHEN is_current_period_refund = 1 AND is_full_refund = 0 AND is_ordered_in_period = 0 THEN '历史下单账期内部分退费'
    WHEN DATE(amortization_start_date) > '{period_end}'::date THEN '试用期中'
    ELSE '正常订单'
  END AS "订单分类",
  ROUND(pay_amount::numeric, 2) AS "原订单金额",
  ROUND(total_refund_amount::numeric, 2) AS "退款金额",
  ROUND((pay_amount - total_refund_amount)::numeric, 2) AS "退后金额",
  ROUND(tax_rate::numeric, 4) AS "税率",
  ROUND(tax_amount::numeric, 2) AS "原订单税额",
  ROUND(
    CASE
      WHEN has_refund = 1 AND is_full_refund = 0
        THEN (pay_amount - total_refund_amount) / (1 + tax_rate)
      WHEN has_refund = 1 AND is_full_refund = 1 THEN 0
      ELSE after_tax_amount
    END::numeric, 2
  ) AS "当前税后金额",
  ROUND(
    CASE
      WHEN has_refund = 1 AND is_full_refund = 0
        THEN (pay_amount - total_refund_amount) / (1 + tax_rate) * tax_rate
      WHEN has_refund = 1 AND is_full_refund = 1 THEN 0
      ELSE tax_amount
    END::numeric, 2
  ) AS "当前税额",
  original_cycle_days AS "原始均摊周期",
  total_cycle_days AS "退后均摊周期",

  -- 历史累计均摊金额（仅历史订单有值，账期内下单=0）
  ROUND(
    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 historical_amorted_days > 0
        THEN historical_amorted_days * original_daily_amort_amount
      ELSE 0
    END::numeric, 2
  ) AS "历史累计均摊金额",

  -- 冲销历史累计摊销金额（场景B/E：历史下单+账期内退费，排除试用期内退费）
  ROUND(
    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(
    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 "退后订单历史累计摊销金额",

  -- 冲销原税款（场景B/E：历史下单+账期内退费，排除试用期内退费）
  ROUND(
    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 "冲销原税款",

  -- 冲销未确认收入（场景B/E）
  ROUND(
    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 "冲销未确认收入",

  -- 当期摊销金额（核心：按6种场景分支计算）
  ROUND(
    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
      -- 场景A：账期内下单+部分退费
      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
      -- 场景B：历史下单+账期内部分退费（仅账期内均摊）
      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
      -- 场景C：账期前部分退费
      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(
    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(
    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 "未确认收入",

  CASE
    WHEN is_current_period_refund = 1 AND is_full_refund = 1 THEN 0
    ELSE (total_cycle_days - total_amorted_days_to_period_end)
  END AS "剩余周期"

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