137 lines
3.9 KiB
PHP
137 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
|
use Maatwebsite\Excel\Concerns\WithHeadings;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
|
use Maatwebsite\Excel\Concerns\WithEvents;
|
|
use Maatwebsite\Excel\Events\AfterSheet;
|
|
|
|
class ExportExcel implements FromCollection, WithHeadings, ShouldAutoSize
|
|
{
|
|
|
|
public $tableName;
|
|
public $columnNames;
|
|
public $exceptsColumns;
|
|
public $module;
|
|
|
|
public function __construct(string $tableName, $columnNames = "", $module = null)
|
|
{
|
|
$this->tableName = $tableName;
|
|
$this->module = $module ?? get("module");
|
|
|
|
$exceptsColumns = [
|
|
'created_at',
|
|
'updated_at',
|
|
'json',
|
|
'pic',
|
|
'slug',
|
|
'email_verified_at',
|
|
'password',
|
|
'permissions',
|
|
'alias',
|
|
'remember_token',
|
|
'recover',
|
|
'last_seen',
|
|
'branslar',
|
|
'ust',
|
|
'uid',
|
|
'note'
|
|
];
|
|
/*
|
|
try {
|
|
$columnNames = explode(",", $columnNames);
|
|
$this->columnNames = $columnNames;
|
|
} catch (\Throwable $th) {
|
|
$this->columnNames = array_diff(Schema::getColumnListing($this->tableName), $exceptsColumns);
|
|
}
|
|
*/
|
|
$this->columnNames = array_diff(Schema::getColumnListing($this->tableName), $exceptsColumns);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
public function collection()
|
|
{
|
|
|
|
$query = db($this->tableName)
|
|
->select($this->columnNames);
|
|
|
|
if($this->tableName == "users") {
|
|
$query = $query->whereNotIn("level", ['Admin']);
|
|
}
|
|
|
|
// Apply employer view filters for weldlog-employer-view module
|
|
if($this->module === 'weldlog-employer-view' && $this->tableName === 'weld_logs') {
|
|
$query = $this->applyEmployerViewFilters($query);
|
|
}
|
|
|
|
return $query->get();
|
|
}
|
|
|
|
/**
|
|
* Apply employer view specific filters to weld_logs table
|
|
* Filters:
|
|
* 1. Exclude records with repair_status = 'Repair' in repair_logs
|
|
* 2. Exclude records with NDT results other than 'Accept / Годен' or empty/null
|
|
*/
|
|
private function applyEmployerViewFilters($query)
|
|
{
|
|
// Filter 1: Exclude repair logs
|
|
$repairFilter = "NOT EXISTS (
|
|
SELECT 1 FROM repair_logs
|
|
WHERE repair_logs.iso_number = weld_logs.iso_number
|
|
AND repair_logs.new_joint_no = weld_logs.no_of_the_joint_as_per_as_built_survey
|
|
AND repair_logs.repair_status = 'Repair'
|
|
)";
|
|
|
|
// Filter 2: Only acceptable NDT results
|
|
$ndtFields = [
|
|
'vt_result',
|
|
'rt_result',
|
|
'ut_result',
|
|
'pt_result',
|
|
'mt_result',
|
|
'pmi_result',
|
|
'ferrite_result',
|
|
'ht_result'
|
|
];
|
|
|
|
$ndtFilters = [];
|
|
foreach ($ndtFields as $field) {
|
|
$ndtFilters[] = "({$field} IN ('Accept / Годен', '') OR {$field} IS NULL)";
|
|
}
|
|
|
|
$ndtFilter = implode(' AND ', $ndtFilters);
|
|
|
|
$combinedFilter = "({$repairFilter}) AND ({$ndtFilter})";
|
|
|
|
return $query->whereRaw($combinedFilter);
|
|
}
|
|
|
|
public function headings(): array
|
|
{
|
|
return $this->columnNames;
|
|
}
|
|
/*
|
|
public function registerEvents(): array
|
|
{
|
|
return [
|
|
AfterSheet::class => function(AfterSheet $event)
|
|
{
|
|
$cellRange = 'A1:G1'; // All headers
|
|
$event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setBorder($cellRange, 'thin');
|
|
$event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);
|
|
},
|
|
];
|
|
}
|
|
*/
|
|
}
|