Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- public function findAvailablePlan($user, $planId, $bypassRestrictions=false)
- {
- if ($bypassRestrictions) {
- return $plan = Plan::with('groups')->find(planId);
- }
- return Plan::with(array('groups' => function($query) use($user) {
- $today = Carbon::now();
- $age = (int)$user->birthday->diffInYears($today) * 12;
- $birthday = $user->birthday;
- # On s'assure que le cours n'est pas fini et qu'il reste de la place
- $query->where('end_date', '>', $today);
- $query->where('nbr_places_remaining', '>', 0)
- ->orWhereNull('nbr_places_remaining');
- # On vérifie le sexe
- $query->where(function($query) use ($user) {
- $query->where('restriction_sex', '=', 'A')
- ->orWhere('restriction_sex', '=', $user->sex);
- });
- # On vérifie l'age.
- $query->where(function($query) use ($age, $birthday) {
- $query->where(function($query) use ($age) {
- $query->whereNull('validate_age_on');
- $query->where(function($query) use ($age) {
- $query->where('restriction_age_min', '<=', $age)
- ->orWhereNull('restriction_age_min');
- });
- $query->where(function($query) use ($age) {
- $query->where('restriction_age_max', '>=', $age)
- ->orWhereNull('restriction_age_max');
- });
- });
- $query->orWhere(function($query) use ($birthday) {
- $query->whereNotNull('validate_age_on');
- $query->where(function($query) use ($birthday) {
- $query->where("restriction_age_min", "<=", DB::raw("EXTRACT(YEAR FROM AGE(validate_age_on, '{$birthday->format('Y-m-d')}')) * 12"))
- ->orWhereNull('restriction_age_min');
- });
- $query->where(function($query) use ($birthday) {
- $query->where("restriction_age_max", ">=", DB::raw("EXTRACT(YEAR FROM AGE(validate_age_on, '{$birthday->format('Y-m-d')}')) * 12"))
- ->orWhereNull('restriction_age_max');
- });
- });
- });
- }))->find($planId);
- }
- public static function registerUser($userId, $planId, $quantity = 1,$billingItemId = NULL, $cartId = NULL, $byPassRestrictions = FALSE, $createdBy = NULL) {
- try {
- $user = User::find($userId); /** @var \App\Models\User $user */
- if (!user) {
- throw new UserNotFoundException();
- }
- $plan = $findAvailablePlan($user, $planId, $byPassRestrictions);
- if (!$plan) {
- throw new PlanNotFoundException();
- }
- if (count($plan->groups) < 1) {
- throw new NoGroupsFoundException();
- }
- if($plan->type_plan == "PASS") { # On crée une entrée dans la table Pass en fct de la quantité
- $pass = new Pass;
- $pass->user()->associate($user);
- $pass->plan()->associate($plan);
- $pass->initial_quantity = $plan->quantity * $quantity;
- $pass->remaining_quantity = $plan->quantity * $quantity;
- $pass->expiration_date = $plan->pass_exp_date;
- $pass->billing_item_id = $billingItemId;
- $pass->save();
- }elseif($plan->type_plan == "INTERVAL" || $plan->type_plan == "SESSION") { # Enregistrement standard
- foreach($plan->groups as $group) {
- if($plan->type_plan == "SESSION") {
- $sessions = $group->sessions()->get();
- }else {
- # Plan par interval, on va chercher les plages où l'utilisateur a le droit de s'inscrire
- $sessions = $group->sessions()->whereBetween('start_date', [$plan->restriction_start_date, $plan->restriction_end_date])->get();
- }
- if(count($sessions) > 0) {
- $registrations = [];
- foreach($sessions as $session) {
- array_push($registrations, array(
- 'user_id' => $userId,
- 'created_by_user_id'=> $createdBy != NULL ? $createdBy: $user->id,
- 'session_id' => $session->id,
- 'type_status_id' => 'OK',
- 'created_at' => Carbon::now(),
- 'updated_at' => Carbon::now(),
- 'billing_item_id' => $billingItemId,
- 'cart_id' => $cartId,
- ));
- }
- # On fait de l'insertion de masse pour pas faire 58 INSERT de suite
- Registration::insert($registrations);
- }else {
- $errorCode = "NO_SESSION";
- $errorDescription = "Aucune session ne peut être inscrite";
- }
- if(isset($group->nbr_places))
- {
- $group->nbr_places_remaining--;
- $group->save();
- # On met à jour les places dans les sessions que l'utilisateur a été inscrit
- if($plan->type_plan == "SESSION") {
- DB::update('UPDATE "session" SET nbr_places_remaining = nbr_places_remaining -1
- WHERE group_id = :groupId AND start_date >= now()' , ['groupId' => $group->id]) ;
- }else {
- DB::update('UPDATE "session" SET nbr_places_remaining = nbr_places_remaining -1
- WHERE group_id = :groupId AND start_date >= now() AND start_date BETWEEN CAST(COALESCE(:startDate, \'1901-01-01\') AS TIMESTAMP) AND CAST(COALESCE(:endDate, \'2099-12-12\') AS TIMESTAMP)'
- , ['groupId' => $group->id, 'startDate' => $plan->restriction_start_date, 'endDate' => $plan->restriction_end_date ]) ;
- }
- }
- # On crée le group_registration, que l'utilisateur ce soit inscrit à une session ou pas. C'est important dans le cas où les horaires ne sont pas décidés
- $groupRegistration = new GroupRegistration;
- $groupRegistration->group_id = $group->id;
- $groupRegistration->user_id = $userId;
- $groupRegistration->billing_item_id = $billingItemId;
- if($plan->type_plan == "SESSION") {
- $groupRegistration->start_date = $group->start_date;
- $groupRegistration->end_date = $group->end_date;
- }else {
- $groupRegistration->start_date = $plan->restriction_start_date;
- $groupRegistration->end_date = $plan->restriction_end_date;
- }
- $groupRegistration->save();
- }
- }
- return TRUE;
- } catch (UserNotFoundException $noUserException) {
- //TODO PaymentLog ...
- return false;
- } catch (PlanNotFoundException $noPlanException) {
- //TODO PaymentLog ...
- return false;
- } catch (NoGroupsFoundException $noGroupsException) {
- //TODO PaymentLog ...
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement