From 6843bdb44b8d5294a21f2ee30886e0c5ad07a150 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期二, 29 八月 2023 18:00:57 +0800
Subject: [PATCH] 价格调整

---
 model/purchase/purchase.go |   94 ++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 77 insertions(+), 17 deletions(-)

diff --git a/model/purchase/purchase.go b/model/purchase/purchase.go
index 198fe97..4506751 100644
--- a/model/purchase/purchase.go
+++ b/model/purchase/purchase.go
@@ -8,23 +8,30 @@
 
 type Purchase struct {
 	global.GVA_MODEL
-	PurchaseTypeId int             `json:"purchaseTypeId" form:"purchaseType" gorm:"type:int(11);not null;default 0;comment:閲囪喘绫诲瀷id"` // 閲囪喘绫诲瀷id
-	PurchaseType   PurchaseType    `json:"purchaseType" gorm:"foreignKey:PurchaseTypeId"`
-	SupplierId     int             `json:"supplierId" form:"supplierId" gorm:"type:int(11);not null;default 0;comment:渚涘簲鍟唅d"` // 渚涘簲鍟唅d
-	Supplier       test.Supplier   `json:"supplier" gorm:"foreignKey:SupplierId"`
-	Number         string          `json:"number" form:"number" gorm:"unique;type:varchar(255);not null;default '';comment:閲囪喘缂栧彿"`      // 閲囪喘缂栧彿
-	Name           string          `json:"name" form:"name" gorm:"type:varchar(255);not null;default '';comment:閲囪喘鍚嶇О"`                 // 閲囪喘鍚嶇О
-	Quantity       decimal.Decimal `json:"quantity" form:"quantity" gorm:"type:decimal(12,4);not null;comment:閲囪喘鏁伴噺"`                   // 閲囪喘鏁伴噺
-	Contact        string          `json:"contact" form:"contact" gorm:"type:varchar(255);not null;default '';comment:鑱旂郴浜�"`            // 鑱旂郴浜�
-	Phone          string          `json:"phone" form:"phone" gorm:"type:varchar(255);not null;default '';comment:鑱旂郴浜虹數璇�"`              // 鑱旂郴浜虹數璇�
-	SigningDate    string          `json:"signingDate" form:"signingDate" gorm:"type:varchar(255);not null;default '';comment:绛剧害鏃ユ湡"`   // 绛剧害鏃ユ湡
-	DeliveryDate   string          `json:"deliveryDate" form:"deliveryDate" gorm:"type:varchar(255);not null;default '';comment:浜や粯鏃ユ湡"` //浜や粯鏃ユ湡
-	Remark         string          `json:"remark" form:"remark" gorm:"type:varchar(1000);not null;default '';comment:澶囨敞"`              //澶囨敞
-	Status         OrderStatus     `json:"status" form:"status" gorm:"type:tinyint(1);not null;default 0;comment:鐘舵��"`                  //鐘舵��
-	HandledBy      string          `json:"handledBy" form:"handledBy" gorm:"type:varchar(255);not null;default '';comment:缁忓姙浜�"`        //缁忓姙浜�
-	Creator        string          `json:"creator" form:"creator" gorm:"type:varchar(255);not null;default '';comment:鍒跺崟浜�"`            //鍒跺崟浜�
-	OrderType      string          `json:"orderType" form:"orderType" gorm:"type:varchar(255);not null;default '';comment:鍗曟嵁绫诲瀷"`       //鍗曟嵁绫诲瀷
-	Warehouse      string          `json:"warehouse" form:"warehouse" gorm:"type:varchar(255);not null;default '';comment:鏀惰揣浠撳簱"`       //鏀惰揣浠撳簱
+	PurchaseTypeId      int                 `json:"purchaseTypeId" form:"purchaseType" gorm:"type:int(11);not null;default 0;comment:閲囪喘绫诲瀷id"` // 閲囪喘绫诲瀷id
+	PurchaseType        PurchaseType        `json:"purchaseType" gorm:"foreignKey:PurchaseTypeId"`
+	OrderSource         string              `json:"orderSource" gorm:"type:varchar(255);not null;default '';comment:鍗曟嵁鏉ユ簮"`             // 鍗曟嵁鏉ユ簮
+	SupplierId          int                 `json:"supplierId" form:"supplierId" gorm:"type:int(11);not null;default 0;comment:渚涘簲鍟唅d"` // 渚涘簲鍟唅d
+	Supplier            test.Supplier       `json:"supplier" gorm:"foreignKey:SupplierId"`
+	Number              string              `json:"number" form:"number" gorm:"unique;type:varchar(255);not null;default '';comment:閲囪喘缂栧彿"`                     // 閲囪喘缂栧彿
+	Name                string              `json:"name" form:"name" gorm:"type:varchar(255);not null;default '';comment:閲囪喘鍚嶇О"`                                // 閲囪喘鍚嶇О
+	Contact             string              `json:"contact" form:"contact" gorm:"type:varchar(255);not null;default '';comment:鑱旂郴浜�"`                           // 鑱旂郴浜�
+	Phone               string              `json:"phone" form:"phone" gorm:"type:varchar(255);not null;default '';comment:鑱旂郴浜虹數璇�"`                             // 鑱旂郴浜虹數璇�
+	SigningDate         string              `json:"signingDate" form:"signingDate" gorm:"type:varchar(255);not null;default '';comment:绛剧害鏃ユ湡"`                  // 绛剧害鏃ユ湡
+	DeliveryDate        string              `json:"deliveryDate" form:"deliveryDate" gorm:"type:varchar(255);not null;default '';comment:浜や粯鏃ユ湡"`                //浜や粯鏃ユ湡
+	Remark              string              `json:"remark" form:"remark" gorm:"type:varchar(1000);not null;default '';comment:澶囨敞"`                             //澶囨敞
+	Status              OrderStatus         `json:"status" form:"status" gorm:"type:tinyint(1);not null;default 0;comment:鐘舵��"`                                 //鐘舵��
+	HandledBy           string              `json:"handledBy" form:"handledBy" gorm:"type:varchar(255);not null;default '';comment:缁忓姙浜�"`                       //缁忓姙浜�
+	Creator             string              `json:"creator" form:"creator" gorm:"type:varchar(255);not null;default '';comment:鍒跺崟浜�"`                           //鍒跺崟浜�
+	OrderType           string              `json:"orderType" form:"orderType" gorm:"type:varchar(255);not null;default '';comment:鍗曟嵁绫诲瀷"`                      //鍗曟嵁绫诲瀷
+	Warehouse           string              `json:"warehouse" form:"warehouse" gorm:"type:varchar(255);not null;default '';comment:鏀惰揣浠撳簱"`                      //鏀惰揣浠撳簱
+	Quantity            decimal.Decimal     `json:"quantity" form:"quantity" gorm:"type:decimal(12,4);not null;comment:閲囪喘鏁伴噺"`                                  // 閲囪喘鏁伴噺
+	TotalPrice          decimal.Decimal     `json:"totalPrice" form:"totalPrice" gorm:"type:decimal(12,2);not null;default '';comment:浠风◣鍚堣"`                   //浠风◣鍚堣
+	WholeDiscountType   WholeDiscountType   `json:"wholeDiscountType" form:"wholeDiscountType" gorm:"type:decimal(12,2);not null;default '';comment:鏁村崟鎶樻墸"`     //鏁村崟鎶樻墸绫诲瀷
+	WholeDiscount       decimal.Decimal     `json:"wholeDiscount" form:"wholeDiscount" gorm:"type:decimal(12,2);not null;default '';comment:鏁村崟鎶樻墸"`             //鏁村崟鎶樻墸鍊�
+	PriceAdjustmentType PriceAdjustmentType `json:"priceAdjustmentType" form:"priceAdjustmentType" gorm:"type:decimal(12,2);not null;default '';comment:浠锋牸璋冩暣"` //浠锋牸璋冩暣绫诲瀷
+	PriceAdjustment     decimal.Decimal     `json:"priceAdjustment" form:"priceAdjustment" gorm:"type:decimal(12,2);not null;default '';comment:浠锋牸璋冩暣"`         //浠锋牸璋冩暣鍊�
+	RealTotalPrice      decimal.Decimal     `json:"realTotalPrice" form:"realTotalPrice" gorm:"type:decimal(12,2);not null;default '';comment:鏈�缁堜环鏍�"`           //鏈�缁堜环鏍�
 }
 
 type OrderStatus int
