1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#油品出库多销售台账绑定 + 销售台账到货记录
#对应需求:一次油品出库可绑定多个销售台账(对应多个客户,且各台账须含同一产品规格);
#         销售台账新增「到货记录」(到货数量/已出库车辆/附件),发货台账据此改为层级展示
#目标库:product-inventory-management-rbhb
#说明:纯新增两张表;第 3 段的存量清理是幂等的,本地/线上若无数据则为 no-op
 
#1. 出库单 ↔ 销售台账 绑定表(一个出库单可绑多个台账,各带本次分摊数量)
#   列类型对齐存量库:stock_out_record.id / sales_ledger.id / sales_ledger_product.id / customer.id 均为 int
create table stock_out_record_sales_ledger
(
    id                     bigint         not null auto_increment comment '主键',
    stock_out_record_id    int            not null comment '出库记录id(stock_out_record.id)',
    sales_ledger_id        int            not null comment '销售台账id(sales_ledger.id)',
    sales_ledger_product_id int           null comment '销售台账销售明细id(sales_ledger_product.id,type=1)',
    customer_id            int            null comment '客户id(冗余自台账)',
    customer_name          varchar(100)   null comment '客户名称(冗余自台账)',
    quantity               decimal(16, 4) null comment '本台账对应的出库数量(吨)',
    create_time            datetime       null comment '创建时间',
    create_user            bigint         null comment '创建人',
    update_time            datetime       null comment '更新时间',
    update_user            bigint         null comment '更新人',
    tenant_id              bigint         null comment '租户id',
    dept_id                bigint         null comment '部门id',
    primary key (id),
    key idx_sosl_record (stock_out_record_id),
    key idx_sosl_ledger (sales_ledger_id)
) engine = InnoDB
  default charset = utf8mb4
  row_format = DYNAMIC comment ='出库记录-销售台账绑定表';
 
#2. 销售台账到货记录表(发货台账里的子行)
#   attachments 存 /common/public/upload 返回 previewURL 的 JSON 数组(永久链接,不走 storage_blob 签名体系)
#   vehicle.id 是 bigint,与上面几张 int 表不同,别搞混
create table sales_ledger_arrival
(
    id                      bigint         not null auto_increment comment '主键',
    sales_ledger_id         int            not null comment '销售台账id(sales_ledger.id)',
    sales_ledger_product_id int            null comment '销售台账销售明细id(sales_ledger_product.id,type=1)',
    stock_out_record_id     int            not null comment '出库记录id(stock_out_record.id),发货台账的父行',
    vehicle_id              bigint         null comment '车辆id(vehicle.id)',
    truck_plate_no          varchar(20)    null comment '到货车牌号',
    arrival_quantity        decimal(16, 4) null comment '到货数量(吨)',
    arrival_date            date           null comment '到货日期',
    attachments             text           null comment '附件(JSON数组,URL列表)',
    remark                  varchar(255)   null comment '备注',
    create_time             datetime       null comment '创建时间',
    create_user             bigint         null comment '创建人',
    update_time             datetime       null comment '更新时间',
    update_user             bigint         null comment '更新人',
    tenant_id               bigint         null comment '租户id',
    dept_id                 bigint         null comment '部门id',
    primary key (id),
    key idx_sla_ledger (sales_ledger_id),
    key idx_sla_record (stock_out_record_id)
) engine = InnoDB
  default charset = utf8mb4
  row_format = DYNAMIC comment ='销售台账到货记录表';
 
#3. 存量清理(幂等):上一轮「油品出库审批通过自动生成发货单」留下的数据
#   本轮回退该生成逻辑,改为由「到货记录」驱动销售台账状态,故把已生成的发货单删掉、只清 record_id
#   油品出库的 record_type 保持原样(「1」),不改成「13」—— 改了会让出库管理列表按出库类型筛选时看不到它
delete
from shipping_product_detail
where shipping_info_id in
      (select record_id
       from stock_out_record
       where record_type = '13' and record_id > 0 and (type is null or type = ''));
 
delete
from shipping_info
where id in
      (select record_id
       from stock_out_record
       where record_type = '13' and record_id > 0 and (type is null or type = ''));
 
update stock_out_record
set record_id = 0
where record_type = '13' and record_id > 0 and (type is null or type = '');
 
#4. 存量回填(幂等):上一轮「一个出库单绑一个销售台账」存在 stock_out_record.sales_ledger_id 单值列里,
#   本轮改成绑定表,不回填的话这些老数据在财务/开票/收款以及「可到货车辆」里会整批消失。
#   数量取 stock_out_num,明细 id 取该台账同规格 type=1 明细中 id 最小的那条;数量<=0 或无匹配明细的老单跳过
#   stock_out_record 没有 tenant_id 列,故 tenant_id 不填(该表也不是租户维度)
insert into stock_out_record_sales_ledger
(stock_out_record_id, sales_ledger_id, sales_ledger_product_id, customer_id, customer_name, quantity,
 create_time, create_user, update_time, update_user, dept_id)
select sor.id,
       sl.id,
       slp.id,
       sl.customer_id,
       sl.customer_name,
       sor.stock_out_num,
       ifnull(sor.create_time, now()),
       ifnull(sor.create_user, 0),
       now(),
       ifnull(sor.create_user, 0),
       sor.dept_id
from stock_out_record sor
         join sales_ledger sl on sl.id = sor.sales_ledger_id and ifnull(sl.ledger_type, '') <> '代储'
         join sales_ledger_product slp on slp.sales_ledger_id = sl.id
             and slp.type = 1
             and slp.id = (select min(x.id)
                           from sales_ledger_product x
                           where x.sales_ledger_id = sl.id
                             and x.type = 1
                             and x.product_model_id = sor.product_model_id)
where (sor.type is null or sor.type = '')
  and ifnull(sor.stock_out_num, 0) > 0
  and not exists (select 1
                  from stock_out_record_sales_ledger b
                  where b.stock_out_record_id = sor.id);
 
#5. 开票申请 / 收款单 增加「关联到货绑定行 id」列。
#   背景:一次油品出库可按客户拆成多条绑定行(stock_out_record_sales_ledger),财务候选列表的粒度
#   因此从「出库单」下沉到「出库单×客户」。但 stock_out_record_ids 里存的是出库单 id,两种 id
#   空间都是从小自增的整数,混在一个列里必然撞号(绑定行 1 与出库单 1 都会被 FIND_IN_SET 命中),
#   所以另开一列,stock_out_record_ids 的语义和类型保持不变,存量数据不受影响。
#   精度 255 与 stock_out_record_ids 保持一致(够存约 40 个 id)。
alter table account_invoice_application
    add column stock_out_binding_ids varchar(255) null comment '关联出库绑定行id(多选,stock_out_record_sales_ledger.id)' after stock_out_record_ids;
 
alter table account_sales_collection
    add column stock_out_binding_ids varchar(255) null comment '关联出库绑定行id(多选,stock_out_record_sales_ledger.id)' after stock_out_record_ids;