Browse Source

goods: 出库单审核通过扣除库存

Yi 4 months ago
parent
commit
51796d5c43
1 changed files with 47 additions and 9 deletions
  1. 47 9
      module/backend/goods.go

+ 47 - 9
module/backend/goods.go

@@ -363,15 +363,53 @@ func (s *StoreEntry) OutboundAudit(ctx context.Context, req *pasturePb.OutboundA
 		return xerr.Custom("登录人信息失效")
 	}
 
-	if err = s.DB.Model(outbound).
-		Where("id = ?", outbound.Id).
-		Updates(map[string]interface{}{
-			"audit_status":    req.AuditStatus,
-			"examine_id":      currentUser.Id,
-			"examine_name":    currentUser.Name,
-			"examine_remarks": req.ExamineRemarks,
-			"examine_at":      time.Now().Unix(),
-		}).Error; err != nil {
+	outboundLogs, err := s.GetOutboundLogsByOutboundId(ctx, int64(outbound.Id))
+	if err != nil {
+		return xerr.WithStack(err)
+	}
+
+	if len(outboundLogs) <= 0 {
+		return xerr.Custom("出库单商品不存在")
+	}
+
+	if err = s.DB.Transaction(func(tx *gorm.DB) error {
+		if err = tx.Model(outbound).
+			Where("id = ?", outbound.Id).
+			Updates(map[string]interface{}{
+				"audit_status":    req.AuditStatus,
+				"examine_id":      currentUser.Id,
+				"examine_name":    currentUser.Name,
+				"examine_remarks": req.ExamineRemarks,
+				"examine_at":      time.Now().Unix(),
+			}).Error; err != nil {
+			return xerr.WithStack(err)
+		}
+
+		if req.AuditStatus != pasturePb.AuditStatus_Pass {
+			return nil
+		}
+
+		tableName := ""
+		switch outbound.OutType {
+		case pasturePb.OutType_Drugs:
+			tableName = new(model.Drugs).TableName()
+		case pasturePb.OutType_Medical_Equipment:
+			tableName = new(model.MedicalEquipment).TableName()
+		default:
+			return nil
+		}
+
+		for _, v := range outboundLogs {
+			if err = tx.Table(tableName).
+				Where("id = ?", v.GoodsId).
+				Updates(map[string]interface{}{
+					"quantity": gorm.Expr("quantity - ?", v.Quantity),
+				}).Error; err != nil {
+				return xerr.WithStack(err)
+			}
+		}
+		return nil
+	}); err != nil {
 		return xerr.WithStack(err)
 	}