![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/dceprojects.corals.io/Corals/modules/Timesheet/Widgets/ |
<?php namespace Corals\Modules\Timesheet\Widgets; use Corals\Modules\Timesheet\Models\Entry; use Illuminate\Support\Facades\DB; class DepartmentHoursChart extends BasePieChart { public function __construct() { parent::__construct(); $this->options([ 'maintainAspectRatio' => false, 'scales' => [ 'yAxes' => [ 'display' => false ], ], 'legend' => [ 'display' => false, ], 'title' => [ 'display' => true, 'text' => 'Departments Hours' ] ]); } public function run($args): string { $departmentsHours = Entry::query() ->join('utility_list_of_values', 'utility_list_of_values.code', '=', 'ts_entries.department_code') ->when(isset($args['start_date']) && isset($args['end_date']), function ($query) use ($args) { $query->where(function ($query) use ($args) { $query->whereDate("ts_entries.spent_at", '>=', $args['start_date']) ->whereDate("ts_entries.spent_at", '<=', $args['end_date']); }); }) ->groupBy('department_code', 'utility_list_of_values.label') ->select( 'utility_list_of_values.label as label', DB::raw('SUM((hours*60 + minutes) / 60) as total_hours') ) ->orderBy('total_hours', 'desc') ->get(); $labels = $departmentsHours->pluck('label')->toArray(); $data = $departmentsHours->pluck('total_hours')->toArray(); $this->setData($labels, $data)->backgroundColor($this->generateRandomColors(count($data))); $legendHtml = $this->generateCustomLegend($labels, $data, $args['legend_position'] ?? self::$defaultLegendPosition); $chartHtml = view('Corals::chart')->with(['chart' => $this])->render(); return $this->wrapChartWithLegend($chartHtml, $legendHtml, $args['legend_position'] ?? self::$defaultLegendPosition); } }