Files
pds/app/Filament/Imports/CourierInwardImporter.php
dhanabalan b4a1dbe29c
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
Added importer logic in courier inward
2026-11-06 19:37:52 +05:30

172 lines
6.0 KiB
PHP

<?php
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
{
protected static ?string $model = CourierInward::class;
public static function getColumns(): array
{
return [
ImportColumn::make('plant')
->requiredMapping()
->exampleHeader('Plant Code')
->example('1000')
->label('Plant Code')
->relationship(resolveUsing: 'code') // Lookup Plant by code column
->rules(['required']),
ImportColumn::make('date')
->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
{
$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;
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
{
$body = 'Your courier inward 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;
}
}