对账单策略、生成、发送与收款同步

本文整理 DGJ2.0 对账单 v2:策略配置、客户绑定、账单生成、审核、发送、发送结果、收款状态同步。该链路跨销售、出库、收款、客户微信绑定和 MQ 任务。

业务目标

  • 按策略定期为客户生成对账单。
  • 支持手动创建、重新生成、审核、取消审核、删除。
  • 支持批量发送、查看发送进度、重发和发送结果。
  • 对账单收款状态需要和收付款单、客户账款保持一致。

入口文件

  • 对账单主入口:application/controllers/reports/StatementBill.php
  • 对账策略:application/controllers/reports/StatementStrategy.php
  • 对账发送:application/controllers/reports/StatementSend.php
  • 客户微信绑定:application/controllers/reports/StatementCustomerWechat.php
  • 通知消费者:application/controllers/tasks/StatementNotify.php
  • 策略消费者:application/controllers/tasks/StatementStrategyConsumer.php
  • 发送消费者:application/controllers/tasks/StatementSendConsumer.php
  • 定时任务:application/controllers/tasks/StatementTasks.php
  • Service:application/Services/Statement/*

核心表

| 表 | 用途 | | --- | --- | | t_bs_statement_bill_v2 | 对账单主表 | | t_bs_statement_bill_detail_v2 | 对账单详情 | | t_bs_statement_bill_detail_item_v2 | 对账单物料明细 | | t_bs_statement_bill_task_v2 | 生成/同步任务 | | t_bs_need_statement_user | 需要对账的客户 | | t_bs_robot_wechat_binding | 客户微信绑定 | | t_bs_statement_strategy_v2 | 对账策略 | | t_bs_statement_strategy_relation_v2 | 策略客户关系 | | t_bs_statement_send_record_v2 | 发送记录 |

核心方法

StatementBill.php

  • getList:账单列表。
  • reGenerate:重新生成。
  • createByUser:按客户手动生成。
  • delete:删除。
  • detail:详情。
  • check / cancelCheck:审核和取消审核。
  • getWaitPayOrdersByBillId:待付款订单。
  • beforeSendMsg / beforeSendMsgV2:发送前校验。
  • sendMsg / sendMsgBatch:发送。
  • resend:重发。
  • getSendResult / getSendRecordList:发送结果。

StatementStrategy.php

  • create / update / delete:策略维护。
  • updateStatus:启停策略。
  • bindCustomers / unbindCustomers:客户绑定。
  • initCustomerStrategy / initializeStrategy:初始化策略。

StatementSend.php

  • createTask:创建发送任务。
  • getProgress:查询进度。

主流程

flowchart TD
  A["配置对账策略"] --> B["绑定客户"]
  B --> C["定时或手工生成账单"]
  C --> D["聚合销售、出库、收款明细"]
  D --> E["账单审核"]
  E --> F["发送给客户/微信"]
  F --> G["记录发送结果"]
  G --> H["客户付款或线下收款"]
  H --> I["同步收款状态"]

口径说明

  • 账单明细通常来自销售、出库、退货、收款等业务数据。
  • 策略决定客户、周期、生成范围和是否展示商品明细。
  • 发送结果是通知链路结果,不等于客户已确认或已付款。
  • 收款状态要结合 t_scm_payment、账户流水和待付款订单判断。

异常流程

  • 账单未生成:查策略状态、客户绑定、任务表、定时任务。
  • 明细缺单:查销售/出库/收款状态是否满足对账条件。
  • 重复生成:查账单任务幂等和客户周期唯一性。
  • 发送失败:查发送记录、微信绑定、MQ 消费日志。
  • 收款状态不对:查收付款单、账户流水、对账单收款同步任务。

常用 SQL

select * from t_bs_statement_strategy_v2 where sid = :sid;
select * from t_bs_statement_strategy_relation_v2 where strategy_id = :strategy_id;
select * from t_bs_statement_bill_v2 where sid = :sid and contact_id = :contact_id;
select * from t_bs_statement_bill_detail_v2 where bill_id = :bill_id;
select * from t_bs_statement_send_record_v2 where bill_id = :bill_id order by id desc;
select * from t_bs_statement_bill_task_v2 where sid = :sid order by id desc limit 50;

常用 rg 命令

rg -n "class StatementBill|reGenerate|sendMsg|resend|getSendResult" application/controllers/reports/StatementBill.php
rg -n "class StatementStrategy|bindCustomers|initializeStrategy|sendSyncTaskMessage" application/controllers/reports application/Services/Statement
rg -n "StatementNotify|StatementTasks|StatementSendConsumer|StatementStrategyConsumer" application/controllers/tasks
rg -n "T_BS_STATEMENT|statement_bill|statement_send|statement_strategy" application

改动风险和回归清单

  • 策略:新增、编辑、启停、删除、客户绑定/解绑。
  • 生成:定时生成、手工生成、重新生成、重复生成。
  • 审核:审核、取消审核、删除限制。
  • 发送:单发、批量发送、重发、发送结果。
  • 收款:待付款订单、收款同步、部分收款、全部收款。