Prepare parameters. ini_set('memory_limit','512M'); $authData = \Utility\Registry::getAuthData(); $selection = array( 'valuation', 'stock', 'createdBy', 'company', 'type', 'model', 'make', 'vehicleYear', 'salesProfile', 'managerProfile', 'companyGroup', 'companyDivision', 'sendToStockFrom', 'dealNotDoneSelection' ); $join = array(); $leftJion = array(); $where = array(); $params = array(); $query = 'SELECT [SELECTION] ' . 'FROM \Valuation\Entity\Valuation valuation ' . 'JOIN valuation.stock stock ' . 'JOIN valuation.createdBy createdBy ' . 'JOIN stock.company company ' . 'JOIN stock.vehicleYear vehicleYear ' . 'JOIN stock.type type ' . 'JOIN type.model model ' . 'JOIN model.make make ' . 'LEFT JOIN valuation.salesProfile salesProfile ' . 'LEFT JOIN valuation.managerProfile managerProfile ' . 'LEFT JOIN company.group companyGroup ' . 'LEFT JOIN company.groupDivision companyDivision ' . 'LEFT JOIN valuation.sendToStockFrom sendToStockFrom ' . 'LEFT JOIN valuation.dealNotDoneSelection dealNotDoneSelection ' . '[WHERE] ' . 'ORDER BY valuation.created ASC'; #-> Construct details. $authData = \Utility\Registry::getAuthData(); if (isset($this->_input['dateFrom']) && !empty($this->_input['dateFrom'])) { $this->_queries['Date Range From'] = $this->_input['dateFrom']; $where[] = 'valuation.created >= :dateFrom'; $params['dateFrom'] = new \DateTime($this->_input['dateFrom']); } if (isset($this->_input['dateTo']) && !empty($this->_input['dateTo'])) { $this->_queries['Date Range Until'] = $this->_input['dateTo']; $where[] = 'valuation.created <= :dateTo'; $params['dateTo'] = new \DateTime($this->_input['dateTo'] . ' 23:59:59'); } #-> Construct details. $authData = \Utility\Registry::getAuthData(); switch (\Utility\Registry::getUserType()) { case 'B4C User': $groupFilter = \Utility\Registry::getSudo('Group', false); if ($groupFilter && 0 != $groupFilter) { $this->_queries['Group'] = \Utility\Registry::getSudo('GroupName', false); $join[] = 'JOIN stock.company company'; $where[] = 'IDENTITY(company.group) = :group'; $params['group'] = $groupFilter; } $divisionFilter = \Utility\Registry::getSudo('Division', false); if ($divisionFilter && 0 != $divisionFilter) { $this->_queries['Division'] = \Utility\Registry::getSudo('DivisionName', false); $join[] = 'JOIN stock.company company'; $where[] = 'IDENTITY(company.groupDivision) = :groupDivision'; $params['groupDivision'] = $divisionFilter; } $companyFilter = \Utility\Registry::getSudo('Company', false); if ($companyFilter && 0 != $companyFilter) { $this->_queries['Company'] = \Utility\Registry::getSudo('CompanyName', false); $where[] = 'IDENTITY(stock.company) = :company'; $params['company'] = $companyFilter; } break; case 'Group User': $join[] = 'JOIN stock.company company'; $where[] = 'IDENTITY(company.group) = :group'; $params['group'] = $authData['company']['group']['id']; $divisionFilter = \Utility\Registry::getSudo('Division', false); if ($divisionFilter && 0 != $divisionFilter) { $this->_queries['Division'] = \Utility\Registry::getSudo('DivisionName', false); $join[] = 'JOIN stock.company company'; $where[] = 'IDENTITY(company.groupDivision) = :groupDivision'; $params['groupDivision'] = $divisionFilter; } $companyFilter = \Utility\Registry::getSudo('Company', false); if ($companyFilter && 0 != $companyFilter) { $this->_queries['Company'] = \Utility\Registry::getSudo('CompanyName', false); $where[] = 'IDENTITY(stock.company) = :company'; $params['company'] = $companyFilter; } break; case 'Dealer Principle': $join[] = 'JOIN stock.company company'; $where[] = 'IDENTITY(company.groupDivision) = :groupDivision'; $params['groupDivision'] = $authData['company']['groupDivision']['id']; $companyFilter = \Utility\Registry::getSudo('Company', false); if ($companyFilter && 0 != $companyFilter) { $this->_queries['Company'] = \Utility\Registry::getSudo('CompanyName', false); $where[] = 'IDENTITY(stock.company) = :company'; $params['company'] = $companyFilter; } break; default: $where[] = 'IDENTITY(stock.company) = :company'; $params['company'] = $authData['company']['id']; break; } #-> Finalize query. $query = str_replace(array( '[SELECTION]', '[WHERE]' ), array( implode(', ', $selection) . ' ', !empty($where) ? 'WHERE ' . implode(' AND ', $where) . ' ' : ' ' ), $query); #-> Collect data. $query = $this->em->createQuery($query); !empty($params) && $query->setParameters($params); $this->_data = $query->getScalarResult(); $this->em->clear(); foreach ($this->_data as $rowId => $rowData) { $this->_data[$rowId]['createdBy_fullName'] = $rowData['createdBy_firstName'] . ' ' . $rowData['createdBy_familyName']; $this->_data[$rowId]['sales_person'] = isset($rowData['salesProfile_firstName']) && !empty($rowData['salesProfile_firstName']) ? $rowData['salesProfile_firstName'] . ' ' . $rowData['salesProfile_familyName'] : ''; $this->_data[$rowId]['manager'] = isset($rowData['managerProfile_firstName']) && !empty($rowData['managerProfile_firstName']) ? $rowData['managerProfile_firstName'] . ' ' . $rowData['managerProfile_familyName'] : ''; if ($this->_data[$rowId]['stock_tradePrice'] == 0) { $this->_data[$rowId]['percentage_trade'] = '0 %'; } else { $this->_data[$rowId]['percentage_trade'] = round(($this->_data[$rowId]['valuation_amountOffered'] * 100) / $this->_data[$rowId]['stock_tradePrice']) . ' %' ; } } } }