|
|
@@ -0,0 +1,200 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services;
|
|
|
+
|
|
|
+use App\Models\CustomSchedule;
|
|
|
+use App\Models\Schedule;
|
|
|
+use App\Models\CustomScheduleSpeciality;
|
|
|
+use Carbon\Carbon;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
+
|
|
|
+class CustomScheduleService
|
|
|
+{
|
|
|
+ public function getAll()
|
|
|
+ {
|
|
|
+ return CustomSchedule::with([
|
|
|
+ 'schedule.client.user',
|
|
|
+ 'schedule.address',
|
|
|
+ 'serviceType',
|
|
|
+ 'specialities.speciality'
|
|
|
+ ])
|
|
|
+ ->orderBy('id', 'desc')
|
|
|
+ ->get();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getById($id)
|
|
|
+ {
|
|
|
+ return CustomSchedule::with([
|
|
|
+ 'schedule.client.user',
|
|
|
+ 'schedule.address',
|
|
|
+ 'serviceType',
|
|
|
+ 'specialities.speciality'
|
|
|
+ ])->findOrFail($id);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function create(array $data)
|
|
|
+ {
|
|
|
+ DB::beginTransaction();
|
|
|
+ try {
|
|
|
+ $quantity = $data['quantity'] ?? 1;
|
|
|
+ $specialityIds = $data['speciality_ids'] ?? [];
|
|
|
+
|
|
|
+ $createdCustomSchedules = [];
|
|
|
+
|
|
|
+ for ($i = 0; $i < $quantity; $i++) {
|
|
|
+ $scheduleData = [
|
|
|
+ 'client_id' => $data['client_id'],
|
|
|
+ 'provider_id' => null,
|
|
|
+ 'address_id' => $data['address_id'],
|
|
|
+ 'date' => $data['date'],
|
|
|
+ 'period_type' => $data['period_type'],
|
|
|
+ 'schedule_type' => 'custom',
|
|
|
+ 'start_time' => $data['start_time'],
|
|
|
+ 'end_time' => $data['end_time'],
|
|
|
+ 'status' => 'pending',
|
|
|
+ 'total_amount' => 0,
|
|
|
+ 'code' => str_pad(random_int(0, 9999), 4, '0', STR_PAD_LEFT),
|
|
|
+ 'code_verified' => false,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $schedule = Schedule::create($scheduleData);
|
|
|
+
|
|
|
+ $customScheduleData = [
|
|
|
+ 'schedule_id' => $schedule->id,
|
|
|
+ 'address_type' => $data['address_type'],
|
|
|
+ 'service_type_id' => $data['service_type_id'],
|
|
|
+ 'description' => $data['description'] ?? null,
|
|
|
+ 'min_price' => $data['min_price'],
|
|
|
+ 'max_price' => $data['max_price'],
|
|
|
+ 'offers_meal' => $data['offers_meal'] ?? false,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $customSchedule = CustomSchedule::create($customScheduleData);
|
|
|
+
|
|
|
+ if (!empty($specialityIds)) {
|
|
|
+ foreach ($specialityIds as $specialityId) {
|
|
|
+ CustomScheduleSpeciality::create([
|
|
|
+ 'custom_schedule_id' => $customSchedule->id,
|
|
|
+ 'speciality_id' => $specialityId,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $createdCustomSchedules[] = $customSchedule->load([
|
|
|
+ 'schedule.client.user',
|
|
|
+ 'schedule.address',
|
|
|
+ 'serviceType',
|
|
|
+ 'specialities.speciality'
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ return $createdCustomSchedules;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ DB::rollBack();
|
|
|
+ Log::error('Error creating custom schedule: ' . $e->getMessage());
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function update($id, array $data)
|
|
|
+ {
|
|
|
+ DB::beginTransaction();
|
|
|
+ try {
|
|
|
+ $customSchedule = CustomSchedule::findOrFail($id);
|
|
|
+ $schedule = $customSchedule->schedule;
|
|
|
+
|
|
|
+ $scheduleUpdateData = [];
|
|
|
+ if (isset($data['address_id'])) {
|
|
|
+ $scheduleUpdateData['address_id'] = $data['address_id'];
|
|
|
+ }
|
|
|
+ if (isset($data['date'])) {
|
|
|
+ $scheduleUpdateData['date'] = $data['date'];
|
|
|
+ }
|
|
|
+ if (isset($data['period_type'])) {
|
|
|
+ $scheduleUpdateData['period_type'] = $data['period_type'];
|
|
|
+ }
|
|
|
+ if (isset($data['start_time'])) {
|
|
|
+ $scheduleUpdateData['start_time'] = $data['start_time'];
|
|
|
+ }
|
|
|
+ if (isset($data['end_time'])) {
|
|
|
+ $scheduleUpdateData['end_time'] = $data['end_time'];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!empty($scheduleUpdateData)) {
|
|
|
+ $schedule->update($scheduleUpdateData);
|
|
|
+ }
|
|
|
+
|
|
|
+ $customScheduleUpdateData = [];
|
|
|
+ if (isset($data['address_type'])) {
|
|
|
+ $customScheduleUpdateData['address_type'] = $data['address_type'];
|
|
|
+ }
|
|
|
+ if (isset($data['service_type_id'])) {
|
|
|
+ $customScheduleUpdateData['service_type_id'] = $data['service_type_id'];
|
|
|
+ }
|
|
|
+ if (isset($data['description'])) {
|
|
|
+ $customScheduleUpdateData['description'] = $data['description'];
|
|
|
+ }
|
|
|
+ if (isset($data['min_price'])) {
|
|
|
+ $customScheduleUpdateData['min_price'] = $data['min_price'];
|
|
|
+ }
|
|
|
+ if (isset($data['max_price'])) {
|
|
|
+ $customScheduleUpdateData['max_price'] = $data['max_price'];
|
|
|
+ }
|
|
|
+ if (isset($data['offers_meal'])) {
|
|
|
+ $customScheduleUpdateData['offers_meal'] = $data['offers_meal'];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!empty($customScheduleUpdateData)) {
|
|
|
+ $customSchedule->update($customScheduleUpdateData);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isset($data['speciality_ids'])) {
|
|
|
+ $custom_schedule = CustomScheduleSpeciality::where('custom_schedule_id', $customSchedule->id);
|
|
|
+ $custom_schedule->delete();
|
|
|
+
|
|
|
+ foreach ($data['speciality_ids'] as $specialityId) {
|
|
|
+ CustomScheduleSpeciality::create([
|
|
|
+ 'custom_schedule_id' => $customSchedule->id,
|
|
|
+ 'speciality_id' => $specialityId,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ return $customSchedule->fresh([
|
|
|
+ 'schedule.client.user',
|
|
|
+ 'schedule.address',
|
|
|
+ 'serviceType',
|
|
|
+ 'specialities.speciality'
|
|
|
+ ]);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ DB::rollBack();
|
|
|
+ Log::error('Error updating custom schedule: ' . $e->getMessage());
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function delete($id)
|
|
|
+ {
|
|
|
+ DB::beginTransaction();
|
|
|
+ try {
|
|
|
+ $customSchedule = CustomSchedule::findOrFail($id);
|
|
|
+ $schedule = $customSchedule->schedule;
|
|
|
+
|
|
|
+ CustomScheduleSpeciality::where('custom_schedule_id', $customSchedule->id)->delete();
|
|
|
+
|
|
|
+ $customSchedule->delete();
|
|
|
+
|
|
|
+ $schedule->delete();
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ return $customSchedule;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ DB::rollBack();
|
|
|
+ Log::error('Error deleting custom schedule: ' . $e->getMessage());
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|