[FIX/BE] adjustment response

This commit is contained in:
ragilap
2026-01-12 16:28:03 +07:00
parent 09f1b29359
commit ac5edb36e7
4 changed files with 277 additions and 25 deletions
@@ -642,8 +642,8 @@ func (s *repportService) GetPurchaseSupplier(c *fiber.Ctx, params *validation.Pu
}
func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSupplierQuery) ([]dto.DebtSupplierDTO, int64, error) {
if params.FilterBy == "" {
params.FilterBy = "do_date"
if params.FilterBy == "" || strings.EqualFold(strings.TrimSpace(params.FilterBy), "do_date") {
params.FilterBy = "received_date"
}
if err := s.Validate.Struct(params); err != nil {
@@ -675,6 +675,11 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
return nil, 0, err
}
payments, err := s.DebtSupplierRepo.GetPaymentsBySuppliers(c.Context(), supplierIDs, params)
if err != nil {
return nil, 0, err
}
purchasesBySupplier := make(map[uint][]entity.Purchase, len(supplierIDs))
references := make([]string, 0)
seenRefs := make(map[string]struct{})
@@ -697,6 +702,21 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
return nil, 0, err
}
paymentsBySupplier := make(map[uint][]entity.Payment, len(supplierIDs))
for _, payment := range payments {
paymentsBySupplier[payment.PartyId] = append(paymentsBySupplier[payment.PartyId], payment)
}
initialPurchaseTotals, err := s.DebtSupplierRepo.GetPurchaseTotalsBeforeDate(c.Context(), supplierIDs, params)
if err != nil {
return nil, 0, err
}
initialPaymentTotals, err := s.DebtSupplierRepo.GetPaymentTotalsBeforeDate(c.Context(), supplierIDs, params)
if err != nil {
return nil, 0, err
}
location, err := time.LoadLocation("Asia/Jakarta")
if err != nil {
return nil, 0, fiber.NewError(fiber.StatusInternalServerError, "failed to load timezone configuration")
@@ -710,29 +730,81 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
continue
}
initialBalance := initialPaymentTotals[supplierID] - initialPurchaseTotals[supplierID]
items := purchasesBySupplier[supplierID]
rows := make([]dto.DebtSupplierRowDTO, 0, len(items))
paymentItems := paymentsBySupplier[supplierID]
rows := make([]dto.DebtSupplierRowDTO, 0, len(items)+len(paymentItems))
total := dto.DebtSupplierTotalDTO{}
type debtSupplierRowItem struct {
Row dto.DebtSupplierRowDTO
SortTime time.Time
Order int
DeltaBalance float64
CountTotals bool
}
combinedRows := make([]debtSupplierRowItem, 0, len(items)+len(paymentItems))
for _, purchase := range items {
row := buildDebtSupplierRow(purchase, paymentTotals, now, location)
rows = append(rows, row)
sortTime := resolveDebtSupplierSortTime(purchase, params.FilterBy, location)
combinedRows = append(combinedRows, debtSupplierRowItem{
Row: row,
SortTime: sortTime,
Order: 0,
DeltaBalance: -row.TotalPrice,
CountTotals: true,
})
}
if row.Aging > total.Aging {
total.Aging = row.Aging
for _, payment := range paymentItems {
row := buildDebtSupplierPaymentRow(payment, location)
sortTime := payment.PaymentDate.In(location)
combinedRows = append(combinedRows, debtSupplierRowItem{
Row: row,
SortTime: sortTime,
Order: 1,
DeltaBalance: payment.Nominal,
CountTotals: false,
})
}
sort.SliceStable(combinedRows, func(i, j int) bool {
if combinedRows[i].SortTime.Equal(combinedRows[j].SortTime) {
return combinedRows[i].Order < combinedRows[j].Order
}
return combinedRows[i].SortTime.Before(combinedRows[j].SortTime)
})
balance := initialBalance
for i := range combinedRows {
balance += combinedRows[i].DeltaBalance
combinedRows[i].Row.Balance = balance
if combinedRows[i].CountTotals {
row := combinedRows[i].Row
if row.Aging > total.Aging {
total.Aging = row.Aging
}
total.TotalPrice += row.TotalPrice
total.PaymentPrice += row.PaymentPrice
total.DebtPrice += row.DebtPrice
} else {
combinedRows[i].Row.DebtPrice = balance
}
total.TotalPrice += row.TotalPrice
total.PaymentPrice += row.PaymentPrice
total.DebtPrice += row.DebtPrice
}
sortDesc := strings.EqualFold(params.SortOrder, "desc")
sort.SliceStable(rows, func(i, j int) bool {
if sortDesc {
return rows[i].PrDate > rows[j].PrDate
if sortDesc {
for i := len(combinedRows) - 1; i >= 0; i-- {
rows = append(rows, combinedRows[i].Row)
}
return rows[i].PrDate < rows[j].PrDate
})
} else {
for i := range combinedRows {
rows = append(rows, combinedRows[i].Row)
}
}
var supplierDTORef *supplierDTO.SupplierRelationDTO
if supplier.Id != 0 {
@@ -741,9 +813,10 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
}
result = append(result, dto.DebtSupplierDTO{
Supplier: supplierDTORef,
Rows: rows,
Total: total,
Supplier: supplierDTORef,
InitialBalance: initialBalance,
Rows: rows,
Total: total,
})
}
@@ -769,6 +842,7 @@ func buildDebtSupplierRow(purchase entity.Purchase, paymentTotals map[string]flo
totalPrice := 0.0
travelNumber := "-"
receivedDate := ""
var area *areaDTO.AreaRelationDTO
var warehouse *warehouseDTO.WarehouseRelationDTO
@@ -787,8 +861,19 @@ func buildDebtSupplierRow(purchase entity.Purchase, paymentTotals map[string]flo
}
}
earliestReceived := time.Time{}
for _, item := range purchase.Items {
totalPrice += item.TotalPrice
if item.ReceivedDate == nil || item.ReceivedDate.IsZero() {
continue
}
received := item.ReceivedDate.In(loc)
if earliestReceived.IsZero() || received.Before(earliestReceived) {
earliestReceived = received
}
}
if !earliestReceived.IsZero() {
receivedDate = earliestReceived.Format("2006-01-02")
}
}
@@ -820,8 +905,8 @@ func buildDebtSupplierRow(purchase entity.Purchase, paymentTotals map[string]flo
return dto.DebtSupplierRowDTO{
PrNumber: prNumber,
PoNumber: poNumber,
PrDate: prDate.Format("2006-01-02"),
PoDate: poDate,
ReceivedDate: receivedDate,
Aging: aging,
Area: area,
Warehouse: warehouse,
@@ -835,6 +920,62 @@ func buildDebtSupplierRow(purchase entity.Purchase, paymentTotals map[string]flo
}
}
func buildDebtSupplierPaymentRow(payment entity.Payment, loc *time.Location) dto.DebtSupplierRowDTO {
referenceNumber := ""
if payment.ReferenceNumber != nil {
referenceNumber = *payment.ReferenceNumber
}
prNumber := payment.PaymentCode
if strings.TrimSpace(prNumber) == "" {
prNumber = referenceNumber
}
return dto.DebtSupplierRowDTO{
PrNumber: prNumber,
PoNumber: referenceNumber,
PoDate: "-",
ReceivedDate: payment.PaymentDate.In(loc).Format("2006-01-02"),
Aging: 0,
Area: nil,
Warehouse: nil,
DueDate: "-",
DueStatus: "-",
TotalPrice: 0,
PaymentPrice: payment.Nominal,
DebtPrice: 0,
Status: "Pembayaran",
TravelNumber: "-",
}
}
func resolveDebtSupplierSortTime(purchase entity.Purchase, filterBy string, loc *time.Location) time.Time {
switch strings.ToLower(strings.TrimSpace(filterBy)) {
case "po_date":
if purchase.PoDate != nil && !purchase.PoDate.IsZero() {
return purchase.PoDate.In(loc)
}
case "pr_date":
return purchase.CreatedAt.In(loc)
default:
earliest := time.Time{}
for _, item := range purchase.Items {
if item.ReceivedDate == nil || item.ReceivedDate.IsZero() {
continue
}
received := item.ReceivedDate.In(loc)
if earliest.IsZero() || received.Before(earliest) {
earliest = received
}
}
if !earliest.IsZero() {
return earliest
}
}
return purchase.CreatedAt.In(loc)
}
func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangResponseData, *dto.HppPerKandangMetaDTO, error) {
params, filters, err := s.parseHppPerKandangQuery(ctx)
if err != nil {