Added view rights against plant on import and export and Validation logic updated
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 10s

This commit is contained in:
dhanabalan
2026-01-13 16:33:57 +05:30
parent f592852b77
commit 5a74f9a4ca
2 changed files with 65 additions and 62 deletions

View File

@@ -37,13 +37,13 @@ class ProductionLineStopExporter extends Exporter
ExportColumn::make('stop_min') ExportColumn::make('stop_min')
->label('STOP MINUTE'), ->label('STOP MINUTE'),
ExportColumn::make('line.name') ExportColumn::make('line.name')
->label('LINE'), ->label('LINE NAME'),
ExportColumn::make('shift.block.name') ExportColumn::make('shift.block.name')
->label('BLOCK'), ->label('BLOCK NAME'),
ExportColumn::make('shift.name') ExportColumn::make('shift.name')
->label('SHIFT'), ->label('SHIFT NAME'),
ExportColumn::make('plant.name') ExportColumn::make('plant.code')
->label('PLANT'), ->label('PLANT CODE'),
ExportColumn::make('operator_id') ExportColumn::make('operator_id')
->label('OPERATOR ID'), ->label('OPERATOR ID'),
ExportColumn::make('created_at') ExportColumn::make('created_at')
@@ -58,10 +58,10 @@ class ProductionLineStopExporter extends Exporter
public static function getCompletedNotificationBody(Export $export): string public static function getCompletedNotificationBody(Export $export): string
{ {
$body = 'Your production line stop export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.'; $body = 'Your production line stop export has completed and '.number_format($export->successful_rows).' '.str('row')->plural($export->successful_rows).' exported.';
if ($failedRowsCount = $export->getFailedRowsCount()) { 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; return $body;

View File

@@ -15,6 +15,7 @@ use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer; use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import; use Filament\Actions\Imports\Models\Import;
use Filament\Facades\Filament; use Filament\Facades\Filament;
use Str;
class ProductionLineStopImporter extends Importer class ProductionLineStopImporter extends Importer
{ {
@@ -28,7 +29,7 @@ class ProductionLineStopImporter extends Importer
->exampleHeader('From DateTime') ->exampleHeader('From DateTime')
->example(['01-01-2025 00:00:00', '01-01-2025 20:00:00']) ->example(['01-01-2025 00:00:00', '01-01-2025 20:00:00'])
->label('From DateTime') ->label('From DateTime')
->rules(['required']), //, 'date_format:"d-m-Y H:i:s"' ->rules(['required']), // , 'date_format:"d-m-Y H:i:s"'
ImportColumn::make('to_datetime') ImportColumn::make('to_datetime')
->requiredMapping() ->requiredMapping()
->exampleHeader('To DateTime') ->exampleHeader('To DateTime')
@@ -45,7 +46,7 @@ class ProductionLineStopImporter extends Importer
ImportColumn::make('stop_min') ImportColumn::make('stop_min')
->requiredMapping() ->requiredMapping()
->exampleHeader('Stop Min') ->exampleHeader('Stop Min')
->example(['25','0']) ->example(['25', '0'])
->label('Stop Min') ->label('Stop Min')
->numeric() ->numeric()
->rules(['required', 'integer']), ->rules(['required', 'integer']),
@@ -54,14 +55,14 @@ class ProductionLineStopImporter extends Importer
->exampleHeader('Line Stop Code') ->exampleHeader('Line Stop Code')
->example(['A7R', 'A1R']) ->example(['A7R', 'A1R'])
->label('Line Stop Code') ->label('Line Stop Code')
->relationship(resolveUsing:'code') ->relationship(resolveUsing: 'code')
->rules(['required']), ->rules(['required']),
ImportColumn::make('line') ImportColumn::make('line')
->requiredMapping() ->requiredMapping()
->exampleHeader('Line Name') ->exampleHeader('Line Name')
->example(['4 inch pump line', '4 inch pump line']) ->example(['4 inch pump line', '4 inch pump line'])
->label('Line Name') ->label('Line Name')
->relationship(resolveUsing:'name') ->relationship(resolveUsing: 'name')
->rules(['required']), ->rules(['required']),
ImportColumn::make('block_reference') ImportColumn::make('block_reference')
->requiredMapping() // Or optionalMapping() if not always present ->requiredMapping() // Or optionalMapping() if not always present
@@ -78,10 +79,10 @@ class ProductionLineStopImporter extends Importer
->rules(['required']), ->rules(['required']),
ImportColumn::make('plant') ImportColumn::make('plant')
->requiredMapping() ->requiredMapping()
->exampleHeader('Plant Name') ->exampleHeader('Plant Code')
->example(['Ransar Industries-I', 'Ransar Industries-I']) ->example(['1000', '1000'])
->label('Plant Name') ->label('Plant Code')
->relationship(resolveUsing:'name') ->relationship(resolveUsing: 'code')
->rules(['required']), ->rules(['required']),
ImportColumn::make('operator_id') ImportColumn::make('operator_id')
->requiredMapping() ->requiredMapping()
@@ -95,61 +96,62 @@ class ProductionLineStopImporter extends Importer
public function resolveRecord(): ?ProductionLineStop public function resolveRecord(): ?ProductionLineStop
{ {
$warnMsg = []; $warnMsg = [];
$plant = Plant::where('name', $this->data['plant'])->first(); $plantCod = $this->data['plant'];
$plant = null;
$line = null; $line = null;
$block = null; $block = null;
if (!$plant) { $shift = null;
$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 { } else {
$plant = Plant::where('code', $plantCod)->first();
if (! $plant) {
$warnMsg[] = 'Plant not found';
} else {
$line = Line::where('name', $this->data['line'])->where('plant_id', $plant->id)->first(); $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(); $block = Block::where('name', $this->data['block_reference'])->where('plant_id', $plant->id)->first();
} }
if (!$line) {
$warnMsg[] = "Line not found";
} }
$shift = null; if (! $line) {
if (!$block) { $warnMsg[] = 'Line not found';
$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('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(); // $shift = Shift::where('id', $this->data['shift'])->where('plant_id', $plant->id)->first();
if (!$shift) { if (! $shift) {
$warnMsg[] = "Shift not found"; $warnMsg[] = 'Shift not found';
} }
$linestop = LineStop::where('code', $this->data['linestop'])->first(); $linestop = LineStop::where('code', $this->data['linestop'])->first();
if (!$linestop) { if (! $linestop) {
$warnMsg[] = "Line stop code not found"; $warnMsg[] = 'Line stop code not found';
} }
$stophour = is_numeric($this->data['stop_hour']) && $this->data['stop_hour'] >= 0; $stophour = is_numeric($this->data['stop_hour']) && $this->data['stop_hour'] >= 0;
$stopmin = is_numeric($this->data['stop_min']) && $this->data['stop_min'] >= 0 && $this->data['stop_min'] <= 60; $stopmin = is_numeric($this->data['stop_min']) && $this->data['stop_min'] >= 0 && $this->data['stop_min'] <= 60;
if (!$stophour && !$stopmin) { if (! $stophour && ! $stopmin) {
$warnMsg[] = "Invalid stop hour/minute found"; $warnMsg[] = 'Invalid stop hour/minute found';
} } elseif (! $stophour) {
else if (!$stophour) { $warnMsg[] = 'Invalid stop hour found';
$warnMsg[] = "Invalid stop hour found"; } elseif (! $stopmin) {
} $warnMsg[] = 'Invalid stop min found';
else if (!$stopmin) { } else {
$warnMsg[] = "Invalid stop min found"; $stophour = (int) $this->data['stop_hour'];
} $stopmin = (int) $this->data['stop_min'];
else {
$stophour = (int)$this->data['stop_hour'];
$stopmin = (int)$this->data['stop_min'];
if ($stophour == 0 && $stopmin == 0) { if ($stophour == 0 && $stopmin == 0) {
$warnMsg[] = "Invalid stop hour/minute found"; $warnMsg[] = 'Invalid stop hour/minute found';
} }
} }
$validHourMin = ($stophour == 0 && $stopmin == 0) ? false : true; $validHourMin = ($stophour == 0 && $stopmin == 0) ? false : true;
if (!$validHourMin) { if (! $validHourMin) {
$warnMsg[] = "Invalid stop hour/minute found"; $warnMsg[] = 'Invalid stop hour/minute found';
} }
$fromDate = $this->data['from_datetime']; $fromDate = $this->data['from_datetime'];
$toDate = $this->data['to_datetime']; $toDate = $this->data['to_datetime'];
$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; $fdateTime = null;
$tdateTime = null; $tdateTime = null;
@@ -175,14 +177,14 @@ class ProductionLineStopImporter extends Importer
} }
// $fDateOnly = ''; // $fDateOnly = '';
if (!isset($fdateTime)) { if (! isset($fdateTime)) {
// throw new \Exception('Invalid date time format'); // throw new \Exception('Invalid date time format');
$warnMsg[] = "Invalid 'From DateTime' format. Expected DD-MM-YYYY HH:MM:SS"; $warnMsg[] = "Invalid 'From DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
} }
// else { // else {
// $fDateOnly = $fdateTime->toDateString(); // $fDateOnly = $fdateTime->toDateString();
// } // }
if (!isset($tdateTime)) { if (! isset($tdateTime)) {
$warnMsg[] = "Invalid 'To DateTime' format. Expected DD-MM-YYYY HH:MM:SS"; $warnMsg[] = "Invalid 'To DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
} }
@@ -200,11 +202,11 @@ class ProductionLineStopImporter extends Importer
// } // }
$user = User::where('name', $this->data['operator_id'])->first(); $user = User::where('name', $this->data['operator_id'])->first();
if (!$user) { if (! $user) {
$warnMsg[] = "Operator ID not found"; $warnMsg[] = 'Operator ID not found';
} }
if (!empty($warnMsg)) { if (! empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg)); throw new RowImportFailedException(implode(', ', $warnMsg));
} }
@@ -213,12 +215,13 @@ class ProductionLineStopImporter extends Importer
'line_id' => $line->id, 'line_id' => $line->id,
'shift_id' => $shift->id, 'shift_id' => $shift->id,
'linestop_id' => $linestop->id, 'linestop_id' => $linestop->id,
'from_datetime' => $fdateTime->format('Y-m-d H:i:s'),//$this->data['from_datetime'], 'from_datetime' => $fdateTime->format('Y-m-d H:i:s'), // $this->data['from_datetime'],
'to_datetime' => $tdateTime->format('Y-m-d H:i:s'),//$this->data['to_datetime'], 'to_datetime' => $tdateTime->format('Y-m-d H:i:s'), // $this->data['to_datetime'],
'stop_hour' => (int)$this->data['stop_hour'], 'stop_hour' => (int) $this->data['stop_hour'],
'stop_min' => (int)$this->data['stop_min'], 'stop_min' => (int) $this->data['stop_min'],
'operator_id' => $this->data['operator_id'], 'operator_id' => $this->data['operator_id'],
]); ]);
return null; return null;
// return ProductionLineStop::firstOrNew([ // return ProductionLineStop::firstOrNew([
// // Update existing records, matching them by `$this->data['column_name']` // // Update existing records, matching them by `$this->data['column_name']`
@@ -230,10 +233,10 @@ class ProductionLineStopImporter extends Importer
public static function getCompletedNotificationBody(Import $import): string public static function getCompletedNotificationBody(Import $import): string
{ {
$body = 'Your production line stop import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; $body = 'Your production line stop import has completed and '.number_format($import->successful_rows).' '.str('row')->plural($import->successful_rows).' imported.';
if ($failedRowsCount = $import->getFailedRowsCount()) { 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; return $body;