<?php
namespace App\Controller\CRM;
use App\Entity\CRM\Client;
use App\Form\CRM\ClientType;
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 App\Entity\Kernel\ConfigurableList;
use App\Entity\Kernel\Setting;
use App\Service\Kernel\DMSService;
use App\Service\CRM\ClientService;
/**
* @Route("/{_locale}/client")
*/
class ClientController extends AbstractController
{
/**
* @Route("/", name="client_index", methods={"GET"})
*/
public function index(): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
/*$clients = $this->getDoctrine()
->getRepository(Client::class)
->findBy(array('state' => 0));*/
/*$em = $this->getDoctrine()->getManager();
if ($this->getUser()->getOrganization() == null)
{
$query = $em->createQuery("SELECT s FROM App\Entity\CRM\Client s WHERE (s.state = 0)");
$clients = $query->getResult();
}
else
{
$query = $em->createQuery("SELECT s FROM App\Entity\CRM\Client s WHERE ((s.state = 0) AND ((s.organization = ?2) or (s.organization is null)))");
$query = $query->setParameter(2, $this->getUser()->getOrganization()->getId());
$clients = $query->getResult();
}*/
return $this->render('crm/client/index.html.twig', [
]);
}
/**
* @Route("/table/{state}", name="client_table", methods={"GET"})
*/
public function table(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);
$newindividuals = $kernelservice->getApplicationInfoValue("CRM.NewIndividuals");
//State 3 = my clients
$em = $this->getDoctrine()->getManager();
if ($this->getUser()->getOrganization() == null)
{
if ($state != 3)
{
$query = $em->createQuery("SELECT s FROM App\Entity\CRM\Client s WHERE (s.state = ?1)");
$query = $query->setParameter(1, $state);
$clients = $query->getResult();
}
else
{
$query = $em->createQuery("SELECT s FROM App\Entity\CRM\Client s WHERE (s.state = 0) AND s.owner = ?1");
$query = $query->setParameter(1, $this->getUser()->getId());
$clients = $query->getResult();
}
}
else
{
if ($state != 3)
{
$query = $em->createQuery("SELECT s FROM App\Entity\CRM\Client s WHERE ((s.state = ?1) AND ((s.organization = ?2) or (s.organization is null)))");
$query = $query->setParameter(1, $state);
$query = $query->setParameter(2, $this->getUser()->getOrganization()->getId());
$clients = $query->getResult();
}
else
{
$query = $em->createQuery("SELECT s FROM App\Entity\CRM\Client s WHERE ((s.state = 0) AND ((s.organization = ?2) or (s.organization is null))) AND s.owner = ?1");
$query = $query->setParameter(2, $this->getUser()->getOrganization()->getId());
$query = $query->setParameter(1, $this->getUser()->getId());
$clients = $query->getResult();
}
}
return $this->render('crm/client/table.html.twig', [
'state' => $state,
'clients' => $clients,
'newindividuals' => $newindividuals
]);
}
/**
* Lists all building entities.
*
* @Route("/select", name="client_select", methods={"GET"})
*/
public function select(Request $request, TranslatorInterface $translator): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
$em = $this->getDoctrine()->getManager();
$id = $request->get('id');
$organization = $this->getUser()->getOrganization();
$organization_id = $request->get('organization');
if (empty($organization_id) or ($organization_id == 0))
{
//$organisation = null;
}
else
{
$organization = $em->getRepository('App\Entity\Kernel\Organization')->findOneById($organization_id);
}
if ($organization == null) {
$query = $em->createQuery("SELECT s FROM App\Entity\CRM\Client s WHERE (s.state = 0) AND ((s.company LIKE ?1 OR s.firstname LIKE ?1) OR s.lastname LIKE ?1)");
$query = $query->setParameter(1, '%'.$id.'%');
$clients = $query->getResult();
} else {
$query = $em->createQuery("SELECT s FROM App\Entity\CRM\Client s WHERE (((s.state = 0) AND ((s.company LIKE ?1 OR s.firstname LIKE ?1) OR s.lastname LIKE ?1)) AND ((s.organization = ?2) or (s.organization is null)))");
$query = $query->setParameter(1, '%'.$id.'%');
$query = $query->setParameter(2, $organization->getId());
$clients = $query->getResult();
}
$clientsfound = array();
foreach ($clients as $client) {
$clientsfound[] = array("id" => "SU" . $client->getId(), "name" => "" . $client);
}
$newcompany = $translator->trans("CRM.Newcompany");
$newclient = $translator->trans("CRM.NewindividualLastname");
if (empty($id) == false) {
$clientsfound[] = array("id" => "NC" . $id, "name" => $id . " (" . $newcompany . ")");
$clientsfound[] = array("id" => "NI" . $id, "name" => $id . " (" . $newclient . ")");
}
//CRM.Newindividual
$return = array("items" => $clientsfound);
$return = json_encode($return);
return new Response($return, 200, array('Content-Type' => 'application/json'));
}
/**
* @Route("/new/{type?0}", name="client_new", methods={"GET","POST"})
*/
public function new(Request $request, int $type = 0, TranslatorInterface $translator, DMSService $dmsservice, ClientService $clientService): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
$entityManager = $this->getDoctrine()->getManager();
$postcodes = array();
$municipalities = array();
$organization = $this->getUser()->getOrganization();
if ($organization != null)
{
$organization = $organization->getId();
}
else
{
$organization = -1;
}
if ($request->isMethod('POST'))
{
$requestdata = $request->request->all("client");
$postcodeid = $requestdata['postcode'];
$postcodeidvalue = $requestdata['postcode'];
if (empty($postcodeid) == false)
{
$type2 = substr($postcodeid, 0, 8);
$postcodeidsub = substr($postcodeid, 8);
if ($type2 == "INTERNAL")
{
$item = $entityManager->getRepository(ListItem::class)->findOneById($postcodeidsub);
$postcodeidvalue = $item->getString1();
}
}
$postcodes[$postcodeidvalue] = $postcodeid;
}
if ($request->isMethod('POST'))
{
$requestdata = $request->request->all("client");
$municipalityid = $requestdata['municipality'];
$municipalityidvalue = $requestdata['municipality'];
if (empty($municipalityid) == false)
{
$type2 = substr($municipalityid, 0, 8);
$municipalityidsub = substr($municipalityid, 8);
if ($type2 == "INTERNAL")
{
$item = $entityManager->getRepository(ListItem::class)->findOneById($municipalityidsub);
$municipalityidvalue = $item->getName();
}
}
$municipalities[$municipalityidvalue] = $municipalityid;
}
$client = new Client();
$client->setContactByEmail(true);
$form = $this->createForm(ClientType::class, $client,
['postcode' => $postcodes, 'municipality' => $municipalities, 'organization' => $organization,
'type' => $type,
'action' => $this->generateUrl('client_new', array('type' => $type)),
'attr' => ['id' => $this->generateUrl('client_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()) {
$organization = $client->getOrganization();
if ($this->getUser()->getOrganization() != null) {
$organization = $this->getUser()->getOrganization();
}
$taxnumberok = true;
if (empty($client->getTax()) == false) {
$clients = $entityManager->getRepository(Client::class)->findBy(array('organization' => $client->getOrganization(), 'state' => 0, 'tax' => $client->getTax()));
foreach ($clients as $checkclient) {
if ($checkclient->getId() != $client->getId()) {
$taxnumberok = false;
}
}
}
if ($taxnumberok == false) {
$this->get('session')->getFlashBag()->add(
'error',
$translator->trans('Client.Taxnumberalreadyinuse')
);
} else {
$stateRegistrationNumberok = true;
if (empty($client->getStateRegistrationNumber()) == false) {
$clients = $entityManager->getRepository(Client::class)->findBy(array('organization' => $client->getOrganization(), 'state' => 0, 'stateRegistrationNumber' => $client->getStateRegistrationNumber()));
foreach ($clients as $checkclient) {
if ($checkclient->getId() != $client->getId()) {
$stateRegistrationNumberok = false;
}
}
}
if ($stateRegistrationNumberok == false) {
$this->get('session')->getFlashBag()->add(
'error',
$translator->trans('Client.Stateregistrationnumberalreadyinuse')
);
} else {
$postcode = null;
$municipality = null;
$requestdata = $request->request->all("client");
$postcodeid = $requestdata['postcode'];
if (empty($postcodeid) == false) {
$type2 = substr($postcodeid, 0, 8);
$postcodeid = substr($postcodeid, 8);
if ($type2 == "INTERNAL") {
$item = $entityManager->getRepository(ListItem::class)->findOneById($postcodeid);
$postcode = $item->getString1();
} else {
$postcode = $requestdata['postcode'];
}
}
$municipalityid = $requestdata['municipality'];
if (empty($municipalityid) == false) {
$type2 = substr($municipalityid, 0, 8);
$municipalityid = substr($municipalityid, 8);
if ($type2 == "INTERNAL") {
$item = $entityManager->getRepository(ListItem::class)->findOneById($municipalityid);
$municipality = $item->getName();
} else {
$municipality = $requestdata['municipality'];
}
}
$weblist = $entityManager->getRepository(ConfigurableList::class)->findOneByIdentifier("CRM.ClientType");
if ($type == 0)
{
$clienttype = $entityManager->getRepository(ListItem::class)->findOneBy(array('configurablelist' => $weblist->getId(), 'name' => 'CRM.Company'));
}
else
{
$clienttype = $entityManager->getRepository(ListItem::class)->findOneBy(array('configurablelist' => $weblist->getId(), 'name' => 'CRM.Individual'));
}
$input = $request->files->get('client');
$uploadfile = null;
if ($input != null) {
$uploadfile = $input['image'];
}
$client = $clientService->CreateClient($clienttype, $client->getCompany(), $client->getTax(), $client->getLastname(), $client->getFirstname(),
$client->getStateRegistrationNumber(), $client->getPhone(), $client->getPhone2(), $client->getEmail(),
$client->getLanguage(), $client->getStreet(), $client->getHousenumber(), $postcode, $municipality, $client->getCountry(),
$client->getIban(), $client->getBic(), $client->getOwner(), $client->getClienttags(), $client->getTaxrate(),
$client->getAdditionalInvoiceText(), $client->getAcronym(), $client->getExternalid(), $client->getDescription(), $client->getContactByEmail(),
$uploadfile, $organization);
/*$client->setClienttype($clienttype);
$entityManager->persist($client);
$entityManager->flush();
$input = $request->files->get('client');
$uploadfile = null;
if ($input != null) {
$uploadfile = $input['image'];
}
$applicationfolder = $dmsservice->createFolderIfNeeded("System");
$clientfolder = $dmsservice->createFolderIfNeeded("Client", $applicationfolder);
$folder = $dmsservice->createFolderIfNeeded("".$client->getId(), $clientfolder);
if ($uploadfile == null) {
$file = $dmsservice->copyfile($entityManager->getRepository(Setting::class)->findOneByIdentifier("Kernel.Newclientimage")->getFileValue(), $clientfolder);
} else {
$file = $dmsservice->uploadfile($uploadfile, $clientfolder);
}
$newname = "" . $client->getId();
$newname = $newname . ".png";
$file->setName($newname);
$entityManager->persist($file);
$entityManager->flush();
$client->setImage($file);
$entityManager->persist($client);
$entityManager->flush();*/
$this->denyAccessUnlessGranted($attribute, $client);
return $this->redirectToRoute('client_show', array('id' => $client->getId()));
}
}
}
return $this->render('crm/client/new.html.twig', [
'client' => $client,
'type' => $type,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="client_show", methods={"GET"})
*/
public function show(Client $client, DMSService $dmsservice): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $client);
$entityManager = $this->getDoctrine()->getManager();
$applicationfolder = $dmsservice->createFolderIfNeeded("System");
$clientfolder = $dmsservice->createFolderIfNeeded("Client", $applicationfolder);
$folder = $dmsservice->createFolderIfNeeded("".$client->getId(), $clientfolder);
if ($client->getImage() == null)
{
$file = $dmsservice->copyfile($entityManager->getRepository(Setting::class)->findOneByIdentifier("Kernel.Newclientimage")->getFileValue(), $clientfolder);
$newname = "" . $client->getId();
$newname = $newname . ".png";
$file->setName($newname);
$entityManager->persist($file);
$entityManager->flush();
$client->setImage($file);
$entityManager->persist($client);
$entityManager->flush();
}
return $this->render('crm/client/show.html.twig', [
'client' => $client,
'folder' => $folder,
]);
}
/**
* @Route("/{id}/audit", name="client_showaudit", methods={"GET"})
*/
public function showaudit(Client $client): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $client);
return $this->render('crm/client/showaudit.html.twig', [
'client' => $client,
]);
}
/**
* @Route("/{id}/archive", name="client_archive", methods={"GET"})
*/
public function archive(Client $client): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $client);
$entityManager = $this->getDoctrine()->getManager();
$client->setState(1);
$entityManager->persist($client);
$entityManager->flush();
return $this->redirectToRoute('client_show', array('id' => $client->getId()));
}
/**
* @Route("/{id}/restore", name="client_restore", methods={"GET"})
*/
public function restore(Client $client): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $client);
$entityManager = $this->getDoctrine()->getManager();
$client->setState(0);
$entityManager->persist($client);
$entityManager->flush();
return $this->redirectToRoute('client_show', array('id' => $client->getId()));
}
/**
* @Route("/{id}/delete", name="client_logicaldelete", methods={"GET"})
*/
public function logicaldelete(Client $client): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $client);
$entityManager = $this->getDoctrine()->getManager();
$client->setState(2);
$entityManager->persist($client);
$entityManager->flush();
return $this->redirectToRoute('client_index');
}
/**
* @Route("/{id}/edit", name="client_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Client $client, TranslatorInterface $translator, DMSService $dmsservice): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $client);
$entityManager = $this->getDoctrine()->getManager();
$postcodes = array();
$municipalities = array();
if (empty($client->getPostcode())==false)
{
$postcodes[$client->getPostcode()] = $client->getPostcode();
}
if ($request->isMethod('POST'))
{
$requestdata = $request->request->all("client");
$postcodeid = $requestdata['postcode'];
$postcodeidvalue = $requestdata['postcode'];
if (empty($postcodeid) == false)
{
$type = substr($postcodeid, 0, 8);
$postcodeidsub = substr($postcodeid, 8);
if ($type == "INTERNAL")
{
$item = $entityManager->getRepository(ListItem::class)->findOneById($postcodeidsub);
$postcodeidvalue = $item->getString1();
}
}
$postcodes[$postcodeidvalue] = $postcodeid;
}
if (empty($client->getMunicipality())==false)
{
$municipalities[$client->getMunicipality()] = $client->getMunicipality();
}
if ($request->isMethod('POST'))
{
$requestdata = $request->request->all("client");
$municipalityid = $requestdata['municipality'];
$municipalityidvalue = $requestdata['municipality'];
if (empty($municipalityid) == false)
{
$type = substr($municipalityid, 0, 8);
$municipalityidsub = substr($municipalityid, 8);
if ($type == "INTERNAL")
{
$item = $entityManager->getRepository(ListItem::class)->findOneById($municipalityidsub);
$municipalityidvalue = $item->getName();
}
}
$municipalities[$municipalityidvalue] = $municipalityid;
}
$type=0;
if($client->getClientType() != null)
{
if ($client->getClientType()->getName() == "CRM.Individual")
{
$type = 1;
}
}
$form = $this->createForm(ClientType::class, $client,
['postcode' => $postcodes, 'municipality' => $municipalities, 'organization' => $client->getOrganization(),
'type' => $type, 'action' => $this->generateUrl('client_edit', array('id' => $client->getId())), 'attr' => ['id' => $this->generateUrl('client_edit', array('id' => $client->getId()))]]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$taxnumberok = true;
if (empty($client->getTax()) == false) {
$clients = $entityManager->getRepository(Client::class)->findBy(array('organization' => $client->getOrganization(), 'state' => 0, 'tax' => $client->getTax()));
foreach ($clients as $checkclient) {
if ($checkclient->getId() != $client->getId()) {
$taxnumberok = false;
}
}
}
if ($taxnumberok == false) {
$this->get('session')->getFlashBag()->add(
'error',
$translator->trans('Client.Taxnumberalreadyinuse')
);
} else {
$stateRegistrationNumberok = true;
if (empty($client->getStateRegistrationNumber()) == false) {
$clients = $entityManager->getRepository(Client::class)->findBy(array('organization' => $client->getOrganization(), 'state' => 0, 'stateRegistrationNumber' => $client->getStateRegistrationNumber()));
foreach ($clients as $checkclient) {
if ($checkclient->getId() != $client->getId()) {
$stateRegistrationNumberok = false;
}
}
}
if ($stateRegistrationNumberok == false) {
$this->get('session')->getFlashBag()->add(
'error',
$translator->trans('Client.Stateregistrationnumberalreadyinuse')
);
} else {
$requestdata = $request->request->all("client");
$postcodeid = $requestdata['postcode'];
if (empty($postcodeid) == false) {
$type = substr($postcodeid, 0, 8);
$postcodeid = substr($postcodeid, 8);
if ($type == "INTERNAL") {
$item = $entityManager->getRepository(ListItem::class)->findOneById($postcodeid);
$client->setPostcode($item->getString1());
} else {
$client->setPostcode($requestdata['postcode']);
}
}
$municipalityid = $requestdata['municipality'];
if (empty($municipalityid) == false) {
$type = substr($municipalityid, 0, 8);
$municipalityid = substr($municipalityid, 8);
if ($type == "INTERNAL") {
$item = $entityManager->getRepository(ListItem::class)->findOneById($municipalityid);
$client->setMunicipality($item->getName());
} else {
$client->setMunicipality($requestdata['municipality']);
}
}
$entityManager->persist($client);
$entityManager->flush();
$input = $request->files->get('client');
$uploadfile = null;
if ($input != null) {
$uploadfile = $input['image'];
if ($uploadfile != null) {
$clientimage = $client->getImage();
$dmsservice->addVersion($clientimage, $uploadfile);
}
}
$this->denyAccessUnlessGranted($attribute, $client);
return $this->redirectToRoute('client_show', array('id' => $client->getId()));
}
}
return $this->redirectToRoute('client_show', array('id' => $client->getId()));
}
return $this->render('crm/client/edit.html.twig', [
'client' => $client,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="client_delete", methods={"DELETE"})
*/
public function delete(Request $request, Client $client): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $client);
if ($this->isCsrfTokenValid('delete'.$client->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($client);
$entityManager->flush();
}
return $this->redirectToRoute('client_index');
}
}