CustomScheduleService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace App\Services;
  3. use App\Models\CustomSchedule;
  4. use App\Models\Schedule;
  5. use App\Models\CustomScheduleSpeciality;
  6. use Carbon\Carbon;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Log;
  9. class CustomScheduleService
  10. {
  11. public function getAll()
  12. {
  13. return CustomSchedule::with([
  14. 'schedule.client.user',
  15. 'schedule.address',
  16. 'serviceType',
  17. 'specialities.speciality'
  18. ])
  19. ->orderBy('id', 'desc')
  20. ->get();
  21. }
  22. public function getById($id)
  23. {
  24. return CustomSchedule::with([
  25. 'schedule.client.user',
  26. 'schedule.address',
  27. 'serviceType',
  28. 'specialities.speciality'
  29. ])->findOrFail($id);
  30. }
  31. public function create(array $data)
  32. {
  33. DB::beginTransaction();
  34. try {
  35. $quantity = $data['quantity'] ?? 1;
  36. $specialityIds = $data['speciality_ids'] ?? [];
  37. $createdCustomSchedules = [];
  38. for ($i = 0; $i < $quantity; $i++) {
  39. $scheduleData = [
  40. 'client_id' => $data['client_id'],
  41. 'provider_id' => null,
  42. 'address_id' => $data['address_id'],
  43. 'date' => $data['date'],
  44. 'period_type' => $data['period_type'],
  45. 'schedule_type' => 'custom',
  46. 'start_time' => $data['start_time'],
  47. 'end_time' => $data['end_time'],
  48. 'status' => 'pending',
  49. 'total_amount' => 0,
  50. 'code' => str_pad(random_int(0, 9999), 4, '0', STR_PAD_LEFT),
  51. 'code_verified' => false,
  52. ];
  53. $schedule = Schedule::create($scheduleData);
  54. $customScheduleData = [
  55. 'schedule_id' => $schedule->id,
  56. 'address_type' => $data['address_type'],
  57. 'service_type_id' => $data['service_type_id'],
  58. 'description' => $data['description'] ?? null,
  59. 'min_price' => $data['min_price'],
  60. 'max_price' => $data['max_price'],
  61. 'offers_meal' => $data['offers_meal'] ?? false,
  62. ];
  63. $customSchedule = CustomSchedule::create($customScheduleData);
  64. if (!empty($specialityIds)) {
  65. foreach ($specialityIds as $specialityId) {
  66. CustomScheduleSpeciality::create([
  67. 'custom_schedule_id' => $customSchedule->id,
  68. 'speciality_id' => $specialityId,
  69. ]);
  70. }
  71. }
  72. $createdCustomSchedules[] = $customSchedule->load([
  73. 'schedule.client.user',
  74. 'schedule.address',
  75. 'serviceType',
  76. 'specialities.speciality'
  77. ]);
  78. }
  79. DB::commit();
  80. return $createdCustomSchedules;
  81. } catch (\Exception $e) {
  82. DB::rollBack();
  83. Log::error('Error creating custom schedule: ' . $e->getMessage());
  84. throw $e;
  85. }
  86. }
  87. public function update($id, array $data)
  88. {
  89. DB::beginTransaction();
  90. try {
  91. $customSchedule = CustomSchedule::findOrFail($id);
  92. $schedule = $customSchedule->schedule;
  93. $scheduleUpdateData = [];
  94. if (isset($data['address_id'])) {
  95. $scheduleUpdateData['address_id'] = $data['address_id'];
  96. }
  97. if (isset($data['date'])) {
  98. $scheduleUpdateData['date'] = $data['date'];
  99. }
  100. if (isset($data['period_type'])) {
  101. $scheduleUpdateData['period_type'] = $data['period_type'];
  102. }
  103. if (isset($data['start_time'])) {
  104. $scheduleUpdateData['start_time'] = $data['start_time'];
  105. }
  106. if (isset($data['end_time'])) {
  107. $scheduleUpdateData['end_time'] = $data['end_time'];
  108. }
  109. if (!empty($scheduleUpdateData)) {
  110. $schedule->update($scheduleUpdateData);
  111. }
  112. $customScheduleUpdateData = [];
  113. if (isset($data['address_type'])) {
  114. $customScheduleUpdateData['address_type'] = $data['address_type'];
  115. }
  116. if (isset($data['service_type_id'])) {
  117. $customScheduleUpdateData['service_type_id'] = $data['service_type_id'];
  118. }
  119. if (isset($data['description'])) {
  120. $customScheduleUpdateData['description'] = $data['description'];
  121. }
  122. if (isset($data['min_price'])) {
  123. $customScheduleUpdateData['min_price'] = $data['min_price'];
  124. }
  125. if (isset($data['max_price'])) {
  126. $customScheduleUpdateData['max_price'] = $data['max_price'];
  127. }
  128. if (isset($data['offers_meal'])) {
  129. $customScheduleUpdateData['offers_meal'] = $data['offers_meal'];
  130. }
  131. if (!empty($customScheduleUpdateData)) {
  132. $customSchedule->update($customScheduleUpdateData);
  133. }
  134. if (isset($data['speciality_ids'])) {
  135. $custom_schedule = CustomScheduleSpeciality::where('custom_schedule_id', $customSchedule->id);
  136. $custom_schedule->delete();
  137. foreach ($data['speciality_ids'] as $specialityId) {
  138. CustomScheduleSpeciality::create([
  139. 'custom_schedule_id' => $customSchedule->id,
  140. 'speciality_id' => $specialityId,
  141. ]);
  142. }
  143. }
  144. DB::commit();
  145. return $customSchedule->fresh([
  146. 'schedule.client.user',
  147. 'schedule.address',
  148. 'serviceType',
  149. 'specialities.speciality'
  150. ]);
  151. } catch (\Exception $e) {
  152. DB::rollBack();
  153. Log::error('Error updating custom schedule: ' . $e->getMessage());
  154. throw $e;
  155. }
  156. }
  157. public function delete($id)
  158. {
  159. DB::beginTransaction();
  160. try {
  161. $customSchedule = CustomSchedule::findOrFail($id);
  162. $schedule = $customSchedule->schedule;
  163. CustomScheduleSpeciality::where('custom_schedule_id', $customSchedule->id)->delete();
  164. $customSchedule->delete();
  165. $schedule->delete();
  166. DB::commit();
  167. return $customSchedule;
  168. } catch (\Exception $e) {
  169. DB::rollBack();
  170. Log::error('Error deleting custom schedule: ' . $e->getMessage());
  171. throw $e;
  172. }
  173. }
  174. }