139 lines
4.6 KiB
PHP
139 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Exports\Tollhouse;
|
|
|
|
use Illuminate\Contracts\View\View;
|
|
use Maatwebsite\Excel\Concerns\FromView;
|
|
use App\Models\ContractSubItems;
|
|
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
|
use Maatwebsite\Excel\Events\AfterSheet;
|
|
use Maatwebsite\Excel\Concerns\WithEvents;
|
|
use Maatwebsite\Excel\Concerns\WithDrawings;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
|
|
use Maatwebsite\Excel\Concerns\WithStyles;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
|
|
class summaryAccidentPerformanceProvince implements FromView, ShouldAutoSize, WithEvents, WithDrawings, WithStyles
|
|
{
|
|
public function __construct($fromDate = null, $toDate = null, $province = null, $last_status_id = null)
|
|
{
|
|
$this->fromDate = $fromDate ?? null;
|
|
$this->toDate = $toDate ?? null;
|
|
$this->province = $province ?? null;
|
|
$this->last_status_id = $last_status_id ?? null;
|
|
}
|
|
|
|
public function view(): View
|
|
{
|
|
$fromDate = $this->fromDate;
|
|
$toDate = $this->toDate;
|
|
$province = $this->province;
|
|
$last_status_id = $this->last_status_id;
|
|
|
|
|
|
$array = [];
|
|
|
|
$provinces = \App\Models\Province::select('id', 'name_fa')
|
|
->when($province, function($query, $province) {
|
|
return $query->where('id', $province);
|
|
})
|
|
->get();
|
|
foreach ($provinces as $key => $value) {
|
|
$array[$value->id] = [
|
|
'province_fa' => $value->name_fa,
|
|
'avg' => '-',
|
|
'count' => '-',
|
|
];
|
|
}
|
|
|
|
$data = ContractSubItems::where('project_type_id', 5)
|
|
->when($this->fromDate, function ($query) use ($fromDate, $toDate) {
|
|
return $query->whereBetween('created_at', [$fromDate, $toDate]);
|
|
})
|
|
->when($this->province, function ($query, $province) {
|
|
return $query->where('province_id', $province);
|
|
})
|
|
->when($this->last_status_id, function ($query, $last_status_id) {
|
|
return $query->where('last_status_id', $last_status_id);
|
|
})
|
|
->orderBy('province_id', 'ASC')
|
|
|
|
->select('province_fa', 'province_id')
|
|
->selectRaw("avg(progress_project) as avg")
|
|
->selectRaw('count(*) as count')
|
|
->groupBy('province_id')
|
|
->get();
|
|
|
|
$sum = [
|
|
'avg'=>0,
|
|
'count'=>0,
|
|
];
|
|
foreach ($data as $key => $value) {
|
|
$array[$value->province_id] = [
|
|
'province_fa' => $value->province_fa,
|
|
'avg' => $value->avg,
|
|
'count' => $value->count,
|
|
];
|
|
$sum['avg'] += $value->avg;
|
|
$sum['count'] += $value->count;
|
|
}
|
|
return view('excel.Tollhouse.summaryAccidentPerformanceProvince', [
|
|
'roads' => $array,
|
|
'fromFa' => $fromDate ?? null,
|
|
'sum' => $sum,
|
|
'province_count' => $provinces->count(),
|
|
'toFa' => $toDate ?? null
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function registerEvents(): array
|
|
{
|
|
return [
|
|
AfterSheet::class => function(AfterSheet $event) {
|
|
$event->sheet->getDelegate()->setRightToLeft(true);
|
|
},
|
|
];
|
|
}
|
|
|
|
|
|
public function drawings()
|
|
{
|
|
$drawing = new Drawing();
|
|
$drawing->setName('Logo');
|
|
$drawing->setDescription('This is my logo');
|
|
$drawing->setPath(public_path('/dist/logo.png'));
|
|
$drawing->setWidth(50);
|
|
$drawing->setHeight(50);
|
|
$drawing->setOffsetX(5);
|
|
$drawing->setOffsetY(5);
|
|
$drawing->setCoordinates('A1');
|
|
|
|
$drawing2 = new Drawing();
|
|
$drawing2->setName('Logo');
|
|
$drawing2->setDescription('This is my logo');
|
|
$drawing2->setPath(public_path('/dist/141icon.png'));
|
|
$drawing2->setWidth(50);
|
|
$drawing2->setHeight(50);
|
|
$drawing2->setOffsetX(5);
|
|
$drawing2->setOffsetY(5);
|
|
$drawing2->setCoordinates('B1');
|
|
return [$drawing, $drawing2];
|
|
}
|
|
|
|
public function styles(Worksheet $sheet)
|
|
{
|
|
|
|
return [
|
|
// Style the first row as bold text.
|
|
'A:BA' => [
|
|
'font' => [
|
|
'name' => 'B Nazanin'
|
|
]
|
|
],
|
|
];
|
|
|
|
}
|
|
} |