feat[BE]: update customer payment report to support multiple customer IDs and nullable aging days

This commit is contained in:
aguhh18
2026-01-14 14:06:34 +07:00
parent f6e872c0aa
commit 7daa509cd0
5 changed files with 109 additions and 38 deletions
@@ -324,10 +324,14 @@ func (s *repportService) GetCustomerPayment(ctx *fiber.Ctx, params *validation.C
var customerIDs []uint
var totalCustomers int64
if params.CustomerID != nil {
// Single customer mode
customerIDs = []uint{*params.CustomerID}
totalCustomers = 1
if len(params.CustomerIDs) > 0 {
// Specific customer IDs mode (no pagination)
customerIDs = params.CustomerIDs
totalCustomers = int64(len(customerIDs))
if len(customerIDs) == 0 {
return []dto.CustomerPaymentReportItem{}, 0, nil
}
} else {
// Multiple customers mode with pagination
page := params.Page
@@ -366,7 +370,6 @@ func (s *repportService) GetCustomerPayment(ctx *fiber.Ctx, params *validation.C
}
func (s *repportService) processCustomerPayment(ctx context.Context, customerID uint) (dto.CustomerPaymentReportItem, error) {
// Get customer info
customer := entity.Customer{}
if err := s.DB.WithContext(ctx).
Where("id = ?", customerID).
@@ -374,24 +377,21 @@ func (s *repportService) processCustomerPayment(ctx context.Context, customerID
return dto.CustomerPaymentReportItem{}, err
}
// Get initial balance
initialBalance, err := s.CustomerPaymentRepo.GetInitialBalanceByCustomer(ctx, customerID)
if err != nil {
return dto.CustomerPaymentReportItem{}, err
}
// Get transactions for this customer
cid := customerID
transactions, err := s.CustomerPaymentRepo.GetCustomerPaymentTransactions(ctx, &cid)
if err != nil {
return dto.CustomerPaymentReportItem{}, err
}
// Process transactions and calculate running balance
rows := make([]dto.CustomerPaymentReportRow, 0, len(transactions))
runningBalance := initialBalance
for _, tx := range transactions {
for i, tx := range transactions {
row := dto.CustomerPaymentReportRow{
TransactionType: tx.TransactionType,
TransactionID: tx.TransactionID,
@@ -412,26 +412,48 @@ func (s *repportService) processCustomerPayment(ctx context.Context, customerID
SalesPerson: tx.SalesPerson,
}
// Calculate running balance
previousBalance := runningBalance
if tx.TransactionType == "SALES" {
runningBalance -= tx.TotalPrice
// Status will be calculated later (requires looking ahead)
row.Status = ""
row.AgingDay = 0 // Will be calculated later
status, paymentDate := s.determineSalesStatusAndPaymentDate(transactions, i, previousBalance, runningBalance)
row.Status = status
if status == "LUNAS" {
if previousBalance >= tx.TotalPrice {
days := 0
row.AgingDay = &days
} else if paymentDate != nil {
// Aging = payment_date - trans_date (SO date)
days := int(paymentDate.Sub(tx.TransDate).Hours() / 24)
if days < 0 {
days = 0
}
row.AgingDay = &days
} else {
days := 0
row.AgingDay = &days
}
} else {
// Aging = current_date - trans_date (SO date)
days := int(time.Since(tx.TransDate).Hours() / 24)
if days < 0 {
days = 0
}
row.AgingDay = &days
}
} else if tx.TransactionType == "PAYMENT" {
runningBalance += tx.PaymentAmount
row.Status = ""
row.AgingDay = 0
row.AgingDay = nil
}
row.AccountsReceivable = runningBalance
rows = append(rows, row)
}
// Calculate summary
summary := s.calculateSummary(rows, initialBalance)
// Build customer DTO
customerDTO := customerDTO.CustomerRelationDTO{
Id: customer.Id,
Name: customer.Name,
@@ -466,12 +488,55 @@ func (s *repportService) calculateSummary(rows []dto.CustomerPaymentReportRow, i
}
}
// Final AR = initial balance + total sales - total payment
summary.TotalAccountsReceivable = initialBalance + summary.TotalGrandAmount - summary.TotalPayment
// Formula: Total AR = Initial Balance - Total Sales + Total Payment
// - Initial balance: positive (customer deposit)
// - Sales: reduces balance (customer debt)
// - Payment: increases balance (customer pays)
summary.TotalAccountsReceivable = initialBalance - summary.TotalGrandAmount + summary.TotalPayment
return summary
}
func (s *repportService) determineSalesStatusAndPaymentDate(transactions []repportRepo.CustomerPaymentTransaction, currentIndex int, previousBalance, currentBalance float64) (string, *time.Time) {
currentSales := transactions[currentIndex]
// Status Logic:
// 1. LUNAS: previousBalance >= salesAmount (paid from deposit)
// 2. LUNAS: future payments make AR >= 0 (eventually paid)
// 3. DIBAYAR SEBAGIAN: has payment but not enough
// 4. BELUM LUNAS: no payment at all
if previousBalance >= currentSales.TotalPrice {
return "LUNAS", nil
}
hasPartialPaymentFromBalance := previousBalance > 0 && previousBalance < currentSales.TotalPrice
futureBalance := currentBalance
hasPayment := false
var paymentDateThatMadeItLunas *time.Time
for i := currentIndex + 1; i < len(transactions); i++ {
if transactions[i].TransactionType == "PAYMENT" {
futureBalance += transactions[i].PaymentAmount
hasPayment = true
if futureBalance >= 0 {
paymentDateThatMadeItLunas = &transactions[i].TransDate
return "LUNAS", paymentDateThatMadeItLunas
}
} else if transactions[i].TransactionType == "SALES" {
futureBalance -= transactions[i].TotalPrice
}
}
if hasPayment || hasPartialPaymentFromBalance {
return "DIBAYAR SEBAGIAN", nil
}
return "BELUM LUNAS", nil
}
func mapRecordingToProductionResultDTO(record entity.Recording) dto.ProductionResultDTO {
result := dto.ProductionResultDTO{
CreatedAt: record.CreatedAt,