<?php
namespace App\Controller\Kanban;
use App\Entity\Clients\Client;
use App\Entity\Kanban\Colonne;
use App\Entity\Kanban\Fiche;
use App\Entity\Notes\Categorie;
use App\Entity\Notes\Note;
use App\Entity\Projets\ProjetUtilisateur;
use App\Entity\Utilisateur\Contact;
use App\Library\Datatable\Util\Datatable;
use App\Security\Voter\EntityVoter;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class AffaireController extends AbstractController
{
/**
* @Route("/tunnels/affaire/modifier-contact/{id}/{contact}", name="dtc_kanban_fiche_contact_changer")
*/
public function changeContactAction(Request $request, Fiche $fiche, Contact $contact = null, EntityManagerInterface $em)
{
if (is_object($contact)) {
$fiche->setContact($contact);
} else {
$fiche->setContact(null);
}
$em->persist($fiche);
$em->flush();
$headers = [
'Content-Type' => 'application/json',
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'POST',
];
return new JsonResponse(['data' => '1']);
}
/**
* @Route("/tunnels/affaire/afficher/{id}", name="dtc_kanban_affaire_fiche")
*/
public function afficherAction(Request $request, Fiche $fiche, EntityManagerInterface $em)
{
$user = $this->getUser();
//$droit = $this->isGranted(EntityVoter::SHOW, "DTCKanbanBundle:Fiche");
$droit = false;
if (is_object($user->getType()) and ($user->getType()->getId() != "1" and $user->getType()->getId() != "2")) {
if ($fiche->getUtilisateur()->getId() == $user->getId()) {
$droit = true;
} else {
//$repo_equipe = $em->getRepository('DTCKanbanBundle:Equipe');
$repo_equipe = $em->getRepository(ProjetUtilisateur::class);
$equipier_obj = $repo_equipe->findOneBy(["fiche" => $fiche, "utilisateur" => $user]);
if (is_object($equipier_obj)) {
$droit = true;
}
}
} else {
$droit = true;
}
if ( ! $droit) {
$referer = $request->headers->get('referer');
$currentUrl = $request->getUri();
$this->addFlash('warning', 'Accès non autorisé');
if ($referer != "" && $referer != $currentUrl) {
return $this->redirect($referer);
} else {
return $this->redirectToRoute('dtcfo_homepage', []);
}
}
$titre_construit = " ";
$adresseMail = "";
$telephone = "";
if ($fiche->getBudget() != "") {
$titre_construit .= " / ".number_format($fiche->getBudget(), 2, ',', ' ')."€ ";
}
if (is_object($fiche->getClient())) {
$titre_construit .= "<div class='gauche' style='_font-size:12px;margin-left:10px;'>".$fiche->getClient()->getNom()."</div>";
if ($fiche->getClient()->getEmail() != "") {
$titre_construit .= "<div class='gauche' style='_font-size:12px;margin-left:10px;'><a href='mailto:".$fiche->getClient()->getEmail()."'>".$fiche->getClient()->getEmail(
)."</a></div>";
}
if ($fiche->getClient()->getTelephone() != "") {
$titre_construit .= "<div class='gauche' style='_font-size:12px;margin-left:10px;'><a href='phone:".$fiche->getClient()->getTelephone()."'>".$fiche->getClient(
)->getTelephone()."</a></div>";
}
}
$titre_modal = "<div class='gauche' style=''>".$fiche->getLibelle()." : </div>".$titre_construit;
$titre_modal = $fiche->getLibelle();
$errors = "";
$parent = "";
$url = "";
//$fichier->setVersion("1");
$repo_notes = $em->getRepository(Note::class);
$repo_colonnes = $em->getRepository(Colonne::class);
$repo_contact = $em->getRepository(Contact::class);
$contactDefaut = "";
if (is_object($fiche->getContact())) {
$contactDefaut = $fiche->getContact();
} elseif (is_object($fiche->getClient())) {
$contactDefaut = $repo_contact->getContactDefautClient($fiche->getClient());
}
$categories_notes = $em->getRepository(Categorie::class)->findAll();;
$notes = $repo_notes->findBy(["fiche" => $fiche], ["dateDebut" => "DESC"]);
$tempsRestant = "0";
$pourcentagePasse = "0";
$now = time(); // or your date as well
$your_date = strtotime($fiche->getDate()->format("Y-m-d"));
$datediff = $now - $your_date;
$tempsPasse = round($datediff / (60 * 60 * 24));
if (is_object($fiche->getDatePrevisionelle())) {
$now = time();// or your date as well
$your_date = strtotime($fiche->getDatePrevisionelle()->format("Y-m-d"));
$datediff = $your_date - $now;
$tempsRestant = round($datediff / (60 * 60 * 24));
$now = time($fiche->getDate()->format("Y-m-d"));// or your date as well
$your_date = strtotime($fiche->getDatePrevisionelle()->format("Y-m-d"));
$datediff = $your_date - $now;
$tempsTotal = round($datediff / (60 * 60 * 24));
$pourcentagePasse = 0;
if ($tempsTotal > 0) {
$pourcentagePasse = round($tempsPasse * 100 / $tempsTotal);
}
}
//$colonnes = $repo_colonnes->findBy(array("kanban"=>$fiche->getColonne()->getKanban()),array("position"=>"ASC"));
$colonnes = [];
if (is_object($fiche->getColonne()) && is_object($fiche->getColonne()->getKanban()->getColonne())) {
$colonnes = $repo_colonnes->findBy(["kanban" => $fiche->getColonne()->getKanban()->getColonne()->getKanban()], ["position" => "ASC"]);
} elseif (is_object($fiche->getColonne())) {
$colonnes = $repo_colonnes->findBy(["kanban" => $fiche->getColonne()->getKanban()], ["position" => "ASC"]);
}
return $this->render(
'Kanban/Affaire/afficher.html.twig',
[
'contactDefaut' => $contactDefaut,
'colonnes' => $colonnes,
'fiche' => $fiche,
'notes' => $notes,
'errors' => $errors,
"pourcentagePasse" => $pourcentagePasse,
"tempsRestant" => $tempsRestant,
"tempsPasse" => $tempsPasse,
"categories_notes" => $categories_notes,
]
);
//$rendu = $this->renderView('Kanban/Fiche/afficher_modal.html.twig', array('contactDefaut'=>$contactDefaut,'colonnes'=>$colonnes,'fiche'=>$fiche,'notes'=>$notes,'errors'=>$errors,"pourcentagePasse"=>$pourcentagePasse,"tempsRestant"=>$tempsRestant,"tempsPasse"=>$tempsPasse,"categories_notes"=>$categories_notes));
$rendu = $this->renderView(
'Kanban/Affaire/afficher.html.twig',
[
'contactDefaut' => $contactDefaut,
'colonnes' => $colonnes,
'fiche' => $fiche,
'notes' => $notes,
'errors' => $errors,
"pourcentagePasse" => $pourcentagePasse,
"tempsRestant" => $tempsRestant,
"tempsPasse" => $tempsPasse,
"categories_notes" => $categories_notes,
]
);
return new JsonResponse(['rendu' => $rendu, 'titre' => $titre_modal]);
}
/**
* @Route("/tunnels/affaire/afficher2/{id}", name="dtc_kanban_affaire_fiche_2")
*/
public function afficher2Action(Request $request, Fiche $fiche, EntityManagerInterface $em)
{
$titre_construit = " ";
$adresseMail = "";
$telephone = "";
if ($fiche->getBudget() != "") {
$titre_construit .= " / ".number_format($fiche->getBudget(), 2, ',', ' ')."€ ";
}
if (is_object($fiche->getClient())) {
$titre_construit .= "<div class='gauche' style='_font-size:12px;margin-left:10px;'>".$fiche->getClient()->getNom()."</div>";
if ($fiche->getClient()->getEmail() != "") {
$titre_construit .= "<div class='gauche' style='_font-size:12px;margin-left:10px;'><a href='mailto:".$fiche->getClient()->getEmail()."'>".$fiche->getClient()->getEmail(
)."</a></div>";
}
if ($fiche->getClient()->getTelephone() != "") {
$titre_construit .= "<div class='gauche' style='_font-size:12px;margin-left:10px;'><a href='phone:".$fiche->getClient()->getTelephone()."'>".$fiche->getClient(
)->getTelephone()."</a></div>";
}
}
$titre_modal = "<div class='gauche' style=''>".$fiche->getLibelle()." : </div>".$titre_construit;
$titre_modal = $fiche->getLibelle();
$errors = "";
$parent = "";
$url = "";
//$fichier->setVersion("1");
$repo_notes = $em->getRepository(Note::class);
$repo_colonnes = $em->getRepository(Colonne::class);
$repo_contact = $em->getRepository(Contact::class);
$contactDefaut = "";
if (is_object($fiche->getClient())) {
$contactDefaut = $repo_contact->getContactDefautClient($fiche->getClient());
}
$categories_notes = $em->getRepository(Categorie::class)->findAll();;
$notes = $repo_notes->findBy(["fiche" => $fiche], ["dateDebut" => "DESC"]);
$tempsRestant = "0";
$pourcentagePasse = "0";
$now = time(); // or your date as well
$your_date = strtotime($fiche->getDate()->format("Y-m-d"));
$datediff = $now - $your_date;
$tempsPasse = round($datediff / (60 * 60 * 24));
if (is_object($fiche->getDatePrevisionelle())) {
$now = time();// or your date as well
$your_date = strtotime($fiche->getDatePrevisionelle()->format("Y-m-d"));
$datediff = $your_date - $now;
$tempsRestant = round($datediff / (60 * 60 * 24));
$now = time($fiche->getDate()->format("Y-m-d"));// or your date as well
$your_date = strtotime($fiche->getDatePrevisionelle()->format("Y-m-d"));
$datediff = $your_date - $now;
$tempsTotal = round($datediff / (60 * 60 * 24));
$pourcentagePasse = round($tempsPasse * 100 / $tempsTotal);
}
//$colonnes = $repo_colonnes->findBy(array("kanban"=>$fiche->getColonne()->getKanban()),array("position"=>"ASC"));
$colonnes = $repo_colonnes->findBy(["kanban" => "1"], ["position" => "ASC"]);
return $this->render(
'Kanban/Affaire/afficher_2.html.twig',
[
'contactDefaut' => $contactDefaut,
'colonnes' => $colonnes,
'fiche' => $fiche,
'notes' => $notes,
'errors' => $errors,
"pourcentagePasse" => $pourcentagePasse,
"tempsRestant" => $tempsRestant,
"tempsPasse" => $tempsPasse,
"categories_notes" => $categories_notes,
]
);
//$rendu = $this->renderView('Kanban/Fiche/afficher_modal.html.twig', array('contactDefaut'=>$contactDefaut,'colonnes'=>$colonnes,'fiche'=>$fiche,'notes'=>$notes,'errors'=>$errors,"pourcentagePasse"=>$pourcentagePasse,"tempsRestant"=>$tempsRestant,"tempsPasse"=>$tempsPasse,"categories_notes"=>$categories_notes));
$rendu = $this->renderView(
'Kanban/Affaire/afficher_2.html.twig',
[
'contactDefaut' => $contactDefaut,
'colonnes' => $colonnes,
'fiche' => $fiche,
'notes' => $notes,
'errors' => $errors,
"pourcentagePasse" => $pourcentagePasse,
"tempsRestant" => $tempsRestant,
"tempsPasse" => $tempsPasse,
"categories_notes" => $categories_notes,
]
);
return new JsonResponse(['rendu' => $rendu, 'titre' => $titre_modal]);
}
/**
* @Route("", name="")
*/
public function listerAction(Request $request, $id, $type = "", EntityManagerInterface $em, Datatable $datatable, TranslatorInterface $translator)
{
if ($type == 'client') {
$repo_objet = $em->getRepository(Client::class);
$objet = $repo_objet->find($id);
}
$tableau_class_cellule = [];
$tableau_class_cellule[] = ["className" => "visible_export colonne_id", "targets" => [0], "visible" => true, "orderable" => true, "searchable" => true];
$tableau_class_cellule[] = ["className" => "visible_export colonne_id", "targets" => [1], "visible" => true, "orderable" => true, "searchable" => true];
$tableau_class_cellule[] = ["className" => "visible_export colonne_id", "targets" => [2], "visible" => true, "orderable" => true, "searchable" => true];
$tableau_class_cellule[] = ["className" => "visible_export colonne_id", "targets" => [3], "visible" => true, "orderable" => true, "searchable" => true];
$tableau_class_cellule[] = ["className" => "visible_export colonne_id", "targets" => [4], "visible" => true, "orderable" => true, "searchable" => true];
$tableau_class_cellule[] = ["className" => "visible_export colonne_id", "targets" => [5], "visible" => true, "orderable" => true, "searchable" => true];
$tableau_class_cellule[] = ["className" => "visible_export colonne_id text-right", "targets" => [6], "visible" => true, "orderable" => true, "searchable" => true];
$tableau_class_cellule[] = ["className" => "visible_export colonne_id text-center", "targets" => [7], "visible" => true, "orderable" => true, "searchable" => true];
$tableau_class_cellule[] = ["className" => "visible_export colonne_id text-center", "targets" => [8], "visible" => true, "orderable" => true, "searchable" => true];
$tableau_class_cellule[] = ["className" => "visible_export colonne_id", "targets" => [9], "visible" => true, "orderable" => true, "searchable" => true];
$tableau_class_cellule[] = ["orderable" => false, "className" => "colonne_id text-center", "targets" => [10], "visible" => true];
$this->datatable($datatable, $translator, $objet, $type);
//return $this->render('Utilisateur/Contact/lister.html.twig', array('tableauClassColonne'=>$tableau_class_cellule,"id"=>$id,"type"=>$type));
return $this->render('Kanban/Affaire/lister.html.twig', ['tableauClassColonne' => $tableau_class_cellule, "id" => $id, "type" => $type]);
}
/**
* set datatable configs
*
* @return \App\Library\Datatable\Util\Datatable
*/
private function datatable(Datatable $datatable, TranslatorInterface $translator, $objet, $type = "")
{
$type_jointure = 'x.'.$type;
$datatable->setDatatableId('dta-affaire')
->setEntity(Fiche::class, "x")
->setFields(
[
//"ID" => 'x.id',
$translator->trans("Référence") => 'x.id',
$translator->trans("Libellé") => 'x.libelle',
$translator->trans("Tunnel") => 't.libelle',
$translator->trans("Étape") => 'col.libelle',
$translator->trans("Client") => 'c.raisonSociale',
$translator->trans("Contact") => 'co.prenom',
$translator->trans("Budget") => 'x.budget',
$translator->trans("Potentiel") => 'x.potentiel',
$translator->trans("Probabilité") => 'x.propabilite',
$translator->trans("Date aboutissement") => 'x.datePrevisionelle',
$translator->trans("Région") => 'r.libelle',
//"Défaut" => 'x.defaut',
"Actions" => 'x.id',
"_identifier_" => 'x.id',
]
)
->addJoin('x.kanban', 't', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->addJoin('x.colonne', 'col', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->addJoin('x.client', 'c', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->addJoin('x.contact', 'co', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->addJoin('x.region', 'r', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->setRenderers(
[
0 => [
'view' => 'FO/DataTable/avec_lien_edit_route.html.twig',
'params' => [
'edit_route' => 'dtc_kanban_affaire_fiche',
],
],
1 => [
'view' => 'FO/DataTable/edit_texte.html.twig',
'params' => [
'objet' => Fiche::class,
'champ' => 'setLibelle',
],
],
/*
1 => array(
'view' => 'FO/DataTable/avec_lien_edit_route.html.twig',
'params' => array(
'edit_route' => 'dtc_kanban_affaire_fiche'
),
),
*/
2 => [
'view' => 'FO/DataTable/avec_lien_edit_route.html.twig',
'params' => [
'edit_route' => 'dtc_kanban_afficher',
'typeDocument' => 'tunnel',
],
],
4 => [
'view' => 'FO/DataTable/avec_lien_edit_route.html.twig',
'params' => [
'edit_route' => 'dtc_client_modifier',
'typeDocument' => 'client',
],
],
5 => [
'view' => 'FO/DataTable/select_contact_affaire.html.twig',
'params' => [
'edit_route' => 'dtc_client_modifier',
'typeDocument' => 'client',
],
],
6 => [
'view' => 'FO/DataTable/edit_texte_prix.html.twig',
'params' => [
'objet' => Fiche::class,
'champ' => 'setBudget',
],
],
/*
6 => array(
'view' => 'FO/DataTable/prix.html.twig',
'params' => array(
)
),
*/
7 => [
'view' => 'FO/DataTable/edit_texte.html.twig',
'params' => [
'objet' => Fiche::class,
'champ' => 'setPotentiel',
'position' => 'text-center',
],
],
8 => [
'view' => 'FO/DataTable/edit_texte.html.twig',
'params' => [
'objet' => Fiche::class,
'champ' => 'setPropabilite',
'position' => 'text-center',
],
],
9 => [
'view' => 'FO/DataTable/edit_texte_date.html.twig',
'params' => [
'objet' => Fiche::class,
'champ' => 'setDatePrevisionelle"',
'position' => 'text-center',
],
],
/*
9=> array(
'view' => 'FO/DataTable/date.html.twig',
'params' => array(
),
),
*/
11 => [
'view' => 'FO/DataTable/actions.html.twig',
'params' => [
'edit_route' => 'dtc_kanban_affaire_fiche',
'objet' => Fiche::class,
//'supprimer_route' => 'dtc_contact_supprimer',
//'id' => $objet->getId(),
//'type' => $type,
//'entite' => 'contact',
//'objet' => "DTCUtilisateurBundle:Contact"
],
],
]
)
->setOrder("x.date", "desc")
->setSearch(true)
->setSearchFields([0, 1, 2, 3, 4, 5, 6, 7, 9]);
$where = $type_jointure." = :objet AND (x.statutFiche is NULL or x.statutFiche = :gagne or x.statutFiche = :perdu)";
$parametres = [];
$parametres["gagne"] = "1";
$parametres["perdu"] = "2";
$parametres["objet"] = $objet;
if ($where != "") {
$datatable->setWhere(
$where,
$parametres
);
}
return $datatable;
}
/**
* Grid action
* @return Response
*/
/**
* @Route("/affaires/grid/{id}/{type}", name="dtc_affaire_liste_grid")
*/
public function gridAction(Request $request, $id = "", $type = "", EntityManagerInterface $em, Datatable $datatable, TranslatorInterface $translator)
{
if ($type == 'client') {
$repo_objet = $em->getRepository(Client::class);
$objet = $repo_objet->find($id);
}
return $this->datatable($datatable, $translator, $objet, $type)->execute();
}
}