Added block_reference column and updated sample data and updated validation func. on import

This commit is contained in:
dhanabalan
2025-05-12 19:48:05 +05:30
parent fb3ce483c7
commit b81d0a0de0

View File

@@ -2,11 +2,21 @@
namespace App\Filament\Imports;
use App\Models\Block;
use App\Models\Item;
use App\Models\Line;
use App\Models\Plant;
use App\Models\ProductionQuantity;
use App\Models\Shift;
use App\Models\User;
use Carbon\Carbon;
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 Illuminate\Database\Eloquent\Model;
use Str;
class ProductionQuantityImporter extends Importer
{
@@ -18,59 +28,65 @@ class ProductionQuantityImporter extends Importer
ImportColumn::make('created_at')
->requiredMapping()
->exampleHeader('Created DateTime')
->example('12-02-2025 15:51:00')
->example(['01-01-2025 08:00:00', '01-01-2025 19:30:00'])
->label('Created DateTime')
->rules(['required']),
ImportColumn::make('production_order')
->requiredMapping()
->exampleHeader('Production Order')
->example('1234567')
->example(['1234567', '1234567'])
->label('Production Order')
->numeric(),
ImportColumn::make('item')
->requiredMapping()
->exampleHeader('Item Code')
->example('123456')
->example(['123456', '123456'])
->label('Item Code')
->relationship(resolveUsing:'code')
->rules(['required']),
ImportColumn::make('serial_number')
->requiredMapping()
->exampleHeader('Serial Number')
->example('12345678901234')
->example(['12345678901234', '12345678902234'])
->label('Serial Number')
->rules(['required']),
ImportColumn::make('line')
->requiredMapping()
->exampleHeader('Line Name')
->example('4 inch pump line')
->example(['4 inch pump line', '4 inch pump line'])
->label('Line Name')
->relationship(resolveUsing:'name')
->rules(['required']),
ImportColumn::make('block_reference')
->requiredMapping() // Or optionalMapping() if not always present
->exampleHeader('Block Name')
->example(['Block A', 'Block A'])
->label('Block Name')
->rules(['required']), // Or remove if not required
ImportColumn::make('shift')
->requiredMapping()
->exampleHeader('Shift Name')
->example('Day')
->example(['Day', 'Night'])
->label('Shift Name')
->relationship(resolveUsing:'name')
->rules(['required']),
ImportColumn::make('plant')
->requiredMapping()
->exampleHeader('Plant Name')
->example('Ransar Industries-I')
->example(['Ransar Industries-I', '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')
->example(['01-01-2025 08:00:00', '01-01-2025 19:30:00'])
->label('Updated DateTime')
->rules(['required']),
ImportColumn::make('operator_id')
->requiredMapping()
->exampleHeader('Operator ID')
->example('admin')
->example([Filament::auth()->user()->name, Filament::auth()->user()->name])
->label('Operator ID')
->rules(['required']),
];
@@ -78,31 +94,142 @@ class ProductionQuantityImporter extends Importer
public function resolveRecord(): ?ProductionQuantity
{
$plant = \App\Models\Plant::where('name', $this->data['plant'])->first();
$item = \App\Models\Item::where('code', $this->data['item'])->where('plant_id', $plant->id)->first();
$line = \App\Models\Line::where('name', $this->data['line'])->where('plant_id', $plant->id)->first();
$shift = \App\Models\Shift::where('name', $this->data['shift'])->where('plant_id', $plant->id)->first();
if (!$plant || !$item || !$line || !$shift) {
// Optionally handle missing plant/item/line/shift
return null;
$warnMsg = [];
$plant = Plant::where('name', $this->data['plant'])->first();
$line = null;
$block = null;
if (!$plant) {
$warnMsg[] = "Plant not found";
}
return ProductionQuantity::updateOrCreate([
'serial_number' => $this->data['serial_number'],
'plant_id' => $plant->id,
],
[
'shift_id' => $shift->id,
'line_id' => $line->id,
'item_id' => $item->id,
'production_order' => $this->data['production_order'] ?? null,
'created_at' => $this->data['created_at'],
'updated_at' => $this->data['updated_at']
]
);
// return ProductionQuantity::firstOrNew([
// // Update existing records, matching them by `$this->data['column_name']`
// 'email' => $this->data['email'],
else {
$line = Line::where('name', $this->data['line'])->where('plant_id', $plant->id)->first();
//block_reference
$block = Block::where('name', $this->data['block_reference'])->where('plant_id', $plant->id)->first();
}
if (!$line) {
$warnMsg[] = "Line not found";
}
$shift = null;
if (!$block) {
$warnMsg[] = "Block not found";
}
else {
$shift = Shift::where('name', $this->data['shift'])->where('plant_id', $plant->id)->where('block_id', $block->id)->first();
}
//$shift = Shift::where('id', $this->data['shift'])->where('plant_id', $plant->id)->first();
if (!$shift) {
$warnMsg[] = "Shift not found";
}
$item = null;
if (!$plant) {
$item = Item::where('code', $this->data['item'])->where('plant_id', $plant->id)->first();
}
if (!$item) {
$warnMsg[] = "Item not found";
}
if (Str::length($this->data['serial_number']) < 9 || !ctype_alnum($this->data['serial_number'])) {
$warnMsg[] = "Invalid serial number found";
}
if (Str::length($this->data['production_order']) > 0 && (Str::length($this->data['production_order']) < 7 || !is_numeric($this->data['production_order']))) {
$warnMsg[] = "Invalid production order found";
}
$fromDate = $this->data['created_at'];
$toDate = $this->data['updated_at'];
$formats = ['d-m-Y H:i', 'd-m-Y H:i:s']; //'07-05-2025 08:00' or '07-05-2025 08:00:00'
$fdateTime = null;
$tdateTime = null;
// Try parsing with multiple formats
foreach ($formats as $format) {
try {
$fdateTime = Carbon::createFromFormat($format, $fromDate);
break;
} catch (\Exception $e) {
// Optionally collect warning messages
// $warnMsg[] = "Date format mismatch with format: $format";
}
}
foreach ($formats as $format) {
try {
$tdateTime = Carbon::createFromFormat($format, $toDate);
break;
} catch (\Exception $e) {
// Optionally collect warning messages
// $warnMsg[] = "Date format mismatch with format: $format";
}
}
// $fDateOnly = '';
if (!isset($fdateTime)) {
// throw new \Exception('Invalid date time format');
$warnMsg[] = "Invalid 'Created DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
}
// else {
// $fDateOnly = $fdateTime->toDateString();
// }
if (!isset($tdateTime)) {
$warnMsg[] = "Invalid 'Updated DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
}
if (isset($fdateTime) && isset($tdateTime)) {
if ($fdateTime->greaterThan($tdateTime)) {
$warnMsg[] = "'Created DataTime' is greater than 'Updated DateTime'.";
}
}
// if (!$fromDate) {
// $warnMsg[] = "Invalid 'Created DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
// }
// else if (!$toDate) {
// $warnMsg[] = "Invalid 'Updated DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
// }
$user = User::where('name', $this->data['operator_id'])->first();
if (!$user) {
$warnMsg[] = "Operator ID not found";
}
if (!empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg));
}
else { //if (empty($warnMsg))
$productionQuan = ProductionQuantity::where('plant_id', $plant->id)
->where('serial_number', $this->data['serial_number'])
->latest()
->first();
if ($productionQuan) {
throw new RowImportFailedException("Serial number already exist!");
}
}
ProductionQuantity::updateOrCreate([
'serial_number' => $this->data['serial_number'],
'plant_id' => $plant->id,
'shift_id' => $shift->id,
'line_id' => $line->id,
'item_id' => $item->id,
'production_order' => $this->data['production_order'] ?? null,
'created_at' => $fdateTime->format('Y-m-d H:i:s'),//$this->data['created_at'],
'updated_at' => $tdateTime->format('Y-m-d H:i:s'),//$this->data['updated_at'],
'operator_id' => $this->data['operator_id'],
]);
// ProductionQuantity::updateOrCreate([
// 'serial_number' => $this->data['serial_number'],
// 'plant_id' => $plant->id,
// ],
// [
// 'shift_id' => $shift->id,
// 'line_id' => $line->id,
// 'item_id' => $item->id,
// 'production_order' => $this->data['production_order'] ?? null,
// 'created_at' => $fdateTime->format('Y-m-d H:i:s'),//$this->data['created_at'],
// 'updated_at' => $tdateTime->format('Y-m-d H:i:s'),//$this->data['updated_at'],
// 'operator_id' => $this->data['operator_id'],
// ]);
return null;
// return new ProductionQuantity();
}
@@ -145,21 +272,21 @@ class ProductionQuantityImporter extends Importer
// Relationship resolvers
private function resolveItemId(string $code): int
{
return \App\Models\Item::where('code', $code)->first()->id;
return Item::where('code', $code)->first()->id;
}
private function resolveLineId(string $name): int
{
return \App\Models\Line::where('name', $name)->first()->id;
return Line::where('name', $name)->first()->id;
}
private function resolveShiftId(string $name): int
{
return \App\Models\Shift::where('name', $name)->first()->id;
return Shift::where('name', $name)->first()->id;
}
private function resolvePlantId(string $name): int
{
return \App\Models\Plant::where('name', $name)->first()->id;
return Plant::where('name', $name)->first()->id;
}
}