1
0
forked from poc/pds

Added update or create record on import

This commit is contained in:
dhanabalan
2025-04-25 12:56:08 +05:30
parent 58a58f3d9b
commit 81640fda90
2 changed files with 46 additions and 2 deletions

View File

@@ -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

View File

@@ -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