mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 21:41:55 +00:00
add api monitoring saldo customer
This commit is contained in:
@@ -52,6 +52,7 @@ type RepportService interface {
|
||||
GetHppV2Breakdown(ctx *fiber.Ctx, params *validation.HppV2BreakdownQuery) (*approvalService.HppV2Breakdown, error)
|
||||
GetProductionResult(ctx *fiber.Ctx, params *validation.ProductionResultQuery) ([]dto.ProductionResultDTO, int64, error)
|
||||
GetCustomerPayment(ctx *fiber.Ctx, params *validation.CustomerPaymentQuery) ([]dto.CustomerPaymentReportItem, int64, error)
|
||||
GetBalanceMonitoring(ctx *fiber.Ctx, params *validation.BalanceMonitoringQuery) ([]dto.BalanceMonitoringRowDTO, dto.BalanceMonitoringTotalsDTO, int64, error)
|
||||
DB() *gorm.DB
|
||||
}
|
||||
|
||||
@@ -74,6 +75,7 @@ type repportService struct {
|
||||
HppPerKandangRepo repportRepo.HppPerKandangRepository
|
||||
ProductionResultRepo repportRepo.ProductionResultRepository
|
||||
CustomerPaymentRepo repportRepo.CustomerPaymentRepository
|
||||
BalanceMonitoringRepo repportRepo.BalanceMonitoringRepository
|
||||
CustomerRepo customerRepo.CustomerRepository
|
||||
StandardGrowthDetailRepo productionStandardRepository.StandardGrowthDetailRepository
|
||||
ProductionStandardDetailRepo productionStandardRepository.ProductionStandardDetailRepository
|
||||
@@ -106,6 +108,7 @@ func NewRepportService(
|
||||
hppPerKandangRepo repportRepo.HppPerKandangRepository,
|
||||
productionResultRepo repportRepo.ProductionResultRepository,
|
||||
customerPaymentRepo repportRepo.CustomerPaymentRepository,
|
||||
balanceMonitoringRepo repportRepo.BalanceMonitoringRepository,
|
||||
customerRepo customerRepo.CustomerRepository,
|
||||
standardGrowthDetailRepo productionStandardRepository.StandardGrowthDetailRepository,
|
||||
productionStandardDetailRepo productionStandardRepository.ProductionStandardDetailRepository,
|
||||
@@ -129,6 +132,7 @@ func NewRepportService(
|
||||
HppPerKandangRepo: hppPerKandangRepo,
|
||||
ProductionResultRepo: productionResultRepo,
|
||||
CustomerPaymentRepo: customerPaymentRepo,
|
||||
BalanceMonitoringRepo: balanceMonitoringRepo,
|
||||
CustomerRepo: customerRepo,
|
||||
StandardGrowthDetailRepo: standardGrowthDetailRepo,
|
||||
ProductionStandardDetailRepo: productionStandardDetailRepo,
|
||||
@@ -2893,3 +2897,163 @@ func parseOptionalFloat64(raw string) (*float64, error) {
|
||||
|
||||
return &value, nil
|
||||
}
|
||||
|
||||
func (s *repportService) GetBalanceMonitoring(ctx *fiber.Ctx, params *validation.BalanceMonitoringQuery) ([]dto.BalanceMonitoringRowDTO, dto.BalanceMonitoringTotalsDTO, int64, error) {
|
||||
if params.SortBy == "" {
|
||||
params.SortBy = "customer"
|
||||
}
|
||||
if params.SortOrder == "" {
|
||||
params.SortOrder = "asc"
|
||||
}
|
||||
if params.FilterBy == "" {
|
||||
params.FilterBy = "sold_at"
|
||||
}
|
||||
if params.Page < 1 {
|
||||
params.Page = 1
|
||||
}
|
||||
if params.Limit < 1 {
|
||||
params.Limit = 10
|
||||
}
|
||||
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, dto.BalanceMonitoringTotalsDTO{}, 0, err
|
||||
}
|
||||
|
||||
locationScope, err := m.ResolveLocationScope(ctx, s.DB())
|
||||
if err != nil {
|
||||
return nil, dto.BalanceMonitoringTotalsDTO{}, 0, err
|
||||
}
|
||||
areaScope, err := m.ResolveAreaScope(ctx, s.DB())
|
||||
if err != nil {
|
||||
return nil, dto.BalanceMonitoringTotalsDTO{}, 0, err
|
||||
}
|
||||
if locationScope.Restrict {
|
||||
params.AllowedLocationIDs = toInt64Slice(locationScope.IDs)
|
||||
}
|
||||
if areaScope.Restrict {
|
||||
params.AllowedAreaIDs = toInt64Slice(areaScope.IDs)
|
||||
}
|
||||
|
||||
offset := (params.Page - 1) * params.Limit
|
||||
|
||||
customerIDs, total, err := s.BalanceMonitoringRepo.GetCustomerIDsForBalanceMonitoring(ctx.Context(), offset, params.Limit, params)
|
||||
if err != nil {
|
||||
return nil, dto.BalanceMonitoringTotalsDTO{}, 0, err
|
||||
}
|
||||
if len(customerIDs) == 0 {
|
||||
emptyTotals, gtErr := s.computeBalanceMonitoringTotals(ctx.Context(), params)
|
||||
if gtErr != nil {
|
||||
return nil, dto.BalanceMonitoringTotalsDTO{}, 0, gtErr
|
||||
}
|
||||
return []dto.BalanceMonitoringRowDTO{}, emptyTotals, total, nil
|
||||
}
|
||||
|
||||
saldoAwalLifetimeMap, err := s.BalanceMonitoringRepo.GetSaldoAwalLifetime(ctx.Context(), customerIDs)
|
||||
if err != nil {
|
||||
return nil, dto.BalanceMonitoringTotalsDTO{}, 0, err
|
||||
}
|
||||
salesBeforeMap, err := s.BalanceMonitoringRepo.GetSalesTotalsBeforeDate(ctx.Context(), customerIDs, params)
|
||||
if err != nil {
|
||||
return nil, dto.BalanceMonitoringTotalsDTO{}, 0, err
|
||||
}
|
||||
paymentBeforeMap, err := s.BalanceMonitoringRepo.GetPaymentTotalsBeforeDate(ctx.Context(), customerIDs, params)
|
||||
if err != nil {
|
||||
return nil, dto.BalanceMonitoringTotalsDTO{}, 0, err
|
||||
}
|
||||
categoryMap, err := s.BalanceMonitoringRepo.GetSalesByCategoryInPeriod(ctx.Context(), customerIDs, params)
|
||||
if err != nil {
|
||||
return nil, dto.BalanceMonitoringTotalsDTO{}, 0, err
|
||||
}
|
||||
paymentInPeriodMap, err := s.BalanceMonitoringRepo.GetPaymentTotalsInPeriod(ctx.Context(), customerIDs, params)
|
||||
if err != nil {
|
||||
return nil, dto.BalanceMonitoringTotalsDTO{}, 0, err
|
||||
}
|
||||
agingMap, err := s.BalanceMonitoringRepo.GetAgingPerCustomer(ctx.Context(), customerIDs, params)
|
||||
if err != nil {
|
||||
return nil, dto.BalanceMonitoringTotalsDTO{}, 0, err
|
||||
}
|
||||
|
||||
customers, err := s.CustomerRepo.GetByIDs(ctx.Context(), customerIDs, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("Pic")
|
||||
})
|
||||
if err != nil {
|
||||
return nil, dto.BalanceMonitoringTotalsDTO{}, 0, err
|
||||
}
|
||||
customerMap := make(map[uint]entity.Customer, len(customers))
|
||||
for _, c := range customers {
|
||||
customerMap[c.Id] = c
|
||||
}
|
||||
|
||||
result := make([]dto.BalanceMonitoringRowDTO, 0, len(customerIDs))
|
||||
for _, customerID := range customerIDs {
|
||||
customer, ok := customerMap[customerID]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
saldoAwal := saldoAwalLifetimeMap[customerID] + paymentBeforeMap[customerID] - salesBeforeMap[customerID]
|
||||
|
||||
category := categoryMap[customerID]
|
||||
ayam := dto.BalanceMonitoringAyamDTO{
|
||||
Ekor: category.AyamQty,
|
||||
Kg: category.AyamKg,
|
||||
Nominal: category.AyamNominal,
|
||||
}
|
||||
telur := dto.BalanceMonitoringTelurDTO{
|
||||
Butir: category.TelurQty,
|
||||
Kg: category.TelurKg,
|
||||
Nominal: category.TelurNominal,
|
||||
}
|
||||
trading := dto.BalanceMonitoringTradingDTO{
|
||||
Qty: category.TradingQty,
|
||||
Kg: category.TradingKg,
|
||||
Nominal: category.TradingNominal,
|
||||
}
|
||||
|
||||
pembayaran := paymentInPeriodMap[customerID]
|
||||
aging := agingMap[customerID]
|
||||
|
||||
row := dto.ToBalanceMonitoringRowDTO(customer, saldoAwal, ayam, telur, trading, pembayaran, aging.AgingMax, aging.AgingRataRata)
|
||||
result = append(result, row)
|
||||
}
|
||||
|
||||
totals, err := s.computeBalanceMonitoringTotals(ctx.Context(), params)
|
||||
if err != nil {
|
||||
return nil, dto.BalanceMonitoringTotalsDTO{}, 0, err
|
||||
}
|
||||
|
||||
return result, totals, total, nil
|
||||
}
|
||||
|
||||
func (s *repportService) computeBalanceMonitoringTotals(ctx context.Context, params *validation.BalanceMonitoringQuery) (dto.BalanceMonitoringTotalsDTO, error) {
|
||||
grand, err := s.BalanceMonitoringRepo.GetGrandTotals(ctx, params)
|
||||
if err != nil {
|
||||
return dto.BalanceMonitoringTotalsDTO{}, err
|
||||
}
|
||||
|
||||
saldoAwal := grand.SaldoAwalLifetime + grand.PaymentBeforeStart - grand.SalesBeforeStart
|
||||
saldoAkhir := saldoAwal + grand.PaymentInPeriod - (grand.AyamNominal + grand.TelurNominal + grand.TradingNominal)
|
||||
|
||||
return dto.BalanceMonitoringTotalsDTO{
|
||||
SaldoAwal: saldoAwal,
|
||||
PenjualanAyam: dto.BalanceMonitoringAyamDTO{
|
||||
Ekor: grand.AyamQty,
|
||||
Kg: grand.AyamKg,
|
||||
Nominal: grand.AyamNominal,
|
||||
},
|
||||
PenjualanTelur: dto.BalanceMonitoringTelurDTO{
|
||||
Butir: grand.TelurQty,
|
||||
Kg: grand.TelurKg,
|
||||
Nominal: grand.TelurNominal,
|
||||
},
|
||||
PenjualanTrading: dto.BalanceMonitoringTradingDTO{
|
||||
Qty: grand.TradingQty,
|
||||
Kg: grand.TradingKg,
|
||||
Nominal: grand.TradingNominal,
|
||||
},
|
||||
Pembayaran: grand.PaymentInPeriod,
|
||||
Aging: grand.AgingMax,
|
||||
AgingRataRata: grand.AgingRataRata,
|
||||
SaldoAkhir: saldoAkhir,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user