From d20acf38c36c11ee4428c3e74a17f5870dc61b51 Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期四, 16 十一月 2023 09:51:00 +0800
Subject: [PATCH] Merge branch 'master' of http://192.168.5.5:10010/r/aps/SRM

---
 service/purchase/purchase.go |  156 +++++++++++++++++++++++++++++----------------------
 1 files changed, 88 insertions(+), 68 deletions(-)

diff --git a/service/purchase/purchase.go b/service/purchase/purchase.go
index ffa39d2..b8654f4 100644
--- a/service/purchase/purchase.go
+++ b/service/purchase/purchase.go
@@ -25,27 +25,11 @@
 //@return: 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
-		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))
-		}
-		if !totalPrice.Equal(record.TotalPrice) {
-			return errors.New("浠风◣鎬昏璁$畻閿欒")
-		}
-		if !quantity.Equal(record.Quantity) {
-			return errors.New("浜у搧鏁伴噺璁$畻閿欒")
-		}
-		realTotalPrice = record.CalcRealTotalPrice()
-		if !realTotalPrice.Equal(record.RealTotalPrice) {
-			return errors.New("鏈�缁堜环鏍艰绠楅敊璇�")
-		}
-		record.UnInvoiceAmount = record.RealTotalPrice
-		record.ShouldPayAmount = record.RealTotalPrice
-
 		err = tx.Create(&record).Error
 		if err != nil {
 			return err
@@ -57,6 +41,27 @@
 	})
 
 	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
@@ -81,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
@@ -123,20 +127,32 @@
 	limit := info.PageSize
 	offset := info.PageSize * (info.Page - 1)
 	db := global.GVA_DB.Model(&purchase.Purchase{})
+	var ids []uint
+	var purchaseList = make([]*purchase.Purchase, 0)
 	if info.Keyword != "" {
-		db.Distinct("purchases.id").Joins("left join purchase_products on purchase_products.purchase_id = purchases.id").
-			Joins("left join Product on Product.Id = purchase_products.product_id").
-			Joins("left join supplier on supplier.Id = purchases.supplier_id").
-			Where("purchases.name like ?", "%"+info.Keyword+"%").
-			Or("Product.name like ?", "%"+info.Keyword+"%").
-			Or("supplier.name like ?", "%"+info.Keyword+"%")
+		db.Distinct("srm_purchase.id").Joins("left join srm_purchase_products on srm_purchase_products.purchase_id = srm_purchase.id").
+			Joins("left join material on material.id = srm_purchase_products.product_id").
+			Joins("left join srm_supplier on srm_supplier.Id = srm_purchase.supplier_id").
+			Where("srm_purchase.name like ?", "%"+info.Keyword+"%").
+			Or("material.name like ?", "%"+info.Keyword+"%").
+			Or("srm_supplier.name like ?", "%"+info.Keyword+"%")
+		err = db.Limit(limit).Offset(offset).Find(&ids).Error
+		if err != nil {
+			return purchaseList, total, err
+		}
 	}
-	var purchaseList []purchase.Purchase
+
 	err = db.Count(&total).Error
 	if err != nil {
 		return purchaseList, total, err
 	}
-	err = db.Limit(limit).Offset(offset).Find(&purchaseList).Error
+	if len(ids) != 0 {
+		db = global.GVA_DB.Model(&purchase.Purchase{})
+		err = db.Where("id in (?)", ids).Find(&purchaseList).Error
+	} else {
+		err = db.Limit(limit).Offset(offset).Find(&purchaseList).Error
+	}
+
 	return purchaseList, total, err
 }
 
@@ -157,33 +173,33 @@
 //@param: id uint
 //@return: err error
 
-func (slf *PurchaseService) Submit(id uint) (err error) {
+func (slf *PurchaseService) Submit(id int, status purchase.OrderStatus) (err error) {
 
-	purchaseData, err := slf.GetPurchase(id)
-	if err != nil {
-		return err
-	}
-	var targetStatus purchase.OrderStatus
-	switch purchaseData.Status {
-	case purchase.OrderStatusConfirmed:
-		targetStatus = purchase.OrderStatusReceived
-	case purchase.OrderStatusReceived:
-		targetStatus = purchase.OrderStatusStored
-	case purchase.OrderStatusStored:
-		targetStatus = purchase.OrderStatusCompleted
-	}
+	//purchaseData, err := slf.GetPurchase(id)
+	//if err != nil {
+	//	return err
+	//}
+	//var targetStatus purchase.OrderStatus
+	//switch purchaseData.Status {
+	//case purchase.OrderStatusConfirmed:
+	//	targetStatus = purchase.OrderStatusReceived
+	//case purchase.OrderStatusReceived:
+	//	targetStatus = purchase.OrderStatusStored
+	//case purchase.OrderStatusStored:
+	//	targetStatus = purchase.OrderStatusCompleted
+	//}
 	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
-		err = tx.Where("id = ?", id).Model(&purchase.Purchase{}).Updates(map[string]interface{}{"status": targetStatus}).Error
+		err = tx.Where("id = ?", id).Model(&purchase.Purchase{}).Updates(map[string]interface{}{"status": status}).Error
 		if err != nil {
 			return err
 		}
 
-		switch targetStatus {
-		case purchase.OrderStatusReceived:
-			return SendInspect(purchaseData)
-		case purchase.OrderStatusStored:
-		case purchase.OrderStatusCompleted:
-		}
+		//switch targetStatus {
+		//case purchase.OrderStatusReceived:
+		//	return SendInspect(purchaseData)
+		//case purchase.OrderStatusStored:
+		//case purchase.OrderStatusCompleted:
+		//}
 		return nil
 	})
 	return err
@@ -196,7 +212,7 @@
 	}
 	productIds := make([]uint, 0, len(productList))
 	for _, product := range productList {
-		productIds = append(productIds, uint(product.ProductId))
+		productIds = append(productIds, product.ID)
 	}
 	productService := &test.ProductService{}
 	_, productMap, err := productService.GetProducts(productIds)
@@ -205,24 +221,28 @@
 	}
 	inspectOrders := make([]*qualityinspect.QualityInspect, 0, len(productList))
 	for _, productItem := range productList {
-		product := productMap[uint(productItem.ProductId)]
+		product := productMap[productItem.ID]
 		if product == nil {
 			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)
@@ -253,7 +273,7 @@
 
 		for _, item := range list {
 			if item.ID != 0 {
-				err = tx.Save(item).Error
+				err = tx.Where("id = ?", item.ID).Updates(item).Error
 				if err != nil {
 					return err
 				}

--
Gitblit v1.8.0