Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
RoiSpotlightAction
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace App\Actions\AccountCenter\Reporting;
4
5use App\DTO\AccountCenter\Reporting\ReportingRequestDTO;
6use Carbon\Carbon;
7
8class RoiSpotlightAction extends AccountCenterReporting
9{
10    public function execute(ReportingRequestDTO $filter)
11    {
12        $query = $this->getFilteredQuery($filter);
13
14        $flycut_usages = $query->get();
15
16        $firstDate = !empty($filter->fromDate) ? Carbon::parse($filter->fromDate)->startOfDay() : null;
17        $lastDate = !empty($filter->toDate) ? Carbon::parse($filter->toDate)->endOfDay() : null;
18
19        if (!$firstDate) {
20            $firstDate = $flycut_usages->min("created_at");
21        }
22
23        if (!$lastDate) {
24            $lastDate = $flycut_usages->max("created_at");
25        }
26
27        $months = $lastDate->diffInMonths($firstDate);
28        $chart = $this->buildLineChartData2($flycut_usages, $months);
29        $total_cost_savings = $flycut_usages->sum("cost_savings");
30        $userIds = $query->distinct("user_id")->get();
31        $total_users = count($userIds);
32        $average_cost_savings = $total_users > 0 ? $total_cost_savings / $total_users : 0;
33
34        return [
35            "chart" => $chart,
36            "total_cost_savings" => number_format($total_cost_savings, 2, ".", ","),
37            "average_cost_savings" => round($average_cost_savings, 2)
38        ];
39    }
40}