mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
feat[BE]: refine customer payment report structure by removing unused fields and enhancing query logic for better performance
This commit is contained in:
@@ -20,6 +20,7 @@ import (
|
||||
marketingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/repositories"
|
||||
areaDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
|
||||
customerDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/dto"
|
||||
customerRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/repositories"
|
||||
supplierDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/dto"
|
||||
warehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/dto"
|
||||
chickinRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/repositories"
|
||||
@@ -59,6 +60,7 @@ type repportService struct {
|
||||
HppPerKandangRepo repportRepo.HppPerKandangRepository
|
||||
ProductionResultRepo repportRepo.ProductionResultRepository
|
||||
CustomerPaymentRepo repportRepo.CustomerPaymentRepository
|
||||
CustomerRepo customerRepo.CustomerRepository
|
||||
}
|
||||
|
||||
type HppCostAggregate struct {
|
||||
@@ -84,6 +86,7 @@ func NewRepportService(
|
||||
hppPerKandangRepo repportRepo.HppPerKandangRepository,
|
||||
productionResultRepo repportRepo.ProductionResultRepository,
|
||||
customerPaymentRepo repportRepo.CustomerPaymentRepository,
|
||||
customerRepo customerRepo.CustomerRepository,
|
||||
) RepportService {
|
||||
return &repportService{
|
||||
Log: utils.Log,
|
||||
@@ -100,6 +103,7 @@ func NewRepportService(
|
||||
HppPerKandangRepo: hppPerKandangRepo,
|
||||
ProductionResultRepo: productionResultRepo,
|
||||
CustomerPaymentRepo: customerPaymentRepo,
|
||||
CustomerRepo: customerRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -356,7 +360,6 @@ func (s *repportService) GetCustomerPayment(ctx *fiber.Ctx, params *validation.C
|
||||
}
|
||||
}
|
||||
|
||||
// Process each customer
|
||||
var result []dto.CustomerPaymentReportItem
|
||||
for _, customerID := range customerIDs {
|
||||
item, err := s.processCustomerPayment(ctx.Context(), customerID, params)
|
||||
@@ -370,10 +373,9 @@ func (s *repportService) GetCustomerPayment(ctx *fiber.Ctx, params *validation.C
|
||||
}
|
||||
|
||||
func (s *repportService) processCustomerPayment(ctx context.Context, customerID uint, params *validation.CustomerPaymentQuery) (dto.CustomerPaymentReportItem, error) {
|
||||
customer := entity.Customer{}
|
||||
if err := s.DB.WithContext(ctx).
|
||||
Where("id = ?", customerID).
|
||||
First(&customer).Error; err != nil {
|
||||
|
||||
customer, err := s.CustomerRepo.GetByID(ctx, customerID, nil)
|
||||
if err != nil {
|
||||
return dto.CustomerPaymentReportItem{}, err
|
||||
}
|
||||
|
||||
@@ -407,7 +409,6 @@ func (s *repportService) processCustomerPayment(ctx context.Context, customerID
|
||||
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
|
||||
@@ -418,7 +419,6 @@ func (s *repportService) processCustomerPayment(ctx context.Context, customerID
|
||||
row.AgingDay = &days
|
||||
}
|
||||
} else {
|
||||
// Aging = current_date - trans_date (SO date)
|
||||
days := int(time.Since(tx.TransDate).Hours() / 24)
|
||||
if days < 0 {
|
||||
days = 0
|
||||
@@ -455,22 +455,18 @@ func (s *repportService) processCustomerPayment(ctx context.Context, customerID
|
||||
if err != nil {
|
||||
return dto.CustomerPaymentReportItem{}, err
|
||||
}
|
||||
// End date should be inclusive, so set to end of day
|
||||
endOfDay := time.Date(parsed.Year(), parsed.Month(), parsed.Day(), 23, 59, 59, 999999999, location)
|
||||
endDate = &endOfDay
|
||||
}
|
||||
|
||||
for _, row := range rows {
|
||||
transDate := row.TransDate.In(location)
|
||||
|
||||
// Check if transaction date is within range
|
||||
if startDate != nil && transDate.Before(*startDate) {
|
||||
continue
|
||||
}
|
||||
if endDate != nil && transDate.After(*endDate) {
|
||||
continue
|
||||
}
|
||||
|
||||
filteredRows = append(filteredRows, row)
|
||||
}
|
||||
|
||||
@@ -480,7 +476,7 @@ func (s *repportService) processCustomerPayment(ctx context.Context, customerID
|
||||
summary := dto.CalculateCustomerPaymentSummary(rows, initialBalance)
|
||||
|
||||
return dto.CustomerPaymentReportItem{
|
||||
Customer: customerDTO.ToCustomerRelationDTO(customer),
|
||||
Customer: customerDTO.ToCustomerRelationDTO(*customer),
|
||||
InitialBalance: initialBalance,
|
||||
Rows: rows,
|
||||
Summary: summary,
|
||||
|
||||
Reference in New Issue
Block a user