Browse Source

合同登记货币单位:导入导出新增

hsun 2 years ago
parent
commit
3c161ca2a4
3 changed files with 132 additions and 64 deletions
  1. 58 63
      controller/contract/register.go
  2. 7 0
      models/fms/contract_register.go
  3. 67 1
      services/fms/currency_rate.go

+ 58 - 63
controller/contract/register.go

@@ -2,12 +2,10 @@ package contract
 
 import (
 	"bytes"
-	"context"
 	"encoding/json"
 	"fmt"
 	"github.com/gin-gonic/gin"
 	"github.com/go-playground/validator/v10"
-	jsoniter "github.com/json-iterator/go"
 	"github.com/shopspring/decimal"
 	"github.com/tealeg/xlsx"
 	"hongze/fms_api/controller/resp"
@@ -219,6 +217,24 @@ func (rg *RegisterController) Add(c *gin.Context) {
 		return
 	}
 
+	// 货币及汇率
+	rateList, e := fmsService.GetTodayCurrencyRateList()
+	if e != nil {
+		resp.FailMsg("操作失败", "获取今日货币汇率失败, Err: "+e.Error(), c)
+		return
+	}
+	var rate float64
+	for i := range rateList {
+		if req.CurrencyUnit == rateList[i].Code {
+			rate = rateList[i].RMBRate
+			break
+		}
+	}
+	if rate <= 0 {
+		resp.FailMsg("操作失败", "货币汇率信息有误", c)
+		return
+	}
+
 	nowTime := time.Now().Local()
 	ob.ContractCode = req.ContractCode
 	ob.RelateContractCode = req.RelateContractCode
@@ -231,6 +247,8 @@ func (rg *RegisterController) Add(c *gin.Context) {
 	ob.SellerName = req.SellerName
 	ob.ContractType = req.ContractType
 	ob.ContractAmount = req.ContractAmount
+	ob.CurrencyUnit = req.CurrencyUnit
+	ob.RMBRate = rate
 	ob.StartDate = startDate
 	ob.EndDate = endDate
 	ob.SignDate = signDate
@@ -350,6 +368,11 @@ func (rg *RegisterController) Edit(c *gin.Context) {
 		return
 	}
 	originHasPayment := item.HasPayment
+	// TODO:确认是否允许修改货币单位,若可修改货币单位则需要相应的处理开票到款
+	if req.CurrencyUnit != item.CurrencyUnit {
+		resp.Fail("暂不允许修改货币单位", c)
+		return
+	}
 
 	updateCols := []string{
 		"ContractCode", "RelateContractCode", "CrmContractId", "ContractSource", "CompanyName", "ActualCompanyName",
@@ -1072,7 +1095,7 @@ func (rg *RegisterController) Export(c *gin.Context) {
 	for i := range otherService {
 		row2Title = append(row2Title, otherService[i].Title)
 	}
-	row2Title = append(row2Title, "套餐备注", "开始时间", "到期时间", "2022年合同金额", "约定付款时间", "签订日", "合同状态",
+	row2Title = append(row2Title, "套餐备注", "开始时间", "到期时间", "2022年合同金额", "金额单位", "约定付款时间", "签订日", "合同状态",
 		"合同编号", "合规备注")
 
 	// 设置表头
@@ -1177,6 +1200,7 @@ func (rg *RegisterController) Export(c *gin.Context) {
 		dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.StartDate)) // 开始时间
 		dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.EndDate))   // 到期时间
 		dataRow.AddCell().SetString(fmt.Sprint("¥", v.ContractAmount))                   // 2022年合同金额
+		dataRow.AddCell().SetString(v.CurrencyUnit)                                      // 货币单位
 		dataRow.AddCell().SetString(v.AgreedPayTime)                                     // 约定付款时间
 		dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.SignDate))  // 签订日
 		dataRow.AddCell().SetString(fms.ContractStatusKeyNameMap[v.ContractStatus])      // 合同状态
