oms/backend/database/migrations/20260909002200-create-stock-orders.js
liufumingren cbd16752a5 feat(backend): 商品、店铺、组合商品、仓库与库存模块
- 商品/商品图片、店铺、店铺商品、组合商品及明细
- 仓库、仓库商品(WMS itemId 映射)、仓库接口配置与字段定义(warehouseApi/warehouseApiField)
- 库存:stock_items、product_stocks(ZC/CC 双维度)与库存重算工具(utils/recalcStock.js)
- 库存单据与对接日志:stock orders、api_outbound_logs
- 相关迁移:shops/shop_products/stock_orders/warehouses/warehouse_apis/warehouse_products/product_stocks/files-images 等表与字段
2026-09-13 02:23:39 +08:00

144 lines
8.0 KiB
JavaScript
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.

'use strict';
/**
* 22. 出入库与明细表:
* - inbound_orders 入库单(单据头)
* - outbound_orders 出库单(单据头)
* - stock_items 出入库明细(共用表,bill_type 区分:1入库 2出库;bill_id 关联对应单据头)
*/
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(
'inbound_orders',
{
id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, autoIncrement: true, primaryKey: true },
bill_no: { type: Sequelize.STRING(50), allowNull: false, comment: '入库单号' },
principal_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '所属主体ID',
references: { model: 'principals', key: 'id' },
},
shop_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: true,
comment: '来源店铺/仓库ID',
references: { model: 'shops', key: 'id' },
onDelete: 'SET NULL',
},
inbound_type: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '入库类型:1采购入库 2退货入库 9其他' },
status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1待入库 2已入库 3已取消' },
supplier: { type: Sequelize.STRING(255), allowNull: true, comment: '供应商' },
operator: { type: Sequelize.STRING(50), allowNull: true, comment: '操作人' },
total_amount: { type: Sequelize.DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '单据总金额' },
remark: { type: Sequelize.STRING(255), allowNull: true, comment: '备注' },
created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') },
updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP') },
},
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '入库单' }
);
await queryInterface.addIndex('inbound_orders', { fields: ['principal_id'], name: 'idx_inbound_orders_principal' });
await queryInterface.addIndex('inbound_orders', { fields: ['shop_id'], name: 'idx_inbound_orders_shop' });
await queryInterface.addIndex('inbound_orders', { fields: ['status'], name: 'idx_inbound_orders_status' });
await queryInterface.addIndex('inbound_orders', { fields: ['bill_no'], unique: true, name: 'uk_inbound_orders_bill_no' });
await queryInterface.createTable(
'outbound_orders',
{
id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, autoIncrement: true, primaryKey: true },
bill_no: { type: Sequelize.STRING(50), allowNull: false, comment: '出库单号' },
principal_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '所属主体ID',
references: { model: 'principals', key: 'id' },
},
shop_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: true,
comment: '目标店铺/仓库ID',
references: { model: 'shops', key: 'id' },
onDelete: 'SET NULL',
},
outbound_type: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '出库类型:1销售出库 2退货出库 9其他' },
status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1待出库 2已出库 3已取消' },
customer: { type: Sequelize.STRING(255), allowNull: true, comment: '客户/收货方' },
operator: { type: Sequelize.STRING(50), allowNull: true, comment: '操作人' },
total_amount: { type: Sequelize.DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '单据总金额' },
remark: { type: Sequelize.STRING(255), allowNull: true, comment: '备注' },
created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') },
updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP') },
},
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '出库单' }
);
await queryInterface.addIndex('outbound_orders', { fields: ['principal_id'], name: 'idx_outbound_orders_principal' });
await queryInterface.addIndex('outbound_orders', { fields: ['shop_id'], name: 'idx_outbound_orders_shop' });
await queryInterface.addIndex('outbound_orders', { fields: ['status'], name: 'idx_outbound_orders_status' });
await queryInterface.addIndex('outbound_orders', { fields: ['bill_no'], unique: true, name: 'uk_outbound_orders_bill_no' });
await queryInterface.createTable(
'stock_items',
{
id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, autoIncrement: true, primaryKey: true },
principal_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '所属主体ID',
references: { model: 'principals', key: 'id' },
},
bill_type: { type: Sequelize.TINYINT, allowNull: false, comment: '单据类型:1入库 2出库' },
bill_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '关联单据头ID(inbound_orders / outbound_orders)' },
shop_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: true,
comment: '店铺/仓库ID(冗余单据的店铺)',
references: { model: 'shops', key: 'id' },
onDelete: 'SET NULL',
},
product_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: true,
comment: '系统商品ID,为空表示仅登记名称',
references: { model: 'products', key: 'id' },
onDelete: 'SET NULL',
},
combo_product_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: true,
comment: '组合商品ID',
references: { model: 'combo_products', key: 'id' },
onDelete: 'SET NULL',
},
shop_product_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: true,
comment: '店铺商品ID',
references: { model: 'shop_products', key: 'id' },
onDelete: 'SET NULL',
},
name: { type: Sequelize.STRING(255), allowNull: true, comment: '商品名称(快照)' },
code: { type: Sequelize.STRING(100), allowNull: true, comment: '商品编码(快照)' },
spec: { type: Sequelize.STRING(255), allowNull: true, comment: '规格(快照)' },
unit: { type: Sequelize.STRING(20), allowNull: true, comment: '单位' },
quantity: { type: Sequelize.DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '数量' },
price: { type: Sequelize.DECIMAL(12, 2), allowNull: true, comment: '单价' },
total_amount: { type: Sequelize.DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '明细金额' },
remark: { type: Sequelize.STRING(255), allowNull: true, comment: '备注' },
created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') },
updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP') },
},
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '出入库明细表' }
);
await queryInterface.addIndex('stock_items', { fields: ['bill_type', 'bill_id'], name: 'idx_stock_items_bill' });
await queryInterface.addIndex('stock_items', { fields: ['principal_id'], name: 'idx_stock_items_principal' });
await queryInterface.addIndex('stock_items', { fields: ['shop_id'], name: 'idx_stock_items_shop' });
await queryInterface.addIndex('stock_items', { fields: ['product_id'], name: 'idx_stock_items_product' });
},
async down(queryInterface) {
await queryInterface.dropTable('stock_items');
await queryInterface.dropTable('outbound_orders');
await queryInterface.dropTable('inbound_orders');
},
};