package service import ( "aps_crm/model" "aps_crm/model/request" "aps_crm/pkg/ecode" "github.com/shopspring/decimal" "gorm.io/gorm" ) type SalesDetailsService struct{} func (SalesDetailsService) AddSalesDetails(salesDetails *model.SalesDetails) int { salesDetails.AmountReceivable = decimal.Zero.Round(2) salesDetails.AmountInvoiced = decimal.Zero.Round(2) salesDetails.AmountReceived = decimal.Zero.Round(2) salesDetails.AmountUnInvoiced = decimal.Zero.Round(2) salesDetails.AmountTotal = decimal.Zero.Round(2) for _, product := range salesDetails.Products { salesDetails.AmountTotal = salesDetails.AmountTotal.Add(product.Amount.Mul(product.Price)) } salesDetails.AmountTotal = salesDetails.AmountTotal.Round(2) salesDetails.AmountReceivable = salesDetails.AmountTotal salesDetails.AmountUnInvoiced = salesDetails.AmountTotal for _, product := range salesDetails.Products { product.Id = 0 } err := model.NewSalesDetailsSearch().Create(salesDetails) if err != nil { return ecode.SalesDetailsExist } return ecode.OK } func (SalesDetailsService) DeleteSalesDetails(id int) int { _, err := model.NewSalesDetailsSearch().SetId(id).First() if err != nil { return ecode.SalesDetailsNotExist } err = model.NewSalesDetailsSearch().SetId(id).Delete() if err != nil { return ecode.SalesDetailsNotExist } return ecode.OK } func (SalesDetailsService) BatchDeleteSalesDetails(ids []int) int { err := model.NewSalesDetailsSearch().DeleteByIds(ids) if err != nil { return ecode.DBErr } return ecode.OK } func (SalesDetailsService) UpdateSalesDetails(salesDetails *model.SalesDetails) int { // check salesDetails exist old, err := model.NewSalesDetailsSearch().SetId(salesDetails.Id).SetPreload(true).First() if err != nil { return ecode.SalesDetailsNotExist } var totalAmount decimal.Decimal totalAmount = salesDetails.AmountTotal for _, product := range salesDetails.Products { totalAmount = totalAmount.Add(product.Amount.Mul(product.Price)) } salesDetails.AmountTotal = totalAmount.Round(2) salesDetails.AmountReceivable = salesDetails.AmountTotal.Sub(salesDetails.AmountReceived) salesDetails.AmountUnInvoiced = salesDetails.AmountTotal.Sub(salesDetails.AmountInvoiced) newProducts, removedProducts := NewProductsService().PickDiffProducts(salesDetails.Products, old.Products) err = model.WithTransaction(func(db *gorm.DB) error { err = model.NewSalesDetailsSearch().SetId(salesDetails.Id).Update(salesDetails) if err != nil { return err } if len(removedProducts) > 0 { removedProductIds := make([]uint, 0, len(removedProducts)) for _, product := range removedProducts { totalAmount = totalAmount.Sub(product.Amount.Mul(product.Price)) 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.SalesDetailsProduct for _, p := range newProducts { rel = append(rel, &model.SalesDetailsProduct{ SalesDetailsId: salesDetails.Id, ProductId: p.Id, }) } err = model.NewSalesDetailsProductSearch().CreateBatch(rel) if err != nil { return err } } return nil }) if err != nil { return ecode.SalesDetailsSetErr } return ecode.OK } func (SalesDetailsService) GetSalesDetailsList(params request.GetSalesDetailsList, memberIds []int) ([]*model.SalesDetails, int64, int) { // get contact list contacts, total, err := model.NewSalesDetailsSearch(). SetPreload(true). SetMemberIds(memberIds). SetKeywordType(params.KeywordType). SetSaleChanceId(params.SaleChanceId). SetClientId(params.ClientId). SetNumber(params.Number). SetKeyword(params.Keyword).SetPage(params.Page, params.PageSize).FindAll() if err != nil { return nil, 0, ecode.SalesDetailsListErr } return contacts, total, ecode.OK }