Files
backend/app/Exports/RoadDanger/CompareProgramAndFunction.php
2024-02-20 13:22:11 +00:00

380 lines
17 KiB
PHP

<?php
namespace App\Exports\RoadDanger;
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 Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\WithDrawings;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class CompareProgramAndFunction 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;
$provinces = \App\Models\Province::when($province, function($query, $province) {
return $query->where('id', $province);
})
->get();
$array = [];
foreach ($provinces as $item) {
$array[$item->id] = [
'province_fa' => $item->name_fa,
'TarizRahCount' => 0,
'EslahGhosCount' => 0,
'TaransheBardariCount' => 0,
'EhdasMeydanCount' => 0,
'EhdasTaghatoCount' => 0,
'ImenSaziCount' => 0,
'EslahVoroodiCount' => 0,
'EhdasToonelCount' => 0,
'EhdasVariantCount' => 0,
'TarizAbnieFaniCount' => 0,
'SakhtHaelCount' => 0,
'SayerCount' => 0,
'countAll' => 0,
'TarizRahAvg' => 0,
'EslahGhosAvg' => 0,
'TaransheBardariAvg' => 0,
'EhdasMeydanAvg' => 0,
'EhdasTaghatoAvg' => 0,
'ImenSaziAvg' => 0,
'EslahVoroodiAvg' => 0,
'EhdasToonelAvg' => 0,
'EhdasVariantAvg' => 0,
'TarizAbnieFaniAvg' => 0,
'SakhtHaelAvg' => 0,
'SayerAvg' => 0,
];
}
$query = "SELECT
COUNT(CASE WHEN operation_type_id1 = 201 OR
operation_type_id2 = 201
OR
operation_type_id3 = 201
OR
operation_type_id4 = 201
THEN 1 END) AS TarizRahCount,
AVG(CASE WHEN operation_type_id1 = 201 THEN operation_type_progress1
WHEN operation_type_id2 = 201 THEN operation_type_progress2
WHEN operation_type_id3 = 201 THEN operation_type_progress3
WHEN operation_type_id4 = 201 THEN operation_type_progress4
END) AS TarizRahAvg,
COUNT(CASE WHEN operation_type_id1 = 202 OR
operation_type_id2 = 202
OR
operation_type_id3 = 202
OR
operation_type_id4 = 202
THEN 1 END) AS EslahGhosCount,
AVG(CASE WHEN operation_type_id1 = 202 THEN operation_type_progress1
WHEN operation_type_id2 = 202 THEN operation_type_progress2
WHEN operation_type_id3 = 202 THEN operation_type_progress3
WHEN operation_type_id4 = 202 THEN operation_type_progress4
END) AS EslahGhosAvg,
COUNT(CASE WHEN operation_type_id1 = 203 OR
operation_type_id2 = 203
OR
operation_type_id3 = 203
OR
operation_type_id4 = 203
THEN 1 END) AS TaransheBardariCount,
AVG(CASE WHEN operation_type_id1 = 203 THEN operation_type_progress1
WHEN operation_type_id2 = 203 THEN operation_type_progress2
WHEN operation_type_id3 = 203 THEN operation_type_progress3
WHEN operation_type_id4 = 203 THEN operation_type_progress4
END) AS TaransheBardariAvg,
COUNT(CASE WHEN operation_type_id1 = 204 OR
operation_type_id2 = 204
OR
operation_type_id3 = 204
OR
operation_type_id4 = 204
THEN 1 END) AS EhdasMeydanCount,
AVG(CASE WHEN operation_type_id1 = 204 THEN operation_type_progress1
WHEN operation_type_id2 = 204 THEN operation_type_progress2
WHEN operation_type_id3 = 204 THEN operation_type_progress3
WHEN operation_type_id4 = 204 THEN operation_type_progress4
END) AS EhdasMeydanAvg,
COUNT(CASE WHEN operation_type_id1 = 205 OR
operation_type_id2 = 205
OR
operation_type_id3 = 205
OR
operation_type_id4 = 205
THEN 1 END) AS EhdasTaghatoCount,
AVG(CASE WHEN operation_type_id1 = 205 THEN operation_type_progress1
WHEN operation_type_id2 = 205 THEN operation_type_progress2
WHEN operation_type_id3 = 205 THEN operation_type_progress3
WHEN operation_type_id4 = 205 THEN operation_type_progress4
END) AS EhdasTaghatoAvg,
COUNT(CASE WHEN operation_type_id1 = 206 OR
operation_type_id2 = 206
OR
operation_type_id3 = 206
OR
operation_type_id4 = 206
THEN 1 END) AS ImenSaziCount,
AVG(CASE WHEN operation_type_id1 = 206 THEN operation_type_progress1
WHEN operation_type_id2 = 206 THEN operation_type_progress2
WHEN operation_type_id3 = 206 THEN operation_type_progress3
WHEN operation_type_id4 = 206 THEN operation_type_progress4
END) AS ImenSaziAvg,
COUNT(CASE WHEN operation_type_id1 = 207 OR
operation_type_id2 = 207
OR
operation_type_id3 = 207
OR
operation_type_id4 = 207
THEN 1 END) AS EslahVoroodiCount,
AVG(CASE WHEN operation_type_id1 = 207 THEN operation_type_progress1
WHEN operation_type_id2 = 207 THEN operation_type_progress2
WHEN operation_type_id3 = 207 THEN operation_type_progress3
WHEN operation_type_id4 = 207 THEN operation_type_progress4
END) AS EslahVoroodiAvg,
COUNT(CASE WHEN operation_type_id1 = 208 OR
operation_type_id2 = 208
OR
operation_type_id3 = 208
OR
operation_type_id4 = 208
THEN 1 END) AS EhdasToonelCount,
AVG(CASE WHEN operation_type_id1 = 208 THEN operation_type_progress1
WHEN operation_type_id2 = 208 THEN operation_type_progress2
WHEN operation_type_id3 = 208 THEN operation_type_progress3
WHEN operation_type_id4 = 208 THEN operation_type_progress4
END) AS EhdasToonelAvg,
COUNT(CASE WHEN operation_type_id1 = 209 OR
operation_type_id2 = 209
OR
operation_type_id3 = 209
OR
operation_type_id4 = 209
THEN 1 END) AS EhdasVariantCount,
AVG(CASE WHEN operation_type_id1 = 209 THEN operation_type_progress1
WHEN operation_type_id2 = 209 THEN operation_type_progress2
WHEN operation_type_id3 = 209 THEN operation_type_progress3
WHEN operation_type_id4 = 209 THEN operation_type_progress4
END) AS EhdasVariantAvg,
COUNT(CASE WHEN operation_type_id1 = 210 OR
operation_type_id2 = 210
OR
operation_type_id3 = 210
OR
operation_type_id4 = 210
THEN 1 END) AS TarizAbnieFaniCount,
AVG(CASE WHEN operation_type_id1 = 210 THEN operation_type_progress1
WHEN operation_type_id2 = 210 THEN operation_type_progress2
WHEN operation_type_id3 = 210 THEN operation_type_progress3
WHEN operation_type_id4 = 210 THEN operation_type_progress4
END) AS TarizAbnieFaniAvg,
COUNT(CASE WHEN operation_type_id1 = 211 OR
operation_type_id2 = 211
OR
operation_type_id3 = 211
OR
operation_type_id4 = 211
THEN 1 END) AS SakhtHaelCount,
AVG(CASE WHEN operation_type_id1 = 211 THEN operation_type_progress1
WHEN operation_type_id2 = 211 THEN operation_type_progress2
WHEN operation_type_id3 = 211 THEN operation_type_progress3
WHEN operation_type_id4 = 211 THEN operation_type_progress4
END) AS SakhtHaelAvg,
COUNT(CASE WHEN operation_type_id1 = 212 OR
operation_type_id2 = 212
OR
operation_type_id3 = 212
OR
operation_type_id4 = 212
THEN 1 END) AS SayerCount,
AVG(CASE WHEN operation_type_id1 = 212 THEN operation_type_progress1
WHEN operation_type_id2 = 212 THEN operation_type_progress2
WHEN operation_type_id3 = 212 THEN operation_type_progress3
WHEN operation_type_id4 = 212 THEN operation_type_progress4
END) AS SayerAvg,
COUNT(*) AS countAll,province_fa,province_id FROM contract_subitems
WHERE project_type_id = 2
";
if ($this->fromDate && $this->toDate) {
$query .= " AND contract_date_from_parent_contract BETWEEN '{$fromDate} 00:00:00' and '{$toDate} 23:59:59'\n";
}
if ($this->province) {
$query .= " AND province_id = {$province}\n";
}
if ($this->last_status_id) {
$query .= " AND last_status_id = {$last_status_id}\n";
}
$query .= " GROUP BY province_id
ORDER BY province_id ASC;";
$data = DB::select($query);
$sum = [
'TarizRahCount' => 0,
'EslahGhosCount' => 0,
'TaransheBardariCount' => 0,
'EhdasMeydanCount' => 0,
'EhdasTaghatoCount' => 0,
'ImenSaziCount' => 0,
'EslahVoroodiCount' => 0,
'EhdasToonelCount' => 0,
'EhdasVariantCount' => 0,
'TarizAbnieFaniCount' => 0,
'SakhtHaelCount' => 0,
'SayerCount' => 0,
'countAll' => 0,
'TarizRahAvg' => 0,
'EslahGhosAvg' => 0,
'TaransheBardariAvg' => 0,
'EhdasMeydanAvg' => 0,
'EhdasTaghatoAvg' => 0,
'ImenSaziAvg' => 0,
'EslahVoroodiAvg' => 0,
'EhdasToonelAvg' => 0,
'EhdasVariantAvg' => 0,
'TarizAbnieFaniAvg' => 0,
'SakhtHaelAvg' => 0,
'SayerAvg' => 0,
];
foreach ($data as $value) {
$array[$value->province_id] = [
'province_fa' => $value->province_fa,
'TarizRahCount' => $value->TarizRahCount ?? 0,
'EslahGhosCount' => $value->EslahGhosCount ?? 0,
'TaransheBardariCount' => $value->TaransheBardariCount ?? 0,
'EhdasMeydanCount' => $value->EhdasMeydanCount ?? 0,
'EhdasTaghatoCount' => $value->EhdasTaghatoCount ?? 0,
'ImenSaziCount' => $value->ImenSaziCount ?? 0,
'EslahVoroodiCount' => $value->EslahVoroodiCount ?? 0,
'EhdasToonelCount' => $value->EhdasToonelCount ?? 0,
'EhdasVariantCount' => $value->EhdasVariantCount ?? 0,
'TarizAbnieFaniCount' => $value->TarizAbnieFaniCount ?? 0,
'SakhtHaelCount' => $value->SakhtHaelCount ?? 0,
'SayerCount' => $value->SakhtHaelCount ?? 0,
'countAll' => $value->countAll ?? 0,
'TarizRahAvg' => $value->TarizRahAvg ?? 0,
'EslahGhosAvg' => $value->EslahGhosAvg ?? 0,
'TaransheBardariAvg' => $value->TaransheBardariAvg ?? 0,
'EhdasMeydanAvg' => $value->EhdasMeydanAvg ?? 0,
'EhdasTaghatoAvg' => $value->EhdasTaghatoAvg ?? 0,
'ImenSaziAvg' => $value->ImenSaziAvg ?? 0,
'EslahVoroodiAvg' => $value->EslahVoroodiAvg ?? 0,
'EhdasToonelAvg' => $value->EhdasToonelAvg ?? 0,
'EhdasVariantAvg' => $value->EhdasVariantAvg ?? 0,
'TarizAbnieFaniAvg' => $value->TarizAbnieFaniAvg ?? 0,
'SakhtHaelAvg' => $value->SakhtHaelAvg ?? 0,
'SayerAvg' => $value->SakhtHaelAvg ?? 0,
];
$sum['TarizRahCount'] += $value->TarizRahCount;
$sum['EslahGhosCount'] += $value->EslahGhosCount;
$sum['TaransheBardariCount'] += $value->TaransheBardariCount;
$sum['EhdasMeydanCount'] += $value->EhdasMeydanCount;
$sum['EhdasTaghatoCount'] += $value->EhdasTaghatoCount;
$sum['ImenSaziCount'] += $value->ImenSaziCount;
$sum['EslahVoroodiCount'] += $value->EslahVoroodiCount;
$sum['EhdasToonelCount'] += $value->EhdasToonelCount;
$sum['EhdasVariantCount'] += $value->EhdasVariantCount;
$sum['TarizAbnieFaniCount'] += $value->TarizAbnieFaniCount;
$sum['SakhtHaelCount'] += $value->SakhtHaelCount;
$sum['SayerCount'] += $value->SakhtHaelCount;
$sum['countAll'] += $value->countAll;
$sum['TarizRahAvg'] += $value->TarizRahAvg;
$sum['EslahGhosAvg'] += $value->EslahGhosAvg;
$sum['TaransheBardariAvg'] += $value->TaransheBardariAvg;
$sum['EhdasMeydanAvg'] += $value->EhdasMeydanAvg;
$sum['EhdasTaghatoAvg'] += $value->EhdasTaghatoAvg;
$sum['ImenSaziAvg'] += $value->ImenSaziAvg;
$sum['EslahVoroodiAvg'] += $value->EslahVoroodiAvg;
$sum['EhdasToonelAvg'] += $value->EhdasToonelAvg;
$sum['EhdasVariantAvg'] += $value->EhdasVariantAvg;
$sum['TarizAbnieFaniAvg'] += $value->TarizAbnieFaniAvg;
$sum['SakhtHaelAvg'] += $value->SakhtHaelAvg;
$sum['SayerAvg'] += $value->SakhtHaelAvg;
}
// dd($data);
return view('excel.RoadDanger.CompareProgramAndFunction', [
'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'
]
],
];
}
}