@@ -1499,6 +1523,17 @@ func (rg *RegisterController) Import(c *gin.Context) {
 		serviceTempNameMap[serviceTempList[i].Title] = serviceTempList[i]
 	}
 
+	// 获取货币列表及汇率(汇率为导入日的汇率)
+	rateList, e := fmsService.GetTodayCurrencyRateList()
+	if e != nil {
+		resp.FailData("获取货币列表及汇率失败", "Err:"+e.Error(), c)
+		return
+	}
+	rateMap := make(map[string]float64)
+	for i := range rateList {
+		rateMap[rateList[i].Code] = rateList[i].RMBRate
+	}
+
 	titleMap := make(map[int]string)
 	newIds := make([]int, 0)
 	newCompanyArr := []string{"0", "1"}
@@ -1751,13 +1786,24 @@ func (rg *RegisterController) Import(c *gin.Context) {
 						rowRegister.ContractAmount = amount
 						continue
 					}
-					// 约定付款日期
+					// 金额单位
 					if k == 37 {
+						rate := rateMap[v]
+						if rate <= 0 {
+							resp.Fail(fmt.Sprintf("第%d行金额单位有误, 请按模板导入", i+1), c)
+							return
+						}
+						rowRegister.CurrencyUnit = v
+						rowRegister.RMBRate = rate
+						continue
+					}
+					// 约定付款日期
+					if k == 38 {
 						rowRegister.AgreedPayTime = v
 						continue
 					}
 					// 签订日
