PATH:
home
/
rwabteecom
/
project_11
/
app
/
Http
/
Controllers
/
Dashboard
/
Subscribe
/
Editing: SubscribeController.php
<?php namespace App\Http\Controllers\Dashboard\Subscribe; use App\Enums\OnboardingEnum; use App\Http\Controllers\Controller; use App\Models\Invoice; use App\Models\User; use App\Models\UserPlan; use App\Services\Plan\PlanServices; use App\Services\Profile\ProfileServices; use Illuminate\Http\Request; use App\Services\Socials\SocialServices; use App\Services\User\UserServices; use Exception; use Log; use Auth; use Carbon\Carbon; use Illuminate\Support\Facades\DB; use Illuminate\Validation\Rule; class SubscribeController extends Controller { public function __construct( private readonly PlanServices $planServices) { } public function store(request $request) { try { $plan_id=$request->plan_id; $user = auth()->user(); $plan = $this->planServices->findOrFail($plan_id, true); $plan_expired_at=Carbon::now()->addDays($plan->duration_in_days)->format('Y-m-d'); $this->attachPlanToUser($plan_id,$user->id,$plan_expired_at,0); } catch (Exception $exception) { \Log::error($exception->getMessage()); return back()->withInput()->with('error', $exception->getMessage()); } if($request->boarding){ skipBoardingStep(OnboardingEnum::THEMES); return redirect()->route('dashboard.onboarding'); } return back()->with("success", __("The plan has been purchased successfully")); } public function attachPlanToUser($plan_id, $user_id, $plan_expired_at, $is_paid): void { DB::transaction(function () use ($user_id, $plan_id, $plan_expired_at, $is_paid) { $plan = $this->planServices->findOrFail($plan_id, true); $user = (new UserServices())->findUserById($user_id); if ($plan->is_free_plan && $user->is_subscribed_to_free_plan) { throw new Exception(__("User already subscribed to free plan before")); } if ($plan->is_free_plan && $user->plans()->count()) { throw new Exception(__("User already subscribed to paid plans, can't subscribe to free plan")); } if ($user->plans()->count() > 0 && $plan->order < $user->currentPlan?->order) { throw new Exception(__("User can't downgrade current plan")); } if ($plan->is_free_plan) { $this->attachFreePlan($user, $plan_expired_at); } else { $this->attachPaidPlan($user, $plan, $plan_expired_at, $is_paid); } }); } private function attachPaidPlan($user, $plan, $plan_expired_at, $is_paid): void { $user->update([ "current_plan_expired_at" => Carbon::parse($plan_expired_at)->format("Y-m-d") ]); $price = $this->calculatePlanPrice( user: $user, requestedPlan: $plan, ); if ($price == 0) { throw new Exception(__("Price is equal zero , please check selected plan")); } $userPlan = UserPlan::query()->create([ "user_id" => $user->id, "title" => [ "ar" => $plan->getTranslation("title", "ar"), "en" => $plan->getTranslation("title", "en"), ], "description" => [ "ar" => $plan->getTranslation("description", "ar"), "en" => $plan->getTranslation("description", "en"), ], "order" => $plan->order, "plan_id" => $plan->id, "price" => $price, "plan_end_at" => Carbon::parse($plan_expired_at)->format("Y-m-d"), ]); $invoice = Invoice::query()->create([ "user_plan_id" => $userPlan->id, "user_id" => $user->id, "price" => $price, "is_paid" => $is_paid ]); $invoice->update([ "invoice_number" => "00000" . $invoice->id ]); } private function attachFreePlan($user, $plan_expired_at): void { $user->update([ "is_subscribed_to_free_plan" => 1, "current_plan_expired_at" => Carbon::parse($plan_expired_at)->format("Y-m-d") ]); } private function calculatePlanPrice($user, $requestedPlan): float { $priceForRemainingDays = 0; $paidPlans = $user->plans()->whereHas('invoice', fn($query) => $query->where("is_paid", 1))->get(); foreach ($paidPlans as $userPlan) { $currentPlanExpiredAt = Carbon::parse($userPlan->plan_end_at)->addDay(); $diffInDays = $currentPlanExpiredAt->diffInDays(now()); $pricePerDay = $userPlan->price / ($userPlan->created_at->diffInDays($currentPlanExpiredAt)); $priceForRemainingDays += $diffInDays * $pricePerDay; } return $requestedPlan->price - $priceForRemainingDays; } }
SAVE
CANCEL