public function isValid() { $isVaild = parent::isValid(); if ($isVaild) { $data = parent::getData(); $expenseCategory = new \Accounting\Model\ExpenseCategory(); if ($data['id']) { $expenseCategory->setId($data['id']); } if ($data['companyId']) { $expenseCategory->setCompanyId($data['companyId']); } if ($data['code']) { $expenseCategory->setCode($data['code']); } if ($data['name']) { $expenseCategory->setName($data['name']); } $expenseCategoryMapper = $this->getServiceLocator()->get('Accounting\\Model\\ExpenseCategoryMapper'); $result = $expenseCategoryMapper->checkunique($expenseCategory); if ($result && $result == 'code') { $this->get('code')->setMessages(['Mã code này đã tồn tại']); $isVaild = false; } if ($result && $result == 'name') { $this->get('name')->setMessages(['tên này đã tồn tại']); $isVaild = false; } return $isVaild; } }
/** * (non-PHPdoc) * @see \Zend\Form\Form::isValid() */ public function isValid() { $isValid = parent::isValid(); if ($isValid) { $data = parent::getData(); // lấy danh sách category $expenseCategory = new \Accounting\Model\ExpenseCategory(); $expenseCategory->setCompanyId($data['companyId']); $expenseCategory->addOption('fetchOnlyIds', true); $expenseCategoryMapper = $this->getServiceLocator()->get('\\Accounting\\Model\\ExpenseCategoryMapper'); $expenseCategoryIds = $expenseCategoryMapper->fetchAll($expenseCategory); $itemsArray = json_decode($data['items'], true); foreach ($itemsArray as $itemDataPopulate) { $itemDataPopulate['accountId'] = $data['accountId']; $itemValidator = new \Accounting\Form\Transaction\ItemValidate($this->getServiceLocator(), ['expenseCategoryIds' => $expenseCategoryIds]); $itemValidator->setData($itemDataPopulate); if (!$itemValidator->isValid()) { $this->get('items')->setMessages($itemValidator->getErrorMessagesList()); $isValid = false; } } } return $isValid; }
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(); }