'']; protected $_checkForeignKeyDependencies = true; protected $_foreignKeyLocksMessages = [ 'Licenses' => 'This Vendor is linked with License(s) and can\'t be removed.', 'Users' => 'This Vendor is linked with User(s) and can\'t be removed.', 'Librarys' => 'This Vendor is linked with Library File(s) and can\'t be removed.', 'Taxations' => 'This Vendor is linked with Taxation(s) and can\'t be removed.', ]; protected function _init() { $this->_filterFields['soleProprietorName'] = $this->getModel()->getSoleProprietorNameExpr(); parent::_init(); } protected function _getFromColumns() { $columns = parent::_getFromColumns(); $columns['soleProprietorName'] = $this->getModel()->getSoleProprietorNameExpr(); $columns['licensesCount'] = $this->getLicensesCountExpr(); $columns['isBlacklisted'] = (new BlacklistObj())->getIsVendorBlacklistedExpr('v', AclPermission::getAdminTribeId()); return $columns; } private function getLicensesCountExpr() { $select = $this->_db->select(); $select->from($this->_getPair('License', 'l'), 'COUNT(*)'); $select->where('`l`.`vendorId` = `v`.`id`'); return new Zend_Db_Expr('(' . $select . ')'); } public function insert(array $data = []) { $data = empty($data) ? $this->_data : $data; $this->_primaryKey = $this->getModel()->insert($data); $this->_handleFiles(); return $this->_primaryKey; } public function getList4ExportStatement() { $select = clone $this->getListSelect(); $this->joinAddresses4CsvExport($select); return $select->query(); } private function joinAddresses4CsvExport(Zend_Db_Select $select) { $columns = []; foreach ([AddressEntity::TYPE_BILLING, AddressEntity::TYPE_MAILING] as $type) { foreach (['address', 'address2', 'city', 'state', 'zip'] as $field) { $key = $type . 'Address' . ucfirst($field); $columns[$key] = "{$type}.{$field}"; if ($field == 'state') { $columns[$key . 'Name'] = "{$type}State.title"; } } $select->joinLeft( $this->_getPair('VendorAddress', $type), "`{$type}`.`vendorId` = `v`.`id` AND `{$type}`.`type` = " . $this->_db->quote($type), [] ); $select->joinLeft( $this->_getPair('DState', "{$type}State"), "`{$type}State`.`id` = `{$type}`.`state`", [] ); } $select->columns($columns); } public function getExportCsvColumns() { $columns = $this->getConfigArray('csvExportColumns'); $this->injectCsvPartnerColumns($columns); $this->injectCsvBusinessReferenceColumns($columns); $this->injectCsvQuestionnaireColumns($columns); return $columns; } /** * @param array $columns * @return void */ protected function injectCsvPartnerColumns(array &$columns) { $partnersColumns = []; $maxPartners = $this->getCsvExportMaxPartners(); for ($number = 1; $number <= $maxPartners; $number++) { foreach ($columns['partners'] as $field => $title) { $partnersColumns['partner' . $number . ucfirst($field)] = str_replace('#', '#' . $number, $title); } } Qs_Array::mergeAssocAfter($columns, $partnersColumns, 'partners'); unset($columns['partners']); } /** * @param array $columns * @return void */ protected function injectCsvBusinessReferenceColumns(array &$columns) { $businessReferencesColumns = []; $maxBusinessReferences = $this->getCsvExportMaxBusinessReferences(); for ($number = 1; $number <= $maxBusinessReferences; $number++) { foreach ($columns['businessReferences'] as $field => $title) { $businessReferencesColumns['businessReference' . $number . ucfirst($field)] = str_replace('#', '#' . $number, $title); } } Qs_Array::mergeAssocAfter($columns, $businessReferencesColumns, 'businessReferences'); unset($columns['businessReferences']); } /** * @param array $columns * @return void */ protected function injectCsvQuestionnaireColumns(array &$columns) { $newColumns = []; foreach ($this->getQuestions() as $question) { $newColumns['question' . $question['id'] . 'answer'] = $question['question']; $newColumns['question' . $question['id'] . 'explanation'] = 'Explanation'; } Qs_Array::mergeAssocAfter($columns, $newColumns, 'questionnaire'); unset($columns['questionnaire']); } protected function getCsvExportMaxPartners() { return $this->getCsvExportMaxRelations('VendorPartner'); } protected function getCsvExportMaxBusinessReferences() { return $this->getCsvExportMaxRelations('VendorBusinessReference'); } protected function getCsvExportMaxRelations($tableAlias) { $select = clone $this->getListSelect(); $select->join($this->_getPair($tableAlias, 'vr'), '`vr`.`vendorId` = `v`.`id`'); $select->reset(Zend_Db_Select::COLUMNS)->columns(['v.id', 'cnt' => 'COUNT(*)']); $select->group('v.id'); $select->order('cnt DESC'); $select->limit(1); if (($row = $this->_db->fetchRow($select))) { return $row['cnt']; } return 0; } /** * @param array $row * @return void * @throws \Qs_Exception */ public function prepareExportRow(array &$row) { $this->getModel()->mapCollections($row); $row['applicationFile'] = Qs_FileFs::getUrl($row['applicationFile']); $this->prepareExportRowPartners($row); $this->prepareExportRowBusinessReferences($row); $this->prepareExportRowQuestions($row); } /** * @param array $row * @return void */ private function prepareExportRowPartners(array &$row) { $partners = $this->getModel()->getPartners($row['id']); $number = 1; foreach ($partners as $partner) { foreach (array_keys($this->getConfigArray('csvExportColumns')['partners']) as $field) { $row['partner' . $number . ucfirst($field)] = $partner[$field]; } $number++; } } /** * @param array $row * @return void */ private function prepareExportRowBusinessReferences(array &$row) { $businessReferences = $this->getModel()->getBusinessReferences($row['id']); $number = 1; foreach ($businessReferences as $businessReference) { foreach (array_keys($this->getConfigArray('csvExportColumns')['businessReferences']) as $field) { $row['businessReference' . $number . ucfirst($field)] = $businessReference[$field]; } $number++; } } /** * @param array $row * @return void * @throws \Qs_Exception */ private function prepareExportRowQuestions(array &$row) { $questions = $this->getModel()->getQuestionnaire($row['id']); $options = $this->getConfigArray('yesNoOptions'); foreach ($questions as $question) { $row['question' . $question['id'] . 'answer'] = Qs_Array::get($options, $question['answer']); $row['question' . $question['id'] . 'explanation'] = $question['explanation']; } } }