/**
  *
  * @author Hungpx
  * @param \Accounting\Model\ExpenseCategory $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() . '%']);
     }
     $select->order(['c.companyId' => 'DESC']);
     $query = $this->getDbSql()->buildSqlString($select);
     $results = $this->getDbAdapter()->query($query, Adapter::QUERY_MODE_EXECUTE);
     $paginator = [];
     $userIds = [];
     $companyIds = [];
     $ids = [];
     if ($results->count()) {
         foreach ($results as $row) {
             $row = (array) $row;
             $category = new ExpenseCategory();
             $category->exchangeArray($row);
             $paginator[] = $category;
             if ($category->getCreatedById()) {
                 $userIds[$category->getCreatedById()] = $category->getCreatedById();
             }
             if ($category->getCompanyId()) {
                 $companyIds[$category->getCompanyId()] = $category->getCompanyId();
             }
             $ids[$category->getId()] = $category->getId();
         }
     }
     if ($item->getOption('fetchOnlyIds')) {
         return $ids;
     }
     $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 $category) {
         $userId = $category->getCreatedById();
         $companyId = $category->getCompanyId();
         $itemId = $category->getId();
         $category->addOption('userName', isset($userNames[$userId]) ? $userNames[$userId] : null);
         $category->addOption('companyName', isset($companyNames[$companyId]) ? $companyNames[$companyId] : null);
     }
     return $paginator;
 }
 public function loadcategoriesAction()
 {
     $companyId = $this->getRequest()->getPost('companyId');
     if (!$companyId) {
         $companyId = $this->user()->getCompanyId();
     } else {
         if (!$this->company()->canManage($companyId)) {
             return $this->getJsonModel()->setVariables(array('code' => 0, 'messages' => ['Bạn không được quyền quản lí doanh nghiệp này']));
         }
     }
     $expenseCategory = new \Accounting\Model\ExpenseCategory();
     $expenseCategory->setCompanyId($companyId);
     $expenseCategoryMapper = $this->getServiceLocator()->get('\\Accounting\\Model\\ExpenseCategoryMapper');
     $tree = new \Home\Model\Tree();
     $categoies = $tree->toArrayRecusived($expenseCategoryMapper->fetchAll($expenseCategory));
     $data = [];
     if ($categoies) {
         foreach ($categoies as $expenseCategory) {
             $data[] = array('id' => $expenseCategory->getId(), 'name' => $expenseCategory->getName(), 'code' => $expenseCategory->getCode(), 'ord' => $expenseCategory->getOption('ord'), 'displayName' => str_repeat('--', $expenseCategory->getOption('ord') ?: 0) . $expenseCategory->getName());
         }
     }
     return $this->getJsonModel()->setVariables(array('code' => 1, 'data' => $data));
 }
 /**
  * @author Hungpx
  * @param \Accounting\Model\ExpenseCategory $item
  * @param array $parentIds
  * @return NULL|Ambigous <multitype:unknown , NULL>
  */
 public function suggest($item)
 {
     $dbAdapter = $this->getDbAdapter();
     $item->prepairSuggest();
     $select = $this->getDbSql()->select(self::TABLE_NAME);
     $select->where(['(name LIKE ?)' => ['%' . $item->getName() . '%']]);
     $query = $this->getDbSql()->buildSqlString($select);
     $results = $dbAdapter->query($query, $dbAdapter::QUERY_MODE_EXECUTE);
     $result = array();
     if ($results->count()) {
         $row = array();
         foreach ($results as $value) {
             $row['id'] = $value['id'];
             $row['name'] = $value['name'];
             $row['label'] = $value['name'];
             $result[] = $row;
         }
     }
     return $result;
 }
 public function suggestAction()
 {
     $q = $this->getRequest()->getPost('q');
     //$q = $this->params()->fromQuery('q');
     $expenseCategory = new ExpenseCategory();
     $expenseCategory->setName($q);
     $expenseCategory->setCompanyId($this->getRequest()->getPost('companyId'));
     $jsonModel = new JsonModel();
     if (!$q) {
         $jsonModel->setVariables(['code' => 1, 'data' => []]);
         return $jsonModel;
     }
     $expenseCategoryMapper = $this->getServiceLocator()->get('Accounting\\Model\\ExpenseCategoryMapper');
     $jsonModel->setVariables(['code' => 1, 'data' => $expenseCategoryMapper->suggest($expenseCategory)]);
     return $jsonModel;
 }