-					if k == 38 {
+					if k == 39 {
 						va := cell.Value
 						if va == "" {
 							continue
@@ -1779,7 +1825,7 @@ func (rg *RegisterController) Import(c *gin.Context) {
 						continue
 					}
 					// 合同状态
-					if k == 39 {
+					if k == 40 {
 						rowRegister.ContractStatus = fms.ContractStatusNameKeyMap[v]
 						if rowRegister.ContractStatus == 0 {
 							resp.Fail(fmt.Sprintf("第%d行合同状态不匹配, 请按模板导入", i+1), c)
@@ -1788,7 +1834,7 @@ func (rg *RegisterController) Import(c *gin.Context) {
 						continue
 					}
 					// 合同编号
-					if k == 40 {
+					if k == 41 {
 						rowContractCode := v
 						if rowContractCode == "" {
 							resp.Fail(fmt.Sprintf("第%d行合同编号不可为空, 请按模板导入", i+1), c)
@@ -1803,12 +1849,12 @@ func (rg *RegisterController) Import(c *gin.Context) {
 						continue
 					}
 					// 合规备注
-					if k == 41 {
+					if k == 42 {
 						rowRegister.Remark = v
 						continue
 					}
 					// 开票列表
-					k2 := 41
+					k2 := 42
 					for ir := 0; ir < invoiceMax; ir++ {
 						n := ir + 1
 						// 开票日
@@ -1998,61 +2044,10 @@ func (rg *RegisterController) Import(c *gin.Context) {
 // @Success 200 {object} fms.CurrencyUnitItem
 // @router /contract/register/currency_list [get]
 func (rg *RegisterController) CurrencyList(c *gin.Context) {
-	// 货币列表
-	ob := new(fms.CurrencyUnit)
-	cond := `enable = 1`
-	pars := make([]interface{}, 0)
-	list, e := ob.List(cond, pars)
+	list, e := fmsService.GetTodayCurrencyRateList()
 	if e != nil {
-		resp.FailData("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
+		resp.FailData("获取失败", "获取今日货币汇率列表失败, Err: "+e.Error(), c)
 		return
 	}
-
-	rateMap := make(map[string]float64)
-	rateList := make([]*fmsService.CurrencyRateItem, 0)
-
-	// 读取缓存
-	cacheJson, _ := global.Redis.Get(context.TODO(), utils.CURRENCY_RMB_RATE).Result()
-	if cacheJson != "" {
-		if e = jsoniter.UnmarshalFromString(cacheJson, &rateList); e != nil {
-			resp.FailData("获取失败", "读取汇率缓存失败, Err: "+e.Error(), c)
-			return
-		}
-	}
-
-	// 请求汇率接口
-	if cacheJson == "" {
-		rateList, e = fmsService.GetCurrencyRateList()
-		if e != nil {
-			resp.FailData("获取失败", "请求汇率接口失败, Err: "+e.Error(), c)
-			return
-		}
-		newCache, e := jsoniter.MarshalToString(rateList)
-		if e != nil {
-			resp.FailData("获取失败", "写入汇率缓存失败, Err: "+e.Error(), c)
-			return
-		}
-		now := time.Now()
-		todayLast := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
-		sub := todayLast.Sub(now)
-		_ = global.Redis.SetEX(context.TODO(), utils.CURRENCY_RMB_RATE, newCache, sub)
-	}
-	for i := range rateList {
-		rateMap[rateList[i].Code] = rateList[i].Rate
-	}
-
-	respList := make([]*fms.CurrencyUnitItem, 0)
-	for i := range list {
-		r := rateMap[list[i].Code]
-		if list[i].Code == fms.BaseCurrencyCode {
-			r = 1
-		}
-		respList = append(respList, &fms.CurrencyUnitItem{
-			Name:    list[i].Name,
-			Code:    list[i].Code,
-			Enable:  list[i].Enable,
-			RMBRate: r,
-		})
-	}
-	resp.OkData("获取成功", respList, c)
+	resp.OkData("获取成功", list, c)
 }

+ 7 - 0
models/fms/contract_register.go

@@ -23,6 +23,8 @@ type ContractRegister struct {
 	ContractAmount     float64   `gorm:"column:contract_amount" json:"contract_amount" description:"合同金额"`
 	InvoicedAmount     float64   `gorm:"column:invoiced_amount" json:"invoiced_amount" description:"开票金额"`
 	PaymentAmount      float64   `gorm:"column:payment_amount" json:"payment_amount" description:"到款金额"`
+	CurrencyUnit       string    `gorm:"currency_unit" json:"currency_unit" description:"货币国际代码"`
+	RMBRate            float64   `gorm:"rmb_rate" json:"rmb_rate" description:"人民币汇率(create_time当日)"`
 	StartDate          time.Time `gorm:"column:start_date" json:"start_date" description:"合同开始日期"`
 	EndDate            time.Time `gorm:"column:end_date" json:"end_date" description:"合同结束日期"`
 	SignDate           time.Time `gorm:"column:sign_date" json:"sign_date" description:"合同签订日期"`
@@ -117,6 +119,8 @@ type ContractRegisterItem struct {
 	ContractAmount     float64 `json:"contract_amount" description:"合同金额"`
 	InvoicedAmount     float64 `json:"invoiced_amount" description:"开票金额"`
 	PaymentAmount      float64 `json:"payment_amount" description:"到款金额"`
+	CurrencyUnit       string  `json:"currency_unit" description:"货币国际代码"`
+	RMBRate            float64 `json:"rmb_rate" description:"人民币汇率(create_time当日)"`
 	StartDate          string  `json:"start_date" description:"合同开始日期"`
 	EndDate            string  `json:"end_date" description:"合同结束日期"`
 	SignDate           string  `json:"sign_date" description:"合同签订日期"`
@@ -161,6 +165,7 @@ type ContractRegisterAddReq struct {
 	SellerName         string                  `json:"seller_name" binding:"required" description:"CRM系统-销售名称"`
 	ContractType       int                     `json:"contract_type" binding:"oneof=1 2 3 4" description:"合同类型: 1-新签; 2-续约; 3-代付; 4-补充协议"`
 	ContractAmount     float64                 `json:"contract_amount" binding:"required" description:"合同金额"`
+	CurrencyUnit       string                  `json:"currency_unit" binding:"required" description:"货币单位"`
 	StartDate          string                  `json:"start_date" binding:"required" description:"合同开始日期"`
 	EndDate            string                  `json:"end_date" binding:"required" description:"合同结束日期"`
 	SignDate           string                  `json:"sign_date" description:"合同签订日期"`
@@ -345,6 +350,8 @@ func formatContractRegister2Item(item *ContractRegister) (formatItem *ContractRe
 	formatItem.ContractAmount = item.ContractAmount
 	formatItem.InvoicedAmount = item.InvoicedAmount
 	formatItem.PaymentAmount = item.PaymentAmount
+	formatItem.CurrencyUnit = item.CurrencyUnit
+	formatItem.RMBRate = item.RMBRate
 	formatItem.StartDate = utils.TimeTransferString(utils.FormatDate, item.StartDate)
 	formatItem.EndDate = utils.TimeTransferString(utils.FormatDate, item.EndDate)
 	formatItem.SignDate = utils.TimeTransferString(utils.FormatDate, item.SignDate)

+ 67 - 1
services/fms/currency_rate.go

@@ -1,8 +1,12 @@
 package fms
 
 import (
+	"context"
 	"encoding/json"
 	"fmt"
+	jsoniter "github.com/json-iterator/go"
+	"hongze/fms_api/global"
+	"hongze/fms_api/models/fms"
 	"hongze/fms_api/utils"
 	"strconv"
 	"time"
@@ -35,7 +39,8 @@ type CurrencyRateItem struct {
 	UpdateTime time.Time `json:"update_time"`
 }
 
-func GetCurrencyRateList() (resList []*CurrencyRateItem, err error) {
+// CurlCurrencyRateApi 请求货币汇率接口
+func CurlCurrencyRateApi() (resList []*CurrencyRateItem, err error) {
 	defer func() {
 		if err != nil {
 			fmt.Println("请求汇率接口失败, Err: " + err.Error())
@@ -98,3 +103,64 @@ func GetCurrencyRateList() (resList []*CurrencyRateItem, err error) {
 	}
 	return
 }
+
+// GetTodayCurrencyRateList 获取今日货币及汇率列表
+func GetTodayCurrencyRateList() (list []*fms.CurrencyUnitItem, err error) {
+	// 货币列表
+	ob := new(fms.CurrencyUnit)
+	cond := `enable = 1`
+	pars := make([]interface{}, 0)
+	currencyList, e := ob.List(cond, pars)
+	if e != nil {
+		err = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
+		return
+	}
+
+	rateMap := make(map[string]float64)
+	rateList := make([]*CurrencyRateItem, 0)
+
+	// 读取缓存
+	cacheJson, _ := global.Redis.Get(context.TODO(), utils.CURRENCY_RMB_RATE).Result()
+	if cacheJson != "" {
+		if e = jsoniter.UnmarshalFromString(cacheJson, &rateList); e != nil {
+			err = fmt.Errorf("解析汇率缓存数据失败, Err: %s", e.Error())
+			return
+		}
+	}
+
+	// 请求汇率接口
+	if cacheJson == "" {
+		rateList, e = CurlCurrencyRateApi()
+		if e != nil {
+			err = fmt.Errorf("请求汇率接口失败, Err: %s", e.Error())
+			return
+		}
+		newCache, e := jsoniter.MarshalToString(rateList)
+		if e != nil {
+			err = fmt.Errorf("写入汇率缓存失败, Err: %s", e.Error())
+			return
+		}
+		now := time.Now()
+		todayLast := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
+		sub := todayLast.Sub(now)
+		_ = global.Redis.SetEX(context.TODO(), utils.CURRENCY_RMB_RATE, newCache, sub)
+	}
+	for i := range rateList {
+		rateMap[rateList[i].Code] = rateList[i].Rate
+	}
+
+	respList := make([]*fms.CurrencyUnitItem, 0)
+	for i := range currencyList {
+		r := rateMap[currencyList[i].Code]
+		if currencyList[i].Code == fms.BaseCurrencyCode {
+			r = 1
+		}
+		respList = append(respList, &fms.CurrencyUnitItem{
+			Name:    currencyList[i].Name,
+			Code:    currencyList[i].Code,
+			Enable:  currencyList[i].Enable,
+			RMBRate: r,
+		})
+	}
+	return
+}