<?php
namespace App\Controller\User;
use App\Common\Tools;
use App\Entity\Admin;
use App\Entity\Adminevent;
use App\Entity\Adminprev;
use App\Entity\Adminprevrdv;
use App\Entity\Client;
use App\Entity\Clientvente;
use App\Entity\Fournisseur;
use App\Entity\Messages;
use App\Entity\Moisvalide;
use App\Form\User\ClientProspectType;
use App\Form\User\ClientType;
use App\Utils\Dates\MalysDates;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class UserController extends AbstractController
{
private $malysDates;
/**
* @required
*/
public function setMalysDates(MalysDates $malysDates)
{
$this->malysDates = $malysDates;
}
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request,EntityManagerInterface $em)
{
return $this->homeAction(date("Y"), date("m"), date("W"), $request,$em);
}
/**
* @Route("/mobile/{filter}", name="malys_user_mobilehome", defaults={"filter"="nom"}, requirements={"filter"="nom|datefr"})
*/
public function mobileAction($filter, Request $request, EntityManagerInterface $em)
{
// on cherche un user-agent apparenté à une plateforme mobile dans la variable
$iphone = strpos($request->server->get('HTTP_USER_AGENT'), 'iPhone');
$ipad = strpos($request->server->get('HTTP_USER_AGENT'), 'iPad');
$android = strpos($request->server->get('HTTP_USER_AGENT'), 'Android');
$blackberry = strpos($request->server->get('HTTP_USER_AGENT'), 'BlackBerry');
// Si c'est un mobile
if ($iphone || $ipad || $android || $blackberry > -1) {
// On recupère les clients du USER
$clients_user = $em->getRepository(Client::class)->findMyClientsAndProspects($this->getUser()->getId(), $filter);
($filter == "datefr") ? $clients_user = array_reverse($clients_user, true) : 0;
($filter == "nom") ? $filter = 'datefr' : $filter = 'nom';
return $this->render('User/mobile.html.twig', array(
"clients" => $clients_user,
"nom_commercial" => $this->getUser()->getUser(),
"filter" => $filter
));
} else {
return $this->redirect($this->generateUrl('homepage'));
}
}
/**
* @Route("/mobile/commande/{id}", name="malys_user_mobilecommande", defaults={"id"=0})
*/
public function mobilecommandeAction($id, EntityManagerInterface $em)
{
$clientRepository = $em->getRepository(Client::class);
$client_info = $clientRepository->find($id);
$limit = 30;
$orders = $em->getRepository(Clientvente::class)->getLastOrdersClient($id, $limit);
return $this->render('User/mobilecommande.html.twig', array(
"client" => $client_info,
"orders" => $orders
));
}
/**
* @Route("/mobile/location/{id}", name="malys_user_mobilelocation", defaults={"id"=0})
*/
public function mobilelocationAction($id, EntityManagerInterface $em)
{
$client_info = $em->getRepository(Client::class)->find($id);
$adresse = $client_info->getAdresse() . " " . $client_info->getCp() . " " . $client_info->getVille();
return $this->render('User/mobilelocation.html.twig', array(
"client" => $client_info,
"adresse" => $adresse
));
}
/**
* @Route("/mobile/chart/{id}", name="malys_user_mobilechart", defaults={"id"=0})
*/
public function mobilechartAction($id, EntityManagerInterface $em)
{
/* RECUPERATION DU CLIENT EN BASE */
$client = $em->getRepository(Client::class)->find($id);
//RECUPERE LE MOIS VALIDE LE PLUS PROCHE
$MonthValid = $em->getRepository(Moisvalide::class)->findLastMoisvalide(date("n"), date("Y"));
$annee_valide = $MonthValid[0]['annee'];
$mois_valide = $MonthValid[0]['mois'];
/* RECUPERATION CA CLIENT */
$cv = $em->getRepository(Clientvente::class)->findMyClientCa($id, 'all');
/* tableau ca pour chart */
$t_valeur_ca_chart = array();
/* tableau frn */
$t_fournisseur = array();
/* Total Ca client */
$total_ca = 0;
/* tableau ca last month */
$t_last_month_ca = array();
foreach ($cv as $v) {
$ca_genere = 0;
$annee_vente = $v[0]->getDatefr()->format('Y');
$mois_vente = $v[0]->getDatefr()->format('n');
(isset($v['ca_genere']) && $annee_vente == $annee_valide) ? $ca_genere = $v['ca_genere'] : $ca_genere = 0;
//TABLEAU MOIS PAR MOIS
if (!isset($t_valeur_ca_chart[intval($v[0]->getDatefr()->format('Y'))][intval($v[0]->getDatefr()->format('m'))])) {
$t_valeur_ca_chart[intval($v[0]->getDatefr()->format('Y'))][intval($v[0]->getDatefr()->format('n'))] = array(
'ca_genere' => 0,
'ca_commission' => 0,
'solde' => 0
);
}
$t_valeur_ca_chart[intval($v[0]->getDatefr()->format('Y'))][intval($v[0]->getDatefr()->format('n'))]['ca_genere'] += number_format($v['ca_genere'], 2, '.', '');
$t_valeur_ca_chart[intval($v[0]->getDatefr()->format('Y'))][intval($v[0]->getDatefr()->format('n'))]['ca_commission'] += ($v[0]->getIdadmin()->getId() == $this->getUser()->getId()) ? number_format($v['ca_commission'], 2, '.', '') : 0;
$t_valeur_ca_chart[intval($v[0]->getDatefr()->format('Y'))][intval($v[0]->getDatefr()->format('n'))]['solde'] += number_format($v[0]->getSolde(), 2, '.', '');
//TABLEAU LAST MONTHS CA
$date_vente = Date("n/Y", mktime(0, 0, 0, $mois_vente, 1, $annee_vente));
$mois_valide_past = Date("n/Y", mktime(0, 0, 0, $mois_valide, 1, $annee_valide));
$mois_past1 = Date("n/Y", mktime(0, 0, 0, $mois_valide - 1, 1, $annee_valide));
$mois_past2 = Date("n/Y", mktime(0, 0, 0, $mois_valide - 2, 1, $annee_valide));
if (!isset($t_last_month_ca[$v[0]->getIdfournisseur()->getId()])) {
$t_last_month_ca[$v[0]->getIdfournisseur()->getId()] = array(
$mois_valide_past => 0,
$mois_past1 => 0,
$mois_past2 => 0
);
if ($date_vente == $mois_valide_past or $date_vente == $mois_past1 or $date_vente == $mois_past2) {
$t_last_month_ca[$v[0]->getIdfournisseur()->getId()][$mois_valide_past] = $v['ca_genere'];
}
} else {
if ($date_vente == $mois_valide_past or $date_vente == $mois_past1 or $date_vente == $mois_past2) {
$t_last_month_ca[$v[0]->getIdfournisseur()->getId()][$date_vente] = $v['ca_genere'];
}
}
//TABLEAU FOURNISSEUR
$montant = 0;
if (!isset($t_fournisseur[$v[0]->getIdfournisseur()->getId()])) {
$montant = $v['ca_genere'];
$t_fournisseur[$v[0]->getIdfournisseur()->getId()] = array(
'nom' => $v[0]->getIdfournisseur()->getNom(),
'total_ca' => $montant
);
} else {
$montant += $v['ca_genere'];
$t_fournisseur[$v[0]->getIdfournisseur()->getId()]['total_ca'] += $montant;
}
//TOTAL CA CLIENT
$total_ca += $ca_genere;
}
//CREATE JSON FOR CHART CA
$t_ca_chart = array();
foreach ($t_valeur_ca_chart as $key => $item_ca) {
$t_ca = array();
for ($i = 1; $i <= 12; $i++) {
(isset($item_ca[$i]['ca_genere']) && intval($item_ca[$i]['ca_genere']) != 0) ? array_push($t_ca, $item_ca[$i]['ca_genere']) : array_push($t_ca, null);
}
($key == date("Y")) ? array_push($t_ca_chart, (object)array(
'name' => $key,
'data' => $t_ca
)) : array_push($t_ca_chart, (object)array('name' => $key, 'visible' => false, 'data' => $t_ca));
}
$t_ca_chart_json = json_encode($t_ca_chart);
return $this->render('User/mobilechart.html.twig', array(
'client' => $client,
'user' => $this->getUser()->getFamille(),
't_ca_chart_json' => $t_ca_chart_json,
't_frn_client' => $t_fournisseur,
'total_ca' => $total_ca,
't_last_month_ca' => $t_last_month_ca,
'month_valid' => Date("n/Y", mktime(0, 0, 0, $mois_valide, 1, $annee_valide)),
'month_valid_past' => Date("n/Y", mktime(0, 0, 0, $mois_valide - 1, 1, $annee_valide)),
'month_valid_pastpast' => Date("n/Y", mktime(0, 0, 0, $mois_valide - 2, 1, $annee_valide)),
));
}
/**
* @Route("/mobile/show/{id}", name="malys_user_mobileshow", defaults={"id"=0})
*/
public function mobileshowAction($id, EntityManagerInterface $em)
{
$tab_fonction = array(
0 => 'non renseigne',
1 => 'Chef',
2 => 'Patron',
3 => 'Resp Bar',
4 => 'Gerant',
5 => 'Autre'
);
$tab_typeaffaire = array(
0 => 'Restaurant traditionnel',
1 => 'VAE',
2 => 'Bistrot <100 couverts/j',
3 => 'Basserie',
4 => 'Bar pur',
5 => 'Hotel',
6 => 'Boite de nuit',
7 => 'Traiteur',
8 => 'Autre',
);
$tab_typecontrat = array(
0 => 'non renseigne',
1 => 'Pret',
2 => 'MaD',
3 => 'Avance sur remise'
);
// On recupère les infos client
$client_info = $em->getRepository(Client::class)->find($id);
return $this->render('User/mobileshow.html.twig', array(
'client' => $client_info,
'tab_fonction' => $tab_fonction,
'tab_typeaffaire' => $tab_typeaffaire,
'tab_typecontrat' => $tab_typecontrat,
));
}
/**
* @Route("/mobile/modify/{id}", name="malys_user_mobilemodify", defaults={"id"=0})
*/
public function mobilemodifyAction($id, Request $request, EntityManagerInterface $em)
{
//$currentRoute = $request->attributes->get('_route');
/*if($currentRoute != 'malys_user_mobileshow')
return $this->redirect($this->generateUrl('malys_user_mobileshow', array('id' => $id)));*/
//if($this->get('request')->headers->get('referer')){ $toto = "redirect ";}
$type = '';
if (isset($id) and $id != 0) {
$client = $em->getRepository(Client::class)->find($id);
$type = "Modifier";
} else {
$client = new Client;
$type = "Ajouter";
}
// On verifie si le user est un admin ou pas pour créer le formualaire correspondant
if ($id && $this->isGranted('ROLE_ADMIN')) {
$form = $this->createForm(new ClientType($client->getProspect(), true), $client);
} else {
$form = $this->createForm(new ClientType($client->getProspect()), $client);
}
//On Regarde si on a recu une requete POST => si oui c'est que le user nous transmet un ajout ou une modif.
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
//Si une aucune date client on met a jour
if ($client->getDateclientfr() == null) {
$client->setDateclientfr($client->getDatefr());
}
// si c'est un nouveau client, on lui associe le user en admin
if ($client->getIdadmin() == null) {
$client->setIdadmin($this->getUser());
}
$em->persist($client);
$em->flush();
$id_client_retour = $client->getId();
return $this->redirect($this->generateUrl('malys_user_mobileshow', array('id' => $id_client_retour)));
}
}
return $this->render('User/mobilemodify.html.twig', array(
'form' => $form->createView(),
'client' => $client,
'type' => $type
));
}
/**
* @Route("/index/{annee}/{mois}/{semaine}", name="malys_user_homepage", defaults={"semaine"=0})
*/
public function homeAction($annee, $mois, $semaine, Request $request, EntityManagerInterface $em)
{
$annee_cal = $annee;
$mois_cal = $mois;
$semaine_cal = $semaine;
//$this->container->get('malys_user.charts');
$tab_mois_fr = $this->malysDates->getTabMoisFr();
$tab_jours_fr = $this->malysDates->getTabJoursFr();
//echo " <pre>";print_r($tab_mois_fr); echo " </pre><pre> "; print_r($tab_jours_fr);echo " </pre>";
// CA GENERE DU MOIS POUR UN COMMERCIAL
/*$t_ListeCA = array();
$ca = $em->getRepository(Clientvente::Class)->findCommercialCa($this->getUser()->getId(), $annee);
foreach ($ca as $c)
$t_ListeCA[intval($c['le_mois'])] = $c['ca_genere'];*/
//RECUPERE LE MOIS VALIDE LE PLUS PROCHE
//
$MonthValid = $em->getRepository(Moisvalide::class)->findLastMoisvalide(date("n"), date("Y"));
// debug here
$annee = $MonthValid[0]['annee'];
$mois_valide = $MonthValid[0]['mois'];
/* GRAPH */
/* $objChart = new \Chart(140, 100, "chart_bar_ca_mois_" . $this->getUser()->getId() . "_gd_small");
$objChart->SetHeaderName("Evolution CA " . $annee);
$objChart->SetY_Barchart_Show_Values(true);
for ($i = 1; $i < 13; $i++)
$objChart->AddValue((string) $i, (isset($t_ListeCA[$i])) ? intval($t_ListeCA[$i]) : 0);
$objChart->SetImageDestination("images/graph/");
$objChart->CreateChartImage(LINE_CHART);*/
// FOURNISSEURS
if ($this->getUser()->getFamille() == 'Commercial') {
$fournisseurs = $em->getRepository(Fournisseur::class)->findMyClientsFournisseurs($this->getUser()->getId());
} else {
$fournisseurs = null;
}
//RECUPERATION DES MESSAGES NON LUS
$mes = $em->getRepository(Messages::class)->findMyMessagesNonLus($this->getUser()->getId());
//AFFICHAGE DES RDV DU JOUR ET DU LENDEMAIN
$rdv = $em->getRepository(Adminevent::class)->findTodayAndTomorrowRdv($this->getUser()->getId());
//RECUPERATION DES STATS CLIENTELE
$t_ListePC = array();
$stats = $em->getRepository(Client::class)->findMyClientsStats($this->getUser()->getId());
foreach ($stats as $s) {
if ($s[0]->getProspect() == 1) {
$t_ListePC['prospect'][$s[0]->getNote()] = $s['nombre'];
} else {
$t_ListePC['client'][$s[0]->getActivite()] = $s['nombre'];
}
}
//LISTE SIMULATION HEBDO
$simu = $em->getRepository(Adminevent::class)->findCommercialSimulationHebdo($this->getUser()->getId(), $annee, $semaine);
//RECUPERATION DES OBJECTIFS RDV
$obj_rdv = $em->getRepository(Adminprevrdv::class)->findOneBy(array(
'idadmin' => $this->getUser()->getId(),
'annee' => $annee,
'semaine' => $semaine
));
//LISTE PROSPECTION HEBDO
$t_ListeProspect = array();
$pro = $em->getRepository(Client::class)->findListeProspectionHebdo($this->getUser()->getId(), $annee, $semaine);
foreach ($pro as $p) {
$t_ListeProspect[$p[0]->getNote()] = $p['nbprospect'];
}
//LISTE RDV CLIENTS
$rdvClient = $em->getRepository(Adminevent::class)->findMyClientRdvHebdo($this->getUser()->getId(), $annee, $semaine);
//LISTE TRANSFOS CLIENTS
$transfo = $em->getRepository(Client::class)->findMyTransfoClientHebdo($this->getUser()->getId(), $annee, $semaine);
//OBJECTIF DE CA
$caObj = $em->getRepository(Adminprev::class)->findCommercialPrevCaInMonth($this->getUser()->getId(), $annee, $mois);
//ALL CA FRN BY MONTH/YEAR
$getAllCaByMonth = $em->getRepository(Clientvente::class)->getAllCaByMonth(3, $this->getUser()->getId(), $this->getUser()->getFamille());
//ALL OBJECTIF BY MONTH/YEAR
$t_Objectifs = $em->getRepository(Adminprev::class)->getAllPrevCaByMonth($this->getUser()->getId(), $this->getUser()->getFamille(), $annee);
$t_objectif = array();
$obj_total = 0;
foreach ($t_Objectifs as $key => $item) {
(intval($item['objectif'] != 0)) ? array_push($t_objectif, intval($item['objectif'])) : array_push($t_objectif, null);
$obj_total += intval($item['objectif']);
}
//création json pour chart CA
$t_ca_chart = array();
foreach ($getAllCaByMonth as $key => $item) {
$t_ca = array();
$i = 0;
foreach ($item as $year => $ca) {
if ($i != 0) {
(intval($ca) != 0) ? array_push($t_ca, intval($ca)) : array_push($t_ca, null);
}
$i++;
}
($item["Annee"] == $annee) ? array_push($t_ca_chart, (object)array(
'name' => $item["Annee"],
'data' => $t_ca
)) : array_push($t_ca_chart, (object)array(
'name' => $item["Annee"],
'visible' => false,
'data' => $t_ca
));
}
array_push($t_ca_chart, (object)array('name' => "Objectifs", 'color' => '#a3dfd9', 'data' => $t_objectif));
array_push($t_objectif, $obj_total);
$t_ca_chart_json = json_encode($t_ca_chart);
unset($t_ca_chart); //Free memoire
unset($getAllCaByMonth);
//ALL CA CLIENT BY MONTH/YEAR
$t_allCaClientByMonth = $em->getRepository(Clientvente::class)->getAllClientsCaLight($annee, null, 'three year', $this->getUser()->getId(), $this->getUser()->getFamille());
$t_listClientActif = array();
$t_listClientNouveau = array();
$t_listClientPerdu = array();
$t_ListeCaClientForChart = array();
$t_ListeCaFamilleForChart = array();
$t_CaFamilleForChart = array();
// Recuperation tab CAclient/mois et tab CaFamille/mois
foreach ($t_allCaClientByMonth as $caClient => $item) {
$yearfr = $item['datefr']->format('Y');
$monthfr = $item['datefr']->format('n');
// Recuperation des clients actifs du dernier mois valider
if ($monthfr == $mois_valide && $yearfr == $annee) {
if (!isset($t_listClientActif[$item['id']])) {
$t_listClientActif[$item['id']] = $item['nom'];
$t_listClientNouveau[$item['id']] = $item['nom'];
}
}
// Recuperation des clients actifs du dernier mois valider-1
if ($monthfr == $mois_valide - 1 && $yearfr == $annee) {
if (!isset($t_listClientPerdu[$item['id']])) {
$t_listClientPerdu[$item['id']] = $item['nom'];
}
}
// Si l'année, le mois et le client n'existe pas dans le tableau d'arrivée, on l'ajoute au tableau
if ($yearfr == $annee || $yearfr == $annee - 1) {
if (!isset($t_ListeCaClientForChart[$yearfr][$monthfr][$item['id']])) {
if ($item['ca_genere'] > 0) {
$t_ListeCaClientForChart[$yearfr][$monthfr][$item['id']] = array();
$t_ListeCaClientForChart[$yearfr][$monthfr][$item['id']]['ca_genere'] = number_format((float)$item['ca_genere'], 2, '.', '');
}
} else { // Si la case existe déjà
if ($item['ca_genere'] > 0) {
$t_ListeCaClientForChart[$yearfr][$monthfr][$item['id']]['ca_genere'] += number_format((float)$item['ca_genere'], 2, '.', '');
}
}
}
// Si l'année n'existe pas dans le tableau CAfamille d'arrivée, on ajoute la case avec la l'année
if ($yearfr == $annee) {
if (!isset($t_ListeCaFamilleForChart[$item['famille']])) {
$t_ListeCaFamilleForChart[$item['famille']] = array();
$t_ListeCaFamilleForChart[$item['famille']] = number_format((float)$item['ca_genere'], 2, '.', '');
} else {
$t_ListeCaFamilleForChart[$item['famille']] += number_format((float)$item['ca_genere'], 2, '.', '');
}
}
}
//ON RETIRE LES CLIENTS QUI ONT COMMANDES DE LA LISTE
foreach ($t_listClientPerdu as $k => $value) {
if (isset($t_listClientActif[$k])) {
unset($t_listClientPerdu[$k]);
}
}
foreach ($t_allCaClientByMonth as $caClient => $item) {
$yearfr = $item['datefr']->format('Y');
$monthfr = $item['datefr']->format('n');
// Recuperation des clients Nouveau du dernier mois valider
if ($yearfr == ($annee)) {
if ($monthfr == ($mois_valide) - 1 ||
$monthfr == ($mois_valide) - 2 ||
$monthfr == ($mois_valide) - 3 ||
$monthfr == ($mois_valide) - 4) {
if (isset($t_listClientNouveau[$item['id']])) {
unset($t_listClientNouveau[$item['id']]);
}
}
}
}
unset($t_allCaClientByMonth); //Free memoire
$t_ca_famille = array();
foreach ($t_ListeCaFamilleForChart as $key => $value) {
($key != "") ? array_push($t_ca_famille, array(
$key,
intval($value)
)) : array_push($t_ca_famille, array("AUTRE", intval($value)));
}
// Obtient une liste de colonnes
foreach ($t_ca_famille as $key => $row) {
$volume[$key] = $row[1];
$edition[$key] = $row[0];
}
// Trie les données par volume décroissant, edition croissant
// Ajoute $data en tant que dernier paramètre, pour trier par la clé commune
if (isset($volume)) {
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $t_ca_famille);
}
array_push($t_CaFamilleForChart, (object)array(
'type' => 'pie',
'name' => 'Famille',
'data' => $t_ca_famille
));
unset($t_ca_famille); // free Memory
$t_CaFamilleForChart_json = json_encode($t_CaFamilleForChart);
unset($t_CaFamilleForChart);
unset($t_ListeCaFamilleForChart);
$t_gp_val_chart = array();
$t_gp_client_chart = array();
$t_ListeGainsPertes = array(
0 => array('gain' => array(), 'perte' => array(), 'type' => 'ca'),
1 => array('gain' => array(), 'perte' => array(), 'type' => 'client')
);
if (isset($t_ListeCaClientForChart[$annee - 1][12])) {
foreach ($t_ListeCaClientForChart[$annee - 1][12] as $id => $ca) {
(!array_key_exists($id, $t_ListeCaClientForChart[$annee][1])) ? $t_ListeCaClientForChart[$annee][1][$id] = array('ca_genere' => 0.0) : null;
}
}
foreach ($t_ListeCaClientForChart as $year => $item) {
foreach ($item as $month => $val) {
foreach ($val as $id => $ca) {
for ($i = 1; $i < count($item); $i++) {
if (isset($t_ListeCaClientForChart[$year][$i])) {
(!array_key_exists($id, $t_ListeCaClientForChart[$year][$i])) ? $t_ListeCaClientForChart[$year][$i][$id] = array('ca_genere' => 0.0) : null;
}
}
}
}
}
for ($i = 1; $i <= 12; $i++) {
(!isset($t_ListeCaClientForChart[$annee][$i])) ? $t_ListeCaClientForChart[$annee][$i] = null : 1;
}
ksort($t_ListeCaClientForChart[$annee], SORT_ASC);
//calcul et recuperation "pertes et gains" total en valeur et client/MOIS
foreach ($t_ListeCaClientForChart as $year => $item) {
$t_gains_valeur = array();
$t_pertes_valeur = array();
$t_gains_client = array();
$t_pertes_client = array();
if ($year == $annee) {
foreach ($item as $month => $val) {
$gainsValeur = 0;
$pertesValeur = 0;
$gainsClient = 0;
$pertesClient = 0;
if ($val != null) {
foreach ($val as $client => $caClient) {
$caMoisPrec = 0;
if ($month != 1) { // Si C'est pas le mois de janvier
if (isset($item[$month - 1][$client])) {
$caMoisPrec = $item[$month - 1][$client]['ca_genere'];
}
} else { // si c'est janvier => IDEM avec décembre de l'année précédente
if (isset($t_ListeCaClientForChart[$year - 1][12][$client])) {
$caMoisPrec = $t_ListeCaClientForChart[$year - 1][12][$client]['ca_genere'];
}
}
$diff = ($caClient['ca_genere']) - ($caMoisPrec); // ON calcul $diff => CA du mois - (Ca du mois-1) [exemple: CAmars - CAfevrier ]
($diff < 0) ? $pertesValeur += $diff : $gainsValeur += $diff; // On met a jour Gains et Pertes
//On calcul $evoCa => (CA du mois - (Ca du mois-1)) / (Ca du mois-1) [exemple: (CAmars - CAfevrier)/CAfevrier ]
if ($caMoisPrec != 0) {
$evoCa = (($caClient['ca_genere']) - ($caMoisPrec)) / ($caMoisPrec);
} else {
$evoCa = 0;
}
// On met a jour Gains et Pertes Client
if ($evoCa < 0) {
$pertesClient--;
}
if ($evoCa > 0) {
$gainsClient++;
}
}
} else {
$gainsClient = null;
$pertesClient = null;
$pertesValeur = null;
$gainsValeur = null;
}
array_push($t_gains_valeur, $gainsValeur);
array_push($t_pertes_valeur, $pertesValeur);
array_push($t_gains_client, $gainsClient);
array_push($t_pertes_client, $pertesClient);
}
}
if ($year == $annee) {
array_push($t_gp_val_chart, (object)array('name' => "Gains", 'data' => $t_gains_valeur)); //Valeur
array_push($t_gp_val_chart, (object)array(
'name' => "Pertes",
'data' => $t_pertes_valeur
)); //Valeur
array_push($t_gp_client_chart, (object)array(
'name' => "Gains",
'data' => $t_gains_client
)); //Client
array_push($t_gp_client_chart, (object)array(
'name' => "Pertes",
'data' => $t_pertes_client
)); //Client
array_push($t_ListeGainsPertes[0]['gain'], $t_gains_valeur); //Valeur
array_push($t_ListeGainsPertes[0]['perte'], $t_pertes_valeur); //Valeur
array_push($t_ListeGainsPertes[1]['gain'], $t_gains_client); //Client
array_push($t_ListeGainsPertes[1]['perte'], $t_pertes_client); //Client
}
}
// COnstrcution JSON pour GAINS PERTES CHARTS
$t_gp_val_chart_json = json_encode($t_gp_val_chart);
$t_gp_client_chart_json = json_encode($t_gp_client_chart);
unset($t_gp_val_chart);
unset($t_gp_client_chart);
//Tableau Gains Pertes
$tab_moy = array('moyValP' => 0, 'moyCliP' => 0, 'moyValG' => 0, 'moyCliG' => 0);
$next = 0;
$nbmonth = 0;
foreach ($t_ListeGainsPertes as $key => $val) {
if ($val['gain'][0]) {
foreach ($val['gain'][0] as $k => $value) {
($next == 0) ? $tab_moy['moyValG'] += $value : $tab_moy['moyCliG'] += $value;
($next == 0) ? $nbmonth++ : null;
}
}
if ($val['perte'][0]) {
foreach ($val['perte'][0] as $k => $value) {
($next == 0) ? $tab_moy['moyValP'] += $value : $tab_moy['moyCliP'] += $value;
}
}
$next++;
}
if ($nbmonth != 0) {
array_push($t_ListeGainsPertes[0]['gain'], $tab_moy['moyValG'] / $nbmonth);
array_push($t_ListeGainsPertes[0]['perte'], $tab_moy['moyValP'] / $nbmonth);
array_push($t_ListeGainsPertes[1]['gain'], $tab_moy['moyCliG'] / $nbmonth);
array_push($t_ListeGainsPertes[1]['perte'], $tab_moy['moyCliP'] / $nbmonth);
}
unset($tab_moy);
unset($t_gp_val_chart);
unset($t_gp_client_chart);
unset($t_gains_valeur);
unset($t_pertes_valeur);
unset($t_gains_client);
unset($t_pertes_client);
// Recuperation segClient/mois et segValeur/mois
$t_seg_client_chart = array();
$t_seg_valeur_chart = array();
$t_seg_client = array(
'0' => array(),
'1' => array(),
'2' => array(),
'3' => array(),
'4' => array(),
'5' => array()
);
$t_seg_valeur = array(
'0' => array(),
'1' => array(),
'2' => array(),
'3' => array(),
'4' => array(),
'5' => array()
);
$t_seg = array(
0 => array("palier" => 500, "valeur" => 0, "client" => 0, "name" => '0-500'),
1 => array("palier" => 1500, "valeur" => 0, "client" => 0, "name" => '500-1500'),
2 => array("palier" => 3000, "valeur" => 0, "client" => 0, "name" => '1500-3000'),
3 => array("palier" => 9000, "valeur" => 0, "client" => 0, "name" => '3000-9000'),
4 => array("palier" => 20000, "valeur" => 0, "client" => 0, "name" => '9000-20000'),
5 => array("valeur" => 0, "client" => 0, "name" => '>20000'),
);
$Liste_client_groupe1 = array($year => array());
// On recupere les valeurs de segmentation
foreach ($t_ListeCaClientForChart as $year => $item) {
if ($year == $annee) {
foreach ($item as $month => $val) {
for ($i = 0; $i < count($t_seg); $i++) {
$t_seg[$i]["valeur"] = 0;
$t_seg[$i]["client"] = 0;
}
(!isset($Liste_client_groupe1[$year][$month])) ? $Liste_client_groupe1[$year][$month] = array() : 1;
if ($val != null) {
foreach ($val as $client => $caClient) {
//Récupération des valeurs de segementation
if ($caClient['ca_genere'] > 1 && $caClient['ca_genere'] <= $t_seg[0]["palier"]) {
$t_seg[0]["valeur"] += $caClient['ca_genere'];
$t_seg[0]["client"] += 1;
//TODO EXTRACT LIST OF CLIENT BY GROUPE AND BY MONTH
array_push($Liste_client_groupe1[$year][$month], $client);
} else if ($caClient['ca_genere'] > $t_seg[0]["palier"] && $caClient['ca_genere'] <= $t_seg[1]["palier"]) {
$t_seg[1]["valeur"] += $caClient['ca_genere'];
$t_seg[1]["client"] += 1;
} else if ($caClient['ca_genere'] > $t_seg[1]["palier"] && $caClient['ca_genere'] <= $t_seg[2]["palier"]) {
$t_seg[2]["valeur"] += $caClient['ca_genere'];
$t_seg[2]["client"] += 1;
} else if ($caClient['ca_genere'] > $t_seg[2]["palier"] && $caClient['ca_genere'] <= $t_seg[3]["palier"]) {
$t_seg[3]["valeur"] += $caClient['ca_genere'];
$t_seg[3]["client"] += 1;
} else if ($caClient['ca_genere'] > $t_seg[3]["palier"] && $caClient['ca_genere'] <= $t_seg[4]["palier"]) {
$t_seg[4]["valeur"] += $caClient['ca_genere'];
$t_seg[4]["client"] += 1;
} else if ($caClient['ca_genere'] > $t_seg[4]["palier"]) {
$t_seg[5]["valeur"] += $caClient['ca_genere'];
$t_seg[5]["client"] += 1;
}
}
}
foreach ($t_seg as $key => $value) {
if ($year == $annee) {
for ($i = 0; $i < 6; $i++) {
if ($key == $i) {
array_push($t_seg_client[$i], $value['client']);
array_push($t_seg_valeur[$i], $value['valeur']);
}
}
}
}
}
}
}
$t_ListeSeg = array();
array_push($t_ListeSeg, $t_seg_client);
array_push($t_ListeSeg, $t_seg_valeur);
foreach ($t_ListeSeg as $key => $val) {
foreach ($val as $k => $value) {
$total = 0;
for ($i = 0; $i < count($value); $i++) {
$total += $value[$i];
}
if ($key == 1) {
$t_ListeSeg[$key][$k]['total_group'] = $total;
} else {
(count($value) != 0) ? $t_ListeSeg[$key][$k]['total_group'] = ($total / count($value)) : null;
}
for ($j = 0; $j < count($t_seg); $j++) {
$t_ListeSeg[$key][$j]['nom'] = $t_seg[$j]['name'];
}
}
}
$t_listeNbCommande = array();
$t_listeCaSeg = array();
for ($j = 0; $j < 12; $j++) {
$total_client = 0;
$total_ca = 0;
foreach ($t_ListeSeg[0] as $key => $val) {
(isset($val[$j])) ? $total_client += $val[$j] : null;
}
array_push($t_listeNbCommande, $total_client);
foreach ($t_ListeSeg[1] as $key => $val) {
(isset($val[$j])) ? $total_ca += $val[$j] : null;
}
array_push($t_listeCaSeg, $total_ca);
}
for ($j = 5; $j > -1; $j--) {
array_push($t_seg_client_chart, (object)array(
'type' => "column",
'name' => $t_seg[$j]['name'],
'data' => $t_seg_client[$j],
'id' => $t_seg[$j]['name'] . 'c',
)
);
array_push($t_seg_valeur_chart, (object)array(
'type' => "column",
'name' => $t_seg[$j]['name'],
'data' => $t_seg_valeur[$j],
'id' => $t_seg[$j]['name'] . 'v',
));
}
for ($j = 5; $j > -1; $j--) {
array_push($t_seg_client_chart, (object)array(
'name' => 'seg1->',
'id' => $t_seg[$j]['name'] . 'cc',
'showInLegend' => false,
'color' => 'red',
'lineWidth' => 1,
'marker' => (object)array(
'lineWidth' => 0.1,
'lineColor' => 'Highcharts.getOptions().colors[4]',
'fillColor' => "white",
"radius" => 2,
'enabled' => true
),
'data' => $t_seg_client[$j]
));
array_push($t_seg_valeur_chart, (object)array(
'name' => "seg1->",
'id' => $t_seg[$j]['name'] . 'vv',
'showInLegend' => false,
'color' => 'red',
'lineWidth' => 1,
'marker' => (object)array(
'lineWidth' => 0.1,
'lineColor' => 'Highcharts.getOptions().colors[4]',
'fillColor' => "white",
"radius" => 2,
'enabled' => true
),
'data' => $t_seg_valeur[$j]
));
}
$t_seg_client_chart_json = json_encode($t_seg_client_chart);
$t_seg_valeur_chart_json = json_encode($t_seg_valeur_chart);
unset($t_seg);
unset($t_seg_client_chart);
unset($t_seg_client);
unset($t_seg_valeur_chart);
unset($t_seg_valeur);
//vide memoire
// Recupération informations pour tableau fournisseurs global
// TRAITEMENT FORMULAIRE ET VALEURS PAR DEFAUT
$year = $annee;
$source = $request->get('source', null) == '' ? null : $request->get('source', null);
$perf_ca = $request->get('perf_ca', null);
$type = 'fournisseur';
// GET CA
$cv = $em->getRepository(Clientvente::class)->getAllFournisseursCa($year, $source, "three year", $this->getUser()->getId(), $this->getUser()->getFamille());
$t_ListeCA_frn = array();
$t_ListeMois = array();
$t_ListeDetail = array();
foreach ($cv as $c) {
$mois = intval($c[0]['datefr']->format('m'));
$yeartraitement = intval($c[0]['datefr']->format('Y'));
$id = $c['id'];
//TABLEAU ANNNEE TOTAUX
if (!isset($t_ListeMois[$yeartraitement])) {
$t_ListeMois[$yeartraitement] = array(
'cagenere_total' => 0,
'cacommission_total' => 0,
'solde_total' => 0
);
}
//TABLEAU DETAILLE
if (!isset($t_ListeCA_frn[$yeartraitement][$id])) {
$t_ListeCA_frn[$yeartraitement][$id] = array(
'cagenere_total' => 0,
'cacommission_total' => 0,
'solde_total' => 0
);
}
$t_ListeCA_frn[$yeartraitement][$id][$mois]['ca_genere'] = number_format($c['ca_genere'], 2, '.', '');
$t_ListeCA_frn[$yeartraitement][$id][$mois]['ca_commission'] = number_format($c['ca_commission'], 2, '.', '');
$t_ListeCA_frn[$yeartraitement][$id][$mois]['solde'] = number_format($c['ca_solde'], 2, '.', '');
$t_ListeCA_frn[$yeartraitement][$id]['id' . $type] = $c['id'];
$t_ListeCA_frn[$yeartraitement][$id]['nom'] = $c['nom'];
$t_ListeCA_frn[$yeartraitement][$id]['cagenere_total'] += number_format($c['ca_genere'], 2, '.', '');
$t_ListeCA_frn[$yeartraitement][$id]['cacommission_total'] += number_format($c['ca_commission'], 2, '.', '');
$t_ListeCA_frn[$yeartraitement][$id]['solde_total'] += number_format($c['ca_solde'], 2, '.', '');
$t_ListeCA_frn[$yeartraitement][$id]['source'] = $c['source'];
//TABLEAU ANNEE MOIS PAR MOIS
if (!isset($t_ListeMois[$yeartraitement][$mois])) {
$t_ListeMois[$yeartraitement][$mois] = array(
'ca_genere' => 0,
'ca_commission' => 0,
'solde' => 0
);
}
$t_ListeMois[$yeartraitement][$mois]['ca_genere'] += number_format($c['ca_genere'], 2, '.', '');
$t_ListeMois[$yeartraitement][$mois]['ca_commission'] += number_format($c['ca_commission'], 2, '.', '');
$t_ListeMois[$yeartraitement][$mois]['solde'] += number_format($c['ca_solde'], 2, '.', '');
//TEABLEAU TOTAL
$t_ListeMois[$yeartraitement]['cagenere_total'] += number_format($c['ca_genere'], 2, '.', '');
$t_ListeMois[$yeartraitement]['cacommission_total'] += number_format($c['ca_commission'], 2, '.', '');
$t_ListeMois[$yeartraitement]['solde_total'] += number_format($c['ca_solde'], 2, '.', '');
//TABLEAU PAR TYPE DETAIL
if (!isset($t_ListeDetail[$yeartraitement][$id])) {
$t_ListeDetail[$yeartraitement][$id] = array(
'ca_genere' => 0,
'ca_commission' => 0,
'solde' => 0
);
}
$t_ListeDetail[$yeartraitement][$id]['ca_genere'] += number_format($c['ca_genere'], 2, '.', '');
$t_ListeDetail[$yeartraitement][$id]['ca_commission'] += number_format($c['ca_commission'], 2, '.', '');
$t_ListeDetail[$yeartraitement][$id]['solde'] += number_format($c['ca_solde'], 2, '.', '');
$t_ListeDetail[$yeartraitement][$id]['source'] = $c['source'];
}
if ($perf_ca != null) {
uasort($t_ListeCA, "App\\Controller\\Marketing::cmpCa");
}
// GET POTENCIEL
$potenciel = $em->getRepository(Client::class)->getPotentielCaClient(0, $this->getUser()->getId(), $this->getUser()->getFamille());
$potentiel_json = array();
$potenciel[0]['potentiel'] = 0;
if (intval($potenciel[0]['potentiel']) - intval(number_format($t_ListeMois[$annee]["cagenere_total"], 2, '.', '')) > 0) {
array_push($potentiel_json, (object)array(
'name' => "Potentiel",
'id' => '' + $potenciel[0]['potentiel'] + '',
'data' => array(intval($potenciel[0]['potentiel']) - intval(number_format($t_ListeMois[$annee]["cagenere_total"], 2, '.', ''))),
));
array_push($potentiel_json, (object)array(
'name' => "CA realise",
'id' => '' + intval(number_format($t_ListeMois[$annee]["cagenere_total"], 2, '.', '')) + '',
'data' => array(intval(number_format($t_ListeMois[$annee]["cagenere_total"], 2, '.', ''))),
));
} else {
if ($potenciel[0]['potentiel'] != 0) {
$ca = intval(number_format($t_ListeMois[$annee]["cagenere_total"], 2, '.', ''));
$pot = $potenciel[0]['potentiel'];
$percent_ca = $ca / $pot * 100;
if ($percent_ca > 200) {
$ca_enplus = $ca - $pot;
$potenciel_maj = $pot - $ca_enplus;
array_push($potentiel_json, (object)array(
'name' => "CA realise",
'id' => '' + $ca + '',
'data' => array($ca_enplus),
));
} else {
$ca_enplus = $ca - $pot;
$potenciel_maj = $pot - $ca_enplus;
echo $potenciel_maj;
array_push($potentiel_json, (object)array(
'color' => 'green',
'name' => "CA realise",
'id' => '' + $ca + '',
'data' => array($ca_enplus),
));
array_push($potentiel_json, (object)array(
'name' => "Potentiel",
'id' => '' + $pot + '',
'data' => array(intval(number_format($potenciel_maj, 2, '.', ''))),
));
}
} else {
array_push($potentiel_json, (object)array(
'name' => "Potentiel",
'id' => 'Aucun Potentiel',
'data' => array(100),
));
}
}
$t_potentiel_json = json_encode($potentiel_json);
unset($potentiel_json);
$mois_francais = array(
1 => 'Jan',
2 => 'Fev',
3 => 'Mars',
4 => 'Avr',
5 => 'Mai',
6 => 'Juin',
7 => 'Juil',
8 => 'Aout',
9 => 'Sept',
10 => 'Oct',
11 => 'Nov',
12 => 'Dec'
);
return $this->render('User/index.html.twig', array(
'Res_CA_obj' => (!empty($caObj)) ? $caObj[0]->getMontantht() : '0.00',
'ListeTransfos' => $transfo[0],
'ListeRDV' => $rdvClient[0],
'Obj_RDV' => $obj_rdv,
'ListeSimulation' => $simu[0],
't_ListeProspect' => $t_ListeProspect,
't_ListePC' => $t_ListePC,
'tab_mois_fr' => $tab_mois_fr,
'tab_jours_fr' => $tab_jours_fr,
'Rdv' => $rdv,
'Messages' => $mes,
'fournisseurs' => $fournisseurs,
'annee_cal' => $annee_cal,
'mois_cal' => $mois_cal,
'semaine_cal' => $semaine_cal,
'mois' => $mois,
'annee' => $annee,
'semaine' => $semaine,
't_potentiel_json' => $t_potentiel_json,
't_objectif' => $t_objectif,
't_ca_chart_json' => $t_ca_chart_json,
't_gp_val_chart_json' => $t_gp_val_chart_json,
't_gp_client_chart_json' => $t_gp_client_chart_json,
't_CaFamilleForChart_json' => $t_CaFamilleForChart_json,
't_seg_client_chart_json' => $t_seg_client_chart_json,
't_seg_valeur_chart_json' => $t_seg_valeur_chart_json,
'type' => 'fournisseur',
't_listeNbCommande' => $t_listeNbCommande,
't_listeCaSeg' => $t_listeCaSeg,
't_ListeSeg' => $t_ListeSeg,
't_ListeGainsPertes' => $t_ListeGainsPertes,
't_ListeDetail' => $t_ListeDetail,
't_ListeMois' => $t_ListeMois,
't_ListeCA' => $t_ListeCA_frn,
'user' => $this->getUser()->getFamille(),
'nb_actif' => count($t_listClientActif),
'nb_nouveau' => count($t_listClientNouveau),
'nb_perdu' => count($t_listClientPerdu),
'current_year' => $annee,
'current_month' => $mois_francais[$mois_valide],
'current_month_detail' => $MonthValid[0]['note']
));
unset($t_listClientActif);
unset($t_listClientNouveau);
unset($t_listClientPerdu);
unset($t_ListeSeg);
unset($t_ListeGainsPertes);
unset($t_ListeDetail);
unset($t_ListeCA_frn);
unset($t_ListeMois);
unset($t_listClientPerdu);
unset($t_seg_client_chart_json);
unset($t_seg_valeur_chart_json);
unset($t_CaFamilleForChart_json);
unset($t_ca_chart_json);
unset($t_gp_client_chart_json);
unset($t_objectif);
unset($t_potentiel_json);
}
/**
* @Route("/menu", name="user_menu")
*/
public function menuAction()
{
return $this->render('User/menu.html.twig');
}
/**
* @Route("/select", name="select_client")
*/
public function selectClientsAction($id, EntityManagerInterface $em)
{
if ($id != 0) {
$clients = $em->getRepository(Client::class)->findMyClients($this->getUser()->getId());
$prospects = $em->getRepository(Client::class)->findMyProsPects($this->getUser()->getId());
} else {
$clients = $em->getRepository(Client::class)->findAllClientsInArray();
$prospects = $em->getRepository(Client::class)->findAllProspectInArray();
}
$form = $this->createForm(ClientProspectType::class);
return $this->render('User/clientsSelectors.html.twig', array(
'clients' => $clients,
'prospects' => $prospects,
'form' => $form->createView(),
));
}
/* AFFICHAGE DES SELECTEURS UTILISATEURS */
public function selectUtilisateursAction($id, $annee, $mois, $semaine, $route, EntityManagerInterface $em)
{
//RECUPERATION DES UTILISATEURS POUR LISTE DEROULANTE
$utilisateurs = $em->getRepository(Admin::class)->findBy(array('valide' => 1), array('contact' => 'ASC'));
return $this->render('User/utilisateursSelectors.html.twig', array(
'utilisateurs' => $utilisateurs,
'id' => $id,
'route' => $route,
'annee' => $annee,
'mois' => $mois,
'semaine' => $semaine
));
}
/* AFFICHAGE DES STATS GLOBALES D'UN UTILISATEUR */
/**
* @Route("/rapport_global/{date}/{id}", name="malys_user_globalreport", defaults={"date"=0, "id"=0})
*/
public function globalReportAction($date, $id, EntityManagerInterface $em)
{
/* annee courante par défaut */
if ($date == 0) {
$date = date('Y');
}
if ($id == 0) {
$id = $this->getUser()->getId();
$currentUser = true;
} else {
$currentUser = false;
}
/* selecteur de date */
$begin = date("Y", time()) - 3;
$end = date("Y", time()) + 1;
/* mois du tableau et service de graph */
$tab_mois_fr = $this->malysDates->getTabMoisFr();
// $this->container->get('malys_user.charts');
// CA GENERE DU MOIS POUR UN COMMERCIAL
$t_ListeCA = array();
$ca = $em->getRepository(Clientvente::class)->findCommercialCa($id, $date);
foreach ($ca as $c) {
$t_ListeCA[intval($c['le_mois'])] = $c['ca_genere'];
}
// CA GENERE PREVISIONNEL
$t_ListePrev = array();
$prev = $em->getRepository(Adminprev::class)->findCommercialPrevCa($id, $date);
foreach ($prev as $p) {
$t_ListePrev[intval($p['le_mois'])] = $p['montantht'];
}
//LISTE PROSPECTION
$t_ListeProspect = array();
$pro = $em->getRepository(Client::class)->findListeProspection($id, $date);
foreach ($pro as $p) {
$t_ListeProspect[intval($p['le_mois'])][$p[0]->getNote()] = $p['nbprospect'];
}
//LISTE SIMULATION
$t_ListeSimulation = array();
$sim = $em->getRepository(Adminevent::class)->findCommercialSimulation($id, $date);
foreach ($sim as $s) {
$t_ListeSimulation[intval($s['le_mois'])] = $s['nbsimulation'];
}
//LISTE RDV CLIENTS
$t_ListeRDV = array();
$rdv = $em->getRepository(Adminevent::class)->findMyClientRdv($id, $date);
foreach ($rdv as $r) {
$t_ListeRDV[intval($r['le_mois'])] = $r['nbrdv'];
}
//LISTE TRANSFOS CLIENTS
$t_ListeTransfos = array();
$tra = $em->getRepository(Client::class)->findMyTransfoClient($id, $date);
foreach ($tra as $t) {
$t_ListeTransfos[intval($t['le_mois'])] = $t['nbtransfos'];
}
/* GRAPH */
// $objChart = new \Chart(500, 240, "chart_bar_ca_mois_" . $id . "_gd");
// $objChart->SetHeaderName("Evolution CA " . $date);
// $objChart->SetY_Barchart_Show_Values(true);
//
// for ($i = 1; $i < 13; $i++) {
// $objChart->AddValue((string)$i, (isset($t_ListeCA[$i])) ? intval($t_ListeCA[$i]) : 0);
// }
//
// $objChart->SetImageDestination("images/graph/");
// $objChart->CreateChartImage(BAR_CHART);
return $this->render('User/globalReport.html.twig', array(
't_ListeTransfos' => $t_ListeTransfos,
't_ListeRDV' => $t_ListeRDV,
't_ListeSimulation' => $t_ListeSimulation,
't_ListeProspect' => $t_ListeProspect,
't_ListePrev' => $t_ListePrev,
't_ListeCA' => $t_ListeCA,
'tab_mois_fr' => $tab_mois_fr,
'date' => $date,
'begin' => $begin,
'end' => $end,
'id' => $id,
'currentUser' => $currentUser
));
}
/**
* @Route("/rapport_client/{date}", name="malys_user_clientreport", defaults={"date"=0})
*/
public function clientReportAction($date, EntityManagerInterface $em)
{
/* annee courante par défaut */
if ($date == 0) {
$date = date('Y');
}
/* selecteur de date */
$begin = date("Y", time()) - 3;
$end = date("Y", time()) + 1;
$tab_mois_fr = $this->malysDates->getTabMoisFr();
$t_ListeCA = array();
$t_ListeMois = array();
$t_ListeDetail = array();
$totalCaGenere = 0;
$totalCaCommission = 0;
$ca = $em->getRepository(Clientvente::class)->findCommercialCaParClient($this->getUser()->getId(), $date);
foreach ($ca as $c) {
//TABLEAU DETAILLE
if (!isset($t_ListeCA[$c[0]->getIdclient()->getNom()][intval($c['le_mois'])])) {
$t_ListeCA[$c[0]->getIdclient()->getNom()][intval($c['le_mois'])] = array();
}
$t_ListeCA[$c[0]->getIdclient()->getNom()][intval($c['le_mois'])]['ca_genere'] = number_format($c['ca_genere'], 2, '.', '');
$t_ListeCA[$c[0]->getIdclient()->getNom()][intval($c['le_mois'])]['ca_commission'] = number_format($c['ca_commission'], 2, '.', '');
$t_ListeCA[$c[0]->getIdclient()->getNom()][intval($c['le_mois'])]['solde'] = number_format($c['ca_solde'], 2, '.', '');
//TABLEAU MOIS PAR MOIS
if (!isset($t_ListeMois[intval($c['le_mois'])])) {
$t_ListeMois[intval($c['le_mois'])] = array('ca_genere' => 0, 'ca_commission' => 0, 'solde' => 0);
}
$t_ListeMois[intval($c['le_mois'])]['ca_genere'] += number_format($c['ca_genere'], 2, '.', '');
$t_ListeMois[intval($c['le_mois'])]['ca_commission'] += number_format($c['ca_commission'], 2, '.', '');
$t_ListeMois[intval($c['le_mois'])]['solde'] += number_format($c['ca_solde'], 2, '.', '');
$totalCaGenere += number_format($c['ca_genere'], 2, '.', '');
$totalCaCommission += number_format($c['ca_commission'], 2, '.', '');
//TABLEAU PAR TYPE DETAIL
if (!isset($t_ListeDetail[$c[0]->getIdclient()->getNom()])) {
$t_ListeDetail[$c[0]->getIdclient()->getNom()] = array(
'ca_genere' => 0,
'ca_commission' => 0,
'solde' => 0
);
}
$t_ListeDetail[$c[0]->getIdclient()->getNom()]['ca_genere'] += number_format($c['ca_genere'], 2, '.', '');
$t_ListeDetail[$c[0]->getIdclient()->getNom()]['ca_commission'] += number_format($c['ca_commission'], 2, '.', '');
$t_ListeDetail[$c[0]->getIdclient()->getNom()]['solde'] += number_format($c['ca_solde'], 2, '.', '');
}
return $this->render('user/clientReport.html.twig', array(
'totalCaGenere' => $totalCaGenere,
'totalCaCommission' => $totalCaCommission,
'tab_mois_fr' => $tab_mois_fr,
't_ListeCA' => $t_ListeCA,
't_ListeMois' => $t_ListeMois,
't_ListeDetail' => $t_ListeDetail,
'date' => $date,
'begin' => $begin,
'end' => $end
)
);
}
/**
* @Route("/rapport_liste_clients/{date}/{id}", name="malys_user_commercialreport", defaults={"date"=0, "id"=0})
*/
public function commercialReportAction($date, $id, Request $request, EntityManagerInterface $em)
{
/* AFFICHAGE DES STATS CLIENT D'UN UTILISATEUR */
/* annee courante par défaut */
if ($request->get('id')) {
$id = $request->get('id');
}
if ($request->get('vannee')) {
$date = $request->get('vannee');
}
if ($date == 0) {
$date = date('Y');
}
if ($id == 0) {
$id = $this->getUser()->getId();
}
$perf_ca = $request->get('perf_ca', null);
//echo $perf_ca;
/* selecteur de date */
$begin = date("Y", time()) - 3;
$end = date("Y", time()) + 1;
$tab_mois_fr = $this->malysDates->getTabMoisFr();
$t_ListeCA = array();
$t_ListeMois = array();
$t_ListeDetail = array();
$totalCaGenere = 0;
$totalCaCommission = 0;
$totalSolde = 0;
$ca = $em->getRepository(Clientvente::class)->findCommercialCaParClient($id, $date);
foreach ($ca as $c) {
//TABLEAU DETAILLE
if (!isset($t_ListeCA[$c[0]->getIdclient()->getNom()]['idclient'])) {
$t_ListeCA[$c[0]->getIdclient()->getNom()]['idclient'] = $c[0]->getIdclient()->getId();
}
if (!isset($t_ListeCA[$c[0]->getIdclient()->getNom()][intval($c['le_mois'])])) {
$t_ListeCA[$c[0]->getIdclient()->getNom()][intval($c['le_mois'])] = array();
}
$t_ListeCA[$c[0]->getIdclient()->getNom()][intval($c['le_mois'])]['ca_genere'] = number_format($c['ca_genere'], 2, '.', '');
$t_ListeCA[$c[0]->getIdclient()->getNom()][intval($c['le_mois'])]['ca_commission'] = number_format($c['ca_commission'], 2, '.', '');
$t_ListeCA[$c[0]->getIdclient()->getNom()][intval($c['le_mois'])]['solde'] = number_format($c['ca_solde'], 2, '.', '');
$t_ListeCA[$c[0]->getIdclient()->getNom()]['cagenere_total'] += number_format($c['ca_genere'], 2, '.', '');
//TABLEAU MOIS PAR MOIS
if (!isset($t_ListeMois[intval($c['le_mois'])])) {
$t_ListeMois[intval($c['le_mois'])] = array('ca_genere' => 0, 'ca_commission' => 0, 'solde' => 0);
}
$t_ListeMois[intval($c['le_mois'])]['ca_genere'] += number_format($c['ca_genere'], 2, '.', '');
$t_ListeMois[intval($c['le_mois'])]['ca_commission'] += number_format($c['ca_commission'], 2, '.', '');
$t_ListeMois[intval($c['le_mois'])]['solde'] += number_format($c['ca_solde'], 2, '.', '');
$totalCaGenere += number_format($c['ca_genere'], 2, '.', '');
$totalCaCommission += number_format($c['ca_commission'], 2, '.', '');
$totalSolde += number_format($c['ca_solde'], 2, '.', '');
//TABLEAU PAR TYPE DETAIL
if (!isset($t_ListeDetail[$c[0]->getIdclient()->getNom()])) {
$t_ListeDetail[$c[0]->getIdclient()->getNom()] = array(
'ca_genere' => 0,
'ca_commission' => 0,
'solde' => 0
);
}
$t_ListeDetail[$c[0]->getIdclient()->getNom()]['ca_genere'] += number_format($c['ca_genere'], 2, '.', '');
$t_ListeDetail[$c[0]->getIdclient()->getNom()]['ca_commission'] += number_format($c['ca_commission'], 2, '.', '');
$t_ListeDetail[$c[0]->getIdclient()->getNom()]['solde'] += number_format($c['ca_solde'], 2, '.', '');
}
if ($perf_ca != null) {
uasort($t_ListeCA, "App\Controller\User\UserController::cmpCa");
}
/*echo "<pre>";
print_r($t_ListeCA);
echo "</pre>";*/
return $this->render('User/commercialReport.html.twig', array(
'totalCaGenere' => $totalCaGenere,
'totalCaCommission' => $totalCaCommission,
'totalSolde' => $totalSolde,
'tab_mois_fr' => $tab_mois_fr,
't_ListeCA' => $t_ListeCA,
't_ListeMois' => $t_ListeMois,
't_ListeDetail' => $t_ListeDetail,
'date' => $date,
'begin' => $begin,
'end' => $end,
'id' => $id
)
);
}
private static function cmpCa($a, $b)
{
if ($a['cagenere_total'] < $b['cagenere_total']) {
return true;
}
return false;
}
/**
* @Route("/rapport_detaille/{annee}/{mois}/{semaine}/{id}", name="malys_user_detailreport", defaults={"semaine"=0, "id"=0})
*/
public function detailReportAction($annee, $mois, $semaine, $id, Request $request, EntityManagerInterface $em)
{
$annee_cal = $annee;
$mois_cal = $mois;
$semaine_cal = $semaine;
if ($id == 0) {
$id = $this->getUser()->getId();
$currentUser = true;
} else {
$currentUser = false;
}
/* SEMAINE PAR DEFAUT */
if ($semaine == 0) {
if ($mois != date("n", time())) {
$semaine = date("W", mktime(0, 0, 0, $mois, 1, $annee));
} else {
$semaine = date("W", mktime(0, 0, 0, $mois, date('d'), $annee));
}
}
$tab_mois_fr = $this->malysDates->getTabMoisFr();
//CA GENERE DU MOIS POUR UN COMMERCIAL
$ca = $em->getRepository(Clientvente::class)->findCommercialCaInMonth($id, $annee, $mois);
//OBJECTIF DE CA
$caObj = $em->getRepository(Adminprev::class)->findCommercialPrevCaInMonth($id, $annee, $mois);
//RECUPERATION DES COMMISSIONS UTILISATEURS
$com = $em->getRepository(Clientvente::class)->findCommercialCommission($id, $annee, $mois);
/* TOTAL COMMISSION */
$totalCommission = 0;
foreach ($com as $c) {
$totalCommission += $c->getMontantht() * $c->getAdmincom() / 100;
}
//LISTE PROSPECTION HEBDO
$t_ListeProspect = array();
$totalProspect = 0;
$pro = $em->getRepository(Client::class)->findListeProspectionHebdo($id, $annee, $semaine);
foreach ($pro as $p) {
$t_ListeProspect[$p[0]->getNote()] = $p['nbprospect'];
$totalProspect += $p['nbprospect'];
}
//RECUPERATION DES OBJECTIFS RDV
$rdv = $em->getRepository(Adminprevrdv::class)->findOneBy(array(
'idadmin' => $id,
'annee' => $annee,
'semaine' => $semaine
));
//LISTE SIMULATION HEBDO
$simu = $em->getRepository(Adminevent::class)->findCommercialSimulationHebdo($id, $annee, $semaine);
//LISTE RDV CLIENTS
$rdvClient = $em->getRepository(Adminevent::class)->findMyClientRdvHebdo($id, $annee, $semaine);
//LISTE TRANSFOS CLIENTS
$transfo = $em->getRepository(Client::class)->findMyTransfoClientHebdo($id, $annee, $semaine);
//LISTE DETAILLEE PROSPECTION HEBDO
$dPro = $em->getRepository(Client::class)->findListeProspectHebdoDetail($id, $annee, $semaine);
//LISTE SIMULATION HEBDO DETAIL
$s = $em->getRepository(Adminevent::class)->findCommercialSimulationHebdoDetail($id, $annee, $semaine);
//LISTE DETAILLEE RDV CLIENTS
$rdvClientDetail = $em->getRepository(Adminevent::class)->findMyClientRdvHebdoDetail($id, $annee, $semaine);
//LISTE DETAILLEE TRANSFOS CLIENTS
$tansfoDet = $em->getRepository(Client::class)->findMyTransfoClientHebdoDetail($id, $annee, $semaine);
//RECUPERATION DES RELANCES PROSPECTS
$relances = $em->getRepository(Adminevent::class)->findProspectRelanceHebdo($id, $annee, $semaine);
//ALL CA FRN BY MONTH/YEAR OF COMMERCIAL
$getAllCaByMonth = $em->getRepository(Clientvente::class)->getAllCaByMonth(3, $id, "Commercial");
//RECUPERE LE MOIS VALIDE LE PLUS PROCHE
$MonthValid = $em->getRepository(Moisvalide::class)->findLastMoisvalide(date("n"), date("Y"));
$annee = $MonthValid[0]['annee'];
$mois_valide = $MonthValid[0]['mois'];
//ALL OBJECTIF BY MONTH/YEAR
$t_Objectifs = $em->getRepository(Adminprev::class)->getAllPrevCaByMonth($id, "Commercial", $annee);
$t_objectif = array();
$obj_total = 0;
foreach ($t_Objectifs as $key => $item) {
(intval($item['objectif'] != 0)) ? array_push($t_objectif, intval($item['objectif'])) : array_push($t_objectif, null);
$obj_total += intval($item['objectif']);
}
//création json pour chart CA
$t_ca_chart = array();
foreach ($getAllCaByMonth as $key => $item) {
$t_ca = array();
$i = 0;
foreach ($item as $year => $ca) {
if ($i != 0) {
(intval($ca) != 0) ? array_push($t_ca, intval($ca)) : array_push($t_ca, null);
}
$i++;
}
($item["Annee"] == $annee) ? array_push($t_ca_chart, (object)array(
'name' => $item["Annee"],
'data' => $t_ca
)) : array_push($t_ca_chart, (object)array(
'name' => $item["Annee"],
'visible' => false,
'data' => $t_ca
));
}
array_push($t_ca_chart, (object)array('name' => "Objectifs", 'color' => '#a3dfd9', 'data' => $t_objectif));
array_push($t_objectif, $obj_total);
$t_ca_chart_json = json_encode($t_ca_chart);
unset($t_ca_chart); //Free memoire
//ALL CA CLIENT BY MONTH/YEAR
$t_allCaClientByMonth = $em->getRepository(Clientvente::class)->getAllClientsCaLight($annee, null, 'three year', $id, "commercial");
$t_ListeCaClientForChart = array();
$t_ListeCaFamilleForChart = array();
$t_CaFamilleForChart = array();
// Recuperation tab CAclient/mois et tab CaFamille/mois
foreach ($t_allCaClientByMonth as $caClient => $item) {
$yearfr = $item['datefr']->format('Y');
$monthfr = $item['datefr']->format('n');
// Si l'année, le mois et le client n'existe pas dans le tableau d'arrivée, on l'ajoute au tableau
if ($yearfr == date('Y') || $yearfr == date('Y') - 1) {
if (!isset($t_ListeCaClientForChart[$yearfr][$monthfr][$item['id']])) {
if ($item['ca_genere'] > 0) {
$t_ListeCaClientForChart[$yearfr][$monthfr][$item['id']] = array();
$t_ListeCaClientForChart[$yearfr][$monthfr][$item['id']]['ca_genere'] = number_format((float)$item['ca_genere'], 2, '.', '');
}
} else { // Si la case existe déjà
if ($item['ca_genere'] > 0) {
$t_ListeCaClientForChart[$yearfr][$monthfr][$item['id']]['ca_genere'] += number_format((float)$item['ca_genere'], 2, '.', '');
}
}
}
// Si l'année n'existe pas dans le tableau CAfamille d'arrivée, on ajoute la case avec la l'année
if ($yearfr == date('Y')) {
if (!isset($t_ListeCaFamilleForChart[$item['famille']])) {
$t_ListeCaFamilleForChart[$item['famille']] = array();
$t_ListeCaFamilleForChart[$item['famille']] = number_format((float)$item['ca_genere'], 2, '.', '');
} else {
$t_ListeCaFamilleForChart[$item['famille']] += number_format((float)$item['ca_genere'], 2, '.', '');
}
}
}
unset($t_allCaClientByMonth); //Free memoire
$t_ca_famille = array();
foreach ($t_ListeCaFamilleForChart as $key => $value) {
($key != "") ? array_push($t_ca_famille, array(
$key,
intval($value)
)) : array_push($t_ca_famille, array("AUTRE", intval($value)));
}
// Obtient une liste de colonnes
$volume = array();
foreach ($t_ca_famille as $key => $row) {
$volume[$key] = $row[1];
$edition[$key] = $row[0];
}
// Trie les données par volume décroissant, edition croissant
// Ajoute $data en tant que dernier paramètre, pour trier par la clé commune
if ($volume) {
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $t_ca_famille);
}
array_push($t_CaFamilleForChart, (object)array(
'type' => 'pie',
'name' => 'Famille',
'data' => $t_ca_famille
));
unset($t_ca_famille); // free Memory
$t_CaFamilleForChart_json = json_encode($t_CaFamilleForChart);
unset($t_CaFamilleForChart);
unset($t_ListeCaFamilleForChart);
$t_gp_val_chart = array();
$t_gp_client_chart = array();
$t_ListeGainsPertes = array(
0 => array('gain' => array(), 'perte' => array(), 'type' => 'ca'),
1 => array('gain' => array(), 'perte' => array(), 'type' => 'client')
);
if (isset($t_ListeCaClientForChart[$annee - 1][12])) {
foreach ($t_ListeCaClientForChart[$annee - 1][12] as $id_client => $ca) {
(!array_key_exists($id_client, $t_ListeCaClientForChart[$annee][1])) ? $t_ListeCaClientForChart[$annee][1][$id_client] = array('ca_genere' => 0.0) : null;
}
}
foreach ($t_ListeCaClientForChart as $year => $item) {
foreach ($item as $month => $val) {
foreach ($val as $id_client => $ca) {
for ($i = 1; $i < count($item); $i++) {
if (isset($t_ListeCaClientForChart[$year][$i])) {
(!array_key_exists($id_client, $t_ListeCaClientForChart[$year][$i])) ? $t_ListeCaClientForChart[$year][$i][$id_client] = array('ca_genere' => 0.0) : null;
}
}
}
}
}
for ($i = 1; $i <= 12; $i++) {
(!isset($t_ListeCaClientForChart[$annee][$i])) ? $t_ListeCaClientForChart[$annee][$i] = null : 1;
}
ksort($t_ListeCaClientForChart[$annee], SORT_ASC);
//calcul et recuperation "pertes et gains" total en valeur et client/MOIS
foreach ($t_ListeCaClientForChart as $year => $item) {
$t_gains_valeur = array();
$t_pertes_valeur = array();
$t_gains_client = array();
$t_pertes_client = array();
if ($year == $annee) {
foreach ($item as $month => $val) {
$gainsValeur = 0;
$pertesValeur = 0;
$gainsClient = 0;
$pertesClient = 0;
if ($val != null) {
foreach ($val as $client => $caClient) {
$caMoisPrec = 0;
if ($month != 1) { // Si C'est pas le mois de janvier
if (isset($item[$month - 1][$client])) {
$caMoisPrec = $item[$month - 1][$client]['ca_genere'];
}
} else { // si c'est janvier => IDEM avec décembre de l'année précédente
if (isset($t_ListeCaClientForChart[$year - 1][12][$client])) {
$caMoisPrec = $t_ListeCaClientForChart[$year - 1][12][$client]['ca_genere'];
}
}
$diff = ($caClient['ca_genere']) - ($caMoisPrec); // ON calcul $diff => CA du mois - (Ca du mois-1) [exemple: CAmars - CAfevrier ]
($diff < 0) ? $pertesValeur += $diff : $gainsValeur += $diff; // On met a jour Gains et Pertes
//On calcul $evoCa => (CA du mois - (Ca du mois-1)) / (Ca du mois-1) [exemple: (CAmars - CAfevrier)/CAfevrier ]
if ($caMoisPrec != 0) {
$evoCa = (($caClient['ca_genere']) - ($caMoisPrec)) / ($caMoisPrec);
} else {
$evoCa = 0;
}
// On met a jour Gains et Pertes Client
if ($evoCa < 0) {
$pertesClient--;
}
if ($evoCa > 0) {
$gainsClient++;
}
}
} else {
$gainsClient = null;
$pertesClient = null;
$pertesValeur = null;
$gainsValeur = null;
}
array_push($t_gains_valeur, $gainsValeur);
array_push($t_pertes_valeur, $pertesValeur);
array_push($t_gains_client, $gainsClient);
array_push($t_pertes_client, $pertesClient);
}
}
if ($year == $annee) {
array_push($t_gp_val_chart, (object)array('name' => "Gains", 'data' => $t_gains_valeur)); //Valeur
array_push($t_gp_val_chart, (object)array(
'name' => "Pertes",
'data' => $t_pertes_valeur
)); //Valeur
array_push($t_gp_client_chart, (object)array(
'name' => "Gains",
'data' => $t_gains_client
)); //Client
array_push($t_gp_client_chart, (object)array(
'name' => "Pertes",
'data' => $t_pertes_client
)); //Client
array_push($t_ListeGainsPertes[0]['gain'], $t_gains_valeur); //Valeur
array_push($t_ListeGainsPertes[0]['perte'], $t_pertes_valeur); //Valeur
array_push($t_ListeGainsPertes[1]['gain'], $t_gains_client); //Client
array_push($t_ListeGainsPertes[1]['perte'], $t_pertes_client); //Client
}
}
$t_gp_val_chart_json = json_encode($t_gp_val_chart);
$t_gp_client_chart_json = json_encode($t_gp_client_chart);
/*TABLEAU GAINS PERTES */
$tab_moy = array('moyValP' => 0, 'moyCliP' => 0, 'moyValG' => 0, 'moyCliG' => 0);
$next = 0;
$nbmonth = 0;
foreach ($t_ListeGainsPertes as $key => $val) {
if ($val['gain'][0]) {
foreach ($val['gain'][0] as $k => $value) {
($next == 0) ? $tab_moy['moyValG'] += $value : $tab_moy['moyCliG'] += $value;
($next == 0) ? $nbmonth++ : null;
}
}
if ($val['perte'][0]) {
foreach ($val['perte'][0] as $k => $value) {
($next == 0) ? $tab_moy['moyValP'] += $value : $tab_moy['moyCliP'] += $value;
}
}
$next++;
}
if ($nbmonth != 0) {
array_push($t_ListeGainsPertes[0]['gain'], $tab_moy['moyValG'] / $nbmonth);
array_push($t_ListeGainsPertes[0]['perte'], $tab_moy['moyValP'] / $nbmonth);
array_push($t_ListeGainsPertes[1]['gain'], $tab_moy['moyCliG'] / $nbmonth);
array_push($t_ListeGainsPertes[1]['perte'], $tab_moy['moyCliP'] / $nbmonth);
}
unset($t_gp_val_chart); //Free Memoire
unset($t_gp_client_chart);
unset($t_gains_valeur);
unset($t_pertes_valeur);
unset($t_gains_client);
unset($t_pertes_client);
// Recuperation segClient/mois et segValeur/mois
$t_seg_client_chart = array();
$t_seg_valeur_chart = array();
$t_seg_client = array(
'0' => array(),
'1' => array(),
'2' => array(),
'3' => array(),
'4' => array(),
'5' => array()
);
$t_seg_valeur = array(
'0' => array(),
'1' => array(),
'2' => array(),
'3' => array(),
'4' => array(),
'5' => array()
);
$t_seg = array(
0 => array("palier" => 500, "valeur" => 0, "client" => 0, "name" => '0-500'),
1 => array("palier" => 1500, "valeur" => 0, "client" => 0, "name" => '500-1500'),
2 => array("palier" => 3000, "valeur" => 0, "client" => 0, "name" => '1500-3000'),
3 => array("palier" => 9000, "valeur" => 0, "client" => 0, "name" => '3000-9000'),
4 => array("palier" => 20000, "valeur" => 0, "client" => 0, "name" => '9000-20000'),
5 => array("valeur" => 0, "client" => 0, "name" => '>20000'),
);
$Liste_client_groupe1 = array($year => array());
// On recupere les valeurs de segmentation
foreach ($t_ListeCaClientForChart as $year => $item) {
if ($year == $annee) {
foreach ($item as $month => $val) {
for ($i = 0; $i < count($t_seg); $i++) {
$t_seg[$i]["valeur"] = 0;
$t_seg[$i]["client"] = 0;
}
(!isset($Liste_client_groupe1[$year][$month])) ? $Liste_client_groupe1[$year][$month] = array() : 1;
if ($val != null) {
foreach ($val as $client => $caClient) {
//Récupération des valeurs de segementation
if ($caClient['ca_genere'] > 1 && $caClient['ca_genere'] <= $t_seg[0]["palier"]) {
$t_seg[0]["valeur"] += $caClient['ca_genere'];
$t_seg[0]["client"] += 1;
//TODO EXTRACT LIST OF CLIENT BY GROUPE AND BY MONTH
array_push($Liste_client_groupe1[$year][$month], $client);
} else if ($caClient['ca_genere'] > $t_seg[0]["palier"] && $caClient['ca_genere'] <= $t_seg[1]["palier"]) {
$t_seg[1]["valeur"] += $caClient['ca_genere'];
$t_seg[1]["client"] += 1;
} else if ($caClient['ca_genere'] > $t_seg[1]["palier"] && $caClient['ca_genere'] <= $t_seg[2]["palier"]) {
$t_seg[2]["valeur"] += $caClient['ca_genere'];
$t_seg[2]["client"] += 1;
} else if ($caClient['ca_genere'] > $t_seg[2]["palier"] && $caClient['ca_genere'] <= $t_seg[3]["palier"]) {
$t_seg[3]["valeur"] += $caClient['ca_genere'];
$t_seg[3]["client"] += 1;
} else if ($caClient['ca_genere'] > $t_seg[3]["palier"] && $caClient['ca_genere'] <= $t_seg[4]["palier"]) {
$t_seg[4]["valeur"] += $caClient['ca_genere'];
$t_seg[4]["client"] += 1;
} else if ($caClient['ca_genere'] > $t_seg[4]["palier"]) {
$t_seg[5]["valeur"] += $caClient['ca_genere'];
$t_seg[5]["client"] += 1;
}
}
}
foreach ($t_seg as $key => $value) {
if ($year == $annee) {
for ($i = 0; $i < 6; $i++) {
if ($key == $i) {
array_push($t_seg_client[$i], $value['client']);
array_push($t_seg_valeur[$i], $value['valeur']);
}
}
}
}
}
}
}
$t_ListeSeg = array();
array_push($t_ListeSeg, $t_seg_client);
array_push($t_ListeSeg, $t_seg_valeur);
foreach ($t_ListeSeg as $key => $val) {
foreach ($val as $k => $value) {
$total = 0;
for ($i = 0; $i < count($value); $i++) {
$total += $value[$i];
}
if ($key == 1) {
$t_ListeSeg[$key][$k]['total_group'] = $total;
} else {
(count($value) != 0) ? $t_ListeSeg[$key][$k]['total_group'] = ($total / count($value)) : null;
}
for ($j = 0; $j < count($t_seg); $j++) {
$t_ListeSeg[$key][$j]['nom'] = $t_seg[$j]['name'];
}
}
}
$t_listeNbCommande = array();
$t_listeCaSeg = array();
for ($j = 0; $j < 12; $j++) {
$total_client = 0;
$total_ca = 0;
foreach ($t_ListeSeg[0] as $key => $val) {
(isset($val[$j])) ? $total_client += $val[$j] : null;
}
array_push($t_listeNbCommande, $total_client);
foreach ($t_ListeSeg[1] as $key => $val) {
(isset($val[$j])) ? $total_ca += $val[$j] : null;
}
array_push($t_listeCaSeg, $total_ca);
}
for ($j = 5; $j > -1; $j--) {
array_push($t_seg_client_chart, (object)array(
'type' => "column",
'name' => $t_seg[$j]['name'],
'data' => $t_seg_client[$j],
'id' => $t_seg[$j]['name'] . 'c',
)
);
array_push($t_seg_valeur_chart, (object)array(
'type' => "column",
'name' => $t_seg[$j]['name'],
'data' => $t_seg_valeur[$j],
'id' => $t_seg[$j]['name'] . 'v',
));
}
for ($j = 5; $j > -1; $j--) {
array_push($t_seg_client_chart, (object)array(
'name' => 'seg1->',
'id' => $t_seg[$j]['name'] . 'cc',
'showInLegend' => false,
'color' => 'red',
'lineWidth' => 1,
'marker' => (object)array(
'lineWidth' => 0.1,
'lineColor' => 'Highcharts.getOptions().colors[4]',
'fillColor' => "white",
"radius" => 2,
'enabled' => true
),
'data' => $t_seg_client[$j]
));
array_push($t_seg_valeur_chart, (object)array(
'name' => "seg1->",
'id' => $t_seg[$j]['name'] . 'vv',
'showInLegend' => false,
'color' => 'red',
'lineWidth' => 1,
'marker' => (object)array(
'lineWidth' => 0.1,
'lineColor' => 'Highcharts.getOptions().colors[4]',
'fillColor' => "white",
"radius" => 2,
'enabled' => true
),
'data' => $t_seg_valeur[$j]
));
}
$t_seg_client_chart_json = json_encode($t_seg_client_chart);
$t_seg_valeur_chart_json = json_encode($t_seg_valeur_chart);
unset($t_seg);
unset($t_seg_client_chart);
unset($t_seg_client); //free Memory
unset($t_seg_valeur_chart);
unset($t_seg_valeur); //free Memory
// Recupération informations pour tableau fournisseurs global
/* TRAITEMENT FORMULAIRE ET VALEURS PAR DEFAUT */
$year = $request->get('year', date('Y'));
$source = $request->get('source', null) == '' ? null : $request->get('source', null);
$perf_ca = $request->get('perf_ca', null);
$type = 'fournisseur';
/* GET CA */
$cv = $em->getRepository(Clientvente::class)->getAllFournisseursCa($year, $source, "three year", $id, "commercial");
$t_ListeCA_frn = array();
$t_ListeMois = array();
$t_ListeDetail = array();
foreach ($cv as $c) {
$mois = intval($c[0]['datefr']->format('m'));
$yeartraitement = intval($c[0]['datefr']->format('Y'));
$id_frn = $c['id'];
//TABLEAU ANNNEE TOTAUX
if (!isset($t_ListeMois[$yeartraitement])) {
$t_ListeMois[$yeartraitement] = array(
'cagenere_total' => 0,
'cacommission_total' => 0,
'solde_total' => 0
);
}
//TABLEAU DETAILLE
if (!isset($t_ListeCA_frn[$yeartraitement][$id_frn])) {
$t_ListeCA_frn[$yeartraitement][$id_frn] = array(
'cagenere_total' => 0,
'cacommission_total' => 0,
'solde_total' => 0
);
}
$t_ListeCA_frn[$yeartraitement][$id_frn][$mois]['ca_genere'] = number_format($c['ca_genere'], 2, '.', '');
$t_ListeCA_frn[$yeartraitement][$id_frn][$mois]['ca_commission'] = number_format($c['ca_commission'], 2, '.', '');
$t_ListeCA_frn[$yeartraitement][$id_frn][$mois]['solde'] = number_format($c['ca_solde'], 2, '.', '');
$t_ListeCA_frn[$yeartraitement][$id_frn]['id' . $type] = $c['id'];
$t_ListeCA_frn[$yeartraitement][$id_frn]['nom'] = $c['nom'];
$t_ListeCA_frn[$yeartraitement][$id_frn]['cagenere_total'] += number_format($c['ca_genere'], 2, '.', '');
$t_ListeCA_frn[$yeartraitement][$id_frn]['cacommission_total'] += number_format($c['ca_commission'], 2, '.', '');
$t_ListeCA_frn[$yeartraitement][$id_frn]['solde_total'] += number_format($c['ca_solde'], 2, '.', '');
$t_ListeCA_frn[$yeartraitement][$id_frn]['source'] = $c['source'];
//TABLEAU ANNEE MOIS PAR MOIS
if (!isset($t_ListeMois[$yeartraitement][$mois])) {
$t_ListeMois[$yeartraitement][$mois] = array(
'ca_genere' => 0,
'ca_commission' => 0,
'solde' => 0
);
}
$t_ListeMois[$yeartraitement][$mois]['ca_genere'] += number_format($c['ca_genere'], 2, '.', '');
$t_ListeMois[$yeartraitement][$mois]['ca_commission'] += number_format($c['ca_commission'], 2, '.', '');
$t_ListeMois[$yeartraitement][$mois]['solde'] += number_format($c['ca_solde'], 2, '.', '');
//TEABLEAU TOTAL
$t_ListeMois[$yeartraitement]['cagenere_total'] += number_format($c['ca_genere'], 2, '.', '');
$t_ListeMois[$yeartraitement]['cacommission_total'] += number_format($c['ca_commission'], 2, '.', '');
$t_ListeMois[$yeartraitement]['solde_total'] += number_format($c['ca_solde'], 2, '.', '');
//TABLEAU PAR TYPE DETAIL
if (!isset($t_ListeDetail[$yeartraitement][$id_frn])) {
$t_ListeDetail[$yeartraitement][$id_frn] = array(
'ca_genere' => 0,
'ca_commission' => 0,
'solde' => 0
);
}
$t_ListeDetail[$yeartraitement][$id_frn]['ca_genere'] += number_format($c['ca_genere'], 2, '.', '');
$t_ListeDetail[$yeartraitement][$id_frn]['ca_commission'] += number_format($c['ca_commission'], 2, '.', '');
$t_ListeDetail[$yeartraitement][$id_frn]['solde'] += number_format($c['ca_solde'], 2, '.', '');
$t_ListeDetail[$yeartraitement][$id_frn]['source'] = $c['source'];
}
if ($perf_ca != null) {
uasort($t_ListeCA, "App\\Controller\\Marketing::cmpCa");
}
return $this->render('User/detailReport.html.twig', array(
'ListeRelance' => $relances[0],
'ListeSimulationDet' => $s,
't_prospect_note' => Tools::getNote(),
'ListeDetProspect' => $dPro,
'ListeDetTransfos' => $tansfoDet,
'ListeTransfos' => $transfo[0],
'ListeDetRDV' => $rdvClientDetail,
'ListeRDV' => $rdvClient[0],
'ListeSimulation' => $simu[0],
'Obj_RDV' => $rdv,
'Res_CA' => (!empty($ca) && isset($ca[0]['ca_genere'])) ? $ca[0]['ca_genere'] : '0.00',
'Res_CA_obj' => (!empty($caObj)) ? $caObj[0]->getMontantht() : '0.00',
'totalProspect' => $totalProspect,
't_ListeProspect' => $t_ListeProspect,
'Commissions' => $com,
'totalCommission' => $totalCommission,
'tab_mois_fr' => $tab_mois_fr,
'mois_cal' => $mois_cal,
'annee_cal' => $annee_cal,
'semaine_cal' => $semaine_cal,
'mois' => $mois,
'annee' => $annee,
'semaine' => $semaine,
'id' => $id,
'currentUser' => $currentUser,
't_objectif' => $t_objectif,
't_ca_chart_json' => $t_ca_chart_json,
'user' => $this->getUser()->getFamille(),
't_ListeSeg' => $t_ListeSeg,
't_ListeGainsPertes' => $t_ListeGainsPertes,
't_ListeDetail' => $t_ListeDetail,
't_ListeMois' => $t_ListeMois,
't_ListeCA' => $t_ListeCA_frn,
't_ca_chart_json' => $t_ca_chart_json,
't_gp_val_chart_json' => $t_gp_val_chart_json,
't_gp_client_chart_json' => $t_gp_client_chart_json,
't_CaFamilleForChart_json' => $t_CaFamilleForChart_json,
't_seg_client_chart_json' => $t_seg_client_chart_json,
't_seg_valeur_chart_json' => $t_seg_valeur_chart_json,
'type' => 'fournisseur',
't_listeNbCommande' => $t_listeNbCommande,
't_listeCaSeg' => $t_listeCaSeg,
));
}
public function calMonthAction($mois, $annee, $semaine, $route, $id = 0)
{
if ($id == 0) {
$currentUser = true;
} else {
$currentUser = false;
}
//valeurs par defaut pour le calendrier
$mois = $mois == "" ? date("n", mktime(0, 0, 0, date("m"), date("d"), $annee)) : $mois;
$annee = $annee == "" ? date("Y", mktime(0, 0, 0, date("m"), date("d"), $annee)) : $annee;
//initialisation txt fran�ais
$mois_nom = array(
"Janvier",
"Février",
"Mars",
"Avril",
"Mai",
"Juin",
"Juillet",
"Août",
"Septembre",
"Octobre",
"Novembre",
"Décembre"
);
//initialisation des dates
$premierj = date("w", mktime(0, 0, 0, $mois, 1, $annee));
$moisnj = date("t", mktime(0, 0, 0, $mois, 1, $annee));
//initialisation des donn�es tableau
if ($premierj == 0) {
$premierj = 7;
}
//nombre de case du calendrier
$nbcases = ($moisnj + $premierj - 1);
//nombre de lignes du calendrier
$nblignes = ceil($nbcases / 7);
//liens précédents & suivant
$mois_pre = $mois == 1 ? 12 : ($mois - 1);
$mois_suiv = $mois == 12 ? 1 : ($mois + 1);
$annee_prev = $mois == 1 ? $annee - 1 : $annee;
$annee_next = $mois == 12 ? $annee + 1 : $annee;
//CONSTRUCTION DU TABLEAU
$tab_html = "<table border='0' width='100%' cellspacing='0' cellpadding='0' align='center' class='Calendrier_fond'>";
//LIGNES DE LA NAVIGATION MOIS
$tab_html .= "<tr>";
$tab_html .= "<td>";
$tab_html .= "<table width='100%' border='0' cellspacing='0' cellpadding='3' class='Calendrier_titre'>";
$tab_html .= "<tr>";
if ($currentUser) {
$tab_html .= "<td width='30'><a href='" . $this->generateUrl($route, array(
'annee' => $annee_prev,
'mois' => $mois_pre
)) . "'><<</a></td>";
} else {
$tab_html .= "<td width='30'><a href='" . $this->generateUrl($route, array(
'annee' => $annee_prev,
'mois' => $mois_pre,
'id' => $id
)) . "'><<</a></td>";
}
$tab_html .= "<td align='center'>" . $mois_nom[$mois - 1] . " " . $annee . "</td>";
if ($currentUser) {
$tab_html .= "<td width='30' align='right'><a href='" . $this->generateUrl($route, array(
'annee' => $annee_next,
'mois' => $mois_suiv
)) . "'>>></a></td>";
} else {
$tab_html .= "<td width='30' align='right'><a href='" . $this->generateUrl($route, array(
'annee' => $annee_next,
'mois' => $mois_suiv,
'id' => $id
)) . "'>>></a></td>";
}
$tab_html .= "</tr>";
$tab_html .= "</table>";
$tab_html .= "</td>";
$tab_html .= "</tr>";
//LIGNES DU CALENDRIER
$tab_html .= "<tr>";
$tab_html .= "<td>";
$tab_html .= "<table width='100%' border='0' cellspacing='0' cellpadding='2' class='Calendrier_jours'>";
$tab_html .= "<tr>";
$tab_html .= "<td width='23'> </td>";
$tab_html .= "<td width='23'>L</td>";
$tab_html .= "<td width='23'>M</td>";
$tab_html .= "<td width='23'>M</td>";
$tab_html .= "<td width='23'>J</td>";
$tab_html .= "<td width='23'>V</td>";
$tab_html .= "<td width='23'>S</td>";
$tab_html .= "<td width='23'>D</td>";
$tab_html .= "</tr>";
//initialisation du compteur de case
$compteurj = 1;
//ecriture des cases
for ($i = 1; $i < ($nblignes + 1); $i++) {
$tab_html .= "<tr>";
//definition de la semaine
if ($i == 1) {
$numsemaine = intval(date("W", mktime(0, 0, 0, $mois, 1, $annee)));
} else {
$numsemaine = intval(date("W", mktime(0, 0, 0, $mois, $compteurj - $premierj + 1, $annee)));
}
if ($currentUser) {
$tab_html .= "<td width='23' " . (($semaine == $numsemaine) ? "class='Calendrier_LaSemaine'" : "") . "><a href='" . $this->generateUrl($route, array(
'annee' => $annee,
'mois' => $mois,
'semaine' => $numsemaine
)) . "'>" . $numsemaine . "</a></td>";
} else {
$tab_html .= "<td width='23' " . (($semaine == $numsemaine) ? "class='Calendrier_LaSemaine'" : "") . "><a href='" . $this->generateUrl($route, array(
'annee' => $annee,
'mois' => $mois,
'semaine' => $numsemaine,
'id' => $id
)) . "'>" . $numsemaine . "</a></td>";
}
for ($j = 1; $j < 8; $j++) {
if (($compteurj >= $premierj) && ($compteurj <= ($moisnj + $premierj - 1))) {
$vclass = (($compteurj - $premierj + 1) . "/" . $mois . "/" . $annee == date("j/m/Y")) ? " class='Calendrier_LeJour'" : "";
$tab_html .= "<td " . $vclass . ">" . ($compteurj - $premierj + 1) . "</td>";
} else {
$tab_html .= "<td> </td>";
}
$compteurj++;
}
$tab_html .= "</tr>";
}
$tab_html .= "</table>";
$tab_html .= "</td>";
$tab_html .= "</tr>";
$tab_html .= "</table>";
//return response
$response = new \Symfony\Component\HttpFoundation\Response;
$response->setContent($tab_html);
return $response;
}
/**
* @Route("/planning/{annee}/{mois}/{semaine}", name="malys_user_planning", defaults={"semaine"=0})
*/
public function planningAction($annee, $mois, $semaine, EntityManagerInterface $em)
{
if ($semaine == 0) {
$semaine = date('W', mktime(0, 0, 0, $mois, 1, $annee));
}
$tab_mois_fr = $this->malysDates->getTabMoisFr();
$tab_jours_fr = $this->malysDates->getTabJoursFr();
//AFFICHAGE DES RDV DE LA SEMAINE
$rdv = $em->getRepository(Adminevent::class)->findMyRdvHebdo($this->getUser()->getId(), $annee, $semaine);
return $this->render('User/planning.html.twig', array(
'tab_mois_fr' => $tab_mois_fr,
'tab_jours_fr' => $tab_jours_fr,
'Rdv' => $rdv,
'annee' => $annee,
'mois' => $mois,
'semaine' => $semaine,
));
}
/**
* @Route("/messages", name="malys_user_messages")
*/
public function messagesAction(EntityManagerInterface $em)
{
//RECUPERATION DE TOUS MES MESSAGES
$mes = $em->getRepository(Messages::class)->findMyMessages($this->getUser()->getId());
return $this->render('User/messages.html.twig', array('Messages' => $mes));
}
/**
* @Route("/message_lu/{id}", name="malys_user_readmessage")
*/
public function readMessageAction($id, EntityManagerInterface $em)
{
$message = $em->getRepository(Messages::class)->find($id);
$message->setLu(1);
$message->setDatelufr(new \DateTime('now'));
$em->flush();
return $this->redirect($this->generateUrl('malys_user_messages'));
}
/**
* @Route("/rapport/{id}", name="malys_user_rapport", defaults={"id"=0})
*/
public function rapportAction($id)
{
($id == 0) ? ($id = '') : ($id);
return $this->render('User/rapport.html.twig', array('id' => $id));
}
}