<?php
namespace App\Controller\GestionComerciale;
use App\Entity\GestionComerciale\Commande;
use App\Entity\GestionComerciale\CommandeValidationClient;
use App\Library\Datatable\Util\Datatable;
use App\Service\FO\SocieteService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class CommandeValidationClientController extends AbstractController
{
/**
* @Route("/public/devis/decision/", name="dtc_devis_choix_client")
*/
public function AccepteRefuserDevisAction(Request $request, EntityManagerInterface $em, SocieteService $service_societe)
{
$repo_commande_validation_client = $em->getRepository(CommandeValidationClient::class);
$commande = null;
$commandeValidationClient = null;
$jeton = "A";
$get = $request->query->all();
$valide = false;
$confirmer = false;
$devisDejaAccepte = false;
$accept = "";
$commandeValidationClient = "";
if (array_key_exists('token', $get) && $get["token"] != "") {
$commandeValidationClient = $repo_commande_validation_client->findOneBy(["jeton" => $get["token"]]);
}
if (is_object($commandeValidationClient)) {
if (array_key_exists('token', $get) && $get["token"] == $commandeValidationClient->getJeton()) {
$valide = true;
if (is_object($commandeValidationClient->getCommande())) {
$commande = $commandeValidationClient->getCommande();
} else {
$valide = false;
}
}
}
if (array_key_exists('accept', $get) && ($get["accept"] == "1" or $get["accept"] == "0")) {
$accept = intval($get["accept"]);
} else {
$valide = false;
}
if ($valide) {
if (array_key_exists('confirm', $get) && ($get["confirm"] == "1") && ! is_object($commandeValidationClient->getDateChoixClient())) {
$confirmer = true;
if ($accept == "1") {
$commandeValidationClient->setStatut(true);
} else {
$commandeValidationClient->setStatut(false);
}
$commandeValidationClient->setDateChoixClient(new \Datetime());
$em->persist($commandeValidationClient);
$em->flush();
$destinataires_mails_valide = [];
if (is_object($commande->getAssistanteCommercial()) && filter_var($commande->getAssistanteCommercial()->getEmail(), FILTER_VALIDATE_EMAIL)) {
$destinataires_mails_valide[] = $commande->getAssistanteCommercial()->getEmail();
}
if (is_object($commande->getCommercial()) && filter_var($commande->getCommercial()->getEmail(), FILTER_VALIDATE_EMAIL)) {
$destinataires_mails_valide[] = $commande->getCommercial()->getEmail();
}
if (is_object($commande->getUtilisateur()) && filter_var($commande->getUtilisateur()->getEmail(), FILTER_VALIDATE_EMAIL)) {
$destinataires_mails_valide[] = $commande->getCommercial()->getEmail();
}
if (count($destinataires_mails_valide) > 0) {
$societe = $service_societe->getSociete();
$contact = "";
if (is_object($commandeValidationClient->getContact())) {
$contact = $commandeValidationClient->getContact()->getPrenom()." ".$commandeValidationClient->getContact()->getNom()." (".$commandeValidationClient->getEmail().")";
} else {
$contact = $commandeValidationClient->getEmail();
}
if ($commandeValidationClient->getStatut()) {
$sujet_mail = "Acceptation du Devis ".$commande->getReference();
$phrase = "Le Devis ".$commande->getReference()." vient d'être accepté par ".$contact;
} else {
$sujet_mail = "Refus du Devis ".$commande->getReference();
$phrase = "Le Devis".$commande->getReference()." vient d'être refusé par ".$contact;
}
$message_mail = "<div style='line-height:13px;'>
Bonjour,<br/>
".$phrase."
</div>
";
$donnees_email = [
'titre' => $sujet_mail,
'message' => $message_mail,
'destinataires' => $destinataires_mails_valide,
'images' => "",
'documents' => "",
];
//$emailService = $this->get('dtc.email');
//$emailService->envoyerEmailClassique($donnees_email);
}
}
}
return $this->render('GestionComerciale/CommandeValidationClient/accepter_refuser_devis.html.twig', [
"commandeValidationClient" => $commandeValidationClient,
"valide" => $valide,
"accept" => intval($accept),
"confirmer" => $confirmer,
"commande" => $commande,
"devisDejaAccepte" => $devisDejaAccepte,
]);
}
/**
* @Route("", name="")
*/
public function listerAction(Request $request, $id = "0", $type = "a", EntityManagerInterface $em, Datatable $datatable, TranslatorInterface $translator)
{
$tableau_class_cellule = [];
$tableau_class_cellule[] = ["orderable" => false, "searchable" => false, "className" => "visible_export colonne_id text-center", "targets" => [0], "visible" => true];
$tableau_class_cellule[] = ["orderable" => false, "searchable" => false, "className" => "visible_export colonne_id text-center", "targets" => [1], "visible" => true];
$tableau_class_cellule[] = ["orderable" => false, "searchable" => false, "className" => "visible_export colonne_id text-center", "targets" => [2], "visible" => true];
$tableau_class_cellule[] = ["orderable" => false, "searchable" => false, "className" => "visible_export colonne_id ", "targets" => [3], "visible" => true];
$tableau_class_cellule[] = ["orderable" => false, "searchable" => false, "className" => "visible_export colonne_id ", "targets" => [4], "visible" => true];
$modal = $request->query->get('modal');
$qte = $request->query->get('qte');
if ($type == "commande") {
$repo_objet = $em->getRepository(Commande::class);
}
$objet = $repo_objet->find($id);
if (is_object($objet)) {
if ($type == "commande") {
$this->datatableCommande($objet, $type, $datatable, $translator);
}
return $this->render('GestionComerciale/CommandeValidationClient/lister.html.twig', ['tableauClassColonne' => $tableau_class_cellule, "id" => $id, "type" => $type]
);
}
}
/**
* set datatable configs
*
* @return \App\Library\Datatable\Util\Datatable
*/
private function datatableCommande($objet, $type = "", Datatable $datatable, TranslatorInterface $translator)
{
$type_jointure = 'x.'.$type;
$datatable->setDatatableId('dta-commande-validation-client')
->setEntity(CommandeValidationClient::class, "x")
->setFields(
[
$translator->trans("Date demande") => 'x.date',
$translator->trans("Date décision") => 'x.dateChoixClient',
$translator->trans("Décision") => 'x.statut',
$translator->trans("Contact") => 'x.id',
$translator->trans("Email") => 'x.email',
"_identifier_" => 'x.id',
]
)
//->addJoin('x.article', 'a', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
//->addJoin('x.articleCommande', 'ac', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN, \Doctrine\ORM\Query\Expr\Join::WITH, 'ac.article = x.id')
//->addJoin('ac.commande', 'c', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN, \Doctrine\ORM\Query\Expr\Join::WITH, 'c.id = ac.commande')
->setWhere(
'x.commande = :objet',
['objet' => $objet]
)
->setRenderers(
[
0 => [
'view' => 'FO/DataTable/heure.html.twig',
'params' => [
'commande' => $objet,
],
],
1 => [
'view' => 'FO/DataTable/heure.html.twig',
'params' => [
'commande' => $objet,
],
],
2 => [
'view' => 'FO/DataTable/booleen_null.html.twig',
'params' => [
'commande' => $objet,
],
],
3 => [
'view' => 'FO/DataTable/contact_decision.html.twig',
'params' => [
'commande' => $objet,
],
],
]
)
//->setOrder("x.id", "desc")
//->setSearch(true)
->setGroupBy("x.id")//->setSearchFields(array(1,2))
;
return $datatable;
}
/**
* @Route("/validation-client/grid/{id}/{type}", name="dtc_commande_validation_client_grid")
*/
public function gridAction(Request $request, $id = "", $type = "", EntityManagerInterface $em, Datatable $datatable, TranslatorInterface $translator)
{
if ($type == "commande") {
$repo_objet = $em->getRepository(Commande::class);
}
$objet = $repo_objet->find($id);
if (is_object($objet) and $type == "commande") {
return $this->datatableCommande($objet, $type, $datatable, $translator)->execute();
}
}
}