_tribeId = $tribeId; return $this; } public function getTribeId() { return $this->_tribeId; } protected function _getFromColumns() { $columns = parent::_getFromColumns(); $columns['isBlacklisted'] = (new BlacklistObj())->getIsVendorBlacklistedExpr('v', $this->_tribeId); return $columns; } public function getListSelect() { if (null !== $this->_select) { return $this->_select; } $select = parent::getListSelect(); $select->join($this->_getPair('Vendor', 'v'), '`v`.`id` = `l`.`vendorId`', ['vendorName' => 'v.dbaName']); if ($this->_tribeId) { $this->_joinLicenseTribeData($select); } return $select; } protected function _filter(Zend_Db_Select $select) { foreach ($this->getFilter() as $field => $value) { if (empty($value)) { continue; } switch ($field) { case 'submittedOnStart': $select->where('`l`.`added` >= ?', $value); break; case 'submittedOnEnd': $select->where('`l`.`added` <= ?', $value); break; case 'expiredOnStart': $select->where('`l`.`expiredOn` >= ?', $value); break; case 'expiredOnEnd': $select->where('`l`.`expiredOn` <= ?', $value); break; case 'status': unset($this->_filter['status']); $this->_filterByStatus($select, $value); break; } } parent::_filter($select); return $this; } protected function _filterByStatus(Zend_Db_Select $select, $status, $licenseTableAlias = 'l') { if ($this->_tribeId) { $select->where($this->getModel()->getLicenseStatus4TribeExpr() . ' = ?', $status); } else { if ($status == Entity::STATUS_EXPIRED) { $select->where("`{$licenseTableAlias}`.`status` = ?", $status); } else { $subSelect = $this->_db->select(); $subSelect->from($this->_getPair('LicenseTribe', 'lict'), [new Zend_Db_Expr('1')]); $subSelect->where("`{$licenseTableAlias}`.`id` = `lict`.`licenseId`", $status); $subSelect->where("`lict`.`status` = ?", $status); $select->where('EXISTS (' . $subSelect . ')'); } } return $this; } protected function _joinLicenseTribeData(Zend_Db_Select $select, $tableAlias = 'l') { $select->joinLeft( $this->_getPair('LicenseTribe', 'lt'), '`lt`.`licenseId` = `' . $tableAlias . '`.`id`', [ 'tribeStatus' => $this->getModel()->getLicenseStatus4TribeExpr(), 'tribeSurcharge' => 'surcharge' ] ); $select->where("`lt`.`tribeId` = ?", $this->_tribeId); return $this; } protected function _prepareList(&$list) { if (!($ids = array_column($list, 'id'))) { return $this; } $this->_prepareListNaicsCodes($list, $ids); $this->_prepareListTribes($list, $ids); return parent::_prepareList($list); } /** * @param array $list * @param array $ids * @return void * @throws \Qs_Exception */ private function _prepareListNaicsCodes(array &$list, array $ids) { $select = $this->_db->select(); $select->from($this->_getPair('Naics', 'n'), ['ln.licenseId', '*']); $select->join($this->_getPair('LicenseNaics', 'ln'), '`ln`.`naicsCode` = `n`.`id`', []); $select->where('`ln`.`licenseId` IN (?)', $ids, Qs_Db::INT_TYPE); $select->order(['licenseId', NaicsModel::getOrderByCodeExpr('n')]); $map = $this->_db->fetchAll($select, [], Qs_Db::FETCH_GROUP | Qs_Db::FETCH_ASSOC); foreach ($list as &$row) { $row['naicsCodes'] = Qs_Array::get($map, $row['id'], []); } } private function _prepareListTribes(array &$list, array $ids) { $select = $this->_db->select(); $select->from( $this->_getPair('LicenseTribe', 'lt'), ['licenseId', 'tribeId', 'status', 'surcharge'] ); $select->join( $this->_getPair('Tribe', 't'), '`t`.`id` = `lt`.`tribeId`', ['tribeTitle' => 'title'] ); $select->where('`lt`.`licenseId` IN (?)', $ids, Qs_Db::INT_TYPE); $tribes = $this->_db->fetchAll($select, [], Qs_Db::FETCH_GROUP | Qs_Db::FETCH_ASSOC); foreach ($list as &$row) { $row['tribes'] = Qs_Array::get($tribes, $row['id'], []); } return $this; } protected function _afterDeleteSuccess() { (new PdfModel())->removeFiles($this->getPrimaryKey()); return parent::_afterDeleteSuccess(); } /** * @return void */ public function disable() { $this->getModel()->disable($this->getPrimaryKey()); } /** * @return void */ public function renew() { $this->getModel()->renew($this->getPrimaryKey()); } /** * @param array $tribeIds * @return void */ public function approve(array $tribeIds) { $this->getModel()->approve($this->getPrimaryKey(), $tribeIds); } public function pending(array $tribeIds) { $this->getModel()->pending($this->getPrimaryKey(), $tribeIds); } public function approvePending(array $tribeIds) { $this->getModel()->approvePending($this->getPrimaryKey(), $tribeIds); } public function updateTribeSurcharge(array $tribeIds, $surchargeData) { $this->getModel()->updateTribeLicenseSurcharge($this->getPrimaryKey(), $tribeIds, $surchargeData); } public function decline(array $tribeIds) { $this->getModel()->decline($this->getPrimaryKey(), $tribeIds); } public function initFromForm(array $data) { if (empty($data['id'])) { // Set active status if admin creates a new licence $data['status'] = Entity::STATUS_ACTIVE; $this->_prepareTribesData($data); } if (isset($data['status']) && $data['status'] == Entity::STATUS_ACTIVE) { // Set activeExpiredOn date only for approved license $data['activeExpiredOn'] = $data['expiredOn']; } if (($vendor = $this->_getTable('Vendor')->search($data['vendorId']))) { $payment = $this->getPaymentSummary('y' == $vendor['preferredVendor']); $data = array_merge($data, $payment); } $this->_prepareTribesSurchargeData($data); return parent::initFromForm($data); } protected function _prepareTribesData(&$data) { $tribeIds = array_keys(TribeObj::getPairs()); foreach ($tribeIds as $tribeId) { $data['tribes'][$tribeId] = [ 'tribeId' => $tribeId, 'status' => Entity::TRIBE_STATUS_PENDING, ]; } return $this; } protected function _prepareTribesSurchargeData(&$data) { foreach($data as $key => $value) { if (0 === stripos($key, 'surcharge_')) { $tribeId = (int) str_replace('surcharge_', '', $key); $value = filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); $data['surcharge'][$tribeId] = ($value) ? $value : null; } } return $this; } public function insert(array $data = []) { $data = empty($data) ? $this->_data : $data; $this->_primaryKey = $this->getModel()->insert($data); $this->_handleFiles(); return $this->_primaryKey; } public function update(array $data = []) { $data = empty($data) ? $this->_data : $data; $result = $this->getModel()->update($data, $this->_primaryKey); $this->_handleFiles(); return $result; } public function getList4ExportStatement() { $select = clone $this->getListSelect(); $select->columns(['naics' => $this->getNaicsListExpr()]); return $select->query(); } private function getNaicsListExpr() { $select = $this->_db->select(); $select->from($this->_getPair('LicenseNaics', 'ln'), ['GROUP_CONCAT(`ln`.`naicsCode` SEPARATOR ", ")']); $select->where('`ln`.`licenseId` = `l`.`id`'); $select->order(NaicsModel::getOrderByCodeExpr('ln', 'naicsCode')); return new Zend_Db_Expr('(' . $select . ')'); } public function getExportCsvColumns() { return $this->getConfigArray('csvExportColumns'); } /** * @param array $row * @return void */ public function prepareExportRow(array &$row) { $this->getModel()->mapCollections($row); } protected function _applySelectOptions(Zend_Db_Select $select, array $options = []) { // Add table alias to avoid 'Column 'XXX' in order clause is ambiguous' exception if (($order = Qs_Array::get($options, 'order'))) { $field = false !== strpos($order, 'DESC') ? substr($order, 0, -5) : $order; if (in_array($field, ['id', 'added', 'expiredOn', 'status', 'total'])) { $options['order'] = $this->_tableShortAlias . '.' . $order; } } return parent::_applySelectOptions($select, $options); } public function getData4Pdf($tribeId = null) { $data = $this->getModel()->get($this->getPrimaryKey()); $data['vendor'] = $this->getVendorModel()->get($data['vendorId']); $data['tribes'] = $this->getModel()->getLicenseTribes($this->getPrimaryKey(), true); if ($tribeId) { $data['tribeStatus'] = $this->getModel()->getLicenseTribeStatus($this->getPrimaryKey(), $tribeId); $data['tribeStatusTitle'] = $this->getModel()->getLicenseStatusTitle4Tribe($data['tribeStatus']); } $yesNoOptions = $this->getConfigArray('yesNoOptions'); foreach ($data['vendor']['questionnaire'] as &$question) { $question['answerTitle'] = Qs_Array::get($yesNoOptions, $question['answer']); } return $data; } }