Merge branch 'development' of https://gitlab.com/mbugroup/lti-api into FEAT/BE/report_customer_payment

This commit is contained in:
aguhh18
2026-01-15 16:20:37 +07:00
27 changed files with 372 additions and 131 deletions
@@ -2,6 +2,7 @@ package service
import (
"context"
"errors"
"fmt"
"math"
"sort"
@@ -22,6 +23,7 @@ import (
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"
productionStandardRepository "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/repositories"
chickinRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/repositories"
recordingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
purchaseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/repositories"
@@ -60,6 +62,8 @@ type repportService struct {
ProductionResultRepo repportRepo.ProductionResultRepository
CustomerPaymentRepo repportRepo.CustomerPaymentRepository
CustomerRepo customerRepo.CustomerRepository
StandardGrowthDetailRepo productionStandardRepository.StandardGrowthDetailRepository
ProductionStandardDetailRepo productionStandardRepository.ProductionStandardDetailRepository
}
type HppCostAggregate struct {
@@ -86,6 +90,8 @@ func NewRepportService(
productionResultRepo repportRepo.ProductionResultRepository,
customerPaymentRepo repportRepo.CustomerPaymentRepository,
customerRepo customerRepo.CustomerRepository,
standardGrowthDetailRepo productionStandardRepository.StandardGrowthDetailRepository,
productionStandardDetailRepo productionStandardRepository.ProductionStandardDetailRepository,
) RepportService {
return &repportService{
Log: utils.Log,
@@ -103,6 +109,8 @@ func NewRepportService(
ProductionResultRepo: productionResultRepo,
CustomerPaymentRepo: customerPaymentRepo,
CustomerRepo: customerRepo,
StandardGrowthDetailRepo: standardGrowthDetailRepo,
ProductionStandardDetailRepo: productionStandardDetailRepo,
}
}
@@ -296,6 +304,21 @@ func (s *repportService) GetProductionResult(ctx *fiber.Ctx, params *validation.
weeklyResults := summarizeProductionResults(dailyResults, recordsPerWeek)
var productionStandardID uint
if s.ProductionResultRepo != nil {
standardID, err := s.ProductionResultRepo.GetProductionStandardIDByProjectFlockKandangID(ctx.Context(), params.ProjectFlockKandangID)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, 0, err
}
} else {
productionStandardID = standardID
}
}
standardDetailCache := make(map[int]*entity.ProductionStandardDetail)
growthDetailCache := make(map[int]*entity.StandardGrowthDetail)
var cumulativeButir int64
var cumulativeKg float64
for i := range weeklyResults {
@@ -311,6 +334,66 @@ func (s *repportService) GetProductionResult(ctx *fiber.Ctx, params *validation.
cumulativeKg += weeklyResults[i].KgJumlah
weeklyResults[i].TotalKg = cumulativeKg
if productionStandardID == 0 {
continue
}
week := int(weeklyResults[i].Woa)
if s.ProductionStandardDetailRepo != nil {
detail, ok := standardDetailCache[week]
if !ok {
fetched, fetchErr := s.ProductionStandardDetailRepo.GetByStandardIDAndWeek(ctx.Context(), productionStandardID, week)
if fetchErr != nil {
if !errors.Is(fetchErr, gorm.ErrRecordNotFound) {
return nil, 0, fetchErr
}
} else {
detail = fetched
}
standardDetailCache[week] = detail
}
if detail != nil {
if detail.TargetHenDayProduction != nil {
weeklyResults[i].HdStd = *detail.TargetHenDayProduction
}
if detail.TargetHenHouseProduction != nil {
weeklyResults[i].HhStd = *detail.TargetHenHouseProduction
}
if detail.TargetEggWeight != nil {
weeklyResults[i].EwStd = *detail.TargetEggWeight
}
if detail.TargetEggMass != nil {
weeklyResults[i].EmStd = *detail.TargetEggMass
}
if detail.StandardFCR != nil {
weeklyResults[i].FcrStd = *detail.StandardFCR
}
}
}
if s.StandardGrowthDetailRepo != nil {
detail, ok := growthDetailCache[week]
if !ok {
fetched, fetchErr := s.StandardGrowthDetailRepo.GetByStandardIDAndWeek(ctx.Context(), productionStandardID, week)
if fetchErr != nil {
if !errors.Is(fetchErr, gorm.ErrRecordNotFound) {
return nil, 0, fetchErr
}
} else {
detail = fetched
}
growthDetailCache[week] = detail
}
if detail != nil && detail.FeedIntake != nil {
weeklyResults[i].FiStd = *detail.FeedIntake
}
if detail != nil && detail.TargetMeanBw != nil {
weeklyResults[i].StdBw = *detail.TargetMeanBw
}
}
}
totalWeeks := int64(math.Ceil(float64(totalRecordings) / float64(recordsPerWeek)))
@@ -524,17 +607,17 @@ func mapRecordingToProductionResultDTO(record entity.Recording) dto.ProductionRe
StdUniformity: "90% up",
DepKum: valueOrZero(record.CumDepletionRate),
DepStd: valueOrZero(record.TotalDepletionQty),
Hd: valueOrZero(record.HenDay),
Fi: valueOrZero(record.FeedIntake),
Fcr: valueOrZero(record.FcrValue),
Hh: valueOrZero(record.TotalChickQty),
Hh: valueOrZero(record.HenHouse),
Em: valueOrZero(record.EggMass),
Ew: valueOrZero(record.EggWeight),
}
if record.Day != nil {
result.Woa = float64(*record.Day)
}
if record.CumIntake != nil {
result.Fi = float64(*record.CumIntake)
}
// avgWeight := calculateAverageBodyWeight(record.BodyWeights)
avgWeight := 1.0
if avgWeight > 0 {
@@ -561,8 +644,6 @@ func mapRecordingToProductionResultDTO(record entity.Recording) dto.ProductionRe
result.PersenPutih = roundFloat((float64(result.ButiranPutih)/total)*100, 2)
result.PersenRetak = roundFloat((float64(result.ButiranRetak)/total)*100, 2)
result.PersenPecah = roundFloat((float64(result.ButiranPecah)/total)*100, 2)
result.Ew = (eggSummary.TotalKg * 1000) / total
result.Em = eggSummary.TotalKg
}
return result
@@ -674,13 +755,13 @@ func summarizeProductionResults(daily []dto.ProductionResultDTO, groupSize int)
if end > len(daily) {
end = len(daily)
}
result = append(result, aggregateProductionResultGroup(daily[i:end]))
result = append(result, aggregateProductionResultGroup(daily[i:end], groupSize))
}
return result
}
func aggregateProductionResultGroup(group []dto.ProductionResultDTO) dto.ProductionResultDTO {
func aggregateProductionResultGroup(group []dto.ProductionResultDTO, groupSize int) dto.ProductionResultDTO {
count := len(group)
if count == 0 {
return dto.ProductionResultDTO{}
@@ -752,6 +833,10 @@ func aggregateProductionResultGroup(group []dto.ProductionResultDTO) dto.Product
if divider == 0 {
divider = 1
}
weeklyDivider := float64(groupSize)
if weeklyDivider == 0 {
weeklyDivider = divider
}
agg.Bw = sumBw / divider
agg.StdBw = sumStdBw / divider
@@ -780,17 +865,17 @@ func aggregateProductionResultGroup(group []dto.ProductionResultDTO) dto.Product
agg.PersenPecah = roundFloat(sumPersenPecah/percentDivider, 2)
}
agg.Hd = sumHd / divider
agg.Hd = roundFloat(sumHd/weeklyDivider, 2)
agg.HdStd = sumHdStd / divider
agg.Fi = sumFi / divider
agg.Fi = roundFloat(sumFi/weeklyDivider, 2)
agg.FiStd = sumFiStd / divider
agg.Em = sumEm / divider
agg.Em = group[count-1].Em
agg.EmStd = sumEmStd / divider
agg.Ew = sumEw / divider
agg.Ew = group[count-1].Ew
agg.EwStd = sumEwStd / divider
agg.Fcr = sumFcr / divider
agg.Fcr = roundFloat(sumFcr/weeklyDivider, 2)
agg.FcrStd = sumFcrStd / divider
agg.Hh = sumHh / divider
agg.Hh = roundFloat(sumHh/weeklyDivider, 2)
agg.HhStd = sumHhStd / divider
return agg