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"`
SalesWeight float64 `json:"sales_weight"`
AverageWeight float64 `json:"average_weight"`
AverageSellingPrice float64 `json:"average_selling_price"`
AverageSellingPrice float64 `json:"chicken_average_selling_price"`
}
type ClosingEggSalesDTO struct {
EggPieces int `json:"egg_pieces"`
EggMassKg float64 `json:"egg_mass_kg"`
AverageEggWeightKg float64 `json:"average_egg_weight_kg"`
AverageSellingPrice float64 `json:"average_selling_price"`
AverageSellingPrice float64 `json:"egg_average_selling_price"`
}
type ClosingPerformanceDTO struct {
Depletion float64 `json:"depletion"`
Age float64 `json:"age"`
Age float64 `json:"age_day"`
MortalityStd float64 `json:"mortality_std"`
MortalityAct float64 `json:"mortality_act"`
DeffMortality float64 `json:"deff_mortality"`
FcrStd float64 `json:"fcr_std"`
FcrAct float64 `json:"fcr_act"`
DeffFcr float64 `json:"deff_fcr"`
Adg float64 `json:"adg"`
Awg float64 `json:"awg"`
}
type ClosingSalesGroupDTO struct {
ChickenProduction ClosingSalesDTO `json:"chicken_production"`
EggProduction ClosingEggSalesDTO `json:"egg_production"`
Chicken ClosingSalesDTO `json:"chicken"`
Egg *ClosingEggSalesDTO `json:"egg,omitempty"`
}
type ClosingProductionReportDTO struct {
@@ -5,6 +5,7 @@ import (
"errors"
"math"
"strconv"
"strings"
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
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)
if err != nil {
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")
}
}
// masih dummy, karena tab penjualan agenya masih dummy juga
// masih dummy, karena tab penjualan agenya masih dummy
age := 1.0
feedUsedPerHead := 0.0
@@ -465,29 +468,6 @@ func (s closingService) GetClosingDataProduksi(c *fiber.Ctx, projectFlockID uint
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{
SalesPopulation: int(chickenSalesQty),
SalesWeight: chickenSalesWeight,
@@ -495,34 +475,65 @@ func (s closingService) GetClosingDataProduksi(c *fiber.Ctx, projectFlockID uint
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
if chickenDepletion < 0 {
chickenDepletion = 0
}
eggDepletion := harvestEggQty - eggSalesQty
if eggDepletion < 0 {
eggDepletion = 0
}
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{
ChickenProduction: chickenSales,
EggProduction: eggSales,
Chicken: chickenSales,
Egg: eggSales,
}
performance := dto.ClosingPerformanceDTO{
@@ -531,10 +542,17 @@ func (s closingService) GetClosingDataProduksi(c *fiber.Ctx, projectFlockID uint
MortalityStd: chickenPerformance.MortalityStd,
MortalityAct: chickenPerformance.MortalityAct,
DeffMortality: chickenPerformance.DeffMortality,
FcrStd: eggPerformance.FcrStd,
FcrAct: eggPerformance.FcrAct,
DeffFcr: eggPerformance.DeffFcr,
Adg: eggPerformance.Adg,
}
if eggPerformance != nil {
performance.FcrStd = eggPerformance.FcrStd
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{
@@ -562,9 +580,9 @@ func calculatePerformanceMetrics(averageWeight, totalWeight, feedUsed, basePopul
deffMortality := mortalityStd - mortalityAct
deffFcr := fcrStd - fcrAct
adg := 0.0
awg := 0.0
if age > 0 {
adg = averageWeight / age
awg = averageWeight / age
}
return dto.ClosingPerformanceDTO{
@@ -576,7 +594,7 @@ func calculatePerformanceMetrics(averageWeight, totalWeight, feedUsed, basePopul
FcrStd: fcrStd,
FcrAct: fcrAct,
DeffFcr: deffFcr,
Adg: adg,
Awg: awg,
}
}