apiCrm = new \LDCCRM\ApiV1(config('crm.endpoint'), config('crm.token')); } /** * Show enter code form * * @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory */ public function index() { /** @var \App\Models\Customer|null $customer */ $customer = auth('customer')->user(); // * Only auth customer can access if (!auth('customer')->check()) { return redirect()->route('home'); } $nbParticipations = Participation::whereHas('customer', function ($q) use ($customer) { $q->whereId($customer->id); })->count(); $settings = app(Settings::class)->select('use_ean_codes', 'nb_participation_player')->first(); if ($nbParticipations >= $settings->nb_participation_player) { return view('front.basic.participation-exhausted', compact('settings')); } return view('front.basic.enter-code', compact('settings')); } /** * Participate Method * * @param ParticipateRequest $request * @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory */ public function participate(ParticipateRequest $request) { // Récupération des données à utilisées. /** @var \App\Models\Settings $settings */ $settings = app(Settings::class); /** @var \App\Models\Customer $customer */ $customer = auth('customer')->user(); // * Cas IP bannie. $ipBanned = BannedIp::where('ip', app('clientIp'))->first() === null ? 0 : 1; if ($ipBanned) { return view('front.basic.participation-exhausted', compact('settings')); } // * Cas nombre de participation maximale atteint. $nbParticipations = Participation::whereHas('customer', function ($q) use ($customer) { $q->whereId($customer->id); })->count(); if ($nbParticipations >= $settings->nb_participation_player) { return view('front.basic.participation-exhausted', compact('settings')); } // * Tous les cas "bloquants" sont passé, l'utilisateur participe à partir d'ici. $participation = new Participation(); $participation->code_unique_id = $request->code->id; $participation->code_ean_id = $request->code_ean ? $request->code_ean->id : null; $participation->ip = app('clientIp'); $participation->customer()->associate($customer); $participation->save(); // * Gestion des instants gagnants. $userAlreadyWon = Participation::has('instantWin') ->whereHas('customer', function ($q) use ($customer) { $q->whereId($customer->id); })->first(); // * Si l'utilisateur actuel à déjà gagné un gros lot, pas d'instant gagnant. if ($userAlreadyWon) { return $this->serveCouponWin($settings, $participation, $customer); } $instantWin = InstantWin::where([['appear_date', '<', now()], ['won', 0]]) ->orderBy('appear_date', 'ASC')->first(); if ($instantWin) { // Ici si fail car unique dupliqué catch. try { // Gain d'un gros lot. return $this->instantWin($settings, $request, $participation, $customer, $instantWin); } catch (\ErrorException $e) { // Unbind instant gagnant. $participation->instantWin()->delete(); // TODO: si erreur il faut envoyer un mail à equipe.web@ldc.fr. } //end try } return $this->serveCouponWin( $settings, $participation, $customer ); } /** * Handle instant wins * * @param Settings $settings * @param Request $request * @param Participation $participation * @param Customer $customer * @param InstantWin|null $instantWin * @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory */ private function instantWin( Settings $settings, Request $request, Participation &$participation, Customer $customer, $instantWin ) { $instantWin->won = true; // Should save now to prevent next player win on the same winning moment. $instantWin->save(); $participation->instantWin()->save($instantWin); // Envoi du courrier Arcadia. if ( $instantWin->prize->arcadia_operation_code !== 0 && $instantWin->prize->arcadia_support_code !== 0 ) { $crmId = $customer->crm_id; $fields = [ 'root' => [ [ 'fieldName' => 'order_date', 'fieldValue' => date('d/m/Y') ], [ 'fieldName' => 'operation', 'fieldValue' => $instantWin->prize->arcadia_operation_code ] ], 'orderLine' => [ [ 'fieldName' => 'code_unique', 'fieldValue' => $request->serial ], [ 'fieldName' => 'support_code', 'fieldValue' => $instantWin->prize->arcadia_support_code ], [ 'fieldName' => 'label_type_support', 'fieldValue' => 'Gain du lot : ' . $instantWin->prize->label_one ] ] ]; if ($settings->use_ean_codes) { $codeEan = $request->code_ean; $fields['orderLine'][] = [ 'fieldName' => 'code_on_pack', 'fieldValue' => "$codeEan" ]; } $arcadiaMail = $this->apiCrm->ArcadiaOutgoingMail($crmId, $fields, 'game'); if ($arcadiaMail->status === 'KO') { Mail::to(config('mail.MAIL_ADMIN'))->send(new CourrierArcadiaError($customer)); } } //end if Mail::to($customer->email)->send(new PrizeWon($customer, $instantWin->prize, $settings)); $mails = collect($settings->notification_emails); foreach ($mails as $mail) { Mail::to($mail)->send(new NotifWinAdmin($settings, $participation)); } $participation->save(); return view("front.basic." . $instantWin->prize->gain_view->file_name, compact('settings')) ->with('prize', $instantWin->prize); } /** * Détermine si un BR doit être servi * renvoi la vue de gain BR ou la vue "Perdu" * * @param Settings $settings * @param Participation $participation * @param Customer $customer * @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory */ private function serveCouponWin(Settings $settings, Participation $participation, Customer $customer) { // Si le jeu est 100% gagnant. if ($settings->reductions_activated) { if (!($coupon = $this->getRandomCoupon($settings))) { throw new \ErrorException('No web coupons are available'); } // ! FIXME: repair this !! $crmCoupon = $this->updateCoupon($settings, $coupon->web_coupon_identifiant, $participation, $customer); if (!$crmCoupon) { $participation->delete(); return back()->withErrors("Une erreur est survenue, merci de réessayez ultérieurement."); } Mail::to(auth('customer')->user()->email)->send(new ReductionWon($customer, $coupon, $settings, $crmCoupon)); return view('front.basic.reduction', compact('crmCoupon', 'coupon')); } else { return view('front.basic.loose'); } } /** * Get a random coupon * * @param Settings $settings * @return string|null */ private function getRandomCoupon(Settings $settings): ?string { $webCoupons = $settings->web_coupons; if (count($webCoupons) > 1) { $lots = []; foreach ($webCoupons as $webCoupon) { $lots[] = $webCoupon; } foreach ($lots as $lot) { for ($i = 0; $i < $lot->percentage; $i++) { $tmp[] = $lot; } } shuffle($tmp); return $tmp[0]; } else { return $webCoupons[0] ?? null; } } /** * Update a coupon * * @param Settings $settings * @param mixed $coupon * @param Participation $participation * @param Customer $customer * @return \App\Models\Reduction|null */ private function updateCoupon( Settings $settings, $coupon, Participation $participation, Customer $customer ) { $reduction = null; $mesBR = new MesBR(config('mesbr.endpoint'), $settings->reductions_site, $settings->reductions_campaign); $crmUser = $this->apiCrm->getCustomer($customer->crm_id); if ($crmUser->status === 'OK') { $mesBR->updateContact($crmUser->user); $result = $mesBR->updatePanier($crmUser->user, $coupon); if ($result["status"] === "OK") { $reduction = new Reduction(); $reduction->link = $result["link"]; $reduction->verification = $result["verif"]; $reduction->basket_id = $result["basketId"]; $reduction->save(); $participation->reduction_id = $reduction->id; $participation->save(); } else { Mail::to(config('mail.MAIL_ADMIN')) ->send(new NotifAdmin(\sprintf( 'MESBR : Une erreur est survenue. Code : %s Message : %s', $result["code"], $result["message"] ))); } } //end if return $reduction; } /** * Show Wining proof form * * @return \Illuminate\Http\Response */ public function formWinningProof() { /** @var \App\Models\Customer|null $customer */ $customer = auth('customer')->user(); // * Only auth customer can access if (!auth('customer')->check() or \get_class($customer) === Customer::class) { return redirect()->route('home'); } $participation = Participation::whereHas('customer', function ($q) use ($customer) { $q->whereId($customer->id); })->whereHas('instantWin')->where([ ['sticker', null], ['ticket', null], ['barcode', null] ])->first(); if ($participation) { return view('front.basic.winning-proof', compact('participation')); } return redirect()->route('home'); } }