浏览代码

汇率接口调试; 开票到款列表货币统计;

hsun 2 年之前
父节点
当前提交
6f0085b149

+ 2 - 2
controller/census/seller.go

@@ -148,7 +148,7 @@ func ExportGroupInvoiceList(c *gin.Context, list []*fms.CensusSellerGroupInvoice
 	}
 
 	// 数据表头
-	rowTitle := []string{"排名", "销售组别", "开票金额", "组别占比"}
+	rowTitle := []string{"排名", "销售组别", "收入金额(元)", "组别占比"}
 	titleRow := sheet.AddRow()
 	for i := range rowTitle {
 		v := titleRow.AddCell()
@@ -346,7 +346,7 @@ func ExportInvoiceList(c *gin.Context, list []*fms.CensusSellerInvoiceItem, req
 	}
 
 	// 数据表头
-	rowTitle := []string{"排名", "销售员", "销售组别", "开票金额", "小组占比", "全员占比"}
+	rowTitle := []string{"排名", "销售员", "销售组别", "收入金额(元)", "小组占比", "全员占比"}
 	titleRow := sheet.AddRow()
 	for i := range rowTitle {
 		v := titleRow.AddCell()

+ 57 - 19
controller/contract/register.go

@@ -701,12 +701,13 @@ func (rg *RegisterController) Invoice(c *gin.Context) {
 		resp.Fail("合同存在代付不允许添加开票/到款登记", c)
 		return
 	}
-	// 合同有效时长
-	dayDiff := item.EndDate.Sub(item.StartDate).Hours() / 24
 
 	noChangeInvoiceIds := make([]int, 0)
 	newInvoice := make([]*fms.ContractInvoice, 0)
 	if len(req.AmountList) > 0 {
+		// 合同有效时长(计算付款方式)
+		dayDiff := item.EndDate.Sub(item.StartDate).Hours() / 24
+
 		// 获取销售分组信息
 		sellerList, e := crmService.GetSellerDepartmentListWithGroupAndTeam()
 		if e != nil {
@@ -739,12 +740,15 @@ func (rg *RegisterController) Invoice(c *gin.Context) {
 			}
 			if req.AmountList[i].InvoiceId > 0 {
 				noChangeInvoiceIds = append(noChangeInvoiceIds, req.AmountList[i].InvoiceId)
+				continue
 			}
 			if req.AmountList[i].InvoiceId == 0 {
 				v := &fms.ContractInvoice{
 					ContractRegisterId: req.ContractRegisterId,
 					ContractCode:       item.ContractCode,
 					Amount:             req.AmountList[i].Amount,
+					OriginAmount:       req.AmountList[i].OriginAmount,
+					CurrencyUnit:       item.CurrencyUnit,
 					InvoiceType:        req.InvoiceType,
 					InvoiceDate:        t,
 					AdminId:            int(adminInfo.AdminId),
@@ -1293,8 +1297,13 @@ func (rg *RegisterController) InvoiceList(c *gin.Context) {
 	// 合同编号
 	if req.ContractCode != "" {
 		kw := fmt.Sprint("%", req.ContractCode, "%")
-		cond += ` AND contract_code LIKE ?`
 		pars = append(pars, kw)
+		// 开票列表同时模糊查询销售名称
+		if req.InvoiceType == fms.ContractInvoiceTypeMake {
+			cond += ` AND (contract_code LIKE ? OR seller_name LIKE ?)`
+		} else {
+			cond += ` AND contract_code LIKE ?`
+		}
 	}
 	if req.StartDate != "" && req.EndDate != "" {
 		st := fmt.Sprint(req.StartDate, " 00:00:00")
@@ -1311,6 +1320,27 @@ func (rg *RegisterController) InvoiceList(c *gin.Context) {
 		pars = append(pars, req.MaxAmount)
 	}
 
+	// 货币列表
+	currencyOB := new(fms.CurrencyUnit)
+	currencyCond := `enable = 1`
+	currencyPars := make([]interface{}, 0)
+	currencyList, e := currencyOB.List(currencyCond, currencyPars)
+	if e != nil {
+		resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
+		return
+	}
+	unitMap := make(map[string]string)
+	currencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
+	for i := range currencyList {
+		unitMap[currencyList[i].Code] = currencyList[i].UnitName
+		currencyTotals = append(currencyTotals, &fms.InvoiceListCurrencyTotal{
+			Name:     currencyList[i].Name,
+			UnitName: currencyList[i].UnitName,
+			Code:     currencyList[i].Code,
+			FlagImg:  currencyList[i].FlagImg,
+		})
+	}
+
 	page := new(base.Page)
 	page.SetPageSize(pageSize)
 	page.SetCurrent(pageIndex)
@@ -1321,25 +1351,33 @@ func (rg *RegisterController) InvoiceList(c *gin.Context) {
 		return
 	}
 	page.SetTotal(total)
+	for i := range list {
+		list[i].UnitName = unitMap[list[i].CurrencyUnit]
+	}
 
-	// 金额合计
-	ob := new(fms.ContractInvoice)
-	amountTotal, e := ob.Sum("amount", cond, pars)
+	// 分币种合计金额
+	var amountTotal float64
+	sumList, e := fms.GetInvoiceListCurrencySum(cond, pars)
 	if e != nil {
-		resp.FailMsg("获取失败", "获取合同开票/到款列表合计金额失败, Err: "+e.Error(), c)
+		resp.FailMsg("获取失败", "获取开票/到款列表合计金额失败, Err: "+e.Error(), c)
 		return
 	}
-	amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountTotal), 64)
-
-	type RespData struct {
-		Page        *base.Page  `json:"page"`
-		List        interface{} `json:"list"`
-		AmountTotal float64     `json:"amount_total"`
+	sumMap := make(map[string]float64)
+	for i := range sumList {
+		if sumList[i].CurrencyUnit == fms.BaseCurrencyCode {
+			amountTotal = sumList[i].AmountTotal
+		}
+		sumMap[sumList[i].CurrencyUnit] = sumList[i].OriginAmountTotal
 	}
-	respData := &RespData{
-		Page:        page,
-		List:        list,
-		AmountTotal: amountTotal,
+	for i := range currencyTotals {
+		currencyTotals[i].Amount = sumMap[currencyTotals[i].Code]
+	}
+
+	respData := &fms.InvoiceListRespData{
+		Page:          page,
+		List:          list,
+		AmountTotal:   amountTotal,
+		CurrencyTotal: currencyTotals,
 	}
 	resp.OkData("获取成功", respData, c)
 }
@@ -1432,10 +1470,10 @@ func (rg *RegisterController) InvoiceExport(c *gin.Context) {
 	cell1.SetString("合同编号")
 	cell1.SetStyle(style)
 	cell2 := titleRow.AddCell()
-	cell2.SetValue(fmt.Sprintf("%s金额", listName))
+	cell2.SetString(fmt.Sprintf("%s金额", listName))
 	cell2.SetStyle(style)
 	cell3 := titleRow.AddCell()
-	cell3.SetValue(fmt.Sprintf("%s日期", listName))
+	cell3.SetString(fmt.Sprintf("%s日期", listName))
 	cell3.SetStyle(style)
 
 	for _, v := range list {

+ 53 - 8
models/fms/contract_invoice.go

@@ -13,7 +13,9 @@ type ContractInvoice struct {
 	ContractInvoiceId  int       `gorm:"primaryKey;column:contract_invoice_id" json:"contract_invoice_id" description:"开票ID"`
 	ContractRegisterId int       `gorm:"column:contract_register_id" json:"contract_register_id" description:"登记ID"`
 	ContractCode       string    `gorm:"column:contract_code" json:"contract_code" description:"合同编号"`
-	Amount             float64   `gorm:"column:amount" json:"amount" description:"金额"`
+	Amount             float64   `gorm:"column:amount" json:"amount" description:"换算后的金额(人民币)"`
+	OriginAmount       float64   `gorm:"column:origin_amount" json:"origin_amount" description:"开票/到款金额"`
+	CurrencyUnit       string    `gorm:"column:currency_unit" json:"currency_unit" description:"货币国际代码"`
 	InvoiceType        int       `gorm:"column:invoice_type" json:"invoice_type" description:"类型: 1-开票登记; 2-到款登记"`
 	InvoiceDate        time.Time `gorm:"column:invoice_time" json:"invoice_time" description:"开票日期/到款月"`
 	SellerId           int       `gorm:"column:seller_id" json:"seller_id" description:"销售ID"`
@@ -39,7 +41,10 @@ type ContractInvoiceItem struct {
 	ContractInvoiceId  int     `gorm:"column:contract_invoice_id" json:"contract_invoice_id" description:"开票ID"`
 	ContractRegisterId int     `gorm:"column:contract_register_id" json:"contract_register_id" description:"登记ID"`
 	ContractCode       string  `gorm:"column:contract_code" json:"contract_code" description:"合同编号"`
-	Amount             float64 `gorm:"column:amount" json:"amount" description:"金额"`
+	Amount             float64 `gorm:"column:amount" json:"amount" description:"换算金额"`
+	OriginAmount       float64 `gorm:"column:origin_amount" json:"origin_amount" description:"开票/到款金额"`
+	CurrencyUnit       string  `gorm:"column:currency_unit" json:"currency_unit" description:"货币国际代码"`
+	UnitName           string  `json:"unit_name" description:"货币单位名称"`
 	InvoiceType        int     `gorm:"column:invoice_type" json:"invoice_type" description:"类型: 1-开票登记; 2-到款登记"`
 	InvoiceDate        string  `gorm:"column:invoice_time" json:"invoice_time" description:"开票日期/到款月"`
 	SellerId           int     `gorm:"column:seller_id" json:"seller_id" description:"销售ID"`
@@ -143,13 +148,14 @@ type ContractInvoiceSaveReq struct {
 	AmountList         []*ContractInvoiceSaveItem `json:"amount_list"`
 }
 
-// ContractInvoiceSaveReq 合同开票数据
+// ContractInvoiceSaveItem 合同开票数据
 type ContractInvoiceSaveItem struct {
-	InvoiceId   int     `json:"invoice_id" description:"开票ID"`
-	Amount      float64 `json:"amount" description:"开票金额/到款金额"`
-	InvoiceDate string  `json:"invoice_date" description:"开票日期/到款月"`
-	Remark      string  `json:"remark" description:"备注"`
-	SellerId    int     `json:"seller_id" description:"销售ID"`
+	InvoiceId    int     `json:"invoice_id" description:"开票ID"`
+	OriginAmount float64 `json:"origin_amount" description:"开票(到款)金额"`
+	Amount       float64 `json:"amount" description:"换算后的金额"`
+	InvoiceDate  string  `json:"invoice_date" description:"开票日期/到款月"`
+	Remark       string  `json:"remark" description:"备注"`
+	SellerId     int     `json:"seller_id" description:"销售ID"`
 }
 
 // GetContractInvoiceItemList 获取开票到款列表
@@ -176,6 +182,8 @@ func formatContractInvoice2ItemList(list []*ContractInvoice) (itemList []*Contra
 			ContractRegisterId: list[i].ContractRegisterId,
 			ContractCode:       list[i].ContractCode,
 			Amount:             list[i].Amount,
+			OriginAmount:       list[i].OriginAmount,
+			CurrencyUnit:       list[i].CurrencyUnit,
 			InvoiceType:        list[i].InvoiceType,
 			InvoiceDate:        utils.TimeTransferString(utils.FormatDate, list[i].InvoiceDate),
 			SellerId:           list[i].SellerId,
@@ -226,6 +234,8 @@ func formatContractInvoice2Item(item *ContractInvoice) (formatItem *ContractInvo
 	formatItem.ContractRegisterId = item.ContractRegisterId
 	formatItem.ContractCode = item.ContractCode
 	formatItem.Amount = item.Amount
+	formatItem.OriginAmount = item.OriginAmount
+	formatItem.CurrencyUnit = item.CurrencyUnit
 	formatItem.InvoiceType = item.InvoiceType
 	formatItem.InvoiceDate = utils.TimeTransferString(utils.FormatDate, item.InvoiceDate)
 	formatItem.Remark = item.Remark
@@ -425,3 +435,38 @@ func GetCensusSellerInvoicePageList(page base.IPage, condition, outCond string,
 	err = global.DEFAULT_MYSQL.Raw(sql, pars...).Scan(&results).Error
 	return
 }
+
+// InvoiceListRespData 开票/到款列表响应体
+type InvoiceListRespData struct {
+	Page          *base.Page                  `json:"page"`
+	List          interface{}                 `json:"list"`
+	AmountTotal   float64                     `json:"amount_total"`
+	CurrencyTotal []*InvoiceListCurrencyTotal `json:"currency_total"`
+}
+
+// InvoiceListCurrencyTotal 开票/到款列表分币种合计信息
+type InvoiceListCurrencyTotal struct {
+	Name     string  `json:"name" description:"货币名称"`
+	UnitName string  `json:"unit_name" description:"单位名称"`
+	Code     string  `json:"code" description:"国际代码"`
+	Amount   float64 `json:"amount" description:"金额"`
+	FlagImg  string  `json:"flag_img" description:"国旗图标"`
+}
+
+// InvoiceListCurrencySum 开票/到款列表分币种总和
+type InvoiceListCurrencySum struct {
+	CurrencyUnit      string  `json:"currency_unit" description:"货币代码"`
+	AmountTotal       float64 `json:"amount_total" description:"换算后合计金额"`
+	OriginAmountTotal float64 `json:"origin_amount_total" description:"原合计金额"`
+}
+
+// GetInvoiceListCurrencySum 获取开票/到款分货币合计
+func GetInvoiceListCurrencySum(condition string, pars []interface{}) (results []*InvoiceListCurrencySum, err error) {
+	query := global.DEFAULT_MYSQL.Table("contract_invoice").
+		Select("currency_unit, SUM(amount) AS amount_total, SUM(origin_amount) AS origin_amount_total").
+		Where("is_deleted = 0").
+		Where(condition, pars...).
+		Group("currency_unit")
+	err = query.Find(&results).Error
+	return
+}

+ 2 - 2
models/fms/contract_register.go

@@ -23,8 +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当日)"`
+	CurrencyUnit       string    `gorm:"column:currency_unit" json:"currency_unit" description:"货币国际代码"`
+	RMBRate            float64   `gorm:"column: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:"合同签订日期"`

+ 8 - 4
models/fms/currency_unit.go

@@ -6,8 +6,10 @@ import "hongze/fms_api/global"
 type CurrencyUnit struct {
 	CurrencyUnitId int    `gorm:"primaryKey;column:currency_unit_id" json:"-" description:"货币ID"`
 	Name           string `gorm:"column:name" json:"name" description:"货币名称"`
+	UnitName       string `gorm:"unit_name" json:"unit_name" description:"单位名称"`
 	Code           string `gorm:"code" json:"code" description:"国际代码"`
 	Enable         int    `gorm:"enable" json:"enable" description:"状态: 0-禁用; 1-启用"`
+	FlagImg        string `gorm:"flag_img" json:"flag_img" description:"国旗图片"`
 }
 
 func (m *CurrencyUnit) TableName() string {
@@ -24,8 +26,10 @@ func (m *CurrencyUnit) List(condition string, pars []interface{}) (list []*Curre
 }
 
 type CurrencyUnitItem struct {
-	Name    string  `json:"name" description:"货币名称"`
-	Code    string  `json:"code" description:"国际代码"`
-	Enable  int     `json:"enable" description:"状态: 0-禁用; 1-启用"`
-	RMBRate float64 `json:"rmb_rate" description:"当日对人民币汇率"`
+	Name     string  `json:"name" description:"货币名称"`
+	UnitName string  `json:"unit_name" description:"单位名称"`
+	Code     string  `json:"code" description:"国际代码"`
+	Enable   int     `json:"enable" description:"状态: 0-禁用; 1-启用"`
+	RMBRate  float64 `json:"rmb_rate" description:"当日对人民币汇率"`
+	FlagImg  string  `json:"flag_img" description:"国旗图片"`
 }

+ 24 - 21
services/fms/currency_rate.go

@@ -7,6 +7,7 @@ import (
 	jsoniter "github.com/json-iterator/go"
 	"hongze/fms_api/global"
 	"hongze/fms_api/models/fms"
+	"hongze/fms_api/services/alarm_msg"
 	"hongze/fms_api/utils"
 	"strconv"
 	"time"
@@ -15,11 +16,13 @@ import (
 const (
 	CurrencyRateApiHost        = "https://jisuhuilv.market.alicloudapi.com"
 	CurrencyRateExchangeSingle = "/exchange/single"
-	CurrencyRateApiAppCode     = "ABC123" // TODO:APPCODE
+	CurrencyRateApiAppCode     = "22553c4ba74545568aba70ac6cfd441d"
+	CurrencyRateApiAppKey      = "203889624"
+	CurrencyRateApiAppSecret   = "voampGGl0yGNx5xA1sZdmZlV2EiBct2P"
 )
 
 type CurrencyRateApiResp struct {
-	Status string `json:"status"`
+	Status int    `json:"status"`
 	Msg    string `json:"msg"`
 	Result struct {
 		Currency string `json:"currency"`
@@ -43,18 +46,15 @@ type CurrencyRateItem struct {
 func CurlCurrencyRateApi() (resList []*CurrencyRateItem, err error) {
 	defer func() {
 		if err != nil {
-			fmt.Println("请求汇率接口失败, Err: " + err.Error())
-			//go alarm_msg.SendAlarmMsg("请求汇率接口失败, Err: "+err.Error(), 3)
+			go alarm_msg.SendAlarmMsg("请求汇率接口失败, Err: "+err.Error(), 3)
 		}
 	}()
 
-	// TODO:请求汇率接口
+	// 请求汇率接口
 	query := "?currency=CNY"
 	reqUrl := fmt.Sprint(CurrencyRateApiHost, CurrencyRateExchangeSingle, query)
 	auth := fmt.Sprintf("APPCODE %s", CurrencyRateApiAppCode)
 	resultByte, e := utils.PublicGetRequest(reqUrl, auth)
-	fmt.Println("reqUrl: ", reqUrl)
-	fmt.Println("PublicGetRequest err: ", e)
 	if e != nil {
 		err = fmt.Errorf("HttpCurrencyRateApi errMsg: %s", e.Error())
 		return
@@ -65,15 +65,12 @@ func CurlCurrencyRateApi() (resList []*CurrencyRateItem, err error) {
 	}
 
 	// 汇率结果
-	//resultJson := `{"status":"0","msg":"ok","result":{"currency":"CNY","name":"人民币","list":{"HKD":{"name":"港币","rate":"1.2198","updatetime":"2015-10-26 16:56:22"},"USD":{"name":"美元","rate":"0.1574","updatetime":"2015-10-26 16:56:22"},"EUR":{"name":"欧元","rate":"0.1426","updatetime":"2015-10-26 16:56:22"}}}}`
 	results := new(CurrencyRateApiResp)
-	fmt.Println("result JSON: ", string(resultByte))
 	if e := json.Unmarshal(resultByte, &results); e != nil {
-		//if e := json.Unmarshal([]byte(resultJson), &results); e != nil {
-		err = fmt.Errorf("rate api result unmarshal err: %s", results.Msg)
+		err = fmt.Errorf("rate api result unmarshal err: %s", e.Error())
 		return
 	}
-	if results.Status != "0" {
+	if results.Status != 0 {
 		err = fmt.Errorf("curl rate api errMsg: %s", results.Msg)
 		return
 	}
@@ -106,6 +103,7 @@ func CurlCurrencyRateApi() (resList []*CurrencyRateItem, err error) {
 
 // GetTodayCurrencyRateList 获取今日货币及汇率列表
 func GetTodayCurrencyRateList() (list []*fms.CurrencyUnitItem, err error) {
+	list = make([]*fms.CurrencyUnitItem, 0)
 	// 货币列表
 	ob := new(fms.CurrencyUnit)
 	cond := `enable = 1`
@@ -120,7 +118,7 @@ func GetTodayCurrencyRateList() (list []*fms.CurrencyUnitItem, err error) {
 	rateList := make([]*CurrencyRateItem, 0)
 
 	// 读取缓存
-	cacheJson, _ := global.Redis.Get(context.TODO(), utils.CURRENCY_RMB_RATE).Result()
+	cacheJson, _ := global.Redis.Get(context.TODO(), utils.CACHE_KEY_CURRENCY_RMB_RATE).Result()
 	if cacheJson != "" {
 		if e = jsoniter.UnmarshalFromString(cacheJson, &rateList); e != nil {
 			err = fmt.Errorf("解析汇率缓存数据失败, Err: %s", e.Error())
@@ -135,6 +133,10 @@ func GetTodayCurrencyRateList() (list []*fms.CurrencyUnitItem, err error) {
 			err = fmt.Errorf("请求汇率接口失败, Err: %s", e.Error())
 			return
 		}
+		if len(rateList) == 0 {
+			err = fmt.Errorf("汇率接口返回数据为空")
+			return
+		}
 		newCache, e := jsoniter.MarshalToString(rateList)
 		if e != nil {
 			err = fmt.Errorf("写入汇率缓存失败, Err: %s", e.Error())
@@ -143,23 +145,24 @@ func GetTodayCurrencyRateList() (list []*fms.CurrencyUnitItem, err error) {
 		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)
+		_ = global.Redis.SetEX(context.TODO(), utils.CACHE_KEY_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,
+		list = append(list, &fms.CurrencyUnitItem{
+			Name:     currencyList[i].Name,
+			UnitName: currencyList[i].UnitName,
+			Code:     currencyList[i].Code,
+			Enable:   currencyList[i].Enable,
+			RMBRate:  r,
+			FlagImg:  currencyList[i].FlagImg,
 		})
 	}
 	return

+ 1 - 1
utils/constants.go

@@ -34,7 +34,7 @@ const (
 	SYSTEM_LOGIN_TOKEN          = "fms:login:token:"
 	SYSTEM_LOGIN_TOKEN_NO_TRUST = "fms:login:no_trust:" //管理后台登录(不可信登录态)
 	SYSTEM_LOGIN_ADMINID_IP     = "fms:login:admin_id:"
-	CURRENCY_RMB_RATE           = "fms:currency:rmb:rate" // 汇率接口
+	CACHE_KEY_CURRENCY_RMB_RATE = "fms:currency:rmb:rate" // 汇率接口
 )
 
 // OSS