mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
Merge branch 'development' of https://gitlab.com/mbugroup/lti-api into FEAT/BE/report_customer_payment
This commit is contained in:
@@ -858,7 +858,7 @@ 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 == "" || strings.EqualFold(strings.TrimSpace(params.FilterBy), "do_date") {
|
||||
if params.FilterBy == "" {
|
||||
params.FilterBy = "received_date"
|
||||
}
|
||||
|
||||
@@ -897,25 +897,8 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
|
||||
}
|
||||
|
||||
purchasesBySupplier := make(map[uint][]entity.Purchase, len(supplierIDs))
|
||||
references := make([]string, 0)
|
||||
seenRefs := make(map[string]struct{})
|
||||
for _, purchase := range purchases {
|
||||
supplierID := purchase.SupplierId
|
||||
purchasesBySupplier[supplierID] = append(purchasesBySupplier[supplierID], purchase)
|
||||
|
||||
reference := purchase.PrNumber
|
||||
if purchase.PoNumber != nil && strings.TrimSpace(*purchase.PoNumber) != "" {
|
||||
reference = *purchase.PoNumber
|
||||
}
|
||||
if _, exists := seenRefs[reference]; !exists {
|
||||
seenRefs[reference] = struct{}{}
|
||||
references = append(references, reference)
|
||||
}
|
||||
}
|
||||
|
||||
paymentTotals, err := s.DebtSupplierRepo.GetPaymentTotalsByReferences(c.Context(), supplierIDs, references)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
purchasesBySupplier[purchase.SupplierId] = append(purchasesBySupplier[purchase.SupplierId], purchase)
|
||||
}
|
||||
|
||||
paymentsBySupplier := make(map[uint][]entity.Payment, len(supplierIDs))
|
||||
@@ -940,6 +923,14 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
|
||||
now := time.Now().In(location)
|
||||
|
||||
result := make([]dto.DebtSupplierDTO, 0, len(supplierIDs))
|
||||
type debtSupplierRowItem struct {
|
||||
Row dto.DebtSupplierRowDTO
|
||||
SortTime time.Time
|
||||
Order int
|
||||
DeltaBalance float64
|
||||
CountTotals bool
|
||||
}
|
||||
|
||||
for _, supplierID := range supplierIDs {
|
||||
supplier, exists := supplierMap[supplierID]
|
||||
if !exists {
|
||||
@@ -947,23 +938,13 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
|
||||
}
|
||||
|
||||
initialBalance := initialPaymentTotals[supplierID] - initialPurchaseTotals[supplierID]
|
||||
|
||||
items := purchasesBySupplier[supplierID]
|
||||
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)
|
||||
row := buildDebtSupplierRow(purchase, now, location)
|
||||
sortTime := resolveDebtSupplierSortTime(purchase, params.FilterBy, location)
|
||||
combinedRows = append(combinedRows, debtSupplierRowItem{
|
||||
Row: row,
|
||||
@@ -996,6 +977,7 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
|
||||
balance := initialBalance
|
||||
for i := range combinedRows {
|
||||
balance += combinedRows[i].DeltaBalance
|
||||
combinedRows[i].Row.DebtPrice = balance
|
||||
combinedRows[i].Row.Balance = balance
|
||||
|
||||
if combinedRows[i].CountTotals {
|
||||
@@ -1004,13 +986,13 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
|
||||
total.Aging = row.Aging
|
||||
}
|
||||
total.TotalPrice += row.TotalPrice
|
||||
total.PaymentPrice += row.PaymentPrice
|
||||
total.DebtPrice += row.DebtPrice
|
||||
} else {
|
||||
combinedRows[i].Row.DebtPrice = balance
|
||||
total.PaymentPrice += combinedRows[i].Row.PaymentPrice
|
||||
}
|
||||
}
|
||||
total.DebtPrice = balance
|
||||
|
||||
rows := make([]dto.DebtSupplierRowDTO, 0, len(combinedRows))
|
||||
sortDesc := strings.EqualFold(params.SortOrder, "desc")
|
||||
if sortDesc {
|
||||
for i := len(combinedRows) - 1; i >= 0; i-- {
|
||||
@@ -1039,18 +1021,13 @@ func (s *repportService) GetDebtSupplier(c *fiber.Ctx, params *validation.DebtSu
|
||||
return result, totalSuppliers, nil
|
||||
}
|
||||
|
||||
func buildDebtSupplierRow(purchase entity.Purchase, paymentTotals map[string]float64, now time.Time, loc *time.Location) dto.DebtSupplierRowDTO {
|
||||
func buildDebtSupplierRow(purchase entity.Purchase, now time.Time, loc *time.Location) dto.DebtSupplierRowDTO {
|
||||
prNumber := purchase.PrNumber
|
||||
poNumber := ""
|
||||
if purchase.PoNumber != nil {
|
||||
poNumber = *purchase.PoNumber
|
||||
}
|
||||
|
||||
reference := prNumber
|
||||
if strings.TrimSpace(poNumber) != "" {
|
||||
reference = poNumber
|
||||
}
|
||||
|
||||
prDate := purchase.CreatedAt.In(loc)
|
||||
startDate := time.Date(prDate.Year(), prDate.Month(), prDate.Day(), 0, 0, 0, 0, loc)
|
||||
endDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc)
|
||||
@@ -1093,9 +1070,6 @@ func buildDebtSupplierRow(purchase entity.Purchase, paymentTotals map[string]flo
|
||||
}
|
||||
}
|
||||
|
||||
paymentPrice := paymentTotals[reference]
|
||||
debtPrice := paymentPrice - totalPrice
|
||||
|
||||
dueDate := ""
|
||||
dueStatus := "-"
|
||||
if purchase.DueDate != nil && !purchase.DueDate.IsZero() {
|
||||
@@ -1109,10 +1083,6 @@ func buildDebtSupplierRow(purchase entity.Purchase, paymentTotals map[string]flo
|
||||
}
|
||||
|
||||
status := "Belum Lunas"
|
||||
if debtPrice >= 0 {
|
||||
status = "Lunas"
|
||||
}
|
||||
|
||||
poDate := ""
|
||||
if purchase.PoDate != nil && !purchase.PoDate.IsZero() {
|
||||
poDate = purchase.PoDate.In(loc).Format("2006-01-02")
|
||||
@@ -1129,10 +1099,11 @@ func buildDebtSupplierRow(purchase entity.Purchase, paymentTotals map[string]flo
|
||||
DueDate: dueDate,
|
||||
DueStatus: dueStatus,
|
||||
TotalPrice: totalPrice,
|
||||
PaymentPrice: paymentPrice,
|
||||
DebtPrice: debtPrice,
|
||||
PaymentPrice: 0,
|
||||
DebtPrice: 0,
|
||||
Status: status,
|
||||
TravelNumber: travelNumber,
|
||||
Balance: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1162,32 +1133,30 @@ func buildDebtSupplierPaymentRow(payment entity.Payment, loc *time.Location) dto
|
||||
DebtPrice: 0,
|
||||
Status: "Pembayaran",
|
||||
TravelNumber: "-",
|
||||
Balance: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func resolveDebtSupplierSortTime(purchase entity.Purchase, filterBy string, loc *time.Location) time.Time {
|
||||
switch strings.ToLower(strings.TrimSpace(filterBy)) {
|
||||
case "po_date":
|
||||
if strings.EqualFold(strings.TrimSpace(filterBy), "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
|
||||
}
|
||||
}
|
||||
|
||||
earliest := time.Time{}
|
||||
for _, item := range purchase.Items {
|
||||
if item.ReceivedDate == nil || item.ReceivedDate.IsZero() {
|
||||
continue
|
||||
}
|
||||
if !earliest.IsZero() {
|
||||
return earliest
|
||||
received := item.ReceivedDate.In(loc)
|
||||
if earliest.IsZero() || received.Before(earliest) {
|
||||
earliest = received
|
||||
}
|
||||
}
|
||||
if !earliest.IsZero() {
|
||||
return earliest
|
||||
}
|
||||
|
||||
return purchase.CreatedAt.In(loc)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user