Added guard_patrol_entry importer and exporter file
This commit is contained in:
61
app/Filament/Exports/GuardPatrolEntryExporter.php
Normal file
61
app/Filament/Exports/GuardPatrolEntryExporter.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Exports;
|
||||
|
||||
use App\Models\GuardPatrolEntry;
|
||||
use Filament\Actions\Exports\ExportColumn;
|
||||
use Filament\Actions\Exports\Exporter;
|
||||
use Filament\Actions\Exports\Models\Export;
|
||||
|
||||
class GuardPatrolEntryExporter extends Exporter
|
||||
{
|
||||
protected static ?string $model = GuardPatrolEntry::class;
|
||||
|
||||
public static function getColumns(): array
|
||||
{
|
||||
static $rowNumber = 0;
|
||||
|
||||
return [
|
||||
ExportColumn::make('no')
|
||||
->label('NO')
|
||||
->state(function ($record) use (&$rowNumber) {
|
||||
// Increment and return the row number
|
||||
return ++$rowNumber;
|
||||
}),
|
||||
// ExportColumn::make('id')
|
||||
// ->label('ID'),
|
||||
ExportColumn::make('plant.name')
|
||||
->label('PLANT'),
|
||||
ExportColumn::make('guardNames.name')
|
||||
->label('GUARD NAME'),
|
||||
ExportColumn::make('checkPointNames.name')
|
||||
->label('CHECK POINT NAME'),
|
||||
ExportColumn::make('reader_code')
|
||||
->label('READER CODE'),
|
||||
ExportColumn::make('patrol_time')
|
||||
->label('PATROL TIME'),
|
||||
ExportColumn::make('created_at')
|
||||
->label('CREATED AT'),
|
||||
ExportColumn::make('created_by')
|
||||
->label('CREATED BY'),
|
||||
ExportColumn::make('updated_at')
|
||||
->label('UPDATED AT'),
|
||||
ExportColumn::make('Updated_by')
|
||||
->label('UPDATED BY'),
|
||||
ExportColumn::make('deleted_at')
|
||||
->enabledByDefault(false)
|
||||
->label('DELETED AT'),
|
||||
];
|
||||
}
|
||||
|
||||
public static function getCompletedNotificationBody(Export $export): string
|
||||
{
|
||||
$body = 'Your guard patrol entry export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.';
|
||||
|
||||
if ($failedRowsCount = $export->getFailedRowsCount()) {
|
||||
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.';
|
||||
}
|
||||
|
||||
return $body;
|
||||
}
|
||||
}
|
||||
160
app/Filament/Imports/GuardPatrolEntryImporter.php
Normal file
160
app/Filament/Imports/GuardPatrolEntryImporter.php
Normal file
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Imports;
|
||||
|
||||
use App\Models\CheckPointName;
|
||||
use App\Models\GuardName;
|
||||
use App\Models\GuardPatrolEntry;
|
||||
use App\Models\Plant;
|
||||
use Carbon\Carbon;
|
||||
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
||||
use Filament\Actions\Imports\ImportColumn;
|
||||
use Filament\Actions\Imports\Importer;
|
||||
use Filament\Actions\Imports\Models\Import;
|
||||
use Filament\Facades\Filament;
|
||||
use Str;
|
||||
|
||||
class GuardPatrolEntryImporter extends Importer
|
||||
{
|
||||
protected static ?string $model = GuardPatrolEntry::class;
|
||||
|
||||
public static function getColumns(): array
|
||||
{
|
||||
return [
|
||||
ImportColumn::make('plant')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Plant Name')
|
||||
->example('Ransar Industries-I')
|
||||
->label('Plant Name')
|
||||
->relationship(resolveUsing: 'name')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('guardNames')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Guard Name')
|
||||
->example('ARUMUGAM - HG')
|
||||
->label('Guard Name')
|
||||
->relationship(resolveUsing: 'name')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('checkPointNames')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Check Point Name')
|
||||
->example('STP BACKSIDE')
|
||||
->label('Check Point Name')
|
||||
->relationship(resolveUsing: 'name')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('reader_code')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Reader Code')
|
||||
->example('0C03-25010112')
|
||||
->label('Reader Code'),
|
||||
ImportColumn::make('patrol_time')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Patrol Time')
|
||||
->example('01-01-2025 08:00:00')
|
||||
->label('Patrol Time')
|
||||
->rules(['required']),
|
||||
// ImportColumn::make('created_by')
|
||||
// ->requiredMapping()
|
||||
// ->exampleHeader('Created By')
|
||||
// ->example(Filament::auth()->user()->name ?? 'Admin')
|
||||
// ->label('Created By')
|
||||
// ->rules(['required']),
|
||||
// ImportColumn::make('updated_by')
|
||||
// ->requiredMapping()
|
||||
// ->rules(['required']),
|
||||
];
|
||||
}
|
||||
|
||||
public function resolveRecord(): ?GuardPatrolEntry
|
||||
{
|
||||
$warnMsg = [];
|
||||
$plant = Plant::where('name', $this->data['plant'])->first();
|
||||
$guardNames = null;
|
||||
$checkPointNames = null;
|
||||
$patrolDateTime = null; //$fdateTime = null;
|
||||
if (!$plant) {
|
||||
$warnMsg[] = "Plant not found"; // '" . $this->data['plant'] . "'
|
||||
}
|
||||
else
|
||||
{
|
||||
$guardNames = GuardName::where('plant_id', $plant->id)->where('name', $this->data['guardNames'])->first();
|
||||
if (!$guardNames) {
|
||||
$warnMsg[] = "Guard name not found";
|
||||
}
|
||||
else
|
||||
{
|
||||
$checkPointNames = CheckPointName::where('plant_id', $plant->id)->where('name', $this->data['checkPointNames'])->first();
|
||||
if (!$checkPointNames) {
|
||||
$warnMsg[] = "Check point name not found";
|
||||
}
|
||||
else
|
||||
{
|
||||
$patrolTime = $this->data['patrol_time'];//$fromDate = $this->data['from_datetime'];
|
||||
$formats = ['d-m-Y H:i', 'd-m-Y H:i:s']; //'07-05-2025 08:00' or '07-05-2025 08:00:00'
|
||||
|
||||
foreach ($formats as $format) {
|
||||
try {
|
||||
$patrolDateTime = Carbon::createFromFormat($format, $patrolTime);
|
||||
break;
|
||||
} catch (\Exception $e) {
|
||||
// Optionally collect warning messages
|
||||
// $warnMsg[] = "Date format mismatch with format: $format";
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($patrolDateTime)) {
|
||||
// throw new \Exception('Invalid date time format');
|
||||
$warnMsg[] = "Invalid 'Patrol DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
|
||||
}
|
||||
else
|
||||
{
|
||||
$guardEntryFound = GuardPatrolEntry::where('plant_id', $plant->id)->where('guard_name_id', $guardNames->id)->where('check_point_name_id', $checkPointNames->id)->where('patrol_time', $patrolDateTime->format('Y-m-d H:i:s'))->first();
|
||||
if ($guardEntryFound) {
|
||||
$warnMsg[] = "Duplicate guard patrol entry found";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$createdBy = Filament::auth()->user()->name;// ?? 'Admin'
|
||||
if (!$createdBy) {
|
||||
$warnMsg[] = "Invalid created by name found";
|
||||
}
|
||||
|
||||
if (!empty($warnMsg)) {
|
||||
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||
}
|
||||
|
||||
GuardPatrolEntry::updateOrCreate([
|
||||
'plant_id' => $plant->id,
|
||||
'guard_name_id' => $guardNames->id,
|
||||
'check_point_name_id' => $checkPointNames->id,
|
||||
'patrol_time' => $patrolDateTime->format('Y-m-d H:i:s')
|
||||
],
|
||||
[
|
||||
'reader_code' => $this->data['reader_code'],
|
||||
'created_by' => $createdBy,
|
||||
'updated_by' => $createdBy
|
||||
]
|
||||
);
|
||||
return null;
|
||||
// // return GuardPatrolEntry::firstOrNew([
|
||||
// // // Update existing records, matching them by `$this->data['column_name']`
|
||||
// // 'email' => $this->data['email'],
|
||||
// // ]);
|
||||
|
||||
// return new GuardPatrolEntry();
|
||||
}
|
||||
|
||||
public static function getCompletedNotificationBody(Import $import): string
|
||||
{
|
||||
$body = 'Your guard patrol entry import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.';
|
||||
|
||||
if ($failedRowsCount = $import->getFailedRowsCount()) {
|
||||
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.';
|
||||
}
|
||||
|
||||
return $body;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user