'use strict'; module.exports = app => { const { STRING, INTEGER, BIGINT, ENUM, DATE, TINYINT } = app.Sequelize; const Storage = app.model.define('wms_storage', { id: { type: BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, comment: '库存ID', }, warehouse_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID', references: { model: 'wms_store', key: 'id', }, }, store_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '货主ID', references: { model: 'wms_store', key: 'id', }, }, space_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '库位ID', references: { model: 'wms_location_space', key: 'id', }, }, stock_id: { type: BIGINT(20), allowNull: false, comment: '商品SKU ID', references: { model: 'wms_stock_warehouse', key: 'id', }, }, batch_no: { type: STRING(100), allowNull: false, comment: '批次号', validate: { notEmpty: true, }, }, inventory_type: { type: ENUM('ZP', 'CC', 'JS', 'XS', 'ZT', 'DJ', 'FP'), allowNull: false, defaultValue: 'ZP', comment: '库存类型:ZP=正品, CC=残次, JS=机损, XS=箱损, ZT=在途库存, DJ=冻结, FP=废品', }, quantity: { type: INTEGER(12), allowNull: false, defaultValue: 0, comment: '库存数量', }, locked_quantity: { type: INTEGER(12), allowNull: false, defaultValue: 0, comment: '锁定数量', }, available_quantity: { type: INTEGER(12), allowNull: false, defaultValue: 0, comment: '可用数量', }, production_date: { type: DATE, allowNull: true, comment: '生产日期', }, expiry_date: { type: DATE, allowNull: true, comment: '过期日期', }, inbound_time: { type: DATE, allowNull: false, comment: '入库时间', }, inbound_order_no: { type: STRING(100), allowNull: false, comment: '入库单号', validate: { notEmpty: true, }, }, receiving_id: { type: INTEGER, allowNull: true, comment: '收货单ID', references: { model: 'wms_receiving', key: 'id', }, }, entry_order_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '入库单ID', references: { model: 'wms_entry_order', key: 'id', }, }, supplier_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '供应商ID', }, supplier_batch_no: { type: STRING(100), allowNull: true, comment: '供应商批次号', }, is_enabled: { type: TINYINT(1), allowNull: false, defaultValue: 1, comment: '是否启用', }, created_at: { type: DATE, allowNull: true, defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, updated_at: { type: DATE, allowNull: true, defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), comment: '更新时间', }, delete_at: { type: DATE, allowNull: true, comment: '删除时间', }, }, { tableName: 'wms_storage', comment: '库存表', timestamps: false, paranoid: true, deletedAt: 'delete_at', indexes: [ { name: 'uk_location_sku_batch_type', unique: true, fields: [ 'space_id', 'stock_id', 'batch_no', 'inventory_type' ], }, { name: 'idx_warehouse_location', fields: [ 'warehouse_id', 'space_id' ], }, { name: 'idx_sku_batch', fields: [ 'stock_id', 'batch_no' ], }, { name: 'idx_inventory_type', fields: [ 'inventory_type' ], }, { name: 'idx_expiry_date', fields: [ 'expiry_date' ], }, { name: 'idx_inbound_order', fields: [ 'inbound_order_no' ], }, { name: 'idx_receiving_id', fields: [ 'receiving_id' ], }, { name: 'idx_entry_order_id', fields: [ 'entry_order_id' ], }, { name: 'idx_store_id', fields: [ 'store_id' ], }, { name: 'idx_is_enabled', fields: [ 'is_enabled' ], }, { name: 'idx_delete_at', fields: [ 'delete_at' ], }, { name: 'idx_warehouse_stock', fields: [ 'warehouse_id', 'stock_id' ], }, { name: 'idx_available_quantity', fields: [ 'available_quantity' ], }, ], }); // 关联关系 Storage.associate = function() { const { Storage, Store, LocationSpace, StockWarehouse } = app.model; // 关联仓库 Storage.belongsTo(Store, { foreignKey: 'warehouse_id', as: 'warehouse', }); // 关联货主 Storage.belongsTo(Store, { foreignKey: 'store_id', as: 'store', }); // 关联库位 Storage.belongsTo(LocationSpace, { foreignKey: 'space_id', as: 'space', }); // 关联商品SKU Storage.belongsTo(StockWarehouse, { foreignKey: 'stock_id', as: 'stock', }); // 关联收货单 Storage.belongsTo(app.model.Receiving, { foreignKey: 'receiving_id', as: 'receiving', }); // 关联入库单 Storage.belongsTo(app.model.EntryOrder, { foreignKey: 'entry_order_id', as: 'entryOrder', }); }; // 实例方法 Storage.prototype.getAvailableQuantity = function() { return this.quantity - this.locked_quantity; }; Storage.prototype.isAvailable = function() { return this.is_enabled === 1 && this.available_quantity > 0; }; Storage.prototype.isLocked = function() { return this.locked_quantity > 0; }; Storage.prototype.canAllocate = function(quantity) { return this.is_enabled === 1 && this.available_quantity >= quantity; }; Storage.prototype.isNormalProduct = function() { return this.inventory_type === 'ZP'; }; Storage.prototype.isDefective = function() { return this.inventory_type === 'CC'; }; Storage.prototype.isExpired = function() { if (!this.expiry_date) return false; return new Date(this.expiry_date) < new Date(); }; return Storage; };