Advertisement
Guest User

Untitled

a guest
Aug 30th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 40.40 KB | None | 0 0
  1. <?php
  2.     public function postRegister($domain, $slug, Request $request)
  3.     {
  4.         if ($request->has('force_error')) {
  5.             //force error
  6.             $a = 1 / 0;
  7.             die();
  8.         }
  9.         $register_by_organiser = false;
  10.         $event                 = Events::where('slug', $slug)->first();
  11.         if (preg_replace('/[0-9]/', '', $slug) == '') {
  12.             $event                 = Events::find($slug);
  13.             $register_by_organiser = Auth::check();
  14.         }
  15.         if (! $event) {
  16.             abort(404);
  17.         }
  18.  
  19.         $payment_method_id = $request->input('payment_method_id', null);
  20.         $payment_method_id = $payment_method_id == '' ? null : $payment_method_id;
  21.  
  22.         $files = Events::saveFiles($request);
  23.         if (count($files) > 0) {
  24.             foreach ($files as $key => $arr) {
  25.                 foreach ($arr as $k => $v) {
  26.                     if (preg_match('/^customizable_file_upload/', $k)) {
  27.                         $new_data                          = $request->all();
  28.                         $new_data['participant'][$key][$k] = $v;
  29.                         $request                           = new Request($new_data);
  30.                         unset($files[$key]);
  31.                     }
  32.                 }
  33.             }
  34.         }
  35.         $user                = null;
  36.         $registrant_id       = null;
  37.         $forms_registrant_id = null;
  38.         $registrant_email    = null;
  39.         $emails              = [];
  40.         $first_registrant    = true;
  41.  
  42.         $totalPrice          = 0;
  43.         $totalFees           = 0;
  44.  
  45.         if ($request->has('discount_code')) {
  46.             $discount = Discounts::where('code', $request->input('discount_code'))
  47.                 ->where('disabled', false)
  48.                 ->whereHas('forms', function($forms) use ($event) {
  49.                     $forms->whereIn('id', $event->forms->lists('id')->toArray());
  50.                 })
  51.                 ->with('forms')
  52.                 ->first();
  53.  
  54.             if (! $discount || ! $discount->isValid()) {
  55.                 unset($discount);
  56.             }
  57.  
  58.         }
  59.  
  60.         $discountSum = 0;
  61.  
  62.         // Gonna use to check if fee percent added for participants (because on subforms we add fee_percent only once)
  63.         $parentFormAmountAdded = [];
  64.  
  65.         // Will be used for group reductions
  66.         $participantCount = 0;
  67.         $teamParticipantCount = [];
  68.  
  69.         // Count participants
  70.         foreach ($request->participant as $participant_id => $participant) {
  71.             if (isset($participant['first_name']) && isset($participant['form']) && $participant['form'] > 0) {
  72.                 $participantCount++;
  73.  
  74.                 $form = Forms::find($participant['form']);
  75.                 if ($form && !$form->isOpenForRegistration($register_by_organiser)) {
  76.                     return redirect()->back();
  77.                 }
  78.  
  79.                 if ($event->group_reduction_team_pins) {
  80.                     $team_name = $request->input('participant.' . explode('_', $participant_id)[0] . '.team_name', '');
  81.                     if (is_string($team_name)) {
  82.                         $team_name = trim($team_name);
  83.                     }
  84.  
  85.                     $team_pin = $request->input('participant.' . explode('_', $participant_id)[0] . '.team_pin', '');
  86.                     if (is_string($team_pin)) {
  87.                         $team_pin = trim($team_pin);
  88.                     }
  89.  
  90.                     if ($team_name && $team_pin && $this->checkIfPinIsValid($team_name, $team_pin, $form)) {
  91.                         if (! isset($teamParticipantCount[$team_name][$form->id])) {
  92.                             $teamParticipantCount[$team_name][$form->id] = $this->getTeamMembers($team_name, $form);
  93.                         }
  94.  
  95.                         $teamParticipantCount[$team_name][$form->id]++;
  96.                     }
  97.                 }
  98.             }
  99.         }
  100.  
  101.         foreach ($request->participant as $participant_id => $participant) {
  102.             if (isset($participant['first_name']) && isset($participant['form']) && $participant['form'] > 0) {
  103.                 $formDiscountAmount = 0;
  104.                 $participantPrice = 0;
  105.                 $form = Forms::find($participant['form']);
  106.                 if ($form && !$form->isOpenForRegistration($register_by_organiser)) {
  107.                     return redirect()->back();
  108.                 }
  109.  
  110.                 // Form we use to calculate price (For example parent form of subform)
  111.                 $priceForm = ($form->form_id ? $form->parent_form : $form);
  112.  
  113.                 $participant_id_exploded = explode('_', $participant_id);
  114.                 $mainParticipantKey = reset($participant_id_exploded); // Parent id for subforms
  115.  
  116.                 $notAddedParentFormFee = (! isset($parentFormAmountAdded[$mainParticipantKey]) || $parentFormAmountAdded[$mainParticipantKey] != true);
  117.  
  118.                 $participantFormPrice = 0;
  119.  
  120.                 if (! $form->isFree() && $notAddedParentFormFee) {
  121.                     $team_name = $request->input('participant.' . explode('_', $participant_id)[0] . '.team_name', '');
  122.                     if (is_string($team_name)) {
  123.                         $team_name = trim($team_name);
  124.                     }
  125.  
  126.                     foreach ($priceForm->prices as $formPrice) {
  127.                         $timeFrom = strtotime($formPrice->date_from . ' ' . $formPrice->time_from);
  128.                         $timeTo = strtotime($formPrice->date_to . ' ' . $formPrice->time_to);
  129.                         $now = time();
  130.  
  131.                         if ($timeFrom <= $now && $timeTo > $now) {
  132.                             if (\Input::get('participant.' . $participant_id . '.form_price') != $formPrice->price) {
  133.                                 \Bugsnag::notifyException(new \Exception('Bad participant form price'));
  134.                             }
  135.  
  136.                             $participantFormPrice = $formPrice->price;
  137.  
  138.                             $teamParticipants = 0;
  139.                             if ($team_name && isset($teamParticipantCount[$team_name][$form->id])) {
  140.                                 $teamParticipants = $teamParticipantCount[$team_name][$form->id];
  141.                             }
  142.  
  143.                             if ($event->group_reduction
  144.                                 && ($participantCount >= $event->group_reduction_places || $teamParticipants >= $event->group_reduction_places)
  145.                             ) {
  146.                                 $participantFormPrice -= $event->group_reduction_amount;
  147.                             }
  148.  
  149.                             if (isset($discount) && $discount
  150.                                 && (! $discount->forms->where('id', $priceForm->id)->isEmpty() || ! $discount->forms->where('form_id', $priceForm->id)->isEmpty())
  151.                             ) {
  152.                                 if ($discount->discount_amount) {
  153.                                     $formDiscountAmount = $discount->discount_amount;
  154.                                 } elseif ($discount->discount_percent) {
  155.                                     $formDiscountAmount = $participantFormPrice * $discount->discount_percent / 100;
  156.                                 } else {
  157.                                     $formDiscountAmount = $participantFormPrice - $discount->discount_price;
  158.                                 }
  159.  
  160.                                 if ($formDiscountAmount > $participantFormPrice) {
  161.                                     $formDiscountAmount = $participantFormPrice;
  162.                                 }
  163.  
  164.                                 $discountSum += $formDiscountAmount;
  165.  
  166.                                 if (!$discount->all_basket_reduction) {
  167.                                     unset($discount);
  168.                                 }
  169.                             }
  170.  
  171.                             $participantPrice += $participantFormPrice;
  172.                         }
  173.                     }
  174.                 }
  175.  
  176.                 $optionsPrice = 0;
  177.  
  178.                 if (! $event->service_fees_per_subforms && $notAddedParentFormFee) {
  179.                     foreach ($request->participant as $request_participant_id => $request_participant) {
  180.                         $tempExploded = explode('_', $request_participant_id);
  181.                         $mainRequestParticipantKey = reset($tempExploded);
  182.  
  183.                         if (isset($request_participant['first_name']) && isset($request_participant['form'])
  184.                             && $request_participant['form'] > 0 && $mainRequestParticipantKey == $mainRequestParticipantKey
  185.                         ) {
  186.                             $optionsPrice += $this->calculateItemsPricesFromForm($request_participant, $request_participant_id);
  187.                         }
  188.                     }
  189.                 } elseif ($event->service_fees_per_subforms) {
  190.                     $optionsPrice += $this->calculateItemsPricesFromForm($participant, $participant_id);
  191.                 }
  192.  
  193.                 $participantPrice += $optionsPrice;
  194.  
  195.                 //save main form values like "north challenge" for each participant
  196.                 $index = explode('_', $participant_id);
  197.                 $index = count($index) > 1 ? $index[0] : 'nonexistingkey';
  198.                 foreach ($request->input('participant.' . $index, []) as $key => $value) {
  199.                     if (substr($key, 0, 7) == 'option_' && $value != '' && $value != '0') {
  200.                         $option = substr($key, 7);
  201.                         $forms_field = FormsFields::select('forms_fields.id', 'forms_fields.price')
  202.                             ->join('forms_fields_types', 'forms_fields.field_type_id', '=', 'forms_fields_types.id')
  203.                             ->where('forms_fields.form_id', $request->input("participant.{$index}.form", 0))
  204.                             ->where('forms_fields_types.title', $option)
  205.                             ->first();
  206.                         if ($forms_field && $forms_field->getPriceBySelectedValue($value)) {
  207.                             $participantPrice += $forms_field->getPriceBySelectedValue($value);
  208.                         }
  209.                     }
  210.                 }
  211.  
  212.                 if ($participantPrice && ($event->service_fees_per_subforms || $notAddedParentFormFee)) {
  213.                     $fee = $priceForm->forms_group->event->serviceFees()
  214.                         ->where('price_from', '<=', $participantPrice)
  215.                         ->where('price_to', '>', $participantPrice)
  216.                         ->first();
  217.  
  218.                     if ($fee && $participantPrice > $formDiscountAmount) {
  219.                         $feePrice = 0;
  220.  
  221.                         if ($participantPrice > 0 && $fee->fee_amount) {
  222.                             $feePrice += $fee->fee_amount;
  223.                         }
  224.  
  225.                         if ($notAddedParentFormFee && $fee->fee_percent) {
  226.                             $feePrice += round(($participantFormPrice - $formDiscountAmount) * $fee->fee_percent) / 100;
  227.                             $feePrice += round($optionsPrice * $fee->fee_percent) / 100;
  228.                         }
  229.  
  230.                         $totalFees += $feePrice;
  231.  
  232.                         if (! $form->forms_group->event->fees_include && ! $register_by_organiser) {
  233.                             $participantPrice += $feePrice;
  234.                         }
  235.                     }
  236.                 }
  237.  
  238.                 $totalPrice += $participantPrice;
  239.  
  240.                 $parentFormAmountAdded[$mainParticipantKey] = true;
  241.             }
  242.         }
  243.  
  244.  
  245.         $totalPrice -= $discountSum;
  246.         $totalPrice = round($totalPrice, 2);
  247.         $totalFees = round($totalFees, 2);
  248.  
  249.  
  250.         if (\Input::get('total_price') != $totalPrice) {
  251.             \Bugsnag::notifyException(new \Exception('Bad total price: ' . \Input::get('total_price') . ' vs ' . $totalPrice));
  252.         }
  253.         if (\Input::get('event_fee') != $totalFees) {
  254.             \Bugsnag::notifyException(new \Exception('Bad fees price: ' . \Input::get('event_fee') . ' vs ' . $totalFees));
  255.         }
  256.  
  257.         $nothing_to_pay      = $totalPrice == 0;
  258.         $prediscountFree     = $nothing_to_pay && ! $discountSum;
  259.         $once                = false;
  260.         $cartItems           = json_decode($request->input('cart_items'));
  261.         $cart                = Cart::create([
  262.             'items' => $cartItems,
  263.             'total' => $request->input('total_price'),
  264.             'fees'  => ($event->fees_hide || ($register_by_organiser && $payment_method_id == 2)) ? 0 : $request->input('event_fee'),
  265.         ]);
  266.         $unique_group        = str_random(50);
  267.         $m                   = 0;
  268.         $alreadyMarkedAsPaid = [];
  269.         foreach ($request->participant as $participant_id => $participant) {
  270.             if (isset($participant['first_name'])) {
  271.                 foreach ($participant as &$participantValue) {
  272.                     if (is_string($participantValue)) {
  273.                         $participantValue = trim($participantValue);
  274.                     }
  275.                 }
  276.                 $discountRegistrant = $request->input('discount_registrant', null);
  277.                 $discountRegistrant = $discountRegistrant ? : null;
  278.                 $discountApplying   = ! $prediscountFree && (! $discountRegistrant || $discountRegistrant == ($m + 1));
  279.                 if (! $once
  280.                     && ! $prediscountFree
  281.                     && ($discount_code = $request->input('discount_code', null))
  282.                     && $discountApplying
  283.                 ) {
  284.                     $once = true;
  285.                     Discounts::where('code', $discount_code)->increment('times_used');
  286.                     Discounts::where('code', $discount_code)->first()
  287.                         ->usages()->where('ip', getIP())
  288.                         ->where('user_uid', $request->input('q9qz8'))
  289.                         ->delete();
  290.                 }
  291.                 $m++;
  292.  
  293.                 $form = Forms::find($participant['form']);
  294.                 if ($form && ! $form->isOpenForRegistration($register_by_organiser)) {
  295.                     return redirect()->back();
  296.                 }
  297.  
  298.                 if (strstr($participant_id, '_')) {
  299.                     $form = Forms::find(@$request->participant[explode('_', $participant_id)[0]]['form']);
  300.                     if ($form && ! $form->isOpenForRegistration($register_by_organiser)) {
  301.                         return redirect()->back();
  302.                     }
  303.                 }
  304.                 $individual_price = strstr($participant_id, '_') ? $request->input('participant.' . explode('_',
  305.                         $participant_id)[0] . '.individual_price') : $request->input('participant.' . $participant_id . '.individual_price');
  306.                 $parent_id        = strstr($participant_id, '_') ? explode('_', $participant_id)[0] : $participant_id;
  307.                 if (is_null($registrant_email)) {
  308.                     $registrant_email = $participant['email'];
  309.                 }
  310.                 // $password = str_random(8);
  311.                 if (isset($participant['ignore_email_validation']) && $participant['ignore_email_validation']) {
  312.                     $user_id = null;
  313.                 } elseif (! array_key_exists($participant['email'], $emails)) {
  314.                     $user = User::where('email', $participant['email'])->first();
  315.                     if (! $user) {
  316.                         $password = Str::random(8);
  317.                         $user     = User::create([
  318.                             'name'     => $participant['first_name'],
  319.                             'email'    => $participant['email'],
  320.                             'password' => Hash::make($password),
  321.                             'role_id'  => 3,
  322.                         ]);
  323.                         /*
  324.                         Mail::send('emails.newUser', [
  325.                             'name' => $user->name,
  326.                             'password' => $password
  327.                         ], function ($m) use ($user) {
  328.                             $m->to($user->email, $user->name);
  329.                         });
  330.                         */
  331.                         $agent = new Agent();
  332.                         FormsRegistrantsAgent::create([
  333.                             'user_id'          => $user->id,
  334.                             'country'          => '',
  335.                             'browser name'     => $agent->browser(),
  336.                             'operating system' => $request->input('os', ''),
  337.                             'device'           => $request->input('device', ''),
  338.                             'ip_address'       => $_SERVER['REMOTE_ADDR'],
  339.                             'agent'            => $agent->getUserAgent(),
  340.                             'screen_size'      => $request->input('screen_size', '0x0')
  341.                         ]);
  342.                     }
  343.                     $emails[$participant['email']] = $user->id;
  344.                     $user_id                       = $user->id;
  345.                 } else {
  346.                     $user_id = $emails[$participant['email']];
  347.                 }
  348.  
  349.                 $registrant = Registrants::create($participant + ['user_id' => $user_id, 'nationality_id' => null]);
  350.  
  351.                 if (is_null($registrant_id)) {
  352.                     $registrant_id = $registrant->id;
  353.                 }
  354.  
  355.                 $paid_fee = ($request->has('event_fee') && is_null($forms_registrant_id)) ? $request->event_fee : null;
  356.                 if ($register_by_organiser && $payment_method_id == 2) {
  357.                     $paid_fee = 0;
  358.                 }
  359.  
  360.                 $team_id   = null;
  361.                 $team_name = $request->input('participant.' . explode('_', $participant_id)[0] . '.team_name', '');
  362.                 if (is_string($team_name)) {
  363.                     $team_name = trim($team_name);
  364.                 }
  365.                 $team_type = $request->input('participant.' . explode('_', $participant_id)[0] . '.team_type_id', '');
  366.                 if ($team_type == '') {
  367.                     //if we do not have team_type field, check maybe it is optional field
  368.                     $team_type = $request->input('participant.' . explode('_',
  369.                             $participant_id)[0] . '.option_team_type', '');
  370.                 }
  371.                 if ($request->has('participant.' . explode('_', $participant_id)[0] . '.team_id')) {
  372.                     $team_id = $request->input('participant.' . explode('_', $participant_id)[0] . '.team_id');
  373.                 }
  374.                 //check if we have team_name field in request
  375.                 //note for myself: $request->has() returns false when field exists, but its content empty
  376.                 if (! $team_id && $request->input('participant.' . explode('_',
  377.                             $participant_id)[0] . '.team_name') !== null
  378.                 ) {
  379.                     $child_form   = Forms::find($participant['form']);
  380.                     $root_form_id = $child_form->form_id ? : $child_form->id;
  381.  
  382.                     //maybe we already have team with same name and type
  383.                     $team = Team::where('teams_type_id', $team_type ? $team_type : null)
  384.                         ->where('form_id', $root_form_id)
  385.                         ->where('title', $team_name)->first();
  386.                     //no.
  387.                     if (! $team && $team_name) {
  388.                         //create new Team
  389.                         $team = Team::create([
  390.                             'form_id'             => $root_form_id,
  391.                             'teams_type_id'       => $team_type ? $team_type : null,
  392.                             'title'               => $team_name,
  393.                             'registrants_maximum' => 1,
  394.                         ]);
  395.  
  396.                         if ($event->team_pins_enabled && ! TeamPin::where('event_id', $event->id)->where('team_name', $team_name)->exists()) {
  397.                             TeamPin::create([
  398.                                 'team_name'     => $team_name,
  399.                                 'event_id'      => $event->id,
  400.                                 'pin'           => Team::generatePin(),
  401.                             ]);
  402.                         }
  403.                     }
  404.                     $team_id = $team ? $team->id : null;
  405.                     //merge team_id into request for the further usage (for team mates)
  406.                     // $request->merge(['participant' => [explode('_', $participant_id)[0] => ['team_id' => $team->id]]]);
  407.                 }
  408.  
  409.                 $forms_ids              = Forms::find($participant['form'])->forms_group->event->forms()->get([DB::raw('forms.id')])->lists('id')->all();
  410.                 $currentMax             = FormsRegistrants::whereIn('form_id', $forms_ids)->max('registrant_number');
  411.                 $next_registrant_number = $currentMax ? ++$currentMax : '1';
  412.  
  413.                 $form_registrant       = FormsRegistrants::create([
  414.                     'discount_code'               => $discountApplying ? ($request->input('discount_code') ? : null) : null,
  415.                     'user_id'                     => $register_by_organiser ? \Auth::id() : null,
  416.                     'form_id'                     => $participant['form'],
  417.                     'registrant_id'               => $registrant->id,
  418.                     'forms_registrant_id'         => $forms_registrant_id,
  419.                     'registrant_comment'          => (isset($participant['comments'])) ? $participant['comments'] : '',
  420.                     'team_id'                     => $team_id,
  421.                     'forms_registrants_status_id' => 1,
  422.                     'registrant_number'           => $register_by_organiser || $nothing_to_pay ? $next_registrant_number : null,
  423.                     'individual_price'            => (! in_array($parent_id,
  424.                         $alreadyMarkedAsPaid) ? $individual_price : null),
  425.                     'unique_group'                => $unique_group,
  426.                     'form_group'                  => $parent_id,
  427.                     'paid_amount'                 => $register_by_organiser || $nothing_to_pay ? ($first_registrant ? $request->total_price : null) : null,
  428.                     'paid_fee'                    => $paid_fee,
  429.                     'testing_mode'                => $event->testing_mode,
  430.                     'invoice_data'                => json_decode($request->input('invoice')),
  431.                     'cart_id'                     => $cart->id,
  432.                 ]);
  433.                 $first_registrant      = false;
  434.                 $alreadyMarkedAsPaid[] = $parent_id;
  435.  
  436.                 if (is_null($forms_registrant_id)) {
  437.                     $forms_registrant_id = $form_registrant->id;
  438.                 }
  439.  
  440.                 if (! empty($participant['challengeGroup'])) {
  441.                     $group_id = Registrants::saveRegistrantGroups($participant, $event);
  442.                     $form_registrant->update(['group_id' => $group_id]);
  443.                 }
  444.  
  445.                 $added = [];
  446.                 if (! empty($files[$participant_id])) {
  447.                     foreach ($files[$participant_id] as $field_name => $filename) {
  448.                         $document = [
  449.                             'registrant_id' => $registrant->id,
  450.                             'filename'      => $filename,
  451.                         ];
  452.                         $type     = 2; // Licence
  453.                         if (isset($participant['medical-license-button']) && $participant['medical-license-button'] == 1) {
  454.                             $type = 1;
  455.                         } // Medical
  456.                         if ($field_name == 'parental_uploaded_file') {
  457.                             $type = 3;
  458.                         } // Parental
  459.                         $document['registrants_documents_type_id'] = $type;
  460.                         $document_field                            = 'parental_document_id';
  461.                         $document_status_field                     = 'parental_status_id';
  462.                         $document_status_id                        = ParentalStatuses::whereTitle('en attente de validation')->pluck('id');
  463.                         if ($type == 2) {
  464.                             $document['licence_type_id'] = \Arr::get($participant, 'licence_type_id');
  465.                             $document['licence_number']  = \Arr::get($participant, 'licence_number');
  466.                             $document['club_name']       = \Arr::get($participant, 'club_name');
  467.                             $document['club_number']     = \Arr::get($participant, 'club_number');
  468.                             $document['document_date']   = \Arr::get($participant, 'licence_date');
  469.                             $document_field              = 'medical_or_licence_document_id';
  470.                             $document_status_field       = 'medical_or_licence_status_id';
  471.                             $document_status_id          = MedicalStatuses::whereTitle('en attente de validation')->pluck('id');
  472.                         }
  473.                         if ($type == 1) {
  474.                             if (isset($participant['medical_certificate_date'])) {
  475.                                 $document['document_date'] = $participant['medical_certificate_date'];
  476.                             }
  477.                             $document_field            = 'medical_or_licence_document_id';
  478.                             $document_status_field     = 'medical_or_licence_status_id';
  479.                             $document_status_id        = MedicalStatuses::whereTitle('en attente de validation')->pluck('id');
  480.                         }
  481.                         $registrant_document = RegistrantsDocuments::create($document);
  482.                         $form_registrant->update([
  483.                             $document_field        => $registrant_document->id,
  484.                             $document_status_field => $document_status_id
  485.                         ]);
  486.                         $added[] = $document_status_field;
  487.                     }
  488.                 } elseif ((isset($participant['licence_type_id']) && $participant['licence_type_id'] != '' && $participant['licence_type_id'] > 0) ||
  489.                     (isset($participant['licence_number']) && $participant['licence_number'] != '')
  490.                 ) {
  491.                     $document                    = [
  492.                         'registrant_id'                 => $registrant->id,
  493.                         'filename'                      => '',
  494.                         'registrants_documents_type_id' => 2,
  495.                     ];
  496.                     $document['licence_type_id'] = \Arr::get($participant, 'licence_type_id');
  497.                     $document['licence_number']  = \Arr::get($participant, 'licence_number');
  498.                     $document['club_name']       = \Arr::get($participant, 'club_name');
  499.                     $document['club_number']     = \Arr::get($participant, 'club_number');
  500.                     $document['document_date']   = \Arr::get($participant, 'licence_date');
  501.                     $document_field              = 'medical_or_licence_document_id';
  502.                     $registrant_document         = RegistrantsDocuments::create($document);
  503.                     $form_registrant->update([
  504.                         $document_field => $registrant_document->id
  505.                     ]);
  506.                 }
  507.  
  508.                 if (! in_array('medical_or_licence_status_id', $added)) {
  509.                     $form_registrant->medical_or_licence_status_id = 1;
  510.                 }
  511.                 if (! in_array('parental_status_id', $added)) {
  512.                     $mainForm = (! $form_registrant->form->form_id ? $form_registrant->form : $form_registrant->form->parent_form);
  513.                     if ($form_registrant->registrant->is_18_at_start || ! $mainForm->parental_required) {
  514.                         $form_registrant->parental_status_id = 11;
  515.                     } else {
  516.                         $form_registrant->parental_status_id = 5;
  517.                     }
  518.                 }
  519.                 $form_registrant->save();
  520.  
  521.                 //save form values
  522.                 foreach ($participant as $key => $value) {
  523.                     if (substr($key, 0, 7) == 'option_' && $value != '' && $value != '0') {
  524.                         $option        = substr($key, 7);
  525.                         $forms_fields  = FormsFields::where('form_id', $participant['form'])->get();
  526.                         $form_field_id = null;
  527.                         $field_type_id = null;
  528.                         $forms_fields->each(function (FormsFields $formsField) use (
  529.                             $option,
  530.                             &$form_field_id,
  531.                             &$field_type_id
  532.                         ) {
  533.                             if ($formsField->field_type && $formsField->field_type->title == $option) {
  534.                                 $field_type_id = $formsField->field_type->id;
  535.                                 $form_field_id = $formsField->id;
  536.                             }
  537.                             if ($formsField->fieldGroup) {
  538.                                 foreach ($formsField->fieldGroup->fields as $field) {
  539.                                     if ($field->title == $option) {
  540.                                         $field_type_id = $field->id;
  541.                                         $form_field_id = $formsField->id;
  542.                                     }
  543.                                 }
  544.                             }
  545.                         });
  546.  
  547.                         if ($form_field_id && $field_type_id) {
  548.                             $forms_field = FormsFields::find($form_field_id);
  549.                             FormsRegistrantsFields::create([
  550.                                 'forms_registrant_id' => $form_registrant->id,
  551.                                 'forms_field_id'      => $form_field_id,
  552.                                 'field_type_id'       => $field_type_id,
  553.                                 'field_value'         => $value,
  554.                                 'price'               => $forms_field->getPriceBySelectedValue($value) ? : 0
  555.                             ]);
  556.                         }
  557.                     }
  558.                 }
  559.  
  560.                 //save customizable fields
  561.                 foreach ($participant as $key => $value) {
  562.                     if (preg_match('/^customizable_/', $key) && $value != '' && $value != '0') {
  563.                         //get field id (last number in title)
  564.                         $form_field_id = preg_replace('/^customizable_(?:.*?)([0-9]+)/', '$1', $key);
  565.                         $forms_field   = FormsFields::whereFormId($participant['form'])->find($form_field_id);
  566.                         //if field found, create form registrant field with data
  567.                         if ($forms_field) {
  568.                             FormsRegistrantsFields::create([
  569.                                 'forms_registrant_id' => $form_registrant->id,
  570.                                 'forms_field_id'      => $forms_field->id,
  571.                                 'field_type_id'       => $forms_field->field_type_id,
  572.                                 'field_value'         => $value,
  573.                                 'price'               => $forms_field->getPriceBySelectedValue($value) ? : 0
  574.                             ]);
  575.                         }
  576.                     }
  577.                 }
  578.                 //check if this is child form
  579.                 if (strstr($participant_id, '_')) {
  580.                     //yes, it is, check if we can get parent form data
  581.                     $parent_data = Arr::get($request->participant, explode('_', $participant_id)[0]);
  582.                     if ($parent_data) {
  583.                         foreach ($parent_data as $key => $value) {
  584.                             //we need to save each parent forms data as registrant own data
  585.                             if (preg_match('/^customizable_/', $key) && $value != '' && $value != '0') {
  586.                                 //get field id (last number in title)
  587.                                 $form_field_id = preg_replace('/^customizable_(?:.*?)([0-9]+)/', '$1', $key);
  588.                                 $forms_field   = FormsFields::whereFormId($parent_data['form'])->find($form_field_id);
  589.                                 //if field found, create form registrant field with data
  590.                                 if ($forms_field) {
  591.                                     FormsRegistrantsFields::create([
  592.                                         'forms_registrant_id' => $form_registrant->id,
  593.                                         'forms_field_id'      => $forms_field->id,
  594.                                         'field_type_id'       => $forms_field->field_type_id,
  595.                                         'field_value'         => $value,
  596.                                         'price'               => $forms_field->getPriceBySelectedValue($value) ? : 0
  597.                                     ]);
  598.                                 }
  599.                             }
  600.                         }
  601.                     }
  602.                 }
  603.  
  604.                 //save main form values like "north challenge" for each participant
  605.                 $index = explode('_', $participant_id);
  606.                 $index = count($index) > 1 ? $index[0] : 'nonexistingkey';
  607.                 foreach ($request->input('participant.' . $index, []) as $key => $value) {
  608.                     if (substr($key, 0, 7) == 'option_' && $value != '' && $value != '0') {
  609.                         $option      = substr($key, 7);
  610.                         $forms_field = FormsFields::select('forms_fields.id', 'forms_fields.price')
  611.                             ->join('forms_fields_types', 'forms_fields.field_type_id', '=', 'forms_fields_types.id')
  612.                             ->where('forms_fields.form_id', $request->input("participant.{$index}.form", 0))
  613.                             ->where('forms_fields_types.title', $option)
  614.                             ->first();
  615.                         if ($forms_field) {
  616.                             FormsRegistrantsFields::create([
  617.                                 'forms_registrant_id' => $form_registrant->id,
  618.                                 'forms_field_id'      => $forms_field->id,
  619.                                 'field_value'         => $value,
  620.                                 'price'               => $forms_field->getPriceBySelectedValue($value) ? : 0
  621.                             ]);
  622.                         }
  623.                     }
  624.                 }
  625.             }
  626.         }
  627.  
  628.         if ($register_by_organiser) {
  629.             $order_number = Transactions::join('forms_registrants', 'transactions.forms_registrant_id', '=',
  630.                 'forms_registrants.id')
  631.                 ->join('forms', 'forms_registrants.form_id', '=', 'forms.id')
  632.                 ->join('forms_groups', 'forms.forms_group_id', '=', 'forms_groups.id')
  633.                 ->where('forms_groups.event_id', $event->id)
  634.                 ->max('transactions.order_number');
  635.             if (is_null($order_number)) {
  636.                 $order_number = 0;
  637.             }
  638.             $order_number++;
  639.  
  640.             $paid_fee = $request->input('event_fee', 0);
  641.             $amount = ($request->has('payment_amount') ? $request->input('payment_amount') : $request->total_price + $paid_fee);
  642.             if ($payment_method_id == 2) {
  643.                 $paid_fee = 0;
  644.                 $amount = $request->total_price;
  645.             }
  646.             Transactions::create([
  647.                 'registrant_id'       => $registrant_id,
  648.                 'forms_registrant_id' => $forms_registrant_id,
  649.                 'user_id'             => (\Auth::check() ? \Auth::user()->id : null),
  650.                 'amount'              => $amount,
  651.                 'currency_id'         => $event->organisation->currency_id,
  652.                 'payment_method_id'   => $payment_method_id,
  653.                 'order_number'        => $order_number,
  654.                 'info'                => 'Inscriptions',
  655.                 'comments'            => ($request->has('payment_comments') ? $request->input('payment_comments') : null)
  656.             ]);
  657.  
  658.             $registrants = FormsRegistrants::where('unique_group', $unique_group)->get();
  659.             $details     = OrderDetails::generateForFormRegistrant($registrants->first());
  660.             /** @var FormsRegistrants $item */
  661.             foreach ($registrants as $item) {
  662.                 $item->registrant->orderDetails()->associate($details)->save();
  663.             }
  664.  
  665.             return redirect()->route('user.registrants')->withMessage(trans('event.admin.newParticipantAdded'));
  666.         }
  667.  
  668.         if ($request->input('payment_type_hidden') == '2') {
  669.             return view('payment_cheque');
  670.         }
  671.  
  672.         if ($nothing_to_pay) {
  673.             $order_number = Transactions::join('forms_registrants', 'transactions.forms_registrant_id', '=',
  674.                 'forms_registrants.id')
  675.                 ->join('forms', 'forms_registrants.form_id', '=', 'forms.id')
  676.                 ->join('forms_groups', 'forms.forms_group_id', '=', 'forms_groups.id')
  677.                 ->where('forms_groups.event_id', $event->id)
  678.                 ->max('transactions.order_number');
  679.             if (is_null($order_number)) {
  680.                 $order_number = 0;
  681.             }
  682.             $order_number++;
  683.  
  684.             if (!is_null($registrant_id)) {
  685.                 Transactions::create([
  686.                     'registrant_id' => $registrant_id,
  687.                     'forms_registrant_id' => $forms_registrant_id,
  688.                     'user_id' => (\Auth::check() ? \Auth::user()->id : null),
  689.                     'amount' => 0,
  690.                     'currency_id' => $event->organisation->currency_id,
  691.                     'payment_method_id' => null,
  692.                     'order_number' => $order_number,
  693.                     'info' => 'Inscriptions',
  694.                 ]);
  695.             }
  696.  
  697.             // Send confirmation email
  698.  
  699.             /** @var \App\Events $event */
  700.             $registrants  = FormsRegistrants::where('unique_group', $unique_group)->get();
  701.             $uniqueEmails = array_unique(array_keys($emails));
  702.             foreach ($uniqueEmails as $email) {
  703.                 $event->sendSuccessEmail($email, $registrants);
  704.             }
  705.  
  706.             // @todo Duplicate code
  707.             $registrant_number = FormsRegistrants::join('forms', 'forms_registrants.form_id', '=',
  708.                 'forms.id')
  709.                 ->join('forms_groups', 'forms.forms_group_id', '=', 'forms_groups.id')
  710.                 ->where('forms_groups.event_id', $event->id)
  711.                 ->where('forms_registrants.unique_group', '<>', $unique_group)
  712.                 ->max('forms_registrants.registrant_number');
  713.             if (is_null($registrant_number)) {
  714.                 $registrant_number = 0;
  715.             }
  716.             $registrant_number++;
  717.  
  718.             foreach ($registrants as $row) {
  719.                 $row->update(['registrant_number' => $registrant_number++]);
  720.  
  721.                 if ($row->discount_code) {
  722.                     $row->update(['discount_used' => 1]);
  723.                 }
  724.             }
  725.  
  726.             return redirect()->route('eventSuccess', [$event->slug, 'event_id' => $event->id]);
  727.         }
  728.  
  729.         $cle = 'F54D5EA925D2E3392F497B3F966F71A2939B2A94';
  730.         $tpe = '6389328';
  731.         try {
  732.             $oTpe      = new CMCIC_Tpe('FR', $cle, $tpe);
  733.             $oHmac     = new CMCIC_Hmac($oTpe);
  734.             $form_data = [
  735.                 'tpe'         => $tpe,
  736.                 'date'        => date('d/m/Y:H:i:s'),
  737.                 'price'       => $request->total_price,
  738.                 'currency'    => $event->organisation->currency->code,
  739.                 'reference'   => $registrant_id . '-A2',
  740.                 'event_id'    => $event->id,
  741.                 'buyer_email' => $registrant_email,
  742.             ];
  743.  
  744.             $PHP1_FIELDS = sprintf(CMCIC_CGI1_FIELDS,
  745.                 $form_data['tpe'],
  746.                 $form_data['date'],
  747.                 $form_data['price'],
  748.                 $event->organisation->currency->code,
  749.                 $form_data['reference'],
  750.                 "",
  751.                 "3.0",
  752.                 "FR",
  753.                 "kiadeo",
  754.                 $form_data['buyer_email'],
  755.                 "",
  756.                 "",
  757.                 "",
  758.                 "",
  759.                 "",
  760.                 "",
  761.                 "",
  762.                 "",
  763.                 "",
  764.                 "");
  765.  
  766.             $form_data['MAC'] = $oHmac->computeHmac($PHP1_FIELDS);
  767.         } catch (\Exception $ex) {
  768.             Log::error($ex->getMessage());
  769.         }
  770.  
  771.         return view('payment_form', compact('event', 'form_data'));
  772.         // return view('payment_success', compact('event'));
  773.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement