adjust response api closing data produksi

This commit is contained in:
MacBook Air M1
2025-12-18 15:25:15 +07:00
parent d675b1e826
commit 047162699e
2 changed files with 75 additions and 57 deletions
+6 -6
View File
@@ -71,31 +71,31 @@ type ClosingSalesDTO struct {
SalesPopulation int `json:"sales_population"` SalesPopulation int `json:"sales_population"`
SalesWeight float64 `json:"sales_weight"` SalesWeight float64 `json:"sales_weight"`
AverageWeight float64 `json:"average_weight"` AverageWeight float64 `json:"average_weight"`
AverageSellingPrice float64 `json:"average_selling_price"` AverageSellingPrice float64 `json:"chicken_average_selling_price"`
} }
type ClosingEggSalesDTO struct { type ClosingEggSalesDTO struct {
EggPieces int `json:"egg_pieces"` EggPieces int `json:"egg_pieces"`
EggMassKg float64 `json:"egg_mass_kg"` EggMassKg float64 `json:"egg_mass_kg"`
AverageEggWeightKg float64 `json:"average_egg_weight_kg"` AverageEggWeightKg float64 `json:"average_egg_weight_kg"`
AverageSellingPrice float64 `json:"average_selling_price"` AverageSellingPrice float64 `json:"egg_average_selling_price"`
} }
type ClosingPerformanceDTO struct { type ClosingPerformanceDTO struct {
Depletion float64 `json:"depletion"` Depletion float64 `json:"depletion"`
Age float64 `json:"age"` Age float64 `json:"age_day"`
MortalityStd float64 `json:"mortality_std"` MortalityStd float64 `json:"mortality_std"`
MortalityAct float64 `json:"mortality_act"` MortalityAct float64 `json:"mortality_act"`
DeffMortality float64 `json:"deff_mortality"` DeffMortality float64 `json:"deff_mortality"`
FcrStd float64 `json:"fcr_std"` FcrStd float64 `json:"fcr_std"`
FcrAct float64 `json:"fcr_act"` FcrAct float64 `json:"fcr_act"`
DeffFcr float64 `json:"deff_fcr"` DeffFcr float64 `json:"deff_fcr"`
Adg float64 `json:"adg"` Awg float64 `json:"awg"`
} }
type ClosingSalesGroupDTO struct { type ClosingSalesGroupDTO struct {
ChickenProduction ClosingSalesDTO `json:"chicken_production"` Chicken ClosingSalesDTO `json:"chicken"`
EggProduction ClosingEggSalesDTO `json:"egg_production"` Egg *ClosingEggSalesDTO `json:"egg,omitempty"`
} }
type ClosingProductionReportDTO struct { type ClosingProductionReportDTO struct {
@@ -5,6 +5,7 @@ import (
"errors" "errors"
"math" "math"
"strconv" "strconv"
"strings"
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service" commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities" entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
@@ -403,6 +404,8 @@ func (s closingService) GetClosingDataProduksi(c *fiber.Ctx, projectFlockID uint
} }
} }
isGrowing := strings.EqualFold(project.Category, string(utils.ProjectFlockCategoryGrowing))
projectFlockKandangIDs, err := s.getProjectFlockKandangIDs(c.Context(), projectFlockID) projectFlockKandangIDs, err := s.getProjectFlockKandangIDs(c.Context(), projectFlockID)
if err != nil { if err != nil {
s.Log.Errorf("Failed to fetch project flock kandangs for %d: %+v", projectFlockID, err) s.Log.Errorf("Failed to fetch project flock kandangs for %d: %+v", projectFlockID, err)
@@ -431,7 +434,7 @@ func (s closingService) GetClosingDataProduksi(c *fiber.Ctx, projectFlockID uint
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch FCR standard data") return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch FCR standard data")
} }
} }
// masih dummy, karena tab penjualan agenya masih dummy juga // masih dummy, karena tab penjualan agenya masih dummy
age := 1.0 age := 1.0
feedUsedPerHead := 0.0 feedUsedPerHead := 0.0
@@ -465,29 +468,6 @@ func (s closingService) GetClosingDataProduksi(c *fiber.Ctx, projectFlockID uint
chickenAverageSellingPrice = chickenSalesPrice / chickenSalesWeight chickenAverageSellingPrice = chickenSalesPrice / chickenSalesWeight
} }
eggFlagNames := []string{
string(utils.FlagTelur),
string(utils.FlagTelurUtuh),
string(utils.FlagTelurPecah),
string(utils.FlagTelurPutih),
string(utils.FlagTelurRetak),
}
eggSalesWeight, eggSalesQty, eggSalesPrice, err := s.Repository.SumMarketingWeightAndQtyByProjectFlockKandangIDsAndFlagNames(c.Context(), projectFlockKandangIDs, eggFlagNames)
if err != nil {
s.Log.Errorf("Failed to fetch egg sales data for project flock %d: %+v", projectFlockID, err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch egg sales data")
}
var averageEggWeight float64
if eggSalesQty > 0 {
averageEggWeight = eggSalesWeight / eggSalesQty
}
var averageEggSellingPrice float64
if eggSalesWeight > 0 {
averageEggSellingPrice = eggSalesPrice / eggSalesWeight
}
chickenSales := dto.ClosingSalesDTO{ chickenSales := dto.ClosingSalesDTO{
SalesPopulation: int(chickenSalesQty), SalesPopulation: int(chickenSalesQty),
SalesWeight: chickenSalesWeight, SalesWeight: chickenSalesWeight,
@@ -495,34 +475,65 @@ func (s closingService) GetClosingDataProduksi(c *fiber.Ctx, projectFlockID uint
AverageSellingPrice: chickenAverageSellingPrice, AverageSellingPrice: chickenAverageSellingPrice,
} }
eggSales := dto.ClosingEggSalesDTO{
EggPieces: int(eggSalesQty),
EggMassKg: eggSalesWeight,
AverageEggWeightKg: averageEggWeight,
AverageSellingPrice: averageEggSellingPrice,
}
harvestEggQty, err := s.Repository.SumRecordingEggQtyByProjectFlockKandangIDsAndFlagNames(c.Context(), projectFlockKandangIDs, eggFlagNames)
if err != nil {
s.Log.Errorf("Failed to fetch recording egg qty for project flock %d: %+v", projectFlockID, err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch egg harvest data")
}
chickenDepletion := population - chickenSalesQty chickenDepletion := population - chickenSalesQty
if chickenDepletion < 0 { if chickenDepletion < 0 {
chickenDepletion = 0 chickenDepletion = 0
} }
eggDepletion := harvestEggQty - eggSalesQty
if eggDepletion < 0 {
eggDepletion = 0
}
chickenPerformance := calculatePerformanceMetrics(chickenAverageWeight, chickenSalesWeight, feedUsed, population, chickenDepletion, age, standards) chickenPerformance := calculatePerformanceMetrics(chickenAverageWeight, chickenSalesWeight, feedUsed, population, chickenDepletion, age, standards)
eggPerformance := calculatePerformanceMetrics(averageEggWeight, eggSalesWeight, feedUsed, harvestEggQty, eggDepletion, age, standards)
var eggSales *dto.ClosingEggSalesDTO
var eggPerformance *dto.ClosingPerformanceDTO
if !isGrowing {
eggFlagNames := []string{
string(utils.FlagTelur),
string(utils.FlagTelurUtuh),
string(utils.FlagTelurPecah),
string(utils.FlagTelurPutih),
string(utils.FlagTelurRetak),
}
eggSalesWeight, eggSalesQty, eggSalesPrice, err := s.Repository.SumMarketingWeightAndQtyByProjectFlockKandangIDsAndFlagNames(c.Context(), projectFlockKandangIDs, eggFlagNames)
if err != nil {
s.Log.Errorf("Failed to fetch egg sales data for project flock %d: %+v", projectFlockID, err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch egg sales data")
}
var averageEggWeight float64
if eggSalesQty > 0 {
averageEggWeight = eggSalesWeight / eggSalesQty
}
var averageEggSellingPrice float64
if eggSalesWeight > 0 {
averageEggSellingPrice = eggSalesPrice / eggSalesWeight
}
eggSales = &dto.ClosingEggSalesDTO{
EggPieces: int(eggSalesQty),
EggMassKg: eggSalesWeight,
AverageEggWeightKg: averageEggWeight,
AverageSellingPrice: averageEggSellingPrice,
}
harvestEggQty, err := s.Repository.SumRecordingEggQtyByProjectFlockKandangIDsAndFlagNames(c.Context(), projectFlockKandangIDs, eggFlagNames)
if err != nil {
s.Log.Errorf("Failed to fetch recording egg qty for project flock %d: %+v", projectFlockID, err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch egg harvest data")
}
eggDepletion := harvestEggQty - eggSalesQty
if eggDepletion < 0 {
eggDepletion = 0
}
eggPerf := calculatePerformanceMetrics(averageEggWeight, eggSalesWeight, feedUsed, harvestEggQty, eggDepletion, age, standards)
eggPerformance = &eggPerf
}
sales := dto.ClosingSalesGroupDTO{ sales := dto.ClosingSalesGroupDTO{
ChickenProduction: chickenSales, Chicken: chickenSales,
EggProduction: eggSales, Egg: eggSales,
} }
performance := dto.ClosingPerformanceDTO{ performance := dto.ClosingPerformanceDTO{
@@ -531,10 +542,17 @@ func (s closingService) GetClosingDataProduksi(c *fiber.Ctx, projectFlockID uint
MortalityStd: chickenPerformance.MortalityStd, MortalityStd: chickenPerformance.MortalityStd,
MortalityAct: chickenPerformance.MortalityAct, MortalityAct: chickenPerformance.MortalityAct,
DeffMortality: chickenPerformance.DeffMortality, DeffMortality: chickenPerformance.DeffMortality,
FcrStd: eggPerformance.FcrStd, }
FcrAct: eggPerformance.FcrAct, if eggPerformance != nil {
DeffFcr: eggPerformance.DeffFcr, performance.FcrStd = eggPerformance.FcrStd
Adg: eggPerformance.Adg, performance.FcrAct = eggPerformance.FcrAct
performance.DeffFcr = eggPerformance.DeffFcr
performance.Awg = eggPerformance.Awg
} else {
performance.FcrStd = chickenPerformance.FcrStd
performance.FcrAct = chickenPerformance.FcrAct
performance.DeffFcr = chickenPerformance.DeffFcr
performance.Awg = chickenPerformance.Awg
} }
result := dto.ClosingProductionReportDTO{ result := dto.ClosingProductionReportDTO{
@@ -562,9 +580,9 @@ func calculatePerformanceMetrics(averageWeight, totalWeight, feedUsed, basePopul
deffMortality := mortalityStd - mortalityAct deffMortality := mortalityStd - mortalityAct
deffFcr := fcrStd - fcrAct deffFcr := fcrStd - fcrAct
adg := 0.0 awg := 0.0
if age > 0 { if age > 0 {
adg = averageWeight / age awg = averageWeight / age
} }
return dto.ClosingPerformanceDTO{ return dto.ClosingPerformanceDTO{
@@ -576,7 +594,7 @@ func calculatePerformanceMetrics(averageWeight, totalWeight, feedUsed, basePopul
FcrStd: fcrStd, FcrStd: fcrStd,
FcrAct: fcrAct, FcrAct: fcrAct,
DeffFcr: deffFcr, DeffFcr: deffFcr,
Adg: adg, Awg: awg,
} }
} }