|
@@ -800,3 +800,247 @@ func (rg *PreRegisterController) Detail(c *gin.Context) {
|
|
|
|
|
|
resp.OkData("获取成功", respItem, c)
|
|
|
}
|
|
|
+
|
|
|
+// Save
|
|
|
+// @Title 预登记保存
|
|
|
+// @Description 预登记保存
|
|
|
+// @Param request body fms.PrepayAddReq true "type json string"
|
|
|
+// @Success 200 string "操作成功"
|
|
|
+// @router /contract/pre_register/save [post]
|
|
|
+func (rg *PreRegisterController) Save(c *gin.Context) {
|
|
|
+ req := new(fms.PreRegisterSaveReq)
|
|
|
+ err := c.ShouldBind(&req)
|
|
|
+ if err != nil {
|
|
|
+ errs, ok := err.(validator.ValidationErrors)
|
|
|
+ if !ok {
|
|
|
+ resp.FailData("参数解析失败", "Err:"+err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ claims, _ := c.Get("adminInfo")
|
|
|
+ adminInfo := claims.(*system.SysAdmin)
|
|
|
+
|
|
|
+ // 日期校验
|
|
|
+ var startDate, endDate time.Time
|
|
|
+ if req.StartDate != "" && req.EndDate != "" {
|
|
|
+ startDate, err = time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
|
|
|
+ if err != nil {
|
|
|
+ resp.FailMsg("约定开始日期格式有误", "合同开始日期格式有误, Err: "+err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ endDate, err = time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
|
|
|
+ if err != nil {
|
|
|
+ resp.FailMsg("约定结束日期格式有误", "合同结束日期格式有误, Err: "+err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取销售分组信息
|
|
|
+ sellerList, e := crmService.GetSellerDepartmentListWithGroupAndTeam()
|
|
|
+ if e != nil {
|
|
|
+ resp.FailData("获取销售失败", "Err:"+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
|
|
|
+ for i := range sellerList {
|
|
|
+ sellerMap[sellerList[i].SellerId] = sellerList[i]
|
|
|
+ }
|
|
|
+
|
|
|
+ admin := new(crm.Admin)
|
|
|
+ sellerIds := strings.Split(req.SellerIds, ",")
|
|
|
+ if len(sellerIds) > 2 {
|
|
|
+ resp.Fail("最多只能选择两个销售", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var sellers []*crm.Admin
|
|
|
+ var pars []interface{}
|
|
|
+ cond := ``
|
|
|
+ if len(sellerIds) > 0 {
|
|
|
+ cond = ` admin_id in (?) `
|
|
|
+ pars = append(pars, sellerIds)
|
|
|
+ sellers, e = admin.List(cond, pars)
|
|
|
+ if e != nil {
|
|
|
+ resp.FailMsg("获取销售信息失败", "获取销售信息失败, Err: "+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ updateCols := []string{"CompanyName", "ProductIds", "StartDate", "EndDate", "ModifyTime"}
|
|
|
+
|
|
|
+ //更新合同
|
|
|
+ ob := new(fms.ContractRegister)
|
|
|
+ //ob.ContractCode = req.ContractCode
|
|
|
+ ob.ContractRegisterId = req.ContractRegisterId
|
|
|
+ ob.CompanyName = req.CompanyName
|
|
|
+ ob.ProductIds = req.ProductIds
|
|
|
+ ob.StartDate = startDate
|
|
|
+ ob.EndDate = endDate
|
|
|
+ ob.ModifyTime = time.Now()
|
|
|
+ if req.RegisterType == fms.ContractInvoiceTypePreMake {
|
|
|
+ //sellerItem := sellerMap[r.SellerId]
|
|
|
+ //if sellerItem == nil {
|
|
|
+ // resp.Fail("销售信息异常", c)
|
|
|
+ // return
|
|
|
+ //}
|
|
|
+ for _, v := range sellers {
|
|
|
+ if v.DepartmentId == crm.SellerDepartmentId {
|
|
|
+ ob.SellerId = v.AdminId
|
|
|
+ ob.SellerName = v.AdminName
|
|
|
+ updateCols = append(updateCols, "SellerId", "SellerName")
|
|
|
+ } else if v.DepartmentId == crm.RaiSellerDepartmentId {
|
|
|
+ ob.RaiSellerId = v.AdminId
|
|
|
+ ob.RaiSellerName = v.AdminName
|
|
|
+ updateCols = append(updateCols, "RaiSellerId", "RaiSellerName")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 套餐信息
|
|
|
+ serviceAmountMap := make(map[int]float64)
|
|
|
+ serviceList, e := fmsService.HandleContractServiceAndDetail(req.Services, true, serviceAmountMap)
|
|
|
+ if e != nil {
|
|
|
+ resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ invoiceUpdateCols := make([]string, 0)
|
|
|
+ invoiceHandleType := 0
|
|
|
+ invoiceList := make([]*fms.ContractInvoice, 0)
|
|
|
+ for _, r := range req.List {
|
|
|
+ // 货币及汇率
|
|
|
+ rateList, e := fmsService.GetTodayCurrencyRateList()
|
|
|
+ if e != nil {
|
|
|
+ resp.FailMsg("操作失败", "获取今日货币汇率失败, Err: "+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var rate float64
|
|
|
+ for i := range rateList {
|
|
|
+ if r.CurrencyUnit == rateList[i].Code {
|
|
|
+ rate = rateList[i].RMBRate
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if rate <= 0 {
|
|
|
+ resp.FailMsg("操作失败", "货币汇率信息有误", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ registerDate, e := time.Parse(utils.FormatDate, r.RegisterDate)
|
|
|
+ if e != nil {
|
|
|
+ resp.FailMsg("日期转换失败", "日期转换失败, Err: "+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ v := &fms.ContractInvoice{
|
|
|
+ ContractInvoiceId: r.InvoiceId,
|
|
|
+ ContractRegisterId: ob.ContractRegisterId,
|
|
|
+ OriginAmount: r.Amount,
|
|
|
+ CurrencyUnit: r.CurrencyUnit,
|
|
|
+ InvoiceType: req.RegisterType,
|
|
|
+ InvoiceDate: registerDate,
|
|
|
+ AdminId: int(adminInfo.AdminId),
|
|
|
+ AdminName: adminInfo.AdminName,
|
|
|
+ Remark: r.Remark,
|
|
|
+ ServiceProductId: r.ServiceProductId,
|
|
|
+ IsPrePay: 1,
|
|
|
+ StartDate: startDate,
|
|
|
+ EndDate: endDate,
|
|
|
+ TimeBase: base.TimeBase{},
|
|
|
+ }
|
|
|
+ v.TimeBase.ModifyTime = time.Now()
|
|
|
+
|
|
|
+ a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.OriginAmount/rate), 64)
|
|
|
+ v.Amount = a
|
|
|
+
|
|
|
+ if req.RegisterType == fms.ContractInvoiceTypePreMake {
|
|
|
+ sellerItem := sellerMap[r.SellerId]
|
|
|
+ if sellerItem == nil {
|
|
|
+ resp.Fail("销售信息异常", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ v.SellerId = sellerItem.SellerId
|
|
|
+ v.SellerName = sellerItem.SellerName
|
|
|
+ v.SellerGroupId = sellerItem.GroupId
|
|
|
+ v.SellerGroupName = sellerItem.GroupName
|
|
|
+ v.SellerTeamId = sellerItem.TeamId
|
|
|
+ v.SellerTeamName = sellerItem.TeamName
|
|
|
+ invoiceUpdateCols = append(invoiceUpdateCols,"SellerId","SellerName","SellerGroupId",
|
|
|
+ "SellerGroupName","SellerTeamId","SellerTeamName")
|
|
|
+ }
|
|
|
+
|
|
|
+ // 新增开票到款记录
|
|
|
+ if v.ContractInvoiceId != 0 {
|
|
|
+ //更新
|
|
|
+ invoiceHandleType = 1
|
|
|
+ invoiceUpdateCols = append(invoiceUpdateCols, "OriginAmount","CurrencyUnit",
|
|
|
+ "InvoiceDate","Remark","ServiceProductId","StartDate","EndDate","ModifyTime")
|
|
|
+ }else {
|
|
|
+ //新增
|
|
|
+ invoiceHandleType = 2
|
|
|
+ }
|
|
|
+ invoiceList = append(invoiceList, v)
|
|
|
+
|
|
|
+ //预登记列表
|
|
|
+ pp := &fms.ContractPreRegister{
|
|
|
+ ContractRegisterId: ob.ContractRegisterId,
|
|
|
+ CompanyName: req.CompanyName,
|
|
|
+ InvoiceType: req.RegisterType,
|
|
|
+ InvoiceDate: registerDate,
|
|
|
+ AdminId: int(adminInfo.AdminId),
|
|
|
+ AdminName: adminInfo.AdminName,
|
|
|
+ Remark: r.Remark,
|
|
|
+ ServiceProductId: r.ServiceProductId,
|
|
|
+ IsPrePay: 1,
|
|
|
+ StartDate: startDate,
|
|
|
+ EndDate: endDate,
|
|
|
+ TimeBase: base.TimeBase{},
|
|
|
+ }
|
|
|
+ pp.Set()
|
|
|
+ if pp.InvoiceType == fms.ContractInvoiceTypePreMake {
|
|
|
+ //开票走开票字段
|
|
|
+ pp.InvoiceId = v.ContractInvoiceId
|
|
|
+ pp.InvoiceDate = registerDate
|
|
|
+ pp.OriginAmount = r.Amount
|
|
|
+ pp.CurrencyUnit = r.CurrencyUnit
|
|
|
+ a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", pp.OriginAmount/rate), 64)
|
|
|
+ pp.Amount = a
|
|
|
+
|
|
|
+ sellerItem := sellerMap[r.SellerId]
|
|
|
+ if sellerItem == nil {
|
|
|
+ resp.Fail("销售信息异常", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ pp.SellerId = sellerItem.SellerId
|
|
|
+ pp.SellerName = sellerItem.SellerName
|
|
|
+ pp.SellerGroupId = sellerItem.GroupId
|
|
|
+ pp.SellerGroupName = sellerItem.GroupName
|
|
|
+ pp.SellerTeamId = sellerItem.TeamId
|
|
|
+ pp.SellerTeamName = sellerItem.TeamName
|
|
|
+ } else {
|
|
|
+ //到款
|
|
|
+ pp.ArriveId = v.ContractInvoiceId
|
|
|
+ pp.ArriveDate = registerDate
|
|
|
+ pp.ArriveOriginAmount = r.Amount
|
|
|
+ pp.ArriveCurrencyUnit = r.CurrencyUnit
|
|
|
+ a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", pp.ArriveOriginAmount/rate), 64)
|
|
|
+ pp.ArriveAmount = a
|
|
|
+ }
|
|
|
+
|
|
|
+ // 新增开票到款记录
|
|
|
+ if e = pp.Create(); e != nil {
|
|
|
+ resp.FailMsg("操作失败", "新增预登记失败, Err: "+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if e = fms.UpdateContractRegisterPre(ob, updateCols, serviceList,invoiceList, invoiceUpdateCols, invoiceHandleType); e != nil {
|
|
|
+ resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 开票到款汇总
|
|
|
+ go fmsService.SummaryInvoicePaymentByContractRegisterId(ob.ContractRegisterId)
|
|
|
+
|
|
|
+ resp.Ok("操作成功", c)
|
|
|
+}
|