mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-25 07:45:44 +00:00
FEAT[BE]: implement HPP on penjualan harian
This commit is contained in:
@@ -218,8 +218,23 @@ func (s *repportService) GetMarketing(c *fiber.Ctx, params *validation.Marketing
|
|||||||
projectFlockIDMap[projectFlockID] = true
|
projectFlockIDMap[projectFlockID] = true
|
||||||
|
|
||||||
category := projectFlockKandang.ProjectFlock.Category
|
category := projectFlockKandang.ProjectFlock.Category
|
||||||
hppPerKg := s.calculateHppPricePerKg(c.Context(), projectFlockID, category)
|
if utils.ProjectFlockCategory(category) == utils.ProjectFlockCategoryLaying {
|
||||||
hppMap[projectFlockID] = hppPerKg
|
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 {
|
||||||
|
|
||||||
|
hppMap[projectFlockID] = 0.0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -228,81 +243,6 @@ func (s *repportService) GetMarketing(c *fiber.Ctx, params *validation.Marketing
|
|||||||
return items, total, nil
|
return items, total, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *repportService) calculateHppPricePerKg(ctx context.Context, projectFlockID uint, category string) float64 {
|
|
||||||
totalCost := s.getTotalProjectCost(ctx, projectFlockID)
|
|
||||||
if totalCost == 0 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
chickinQty, err := s.ChickinRepo.GetTotalChickinQtyByProjectFlockID(ctx, projectFlockID)
|
|
||||||
if err != nil {
|
|
||||||
s.Log.Warnf("HPP calculation: Failed to get chickin qty for project flock ID %d: %v", projectFlockID, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
depletion, err := s.RecordingRepo.GetTotalDepletionByProjectFlockID(ctx, projectFlockID)
|
|
||||||
if err != nil {
|
|
||||||
s.Log.Warnf("HPP calculation: Failed to get depletion for project flock ID %d: %v", projectFlockID, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
avgWeight, err := s.RecordingRepo.GetLatestAvgWeightByProjectFlockID(ctx, projectFlockID)
|
|
||||||
if err != nil {
|
|
||||||
s.Log.Warnf("HPP calculation: Failed to get avg weight for project flock ID %d: %v", projectFlockID, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var totalWeight float64
|
|
||||||
if utils.ProjectFlockCategory(category) == utils.ProjectFlockCategoryGrowing {
|
|
||||||
totalWeight = (chickinQty - depletion) * avgWeight
|
|
||||||
} else {
|
|
||||||
eggWeight, err := s.RecordingRepo.GetTotalEggProductionWeightByProjectFlockID(ctx, projectFlockID)
|
|
||||||
if err != nil {
|
|
||||||
s.Log.Warnf("HPP calculation: Failed to get egg weight for project flock ID %d: %v", projectFlockID, err)
|
|
||||||
}
|
|
||||||
totalWeight = (chickinQty-depletion)*avgWeight + eggWeight
|
|
||||||
}
|
|
||||||
|
|
||||||
if totalWeight == 0 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
hppPricePerKg := totalCost / totalWeight
|
|
||||||
return hppPricePerKg
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *repportService) getTotalProjectCost(ctx context.Context, projectFlockID uint) float64 {
|
|
||||||
if projectFlockID == 0 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
purchases, err := s.PurchaseRepo.GetItemsByProjectFlockID(ctx, projectFlockID)
|
|
||||||
if err != nil {
|
|
||||||
s.Log.Errorf("getTotalProjectCost: GetItemsByProjectFlockID error for project flock ID %d: %v", projectFlockID, err)
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
cost := float64(0)
|
|
||||||
purchaseCost := float64(0)
|
|
||||||
for _, p := range purchases {
|
|
||||||
purchaseCost += p.TotalPrice
|
|
||||||
}
|
|
||||||
cost += purchaseCost
|
|
||||||
|
|
||||||
realizations, err := s.ExpenseRealizationRepo.GetByProjectFlockID(ctx, projectFlockID)
|
|
||||||
if err != nil {
|
|
||||||
s.Log.Warnf("getTotalProjectCost: GetByProjectFlockID error for project flock ID %d: %v", projectFlockID, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
bopCost := float64(0)
|
|
||||||
for _, r := range realizations {
|
|
||||||
if r.ExpenseNonstock != nil && r.ExpenseNonstock.Expense != nil &&
|
|
||||||
r.ExpenseNonstock.Expense.Category == string(utils.ExpenseCategoryBOP) {
|
|
||||||
bopCost += r.Price * r.Qty
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cost += bopCost
|
|
||||||
|
|
||||||
return cost
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *repportService) GetProductionResult(ctx *fiber.Ctx, params *validation.ProductionResultQuery) ([]dto.ProductionResultDTO, int64, error) {
|
func (s *repportService) GetProductionResult(ctx *fiber.Ctx, params *validation.ProductionResultQuery) ([]dto.ProductionResultDTO, int64, error) {
|
||||||
if err := s.Validate.Struct(params); err != nil {
|
if err := s.Validate.Struct(params); err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
|
|||||||
Reference in New Issue
Block a user