<?php
namespace App\Controller\InscriptionSite;
use Symfony\Component\HttpFoundation\Request;
use Amp\Http\Client\Response;
use App\Controller\MailerController;
use App\Entity\Carte;
use App\Entity\EcheanceInscrit;
use App\Entity\Elearning;
use App\Entity\Inscrit;
use App\Form\InscritType;
use App\Repository\InscritRepository;
use App\Entity\Tier;
use App\Entity\JustifFourni;
use App\Entity\Reglement;
use App\Entity\Financement;
use App\Entity\Organisme;
use App\Form\TierType;
use App\Repository\TierRepository;
use App\Repository\OrganismeRepository;
use App\Repository\SiteRepository;
use App\Repository\FormationRepository;
use App\Repository\JustificatifRepository;
use App\Repository\MoyPaiementRepository;
use App\Repository\CondRegleRepository;
use App\Repository\SourceFinanceRepository;
use App\Repository\StatusRepository;
use App\Repository\StatusInscritRepository;
use App\Entity\Entreprise;
use App\Entity\ReglementEcheance;
use App\Entity\Session;
use App\Entity\UniteInscription;
use App\Form\EntrepriseType;
use App\Repository\DemandeRepository;
use App\Repository\EcheanceInscritRepository;
use App\Repository\ElearningRepository;
use App\Repository\EnqueteJ1Repository;
use App\Repository\EnqueteJ30Repository;
use App\Repository\EntrepriseRepository;
use App\Repository\ExamenRepository;
use App\Repository\FinancementRepository;
use App\Repository\CarteRepository;
use App\Repository\OptionCategorieInscritRepository;
use App\Repository\ReglementRepository;
use App\Repository\SessionRepository;
use App\Repository\TypeInscritRepository;
use App\Repository\UniteInscriptionRepository;
use App\Service\FileUploader;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request as HttpFoundationRequest;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
class ApiCarteController extends AbstractController
{
/**
* @Route("/api/carte", name="app_api_carte",methods={"GET"})
*/
public function carte(CarteRepository $carteRepository,$session=null){
$carte = [];
$carte["vtc"] =[] ;
$carte["tm"] =[] ;
$carte["t"] =[] ;
$carteTMT = $carteRepository->findBy(['site'=>2,'optioncategorie'=>4,'is_activate'=>true],['wording'=>'asc']);
$carteTMP = $carteRepository->findBy(['site'=>2,'optioncategorie'=>5,'is_activate'=>true],['wording'=>'asc']);
$carteTMTP = $carteRepository->findBy(['site'=>2,'optioncategorie'=>6,'is_activate'=>true],['wording'=>'asc']);
//vtc
$carteVTCT = $carteRepository->findBy(['site'=>3,'optioncategorie'=>1,'is_activate'=>true],['wording'=>'asc']);
$carteVTCP = $carteRepository->findBy(['site'=>3,'optioncategorie'=>2,'is_activate'=>true],['wording'=>'asc']);
$carteVTCTP = $carteRepository->findBy(['site'=>3,'optioncategorie'=>3,'is_activate'=>true],['wording'=>'asc']);
//taxi
$carteTI = $carteRepository->findBy(['site'=>1,'optioncategorie'=>7,'is_activate'=>true],['wording'=>'asc']);
$carteTC = $carteRepository->findBy(['site'=>1,'optioncategorie'=>8,'is_activate'=>true],['wording'=>'asc']);
$carteTTMP = $carteRepository->findBy(['site'=>1,'optioncategorie'=>9,'is_activate'=>true],['wording'=>'asc']);
$carteTVTCTAXI = $carteRepository->findBy(['site'=>1,'optioncategorie'=>10,'is_activate'=>true],['wording'=>'asc']);
//vtccontinu
$carteVTCC = $carteRepository->findBy(['site'=>3,'optioncategorie'=>11,'is_activate'=>true],['wording'=>'asc']);
//vtctpmr
$carteVTCTPMR = $carteRepository->findBy(['site'=>3,'optioncategorie'=>12,'is_activate'=>true],['wording'=>'asc']);
//preparation examen
$carteExamen = $carteRepository->findBy(['site'=>[3],'optioncategorie'=>14,'is_activate'=>true],['id'=>'asc']);
$carteExamenTaxi = $carteRepository->findBy(['site'=>[1],'optioncategorie'=>15,'is_activate'=>true],['id'=>'asc']);
$carteVTC["theoriepratique"] = [];
$carteVTC["theorie"] = [];
$carteVTC["pratique"] = [];
$carteVTC["continue"] = [];
$carteVTC["tpmr"] = [];
$carteTM["pratique"] = [];
$carteTM["theorie"] = [];
$carteTM["theoriepratique"] = [];
$carteT["fi"] = [];
$carteT["fc"] = [];
$carteT["mp"] = [];
$carteT["vtctaxi"] = [];
$carteE = [];
$carteETaxi = [];
foreach ($carteVTCT as $key => $value) {
$formations = [];
foreach ($value->getFormation() as $key => $formation) {
array_push($formations,[
"id"=>$formation->getId(),
"wording" => $formation->getWording(),
]);
}
array_push($carteVTC["theorie"],
[
"id" => $value->getId(),
"wording" => $value->getWording(),
"libellelibre" => $value->getLibellelibre(),
"prerequis" => $value->getPrerequis(),
"pricepersonal" => $value->getPricepersonal(),
"pricecompany" => $value->getPricecompany(),
"pricecpf" => $value->getPricecpf(),
"formation" => count($formations) > 0?$formations:[]
]);
}
foreach ($carteVTCP as $key => $value) {
$formations = [];
foreach ($value->getFormation() as $key => $formation) {
array_push($formations,[
"id"=>$formation->getId(),
"wording" => $formation->getWording(),
]);
}
array_push($carteVTC["pratique"],
[
"id" => $value->getId(),
"wording" => $value->getWording(),
"libellelibre" => $value->getLibellelibre(),
"pricepersonal" => $value->getPricepersonal(),
"pricecompany" => $value->getPricecompany(),
"pricecpf" => $value->getPricecpf(),
"formation" => count($formations) > 0?$formations:[]
]);
}
foreach ($carteVTCTP as $key => $value) {
$formations = [];
foreach ($value->getFormation() as $key => $formation) {
array_push($formations,[
"id"=>$formation->getId(),
"wording" => $formation->getWording(),
]);
}
array_push($carteVTC["theoriepratique"],
[
"id" => $value->getId(),
"wording" => $value->getWording(),
"libellelibre" => $value->getLibellelibre(),
"pricepersonal" => $value->getPricepersonal(),
"pricecompany" => $value->getPricecompany(),
"pricecpf" => $value->getPricecpf(),
"formation" => count($formations) > 0?$formations:[]
]);
}
foreach ($carteVTCC as $key => $value) {
$formations = [];
foreach ($value->getFormation() as $key => $formation) {
array_push($formations,[
"id"=>$formation->getId(),
"wording" => $formation->getWording(),
]);
}
array_push($carteVTC["continue"],
[
"id" => $value->getId(),
"wording" => $value->getWording(),
"libellelibre" => $value->getLibellelibre(),
"pricepersonal" => $value->getPricepersonal(),
"pricecompany" => $value->getPricecompany(),
"pricecpf" => $value->getPricecpf(),
"formation" => count($formations) > 0?$formations:[]
]);
}
foreach ($carteVTCTPMR as $key => $value) {
$formations = [];
foreach ($value->getFormation() as $key => $formation) {
array_push($formations,[
"id"=>$formation->getId(),
"wording" => $formation->getWording(),
]);
}
array_push($carteVTC["tpmr"],
[
"id" => $value->getId(),
"wording" => $value->getWording(),
"libellelibre" => $value->getLibellelibre(),
"pricepersonal" => $value->getPricepersonal(),
"pricecompany" => $value->getPricecompany(),
"pricecpf" => $value->getPricecpf(),
"formation" => count($formations) > 0?$formations:[]
]);
}
$carte["vtc"] =$carteVTC;
// fin vtc
foreach ($carteTMT as $key => $value) {
$formations = [];
foreach ($value->getFormation() as $key => $formation) {
array_push($formations,[
"id"=>$formation->getId(),
"wording" => $formation->getWording(),
]);
}
array_push($carteTM["theorie"],
[
"id" => $value->getId(),
"wording" => $value->getWording(),
"libellelibre" => $value->getLibellelibre(),
"pricepersonal" => $value->getPricepersonal(),
"pricecompany" => $value->getPricecompany(),
"pricecpf" => $value->getPricecpf(),
"formation" => count($formations) > 0?$formations:[]
]);
}
foreach ($carteTMP as $key => $value) {
$formations = [];
//dd($value);
foreach ($value->getFormation() as $key => $formation) {
array_push($formations,[
"id"=>$formation->getId(),
"wording" => $formation->getWording(),
]);
}
array_push($carteTM["pratique"],
[
"id" => $value->getId(),
"wording" => $value->getWording(),
"libellelibre" => $value->getLibellelibre(),
"pricepersonal" => $value->getPricepersonal(),
"pricecompany" => $value->getPricecompany(),
"pricecpf" => $value->getPricecpf(),
"formation" => count($formations) > 0?$formations:[]
]);
}
foreach ($carteTMTP as $key => $value) {
$formations = [];
foreach ($value->getFormation() as $key => $formation) {
array_push($formations,[
"id"=>$formation->getId(),
"wording" => $formation->getWording(),
]);
}
array_push($carteTM["theoriepratique"],
[
"id" => $value->getId(),
"wording" => $value->getWording(),
"libellelibre" => $value->getLibellelibre(),
"pricepersonal" => $value->getPricepersonal(),
"pricecompany" => $value->getPricecompany(),
"pricecpf" => $value->getPricecpf(),
"formation" => count($formations) > 0?$formations:[]
]);
}
$carte['tm']=$carteTM;
foreach ($carteTI as $key => $value) {
$formations = [];
foreach ($value->getFormation() as $key => $formation) {
array_push($formations,[
"id"=>$formation->getId(),
"wording" => $formation->getWording(),
]);
}
array_push($carteT["fi"],
[
"id" => $value->getId(),
"wording" => $value->getWording(),
"libellelibre" => $value->getLibellelibre(),
"pricepersonal" => $value->getPricepersonal(),
"pricecompany" => $value->getPricecompany(),
"pricecpf" => $value->getPricecpf(),
"formation" => count($formations) > 0?$formations:[]
]);
}
foreach ($carteTC as $key => $value) {
$formations = [];
foreach ($value->getFormation() as $key => $formation) {
array_push($formations,[
"id"=>$formation->getId(),
"wording" => $formation->getWording(),
]);
}
array_push($carteT["fc"],
[
"id" => $value->getId(),
"wording" => $value->getWording(),
"libellelibre" => $value->getLibellelibre(),
"pricepersonal" => $value->getPricepersonal(),
"pricecompany" => $value->getPricecompany(),
"pricecpf" => $value->getPricecpf(),
"formation" => count($formations) > 0?$formations:[]
]);
}
foreach ($carteTTMP as $key => $value) {
$formations = [];
foreach ($value->getFormation() as $key => $formation) {
array_push($formations,[
"id"=>$formation->getId(),
"wording" => $formation->getWording(),
]);
}
array_push($carteT["mp"],
[
"id" => $value->getId(),
"wording" => $value->getWording(),
"libellelibre" => $value->getLibellelibre(),
"pricepersonal" => $value->getPricepersonal(),
"pricecompany" => $value->getPricecompany(),
"pricecpf" => $value->getPricecpf(),
"formation" => count($formations) > 0?$formations:[]
]);
}
foreach ($carteTVTCTAXI as $key => $value) {
$formations = [];
foreach ($value->getFormation() as $key => $formation) {
array_push($formations,[
"id"=>$formation->getId(),
"wording" => $formation->getWording(),
]);
}
array_push($carteT["vtctaxi"],
[
"id" => $value->getId(),
"wording" => $value->getWording(),
"libellelibre" => $value->getLibellelibre(),
"pricepersonal" => $value->getPricepersonal(),
"pricecompany" => $value->getPricecompany(),
"pricecpf" => $value->getPricecpf(),
"formation" => count($formations) > 0?$formations:[]
]);
}
$carte["t"] = $carteT;
foreach ($carteExamen as $key => $value) {
$formations = [];
foreach ($value->getFormation() as $key => $formation) {
array_push($formations,[
"id"=>$formation->getId(),
"wording" => $formation->getWording(),
]);
}
array_push($carteE,
[
"id" => $value->getId(),
"wording" => $value->getWording(),
"libellelibre" => $value->getLibellelibre(),
"pricepersonal" => $value->getPricepersonal(),
"pricecompany" => $value->getPricecompany(),
"pricecpf" => $value->getPricecpf(),
"formation" => count($formations) > 0?$formations:[]
]);
}
$carte["examen"] = $carteE;
//examen taxi
foreach ($carteExamenTaxi as $key => $value) {
$formations = [];
foreach ($value->getFormation() as $key => $formation) {
array_push($formations,[
"id"=>$formation->getId(),
"wording" => $formation->getWording(),
]);
}
array_push($carteETaxi,
[
"id" => $value->getId(),
"wording" => $value->getWording(),
"libellelibre" => $value->getLibellelibre(),
"pricepersonal" => $value->getPricepersonal(),
"pricecompany" => $value->getPricecompany(),
"pricecpf" => $value->getPricecpf(),
"formation" => count($formations) > 0?$formations:[]
]);
}
$carte["examenTaxi"] = $carteETaxi;
return new JsonResponse($carte);
}
/**
* @Route("/api/carte/{id}", name="app_api_carte_detail",methods={"GET"})
*/
public function getCarte(Carte $carte)
{
$response = $formations = [];
foreach ($carte->getFormation() as $key => $formation) {
array_push($formations,[
"id"=>$formation->getId(),
"wording" => $formation->getWording(),
]);
}
$response = [
"id" => $carte->getId(),
"wording" => $carte->getWording(),
"libellelibre" => $carte->getLibellelibre(),
"prerequis" => $carte->getPrerequis(),
"pricepersonal" => $carte->getPricepersonal(),
"pricecompany" => $carte->getPricecompany(),
"pricecpf" => $carte->getPricecpf(),
"formation" => count($formations) > 0?$formations:[]
];
return new JsonResponse($response);
}
/**
* @Route("/carte/inscription/{session}", name="app_inscrit_site_carte", methods={"GET", "POST"})
*/
public function carteView(CarteRepository $carteRepository,$session=null){
$carte = [];
$carteTM = $carteRepository->findBy(['site'=>2]);
$carteVTC = $carteRepository->findBy(['site'=>3]);
/*if ($request->getMethod()=='POST') {
$carte = $request->request->get('carte');
}*/
return $this->renderForm('inscription_site/carte.html.twig',[
'carteTM' =>$carteTM,
'carteVTC'=> $carteVTC,
'menuLink' => 'inscription',
'path' => 'app_inscrit_index',
'page' => 'Carte',
'menu' => ''
]);
}
private function cleanValueFromRequest($temp){
return trim($temp);
}
/**
* @Route("/nouvelle/insciption/", name="app_inscrit_site_new", methods={"GET", "POST"})
*/
public function new(EntityManagerInterface $em,Request $request, InscritRepository $inscritRepository,SiteRepository $siteRepository,FormationRepository $formationRepository,TypeInscritRepository $typeInscritRepository, ReglementRepository $reglementRepository,TierRepository $tierRepository,FileUploader $fileUploader,OrganismeRepository $organimseRepository,
StatusInscritRepository $statusInscritRepository,CarteRepository $carteRepository,OptionCategorieInscritRepository $optionCategorieInscritRepository,DemandeRepository $demandeRepository, StatusRepository $statusRepository,MoyPaiementRepository $moyPaiementRepository,CondRegleRepository $condRegleRepository,SourceFinanceRepository $sourceFinanceRepository,
SessionRepository $sessionRepository,UniteInscriptionRepository $uniteInscriptionRepository,ExamenRepository $examenRepository,ElearningRepository $elearningRepository,EntrepriseRepository $entrepriseRepository,MailerController $mailer, MailerInterface $mailerinterf,ApiElearning $apiElearning)
{
$inscrit = new Inscrit();
//$form = $this->createForm(InscritType::class, $inscrit);
$defaultData=['message'=>'type'];
$form=$this->createFormBuilder($defaultData)->getForm();
$form->handleRequest($request);
// get all site
$site_id = $request->query->get('site');
$isElearning = false; // c'est pour afficher le message mail elearning envoye
if ($site_id != null) {
$carte = $carteRepository->find($site_id);
if ($carte == null) {
throw new Exception("Error Processing carte", 1);
}
//dd($carte->getFormation()[2]->getWording());
}else {
throw new Exception("Error Processing Request site", 1);
}
//if ($this->isGranted('ROLE_ADMIN')) {
$site = $siteRepository->find($carte->getSite()->getId());
//dd($site);
/*}
else{
$site = $siteRepository->findBy(["id"=>(string)$this->getUser()->getSite()->getId()], ["id" => "DESC"]);
}*/
//get formation
$formation = $carte->getFormation();
$sessionArray = [];
for ($i=0; $i < count($formation); $i++) {
//dump($formation[$i]->getTypeformation());
if ($formation[$i]->getTypeformation() == 1 ) {
$tempSession = $sessionRepository->findBy(['formation'=>$formation[$i]->getId(),'status'=>[1,2],'modeapprentissage'=>$carte->getModeapprentissage()]);
$onlySessionForPole = [];
for ($j=0; $j < count($tempSession); $j++) {
if ($tempSession[$j]->getSite()->getId() == $carte->getSite()->getId()) {
array_push($onlySessionForPole,$tempSession[$j]);
}
}
$sessionArray[$i] = $onlySessionForPole;
}
elseif ($formation[$i]->getTypeformation() == 2) { //2 pour e learning
//$tempSession = $elearningRepository->findBy(['is_archived'=>false]);
$sessionArray[$i] = ['elearning'];
}
elseif ($formation[$i]->getTypeformation() == 3) { //2 pour examen
$tempSession = $examenRepository->findBy(['is_archived'=>false]);
$sessionArray[$i] = $tempSession;
//dd($formation[$i]);
}
elseif ($formation[$i]->getTypeformation() == 4) { //4 pour conduite
//$tempSession = $elearningRepository->findBy(['is_archived'=>false]);
$sessionArray[$i] = ['conduite'];
}
//unset($tempSession);
//dump($sessionArray[$i]);
}
//get optioncategori
$optioncategorieinscrit = $optionCategorieInscritRepository->findAll();
//$request->getMethod()=='POST'
if ($request->getMethod()=='POST') {
$buttonSubmitPaiement = $request->request->get('paeiement');
$inscritavecpaiement = $buttonSubmitPaiement == 1?true:false; // 1 pour paiement
$em->getConnection()->beginTransaction();
try{
$uniteinscription = new UniteInscription();
//creer entite justiffouri puis passer le fichier uploader avant de lui faire un push
//$formation = $formationRepository->find($request->request->get('formation'));
$sessions = $request->request->get('session');
$examens = $request->request->get('examen');
$conduites = $carte->getHeureconduite();
$typeInscrit = $typeInscritRepository->find(1);
$statusinscrit = $statusInscritRepository->find(1/*$request->request->get('statutinscrit')*/);
$condRegle = $condRegleRepository->find(1);
$sourceF = $sourceFinanceRepository->find($request->request->get('typeIns'));
$categorieinscrit = $request->request->get('categorieinscrit');
//information sur le tier
$tier = new Tier();
$tier->setLastname($this->cleanValueFromRequest($request->request->get('lastname')))
->setFirstname($this->cleanValueFromRequest($request->request->get('firstname')))
->setSexe($this->cleanValueFromRequest($request->request->get('civilite')))
->setTelephone($this->cleanValueFromRequest($request->request->get('telephone')))
->setEmail($this->cleanValueFromRequest($request->request->get('email')));
if ($tierRepository->findOneBy(["email"=>$tier->getEmail()]) == null) {
/*$this->addFlash("message", "Cette adresse mail ".$tier->getEmail()." est déjà utilisée !");
return $this->redirect($request->headers->get('referer'));*/
$tierRepository->add($tier,true);
}
$tier = $tierRepository->findOneBy(["email"=>$tier->getEmail()]);
//dd($tier->getId());
$uniteinscription->setStatutinscrit($statusinscrit)
->setCreatedat(new \DateTime())
->setSourcefinancement($sourceF)
->setCategorieinscrit($categorieinscrit)
->setCarte($carte)
->setTier($tier)
->setRemise(0/*$request->request->get('remise')*/)
->setMontantapresremise($carte->getPricepersonal()/*$request->request->get('montantremise')*/)
->setTypeinscrit($typeInscrit)
->setMontanttotal($carte->getPricepersonal()/*$request->request->get('montantTotal')*/)
->setSourceinscription("Site");
//dd($uniteinscription);
foreach($carte->getFormation() as $key => $formation)
{
if ($formation->getTypeformation() == 1) {
$session = null;
if(!is_array($sessions))
throw new Exception('Les session attendues devraient etre de type "array" ou "iterable object"');
foreach ($sessions as $sessionValue) {
$session = $sessionRepository->findOneBy(['id'=>$sessionValue,'formation'=>$formation]);
if ($session != null) {
break;
}
}
//dump($formation->getWording());
if($session == null)
{
//dd("session null");
$this->addFlash("message", "Oups!!! Une erreur est survenue lors de l'inscription du stagiaire. Une session n'a pas été retrouvée. Veuillez reprendre svp!");
return $this->redirect($request->headers->get('referer'));
}
$tierinsession = $inscritRepository->findOneBy(['session' => $session, 'tier' => $tier, 'isdelete' => false]);
if($tierinsession != null)
{
//dd("session deja");
$this->addFlash("message", "Le stagiaire ".$tier->getLastname()." ".$tier->getFirstname()." a déjà été inscrit à cette session!");
return $this->redirect($request->headers->get('referer'));
}
$inscrit = (new Inscrit())->setUniteinscription($uniteinscription)
->setSession($session)
->setFormation($formation)
->setTypeinscrit($typeInscrit)
->setTier($tier)
->setIsdelete(false)
->setCreatedAt(new \DateTime());
$uniteinscription->addInscrit($inscrit);
//upload fichiers justif
//retourne array du nombre de pieces requises et du nombre founi pour la formation
//array_push($jaugejustifs, $this->uploadInscriptionJustif($fichier, $inscrit, $formation));
}
elseif ($formation->getTypeformation() == 2) {
//$conduites = (array)$conduites;
//$session = $elearningRepository->find(key_exists($key, $conduites) ? $conduites[$key] : 0); //if key doesn't exist will return null and fail
$dateDebut = $request->request->get('datedebut');
$dateFin = $request->request->get('datefin');
if ($request->request->get('datedebut') == null ) {
//dd("date");
$this->addFlash("message", "Oups!!! Une erreur est survenue lors de l'inscription du stagiaire. Date de début et fin obligatoire. Veuillez reprendre svp!");
return $this->redirect($request->headers->get('referer'));
}
//definition des dates de debut et de fin au cas ou cest une carte examen
if((int)$carte->getDuree() > 0 && (int)$carte->getThinkificFormationId() > 0)
{
$dateDebut = date("Y-m-d H:i:s");
$dateFin = date('Y-m-d H:i:s', strtotime($dateDebut. ' + '.$carte->getDuree().' days'));
}
$elearning = new Elearning();
$elearning->setDatedebut(new \DateTime($dateDebut))
->setDatefin(new \DateTime($dateFin))
->setIsArchived(false);
$inscrit = (new Inscrit())->setUniteinscription($uniteinscription)
->setElearning($elearning)
->setFormation($formation)
->setTypeinscrit($typeInscrit)
->setTier($tier)
->setIsdelete(false)
->setCreatedAt(new \DateTime());
$uniteinscription->addInscrit($inscrit);
$isElearning = true;
//dd($elearning);
//upload fichiers justif
//retourne array du nombre de pieces requises et du nombre founi pour la formation
//array_push($jaugejustifs, $this->uploadInscriptionJustif($fichier, $inscrit, $formation));
}
elseif ($formation->getTypeformation() == 3) { //3 examen
$session = $examenRepository->find($examens); //if key doesn't exist will return null and fail
if($session == null)
{
//dd("examen");
$this->addFlash("message", "Oups!!! Une erreur est survenue lors de l'inscription du stagiaire. La date d'examen n'a pas été retrouvée. Veuillez reprendre svp!");
return $this->redirect($request->headers->get('referer'));
}
$tierinsession = $inscritRepository->findOneBy(['elearning' => $session, 'tier' => $tier, 'isdelete' => false]);
$inscrit = (new Inscrit())->setUniteinscription($uniteinscription)
->setExamen($session)
->setFormation($formation)
->setTypeinscrit($typeInscrit)
->setTier($tier)
->setIsdelete(false)
->setCreatedAt(new \DateTime());
$uniteinscription->addInscrit($inscrit);
//upload fichiers justif
//retourne array du nombre de pieces requises et du nombre founi pour la formation
//array_push($jaugejustifs, $this->uploadInscriptionJustif($fichier, $inscrit, $formation));
}
elseif ($formation->getTypeformation() == 4) { //4 conduite
//$session = $examenRepository->find($examens); //if key doesn't exist will return null and fail
if($conduites == null)
{
//dd("conduite");
$this->addFlash("message", "Oups!!! Une erreur est survenue lors de l'inscription du stagiaire. Les heures de conduite n'ont pas été retrouver. Veuillez reprendre svp!");
return $this->redirect($request->headers->get('referer'));
}
$inscrit = (new Inscrit())->setUniteinscription($uniteinscription)
->setConduiteheure($conduites)
->setFormation($formation)
->setTypeinscrit($typeInscrit)
->setTier($tier)
->setIsdelete(false)
->setCreatedAt(new \DateTime());
$uniteinscription->addInscrit($inscrit);
//upload fichiers justif
//retourne array du nombre de pieces requises et du nombre founi pour la formation
// array_push($jaugejustifs, $this->uploadInscriptionJustif($fichier, $inscrit, $formation));
}
}
//dd("regle");
if ($sourceF->getId() == 2) //personnel
{
$reglement= new Reglement();
$moyenPaiement = 1;//$request->request->get('moypaiement');
$reglement->setMontanttotal($carte->getPricepersonal())
->setMontantapresremise($carte->getPricepersonal()/*$request->request->get('montantremise')*/)
->setRemise(0/*$request->request->get('remise')*/)
->setModpaiement($moyPaiementRepository->find($moyenPaiement))
->setMontant((float)0/*$request->request->get('montantpayer')*/)
->setCreatedat(new \DateTime($request->request->get('dateregle')));
if($carte->getPricepersonal()/*$request->request->get('restepayer')*/ != ""){
$reglement->setRestepayer(/*$request->request->get('restepayer')*/$carte->getPricepersonal());
}
//}
//dump("source");
$financement = new Financement();
$financement->setTypeF("1")
->setConditionreglement($condRegle);
$uniteinscription->addFinancement($financement);
$financement->setMontant($carte->getPricepersonal());
if($request->request->get('restepayer') != ""){
$reste = (float)$carte->getPricepersonal();//$request->request->get('restepayer');
//unset auto validation
/*if($statusInscritRepository->find(2) != null && (float)$request->request->get('montantpayer') > 0)
$statusinscrit=$statusInscritRepository->find(2);*/
if ($reste == 0) {
$inscrit->setRegler("Réglé");
}else if ($reste > 0){
$inscrit->setRegler("Partiellement réglé");
}
}else{
$inscrit->setRegler("Non réglé");
}
//$financement->
if(isset($reglement))
{
//frequence of reglement of tranches if financement personnelle
$financement->setFrequence(1);
//echeanceInscrit
$tranche = (int)$condRegle->getWording();
$dateEcheance = date('Y-m-d'); //$request->request->get('dateecheance');
if ($dateEcheance == null) {
// dump("date");
$this->addFlash("message", "Oups!!!. Veuillez indiquer la date du début de l'échéance!");
return $this->redirect($request->headers->get('referer'));
}
$dateEcheance = date_create($dateEcheance);
$montantEcheance = 0.00;
if($tranche<=0)
$tranche=1;//prevent division by zero
$montantEcheance =$carte->getPricepersonal();
//round(($carte->getPricepersonal() - (float)$carte->getPricepersonal())/$tranche, 2);
$next_date = new \DateTime(date('Y-m-d', strtotime(date('Y-m-d', $dateEcheance->getTimestamp()). ' + 1 days')));
$echeanceInscrit = new EcheanceInscrit();
$echeanceInscrit->setMontantEcheance($montantEcheance);
$echeanceInscrit->setMontantRegle(0.00);
$echeanceInscrit->setRestearegler($montantEcheance);
$echeanceInscrit->setDateEcheance($next_date);
$echeanceInscrit->setCreatedAt(new \DateTimeImmutable());
$financement->addEcheanceInscrit($echeanceInscrit);
for($i=2; $i<=$tranche; $i++)
{
$next_date = new \DateTime(date('Y-m-d', strtotime(date_format($next_date, 'Y-m-d'). ' + 1 days')));
$echeanceInscrit = new EcheanceInscrit();
$echeanceInscrit->setMontantEcheance($montantEcheance);
$echeanceInscrit->setMontantRegle(0.00);
$echeanceInscrit->setRestearegler($montantEcheance);
$echeanceInscrit->setDateEcheance($next_date);
$echeanceInscrit->setCreatedAt(new \DateTimeImmutable());
$financement->addEcheanceInscrit($echeanceInscrit);
}
}
//imputations start if montant regle > 0
if(isset($reglement))
{
//$this->imputerReglerAEcheance($reglement, $financement);
$reglement->setFinancement($financement);
$financement->addReglement($reglement);
$reglementRepository->add($reglement,true);
}
//update inscrit statutregle(non regle, partiellement regle or regle after each imputation)
//$this->setInscritStatutRegle($uniteinscription);
}
elseif ($sourceF->getId() == 4) {
$financement2 = new Financement();
$financement2->setTypeF("3"); // cpf
$montant =$carte->getPricecpf();
if ($montant != "") {
$financement2->setMontant($montant);
}
//l'inscrit est passe au statut réglé automatiquement si CPF
$uniteinscription->setRegler("Réglé");
$uniteinscription->addFinancement($financement2);
$uniteInscriptionRepository->add($uniteinscription);
//dd($financement2);
}
//dd($carte->getPricecpf());
//dd("fd");
//calcul jauge des pieces fournis
/*foreach($jaugejustifs as $justif)
{
$nbrFourni += (int)$justif["fourni"];
$nbrRequis += (int)$justif["requis"];
}
if($nbrRequis == 0)
$nbrRequis=1;
*/
$uniteinscription->setPiecefourni(0) ;
//fin calcul jauge
// if(isset($reglement))
// $reglementRepository->add($reglement,true);
if ($tier->getEmail() != null) {
$mailer->sendPreinscriptionMail($mailerinterf,$uniteinscription);
}
$em->flush();
$em->getConnection()->commit();
} catch (Exception $e) {
$em->getConnection()->rollBack();
throw $e;
}
if($inscritavecpaiement){
$apiPaiementController = new ApiPaiementController();
$getDateForHMAC = $apiPaiementController->sendRequestPayment($uniteinscription->getMontanttotal(),$tier->getLastname(),$tier->getFirstname(),$tier->getEmail(), $uniteinscription->getId());
$getDateForHMAC = json_decode($getDateForHMAC->getContent());
$n = '1';
$pbx_shoppingcart = "<?xml version=\"1.0\" encoding=\"utf-8\"?><shoppingcart><total><totalQuantity>".$n."</totalQuantity></total></shoppingcart>";
return $this->render('inscription_site/paiement.html.twig',[
'status'=>'success',
'PBX_HMAC'=>$getDateForHMAC->hmac,
'PBX_BILLING'=>$getDateForHMAC->billing,
'PBX_TOTAL'=>$getDateForHMAC->total,
'PBX_PORTEUR'=>$getDateForHMAC->email,
'PBX_CMD'=> $getDateForHMAC->id,
'PBX_TIME'=> $getDateForHMAC->time,
'pbx_shoppingcart' => $pbx_shoppingcart
]);
}else{
if ($carte->getSite()->getId() == 1) {
return $this->render('inscription_site/terminer.html.twig',[
'path'=>'https://www.taxi.ecole-chauffeur-prive.fr/',
'elearning'=>false
]);
}
return $this->render('inscription_site/terminer.html.twig',[
'path'=>'https://www.ecole-chauffeur-prive.fr/',
'elearning'=>false]);
}
}
//les pieces justificatives de toutes les formations
//$cartejutificatifs = $this->getCarteJustificatif($formation);
return $this->renderForm('inscription_site/inscription.html.twig', [
'inscrit' => $inscrit,
//'form' => $form,
'menu' => 'Inscription',
'page' => 'Nouvelle inscription',
'menuLink' => 'inscription',
'path'=>'app_inscrit_index',
'sites'=>$site,
'carte' => $carte,
'formations'=>$formation,
'session'=>$sessionArray,
'datedebutelearning' => new \DateTime(date("Y-m-d H:i:s")),
'datefinelearning' => new \DateTime(date('Y-m-d H:i:s', strtotime(date("Y-m-d H:i:s"). ' + '.$carte->getDuree().' days'))),
'conduitehoure'=>$carte->getHeureconduite(),
//'cartejutificatifs' => $cartejutificatifs,
'optioncategorieinscrit'=>$optioncategorieinscrit
]);
}
}