# payment — fact_settlement_mode_out
# 消费 transforms 输出表 payment_rows(transforms.pick_first_nonzero 已推 payment_channel)
canonical: payment

emits:
  action: payment_received
  subjectType: payment
  occurredAtField: paidAt

primary:
  table: payment_rows
  key: id

field_mapping:
  externalId:        id
  # W4 末 DW upsert:updatedAt 进 source_event_id 幂等键,host UPDATE → 新 transaction(否则永久 dedup 漏 update)
  updatedAt:         updated_date
  createdAt:         created_date
  patientExternalId: patient_id
  clinicId:          organization_id
  paidAt:            created_date    # W3 末从 billing_date 升级 — 跟 treatment_actual 口径一致。
                                     #   - billing_date    = 开单/收费动作时间(团购预付等失真)
                                     #   - created_date    = 结算入库时间(接近实际治疗时点)
                                     #   - 100% created_date >= billing_date(9.4 万行 settlement_mode 实测,0 反向)
  # ⭐ 2026-07 业务定调:amount 回到 settlement_money(实付)—— 一线关注真金白银。
  #   历史沿革:W3 末曾从 settlement_money 改为 receivable_this(应收,王辉会员卡扣费案例),
  #   但一线反馈应收对免费套餐/团购客严重虚高(胡新丽:应收 ¥1,270 实付 ¥0),
  #   且 refund 侧一直是 settlement_money(实收冲减)→ 原 LTV 实为"应收收入−实收退款"混口径。
  #   现:amount=实付(与 refund 同口径,LTV 自洽);应收降级为 receivableAmount 辅助字段
  #   (折扣率分母 / 权益客识别:实付 0 + 应收>0 = 纯权益客)。
  amount:            settlement_money   # 实付;FieldMapper.normalize 按 amount_unit=yuan 转 cents
  receivableAmount:  receivable_this    # 应收(原价)— 折扣锚点分母/参考,同 yuan→cents
  method:            payment_channel    # transforms.pick_first_nonzero 推断的主导支付通道
  # 卡券名称(B.1.3 权益身份关键词匹配源:储值/儿牙/银行私行/商保/医保)
  cardTypeName:      card_type_name
  cardName:          card_name
  # 折扣锚点(D.1.3):4 渠道折扣额(元)+ 项目名;折扣率=1-Σ折扣/应收(parser 算)
  discountDept:      discount_dept_rate
  discountCompany:   discount_company_rate
  discountInsurance: discount_insurance_rate
  discountCard:      discount_card_rate
  settlementProject: settlement_project_name
  # 商业保险公司名(settlement_mode_out.insurance_name)— 喂 persona「权益身份」特征。
  #   保司名脏(57 个别名),归一在 entitlement-status.feature 的 canonicalInsurer 里做;
  #   此处原样带入 fact.content.insurance_name(单一收口),非空即"商保结算"强信号。
  insuranceName:     insurance_name
  doctorId:           doctor_id          # 收费医生(医患关系信号)
  encounterExternalId: registration_id   # 关联接诊(语义准:registration_id = 接诊号)
  # orderExternalId 不映射:host 没真正的"医嘱单 id"概念,留 null 等其他 host(charge_order)填
