<?php
namespace App\Controller;
use App\Entity\Adresse;
use App\Entity\Document;
use App\Entity\DocumentLigne;
use App\Entity\LdvStatutProjet;
use App\Entity\Projet;
use App\Entity\Societe;
use App\Entity\Souche;
use App\Entity\Tiers;
use App\Repository\DocumentRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Tools\JsonRes;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use function Symfony\Component\Translation\t;
class CommandeController extends AbstractController
{
private ?ObjectManager $em = null;
private ?string $prefixSociete = null;
/**
* Stockage des informations de la société pour affichage sur entête document
* @var object|mixed|null
*/
private ?object $dataSociete = null;
public function __construct(RequestStack $requestStack, ManagerRegistry $doctrine)
{
$request = $requestStack->getCurrentRequest();
//TODO: page erreur subdomain
if(null===$request->getSession()->get('societe')) return $this->render('misc/404.html.twig');
// on récupère le sous-domaine stocké en session pour pointer vers le bon entity manager
$subdomain = $request->getSession()->get('societe')->getBase();
$this->em = $doctrine->getManager($subdomain);
$this->prefixSociete = $request->getSession()->get('societe')->getPrefix();
if(null!==$this->prefixSociete) $this->prefixSociete = strtoupper($this->prefixSociete);
$jsonString = $request->getSession()->get('societe')->getData();
if(null!==$jsonString) {
$this->dataSociete = json_decode($jsonString);
$this->dataSociete->lib_societe = $request->getSession()->get('societe')->getLibSociete();
$this->dataSociete->logo_path = $request->getSession()->get('societe')->getLogoPath();
}
}
/**
* @Route("/commande/vierge/{id}", methods={"GET"}, name="init_commande_vierge")
*/
public function getCommandeVierge($id)
{
$projetRepository = $this->em->getRepository(Projet::class);
/** @var Projet $projet */
$projet = $projetRepository->find($id);
if(null===$projet) return $this->render('misc/404.html.twig');
$commandeVierge = new Document();
$commandeVierge->initDocVierge($projet, 'COMMANDE PRESTATAIRE');
return $this->render('projet/prestataire/commande-form.html.twig', [
'commande_vierge' => $commandeVierge,
'label_btn_cancel' => 'Annuler',
]);
}
/**
* @Route("/commande/reload-list/{idProjet}", methods={"GET"}, name="commande_reload_list")
*/
public function commandeReloadList($idProjet)
{
$projetRepository = $this->em->getRepository(Projet::class);
/** @var Projet $projet */
$projet = $projetRepository->find($idProjet);
$docList = $projet->getDocuments()->filter(function (Document $document){
return $document->getTypeDocument() === 'COMMANDE PRESTATAIRE';
});
return $this->render('projet/prestataire/document-commande-list.html.twig', [
'commandeList' => $docList,
]);
}
/**
* @Route("/commande/insert", methods={"POST"}, name="insert_commande")
*/
public function insertCommande(Request $request)
{
//TODO: page erreur entity manager
if(null===$this->em) return $this->render('misc/404.html.twig');
// Initialisation du retour Json
$res = new JsonRes($request->request->all());
if(!$res->isSuccess()) die($res->returnJson());
$data = $res->getData();
if(!isset($data['id_projet'])) $res->addErrMsg('champs id_projet inconnu');
if(!isset($data['id_prestataire'])) $res->addErrMsg('champs id_prestataire inconnu');
if(!isset($data['reference_commande'])) $res->addErrMsg('champs reference_commande inconnu');
if(!isset($data['puht'])) $res->addErrMsg('champs puht inconnu');
if(!isset($data['taux_tva'])) $res->addErrMsg('champs taux_tva inconnu');
if(!isset($data['qte'])) $res->addErrMsg('champs qte inconnu');
if(!isset($data['date_commande'])) $res->addErrMsg('champs date_commande inconnu');
if(!isset($data['statut_commande'])) $res->addErrMsg('champs statut_commande inconnu');
if(!isset($data['description'])) $res->addErrMsg('champs description inconnu');
if(!$res->isSuccess()) die($res->returnJson());
if(''===($data['id_projet'])) $res->addErrMsg('ID Projet manquant');
if(''===($data['id_prestataire'])) $res->addErrMsg('ID Prestataire manquant');
if(''===($data['reference_commande'])) $res->addErrMsg('Référence commande obligatoire');
if(''===($data['puht'])) $res->addErrMsg('Prix unitaire HT obligatoire');
if(''===($data['taux_tva'])) $res->addErrMsg('Taux TVA obligatoire');
if(''===($data['qte'])) $res->addErrMsg('Quantité obligatoire');
if(''===($data['date_commande'])) $res->addErrMsg('Date commande obligatoire');
if(''===($data['statut_commande'])) $res->addErrMsg('Statut commande obligatoire');
if(!$res->isSuccess()) die($res->returnJson());
$dateBdc = date_create_from_format('Y-m-d', $data['date_commande']);
if(!$dateBdc) $res->addErrMsg('Le format de la date est incorrect');
$projetRepository = $this->em->getRepository(Projet::class);
/** @var Projet $projet */
$projet = $projetRepository->find($data['id_projet']);
if(null==$projet) $res->addErrMsg('Projet non identifié');
$societeRepository = $this->em->getRepository(Societe::class);
/** @var Societe $prestataire */
$prestataire = $societeRepository->find($data['id_prestataire']);
if(null==$prestataire) $res->addErrMsg('Prestataire non identifié');
if(!$res->isSuccess()) die($res->returnJson());
/** @var \App\Entity\User $currentUser */
$currentUser = $this->getUser()->getId();
$data['puht'] = floatval($data['puht']);
$data['taux_tva'] = floatval($data['taux_tva']);
$data['qte'] = floatval($data['qte']);
$montantHT = $data['puht'] * $data['qte'];
$montantTVA = $montantHT * $data['taux_tva'];
$commande = new Document();
$commande->setProjet($projet)
->setTiers($prestataire)
->setStatut($data['statut_commande'])
->setReference($data['reference_commande'])
->setTypeDocument('COMMANDE PRESTATAIRE')
->setDateBdc($dateBdc)
->setDateCrea(new \DateTime())
->setUserCrea($currentUser)
->setMontantHt($montantHT)
->setMontantTva($montantTVA)
->setCommentaire($data['description'])
;
$this->em->persist($commande);
$this->em->flush();
$numPiece = 'BDC-' . $commande->getId();
$commande->setNumPiece($numPiece);
$this->em->persist($commande);
$this->em->flush();
$ligne = new DocumentLigne();
$ligne->setDocument($commande)
->setLibelle($data['reference_commande'])
->setMontantHt($montantHT)
->setTauxTva($data['taux_tva'])
->setMontantTva($montantTVA)
->setDateCrea(new \DateTime())
->setUserCrea($currentUser)
->setQte($data['qte'])
->setPuht($data['puht'])
;
$commande->addDocumentLigne($ligne);
$this->em->persist($commande);
$this->em->flush();
$data['id_document'] = $commande->getId();
$res->setData($data);
die($res->returnJson());
}
/**
* @Route("/commande/update", methods={"POST"}, name="update_commande")
*/
public function updateCommande(Request $request)
{
//TODO: page erreur entity manager
if(null===$this->em) return $this->render('misc/404.html.twig');
// Initialisation du retour Json
$res = new JsonRes($request->request->all());
if(!$res->isSuccess()) die($res->returnJson());
$data = $res->getData();
if(!isset($data['id_commande'])) $res->addErrMsg('champs id_commande inconnu');
if(!isset($data['reference_commande'])) $res->addErrMsg('champs reference_commande inconnu');
if(!isset($data['puht'])) $res->addErrMsg('champs puht inconnu');
if(!isset($data['taux_tva'])) $res->addErrMsg('champs taux_tva inconnu');
if(!isset($data['qte'])) $res->addErrMsg('champs qte inconnu');
if(!isset($data['date_commande'])) $res->addErrMsg('champs date_commande inconnu');
if(!isset($data['statut_commande'])) $res->addErrMsg('champs statut_commande inconnu');
if(!isset($data['description'])) $res->addErrMsg('champs description inconnu');
if(!$res->isSuccess()) die($res->returnJson());
if(''===($data['id_commande'])) $res->addErrMsg('ID Commande manquant');
if(''===($data['reference_commande'])) $res->addErrMsg('Référence commande obligatoire');
if(''===($data['puht'])) $res->addErrMsg('Prix unitaire HT obligatoire');
if(''===($data['taux_tva'])) $res->addErrMsg('Taux TVA obligatoire');
if(''===($data['qte'])) $res->addErrMsg('Quantité obligatoire');
if(''===($data['date_commande'])) $res->addErrMsg('Date commande obligatoire');
if(''===($data['statut_commande'])) $res->addErrMsg('Statut commande obligatoire');
if(!$res->isSuccess()) die($res->returnJson());
$dateBdc = date_create_from_format('Y-m-d', $data['date_commande']);
if(!$dateBdc) $res->addErrMsg('Le format de la date est incorrect');
$documentRepository = $this->em->getRepository(Document::class);
/** @var Document $commande */
$commande = $documentRepository->find($data['id_commande']);
if(null==$commande) $res->addErrMsg('Commande non identifiée');
if(!$res->isSuccess()) die($res->returnJson());
/** @var \App\Entity\User $currentUser */
$currentUser = $this->getUser()->getId();
$data['puht'] = floatval($data['puht']);
$data['taux_tva'] = floatval($data['taux_tva']);
$data['qte'] = floatval($data['qte']);
$montantHT = $data['puht'] * $data['qte'];
$montantTVA = $montantHT * $data['taux_tva'];
$commande->setStatut($data['statut_commande'])
->setReference($data['reference_commande'])
->setDateBdc($dateBdc)
->setDateModif(new \DateTime())
->setUserModif($currentUser)
->setMontantHt($montantHT)
->setMontantTva($montantTVA)
->setCommentaire($data['description'])
;
$this->em->persist($commande);
$this->em->flush();
$ligne = $commande->getDocumentLignes()->first();
$ligne->setDocument($commande)
->setLibelle($data['reference_commande'])
->setMontantHt($montantHT)
->setTauxTva($data['taux_tva'])
->setMontantTva($montantTVA)
->setDateModif(new \DateTime())
->setUserModif($currentUser)
->setQte($data['qte'])
->setPuht($data['puht'])
;
$this->em->persist($ligne);
$this->em->flush();
$data['id_document'] = $commande->getId();
$res->setData($data);
die($res->returnJson());
}
/**
* @Route("/commande/toggle-statut", methods={"POST"}, name="commande_toggle_statut")
*/
public function toggleStatutCommande(Request $request)
{
//TODO: page erreur entity manager
if(null===$this->em) return $this->render('misc/404.html.twig');
// Initialisation du retour Json
$res = new JsonRes($request->request->all());
if(!$res->isSuccess()) die($res->returnJson());
$data = $res->getData();
if(!isset($data['id_commande'])) $res->addErrMsg('champs id_commande inconnu');
if(!$res->isSuccess()) die($res->returnJson());
if(''===$data['id_commande']) $res->addErrMsg('id_commande obligatoire');
if(!$res->isSuccess()) die($res->returnJson());
if(0===intval($data['id_commande'])) $res->addErrMsg('id_commande non conforme');
if(!$res->isSuccess()) die($res->returnJson());
$commandeRepository = $this->em->getRepository(Document::class);
/** @var Document $commande */
$commande = $commandeRepository->find($data['id_commande']);
if(null===$commande) $res->addErrMsg('Commande non identifiée');
if(!$res->isSuccess()) die($res->returnJson());
$statut = $commande->getStatut();
if('Non payée'===$statut) {
$commande->setStatut('Payée');
$commande->setDatePaiement(new \DateTime());
}
if('Payée'===$statut || null===$statut) {
$commande->setStatut('Non payée');
$commande->setDatePaiement(null);
}
$commande->setDateModif(new \DateTime());
$this->em->persist($commande);
$this->em->flush();
die($res->returnJson());
}
/**
* @Route("/commande/edit/{id}", methods={"GET"}, name="edit_commande")
*/
public function getCommandeForEdit($id)
{
$commandeRepository = $this->em->getRepository(Document::class);
$commande = $commandeRepository->find($id);
return $this->render('projet/prestataire/commande-form.html.twig', [
'commande' => $commande,
'label_btn_cancel' => 'Supprimer',
]);
}
/**
* @Route("/commande/delete-document", methods={"POST"}, name="delete_commande")
*/
public function deleteCommande(Request $request): Response
{
//TODO: page erreur entity manager
if(null===$this->em) return $this->render('misc/404.html.twig');
// Initialisation du retour Json
$res = new JsonRes($request->request->all());
if($res->isSuccess()) {
$data = $res->getData();
if(!isset($data['id_commande'])) $res->addErrMsg('champs id_commande inconnu');
if($res->isSuccess()) {
if(''===$data['id_commande']) $res->addErrMsg('champs id_commande obligatoire');
if($res->isSuccess()) {
$commandeRepository = $this->em->getRepository(Document::class);
/** @var Document $commande */
$commande = $commandeRepository->find($data['id_commande']);
if(null===$commande) $res->addErrMsg('Commande non identifiée');
if($res->isSuccess()) {
$docLigneRepository = $this->em->getRepository(DocumentLigne::class);
foreach ($commande->getDocumentLignes()->toArray() as $documentLigne) {
$docLigneRepository->remove($documentLigne);
}
$commandeRepository->remove($commande);
$res->setData($data);
}
}
}
}
die($res->returnJson());
}
}