# 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 反向)
  # ⭐ W3 末改 settlement_money → receivable_this(应收金额,不是实收)
  #   - settlement_money = 实际收到现金(会员卡扣费/套餐/团购/员工免费时 = 0)
  #   - receivable_this  = 服务应收金额(反映患者带来的真实业务价值)
  #   - 例:王辉 5 次洁牙 settlement_money=0(扣会员卡)→ 改前 LTV=0 不合理
  #         receivable_this=¥760+350+200×N(应收)→ 改后真实反映"患者价值"
  #   - 跟 design.md 患者价值定义对齐:LTV = 患者带来的业务量,不是现金流
  amount:            receivable_this    # FieldMapper.normalize 按 amount_unit=yuan 转 cents
  method:            payment_channel    # transforms.pick_first_nonzero 推断的主导支付通道
  # 商业保险公司名(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)填
