From c5a0eb549cba2cd358a2d0496c44f3a289f15d9c Mon Sep 17 00:00:00 2001
From: wangpengfei <274878379@qq.com>
Date: 星期一, 28 八月 2023 14:22:06 +0800
Subject: [PATCH] fix

---
 model/serviceContract.go |   66 +++++++++++++++++++++++++++++----
 1 files changed, 58 insertions(+), 8 deletions(-)

diff --git a/model/serviceContract.go b/model/serviceContract.go
index 0b64a4f..d4b8744 100644
--- a/model/serviceContract.go
+++ b/model/serviceContract.go
@@ -17,7 +17,9 @@
 		Client                  Client                `json:"client" gorm:"foreignKey:ClientId"`
 		Number                  string                `json:"number" gorm:"column:number;type:varchar(255);comment:鍚堝悓缂栧彿"`
 		MemberId                int                   `json:"memberId" gorm:"column:member_id;type:int;comment:璐熻矗浜篿d"`
+		Member                  User                  `json:"member" gorm:"foreignKey:MemberId"`
 		ContactId               int                   `json:"contactId" gorm:"column:contact_id;type:int;comment:鑱旂郴浜篿d"`
+		Contact                 Contact               `json:"contact" gorm:"foreignKey:ContactId"`
 		SaleChanceId            int                   `json:"saleChanceId" gorm:"column:sale_chance_id;type:int;comment:閿�鍞満浼歩d"`
 		SaleChance              SaleChance            `json:"SaleChance" gorm:"foreignKey:SaleChanceId"`
 		SalesDetailsId          int                   `json:"salesDetailsId" gorm:"column:sales_details_id;type:int;comment:鍚堝悓璁㈠崟id"`
@@ -34,12 +36,13 @@
 		ServiceTimes            int                   `json:"serviceTimes" gorm:"column:service_times;type:int;comment:鏈嶅姟娆℃暟"`
 		Terms                   string                `json:"terms" gorm:"column:terms;type:text;comment:鏉℃"`
 		Remark                  string                `json:"remark" gorm:"column:remark;type:text;comment:澶囨敞"`
-		AmountReceivable        decimal.Decimal       `gorm:"amount_receivable" json:"amountReceivable"` // 搴旀敹閲戦
-		AmountReceived          decimal.Decimal       `gorm:"amount_received" json:"amountReceived"`     // 宸叉敹閲戦
-		AmountInvoiced          decimal.Decimal       `gorm:"amount_invoiced" json:"amountInvoiced"`     // 宸插紑绁ㄩ噾棰�
-		AmountUnInvoiced        decimal.Decimal       `gorm:"-" json:"amountUnInvoiced"`                 // 鏈紑绁ㄩ噾棰�
+		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"`        // 宸叉敹閲戦
+		AmountInvoiced          decimal.Decimal       `gorm:"column:amount_invoiced;type:decimal(12,2);comment:宸插紑绁ㄩ噾棰�" json:"amountInvoiced"`       // 宸插紑绁ㄩ噾棰�
+		AmountUnInvoiced        decimal.Decimal       `gorm:"column:amount_not_invoiced;type:decimal(12,2);comment:鏈紑绁ㄩ噾棰�" json:"amountUnInvoiced"` // 鏈紑绁ㄩ噾棰�
+		AmountTotal             decimal.Decimal       `gorm:"column:amount_total;type:decimal(12,2);comment:浠风◣鍚堣" json:"amountTotal"`              // 浠风◣鍚堣
 		Products                []*Product            `json:"products" gorm:"many2many:service_contract_product;"`
-		gorm.Model              `json:"-"`
+		CrmModel
 	}
 
 	ServiceContractSearch struct {
@@ -55,7 +58,7 @@
 	}
 )
 
-func (ServiceContract) TableName() string {
+func (slf *ServiceContract) TableName() string {
 	return "service_contract"
 }
 
@@ -107,6 +110,9 @@
 	}
 	if slf.Preload {
 		db = db.
+			Preload("Client").
+			Preload("Member").
+			Preload("Contact").
 			Preload("SaleChance").
 			Preload("SalesDetails").
 			Preload("Quotation").
@@ -115,7 +121,8 @@
 			Preload("Products").
 			Preload("Client").
 			Preload("ServiceContractType").
-			Preload("ServiceContractStatus")
+			Preload("ServiceContractStatus").
+			Preload("Contact")
 	}
 
 	return db
@@ -136,6 +143,12 @@
 	return db.Delete(&ServiceContract{}).Error
 }
 
+func (slf *ServiceContractSearch) DeleteByIds(ids []int) error {
+	var db = slf.build()
+	db = db.Where("id in ?", ids)
+	return db.Delete(&ServiceContract{}).Error
+}
+
 func (slf *ServiceContractSearch) Find() ([]*ServiceContract, int64, error) {
 	var db = slf.build()
 	var records = make([]*ServiceContract, 0)
@@ -147,7 +160,7 @@
 		db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
 	}
 
-	err := db.Find(&records).Error
+	err := db.Order("id desc").Order("id desc").Find(&records).Error
 	return records, total, err
 }
 
@@ -214,3 +227,40 @@
 
 	return record, nil
 }
+
+func (slf *ServiceContractSearch) AmountReceivableAdd(tx *gorm.DB, id int, amount decimal.Decimal) error {
+	slf.Orm = tx
+	record, err := slf.SetId(id).First()
+	if err != nil {
+		return err
+	}
+	amount = record.AmountReceivable.Add(amount)
+	return slf.UpdateByMap(map[string]interface{}{"amount_receivable": amount})
+}
+func (slf *ServiceContractSearch) AmountReceivedAdd(tx *gorm.DB, id int, amount decimal.Decimal) error {
+	slf.Orm = tx
+	record, err := slf.SetId(id).First()
+	if err != nil {
+		return err
+	}
+	amount = record.AmountReceived.Add(amount)
+	return slf.UpdateByMap(map[string]interface{}{"amount_received": amount})
+}
+func (slf *ServiceContractSearch) AmountInvoicedAdd(tx *gorm.DB, id int, amount decimal.Decimal) error {
+	slf.Orm = tx
+	record, err := slf.SetId(id).First()
+	if err != nil {
+		return err
+	}
+	amount = record.AmountInvoiced.Add(amount)
+	return slf.UpdateByMap(map[string]interface{}{"amount_invoiced": amount})
+}
+func (slf *ServiceContractSearch) AmountNotInvoicedAdd(tx *gorm.DB, id int, amount decimal.Decimal) error {
+	slf.Orm = tx
+	record, err := slf.SetId(id).First()
+	if err != nil {
+		return err
+	}
+	amount = record.AmountUnInvoiced.Add(amount)
+	return slf.UpdateByMap(map[string]interface{}{"amount_not_invoiced": amount})
+}

--
Gitblit v1.8.0