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:34:49 +05:30
parent 5a74f9a4ca
commit 0cb5373312
2 changed files with 60 additions and 51 deletions

View File

@@ -29,13 +29,13 @@ class ProductionPlanExporter extends Exporter
ExportColumn::make('production_quantity') ExportColumn::make('production_quantity')
->label('PRODUCTION QUANTITY'), ->label('PRODUCTION QUANTITY'),
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')
@@ -50,10 +50,10 @@ class ProductionPlanExporter extends Exporter
public static function getCompletedNotificationBody(Export $export): string 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()) { 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

@@ -48,7 +48,7 @@ class ProductionPlanImporter extends Importer
->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
@@ -58,17 +58,17 @@ class ProductionPlanImporter extends Importer
->rules(['required']), // Or remove if not required ->rules(['required']), // Or remove if not required
ImportColumn::make('shift') ImportColumn::make('shift')
->requiredMapping() ->requiredMapping()
->exampleHeader('Shift Name') //ID ->exampleHeader('Shift Name') // ID
->example(['Day', 'Night']) //'2', '7' ->example(['Day', 'Night']) // '2', '7'
->label('Shift Name') // ID ->label('Shift Name') // ID
->relationship(resolveUsing: 'name') ->relationship(resolveUsing: 'name')
->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('updated_at') ImportColumn::make('updated_at')
->requiredMapping() ->requiredMapping()
@@ -88,42 +88,51 @@ class ProductionPlanImporter extends Importer
public function resolveRecord(): ?ProductionPlan public function resolveRecord(): ?ProductionPlan
{ {
$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) {
$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(); $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"; if (! $line) {
$warnMsg[] = 'Line not found';
} }
$shift = null; $shift = null;
if (!$block) { if (! $block) {
$warnMsg[] = "Block not found"; $warnMsg[] = 'Block not found';
} } elseif ($plant) {
else {
$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();
if (!$shift) { // $shift = Shift::where('id', $this->data['shift'])->where('plant_id', $plant->id)->first();
$warnMsg[] = "Shift not found"; 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) { if (Str::length($this->data['plan_quantity']) < 0 || ! is_numeric($this->data['plan_quantity']) || $this->data['plan_quantity'] <= 0) {
$warnMsg[] = "Invalid plan quantity found"; $warnMsg[] = 'Invalid plan quantity found';
} }
if (Str::length($this->data['production_quantity']) < 0 || !is_numeric($this->data['production_quantity']) || $this->data['production_quantity'] < 0) { if (Str::length($this->data['production_quantity']) < 0 || ! is_numeric($this->data['production_quantity']) || $this->data['production_quantity'] < 0) {
$warnMsg[] = "Invalid production quantity found"; $warnMsg[] = 'Invalid production quantity found';
} }
$fromDate = $this->data['created_at']; $fromDate = $this->data['created_at'];
$toDate = $this->data['updated_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; $fdateTime = null;
$tdateTime = null; $tdateTime = null;
@@ -149,14 +158,13 @@ class ProductionPlanImporter 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 'Created DateTime' format. Expected DD-MM-YYYY HH:MM:SS"; $warnMsg[] = "Invalid 'Created 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 'Updated DateTime' format. Expected DD-MM-YYYY HH:MM:SS"; $warnMsg[] = "Invalid 'Updated DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
} }
@@ -174,14 +182,13 @@ class ProductionPlanImporter 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));
} } else { // if (empty($warnMsg))
else { //if (empty($warnMsg))
$productionPlan = ProductionPlan::where('plant_id', $plant->id) $productionPlan = ProductionPlan::where('plant_id', $plant->id)
->where('shift_id', $shift->id) ->where('shift_id', $shift->id)
->where('line_id', $line->id) ->where('line_id', $line->id)
@@ -205,6 +212,7 @@ class ProductionPlanImporter extends Importer
'operator_id' => $this->data['operator_id'], 'operator_id' => $this->data['operator_id'],
]); ]);
$productionPlan->save(); $productionPlan->save();
return null; return null;
} }
} }
@@ -215,10 +223,11 @@ class ProductionPlanImporter extends Importer
'shift_id' => $shift->id, 'shift_id' => $shift->id,
'plan_quantity' => $this->data['plan_quantity'], 'plan_quantity' => $this->data['plan_quantity'],
'production_quantity' => $this->data['production_quantity'], 'production_quantity' => $this->data['production_quantity'],
'created_at' => $fdateTime->format('Y-m-d H:i:s'),//$this->data['created_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'], 'updated_at' => $tdateTime->format('Y-m-d H:i:s'), // $this->data['updated_at'],
'operator_id' => $this->data['operator_id'], 'operator_id' => $this->data['operator_id'],
]); ]);
return null; return null;
// return ProductionPlan::firstOrNew([ // return ProductionPlan::firstOrNew([
// // Update existing records, matching them by `$this->data['column_name']` // // 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 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()) { 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;