From 0cb5373312445e8720dc9ab154daf89d76f05dad Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Tue, 13 Jan 2026 16:34:49 +0530 Subject: [PATCH] Added view rights against plant on import and export and Validation logic updated --- .../Exports/ProductionPlanExporter.php | 14 +-- .../Imports/ProductionPlanImporter.php | 97 ++++++++++--------- 2 files changed, 60 insertions(+), 51 deletions(-) diff --git a/app/Filament/Exports/ProductionPlanExporter.php b/app/Filament/Exports/ProductionPlanExporter.php index 2f9437f..2b79f07 100644 --- a/app/Filament/Exports/ProductionPlanExporter.php +++ b/app/Filament/Exports/ProductionPlanExporter.php @@ -29,13 +29,13 @@ class ProductionPlanExporter extends Exporter ExportColumn::make('production_quantity') ->label('PRODUCTION QUANTITY'), ExportColumn::make('line.name') - ->label('LINE'), + ->label('LINE NAME'), ExportColumn::make('shift.block.name') - ->label('BLOCK'), + ->label('BLOCK NAME'), ExportColumn::make('shift.name') - ->label('SHIFT'), - ExportColumn::make('plant.name') - ->label('PLANT'), + ->label('SHIFT NAME'), + ExportColumn::make('plant.code') + ->label('PLANT CODE'), ExportColumn::make('operator_id') ->label('OPERATOR ID'), ExportColumn::make('created_at') @@ -50,10 +50,10 @@ class ProductionPlanExporter extends Exporter public static function getCompletedNotificationBody(Export $export): string { - $body = 'Your production plan export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.'; + $body = 'Your production plan 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.'; + $body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to export.'; } return $body; diff --git a/app/Filament/Imports/ProductionPlanImporter.php b/app/Filament/Imports/ProductionPlanImporter.php index a71fe7b..54d27c9 100644 --- a/app/Filament/Imports/ProductionPlanImporter.php +++ b/app/Filament/Imports/ProductionPlanImporter.php @@ -48,7 +48,7 @@ class ProductionPlanImporter extends Importer ->exampleHeader('Line Name') ->example(['4 inch pump line', '4 inch pump line']) ->label('Line Name') - ->relationship(resolveUsing:'name') + ->relationship(resolveUsing: 'name') ->rules(['required']), ImportColumn::make('block_reference') ->requiredMapping() // Or optionalMapping() if not always present @@ -58,17 +58,17 @@ class ProductionPlanImporter extends Importer ->rules(['required']), // Or remove if not required ImportColumn::make('shift') ->requiredMapping() - ->exampleHeader('Shift Name') //ID - ->example(['Day', 'Night']) //'2', '7' + ->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') + ->exampleHeader('Plant Code') + ->example(['1000', '1000']) + ->label('Plant Code') + ->relationship(resolveUsing: 'code') ->rules(['required']), ImportColumn::make('updated_at') ->requiredMapping() @@ -88,42 +88,51 @@ class ProductionPlanImporter extends Importer public function resolveRecord(): ?ProductionPlan { $warnMsg = []; - $plant = Plant::where('name', $this->data['plant'])->first(); + $plantCod = $this->data['plant']; + $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_reference $block = Block::where('name', $this->data['block_reference'])->where('plant_id', $plant->id)->first(); } - if (!$line) { - $warnMsg[] = "Line not found"; + + if (! $line) { + $warnMsg[] = 'Line not found'; } + $shift = null; - if (!$block) { - $warnMsg[] = "Block not found"; - } - else { + if (! $block) { + $warnMsg[] = 'Block not found'; + } elseif ($plant) { $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"; + + // $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['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"; + 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' + $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; @@ -149,14 +158,13 @@ class ProductionPlanImporter extends Importer } $fDateOnly = ''; - if (!isset($fdateTime)) { + if (! isset($fdateTime)) { // throw new \Exception('Invalid date time format'); $warnMsg[] = "Invalid 'Created DateTime' format. Expected DD-MM-YYYY HH:MM:SS"; - } - else { + } else { $fDateOnly = $fdateTime->toDateString(); } - if (!isset($tdateTime)) { + if (! isset($tdateTime)) { $warnMsg[] = "Invalid 'Updated DateTime' format. Expected DD-MM-YYYY HH:MM:SS"; } @@ -174,21 +182,20 @@ class ProductionPlanImporter extends Importer // } $user = User::where('name', $this->data['operator_id'])->first(); - if (!$user) { - $warnMsg[] = "Operator ID not found"; + if (! $user) { + $warnMsg[] = 'Operator ID not found'; } - if (!empty($warnMsg)) { + if (! empty($warnMsg)) { throw new RowImportFailedException(implode(', ', $warnMsg)); - } - else { //if (empty($warnMsg)) + } else { // if (empty($warnMsg)) $productionPlan = ProductionPlan::where('plant_id', $plant->id) - ->where('shift_id', $shift->id) - ->where('line_id', $line->id) - ->whereDate('created_at', $fDateOnly) + ->where('shift_id', $shift->id) + ->where('line_id', $line->id) + ->whereDate('created_at', $fDateOnly) // ->where('plan_quantity', $productionQuantity->plan_quantity) - ->latest() - ->first(); + ->latest() + ->first(); if ($productionPlan) { // if($productionPlan->production_quantity) @@ -205,6 +212,7 @@ class ProductionPlanImporter extends Importer 'operator_id' => $this->data['operator_id'], ]); $productionPlan->save(); + return null; } } @@ -215,10 +223,11 @@ class ProductionPlanImporter extends Importer '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'], + '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 ProductionPlan::firstOrNew([ // // Update existing records, matching them by `$this->data['column_name']` @@ -230,10 +239,10 @@ class ProductionPlanImporter extends Importer 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;