更新密码找回uniapp
This commit is contained in:
1 parent
7f9a66da0f
commit
3fdda89833
1304 files changed
+3503
-26132
No files matched your search
@@ -16,31 +16,23 @@ import (
|
||||
"server/pkg/identity"
|
||||
)
|
||||
|
||||
// BackendCrmContractController CRM 合同管理
|
||||
//
|
||||
// 进度式创建:前端每完成一步即可保存(Create / Update 均支持),
|
||||
// step 记录创建进度(1=合同信息 2=产品清单),status=1 表示草稿、2 表示已完成。
|
||||
type BackendCrmContractController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// contractSummary 金额汇总(后端按 products 重算,与前端展示逻辑一致)。
|
||||
type contractSummary struct {
|
||||
HardwareAmount float64 `json:"hardware_amount"` // 硬件部分金额
|
||||
SoftwareAmount float64 `json:"software_amount"` // 软件部分金额
|
||||
OtherAmount float64 `json:"other_amount"` // 其他部分金额(服务/开发/其他)
|
||||
TotalAmount float64 `json:"total_amount"` // 合同总金额 = 硬件 + 软件 + 其他
|
||||
TotalCost float64 `json:"total_cost"` // 产品总成本 = Σ(数量 × 成本单价)
|
||||
TotalProfit float64 `json:"total_profit"` // 合同总利润 = 总金额 - 总成本
|
||||
HardwareAmount float64 `json:"hardware_amount"`
|
||||
SoftwareAmount float64 `json:"software_amount"`
|
||||
OtherAmount float64 `json:"other_amount"`
|
||||
TotalAmount float64 `json:"total_amount"`
|
||||
TotalCost float64 `json:"total_cost"`
|
||||
TotalProfit float64 `json:"total_profit"`
|
||||
}
|
||||
|
||||
// contractPayload 创建 / 更新请求体。
|
||||
type contractPayload struct {
|
||||
ContractNo string `json:"contract_no"`
|
||||
ContractName string `json:"contract_name"`
|
||||
ContractCategory string `json:"contract_category"`
|
||||
// 我方角色:0=我方不参与(如上中游项目中上游与中游主体之间的合同,我方不是签约方)
|
||||
// 1甲方/2乙方/3丙方/4丁方。用指针区分「未传」与「传 0」。
|
||||
OurRole *int8 `json:"our_role"`
|
||||
PartyCount int8 `json:"party_count"`
|
||||
ProjectID uint64 `json:"project_id"`
|
||||
@@ -57,7 +49,6 @@ type contractPayload struct {
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
// contractResp 列表 / 详情响应:parties / products 解析为 JSON 数组透出,summary 由金额字段组装。
|
||||
type contractResp struct {
|
||||
models.TenantCrmContract
|
||||
Parties json.RawMessage `json:"parties"`
|
||||
@@ -65,7 +56,6 @@ type contractResp struct {
|
||||
Summary *contractSummary `json:"summary"`
|
||||
}
|
||||
|
||||
// List GET /backend/crm/contract/list
|
||||
func (c *BackendCrmContractController) List() {
|
||||
claims, err := pipelineClaims(&c.Controller)
|
||||
if err != nil {
|
||||
@@ -90,10 +80,8 @@ func (c *BackendCrmContractController) List() {
|
||||
|
||||
tenantID := pipelineTenantID(claims)
|
||||
cond := orm.NewCondition().And("tenant_id", tenantID).And("delete_time__isnull", true)
|
||||
// 数据范围:平台/租户管理员可见全部,其他用户仅本人(负责人或创建人)
|
||||
cond = crmApplyOwnerScope(cond, claims)
|
||||
if keyword != "" {
|
||||
// 签约主体名称存储在 parties JSON 中,用 LIKE 一并匹配
|
||||
kw := orm.NewCondition().
|
||||
Or("contract_name__contains", keyword).
|
||||
Or("contract_no__contains", keyword).
|
||||
@@ -112,20 +100,16 @@ func (c *BackendCrmContractController) List() {
|
||||
cond = cond.And("status", status)
|
||||
}
|
||||
switch projectType {
|
||||
case "project": // 项目合同
|
||||
case "project":
|
||||
cond = cond.And("project_id__gt", 0)
|
||||
case "headless": // 无头合同
|
||||
case "headless":
|
||||
cond = cond.And("project_id__isnull", true)
|
||||
}
|
||||
// 按项目精确筛选(项目详情「合同管理」Tab:一个项目可关联多份合同)
|
||||
if projectID != "" {
|
||||
if pid, err := strconv.ParseUint(projectID, 10, 64); err == nil && pid > 0 {
|
||||
cond = cond.And("project_id", pid)
|
||||
}
|
||||
}
|
||||
// 周期筛选:按「签订日期」归类(与顶部统计同口径)——签订日期落在所选周期内即命中。
|
||||
// period=month/quarter/year 取当期区间,period=custom 用 start_date / end_date;
|
||||
// 签订日期为空的合同(草稿 / 尚未填写)不受周期限制,避免默认「本年」时被隐藏。
|
||||
period := strings.TrimSpace(c.GetString("period"))
|
||||
rangeStart := strings.TrimSpace(c.GetString("start_date"))
|
||||
rangeEnd := strings.TrimSpace(c.GetString("end_date"))
|
||||
@@ -143,14 +127,11 @@ func (c *BackendCrmContractController) List() {
|
||||
total, _ := qs.Count()
|
||||
var list []models.TenantCrmContract
|
||||
if total > 0 {
|
||||
// 列表排列:按「签订日期」倒序,最新签约的合同排最前。
|
||||
// 同日签约多份时按 id 倒序,保证翻页顺序稳定;签订日期为空的合同(草稿 / 尚未签约)沉底。
|
||||
if _, err := qs.OrderBy("-sign_date", "-id").Offset((page - 1) * pageSize).Limit(pageSize).All(&list); err != nil {
|
||||
pipelineErr(&c.Controller, 500, 500, "查询失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
// 回款逾期日期(取最早,未收齐且已过计划回款日期):合同「逾期」标签与结束日期口径一并参考
|
||||
if len(list) > 0 {
|
||||
contractIDs := make([]uint64, 0, len(list))
|
||||
for i := range list {
|
||||
@@ -170,11 +151,6 @@ func (c *BackendCrmContractController) List() {
|
||||
})
|
||||
}
|
||||
|
||||
// Stats GET /backend/crm/contract/stats
|
||||
// 全租户合同统计(排除已作废),供列表页顶部汇总卡片使用。
|
||||
// 支持按周期筛选:period=month/quarter/year(当期)或 custom(start_date~end_date),
|
||||
// 按合同「签订日期」落在所选周期内统计(与列表口径一致)。
|
||||
// 「合同总利润」仅统计非甲方(收款方)合同:我方为甲方的合同是采购付款,只有支出、不存在利润。
|
||||
func (c *BackendCrmContractController) Stats() {
|
||||
claims, err := pipelineClaims(&c.Controller)
|
||||
if err != nil {
|
||||
@@ -191,11 +167,8 @@ func (c *BackendCrmContractController) Stats() {
|
||||
}
|
||||
|
||||
table := new(models.TenantCrmContract).TableName()
|
||||
// 我方不参与的合同(our_role=0,如上中游项目的链条合同)不属于我方经营合同,不计入经营统计
|
||||
where := "tenant_id = ? AND delete_time IS NULL AND status <> 3 AND our_role <> 0"
|
||||
args := []interface{}{tenantID}
|
||||
// 按「签订日期」落在所选周期 [start, end] 内统计(与列表口径一致):
|
||||
// 签订日期为空的合同不受周期限制,视为不限制对应边界。
|
||||
if start != "" {
|
||||
where += " AND (sign_date IS NULL OR sign_date >= ?)"
|
||||
args = append(args, start)
|
||||
@@ -204,7 +177,6 @@ func (c *BackendCrmContractController) Stats() {
|
||||
where += " AND (sign_date IS NULL OR sign_date <= ?)"
|
||||
args = append(args, end)
|
||||
}
|
||||
// 「合同总利润」只汇总非甲方(收款方)合同:我方为甲方时是采购付款方,合同金额即支出,不存在利润
|
||||
raw := "SELECT COUNT(*) AS total, IFNULL(SUM(total_amount),0) AS total_amount, " +
|
||||
"IFNULL(SUM(total_cost),0) AS total_cost, " +
|
||||
"IFNULL(SUM(CASE WHEN our_role <> 1 THEN total_profit ELSE 0 END),0) AS total_profit " +
|
||||
@@ -225,8 +197,6 @@ func (c *BackendCrmContractController) Stats() {
|
||||
})
|
||||
}
|
||||
|
||||
// periodRange 根据 period 返回当期区间 [start, end](YYYY-MM-DD):month/quarter/year。
|
||||
// 统计与列表均以该区间与合同「签订日期」比较(签订日期落在区间内即命中)。
|
||||
func periodRange(period string) (string, string) {
|
||||
now := time.Now()
|
||||
loc := now.Location()
|
||||
@@ -241,14 +211,13 @@ func periodRange(period string) (string, string) {
|
||||
start := time.Date(y, 1, 1, 0, 0, 0, 0, loc)
|
||||
end := start.AddDate(1, 0, 0).Add(-time.Nanosecond)
|
||||
return start.Format("2006-01-02"), end.Format("2006-01-02")
|
||||
default: // month
|
||||
default:
|
||||
start := time.Date(y, m, 1, 0, 0, 0, 0, loc)
|
||||
end := start.AddDate(0, 1, 0).Add(-time.Nanosecond)
|
||||
return start.Format("2006-01-02"), end.Format("2006-01-02")
|
||||
}
|
||||
}
|
||||
|
||||
// Detail GET /backend/crm/contract/:id
|
||||
func (c *BackendCrmContractController) Detail() {
|
||||
claims, err := pipelineClaims(&c.Controller)
|
||||
if err != nil {
|
||||
@@ -268,20 +237,16 @@ func (c *BackendCrmContractController) Detail() {
|
||||
pipelineErr(&c.Controller, 404, 404, "合同未找到")
|
||||
return
|
||||
}
|
||||
// 数据范围校验:非管理员仅能查看本人(负责人或创建人)的数据
|
||||
if !canAccessCrmRecord(claims, row.OwnerUserID, row.CreateUserID) {
|
||||
pipelineErr(&c.Controller, 403, 403, "无权查看该合同数据")
|
||||
return
|
||||
}
|
||||
// 回款逾期日期(取最早,未收齐且已过计划回款日期):合同「逾期」标签与结束日期口径一并参考
|
||||
if od := paybackOverdueDateByContracts(pipelineTenantID(claims), []uint64{row.ID}); od[row.ID] != "" {
|
||||
row.PaybackOverdueDate = od[row.ID]
|
||||
}
|
||||
pipelineOk(&c.Controller, buildContractResp(&row))
|
||||
}
|
||||
|
||||
// Create POST /backend/crm/contract
|
||||
// 进度式保存入口之一:首次保存(通常为草稿),返回 id 后续走 Update。
|
||||
func (c *BackendCrmContractController) Create() {
|
||||
claims, err := pipelineClaims(&c.Controller)
|
||||
if err != nil {
|
||||
@@ -327,11 +292,9 @@ func (c *BackendCrmContractController) Create() {
|
||||
if strings.TrimSpace(p.ContractCategory) != "" {
|
||||
row.ContractCategory = strings.TrimSpace(p.ContractCategory)
|
||||
}
|
||||
// 编号为空时自动生成(查重)
|
||||
if row.ContractNo == "" {
|
||||
row.ContractNo = genContractNo(tenantID)
|
||||
}
|
||||
// 绑定项目:校验项目归属并取标准项目名称
|
||||
if p.ProjectID > 0 {
|
||||
projID, projName, ok := c.resolveProject(tenantID, p.ProjectID)
|
||||
if !ok {
|
||||
@@ -341,7 +304,6 @@ func (c *BackendCrmContractController) Create() {
|
||||
row.ProjectID = &projID
|
||||
row.ProjectName = projName
|
||||
}
|
||||
// 参与方 / 产品清单 JSON 落库 + 金额重算
|
||||
parties, err := normalizeContractJSON(p.Parties)
|
||||
if err != nil {
|
||||
pipelineErr(&c.Controller, 400, 400, "参与方数据格式错误")
|
||||
@@ -354,7 +316,6 @@ func (c *BackendCrmContractController) Create() {
|
||||
return
|
||||
}
|
||||
row.Products = products
|
||||
// 合同产品清单同步到产品管理(新增产品自动建档、成本单价取产品管理;含软件开发行)
|
||||
projID := uint64(0)
|
||||
if row.ProjectID != nil {
|
||||
projID = *row.ProjectID
|
||||
@@ -364,8 +325,6 @@ func (c *BackendCrmContractController) Create() {
|
||||
}
|
||||
applyContractAmounts(&row)
|
||||
|
||||
// 唯一性校验:同租户内「合同名称 + 签约方」完全一致才视为重复。
|
||||
// 同名但签约方不同(例如我方分别作为甲方 / 乙方的两份合同)不算重复。
|
||||
if crmContractDuplicated(tenantID, row.ContractName, row.Parties) {
|
||||
pipelineErr(&c.Controller, 409, 409, "已存在同名且签约方相同的合同(可能由其他同事创建),如需继续跟进请联系管理员转移")
|
||||
return
|
||||
@@ -374,14 +333,11 @@ func (c *BackendCrmContractController) Create() {
|
||||
pipelineErr(&c.Controller, 500, 500, "创建失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
// 参与方填写的签约人(姓名 + 电话)同步写入对应企业的联系人通讯录
|
||||
syncContractPartyContacts(tenantID, row.Parties)
|
||||
crmWriteLog(tenantID, 6, row.ID, "create", "创建合同:"+row.ContractName, claims)
|
||||
pipelineOk(&c.Controller, map[string]interface{}{"id": row.ID})
|
||||
}
|
||||
|
||||
// Update PUT /backend/crm/contract/:id
|
||||
// 进度式保存入口之一:每一步保存都走这里,全量覆盖业务字段。
|
||||
func (c *BackendCrmContractController) Update() {
|
||||
claims, err := pipelineClaims(&c.Controller)
|
||||
if err != nil {
|
||||
@@ -408,7 +364,6 @@ func (c *BackendCrmContractController) Update() {
|
||||
pipelineErr(&c.Controller, 404, 404, "合同未找到")
|
||||
return
|
||||
}
|
||||
// 数据范围校验:非管理员仅能操作本人(负责人或创建人)的数据
|
||||
if !canAccessCrmRecord(claims, row.OwnerUserID, row.CreateUserID) {
|
||||
pipelineErr(&c.Controller, 403, 403, "无权操作该合同数据")
|
||||
return
|
||||
@@ -426,13 +381,11 @@ func (c *BackendCrmContractController) Update() {
|
||||
row.ContractCategory = strings.TrimSpace(p.ContractCategory)
|
||||
}
|
||||
if p.OurRole != nil {
|
||||
// 0=我方不参与也是合法值,用指针判断避免误吞
|
||||
row.OurRole = normalizeOurRole(*p.OurRole)
|
||||
}
|
||||
if p.PartyCount != 0 {
|
||||
row.PartyCount = p.PartyCount
|
||||
}
|
||||
// 项目绑定支持切换 / 解绑(清空即为无头合同)
|
||||
if p.ProjectID > 0 {
|
||||
projID, projName, ok := c.resolveProject(tenantID, p.ProjectID)
|
||||
if !ok {
|
||||
@@ -469,7 +422,6 @@ func (c *BackendCrmContractController) Update() {
|
||||
return
|
||||
}
|
||||
row.Products = products
|
||||
// 合同产品清单同步到产品管理(新增产品自动建档、成本单价取产品管理;含软件开发行)
|
||||
projID := uint64(0)
|
||||
if row.ProjectID != nil {
|
||||
projID = *row.ProjectID
|
||||
@@ -479,8 +431,6 @@ func (c *BackendCrmContractController) Update() {
|
||||
}
|
||||
}
|
||||
applyContractAmounts(&row)
|
||||
// 状态不随合同编辑变更(避免编辑保存把已流转的状态打回草稿);
|
||||
// 签订/履约/作废等流转统一由「状态流转」接口 ChangeStatus 控制。
|
||||
if p.Step != 0 {
|
||||
row.Step = p.Step
|
||||
}
|
||||
@@ -491,13 +441,11 @@ func (c *BackendCrmContractController) Update() {
|
||||
pipelineErr(&c.Controller, 500, 500, "更新失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
// 参与方填写的签约人(姓名 + 电话)同步写入对应企业的联系人通讯录
|
||||
syncContractPartyContacts(tenantID, row.Parties)
|
||||
crmWriteLog(tenantID, 6, row.ID, "update", "更新合同:"+row.ContractName, claims)
|
||||
pipelineOk(&c.Controller, map[string]interface{}{"id": row.ID})
|
||||
}
|
||||
|
||||
// Delete DELETE /backend/crm/contract/:id
|
||||
func (c *BackendCrmContractController) Delete() {
|
||||
claims, err := pipelineClaims(&c.Controller)
|
||||
if err != nil {
|
||||
@@ -518,7 +466,6 @@ func (c *BackendCrmContractController) Delete() {
|
||||
pipelineErr(&c.Controller, 404, 404, "合同未找到")
|
||||
return
|
||||
}
|
||||
// 数据范围校验:非管理员仅能操作本人(负责人或创建人)的数据
|
||||
if !canAccessCrmRecord(claims, row.OwnerUserID, row.CreateUserID) {
|
||||
pipelineErr(&c.Controller, 403, 403, "无权操作该合同数据")
|
||||
return
|
||||
@@ -538,9 +485,6 @@ func (c *BackendCrmContractController) Delete() {
|
||||
pipelineOk(&c.Controller, nil)
|
||||
}
|
||||
|
||||
// ChangeStatus POST /backend/crm/contract/:id/status
|
||||
// 合同状态流转:1=草稿 2=已完成 3=已作废 4=履约中 5=异常。
|
||||
// 向导创建的合同默认为草稿,签订 / 履约等状态在列表中手动流转;各状态间可互切。
|
||||
func (c *BackendCrmContractController) ChangeStatus() {
|
||||
claims, err := pipelineClaims(&c.Controller)
|
||||
if err != nil {
|
||||
@@ -561,7 +505,6 @@ func (c *BackendCrmContractController) ChangeStatus() {
|
||||
pipelineErr(&c.Controller, 400, 400, "参数错误")
|
||||
return
|
||||
}
|
||||
// 合法目标状态:2已完成 / 3已作废 / 4履约中 / 5异常
|
||||
if p.Status < 2 || p.Status > 5 {
|
||||
pipelineErr(&c.Controller, 400, 400, "无效的状态值")
|
||||
return
|
||||
@@ -574,7 +517,6 @@ func (c *BackendCrmContractController) ChangeStatus() {
|
||||
pipelineErr(&c.Controller, 404, 404, "合同未找到")
|
||||
return
|
||||
}
|
||||
// 数据范围校验:非管理员仅能操作本人(负责人或创建人)的数据
|
||||
if !canAccessCrmRecord(claims, row.OwnerUserID, row.CreateUserID) {
|
||||
pipelineErr(&c.Controller, 403, 403, "无权操作该合同数据")
|
||||
return
|
||||
@@ -593,7 +535,6 @@ func (c *BackendCrmContractController) ChangeStatus() {
|
||||
pipelineOk(&c.Controller, map[string]interface{}{"id": row.ID, "status": p.Status})
|
||||
}
|
||||
|
||||
// contractStatusName 状态文案(用于操作日志)。
|
||||
func contractStatusName(s int8) string {
|
||||
switch s {
|
||||
case 1:
|
||||
@@ -611,10 +552,6 @@ func contractStatusName(s int8) string {
|
||||
}
|
||||
}
|
||||
|
||||
// ========================== 内部辅助 ==========================
|
||||
|
||||
// buildContractResp 组装响应:parties / products 透传 JSON 数组;
|
||||
// 金额字段在返回前按 products 以当前口径重算(单价含税、不再叠加税率),summary 随之组装。
|
||||
func buildContractResp(row *models.TenantCrmContract) contractResp {
|
||||
parties := json.RawMessage("[]")
|
||||
if strings.TrimSpace(row.Parties) != "" {
|
||||
@@ -624,8 +561,6 @@ func buildContractResp(row *models.TenantCrmContract) contractResp {
|
||||
if strings.TrimSpace(row.Products) != "" {
|
||||
products = json.RawMessage(row.Products)
|
||||
}
|
||||
// 返回前按「单价含税、不再叠加税率」的口径重算金额,
|
||||
// 保证列表/详情与产品清单一致,并自动修正历史数据按旧口径(叠加税率)存的金额
|
||||
if strings.TrimSpace(row.Products) != "" && string(products) != "[]" {
|
||||
applyContractAmounts(row)
|
||||
}
|
||||
@@ -644,11 +579,6 @@ func buildContractResp(row *models.TenantCrmContract) contractResp {
|
||||
}
|
||||
}
|
||||
|
||||
// syncContractPartyContacts 把合同参与方填写的签约人(姓名 + 电话)写入对应企业的联系人通讯录:
|
||||
// 新建的客户 / 供应商往往还没有联系人,用户只能手填签约人姓名与电话,
|
||||
// 这里自动建档(仅姓名 + 电话,其余资料留空,可在联系人管理中继续补全),
|
||||
// 避免这些信息只停留在合同文本里;同名或同号已存在时不会重复建档。
|
||||
// 仅处理关联了客户 / 供应商库的参与方:本公司方(ref_type=0)与手工填写未关联的名称(ref_id=0)跳过。
|
||||
func syncContractPartyContacts(tenantID, partiesJSON string) {
|
||||
partiesJSON = strings.TrimSpace(partiesJSON)
|
||||
if models.Orm == nil || partiesJSON == "" || partiesJSON == "[]" {
|
||||
@@ -664,7 +594,6 @@ func syncContractPartyContacts(tenantID, partiesJSON string) {
|
||||
return
|
||||
}
|
||||
for _, p := range parties {
|
||||
// ref_type:0=本公司 1=客户 2=供应商;未关联库(ref_id=0)时无处可写
|
||||
if p.RefID == 0 || (p.RefType != 1 && p.RefType != 2) {
|
||||
continue
|
||||
}
|
||||
@@ -680,7 +609,6 @@ func syncContractPartyContacts(tenantID, partiesJSON string) {
|
||||
}
|
||||
}
|
||||
|
||||
// resolveProject 校验项目归属当前租户并返回 (id, 标准项目名称)。
|
||||
func (c *BackendCrmContractController) resolveProject(tenantID string, projectID uint64) (uint64, string, bool) {
|
||||
var proj models.TenantCrmProject
|
||||
if err := models.Orm.QueryTable(new(models.TenantCrmProject)).
|
||||
@@ -691,7 +619,6 @@ func (c *BackendCrmContractController) resolveProject(tenantID string, projectID
|
||||
return proj.ID, proj.ProjectName, true
|
||||
}
|
||||
|
||||
// normalizeContractJSON 校验并规范化 JSON 数组(参与方 / 产品清单),返回紧凑 JSON 文本。
|
||||
func normalizeContractJSON(raw json.RawMessage) (string, error) {
|
||||
if len(raw) == 0 || strings.TrimSpace(string(raw)) == "" || strings.TrimSpace(string(raw)) == "null" {
|
||||
return "[]", nil
|
||||
@@ -707,13 +634,6 @@ func normalizeContractJSON(raw json.RawMessage) (string, error) {
|
||||
return string(out), nil
|
||||
}
|
||||
|
||||
// applyContractAmounts 按产品清单重算各部分金额(与前端 ProductList 汇总口径一致):
|
||||
// 清单单价为含税价,每行小计=数量×单价(不再叠加税率,税率仅作开票记录);
|
||||
// 硬件部分=Σ硬件小计;软件部分=Σ软件小计;其他部分=Σ服务/开发/其他小计;
|
||||
// 合同总金额=硬件+软件+其他;产品总成本=Σ(数量×成本单价)(成本为不含税原值);
|
||||
// 合同总利润=总金额-总成本。
|
||||
// 归类口径:分类代码 1/2 或名称含「硬件」/「软件」分别计入硬件/软件,其余计入其他。
|
||||
// contractTreeSellTotal 软件开发模块树售价合计:子节点汇总,叶子=人天×统一人天单价。
|
||||
func contractTreeSellTotal(nodes []interface{}, unitPrice float64) float64 {
|
||||
var sum float64
|
||||
for _, n := range nodes {
|
||||
@@ -737,9 +657,6 @@ func applyContractAmounts(row *models.TenantCrmContract) {
|
||||
}
|
||||
var hardware, software, other, cost float64
|
||||
for _, item := range items {
|
||||
// 软件开发行:开发总成本由模块报价带出(默认 = 人天×统一人天单价,可手工改写),计入产品成本;
|
||||
// 对外金额取销售单价(缺省时回退开发总成本 / 人天汇总,兼容历史数据);不计税率。
|
||||
// 口径与前端 utils.js 的 lineAmount / lineCost / buildSummary 保持一致。
|
||||
if fmt.Sprintf("%v", item["line_type"]) == "dev" {
|
||||
qty := toFloat64(item["quantity"])
|
||||
if qty <= 0 {
|
||||
@@ -766,7 +683,6 @@ func applyContractAmounts(row *models.TenantCrmContract) {
|
||||
qty := toFloat64(item["quantity"])
|
||||
price := toFloat64(item["price"])
|
||||
costPrice := toFloat64(item["cost_price"])
|
||||
// 清单单价为含税价:小计 = 数量 × 单价,不再叠加税率(税率仅作开票记录,不参与金额计算)
|
||||
amount := round2(qty * price)
|
||||
cost = round2(cost + round2(qty*costPrice))
|
||||
cat := fmt.Sprintf("%v", item["category"])
|
||||
@@ -787,7 +703,6 @@ func applyContractAmounts(row *models.TenantCrmContract) {
|
||||
row.TotalProfit = round2(row.TotalAmount - cost)
|
||||
}
|
||||
|
||||
// contractReceivedAmount 汇总合同已回款金额(该合同下所有未删除回款计划的 received_amount 之和)。
|
||||
func contractReceivedAmount(tenantID string, contractID uint64) float64 {
|
||||
if contractID == 0 {
|
||||
return 0
|
||||
@@ -802,10 +717,6 @@ func contractReceivedAmount(tenantID string, contractID uint64) float64 {
|
||||
return toFloat64(rows[0]["received"])
|
||||
}
|
||||
|
||||
// crmContractDuplicated 判断同租户内是否已存在「合同名称 + 签约方」均一致的合同。
|
||||
//
|
||||
// 判重口径:合同名称一致,且参与方(甲/乙/丙/丁)归一化签名一致。
|
||||
// 因此同一名称下「我方作为甲方」与「我方作为乙方」的两份合同不算重复。
|
||||
func crmContractDuplicated(tenantID, name, partiesJSON string) bool {
|
||||
name = strings.TrimSpace(name)
|
||||
if name == "" || strings.TrimSpace(tenantID) == "" || models.Orm == nil {
|
||||
@@ -829,9 +740,6 @@ func crmContractDuplicated(tenantID, name, partiesJSON string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// contractPartiesSignature 参与方归一化签名:按甲/乙/丙/丁固定顺序拼接「角色:主体类型|主体」。
|
||||
// 主体优先取关联主体 ID(ref_id),无 ID(手工录入名称)时退化为名称(ref_name);
|
||||
// 参与方为空或解析失败时返回空串,此时判重退化为「仅合同名称一致」。
|
||||
func contractPartiesSignature(partiesJSON string) string {
|
||||
if strings.TrimSpace(partiesJSON) == "" {
|
||||
return ""
|
||||
@@ -853,8 +761,6 @@ func contractPartiesSignature(partiesJSON string) string {
|
||||
continue
|
||||
}
|
||||
subject := str(p["ref_id"])
|
||||
// ref_id 为 0 / 空表示未关联主体库(本公司或手工录入),此时用名称兜底,
|
||||
// 避免不同主体都归到 "0" 造成误判为重复
|
||||
if subject == "" || subject == "0" {
|
||||
subject = str(p["ref_name"])
|
||||
}
|
||||
@@ -872,7 +778,6 @@ func contractPartiesSignature(partiesJSON string) string {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// genContractNo 生成合同编号:HT-YYYYMMDD-4位随机,租户内查重,最多重试 5 次。
|
||||
func genContractNo(tenantID string) string {
|
||||
for i := 0; i < 5; i++ {
|
||||
no := fmt.Sprintf("HT-%s-%04d", time.Now().Format("20060102"), rand.Intn(10000))
|
||||
@@ -886,8 +791,6 @@ func genContractNo(tenantID string) string {
|
||||
return fmt.Sprintf("HT-%s-%d", time.Now().Format("20060102"), time.Now().UnixNano()%100000)
|
||||
}
|
||||
|
||||
// normalizeOurRole 归一我方角色:0=我方不参与(如上中游项目中上游与中游主体之间的合同,
|
||||
// 我方不是签约方),1=甲方 2=乙方 3=丙方 4=丁方,越界回退乙方。
|
||||
func normalizeOurRole(v int8) int8 {
|
||||
if v == 0 {
|
||||
return 0
|
||||
@@ -895,7 +798,6 @@ func normalizeOurRole(v int8) int8 {
|
||||
return pickInt8(v, 2, 4)
|
||||
}
|
||||
|
||||
// pickInt8 取值约束:v 落在 [min, max] 内返回 v,否则返回 def。
|
||||
func pickInt8(v, def, max int8) int8 {
|
||||
if v >= 1 && v <= max {
|
||||
return v
|
||||
@@ -903,12 +805,10 @@ func pickInt8(v, def, max int8) int8 {
|
||||
return def
|
||||
}
|
||||
|
||||
// round2 保留两位小数。
|
||||
func round2(n float64) float64 {
|
||||
return float64(int64((n+1e-9)*100+0.5)) / 100
|
||||
}
|
||||
|
||||
// toInt64 orm.Params 值转 int64。
|
||||
func toInt64(v interface{}) int64 {
|
||||
switch n := v.(type) {
|
||||
case int64:
|
||||
@@ -923,7 +823,6 @@ func toInt64(v interface{}) int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// toFloat64 orm.Params / JSON 数值转 float64。
|
||||
func toFloat64(v interface{}) float64 {
|
||||
switch n := v.(type) {
|
||||
case float64:
|
||||
|
||||
Reference in new issue
Block a user