src/Entity/FournisseursFtp.php line 18

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: fmartin
  5.  * Date: 31/07/18
  6.  * Time: 14:49
  7.  */
  8. namespace App\Entity;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Symfony\Component\Filesystem\Filesystem;
  11. use Vich\UploaderBundle\Entity\File;
  12. /**
  13.  * @ORM\Entity()
  14.  * @ORM\Table(name="tfournisseurs_ftp")
  15.  */
  16. class FournisseursFtp
  17. {
  18.     const FTP_PASSWORD_KEY "?BienFaireVautMieuxQueB1Dire!LesNouillesNeSontPasToutesDansLaSoupe?";
  19.     //Next constants is for local folder, not in FTP
  20.     const SERVER_RACINE_IMPORT 'Resources/Imports/FacturesFournisseurs/';
  21.     const SERVER_RACINE_EXPORT 'Resources/Exports/Fournisseurs/';
  22.     const SERVER_AR_FOLDER_AR_FAC 'ARorder/';
  23.     const SERVER_EXPORT_COMMAND_FOLDER 'Commandes/';
  24.     const SERVER_ATRAITER_IMPORT_FACTURES 'atraiter/';
  25.     const SERVER_ARCHIVES_IMPORT_FACTURES 'archives/';
  26.     const IN_TYPE 0;
  27.     const OUT_TYPE 1;
  28.     const STANDARD_FTP_IN_FOLDER '/entrees/';
  29.     const STANDARD_FTP_OUT_FOLDER '/sorties/';
  30.     /**
  31.      * @ORM\Column(name="id", type="integer")
  32.      * @ORM\Id
  33.      * @ORM\GeneratedValue(strategy="AUTO")
  34.      */
  35.     protected $id;
  36.     /**
  37.      * @ORM\Column(name="url", type="string", length=255, nullable=false)
  38.      */
  39.     protected $url;
  40.     /**
  41.      * @ORM\Column(name="login", type="string", length=255, nullable=false)
  42.      */
  43.     protected $login;
  44.     /**
  45.      * @ORM\Column(name="password", type="string", length=255, nullable=false)
  46.      */
  47.     protected $password;
  48.     /**
  49.      * @ORM\ManyToOne(targetEntity="App\Entity\FournisseursCompany", cascade={"persist"})
  50.      * @ORM\JoinColumn(name="fournisseur_company", referencedColumnName="id", nullable=false)
  51.      */
  52.     protected $fournisseurCompany;
  53.     /**
  54.      * @ORM\Column(name="folder", type="string", length=255, nullable=false)
  55.      */
  56.     protected $folder;
  57.     /**
  58.      * @ORM\Column(name="archive_folder", type="string", length=255, nullable=false)
  59.      */
  60.     protected $archiveFolder;
  61.     /**
  62.      * @ORM\ManyToOne(targetEntity="App\Entity\Company", cascade={"persist"})
  63.      * @ORM\JoinColumn(name="company", referencedColumnName="id", nullable=false)
  64.      */
  65.     protected $company;
  66.     /**
  67.      * @ORM\Column(name="type", type="string", length=255, nullable=false)
  68.      */
  69.     protected $type;
  70.     /**
  71.      * @return int
  72.      */
  73.     public function getId()
  74.     {
  75.         return $this->id;
  76.     }
  77.     /**
  78.      * @param int $id
  79.      */
  80.     public function setId($id)
  81.     {
  82.         $this->id $id;
  83.     }
  84.     /**
  85.      * @return string
  86.      */
  87.     public function getUrl()
  88.     {
  89.         return $this->url;
  90.     }
  91.     /**
  92.      * @param string $url
  93.      */
  94.     public function setUrl($url)
  95.     {
  96.         $this->url $url;
  97.     }
  98.     /**
  99.      * @return string
  100.      */
  101.     public function getLogin()
  102.     {
  103.         return $this->login;
  104.     }
  105.     /**
  106.      * @param string $login
  107.      */
  108.     public function setLogin($login)
  109.     {
  110.         $this->login $login;
  111.     }
  112.     /**
  113.      * @return string
  114.      */
  115.     public function getPassword()
  116.     {
  117.         return $this->password;
  118.     }
  119.     /**
  120.      * @param string $password
  121.      */
  122.     public function setPassword($password)
  123.     {
  124.         $this->password $password;
  125.     }
  126.     /**
  127.      * @return FournisseursCompany
  128.      */
  129.     public function getFournisseurCompany()
  130.     {
  131.         return $this->fournisseurCompany;
  132.     }
  133.     /**
  134.      * @param FournisseursCompany $fournisseurCompany
  135.      */
  136.     public function setFournisseurCompany($fournisseurCompany)
  137.     {
  138.         $this->fournisseurCompany $fournisseurCompany;
  139.     }
  140.     /**
  141.      * @return string
  142.      */
  143.     public function getFolder()
  144.     {
  145.         return $this->folder;
  146.     }
  147.     /**
  148.      * @param string $folder
  149.      */
  150.     public function setFolder($folder)
  151.     {
  152.         $this->folder $folder;
  153.     }
  154.     /**
  155.      * @return mixed
  156.      */
  157.     public function getCompany()
  158.     {
  159.         return $this->company;
  160.     }
  161.     /**
  162.      * @param mixed $company
  163.      */
  164.     public function setCompany($company)
  165.     {
  166.         $this->company $company;
  167.     }
  168.     /**
  169.      * @return string
  170.      */
  171.     public function getArchiveFolder()
  172.     {
  173.         return $this->archiveFolder;
  174.     }
  175.     /**
  176.      * @param string $archiveFolder
  177.      */
  178.     public function setArchiveFolder($archiveFolder)
  179.     {
  180.         $this->archiveFolder $archiveFolder;
  181.     }
  182.     /**
  183.      * @return string
  184.      */
  185.     public function getType()
  186.     {
  187.         return $this->type;
  188.     }
  189.     /**
  190.      * @param string $type
  191.      */
  192.     public function setType($type)
  193.     {
  194.         $this->type $type;
  195.     }
  196.     /**
  197.      * @param null $racineParam
  198.      * @return bool
  199.      */
  200.     public function createImportFournisseurStructure($racineParam null)
  201.     {
  202.         $racine '';
  203.         if (!empty($racineParam)) {
  204.             $racine $racineParam;
  205.         }
  206.         $companyId $this->getCompany()->getId();
  207.         $fournisseurId $this->getFournisseurCompany()->getFournisseur()->getId();
  208.         $speFolder $racine.$this::SERVER_RACINE_IMPORT.$companyId.'/'.$fournisseurId.'/';
  209.         $fileSystem = new Filesystem();
  210.         //Create the folder for future company/fournisseur/files
  211.         if (false === $fileSystem->exists($speFolder)) {
  212.             $fileSystem->mkdir($speFolder);
  213.             chown($racine.$this::SERVER_RACINE_IMPORT'root');
  214.             chgrp($racine.$this::SERVER_RACINE_IMPORT'www-data');
  215.             chmod($racine.$this::SERVER_RACINE_IMPORT0775);
  216.             chown($racine.$this::SERVER_RACINE_IMPORT.$companyId'root');
  217.             chgrp($racine.$this::SERVER_RACINE_IMPORT.$companyId'www-data');
  218.             chmod($racine.$this::SERVER_RACINE_IMPORT.$companyId0775);
  219.             chown($speFolder'root');
  220.             chgrp($speFolder'www-data');
  221.             chmod($speFolder0775);
  222.             $atraiterFolder $speFolder.$this::SERVER_ATRAITER_IMPORT_FACTURES;
  223.             $fileSystem->mkdir($atraiterFolder);
  224.             chown ($atraiterFolder'root');
  225.             chgrp($atraiterFolder'www-data');
  226.             chmod($atraiterFolder0775);
  227.             $archiveFolder $speFolder.$this::SERVER_ARCHIVES_IMPORT_FACTURES;
  228.             $fileSystem->mkdir($archiveFolder);
  229.             chown ($archiveFolder'root');
  230.             chgrp($archiveFolder'www-data');
  231.             chmod($archiveFolder0775);
  232.             return true;
  233.         }
  234.         return false;
  235.     }
  236.     public function createExportFournisseurStructure($racineParam)
  237.     {
  238.         $racine '';
  239.         if (!empty($racineParam)) {
  240.             $racine $racineParam;
  241.         }
  242.         $companyId $this->getCompany()->getId();
  243.         $fournisseurId $this->getFournisseurCompany()->getFournisseur()->getId();
  244.         $speFolder $racine.$this::SERVER_RACINE_EXPORT.$companyId.'/'.$fournisseurId.'/';
  245.         $fileSystem = new Filesystem();
  246.         //Create the folder for future company/fournisseur/files
  247.         if (false === $fileSystem->exists($speFolder)) {
  248.             $fileSystem->mkdir($speFolder);
  249.             chgrp($speFolder'www-data');
  250.             $fileSystem->mkdir($speFolder.$this::SERVER_AR_FOLDER_AR_FAC);
  251.             chgrp($speFolder.$this::SERVER_AR_FOLDER_AR_FAC'www-data');
  252.             $fileSystem->mkdir($speFolder.$this::SERVER_EXPORT_COMMAND_FOLDER);
  253.             chgrp($speFolder.$this::SERVER_EXPORT_COMMAND_FOLDER'www-data');
  254.             return true;
  255.         }
  256.         return false;
  257.     }
  258.     /***
  259.      * Return folder structure for factures impot
  260.      * @param $racine
  261.      * @return string
  262.      */
  263.     public function getFournisseurStructure($racine)
  264.     {
  265.         $companyId $this->getCompany()->getId();
  266.         $fournisseurId $this->getFournisseurCompany()->getFournisseur()->getId();
  267.         $speFolder $racine.$companyId.'/'.$fournisseurId.'/';
  268.         return $speFolder;
  269.     }
  270.     /**
  271.      * Return LOCAL atraiter folder (not ftp)
  272.      * @return string
  273.      */
  274.     public function getFacturesAtraiterFolder()
  275.     {
  276.         return $this->getFournisseurStructure($this::SERVER_RACINE_IMPORT).$this::SERVER_ATRAITER_IMPORT_FACTURES;
  277.     }
  278.     /**
  279.      * Return LOCAL archive folder (not ftp)
  280.      * @return string
  281.      */
  282.     public function getFacturesArchivesFolder()
  283.     {
  284.         return $this->getFournisseurStructure($this::SERVER_RACINE_IMPORT).$this::SERVER_ARCHIVES_IMPORT_FACTURES;
  285.     }
  286.     /**
  287.      * Return LOCAL archive folder (not ftp)
  288.      * @return string
  289.      */
  290.     public function getFacturesAccuseReceptionFolder()
  291.     {
  292.         return $this->getFournisseurStructure($this::SERVER_RACINE_EXPORT).$this::SERVER_AR_FOLDER_AR_FAC;
  293.     }
  294.     /**
  295.      * Return LOCAL archive folder (not ftp)
  296.      * @return string
  297.      */
  298.     public function getFacturesExportCommandFolder()
  299.     {
  300.         return $this->getFournisseurStructure($this::SERVER_RACINE_EXPORT).$this::SERVER_EXPORT_COMMAND_FOLDER;
  301.     }
  302.     /**
  303.      * @param int $type
  304.      * @return bool
  305.      */
  306.     public function checkIfTypeAvailable(int $type) : bool
  307.     {
  308.         if ($this::IN_TYPE === $type || $this::OUT_TYPE === $type) {
  309.             return true;
  310.         }
  311.         return false;
  312.     }
  313. }