|
|
@@ -3,10 +3,12 @@
|
|
|
namespace App\Services;
|
|
|
|
|
|
use App\Enums\ServicePackageStatusEnum;
|
|
|
+use App\Enums\NotificationTypeEnum;
|
|
|
use App\Enums\PaymentSplitStatusEnum;
|
|
|
use App\Enums\PaymentStatusEnum;
|
|
|
use App\Exceptions\PaymentFailedException;
|
|
|
use App\Exceptions\PaymentException;
|
|
|
+use App\Jobs\StartScheduleJob;
|
|
|
use App\Models\ServicePackage;
|
|
|
use App\Models\ClientPaymentMethod;
|
|
|
use App\Models\Payment;
|
|
|
@@ -106,7 +108,7 @@ class PaymentService
|
|
|
throw new AuthorizationException;
|
|
|
}
|
|
|
|
|
|
- $schedules = $servicePackage->items->pluck('schedule')->filter()->values();
|
|
|
+ $schedules = $this->activePackageSchedules($servicePackage);
|
|
|
|
|
|
$this->validateServicePackageForPayment($servicePackage, $schedules);
|
|
|
|
|
|
@@ -324,7 +326,7 @@ class PaymentService
|
|
|
throw new AuthorizationException;
|
|
|
}
|
|
|
|
|
|
- $schedules = $servicePackage->items->pluck('schedule')->filter()->values();
|
|
|
+ $schedules = $this->activePackageSchedules($servicePackage);
|
|
|
|
|
|
$this->validateServicePackageForPayment($servicePackage, $schedules);
|
|
|
|
|
|
@@ -459,25 +461,35 @@ class PaymentService
|
|
|
}
|
|
|
|
|
|
if ($payment->service_package_id) {
|
|
|
- DB::transaction(function () use ($payment): void {
|
|
|
+ $paidSchedules = DB::transaction(function () use ($payment): SupportCollection {
|
|
|
$servicePackage = ServicePackage::query()->lockForUpdate()->with('items')->find($payment->service_package_id);
|
|
|
|
|
|
if (! $servicePackage) {
|
|
|
- return;
|
|
|
+ return collect();
|
|
|
}
|
|
|
|
|
|
$scheduleIds = $servicePackage->items->pluck('schedule_id')->filter()->unique();
|
|
|
|
|
|
- Schedule::query()
|
|
|
+ $schedulesToPay = Schedule::query()
|
|
|
+ ->with('provider')
|
|
|
->whereIn('id', $scheduleIds)
|
|
|
->where('status', 'accepted')
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ Schedule::query()
|
|
|
+ ->whereIn('id', $schedulesToPay->pluck('id'))
|
|
|
+ ->where('status', 'accepted')
|
|
|
->update(['status' => 'paid']);
|
|
|
|
|
|
if ($servicePackage->status !== ServicePackageStatusEnum::PAID) {
|
|
|
$servicePackage->update(['status' => ServicePackageStatusEnum::PAID->value]);
|
|
|
}
|
|
|
+
|
|
|
+ return $schedulesToPay->toBase();
|
|
|
});
|
|
|
|
|
|
+ $paidSchedules->each(fn (Schedule $schedule) => $this->notifyProviderAndScheduleStart($schedule));
|
|
|
+
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -488,6 +500,25 @@ class PaymentService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private function notifyProviderAndScheduleStart(Schedule $schedule): void
|
|
|
+ {
|
|
|
+ if ($schedule->provider_id && $schedule->provider) {
|
|
|
+ app(NotificationService::class)->create([
|
|
|
+ 'title' => __('notifications.payment_confirmed_title'),
|
|
|
+ 'description' => __('notifications.payment_confirmed_description'),
|
|
|
+ 'origin' => 'schedule',
|
|
|
+ 'origin_id' => $schedule->id,
|
|
|
+ 'type' => NotificationTypeEnum::SCHEDULE_PROVIDER_START->value,
|
|
|
+ 'user_id' => $schedule->provider->user_id,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ $dateCleaned = Carbon::parse($schedule->date)->format('Y-m-d');
|
|
|
+
|
|
|
+ StartScheduleJob::dispatch($schedule->id)
|
|
|
+ ->delay(Carbon::parse($dateCleaned . ' ' . $schedule->start_time)->subHour());
|
|
|
+ }
|
|
|
+
|
|
|
private function syncServicePackagesForSchedule(Schedule $schedule): void
|
|
|
{
|
|
|
ServicePackage::query()
|
|
|
@@ -525,6 +556,15 @@ class PaymentService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private function activePackageSchedules(ServicePackage $servicePackage): SupportCollection
|
|
|
+ {
|
|
|
+ return $servicePackage->items
|
|
|
+ ->pluck('schedule')
|
|
|
+ ->filter()
|
|
|
+ ->reject(fn (Schedule $schedule) => in_array($schedule->status, ['cancelled', 'rejected'], true))
|
|
|
+ ->values();
|
|
|
+ }
|
|
|
+
|
|
|
private function validateServicePackageForPayment(ServicePackage $servicePackage, SupportCollection $schedules): void
|
|
|
{
|
|
|
if (! in_array($servicePackage->status, [ServicePackageStatusEnum::OPEN, ServicePackageStatusEnum::PAID], true)) {
|