91 lines
3.6 KiB
Go
91 lines
3.6 KiB
Go
package adminfinance
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"hfb_sys/backend/internal/auditlog"
|
|
"hfb_sys/backend/internal/database"
|
|
"hfb_sys/backend/internal/timeutil"
|
|
)
|
|
|
|
func TestOperatingExpenseCreateListAndVoid(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
if err := db.Exec(`INSERT INTO admin_users (id, nickname, username, password_hash) VALUES (7, '运营甲', 'ops_a', ''), (8, '运营乙', 'ops_b', '')`).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
repo := NewRepository(db)
|
|
occurredAt := time.Date(2026, 8, 20, 10, 30, 0, 0, timeutil.ShanghaiLocation())
|
|
created, err := repo.CreateOperatingExpense(t.Context(), CreateOperatingExpenseRequest{
|
|
Category: "推广投放",
|
|
PayeeName: "测试媒体",
|
|
AmountCent: 12800,
|
|
OccurredAt: occurredAt,
|
|
Remark: "八月推广费用",
|
|
VoucherURL: "/api/files/object?key=operating-expense%2Fvoucher.webp",
|
|
}, 7, auditlog.Meta{RequestID: "req-create"})
|
|
if err != nil {
|
|
t.Fatalf("创建运营开支失败: %v", err)
|
|
}
|
|
if created.ID == 0 || created.ExpenseNo == "" || created.Status != "paid" || created.CreatedByName != "运营甲" {
|
|
t.Fatalf("创建结果不正确: %+v", created)
|
|
}
|
|
|
|
list, err := repo.OperatingExpenses(t.Context(), OperatingExpenseQuery{
|
|
DateType: "occurred", StartDate: occurredAt.Add(-time.Hour), EndDate: occurredAt.Add(time.Hour), Page: 1, PageSize: 20,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("查询运营开支失败: %v", err)
|
|
}
|
|
if list.Total != 1 || list.Summary.PaidAmountCent != 12800 || list.Items[0].Category != "推广投放" {
|
|
t.Fatalf("运营开支列表不正确: %+v", list)
|
|
}
|
|
|
|
voided, err := repo.VoidOperatingExpense(t.Context(), created.ID, "重复录入", 8, auditlog.Meta{RequestID: "req-void"})
|
|
if err != nil {
|
|
t.Fatalf("作废运营开支失败: %v", err)
|
|
}
|
|
if voided.Status != "voided" || voided.VoidedByName != "运营乙" || voided.VoidReason != "重复录入" {
|
|
t.Fatalf("作废结果不正确: %+v", voided)
|
|
}
|
|
if _, err := repo.VoidOperatingExpense(t.Context(), created.ID, "重复作废", 8, auditlog.Meta{}); !errors.Is(err, ErrOperatingExpenseNotPaid) {
|
|
t.Fatalf("重复作废错误 = %v, want ErrOperatingExpenseNotPaid", err)
|
|
}
|
|
}
|
|
|
|
func TestOperatingExpenseDashboardSummaryExcludesVoided(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
loc := timeutil.ShanghaiLocation()
|
|
inRange := time.Date(2026, 8, 20, 10, 0, 0, 0, loc)
|
|
if err := db.Exec(`INSERT INTO operating_expenses
|
|
(id, expense_no, category, payee_name, remark, created_by, amount_cent, occurred_at, status) VALUES
|
|
(1, 'OE-1', '推广投放', '测试收款方', '测试备注', 1, 12000, ?, 'paid'),
|
|
(2, 'OE-2', '推广投放', '测试收款方', '测试备注', 1, 8000, ?, 'voided'),
|
|
(3, 'OE-3', '推广投放', '测试收款方', '测试备注', 1, 5000, ?, 'paid')`,
|
|
inRange, inRange, inRange.AddDate(0, 0, -1)).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
summary, err := NewRepository(db).operatingExpenseSummary(t.Context(), DashboardQuery{
|
|
StartDate: time.Date(2026, 8, 20, 0, 0, 0, 0, loc),
|
|
EndDate: time.Date(2026, 8, 20, 23, 59, 59, 0, loc),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("运营开支仪表盘统计失败: %v", err)
|
|
}
|
|
if summary.AmountCent != 12000 || summary.Count != 1 {
|
|
t.Fatalf("运营开支统计 = %+v, want 12000/1", summary)
|
|
}
|
|
daily, err := NewRepository(db).dailyOperatingExpenses(t.Context(), DashboardQuery{
|
|
StartDate: time.Date(2026, 8, 20, 0, 0, 0, 0, loc),
|
|
EndDate: time.Date(2026, 8, 20, 23, 59, 59, 0, loc),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("每日运营开支统计失败: %v", err)
|
|
}
|
|
if len(daily) != 1 || daily[0].Date != "2026-08-20" || daily[0].AmountCent != 12000 || daily[0].Count != 1 {
|
|
t.Fatalf("每日运营开支统计 = %+v, want 2026-08-20/12000/1", daily)
|
|
}
|
|
}
|