<?php
namespace App\Controller\Api\Prive;
use App\Entity\Campagne;
use App\Entity\Campagnecollaborateur;
use App\Entity\Campagneprospect;
use App\Entity\Client;
use App\Entity\Utilisateur;
use App\Repository\CampagneprospectRepository;
use Doctrine\ORM\Query\ResultSetMapping;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/campagne")
*/
class ApiCampagneController extends ApiController
{
/**
* @Route("/{id}", name="api_Campagne_getById", methods={"GET"})
* @param int $id
* @return JsonResponse
*/
public function getById(int $id)
{
if ($id > 0) {
$campagne = $this->em->getRepository(Campagne::class)->find($id);
if ($campagne) {
return $this->apiOk($campagne);
}
}
return $this->apiKo("Campagne non trouvée !!");
}
/**
* @Route("/{id}", name="api_Campagne_delete", methods={"DELETE"})
* @param int $id
* @return JsonResponse
*/
public function delete(int $id)
{
if ($id > 0) {
try {
$campagne = $this->em->getRepository(Campagne::class)->find($id);
if (!$campagne) {
return $this->apiKo("Campagne non trouvée !!");
}
// VERIFIER SI la campagne est déja mise en prod !
/** @var CampagneprospectRepository $repoCampagneprospect */
$repoCampagneprospect = $this->em->getRepository(Campagneprospect::class);
$estEnProduction = $repoCampagneprospect->estEnProduction($campagne);
if ($estEnProduction) {
// Suppression Logique
$campagne->setActive(2);
$this->em->persist($campagne);
$this->em->flush();
return $this->apiOk($id);
} else {
// Suppression Physique
try {
return $this->removeElement($campagne);
} catch (\Exception $e) {
return $this->apiKo($e->getMessage());
}
}
} catch (\Exception $e) {
return $this->apiKo($e->getMessage());
}
}
return $this->apiKo("Campagne non trouvée !!");
}
/**
* @Route("/", name="api_Campagne_save", methods={"POST"})
* @param Request $request
* @return JsonResponse
*/
public function save(Request $request)
{
$data = json_decode($request->getContent(), true);
$campagne = new Campagne();
if (isset($data['id'])) {
$campagne = $this->em->getRepository(Campagne::class)->find($data['id']);
} else {
//dateCreation
$campagne->setDatecreation(new \DateTime());
}
if (isset($data['libelle'])) {
$campagne->setLibelle($data['libelle']);
}
if (isset($data['objectif'])) {
$campagne->setObjectif($data['objectif']);
}
if (isset($data['objectifagent'])) {
$campagne->setObjectifagent($data['objectifagent']);
}
if (isset($data['description'])) {
$campagne->setDescription($data['description']);
}
if (isset($data['datedebut'])) {
$campagne->setDatedebut(new \DateTime($data['datedebut']));
}
if (isset($data['datefin'])) {
$campagne->setDatefin(new \DateTime($data['datefin']));
}
//
$idCampagneCible = 0 ;
if (isset($data['type']) && $data['type'] != "0") {
$campagne->setType($data['type']);
if (isset($data['campagnecible'])) {
$campagne->setCampagnecible($data['campagnecible']);
}else{
$campagne->setCampagnecible(0);
}
} else {
$idCampagneCible = $campagne->getCampagnecible();
$campagne->setType(0);
$campagne->setCampagnecible(0);
}
// set Objects : Client
if (isset($data['client']) && isset($data['client']['id'])) {
$client = $this->getObject(Client::class, $data['client']['id']);
if ($client) {
$campagne->setClient($client);
}
}
// set Objects : Clientmanager
if (isset($data['clientmanager']) && isset($data['clientmanager']['id'])) {
$clientmanager = $this->getObject(Utilisateur::class, $data['clientmanager']['id']);
if ($clientmanager) {
$campagne->setClientmanager($clientmanager);
}
}
// set Objects : Client
if (isset($data['reponsable']) && isset($data['reponsable']['id'])) {
$reponsable = $this->getObject(Utilisateur::class, $data['reponsable']['id']);
if ($reponsable) {
$campagne->setReponsable($reponsable);
}
}
if (isset($data['recupererTousHistorique'])) {
$campagne->setRecupererTousHistorique($data['recupererTousHistorique']);
}
if (isset($data['active'])) {
$campagne->setActive($data['active']);
}
try {
$this->em->persist($campagne);
$this->em->flush();
//// mettre à jours campagne cible
if (isset($data['type']) && $data['type'] != "0") {
// ajout
if (isset($data['campagnecible']) && $data['campagnecible'] > 0) {
/** @var Campagne $campagnecible */
$campagnecible = $this->em->getRepository(Campagne::class)->find($data['campagnecible']);
if($campagnecible){
$type = $data['type'] == "1" ? "2":"1";
$campagnecible->setType($type);
$campagnecible->setCampagnecible($campagne->getId());
$this->em->persist($campagnecible);
$this->em->flush();
}
}
}else{
// Suppression
if($idCampagneCible > 0){
/** @var Campagne $campagnecible */
$campagnecible = $this->em->getRepository(Campagne::class)->find($idCampagneCible);
if($campagnecible){
$campagnecible->setType(0);
$campagnecible->setCampagnecible(0);
$this->em->persist($campagnecible);
$this->em->flush();
}
}
}
return $this->apiOk($campagne);
} catch (\Exception $e) {
return $this->apiKo($e->getMessage());
}
}
/**
* @Route("/find", name="api_Campagne_find", methods={"POST"})
* @param Request $request
* @return JsonResponse
*/
public function find(Request $request)
{
$data = json_decode($request->getContent(), true);
//$data['filter']['and']['campagne.active'] = ' <> 2 '; // ne pas affichier les elements supprimé logiquement
$sql = "
SELECT campagne
FROM " . Campagne::class . " campagne
LEFT JOIN " . Client::class . " client WITH campagne.client=client.id
";
if (isset($data['filter']['params']['espaceagent']) && $data['filter']['params']['espaceagent']) {
$sql = $sql . " LEFT JOIN " . Campagnecollaborateur::class . " as campagnecollaborateur WITH campagnecollaborateur.campagne = campagne.id";
$data['filter']['and']['campagnecollaborateur.collaborateur'] = ' = ' . $this->getUtilisateurConnecte()->getId();
}
if (isset($data['filter']['params']['espaceclient']) && $data['filter']['params']['espaceclient']) {
// a faire
/** @var Utilisateur $utilisateur */
$utilisateur = $this->getUtilisateurConnecte();
/** @var Client $client */
$client = $utilisateur->getClient();
if ($client) {
/** @var Utilisateur $manager */
$manager = $client->getManager();
if ($manager && ($manager->getId() == $utilisateur->getId())) {
// Cas Manager Principale : alors récupérer toute campagnes client
$data['filter']['and']['campagne.client'] = ' = ' . $client->getId();
} else {
// Cas Non Manager Principale : alors récupérer seulement ses campagnes
$data['filter']['and']['campagne.clientmanager'] = ' = ' . $utilisateur->getId();
}
} else {
$data['filter']['and']['1'] = ' = 0';
}
}
return $this->findElementsWithJoin($sql, $data);
}
/**
* @Route("/campagnedoublon/{id}", name="api_Campagne_campagnedoublon", methods={"GET"})
* @param int $id
* @return JsonResponse
*/
public function campagnedoublon(int $id){
$sql = "
SELECT COUNT(*) as doublon FROM
campagneprospect cp
LEFT JOIN prospect p ON p.id=cp.prospect_id
WHERE cp.campagne_id=".$id." AND p.societe IN (SELECT d.societe FROM prospectdoublon AS d)";
$rsm = new ResultSetMapping();
$rsm->addScalarResult('doublon','doublon');
$items = $this->em->createNativeQuery($sql,$rsm)->getScalarResult();
$data = ["doublon" => 0];
foreach ($items as $item){
$data = ["doublon" => $item["doublon"]];
}
return $this->apiOk($data);
}
}