public function payAction()
 {
     //grab the id which is a section passed from index
     $entityManager = $this->getEntityManager();
     $id = $this->params()->fromRoute('id');
     $student = $id;
     $form = new FeeStudentForm($entityManager);
     $feestudent = new FeeStudent();
     $feepayments = new FeePayments();
     $feestudenttotal = new FeeStudentTotal();
     $form->bind($feestudent);
     //var_dump($form);
     $request = $this->getRequest();
     if ($request->isPost()) {
         $data = $request->getPost();
         //get year value from the submited form
         // $student= $data['feestudent']['student'];
         //get the current session
         $session = $this->getEntityManager()->getRepository('Admin\\Entity\\Session')->findOneBy(array(), array('id' => 'DESC'));
         $year = $session->getYear();
         //check to see if the student and sesion already exist.
         $studfee = $this->getEntityManager()->getRepository('Admin\\Entity\\FeeStudent')->findOneBy(array('student' => $id, 'session' => $session->getId()));
         //get the section of student
         $studsection = $this->getEntityManager()->getRepository('Admin\\Entity\\Student')->findOneBy(array('id' => $id));
         $section = $studsection->getSection();
         //var_dump($studfee);die;
         //get the Total of the student section
         $feetotal = $this->getEntityManager()->getRepository('Admin\\Entity\\FeeTotal')->findOneBy(array('section' => $section->getId(), 'year' => $year->getId()));
         $sectiontotal = $feetotal->getAmount();
         $form->setData($request->getPost());
         //  var_dump($form->setData($request->getPost()));die;
         if ($form->isValid()) {
             // var_dump($form);die;
             //if it exist throw an error message and redirect to index of fee
             if (isset($studfee)) {
                 $this->flashMessenger()->addSuccessMessage("Student Fee Entered Successfully!");
                 $studfeeid = $studfee->getId();
                 $fee_total = $entityManager->getRepository('Admin\\Entity\\FeePayments')->getTotal($studfeeid);
                 $feestoo = $this->getEntityManager()->getRepository('Admin\\Entity\\FeeStudentTotal')->findOneBy(array('feeStudent' => $studfeeid));
                 try {
                     $feepayments->setFeeStudent($studfee);
                     $feepayments->setAmount($data['feestudent']['feepayments']['amount']);
                     $feepayments->setReceipt($data['feestudent']['feepayments']['receipt']);
                     $feepayments->setDop($data['feestudent']['feepayments']['dop']);
                     $feepayments->setMethod($data['feestudent']['feepayments']['method']);
                     $entityManager->persist($feepayments);
                     // $entityManager->flush();
                     $fees_total = $data['feestudent']['feepayments']['amount'] + $fee_total;
                     //var_dump($fees_total);
                     $feestatus = '';
                     if ($fees_total >= $sectiontotal) {
                         $feestatus = 'Paid';
                     } elseif ($fees_total < $sectiontotal) {
                         $feestatus = 'Owning';
                     }
                     //set the feetotal entity with info from feesection
                     $feestoo->setAmount($fees_total);
                     $feestoo->setFeeStatus($feestatus);
                     // var_dump($feestoo);die;
                     //persits and walahhh!
                     $entityManager->persist($feestoo);
                     //map the total in the student entity
                     $studsection->setStudentTotal($feestoo);
                     //persits and walahhh!
                     $entityManager->persist($studsection);
                     $entityManager->flush();
                     return $this->redirect()->toRoute('collection', array('controller' => 'feecollection', 'action' => 'index'));
                 } catch (\Exception $ex) {
                     $this->redirect()->toRoute('collection', array('controller' => 'feecollection', 'action' => 'index'));
                 }
             } else {
                 try {
                     //do hydration
                     $this->flashMessenger()->addSuccessMessage('Student Fee Entered Successfully!');
                     //set section for feesection
                     $feestudent->setStudent($studsection);
                     $feestudent->setSession($session);
                     // var_dump($feestudent);die;
                     $entityManager->persist($feestudent);
                     //get the total fee from the feesection
                     //set feepayment
                     $feepayments->setFeeStudent($feestudent);
                     $feepayments->setAmount($data['feestudent']['feepayments']['amount']);
                     $feepayments->setReceipt($data['feestudent']['feepayments']['receipt']);
                     $feepayments->setDop($data['feestudent']['feepayments']['dop']);
                     $feepayments->setMethod($data['feestudent']['feepayments']['method']);
                     $entityManager->persist($feepayments);
                     // $total=$entityManager->getRepository('Admin\Entity\FeePayments')->getTotal($feestudent->getId());
                     $amount = $data['feestudent']['feepayments']['amount'];
                     $feestatus = '';
                     if ($amount >= $sectiontotal) {
                         $feestatus = 'Paid';
                     } elseif ($amount < $sectiontotal) {
                         $feestatus = 'Owning';
                     }
                     //set the feetotal entity with info from feesection
                     $feestudenttotal->setFeeStudent($feestudent);
                     $feestudenttotal->setSession($session);
                     $feestudenttotal->setSectionFee($sectiontotal);
                     $feestudenttotal->setAmount($data['feestudent']['feepayments']['amount']);
                     $feestudenttotal->setFeeStatus($feestatus);
                     //persits and walahhh!
                     $entityManager->persist($feestudenttotal);
                     //map the total in the student entity
                     $studsection->setStudentTotal($feestudenttotal);
                     //persits and walahhh!
                     $entityManager->persist($studsection);
                     $entityManager->flush();
                     // Redirect to feesetup index
                     return $this->redirect()->toRoute('collection', array('controller' => 'feecollection', 'action' => 'index'));
                 } catch (\Exception $ex) {
                     $this->redirect()->toRoute('collection', array('controller' => 'feecollection', 'action' => 'index'));
                 }
             }
         }
     }
     //give em the form and section as id
     return new ViewModel(array('form' => $form, 'id' => $id));
 }
 /**
  * {@inheritDoc}
  */
 public function getSectionFee()
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'getSectionFee', array());
     return parent::getSectionFee();
 }