fix
wangpengfei
2023-08-28 c5a0eb549cba2cd358a2d0496c44f3a289f15d9c
model/salesDetails.go
@@ -20,6 +20,7 @@
      SaleType            int               `json:"saleType" gorm:"column:sale_type;type:int;comment:销售类型"`
      SignTime            string            `json:"signTime" gorm:"column:sign_time;type:varchar(255);comment:签单时间"`
      MemberId            int               `json:"memberId" gorm:"column:member_id;type:int;comment:负责人id"`
      Member              User              `json:"Member"  gorm:"foreignKey:MemberId"`
      DeliveryDate        string            `json:"deliveryDate" gorm:"column:delivery_date;type:varchar(255);comment:交货日期"`
      WechatOrderStatusId int               `json:"wechatOrderStatusId" gorm:"column:wechat_order_status;type:int;comment:微信订单状态"`
      WechatOrderStatus   WechatOrderStatus `json:"wechatOrderStatus" gorm:"foreignKey:WechatOrderStatusId"`
@@ -27,16 +28,18 @@
      Phone               string            `json:"phone" gorm:"column:phone;type:varchar(255);comment:电话"`
      Addressee           string            `json:"addressee" gorm:"column:addressee;type:varchar(255);comment:收件人"`
      Conditions          string            `json:"conditions" gorm:"column:conditions;type:text;comment:条件"`
      CreatorId           int               `json:"creatorId" gorm:"column:creator_id;type:int;comment:创建人id"`
      Remark              string            `json:"remark" gorm:"column:remark;type:text;comment:备注"`
      Products            []Product         `json:"products" gorm:"many2many:sales_details_product;"`
      Products            []*Product        `json:"products" gorm:"many2many:SalesDetails_Product;"`
      LogisticCompany     string            `json:"logisticCompany" gorm:"column:logistic_company;type:varchar(255);comment:物流公司"`
      LogisticNumber      string            `json:"logisticNumber" gorm:"column:logistic_number;type:varchar(255);comment:物流单号"`
      LogisticCost        float64           `json:"logisticCost" gorm:"column:logistic_cost;type:decimal(10,2);comment:物流费用"`
      AmountReceivable    decimal.Decimal   `gorm:"amount_receivable" json:"amountReceivable"` // 应收金额
      AmountReceived      decimal.Decimal   `gorm:"amount_received" json:"amountReceived"`     // 已收金额
      AmountInvoiced      decimal.Decimal   `gorm:"amount_invoiced" json:"amountInvoiced"`     // 已开票金额
      AmountUnInvoiced    decimal.Decimal   `gorm:"-" json:"amountUnInvoiced"`                 // 未开票金额
      gorm.Model          `json:"-"`
      AmountReceivable    decimal.Decimal   `gorm:"column:amount_receivable;type:decimal(12,2);comment:应收金额" json:"amountReceivable"`    // 应收金额
      AmountReceived      decimal.Decimal   `gorm:"column:amount_received;type:decimal(12,2);comment:已收金额" json:"amountReceived"`        // 已收金额
      AmountInvoiced      decimal.Decimal   `gorm:"column:amount_invoiced;type:decimal(12,2);comment:已开票金额" json:"amountInvoiced"`       // 已开票金额
      AmountUnInvoiced    decimal.Decimal   `gorm:"column:amount_not_invoiced;type:decimal(12,2);comment:未开票金额" json:"amountUnInvoiced"` // 未开票金额
      AmountTotal         decimal.Decimal   `gorm:"column:amount_total;type:decimal(12,2);comment:价税合计" json:"amountTotal"`              // 价税合计
      CrmModel
   }
   SalesDetailsSearch struct {
@@ -77,12 +80,12 @@
   case constvar.SalesDetailsKeywordTypePrincipal:
      db = db.Joins("left join user on user.id = sales_details.member_id").Where("user.username like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
   case constvar.SalesDetailsKeywordTypeProductName:
      //db = db.Joins("left join sales_details_product sdp on sdp.sales_details_id = sales_details.id left join product").Where("user.username like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
      db = db.Joins("left join sales_details_product sdp on sdp.sales_details_id = sales_details.id left join products on sdp.product_id = products.id").Where("products.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
   }
   if slf.Preload {
      db = db.Preload("Products").
         Preload("Client").
         Preload("Member").
         Preload("SaleChance").
         Preload("WechatOrderStatus").
         Preload("Client")
@@ -101,6 +104,12 @@
   return db.Delete(&SalesDetails{}).Error
}
func (slf *SalesDetailsSearch) DeleteByIds(ids []int) error {
   var db = slf.build()
   db = db.Where("id in ?", ids)
   return db.Delete(&SalesDetails{}).Error
}
func (slf *SalesDetailsSearch) Update(record *SalesDetails) error {
   var db = slf.build()
   return db.Updates(record).Error
@@ -111,7 +120,7 @@
   return slf
}
func (slf *SalesDetailsSearch) Find() (*SalesDetails, error) {
func (slf *SalesDetailsSearch) First() (*SalesDetails, error) {
   var db = slf.build()
   var record = new(SalesDetails)
   err := db.First(record).Error
@@ -133,7 +142,7 @@
      db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
   }
   err := db.Find(&records).Error
   err := db.Order("id desc").Find(&records).Error
   return records, total, err
}
@@ -161,3 +170,62 @@
   slf.OrderBy = order
   return slf
}
func (slf *SalesDetailsSearch) UpdateByMap(upMap map[string]interface{}) error {
   var (
      db = slf.build()
   )
   if err := db.Updates(upMap).Error; err != nil {
      return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap)
   }
   return nil
}
func (slf *SalesDetailsSearch) SetOrm(tx *gorm.DB) *SalesDetailsSearch {
   slf.Orm = tx
   return slf
}
func (slf *SalesDetailsSearch) AmountReceivableAdd(tx *gorm.DB, id int, amount decimal.Decimal) error {
   slf.Orm = tx
   record, err := slf.SetId(id).First()
   if err != nil {
      return err
   }
   amount = record.AmountReceivable.Add(amount)
   return slf.UpdateByMap(map[string]interface{}{"amount_receivable": amount})
}
func (slf *SalesDetailsSearch) AmountReceivedAdd(tx *gorm.DB, id int, amount decimal.Decimal) error {
   slf.Orm = tx
   record, err := slf.SetId(id).First()
   if err != nil {
      return err
   }
   amount = record.AmountReceived.Add(amount)
   return slf.UpdateByMap(map[string]interface{}{"amount_received": amount})
}
func (slf *SalesDetailsSearch) AmountInvoicedAdd(tx *gorm.DB, id int, amount decimal.Decimal) error {
   slf.Orm = tx
   record, err := slf.SetId(id).First()
   if err != nil {
      return err
   }
   amount = record.AmountInvoiced.Add(amount)
   return slf.UpdateByMap(map[string]interface{}{"amount_invoiced": amount})
}
func (slf *SalesDetailsSearch) AmountNotInvoicedAdd(tx *gorm.DB, id int, amount decimal.Decimal) error {
   slf.Orm = tx
   record, err := slf.SetId(id).First()
   if err != nil {
      return err
   }
   amount = record.AmountUnInvoiced.Add(amount)
   return slf.UpdateByMap(map[string]interface{}{"amount_not_invoiced": amount})
}
func (slf *SalesDetailsSearch) UpdateProducts(record *SalesDetails, newProducts, removedProducts []*Product) error {
   var db = slf.build()
   return db.Updates(record).Error
}