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 ReceiptService struct{} func NewReceiptService() ReceiptService { return ReceiptService{} } func (ReceiptService) AddReceipt(receipt *model.Receipt) int { var moneyAdder model.MoneyAdder var sourceId int var collection *model.ServiceCollectionPlan var err error switch receipt.SourceType { case constvar.ReceiptSourceTypeSalesDetails: moneyAdder = model.NewSalesDetailsSearch() sourceId = receipt.SourceId case constvar.ReceiptSourceTypeServiceContract: moneyAdder = model.NewServiceContractSearch() sourceId = receipt.SourceId case constvar.ReceiptSourceTypeCollectionPlan: collection, err = model.NewServiceCollectionPlanSearch().SetId(receipt.SourceId).First() if err != nil { return ecode.DBErr } sourceId = collection.SourceId switch collection.SourceType { case constvar.CollectionSourceTypeSaleDetails: moneyAdder = model.NewSalesDetailsSearch() case constvar.CollectionSourceTypeServiceContract: moneyAdder = model.NewServiceContractSearch() } } err = model.WithTransaction(func(db *gorm.DB) error { err := model.NewReceiptSearch().SetOrm(db).Create(receipt) if err != nil { return err } if receipt.SourceType == constvar.ReceiptSourceTypeCollectionPlan { err = model.NewServiceCollectionPlanSearch().SetOrm(db).UpdateStatusAndAmount(collection, receipt.Amount) if err != nil { return err } } // todo tmp if sourceId != 0 { err = moneyAdder.AmountReceivableAdd(db, sourceId, decimal.Zero.Sub(receipt.Amount).Round(2)) if err != nil { return err } err = moneyAdder.AmountReceivedAdd(db, sourceId, receipt.Amount.Round(2)) if err != nil { return err } } return nil }) if err != nil { return ecode.DBErr } return ecode.OK } func (ReceiptService) DeleteReceipt(id int) int { receipt, err := model.NewReceiptSearch().SetId(id).First() if err != nil { return ecode.DBErr } var moneyAdder model.MoneyAdder var sourceId int var collection *model.ServiceCollectionPlan switch receipt.SourceType { case constvar.ReceiptSourceTypeSalesDetails: moneyAdder = model.NewSalesDetailsSearch() sourceId = receipt.SourceId case constvar.ReceiptSourceTypeServiceContract: moneyAdder = model.NewServiceContractSearch() sourceId = receipt.SourceId case constvar.ReceiptSourceTypeCollectionPlan: collection, err = model.NewServiceCollectionPlanSearch().SetId(receipt.SourceId).First() if err != nil { return ecode.DBErr } sourceId = collection.SourceId switch collection.SourceType { case constvar.CollectionSourceTypeSaleDetails: moneyAdder = model.NewSalesDetailsSearch() case constvar.CollectionSourceTypeServiceContract: moneyAdder = model.NewServiceContractSearch() } } err = model.WithTransaction(func(db *gorm.DB) error { err := model.NewReceiptSearch().SetOrm(db).SetId(id).Delete() if err != nil { return err } if receipt.SourceType == constvar.ReceiptSourceTypeCollectionPlan { err = model.NewServiceCollectionPlanSearch().SetOrm(db).UpdateStatusAndAmount(collection, decimal.Zero.Sub(receipt.Amount)) if err != nil { return err } } err = moneyAdder.AmountReceivableAdd(db, sourceId, receipt.Amount.Round(2)) if err != nil { return err } err = moneyAdder.AmountReceivedAdd(db, sourceId, decimal.Zero.Sub(receipt.Amount).Round(2)) if err != nil { return err } return nil }) if err != nil { return ecode.DBErr } return ecode.OK } func (ReceiptService) GetReceiptList(params request.GetReceiptList) ([]*model.Receipt, int64, int) { list, total, err := model.NewReceiptSearch(). SetPage(params.Page, params.PageSize). SetSourceType(params.SourceType). SetSourceId(params.SourceId). SetClientId(params.ClientId). SetPrincipalIds(params.PrincipleIds). Find() if err != nil { return nil, 0, ecode.DBErr } return list, total, ecode.OK } func (ReceiptService) UpdateReceipts(Receipts []*request.UpdateReceipt) int { for _, v := range Receipts { // check Receipt exist _, err := model.NewReceiptSearch().SetId(v.Id).First() if err != nil { return ecode.DBErr } err = model.NewReceiptSearch().SetId(v.Id).Updates(map[string]interface{}{}) if err != nil { return ecode.DBErr } } return ecode.OK } func (ReceiptService) UpdateReceipt(receipt *model.Receipt) int { old, err := model.NewReceiptSearch().SetId(receipt.Id).First() if err != nil { return ecode.DBErr } var moneyAdder model.MoneyAdder var sourceId int var collection *model.ServiceCollectionPlan switch receipt.SourceType { case constvar.ReceiptSourceTypeSalesDetails: moneyAdder = model.NewSalesDetailsSearch() sourceId = receipt.SourceId case constvar.ReceiptSourceTypeServiceContract: moneyAdder = model.NewServiceContractSearch() sourceId = receipt.SourceId case constvar.ReceiptSourceTypeCollectionPlan: collection, err = model.NewServiceCollectionPlanSearch().SetId(receipt.SourceId).First() if err != nil { return ecode.DBErr } sourceId = collection.SourceId switch collection.SourceType { case constvar.CollectionSourceTypeSaleDetails: moneyAdder = model.NewSalesDetailsSearch() case constvar.CollectionSourceTypeServiceContract: moneyAdder = model.NewServiceContractSearch() } } err = model.WithTransaction(func(db *gorm.DB) error { err = model.NewReceiptSearch().SetId(receipt.Id).Update(receipt) if err != nil { return err } diffAmount := receipt.Amount.Sub(old.Amount) if receipt.SourceType == constvar.ReceiptSourceTypeCollectionPlan { err = model.NewServiceCollectionPlanSearch().SetOrm(db).UpdateStatusAndAmount(collection, diffAmount) if err != nil { return err } } err = moneyAdder.AmountReceivedAdd(db, sourceId, diffAmount.Round(2)) if err != nil { return err } err = moneyAdder.AmountReceivableAdd(db, sourceId, decimal.Zero.Sub(diffAmount).Round(2)) if err != nil { return err } return nil }) if err != nil { return ecode.DBErr } return ecode.OK }