|
@@ -12,7 +12,8 @@ type Manifest struct {
|
|
|
ManifestGenre string `json:"manifestGenre" gorm:"comment:'货单类型'"`
|
|
ManifestGenre string `json:"manifestGenre" gorm:"comment:'货单类型'"`
|
|
|
Title string `json:"title" gorm:"comment:'标题'"`
|
|
Title string `json:"title" gorm:"comment:'标题'"`
|
|
|
Cargos []Cargo `json:"cargos" gorm:"foreignKey:ManifestId"`
|
|
Cargos []Cargo `json:"cargos" gorm:"foreignKey:ManifestId"`
|
|
|
- Custodian string `json:"custodian" gorm:"comment:管理人"`
|
|
|
|
|
|
|
+ Custodian string `json:"custodian" gorm:"comment:负责人"`
|
|
|
|
|
+ Remarks string `json:"remarks" gorm:"comment:'备注'"`
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (Manifest) TableName() string {
|
|
func (Manifest) TableName() string {
|
|
@@ -75,8 +76,8 @@ func CreateInboundManifest(manifestGenre, title, custodian string, cargos []Carg
|
|
|
for _, cargo := range manifest.Cargos {
|
|
for _, cargo := range manifest.Cargos {
|
|
|
// 检查是否已存在相同商品、仓库、库区、库位的记录
|
|
// 检查是否已存在相同商品、仓库、库区、库位的记录
|
|
|
var goods Goods
|
|
var goods Goods
|
|
|
- err := tx.Where("commodity_id = ? AND warehouse_id = ? AND storage_area_id = ? AND place_id = ?",
|
|
|
|
|
- cargo.CommodityId, cargo.WarehouseId, cargo.StorageAreaId, cargo.PlaceId).
|
|
|
|
|
|
|
+ err := tx.Where("good_id = ?",
|
|
|
|
|
+ cargo.GoodsId).
|
|
|
First(&goods).Error
|
|
First(&goods).Error
|
|
|
|
|
|
|
|
if err == nil {
|
|
if err == nil {
|
|
@@ -86,17 +87,8 @@ func CreateInboundManifest(manifestGenre, title, custodian string, cargos []Carg
|
|
|
return fmt.Errorf("更新库存失败: %v", err)
|
|
return fmt.Errorf("更新库存失败: %v", err)
|
|
|
}
|
|
}
|
|
|
} else if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
} else if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
- // 不存在记录:新建库存
|
|
|
|
|
- newGoods := Goods{
|
|
|
|
|
- CommodityId: cargo.CommodityId,
|
|
|
|
|
- Number: cargo.Number,
|
|
|
|
|
- WarehouseId: cargo.WarehouseId,
|
|
|
|
|
- StorageAreaId: cargo.StorageAreaId,
|
|
|
|
|
- PlaceId: cargo.PlaceId,
|
|
|
|
|
- }
|
|
|
|
|
- if err := tx.Create(&newGoods).Error; err != nil {
|
|
|
|
|
- return fmt.Errorf("创建库存失败: %v", err)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // 不存在记录
|
|
|
|
|
+ return fmt.Errorf("查询库存失败: %v", err)
|
|
|
} else {
|
|
} else {
|
|
|
return fmt.Errorf("查询库存失败: %v", err)
|
|
return fmt.Errorf("查询库存失败: %v", err)
|
|
|
}
|
|
}
|
|
@@ -112,14 +104,14 @@ func CreateOutboundManifest(manifestGenre, title, custodian string, cargos []Car
|
|
|
// 1. 检查库存是否充足(预扣减检查)
|
|
// 1. 检查库存是否充足(预扣减检查)
|
|
|
for _, cargo := range cargos {
|
|
for _, cargo := range cargos {
|
|
|
var goods Goods
|
|
var goods Goods
|
|
|
- if err := tx.Where("commodity_id = ? AND warehouse_id = ? AND storage_area_id = ? AND place_id = ?",
|
|
|
|
|
- cargo.CommodityId, cargo.WarehouseId, cargo.StorageAreaId, cargo.PlaceId).
|
|
|
|
|
|
|
+ if err := tx.Where("good_id = ?",
|
|
|
|
|
+ cargo.GoodsId).
|
|
|
First(&goods).Error; err != nil {
|
|
First(&goods).Error; err != nil {
|
|
|
return fmt.Errorf("库存记录不存在: %v", err)
|
|
return fmt.Errorf("库存记录不存在: %v", err)
|
|
|
}
|
|
}
|
|
|
if goods.Number < cargo.Number {
|
|
if goods.Number < cargo.Number {
|
|
|
return fmt.Errorf("商品 %v 库存不足(当前: %d,需要: %d)",
|
|
return fmt.Errorf("商品 %v 库存不足(当前: %d,需要: %d)",
|
|
|
- cargo.Commodity.Name, goods.Number, cargo.Number)
|
|
|
|
|
|
|
+ cargo.Goods.Name, goods.Number, cargo.Number)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -137,8 +129,8 @@ func CreateOutboundManifest(manifestGenre, title, custodian string, cargos []Car
|
|
|
// 3. 扣减库存
|
|
// 3. 扣减库存
|
|
|
for _, cargo := range manifest.Cargos {
|
|
for _, cargo := range manifest.Cargos {
|
|
|
if err := tx.Model(&Goods{}).
|
|
if err := tx.Model(&Goods{}).
|
|
|
- Where("commodity_id = ? AND warehouse_id = ? AND storage_area_id = ? AND place_id = ?",
|
|
|
|
|
- cargo.CommodityId, cargo.WarehouseId, cargo.StorageAreaId, cargo.PlaceId).
|
|
|
|
|
|
|
+ Where("goods_id = ?",
|
|
|
|
|
+ cargo.GoodsId).
|
|
|
Update("number", gorm.Expr("number - ?", cargo.Number)).
|
|
Update("number", gorm.Expr("number - ?", cargo.Number)).
|
|
|
Error; err != nil {
|
|
Error; err != nil {
|
|
|
return fmt.Errorf("扣减库存失败: %v", err)
|
|
return fmt.Errorf("扣减库存失败: %v", err)
|