Files
BMS/bms-back/sql/ar/ar_balance_writeoff.sql
T
2026-09-16 15:19:05 +08:00

76 lines
4.5 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- =====================================================================
-- 客户余额核销应收账单 V1.0 2026-09-16
-- 1) 余额核销单表 ar_balance_writeoff(资金来源:customer_account.balance)
-- 2) 存量账单未核销金额重算 customer_account.used_credit 占用
-- 3) 菜单与授权:客户账单(2435) / 账单详情(2436) / 余额核销(2437) / 反核销(2438)
-- =====================================================================
-- 余额核销单:用客户账户余额核销应收账单(与 ar_receipt_writeoff 收款核销并列,
-- 两者共享 ar_bill.written_off_amount / unwritten_amount 缓存字段)
CREATE TABLE IF NOT EXISTS ar_balance_writeoff (
writeoff_id BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '余额核销ID',
writeoff_no VARCHAR(50) NOT NULL COMMENT '核销单号(BWO开头)',
ar_bill_id BIGINT(20) NOT NULL COMMENT '应收账单ID',
bill_no VARCHAR(50) NOT NULL COMMENT '账单号快照',
customer_id BIGINT(20) NOT NULL COMMENT '客户ID',
account_id BIGINT(20) DEFAULT NULL COMMENT '客户账户ID',
currency VARCHAR(10) NOT NULL DEFAULT 'CNY' COMMENT '币种',
writeoff_amount DECIMAL(18,2) NOT NULL DEFAULT 0.00 COMMENT '本次核销金额',
balance_flow_id BIGINT(20) DEFAULT NULL COMMENT '核销扣余额流水ID',
reverse_flow_id BIGINT(20) DEFAULT NULL COMMENT '反核销回补余额流水ID',
writeoff_date DATE NOT NULL COMMENT '核销日期',
status VARCHAR(30) NOT NULL COMMENT 'EFFECTIVE/CANCELLED',
remark VARCHAR(500) DEFAULT NULL COMMENT '核销备注',
create_by VARCHAR(64) NOT NULL COMMENT '创建人',
create_time DATETIME NOT NULL COMMENT '创建时间',
cancel_by VARCHAR(64) DEFAULT NULL COMMENT '反核销人',
cancel_time DATETIME DEFAULT NULL COMMENT '反核销时间',
cancel_remark VARCHAR(500) DEFAULT NULL COMMENT '反核销原因',
PRIMARY KEY (writeoff_id),
UNIQUE KEY uk_ar_balance_writeoff_no (writeoff_no),
KEY idx_abw_bill (ar_bill_id, status),
KEY idx_abw_customer (customer_id, status),
KEY idx_abw_account (account_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='应收账单余额核销表';
-- 存量数据:为仅有账单、没有账户的客户补建账户(理论上CNY账户均已自动开户)
INSERT INTO customer_account (customer_id, currency, balance, frozen_balance, total_credit, extra_credit, used_credit, tax, version, create_by, create_time, del_flag)
SELECT b.customer_id, b.currency, 0, 0, 0, 0, 0, 0, 0, 'system', NOW(), '0'
FROM (
SELECT customer_id, currency FROM ar_bill
WHERE status IN ('CONFIRMED','PARTIAL_WRITTEN_OFF')
GROUP BY customer_id, currency
) b
LEFT JOIN customer_account a
ON a.customer_id = b.customer_id
AND a.currency COLLATE utf8mb4_general_ci = b.currency
AND a.del_flag = '0'
WHERE a.id IS NULL;
-- 已用额度 = 该客户该币种 已确认/部分核销账单的未核销金额合计
UPDATE customer_account a
JOIN (
SELECT customer_id, currency, SUM(unwritten_amount) AS used_total
FROM ar_bill
WHERE status IN ('CONFIRMED','PARTIAL_WRITTEN_OFF')
GROUP BY customer_id, currency
) t ON t.customer_id = a.customer_id
AND t.currency COLLATE utf8mb4_general_ci = a.currency
SET a.used_credit = t.used_total,
a.version = a.version + 1,
a.update_by = 'system',
a.update_time = NOW()
WHERE a.del_flag = '0';
-- 菜单(父菜单 2309 = 客户/供应商管理;如环境ID不同请自行调整)
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark)
VALUES
(2435, '客户账单', 2309, 3, 'customerbill', 'customersuppliermanagement/customerinfo/customerbill/index', '', 1, 0, 'C', '0', '0', 'customer:bill:list', 'documentation', 'admin', NOW(), '客户应收账单只读查询'),
(2436, '账单详情', 2435, 1, '', NULL, '', 1, 0, 'F', '0', '0', 'customer:bill:query', '#', 'admin', NOW(), ''),
(2437, '余额核销', 2435, 2, '', NULL, '', 1, 0, 'F', '0', '0', 'customer:bill:writeoff', '#', 'admin', NOW(), '用客户账户余额核销应收账单'),
(2438, '取消余额核销', 2435, 3, '', NULL, '', 1, 0, 'F', '0', '0', 'customer:bill:writeoff:cancel', '#', 'admin', NOW(), '反核销');
-- 普通角色授权(按实际角色调整)
INSERT INTO sys_role_menu (role_id, menu_id) VALUES
(2, 2435), (2, 2436), (2, 2437), (2, 2438);