Files
pds/app/Filament/Imports/CustomerPoMasterImporter.php
dhanabalan 8dabfb1fa5
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 16s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 25s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 12s
Laravel Pint / pint (pull_request) Successful in 3m13s
Laravel Larastan / larastan (pull_request) Failing after 3m42s
Added CustomerPoMaster importer and exporter
2026-02-24 13:13:09 +05:30

141 lines
4.7 KiB
PHP

<?php
namespace App\Filament\Imports;
use App\Models\CustomerPoMaster;
use App\Models\Item;
use App\Models\Plant;
use App\Models\User;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use Filament\Facades\Filament;
use Str;
class CustomerPoMasterImporter extends Importer
{
protected static ?string $model = CustomerPoMaster::class;
public static function getColumns(): array
{
return [
ImportColumn::make('plant')
->requiredMapping()
->exampleHeader('Plant Code')
->example('1000')
->label('Plant Code')
->relationship(resolveUsing: 'code')
->rules(['required']),
ImportColumn::make('item')
->requiredMapping()
->exampleHeader('Item Code')
->example('630214')
->label('Item Code')
->relationship(resolveUsing: 'code')
->rules(['required']),
ImportColumn::make('customer_po')
->requiredMapping()
->exampleHeader('Customer PO')
->example('1JA0029512')
->label('Customer PO')
->rules(['required']),
ImportColumn::make('customer_name')
->requiredMapping()
->exampleHeader('Customer Name')
->example('KANKARIA MACHINERY STORE')
->label('Customer Name')
->rules(['required']),
ImportColumn::make('quantity')
->requiredMapping()
->exampleHeader('Quantity')
->example('5')
->label('Quantity')
->rules(['required']),
// ImportColumn::make('created_by')
// ->requiredMapping()
// ->exampleHeader('Created By')
// ->example('Admin')
// ->label('Created By')
// ->rules(['required']),
];
}
public function resolveRecord(): ?CustomerPoMaster
{
$warnMsg = [];
$plantCod = $this->data['plant'];
$plant = null;
$item = null;
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';
} else {
$item = Item::where('code', $this->data['item'])->where('plant_id', $plant->id)->first();
}
if (! $item) {
$warnMsg[] = 'Item not found';
}
}
if (Str::length($this->data['customer_po']) < 9) {
$warnMsg[] = 'Invalid Customer PO number found';
}
if (empty($this->data['customer_po'])) {
$warnMsg[] = 'Customer PO cannot be empty.';
}
// $user = User::where('name', $this->data['created_by'])->first();
// if (! $user) {
// $warnMsg[] = 'User not found';
// }
$user = Filament::auth()->user();
$operatorName = $user->name;
if (! empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg));
}
//else { // if (empty($warnMsg))
// $grMaster = GrMaster::where('plant_id', $plant->id)
// ->where('serial_number', $this->data['serial_number'])
// ->latest()
// ->first();
// if ($grMaster) {
// throw new RowImportFailedException('Serial number already exist!');
// }
// }
CustomerPoMaster::updateOrCreate([
'plant_id' => $plant->id,
'item_id' => $item->id,
'customer_po' => $this->data['customer_po'],
'customer_name' => $this->data['customer_name'],
'quantity' => $this->data['quantity'] ?? null,
'created_by' => $operatorName,
]);
return null;
// return new CustomerPoMaster();
}
public static function getCompletedNotificationBody(Import $import): string
{
$body = 'Your customer po master 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;
}
}