public function paymentAction()
 {
     $id = $this->getRequest()->getQuery('id');
     if (!$id) {
         return $this->getViewModel()->setVariable('errorMsg', ['Không tìm thấy phiếu thu chi']);
     }
     $transaction = new \Accounting\Model\Transaction();
     $transaction->setId($id);
     $transactionMapper = $this->getServiceLocator()->get('\\Accounting\\Model\\TransactionMapper');
     if (!$transactionMapper->get($transaction)) {
         return $this->getViewModel()->setVariable('errorMsg', ['Không tìm thấy phiếu thu chi']);
     }
     if (!in_array($transaction->getStatus(), [\Accounting\Model\Transaction::STATUS_ACCOUNTING])) {
         return $this->getViewModel()->setVariable('errorMsg', ['Phiếu chưa được hạch toán, không thể kí chi thu']);
     }
     $form = new \Accounting\Form\Transaction\ApproveReq($this->getServiceLocator());
     $form->setData($transaction->toFormValue());
     $form->setCompanyId($transaction->getCompanyId());
     $form->setId($transaction->getId());
     $this->getViewModel()->setVariable('form', $form);
     if ($this->getRequest()->isPost()) {
         $form->setData($this->getRequest()->getPost());
         if ($form->isValid()) {
             $formData = $form->getData();
             $transaction->exchangeArray($formData);
             $transaction->setPaymentById($this->user()->getIdentity());
             $transaction->setPaymentDateTime(DateBase::getCurrentDateTime());
             $transaction->setStatus(\Accounting\Model\Transaction::STATUS_PAYMENT);
             $transactionMapper->save($transaction);
             if (isset($formData['itemData']) && count($formData['itemData'])) {
                 $transactionItemMapper = $this->getServiceLocator()->get('\\Accounting\\Model\\Transaction\\ItemMapper');
                 foreach ($formData['itemData'] as $itemData) {
                     $transactionItem = new \Accounting\Model\Transaction\Item();
                     $transactionItem->exchangeArray($itemData);
                     $transactionItem->setTransactionDate($transaction->getApplyDate());
                     $transactionItem->setTransactionId($transaction->getId());
                     $transactionItem->setStatus($transaction->getStatus());
                     $transactionItemMapper->save($transactionItem);
                 }
             }
             return $this->redirect()->toUrl('/accounting/transaction/index?id=' . $transaction->getId());
         }
     }
     $this->getViewModel()->setVariable('transaction', $transaction);
     $transactionItem = new \Accounting\Model\Transaction\Item();
     $transactionItem->setTransactionId($transaction->getId());
     $transactionItem->addOption('loadAccountId', true);
     $transactionItem->addOption('loadExpenseCategory', true);
     $transactionItemMapper = $this->getServiceLocator()->get('\\Accounting\\Model\\Transaction\\ItemMapper');
     $items = $transactionItemMapper->fetchAll($transactionItem);
     $this->getViewModel()->setVariable('items', $items);
     $company = new \Company\Model\Company();
     $company->setId($transaction->getCompanyId());
     $companyMapper = $this->getServiceLocator()->get('\\Company\\Model\\CompanyMapper');
     if ($companyMapper->get($company)) {
         $this->getViewModel()->setVariable('company', $company);
     }
     $userMapper = $this->getServiceLocator()->get('\\User\\Model\\UserMapper');
     $this->getViewModel()->setVariable('createdBy', $userMapper->get($transaction->getCreatedById()));
     if ($transaction->getApprovedById()) {
         $this->getViewModel()->setVariable('approveBy', $userMapper->get($transaction->getApprovedById()));
     }
     if ($transaction->getAccountingById()) {
         $this->getViewModel()->setVariable('accountingBy', $userMapper->get($transaction->getAccountingById()));
     }
     $expenseCategory = new \Accounting\Model\ExpenseCategory();
     $expenseCategory->setCompanyId($transaction->getCompanyId());
     $expenseCategoryMapper = $this->getServiceLocator()->get('\\Accounting\\Model\\ExpenseCategoryMapper');
     $tree = new \Home\Model\Tree();
     $this->getViewModel()->setVariable('categories', $tree->toArrayRecusived($expenseCategoryMapper->fetchAll($expenseCategory)));
     $account = new \Accounting\Model\Account();
     $account->setCompanyId($transaction->getCompanyId());
     $account->addOption('sort', ['sort' => 'c.id', 'dir' => 'ASC']);
     $accountMapper = $this->getServiceLocator()->get('\\Accounting\\Model\\AccountMapper');
     $this->getViewModel()->setVariable('accounts', $tree->toArrayRecusived($accountMapper->fetchAll($account)));
     return $this->getViewModel();
 }