Exemplo n.º 1
0
 /**
  * @Route("/admin/contract/freeze/{id}", name="tsk_contract_freeze")
  * @Template()
  */
 public function freezeAction(Contract $contract)
 {
     if (!$contract->getIsActive()) {
         throw new \Exception('Contract is not active');
     }
     $request = $this->getRequest();
     $contractFreeze = new \TSK\ContractBundle\Form\Model\ContractFreeze();
     $contractFreeze->setContract($contract);
     $contractFreeze->setStudent($contract->getStudents()->first());
     $form = $this->createForm(new \TSK\ContractBundle\Form\Type\ContractFreezeType(), $contractFreeze);
     if ($request->isMethod('POST')) {
         $form->bind($request);
         if ($form->isValid()) {
             // Validate contractFreeze
             // - sum of freeze days plus newly requested freeze days ust be less than MaxFreezeDays
             // - freeze days should not overlap with any existing freeze days
             // - freeze start_date >= today()
             // - start_date < end_date
             $em = $this->getDoctrine()->getManager();
             $cfRepo = $em->getRepository('TSK\\ContractBundle\\Entity\\ContractFreeze');
             $existingFreezeDays = $cfRepo->sumByContract($contractFreeze->getContract());
             $requestedFreezeDays = $contractFreeze->getTotalFreezeDays();
             $limit = 60;
             if ($existingFreezeDays + $requestedFreezeDays < $limit) {
                 if ($cfRepo->datesOverlapForContract($contractFreeze->getContract(), $contractFreeze->getStartDate(), $contractFreeze->getEndDate())) {
                     $this->get('session')->getFlashBag()->add('error', 'Unable to honor freeze request.  Start date and end date already overlap');
                 } else {
                     // Save contract freeze
                     $cf = new \TSK\ContractBundle\Entity\ContractFreeze();
                     $cf->setStudent($contractFreeze->getStudent());
                     $cf->setContract($contractFreeze->getContract());
                     $cf->setStartDate($contractFreeze->getStartDate());
                     $cf->setEndDate($contractFreeze->getEndDate());
                     $em->persist($cf);
                     $em->flush();
                     $this->get('session')->getFlashBag()->add('success', 'Contract freeze saved!');
                 }
             } else {
                 $this->get('session')->getFlashBag()->add('error', 'Unable to honor freeze request.  Request would put student over ' . $limit . ' day limit');
             }
             // return $this->redirect($this->generateUrl('admin_tsk_student_student_list'));
         }
     }
     return array('form' => $form->createView());
 }