Added importer logic in courier inward
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 17s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 20s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 15s
Laravel Pint / pint (pull_request) Successful in 3m10s
Laravel Larastan / larastan (pull_request) Failing after 6m4s

This commit is contained in:
dhanabalan
2026-11-06 19:37:52 +05:30
parent f78c2ea300
commit b4a1dbe29c

View File

@@ -3,9 +3,15 @@
namespace App\Filament\Imports; namespace App\Filament\Imports;
use App\Models\CourierInward; use App\Models\CourierInward;
use App\Models\Plant;
use Filament\Actions\Imports\ImportColumn; use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer; use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import; 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 class CourierInwardImporter extends Importer
{ {
@@ -16,30 +22,140 @@ class CourierInwardImporter extends Importer
return [ return [
ImportColumn::make('plant') ImportColumn::make('plant')
->requiredMapping() ->requiredMapping()
->relationship() ->exampleHeader('Plant Code')
->example('1000')
->label('Plant Code')
->relationship(resolveUsing: 'code') // Lookup Plant by code column
->rules(['required']), ->rules(['required']),
ImportColumn::make('date') ImportColumn::make('date')
->rules(['date']), ->requiredMapping()
ImportColumn::make('from_address_name'), ->exampleHeader('Date')
ImportColumn::make('cover_material'), ->example('06-11-2026')
ImportColumn::make('pod_number'), ->label('Date')
ImportColumn::make('courier_name'), ->rules(['required']),
ImportColumn::make('place'), ImportColumn::make('from_address_name')
ImportColumn::make('to_address_name'), ->exampleHeader('From Address Name')
ImportColumn::make('return_to_other_unit'), ->example('G Tech Industrial Resource & Services')
ImportColumn::make('created_by'), ->label('From Address Name')
ImportColumn::make('updated_by'), ->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 public function resolveRecord(): ?CourierInward
{ {
// return CourierInward::firstOrNew([ $warnMsg = [];
// // Update existing records, matching them by `$this->data['column_name']` $plantCod = $this->data['plant'];
// 'email' => $this->data['email'], $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 public static function getCompletedNotificationBody(Import $import): string