/**
  * @param Receivable $receivable
  * @return Receivable
  * @throws \Exception
  */
 public function saveReceivable(Receivable $receivable)
 {
     $id = $receivable->getReceiveVoucherId();
     $data = $receivable->getArrayCopy();
     if (empty($data['status'])) {
         $data['status'] = 'R';
         if (empty($data['requestedDate'])) {
             $data['requestedDate'] = date('Y-m-d H:i:s', time());
         }
         $data['depositBy'] = $this->staffId;
     } else {
         if ($data['status'] === 'A') {
             if (empty($data['approvedDate'])) {
                 $data['approvedDate'] = date('Y-m-d H:i:s', time());
             }
             $data['approveBy'] = $this->staffId;
         } else {
             if ($data['status'] === 'C') {
                 if (empty($data['approvedDate'])) {
                     $data['approvedDate'] = date('Y-m-d H:i:s', time());
                 }
                 $data['approveBy'] = $this->staffId;
             } else {
                 throw new \Exception('Invalid Status');
             }
         }
     }
     if ($id > 0) {
         $this->update($data, array('receiveVoucherId' => $id));
     } else {
         unset($data['receiveVoucherId']);
         $this->insert($data);
     }
     if (!$receivable->getReceiveVoucherId()) {
         $receivable->setReceiveVoucherId($this->getLastInsertValue());
     }
     return $receivable;
 }
 /**
  * @return \Zend\Http\Response|ViewModel
  * @throws \Exception
  */
 public function requestAction()
 {
     $helper = new ReceivableHelper($this->receivableTable());
     $form = $helper->getForm($this->currencyCombo());
     $receivable = new Receivable();
     $generateNo = $this->receivableTable()->getVoucherNo(date('Y-m-d', time()));
     $receivable->setVoucherNo($generateNo);
     $receivable->setDepositBy($this->staffId);
     $form->bind($receivable);
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setInputFilter($helper->getInputFilter());
         $post_data = $request->getPost()->toArray();
         $form->setData($post_data);
         if ($form->isValid()) {
             $this->receivableTable()->saveReceivable($receivable);
             $this->flashMessenger()->addInfoMessage('Requested voucher by ' . $generateNo . '.');
             return $this->redirect()->toRoute('account_receivable');
         }
     }
     return new ViewModel(array('form' => $form, 'staffName' => $this->staffName, 'accountTypes' => $this->accountTypes()));
 }