| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 | 
							- <?php
 - 
 - namespace App\Traits;
 - 
 - use Illuminate\Database\Eloquent\Model;
 - use Illuminate\Support\Facades\Auth;
 - 
 - trait HandlesRecordCreation
 - {
 -     abstract protected function getRelatedEntities(): array;
 -     abstract protected function getFormModel(): string;
 - 
 -     protected function handleRecordCreation(array $data): Model
 -     {
 -         $relatedEntities = $this->getRelatedEntities();
 - 
 -         $model = $this->getFormModel();
 - 
 -         $existingRecord = $model::where('company_id', Auth::user()->currentCompany->id)
 -             ->latest()
 -             ->first();
 - 
 -         $newData = [
 -             'company_id' => Auth::user()->currentCompany->id,
 -             'updated_by' => Auth::id(),
 -         ];
 - 
 -         foreach ($relatedEntities as $field => $params) {
 -             [$class, $key, $type] = array_pad($params, 3, null);
 - 
 -             if (isset($data[$field]) && $data[$field] !== $existingRecord->{$field}) {
 -                 $this->updateEnabledRecord($class, $key, $existingRecord->{$field}, $type, false);
 -                 $this->updateEnabledRecord($class, $key, $data[$field], $type, true);
 - 
 -                 $newData[$field] = $data[$field];
 -             } else {
 -                 $newData[$field] = $existingRecord->{$field};
 -             }
 -         }
 - 
 -         return $model::create($newData);
 -     }
 - 
 -     protected function updateEnabledRecord($class, $key, $value, $type = null, $enabled = true): void
 -     {
 -         $query = $class::where('company_id', Auth::user()->currentCompany->id)
 -             ->where('enabled', !$enabled);
 - 
 -         if ($type !== null) {
 -             $query = $query->where('type', $type);
 -         }
 - 
 -         $query->where($key, $value)
 -             ->update([
 -                 'enabled' => $enabled,
 -                 'updated_by' => Auth::id(),
 -             ]);
 -     }
 - }
 
 
  |