Files
pds/app/Filament/Imports/SaleOrderMasterImporter.php
dhanabalan a4dcbcd771
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 12s
Added sale order master importer and exporter
2026-09-01 12:26:02 +05:30

154 lines
5.5 KiB
PHP

<?php
namespace App\Filament\Imports;
use App\Models\Item;
use App\Models\Plant;
use App\Models\SaleOrderMaster;
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;
use Filament\Facades\Filament;
use Str;
class SaleOrderMasterImporter extends Importer
{
protected static ?string $model = SaleOrderMaster::class;
protected array $importedSaleOrders = [];
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('sale_order_number')
->requiredMapping()
->exampleHeader('Sale Order Number')
->example('1JA0029512')
->label('Sale Order Number')
->rules(['required']),
ImportColumn::make('supplier_name')
->exampleHeader('Supplier Name')
->example('ABC Supplier')
->label('Supplier Name'),
ImportColumn::make('quantity')
->exampleHeader('Quantity')
->example('100')
->label('Quantity'),
];
}
public function resolveRecord(): ?SaleOrderMaster
{
$warnMsg = [];
$plantCod = $this->data['plant'];
$plant = null;
$item = null;
$saleOrder = trim($this->data['sale_order_number']);
$supplierName = trim($this->data['supplier_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';
} 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['sale_order_number']) < 9) {
$warnMsg[] = 'Invalid Sale Order number found';
}
if (empty($this->data['sale_order_number'])) {
$warnMsg[] = 'Sale Order Number cannot be empty.';
}
if (empty($this->data['supplier_name'])) {
$warnMsg[] = 'Supplier Name cannot be empty.';
}
if (empty($this->data['quantity'])) {
$warnMsg[] = 'Quantity cannot be empty.';
}
if (! is_numeric($this->data['quantity'])) {
$warnMsg[] = 'Quantity must be a number.';
}
if (isset($this->importedSaleOrders[$saleOrder])) {
if (strtolower(trim($this->importedSaleOrders[$saleOrder])) != strtolower($supplierName)){
$warnMsg[] = "Sale Order '{$saleOrder}' has multiple supplier names in the import file.";
}
}
else {
$this->importedSaleOrders[$saleOrder] = $supplierName;
}
$existingPo = SaleOrderMaster::where('plant_id', $plant->id)->where('sale_order_number', $this->data['sale_order_number'])->first();
if ($existingPo && trim(strtolower($existingPo->supplier_name)) != trim(strtolower($this->data['supplier_name'])))
{
$warnMsg[] = "Sale Order '{$this->data['sale_order_number']}' is already mapped to supplier '{$existingPo->supplier_name}'.";
}
$existingPoItem = SaleOrderMaster::where('plant_id', $plant->id)->where('item_id', $item->id)->where('sale_order_number', $this->data['sale_order_number'])->where('supplier_name', $this->data['supplier_name'])->first();
if ($existingPoItem)
{
$warnMsg[] = "Sale Order '{$this->data['sale_order_number']}' is already exist with item code '{$this->data['item']}' and supplier name '{$this->data['supplier_name']}'.";
}
$user = Filament::auth()->user();
$operatorName = $user->name;
if (! empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg));
}
SaleOrderMaster::updateOrCreate([
'plant_id' => $plant->id,
'item_id' => $item->id,
'sale_order_number' => $this->data['sale_order_number'],
'supplier_name' => $this->data['supplier_name'],
'quantity' => $this->data['quantity'] ?? null,
'created_by' => $operatorName,
'updated_by' => $operatorName,
]);
return null;
}
public static function getCompletedNotificationBody(Import $import): string
{
$body = 'Your sale order 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;
}
}