zhangqian
2023-11-01 530fed8ec225453572d57b15c200ab062c335457
service/receipt.go
@@ -17,25 +17,54 @@
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.WithTransaction(func(db *gorm.DB) error {
      err := model.NewReceiptSearch().SetOrm(db).Create(receipt)
      if err != nil {
         return err
      }
      err = moneyAdder.AmountReceivableAdd(db, receipt.SourceId, decimal.Zero.Sub(receipt.Amount).Round(2))
      if err != nil {
         return err
      if receipt.SourceType == constvar.ReceiptSourceTypeCollectionPlan {
         err = model.NewServiceCollectionPlanSearch().SetOrm(db).UpdateStatusAndAmount(collection, receipt.Amount)
         if err != nil {
            return err
         }
      }
      err = moneyAdder.AmountReceivedAdd(db, receipt.SourceId, receipt.Amount.Round(2))
      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 {
@@ -50,23 +79,46 @@
      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(id).Delete()
      err := model.NewReceiptSearch().SetOrm(db).SetId(id).Delete()
      if err != nil {
         return err
      }
      err = moneyAdder.AmountReceivableAdd(db, receipt.SourceId, receipt.Amount.Round(2))
      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, receipt.SourceId, decimal.Zero.Sub(receipt.Amount).Round(2))
      err = moneyAdder.AmountReceivedAdd(db, sourceId, decimal.Zero.Sub(receipt.Amount).Round(2))
      if err != nil {
         return err
      }
@@ -78,8 +130,14 @@
   return ecode.OK
}
func (ReceiptService) GetReceiptList() ([]*model.Receipt, int64, int) {
   list, total, err := model.NewReceiptSearch().Find()
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
   }
@@ -110,23 +168,45 @@
      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).Save(receipt)
      err = model.NewReceiptSearch().SetId(receipt.Id).Update(receipt)
      if err != nil {
         return err
      }
      diffAmount := receipt.Amount.Sub(old.Amount)
      err = moneyAdder.AmountReceivedAdd(db, receipt.SourceId, diffAmount.Round(2))
      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, receipt.SourceId, decimal.Zero.Sub(diffAmount).Round(2))
      err = moneyAdder.AmountReceivableAdd(db, sourceId, decimal.Zero.Sub(diffAmount).Round(2))
      if err != nil {
         return err
      }