From 07b0ce2d9023586491240c09f6c3f50fa3052497 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Mon, 16 Nov 2026 09:49:22 +0530 Subject: [PATCH] Changed importer production quantities importer logic --- .../Imports/ProductionQuantityImporter.php | 83 ++++++++++--------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/app/Filament/Imports/ProductionQuantityImporter.php b/app/Filament/Imports/ProductionQuantityImporter.php index f15f4f20..d568a248 100644 --- a/app/Filament/Imports/ProductionQuantityImporter.php +++ b/app/Filament/Imports/ProductionQuantityImporter.php @@ -83,12 +83,12 @@ class ProductionQuantityImporter extends Importer ->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('operator_id') + // ->requiredMapping() + // ->exampleHeader('Operator ID') + // ->example([Filament::auth()->user()->name, Filament::auth()->user()->name]) + // ->label('Operator ID') + // ->rules(['required']), ]; } @@ -133,59 +133,64 @@ class ProductionQuantityImporter extends Importer $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:s A', // 13-11-26 01:31:26 PM + 'd-m-y h:i A', // 13-11-26 01:31 PM + ]; $fdateTime = null; $tdateTime = null; - // Try parsing with multiple formats + + // Parse Created DateTime foreach ($formats as $format) { try { - $fdateTime = Carbon::createFromFormat($format, $fromDate); - break; + $fdateTime = Carbon::createFromFormat('!' . $format, trim($fromDate)); + + if ($fdateTime !== false) { + break; + } } catch (\Exception $e) { - // Optionally collect warning messages - // $warnMsg[] = "Date format mismatch with format: $format"; + $fdateTime = null; } } + // Parse Updated DateTime foreach ($formats as $format) { try { - $tdateTime = Carbon::createFromFormat($format, $toDate); - break; + $tdateTime = Carbon::createFromFormat('!' . $format, trim($toDate)); + + if ($tdateTime !== false) { + break; + } } catch (\Exception $e) { - // Optionally collect warning messages - // $warnMsg[] = "Date format mismatch with format: $format"; + $tdateTime = null; } } - // $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"; + // Validate Created DateTime + if (! $fdateTime) { + $warnMsg[] = "Invalid 'Created DateTime' format. Expected DD-MM-YY HH:MM:SS AM/PM"; } - if (isset($fdateTime) && isset($tdateTime)) { + // Validate Updated DateTime + if (! $tdateTime) { + $warnMsg[] = "Invalid 'Updated DateTime' format. Expected DD-MM-YY HH:MM:SS AM/PM"; + } + + // Compare Created and Updated DateTime + if ($fdateTime && $tdateTime) { + if ($fdateTime->greaterThan($tdateTime)) { - $warnMsg[] = "'Created DataTime' is greater than 'Updated DateTime'."; + $warnMsg[] = "'Created DateTime' is greater than 'Updated DateTime'."; } } - // 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 = User::where('name', $this->data['operator_id'])->first(); + // if (! $user) { + // $warnMsg[] = 'Operator ID not found'; // } - $user = User::where('name', $this->data['operator_id'])->first(); - if (! $user) { - $warnMsg[] = 'Operator ID not found'; - } + $userName = Filament::auth()->user()->name; $shift = null; @@ -225,8 +230,6 @@ class ProductionQuantityImporter extends Importer } } - - ProductionQuantity::updateOrCreate([ 'serial_number' => $this->data['serial_number'], 'plant_id' => $plant->id, @@ -236,7 +239,7 @@ class ProductionQuantityImporter extends Importer 'production_order' => $this->data['production_order'] ?? null, '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'], + 'operator_id' => $userName, ]); // ProductionQuantity::updateOrCreate([