From 6285cf3f366b0da28ba7bddbb3bb4c7ec4585d25 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期五, 11 八月 2023 16:48:05 +0800
Subject: [PATCH] 新增、删除、修改改收款单时若收款源单是收款计划,更新收款单状态和应收、已收金额

---
 model/serviceCollectionPlan.go |   61 ++++++++++++++++++++++++------
 1 files changed, 48 insertions(+), 13 deletions(-)

diff --git a/model/serviceCollectionPlan.go b/model/serviceCollectionPlan.go
index cb15770..cfe065c 100644
--- a/model/serviceCollectionPlan.go
+++ b/model/serviceCollectionPlan.go
@@ -13,19 +13,22 @@
 type (
 	// ServiceCollectionPlan 鏀舵璁″垝
 	ServiceCollectionPlan struct {
-		Id             int                       `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
-		CollectionType int                       `gorm:"collection_type" json:"collectionType"` // 绫诲瀷锛�1 璁″垝鏀舵鏃ユ湡 2 椤圭洰鐘舵�侊級
-		SourceType     int                       `gorm:"source_type" json:"sourceType"`         // 婧愬崟绫诲瀷锛�1閿�鍞槑缁�2鏈嶅姟鍚堝悓3閿�鍞彂绁級
-		SourceId       int                       `gorm:"source_id" json:"sourceId"`             // 婧愬崟id
-		PrincipalId    int                       `gorm:"principal_id" json:"principalId"`       // 鏀舵璐熻矗浜篒D
-		Term           int                       `gorm:"term" json:"term"`                      // 鏈熸
-		Percent        decimal.Decimal           `gorm:"percent" json:"percent"`                // 姣斾緥
-		Amount         decimal.Decimal           `gorm:"amount" json:"amount"`                  // 閲戦
-		MoneyType      string                    `gorm:"money_type" json:"moneyType"`           // 甯佺
-		CollectionDate time.Time                 `gorm:"collection_date" json:"collectionDate"` // 璁″垝鏀舵鏃ユ湡
-		Remark         string                    `gorm:"remark" json:"remark"`                  // 澶囨敞
-		Status         constvar.CollectionStatus `gorm:"status" json:"status"`                  // 鐘舵�侊紙1鏈敹2宸叉敹锛�
-		FileId         int                       `gorm:"file_id" json:"fileId"`                 // 闄勪欢id
+		Id               int                           `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
+		CollectionType   int                           `gorm:"collection_type" json:"collectionType"`                                            // 绫诲瀷锛�1 璁″垝鏀舵鏃ユ湡 2 椤圭洰鐘舵�侊級
+		SourceType       constvar.CollectionSourceType `gorm:"source_type" json:"sourceType"`                                                    // 婧愬崟绫诲瀷锛�1閿�鍞槑缁�2鏈嶅姟鍚堝悓3閿�鍞彂绁級
+		SourceId         int                           `gorm:"source_id" json:"sourceId"`                                                        // 婧愬崟id
+		PrincipalId      int                           `gorm:"principal_id" json:"principalId"`                                                  // 鏀舵璐熻矗浜篒D
+		Term             int                           `gorm:"term" json:"term"`                                                                 // 鏈熸
+		Percent          decimal.Decimal               `gorm:"percent" json:"percent"`                                                           // 姣斾緥
+		Amount           decimal.Decimal               `gorm:"amount" json:"amount"`                                                             // 閲戦
+		MoneyType        string                        `gorm:"money_type" json:"moneyType"`                                                      // 甯佺
+		CollectionDate   time.Time                     `gorm:"collection_date" json:"collectionDate"`                                            // 璁″垝鏀舵鏃ユ湡
+		Remark           string                        `gorm:"remark" json:"remark"`                                                             // 澶囨敞
+		Status           constvar.CollectionStatus     `gorm:"status" json:"status"`                                                             // 鐘舵�侊紙1鏈敹2閮ㄥ垎宸叉敹3宸叉敹锛�
+		AmountReceivable decimal.Decimal               `gorm:"column:amount_receivable;type:decimal(12,2);comment:搴旀敹閲戦" json:"amountReceivable"` // 搴旀敹閲戦
+		AmountReceived   decimal.Decimal               `gorm:"column:amount_received;type:decimal(12,2);comment:宸叉敹閲戦" json:"amountReceived"`     // 宸叉敹閲戦
+		AmountTotal      decimal.Decimal               `gorm:"column:amount_total;type:decimal(12,2);comment:鎬婚" json:"amountTotal"`             // 鎬婚
+		FileId           int                           `gorm:"file_id" json:"fileId"`                                                            // 闄勪欢id
 	}
 
 	// ServiceCollectionPlanSearch 鏀舵璁″垝鎼滅储鏉′欢
@@ -145,6 +148,38 @@
 	return records, total, err
 }
 
+func (slf *ServiceCollectionPlanSearch) UpdateByMap(upMap map[string]interface{}) error {
+	var (
+		db = slf.build()
+	)
+
+	if err := db.Updates(upMap).Error; err != nil {
+		return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap)
+	}
+
+	return nil
+}
+
+func (slf *ServiceCollectionPlanSearch) UpdateStatusAndAmount(collection *ServiceCollectionPlan, receiveAmount decimal.Decimal) error {
+	receivedAmount := collection.AmountReceived.Add(receiveAmount)
+	var status constvar.CollectionStatus
+	if receiveAmount.GreaterThanOrEqual(collection.AmountTotal) {
+		status = constvar.CollectionStatusCollected
+	} else {
+		status = constvar.CollectionStatusSubCollected
+	}
+	receivableAmount := collection.AmountTotal.Add(receivedAmount)
+	err := slf.SetId(collection.Id).UpdateByMap(map[string]interface{}{
+		"status":            status,
+		"amount_received":   receivedAmount,
+		"amount_receivable": receivableAmount})
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
 // InitDefaultData 鍒濆鍖栨暟鎹�
 func (slf *ServiceCollectionPlanSearch) InitDefaultData() error {
 	var (

--
Gitblit v1.8.0