2020-10-30 16:12:14 +01:00
< ? php
namespace App\Http\Controllers ;
2021-02-23 18:27:03 +01:00
use App\Mail\NotifAdmin ;
2020-11-02 16:18:06 +01:00
use App\Mail\WinningProof ;
2020-11-04 11:49:43 +01:00
use App\Models\Participation ;
2020-11-02 16:18:06 +01:00
use App\Models\Settings ;
2020-11-09 15:15:18 +01:00
use App\User ;
2020-10-30 16:12:14 +01:00
use Illuminate\Http\Request ;
2020-11-04 11:49:43 +01:00
use Illuminate\Support\Facades\Auth ;
2020-11-20 15:18:31 +01:00
use Illuminate\Support\Facades\Log ;
2020-11-02 16:18:06 +01:00
use Illuminate\Support\Facades\Mail ;
2020-10-30 16:12:14 +01:00
class WinningProofController extends Controller
{
2021-08-18 17:08:02 +02:00
2021-02-23 18:27:03 +01:00
private $apiCrm ;
2021-08-18 17:08:02 +02:00
public function __construct ()
{
2021-02-23 18:27:03 +01:00
$this -> apiCrm = new \LDCCRM\ApiV1 ( config ( 'crm.endpoint' ), config ( 'crm.token' ));
}
2021-08-18 17:08:02 +02:00
public function sendWinningProof ( Request $request )
{
2020-11-04 11:49:43 +01:00
// On récupère la participation gagnante en attente des infos
$participation = Participation :: where ([
[ 'user_id' , Auth :: user () -> id ],
[ 'instant_win_id' , '<>' , null ],
[ 'sticker' , null ],
[ 'ticket' , null ],
[ 'barcode' , null ]
]) -> first ();
2021-08-18 17:08:02 +02:00
if ( ! $participation ) {
2020-11-04 11:49:43 +01:00
return redirect () -> route ( 'home' );
2021-08-18 17:08:02 +02:00
}
2020-11-04 11:49:43 +01:00
2020-11-23 11:22:42 +01:00
$request -> validate ([
'lastname' => 'required|string|max:255' ,
'firstname' => 'required|string|max:255' ,
'address' => 'required|string|max:255' ,
'address2' => 'max:255' ,
'town' => 'required|string|max:255' ,
'postal_code' => 'required|max:255' ,
'country' => 'required|string|max:255' ,
'phone' => 'required|string|max:50' ,
'sticker' => 'required|mimes:jpeg,png,jpg,pdf|max:2048' ,
'ticket' => 'required|mimes:jpeg,png,jpg,pdf|max:2048' ,
'barcode' => 'required|mimes:jpeg,png,jpg,pdf|max:2048' ,
]);
2021-08-18 17:08:02 +02:00
$lastname = filter_var ( $request -> lastname , FILTER_SANITIZE_STRING );
$firstname = filter_var ( $request -> firstname , FILTER_SANITIZE_STRING );
$address = filter_var ( $request -> address , FILTER_SANITIZE_STRING );
$address2 = $request -> address2 !== null ? filter_var ( $request -> address2 , FILTER_SANITIZE_STRING ) : null ;
$town = filter_var ( $request -> town , FILTER_SANITIZE_STRING );
2020-10-30 16:12:14 +01:00
$postalCode = filter_var ( $request -> postal_code , FILTER_SANITIZE_STRING );
2021-08-18 17:08:02 +02:00
$country = filter_var ( $request -> country , FILTER_SANITIZE_STRING );
$phone = filter_var ( $request -> phone , FILTER_SANITIZE_STRING );
$sticker = $request -> sticker ;
$ticket = $request -> ticket ;
$barcode = $request -> barcode ;
2020-10-30 16:12:14 +01:00
2020-11-20 15:18:31 +01:00
// Mise à jour de l'utilisateur en local
2021-08-18 17:08:02 +02:00
$user = User :: find ( Auth :: user () -> id );
$user -> lastname = $lastname ;
$user -> firstname = $firstname ;
$user -> address = $address ;
$user -> address2 = $address2 ;
$user -> town = $town ;
2020-11-09 15:15:18 +01:00
$user -> postal_code = $postalCode ;
2021-08-18 17:08:02 +02:00
$user -> country = $country ;
$user -> phone = $phone ;
2020-11-09 15:15:18 +01:00
$user -> save ();
2020-11-20 15:18:31 +01:00
// Mise à jour utilisateur dans la CRM
2021-02-23 18:27:03 +01:00
$update = $this -> apiCrm -> updateCustomer ( $user -> crm_id , $firstname , $lastname , null , null , $phone , $address , $address2 , $town , $postalCode , ucfirst ( $country ));
2021-08-18 17:08:02 +02:00
if ( $update -> status !== 'OK' ) {
Log :: warning ( 'Erreur lors de la mise a jour du user (crm_id : ' . $user -> crm_id . ') - code : ' . $update -> code . ' - message : ' . $update -> message );
Mail :: to ( config ( 'mail.MAIL_ADMIN' )) -> send ( new NotifAdmin ( 'CRM : Erreur lors de la mise a jour du user (crm_id : ' . $user -> crm_id . ') - code : ' . $update -> code . ' - message : ' . $update -> message ));
2020-11-20 15:18:31 +01:00
}
2020-11-04 13:51:22 +01:00
$participationId = $participation -> id ;
2020-10-30 16:12:14 +01:00
$stickerFileName = $participationId . '-sticker.' . $sticker -> extension ();
$sticker -> move ( public_path ( 'fichiers/preuve-de-gain/' . $participationId . '/' ), $stickerFileName );
2020-11-12 17:22:54 +01:00
$stickerPath = public_path () . '/fichiers/preuve-de-gain/' . $participationId . '/' . $stickerFileName ;
2020-10-30 16:12:14 +01:00
$ticketFileName = $participationId . '-ticket.' . $ticket -> extension ();
$ticket -> move ( public_path ( 'fichiers/preuve-de-gain/' . $participationId . '/' ), $ticketFileName );
2020-11-12 17:22:54 +01:00
$ticketPath = public_path () . '/fichiers/preuve-de-gain/' . $participationId . '/' . $ticketFileName ;
2020-10-30 16:12:14 +01:00
$barcodeFileName = $participationId . '-code-barre.' . $barcode -> extension ();
$barcode -> move ( public_path ( 'fichiers/preuve-de-gain/' . $participationId . '/' ), $barcodeFileName );
2020-11-12 17:22:54 +01:00
$barcodePath = public_path () . '/fichiers/preuve-de-gain/' . $participationId . '/' . $barcodeFileName ;
2020-11-02 16:18:06 +01:00
2020-11-04 11:49:43 +01:00
$participation -> sticker = $stickerFileName ;
2021-08-18 17:08:02 +02:00
$participation -> ticket = $ticketFileName ;
2020-11-04 11:49:43 +01:00
$participation -> barcode = $barcodeFileName ;
$participation -> save ();
2020-11-02 16:18:06 +01:00
$settings = Settings :: find ( 1 );
2021-08-18 17:08:02 +02:00
$mails = json_decode ( $settings -> notification_emails );
2020-11-02 16:18:06 +01:00
foreach ( $mails as $mail ) {
2020-11-13 18:41:09 +01:00
Mail :: to ( $mail -> email ) -> send ( new WinningProof ( $stickerPath , $ticketPath , $barcodePath , $participation , $user , $settings ));
2020-11-02 16:18:06 +01:00
}
2020-10-30 16:12:14 +01:00
return view ( 'front.basic.replay' );
}
}