<?php
/**
* Created by PhpStorm.
* User: fmartin
* Date: 31/07/18
* Time: 14:49
*/
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Filesystem\Filesystem;
use Vich\UploaderBundle\Entity\File;
/**
* @ORM\Entity()
* @ORM\Table(name="tfournisseurs_ftp")
*/
class FournisseursFtp
{
const FTP_PASSWORD_KEY = "?BienFaireVautMieuxQueB1Dire!LesNouillesNeSontPasToutesDansLaSoupe?";
//Next constants is for local folder, not in FTP
const SERVER_RACINE_IMPORT = 'Resources/Imports/FacturesFournisseurs/';
const SERVER_RACINE_EXPORT = 'Resources/Exports/Fournisseurs/';
const SERVER_AR_FOLDER_AR_FAC = 'ARorder/';
const SERVER_EXPORT_COMMAND_FOLDER = 'Commandes/';
const SERVER_ATRAITER_IMPORT_FACTURES = 'atraiter/';
const SERVER_ARCHIVES_IMPORT_FACTURES = 'archives/';
const IN_TYPE = 0;
const OUT_TYPE = 1;
const STANDARD_FTP_IN_FOLDER = '/entrees/';
const STANDARD_FTP_OUT_FOLDER = '/sorties/';
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="url", type="string", length=255, nullable=false)
*/
protected $url;
/**
* @ORM\Column(name="login", type="string", length=255, nullable=false)
*/
protected $login;
/**
* @ORM\Column(name="password", type="string", length=255, nullable=false)
*/
protected $password;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\FournisseursCompany", cascade={"persist"})
* @ORM\JoinColumn(name="fournisseur_company", referencedColumnName="id", nullable=false)
*/
protected $fournisseurCompany;
/**
* @ORM\Column(name="folder", type="string", length=255, nullable=false)
*/
protected $folder;
/**
* @ORM\Column(name="archive_folder", type="string", length=255, nullable=false)
*/
protected $archiveFolder;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Company", cascade={"persist"})
* @ORM\JoinColumn(name="company", referencedColumnName="id", nullable=false)
*/
protected $company;
/**
* @ORM\Column(name="type", type="string", length=255, nullable=false)
*/
protected $type;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* @param string $url
*/
public function setUrl($url)
{
$this->url = $url;
}
/**
* @return string
*/
public function getLogin()
{
return $this->login;
}
/**
* @param string $login
*/
public function setLogin($login)
{
$this->login = $login;
}
/**
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* @param string $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* @return FournisseursCompany
*/
public function getFournisseurCompany()
{
return $this->fournisseurCompany;
}
/**
* @param FournisseursCompany $fournisseurCompany
*/
public function setFournisseurCompany($fournisseurCompany)
{
$this->fournisseurCompany = $fournisseurCompany;
}
/**
* @return string
*/
public function getFolder()
{
return $this->folder;
}
/**
* @param string $folder
*/
public function setFolder($folder)
{
$this->folder = $folder;
}
/**
* @return mixed
*/
public function getCompany()
{
return $this->company;
}
/**
* @param mixed $company
*/
public function setCompany($company)
{
$this->company = $company;
}
/**
* @return string
*/
public function getArchiveFolder()
{
return $this->archiveFolder;
}
/**
* @param string $archiveFolder
*/
public function setArchiveFolder($archiveFolder)
{
$this->archiveFolder = $archiveFolder;
}
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @param string $type
*/
public function setType($type)
{
$this->type = $type;
}
/**
* @param null $racineParam
* @return bool
*/
public function createImportFournisseurStructure($racineParam = null)
{
$racine = '';
if (!empty($racineParam)) {
$racine = $racineParam;
}
$companyId = $this->getCompany()->getId();
$fournisseurId = $this->getFournisseurCompany()->getFournisseur()->getId();
$speFolder = $racine.$this::SERVER_RACINE_IMPORT.$companyId.'/'.$fournisseurId.'/';
$fileSystem = new Filesystem();
//Create the folder for future company/fournisseur/files
if (false === $fileSystem->exists($speFolder)) {
$fileSystem->mkdir($speFolder);
chown($racine.$this::SERVER_RACINE_IMPORT, 'root');
chgrp($racine.$this::SERVER_RACINE_IMPORT, 'www-data');
chmod($racine.$this::SERVER_RACINE_IMPORT, 0775);
chown($racine.$this::SERVER_RACINE_IMPORT.$companyId, 'root');
chgrp($racine.$this::SERVER_RACINE_IMPORT.$companyId, 'www-data');
chmod($racine.$this::SERVER_RACINE_IMPORT.$companyId, 0775);
chown($speFolder, 'root');
chgrp($speFolder, 'www-data');
chmod($speFolder, 0775);
$atraiterFolder = $speFolder.$this::SERVER_ATRAITER_IMPORT_FACTURES;
$fileSystem->mkdir($atraiterFolder);
chown ($atraiterFolder, 'root');
chgrp($atraiterFolder, 'www-data');
chmod($atraiterFolder, 0775);
$archiveFolder = $speFolder.$this::SERVER_ARCHIVES_IMPORT_FACTURES;
$fileSystem->mkdir($archiveFolder);
chown ($archiveFolder, 'root');
chgrp($archiveFolder, 'www-data');
chmod($archiveFolder, 0775);
return true;
}
return false;
}
public function createExportFournisseurStructure($racineParam)
{
$racine = '';
if (!empty($racineParam)) {
$racine = $racineParam;
}
$companyId = $this->getCompany()->getId();
$fournisseurId = $this->getFournisseurCompany()->getFournisseur()->getId();
$speFolder = $racine.$this::SERVER_RACINE_EXPORT.$companyId.'/'.$fournisseurId.'/';
$fileSystem = new Filesystem();
//Create the folder for future company/fournisseur/files
if (false === $fileSystem->exists($speFolder)) {
$fileSystem->mkdir($speFolder);
chgrp($speFolder, 'www-data');
$fileSystem->mkdir($speFolder.$this::SERVER_AR_FOLDER_AR_FAC);
chgrp($speFolder.$this::SERVER_AR_FOLDER_AR_FAC, 'www-data');
$fileSystem->mkdir($speFolder.$this::SERVER_EXPORT_COMMAND_FOLDER);
chgrp($speFolder.$this::SERVER_EXPORT_COMMAND_FOLDER, 'www-data');
return true;
}
return false;
}
/***
* Return folder structure for factures impot
* @param $racine
* @return string
*/
public function getFournisseurStructure($racine)
{
$companyId = $this->getCompany()->getId();
$fournisseurId = $this->getFournisseurCompany()->getFournisseur()->getId();
$speFolder = $racine.$companyId.'/'.$fournisseurId.'/';
return $speFolder;
}
/**
* Return LOCAL atraiter folder (not ftp)
* @return string
*/
public function getFacturesAtraiterFolder()
{
return $this->getFournisseurStructure($this::SERVER_RACINE_IMPORT).$this::SERVER_ATRAITER_IMPORT_FACTURES;
}
/**
* Return LOCAL archive folder (not ftp)
* @return string
*/
public function getFacturesArchivesFolder()
{
return $this->getFournisseurStructure($this::SERVER_RACINE_IMPORT).$this::SERVER_ARCHIVES_IMPORT_FACTURES;
}
/**
* Return LOCAL archive folder (not ftp)
* @return string
*/
public function getFacturesAccuseReceptionFolder()
{
return $this->getFournisseurStructure($this::SERVER_RACINE_EXPORT).$this::SERVER_AR_FOLDER_AR_FAC;
}
/**
* Return LOCAL archive folder (not ftp)
* @return string
*/
public function getFacturesExportCommandFolder()
{
return $this->getFournisseurStructure($this::SERVER_RACINE_EXPORT).$this::SERVER_EXPORT_COMMAND_FOLDER;
}
/**
* @param int $type
* @return bool
*/
public function checkIfTypeAvailable(int $type) : bool
{
if ($this::IN_TYPE === $type || $this::OUT_TYPE === $type) {
return true;
}
return false;
}
}