Last active
November 30, 2020 10:47
-
-
Save DennisdeBest/1a1d182f9c1cc6b3531b4661050e111a to your computer and use it in GitHub Desktop.
kek
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Model; | |
use App\Entity\Adresse; | |
use App\Entity\Client; | |
use App\Entity\DetailsDevis; | |
use App\Entity\Entreprise; | |
use App\Entity\Produit; | |
use App\Entity\Site; | |
use App\Entity\User; | |
use PhpOffice\PhpSpreadsheet\IOFactory; | |
use PhpOffice\PhpSpreadsheet\Reader\Exception; | |
use PhpOffice\PhpSpreadsheet\Spreadsheet; | |
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; | |
use PhpOffice\PhpSpreadsheet\Writer\IWriter; | |
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Dompdf; | |
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf; | |
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; | |
use Psr\Log\LoggerInterface; | |
use Symfony\Component\Mailer\MailerInterface; | |
use Symfony\Component\Mime\Email; | |
class DevisGenerator | |
{ | |
/** | |
* @var LoggerInterface | |
*/ | |
private LoggerInterface $logger; | |
public function __construct(LoggerInterface $logger) | |
{ | |
$this->logger = $logger; | |
} | |
public function runDevis($user, $client, $site, $produits_devis, $nb_lignes_devis, $adresse_site_select, $entreprise_client, $valeur, MailerInterface $mailer) | |
{ | |
try { | |
$path = $this->createFolderDevis(); | |
$spreadsheet = $this->loadexcelTemplate(); | |
$uuid = $this->uniqidReal(); | |
$filename = $this->writeName($spreadsheet, $uuid, $path); | |
$differentsEntities = $this->checkIfSiteIsDifferent($client, $site); | |
$this->writeEmployeeInfo($spreadsheet, $uuid, $path, $user); | |
$this->writeEntrepriseInfo($spreadsheet, $uuid, $path, $entreprise_client, $adresse_site_select, $client); | |
if ($differentsEntities === false) { | |
$this->writeSiteInfo($spreadsheet, $uuid, $path, $site); | |
} | |
$this->writeCustomerInfo($spreadsheet, $uuid, $path, $client); | |
$this->writeDate($spreadsheet, $uuid, $path); | |
$this->insertRowsNeeded($spreadsheet, $uuid, $path, $nb_lignes_devis); | |
$this->fillProductsRows($spreadsheet, $uuid, $path, $nb_lignes_devis, $produits_devis); | |
$this->setPrice($spreadsheet, $uuid, $path, $nb_lignes_devis, $produits_devis, $valeur); | |
$devisPdf = $this->convertPdf($filename, $path); | |
$this->EmailPDF($user, $client, $devisPdf, $mailer); | |
return ['devis_pdf' => $devisPdf, 'nom_commande' => $filename]; | |
} catch (\Exception $e) { | |
$this->logger->critical($e->getMessage()); | |
} | |
} | |
public function createFolderDevis() | |
{ | |
try { | |
if (!file_exists('devis/xlsx')) { | |
mkdir('devis/xlsx', 0777, true); | |
} | |
$path = realpath('devis/xlsx'); | |
} catch (Exception $e) { | |
} | |
return $path; | |
} | |
/** | |
* @return mixed | |
* @throws Exception | |
*/ | |
public function loadexcelTemplate() | |
{ | |
// $folders = scandir('/mnt/c/0Perso/LinuxSub/spie_symfony/assets/devis/'); | |
// print_r($folders); | |
//die(); | |
$spreadsheet = IOFactory::load('ressources/devis/Modele_Devis.xlsx'); | |
return $spreadsheet; | |
} | |
/** | |
* @param int $lenght | |
* @return false|string | |
* @throws \Exception | |
*/ | |
public function uniqidReal($lenght = 13) | |
{ | |
// uniqid gives 13 chars, but you could adjust it to your needs. | |
if (function_exists("random_bytes")) { | |
$bytes = random_bytes(ceil($lenght / 2)); | |
} elseif (function_exists("openssl_random_pseudo_bytes")) { | |
$bytes = openssl_random_pseudo_bytes(ceil($lenght / 2)); | |
} else { | |
throw new Exception("no cryptographically secure random function available"); | |
} | |
return substr(bin2hex($bytes), 0, $lenght); | |
} | |
/** | |
* @param Spreadsheet $spreadsheet | |
* @param $uuid | |
* @param $path | |
* @return mixed | |
* @throws \Exception | |
*/ | |
public function writeName(Spreadsheet $spreadsheet, $uuid, $path) | |
{ | |
/** @var Worksheet $worksheet */ | |
$worksheet = $spreadsheet->getActiveSheet(); | |
$worksheet->getCell('C10')->setValue('SPICS_' . $uuid); | |
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); | |
$filename = $uuid; | |
$writer->save($path . '/' . $filename . '.xlsx'); | |
return $filename; | |
} | |
/** | |
* @param Spreadsheet $spreadsheet | |
* @param $uuid | |
* @param $path | |
* @param User $user | |
* @return mixed | |
* @throws \Exception | |
*/ | |
public function writeEmployeeInfo(Spreadsheet $spreadsheet, $uuid, $path, User $user) | |
{ | |
/** @var Worksheet $worksheet */ | |
$worksheet = $spreadsheet->getActiveSheet(); | |
$worksheet->getCell('C13')->setValue($user->getPrenom() . " " . $user->getNom()); | |
$worksheet->getCell('C14')->setValue('8 rue Ariane'); | |
$worksheet->getCell('c16')->setValue($user->getTelephone()); | |
$worksheet->getCell('F16')->setValue(" "); | |
$worksheet->getCell('C17')->setValue($user->getFax()); | |
$worksheet->getCell('C18')->setValue($user->getEmail()); | |
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); | |
$filename = $uuid; | |
$writer->save($path . '/' . $filename . '.xlsx'); | |
return $filename; | |
} | |
/** | |
* @param Spreadsheet $spreadsheet | |
* @param $uuid | |
* @param $path | |
* @param Client $client | |
* @return mixed | |
* @throws Exception | |
*/ | |
public function writeCustomerInfo(Spreadsheet $spreadsheet, $uuid, $path, Client $client) | |
{ | |
/** @var Worksheet $worksheet */ | |
$worksheet = $spreadsheet->getActiveSheet(); | |
$worksheet->getCell('M13')->setValue($client->getPrenom() . " " . $client->getNom()); | |
$worksheet->getCell('M14')->setValue($client->getClientFonction()); | |
$worksheet->getCell('M15')->setValue($client->getTelephone()); | |
$worksheet->getCell('M16')->setValue(' '); | |
$worksheet->getCell('M17')->setValue($client->getEmail()); | |
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); | |
$filename = $uuid; | |
$writer->save($path . '/' . $filename . '.xlsx'); | |
return $filename; | |
} | |
/** | |
* @param Spreadsheet $spreadsheet | |
* @param $uuid | |
* @param $path | |
* @param Entreprise $entreprise_client | |
* @param Adresse $adresse_site_select | |
* @param Client $client | |
* @return mixed | |
* @throws \Exception | |
*/ | |
public function writeEntrepriseInfo(Spreadsheet $spreadsheet, $uuid, $path, Entreprise $entreprise_client, Adresse $adresse_site_select, Client $client) | |
{ | |
/** @var $worksheet Worksheet */ | |
$worksheet = $spreadsheet->getActiveSheet(); | |
$worksheet->getCell('M19')->setValue($entreprise_client->getEntrepriseRaisonSociale()); | |
$worksheet->getCell('M20')->setValue($adresse_site_select->getAdresseLigne1()); | |
$worksheet->getCell('M21')->setValue($adresse_site_select->getAdresseLigne2()); | |
$worksheet->getCell('O21')->setValue($adresse_site_select->getAdresseLigne3()); | |
$worksheet->getCell('M23')->setValue($entreprise_client->getEntrepriseSiret()); | |
$worksheet->getCell('Q23')->setValue($entreprise_client->getEntrepriseAPE()); | |
$worksheet->getCell('M24')->setValue($client->getSite()->getNumeroCompteClient()); | |
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); | |
$filename = $uuid; | |
$writer->save($path . '/' . $filename . '.xlsx'); | |
return $filename; | |
} | |
/** | |
* @param Spreadsheet $spreadsheet | |
* @param $uuid | |
* @param $path | |
* @return mixed | |
* @throws \Exception | |
*/ | |
public function writeDate(Spreadsheet $spreadsheet, $uuid, $path) | |
{ | |
/** @var $worksheet Worksheet */ | |
$worksheet = $spreadsheet->getActiveSheet(); | |
$today = getdate(); | |
$jour = (strlen($today['mday']) == 1) ? "0" . $today['mday'] : $today['mday']; | |
$mois = (strlen($today['mon']) == 1) ? "0" . $today['mon'] : $today['mon']; | |
$annee = substr($today['year'], -2); | |
$date = $jour . '/' . $mois . '/' . $annee; | |
$worksheet->getCell('k10')->setValue($date); | |
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); | |
$filename = $uuid; | |
$writer->save($path . '/' . $filename . '.xlsx'); | |
return $filename; | |
} | |
public function checkIfSiteIsDifferent($client, $site) | |
{ | |
if ($client->getSite()->getId() === $site->getId()) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* @param Spreadsheet $spreadsheet | |
* @param $uuid | |
* @param $path | |
* @param Site $site | |
* @return mixed | |
* @throws \Exception | |
*/ | |
public function writeSiteInfo(Spreadsheet $spreadsheet, $uuid, $path, Site $site) | |
{ | |
/** @var $worksheet Worksheet */ | |
$worksheet = $spreadsheet->getActiveSheet(); | |
$worksheet->getCell('D20')->setValue($site->getSiteRaisonSociale()); | |
$worksheet->getCell('D21')->setValue($site->getAdresseId()->getAdresseLigne1()); | |
$worksheet->getCell('D22')->setValue($site->getAdresseId()->getAdresseLigne2()); | |
$worksheet->getCell('F22')->setValue($site->getAdresseId()->getAdresseLigne3()); | |
$worksheet->getCell('D23')->setValue($site->getEntreprise()->getEntrepriseSiret()); | |
$worksheet->getCell('H23')->setValue($site->getSiteAPE()); | |
$worksheet->getCell('D24')->setValue($site->getNumeroCompteClient()); | |
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); | |
$filename = $uuid; | |
$writer->save($path . '/' . $filename . '.xlsx'); | |
return $filename; | |
} | |
/** | |
* @param Spreadsheet $spreadsheet | |
* @param $uuid | |
* @param $path | |
* @param $nb_lignes_devis | |
* @return mixed | |
* @throws \Exception | |
*/ | |
public function insertRowsNeeded(Spreadsheet $spreadsheet, $uuid, $path, $nb_lignes_devis) | |
{ | |
$nb_row = $nb_lignes_devis; | |
/** @var $worksheet Worksheet */ | |
$worksheet = $spreadsheet->getActiveSheet(); | |
if ($nb_row === 1) { | |
$worksheet->removeRow(28); | |
$worksheet->removeRow(29); | |
} elseif ($nb_row === 2) { | |
$worksheet->removeRow(30); | |
} elseif ($nb_row > 3) { | |
$row_insert = 30; | |
$worksheet->insertNewRowBefore($row_insert, $nb_row - 3); | |
for ($i = $row_insert; $i < $row_insert + ($nb_row - 3); $i++) { | |
$worksheet->mergeCells("A" . $i . ":B" . $i); | |
$worksheet->mergeCells("C" . $i . ":L" . $i); | |
$worksheet->mergeCells("N" . $i . ":O" . $i); | |
$worksheet->mergeCells("P" . $i . ":Q" . $i); | |
} | |
} | |
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); | |
$filename = $uuid; | |
$writer->save($path . '/' . $filename . '.xlsx'); | |
return $filename; | |
} | |
/** | |
* @param Spreadsheet $spreadsheet | |
* @param $uuid | |
* @param $path | |
* @param $nb_lignes_devis | |
* @param $produits_devis | |
* @return mixed | |
* @throws \Exception | |
*/ | |
public function fillProductsRows(Spreadsheet $spreadsheet, $uuid, $path, $nb_lignes_devis, $produits_devis) | |
{ | |
/** | |
* @var $worksheet Worksheet | |
* @var $detailsproduits Produit | |
* @var $detailsDevis DetailsDevis | |
*/ | |
$nb_row = $nb_lignes_devis; | |
$worksheet = $spreadsheet->getActiveSheet(); | |
$row_insert = 28; | |
for ($i = $row_insert; $i < $row_insert + $nb_row;) { | |
foreach ($produits_devis as $produit_devis) { | |
foreach ($produit_devis->getProduits() as $produit) { | |
$worksheet->getCell('A' . $i)->setValue($produit->getProduitReferenceConstructeur()); | |
$worksheet->getCell('C' . $i)->setValue($produit->getProduitDescription()); | |
$worksheet->getCell('M' . $i)->setValue($produit_devis->getQuantite()); | |
$worksheet->getCell('N' . $i)->setValue($produit_devis->getPrixVente()); | |
$worksheet->getCell('P' . $i)->setValue($produit_devis->getQuantite() * $produit_devis->getPrixVente()); | |
$i++; | |
} | |
} | |
} | |
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); | |
$filename = $uuid; | |
$writer->save($path . '/' . $filename . '.xlsx'); | |
return $filename; | |
} | |
/** | |
* @param Spreadsheet $spreadsheet | |
* @param $uuid | |
* @param $path | |
* @param $nb_lignes_devis | |
* @param $valeur | |
* @return mixed | |
* @throws \Exception | |
*/ | |
public function setPrice(Spreadsheet $spreadsheet, $uuid, $path, $nb_lignes_devis, $valeur) | |
{ | |
/** | |
* @var $worksheet Worksheet | |
* @var $detailsproduits Produit | |
* @var $detailsDevis DetailsDevis | |
*/ | |
$colonne_prix = 'P'; | |
$ligne_prix_ht = 32; | |
$ligne_prix_tva = 33; | |
$ligne_prix_ttc = 34; | |
function calculTVA($valeur) | |
{ | |
$tva = $valeur * (20 / 100); | |
return $tva; | |
} | |
$worksheet = $spreadsheet->getActiveSheet(); | |
if ($nb_lignes_devis === 3) { | |
$worksheet->getCell($colonne_prix . $ligne_prix_ht)->setValue($valeur); | |
$worksheet->getCell($colonne_prix . $ligne_prix_tva)->setValue(calculTVA($valeur)); | |
$worksheet->getCell($colonne_prix . $ligne_prix_ttc)->setValue(calculPrixTTC($valeur)); | |
} elseif ($nb_lignes_devis === 1) { | |
$worksheet->getCell($colonne_prix . ($ligne_prix_ht - 2))->setValue($valeur); | |
$worksheet->getCell($colonne_prix . ($ligne_prix_tva - 2))->setValue(calculTVA($valeur)); | |
$worksheet->getCell($colonne_prix . ($ligne_prix_ttc - 2))->setValue(calculPrixTTC($valeur)); | |
} elseif ($nb_lignes_devis === 2) { | |
$worksheet->getCell($colonne_prix . ($ligne_prix_ht - 1))->setValue($valeur); | |
$worksheet->getCell($colonne_prix . ($ligne_prix_tva - 1))->setValue(calculTVA($valeur)); | |
$worksheet->getCell($colonne_prix . ($ligne_prix_ttc - 1))->setValue(calculPrixTTC($valeur)); | |
} elseif ($nb_lignes_devis > 3) { | |
$worksheet->getCell($colonne_prix . ($ligne_prix_ht + ($nb_lignes_devis - 3)))->setValue($valeur); | |
$worksheet->getCell($colonne_prix . ($ligne_prix_tva + ($nb_lignes_devis - 3)))->setValue(calculTVA($valeur)); | |
$worksheet->getCell($colonne_prix . ($ligne_prix_ttc + ($nb_lignes_devis - 3)))->setValue(calculPrixTTC($valeur)); | |
} | |
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); | |
$filename = $uuid; | |
$writer->save($path . '/' . $filename . '.xlsx'); | |
return $filename; | |
} | |
/** | |
* @param $filename | |
* @param $path | |
* @return string | |
* @throws \Exception | |
*/ | |
public function convertPdf($filename, $path) | |
{ | |
shell_exec("libreoffice --headless --convert-to pdf:calc_pdf_Export --outdir devis/pdf/ " . $path . '/' . $filename . ".xlsx"); | |
$devisPdf = "devis/pdf/" . $filename . ".pdf"; | |
echo $devisPdf; | |
return $devisPdf; | |
} | |
public function EmailPDF($user, $client, $devisPdf, MailerInterface $mailer) | |
{ | |
$email = (new Email()) | |
->from('ne_pas_repondre@spics.net') | |
->to($client->getEmail()) | |
->addCc($user->getEmail()) | |
->attachFromPath($devisPdf, 'Devis.pdf', 'application/pdf') | |
->subject("Votre devis SPICS") | |
->text('Bonjour, Ci joint votre devis demandé ce jour.'); | |
$mailer->send($email); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment