codex: initiated changes

This commit is contained in:
Adnan Zahir
2026-03-30 13:40:29 +07:00
parent d76f72050e
commit be00837148
22 changed files with 1762 additions and 328 deletions
@@ -18,6 +18,7 @@ import (
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/validations"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
approvalService "gitlab.com/mbugroup/lti-api.git/internal/common/service"
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
expenseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/repositories"
@@ -215,39 +216,95 @@ func (s *repportService) GetMarketing(c *fiber.Ctx, params *validation.Marketing
}
}
projectFlockIDMap := make(map[uint]bool)
hppMap := make(map[uint]float64)
deliveryIDs := make([]uint, 0, len(deliveryProducts))
for _, delivery := range deliveryProducts {
deliveryIDs = append(deliveryIDs, delivery.Id)
}
for _, dp := range deliveryProducts {
if projectFlockKandang := dp.MarketingProduct.ProductWarehouse.ProjectFlockKandang; projectFlockKandang != nil {
projectFlockID := projectFlockKandang.ProjectFlockId
if projectFlockID > 0 && !projectFlockIDMap[projectFlockID] {
projectFlockIDMap[projectFlockID] = true
attributionRows, err := s.MarketingDeliveryRepo.GetAttributionRowsByDeliveryProductIDs(c.Context(), deliveryIDs)
if err != nil {
return nil, 0, err
}
category := projectFlockKandang.ProjectFlock.Category
if utils.ProjectFlockCategory(category) == utils.ProjectFlockCategoryLaying {
if s.HppSvc != nil {
hppCost, err := s.HppSvc.CalculateHppCost(projectFlockID, nil)
if err != nil {
hppMap[projectFlockID] = 0.0
} else if hppCost != nil {
hppMap[projectFlockID] = hppCost.Real.HargaKg
} else {
hppMap[projectFlockID] = 0.0
}
} else {
hppMap[projectFlockID] = 0.0
}
} else {
hppByDelivery := buildMarketingHppByDelivery(c.Context(), s.HppSvc, attributionRows)
categoryByDelivery := buildMarketingCategoryByDelivery(deliveryProducts, attributionRows)
hppMap[projectFlockID] = 0.0
items := dto.ToMarketingReportItems(deliveryProducts, hppByDelivery, categoryByDelivery, agingMap)
return items, total, nil
}
func buildMarketingHppByDelivery(
ctx context.Context,
hppSvc approvalService.HppService,
attributionRows []commonRepo.MarketingDeliveryAttributionRow,
) map[uint]float64 {
if len(attributionRows) == 0 {
return map[uint]float64{}
}
hppByKandang := make(map[uint]float64)
weightedByDelivery := make(map[uint]float64)
totalQtyByDelivery := make(map[uint]float64)
for _, row := range attributionRows {
if row.MarketingDeliveryProductID == 0 || row.ProjectFlockKandangID == 0 || row.AllocatedQty <= 0 {
continue
}
hppPerKg, exists := hppByKandang[row.ProjectFlockKandangID]
if !exists {
hppPerKg = 0
if hppSvc != nil && utils.ProjectFlockCategory(row.ProjectFlockCategory) == utils.ProjectFlockCategoryLaying {
if hppCost, err := hppSvc.CalculateHppCost(row.ProjectFlockKandangID, nil); err == nil && hppCost != nil {
hppPerKg = hppCost.Real.HargaKg
}
}
hppByKandang[row.ProjectFlockKandangID] = hppPerKg
}
weightedByDelivery[row.MarketingDeliveryProductID] += row.AllocatedQty * hppPerKg
totalQtyByDelivery[row.MarketingDeliveryProductID] += row.AllocatedQty
}
result := make(map[uint]float64, len(totalQtyByDelivery))
for deliveryID, totalQty := range totalQtyByDelivery {
if totalQty <= 0 {
continue
}
result[deliveryID] = weightedByDelivery[deliveryID] / totalQty
}
return result
}
func buildMarketingCategoryByDelivery(
deliveryProducts []entity.MarketingDeliveryProduct,
attributionRows []commonRepo.MarketingDeliveryAttributionRow,
) map[uint]string {
result := make(map[uint]string, len(deliveryProducts))
for _, row := range attributionRows {
if row.MarketingDeliveryProductID == 0 || strings.TrimSpace(row.ProjectFlockCategory) == "" {
continue
}
if _, exists := result[row.MarketingDeliveryProductID]; !exists {
result[row.MarketingDeliveryProductID] = row.ProjectFlockCategory
}
}
items := dto.ToMarketingReportItems(deliveryProducts, hppMap, agingMap)
return items, total, nil
for _, delivery := range deliveryProducts {
if _, exists := result[delivery.Id]; exists {
continue
}
if delivery.AttributedProjectFlockKandang != nil {
result[delivery.Id] = delivery.AttributedProjectFlockKandang.ProjectFlock.Category
continue
}
if delivery.MarketingProduct.ProductWarehouse.ProjectFlockKandang != nil {
result[delivery.Id] = delivery.MarketingProduct.ProductWarehouse.ProjectFlockKandang.ProjectFlock.Category
}
}
return result
}
func (s *repportService) GetProductionResult(ctx *fiber.Ctx, params *validation.ProductionResultQuery) ([]dto.ProductionResultDTO, int64, error) {