<?php
namespace App\Controller;
use App\Entity\Comercial;
use App\Entity\User;
use App\Form\ComercialType;
use App\Repository\ComercialRepository;
use App\Repository\ContratoRepository;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class ComercialController extends AbstractController
{
/*
* @Route("/gestionarContratos", name="comercial_contratos_index")
*
public function contratosIndex(ContratoRepository $contratoRepository): Response
{
if( ($this->getUser()??new User)->getComercial()==null ){
$this->denyAccessUnlessGranted("ROLE_COMERCIAL");
}
$contratos=$contratoRepository->findBy([ 'seller'=>$this->getUser()->getComercial()??'' ]);
foreach ($contratos as $contrato){
if( $contrato->getUltimaLectura()==null ){
$ultimaLectura=$contratoRepository->findUltimaLectura($contrato);
if( !empty($ultimaLectura) ){
$contrato->setUltimaLectura($ultimaLectura['fecha']??null);
}
}
}
$this->getDoctrine()->getManager()->flush();
return $this->render('contrato/index.html.twig', [
'contratos' => $contratos,
]);
}*/
/**
* @Route("/comercial/contratos", name="contratos_comercial")
* @Route("/gestionarContratos", name="comercial_contratos_index")
*/
public function contratos(Request $request, ContratoRepository $contratoRepository,
ComercialRepository $comercialRepository, EntityManagerInterface $entityManager): Response
{
if( ($this->getUser()??new User)->getComercial()==null ){
$this->denyAccessUnlessGranted("ROLE_COMERCIAL");
}
if( !$this->isGranted("ROLE_ADMIN") ){
$comercial=$this->getUser()->getComercial();
$contratos=$contratoRepository->findBy([ 'seller'=>$comercial??'' ]);
$contratos=array_unique( array_merge($contratos,$contratoRepository->findBySubseller($comercial??'')) );
$comercial=$comercial->__toString();
}
elseif( $request->isMethod('get')){
$comercial=$request->get('comercial');
if($comercial===''){
$comercial=null;
}
if( is_numeric($comercial) ){
$comercial=$comercialRepository->find($comercial);
$contratos=$contratoRepository->findBy([ 'oculto'=>false, 'seller'=>$comercial ]);
$contratos=array_unique( array_merge($contratos,$contratoRepository->findBySubseller($comercial,false)) );
}else{
$contratos=$contratoRepository->findBy([ 'oculto'=>false, 'comercial'=>$comercial ]);
}
}
else{
$contratos=$contratoRepository->findBy([ 'oculto'=>false ]);
}
foreach ($contratos as $contrato){
if( $contrato->getUltimaLectura()==null ){
$ultimaLectura=$contratoRepository->findUltimaLectura($contrato);
if( !empty($ultimaLectura) ){
$contrato->setUltimaLectura($ultimaLectura['fecha']??null);
}
}
}
$entityManager->flush();
return $this->render('comercial/contratos.html.twig', [
'comercial'=>$comercial??'Sin comercial',
'contratos' => $contratos,
]);
}
/**
* @Route("/puntos", name="comercial_puntos")
*/
public function puntos(ComercialRepository $comercialRepository, ContratoRepository $contratoRepository): Response
{
$this->denyAccessUnlessGranted("ROLE_ADMIN");
$comerciales=$comercialRepository->findAll();
$contratos=$contratoRepository->findByMonth(date_create());
return $this->render('comercial/index.html.twig', [
'comerciales' => $comerciales,
'contratos' => $contratos,
]);
}
/**
* @Route("/admin/comerciales", name="comercial_lista_admin")
*/
public function listaAdmin(ComercialRepository $comercialRepository): Response
{
$this->denyAccessUnlessGranted("ROLE_ADMIN");
return $this->render('comercial/listaAdmin.html.twig', [
'comerciales' => $comercialRepository->findAll()
]);
}
/**
* @Route("/comercial/lista", name="comercial_lista")
*/
public function lista(ContratoRepository $contratoRepository): Response
{
$this->denyAccessUnlessGranted("ROLE_ADMIN");
$comercialesOld = $contratoRepository->createQueryBuilder('c')
->select('DISTINCT c.comercial')
->where('c.oculto!=1')
->getQuery()
->getResult();
foreach ($comercialesOld as $comercial){
$comerciales[]=$comercial['comercial'];
}
foreach ($comerciales??[] as $comercial){
$contratos[$comercial]=$contratoRepository->findBy(['comercial'=>$comercial]);
}
return $this->render('comercial/lista.html.twig', [
'comerciales' => $comerciales??[],
'contratos' => $contratos??[],
]);
}
/**
* @Route("/comercial/registrar", name="comercial_new", methods={"GET","POST"})
*/
public function new(Request $request, UserRepository $userRepository, UserPasswordHasherInterface $passwordEncoder,
EntityManagerInterface $entityManager): Response
{
$this->denyAccessUnlessGranted('ROLE_ALTACOMERCIALES');
$comercial = new Comercial();
$form = $this->createForm(ComercialType::class, $comercial);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$comercial
->setCodigoVendedor( strtoupper($comercial->getCodigoVendedor()) )
->setNombre( strtoupper($comercial->getNombre()) )
->setApellido1( strtoupper($comercial->getApellido1()) )
->setApellido2( strtoupper($comercial->getApellido2()) )
->setNif( strtoupper($comercial->getNif()) )
->setEmail( strtolower($comercial->getEmail()) );
$user=$userRepository->findOneBy(['username'=>$comercial->getNif()]);
if($user==null){
$user = new User();
$user->setUsername( strtoupper($comercial->getNif()) );
$user->setPassword(
$passwordEncoder->hashPassword(
$user,
strtoupper($comercial->getNif())
)
);
$user->setRoles(["ROLE_USER","ROLE_COMERCIAL"]);
$entityManager->persist($user);
}
$comercial->setUser($user);
$entityManager->persist($comercial);
$entityManager->flush();
return $this->redirectToRoute('comercial_lista_admin');
}
return $this->render('comercial/new.html.twig', [
'comercial' => $comercial,
'form' => $form->createView(),
]);
}
/**
* @Route("/comercial/{id}/editar", name="comercial_edit", methods={"GET","POST"})
*/
public function edit(Comercial $comercial, Request $request, UserRepository $userRepository,
UserPasswordHasherInterface $passwordEncoder, EntityManagerInterface $entityManager): Response
{
$this->denyAccessUnlessGranted('ROLE_SUPERADMIN');
$form = $this->createForm(ComercialType::class, $comercial);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$comercial
->setCodigoVendedor( strtoupper($comercial->getCodigoVendedor()) )
->setNombre( strtoupper($comercial->getNombre()) )
->setApellido1( strtoupper($comercial->getApellido1()) )
->setApellido2( strtoupper($comercial->getApellido2()) )
->setNif( strtoupper($comercial->getNif()) )
->setEmail( strtolower($comercial->getEmail()) );
$user=$userRepository->findOneBy(['username'=>$comercial->getNif()]);
if($user==null){
$user = new User();
$user->setUsername( strtoupper($comercial->getNif()) );
$user->setPassword(
$passwordEncoder->hashPassword(
$user,
strtoupper($comercial->getNif())
)
);
$user->setRoles(["ROLE_USER","ROLE_COMERCIAL"]);
$entityManager->persist($user);
}
$comercial->setUser($user);
$entityManager->persist($comercial);
$entityManager->flush();
return $this->redirectToRoute('comercial_lista_admin');
}
return $this->render('comercial/new.html.twig', [
'comercial' => $comercial,
'form' => $form->createView(),
]);
}
}