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:35:57 +05:30
parent 0cb5373312
commit b2044bc851
2 changed files with 62 additions and 54 deletions

View File

@@ -33,13 +33,13 @@ class ProductionQuantityExporter extends Exporter
ExportColumn::make('item.uom') ExportColumn::make('item.uom')
->label('UNIT OF MEASURE'), ->label('UNIT OF MEASURE'),
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('sap_msg_status') ExportColumn::make('sap_msg_status')
->label('SAP MESSAGE STATUS'), ->label('SAP MESSAGE STATUS'),
ExportColumn::make('sap_msg_description') ExportColumn::make('sap_msg_description')
@@ -58,10 +58,10 @@ class ProductionQuantityExporter extends Exporter
public static function getCompletedNotificationBody(Export $export): string public static function getCompletedNotificationBody(Export $export): string
{ {
$body = 'Your production quantity export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.'; $body = 'Your production quantity 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

@@ -42,7 +42,7 @@ class ProductionQuantityImporter extends Importer
->exampleHeader('Item Code') ->exampleHeader('Item Code')
->example(['123456', '123456']) ->example(['123456', '123456'])
->label('Item Code') ->label('Item Code')
->relationship(resolveUsing:'code') ->relationship(resolveUsing: 'code')
->rules(['required']), ->rules(['required']),
ImportColumn::make('serial_number') ImportColumn::make('serial_number')
->requiredMapping() ->requiredMapping()
@@ -55,27 +55,27 @@ class ProductionQuantityImporter 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
->exampleHeader('Block') ->exampleHeader('Block Name')
->example(['Block A', 'Block A']) ->example(['Block A', 'Block A'])
->label('Block') ->label('Block Name')
->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') ->exampleHeader('Shift Name')
->example(['Day', 'Night']) ->example(['Day', 'Night'])
->label('Shift Name') ->label('Shift Name')
->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()
@@ -95,49 +95,57 @@ class ProductionQuantityImporter extends Importer
public function resolveRecord(): ?ProductionQuantity public function resolveRecord(): ?ProductionQuantity
{ {
$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 {
$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; 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';
} }
$item = null; $item = null;
if ($plant) { if ($plant) {
$item = Item::where('code', $this->data['item'])->where('plant_id', $plant->id)->first(); $item = Item::where('code', $this->data['item'])->where('plant_id', $plant->id)->first();
} }
if (!$item) { if (! $item) {
$warnMsg[] = "Item not found"; $warnMsg[] = 'Item not found';
} }
if (Str::length($this->data['serial_number']) < 9 || !ctype_alnum($this->data['serial_number'])) { if (Str::length($this->data['serial_number']) < 9 || ! ctype_alnum($this->data['serial_number'])) {
$warnMsg[] = "Invalid serial number found"; $warnMsg[] = 'Invalid serial number found';
} }
if (Str::length($this->data['production_order']) > 0 && (Str::length($this->data['production_order']) < 7 || Str::length($this->data['production_order']) > 14 || !is_numeric($this->data['production_order']))) { if (Str::length($this->data['production_order']) > 0 && (Str::length($this->data['production_order']) < 7 || Str::length($this->data['production_order']) > 14 || ! is_numeric($this->data['production_order']))) {
$warnMsg[] = "Invalid production order found"; $warnMsg[] = 'Invalid production order 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;
@@ -163,14 +171,14 @@ class ProductionQuantityImporter 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";
} }
@@ -187,21 +195,20 @@ class ProductionQuantityImporter 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))
$productionQuan = ProductionQuantity::where('plant_id', $plant->id) $productionQuan = ProductionQuantity::where('plant_id', $plant->id)
->where('serial_number', $this->data['serial_number']) ->where('serial_number', $this->data['serial_number'])
->latest() ->latest()
->first(); ->first();
if ($productionQuan) { if ($productionQuan) {
throw new RowImportFailedException("Serial number already exist!"); throw new RowImportFailedException('Serial number already exist!');
} }
} }
@@ -212,10 +219,11 @@ class ProductionQuantityImporter extends Importer
'line_id' => $line->id, 'line_id' => $line->id,
'item_id' => $item->id, 'item_id' => $item->id,
'production_order' => $this->data['production_order'] ?? null, 'production_order' => $this->data['production_order'] ?? null,
'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'],
]); ]);
// ProductionQuantity::updateOrCreate([ // ProductionQuantity::updateOrCreate([
// 'serial_number' => $this->data['serial_number'], // 'serial_number' => $this->data['serial_number'],
// 'plant_id' => $plant->id, // 'plant_id' => $plant->id,
@@ -236,10 +244,10 @@ class ProductionQuantityImporter extends Importer
public static function getCompletedNotificationBody(Import $import): string public static function getCompletedNotificationBody(Import $import): string
{ {
$body = 'Your production quantity import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; $body = 'Your production quantity 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;
@@ -259,7 +267,7 @@ class ProductionQuantityImporter extends Importer
'line_id' => $this->resolveLineId($row['line']), 'line_id' => $this->resolveLineId($row['line']),
'shift_id' => $this->resolveShiftId($row['shift']), 'shift_id' => $this->resolveShiftId($row['shift']),
'plant_id' => $this->resolvePlantId($row['plant']), 'plant_id' => $this->resolvePlantId($row['plant']),
'updated_at' => $row['updated_at'] 'updated_at' => $row['updated_at'],
]); ]);
} finally { } finally {
// Always disable flag even if errors occur // Always disable flag even if errors occur