'use strict'; /** * 出入库明细模型,对应 stock_items 表(出入库单据的共用副表) * bill_type 区分单据类型(1入库 2出库),bill_id 关联对应单据头 * @param {Egg.Application} app egg application * @return {object} stockItem model */ module.exports = app => { const { BIGINT, STRING, TINYINT, DECIMAL, DATE } = app.Sequelize; const StockItem = app.model.define('stock_item', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' }, bill_type: { type: TINYINT, allowNull: false, comment: '单据类型:1入库 2出库' }, bill_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '关联单据头ID' }, shop_id: { type: BIGINT.UNSIGNED, comment: '店铺/仓库ID(冗余单据的店铺)' }, product_id: { type: BIGINT.UNSIGNED, comment: '系统商品ID,为空表示仅登记名称' }, combo_product_id: { type: BIGINT.UNSIGNED, comment: '组合商品ID' }, shop_product_id: { type: BIGINT.UNSIGNED, comment: '店铺商品ID' }, name: { type: STRING(255), comment: '商品名称(快照)' }, code: { type: STRING(100), comment: '商品编码(快照)' }, inventory_type: { type: STRING(10), allowNull: false, defaultValue: 'ZP', comment: '库存类型:ZP=正品 CC=残次(对齐 WMS 入库单行级 inventoryType)' }, spec: { type: STRING(255), comment: '规格(快照)' }, unit: { type: STRING(20), comment: '单位' }, quantity: { type: DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '数量' }, plan_in_qty: { type: DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '应入数量(入库明细,默认取明细数量)' }, actual_in_qty: { type: DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '已入数量(入库明细,WMS 收货确认回推)' }, plan_out_qty: { type: DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '应出数量(出库明细,默认取明细数量)' }, actual_out_qty: { type: DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '已出数量(出库明细,WMS 发货回推)' }, price: { type: DECIMAL(12, 2), comment: '单价' }, total_amount: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '明细金额' }, remark: { type: STRING(255), comment: '备注' }, created_at: { type: DATE, comment: '创建时间' }, updated_at: { type: DATE, comment: '更新时间' }, }); StockItem.associate = () => { app.model.StockItem.belongsTo(app.model.Principal, { foreignKey: 'principal_id', as: 'principal' }); app.model.StockItem.belongsTo(app.model.Product, { foreignKey: 'product_id', as: 'product' }); app.model.StockItem.belongsTo(app.model.ComboProduct, { foreignKey: 'combo_product_id', as: 'comboProduct' }); app.model.StockItem.belongsTo(app.model.ShopProduct, { foreignKey: 'shop_product_id', as: 'shopProduct' }); }; return StockItem; };