Example #1
0
 /**
  * @author KienNN
  * @param \Accounting\Model\Transaction\Item $item
  */
 public function fetchAll($item)
 {
     $select = $this->getDbSql()->select(['i' => self::TABLE_NAME]);
     if ($item->getTransactionId()) {
         $select->where(['i.transactionId' => $item->getTransactionId()]);
     }
     $query = $this->getDbSql()->buildSqlString($select);
     $rows = $this->getDbAdapter()->query($query, Adapter::QUERY_MODE_EXECUTE);
     $result = [];
     if ($rows->count()) {
         foreach ($rows->toArray() as $row) {
             $model = new \Accounting\Model\Transaction\Item();
             $model->exchangeArray($row);
             $result[] = $model;
         }
     }
     return $result;
 }
 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();
 }
 /**
  * Rà soát tất cả payment
  * check nếu payment đã có transactionId => bỏ qua
  * Nếu không, tạo mới transaction, transactionItem, với transaction.id=payment.id
  * update transactionId cho payment
  *
  */
 public function paymenttotransactionAction()
 {
     $dbAdapter = $this->getServiceLocator()->get('dbAdapter');
     $dbSql = $this->getServiceLocator()->get('dbSql');
     $select = $dbSql->select(['p' => \Crm\Model\Contract\PaymentMapper::TABLE_NAME]);
     $select->join(['c' => \Crm\Model\ContractMapper::TABLE_NAME], 'p.contractId=c.id', ['c.companyId' => 'companyId']);
     $select->order(['p.id asc']);
     $paginatorAdapter = new \Zend\Paginator\Adapter\DbSelect($select, $dbAdapter);
     $paginator = new \Zend\Paginator\Paginator($paginatorAdapter);
     $paginator->setItemCountPerPage(200);
     $page = $this->getRequest()->getQuery('page', 1);
     $totalConvert = $this->getRequest()->getQuery('totalConvert', 0);
     $paginator->setCurrentPageNumber($page);
     $transactionItemMapper = $this->getServiceLocator()->get('\\Accounting\\Model\\Transaction\\ItemMapper');
     $transactionMapper = $this->getServiceLocator()->get('\\Accounting\\Model\\TransactionMapper');
     $paymentMapper = $this->getServiceLocator()->get('\\Crm\\Model\\Contract\\PaymentMapper');
     foreach ($paginator as $row) {
         $row = (array) $row;
         $payment = new \Crm\Model\Contract\Payment();
         $payment->exchangeArray($row);
         if ($row['transactionId']) {
             continue;
         }
         $transaction = new \Accounting\Model\Transaction();
         $transaction->setId($payment->getId());
         if ($payment->getCompanyId()) {
             $transaction->setCompanyId($payment->getCompanyId());
         } else {
             $transaction->setCompanyId($row['c.companyId']);
         }
         if ($payment->getPaymentType() == \Crm\Model\Contract\Payment::PAYMENT_TYPE_EXPORT) {
             $transaction->setType(\Accounting\Model\Transaction::TYPE_PAY);
         } else {
             $transaction->setType(\Accounting\Model\Transaction::TYPE_RECEIPT);
         }
         $transaction->setItemType(\Accounting\Model\Transaction::ITEM_TYPE_CRM_CONTRACT);
         $transaction->setItemId($payment->getContractId());
         $transaction->setCreatedById($payment->getCreatedById());
         $createdDateTime = new \DateTime($payment->getCreatedDateTime());
         $transaction->setCreatedDate($createdDateTime->format(DateBase::COMMON_DATE_FORMAT));
         $transaction->setCreatedTime($createdDateTime->format(DateBase::COMMON_TIME_FORMAT));
         if ($payment->getStatus() == \Crm\Model\Contract\Payment::STATUS_DELETED) {
             $transaction->setStatus(\Accounting\Model\Transaction::STATUS_INAPPROVED);
         } elseif ($payment->getStatus() == \Crm\Model\Contract\Payment::STATUS_CHECKED) {
             $transaction->setStatus(\Accounting\Model\Transaction::STATUS_ACCOUNTING);
         } else {
             $transaction->setStatus(\Accounting\Model\Transaction::STATUS_NEW);
         }
         $transaction->setAmount($payment->getAmount());
         $transaction->setDescription($payment->getDescription());
         if ($payment->getCheckedDate()) {
             $transaction->setApplyDate($payment->getCheckedDate());
         } else {
             $transaction->setApplyDate($createdDateTime->format(DateBase::COMMON_DATE_FORMAT));
         }
         if ($payment->getCheckedById()) {
             $transaction->setAccountingById($payment->getCheckedById());
         }
         if ($payment->getCheckedDateTime()) {
             $transaction->setAccountingDateTime($payment->getCheckedDateTime());
         }
         $data = array('id' => $transaction->getId(), 'companyId' => $transaction->getCompanyId(), 'type' => $transaction->getType(), 'applyDate' => $transaction->getApplyDate() ?: null, 'amount' => $transaction->getAmount() ?: null, 'description' => $transaction->getDescription() ?: null, 'status' => $transaction->getStatus(), 'createdDate' => $transaction->getCreatedDate(), 'createdById' => $transaction->getCreatedById(), 'createdTime' => $transaction->getCreatedTime(), 'approvedById' => $transaction->getApprovedById() ?: null, 'approvedDateTime' => $transaction->getApprovedDateTime() ?: null, 'accountingById' => $transaction->getAccountingById() ?: null, 'accountingDateTime' => $transaction->getAccountingDateTime() ?: null, 'paymentById' => $transaction->getPaymentById() ?: null, 'paymentDateTime' => $transaction->getPaymentDateTime() ?: null, 'itemType' => $transaction->getItemType() ?: null, 'itemId' => $transaction->getItemId() ?: null);
         $insert = $dbSql->insert($transactionMapper::TABLE_NAME);
         $insert->values($data);
         $dbAdapter->query($dbSql->buildSqlString($insert), Adapter::QUERY_MODE_EXECUTE);
         //$transactionMapper->save($transaction);
         $payment->setTransactionId($transaction->getId());
         $paymentMapper->save($payment);
         $transactionItem = new \Accounting\Model\Transaction\Item();
         $transactionItem->setTransactionId($transaction->getId());
         $transactionItem->setDate($transaction->getApplyDate());
         $transactionItem->setAmount($transaction->getAmount());
         if ($transaction->getType() == \Accounting\Model\Transaction::TYPE_RECEIPT) {
             if ($payment->getType() == $payment::TYPE_MONEY_TRANSFER) {
                 $transactionItem->setCreditAccountId(308);
             } else {
                 $transactionItem->setCreditAccountId(303);
             }
         } else {
             if ($payment->getType() == $payment::TYPE_MONEY_TRANSFER) {
                 $transactionItem->setDebitAccountId(308);
             } else {
                 $transactionItem->setDebitAccountId(303);
             }
         }
         $transactionItem->setItemType($transaction->getItemType());
         $transactionItem->setItemId($transaction->getItemId());
         $transactionItem->setDescription($transaction->getDescription());
         $transactionItem->setStatus($transaction->getStatus());
         $transactionItemMapper->save($transactionItem);
         $totalConvert++;
     }
     $this->getViewModel()->setTerminal(true);
     $this->getViewModel()->setVariable('paginator', $paginator);
     $this->getViewModel()->setVariable('page', $page);
     $this->getViewModel()->setVariable('totalPages', $paginator->count() + 1);
     $this->getViewModel()->setVariable('totalConvert', $totalConvert);
     if ($page <= $paginator->count()) {
         $this->getViewModel()->setVariable('redirectUri', Uri::build('/system/tool/paymenttotransaction', ['page' => $page + 1, 'totalConvert' => $totalConvert]));
     }
     return $this->getViewModel();
 }