/**
  * @param Leave $leave
  * @return Leave
  */
 public function saveLeave(Leave $leave)
 {
     $id = $leave->getLeaveId();
     $data = $leave->getArrayCopy();
     if (empty($data['status'])) {
         $data['status'] = 'A';
     }
     if ($id > 0) {
         $this->update($data, array('leaveId' => $id));
     } else {
         unset($data['leaveId']);
         $this->insert($data);
     }
     if (!$leave->getLeaveId()) {
         $leave->setLeaveId($this->lastInsertValue);
     }
     return $leave;
 }
 /**
  * @return \Zend\Http\Response|ViewModel
  */
 public function detailAction()
 {
     $id = (int) $this->params()->fromRoute('id', 0);
     $staff = (int) $this->params()->fromQuery('staff', 0);
     $helper = new LeaveHelper();
     $this->initCombo();
     $leave = $this->leaveTable()->getLeave($id);
     $isSave = false;
     if (!$leave) {
         $leave = new Leave();
         $isSave = true;
         $form = $helper->getForm($this->staffList, $this->statusList, $this->leaveTypeList);
     } else {
         $formType = 'V';
         if ($leave->getStatus() == 'R') {
             $isSave = true;
             $formType = 'R';
         }
         $form = $helper->getForm($this->staffList, $this->statusList, $this->leaveTypeList, $formType);
     }
     if ($staff > 0) {
         $leave->setStaffId($staff);
     }
     $form->bind($leave);
     $request = $this->getRequest();
     if ($request->isPost()) {
         $post_data = array_merge($leave->getArrayCopy(), $request->getPost()->toArray());
         $form->setData($post_data);
         $form->setInputFilter($form->getInputFilter());
         if ($form->isValid()) {
             $hasLeave = $this->leaveTable()->getLeaveByStaff($leave->getStaffId(), $leave->getDate());
             if ($hasLeave && $hasLeave->getStatus() == 'A') {
                 $this->flashMessenger()->addWarningMessage('Sorry! you already approved leave for this date.');
             } else {
                 if ($leave->getStatus() == 'A' && in_array($leave->getLeaveType(), array_keys($this->annualLeave))) {
                     $db = $this->leaveTable()->getAdapter();
                     $conn = $db->getDriver()->getConnection();
                     $conn->beginTransaction();
                     try {
                         $deduct = $this->annualLeave[$leave->getLeaveType()];
                         $this->staffTable()->updateLeave($deduct, $leave->getStaffId());
                         $this->leaveTable()->saveLeave($leave);
                         $conn->commit();
                     } catch (\Exception $ex) {
                         $conn->rollback();
                         $this->flashMessenger()->addErrorMessage($ex->getMessage());
                     }
                 } else {
                     $this->leaveTable()->saveLeave($leave);
                 }
                 $this->flashMessenger()->addSuccessMessage('Save successful.');
             }
             return $this->redirect()->toRoute('hr_leave');
         }
     }
     return new ViewModel(array('form' => $form, 'id' => $id, 'isSave' => $isSave, 'backLink' => $this->getRequest()->getHeader('Referer')->getUri()));
 }