diff --git a/app/Filament/Imports/PlantImporter.php b/app/Filament/Imports/PlantImporter.php index 504e44d4d..189cff44e 100644 --- a/app/Filament/Imports/PlantImporter.php +++ b/app/Filament/Imports/PlantImporter.php @@ -45,12 +45,22 @@ class PlantImporter extends Importer public function resolveRecord(): ?Plant { + $company = \App\Models\Company::where('name', $this->data['company'])->first(); + return Plant::updateOrCreate([ + 'code' => $this->data['code'], + 'name' => $this->data['name'], + ], + [ + 'address' => $this->data['address'], + 'company_id' => $company->id + ] + ); // return Plant::firstOrNew([ // // Update existing records, matching them by `$this->data['column_name']` // 'email' => $this->data['email'], // ]); - return new Plant(); + // return new Plant(); } public static function getCompletedNotificationBody(Import $import): string diff --git a/app/Filament/Imports/ShiftImporter.php b/app/Filament/Imports/ShiftImporter.php index bb34f9ceb..2a0260b72 100644 --- a/app/Filament/Imports/ShiftImporter.php +++ b/app/Filament/Imports/ShiftImporter.php @@ -64,12 +64,46 @@ class ShiftImporter extends Importer public function resolveRecord(): ?Shift { + $plant = \App\Models\Plant::where('name', $this->data['plant'])->first(); + $block = \App\Models\Block::where('name', $this->data['block'])->where('plant_id', $plant->id)->first(); + + if (!$plant || !$block) { + // Optionally handle missing plant/block + return null; + } + + $shift = Shift::where('name', $this->data['name']) + ->where('plant_id', $plant->id) + ->where('block_id', $block->id) + ->first(); + + if ($shift) { + // Update only if necessary to avoid duplicate key error + $shift->update([ + 'start_time' => $this->data['start_time'], + 'duration' => $this->data['duration'], + 'end_time' => $this->data['end_time'], + 'status' => $this->data['status'] + ]); + return $shift; + } else { + // Safe to create new + return Shift::create([ + 'name' => $this->data['name'], + 'plant_id' => $plant->id, + 'block_id' => $block->id, + 'start_time' => $this->data['start_time'], + 'duration' => $this->data['duration'], + 'end_time' => $this->data['end_time'], + 'status' => $this->data['status'] + ]); + } // return Shift::firstOrNew([ // // Update existing records, matching them by `$this->data['column_name']` // 'email' => $this->data['email'], // ]); - return new Shift(); + // return new Shift(); } public static function getCompletedNotificationBody(Import $import): string