/** * @Route("/contrat/{id}/acceptation", name="contrat_acceptation") * @ParamConverter("contrat", class="AppBundle:Contrat") */ public function acceptationAction(Request $request, Contrat $contrat) { $dm = $this->get('doctrine_mongodb')->getManager(); $contratManager = new ContratManager($dm); $oldTechnicien = $contrat->getTechnicien(); $oldNbFactures = $contrat->getNbFactures(); $form = $this->createForm(new ContratAcceptationType($dm, $contrat), $contrat, array('action' => $this->generateUrl('contrat_acceptation', array('id' => $contrat->getId())), 'method' => 'POST')); $isBrouillon = $request->get('brouillon'); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $contrat = $form->getData(); if ($contrat->isModifiable() && !$isBrouillon && $contrat->getDateDebut()) { $contratManager->generateAllPassagesForContrat($contrat); $contrat->setDateFin($contrat->getDateDebut()->modify("+" . $contrat->getDuree() . " month")); $contrat->setStatut(ContratManager::STATUT_EN_COURS); $dm->persist($contrat); $dm->flush(); return $this->redirectToRoute('contrat_visualisation', array('id' => $contrat->getId())); } else { if (!$oldTechnicien || $oldTechnicien->getId() != $contrat->getTechnicien()->getId()) { $contrat->changeTechnicien($contrat->getTechnicien()); } if ($oldNbFactures != $contrat->getNbFactures()) { $contratManager->updateNbFactureForContrat($contrat); } if ($contrat->getDateDebut()) { $dateFinCalcule = \DateTime::createFromFormat('Y-m-d H:i:s', $contrat->getDateDebut()->format('Y-m-d') . " 00:00:00"); $contrat->setDateFin($dateFinCalcule->modify("+" . $contrat->getDuree() . " month")); } $dm->persist($contrat); $dm->flush(); return $this->redirectToRoute('passage_etablissement', array('id' => $contrat->getEtablissements()->first()->getId())); } } $factures = $contratManager->getAllFactureForContrat($contrat); return $this->render('contrat/acceptation.html.twig', array('contrat' => $contrat, 'factures' => $factures, 'form' => $form->createView(), 'societe' => $contrat->getSociete())); }