From 82c058c91daed212fd9b3807dca9ca80217ce738 Mon Sep 17 00:00:00 2001
From: wangpengfei <274878379@qq.com>
Date: 星期三, 30 八月 2023 20:25:01 +0800
Subject: [PATCH] fix

---
 service/purchase/purchase.go |   74 ++++++++++++++++++++++++------------
 1 files changed, 49 insertions(+), 25 deletions(-)

diff --git a/service/purchase/purchase.go b/service/purchase/purchase.go
index 352395d..2251d8e 100644
--- a/service/purchase/purchase.go
+++ b/service/purchase/purchase.go
@@ -2,6 +2,7 @@
 
 import (
 	"context"
+	"errors"
 	"github.com/shopspring/decimal"
 	"github.com/spf13/cast"
 	"gorm.io/gorm"
@@ -23,24 +24,44 @@
 //@param: params *purchaserequest.AddPurchase
 //@return: err error
 
-func (slf *PurchaseService) CreatePurchase(params *purchase.Purchase, productList []*purchase.PurchaseProducts) (err error) {
+func (slf *PurchaseService) CreatePurchase(record *purchase.Purchase, productList []*purchase.PurchaseProducts) (err error) {
+	err = DealPrice(record, productList)
+	if err != nil {
+		return err
+	}
 	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
-		var quantity decimal.Decimal
-		for _, product := range productList {
-			quantity = quantity.Add(product.Amount)
-		}
-		params.Quantity = quantity
-		err = tx.Create(&params).Error
+		err = tx.Create(&record).Error
 		if err != nil {
 			return err
 		}
 		for _, product := range productList {
-			product.PurchaseId = cast.ToInt(params.ID)
+			product.PurchaseId = cast.ToInt(record.ID)
 		}
 		return tx.Create(productList).Error
 	})
 
 	return err
+}
+
+func DealPrice(record *purchase.Purchase, productList []*purchase.PurchaseProducts) error {
+	var quantity decimal.Decimal
+	var totalPrice decimal.Decimal
+	var realTotalPrice decimal.Decimal
+	for _, product := range productList {
+		quantity = quantity.Add(product.Amount)
+		totalPrice = totalPrice.Add(product.Price.Mul(product.Amount))
+	}
+	record.Quantity = quantity
+	if !totalPrice.Equal(record.TotalPrice) {
+		return errors.New("浠风◣鎬昏璁$畻閿欒")
+	}
+	realTotalPrice = record.CalcRealTotalPrice()
+	if !realTotalPrice.Equal(record.RealTotalPrice) {
+		return errors.New("鏈�缁堜环鏍艰绠楅敊璇�")
+	}
+	record.UnInvoiceAmount = record.RealTotalPrice
+	record.ShouldPayAmount = record.RealTotalPrice
+	return nil
 }
 
 //@function: DeletePurchase
@@ -65,12 +86,11 @@
 //@return: err error
 
 func (slf *PurchaseService) UpdatePurchase(params *purchase.Purchase, productList []*purchase.PurchaseProducts) (err error) {
+	err = DealPrice(params, productList)
+	if err != nil {
+		return err
+	}
 	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
-		var quantity decimal.Decimal
-		for _, product := range productList {
-			quantity = quantity.Add(product.Amount)
-		}
-		params.Quantity = quantity
 		err = tx.Where("id = ?", params.ID).Updates(params).Error
 		if err != nil {
 			return err
@@ -115,7 +135,7 @@
 			Or("Product.name like ?", "%"+info.Keyword+"%").
 			Or("supplier.name like ?", "%"+info.Keyword+"%")
 	}
-	var purchaseList []purchase.Purchase
+	var purchaseList []*purchase.Purchase
 	err = db.Count(&total).Error
 	if err != nil {
 		return purchaseList, total, err
@@ -194,20 +214,24 @@
 			continue
 		}
 		inspectOrder := &qualityinspect.QualityInspect{
-			InspectType:   qualityinspect.InspectType_InspectTypePurchase,
-			MaterialType:  qualityinspect.MaterialType_MaterialTypeRaw,
-			MaterialName:  product.Name,
-			MaterialId:    product.Number,
-			MaterialTp:    product.ModelNumber,
-			MaterialUnit:  product.Unit,
-			Supplier:      record.Supplier.Name,
-			WarehouseName: "閲囪喘鎬讳粨",
-			ReportAmount:  productItem.Amount.InexactFloat64(),
-			InspectMethod: qualityinspect.InspectMethod_InspectMethodAll,
-			InspectAmount: productItem.Amount.InexactFloat64(),
+			InspectType:     qualityinspect.InspectType_InspectTypePurchase,
+			MaterialType:    qualityinspect.MaterialType_MaterialTypeRaw,
+			MaterialName:    product.Name,
+			MaterialId:      product.Number,
+			MaterialTp:      product.ModelNumber,
+			MaterialUnit:    product.Unit,
+			Supplier:        record.Supplier.Name,
+			WarehouseName:   "閲囪喘鎬讳粨",
+			ReportAmount:    productItem.Amount.InexactFloat64(),
+			InspectMethod:   qualityinspect.InspectMethod_InspectMethodAll,
+			InspectAmount:   productItem.Amount.InexactFloat64(),
+			PurchaseOrderId: record.Number,
 		}
 		inspectOrders = append(inspectOrders, inspectOrder)
 	}
+	if len(inspectOrders) == 0 {
+		return nil
+	}
 	inspectRequest := qualityinspect.SendPurchaseInspectRequest{List: inspectOrders}
 	_, err = qualityinspect.NewQualityInspectServiceClient(qualityinspect.Conn).SendPurchaseInspect(context.Background(), &inspectRequest)
 	return err

--
Gitblit v1.8.0