fix
zhangqian
2023-08-18 ae1b07994f7785196d1d3ba1d53215bbded2dbc0
service/salesDetails.go
@@ -59,46 +59,49 @@
   }
   var totalAmount decimal.Decimal
   totalAmount = salesDetails.AmountTotal
   newProducts, removedProducts := NewProductsService().PickDiffProducts(salesDetails.Products, old.Products)
   for _, product := range newProducts {
   for _, product := range salesDetails.Products {
      totalAmount = totalAmount.Add(product.Amount.Mul(product.Price))
   }
   removedProductIds := make([]uint, 0, len(removedProducts))
   for _, product := range removedProducts {
      totalAmount = totalAmount.Sub(product.Amount.Mul(product.Price))
      removedProductIds = append(removedProductIds, product.Id)
   }
   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(removedProductIds) > 0 {
      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
         //}
         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