<?php
namespace App\Controller\Finance;
use App\Entity\Finance\Contract;
use App\Entity\Finance\ContractElement;
use App\Entity\HR\Employee;
use App\Entity\Kernel\User;
use App\Form\Finance\ContractType;
use App\Form\Finance\ContractAdminType;
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\Service\Kernel\KernelService;
use App\Service\Finance\ContractService;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
/**
* @Route("/{_locale}/contract")
*/
class ContractController extends AbstractController
{
/**
* @Route("/ownerpercentage", name="contract_ownerpercentage", methods={"GET"})
*/
public function ownerpercentage(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();
$marginproduct = $kernelservice->getApplicationInfoValue("Finance.DefaultProductForResourceContracts", $organization);
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery("SELECT s.id as id FROM App\Entity\Finance\Contract t JOIN t.owner s WHERE (t.endDate >= ?1) AND (t.startDate <= ?2) GROUP BY s");
$param1 = date("Y-m-d",strtotime("first day of last month"));
$param2 = date("Y-m-d",strtotime("last day of last month"));
$query = $query->setParameter(1, $param1);
$query = $query->setParameter(2, $param2);
$recruiters = $query->getResult();
$result = array();
foreach ($recruiters as $recruiter) {
$query = $em->createQuery("SELECT t FROM App\Entity\Finance\Contract t JOIN t.owner s WHERE ((t.endDate >= ?1) AND (t.startDate <= ?2)) AND (s.id = ?3)");
$param1 = date("Y-m-d", strtotime("first day of last month"));
$param2 = date("Y-m-d", strtotime("last day of last month"));
$param3 = $recruiter;
$query = $query->setParameter(1, $param1);
$query = $query->setParameter(2, $param2);
$query = $query->setParameter(3, $param3);
$entities = $query->getResult();
foreach ($entities as $contract) {
$query = $em->createQuery("SELECT t FROM App\Entity\Finance\Invoice t WHERE (t.invoiceDate >= ?1) AND (t.contract = ?2)");
$param = date("Y-m-d", strtotime("first day of last month"));
$query = $query->setParameter(1, $param);
$query = $query->setParameter(2, $contract->getId());
$entities2 = $query->getResult();
$newitem = array();
$newitem['owner'] = $contract->getOwner();
$newitem['contract'] = $contract;
$newitem['invoice'] = null;
foreach ($entities2 as $invoice) {
$newitem['invoice'] = $invoice;
}
$result[] = $newitem;
}
}
$query = $em->createQuery("SELECT s.id as id FROM App\Entity\Finance\Contract t JOIN t.secondaryOwner s WHERE (t.endDate >= ?1) AND (t.startDate <= ?2) GROUP BY s");
$param1 = date("Y-m-d", strtotime("first day of last month"));
$param2 = date("Y-m-d", strtotime("last day of last month"));
$query = $query->setParameter(1, $param1);
$query = $query->setParameter(2, $param2);
$recruiters = $query->getResult();
$result2 = array();
foreach ($recruiters as $recruiter) {
$query = $em->createQuery("SELECT t FROM App\Entity\Finance\Contract t JOIN t.secondaryOwner s WHERE ((t.endDate >= ?1) AND (t.startDate <= ?2)) AND (s.id = ?3)");
$param1 = date("Y-m-d", strtotime("first day of last month"));
$param2 = date("Y-m-d", strtotime("last day of last month"));
$param3 = $recruiter;
$query = $query->setParameter(1, $param1);
$query = $query->setParameter(2, $param2);
$query = $query->setParameter(3, $param3);
$entities = $query->getResult();
foreach ($entities as $contract) {
$query = $em->createQuery("SELECT t FROM App\Entity\Finance\Invoice t WHERE (t.invoiceDate >= ?1) AND (t.contract = ?2)");
$param = date("Y-m-d", strtotime("first day of last month"));
$query = $query->setParameter(1, $param);
$query = $query->setParameter(2, $contract->getId());
$entities2 = $query->getResult();
$newitem = array();
$newitem['secondaryowner'] = $contract->getSecondaryowner();
$newitem['contract'] = $contract;
$newitem['invoice'] = null;
foreach ($entities2 as $invoice) {
$newitem['invoice'] = $invoice;
}
$result2[] = $newitem;
}
}
return $this->render('finance/contract/ownerpercentage.html.twig', [
'result' => $result,
'result2' => $result2,
'marginproduct' => $marginproduct]);
}
/**
* @Route("/index/{state?0}/{open?2}/{my?0}/{user?0}/{action?0}/{month?empty}", name="contract_index", methods={"GET"})
*/
public function index(int $state, int $open=2, int $my=0, int $user=0, int $action=0, string $month = "", ContractService $contractService): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
if ($month == "empty")
{
$month = "";
}
/*$contracts = $this->getDoctrine()->getRepository(Contract::class)->findAll();
foreach($contracts as $contract)
{
$contractService->updateInvoicedDays($contract);
}*/
//
return $this->render('finance/contract/index.html.twig',[
'state' => $state,
'open' => $open,
'my' => $my,
'user' => $user,
'action' => $action,
'month' => $month
]);
}
/**
* @Route("/table/{state}/{open}/{my}/{user}/{action}/{month}", name="contract_table", methods={"GET"})
*/
public function table(int $state, int $open, int $my, int $user = 0, int $action=0, string $month = ""): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
$username = "";
if ($my != 0)
{
$username = " - " . $this->getUser();
}
if ($user != 0)
{
$username = " - " . $this->getDoctrine()->getRepository(User::class)->findOneById($user);
}
if (empty($month))
{
$month = strtotime("now");
$month = date('Y-m-01', $month);
}
$contracts = $this->getDoctrine()
->getRepository(Contract::class)
->findBy(array('state' => $state));
$button = $state + 3*$open + 9*$my + 18*$action;
return $this->render('finance/contract/table.html.twig', [
'button' => $button, 'state' => $state, 'open' => $open, 'my' => $my, 'user' => $user, 'action' => $action,
'month' => $month,
'username' => $username
]);
}
/**
* @Route("/tabledetails/{state}/{open}/{my}/{user}/{action}/{month}", name="contract_tabledetails", methods={"GET","POST"})
*/
public function tabledetails(Request $request, int $state,int $open,int $my,int $user=0, int $action=0, string $month = "",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);
$start = $request->get('start');
$length = $request->get('length');
$usermargin = 0;
$em = $this->getDoctrine()->getManager();
$organization = $this->getUser()->getOrganization();
if ($organization == null)
{
$organizationid = 0;
}
else
{
$organizationid = $organization->getId();
}
$filter = "";
if ($request->get('search'))
{
$filter = $request->get('search')['value'];
}
if (empty($month) != true)
{
$param = strtotime($month);
$monthLastDay = date('Y-m-t', $param);
$monthFirstDay= date('Y-m-01', $param);
} else
{
$param = strtotime("now");
$monthLastDay = date('Y-m-t', $param);
$monthFirstDay= date('Y-m-01', $param);
}
$columns = $request->get('columns');
$orderColumn = 0;
$order = $request->get('order');
if ($order) {
$orderColumn = $order[0]["column"];
}
$orderby = $this->orderColumns($orderColumn);
$direction = "DESC";
if ($order) {
$direction = strtoupper($order[0]["dir"]);
}
$queryString = "SELECT s FROM App\Entity\Finance\Contract s JOIN s.contractsElement t JOIN t.employee u WHERE ((s.state = ?1) ";
if($open != 2){
$queryString = $queryString . "AND ((s.endDate >= ?7 or s.endDate is null) AND (s.startDate <= ?6))";
/*$query = $em->createQuery("SELECT s.firstName as owner, s.id as id, COUNT(t.id) as total FROM App\Entity\Finance\Contract t JOIN t.owner s WHERE (t.state = 0) AND (((t.endDate >= ?1) OR (t.endDate is null))AND (t.startDate <= ?2)) AND t.state = 0 GROUP BY s");
$param = date("Y-m-d");
$query = $query->setParameter(1, $lastmonthFirstDay);
$query = $query->setParameter(2, $lastmonthLastDay);
$activecontracts = $query->getResult();*/
}
if($my == 1){
$queryString = $queryString . "AND ((s.owner = ?4 ) or (s.secondaryOwner = ?4)) ";
}
if($user != 0){
if($action != 0){
$queryString = $queryString . "AND (s.owner = ?4 ) ";
}
else
{
$queryString = $queryString . "AND ((s.owner = ?4 ) or (s.secondaryOwner = ?4)) ";
}
}
if($action != 0){
$queryString = $queryString . "AND (((s.endDate <= ?5) OR (s.invoicedDays + 20 >= s.maxDays)) AND (s.noExtension = false)) ";
}
$queryString = $queryString ."AND ((?2 = 0) or ((s.organization = ?2) or (s.organization is null)))) ";
if(!empty($filter) == true){
$queryString = $queryString ."AND ((s.subject LIKE '%".$filter."%') ";
$queryString = $queryString ."OR (concat(u.firstname,' ', u.lastname) LIKE '%".$filter."%')) ";
}
$queryString = $queryString ."ORDER BY ".$orderby." ".$direction;
$query = $em->createQuery($queryString);
$count = $query->setParameter(2, $organizationid)
->setParameter(1, $state);
/*if($open != 2){
$count->setParameter(3, $open);
}*/
if($my == 1){
$count->setParameter(4, $this->getUser());
}
if($open != 2){
$count->setParameter(7, $monthFirstDay);
$count->setParameter(6, $monthLastDay);
}
if($user != 0){
$count->setParameter(4, $user);
}
if($action != 0){
$count->setParameter(5, date("Y-m-d", strtotime("+1 month")));
}
$total = count($count->getResult());
$query->setMaxResults($length)
->setFirstResult($start);
$proposals = $query->getResult();
$twig = clone $this->get('twig');
$datawithlinks = array();
foreach($proposals as $row){
$i = 0;
$newrow = array();
$element = null;
foreach ($row->getContractsElement() as $element2)
{
if ($element2->getState() == 0)
{
$element =$element2;
}
}
$url = $twig->createTemplate("{{ path('contract_show', {'id': id }) }}")->render(array("id" => $row->getId()));
$newrow[$i] = "".$row->getId();
$i++;
$newrow[$i] = $url;
$i++;
if ($row->getProposal() != null)
{
//$link = "<a href='".$twig->createTemplate("{{ path('proposal_show', {'id': id }) }}")->render(array("id" => $row->getProposal()->getId()))."'>".$row->getProposal()->getId()."</a>";
$newrow[$i] = "".$row->getProposal()->getId();
//;
$i++;
}
else
{
$newrow[$i] = "";
$i++;
}
if ($element != null)
{
$newrow[$i] = "".$element->getEmployee();
$i++;
$newrow[$i] = "".$element->getIntakePrice();
$i++;
$newrow[$i] = "".$element->getUnitPrice();
$i++;
} else
{
$newrow[$i] = "";
$i++;
$newrow[$i] = "";
$i++;
$newrow[$i] = "";
$i++;
}
$newrow[$i] = $row->getSubject();
$i++;
if ($row->getendDate() != null)
{
$newrow[$i] = "".$row->getendDate()->format('d/m/Y');
}
else
{
$newrow[$i] = "";
}
$i++;
$infodays = "";
if ($row->getMaxDays() != null) {
if ($row->getInvoicedDays() != null) {
$infodays = $row->getInvoicedDays() . "";
} else
{
$infodays = "0";
}
$infodays = $infodays . "/" . $row->getMaxDays();
}
$newrow[$i] = $infodays;
$i++;
$newrow[$i] = "".$row->getClient();
$i++;
$newrow[$i] = "".$row->getEndClient();
$i++;
if($open != 2){
$margin = 0;
if ((($row->getOwner() != null) && ($this->getUser()->getId() == $row->getOwner()->getId())) ||
($this->getUser()->getUserfunction()->getAuthorizationtype()->getName() != 'Authorizationtype.Default'))
{
foreach ($row->getInvoices() as $invoice)
{
if ($monthFirstDay == $invoice->getInvoiceDate()->format('Y-m-01'))
{
$margin += $invoice->getMarginowner();
if ($row->getOwner())
{
if ($row->getOwner()->getId() == $user)
{
$usermargin += $margin;
}
}
}
}
}
if ($margin > 0)
{
$margin = " / " . $margin;
} else
{
$margin = "";
}
$newrow[$i] = "".$row->getOwner() . $margin;
$i++;
$margin = 0;
if ((($row->getSecondaryOwner() != null) && ($this->getUser()->getId() == $row->getSecondaryOwner()->getId())) ||
($this->getUser()->getUserfunction()->getAuthorizationtype()->getName() != 'Authorizationtype.Default'))
{
foreach ($row->getInvoices() as $invoice)
{
if ($monthFirstDay == $invoice->getInvoiceDate()->format('Y-m-01'))
{
$margin += $invoice->getSecondarymarginowner();
if ($row->getSecondaryOwner() != null)
{
if ($row->getSecondaryOwner()->getId() == $user)
{
$usermargin += $margin;
}
}
}
}
}
if ($margin > 0)
{
$margin = " / " . $margin;
} else
{
$margin = "";
}
$newrow[$i] = "".$row->getSecondaryOwner() . $margin;
$i++;
}
$newrow[$i] = "".$row->getOrganization();
$i++;
$datawithlinks[] = $newrow;
}
$return = array(
"draw" => intval($request->get('draw')),
"recordsTotal" => $total,
"recordsFiltered" => $total,
"aaData" => $datawithlinks,
"margin" => $usermargin
);
$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"; // i don't think this works
break;
case 2:
$orderby = "s.proposal";
break;
case 3:
$orderby = "s.id";
break;
case 4:
$orderby = "s.id";
break;
case 5:
$orderby = "s.id";
break;
case 6:
$orderby = "s.subject"; // total amount don't work
break;
case 7:
$orderby = "s.endDate";
break;
case 8:
$orderby = "s.client";
break;
case 9:
$orderby = "s.endClient";
break;
case 10:
$orderby = "s.owner";
break;
case 11:
$orderby = "s.organization";
break;
}
return $orderby;
}
/**
* @Route("/showopen", name="contract_showopen", methods={"GET"})
*/
public function showopen() : Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
$contracts = $this->getDoctrine()
->getManager()
->createQuery('SELECT c FROM App\Entity\Finance\Contract c '
. 'WHERE c.state = 0 and (c.endDate >= CURRENT_DATE() or c.endDate is null)')->getResult();
return $this->render('finance/contract/table.html.twig', ['contracts' => $contracts]); //vervangen door index soort filter optie
}
/**
* @Route("/showMy", name="contract_showMy", methods={"GET"})
*/
public function showMy() : Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
$contracts = $this->getDoctrine()
->getRepository(Contract::class)
->findBy(array('owner' => $this->getUser(),'state' => 0 ));
return $this->render('finance/contract/table.html.twig',['contracts' => $contracts] );
}
/**
* @Route("/showMyOpen", name="contract_showMyOpen", methods={"GET"})
*/
public function showMyOpen() : Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
$contracts = $this->getDoctrine()
->getManager()
->createQueryBuilder()->select(array('c'))->from('App\Entity\Finance\Contract' ,'c')
->where('c.owner =' . $this->getUser()->getId())
->andWhere('c.state = 0')
->andWhere('(c.endDate >= CURRENT_DATE() or c.endDate is null)')
->getQuery()->getResult();
return $this->render('finance/contract/table.html.twig',['contracts' => $contracts] );
}
/**
* @Route("/new", name="contract_new", methods={"GET","POST"})
*/
public function new(Request $request, TranslatorInterface $translator, 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);
$contract = new Contract();
$date = new \DateTime();
$contract->setStartDate($date);
$contract->setEndDate($date);
$contract->setContractCreated($date);
$contract->setOwner($this->getUser());
$contract->setSend2signedCopies(false);
$contract->setReceivedCounterSigned(false);
$organization= $this->getUser()->getOrganization();
$contract->setOrganization($organization);
$endDateMandatoryForContract = $kernelservice->getApplicationInfoValue("Finance.EndDateMandatoryForContract");
$form = $this->createForm(ContractType::class, $contract,
['organization' => $organization, 'endDateMandatoryForContract' => $endDateMandatoryForContract,
'action' => $this->generateUrl('contract_new'),
'attr' => ['id' => $this->generateUrl('contract_new')]]);
$simpleProposals = $kernelservice->getApplicationInfoValue("Sales.SimpleProposals");
if($simpleProposals)
{
$employees = array();
$employee = "";
$unitPrice = 0;
$intakePrice = 0;
$required = true;
$form->add('employeeselect2',ChoiceType::class, array('priority' => 10, 'label' => 'Contract.Employee',
'required' => true,
'multiple' => false,
'mapped' => false,
'data' => $employee,
'choices' => $employees,
'attr' => array('class' => 'selectemployee')
));
$form->add('intakePrice', NumberType::class, array('priority' => 2, 'data' => $intakePrice, 'mapped' => false, 'label' => 'IntakePrice','required' => $required));
$form->add('unitPrice', NumberType::class, array('priority' => 1, 'data' => $unitPrice, 'mapped' => false, 'label' => 'Contract.UnitPrice','required' => $required));
}
if ($organization == 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()) {
if($contract->getEndDate() >= $contract->getStartDate() || $contract->getEndDate() == null){
$entityManager = $this->getDoctrine()->getManager();
$contract->setState(0);
if ($this->getUser()->getOrganization() != null) {
$contract->setOrganization($this->getUser()->getOrganization());
}
$entityManager->persist($contract);
if($simpleProposals)
{
$element = new ContractElement();
$element->setQuantity(1);
$element->setReceivedBack(false);
$element->setSendCounterSigned(false);
$element->setContractSendToConsultant(false);
$element->setState(0);
$element->setContract($contract);
$requestdata = $request->request->all("contract");
if (isset($requestdata['unitPrice']) == true)
{
$element->setUnitPrice(str_replace(',', '.',$requestdata['unitPrice']));
}
if (isset($requestdata['intakePrice']) == true)
{
$element->setIntakePrice(str_replace(',', '.',$requestdata['intakePrice']));
}
if (isset($requestdata['employeeselect2']) == true) {
$employeeselect2 = $requestdata['employeeselect2'];
$employee = null;
$entityManager = $this->getDoctrine()->getManager();
$employeetype = substr($employeeselect2, 0, 2);
$employeeid = substr($employeeselect2, 2);
if ($employeetype == "SU") {
$element
->setEmployee(
$entityManager->getRepository(Employee::class)
->findOneById($employeeid));
}
}
$entityManager->persist($element);
}
$entityManager->flush();
$this->denyAccessUnlessGranted($attribute, $contract);
return $this->redirectToRoute('contract_index');
}else{
$this->get('session')->getFlashBag()->add('error',
$translator->trans('Contract.startdatecannotbeafterenddate'));
}
}
return $this->render('finance/contract/new.html.twig', [
'contract' => $contract,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="contract_show", methods={"GET"})
*/
public function show(Contract $contract, 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, $contract);
$simpleProposals = $kernelservice->getApplicationInfoValue("Sales.SimpleProposals");
return $this->render('finance/contract/show.html.twig', [
'contract' => $contract,
'simpleProposals' => $simpleProposals
]);
}
/**
* @Route("/{id}/audit", name="contract_showaudit", methods={"GET"})
*/
public function showaudit(Contract $contract): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $contract);
return $this->render('finance/contract/showaudit.html.twig', [
'contract' => $contract,
]);
}
/**
* @Route("/{id}/archive", name="contract_archive", methods={"GET"})
*/
public function archive(Contract $contract): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $contract);
$entityManager = $this->getDoctrine()->getManager();
$contract->setState(1);
$entityManager->persist($contract);
$entityManager->flush();
return $this->redirectToRoute('contract_show', array('id' => $contract->getId()));
}
/**
* @Route("/{id}/restore", name="contract_restore", methods={"GET"})
*/
public function restore(Contract $contract): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $contract);
$entityManager = $this->getDoctrine()->getManager();
$contract->setState(0);
$entityManager->persist($contract);
$entityManager->flush();
return $this->redirectToRoute('contract_show', array('id' => $contract->getId()));
}
/**
* @Route("/{id}/delete", name="contract_logicaldelete", methods={"GET"})
*/
public function logicaldelete(Contract $contract): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $contract);
$entityManager = $this->getDoctrine()->getManager();
$contract->setState(2);
$entityManager->persist($contract);
$entityManager->flush();
return $this->redirectToRoute('contract_index');
}
/**
* @Route("/{id}/edit", name="contract_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Contract $contract, 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, $contract);
$endDateMandatoryForContract = $kernelservice->getApplicationInfoValue("Finance.EndDateMandatoryForContract");
if ($this->getUser()->getUserfunction()->getAuthorizationtype()->getName() != 'Authorizationtype.Default')
{
$form = $this->createForm(ContractAdminType::class, $contract,
['organization' => $contract->getOrganization(), 'endDateMandatoryForContract' => $endDateMandatoryForContract,
'action' => $this->generateUrl('contract_edit', array('id' => $contract->getId())),
'attr' => ['id' => $this->generateUrl('contract_edit', array('id' => $contract->getId()))]]);
}
else
{
$form = $this->createForm(ContractType::class, $contract,
['organization' => $contract->getOrganization(), 'endDateMandatoryForContract' => $endDateMandatoryForContract,
'action' => $this->generateUrl('contract_edit', array('id' => $contract->getId())),
'attr' => ['id' => $this->generateUrl('contract_edit', array('id' => $contract->getId()))]]);
}
$simpleProposals = $kernelservice->getApplicationInfoValue("Sales.SimpleProposals");
if($simpleProposals)
{
$employees = array();
$employee = "";
$unitPrice = 0;
$intakePrice = 0;
$contractSendToConsultant = false;
$contractSendDate = null;
$receivedBack = false;
$sendCounterSigned = false;
if ($contract->getContractsElement()->isEmpty() == false) {
$element = $contract->getContractsElement()->first();
$employee = $element->getEmployee();
$unitPrice = $element->getUnitPrice();
$intakePrice = $element->getIntakePrice();
$contractSendToConsultant = $element->getContractSendToConsultant();
$contractSendDate = $element->getContractSendDate();
$receivedBack = $element->getReceivedBack();
$sendCounterSigned = $element->getSendCounterSigned();
if ($element->getEmployee() != null)
{
$employees["".$element->getEmployee()]
= "SU".$element->getEmployee()->getId();
$employee = "SU".$element->getEmployee()->getId();
}
}
$required = true;
$form->add('employeeselect2',ChoiceType::class, array('priority' => 3, 'label' => 'Employee',
'required' => true,
'multiple' => false,
'mapped' => false,
'data' => $employee,
'choices' => $employees,
'attr' => array('class' => 'selectemployee')
));
$form->add('intakePrice', NumberType::class, array('priority' => 2, 'data' => $intakePrice, 'mapped' => false, 'label' => 'IntakePrice','required' => $required));
$form->add('unitPrice', NumberType::class, array('priority' => 1, 'data' => $unitPrice, 'mapped' => false, 'label' => 'UnitPrice','required' => $required));
//$form->add('contractSendToConsultant', CheckboxType::class, array('data' => $contractSendToConsultant, 'mapped' => false, 'label' => 'ContractElement.ContractSendToConsultant','required' =>false));
//$form->add('contractSendDate', DateType::class, array('data' => $contractSendDate,'label' => 'ContractElement.ContractSendDate','mapped' => false,'required' =>false,'widget' => 'single_text', 'attr' => array('class' => 'date')));
//$form->add('receivedBack', CheckboxType::class, array('data' => $receivedBack, 'mapped' => false, 'label' => 'ContractElement.ReceivedBack','required' =>false));
//$form->add('sendCounterSigned', CheckboxType::class, array('data' => $sendCounterSigned, 'mapped' => false, 'label' => 'ContractElement.SendCounterSigned','required' =>false));
}
$form->handleRequest($request);
if ($form->isSubmitted()) { // && $form->isValid()) {
if($simpleProposals)
{
if ($contract->getContractsElement()->isEmpty() == false) {
$element = $contract->getContractsElement()->first();
$requestdata = array_merge($request->request->all("contract"), $request->request->all("contract_admin"));
if (isset($requestdata['contractSendToConsultant']) == true)
{
$element->setContractSendToConsultant(true);
}
else
{
$element->setContractSendToConsultant(false);
}
if (isset($requestdata['contractSendDate']) == true)
{
$element->setContractSendDate(new \DateTime($requestdata['contractSendDate']));
}
if (isset($requestdata['receivedBack']) == true)
{
$element->setReceivedBack(true);
}
else
{
$element->setReceivedBack(false);
}
if (isset($requestdata['sendCounterSigned']) == true)
{
$element->setSendCounterSigned(true);
}
else
{
$element->setSendCounterSigned(false);
}
if (isset($requestdata['unitPrice']) == true)
{
$element->setUnitPrice(str_replace(',', '.',$requestdata['unitPrice']));
}
if (isset($requestdata['intakePrice']) == true)
{
$element->setIntakePrice(str_replace(',', '.',$requestdata['intakePrice']));
}
if (isset($requestdata['employeeselect2']) == true) {
$employeeselect2 = $requestdata['employeeselect2'];
$employee = null;
$entityManager = $this->getDoctrine()->getManager();
$employeetype = substr($employeeselect2, 0, 2);
$employeeid = substr($employeeselect2, 2);
if ($employeetype == "SU") {
$element
->setEmployee(
$entityManager->getRepository(Employee::class)
->findOneById($employeeid));
}
}
$this->getDoctrine()->getManager()->persist($element);
$this->getDoctrine()->getManager()->flush();
}
}
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('contract_show', array('id' => $contract->getId()));
}
return $this->render('finance/contract/edit.html.twig', [
'contract' => $contract,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="contract_delete", methods={"DELETE"})
*/
public function delete(Request $request, Contract $contract): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $contract);
if ($this->isCsrfTokenValid('delete'.$contract->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($contract);
$entityManager->flush();
}
return $this->redirectToRoute('contract_index');
}
}