149 lines
5.5 KiB
PHP
149 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Mail\CourrierArcadiaError;
|
|
use App\Models\Participation;
|
|
use App\Models\Settings;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
class WinnersListController extends Controller
|
|
{
|
|
/**
|
|
* crm Api
|
|
*
|
|
* @var \LDCCRM\ApiV1
|
|
*/
|
|
private $apiCrm;
|
|
|
|
/**
|
|
* WinnersListController
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->apiCrm = new \LDCCRM\ApiV1(config('crm.endpoint'), config('crm.token'));
|
|
}
|
|
|
|
/**
|
|
* Show Instant win list
|
|
*
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
|
*/
|
|
public function index()
|
|
{
|
|
$winnersList = Participation::has('instantWin')->get();
|
|
$settings = Settings::select('use_ean_codes', 'winning_proof')->first();
|
|
return view('bo.winners-list.index', compact('winnersList', 'settings'));
|
|
}
|
|
|
|
/**
|
|
* Crete and show courrier arcadia
|
|
*
|
|
* @param mixed $id
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
|
*/
|
|
public function courrierArcadia($id)
|
|
{
|
|
/** @var \App\Models\Participation $participation */
|
|
$participation = Participation::findOrFail($id);
|
|
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer = $participation->customer;
|
|
|
|
$crmUser = $this->apiCrm->getCustomer($customer->crm_id);
|
|
if ($crmUser->status === 'NOK') {
|
|
return back()->withErrors("Erreur lors de la récupération de l'utilisateur dans la CRM : Code : " .
|
|
$crmUser->code . " Message : " . $crmUser->message);
|
|
}
|
|
|
|
$crmId = $customer->crm_id;
|
|
|
|
$crmUser = $crmUser->user;
|
|
|
|
$corps = '<arcadia>' . "\n";
|
|
$corps .= '<Commande> ' . "\n";
|
|
$corps .= "\t" . '<Num_Commande></Num_Commande>' . "\n";
|
|
$corps .= "\t" . '<Date_Commande>' . date('d/m/Y') . '</Date_Commande>' . "\n";
|
|
$corps .= "\t" . '<Operation></Operation> ' . "\n";
|
|
$corps .= "\t" . '<Civilite></Civilite>' . "\n";
|
|
$corps .= "\t" . '<Nom><![CDATA[' . trim($crmUser->firstname) . ']]></Nom>' . "\n";
|
|
$corps .= "\t" . '<Prenom><![CDATA[' . trim($crmUser->lastname) . ']]></Prenom>' . "\n";
|
|
$corps .= "\t" . '<Adresse><![CDATA[' . trim($crmUser->address) . ']]></Adresse>' . "\n";
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
$corps .= "\t" . '<Adresse_Complementaire><![CDATA[' . trim($crmUser->address2) . ']]></Adresse_Complementaire>' . "\n";
|
|
$corps .= "\t" . '<CP><![CDATA[' . trim($crmUser->postcode) . ']]></CP>' . "\n";
|
|
$corps .= "\t" . '<Ville><![CDATA[' . trim($crmUser->city) . ']]></Ville>' . "\n";
|
|
$corps .= '<Ligne_de_Commande>' . "\n";
|
|
$corps .= "\t" . '<Code_Support></Code_Support> ' . "\n";
|
|
$corps .= "\t" . '<Libelle_Type_Support></Libelle_Type_Support> ' . "\n";
|
|
$corps .= "\t" . '<Quantite>1</Quantite>' . "\n";
|
|
$corps .= "\t" . '<Poids_Unitaire_Colis></Poids_Unitaire_Colis>' . "\n";
|
|
$corps .= '</Ligne_de_Commande>' . "\n";
|
|
$corps .= '</Commande> ' . "\n";
|
|
$corps .= '</arcadia>';
|
|
|
|
return view('bo.winners-list.arcadia', compact('participation', 'corps', 'crmId'));
|
|
}
|
|
|
|
/**
|
|
* Send Arcadia mail
|
|
*
|
|
* @param mixed $id
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function createCourrierArcadia($id)
|
|
{
|
|
$participation = Participation::findOrFail($id);
|
|
$instantWin = $participation->instantWin;
|
|
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer = $participation->customer;
|
|
|
|
if ($instantWin->prize->arcadia_operation_code !== 0 && $instantWin->prize->arcadia_support_code !== 0) {
|
|
$fields = [
|
|
'root' => [
|
|
[
|
|
'fieldName' => 'order_date',
|
|
'fieldValue' => date('d/m/Y')
|
|
],
|
|
[
|
|
'fieldName' => 'operation',
|
|
'fieldValue' => $instantWin->prize->arcadia_operation_code
|
|
]
|
|
],
|
|
'orderLine' => [
|
|
[
|
|
'fieldName' => 'code_unique',
|
|
'fieldValue' => $participation->code->code_unique
|
|
],
|
|
[
|
|
'fieldName' => 'support_code',
|
|
'fieldValue' => $instantWin->prize->arcadia_support_code
|
|
],
|
|
[
|
|
'fieldName' => 'label_type_support',
|
|
'fieldValue' => 'Gain du lot : ' . $instantWin->prize->label_one
|
|
]
|
|
]
|
|
];
|
|
|
|
/** @var \App\Models\Settings $settings */
|
|
$settings = app(Settings::class);
|
|
if ($settings->use_ean_codes) {
|
|
$fields['orderLine'][] = [
|
|
'fieldName' => 'code_on_pack',
|
|
'fieldValue' => $participation->codeEan->code
|
|
];
|
|
}
|
|
|
|
$arcadiaMail = $this->apiCrm->ArcadiaOutgoingMail($customer->crm_id, $fields, 'game');
|
|
if ($arcadiaMail->status === 'NOK') {
|
|
Mail::to(config('mail.MAIL_ADMIN'))->send(new CourrierArcadiaError($customer));
|
|
return back()->withErrors("Erreur lors de l'envoi du courrier Arcadia. Code : " .
|
|
$arcadiaMail->code . " Message : " . $arcadiaMail->message);
|
|
}
|
|
}//end if
|
|
return back()->with('success', 'Courrier envoyé !');
|
|
}
|
|
}
|