Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 19s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 20s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 16s
Laravel Larastan / larastan (pull_request) Failing after 4m14s
Laravel Pint / pint (pull_request) Successful in 7m14s
164 lines
5.6 KiB
PHP
164 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Imports;
|
|
|
|
use App\Models\CourierOutward;
|
|
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 Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
|
use Illuminate\Support\Str;
|
|
use Carbon\Carbon;
|
|
|
|
class CourierOutwardImporter extends Importer
|
|
{
|
|
protected static ?string $model = CourierOutward::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('to_address')
|
|
->exampleHeader('To Address Name')
|
|
->example('G Tech Industrial Resource & Services')
|
|
->label('To Address Name')
|
|
->rules(['required']),
|
|
ImportColumn::make('cover_material')
|
|
->exampleHeader('Cover/Material')
|
|
->example('Cover')
|
|
->label('Cover/Material')
|
|
->rules(['required']),
|
|
ImportColumn::make('name_of_sender')
|
|
->exampleHeader('Name Of Sender')
|
|
->example('Suresh')
|
|
->label('Name Of Sender')
|
|
->rules(['required']),
|
|
ImportColumn::make('place_pincode')
|
|
->exampleHeader('Which Place & PinCode')
|
|
->example('Chennai - 628001')
|
|
->label('Which Place & PinCode')
|
|
->rules(['required']),
|
|
ImportColumn::make('pod_number')
|
|
->exampleHeader('POD Number')
|
|
->example('ET563615242IN')
|
|
->label('POD Number')
|
|
->rules(['required']),
|
|
ImportColumn::make('remark')
|
|
->exampleHeader('Remark')
|
|
->example('')
|
|
->label('Remark'),
|
|
];
|
|
}
|
|
|
|
public function resolveRecord(): ?CourierOutward
|
|
{
|
|
$warnMsg = [];
|
|
$plantCod = $this->data['plant'];
|
|
$plant = null;
|
|
$date = trim($this->data['date']);
|
|
$toAddName = trim($this->data['to_address']);
|
|
$coverMat = trim($this->data['cover_material']);
|
|
$nameOfSender = trim($this->data['name_of_sender']);
|
|
$placePinCode = trim($this->data['place_pincode']);
|
|
$podNo = trim($this->data['pod_number']);
|
|
$remark = trim($this->data['remark']);
|
|
$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($toAddName) == '' || trim($toAddName) == null) {
|
|
$warnMsg[] = 'From Address Name is required';
|
|
}
|
|
|
|
if (trim($coverMat) == '' || trim($coverMat) == null) {
|
|
$warnMsg[] = 'Cover Material is required';
|
|
}
|
|
|
|
if (trim($nameOfSender) == '' || trim($nameOfSender) == null) {
|
|
$warnMsg[] = 'Name Of Sender is required';
|
|
}
|
|
|
|
if (trim($placePinCode) == '' || trim($placePinCode) == null) {
|
|
$warnMsg[] = 'Which place and pincode is required';
|
|
}
|
|
|
|
if (trim($podNo) == '' || trim($podNo) == null) {
|
|
$warnMsg[] = 'POD Number is required';
|
|
}
|
|
|
|
if (trim($remark) == '' || trim($remark) == null) {
|
|
$warnMsg[] = 'Remark 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 = CourierOutward::create([
|
|
'plant_id' => $plant->id,
|
|
'date' => $date,
|
|
'to_address' => $toAddName,
|
|
'cover_material' => $coverMat,
|
|
'name_of_sender' => $nameOfSender,
|
|
'place_pincode' => $placePinCode,
|
|
'pod_number' => $podNo,
|
|
'remark' => $remark,
|
|
'created_by' => $userName,
|
|
'updated_by' => $userName,
|
|
]);
|
|
|
|
return null;
|
|
|
|
// return new CourierOutward();
|
|
}
|
|
|
|
public static function getCompletedNotificationBody(Import $import): string
|
|
{
|
|
$body = 'Your courier outward 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;
|
|
}
|
|
}
|