src/Controller/DefaultController.php line 82

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use ZipArchive;
  4. use App\Entity\Tva;
  5. use App\Entity\User;
  6. use App\Entity\Gamme;
  7. use App\Entity\Media;
  8. use App\Entity\Poids;
  9. use App\Entity\Stock;
  10. use App\Entity\Client;
  11. use App\Entity\Famille;
  12. use App\Entity\Produit;
  13. use App\Entity\Commande;
  14. use App\Entity\Catalogue;
  15. use App\Entity\Reglement;
  16. use App\Entity\Expedition;
  17. use App\Entity\Pmarketing;
  18. use App\Entity\StockPerso;
  19. use App\Entity\UniteVente;
  20. use App\Entity\Nomenclature;
  21. use App\Entity\WebCatalogue;
  22. use App\Entity\CategorieTarif;
  23. use App\Entity\Caracteristique;
  24. use App\Entity\AdresseLivraison;
  25. use Symfony\Component\Mime\Email;
  26. use Twig\Loader\FilesystemLoader;
  27. use App\Entity\AdresseFacturation;
  28. use App\Entity\CategorieTarifClient;
  29. use Symfony\Component\Mailer\Mailer;
  30. use App\Entity\CaracteristiqueLibelle;
  31. use App\Service\PanierService;
  32. use Symfony\Component\Mailer\Transport;
  33. use Symfony\Bridge\Twig\Mime\BodyRenderer;
  34. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  35. use Symfony\Component\HttpFoundation\Request;
  36. use Symfony\Component\Mailer\MailerInterface;
  37. use Symfony\Component\HttpFoundation\Response;
  38. use Symfony\Component\Routing\Annotation\Route;
  39. use Symfony\Component\Validator\Constraints\Date;
  40. use Symfony\Component\EventDispatcher\EventDispatcher;
  41. use Symfony\Component\Mailer\EventListener\MessageListener;
  42. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  43. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  44. use Twig\Environment as TwigEnvironment;
  45. class DefaultController extends AbstractController
  46. {
  47.     private $panierService;
  48.     private $mailer;
  49.     public function __construct(PanierService $panierServiceMailerInterface $mailer)
  50.     {
  51.         $this->panierService $panierService;
  52.         // 1
  53.         // $this->mailer = $mailer;
  54.         // 2
  55.         // $transport = Transport::fromDsn('sendmail://default');
  56.         // $this->mailer = new Mailer($transport);
  57.         
  58.         // 3
  59.         $loader = new FilesystemLoader('../templates');
  60.         // $twig = new TwigEnvironment();
  61.         $twig = new TwigEnvironment($loader);
  62.         $messageListener = new MessageListener(null, new BodyRenderer($twig));
  63.         $eventDispatcher = new EventDispatcher();
  64.         $eventDispatcher->addSubscriber($messageListener);
  65.         // $transport = Transport::fromDsn('smtp://localhost', $eventDispatcher);
  66.         // $mailer = new Mailer($transport, null, $eventDispatcher);
  67.         $transport Transport::fromDsn('sendmail://default'$eventDispatcher);
  68.         $this->mailer = new Mailer($transportnull$eventDispatcher);
  69.     }
  70.     /**
  71.      * @Route("/", name="default")
  72.      */
  73.     public function index(): Response
  74.     {
  75.         if (! $this->isGranted('ROLE_FILIALE')) {
  76.             return $this->redirectToRoute("login");
  77.         }
  78.         if ($this->isGranted('ROLE_SUPER_ADMIN')) {
  79.             return $this->redirectToRoute("maisons_mere_index");
  80.         }
  81.         if ($this->isGranted('ROLE_MAISON')) {
  82.             return $this->redirectToRoute("client_show_filiale_for_maisonmere");
  83.         }
  84.         return $this->redirectToRoute("produit_show_mercuriale");
  85.     }
  86.     /**
  87.      * @Route("/import/crypt", name="import_crypt")
  88.      */
  89.     public function importCrypt(): Response
  90.     {
  91.         echo "<pre>";
  92.         $key "Multypack2021";
  93.         $md5 md5($key);
  94.         $iv substr($md5016);
  95.         var_dump($md5);
  96.         $base64 "plpAj6EwTJ4u9CYKrX5TfUt/R7Otoy3zNNQ8uDCrn0xmbs6UQlV6MnVH+8y/2pZF";
  97.         // var_dump(bin2hex($base64));
  98.         $aes128 base64_decode($base64);
  99.         // var_dump(bin2hex($aes128));
  100.             $decrypte openssl_decrypt($base64"aes-128-cbc"$md5,OPENSSL_RAW_DATA OPENSSL_ZERO_PADDING $iv);
  101.             var_dump($decrypte);
  102.             $decrypte openssl_decrypt($aes128"aes-128-cbc"$md5,OPENSSL_RAW_DATA OPENSSL_ZERO_PADDING $iv);
  103.         var_dump($decrypte);
  104.         var_dump("-------------------------------------------------------");
  105.         // $method = "aes128";
  106.         // $iv = substr($aes128, 0, 16);
  107.         // var_dump($iv);
  108.         // $hash = substr($aes128, 16, 32);
  109.         // var_dump($hash);
  110.         // $ciphertext = substr($aes128, 48);
  111.         // var_dump($ciphertext);
  112.         
  113.         // $open =  openssl_decrypt($ciphertext, $method, $key, OPENSSL_RAW_DATA, $iv);
  114.         // var_dump($open);
  115.         $cipher "aes-256-cbc"
  116.         // Clef de cryptage
  117.         $encryption_key "Multypack2021"
  118.         
  119.         // Récupère la longueur cipher du vecteur d'initialisation. 
  120.         $iv_size openssl_cipher_iv_length($cipher); 
  121.         // Génère une chaine pseudo-aléatoire d'octets
  122.         $iv openssl_random_pseudo_bytes($iv_size); 
  123.         // Data to encrypt 
  124.         $data "Projet Multypack"
  125.         
  126.         $encrypted_data openssl_encrypt($data$cipher$encryption_key0$iv); 
  127.         echo "Texte crypté : " $encrypted_data."<br>"
  128.         $encrypted_data base64_encode($encrypted_data);
  129.         $encrypted_data base64_decode($encrypted_data);
  130.         //Decrypt data 
  131.         $decrypted_data openssl_decrypt($encrypted_data$cipher$encryption_key0$iv); 
  132.         echo "Texte décrypté: " $decrypted_data
  133.             
  134.             echo "</pre>";
  135.             exit;
  136.     }
  137.     /**
  138.      * @Route("/import/clients", name="import_clients")
  139.      */
  140.     public function importClients(): Response
  141.     {
  142.         $entityManager $this->getDoctrine()->getManager();
  143.         $local_file $this->getDernierFichier("clients"); // Nom du dernier fichier
  144.         if (\file_exists($local_file)) {
  145.             ini_set('auto_detect_line_endings',TRUE);
  146.             $fic fopen($local_file"r+");
  147.             $ligne 0;
  148.             $arrayFilialesSansMere = []; 
  149.             // Les references SAGE qui sont dans le fichier Excel
  150.             $ref_fichier = [];
  151.             
  152.             while($tab=fgetcsv($fic,10000,';')){
  153.                 set_time_limit(20);
  154.                 
  155.                 if($ligne != 0){
  156.                     $tab array_map("utf8_encode"$tab); //added
  157.                     if ($tab[0] != "" ){
  158.                         $client $entityManager->getRepository(Client::class)->findOneByReferenceSage($tab[0]);
  159.                         
  160.                         //New ou edit client
  161.                         if($client == null){
  162.                             $client = new Client($tab[0], $tab[1],$tab[5]);
  163.                         }else{
  164.                             $client->setReferenceSage($tab[0]); // Pas besoin ?
  165.                             $client->setRegroupement($tab[5]);
  166.                             $client->setNom(trim($tab[1]));
  167.                         }
  168.                         $client->setActif(true);
  169.                         $entityManager->persist($client);
  170.                         $entityManager->flush();
  171.                         //Si 0 != 2 alors c'est une filiale
  172.                         if( $tab[0] != $tab[2]){
  173.                             $maisonMere $entityManager->getRepository(Client::class)->findOneBy(array("referenceSage"=>$tab[2],"maisonMere"=>null));
  174.                             //Si mere existe on ajoute
  175.                             if($maisonMere != null){
  176.                                 $maisonMere->addFiliale($client);
  177.                                 $maisonMere->setCodeCentral($tab[2]);
  178.                                 $entityManager->persist($client);
  179.                                 $entityManager->persist($maisonMere);
  180.                                 $entityManager->flush();
  181.                             
  182.                             //Sinon on met l'id dans un tableau en attendant
  183.                             }else{
  184.                                 $arrayFilialesSansMere[$tab[2]][] = $tab[0];
  185.                             }
  186.                         }
  187.                         array_push($ref_fichier$client->getReferenceSage());
  188.                     } 
  189.                         
  190.                 }
  191.                 $ligne++;
  192.             }
  193.             fclose($fic);
  194.             
  195.             //Parcours des filiales ajoutées avant leurs maison mere
  196.             foreach ($arrayFilialesSansMere as $refMere => $sousTab) {
  197.                 $maisonMere $entityManager->getRepository(Client::class)->findOneBy(array("referenceSage"=>$refMere,"maisonMere"=>null));
  198.                 if($maisonMere != null){
  199.                 
  200.                     foreach ($sousTab as $refFiliale) {
  201.                         $filiale $entityManager->getRepository(Client::class)->findOneBy(array("referenceSage"=>$refFiliale));
  202.                         
  203.                         if($filiale != null){
  204.                             $maisonMere->addFiliale($filiale);
  205.                             $entityManager->persist($maisonMere);
  206.                             $entityManager->persist($filiale);
  207.                             $entityManager->flush();
  208.                         }
  209.                     }
  210.                 }
  211.             }
  212.             $all_items $entityManager->getRepository(Client::class)->findAll();
  213.             foreach ($all_items as $item) {
  214.                 if ( ! in_array($item->getReferenceSage(), $ref_fichier) ) {
  215.                     // $entityManager->remove($item);
  216.                     $item->setActif(false);
  217.                     $entityManager->persist($item);
  218.                     $entityManager->flush();
  219.                 }
  220.             }
  221.             
  222.             return new Response('Ok ' $local_file);
  223.         }
  224.         return new Response('Erreur ' $local_file ' : fichier non trouvé.');
  225.     }
  226.     /**
  227.      * @Route("/import/adr", name="import_adresses")
  228.      */
  229.     public function importAdresses(): Response
  230.     {
  231.         $entityManager $this->getDoctrine()->getManager();
  232.         $local_file $this->getDernierFichier("adresses"); // Nom du dernier fichier
  233.         if (\file_exists($local_file)) {
  234.             ini_set('auto_detect_line_endings',TRUE);
  235.             $fic fopen($local_file"r+");
  236.             $ligne 0;
  237.             // Les references SAGE qui sont dans le fichier Excel
  238.             $ref_fichier = [];
  239.             while($tab=fgetcsv($fic,10000,';')){
  240.                     
  241.                 set_time_limit(20);
  242.                 
  243.                 if($ligne != 0){ 
  244.                     
  245.                     $tab array_map("utf8_encode"$tab); //added
  246.                     if ($tab[1] != "" ){
  247.                         $client $entityManager->getRepository(Client::class)->findOneByReferenceSage($tab[1]);
  248.                         
  249.                         if($tab[2] =="L"){       
  250.                             $livraison $entityManager->getRepository(AdresseLivraison::class)->findOneByReferenceSage($tab[0]);
  251.                             // On évite de créer 2 fois la même adresse pour le client
  252.                             if ($livraison == null){
  253.                                 $livraison = new AdresseLivraison();
  254.                                 $livraison->setReferenceSage($tab[0]);
  255.                                 $livraison->setClient($client);
  256.                             }
  257.                             
  258.                             $livraison->setRue($tab[4]);
  259.                             $livraison->setComplement($tab[5]);
  260.                             $livraison->setCodePostal($tab[6]);
  261.                             $livraison->setVille($tab[7]);
  262.                             
  263.                             
  264.                             $livraison->setActif(true);
  265.                             
  266.                             $entityManager->persist($livraison);
  267.                             $entityManager->persist($client);
  268.                             $entityManager->flush();
  269.                             
  270.                             array_push($ref_fichier$livraison->getReferenceSage());
  271.                         }
  272.                         if($tab[2] =="F" || $tab[2] == "L" ){
  273.                             $facturation $entityManager->getRepository(AdresseFacturation::class)->findOneByReferenceSage($tab[0]);
  274.                             if ($facturation == null){
  275.                                 $facturation = new AdresseFacturation();
  276.                                 $facturation->setReferenceSage($tab[0]);
  277.                                 $facturation->setClient($client);
  278.                             }
  279.                             $facturation->setRue($tab[4]);
  280.                             $facturation->setComplement($tab[5]);
  281.                             $facturation->setCodePostal($tab[6]);
  282.                             $facturation->setVille($tab[7]);
  283.                             $facturation->setActif(true);
  284.                             $entityManager->persist($facturation);
  285.                             $entityManager->persist($client);
  286.                             $entityManager->flush();
  287.                             
  288.                             array_push($ref_fichier$facturation->getReferenceSage());                    
  289.                         }
  290.                     }
  291.                 }
  292.                 $ligne++;
  293.             }
  294.             fclose($fic);        
  295.             
  296.             $all_livraison $entityManager->getRepository(AdresseLivraison::class)->findAll();
  297.             $all_facturation $entityManager->getRepository(AdresseFacturation::class)->findAll();
  298.             $all_items array_merge($all_livraison$all_facturation);
  299.             foreach ($all_items as $item) {
  300.                 if ( ! in_array$item->getReferenceSage(), $ref_fichier ) ) {
  301.                     
  302.                     $item->setActif(false);
  303.                     $entityManager->persist($item);
  304.                     $entityManager->flush();
  305.                 }
  306.             }
  307.             
  308.             return new Response('Ok');
  309.         }
  310.         return new Response('Erreur ' $local_file ' : fichier non trouvé.');
  311.     }
  312.     /**
  313.      * @Route("/import/libelles", name="import_libelles")
  314.      */
  315.     public function importLibelles(): Response
  316.     {
  317.         $entityManager $this->getDoctrine()->getManager();
  318.         $local_file $this->getDernierFichier("LIBELLES"); // Nom du dernier fichier
  319.         if (\file_exists($local_file)) {
  320.             ini_set('auto_detect_line_endings',TRUE);
  321.             $fic fopen($local_file"r+");
  322.             $entityManager $this->getDoctrine()->getManager();
  323.             $ligne 0;
  324.             while($tab=fgetcsv($fic,10000,';')){
  325.                 set_time_limit(20);
  326.                             
  327.                 if($ligne != 0){ 
  328.                     $tab array_map("utf8_encode"$tab); //added
  329.                     $table =$tab[0];
  330.                     $idSage =$tab[1];
  331.                     $libelle =$tab[2];
  332.                     $info =$tab[3];
  333.                     if ($table != "" ){
  334.                         
  335.                         if($table == "CARACTERISTIQUE"){
  336.                             if($idSage != ""){
  337.                                 $unit $entityManager->getRepository(Caracteristique::class)->findOneByIdMulty($idSage);                        
  338.                                 if($unit != null){
  339.                                     $unit->setNom($libelle);
  340.                                     $unit->setInfo($info);
  341.                                 }else{
  342.                                     $unit = new Caracteristique();
  343.                                     $unit->setNom($libelle);
  344.                                     $unit->setIdMulty($idSage);
  345.                                     $unit->setInfo($info);                            
  346.                                 }
  347.                                 $entityManager->persist($unit);
  348.                             }
  349.                         }
  350.                         if($table == "CATALOGUE"){
  351.                             if($idSage != ""){
  352.                                 $unit $entityManager->getRepository(Catalogue::class)->findOneByIdMulty($idSage);                        
  353.                                 if($unit != null){
  354.                                     $unit->setNom($libelle);
  355.                                     $unit->setInfo($info);
  356.                                 }else{
  357.                                     $unit = new Catalogue();
  358.                                     $unit->setNom($libelle);
  359.                                     $unit->setIdMulty($idSage);
  360.                                     $unit->setInfo($info);                            
  361.                                 }
  362.                                 $entityManager->persist($unit);
  363.                             }
  364.                         }
  365.                         if($table == "CATEGORIETARIF"){
  366.                             if($idSage != ""){
  367.                                 $unit $entityManager->getRepository(CategorieTarif::class)->findOneByIdMulty($idSage);                        
  368.                                 if($unit != null){
  369.                                     $unit->setNom($libelle);
  370.                                     $unit->setInfo($info);
  371.                                 }else{
  372.                                     $unit = new CategorieTarif();
  373.                                     $unit->setNom($libelle);
  374.                                     $unit->setIdMulty($idSage);
  375.                                     $unit->setInfo($info);                            
  376.                                 }
  377.                                 $entityManager->persist($unit);
  378.                             }
  379.                         }
  380.                         if($table == "EXPEDITION"){
  381.                             if($idSage != ""){
  382.                                 $unit $entityManager->getRepository(Expedition::class)->findOneByIdMulty($idSage);                        
  383.                                 if($unit != null){
  384.                                     $unit->setNom($libelle);
  385.                                     $unit->setInfo($info);
  386.                                 }else{
  387.                                     $unit = new Expedition();
  388.                                     $unit->setNom($libelle);
  389.                                     $unit->setIdMulty($idSage);
  390.                                     $unit->setInfo($info);                            
  391.                                 }
  392.                                 $entityManager->persist($unit);
  393.                             }
  394.                         }
  395.                         if($table == "FAMILLE"){
  396.                             if($idSage != ""){
  397.                                 $unit $entityManager->getRepository(Famille::class)->findOneByIdMulty($idSage);                        
  398.                                 if($unit != null){
  399.                                     $unit->setNom($libelle);
  400.                                     $unit->setInfo($info);
  401.                                 }else{
  402.                                     $unit = new Famille();
  403.                                     $unit->setNom($libelle);
  404.                                     $unit->setIdMulty($idSage);
  405.                                     $unit->setInfo($info);                            
  406.                                 }
  407.                                 $entityManager->persist($unit);
  408.                             }
  409.                         }
  410.                         if($table == "GAMME"){
  411.                             if($idSage != ""){
  412.                                 $unit $entityManager->getRepository(Gamme::class)->findOneByIdMulty($idSage);                        
  413.                                 if($unit != null){
  414.                                     $unit->setNom($libelle);
  415.                                     $unit->setInfo($info);
  416.                                 }else{
  417.                                     $unit = new Gamme();
  418.                                     $unit->setNom($libelle);
  419.                                     $unit->setIdMulty($idSage);
  420.                                     $unit->setInfo($info);                            
  421.                                 }
  422.                                 $entityManager->persist($unit);
  423.                             }
  424.                         }
  425.                         if($table == "MEDIA"){
  426.                             if($idSage != ""){
  427.                                 $unit $entityManager->getRepository(Media::class)->findOneByIdMulty($idSage);                        
  428.                                 if($unit != null){
  429.                                     $unit->setNom($libelle);
  430.                                     $unit->setInfo($info);
  431.                                 }else{
  432.                                     $unit = new Media();
  433.                                     $unit->setNom($libelle);
  434.                                     $unit->setIdMulty($idSage);
  435.                                     $unit->setInfo($info);                            
  436.                                 }
  437.                                 $entityManager->persist($unit);
  438.                             }
  439.                         }
  440.                         if($table == "NOMENCLATURE"){
  441.                             if($idSage != ""){
  442.                                 $unit $entityManager->getRepository(Nomenclature::class)->findOneByIdMulty($idSage);                        
  443.                                 if($unit != null){
  444.                                     $unit->setNom($libelle);
  445.                                     $unit->setInfo($info);
  446.                                 }else{
  447.                                     $unit = new Nomenclature();
  448.                                     $unit->setNom($libelle);
  449.                                     $unit->setIdMulty($idSage);
  450.                                     $unit->setInfo($info);                            
  451.                                 }
  452.                                 $entityManager->persist($unit);
  453.                             }
  454.                         }
  455.                         if($table == "PMARKETING"){
  456.                             if($idSage != ""){
  457.                                 $unit $entityManager->getRepository(Pmarketing::class)->findOneByIdMulty($idSage);                        
  458.                                 if($unit != null){
  459.                                     $unit->setNom($libelle);
  460.                                     $unit->setInfo($info);
  461.                                 }else{
  462.                                     $unit = new Pmarketing();
  463.                                     $unit->setNom($libelle);
  464.                                     $unit->setIdMulty($idSage);
  465.                                     $unit->setInfo($info);                            
  466.                                 }
  467.                                 $entityManager->persist($unit);
  468.                             }
  469.                         }
  470.                         if($table == "POIDS"){
  471.                             if($idSage != ""){
  472.                                 $unit $entityManager->getRepository(Poids::class)->findOneByIdMulty($idSage);                        
  473.                                 if($unit != null){
  474.                                     $unit->setNom($libelle);
  475.                                     $unit->setInfo($info);
  476.                                 }else{
  477.                                     $unit = new Poids();
  478.                                     $unit->setNom($libelle);
  479.                                     $unit->setIdMulty($idSage);
  480.                                     $unit->setInfo($info);                            
  481.                                 }
  482.                                 $entityManager->persist($unit);
  483.                             }
  484.                         }
  485.                         if($table == "REGLEMENT"){
  486.                             if($idSage != ""){
  487.                                 $unit $entityManager->getRepository(Reglement::class)->findOneByIdMulty($idSage);                        
  488.                                 if($unit != null){
  489.                                     $unit->setNom($libelle);
  490.                                     $unit->setInfo($info);
  491.                                 }else{
  492.                                     $unit = new Reglement();
  493.                                     $unit->setNom($libelle);
  494.                                     $unit->setIdMulty($idSage);
  495.                                     $unit->setInfo($info);                            
  496.                                 }
  497.                                 $entityManager->persist($unit);
  498.                             }
  499.                         }
  500.                         if($table == "STOCK"){
  501.                             if($idSage != ""){
  502.                                 $unit $entityManager->getRepository(Stock::class)->findOneByIdMulty($idSage);                        
  503.                                 if($unit != null){
  504.                                     $unit->setNom($libelle);
  505.                                     $unit->setInfo($info);
  506.                                 }else{
  507.                                     $unit = new Stock();
  508.                                     $unit->setNom($libelle);
  509.                                     $unit->setIdMulty($idSage);
  510.                                     $unit->setInfo($info);                            
  511.                                 }
  512.                                 $entityManager->persist($unit);
  513.                             }
  514.                         }
  515.                         if($table == "TVA"){
  516.                             if($idSage != ""){
  517.                                 $unit $entityManager->getRepository(Tva::class)->findOneByIdMulty($idSage);                        
  518.                                 if($unit != null){
  519.                                     $unit->setNom($libelle);
  520.                                     $unit->setInfo($info);
  521.                                 }else{
  522.                                     $unit = new Tva();
  523.                                     $unit->setNom($libelle);
  524.                                     $unit->setIdMulty($idSage);
  525.                                     $unit->setInfo($info);                            
  526.                                 }
  527.                                 $entityManager->persist($unit);
  528.                             }
  529.                         }
  530.                         if($table == "UNITEVENTE"){
  531.                             if($idSage != ""){
  532.                                 $unit $entityManager->getRepository(UniteVente::class)->findOneByIdMulty($idSage);                        
  533.                                 if($unit != null){
  534.                                     $unit->setNom($libelle);
  535.                                     $unit->setInfo($info);
  536.                                 }else{
  537.                                     $unit = new UniteVente();
  538.                                     $unit->setNom($libelle);
  539.                                     $unit->setIdMulty($idSage);
  540.                                     $unit->setInfo($info);                            
  541.                                 }
  542.                                 $entityManager->persist($unit);
  543.                             }
  544.                         }
  545.                         if($table == "WEBCATALOGUE"){
  546.                             if($idSage != ""){
  547.                                 $unit $entityManager->getRepository(WebCatalogue::class)->findOneByIdMulty($idSage);                        
  548.                                 if($unit != null){
  549.                                     $unit->setNom($libelle);
  550.                                     $unit->setInfo($info);
  551.                                 }else{
  552.                                     $unit = new WebCatalogue();
  553.                                     $unit->setNom($libelle);
  554.                                     $unit->setIdMulty($idSage);
  555.                                     $unit->setInfo($info);                            
  556.                                 }
  557.                                 $entityManager->persist($unit);
  558.                             }
  559.                         }
  560.                                             
  561.                     } 
  562.                         
  563.                     $entityManager->flush();
  564.                 }
  565.                 $ligne++;
  566.             }
  567.             fclose($fic);
  568.             return new Response('Ok');
  569.         }
  570.         return new Response('Erreur ' $local_file ' : fichier non trouvé.');
  571.         
  572.     }
  573.     /**
  574.      * @Route("/import/produits", name="import_produits")
  575.      */
  576.     public function importProduit(): Response
  577.     {
  578.         $entityManager $this->getDoctrine()->getManager();
  579.         /**
  580.          * On copie le dernier fichier correspond au type entre parenthèses à partir du serveur dans un dossier local de /public/ 
  581.          * On récupère le nom du dernier fichier
  582.          */
  583.         $local_file $this->getDernierFichier("ARTICLES"); // Nom du dernier fichier
  584.         if (\file_exists($local_file)) {
  585.             ini_set('auto_detect_line_endings',TRUE);
  586.             $fic fopen($local_file"r+");
  587.             $ligne 0;
  588.             $arrayFilialesSansMere = [];
  589.             // Les references SAGE qui sont dans le fichier Excel
  590.             $ref_fichier = [];
  591.             while($tab=fgetcsv($fic,10000,';')){
  592.                 set_time_limit(20);
  593.                 $tab array_map("utf8_encode"$tab); 
  594.                 if($ligne != 0){
  595.                     $produit $entityManager->getRepository(Produit::class)->findOneByReferenceSage($tab[0]);
  596.                     if($produit == null){
  597.                         $produit = new Produit();
  598.                         $produit->setReferenceSage($tab[0]);
  599.                     }
  600.                     $produit->setNom($tab[2]);
  601.                     $produit->setDescri($tab[3]);
  602.                     $produit->setDescription($tab[4]);
  603.                     $entityManager->persist($produit);
  604.                     $entityManager->flush();
  605.                     //Famille
  606.                     if($tab[9] != ""){
  607.                         $codeFamille $entityManager->getRepository(Famille::class)->findOneByIdMulty(trim($tab[9]));
  608.                         if($codeFamille != null){
  609.                             $codeFamille->addProduit($produit);
  610.                             $entityManager->persist($produit);
  611.                             $entityManager->persist($codeFamille);
  612.                         }
  613.                     }
  614.                     //Catalogue
  615.                     if($tab[10] != ""){
  616.                         $packaging $entityManager->getRepository(Catalogue::class)->findOneByIdMulty($tab[10]);
  617.                         if($packaging != null){
  618.                             $packaging->addProduitPackaging($produit);
  619.                             $entityManager->persist($produit);
  620.                             $entityManager->persist($packaging);
  621.                         }
  622.                     }
  623.                     if($tab[11] != ""){
  624.                         $matiere $entityManager->getRepository(Catalogue::class)->findOneByIdMulty($tab[11]);
  625.                         if($matiere != null){
  626.                             $matiere->addProduitMatiere($produit);
  627.                             $entityManager->persist($produit);
  628.                             $entityManager->persist($matiere);
  629.                         }
  630.                     }
  631.                     if($tab[12] != ""){
  632.                         $type $entityManager->getRepository(Catalogue::class)->findOneByIdMulty($tab[12]);
  633.                         if($type != null){
  634.                             $type->addProduitType($produit);
  635.                             $entityManager->persist($produit);
  636.                             $entityManager->persist($type);
  637.                         }
  638.                     }
  639.                     if($tab[13] != ""){
  640.                         $couleur $entityManager->getRepository(Catalogue::class)->findOneByIdMulty($tab[13]);
  641.                         if($couleur != null){
  642.                             $couleur->addProduitCouleur($produit);
  643.                             $entityManager->persist($produit);
  644.                             $entityManager->persist($couleur);
  645.                         }
  646.                     }
  647.                     //Nomenclature
  648.                     if($tab[14] != ""){
  649.                         $nomenclature $entityManager->getRepository(Nomenclature::class)->findOneByIdMulty($tab[14]);
  650.                         if($nomenclature != null){
  651.                             $nomenclature->addProduit($produit);
  652.                             $entityManager->persist($produit);
  653.                             $entityManager->persist($nomenclature);
  654.                         }
  655.                     }
  656.                     //Gamme
  657.                     if($tab[15] != ""){
  658.                         $gamme $entityManager->getRepository(Gamme::class)->findOneByIdMulty(trim($tab[15]));
  659.                         if($gamme != null){
  660.                             $gamme->addProduit($produit);
  661.                             $entityManager->persist($produit);
  662.                             $entityManager->persist($gamme);
  663.                         }
  664.                     }
  665.                     //Prix unitaire
  666.                     $produit->setPrixUnitaireHT($tab[16]);
  667.                     
  668.                     $prixVenteC $tab[16];
  669.                     $uniteVenteC null;                
  670.                     //UniteVente
  671.                     if($tab[17] != ""){
  672.                         $uniteVente $entityManager->getRepository(UniteVente::class)->findOneByIdMulty($tab[17]);
  673.                         if($uniteVente != null){
  674.                             $uniteVente->addProduit($produit);
  675.                             $entityManager->persist($produit);
  676.                             $entityManager->persist($uniteVente);
  677.                         }
  678.                         $uniteVenteC $uniteVente->getIdMulty();
  679.                     }
  680.                     //Poids
  681.                     $produit->setPoids($tab[19]);
  682.                     $poidsNetC $tab[19];
  683.                     $unitePoidsC null;
  684.                     //Poids unite
  685.                     if($tab[20] != ""){
  686.                         $poids $entityManager->getRepository(Poids::class)->findOneByIdMulty($tab[20]);
  687.                         if($poids != null){
  688.                             $poids->addProduit($produit);
  689.                             $entityManager->persist($produit);
  690.                             $entityManager->persist($poids);
  691.                         }
  692.                         $unitePoidsC $poids->getNom();
  693.                     }
  694.                     
  695.                     //Stock
  696.                     if($tab[21] != ""){
  697.                         $stock $entityManager->getRepository(Stock::class)->findOneByIdMulty($tab[21]);
  698.                         if($stock != null){
  699.                             $stock->addProduit($produit);
  700.                             $entityManager->persist($produit);
  701.                             $entityManager->persist($stock);
  702.                         }
  703.                     }
  704.                     //WebCatalogue
  705.                     if($tab[23] != ""){
  706.                         $webPackaging $entityManager->getRepository(WebCatalogue::class)->findOneByIdMulty($tab[23]);
  707.                         if($webPackaging != null){
  708.                             $webPackaging->addProduitWebPackaging($produit);
  709.                             $entityManager->persist($produit);
  710.                             $entityManager->persist($webPackaging);
  711.                         }
  712.                     }
  713.                     if($tab[24] != ""){
  714.                         $webMatiere $entityManager->getRepository(WebCatalogue::class)->findOneByIdMulty($tab[24]);
  715.                         if($webMatiere != null){
  716.                             $webMatiere->addProduitWebMatiere($produit);
  717.                             $entityManager->persist($produit);
  718.                             $entityManager->persist($webMatiere);
  719.                         }
  720.                     }
  721.                     if($tab[25] != ""){
  722.                         $webCouleur $entityManager->getRepository(WebCatalogue::class)->findOneByIdMulty($tab[25]);
  723.                         if($webCouleur != null){
  724.                             $webCouleur->addProduitWebCouleur($produit);
  725.                             $entityManager->persist($produit);
  726.                             $entityManager->persist($webCouleur);
  727.                         }
  728.                     }
  729.                     //TVA
  730.                     $produit->setTva($tab[51]);
  731.                     //Quantite colis
  732.                     $produit->setQuantite($tab[56]);
  733.                     
  734.                     $quantiteCartonC $tab[56];
  735.                     if($tab[57] != ""){
  736.                         $maisonMeres $entityManager->getRepository(Client::class)->findBy(array("maisonMere"=>null "regroupement"=>trim($tab[57])));
  737.                         if($maisonMeres != null){
  738.                             foreach($maisonMeres as $maisonMere){
  739.                                 if(!$maisonMere->getCatalogue()->contains($produit)){
  740.                                     $maisonMere->addCatalogue($produit);
  741.                                     $entityManager->persist($produit);
  742.                                     $entityManager->persist($maisonMere);
  743.                                 }
  744.                             }
  745.                         }
  746.                     }
  747.                     $refsFichier = [];
  748.                     if($tab[41] != ""){
  749.                         $lie $entityManager->getRepository(Produit::class)->findOneBy(array(
  750.                             "referenceSage" => $tab[41]
  751.                         ));
  752.                         if($lie != null){
  753.                             $produit->addProduit($lie);
  754.                             $refsFichier[] = $lie->getReferenceSage();
  755.                             $entityManager->persist($produit);
  756.                             $entityManager->flush();
  757.                         } 
  758.                         if($tab[42] != ""){
  759.                             $lie $entityManager->getRepository(Produit::class)->findOneBy(array(
  760.                                 "referenceSage" => $tab[42]
  761.                             ));
  762.                             if($lie != null){
  763.                                 $produit->addProduit($lie);
  764.                                 $refsFichier[] = $lie->getReferenceSage();
  765.                                 $entityManager->persist($produit);
  766.                                 $entityManager->flush();
  767.                             } 
  768.                         }
  769.                         if($tab[43] != ""){
  770.                             $lie $entityManager->getRepository(Produit::class)->findOneBy(array(
  771.                                 "referenceSage" => $tab[43]
  772.                             ));
  773.                             if($lie != null){
  774.                                 $produit->addProduit($lie);
  775.                                 $refsFichier[] = $lie->getReferenceSage();
  776.                                 $entityManager->persist($produit);
  777.                                 $entityManager->flush();
  778.                             } 
  779.                         }
  780.                         if($tab[44] != ""){
  781.                             $lie $entityManager->getRepository(Produit::class)->findOneBy(array(
  782.                                 "referenceSage" => $tab[44]
  783.                             ));
  784.                             if($lie != null){
  785.                                 $produit->addProduit($lie);
  786.                                 $refsFichier[] = $lie->getReferenceSage();
  787.                                 $entityManager->persist($produit);
  788.                                 $entityManager->flush();
  789.                             } 
  790.                         }
  791.                         if($tab[45] != ""){
  792.                             $lie $entityManager->getRepository(Produit::class)->findOneBy(array(
  793.                                 "referenceSage" => $tab[45]
  794.                             ));
  795.                             if($lie != null){
  796.                                 $produit->addProduit($lie);
  797.                                 $refsFichier[] = $lie->getReferenceSage();
  798.                                 $entityManager->persist($produit);
  799.                                 $entityManager->flush();
  800.                             } 
  801.                         }
  802.                         if($tab[46] != ""){
  803.                             $lie $entityManager->getRepository(Produit::class)->findOneBy(array(
  804.                                 "referenceSage" => $tab[46]
  805.                             ));
  806.                             if($lie != null){
  807.                                 $produit->addProduit($lie);
  808.                                 $refsFichier[] = $lie->getReferenceSage();
  809.                                 $entityManager->persist($produit);
  810.                                 $entityManager->flush();
  811.                             } 
  812.                         }
  813.                         if($tab[47] != ""){
  814.                             $lie $entityManager->getRepository(Produit::class)->findOneBy(array(
  815.                                 "referenceSage" => $tab[47]
  816.                             ));
  817.                             if($lie != null){
  818.                                 $produit->addProduit($lie);
  819.                                 $refsFichier[] = $lie->getReferenceSage();
  820.                                 $entityManager->persist($produit);
  821.                                 $entityManager->flush();
  822.                             } 
  823.                         }
  824.                         if($tab[48] != ""){
  825.                             $lie $entityManager->getRepository(Produit::class)->findOneBy(array(
  826.                                 "referenceSage" => $tab[48]
  827.                             ));
  828.                             if($lie != null){
  829.                                 $produit->addProduit($lie);
  830.                                 $refsFichier[] = $lie->getReferenceSage();
  831.                                 
  832.                                 $entityManager->persist($produit);
  833.                                 $entityManager->flush();
  834.                             } 
  835.                         }
  836.                         if($tab[49] != ""){
  837.                             $lie $entityManager->getRepository(Produit::class)->findOneBy(array(
  838.                                 "referenceSage" => $tab[49]
  839.                             ));
  840.                             if($lie != null){
  841.                                 $produit->addProduit($lie);
  842.                                 $refsFichier[] = $lie->getReferenceSage();
  843.                                 
  844.                                 $entityManager->persist($produit);
  845.                                 $entityManager->flush();
  846.                             } 
  847.                         }
  848.                         if($tab[50] != ""){
  849.                             $lie $entityManager->getRepository(Produit::class)->findOneBy(array(
  850.                                 "referenceSage" => $tab[50]
  851.                             ));
  852.                             if($lie != null){
  853.                                 $produit->addProduit($lie);
  854.                                 $refsFichier[] = $lie->getReferenceSage();
  855.                                 
  856.                                 $entityManager->persist($produit);
  857.                                 $entityManager->flush();
  858.                             } 
  859.                         }
  860.                     }
  861.                     foreach ($produit->getProduits() as $prd) {
  862.                         if(!in_array($prd->getReferenceSage(), $refsFichier)){
  863.                             $produit->removeProduit($prd);
  864.                             $entityManager->flush();
  865.                         }
  866.                     }
  867.                     $prixParColis $this->calculPrixColis($prixVenteC$uniteVenteC$poidsNetC$unitePoidsC$quantiteCartonC);
  868.                     $produit->setPrixUnitaireColisHT($prixParColis);
  869.                     $produit->setActif(true);
  870.                     array_push($ref_fichier$produit->getReferenceSage());
  871.                 }
  872.                 $ligne++;
  873.             }
  874.             fclose($fic);
  875.             $entityManager->persist($produit);
  876.             $entityManager->flush();
  877.             $all_items $entityManager->getRepository(Produit::class)->findAll();
  878.             foreach ($all_items as $item) {
  879.                 if ( ! in_array($item->getReferenceSage(), $ref_fichier) ) {
  880.                     // $entityManager->remove($item);
  881.                     $item->setActif(false);
  882.                     $entityManager->persist($item);
  883.                     $entityManager->flush();
  884.                 }
  885.             }
  886.             return new Response("Ok");
  887.         }
  888.         return new Response('Erreur ' $local_file ' : fichier non trouvé.');
  889.     }
  890.     /**
  891.      * @Route("/import/stocks_general", name="import_stocks_general")
  892.      */
  893.     public function importProduitStockGeneral(): Response
  894.     {
  895.         $entityManager $this->getDoctrine()->getManager();
  896.         $local_file $this->getDernierFichier("stocks"); // Nom du dernier fichier
  897.         if (\file_exists($local_file)) {
  898.             ini_set('auto_detect_line_endings',TRUE);
  899.             $fic fopen($local_file"r+");
  900.             $ligne 0;
  901.             $arrayFilialesSansMere = [];
  902.             $refsFichier = [];
  903.             while($tab=fgetcsv($fic,10000,';')){
  904.                 set_time_limit(20);
  905.                 $tab array_map("utf8_encode"$tab); 
  906.                 if($ligne != 0){ 
  907.                     $produit $entityManager->getRepository(Produit::class)->findOneByReferenceSage($tab[0]);
  908.                    
  909.                     if($produit == null){
  910.                         $produit = new Produit();
  911.                         $produit->setReferenceSage($tab[0]);
  912.                     }
  913.                     if($tab[2] == 1){
  914.                         if($tab[1] != ""){
  915.                             $produit->setStockGeneral($tab[1]);
  916.                         }
  917.                     }
  918.                     if($tab[2] == 3){            
  919.                         $client null;
  920.                         if($produit->getClients() != null && !$produit->getClients()->isEmpty()){      
  921.                             $client $produit->getClients();
  922.                             $stockPerso $entityManager->getRepository(StockPerso::class)->findOneByClientProduitForImport$client->first()->getId(), $produit->getId());
  923.                         }else{      
  924.                             $client null;
  925.                             $stockPerso $entityManager->getRepository(StockPerso::class)->findOneByClientProduitForImport$client$produit->getId());
  926.                         }      
  927.                         
  928.                         if($stockPerso == null){
  929.                             $stockPerso = new StockPerso();
  930.                             $stockPerso->setProduits($produit);
  931.                             if($client != null){
  932.                                 $stockPerso->setClients($client->first());
  933.                             }                            
  934.                         }
  935.                         $stockPerso->setStock($tab[1]);
  936.                         if ($produit->getQuantite() != null) {
  937.                             $stockPerso->setStock( ($tab[1] * 1000) / $produit->getQuantite() );
  938.                         }
  939.                         $entityManager->persist($stockPerso);
  940.                         if($client != null){
  941.                             $refsFichier[] = [$produit->getId(), $client->first()->getId()];
  942.                         }
  943.                     }
  944.                     $entityManager->persist($produit);
  945.                     $entityManager->flush();
  946.                 }
  947.                 $ligne++;
  948.             }
  949.             fclose($fic);
  950.             /* Pas besoin de ces 2 lignes ? */
  951.             $entityManager->persist($produit);
  952.             $entityManager->flush();
  953.             if($refsFichier != []){
  954.                 $stockPersos $entityManager->getRepository(StockPerso::class)->findAll();
  955.                 foreach ($stockPersos as $sp) {
  956.                     $suppr 1;
  957.                     foreach ($refsFichier as $ref) {
  958.                         if($sp->getProduits()->getId() == $ref[0] && $sp->getClients()->getId() == $ref[1]){
  959.                             $suppr 0;
  960.                         }
  961.                     }    
  962.                     
  963.                     if($suppr){
  964.                         // var_dump($sp->getId());
  965.                         $entityManager->remove($sp);
  966.                         $entityManager->flush();
  967.                     }
  968.                 }
  969.             }
  970.             return new Response("Ok");            
  971.         }
  972.         return new Response('Erreur ' $local_file ' : fichier non trouvé.');
  973.     }
  974.     /**
  975.      * @Route("/import/medias", name="import_medias")
  976.      */
  977.     public function importMedia(): Response
  978.     {
  979.         $entityManager $this->getDoctrine()->getManager();
  980.         $local_file $this->getDernierFichier("medias"); // Nom du dernier fichier
  981.         if (\file_exists($local_file)) {
  982.             ini_set('auto_detect_line_endings',TRUE);
  983.             $fic fopen($local_file"r+");
  984.             $ligne 0;
  985.             // Connexion au serveur FTP
  986.             $serveur_ftp "79.99.165.90";
  987.             $login_ftp "setg@multypack.net";
  988.             $mdp_ftp "9QGawg9HTF2g";
  989.             
  990.             $ftp ftp_connect($serveur_ftp21120) or die("Could not connect to $serveur_ftp");
  991.             $login ftp_login($ftp$login_ftp$mdp_ftp);
  992.             ftp_pasv($ftptrue);
  993.             while($tab=fgetcsv($fic,10000,';')){
  994.                 set_time_limit(20);
  995.                 $tab array_map("utf8_encode"$tab); 
  996.                 if($ligne != 0){ 
  997.                     $produit $entityManager->getRepository(Produit::class)->findOneByReferenceSage($tab[0]);
  998.                     if($produit == null){
  999.                         $produit = new Produit();
  1000.                         $produit->setReferenceSage($tab[0]);
  1001.                     }
  1002.                     // $tab[4] => nom de l'image du produit // ME_FICHIER
  1003.                     if ($tab[4] != ""){
  1004.                         
  1005.                         // Si le fichier n'existe pas déjà en local
  1006.                         if ( ! file_exists("./uploadHfLr7KCq/produits/$tab[4]")) {
  1007.                             // Si le fichier existe sur le serveur
  1008.                             if ( ftp_size($ftp"/IN/images/$tab[4]") != -) {
  1009.                                 ftp_get($ftp"./uploadHfLr7KCq/produits/$tab[4]""/IN/images/$tab[4]"FTP_BINARY);
  1010.                                 $produit->setImage($tab[4]);
  1011.                             }
  1012.                         }
  1013.                         else {
  1014.                             $produit->setImage($tab[4]);
  1015.                         }
  1016.                     }
  1017.                     $entityManager->persist($produit);
  1018.                     $entityManager->flush();
  1019.                 }
  1020.                 $ligne++;
  1021.             }
  1022.             fclose($fic);
  1023.             return new Response("Ok");            
  1024.         }
  1025.         return new Response('Erreur ' $local_file ' : fichier non trouvé.');
  1026.     }
  1027.     /**
  1028.      * @Route("/import/conditionnements", name="import_conditionnements")
  1029.      */
  1030.     public function importConditionnements(): Response
  1031.     {
  1032.         $em $this->getDoctrine()->getManager();
  1033.         
  1034.         $local_file $this->getDernierFichier("CONDITIONNEMENTS"); // Nom du dernier fichier
  1035.         
  1036.         if (\file_exists($local_file)) {
  1037.             //On reset les conditionnements de tous les produits concernés
  1038.             $allProduits $em->getRepository(Produit::class)->findAllConditionnementNotNull();
  1039.             foreach ($allProduits as $key => $singleProduit) {
  1040.                 $singleProduit->setPrixPalette(NULL);
  1041.                 $singleProduit->setQuantiteColisParPalette(NULL);
  1042.                 $singleProduit->setIdFichierConditionnementPalette(NULL);
  1043.                 
  1044.                 $em->persist($singleProduit);
  1045.                 $em->flush();
  1046.             }
  1047.             //Puis on modifie les produits concernés par le fichier uniquement
  1048.             ini_set('auto_detect_line_endings',TRUE);
  1049.             $fic fopen($local_file"r+");
  1050.             $ligne 0;
  1051.             //NB : Si une référence est présente plusieurs fois, on ne gardera que la dernière ligne qui la mentionne dans le fichier 
  1052.             // → pas de gestion de plusieurs conditionnements par palette
  1053.             while($tab=fgetcsv($fic,10000,';')){
  1054.                 set_time_limit(20);
  1055.                 $tab array_map("utf8_encode"$tab); 
  1056.                 if($ligne != 0){ 
  1057.                     $idFichierConditionnementPalette $tab[0];
  1058.                     $ref $tab[1];
  1059.                     $conditionnement $tab[2]; 
  1060.                     $quantiteProduitsParPalette $tab[3];
  1061.                     if (str_contains(strtolower($conditionnement), 'palette')) {
  1062.                         
  1063.                         $produit $em->getRepository(Produit::class)->findOneByReferenceSage($ref);
  1064.                         if ($produit != null) {
  1065.                             
  1066.                             $quantiteParColis $produit->getQuantite();
  1067.                             $quantiteColisParPalette $quantiteProduitsParPalette $quantiteParColis;
  1068.                             $prixPalette $produit->getPrixUnitaireColisHT() * $quantiteColisParPalette;
  1069.                             $produit->setPrixPalette($prixPalette);
  1070.                             $produit->setQuantiteColisParPalette($quantiteColisParPalette);
  1071.                             $produit->setIdFichierConditionnementPalette($idFichierConditionnementPalette);
  1072.                             
  1073.                             $em->persist($produit);
  1074.                             $em->flush();
  1075.                         }
  1076.                     }
  1077.                 }
  1078.                 $ligne++;
  1079.             }
  1080.             fclose($fic);
  1081.             return new Response("Ok");
  1082.         }
  1083.         return new Response('Erreur ' $local_file ' : fichier non trouvé.');
  1084.     }
  1085.     /**
  1086.      * @Route("/import/cate_tarif_palettes", name="import_cate_tarif_palettes")
  1087.      */
  1088.     public function importCatTarifPalettesClient(): Response
  1089.     {
  1090.         
  1091.         $em $this->getDoctrine()->getManager();
  1092.         $local_file $this->getDernierFichier("CONDITIONNEMENTTARIFS"); // Nom du dernier fichier
  1093.         if (\file_exists($local_file)) {
  1094.             ini_set('auto_detect_line_endings',TRUE);
  1095.             $fic fopen($local_file"r+");
  1096.             $ligne 0;
  1097.             while($tab=fgetcsv($fic,10000,';')){
  1098.                 set_time_limit(20);
  1099.                 $tab array_map("utf8_encode"$tab); 
  1100.                 if($ligne != 0){ 
  1101.                     $produit $em->getRepository(Produit::class)->findOneBy( array(
  1102.                         "idFichierConditionnementPalette" => $tab[2],
  1103.                         "referenceSage" => $tab[4]
  1104.                     ));   
  1105.                     $client $em->getRepository(Client::class)->findOneBy(array(
  1106.                         "codeCentral" => $tab[3],
  1107.                         "maisonMere" => null
  1108.                     ));
  1109.                     // Pour qu'on puisse faire une remise il faut que produit ET client ne soient pas nuls 
  1110.                     if ($produit != null && $client != null) {
  1111.                         // Pas besoin de rechercher les éventuelles remises pour le client, les remises palettes ont été remises à zéro
  1112.                         $type $tab[1];
  1113.                         $remise $tab[6];
  1114.                         $prixPaletteFichier $tab[5];
  1115.                         $catTarifClient = new CategorieTarifClient();
  1116.                         
  1117.                         $catTarifClient->setClient($client);
  1118.                         $catTarifClient->setProduit($produit);
  1119.                         $catTarifClient->setRemise($remise); //Toujours à 0 selon le fichier "Structure fichiers.csv" fourni par le client
  1120.                         $catTarifClient->setPrixSeul($prixPaletteFichier);
  1121.                         $catTarifClient->setType($type);
  1122.                         $catTarifClient->setPrix($prixPaletteFichier);
  1123.                         $em->persist($catTarifClient);
  1124.                         $em->persist($client);
  1125.                         $em->persist($produit);
  1126.                         $em->flush();
  1127.                         
  1128.                     }
  1129.                 }
  1130.                 $ligne++;
  1131.             }
  1132.             fclose($fic);
  1133.             return new Response("Ok");
  1134.         }
  1135.         return new Response('Erreur ' $local_file ' : fichier non trouvé.');
  1136.     }
  1137.     /**
  1138.      * @Route("/import/cate_tarif", name="import_cate_tarif")
  1139.      */
  1140.     public function importCatTarifClient(): Response
  1141.     {
  1142.         // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1143.         // Vider la table entière (categorie_tarif_client) et remplir de nouveau avec le fichier excel 
  1144.         // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1145.         
  1146.         $em $this->getDoctrine()->getManager();
  1147.         $allCatTarifClient $em->getRepository(CategorieTarifClient::class)->findAll();
  1148.         $local_file $this->getDernierFichier("CATTARIFAIRES"); // Nom du dernier fichier
  1149.         if (\file_exists($local_file)) {
  1150.             ini_set('auto_detect_line_endings',TRUE);
  1151.             $fic fopen($local_file"r+");
  1152.             $ligne 0;
  1153.             while($tab=fgetcsv($fic,10000,';')){
  1154.                 set_time_limit(200);
  1155.                 $tab array_map("utf8_encode"$tab); 
  1156.                 if($ligne != 0){ 
  1157.                     $produit $em->getRepository(Produit::class)->findOneByReferenceSage($tab[4]);
  1158.                     $client $em->getRepository(Client::class)->findOneBy(array(
  1159.                         "codeCentral" => $tab[3],
  1160.                         "maisonMere" => null
  1161.                     ));
  1162.                     // Pour qu'on puisse faire une remise il faut que produit ET client ne soient pas nuls 
  1163.                     if ($produit != null && $client != null) {
  1164.                         // Il est possible qu'une remise soit déjà attribué à un client pour un produit
  1165.                         $catTarifClientExists $em->getRepository(CategorieTarifClient::class)->findOneBy(array(
  1166.                             "produit" => $produit,
  1167.                             "client" => $client
  1168.                         ));
  1169.                         $type $tab[1];
  1170.                         $remise $tab[6];
  1171.                         $prix_public_fichier $tab[5];
  1172.                         $prix_public $produit->getPrixUnitaireColisHT();
  1173.                         // Cas 1 : Aucune remise n'a été attribué au client pour ce produit
  1174.                         if ($catTarifClientExists == null)
  1175.                         {
  1176.                             $catTarifClient = new CategorieTarifClient();
  1177.                             
  1178.                             $catTarifClient->setClient($client);
  1179.                             $catTarifClient->setProduit($produit);
  1180.                             $catTarifClient->setRemise($remise);
  1181.                             $catTarifClient->setPrixSeul($prix_public_fichier);
  1182.                             $catTarifClient->setType($type);
  1183.     
  1184.                             if ($remise != 0) {
  1185.                                 
  1186.                                 // $catTarifClient->setPrix( $prix_public - ( $prix_public * ($remise / 100) ) );
  1187.                                 $catTarifClient->setPrix(
  1188.                                     $this->calculPrixColis(
  1189.                                         ( $prix_public - ( $prix_public * ($remise 100) ) ), 
  1190.                                         // $prix_public, 
  1191.                                         // $produit->getUniteVente(),
  1192.                                         $produit->getUniteVente()->getId(),
  1193.                                         $produit->getPoids(),
  1194.                                         $produit->getUnitePoids(),
  1195.                                         $produit->getQuantite()
  1196.                                     )
  1197.                                 );
  1198.                             }
  1199.     
  1200.                             else{
  1201.                                 $catTarifClient->setPrix(
  1202.                                     $this->calculPrixColis(
  1203.                                         $prix_public_fichier
  1204.                                         $produit->getUniteVente()->getId(),
  1205.                                         $produit->getPoids(),
  1206.                                         $produit->getUnitePoids(),
  1207.                                         $produit->getQuantite()
  1208.                                     )
  1209.                                 );
  1210.                             }
  1211.                             $em->persist($catTarifClient);
  1212.                             $em->persist($client);
  1213.                             $em->persist($produit);
  1214.                             $catTarifClientExists $catTarifClient;
  1215.                         }
  1216.                         // Cas 2 : Une remise a été attribué au client pour ce produit
  1217.                         else {
  1218.                             // Si l'ancien type est inférieur à l'actuel ( ex : ancien type : 2, nouveau type : 3 )
  1219.                             if ($catTarifClientExists->getType() < $type) {
  1220.                                 $catTarifClientExists->setRemise($remise);
  1221.                                 $catTarifClientExists->setType($type);   
  1222.                                 $catTarifClientExists->setPrixSeul($prix_public_fichier);                                
  1223.                                 if ($remise != 0) {
  1224.                                     $catTarifClient->setPrix(
  1225.                                         $this->calculPrixColis(
  1226.                                             ( $prix_public - ( $prix_public * ($remise 100) ) ), 
  1227.                                             // $prix_public, 
  1228.                                             $produit->getUniteVente(),
  1229.                                             $produit->getPoids(),
  1230.                                             $produit->getUnitePoids(),
  1231.                                             $produit->getQuantite()
  1232.                                         )
  1233.                                     );
  1234.                                 }
  1235.         
  1236.                                 else{
  1237.                                     $catTarifClient->setPrix(
  1238.                                         $this->calculPrixColis(
  1239.                                             $prix_public_fichier
  1240.                                             $produit->getUniteVente()->getId(),
  1241.                                             $produit->getPoids(),
  1242.                                             $produit->getUnitePoids(),
  1243.                                             $produit->getQuantite()
  1244.                                         )
  1245.                                     );
  1246.                                 }
  1247.                             }
  1248.                             $em->persist($catTarifClientExists);
  1249.                         }
  1250.                         $aGarde[]= $catTarifClientExists->getId();
  1251.                         $em->flush();
  1252.                         
  1253.                     }
  1254.                 }
  1255.                 $ligne++;
  1256.             }
  1257.             fclose($fic);
  1258.             foreach ($allCatTarifClient as $catTarifClient) {
  1259.                                 
  1260.                 if(!in_array($catTarifClient->getId(), $aGarde)){
  1261.                     $em->remove($catTarifClient);
  1262.                     $em->flush();
  1263.                 }
  1264.             }   
  1265.             return new Response("Ok");
  1266.         }
  1267.         return new Response('Erreur ' $local_file ' : fichier non trouvé.');
  1268.     }
  1269.     /**
  1270.      * @Route("/import/cat_libelle", name="import_cat_libelle")
  1271.      */
  1272.     public function importCategorieLibelle(): Response
  1273.     {
  1274.         $entityManager $this->getDoctrine()->getManager();
  1275.         $local_file $this->getDernierFichier("CARACTERISTIQUES"); // Nom du dernier fichier
  1276.         if (\file_exists($local_file)) {
  1277.             ini_set('auto_detect_line_endings',TRUE);
  1278.             $fic fopen($local_file"r+");
  1279.             $ligne 0;
  1280.             while($tab=fgetcsv($fic,10000,';')){     
  1281.                 set_time_limit(20);
  1282.                 $produit null;
  1283.                 $caracteristique null;
  1284.                 
  1285.                 if($ligne != 0){ 
  1286.                     
  1287.                     $tab array_map("utf8_encode"$tab); //added
  1288.                     if ($tab[0] != "" ){
  1289.                         $produit $entityManager->getRepository(Produit::class)->findOneByReferenceSage($tab[0]);  
  1290.                     }                     
  1291.                     if($tab[1] != ""){      
  1292.                         $caracteristique $entityManager->getRepository(Caracteristique::class)->findOneByIdMulty($tab[1]);
  1293.                     }
  1294.                     if ($produit != null && $caracteristique != null) {
  1295.                         $caract_libelle = new CaracteristiqueLibelle();
  1296.                         $caract_libelle->setProduit($produit);
  1297.                         $caract_libelle->setCaracteristique($caracteristique);
  1298.                         if ($tab[2] != ""){
  1299.                             $caract_libelle->setLibelle($tab[2]);
  1300.                         }
  1301.                     }
  1302.                     
  1303.                     $entityManager->persist($caract_libelle);
  1304.                     $entityManager->persist($produit);
  1305.                     $entityManager->persist($caracteristique);
  1306.                     $entityManager->flush();
  1307.                 }
  1308.                 $ligne++;
  1309.             }
  1310.             fclose($fic);         
  1311.             
  1312.             return new Response('Ok ' $local_file);
  1313.         }
  1314.         return new Response('Erreur ' $local_file ' : fichier non trouvé.');
  1315.     }
  1316.     /**
  1317.      * @Route("/import/contacts", name="import_contacts")
  1318.      */
  1319.     public function importContact(UserPasswordHasherInterface $userPasswordHasher): Response
  1320.     {
  1321.         
  1322.         $entityManager $this->getDoctrine()->getManager();
  1323.         $local_file $this->getDernierFichier("contacts"); // Nom du dernier fichier
  1324.         if (\file_exists($local_file)) {
  1325.             
  1326.             ini_set('auto_detect_line_endings',TRUE);
  1327.             $fic fopen($local_file"r+");
  1328.             $myfile fopen("uploadHfLr7KCq/users.txt""a+") or die("Unable to open file!");
  1329.             $ligne 0;
  1330.             // Les references SAGE des utilisateurs qui sont dans le fichier Excel
  1331.             $users_fichier = [];
  1332.             while($tab=fgetcsv($fic,10000,';')){     
  1333.                 set_time_limit(20);
  1334.                 $user null;
  1335.                 
  1336.                 if($ligne != 0){ 
  1337.                     
  1338.                     $tab array_map("utf8_encode"$tab); //added
  1339.                     if ($tab[1] != ""  && $tab[5] != "commercial@multypack.fr" ){
  1340.                         $client $entityManager->getRepository(Client::class)->findOneByReferenceSage($tab[1]);  
  1341.                         
  1342.                         if ($client != null) {
  1343.                             $user null;
  1344.                             $newUser false;
  1345.                             if ($tab[0] != "" && $tab[5] != "") {
  1346.                                 $username explode("@",$tab[5]);
  1347.                                 $username $username[0];
  1348.                                 $user $entityManager->getRepository(User::class)->findOneBy( array(
  1349.                                     "referenceSage" => $tab[0],
  1350.                                     // "email" => $tab[5]
  1351.                                     "email" => $tab[5]
  1352.                                 )); 
  1353.                                 
  1354.                                 if ($user == null) {
  1355.                                     $newUser true;
  1356.                                     $user = new User();
  1357.                                     $user->setReferenceSage($tab[0]);
  1358.                                     
  1359.                                     // $email = $this->uniqueEmail($tab[5]);
  1360.                                     $email $tab[5];
  1361.                                     $password $this->randomPassword();
  1362.                                     $usernameExist $entityManager->getRepository(User::class)->findOneBy( array(
  1363.                                         "username" => $username
  1364.                                     )); 
  1365.                                     if($usernameExist){
  1366.                                         $username uniqid($username.'.');
  1367.                                     }
  1368.                                     $user->setUsername($username);
  1369.                                     
  1370.                                     // $user->setEmail($tab[5]);
  1371.                                     $user->setEmail($email);
  1372.                                     $user->setPassword(
  1373.                                         $userPasswordHasher->hashPassword(
  1374.                                             $user,
  1375.                                             $password
  1376.                                         )
  1377.                                     );
  1378.                                     $text $email "\n" $password "\n\n";
  1379.                                     fwrite($myfile$text);
  1380.                                 }
  1381.     
  1382.                                 if ($tab[2] != "") {
  1383.                                     switch ($tab[2]) {
  1384.                                         case "0" :
  1385.                                             $user->setCivilite("autre"); // Madame, Monsieur
  1386.                                             break;
  1387.             
  1388.                                         case "1" :
  1389.                                             $user->setCivilite("m"); // Monsieur
  1390.                                             break;
  1391.                                             
  1392.                                         case "2" :
  1393.                                             $user->setCivilite("mme"); // Madame
  1394.                                             break;
  1395.                                             
  1396.                                         case "3" :
  1397.                                             $user->setCivilite("mlle"); // Mademoiselle
  1398.                                             break;
  1399.             
  1400.                                         default:
  1401.                                             $user->setCivilite("autre"); // Madame, Monsieur
  1402.                                             break;
  1403.                                     }
  1404.                                 }
  1405.         
  1406.                                 if ($tab[3] != "") {
  1407.                                     $user->setNom($tab[3]);
  1408.                                 }
  1409.         
  1410.                                 if ($tab[4] != "") {
  1411.                                     $user->setPrenom($tab[4]);
  1412.                                 }
  1413.                                 
  1414.                                 /* 
  1415.                                 if ($tab[5] != "") {
  1416.                                     // On ne peut pas encore affecter le champs email pour les contacts 
  1417.                                     // => il faut que les emails soient UNIQUES
  1418.         
  1419.                                     $user->setEmail($tab[5]);
  1420.                                 }
  1421.                                  */
  1422.                                 /* 
  1423.                                 if ($tab[7] != "") {
  1424.                                     $array_nom_role = explode("-", $tab[7]);
  1425.         
  1426.                                     switch ( strtolower( $array_nom_role[1] ) ) {
  1427.                                         case 'administrateur':
  1428.                                             $user->setRole(["ROLE_ADMIN"]);
  1429.                                             break;
  1430.         
  1431.                                         case 'utilisateur':
  1432.                                             $user->setRole(["ROLE_USER"]);
  1433.                                             break;
  1434.                                         
  1435.                                         default:
  1436.                                             $user->setRole(["ROLE_USER"]);
  1437.                                             break;
  1438.                                     }
  1439.                                 }
  1440.                                 */
  1441.                                 if ( $client->getMaisonMere() == null ) {
  1442.                                     $user->setRoles(["ROLE_MAISON"]);
  1443.                                 }
  1444.                                 else{
  1445.                                     $user->setRoles(["ROLE_FILIALE"]);
  1446.                                 }
  1447.                                 $client->addUser($user);
  1448.                                 $clientNom $client->getNom();
  1449.                                 
  1450.                                 $entityManager->persist($user);
  1451.                                 $entityManager->flush();
  1452.                                 
  1453.                                 $entityManager->persist($client);
  1454.                                 $entityManager->flush();
  1455.                                 if($newUser){
  1456.                                     $emailTemplate = (new TemplatedEmail())
  1457.                                         ->from('noreply@multypack.fr')
  1458.                                         
  1459.                                         // ->to('tmarco@bleu-digital.fr')
  1460.                                         ->to('fdieu@bleu-digital.fr')
  1461.                                         
  1462.                                         ->subject('Nouveau compte MULTYPACK')
  1463.                                         ->htmlTemplate('mpck_emails/nouveau_compte.html.twig')
  1464.                                         //     // ->embedFromPath('images/SVG/Multypack_logo.svg', 'logo')
  1465.                                         ->context([
  1466.                                             "emailUser" => $email,
  1467.                                             "username" => $username,
  1468.                                             "clientNom" => $clientNom,
  1469.                                             "mdp" => $password,
  1470.                                             "logo" => "images/PNG/MP_Multypack(vert).png",
  1471.                                             "header" => "images/PNG/header_nouveau_compte.png",
  1472.                                         ])
  1473.                                     ;
  1474.                                     $this->mailer->send($emailTemplate);
  1475.                                 }
  1476.                                                 
  1477.                                 array_push($users_fichier$user->getReferenceSage());
  1478.                             }
  1479.                         }
  1480.                     } 
  1481.                     
  1482.                 }
  1483.                 $ligne++;
  1484.             }
  1485.             fclose($fic);   
  1486.             fclose($myfile);   
  1487.             $all_users $entityManager->getRepository(User::class)->findAll();
  1488.             foreach ($all_users as $user) {
  1489.                 if ( $user->getReferenceSage() == null || in_array('ROLE_ADMIN'$user->getRoles()) || in_array('ROLE_SUPER_ADMIN'$user->getRoles()) ) {
  1490.                     // Si la reference SAGE est null OU que le user est un ADMIN 
  1491.                     // do nothing
  1492.                 }
  1493.                 else{
  1494.                     if ( ! in_array($user->getReferenceSage(), $users_fichier) ) {
  1495.                         $entityManager->remove($user);
  1496.                         $entityManager->flush();
  1497.                     }
  1498.                 }
  1499.             }
  1500.             
  1501.             return new Response('Ok ' $local_file);
  1502.            
  1503.         }
  1504.         return new Response('Erreur ' $local_file ' : fichier non trouvé.');
  1505.     }
  1506.     /**
  1507.      * @Route("/mentions", name="mentions")
  1508.      */
  1509.     public function mentions(): Response
  1510.     {
  1511.         $user $this->getUser();
  1512.         $client null;
  1513.         $maison_mere null;
  1514.         $infosPanier null;
  1515.         $totalPanier null;
  1516.         $quantitePanier null;
  1517.         if ($user) {
  1518.             $client $user->getClients();
  1519.     
  1520.             $maison_mere = ($client->getMaisonMere() == null) ? $client $client->getMaisonMere();
  1521.             $infosPanier $this->panierService->panier();
  1522.             $totalPanier $infosPanier['totalPanier'];
  1523.             $quantitePanier $infosPanier['quantitePanier'];
  1524.     
  1525.         }
  1526.         return $this->renderForm('mentions.html.twig', [
  1527.             "client" => $client,
  1528.             "maisoMere" => $maison_mere,
  1529.             "totalPanier" => $totalPanier,
  1530.             "quantitePanier" => $quantitePanier,
  1531.         ]);
  1532.     }
  1533.     /**
  1534.      * @Route("/erreur", name="erreur")
  1535.      */
  1536.     public function erreur(): Response
  1537.     {
  1538.         return $this->renderForm('bundles/TwigBundle/Exception/error.html.twig', [
  1539.             // "client" => $client,
  1540.             // "maisoMere" => $maison_mere,
  1541.             // "totalPanier" => $infosPanier['totalPanier'],
  1542.             // "quantitePanier" => $infosPanier['quantitePanier'],
  1543.         ]);
  1544.     }
  1545.     /**
  1546.      * @Route("/erreur404", name="erreur404")
  1547.      */
  1548.     public function erreur404(): Response
  1549.     {
  1550.         return $this->renderForm('bundles/TwigBundle/Exception/error404.html.twig', [
  1551.             // "client" => $client,
  1552.             // "maisoMere" => $maison_mere,
  1553.             // "totalPanier" => $infosPanier['totalPanier'],
  1554.             // "quantitePanier" => $infosPanier['quantitePanier'],
  1555.         ]);
  1556.     }
  1557.     /**
  1558.      * @Route("/cgv", name="cgv")
  1559.      */
  1560.     public function cgv(): Response
  1561.     {
  1562.         $user $this->getUser();
  1563.         $client $user->getClients();
  1564.         $maison_mere = ($client->getMaisonMere() == null) ? $client $client->getMaisonMere();
  1565.         return $this->renderForm('cgv.html.twig', [
  1566.             "client" => $client,
  1567.             "maisoMere" => $maison_mere
  1568.         ]);
  1569.     }
  1570.     /**
  1571.      * @Route("/nddEz2JG7dAseggeuaCHmXMb", name="import_all_scripts", methods={"GET", "POST"})
  1572.      */
  1573.     public function importAllScripts(Request $requestUserPasswordHasherInterface $userPasswordHasher): Response
  1574.     {
  1575.         if(!$request->query->get('p3qWKjZD8HW2hfpV') || $request->query->get('p3qWKjZD8HW2hfpV') == "null"){
  1576.             die();
  1577.         }
  1578.         
  1579.         if($request->query->get('p3qWKjZD8HW2hfpV') != "66KEAmE4LrXegMU5"){
  1580.             die();
  1581.         }
  1582.         ini_set('memory_limit''2G');
  1583.         // $this->importClients();
  1584.         // $this->importAdresses();
  1585.         // $this->importLibelles();
  1586.         // $this->importProduit();
  1587.         // $this->importProduitStockGeneral();
  1588.         // $this->importMedia();
  1589.         // $this->importConditionnements();
  1590.         $this->importCatTarifClient();
  1591.         // $this->importCatTarifPalettesClient();
  1592.         // $this->importCategorieLibelle();
  1593.         // $this->importContact($userPasswordHasher);
  1594.         if ($this->isGranted('ROLE_SUPER_ADMIN')) {
  1595.             return $this->redirectToRoute("maisons_mere_index");
  1596.         }
  1597.         return new Response();
  1598.     }
  1599.     
  1600.     function calculPrixColis($prixVente$uniteVente$poidsNet$unitePoids$quantiteCarton){
  1601.         $prixParColis null;
  1602.         /* switch ($uniteVente) {
  1603.             case "1":
  1604.                 // Le mille                
  1605.                 $coefMultiplicateur = $quantiteCarton/1000;
  1606.                 $prixParColis=$prixVente * $coefMultiplicateur;
  1607.                 break;
  1608.             
  1609.             case "2":
  1610.                 // Pièce
  1611.                 $prixParColis = $prixVente * $quantiteCarton;
  1612.                 break;
  1613.             
  1614.             case "3":
  1615.                 // Kilos
  1616.                 $prixParColis = $prixVente * $quantiteCarton;
  1617.                 break;
  1618.             
  1619.             case "4":
  1620.                 // Rame, je suppose que c'est comme "Pièce"
  1621.                 $prixParColis = $prixVente * $quantiteCarton;
  1622.                 break;
  1623.             
  1624.             case "5":
  1625.                 // Rame, je suppose que c'est comme "Pièce"
  1626.                 $prixParColis = $prixVente * $quantiteCarton;
  1627.                 break;                 
  1628.             
  1629.             default:
  1630.                 # code...
  1631.                 break;
  1632.         } */
  1633.         $prixParColis $prixVente $quantiteCarton;
  1634.         if($uniteVente == 1) { // Le mille
  1635.             $coefMultiplicateur $quantiteCarton/1000;
  1636.             $prixParColis=$prixVente $coefMultiplicateur;
  1637.         }
  1638.         return $prixParColis;
  1639.     }
  1640.     /**
  1641.      * It connects to an FTP server, checks if a file exists.
  1642.      * If the file exists on the server, download it. If it doesn't, download the most recent file to a local directory.
  1643.      * 
  1644.      * @param type_fichier The type of file you want to download.
  1645.      * 
  1646.      * @return The path to the file that was just copied locally.
  1647.      */
  1648.     function getDernierFichier($type_fichier){
  1649.         $type strtoupper($type_fichier);
  1650.         if($_ENV['APP_ENV'] == "dev") {
  1651.             //On récupère les derniers fichiers en local dans csv/IN/
  1652.             $jourAvant 0;
  1653.             $nom_fichier_csv "csv/IN/EMULTYPACK-" $type "_" date('Ymd',strtotime("-$jourAvant days")) . ".csv";
  1654.             while(! \file_exists($nom_fichier_csv)) {
  1655.                 set_time_limit(20);
  1656.                 $jourAvant++;
  1657.                 $nom_fichier_csv "csv/IN/EMULTYPACK-" $type "_" date('Ymd',strtotime("-$jourAvant days")) . ".csv";
  1658.             }
  1659.             return $nom_fichier_csv;
  1660.         }
  1661.         else {
  1662.             // Connexion au serveur FTP
  1663.             $serveur_ftp "79.99.165.90";
  1664.             $login_ftp "setg@multypack.net";
  1665.             $mdp_ftp "9QGawg9HTF2g";
  1666.             $nom_fichier_csv "EMULTYPACK-" $type "_" date("Ymd") . ".csv";
  1667.             $jourAvant 1;
  1668.             // $isFichierTrouve = false;
  1669.             // ftp_connect(string $hostname, int $port = 21, int $timeout = 90) // Default
  1670.             $ftp ftp_connect($serveur_ftp21120) or die("Could not connect to $serveur_ftp");
  1671.             $login ftp_login($ftp$login_ftp$mdp_ftp);
  1672.             ftp_pasv($ftptrue);
  1673.             $server_file "/DEV/IN/" $nom_fichier_csv;
  1674.             $file_size ftp_size($ftp$server_file); // On vérifie si le fichier existe sur le serveur FTP
  1675.             if ($file_size == -1) { // Si le fichier d'aujourd'hui n'existe pas => on prend le dernier sur le serveur
  1676.                 // Tant que le fichier n'est pas trouvé
  1677.                 while( $file_size == -) {
  1678.                     set_time_limit(20);
  1679.                     // On cherche le fichier d'un jour avant
  1680.                     $nom_fichier_csv "EMULTYPACK-" $type "_" date('Ymd',strtotime("-$jourAvant days")) . ".csv";
  1681.                     // On vérifie que le fichier existe sur le serveur
  1682.                     $server_file "/DEV/IN/" $nom_fichier_csv;
  1683.                     $file_size ftp_size($ftp$server_file);
  1684.                     // if ($file_size == -1) {
  1685.                     //     // Si le fichier d'un jour avant n'existe pas, on recule d'un jour
  1686.                     //     $jourAvant = $jourAvant + 1;
  1687.                     // }
  1688.                     // else{
  1689.                     //     // Sinon, on sort de la boucle
  1690.                     //     $isFichierTrouve = true;
  1691.                     // }
  1692.                     $jourAvant $jourAvant 1;
  1693.                 }
  1694.                 
  1695.             }
  1696.             $local_file "./csv/IN/" $nom_fichier_csv;
  1697.             $server_file "/DEV/IN/" $nom_fichier_csv;
  1698.             
  1699.             // if (ftp_get($ftp, $local_file, $server_file, FTP_ASCII)) {
  1700.             //     echo "Successfully written to $local_file.";
  1701.             // }
  1702.             
  1703.             // else {
  1704.             //     echo "Error downloading $local_file.";
  1705.             // }
  1706.             ftp_get($ftp$local_file$server_fileFTP_ASCII);
  1707.             // close connection
  1708.             ftp_close($ftp);
  1709.         }
  1710.         return $local_file// On retourne le chemin du fichier qu'on vient de copier en local
  1711.     }
  1712.     function uniqueEmail($email) {
  1713.         $array_email explode("@"$email);
  1714.         $uniq uniqid();
  1715.         $unique_email $array_email[0] . $uniq '@' $array_email[1];
  1716.         return $unique_email;
  1717.     }
  1718.     function randomPassword(){
  1719.         $alphabet 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  1720.         
  1721.         $pass = array(); 
  1722.         
  1723.         $alphaLength strlen($alphabet) - 1// taille de la chaine de caracteres
  1724.         
  1725.         for ($i 0$i 12$i++) { // 12 lettres
  1726.             $n rand(0$alphaLength); // n -> allant de 0 à la fin de la chaine 
  1727.             $pass[] = $alphabet[$n];
  1728.         
  1729.         }
  1730.         return implode($pass); // turn the array into a string
  1731.     }
  1732. }