| | |
| | | "aps_crm/pkg/ecode" |
| | | "github.com/gin-gonic/gin" |
| | | "strconv" |
| | | "strings" |
| | | ) |
| | | |
| | | type SalesRefundApi struct{} |
| | | |
| | | // Add |
| | | // |
| | | // @Tags SalesRefund |
| | | // @Tags 销售退款单 |
| | | // @Summary 添加销售退款 |
| | | // @Produce application/json |
| | | // @Param object body request.AddSalesRefundRequest true "查询参数" |
| | |
| | | |
| | | // Delete |
| | | // |
| | | // @Tags SalesRefund |
| | | // @Tags 销售退款单 |
| | | // @Summary 删除销售退款 |
| | | // @Produce application/json |
| | | // @Param id path int true "查询参数" |
| | |
| | | ctx.Ok() |
| | | } |
| | | |
| | | // BatchDelete |
| | | // @Tags 销售退款单 |
| | | // @Summary 批量删除销售退款单 |
| | | // @Produce application/json |
| | | // @Param object body request.CommonIds true "参数" |
| | | // @Success 200 {object} contextx.Response{} |
| | | // @Router /api/salesRefund/delete [delete] |
| | | func (s *SalesRefundApi) BatchDelete(c *gin.Context) { |
| | | var params request.CommonIds |
| | | ctx, ok := contextx.NewContext(c, ¶ms) |
| | | if !ok { |
| | | return |
| | | } |
| | | |
| | | failIds, _ := salesRefundService.BatchDeleteSalesRefund(params.Ids) |
| | | if len(failIds) == 0 { |
| | | ctx.Ok() |
| | | return |
| | | } |
| | | salesReturns, code := salesRefundService.GetSalesRefundListByIds(failIds) |
| | | if code != ecode.OK { |
| | | ctx.Ok() |
| | | return |
| | | } |
| | | var numbers []string |
| | | for _, salesReturn := range salesReturns { |
| | | numbers = append(numbers, salesReturn.Number) |
| | | } |
| | | ctx.FailWithMsg(ecode.DBErr, strings.Join(numbers, ",")+"删除失败") |
| | | } |
| | | |
| | | // Update |
| | | // |
| | | // @Tags SalesRefund |
| | | // @Tags 销售退款单 |
| | | // @Summary 更新销售退款 |
| | | // @Produce application/json |
| | | // @Param object body request.UpdateSalesRefundRequest true "查询参数" |
| | |
| | | salesRefundRes.IsInvoice = salesRefund.IsInvoice |
| | | salesRefundRes.Reason = salesRefund.Reason |
| | | salesRefundRes.Products = salesRefund.Products |
| | | salesRefundRes.SourceType = salesRefund.SourceType |
| | | salesRefundRes.SourceId = salesRefund.SourceId |
| | | |
| | | return ecode.OK, salesRefundRes |
| | | } |
| | | |
| | | // List |
| | | // |
| | | // @Tags SalesRefund |
| | | // @Tags 销售退款单 |
| | | // @Summary 销售退款单列表 |
| | | // @Produce application/json |
| | | // @Param object body request.GetSalesRefundList true "参数" |
| | |
| | | "aps_crm/pkg/ecode" |
| | | "github.com/gin-gonic/gin" |
| | | "strconv" |
| | | "strings" |
| | | ) |
| | | |
| | | type SalesReturnApi struct{} |
| | |
| | | if !ok { |
| | | return |
| | | } |
| | | |
| | | errCode := salesReturnService.BatchDeleteSalesReturn(params.Ids) |
| | | if errCode != ecode.OK { |
| | | ctx.Fail(errCode) |
| | | failIds, _ := salesReturnService.BatchDeleteSalesReturn(params.Ids) |
| | | if len(failIds) == 0 { |
| | | ctx.Ok() |
| | | return |
| | | } |
| | | |
| | | ctx.Ok() |
| | | salesReturns, code := salesReturnService.GetSalesReturnListByIds(failIds) |
| | | if code != ecode.OK { |
| | | ctx.Ok() |
| | | return |
| | | } |
| | | var numbers []string |
| | | for _, salesReturn := range salesReturns { |
| | | numbers = append(numbers, salesReturn.Number) |
| | | } |
| | | ctx.FailWithMsg(ecode.DBErr, strings.Join(numbers, ",")+"删除失败") |
| | | } |
| | | |
| | | // Update |
| | |
| | | "application/json" |
| | | ], |
| | | "tags": [ |
| | | "SalesRefund" |
| | | "销售退款单" |
| | | ], |
| | | "summary": "添加销售退款", |
| | | "parameters": [ |
| | |
| | | } |
| | | } |
| | | }, |
| | | "/api/salesRefund/delete": { |
| | | "delete": { |
| | | "produces": [ |
| | | "application/json" |
| | | ], |
| | | "tags": [ |
| | | "销售退款单" |
| | | ], |
| | | "summary": "批量删除销售退款单", |
| | | "parameters": [ |
| | | { |
| | | "description": "参数", |
| | | "name": "object", |
| | | "in": "body", |
| | | "required": true, |
| | | "schema": { |
| | | "$ref": "#/definitions/request.CommonIds" |
| | | } |
| | | } |
| | | ], |
| | | "responses": { |
| | | "200": { |
| | | "description": "OK", |
| | | "schema": { |
| | | "$ref": "#/definitions/contextx.Response" |
| | | } |
| | | } |
| | | } |
| | | } |
| | | }, |
| | | "/api/salesRefund/delete/{id}": { |
| | | "delete": { |
| | | "produces": [ |
| | | "application/json" |
| | | ], |
| | | "tags": [ |
| | | "SalesRefund" |
| | | "销售退款单" |
| | | ], |
| | | "summary": "删除销售退款", |
| | | "parameters": [ |
| | |
| | | "application/json" |
| | | ], |
| | | "tags": [ |
| | | "SalesRefund" |
| | | "销售退款单" |
| | | ], |
| | | "summary": "销售退款单列表", |
| | | "parameters": [ |
| | |
| | | "application/json" |
| | | ], |
| | | "tags": [ |
| | | "SalesRefund" |
| | | "销售退款单" |
| | | ], |
| | | "summary": "更新销售退款", |
| | | "parameters": [ |
| | |
| | | "type": "integer" |
| | | }, |
| | | "sourceType": { |
| | | "description": "来源类型(1销售明细单2服务合同3销售发票4收款计划5出库单)", |
| | | "description": "来源类型(1销售退货)", |
| | | "allOf": [ |
| | | { |
| | | "$ref": "#/definitions/constvar.RefundSourceType" |
| | |
| | | "application/json" |
| | | ], |
| | | "tags": [ |
| | | "SalesRefund" |
| | | "销售退款单" |
| | | ], |
| | | "summary": "添加销售退款", |
| | | "parameters": [ |
| | |
| | | } |
| | | } |
| | | }, |
| | | "/api/salesRefund/delete": { |
| | | "delete": { |
| | | "produces": [ |
| | | "application/json" |
| | | ], |
| | | "tags": [ |
| | | "销售退款单" |
| | | ], |
| | | "summary": "批量删除销售退款单", |
| | | "parameters": [ |
| | | { |
| | | "description": "参数", |
| | | "name": "object", |
| | | "in": "body", |
| | | "required": true, |
| | | "schema": { |
| | | "$ref": "#/definitions/request.CommonIds" |
| | | } |
| | | } |
| | | ], |
| | | "responses": { |
| | | "200": { |
| | | "description": "OK", |
| | | "schema": { |
| | | "$ref": "#/definitions/contextx.Response" |
| | | } |
| | | } |
| | | } |
| | | } |
| | | }, |
| | | "/api/salesRefund/delete/{id}": { |
| | | "delete": { |
| | | "produces": [ |
| | | "application/json" |
| | | ], |
| | | "tags": [ |
| | | "SalesRefund" |
| | | "销售退款单" |
| | | ], |
| | | "summary": "删除销售退款", |
| | | "parameters": [ |
| | |
| | | "application/json" |
| | | ], |
| | | "tags": [ |
| | | "SalesRefund" |
| | | "销售退款单" |
| | | ], |
| | | "summary": "销售退款单列表", |
| | | "parameters": [ |
| | |
| | | "application/json" |
| | | ], |
| | | "tags": [ |
| | | "SalesRefund" |
| | | "销售退款单" |
| | | ], |
| | | "summary": "更新销售退款", |
| | | "parameters": [ |
| | |
| | | "type": "integer" |
| | | }, |
| | | "sourceType": { |
| | | "description": "来源类型(1销售明细单2服务合同3销售发票4收款计划5出库单)", |
| | | "description": "来源类型(1销售退货)", |
| | | "allOf": [ |
| | | { |
| | | "$ref": "#/definitions/constvar.RefundSourceType" |
| | |
| | | sourceType: |
| | | allOf: |
| | | - $ref: '#/definitions/constvar.RefundSourceType' |
| | | description: 来源类型(1销售明细单2服务合同3销售发票4收款计划5出库单) |
| | | description: 来源类型(1销售退货) |
| | | type: object |
| | | model.SalesReturn: |
| | | properties: |
| | |
| | | $ref: '#/definitions/contextx.Response' |
| | | summary: 添加销售退款 |
| | | tags: |
| | | - SalesRefund |
| | | - 销售退款单 |
| | | /api/salesRefund/delete: |
| | | delete: |
| | | parameters: |
| | | - description: 参数 |
| | | in: body |
| | | name: object |
| | | required: true |
| | | schema: |
| | | $ref: '#/definitions/request.CommonIds' |
| | | produces: |
| | | - application/json |
| | | responses: |
| | | "200": |
| | | description: OK |
| | | schema: |
| | | $ref: '#/definitions/contextx.Response' |
| | | summary: 批量删除销售退款单 |
| | | tags: |
| | | - 销售退款单 |
| | | /api/salesRefund/delete/{id}: |
| | | delete: |
| | | parameters: |
| | |
| | | $ref: '#/definitions/contextx.Response' |
| | | summary: 删除销售退款 |
| | | tags: |
| | | - SalesRefund |
| | | - 销售退款单 |
| | | /api/salesRefund/list: |
| | | post: |
| | | parameters: |
| | |
| | | type: object |
| | | summary: 销售退款单列表 |
| | | tags: |
| | | - SalesRefund |
| | | - 销售退款单 |
| | | /api/salesRefund/update: |
| | | put: |
| | | parameters: |
| | |
| | | $ref: '#/definitions/contextx.Response' |
| | | summary: 更新销售退款 |
| | | tags: |
| | | - SalesRefund |
| | | - 销售退款单 |
| | | /api/salesReturn/add: |
| | | post: |
| | | parameters: |
| | |
| | | Currency{}, |
| | | CollectionProjection{}, |
| | | ContactInformation{}, |
| | | SalesReturn{}, |
| | | SalesRefund{}, |
| | | ) |
| | | return err |
| | | } |
| | |
| | | ClientId int `json:"clientId"` |
| | | Number string `json:"number"` |
| | | MemberId int `json:"memberId"` |
| | | SourceType constvar.RefundSourceType `gorm:"column:source_type;type:int;not null;default 0;comment:来源类型(1销售明细单2服务合同3销售发票4收款计划5出库单)" json:"sourceType"` // 来源类型(1销售明细单2服务合同3销售发票4收款计划5出库单) |
| | | SourceId int `gorm:"column:source_id;type:int;not null;default 0;comment:源单id " json:"sourceId"` // 源单id |
| | | SourceType constvar.RefundSourceType `gorm:"column:source_type;type:int;not null;default 0;comment:来源类型(1销售退款的)" json:"sourceType" binding:"required"` // 来源类型(1销售退款单) |
| | | SourceId int `gorm:"column:source_id;type:int;not null;default 0;comment:源单id " json:"sourceId" binding:"required"` // 源单id |
| | | RefundDate string `json:"refundDate"` |
| | | PaymentTypeId int `gorm:"column:payment_type_id;type:int;not null;default 0;comment:收款方式ID" json:"paymentTypeId"` // 收款方式ID |
| | | BankAccountId int `gorm:"column:bank_account_id;type:int;not null;default 0;comment:账户id" json:"bankAccountId"` // 账户id |
| | |
| | | ClientId int `json:"clientId" gorm:"column:client_id;type:int;comment:客户id"` |
| | | Client Client `json:"client" gorm:"foreignKey:ClientId"` |
| | | Number string `json:"number" gorm:"column:number;type:varchar(255);comment:退款单号"` |
| | | SourceType constvar.RefundSourceType `gorm:"column:source_type;type:int;not null;default 0;comment:来源类型(1销售明细单2服务合同3销售发票4收款计划5出库单)" json:"sourceType"` // 来源类型(1销售明细单2服务合同3销售发票4收款计划5出库单) |
| | | SourceId int `gorm:"column:source_id;type:int;not null;default 0;comment:源单id " json:"sourceId"` // 源单id |
| | | SourceType constvar.RefundSourceType `gorm:"column:source_type;type:int;not null;default 0;comment:来源类型(1销售退货)" json:"sourceType"` // 来源类型(1销售退货) |
| | | SourceId int `gorm:"column:source_id;type:int;not null;default 0;comment:源单id " json:"sourceId"` // 源单id |
| | | MemberId int `json:"memberId" gorm:"column:member_id;type:int;comment:负责人id"` |
| | | Member User `json:"member" gorm:"foreignKey:MemberId"` |
| | | RefundDate string `json:"refundDate" gorm:"column:refund_date;type:datetime;comment:退款日期"` |
| | | RefundDate string `json:"refundDate" gorm:"column:refund_date;type:varchar(255);comment:退款日期"` |
| | | PaymentTypeId int `gorm:"column:payment_type_id;type:int;not null;default 0;comment:收款方式ID" json:"paymentTypeId"` // 收款方式ID |
| | | PaymentType PaymentType `gorm:"foreignKey:PaymentTypeId" json:"paymentType"` |
| | | BankAccountId int `gorm:"column:bank_account_id;type:int;not null;default 0;comment:账户id" json:"bankAccountId"` // 账户id |
| | |
| | | PageNum int |
| | | PageSize int |
| | | Preload bool |
| | | Ids []int |
| | | } |
| | | ) |
| | | |
| | |
| | | var db = slf.Orm.Model(&SalesRefund{}) |
| | | if slf.Id != 0 { |
| | | db = db.Where("id = ?", slf.Id) |
| | | } |
| | | |
| | | if len(slf.Ids) != 0 { |
| | | db = db.Where("id in ?", slf.Ids) |
| | | } |
| | | |
| | | if slf.Preload { |
| | |
| | | return db.Updates(record).Error |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) Find() (*SalesRefund, error) { |
| | | func (slf *SalesRefundSearch) First() (*SalesRefund, error) { |
| | | var db = slf.build() |
| | | var record = new(SalesRefund) |
| | | err := db.First(record).Error |
| | | return record, err |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) Find() ([]*SalesRefund, error) { |
| | | var db = slf.build() |
| | | var records = make([]*SalesRefund, 0) |
| | | |
| | | err := db.Find(&records).Error |
| | | return records, err |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) FindAll() ([]*SalesRefund, int64, error) { |
| | |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) SetIds(id []int) *SalesRefundSearch { |
| | | slf.Ids = id |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) SetSourceType(sourceType constvar.RefundSourceType) *SalesRefundSearch { |
| | | slf.SourceType = sourceType |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) SetSourceId(id int) *SalesRefundSearch { |
| | | slf.SourceId = id |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesRefundSearch) SetPreload(preload bool) *SalesRefundSearch { |
| | | slf.Preload = preload |
| | | return slf |
| | |
| | | Repository string `json:"repository" gorm:"column:repository;type:varchar(255);comment:仓库"` |
| | | MemberId int `json:"memberId" gorm:"column:member_id;type:int;comment:负责人id"` |
| | | Member User `json:"Member" gorm:"foreignKey:MemberId"` |
| | | ReturnDate string `json:"returnDate" gorm:"column:return_date;type:datetime(3);comment:退货日期"` //退货日期 |
| | | ReturnDate string `json:"returnDate" gorm:"column:return_date;type:varchar(255);comment:退货日期"` //退货日期 |
| | | SalesReturnStatusId int `json:"salesReturnStatusId" gorm:"column:sales_return_status;type:int;comment:退货状态"` //退货状态id |
| | | SalesReturnStatus SalesReturnStatus `json:"SalesReturnStatus" gorm:"foreignKey:SalesReturnStatusId"` //退货状态 |
| | | CreatorId int `json:"-" gorm:"column:creator_id;type:int;comment:创建人id"` //创建人ID |
| | |
| | | Products []*Product `json:"products" gorm:"many2many:salesReturn_product;"` //退货产品 |
| | | AmountShouldRefund decimal.Decimal `gorm:"column:amount_should_refund;type:decimal(12,2);comment:应退款金额" json:"amountShouldRefund"` // 应退款金额 |
| | | AmountHasRefund decimal.Decimal `gorm:"column:amount_has_refund;type:decimal(12,2);comment:已退款金额" json:"amountHasRefund"` // 已退款金额 |
| | | |
| | | AmountTotal decimal.Decimal `gorm:"column:amount_total;type:decimal(12,2);comment:退货产品总金额" json:"-"` |
| | | CrmModel |
| | | } |
| | | |
| | |
| | | PageNum int |
| | | PageSize int |
| | | Preload bool |
| | | Ids []int |
| | | } |
| | | ) |
| | | |
| | |
| | | var db = slf.Orm.Model(&SalesReturn{}) |
| | | if slf.Id != 0 { |
| | | db = db.Where("id = ?", slf.Id) |
| | | } |
| | | if len(slf.Ids) != 0 { |
| | | db = db.Where("id in ?", slf.Ids) |
| | | } |
| | | |
| | | if slf.Preload { |
| | |
| | | return db.Updates(record).Error |
| | | } |
| | | |
| | | func (slf *SalesReturnSearch) Find() (*SalesReturn, error) { |
| | | func (slf *SalesReturnSearch) First() (*SalesReturn, error) { |
| | | var db = slf.build() |
| | | var record = new(SalesReturn) |
| | | err := db.First(record).Error |
| | |
| | | if slf.PageNum > 0 && slf.PageSize > 0 { |
| | | db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize) |
| | | } |
| | | |
| | | err := db.Preload("Member").Preload("Client").Preload("Products").Order("id desc").Find(&records).Error |
| | | return records, total, err |
| | | } |
| | | |
| | | func (slf *SalesReturnSearch) Find() ([]*SalesReturn, error) { |
| | | var db = slf.build() |
| | | var records = make([]*SalesReturn, 0) |
| | | |
| | | err := db.Find(&records).Error |
| | | return records, err |
| | | } |
| | | |
| | | func (slf *SalesReturnSearch) SetId(id int) *SalesReturnSearch { |
| | | slf.Id = id |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesReturnSearch) SetIds(id []int) *SalesReturnSearch { |
| | | slf.Ids = id |
| | | return slf |
| | | } |
| | | |
| | | func (slf *SalesReturnSearch) SetKeyword(keyword string) *SalesReturnSearch { |
| | | slf.Keyword = keyword |
| | | return slf |
| | |
| | | salesRefundRouter := router.Group("salesRefund") |
| | | salesRefundApi := v1.ApiGroup.SalesRefundApi |
| | | { |
| | | salesRefundRouter.POST("add", salesRefundApi.Add) // 添加销售退款 |
| | | salesRefundRouter.DELETE("delete/:id", salesRefundApi.Delete) // 删除销售退款 |
| | | salesRefundRouter.PUT("update", salesRefundApi.Update) // 更新销售退款 |
| | | salesRefundRouter.POST("add", salesRefundApi.Add) // 添加销售退款 |
| | | salesRefundRouter.DELETE("delete/:id", salesRefundApi.Delete) // 删除销售退款 |
| | | salesRefundRouter.DELETE("delete", salesRefundApi.BatchDelete) // 批量删除销售退款单 |
| | | salesRefundRouter.PUT("update", salesRefundApi.Update) // 更新销售退款 |
| | | salesRefundRouter.POST("list", salesRefundApi.List) // 获取销售退款单列表 |
| | | } |
| | | } |
| | | } |
| | |
| | | "aps_crm/constvar" |
| | | "aps_crm/model" |
| | | "aps_crm/pkg/ecode" |
| | | "github.com/shopspring/decimal" |
| | | "gorm.io/gorm" |
| | | ) |
| | | |
| | | type SalesRefundService struct{} |
| | | |
| | | func (SalesRefundService) AddSalesRefund(salesRefund *model.SalesRefund) int { |
| | | salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SourceId).SetPreload(true).Find() |
| | | salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SourceId).SetPreload(true).First() |
| | | if err != nil { |
| | | return ecode.SalesReturnNotExist |
| | | } |
| | |
| | | if code := CheckProduct(salesReturnRecord.Products, salesRefund.Products); code != ecode.OK { |
| | | return code |
| | | } |
| | | var amount decimal.Decimal |
| | | for _, product := range salesRefund.Products { |
| | | amount = amount.Add(product.Amount.Mul(product.Price)) |
| | | } |
| | | |
| | | err = model.NewSalesRefundSearch().Create(salesRefund) |
| | | err = model.WithTransaction(func(db *gorm.DB) error { |
| | | err = model.NewSalesRefundSearch().Create(salesRefund) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | salesReturnRecord.AmountHasRefund = salesReturnRecord.AmountHasRefund.Add(amount).Round(2) |
| | | salesReturnRecord.AmountShouldRefund = salesReturnRecord.AmountTotal.Sub(salesReturnRecord.AmountHasRefund).Round(2) |
| | | err = model.NewSalesReturnSearch().SetId(salesRefund.SourceId).Update(salesReturnRecord) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | return nil |
| | | }) |
| | | |
| | | if err != nil { |
| | | return ecode.SalesRefundExist |
| | | } |
| | |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (SalesRefundService) DeleteSalesRefund(id int) int { |
| | | _, err := model.NewSalesRefundSearch().SetId(id).Find() |
| | | func (slf SalesRefundService) DeleteSalesRefund(id int) int { |
| | | refund, err := model.NewSalesRefundSearch().SetId(id).First() |
| | | if err != nil { |
| | | return ecode.SalesRefundNotExist |
| | | } |
| | | salesReturnRecord, err := model.NewSalesReturnSearch().SetId(refund.SourceId).SetPreload(true).First() |
| | | if err != nil { |
| | | return ecode.SalesReturnNotExist |
| | | } |
| | | |
| | | err = model.NewSalesRefundSearch().SetId(id).Delete() |
| | | var amount decimal.Decimal |
| | | for _, product := range refund.Products { |
| | | amount = amount.Add(product.Amount.Mul(product.Price)) |
| | | } |
| | | |
| | | err = model.WithTransaction(func(db *gorm.DB) error { |
| | | err = model.NewSalesRefundSearch().SetId(id).Delete() |
| | | if err != nil { |
| | | return err |
| | | } |
| | | salesReturnRecord.AmountHasRefund = salesReturnRecord.AmountHasRefund.Sub(amount).Round(2) |
| | | salesReturnRecord.AmountShouldRefund = salesReturnRecord.AmountTotal.Sub(salesReturnRecord.AmountHasRefund).Round(2) |
| | | err = model.NewSalesReturnSearch().SetId(refund.SourceId).Update(salesReturnRecord) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | return nil |
| | | }) |
| | | |
| | | if err != nil { |
| | | return ecode.SalesRefundNotExist |
| | | } |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (slf SalesRefundService) BatchDeleteSalesRefund(ids []int) (failIds []int, code int) { |
| | | for _, id := range ids { |
| | | code = slf.DeleteSalesRefund(id) |
| | | if code != ecode.OK { |
| | | failIds = append(failIds, id) |
| | | } |
| | | } |
| | | return failIds, code |
| | | } |
| | | |
| | | func (SalesRefundService) UpdateSalesRefund(salesRefund *model.SalesRefund) int { |
| | | // check salesRefund exist |
| | | _, err := model.NewSalesRefundSearch().SetId(salesRefund.Id).Find() |
| | | oldRefund, err := model.NewSalesRefundSearch().SetId(salesRefund.Id).First() |
| | | if err != nil { |
| | | return ecode.SalesRefundNotExist |
| | | } |
| | | |
| | | salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SourceId).SetPreload(true).Find() |
| | | salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SourceId).SetPreload(true).First() |
| | | if err != nil { |
| | | return ecode.SalesReturnNotExist |
| | | } |
| | |
| | | return code |
| | | } |
| | | |
| | | err = model.NewSalesRefundSearch().SetId(salesRefund.Id).Update(salesRefund) |
| | | var diffAmount decimal.Decimal |
| | | |
| | | for _, product := range salesRefund.Products { |
| | | diffAmount = diffAmount.Add(product.Amount.Mul(product.Price)) |
| | | } |
| | | for _, product := range oldRefund.Products { |
| | | diffAmount = diffAmount.Sub(product.Amount.Mul(product.Price)) |
| | | } |
| | | |
| | | err = model.WithTransaction(func(db *gorm.DB) error { |
| | | err = model.NewSalesRefundSearch().SetId(salesRefund.Id).Update(salesRefund) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | salesReturnRecord.AmountHasRefund = salesReturnRecord.AmountHasRefund.Sub(diffAmount).Round(2) |
| | | salesReturnRecord.AmountShouldRefund = salesReturnRecord.AmountTotal.Sub(salesReturnRecord.AmountHasRefund).Round(2) |
| | | err = model.NewSalesReturnSearch().SetId(salesRefund.SourceId).Update(salesReturnRecord) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | return nil |
| | | }) |
| | | |
| | | if err != nil { |
| | | return ecode.SalesRefundSetErr |
| | | } |
| | |
| | | } |
| | | return contacts, total, ecode.OK |
| | | } |
| | | |
| | | func (SalesRefundService) GetSalesRefundListByIds(ids []int) ([]*model.SalesRefund, int) { |
| | | // get contact list |
| | | salesRefunds, err := model.NewSalesRefundSearch(). |
| | | SetIds(ids). |
| | | Find() |
| | | if err != nil { |
| | | return nil, ecode.SalesReturnListErr |
| | | } |
| | | return salesRefunds, ecode.OK |
| | | } |
| | |
| | | "aps_crm/constvar" |
| | | "aps_crm/model" |
| | | "aps_crm/pkg/ecode" |
| | | "github.com/shopspring/decimal" |
| | | "gorm.io/gorm" |
| | | ) |
| | | |
| | | type SalesReturnService struct{} |
| | | |
| | | func (slf SalesReturnService) AddSalesReturn(salesReturn *model.SalesReturn) int { |
| | | salesReturn.AmountShouldRefund = decimal.Zero |
| | | salesReturn.AmountHasRefund = decimal.Zero |
| | | salesReturn.AmountTotal = decimal.Zero |
| | | for _, product := range salesReturn.Products { |
| | | salesReturn.AmountTotal = salesReturn.AmountTotal.Add(product.Amount.Mul(product.Price)) |
| | | } |
| | | salesReturn.AmountShouldRefund = salesReturn.AmountTotal |
| | | err := model.NewSalesReturnSearch().Create(salesReturn) |
| | | if err != nil { |
| | | return ecode.SalesReturnExist |
| | |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (SalesReturnService) DeleteSalesReturn(id int) int { |
| | | _, err := model.NewSalesReturnSearch().SetId(id).Find() |
| | | func (slf SalesReturnService) DeleteSalesReturn(id int) int { |
| | | _, err := model.NewSalesReturnSearch().SetId(id).First() |
| | | if err != nil { |
| | | return ecode.SalesReturnNotExist |
| | | } |
| | | |
| | | err = model.NewSalesReturnSearch().SetId(id).Delete() |
| | | err = model.WithTransaction(func(db *gorm.DB) error { |
| | | err = model.NewSalesReturnSearch().SetId(id).Delete() |
| | | if err != nil { |
| | | return err |
| | | } |
| | | err = model.NewSalesRefundSearch().SetSourceType(constvar.RefundSourceTypeSalesReturn).SetSourceId(id).Delete() |
| | | if err != nil { |
| | | return err |
| | | } |
| | | return nil |
| | | }) |
| | | if err != nil { |
| | | return ecode.SalesReturnNotExist |
| | | } |
| | | return ecode.OK |
| | | } |
| | | |
| | | func (SalesReturnService) BatchDeleteSalesReturn(ids []int) int { |
| | | err := model.NewSalesReturnSearch().DeleteByIds(ids) |
| | | if err != nil { |
| | | return ecode.DBErr |
| | | func (slf SalesReturnService) BatchDeleteSalesReturn(ids []int) (failIds []int, code int) { |
| | | for _, id := range ids { |
| | | code = slf.DeleteSalesReturn(id) |
| | | if code != ecode.OK { |
| | | failIds = append(failIds, id) |
| | | } |
| | | } |
| | | return ecode.OK |
| | | return failIds, code |
| | | } |
| | | |
| | | func (SalesReturnService) UpdateSalesReturn(salesReturn *model.SalesReturn) int { |
| | | // check salesReturn exist |
| | | _, err := model.NewSalesReturnSearch().SetId(salesReturn.Id).Find() |
| | | _, err := model.NewSalesReturnSearch().SetId(salesReturn.Id).First() |
| | | if err != nil { |
| | | return ecode.SalesReturnNotExist |
| | | } |
| | | |
| | | salesReturn.AmountShouldRefund = decimal.Zero |
| | | salesReturn.AmountHasRefund = decimal.Zero |
| | | salesReturn.AmountTotal = decimal.Zero |
| | | for _, product := range salesReturn.Products { |
| | | salesReturn.AmountTotal = salesReturn.AmountTotal.Add(product.Amount.Mul(product.Price)) |
| | | } |
| | | salesReturn.AmountShouldRefund = salesReturn.AmountTotal |
| | | |
| | | err = model.NewSalesReturnSearch().SetId(salesReturn.Id).Update(salesReturn) |
| | | if err != nil { |
| | |
| | | } |
| | | return contacts, total, ecode.OK |
| | | } |
| | | |
| | | func (SalesReturnService) GetSalesReturnListByIds(ids []int) ([]*model.SalesReturn, int) { |
| | | // get contact list |
| | | salesReturns, err := model.NewSalesReturnSearch(). |
| | | SetIds(ids). |
| | | Find() |
| | | if err != nil { |
| | | return nil, ecode.SalesReturnListErr |
| | | } |
| | | return salesReturns, ecode.OK |
| | | } |