diff --git a/app/Filament/Imports/ProductionPlanImporter.php b/app/Filament/Imports/ProductionPlanImporter.php index a71fe7b..10e6b06 100644 --- a/app/Filament/Imports/ProductionPlanImporter.php +++ b/app/Filament/Imports/ProductionPlanImporter.php @@ -3,6 +3,7 @@ namespace App\Filament\Imports; use App\Models\Block; +use App\Models\Item; use App\Models\Line; use App\Models\Plant; use App\Models\ProductionPlan; @@ -23,11 +24,33 @@ class ProductionPlanImporter extends Importer public static function getColumns(): array { return [ - ImportColumn::make('created_at') + // ImportColumn::make('created_at') + // ->requiredMapping() + // ->exampleHeader('Created DateTime') + // ->example(['01-01-2025 08:00:00', '01-01-2025 19:30:00']) + // ->label('Created DateTime') + // ->rules(['required']), + + ImportColumn::make('plant') ->requiredMapping() - ->exampleHeader('Created DateTime') - ->example(['01-01-2025 08:00:00', '01-01-2025 19:30:00']) - ->label('Created DateTime') + ->exampleHeader('Plant Code') + ->example(['1000', '1000']) + ->label('Plant Code') + ->relationship(resolveUsing: 'code') + ->rules(['required']), + ImportColumn::make('line') + ->requiredMapping() + ->exampleHeader('Line Name') + ->example(['4 inch pump line', '4 inch pump line']) + ->label('Line Name') + ->relationship(resolveUsing: 'name') + ->rules(['required']), + ImportColumn::make('item') + ->requiredMapping() + ->exampleHeader('Item Code') + ->example(['123456', '210987']) + ->label('Item Code') + ->relationship(resolveUsing: 'code') ->rules(['required']), ImportColumn::make('plan_quantity') ->requiredMapping() @@ -36,175 +59,111 @@ class ProductionPlanImporter extends Importer ->label('Plan Quantity') ->numeric() ->rules(['required', 'integer']), - ImportColumn::make('production_quantity') - ->requiredMapping() - ->exampleHeader('Production Quantity') - ->example(['0', '0']) - ->label('Production Quantity') - ->numeric() - ->rules(['required', 'integer']), - ImportColumn::make('line') - ->requiredMapping() - ->exampleHeader('Line Name') - ->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') //ID - ->example(['Day', 'Night']) //'2', '7' - ->label('Shift Name') // ID - ->relationship(resolveUsing: 'name') - ->rules(['required']), - ImportColumn::make('plant') - ->requiredMapping() - ->exampleHeader('Plant Name') - ->example(['Ransar Industries-I', 'Ransar Industries-I']) - ->label('Plant Name') - ->relationship(resolveUsing:'name') - ->rules(['required']), - ImportColumn::make('updated_at') - ->requiredMapping() - ->exampleHeader('Updated DateTime') - ->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([Filament::auth()->user()->name, Filament::auth()->user()->name]) - ->label('Operator ID') - ->rules(['required']), + // ImportColumn::make('production_quantity') + // ->requiredMapping() + // ->exampleHeader('Production Quantity') + // ->example(['0', '0']) + // ->label('Production Quantity') + // ->numeric() + // ->rules(['required', 'integer']), + + // 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') // ID + // ->example(['Day', 'Night']) // '2', '7' + // ->label('Shift Name') // ID + // ->relationship(resolveUsing: 'name') + // ->rules(['required']), + + // ImportColumn::make('updated_at') + // ->requiredMapping() + // ->exampleHeader('Updated DateTime') + // ->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([Filament::auth()->user()->name, Filament::auth()->user()->name]) + // ->label('Operator ID') + // ->rules(['required']), ]; } public function resolveRecord(): ?ProductionPlan { $warnMsg = []; - $plant = Plant::where('name', $this->data['plant'])->first(); + $plantCod = $this->data['plant']; + $itemCod = $this->data['item']; + $plant = null; $line = null; $block = null; - if (!$plant) { - $warnMsg[] = "Plant not found"; + + 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(); } - else { + + if (! $plant) { + $warnMsg[] = 'Plant not found'; + } 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"; - } - if (Str::length($this->data['plan_quantity']) < 0 || !is_numeric($this->data['plan_quantity']) || $this->data['plan_quantity'] <= 0) { - $warnMsg[] = "Invalid plan quantity found"; - } - if (Str::length($this->data['production_quantity']) < 0 || !is_numeric($this->data['production_quantity']) || $this->data['production_quantity'] < 0) { - $warnMsg[] = "Invalid production quantity 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"; - } + if (! $line) { + $warnMsg[] = 'Line not found'; } - 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"; - } + if (Str::length($itemCod) < 6 || ! is_numeric($itemCod)) { + $warnMsg[] = 'Invalid item code found'; + } else { + $item = Item::where('code', $itemCod)->first(); } - $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 (! $item) { + $warnMsg[] = 'Item not found'; } - if (isset($fdateTime) && isset($tdateTime)) { - if ($fdateTime->greaterThan($tdateTime)) { - $warnMsg[] = "'Created DataTime' is greater than 'Updated DateTime'."; - } + $plantId = $plant->id; + + $itemAgaPlant = Item::where('plant_id', $plantId)->where('code', $itemCod)->first(); + + if(!$itemAgaPlant){ + $warnMsg[] = 'Item not found against plant code'; } - // 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 = Filament::auth()->user(); - $user = User::where('name', $this->data['operator_id'])->first(); - if (!$user) { - $warnMsg[] = "Operator ID not found"; + $operatorName = $user->name; + + if (Str::length($this->data['plan_quantity']) < 0 || ! is_numeric($this->data['plan_quantity']) || $this->data['plan_quantity'] <= 0) { + $warnMsg[] = 'Invalid plan quantity found'; } - if (!empty($warnMsg)) { + if (! empty($warnMsg)) { throw new RowImportFailedException(implode(', ', $warnMsg)); - } - else { //if (empty($warnMsg)) + } else { $productionPlan = ProductionPlan::where('plant_id', $plant->id) - ->where('shift_id', $shift->id) - ->where('line_id', $line->id) - ->whereDate('created_at', $fDateOnly) - // ->where('plan_quantity', $productionQuantity->plan_quantity) - ->latest() - ->first(); + ->where('line_id', $line->id) + ->where('item_id', $itemAgaPlant->id) + ->latest() + ->first(); if ($productionPlan) { - // if($productionPlan->production_quantity) - // { - // throw new RowImportFailedException("{$productionPlan->created_at}, {$productionPlan->production_quantity}"); - // } - // $warnMsg[] = "Production plan already exist on '{$fDateOnly}'!"; $productionPlan->update([ 'plan_quantity' => $this->data['plan_quantity'], - // 'production_quantity' => $productionPlan->production_quantity, - // 'created_at' => $productionPlan->created_at,//$fdateTime->format('Y-m-d H:i:s'), - // 'updated_at' => $tdateTime->format('Y-m-d H:i:s'), - 'operator_id' => $this->data['operator_id'], + 'operator_id' => $operatorName, ]); $productionPlan->save(); + return null; } } @@ -212,28 +171,23 @@ class ProductionPlanImporter extends Importer ProductionPlan::updateOrCreate([ 'plant_id' => $plant->id, 'line_id' => $line->id, - 'shift_id' => $shift->id, + 'item_id' => $itemAgaPlant->id, + // 'shift_id' => $shift->id, 'plan_quantity' => $this->data['plan_quantity'], - 'production_quantity' => $this->data['production_quantity'], - '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'], + 'created_at' =>now(), + 'updated_at' => now(), + 'operator_id' => $operatorName, ]); - return null; - // return ProductionPlan::firstOrNew([ - // // Update existing records, matching them by `$this->data['column_name']` - // 'email' => $this->data['email'], - // ]); - // return new ProductionPlan(); + return null; } public static function getCompletedNotificationBody(Import $import): string { - $body = 'Your production plan import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; + $body = 'Your production plan 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.'; + $body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to import.'; } return $body;