<?php
namespace App\Controller\Widget;
use App\Entity\Articles\FamilleCompta;
use App\Entity\FO\Societe;
use App\Entity\GestionComerciale\Commande;
use App\Entity\Localisation\Zone;
use App\Entity\MarketPlace\CompteMarketPlace;
use App\Form\Clients\ClientType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class WidgetCaController extends AbstractController
{
/**
* @Route("", name="")
*/
public function widgetCaAction(Request $request, $parametres = [], EntityManagerInterface $em)
{
$repo_commande = $em->getRepository(Commande::class);
$repoCompteMarketPlace = $em->getRepository(CompteMarketPlace::class);
$compteMarketPlace = $repoCompteMarketPlace->findBy(["statut" => 1], ["libelle" => "asc"]);
if (empty($parametres["date_debut_ca"]) && empty($parametres["date_fin_ca"])) {
$dateDebut = new \DateTime();
$dateDebut->setTime(00, 00, 00);
$parametres["date_debut_ca"] = $dateDebut->format("d/m/Y");
$dateFin = new \DateTime();
$dateFin->setTime(23, 59, 59);
$parametres["date_fin_ca"] = $dateFin->format("d/m/Y");
}
/*
$fin = new \Datetime();
$debut = new \Datetime($fin->format("Y")."-".$fin->format("m")."-01");
$caMoisEnCours = $repo_commande->getCa($debut,$fin);
$caComptoirMoisEnCours = $repo_commande->getCaComptoir($debut,$fin);
$caOrMoisEnCours = $repo_commande->getCaOr($debut,$fin);
//\Doctrine\Common\Util\Debug::dump($caMoisEnCours);
$fin = new \Datetime();
$fin->modify('-1 year');
$debut = new \Datetime($fin->format("Y")."-".$fin->format("m")."-01");
$caMoisEnCoursN1 = $repo_commande->getCa($debut,$fin);
$fin = new \Datetime();
$debut = new \Datetime($fin->format("Y")."-01-01");
$caAnneeEnCours = $repo_commande->getCa($debut,$fin);
$fin = new \Datetime();
$fin->modify('-1 year');
$debut = new \Datetime($fin->format("Y")."-01-01");
$caAnneeEnCoursN1 = $repo_commande->getCa($debut,$fin);
$fin = new \Datetime();
$debut = new \Datetime();
$debut->modify('-30 days');
$caMoisGlissant = $repo_commande->getCa($debut,$fin);
$fin = new \Datetime();
$debut = new \Datetime();
$debut->modify('-365 days');
$caAnneeGlissant = $repo_commande->getCa($debut,$fin);
*/
return $this->render('Widget/Widget/ca.html.twig', [
"compteMarketPlace" => $compteMarketPlace,
"parametres" => $parametres,
]);
}
/**
* @Route("", name="")
*/
public function widgetCaPaysFamilleComptaAction(Request $request, $parametres = [], EntityManagerInterface $em)
{
set_time_limit(300);
$repo_commande = $em->getRepository(Commande::class);
$repo_famille_compta = $em->getRepository(FamilleCompta::class);
$familleComptaPardefaut = $repo_famille_compta->find(1);
$societe = $em->getRepository(Societe::class)->find(1);
$paysFrance = $em->getRepository(Zone::class)->find(370095);
if (empty($parametres["date_debut_ca_pfc"]) && empty($parametres["date_fin_ca_pfc"])) {
$dateDebut = new \DateTime();
//$dateDebut->modify('- 1 month');
$dateDebut->modify('- 1 days');
$dateDebut->setTime(00, 00, 00);
$parametres["date_debut_ca_pfc"] = $dateDebut->format("d/m/Y");
$dateFin = new \DateTime();
$dateFin->setTime(23, 59, 59);
$parametres["date_fin_ca_pfc"] = $dateFin->format("d/m/Y");
}
$caPaysFamilleCompta = [];
$pays = [];
$famillesCompta = $repo_famille_compta->findAll();
$facturesEtAvoirs = $em->getRepository(Commande::class)->getFacturesEtAvoirs($parametres);
foreach ($facturesEtAvoirs as $facTmp) {
$paysTmp = '';
if (is_object($facTmp->getPaysLivraison())) {
$paysTmp = $facTmp->getPaysLivraison();
}
if ( ! is_object($paysTmp) && is_object($facTmp->getClient()->getPays())) {
$paysTmp = $facTmp->getClient()->getPays();
}
if ( ! is_object($paysTmp)) {
$paysTmp = $paysFrance;
}
if (is_object($paysTmp)) {
$keyPays = $paysTmp->getId();
$pays[$keyPays] = $paysTmp->getTitre();
//if($paysTmp->getTitre() == 'Luxembourg') dump('Luxembourg : '.$facTmp->getId());
} else {
$keyPays = 0;
$pays[$keyPays] = 'Non défini';
//dump($facTmp->getId());
}
//frais de port
$familleCompta = $societe->getFamilleComptaFraisPort();
$keyFamilleCompta = $familleCompta->getId();
if ( ! array_key_exists($keyPays, $caPaysFamilleCompta) || ! array_key_exists($keyFamilleCompta, $caPaysFamilleCompta[$keyPays])) {
$caPaysFamilleCompta[$keyPays][$keyFamilleCompta] = $facTmp->getTotalFraisPort();
} else {
$caPaysFamilleCompta[$keyPays][$keyFamilleCompta] += $facTmp->getTotalFraisPort();
}
$caPaysFamilleCompta[$keyPays][$keyFamilleCompta] += $facTmp->getFraisPortSupplementaire();
$acs = [];
$bls = $repo_commande->findByFacture($facTmp);
if (count($bls)) {
foreach ($bls as $bl) {
foreach ($bl->getArticleCommande() as $ac) {
$acs[] = $ac;
}
}
} else {
$acs = $facTmp->getArticleCommande();
}
foreach ($acs as $ac) {
$familleCompta = $familleComptaPardefaut;
if (is_object($ac->getArticle()->getFamilleCompta())) {
$familleCompta = $ac->getArticle()->getFamilleCompta();
}
if (is_object($familleCompta)) {
$keyFamilleCompta = $familleCompta->getId();
} else {
$keyFamilleCompta = 0;
}
if ( ! array_key_exists($keyPays, $caPaysFamilleCompta) || ! array_key_exists($keyFamilleCompta, $caPaysFamilleCompta[$keyPays])) {
$caPaysFamilleCompta[$keyPays][$keyFamilleCompta] = $ac->getTotalHt();
} else {
$caPaysFamilleCompta[$keyPays][$keyFamilleCompta] += $ac->getTotalHt();
}
}
}
//dump($caPaysFamilleCompta);
return $this->render('Widget/Widget/ca-pays-famille-compta.html.twig', [
"caPaysFamilleCompta" => $caPaysFamilleCompta,
'famillesCompta' => $famillesCompta,
'pays' => $pays,
"parametres" => $parametres,
]);
}
}