Added CustomerPoMaster importer and exporter #381
58
app/Filament/Exports/CustomerPoMasterExporter.php
Normal file
58
app/Filament/Exports/CustomerPoMasterExporter.php
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Exports;
|
||||||
|
|
||||||
|
use App\Models\CustomerPoMaster;
|
||||||
|
use Filament\Actions\Exports\ExportColumn;
|
||||||
|
use Filament\Actions\Exports\Exporter;
|
||||||
|
use Filament\Actions\Exports\Models\Export;
|
||||||
|
|
||||||
|
class CustomerPoMasterExporter extends Exporter
|
||||||
|
{
|
||||||
|
protected static ?string $model = CustomerPoMaster::class;
|
||||||
|
|
||||||
|
public static function getColumns(): array
|
||||||
|
{
|
||||||
|
static $rowNumber = 0;
|
||||||
|
return [
|
||||||
|
ExportColumn::make('no')
|
||||||
|
->label('NO')
|
||||||
|
->state(function ($record) use (&$rowNumber) {
|
||||||
|
// Increment and return the row number
|
||||||
|
return ++$rowNumber;
|
||||||
|
}),
|
||||||
|
ExportColumn::make('plant.code')
|
||||||
|
->label('PLANT CODE'),
|
||||||
|
ExportColumn::make('item.code')
|
||||||
|
->label('ITEM CODE'),
|
||||||
|
ExportColumn::make('customer_po')
|
||||||
|
->label('CUSTOMER PO'),
|
||||||
|
ExportColumn::make('customer_name')
|
||||||
|
->label('CUSTOMER NAME'),
|
||||||
|
ExportColumn::make('quantity')
|
||||||
|
->label('QUANTITY'),
|
||||||
|
ExportColumn::make('created_at')
|
||||||
|
->label('CREATED AT'),
|
||||||
|
ExportColumn::make('updated_at')
|
||||||
|
->label('UPDATED AT'),
|
||||||
|
ExportColumn::make('created_by')
|
||||||
|
->label('CREATED BY'),
|
||||||
|
ExportColumn::make('updated_by')
|
||||||
|
->label('UPDATED BY'),
|
||||||
|
ExportColumn::make('deleted_at')
|
||||||
|
->label('DELETED AT')
|
||||||
|
->enabledByDefault(false),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getCompletedNotificationBody(Export $export): string
|
||||||
|
{
|
||||||
|
$body = 'Your customer po master export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.';
|
||||||
|
|
||||||
|
if ($failedRowsCount = $export->getFailedRowsCount()) {
|
||||||
|
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $body;
|
||||||
|
}
|
||||||
|
}
|
||||||
140
app/Filament/Imports/CustomerPoMasterImporter.php
Normal file
140
app/Filament/Imports/CustomerPoMasterImporter.php
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
<?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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user