mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
[FIX/BE-US] recording,reporting,closing and uniformity
This commit is contained in:
@@ -1129,6 +1129,17 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
initialBalanceTotals, err := s.DebtSupplierRepo.GetInitialBalanceTotals(c.Context(), supplierIDs)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
references := collectDebtSupplierReferences(purchases)
|
||||
paymentSummaries, err := s.DebtSupplierRepo.GetPaymentSummariesByReferences(c.Context(), supplierIDs, references)
|
||||
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")
|
||||
@@ -1150,7 +1161,7 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
|
||||
continue
|
||||
}
|
||||
|
||||
initialBalance := initialPaymentTotals[supplierID] - initialPurchaseTotals[supplierID]
|
||||
initialBalance := initialBalanceTotals[supplierID] + (initialPaymentTotals[supplierID] - initialPurchaseTotals[supplierID])
|
||||
items := purchasesBySupplier[supplierID]
|
||||
paymentItems := paymentsBySupplier[supplierID]
|
||||
total := dto.DebtSupplierTotalDTO{}
|
||||
@@ -1158,6 +1169,16 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
|
||||
combinedRows := make([]debtSupplierRowItem, 0, len(items)+len(paymentItems))
|
||||
for _, purchase := range items {
|
||||
row := buildDebtSupplierRow(purchase, now, location)
|
||||
if reference := resolveDebtSupplierReference(purchase); reference != "" {
|
||||
if summary, ok := paymentSummaries[reference]; ok {
|
||||
if isDebtSupplierPaid(row.TotalPrice, summary.Total) {
|
||||
row.Status = "Lunas"
|
||||
if !summary.LatestPaymentDate.IsZero() {
|
||||
row.Aging = calculateDebtSupplierAging(purchase, summary.LatestPaymentDate, location)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
sortTime := resolveDebtSupplierSortTime(purchase, params.FilterBy, location)
|
||||
combinedRows = append(combinedRows, debtSupplierRowItem{
|
||||
Row: row,
|
||||
@@ -1374,6 +1395,55 @@ func resolveDebtSupplierSortTime(purchase entity.Purchase, filterBy string, loc
|
||||
return purchase.CreatedAt.In(loc)
|
||||
}
|
||||
|
||||
func collectDebtSupplierReferences(purchases []entity.Purchase) []string {
|
||||
if len(purchases) == 0 {
|
||||
return nil
|
||||
}
|
||||
seen := make(map[string]struct{}, len(purchases))
|
||||
result := make([]string, 0, len(purchases))
|
||||
for _, purchase := range purchases {
|
||||
ref := resolveDebtSupplierReference(purchase)
|
||||
if ref == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[ref]; ok {
|
||||
continue
|
||||
}
|
||||
seen[ref] = struct{}{}
|
||||
result = append(result, ref)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func resolveDebtSupplierReference(purchase entity.Purchase) string {
|
||||
if purchase.PoNumber != nil {
|
||||
if ref := strings.TrimSpace(*purchase.PoNumber); ref != "" {
|
||||
return ref
|
||||
}
|
||||
}
|
||||
if ref := strings.TrimSpace(purchase.PrNumber); ref != "" {
|
||||
return ref
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func isDebtSupplierPaid(totalPrice, paymentTotal float64) bool {
|
||||
if totalPrice <= 0 {
|
||||
return true
|
||||
}
|
||||
return paymentTotal >= totalPrice-0.000001
|
||||
}
|
||||
|
||||
func calculateDebtSupplierAging(purchase entity.Purchase, endDate time.Time, loc *time.Location) int {
|
||||
prDate := purchase.CreatedAt.In(loc)
|
||||
startDate := time.Date(prDate.Year(), prDate.Month(), prDate.Day(), 0, 0, 0, 0, loc)
|
||||
stopDate := time.Date(endDate.Year(), endDate.Month(), endDate.Day(), 0, 0, 0, 0, loc)
|
||||
if stopDate.Before(startDate) {
|
||||
return 0
|
||||
}
|
||||
return int(stopDate.Sub(startDate).Hours() / 24)
|
||||
}
|
||||
|
||||
func (s *repportService) GetHppPerKandang(ctx *fiber.Ctx) (*dto.HppPerKandangResponseData, *dto.HppPerKandangMetaDTO, error) {
|
||||
params, filters, err := s.parseHppPerKandangQuery(ctx)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user