82 lines
2.8 KiB
PHP
82 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Reports;
|
|
|
|
use App\Models\InfoItem;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class RoadItemReportService
|
|
{
|
|
public function countryActivityPerSubItem(int $item, string $fromDate, string $toDate): array
|
|
{
|
|
$subItems = InfoItem::query()->where('item', $item)->get(['sub_item', 'sub_item_str', 'sub_item_unit']);
|
|
|
|
$query = DB::table('road_items_projects')
|
|
->selectRaw('province_id, province_fa, sub_item');
|
|
|
|
foreach ($subItems as $subItem) {
|
|
$query->selectRaw("
|
|
COUNT(CASE WHEN sub_item = {$subItem->sub_item} THEN 1 END) as Count_{$subItem->sub_item},
|
|
SUM(CASE WHEN sub_item = {$subItem->sub_item} THEN sub_item_data END) as Sum_{$subItem->sub_item}
|
|
");
|
|
}
|
|
|
|
$query->where('item', '=', $item)
|
|
->where('status', '=', 1)
|
|
->whereBetween('activity_date_time', [$fromDate, $toDate])
|
|
->groupBy('province_id', 'province_fa')
|
|
->orderBy('province_id');
|
|
|
|
$activities = $query->get();
|
|
|
|
$totalItemCount = [
|
|
'province_fa' => 'کشوری'
|
|
];
|
|
|
|
foreach ($subItems as $subItem) {
|
|
$totalItemCount[] = [
|
|
"total_count_{$subItem->sub_item}" => $activities->sum("Count_{$subItem->sub_item}"),
|
|
"total_sum_{$subItem->sub_item}" => $activities->sum("Sum_{$subItem->sub_item}"),
|
|
];
|
|
}
|
|
|
|
return [
|
|
'data' => $activities->push($totalItemCount),
|
|
'fromDate' => $fromDate,
|
|
'toDate' => $toDate,
|
|
'details' => $subItems,
|
|
];
|
|
}
|
|
|
|
public function provinceActivityPerSubItem(int $item, string $fromDate, string $toDate, int $provinceId): Collection
|
|
{
|
|
$subItems = InfoItem::query()->where('item', $item)->get(['sub_item', 'sub_item_str', 'sub_item_unit']);
|
|
|
|
$query = DB::table('road_items_projects')
|
|
->selectRaw('province_id, province_fa, user_name as city_fa, city_id');
|
|
|
|
foreach ($subItems as $subItem) {
|
|
$query->selectRaw("
|
|
COUNT(CASE WHEN sub_item = {$subItem->sub_item} THEN 1 END) as Count_{$subItem->sub_item},
|
|
SUM(CASE WHEN sub_item = {$subItem->sub_item} THEN sub_item_data END) as Sum_{$subItem->sub_item}
|
|
");
|
|
}
|
|
|
|
$query->where('item', '=', $item)
|
|
->where('status', '=', 1)
|
|
->where('province_id', '=', $provinceId)
|
|
->whereBetween('activity_date_time', [$fromDate, $toDate])
|
|
->groupBy('city_id')
|
|
->orderBy('province_id');
|
|
|
|
$activities = $query->get();
|
|
|
|
return collect([
|
|
'data' => $activities,
|
|
'fromDate' => $fromDate,
|
|
'toDate' => $toDate,
|
|
'details' => $subItems
|
|
]);
|
|
}
|
|
} |