From 886b1ae4bdaac1fc9a2eb98b91dc333f14f70c50 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期三, 09 八月 2023 17:41:42 +0800 Subject: [PATCH] 新增和编辑服务合同时更新应收款金额,新增服务合同发票时更新已开票金额 --- model/product.go | 19 +++-- model/serviceContract.go | 9 +- pkg/ecode/code.go | 15 +++-- model/invoice.go | 1 service/invoice.go | 51 +++++++++------- service/serviceContract.go | 24 +++++++ model/request/invoice.go | 2 pkg/ecode/msg.go | 3 + 8 files changed, 82 insertions(+), 42 deletions(-) diff --git a/model/invoice.go b/model/invoice.go index ef97ed8..8db5f13 100644 --- a/model/invoice.go +++ b/model/invoice.go @@ -28,6 +28,7 @@ CourierNumber string `gorm:"courier_number" json:"courierNumber"` // 鐗╂祦鍗曞彿 CourierCompanyId int `gorm:"courier_company_id" json:"courierCompanyId"` // 鐗╂祦鍏徃 CourierCompany CourierCompany `gorm:"foreignKey:CourierCompanyId"` + Products []Product `json:"products" gorm:"many2many:invoice_product;"` } // InvoiceSearch 閿�鍞彂绁ㄦ悳绱㈡潯浠� diff --git a/model/product.go b/model/product.go index 029857b..20f0fc2 100644 --- a/model/product.go +++ b/model/product.go @@ -1,15 +1,18 @@ package model -import "gorm.io/gorm" +import ( + "github.com/shopspring/decimal" + "gorm.io/gorm" +) type Product struct { - Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` - Name string `json:"name" gorm:"column:name;type:varchar(255);comment:浜у搧鍚嶇О"` - Price float64 `json:"price" gorm:"column:price;type:decimal(10,2);comment:浜у搧浠锋牸"` - Number string `json:"number" gorm:"column:number;type:varchar(255);comment:浜у搧缂栧彿"` - Amount int `json:"amount" gorm:"column:amount;type:int;comment:浜у搧鏁伴噺"` - Total float64 `json:"total" gorm:"column:total;type:decimal(10,2);comment:浜у搧鎬讳环"` - Desc string `json:"desc" gorm:"column:desc;type:varchar(255);comment:浜у搧鎻忚堪"` + Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` + Name string `json:"name" gorm:"column:name;type:varchar(255);comment:浜у搧鍚嶇О"` + Price decimal.Decimal `json:"price" gorm:"column:price;type:decimal(10,2);comment:浜у搧浠锋牸"` + Number string `json:"number" gorm:"column:number;type:varchar(255);comment:浜у搧缂栧彿"` + Amount decimal.Decimal `json:"amount" gorm:"column:amount;type:int;comment:浜у搧鏁伴噺"` + Total decimal.Decimal `json:"total" gorm:"column:total;type:decimal(10,2);comment:浜у搧鎬讳环"` + Desc string `json:"desc" gorm:"column:desc;type:varchar(255);comment:浜у搧鎻忚堪"` gorm.Model `json:"-"` } diff --git a/model/request/invoice.go b/model/request/invoice.go index 1b1eddd..5f9128f 100644 --- a/model/request/invoice.go +++ b/model/request/invoice.go @@ -2,6 +2,7 @@ import ( "aps_crm/constvar" + "aps_crm/model" ) type AddInvoice struct { @@ -17,6 +18,7 @@ InvoiceDate string `gorm:"invoice_date" json:"invoiceDate"` // 寮�绁ㄦ棩鏈� CourierNumber string `gorm:"courier_number" json:"courierNumber"` // 鐗╂祦鍗曞彿 CourierCompanyId int `gorm:"courier_company_id" json:"courierCompanyId"` // 鐗╂祦鍏徃 + Products []model.Product `json:"products"` //鍙戠エ瀵瑰簲浜у搧锛屼粠鐩稿簲婧愬崟閲岃幏鍙� } type UpdateInvoice struct { diff --git a/model/serviceContract.go b/model/serviceContract.go index 22dac27..4345d2c 100644 --- a/model/serviceContract.go +++ b/model/serviceContract.go @@ -35,6 +35,7 @@ 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"` // 鏈紑绁ㄩ噾棰� Products []Product `json:"products" gorm:"many2many:service_contract_product;"` gorm.Model `json:"-"` } @@ -70,13 +71,13 @@ } switch slf.QueryClass { case constvar.ServiceContractQueryClassExpireAfter30Day: - db = db.Where("end_time > ?", time.Now(), time.Now().AddDate(0, 0, 30)) + db = db.Where("end_time > ?", time.Now().AddDate(0, 0, 30).Format("2006-01-02")) case constvar.ServiceContractQueryClassExpireAfter60Day: - db = db.Where("end_time > ?", time.Now(), time.Now().AddDate(0, 0, 60)) + db = db.Where("end_time > ?", time.Now().AddDate(0, 0, 60).Format("2006-01-02")) case constvar.ServiceContractQueryClassExpiredBefore15Day: - db = db.Where("end_time < ?", time.Now().AddDate(0, 0, -15)) + db = db.Where("end_time < ?", time.Now().AddDate(0, 0, -15).Format("2006-01-02")) case constvar.ServiceContractQueryClassExpiredBefore60Day: - db = db.Where("end_time < ?", time.Now().AddDate(0, 0, -60)) + db = db.Where("end_time < ?", time.Now().AddDate(0, 0, -60).Format("2006-01-02")) } switch slf.KeywordType { diff --git a/pkg/ecode/code.go b/pkg/ecode/code.go index a574c94..6a3d1a2 100644 --- a/pkg/ecode/code.go +++ b/pkg/ecode/code.go @@ -235,12 +235,15 @@ PlanUpdateErr = 3200005 // 鏇存柊璁″垝澶辫触 PlanDeleteErr = 3200006 // 鍒犻櫎璁″垝澶辫触 - SContractExist = 3300001 // 鏈嶅姟鍚堝悓宸插瓨鍦� - SContractNotExist = 3300002 // 鏈嶅姟鍚堝悓涓嶅瓨鍦� - SContractListErr = 3300003 // 鑾峰彇鏈嶅姟鍚堝悓鍒楄〃澶辫触 - SContractSetErr = 3300004 // 璁剧疆鏈嶅姟鍚堝悓澶辫触 - SContractUpdateErr = 3300005 // 鏇存柊鏈嶅姟鍚堝悓澶辫触 - SContractDeleteErr = 3300006 // 鍒犻櫎鏈嶅姟鍚堝悓澶辫触 + SContractExist = 3300001 // 鏈嶅姟鍚堝悓宸插瓨鍦� + SContractNotExist = 3300002 // 鏈嶅姟鍚堝悓涓嶅瓨鍦� + SContractListErr = 3300003 // 鑾峰彇鏈嶅姟鍚堝悓鍒楄〃澶辫触 + SContractSetErr = 3300004 // 璁剧疆鏈嶅姟鍚堝悓澶辫触 + SContractUpdateErr = 3300005 // 鏇存柊鏈嶅姟鍚堝悓澶辫触 + SContractDeleteErr = 3300006 // 鍒犻櫎鏈嶅姟鍚堝悓澶辫触 + SContractProductPriceLowerThanInvoiceAmountErr = 3300007 //浜у搧鎬讳环浣庝簬宸插紑绁ㄩ噾棰� + SContractProductPriceLowerThanReceivedAmountErr = 3300008 //浜у搧鎬讳环浣庝簬宸叉敹閲戦 + SContractInvoiceProductPriceGreaterThanReceivableAmountErr = 3300009 //寮�绁ㄦ�婚楂樹簬搴旀敹閲戦 OrderManageExist = 3400001 // 璁㈠崟绠$悊宸插瓨鍦� OrderManageNotExist = 3400002 // 璁㈠崟绠$悊涓嶅瓨鍦� diff --git a/pkg/ecode/msg.go b/pkg/ecode/msg.go index 5b43237..deeff57 100644 --- a/pkg/ecode/msg.go +++ b/pkg/ecode/msg.go @@ -20,6 +20,9 @@ ChildrenExistErr: "瀛樺湪瀛愯彍鍗�", MenuNotExist: "鑿滃崟涓嶅瓨鍦�", MenuNameExistErr: "鑿滃崟鍚嶅凡瀛樺湪", + SContractProductPriceLowerThanInvoiceAmountErr: "浜у搧鎬讳环浣庝簬宸插紑绁ㄩ噾棰�", + SContractProductPriceLowerThanReceivedAmountErr: "浜у搧鎬讳环浣庝簬宸叉敹閲戦", + SContractInvoiceProductPriceGreaterThanReceivableAmountErr: "寮�绁ㄦ�婚楂樹簬搴旀敹閲戦", } func GetMsg(errCode int) (errMsg string) { diff --git a/service/invoice.go b/service/invoice.go index 9bd1b91..c70450d 100644 --- a/service/invoice.go +++ b/service/invoice.go @@ -1,9 +1,11 @@ package service import ( + "aps_crm/constvar" "aps_crm/model" "aps_crm/model/request" "aps_crm/pkg/ecode" + "github.com/shopspring/decimal" "gorm.io/gorm" ) @@ -15,30 +17,35 @@ func (InvoiceService) AddInvoice(invoice *model.Invoice) int { - err := model.WithTransaction(func(db *gorm.DB) error { - err := model.NewInvoiceSearch().Create(invoice) + if invoice.SourceType == constvar.InvoiceSourceTypeServiceContract { + serviceContract, err := model.NewServiceContractSearch().SetId(invoice.SourceId).First() if err != nil { - return err + return ecode.DBErr } - //if invoice.SourceType == constvar.InvoiceSourceTypeServiceContract { - // contract,err := model.NewServiceContractSearch().SetId(invoice.SourceId).First() - // if err != nil { - // return err - // } - // AmountInvoiced := contract.AmountReceived.Add() - // err := model.NewServiceContractSearch().SetId(invoice.SourceId).UpdateByMap(map[string]interface{}{ - // "amount_received" : - // }) - // if err != nil { - // return err - // } - //} - - return nil - }) - - if err != nil { - return ecode.DBErr + var amountInvoiced decimal.Decimal + for _, product := range invoice.Products { + amountInvoiced = serviceContract.AmountInvoiced.Add(product.Amount.Mul(product.Price)) + } + amountInvoiced = amountInvoiced.Round(2) + if amountInvoiced.GreaterThan(serviceContract.AmountReceivable) { + return ecode.SContractInvoiceProductPriceGreaterThanReceivableAmountErr + } + err = model.WithTransaction(func(db *gorm.DB) error { + err = model.NewInvoiceSearch().Create(invoice) + if err != nil { + return err + } + err = model.NewServiceContractSearch().SetId(invoice.SourceId).UpdateByMap(map[string]interface{}{ + "amount_invoiced": amountInvoiced, + }) + if err != nil { + return err + } + return nil + }) + if err != nil { + return ecode.DBErr + } } return ecode.OK diff --git a/service/serviceContract.go b/service/serviceContract.go index 664382f..eb901f2 100644 --- a/service/serviceContract.go +++ b/service/serviceContract.go @@ -4,11 +4,19 @@ "aps_crm/constvar" "aps_crm/model" "aps_crm/pkg/ecode" + "github.com/shopspring/decimal" ) type SContractService struct{} func (SContractService) AddServiceContract(serviceContract *model.ServiceContract) int { + serviceContract.AmountReceivable = decimal.Zero.Round(2) + serviceContract.AmountInvoiced = decimal.Zero.Round(2) + serviceContract.AmountReceived = decimal.Zero.Round(2) + for _, product := range serviceContract.Products { + serviceContract.AmountReceivable = serviceContract.AmountReceivable.Add(product.Amount.Mul(product.Price)) + } + serviceContract.AmountReceivable = serviceContract.AmountReceivable.Round(2) err := model.NewServiceContractSearch().Create(serviceContract) if err != nil { return ecode.SContractExist @@ -19,11 +27,23 @@ func (SContractService) UpdateServiceContract(serviceContract *model.ServiceContract) int { // check serviceContract exist - _, err := model.NewServiceContractSearch().SetId(serviceContract.Id).First() + old, err := model.NewServiceContractSearch().SetId(serviceContract.Id).First() if err != nil { return ecode.SContractNotExist } - + var amountReceivable decimal.Decimal + for _, product := range serviceContract.Products { + amountReceivable = serviceContract.AmountReceivable.Add(product.Amount.Mul(product.Price)) + } + if amountReceivable.LessThan(serviceContract.AmountInvoiced) { + return ecode.SContractProductPriceLowerThanInvoiceAmountErr + } + if amountReceivable.LessThan(serviceContract.AmountReceived) { + return ecode.SContractProductPriceLowerThanReceivedAmountErr + } + serviceContract.AmountInvoiced = old.AmountReceived + serviceContract.AmountReceived = old.AmountReceived + serviceContract.AmountReceivable = amountReceivable.Round(2) err = model.NewServiceContractSearch().SetId(serviceContract.Id).Update(serviceContract) if err != nil { return ecode.SContractSetErr -- Gitblit v1.8.0