@@ -35,3 +42,56 @@
 	OrderStatusStored    OrderStatus = 3 //宸插叆搴�
 	OrderStatusCompleted OrderStatus = 4 //宸插畬鎴�
 )
+
+type WholeDiscountType int
+
+const (
+	WholeDiscountTypePercent  WholeDiscountType = 1 //鐧惧垎姣旈檷浠�
+	WholeDiscountTypeDiscount WholeDiscountType = 2 //鐩存帴闄嶄环
+)
+
+func (wdt WholeDiscountType) IsValid(totalPrice, value decimal.Decimal) bool {
+	if wdt != WholeDiscountTypePercent && wdt != WholeDiscountTypeDiscount {
+		return false
+	}
+	if wdt == WholeDiscountTypeDiscount && value.GreaterThanOrEqual(totalPrice) {
+		return false
+	}
+	if wdt == WholeDiscountTypePercent && totalPrice.Mul(value).Div(decimal.NewFromInt(100)).GreaterThanOrEqual(totalPrice) {
+		return false
+	}
+	return true
+}
+
+type PriceAdjustmentType int
+
+const (
+	PriceAdjustmentTypeAdd PriceAdjustmentType = 1 //澧炲姞
+	PriceAdjustmentTypeSub PriceAdjustmentType = 2 //鍑忓皯
+)
+
+func (pat PriceAdjustmentType) IsValid(totalPrice, value decimal.Decimal) bool {
+	if pat != PriceAdjustmentTypeAdd && pat != PriceAdjustmentTypeSub {
+		return false
+	}
+	if pat == PriceAdjustmentTypeSub && value.GreaterThanOrEqual(totalPrice) {
+		return false
+	}
+	return true
+}
+
+func (slf Purchase) CalcRealTotalPrice() decimal.Decimal {
+	totalPrice := slf.TotalPrice
+	if slf.WholeDiscountType == WholeDiscountTypePercent {
+		totalPrice = totalPrice.Mul(slf.WholeDiscount).Div(decimal.NewFromInt(100))
+	} else if slf.WholeDiscountType == WholeDiscountTypeDiscount {
+		totalPrice = totalPrice.Sub(slf.WholeDiscount)
+	}
+
+	if slf.PriceAdjustmentType == PriceAdjustmentTypeAdd {
+		totalPrice = totalPrice.Add(slf.PriceAdjustment)
+	} else if slf.PriceAdjustmentType == PriceAdjustmentTypeSub {
+		totalPrice = totalPrice.Sub(slf.PriceAdjustment)
+	}
+	return totalPrice.RoundCeil(2)
+}

--
Gitblit v1.8.0