115 lines
4.7 KiB
PHP
115 lines
4.7 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\Http\Request;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
class WinnersListController extends Controller
|
|
{
|
|
|
|
private $apiCrm;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->apiCrm = new \LDCCRM\ApiV1(config('crm.endpoint'), config('crm.token'));
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$winnersList = Participation::where('instant_win_id', '<>', null)->get();
|
|
$settings = Settings::select('use_ean_codes', 'winning_proof')->first();
|
|
return view('bo.winners-list.index', compact('winnersList', 'settings'));
|
|
}
|
|
|
|
public function courrierArcadia($id)
|
|
{
|
|
$participation = Participation::findOrFail($id);
|
|
|
|
$crmUser = $this->apiCrm->getCustomer($participation->user->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 = $participation->user->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";
|
|
$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'));
|
|
}
|
|
|
|
public function createCourrierArcadia(Request $request, $id)
|
|
{
|
|
$participation = Participation::findOrFail($id);
|
|
$instantWin = $participation->instant_win;
|
|
|
|
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
|
|
]
|
|
]
|
|
];
|
|
|
|
$settings = app(Settings::class);
|
|
if ($settings->use_ean_codes === 1) {
|
|
$fields['orderLine'][] = [
|
|
'fieldName' => 'code_on_pack',
|
|
'fieldValue' => $participation->code_ean->code
|
|
];
|
|
}
|
|
|
|
$arcadiaMail = $this->apiCrm->ArcadiaOutgoingMail($participation->user->crm_id, $fields, 'game');
|
|
if ($arcadiaMail->status === 'NOK') {
|
|
Mail::to(config('mail.MAIL_ADMIN'))->send(new CourrierArcadiaError($participation->user));
|
|
return back()->withErrors("Erreur lors de l'envoi du courrier Arcadia. Code : " . $arcadiaMail->code . " Message : " . $arcadiaMail->message);
|
|
}
|
|
}
|
|
return back()->with('success', 'Courrier envoyé !');
|
|
}
|
|
}
|