作者 陈志颖

fix:分红预算报告

... ... @@ -353,6 +353,7 @@ func (creditAccountService *CreditAccountService) PayCreditAccount(payCreditAcco
if creditAccountSaved, err4 := creditAccountRepository.Save(creditAccount); err4 != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err4.Error())
} else {
// TODO 更新分红预算单结算状态(已支付)
if err3 := transactionContext.CommitTransaction(); err3 != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err3.Error())
}
... ...
... ... @@ -609,8 +609,11 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
})
}
// 统计分红订单
var countDividendsOrders int32
// 统计成功预算的分红订单
var estimateSuccessfullyDividendsOrders map[string]int
// 统计预算失败的分红订单
var estimateFailedDividendsOrders map[string]int
// 获取订单产品
if _, orderGoods, err := orderGoodRepository.Find(map[string]interface{}{
... ... @@ -730,16 +733,17 @@ func (dividendsEstimateService *DividendsEstimateService) ConfirmDividendsIncent
log.Logger.Info("新增的分红预算单", map[string]interface{}{
"dividendsEstimates": dividendsEstimates,
})
if dividendsEstimatesSaved, err := dividendsEstimateRepository.SaveMany(dividendsEstimates); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
// TODO 分析成功和失败原因
// 分析成功和失败原因
successfullyCount := len(dividendsEstimatesSaved)
return fmt.Sprintf("已完成%d笔单订单分红预算,生成%d笔单分红预算,%d笔订单分红预算失败,失败原因:%s", countDividendsOrders, successfullyCount, 0, ""), nil
return map[string]interface{}{
"report": fmt.Sprintf("已完成%d笔单订单分红预算,生成%d笔单分红预算,%d笔订单分红预算失败,失败原因:%s", len(estimateSuccessfullyDividendsOrders), successfullyCount, len(estimateFailedDividendsOrders), ""),
}, nil
}
}
}
... ...
... ... @@ -5,6 +5,7 @@ import (
"github.com/linmadan/egglib-go/core/application"
coreDomain "github.com/linmadan/egglib-go/core/domain"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/shopspring/decimal"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
... ... @@ -18,37 +19,62 @@ type ConfirmDividendsIncentivesEstimateService struct {
// Confirm 确认业绩分红预算
func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoods []*domain.OrderGood) ([]*service.DividendsEstimateDetail, error) {
var cooperationContractRepository domain.CooperationContractRepository // 共创合约仓储
var cooperationProjectRepository domain.CooperationProjectRepository // 共创项目仓储
var dividendsOrderRepository domain.DividendsOrderRepository // 分红订单仓储
var dividendsReturnedOrderRepository domain.DividendsReturnedOrderRepository // 分红退货单仓储
// 共创合约仓储初始化
if repo, err := repository.NewCooperationContractRepository(domainService.transactionContext); err != nil {
return nil, err
} else {
cooperationContractRepository = repo
}
// 共创项目仓储初始化
if repo, err := repository.NewCooperationProjectRepository(domainService.transactionContext); err != nil {
return nil, err
} else {
cooperationProjectRepository = repo
}
// 分红订单仓储初始化
if repo, err := repository.NewDividendsOrderRepository(domainService.transactionContext); err != nil {
return nil, err
} else {
dividendsOrderRepository = repo
}
// 分红退货单仓储初始化
if repo, err := repository.NewDividendsReturnedOrderRepository(domainService.transactionContext); err != nil {
return nil, err
} else {
dividendsReturnedOrderRepository = repo
}
// 确认业绩分红预算
var dividendsEstimateDetails []*service.DividendsEstimateDetail
for _, orderGood := range orderGoods {
// 获取合约
cooperationContract, err := cooperationContractRepository.FindOne(map[string]interface{}{"cooperationContractNumber": orderGood.CooperationContractNumber})
cooperationContract, err := cooperationContractRepository.FindOne(map[string]interface{}{
"cooperationContractNumber": orderGood.CooperationContractNumber,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if cooperationContract == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("共创合约%s不存在", orderGood.CooperationContractNumber))
}
// TODO 校验合约关联的项目是否已结束
cooperationProject, err := cooperationProjectRepository.FindOne(map[string]interface{}{
"cooperationProjectNumber": cooperationContract.CooperationProjectNumber,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if cooperationProject == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("共创项目%s不存在", cooperationContract.CooperationProjectNumber))
}
if orderGood.DividendsOrderNumber != "" {
// 获取分红订单
dividendsOrder, err2 := dividendsOrderRepository.FindOne(map[string]interface{}{
... ... @@ -78,8 +104,7 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo
if dividendsIncentivesRuleMatched != nil {
for _, undertaker := range cooperationContract.Undertakers {
// 添加承接人分红预算信息详情
// TODO 使用decimal提高精度
undertakerDividendsAmount := orderGood.OrderGoodAmount * dividendsIncentivesRuleMatched.DividendsIncentivesPercentage / 100
undertakerDividendsAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodAmount).Mul(decimal.NewFromFloat(dividendsIncentivesRuleMatched.DividendsIncentivesPercentage).Div(decimal.NewFromFloat(100))).Float64()
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
DividendsUser: &domain.User{
UserId: undertaker.UserId,
... ... @@ -101,7 +126,7 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo
})
// 添加推荐人分红预算信息详情
if undertaker.Referrer != nil {
referrerDividendsAmount := orderGood.OrderGoodAmount * dividendsIncentivesRuleMatched.ReferrerPercentage / 100
referrerDividendsAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodAmount).Mul(decimal.NewFromFloat(dividendsIncentivesRuleMatched.ReferrerPercentage).Div(decimal.NewFromFloat(100))).Float64()
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
DividendsUser: &domain.User{
UserId: undertaker.Referrer.UserId,
... ... @@ -123,7 +148,7 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo
}
// 添加关联业务员分红预算信息详情
if undertaker.Salesman != nil {
salesmanDividendsAmount := orderGood.OrderGoodAmount * dividendsIncentivesRuleMatched.SalesmanPercentage / 100
salesmanDividendsAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodAmount).Mul(decimal.NewFromFloat(dividendsIncentivesRuleMatched.SalesmanPercentage).Div(decimal.NewFromFloat(100))).Float64()
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
DividendsUser: &domain.User{
UserId: undertaker.Salesman.UserId,
... ... @@ -166,7 +191,7 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo
// 计算分红
for _, undertaker := range cooperationContract.Undertakers {
// 添加承接人分红退货预算信息详情
undertakerDividendsAmount := -orderGood.OrderGoodAmount * dividendsIncentivesRuleMatched.DividendsIncentivesPercentage / 100
undertakerDividendsAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodAmount).Mul(decimal.NewFromFloat(dividendsIncentivesRuleMatched.DividendsIncentivesPercentage).Div(decimal.NewFromFloat(100))).Float64()
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
DividendsUser: &domain.User{
UserId: undertaker.UserId,
... ... @@ -184,11 +209,11 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo
},
DividendsParticipateType: domain.UNDERTAKER,
DividendsStage: dividendsIncentivesRuleMatched.DividendsIncentivesStage,
DividendsAmount: undertakerDividendsAmount,
DividendsAmount: -undertakerDividendsAmount,
})
// 添加推荐人分红退货预算信息详情
if undertaker.Referrer != nil {
referrerDividendsAmount := -orderGood.OrderGoodAmount * dividendsIncentivesRuleMatched.ReferrerPercentage / 100
referrerDividendsAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodAmount).Mul(decimal.NewFromFloat(dividendsIncentivesRuleMatched.ReferrerPercentage).Div(decimal.NewFromFloat(100))).Float64()
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
DividendsUser: &domain.User{
UserId: undertaker.Referrer.UserId,
... ... @@ -205,12 +230,12 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo
},
DividendsParticipateType: domain.REFERRER,
DividendsStage: dividendsIncentivesRuleMatched.DividendsIncentivesStage,
DividendsAmount: referrerDividendsAmount,
DividendsAmount: -referrerDividendsAmount,
})
}
// 添加关联业务员分红退货预算信息详情
if undertaker.Salesman != nil {
salesmanDividendsAmount := -orderGood.OrderGoodAmount * dividendsIncentivesRuleMatched.SalesmanPercentage / 100
salesmanDividendsAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodAmount).Mul(decimal.NewFromFloat(dividendsIncentivesRuleMatched.SalesmanPercentage).Div(decimal.NewFromFloat(100))).Float64()
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
DividendsUser: &domain.User{
UserId: undertaker.Salesman.UserId,
... ... @@ -227,7 +252,7 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo
},
DividendsParticipateType: domain.SALESMAN,
DividendsStage: dividendsIncentivesRuleMatched.DividendsIncentivesStage,
DividendsAmount: salesmanDividendsAmount,
DividendsAmount: -salesmanDividendsAmount,
})
}
}
... ...
... ... @@ -4,6 +4,7 @@ import (
"fmt"
coreDomain "github.com/linmadan/egglib-go/core/domain"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/shopspring/decimal"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
... ... @@ -68,7 +69,7 @@ func (domainService *ConfirmMoneyIncentivesEstimateService) Confirm(contract *do
if undertakerEstimated {
return nil, fmt.Errorf("用户 " + undertaker.UserName + " 已分红")
} else {
undertakerDividendsAmount := moneyIncentivesRuleMatched.MoneyIncentivesAmount * (1 - (moneyIncentivesRuleMatched.SalesmanPercentage+moneyIncentivesRuleMatched.ReferrerPercentage)/100)
undertakerDividendsAmount, _ := decimal.NewFromFloat(moneyIncentivesRuleMatched.MoneyIncentivesAmount).Mul(decimal.NewFromFloat(1).Sub(decimal.NewFromFloat(moneyIncentivesRuleMatched.SalesmanPercentage).Add(decimal.NewFromFloat(moneyIncentivesRuleMatched.ReferrerPercentage))).Div(decimal.NewFromFloat(100))).Float64()
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
DividendsUser: &domain.User{
UserId: undertaker.UserId,
... ... @@ -103,7 +104,7 @@ func (domainService *ConfirmMoneyIncentivesEstimateService) Confirm(contract *do
if salesmanEstimated {
return nil, fmt.Errorf("业务员 " + undertaker.Salesman.UserName + " 已分红")
} else {
undertakerDividendsAmount := moneyIncentivesRuleMatched.MoneyIncentivesAmount * (moneyIncentivesRuleMatched.SalesmanPercentage / 100)
undertakerDividendsAmount, _ := decimal.NewFromFloat(moneyIncentivesRuleMatched.MoneyIncentivesAmount).Mul(decimal.NewFromFloat(moneyIncentivesRuleMatched.SalesmanPercentage).Div(decimal.NewFromFloat(100))).Float64()
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
DividendsUser: &domain.User{
UserId: undertaker.Salesman.UserId,
... ... @@ -139,8 +140,7 @@ func (domainService *ConfirmMoneyIncentivesEstimateService) Confirm(contract *do
if referrerEstimated {
return nil, fmt.Errorf("推荐人 " + undertaker.Salesman.UserName + " 已分红")
} else {
// TODO 使用decimal提高精度
undertakerDividendsAmount := moneyIncentivesRuleMatched.MoneyIncentivesAmount * (moneyIncentivesRuleMatched.ReferrerPercentage / 100)
undertakerDividendsAmount, _ := decimal.NewFromFloat(moneyIncentivesRuleMatched.MoneyIncentivesAmount).Mul(decimal.NewFromFloat(moneyIncentivesRuleMatched.ReferrerPercentage).Div(decimal.NewFromFloat(100))).Float64()
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
DividendsUser: &domain.User{
UserId: undertaker.Referrer.UserId,
... ...
... ... @@ -211,6 +211,12 @@ func (repository *CooperationProjectRepository) FindOne(queryOptions map[string]
if cooperationProjectNumber, ok := queryOptions["cooperationProjectNumber"]; ok && cooperationProjectNumber != "" {
query.Where("cooperation_project.cooperation_project_number = ?", cooperationProjectNumber)
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query.Where("company->>'companyId' = '?'", companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query.Where("org->>'orgId' = '?'", orgId)
}
if cooperationProjectId, ok := queryOptions["cooperationProjectId"]; ok && cooperationProjectId.(int64) != 0 {
query.Where("cooperation_project.cooperation_project_id = ?", cooperationProjectId)
}
... ...
... ... @@ -112,7 +112,7 @@ func (controller *CooperationContractController) SearchCooperationContract() {
_ = controller.Unmarshal(searchCooperationContractQuery)
header := controller.GetRequestHeader(controller.Ctx)
searchCooperationContractQuery.CompanyId = header.CompanyId
searchCooperationContractQuery.OrgId = header.OrgId
//searchCooperationContractQuery.OrgId = header.OrgId
searchCooperationContractQuery.UserId = header.UserId
searchCooperationContractQuery.UserBaseId = header.UserBaseId
data, err := cooperationContractService.SearchCooperationContract(searchCooperationContractQuery)
... ...