| | |
| | | package service |
| | | |
| | | import ( |
| | | "aps_crm/constvar" |
| | | "aps_crm/model" |
| | | "aps_crm/model/request" |
| | | "aps_crm/pkg/ecode" |
| | | "github.com/shopspring/decimal" |
| | | "gorm.io/gorm" |
| | | ) |
| | | |
| | | 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) |
| | | serviceContract.AmountUnInvoiced = decimal.Zero.Round(2) |
| | | serviceContract.AmountTotal = decimal.Zero.Round(2) |
| | | for _, product := range serviceContract.Products { |
| | | serviceContract.AmountTotal = serviceContract.AmountTotal.Add(product.Amount.Mul(product.Price)) |
| | | } |
| | | serviceContract.AmountTotal = serviceContract.AmountTotal.Round(2) |
| | | serviceContract.AmountReceivable = serviceContract.AmountTotal |
| | | serviceContract.AmountUnInvoiced = serviceContract.AmountTotal |
| | | |
| | | err := model.NewServiceContractSearch().Create(serviceContract) |
| | | if err != nil { |
| | | return ecode.SContractExist |
| | |
| | | |
| | | func (SContractService) UpdateServiceContract(serviceContract *model.ServiceContract) int { |
| | | // check serviceContract exist |
| | | _, err := model.NewServiceContractSearch().SetId(serviceContract.Id).Find() |
| | | old, err := model.NewServiceContractSearch().SetId(serviceContract.Id).SetPreload(true).First() |
| | | if err != nil { |
| | | return ecode.SContractNotExist |
| | | } |
| | | var totalAmount decimal.Decimal |
| | | totalAmount = serviceContract.AmountTotal |
| | | newProducts, removedProducts := NewProductsService().PickDiffProducts(serviceContract.Products, old.Products) |
| | | for _, product := range serviceContract.Products { |
| | | totalAmount = totalAmount.Add(product.Amount.Mul(product.Price)) |
| | | } |
| | | serviceContract.AmountTotal = totalAmount.Round(2) |
| | | serviceContract.AmountReceivable = serviceContract.AmountTotal.Sub(serviceContract.AmountReceived) |
| | | serviceContract.AmountUnInvoiced = serviceContract.AmountTotal.Sub(serviceContract.AmountInvoiced) |
| | | err = model.WithTransaction(func(db *gorm.DB) error { |
| | | err := model.NewServiceContractSearch().SetId(serviceContract.Id).Update(serviceContract) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | if len(removedProducts) > 0 { |
| | | removedProductIds := make([]uint, 0, len(removedProducts)) |
| | | for _, product := range removedProducts { |
| | | removedProductIds = append(removedProductIds, product.Id) |
| | | } |
| | | err = model.NewProductSearch(db).SetIds(removedProductIds).Delete() |
| | | if err != nil { |
| | | return err |
| | | } |
| | | } |
| | | if len(newProducts) > 0 { |
| | | for _, p := range newProducts { |
| | | p.Id = 0 |
| | | } |
| | | err = model.NewProductSearch(db).CreateBatch(newProducts) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | var rel []*model.ServiceContractProduct |
| | | for _, p := range newProducts { |
| | | rel = append(rel, &model.ServiceContractProduct{ |
| | | ServiceContractId: serviceContract.Id, |
| | | ProductId: p.Id, |
| | | }) |
| | | } |
| | | err = model.NewServiceContractProductSearch().CreateBatch(rel) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | } |
| | | return nil |
| | | }) |
| | | |
| | | err = model.NewServiceContractSearch().SetId(serviceContract.Id).Update(serviceContract) |
| | | if err != nil { |
| | | return ecode.SContractSetErr |
| | | return ecode.DBErr |
| | | } |
| | | |
| | | return ecode.OK |
| | |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (SContractService) GetServiceContractList(page, pageSize int, queryClass constvar.ServiceContractQueryClass, keywordType constvar.ServiceContractKeywordType, keyword string) ([]*model.ServiceContract, int64, int) { |
| | | func (SContractService) BatchDeleteServiceContract(ids []int) int { |
| | | err := model.NewServiceContractSearch().DeleteByIds(ids) |
| | | if err != nil { |
| | | return ecode.DBErr |
| | | } |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (SContractService) GetServiceContractList(params request.GetServiceContractList, memberIds []int) ([]*model.ServiceContract, int64, int) { |
| | | // get contact list |
| | | contacts, total, err := model.NewServiceContractSearch(). |
| | | SetKeyword(keyword). |
| | | SetKeywordType(keywordType). |
| | | SetQueryClass(queryClass). |
| | | SetPage(page, pageSize).FindAll() |
| | | SetKeyword(params.Keyword). |
| | | SetKeywordType(params.KeywordType). |
| | | SetQueryClass(params.QueryClass). |
| | | SetPage(params.Page, params.PageSize). |
| | | SetSalesDetailsId(params.SalesDetailsId). |
| | | SetQuotationId(params.QuotationId). |
| | | SetSaleChanceId(params.SaleChanceId). |
| | | SetContactId(params.ContactId). |
| | | SetMemberIds(memberIds). |
| | | SetPreload(true). |
| | | Find() |
| | | if err != nil { |
| | | return nil, 0, ecode.SContractListErr |
| | | } |