139 lines
4.8 KiB
PHP
139 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Imports;
|
|
|
|
use App\Models\ProductionQuantity;
|
|
use Filament\Actions\Imports\ImportColumn;
|
|
use Filament\Actions\Imports\Importer;
|
|
use Filament\Actions\Imports\Models\Import;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class ProductionQuantityImporter extends Importer
|
|
{
|
|
protected static ?string $model = ProductionQuantity::class;
|
|
|
|
public static function getColumns(): array
|
|
{
|
|
return [
|
|
ImportColumn::make('created_at')
|
|
->requiredMapping()
|
|
->exampleHeader('Created DateTime')
|
|
->example('12-02-2025 15:51:00')
|
|
->label('Created DateTime')
|
|
->rules(['required']),
|
|
// ImportColumn::make('hourly_quantity')
|
|
// ->requiredMapping()
|
|
// ->exampleHeader('Hourly Quantity')
|
|
// ->label('Hourly Quantity')
|
|
// ->numeric()
|
|
// ->rules(['required', 'integer']),
|
|
ImportColumn::make('item')
|
|
->requiredMapping()
|
|
->exampleHeader('Item Code')
|
|
->example('123456')
|
|
->label('Item Code')
|
|
->relationship(resolveUsing:'code')
|
|
->rules(['required']),
|
|
ImportColumn::make('serial_number')
|
|
->requiredMapping()
|
|
->exampleHeader('Serial Number')
|
|
->example('12345678901234')
|
|
->label('Serial Number')
|
|
->rules(['required']),
|
|
ImportColumn::make('line')
|
|
->requiredMapping()
|
|
->exampleHeader('Line Name')
|
|
->example('4 inch pump line')
|
|
->label('Line Name')
|
|
->relationship(resolveUsing:'name')
|
|
->rules(['required']),
|
|
ImportColumn::make('shift')
|
|
->requiredMapping()
|
|
->exampleHeader('Shift Name')
|
|
->example('Day')
|
|
->label('Shift Name')
|
|
->relationship(resolveUsing:'name')
|
|
->rules(['required']),
|
|
ImportColumn::make('plant')
|
|
->requiredMapping()
|
|
->exampleHeader('Plant Name')
|
|
->example('Ransar Industries-I')
|
|
->label('Plant Name')
|
|
->relationship(resolveUsing:'name')
|
|
->rules(['required']),
|
|
ImportColumn::make('updated_at')
|
|
->requiredMapping()
|
|
->exampleHeader('Updated DateTime')
|
|
->example('12-02-2025 15:51:00')
|
|
->label('Updated DateTime')
|
|
->rules(['required']),
|
|
];
|
|
}
|
|
|
|
public function resolveRecord(): ?ProductionQuantity
|
|
{
|
|
// return ProductionQuantity::firstOrNew([
|
|
// // Update existing records, matching them by `$this->data['column_name']`
|
|
// 'email' => $this->data['email'],
|
|
// ]);
|
|
|
|
return new ProductionQuantity();
|
|
}
|
|
|
|
public static function getCompletedNotificationBody(Import $import): string
|
|
{
|
|
$body = 'Your production quantity 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;
|
|
}
|
|
|
|
public function processRow(array $row): Model
|
|
{
|
|
// Activate import flag
|
|
ProductionQuantity::$importing = true;
|
|
|
|
try {
|
|
// Create record with relationships
|
|
$productionQuantity = ProductionQuantity::create([
|
|
'created_at' => $row['created_at'],
|
|
'item_id' => $this->resolveItemId($row['item']),
|
|
'serial_number' => $row['serial_number'],
|
|
'line_id' => $this->resolveLineId($row['line']),
|
|
'shift_id' => $this->resolveShiftId($row['shift']),
|
|
'plant_id' => $this->resolvePlantId($row['plant']),
|
|
'updated_at' => $row['updated_at']
|
|
]);
|
|
} finally {
|
|
// Always disable flag even if errors occur
|
|
ProductionQuantity::$importing = false;
|
|
}
|
|
|
|
return $productionQuantity;
|
|
}
|
|
|
|
// Relationship resolvers
|
|
private function resolveItemId(string $code): int
|
|
{
|
|
return \App\Models\Item::where('code', $code)->first()->id;
|
|
}
|
|
|
|
private function resolveLineId(string $name): int
|
|
{
|
|
return \App\Models\Line::where('name', $name)->first()->id;
|
|
}
|
|
|
|
private function resolveShiftId(string $name): int
|
|
{
|
|
return \App\Models\Shift::where('name', $name)->first()->id;
|
|
}
|
|
|
|
private function resolvePlantId(string $name): int
|
|
{
|
|
return \App\Models\Plant::where('name', $name)->first()->id;
|
|
}
|
|
}
|