customerRepository->findBy('email', $data['email'] ?? null)) { abort(422, 'Email already exists on the system!'); } return DB::transaction(function () use ($data) { $note = $data['note'] ?? null; unset($data['note']); $data['password'] = Hash::make($data['password']); $data['email_marketing'] = (bool) ($data['email_marketing'] ?? true); $customer = $this->customerRepository->create($data); if ($note !== null) { $customer->update(['note' => $note]); } return $customer->fresh(); }); } public function update(string $uuid, array $data): Customer { $customer = $this->resolveCustomer($uuid); return DB::transaction(function () use ($customer, $data) { $note = $data['note'] ?? null; unset($data['note'], $data['password']); if (!empty($data['password_raw'])) { $data['password'] = Hash::make($data['password_raw']); unset($data['password_raw']); } $this->customerRepository->update($customer->id, $data); if ($note !== null) { $customer->update(['note' => $note]); } return $customer->fresh(); }); } public function destroy(string $uuid): void { $customer = $this->resolveCustomer($uuid); DB::transaction(function () use ($customer) { $this->deleteRelatedRecords($customer->id); $customer->delete(); }); } public function restore(string $uuid): void { $customer = $this->customerRepository->findBy('uuid', $uuid) ?? $this->customerRepository->onlyTrashed()->where('uuid', $uuid)->first(); abort_if(!$customer, 404, "Can't find customer by UUID #{$uuid}"); DB::transaction(function () use ($customer) { $customer->restore(); $this->restoreRelatedRecords($customer->id); }); } public function forceDelete(string $uuid): void { $customer = $this->customerRepository->onlyTrashed()->where('uuid', $uuid)->first(); abort_if(!$customer, 404, "Can't find customer by UUID #{$uuid}"); DB::transaction(function () use ($customer) { $this->forceDeleteRelatedRecords($customer->id); $customer->forceDelete(); }); } public function updateStatus(string $uuid, int $status): void { $customer = $this->resolveCustomer($uuid); $customer->update(['status' => $status]); } public function bulkDelete(array $uuids): int { $customers = Customer::whereIn('uuid', $uuids)->get(); DB::transaction(function () use ($customers) { foreach ($customers as $customer) { $this->deleteRelatedRecords($customer->id); $customer->delete(); } }); return $customers->count(); } public function bulkRestore(array $uuids): int { $customers = Customer::onlyTrashed()->whereIn('uuid', $uuids)->get(); DB::transaction(function () use ($customers) { foreach ($customers as $customer) { $customer->restore(); $this->restoreRelatedRecords($customer->id); } }); return $customers->count(); } public function bulkForceDelete(array $uuids): int { $customers = Customer::onlyTrashed()->whereIn('uuid', $uuids)->get(); DB::transaction(function () use ($customers) { foreach ($customers as $customer) { $this->forceDeleteRelatedRecords($customer->id); $customer->forceDelete(); } }); return $customers->count(); } public function bulkStatus(array $uuids, int $status): int { return Customer::whereIn('uuid', $uuids)->update(['status' => $status]); } private function deleteRelatedRecords(int $customerId): void { Invoice::where('customer_id', $customerId)->delete(); Order::where('customer_id', $customerId)->delete(); Transaction::where('customer_id', $customerId)->delete(); } private function restoreRelatedRecords(int $customerId): void { Invoice::withTrashed()->where('customer_id', $customerId)->restore(); Order::withTrashed()->where('customer_id', $customerId)->restore(); Transaction::withTrashed()->where('customer_id', $customerId)->restore(); } private function forceDeleteRelatedRecords(int $customerId): void { Invoice::withTrashed()->where('customer_id', $customerId)->forceDelete(); Order::withTrashed()->where('customer_id', $customerId)->forceDelete(); Transaction::withTrashed()->where('customer_id', $customerId)->forceDelete(); } }