mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
feat[BE-384]: enhance closing reports by introducing calculation context and improving data handling; refactor related functions for better clarity and maintainability
This commit is contained in:
@@ -123,25 +123,42 @@ func (s *repportService) GetMarketing(c *fiber.Ctx, params *validation.Marketing
|
||||
func (s *repportService) calculateHppPricePerKg(ctx context.Context, projectFlockID uint, category string) float64 {
|
||||
totalCost := s.getTotalProjectCost(ctx, projectFlockID)
|
||||
if totalCost == 0 {
|
||||
s.Log.Warnf("HPP calculation: No cost found for project flock ID %d. Check if purchase items are linked to project_flock_kandang_id", projectFlockID)
|
||||
return 0
|
||||
}
|
||||
|
||||
chickinQty, _ := s.ChickinRepo.GetTotalChickinQtyByProjectFlockID(ctx, projectFlockID)
|
||||
depletion, _ := s.RecordingRepo.GetTotalDepletionByProjectFlockID(ctx, projectFlockID)
|
||||
avgWeight, _ := s.RecordingRepo.GetLatestAvgWeightByProjectFlockID(ctx, projectFlockID)
|
||||
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, _ := s.RecordingRepo.GetTotalEggProductionWeightByProjectFlockID(ctx, projectFlockID)
|
||||
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
|
||||
}
|
||||
return totalCost / totalWeight
|
||||
|
||||
hppPricePerKg := totalCost / totalWeight
|
||||
return hppPricePerKg
|
||||
}
|
||||
|
||||
func (s *repportService) getTotalProjectCost(ctx context.Context, projectFlockID uint) float64 {
|
||||
@@ -151,24 +168,30 @@ func (s *repportService) getTotalProjectCost(ctx context.Context, projectFlockID
|
||||
|
||||
purchases, err := s.PurchaseRepo.GetItemsByProjectFlockID(ctx, projectFlockID)
|
||||
if err != nil {
|
||||
s.Log.Warnf("GetItemsByProjectFlockID error: %v", err)
|
||||
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 {
|
||||
cost += p.TotalPrice
|
||||
purchaseCost += p.TotalPrice
|
||||
}
|
||||
cost += purchaseCost
|
||||
|
||||
realizations, err := s.ExpenseRealizationRepo.GetByProjectFlockID(ctx, projectFlockID)
|
||||
if err != nil {
|
||||
s.Log.Warnf("GetByProjectFlockID error: %v", err)
|
||||
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) {
|
||||
cost += r.Price * r.Qty
|
||||
bopCost += r.Price * r.Qty
|
||||
}
|
||||
}
|
||||
cost += bopCost
|
||||
|
||||
return cost
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user