647 lines
21 KiB
Go
647 lines
21 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"server/models"
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
// BackendCrmPaybackController CRM 回款管理
|
|
//
|
|
// 回款计划针对于合同创建:
|
|
// - plan_type 回款周期:1月度/2季度/3年度/4进度/5自定义;
|
|
// - 月度/季度/年度:明细为「期数 + 支付金额」;
|
|
// - 进度:明细为「进度名称 + 进度百分比 + 进度金额」;
|
|
// - 自定义:明细为「名称 + 计划日期 + 金额」;
|
|
// - 回款进度:按计划明细逐期登记实收金额/日期。
|
|
type BackendCrmPaybackController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
// paybackItem 计划明细行(items JSON 元素)。
|
|
type paybackItem struct {
|
|
Seq int `json:"seq"`
|
|
Name string `json:"name"`
|
|
Percent float64 `json:"percent"`
|
|
Amount float64 `json:"amount"`
|
|
PlanDate string `json:"plan_date"`
|
|
ReceivedAmount float64 `json:"received_amount"`
|
|
ReceivedDate string `json:"received_date"`
|
|
ReceiptURL string `json:"receipt_url"` // 流水回执地址(选填)
|
|
ReceiptName string `json:"receipt_name"` // 流水回执文件名(选填)
|
|
Status int8 `json:"status"` // 1未回款/2部分回款/3已回款
|
|
}
|
|
|
|
// paybackPayload 创建 / 更新请求体。
|
|
type paybackPayload struct {
|
|
ContractID uint64 `json:"contract_id"`
|
|
ContractNo string `json:"contract_no"`
|
|
ContractName string `json:"contract_name"`
|
|
CustomerID uint64 `json:"customer_id"`
|
|
CustomerName string `json:"customer_name"`
|
|
PlanType int8 `json:"plan_type"`
|
|
PayMethod int8 `json:"pay_method"`
|
|
RemindDays int `json:"remind_days"`
|
|
OwnerUserID string `json:"owner_user_id"`
|
|
OwnerUserName string `json:"owner_user_name"`
|
|
ContractAmount float64 `json:"contract_amount"`
|
|
Items json.RawMessage `json:"items"`
|
|
Remark string `json:"remark"`
|
|
Status int8 `json:"status"`
|
|
ReceiptURL string `json:"receipt_url"` // 流水回执地址(选填)
|
|
ReceiptName string `json:"receipt_name"` // 流水回执文件名(选填)
|
|
}
|
|
|
|
// paybackResp 列表 / 详情响应:items 解析为 JSON 数组透出。
|
|
type paybackResp struct {
|
|
models.TenantCrmPayback
|
|
Items json.RawMessage `json:"items"`
|
|
}
|
|
|
|
// paybackProgress 回款进度行(跨计划摊平的计划明细)。
|
|
type paybackProgress struct {
|
|
PaybackID uint64 `json:"payback_id"`
|
|
ContractID *uint64 `json:"contract_id"`
|
|
ContractNo string `json:"contract_no"`
|
|
ContractName string `json:"contract_name"`
|
|
CustomerName string `json:"customer_name"`
|
|
OwnerUserName string `json:"owner_user_name"`
|
|
PlanType int8 `json:"plan_type"`
|
|
PayMethod int8 `json:"pay_method"`
|
|
Seq int `json:"seq"`
|
|
Name string `json:"name"`
|
|
Percent float64 `json:"percent"`
|
|
Amount float64 `json:"amount"`
|
|
PlanDate string `json:"plan_date"`
|
|
ReceivedAmount float64 `json:"received_amount"`
|
|
ReceivedDate string `json:"received_date"`
|
|
Status int8 `json:"status"`
|
|
}
|
|
|
|
// List GET /backend/crm/payback/list
|
|
func (c *BackendCrmPaybackController) List() {
|
|
claims, err := pipelineClaims(&c.Controller)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 401, 401, err.Error())
|
|
return
|
|
}
|
|
page, _ := c.GetInt("page", 1)
|
|
pageSize, _ := c.GetInt("pageSize", 20)
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if pageSize < 1 || pageSize > 100 {
|
|
pageSize = 20
|
|
}
|
|
keyword := strings.TrimSpace(c.GetString("keyword"))
|
|
planType := strings.TrimSpace(c.GetString("plan_type"))
|
|
payMethod := strings.TrimSpace(c.GetString("pay_method"))
|
|
status := strings.TrimSpace(c.GetString("status"))
|
|
contractID := strings.TrimSpace(c.GetString("contract_id"))
|
|
|
|
tenantID := pipelineTenantID(claims)
|
|
cond := orm.NewCondition().And("tenant_id", tenantID).And("delete_time__isnull", true)
|
|
if keyword != "" {
|
|
cond = cond.AndCond(orm.NewCondition().
|
|
Or("contract_name__contains", keyword).
|
|
Or("contract_no__contains", keyword).
|
|
Or("customer_name__contains", keyword).
|
|
Or("owner_user_name__contains", keyword))
|
|
}
|
|
if planType != "" {
|
|
cond = cond.And("plan_type", planType)
|
|
}
|
|
if payMethod != "" {
|
|
cond = cond.And("pay_method", payMethod)
|
|
}
|
|
if status != "" {
|
|
cond = cond.And("status", status)
|
|
}
|
|
if contractID != "" {
|
|
if cid, err := strconv.ParseUint(contractID, 10, 64); err == nil && cid > 0 {
|
|
cond = cond.And("contract_id", cid)
|
|
}
|
|
}
|
|
qs := models.Orm.QueryTable(new(models.TenantCrmPayback)).SetCond(cond)
|
|
|
|
total, _ := qs.Count()
|
|
var list []models.TenantCrmPayback
|
|
if total > 0 {
|
|
_, _ = qs.OrderBy("-id").Offset((page - 1) * pageSize).Limit(pageSize).All(&list)
|
|
}
|
|
items := make([]paybackResp, 0, len(list))
|
|
for i := range list {
|
|
items = append(items, buildPaybackResp(&list[i]))
|
|
}
|
|
|
|
// 汇总(当前筛选条件下全部计划的计划总额 / 已回款 / 待回款),用于列表页统计卡片
|
|
var all []models.TenantCrmPayback
|
|
_, _ = models.Orm.QueryTable(new(models.TenantCrmPayback)).SetCond(cond).All(&all)
|
|
var summaryTotal, summaryReceived float64
|
|
for i := range all {
|
|
summaryTotal = round2(summaryTotal + all[i].TotalAmount)
|
|
summaryReceived = round2(summaryReceived + all[i].ReceivedAmount)
|
|
}
|
|
pipelineOk(&c.Controller, map[string]interface{}{
|
|
"list": items, "total": total, "page": page, "pageSize": pageSize,
|
|
"summary": map[string]interface{}{
|
|
"total_amount": summaryTotal,
|
|
"received_amount": summaryReceived,
|
|
"pending_amount": round2(summaryTotal - summaryReceived),
|
|
},
|
|
})
|
|
}
|
|
|
|
// Detail GET /backend/crm/payback/:id
|
|
func (c *BackendCrmPaybackController) Detail() {
|
|
claims, err := pipelineClaims(&c.Controller)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 401, 401, err.Error())
|
|
return
|
|
}
|
|
id, _ := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if id == 0 {
|
|
pipelineErr(&c.Controller, 400, 400, "无效的ID")
|
|
return
|
|
}
|
|
var row models.TenantCrmPayback
|
|
if err := models.Orm.QueryTable(new(models.TenantCrmPayback)).
|
|
Filter("id", id).Filter("tenant_id", pipelineTenantID(claims)).
|
|
Filter("delete_time__isnull", true).One(&row); err != nil {
|
|
pipelineErr(&c.Controller, 404, 404, "回款计划未找到")
|
|
return
|
|
}
|
|
pipelineOk(&c.Controller, buildPaybackResp(&row))
|
|
}
|
|
|
|
// Create POST /backend/crm/payback
|
|
func (c *BackendCrmPaybackController) Create() {
|
|
claims, err := pipelineClaims(&c.Controller)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 401, 401, err.Error())
|
|
return
|
|
}
|
|
raw, _ := io.ReadAll(c.Ctx.Request.Body)
|
|
var p paybackPayload
|
|
if err := json.Unmarshal(raw, &p); err != nil {
|
|
pipelineErr(&c.Controller, 400, 400, "参数错误")
|
|
return
|
|
}
|
|
if p.ContractID == 0 {
|
|
pipelineErr(&c.Controller, 400, 400, "请先选择关联合同")
|
|
return
|
|
}
|
|
tenantID := pipelineTenantID(claims)
|
|
|
|
itemsJSON, totalAmount, err := normalizePaybackItems(p.Items)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 400, 400, "计划明细格式错误")
|
|
return
|
|
}
|
|
if totalAmount <= 0 {
|
|
pipelineErr(&c.Controller, 400, 400, "计划回款总额不能为 0")
|
|
return
|
|
}
|
|
|
|
now := time.Now()
|
|
row := models.TenantCrmPayback{
|
|
TenantID: tenantID,
|
|
ContractNo: strings.TrimSpace(p.ContractNo),
|
|
ContractName: strings.TrimSpace(p.ContractName),
|
|
CustomerName: strings.TrimSpace(p.CustomerName),
|
|
PlanType: pickInt8(p.PlanType, 1, 5),
|
|
PayMethod: pickInt8(p.PayMethod, 1, 7),
|
|
RemindDays: p.RemindDays,
|
|
OwnerUserID: firstNonEmpty(p.OwnerUserID, pipelineUID(claims)),
|
|
OwnerUserName: firstNonEmpty(p.OwnerUserName, resolveUserName(claims)),
|
|
ContractAmount: round2(p.ContractAmount),
|
|
TotalAmount: totalAmount,
|
|
Status: pickInt8(p.Status, 1, 2),
|
|
Items: itemsJSON,
|
|
Remark: p.Remark,
|
|
ReceiptURL: strings.TrimSpace(p.ReceiptURL),
|
|
ReceiptName: strings.TrimSpace(p.ReceiptName),
|
|
CreateUserID: pipelineUID(claims),
|
|
CreateTime: now,
|
|
UpdateTime: now,
|
|
}
|
|
c.fillFromContract(tenantID, p.ContractID, &row)
|
|
applyPaybackReceived(&row)
|
|
|
|
if _, err := models.Orm.Insert(&row); err != nil {
|
|
pipelineErr(&c.Controller, 500, 500, "创建失败: "+err.Error())
|
|
return
|
|
}
|
|
crmWriteLog(tenantID, 4, row.ID, "create", "创建回款计划:"+row.ContractName, claims)
|
|
pipelineOk(&c.Controller, map[string]interface{}{"id": row.ID})
|
|
}
|
|
|
|
// Update PUT /backend/crm/payback/:id
|
|
func (c *BackendCrmPaybackController) Update() {
|
|
claims, err := pipelineClaims(&c.Controller)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 401, 401, err.Error())
|
|
return
|
|
}
|
|
id, _ := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if id == 0 {
|
|
pipelineErr(&c.Controller, 400, 400, "无效的ID")
|
|
return
|
|
}
|
|
raw, _ := io.ReadAll(c.Ctx.Request.Body)
|
|
var p paybackPayload
|
|
if err := json.Unmarshal(raw, &p); err != nil {
|
|
pipelineErr(&c.Controller, 400, 400, "参数错误")
|
|
return
|
|
}
|
|
tenantID := pipelineTenantID(claims)
|
|
var row models.TenantCrmPayback
|
|
if err := models.Orm.QueryTable(new(models.TenantCrmPayback)).
|
|
Filter("id", id).Filter("tenant_id", tenantID).
|
|
Filter("delete_time__isnull", true).One(&row); err != nil {
|
|
pipelineErr(&c.Controller, 404, 404, "回款计划未找到")
|
|
return
|
|
}
|
|
|
|
if p.ContractID > 0 {
|
|
row.ContractID = &p.ContractID
|
|
row.ContractNo = strings.TrimSpace(p.ContractNo)
|
|
row.ContractName = strings.TrimSpace(p.ContractName)
|
|
row.CustomerName = strings.TrimSpace(p.CustomerName)
|
|
c.fillFromContract(tenantID, p.ContractID, &row)
|
|
}
|
|
if p.PlanType != 0 {
|
|
row.PlanType = pickInt8(p.PlanType, 1, 5)
|
|
}
|
|
if p.PayMethod != 0 {
|
|
row.PayMethod = pickInt8(p.PayMethod, 1, 7)
|
|
}
|
|
row.RemindDays = p.RemindDays
|
|
if strings.TrimSpace(p.OwnerUserID) != "" {
|
|
row.OwnerUserID = strings.TrimSpace(p.OwnerUserID)
|
|
}
|
|
if strings.TrimSpace(p.OwnerUserName) != "" {
|
|
row.OwnerUserName = strings.TrimSpace(p.OwnerUserName)
|
|
}
|
|
if p.ContractAmount > 0 {
|
|
row.ContractAmount = round2(p.ContractAmount)
|
|
}
|
|
row.Remark = p.Remark
|
|
row.ReceiptURL = strings.TrimSpace(p.ReceiptURL)
|
|
row.ReceiptName = strings.TrimSpace(p.ReceiptName)
|
|
if p.Status != 0 {
|
|
row.Status = pickInt8(p.Status, 1, 2)
|
|
}
|
|
if p.Items != nil {
|
|
itemsJSON, _, err := normalizePaybackItems(p.Items)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 400, 400, "计划明细格式错误")
|
|
return
|
|
}
|
|
row.Items = itemsJSON
|
|
}
|
|
applyPaybackReceived(&row)
|
|
row.UpdateTime = time.Now()
|
|
|
|
if _, err := models.Orm.Update(&row); err != nil {
|
|
pipelineErr(&c.Controller, 500, 500, "更新失败: "+err.Error())
|
|
return
|
|
}
|
|
crmWriteLog(tenantID, 4, row.ID, "update", "更新回款计划:"+row.ContractName, claims)
|
|
pipelineOk(&c.Controller, map[string]interface{}{"id": row.ID})
|
|
}
|
|
|
|
// Delete DELETE /backend/crm/payback/:id
|
|
func (c *BackendCrmPaybackController) Delete() {
|
|
claims, err := pipelineClaims(&c.Controller)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 401, 401, err.Error())
|
|
return
|
|
}
|
|
id, _ := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if id == 0 {
|
|
pipelineErr(&c.Controller, 400, 400, "无效的ID")
|
|
return
|
|
}
|
|
tenantID := pipelineTenantID(claims)
|
|
var row models.TenantCrmPayback
|
|
if err := models.Orm.QueryTable(new(models.TenantCrmPayback)).
|
|
Filter("id", id).Filter("tenant_id", tenantID).
|
|
Filter("delete_time__isnull", true).One(&row); err != nil {
|
|
pipelineErr(&c.Controller, 404, 404, "回款计划未找到")
|
|
return
|
|
}
|
|
if !canDeleteCrmRecord(claims, row.CreateUserID) {
|
|
pipelineErr(&c.Controller, 403, 403, "只有创建人、租户管理员或平台管理员可以删除该回款计划")
|
|
return
|
|
}
|
|
now := time.Now()
|
|
_, err = models.Orm.QueryTable(new(models.TenantCrmPayback)).
|
|
Filter("id", id).Filter("tenant_id", tenantID).
|
|
Update(map[string]interface{}{"delete_time": now, "update_time": now})
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 500, 500, "删除失败: "+err.Error())
|
|
return
|
|
}
|
|
pipelineOk(&c.Controller, nil)
|
|
}
|
|
|
|
// ProgressList GET /backend/crm/payback/progress/list
|
|
// 回款进度:把全部回款计划的分期明细摊平返回,并汇总计划总额 / 已回款总额。
|
|
func (c *BackendCrmPaybackController) ProgressList() {
|
|
claims, err := pipelineClaims(&c.Controller)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 401, 401, err.Error())
|
|
return
|
|
}
|
|
tenantID := pipelineTenantID(claims)
|
|
keyword := strings.TrimSpace(c.GetString("keyword"))
|
|
status := strings.TrimSpace(c.GetString("status"))
|
|
contractID := strings.TrimSpace(c.GetString("contract_id"))
|
|
|
|
cond := orm.NewCondition().And("tenant_id", tenantID).And("delete_time__isnull", true)
|
|
if keyword != "" {
|
|
cond = cond.AndCond(orm.NewCondition().
|
|
Or("contract_name__contains", keyword).
|
|
Or("customer_name__contains", keyword).
|
|
Or("owner_user_name__contains", keyword))
|
|
}
|
|
if contractID != "" {
|
|
if cid, err := strconv.ParseUint(contractID, 10, 64); err == nil && cid > 0 {
|
|
cond = cond.And("contract_id", cid)
|
|
}
|
|
}
|
|
var list []models.TenantCrmPayback
|
|
_, _ = models.Orm.QueryTable(new(models.TenantCrmPayback)).SetCond(cond).OrderBy("-id").All(&list)
|
|
|
|
rows := make([]paybackProgress, 0)
|
|
var totalPlanned, totalReceived float64
|
|
for i := range list {
|
|
p := list[i]
|
|
for _, it := range parsePaybackItems(p.Items) {
|
|
st := it.Status
|
|
if st == 0 {
|
|
st = itemStatus(it.ReceivedAmount, it.Amount)
|
|
}
|
|
if status != "" && fmt.Sprintf("%d", st) != status {
|
|
continue
|
|
}
|
|
totalPlanned = round2(totalPlanned + it.Amount)
|
|
totalReceived = round2(totalReceived + it.ReceivedAmount)
|
|
rows = append(rows, paybackProgress{
|
|
PaybackID: p.ID,
|
|
ContractID: p.ContractID,
|
|
ContractNo: p.ContractNo,
|
|
ContractName: p.ContractName,
|
|
CustomerName: p.CustomerName,
|
|
OwnerUserName: p.OwnerUserName,
|
|
PlanType: p.PlanType,
|
|
PayMethod: p.PayMethod,
|
|
Seq: it.Seq,
|
|
Name: it.Name,
|
|
Percent: it.Percent,
|
|
Amount: it.Amount,
|
|
PlanDate: it.PlanDate,
|
|
ReceivedAmount: it.ReceivedAmount,
|
|
ReceivedDate: it.ReceivedDate,
|
|
Status: st,
|
|
})
|
|
}
|
|
}
|
|
pipelineOk(&c.Controller, map[string]interface{}{
|
|
"list": rows, "total": len(rows),
|
|
"total_planned": totalPlanned, "total_received": totalReceived,
|
|
})
|
|
}
|
|
|
|
// Receive POST /backend/crm/payback/:id/receive
|
|
// 回款进度登记:按明细序号写入实收金额 / 回款日期,并重算计划汇总。
|
|
func (c *BackendCrmPaybackController) Receive() {
|
|
claims, err := pipelineClaims(&c.Controller)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 401, 401, err.Error())
|
|
return
|
|
}
|
|
id, _ := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if id == 0 {
|
|
pipelineErr(&c.Controller, 400, 400, "无效的ID")
|
|
return
|
|
}
|
|
raw, _ := io.ReadAll(c.Ctx.Request.Body)
|
|
var p struct {
|
|
Seq int `json:"seq"`
|
|
ReceivedAmount float64 `json:"received_amount"`
|
|
ReceivedDate string `json:"received_date"`
|
|
ReceiptURL string `json:"receipt_url"` // 流水回执地址(选填)
|
|
ReceiptName string `json:"receipt_name"` // 流水回执文件名(选填)
|
|
}
|
|
if err := json.Unmarshal(raw, &p); err != nil {
|
|
pipelineErr(&c.Controller, 400, 400, "参数错误")
|
|
return
|
|
}
|
|
tenantID := pipelineTenantID(claims)
|
|
var row models.TenantCrmPayback
|
|
if err := models.Orm.QueryTable(new(models.TenantCrmPayback)).
|
|
Filter("id", id).Filter("tenant_id", tenantID).
|
|
Filter("delete_time__isnull", true).One(&row); err != nil {
|
|
pipelineErr(&c.Controller, 404, 404, "回款计划未找到")
|
|
return
|
|
}
|
|
items := parsePaybackItems(row.Items)
|
|
matched := false
|
|
for i := range items {
|
|
if items[i].Seq != p.Seq {
|
|
continue
|
|
}
|
|
items[i].ReceivedAmount = round2(p.ReceivedAmount)
|
|
items[i].ReceivedDate = strings.TrimSpace(p.ReceivedDate)
|
|
items[i].ReceiptURL = strings.TrimSpace(p.ReceiptURL)
|
|
items[i].ReceiptName = strings.TrimSpace(p.ReceiptName)
|
|
items[i].Status = itemStatus(items[i].ReceivedAmount, items[i].Amount)
|
|
matched = true
|
|
break
|
|
}
|
|
if !matched {
|
|
pipelineErr(&c.Controller, 404, 404, "计划明细不存在")
|
|
return
|
|
}
|
|
out, err := json.Marshal(items)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 500, 500, "保存失败")
|
|
return
|
|
}
|
|
row.Items = string(out)
|
|
applyPaybackReceived(&row)
|
|
row.UpdateTime = time.Now()
|
|
if _, err := models.Orm.Update(&row); err != nil {
|
|
pipelineErr(&c.Controller, 500, 500, "保存失败: "+err.Error())
|
|
return
|
|
}
|
|
crmWriteLog(tenantID, 4, row.ID, "receive", fmt.Sprintf("回款登记:%s 第 %d 期 %s 元", row.ContractName, p.Seq, formatAmount(p.ReceivedAmount)), claims)
|
|
pipelineOk(&c.Controller, buildPaybackResp(&row))
|
|
}
|
|
|
|
// ========================== 内部辅助 ==========================
|
|
|
|
// buildPaybackResp 组装响应:items 透传 JSON 数组。
|
|
func buildPaybackResp(row *models.TenantCrmPayback) paybackResp {
|
|
items := json.RawMessage("[]")
|
|
if strings.TrimSpace(row.Items) != "" {
|
|
items = json.RawMessage(row.Items)
|
|
}
|
|
return paybackResp{TenantCrmPayback: *row, Items: items}
|
|
}
|
|
|
|
// parsePaybackItems 解析明细 JSON(容错:空 / 非法时返回空数组)。
|
|
func parsePaybackItems(raw string) []paybackItem {
|
|
items := make([]paybackItem, 0)
|
|
if strings.TrimSpace(raw) == "" {
|
|
return items
|
|
}
|
|
_ = json.Unmarshal([]byte(raw), &items)
|
|
return items
|
|
}
|
|
|
|
// normalizePaybackItems 校验并规范化明细 JSON:补齐序号、金额取两位小数,返回紧凑 JSON 与计划总额。
|
|
func normalizePaybackItems(raw json.RawMessage) (string, float64, error) {
|
|
if len(raw) == 0 || strings.TrimSpace(string(raw)) == "" || strings.TrimSpace(string(raw)) == "null" {
|
|
return "[]", 0, nil
|
|
}
|
|
var arr []map[string]interface{}
|
|
if err := json.Unmarshal(raw, &arr); err != nil {
|
|
return "", 0, err
|
|
}
|
|
items := make([]paybackItem, 0, len(arr))
|
|
var total float64
|
|
for i, m := range arr {
|
|
it := paybackItem{
|
|
Seq: i + 1,
|
|
Name: strings.TrimSpace(strVal(m["name"])),
|
|
Percent: round2(toFloat64(m["percent"])),
|
|
Amount: round2(toFloat64(m["amount"])),
|
|
PlanDate: strings.TrimSpace(strVal(m["plan_date"])),
|
|
ReceivedAmount: round2(toFloat64(m["received_amount"])),
|
|
ReceivedDate: strings.TrimSpace(strVal(m["received_date"])),
|
|
}
|
|
if v, ok := m["receipt_url"]; ok && v != nil {
|
|
it.ReceiptURL = strings.TrimSpace(fmt.Sprintf("%v", v))
|
|
}
|
|
if v, ok := m["receipt_name"]; ok && v != nil {
|
|
it.ReceiptName = strings.TrimSpace(fmt.Sprintf("%v", v))
|
|
}
|
|
if v, ok := m["seq"]; ok {
|
|
if s := int(toFloat64(v)); s > 0 {
|
|
it.Seq = s
|
|
}
|
|
}
|
|
it.Status = itemStatus(it.ReceivedAmount, it.Amount)
|
|
total = round2(total + it.Amount)
|
|
items = append(items, it)
|
|
}
|
|
out, err := json.Marshal(items)
|
|
if err != nil {
|
|
return "", 0, err
|
|
}
|
|
return string(out), total, nil
|
|
}
|
|
|
|
// itemStatus 由实收金额推导明细状态:0/空=未回款 1、部分回款 2、已回款 3。
|
|
func itemStatus(received, amount float64) int8 {
|
|
if amount > 0 && received >= amount {
|
|
return 3
|
|
}
|
|
if received > 0 {
|
|
return 2
|
|
}
|
|
return 1
|
|
}
|
|
|
|
// strVal 安全取字符串:nil / 非字符串时返回空串,避免写出 "<nil>"。
|
|
func strVal(v interface{}) string {
|
|
if v == nil {
|
|
return ""
|
|
}
|
|
if s, ok := v.(string); ok {
|
|
return s
|
|
}
|
|
return fmt.Sprintf("%v", v)
|
|
}
|
|
|
|
// applyPaybackReceived 按明细重算计划总额、已回款金额与状态。
|
|
func applyPaybackReceived(row *models.TenantCrmPayback) {
|
|
items := parsePaybackItems(row.Items)
|
|
var total, received float64
|
|
allDone := len(items) > 0
|
|
for i := range items {
|
|
if items[i].Status == 0 {
|
|
items[i].Status = itemStatus(items[i].ReceivedAmount, items[i].Amount)
|
|
}
|
|
if items[i].Status != 3 {
|
|
allDone = false
|
|
}
|
|
total = round2(total + items[i].Amount)
|
|
received = round2(received + items[i].ReceivedAmount)
|
|
}
|
|
row.TotalAmount = total
|
|
row.ReceivedAmount = received
|
|
if out, err := json.Marshal(items); err == nil {
|
|
row.Items = string(out)
|
|
}
|
|
// 状态:1进行中 / 2已完成(全部明细回款完成)
|
|
if allDone {
|
|
row.Status = 2
|
|
} else if row.Status == 0 {
|
|
row.Status = 1
|
|
}
|
|
}
|
|
|
|
// fillFromContract 由关联合同补齐合同编号 / 名称 / 客户 / 合同金额(仅在字段为空时填充)。
|
|
func (c *BackendCrmPaybackController) fillFromContract(tenantID string, contractID uint64, row *models.TenantCrmPayback) {
|
|
if contractID == 0 {
|
|
return
|
|
}
|
|
var ct models.TenantCrmContract
|
|
if err := models.Orm.QueryTable(new(models.TenantCrmContract)).
|
|
Filter("id", contractID).Filter("tenant_id", tenantID).
|
|
Filter("delete_time__isnull", true).One(&ct); err != nil {
|
|
return
|
|
}
|
|
row.ContractID = &ct.ID
|
|
if row.ContractNo == "" {
|
|
row.ContractNo = ct.ContractNo
|
|
}
|
|
if row.ContractName == "" {
|
|
row.ContractName = ct.ContractName
|
|
}
|
|
if row.ContractAmount <= 0 {
|
|
row.ContractAmount = ct.TotalAmount
|
|
}
|
|
if row.CustomerName == "" {
|
|
var parties []map[string]interface{}
|
|
_ = json.Unmarshal([]byte(ct.Parties), &parties)
|
|
for _, party := range parties {
|
|
if fmt.Sprintf("%v", party["ref_type"]) != "1" {
|
|
continue
|
|
}
|
|
row.CustomerName = strings.TrimSpace(strVal(party["ref_name"]))
|
|
if cid := uint64(toFloat64(party["ref_id"])); cid > 0 {
|
|
row.CustomerID = &cid
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// formatAmount 金额文案(用于操作日志)。
|
|
func formatAmount(n float64) string {
|
|
return strconv.FormatFloat(round2(n), 'f', 2, 64)
|
|
}
|