<?php
namespace App\Controller\Finance;
use App\Entity\Finance\Lead;
use App\Form\Finance\LeadType;
use App\Entity\Finance\Proposal;
use App\Form\Finance\ProposalType;
use DateTime;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Entity\Kernel\ListItem;
use App\Service\Kernel\KernelService;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
/**
* @Route("/{_locale}/lead")
*/
class LeadController extends AbstractController {
/**
* @Route("/index/{state}/{open}/{my}/{user}/{website}", name="lead_index", methods={"GET"})
*/
public function index(int $state=0, int $open=2, int $my=0, int $user=0, int $website=0): Response {
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
return $this->render('finance/lead/index.html.twig', [
'state' => $state,
'open' => $open,
'my' => $my,
'user' => $user,
'website' => $website,
]);
}
/**
* @Route("/table/{state}/{open}/{my}/{user?0}/{website?0}", name="lead_table", methods={"GET"})
*/
public function table(int $state, int $open, int $my, int $user = 0, int $website=0, KernelService $kernelservice): Response {
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
$leadnewconsultant = $kernelservice->getApplicationInfoValue("Sales.LeadNewConsultant");
$leadnewproduct = $kernelservice->getApplicationInfoValue("Sales.LeadNewProduct");
// variable of button
$button = $state + 3*$open + 9*$my;
return $this->render('finance/lead/table.html.twig', [
'button' => $button, 'state' => $state, 'open' => $open, 'my' => $my, 'user' => $user,
'website' => $website,
'leadnewconsultant' => $leadnewconsultant, 'leadnewproduct' => $leadnewproduct
]);
}
/**
* @Route("/tabledetails/{state}/{open}/{my}/{user}/{website}", name="lead_tabledetails", methods={"GET","POST"})
*/
public function tabledetails(Request $request, int $state, int $open,int $my,int $user=0, int $website=0,TranslatorInterface $translator, KernelService $kernel_service) : Response {
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize - 1]).":".str_replace(
"Action",
"",
__FUNCTION__
);
$this->denyAccessUnlessGranted($attribute);
$start = $request->get('start');
$length = $request->get('length');
$em = $this->getDoctrine()->getManager();
$organization = $this->getUser()->getOrganization();
if ($organization == null) {
$organizationid = 0;
} else {
$organizationid = $organization->getId();
}
$openbasedondate = $kernel_service->getApplicationInfoValue('Kernel.OpenLeadBasedOnDate');
$filter = $request->get('search')['value'];
$order = $request->get('order');
$orderColumn = $order[0]["column"];
$orderby = $this->orderColumns($orderColumn);
$direction = strtoupper($order[0]["dir"]);
$queryString = "SELECT s FROM App\Entity\Finance\Lead s LEFT JOIN s.vacancies t WHERE ((s.state = ?1) ";
if($open != 2){
if($website == 0){
if ($openbasedondate == true)
{
$queryString = $queryString . "AND (s.created >= ?3) ";
}
else
{
$queryString = $queryString . "AND (s.open = ?3) ";
}
}
}
if($my == 1){
$queryString = $queryString . "AND (s.owner = ?4 ) ";
}
if($user != 0){
$queryString = $queryString . "AND (s.owner = ?4 ) ";
}
if($website != 0){
$queryString = $queryString . "AND (t.state = 0 ) ";
}
$queryString = $queryString ."AND ((?2 = 0) or ((s.organization = ?2) or (s.organization is null)))) ";
//$queryString = $queryString . ")";
if(!empty($filter) == true){
$queryString = $queryString ."AND ((s.subject LIKE '%".$filter."%') "
. "or (s.created LIKE '%".$filter."%')) ";
}
$queryString = $queryString ."ORDER BY ".$orderby." ".$direction;
$query = $em->createQuery($queryString);
$count = $query->setParameter(2, $organizationid)
->setParameter(1, $state);
if($open != 2){
if($website == 0){
if ($openbasedondate == true)
{
$dateInTwoWeeks = new \DateTime();
$dateInTwoWeeks->modify('-4 week');
$count->setParameter(3, $dateInTwoWeeks);
}
else
{
$count->setParameter(3, $open);
}
}
}
if($my == 1){
$count->setParameter(4, $this->getUser());
}
if($user != 0){
$count->setParameter(4, $user);
}
$total = count($count->getResult());
$query->setMaxResults($length)
->setFirstResult($start);
$leads = $query->getResult();
$twig = clone $this->get('twig');
$datawithlinks = array();
foreach($leads as $row){
$i = 0;
$newrow = array();
$url = $twig->createTemplate("{{ path('lead_show', {'id' : id,'state' : state }) }}")
->render(array("id" => $row->getId(),"state" => $row->getState()));
$newrow[$i] = $row->getId();
$i++;
$newrow[$i] = $url;
$i++;
if (false)
{
$newrow[$i] = $translator->trans($row->getType());
$i++;
}
$newrow[$i] = "".$row;
$i++;
$newrow[$i] = "".$row->getClient();
$i++;
$newrow[$i] = "".$row->getendClient();
$i++;
$newrow[$i] = "".$row->getCreated()->format('d/m/Y');
$i++;
$newrow[$i] = "".$row->getFunction();
$i++;
$newrow[$i] = "".$row->getTechnology();
$i++;
$newrow[$i] = "".$row->getOwner();
$i++;
$newrow[$i] = "".$this->giveAmountOfProposals($row);
$i++;
if($row->getOpen()){
$state = "Open";
}else{
$state = "Closed";
}
$newrow[$i] = $translator->trans($state);
$i++;
$newrow[$i] = "".$row->getChance();
$i++;
$newrow[$i] = "".$row->getOrganization();
$i++;
$datawithlinks[] = $newrow;
}
$return = array(
"draw" => intval($request->get('draw')),
"recordsTotal" => $total,
"recordsFiltered" => $total,
"aaData" => $datawithlinks
);
$return = json_encode($return);
return new Response($return,200,array('Content-Type' => 'application/json'));
}
public function orderColumns($orderColumn){
$orderby = "";
switch($orderColumn){
case 0:
$orderby = "s.id";
break;
case 2:
$orderby = "s.subject";
break;
case 3:
$orderby = "s.client";
break;
case 4:
$orderby = "s.endClient";
break;
case 5:
$orderby = "s.created";
break;
case 6:
$orderby = "s.owner";
break;
case 8:
$orderby = "s.open";
break;
case 9:
$orderby = "s.chance";
break;
case 10:
$orderby = "s.organization";
break;
}
return $orderby;
}
/**
* @Route("/{id}/newProposal", name="lead_newProposal", methods={"GET","POST"})
*/
public function newProposal(Request $request, Lead $lead, KernelService $kernelservice): Response {
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
$proposal = new Proposal();
$date = new DateTime();
$proposal->setLead($lead);
$proposal->setProposedOn($date);
$proposal->setOpen($lead->getOpen());
$proposal->setSubject($lead->getSubject());
$proposal->setType($lead->getType());
$proposal->setClient($lead->getClient());
$proposal->setEndClient($lead->getEndClient());
$proposal->setOwner($lead->getOwner());
$proposal->setOrganization($lead->getOrganization());
$simpleProposals = $kernelservice->getApplicationInfoValue("Sales.SimpleProposals");
//$proposal->setOrganization($lead->getOrganization());
$form = $this->createForm(ProposalType::class, $proposal,
[
'organization' => $this->getUser()->getOrganization(),
'action' => $this->generateUrl('proposal_new'),
'attr' => ['id' => $this->generateUrl('proposal_new')
]]);
if($simpleProposals)
{
$required = true;
$form->add('employeeselect2',ChoiceType::class, array('priority' => 3, 'label' => 'Proposal.Employee',
'required' => true,
'multiple' => false,
'mapped' => false,
'attr' => array('class' => 'selectemployee')
));
$form->add('intakePrice', NumberType::class, array('priority' => 2, 'mapped' => false, 'label' => 'IntakePrice','required' => $required));
$form->add('unitPrice', NumberType::class, array('priority' => 1, 'mapped' => false, 'label' => 'Proposal.UnitPrice','required' => $required));
}
if ($this->getUser()->getOrganization() == null) {
$form->add('organization', null, array('label' => 'Organization',
'query_builder' => function ($repository) {
return $repository->createQueryBuilder('s')->where("s.state = 0");
},
'attr' => ['class' => 'select2']));
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$proposal->setState(0);
$entityManager->persist($proposal);
$entityManager->flush();
$this->denyAccessUnlessGranted($attribute, $proposal);
return $this->redirectToRoute('proposal_index');
}
return $this->render('finance/proposal/new.html.twig', [
'proposal' => $proposal,
'form' => $form->createView(),
]);
}
/**
* @Route("/new/{type}", name="lead_new", methods={"GET","POST"})
*/
public function new(Request $request, TranslatorInterface $translator, int $type, KernelService $kernelService): Response {
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
$organization = $this->getUser()->getOrganization();
$lead = new Lead();
$lead->setOpen(true);
$lead->setChance(0);
$lead->setOwner($this->getUser());
if ($type == 0) {
$typeListItem = $kernelService->getListItem("Finance.LeadType", "LeadType.Product", $organization);
} else {
$typeListItem = $kernelService->getListItem("Finance.LeadType", "LeadType.Resource", $organization);
}
$form = $this->createForm(LeadType::class, $lead,
['organization' => $organization
, 'action' => $this->generateUrl('lead_new', array('type' => $type)),
'type' => $type,
'attr' => ['id' => $this->generateUrl('lead_new', array('type' => $type))]]);
if ($this->getUser()->getOrganization() == null) {
$form->add('organization', null, array('label' => 'Organization',
'query_builder' => function ($repository) {
return $repository->createQueryBuilder('s')->where("s.state = 0");
},
'attr' => ['class' => 'select2']));
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$chance = $lead->getChance();
$lead->setType($typeListItem);
if ($this->checkBetween0and100($chance, $translator)) {
$entityManager = $this->getDoctrine()->getManager();
$lead->setState(0);
if ($organization != null) {
$lead->setOrganization($organization);
}
$entityManager->persist($lead);
$entityManager->flush();
$this->denyAccessUnlessGranted($attribute, $lead);
return $this->redirectToRoute('lead_index');
}
}
return $this->render('finance/lead/new.html.twig', [
'lead' => $lead,
'form' => $form->createView(),
'type' => $type
]);
}
/**
* @Route("/{id}/showLead/{state}", name="show_lead", methods={"GET"})
*/
public function showLead(Lead $lead, int $state, KernelService $kernelservice): Response {
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $lead);
$leadnewconsultant = $kernelservice->getApplicationInfoValue("Sales.LeadNewConsultant");
$leadnewproduct = $kernelservice->getApplicationInfoValue("Sales.LeadNewProduct");
return $this->render('finance/lead/lead_show.html.twig', [
'lead' => $lead, 'state' => $state
, 'leadnewconsultant' => $leadnewconsultant, 'leadnewproduct' => $leadnewproduct
]);
}
/**
* @Route("/{id}/show/{state?0}", name="lead_show", methods={"GET"})
*/
public function show(Lead $lead, int $state = 0, KernelService $kernelservice): Response {
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $lead);
$proposals = $this->getDoctrine()
->getRepository(Proposal::class)
->findBy(array('state' => $state, 'lead' => $lead));
$leadnewconsultant = $kernelservice->getApplicationInfoValue("Sales.LeadNewConsultant");
$leadnewproduct = $kernelservice->getApplicationInfoValue("Sales.LeadNewProduct");
return $this->render('finance/lead/show.html.twig', [
'lead' => $lead, 'proposals' => $proposals, 'state' => $state
, 'leadnewconsultant' => $leadnewconsultant, 'leadnewproduct' => $leadnewproduct
]);
}
/**
* @Route("/{id}/audit", name="lead_showaudit", methods={"GET"})
*/
public function showaudit(Lead $lead): Response {
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $lead);
return $this->render('finance/lead/showaudit.html.twig', [
'lead' => $lead,
]);
}
/**
* @Route("/{id}/restore", name="lead_restore", methods={"GET"})
*/
public function restore(Lead $lead): Response {
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $lead);
$entityManager = $this->getDoctrine()->getManager();
$lead->setState(0);
$entityManager->persist($lead);
$entityManager->flush();
return $this->redirectToRoute('show_lead', array('id' => $lead->getId(), 'state' => 0));
}
/**
* @Route("/{id}/archived", name="lead_archived", methods={"GET"})
*/
public function archive(Lead $lead): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize - 1]).":".str_replace(
"Action",
"",
__FUNCTION__
);
$this->denyAccessUnlessGranted($attribute, $lead);
$entityManager = $this->getDoctrine()->getManager();
$lead->setState(1);
foreach($lead->getVacancies() as $vancancy)
{
$vancancy->setState(1);
$entityManager->persist($vancancy);
}
$entityManager->persist($lead);
$entityManager->flush();
return $this->redirectToRoute('show_lead', array('id' => $lead->getId(), 'state' => 1));
}
/**
* @Route("/{id}/delete", name="lead_delete", methods={"GET"})
*/
public function delete(Lead $lead): Response {
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $lead);
$entityManager = $this->getDoctrine()->getManager();
$lead->setState(2);
$entityManager->persist($lead);
$entityManager->flush();
return $this->redirectToRoute('lead_index');
}
/**
* @Route("/{id}/edit", name="lead_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Lead $lead): Response {
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $lead);
$type = 0;
if ($lead->getType()->getName() == "LeadType.Resource")
{
$type = 1;
}
$form = $this->createForm(LeadType::class, $lead,
['organization' => $lead->getOrganization(), 'type' => $type,
'action' => $this->generateUrl('lead_edit', array('id' => $lead->getId())), 'attr' => ['id' => $this->generateUrl('lead_edit', array('id' => $lead->getId()))]]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('lead_show', array('id' => $lead->getId()));
}
return $this->render('finance/lead/edit.html.twig', [
'lead' => $lead,
'form' => $form->createView(),
]);
}
public function giveAmountOfProposals(Lead $lead) {
$amountOfProposals = $lead->giveAmountOfProposals();
return $amountOfProposals;
}
public function checkBetween0and100(float $chance, TranslatorInterface $translator) {
if ($chance > 1 || $chance < 0) {
$this->get('session')->getFlashBag()->add('error', $translator->trans('Lead.valuemustbebetween0and100'));
return false;
} else {
return true;
}
}
}