diff --git a/app/Filament/Imports/CourierInwardImporter.php b/app/Filament/Imports/CourierInwardImporter.php index 8ec77df5..6e001d8a 100644 --- a/app/Filament/Imports/CourierInwardImporter.php +++ b/app/Filament/Imports/CourierInwardImporter.php @@ -3,9 +3,15 @@ namespace App\Filament\Imports; use App\Models\CourierInward; +use App\Models\Plant; use Filament\Actions\Imports\ImportColumn; use Filament\Actions\Imports\Importer; use Filament\Actions\Imports\Models\Import; +use Filament\Facades\Filament; +use Illuminate\Support\Facades\Log; +use Illuminate\Support\Str; +use Filament\Actions\Imports\Exceptions\RowImportFailedException; +use Carbon\Carbon; class CourierInwardImporter extends Importer { @@ -16,30 +22,140 @@ class CourierInwardImporter extends Importer return [ ImportColumn::make('plant') ->requiredMapping() - ->relationship() + ->exampleHeader('Plant Code') + ->example('1000') + ->label('Plant Code') + ->relationship(resolveUsing: 'code') // Lookup Plant by code column ->rules(['required']), ImportColumn::make('date') - ->rules(['date']), - ImportColumn::make('from_address_name'), - ImportColumn::make('cover_material'), - ImportColumn::make('pod_number'), - ImportColumn::make('courier_name'), - ImportColumn::make('place'), - ImportColumn::make('to_address_name'), - ImportColumn::make('return_to_other_unit'), - ImportColumn::make('created_by'), - ImportColumn::make('updated_by'), + ->requiredMapping() + ->exampleHeader('Date') + ->example('06-11-2026') + ->label('Date') + ->rules(['required']), + ImportColumn::make('from_address_name') + ->exampleHeader('From Address Name') + ->example('G Tech Industrial Resource & Services') + ->label('From Address Name') + ->rules(['required']), + ImportColumn::make('cover_material') + ->exampleHeader('Cover/Material') + ->example('Cover') + ->label('Cover/Material') + ->rules(['required']), + ImportColumn::make('pod_number') + ->exampleHeader('POD Number') + ->example('ET563615242IN') + ->label('POD Number') + ->rules(['required']), + ImportColumn::make('courier_name') + ->exampleHeader('Courier Name') + ->example('Trackon') + ->label('Courier Name') + ->rules(['required']), + ImportColumn::make('place') + ->exampleHeader('Place') + ->example('Ahmedabad') + ->label('Place') + ->rules(['required']), + ImportColumn::make('to_address_name') + ->exampleHeader('To Address Name') + ->example('Padmini') + ->label('To Address Name') + ->rules(['required']), + ImportColumn::make('return_to_other_unit') + ->exampleHeader('Return To Other Unit') + ->example('') + ->label('Return To Other Unit'), ]; } public function resolveRecord(): ?CourierInward { - // return CourierInward::firstOrNew([ - // // Update existing records, matching them by `$this->data['column_name']` - // 'email' => $this->data['email'], - // ]); + $warnMsg = []; + $plantCod = $this->data['plant']; + $plant = null; + $date = trim($this->data['date']); + $fromAddName = trim($this->data['from_address_name']); + $coverMat = trim($this->data['cover_material']); + $podNo = trim($this->data['pod_number']); + $courierName = trim($this->data['courier_name']); + $place = trim($this->data['place']); + $toAddName = trim($this->data['to_address_name']); + $returnToOtherUnit = trim($this->data['return_to_other_unit']); + $userName = Filament::auth()->user()->name; - return new CourierInward(); + if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) { + $warnMsg[] = 'Invalid plant code found'; + } else { + $plant = Plant::where('code', $plantCod)->first(); + if (! $plant) { + $warnMsg[] = 'Plant not found'; // '" . $plantCod . "' + } + } + + if (trim($date) == '' || trim($date) == null) { + $warnMsg[] = 'Date is required'; + } + + if (trim($fromAddName) == '' || trim($fromAddName) == null) { + $warnMsg[] = 'From Address Name is required'; + } + + if (trim($coverMat) == '' || trim($coverMat) == null) { + $warnMsg[] = 'Cover Material is required'; + } + + if (trim($podNo) == '' || trim($podNo) == null) { + $warnMsg[] = 'POD Number is required'; + } + + if (trim($courierName) == '' || trim($courierName) == null) { + $warnMsg[] = 'Courier Name is required'; + } + + if (trim($place) == '' || trim($place) == null) { + $warnMsg[] = 'Place is required'; + } + + if (trim($toAddName) == '' || trim($toAddName) == null) { + $warnMsg[] = 'To Address Name is required'; + } + + try { + $parsedDate = Carbon::createFromFormat('!d-m-Y', trim($date)); + + if ($parsedDate === false || $parsedDate->format('d-m-Y') !== trim($date)) { + throw new \Exception('Invalid date'); + } + + $date = $parsedDate->format('Y-m-d'); + + } catch (\Exception $e) { + $warnMsg[] = 'Invalid date format. Expected format: dd-mm-yyyy'; + } + + if (! empty($warnMsg)) { + throw new RowImportFailedException(implode(', ', $warnMsg)); + } + + $record = CourierInward::create([ + 'plant_id' => $plant->id, + 'date' => $date, + 'from_address_name' => $fromAddName, + 'cover_material' => $coverMat, + 'pod_number' => $podNo, + 'courier_name' => $courierName, + 'place' => $place, + 'to_address_name' => $toAddName, + 'return_to_other_unit' => $returnToOtherUnit, + 'created_by' => $userName, + 'updated_by' => $userName, + ]); + + return null; + + // return new CourierInward(); } public static function getCompletedNotificationBody(Import $import): string