/**
  *
  * @author Hungpx
  * @param \Accounting\Model\Account $item
  */
 public function fetchAll($item)
 {
     $select = $this->getDbSql()->select(array('c' => self::TABLE_NAME));
     if ($item->getId()) {
         $select->where(['c.id' => $item->getId()]);
     } elseif ($item->getOption('ids')) {
         $select->where(['c.id' => $item->getOption('ids')]);
     }
     if ($item->getCompanyId()) {
         $select->where(['c.companyId' => $item->getCompanyId()]);
     }
     if ($item->getOption('companyId')) {
         $select->where(['c.companyId' => $item->getOption('companyId')]);
     }
     if ($item->getName()) {
         $select->where(['c.name LIKE ?' => '%' . $item->getName() . '%']);
     }
     if ($item->getOption('sort')) {
         $select->order([implode(' ', $item->getOption('sort'))]);
     } else {
         $select->order(['c.id' => 'DESC']);
     }
     $query = $this->getDbSql()->buildSqlString($select);
     $results = $this->getDbAdapter()->query($query, Adapter::QUERY_MODE_EXECUTE);
     $paginator = [];
     $userIds = [];
     $companyIds = [];
     $accountIds = [];
     if ($results->count()) {
         foreach ($results as $row) {
             $row = (array) $row;
             $account = new Account();
             $account->exchangeArray($row);
             $paginator[] = $account;
             if ($account->getCreatedById()) {
                 $userIds[$account->getCreatedById()] = $account->getCreatedById();
             }
             if ($account->getCompanyId()) {
                 $companyIds[$account->getCompanyId()] = $account->getCompanyId();
             }
             $accountIds[$account->getId()] = $account->getId();
         }
     }
     if ($item->getOption('fetchOnlyIds')) {
         return $accountIds;
     }
     $userNames = [];
     $companyNames = [];
     // get User's name
     if (count($userIds) && $item->getOption('loadUser')) {
         $select = $this->getDbSql()->select(['c' => \User\Model\UserMapper::TABLE_NAME], ['id', 'fullName']);
         $select->where(['c.id' => $userIds]);
         $query = $this->getDbSql()->buildSqlString($select);
         $results = $this->getDbAdapter()->query($query, Adapter::QUERY_MODE_EXECUTE);
         unset($select);
         if ($results) {
             $resultArr = $results->toArray();
             foreach ($resultArr as $result) {
                 $userNames[$result['id']] = $result['fullName'];
             }
         }
     }
     // Get Company
     if (count($companyIds) && $item->getOption('loadCompany')) {
         $select = $this->getDbSql()->select(['cp' => \Company\Model\CompanyMapper::TABLE_NAME], ['id', 'name']);
         $select->where(['cp.id' => $companyIds]);
         $query = $this->getDbSql()->buildSqlString($select);
         $results = $this->getDbAdapter()->query($query, Adapter::QUERY_MODE_EXECUTE);
         unset($select);
         if ($results) {
             $resultArr = $results->toArray();
             foreach ($resultArr as $result) {
                 $companyNames[$result['id']] = $result['name'];
             }
         }
     }
     foreach ($paginator as $account) {
         $userId = $account->getCreatedById();
         $companyId = $account->getCompanyId();
         $itemId = $account->getId();
         $account->addOption('userName', isset($userNames[$userId]) ? $userNames[$userId] : null);
         $account->addOption('companyName', isset($companyNames[$companyId]) ? $companyNames[$companyId] : null);
     }
     return $paginator;
 }
 /**
  * @author KienNN
  * @param \Accounting\Model\Account $item
  * @param array $parentIds
  * @return NULL|Ambigous <multitype:unknown , NULL>
  */
 public function recusiveBranchChildIds($item, $parentIds)
 {
     if (!$parentIds || !count($parentIds)) {
         return null;
     }
     $result = [];
     foreach ($parentIds as $parentId) {
         $result[$parentId] = $parentId;
     }
     $select = $this->getDbSql()->select(['aa' => self::TABLE_NAME]);
     $select->where(['parentId' => $parentIds]);
     if ($item->getCompanyId()) {
         $select->where(['companyId' => $item->getCompanyId()]);
     }
     $query = $this->getDbSql()->buildSqlString($select);
     $rows = $this->getDbAdapter()->query($query, Adapter::QUERY_MODE_EXECUTE);
     $childIds = [];
     if ($rows->count()) {
         foreach ($rows as $row) {
             $childIds[$row['id']] = $row['id'];
         }
     }
     if (count($childIds)) {
         $result += $this->recusiveBranchChildIds($item, $childIds);
     }
     return $result;
 }
 public function suggestAction()
 {
     $q = $this->getRequest()->getPost('q');
     //$q = $this->params()->fromQuery('q');
     $account = new Account();
     $account->setName($q);
     $jsonModel = new JsonModel();
     if (!$q) {
         $jsonModel->setVariables(['code' => 1, 'data' => []]);
         return $jsonModel;
     }
     $accountMapper = $this->getServiceLocator()->get('Accounting\\Model\\AccountMapper');
     $jsonModel->setVariables(['code' => 1, 'data' => $accountMapper->suggest($account)]);
     return $jsonModel;
 }
 private function insertaccount($accounts, $companyId, $parentId = null)
 {
     if (!$accounts || !count($accounts)) {
         return null;
     }
     $accountMapper = $this->getServiceLocator()->get('\\Accounting\\Model\\AccountMapper');
     foreach ($accounts as $accountArray) {
         $account = new \Accounting\Model\Account();
         $account->setCode($accountArray['code']);
         $account->setName($accountArray['name']);
         $account->setParentId($parentId);
         $account->setCompanyId($companyId);
         if (!$accountMapper->isExisted($account)) {
             $account->setStatus(\Accounting\Model\Account::STATUS_ACTIVE);
             $account->setCreatedById($this->user()->getIdentity());
             $account->setCreatedDateTime(DateBase::getCurrentDateTime());
             $accountMapper->save($account);
         }
         if (isset($accountArray['childs']) && count($accountArray['childs'])) {
             $this->insertaccount($accountArray['childs'], $companyId, $account->getId());
         }
     }
 }
 public function deletecategoryAction()
 {
     $id = $this->params()->fromQuery('id');
     $account = new Account();
     $account->setId($id);
     $accountMapper = $this->getServiceLocator()->get('Accounting\\Model\\AccountMapper');
     if (!$accountMapper->get($account)) {
         return $this->page404();
     }
     $auth = $this->getServiceLocator()->get('Authorize\\Service\\Authorize');
     if ($account->getCreatedById() == $this->user()->getIdentity() || $auth->isAllowed('accounting:expense', 'delete')) {
         $accountMapper->delete($account);
         $jsonModel = new JsonModel();
         $jsonModel->setVariables(['code' => 1, 'messages' => ['Đã xóa thành công.']]);
         return $jsonModel;
     } else {
         $jsonModel = new JsonModel();
         $jsonModel->setVariables(['code' => 0, 'messages' => ['Bạn không có quyền xóa.']]);
         return $jsonModel;
     }
     return $this->redirect()->toUrl('/accounting');
 }