支持提号完成后财务调整
This commit is contained in:
@@ -52,13 +52,25 @@ func (r *Repository) pickupSummary(ctx context.Context, query DashboardQuery) (*
|
||||
Scan(&row).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var adjustments struct {
|
||||
SettledAmountCent int64
|
||||
ProfitAmountCent int64
|
||||
}
|
||||
if err := db.Table("admin_pickup_financial_adjustments AS fa").
|
||||
Select(`COALESCE(SUM(fa.settle_delta_cent), 0) AS settled_amount_cent,
|
||||
COALESCE(SUM(fa.profit_delta_cent), 0) AS profit_amount_cent`).
|
||||
Joins("JOIN admin_pickups AS p ON p.id = fa.pickup_id AND p.status = ?", "completed").
|
||||
Where("fa.created_at >= ? AND fa.created_at <= ?", query.StartDate, query.EndDate).
|
||||
Scan(&adjustments).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var inProgress int64
|
||||
if err := db.Table("admin_pickups").Where("status = ?", "picking_up").Count(&inProgress).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &PickupSummaryDTO{
|
||||
SettledAmountCent: row.SettledAmountCent,
|
||||
ProfitAmountCent: row.ProfitAmountCent,
|
||||
SettledAmountCent: row.SettledAmountCent + adjustments.SettledAmountCent,
|
||||
ProfitAmountCent: row.ProfitAmountCent + adjustments.ProfitAmountCent,
|
||||
CompletedCount: row.CompletedCount,
|
||||
InProgressCount: inProgress,
|
||||
}, nil
|
||||
@@ -257,6 +269,16 @@ func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]Fi
|
||||
Scan(&pickupProfits).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pickupAdjustments := make([]dailyPickupAdjustmentRow, 0)
|
||||
if err := db.Table("admin_pickup_financial_adjustments AS fa").
|
||||
Select(`DATE(fa.created_at) AS date,
|
||||
COALESCE(SUM(fa.profit_delta_cent), 0) AS profit_amount_cent`).
|
||||
Joins("JOIN admin_pickups AS p ON p.id = fa.pickup_id AND p.status = ?", "completed").
|
||||
Where("fa.created_at >= ? AND fa.created_at <= ?", query.StartDate, query.EndDate).
|
||||
Group("DATE(fa.created_at)").
|
||||
Scan(&pickupAdjustments).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 取消提号按取消日统计,与「已付款订单数」中的提号创建口径区分开。
|
||||
pickupCancelled, err := r.dailyPickupCancelled(ctx, query)
|
||||
@@ -335,6 +357,13 @@ func (r *Repository) dailyItems(ctx context.Context, query DashboardQuery) ([]Fi
|
||||
item.SalesCount += row.CompletedCount
|
||||
itemsByDate[date] = item
|
||||
}
|
||||
for _, row := range pickupAdjustments {
|
||||
date := dailyDateKey(row.Date)
|
||||
item := itemsByDate[date]
|
||||
item.Date = date
|
||||
item.PlatformIncomeAmountCent += row.ProfitAmountCent
|
||||
itemsByDate[date] = item
|
||||
}
|
||||
for _, row := range pickupCancelled {
|
||||
date := dailyDateKey(row.Date)
|
||||
item := itemsByDate[date]
|
||||
@@ -496,6 +525,12 @@ type dailyPickupProfitRow struct {
|
||||
CompletedCount int64
|
||||
}
|
||||
|
||||
// dailyPickupAdjustmentRow 调整按调整创建日进入当日利润,避免回写历史完成日报。
|
||||
type dailyPickupAdjustmentRow struct {
|
||||
Date string
|
||||
ProfitAmountCent int64
|
||||
}
|
||||
|
||||
type dailyEstimatedRow struct {
|
||||
Date string
|
||||
EstimatedIncomeAmountCent int64
|
||||
|
||||
Reference in New Issue
Block a user