55 lines
1.6 KiB
PHP
55 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Reports;
|
|
|
|
use App\Models\Activity;
|
|
use App\Reports\Contracts\Report;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
class SegmentActivityCountsReport extends BaseEloquentReport implements Report
|
|
{
|
|
public function slug(): string
|
|
{
|
|
return 'segment-activity-counts';
|
|
}
|
|
|
|
public function name(): string
|
|
{
|
|
return 'Aktivnosti po segmentih';
|
|
}
|
|
|
|
public function description(): ?string
|
|
{
|
|
return 'Število aktivnosti po segmentih v izbranem obdobju (glede na segment dejanja).';
|
|
}
|
|
|
|
public function inputs(): array
|
|
{
|
|
return [
|
|
['key' => 'from', 'type' => 'date', 'label' => 'Od', 'nullable' => true],
|
|
['key' => 'to', 'type' => 'date', 'label' => 'Do', 'nullable' => true],
|
|
];
|
|
}
|
|
|
|
public function columns(): array
|
|
{
|
|
return [
|
|
['key' => 'segment_name', 'label' => 'Segment'],
|
|
['key' => 'activities_count', 'label' => 'Št. aktivnosti'],
|
|
];
|
|
}
|
|
|
|
public function query(array $filters): Builder
|
|
{
|
|
$q = Activity::query()
|
|
->join('actions', 'activities.action_id', '=', 'actions.id')
|
|
->leftJoin('segments', 'actions.segment_id', '=', 'segments.id')
|
|
->when(! empty($filters['from']), fn ($qq) => $qq->whereDate('activities.created_at', '>=', $filters['from']))
|
|
->when(! empty($filters['to']), fn ($qq) => $qq->whereDate('activities.created_at', '<=', $filters['to']))
|
|
->groupBy('segments.name')
|
|
->selectRaw("COALESCE(segments.name, 'Brez segmenta') as segment_name, COUNT(*) as activities_count");
|
|
|
|
return $q;
|
|
}
|
|
}
|