diff --git a/internal/modules/dashboards/services/dashboard.service.go b/internal/modules/dashboards/services/dashboard.service.go index 8fa0a2c9..b4635b2e 100644 --- a/internal/modules/dashboards/services/dashboard.service.go +++ b/internal/modules/dashboards/services/dashboard.service.go @@ -98,12 +98,14 @@ func (s dashboardService) buildPerformanceStatistics(ctx context.Context, params endDate := params.PeriodEnd endExclusive := params.PeriodEndExclusive - hppCurrent, hppLast, err := s.calculateHppGlobal(ctx, filter, startDate, endExclusive, endDate, location) + globalStartDate, globalEndDate, globalEndExclusive := currentPeriodDates(location) + + hppCurrent, hppLast, err := s.calculateHppGlobal(ctx, globalStartDate, globalEndExclusive, globalEndDate, location) if err != nil { return nil, err } - sellingCurrent, sellingLast, err := s.calculateSellingPrice(ctx, filter, endDate, location) + sellingCurrent, sellingLast, err := s.calculateSellingPrice(ctx, globalEndDate, location) if err != nil { return nil, err } @@ -271,15 +273,15 @@ func (s dashboardService) buildPerformanceCharts(ctx context.Context, params *va weekFeed := weeklyFeedMap[week] actFcr := 0.0 - if weekFeed > 0 { - actFcr = weekEgg / weekFeed + if weekEgg > 0 { + actFcr = weekFeed / weekEgg } cumEgg += weekEgg cumFeed += weekFeed actFcrCum := 0.0 - if cumFeed > 0 { - actFcrCum = cumEgg / cumFeed + if cumEgg > 0 { + actFcrCum = cumFeed / cumEgg } bodyWeightDataset = append(bodyWeightDataset, map[string]interface{}{ @@ -357,10 +359,10 @@ func (s dashboardService) buildPerformanceCharts(ctx context.Context, params *va }, "fcr": { Series: []dto.DashboardChartSeriesDTO{ - {Id: "act_fcr", Label: "Act. FCR", Unit: "%"}, - {Id: "std_fcr", Label: "STD. FCR", Unit: "%"}, - {Id: "act_fcr_cum", Label: "Act. FCR Cummulative", Unit: "%"}, - {Id: "std_fcr_cum", Label: "STD. FCR Cummulative", Unit: "%"}, + {Id: "act_fcr", Label: "Act. FCR", Unit: "kg/kg"}, + {Id: "std_fcr", Label: "STD. FCR", Unit: "kg/kg"}, + {Id: "act_fcr_cum", Label: "Act. FCR Cummulative", Unit: "kg/kg"}, + {Id: "std_fcr_cum", Label: "STD. FCR Cummulative", Unit: "kg/kg"}, }, Dataset: fcrDataset, }, @@ -843,12 +845,12 @@ func percentDelta(current, last float64) float64 { return (current - last) / last } -func (s dashboardService) calculateHppGlobal(ctx context.Context, filter *validation.DashboardFilter, startDate, endExclusive, endDate time.Time, location *time.Location) (float64, float64, error) { - totalEggKg, err := s.Repository.SumEggProductionWeightKg(ctx, startDate, endExclusive, filter) +func (s dashboardService) calculateHppGlobal(ctx context.Context, startDate, endExclusive, endDate time.Time, location *time.Location) (float64, float64, error) { + totalEggKg, err := s.Repository.SumEggProductionWeightKg(ctx, startDate, endExclusive, nil) if err != nil { return 0, 0, err } - totalCost, err := s.sumHppCost(ctx, filter, startDate, endExclusive) + totalCost, err := s.sumHppCost(ctx, nil, startDate, endExclusive) if err != nil { return 0, 0, err } @@ -859,11 +861,11 @@ func (s dashboardService) calculateHppGlobal(ctx context.Context, filter *valida } lastMonthStart, lastMonthEndExclusive := monthRange(endDate.AddDate(0, -1, 0), location) - lastEggKg, err := s.Repository.SumEggProductionWeightKg(ctx, lastMonthStart, lastMonthEndExclusive, filter) + lastEggKg, err := s.Repository.SumEggProductionWeightKg(ctx, lastMonthStart, lastMonthEndExclusive, nil) if err != nil { return 0, 0, err } - lastCost, err := s.sumHppCost(ctx, filter, lastMonthStart, lastMonthEndExclusive) + lastCost, err := s.sumHppCost(ctx, nil, lastMonthStart, lastMonthEndExclusive) if err != nil { return 0, 0, err } @@ -876,16 +878,16 @@ func (s dashboardService) calculateHppGlobal(ctx context.Context, filter *valida return hppCurrent, hppLast, nil } -func (s dashboardService) calculateSellingPrice(ctx context.Context, filter *validation.DashboardFilter, endDate time.Time, location *time.Location) (float64, float64, error) { +func (s dashboardService) calculateSellingPrice(ctx context.Context, endDate time.Time, location *time.Location) (float64, float64, error) { startPrevMonth, endPrevMonthExclusive := monthRange(endDate.AddDate(0, -1, 0), location) currentEndExclusive := endDate.AddDate(0, 0, 1) - currentAvg, err := s.avgSellingPrice(ctx, filter, startPrevMonth, currentEndExclusive) + currentAvg, err := s.avgSellingPrice(ctx, nil, startPrevMonth, currentEndExclusive) if err != nil { return 0, 0, err } - lastAvg, err := s.avgSellingPrice(ctx, filter, startPrevMonth, endPrevMonthExclusive) + lastAvg, err := s.avgSellingPrice(ctx, nil, startPrevMonth, endPrevMonthExclusive) if err != nil { return 0, 0, err } @@ -935,11 +937,11 @@ func (s dashboardService) fcrValue(ctx context.Context, filter *validation.Dashb } feedUsageGrams := feedUsageToGrams(feedRows) - if feedUsageGrams <= 0 { + if eggWeightGrams <= 0 { return 0, nil } - return eggWeightGrams / feedUsageGrams, nil + return feedUsageGrams / eggWeightGrams, nil } func (s dashboardService) mortalityValue(ctx context.Context, filter *validation.DashboardFilter, startDate, endExclusive time.Time) (float64, error) { @@ -1027,3 +1029,11 @@ func monthRange(t time.Time, location *time.Location) (time.Time, time.Time) { endExclusive := start.AddDate(0, 1, 0) return start, endExclusive } + +func currentPeriodDates(location *time.Location) (time.Time, time.Time, time.Time) { + now := time.Now().In(location) + startDate := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, location) + endDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, location) + endExclusive := endDate.AddDate(0, 0, 1) + return startDate, endDate, endExclusive +}