diff --git a/app/Services/Manage/PurchaseService.php b/app/Services/Manage/PurchaseService.php index 494a4f9..f8f5bd7 100644 --- a/app/Services/Manage/PurchaseService.php +++ b/app/Services/Manage/PurchaseService.php @@ -287,11 +287,25 @@ public function createRawMaterialAndDraft(array $validated, User $user): array { $data = $this->runInTransaction( function () use ($validated, $user): array { - $rawMaterial = RawMaterial::create([ - 'name' => $validated['name'], - 'unit' => $validated['unit'], - 'is_active' => true, - ]); + $rawMaterial = RawMaterial::withTrashed() + ->where('name', $validated['name']) + ->where('unit', $validated['unit']) + ->first(); + + if ($rawMaterial) { + if ($rawMaterial->trashed()) { + $rawMaterial->restore(); + } + if (! $rawMaterial->is_active) { + $rawMaterial->update(['is_active' => true]); + } + } else { + $rawMaterial = RawMaterial::create([ + 'name' => $validated['name'], + 'unit' => $validated['unit'], + 'is_active' => true, + ]); + } $price = RawMaterialPrice::create([ 'raw_material_id' => $rawMaterial->id,