getTable()->emptyToNull($data); return $this; } protected function saveDependencies(array $data) { foreach (['naicsCodes', 'tribes', 'surcharge'] as $name) { $method = 'save' . ucfirst($name); if (array_key_exists($name, $data) && method_exists($this, $method)) { $this->$method($data[$name], $data['id']); } } $this->removePdfFiles($data['id']); return $this; } /** * @param $licenseId * @return void */ private function removePdfFiles($licenseId) { (new PdfModel())->removeFiles($licenseId); } /** @noinspection PhpUnusedPrivateMethodInspection */ /** * @param array $codes * @param $licenseId * @return $this */ private function saveNaicsCodes(array $codes, $licenseId) { $this->getTable('LicenseNaics')->saveRelationIds($codes, 'naicsCode', compact('licenseId')); return $this; } /** @noinspection PhpUnusedPrivateMethodInspection */ private function saveTribes($tribes, $licenseId) { $this->getTable('LicenseTribe')->saveRelations($tribes, compact('licenseId')); return $this; } /** @noinspection PhpUnusedPrivateMethodInspection */ private function saveSurcharge($surcharge, $licenseId) { $this->updateTribeLicenseSurcharge($licenseId, array_keys($surcharge), $surcharge); return $this; } public function getLicenseNaics($licenseId) { $select = $this->db->select(); $select->from(Qs_Db::getPair('LicenseNaics', 'ln'), []); $select->join(Qs_Db::getPair('Naics', 'n'), '`ln`.`naicsCode` = `n`.`id`', ['code' => 'id', 'title']); $select->where('`ln`.`licenseId` = ?', $licenseId, Qs_Db::INT_TYPE); $select->order(NaicsModel::getOrderByCodeExpr('n')); return $this->db->fetchAll($select); } public function getLicenseTribes($licenseId, $forViewItem = false) { $select = $this->db->select(); $columns = ['tribeId', 'status']; if ($forViewItem) { $columns[] = 'surcharge'; } $select->from(Qs_Db::getPair('LicenseTribe', 'lt'), $columns); if ($forViewItem) { $select->join( Qs_Db::getPair('Tribe', 't'), '`t`.`id` = `lt`.`tribeId`', ['tribeTitle' => 'title', 'useSurcharge'] ); } $select->where('`lt`.`licenseId` = ?', $licenseId, Qs_Db::INT_TYPE); $tribes = $this->db->fetchAll($select); if ($forViewItem) { $statuses = $this->getConfigArray('tribeStatuses'); foreach ($tribes as &$tribe) { $tribe['statusTitle'] = $statuses[$tribe['status']]; } unset($tribe); } return $tribes; } public function getLicenseTribeStatus($licenseId, $tribeId) { if (!($license = $this->getTable()->search($licenseId))) { throw new RuntimeException('License #' . $licenseId . ' is not found'); } if ($license['status'] == Entity::STATUS_EXPIRED) { return Entity::STATUS_EXPIRED; } else { return $this->getTable('LicenseTribe')->searchBy( ['licenseId' => $licenseId, 'tribeId' => $tribeId], 'status'); } } public function disable($licenseId) { $expiredOn = date('Y-m-d'); $result = $this->getTable('License')->updateByKey([ 'status' => Entity::STATUS_EXPIRED, 'activeExpiredOn' => $expiredOn, 'expiredOn' => $expiredOn, ], $licenseId); $this->removePdfFiles($licenseId); return $result; } public function renew($licenseId) { if (!($license = $this->getTable()->search($licenseId))) { throw new RuntimeException('License #' . $licenseId . ' is not found'); } if (!($date = Qs_Array::get($license, 'activeExpiredOn'))) { $date = Qs_Array::get($license, 'added'); } $time = strtotime($date); $months = App_Settings_Obj::get('licenseDuration'); $newExpiredOn = date('Y-m-d', strtotime('+' . $months . ' months', $time)); $result = $this->getTable('License')->updateByKey([ 'status' => Entity::STATUS_ACTIVE, 'activeExpiredOn' => $newExpiredOn, 'expiredOn' => $newExpiredOn, ], $licenseId); $this->removePdfFiles($licenseId); return $result; } public function updateTribeLicenseSurcharge($licenseId, array $tribeIds, $surchargeData = []) { if (!($license = $this->getTable()->search($licenseId))) { throw new RuntimeException('License #' . $licenseId . ' is not found'); } if (!$tribeIds) { throw new RuntimeException('License Tribes is empty'); } $tribeLicenses = $this->getTable('LicenseTribe')->fetchAll(['`licenseId` = ?' => $licenseId])->toArray(); $tribeLicensesIds = array_column($tribeLicenses, 'tribeId'); if ($unknownTribesIds = array_diff($tribeIds, $tribeLicensesIds)) { throw new RuntimeException('Tribe(s) #' . implode(', #', $unknownTribesIds) . ' is(are) not exists in License #' . $licenseId); } $result = false; foreach ($tribeIds as $tribeId) { $surcharge = (isset($surchargeData[$tribeId]) && $surchargeData[$tribeId]) ? $surchargeData[$tribeId] : null; $result = $this->getTable('LicenseTribe')->updateByKey(['surcharge' => $surcharge], [ 'licenseId' => $licenseId, 'tribeId' => $tribeId, ]); } return $result; } public function approve($licenseId, array $tribeIds) { return $this->_setTribeLicenseStatus($licenseId, $tribeIds, Entity::TRIBE_STATUS_APPROVED); } public function pending($licenseId, array $tribeIds) { return $this->_setTribeLicenseStatus($licenseId, $tribeIds, Entity::TRIBE_STATUS_PENDING); } public function approvePending($licenseId, array $tribeIds) { return $this->_setTribeLicenseStatus($licenseId, $tribeIds, Entity::TRIBE_STATUS_APPROVED_PENDING); } public function decline($licenseId, array $tribeIds) { return $this->_setTribeLicenseStatus($licenseId, $tribeIds, Entity::TRIBE_STATUS_DECLINED); } protected function _setTribeLicenseStatus($licenseId, array $tribeIds, $status) { if (!($license = $this->getTable()->search($licenseId))) { throw new RuntimeException('License #' . $licenseId . ' is not found'); } if (!$tribeIds) { throw new RuntimeException('License Tribes is empty'); } $tribeLicenses = $this->getTable('LicenseTribe')->fetchAll(['`licenseId` = ?' => $licenseId])->toArray(); $tribeLicensesIds = array_column($tribeLicenses, 'tribeId'); if ($unknownTribesIds = array_diff($tribeIds, $tribeLicensesIds)) { throw new RuntimeException('Tribe(s) #' . implode(', #', $unknownTribesIds) . ' is(are) not exists in License #' . $licenseId); } $result = false; $today = date('Y-m-d'); foreach ($tribeIds as $tribeId) { $key = [ 'licenseId' => $licenseId, 'tribeId' => $tribeId, ]; if (!($tribeLicense = $this->getTable('LicenseTribe')->searchBy($key))) { throw new Exception('Can not find tribe license'); } $data = compact('status'); if ($status == Entity::TRIBE_STATUS_PENDING) { $data['surcharge'] = null; $data['surchargeExpiredOn'] = null; $data['surchargePaid'] = null; } else { if (!empty($tribeLicense['surchargeExpiredOn'])) { $data['surchargeExpiredOn'] = $today; } } $result = $this->getTable('LicenseTribe')->updateBy($data, $key); } $this->removePdfFiles($licenseId); return $result; } /** * @param array $data * @return void * @throws \Qs_Exception */ public function mapCollections(array &$data) { $dropdownMap = [ 'statusTitle' => ['field' => 'status', 'collection' => 'statuses'], 'tribeStatusTitle' => ['field' => 'tribeStatus', 'collection' => 'tribeStatuses'], 'paymentMethodTitle' => ['field' => 'paymentMethod', 'collection' => 'paymentMethods'], 'checkTypeTitle' => ['field' => 'checkType', 'collection' => 'checkTypes'], ]; foreach ($dropdownMap as $field => $options) { if (($value = Qs_Array::get($data, $options['field']))) { $data[$field] = $this->getConfigArray($options['collection'])[$value]; } } } public function joinTribe(Zend_Db_Select $select, $tribeId) { $select->join( Qs_Db::getPair('LicenseTribe', 'lt'), '`lt`.`licenseId` = `l`.`id` AND `lt`.`tribeId` = ' . $this->db->quote($tribeId, Qs_Db::INT_TYPE), [ 'tribeId', 'tribeStatus' => 'status', 'tribeSurcharge' => 'surcharge', 'tribeSurchargePaid' => 'surchargePaid', 'tribeSurchargeExpiredOn' => 'surchargeExpiredOn', 'tribePaymentId' => 'paymentId', 'tribeAdded' => 'added', ] ); $select->join(Qs_Db::getPair('Tribe', 't'), '`t`.`id` = `lt`.`tribeId`', [ 'tribeTitle' => 'title', 'tribeLogo' => 'logo', 'tribeSignature' => 'signature', 'tribeSignatureTitle' => 'signatureTitle', ]); return $this; } public function find(array $key) { if (($tribeId = $this->getFilter('tribeId'))) { $select = Qs_Db::getSelect()->from(Qs_Db::getPair('License', 'l')); $this->joinTribe($select, $tribeId); Qs_Db::filter($select, $key, 'l', Qs_Db::FILTER_THROW_UNKNOWN); return $this->db->fetchRow($select); } return parent::find($key); } public function get($id) { if (($data = $this->find(compact('id')))) { $this->mapCollections($data); $data['naicsCodes'] = $this->getLicenseNaics($id); $data['tribes'] = $this->getLicenseTribes($id); } return $data; } public function hasApprovedLicense($vendorId) { $select = $this->getTable()->getAdapter()->select(); $select->from(Qs_Db::getPair('License', 'l'), new Zend_Db_Expr('1')); $select->join(Qs_Db::getPair('LicenseTribe', 'lt'), '`l`.`id` = `lt`.`licenseId`', []); $select->where('`l`.`vendorId` = ?', $vendorId, Qs_Db::INT_TYPE); $select->where('`l`.`status` = ?', Entity::STATUS_ACTIVE); $select->where('`lt`.`status` = ?', Entity::TRIBE_STATUS_APPROVED); $select->limit(1); return '1' === $this->getTable()->getAdapter()->fetchOne($select); } public function getLicenseStatus4TribeExpr($licenseAlias = 'l', $licenseTribeAlias = 'lt') { return new Zend_Db_Expr( "IF (`{$licenseAlias}`.`status` = '" . Entity::STATUS_ACTIVE . "', `{$licenseTribeAlias}`.`status`, '" . Entity::STATUS_EXPIRED . "')" ); } public function getLicenseStatusTitle4Tribe($status) { $statuses = $this->getConfigArray('tribeStatuses') + [Entity::STATUS_EXPIRED => 'Expired']; return Qs_Array::get($statuses, $status); } public function getTribeUseSurcharge($tribeId = null) { if ($tribeId) { return $this->getTable('Tribe')->search($tribeId, 'useSurcharge'); } else { return $this->getTable('Tribe')->get4Select(['id', 'useSurcharge']); } } public function getUserLicenceTypeTitleExpr($fileType, $licenseAlias = 'l', $tribeAlias = 't') { $titles = $this->getConfigArray('userLicenseTypeTitle'); $title = $this->db->quote($titles[$fileType]); switch ($fileType) { case Entity::USER_TYPE_LICENSE: $expr = "REPLACE({$title}, '{id}', `{$licenseAlias}`.`id`)"; // license id break; case Entity::USER_TYPE_PREMIUM: $expr = "REPLACE({$title}, '{id}', `{$licenseAlias}`.`id`)"; // license id $expr = "REPLACE({$expr}, '{tribeTitle}', `{$tribeAlias}`.`title`)"; // tribe title break; default: throw new Exception('Unknown library file type'); } return new Zend_Db_Expr($expr); } public function getPremiumLicenseApprovedDateExpr($licenseAlias = 'l', $licenseTribeAlias = 'lt') { return new Zend_Db_Expr( "IF (`{$licenseAlias}`.`status` = '" . Entity::STATUS_ACTIVE . "'" . " && `{$licenseTribeAlias}`.`status` = '" . Entity::TRIBE_STATUS_APPROVED . "'" . ", IF (`{$licenseTribeAlias}`.`surcharge` IS NOT NULL" . ", `{$licenseTribeAlias}`.`surchargePaid`" . ", `{$licenseAlias}`.`added`)" . ", null)" ); } public function getPremiumLicenseExpiredOnDateExpr($licenseAlias = 'l', $licenseTribeAlias = 'lt') { return new Zend_Db_Expr( "IF (`{$licenseAlias}`.`status` = '" . Entity::STATUS_ACTIVE . "'" . " && `{$licenseTribeAlias}`.`status` = '" . Entity::TRIBE_STATUS_APPROVED . "'" . ", IF (`{$licenseTribeAlias}`.`surcharge` IS NOT NULL" . ", `{$licenseTribeAlias}`.`surchargeExpiredOn`" . ", `{$licenseAlias}`.`expiredOn`)" . ", null)" ); } public function getPremiumLicenseTotalTitleExpr($licenseTribeAlias = 'lt') { return new Zend_Db_Expr( "IF (`{$licenseTribeAlias}`.`status` = '" . Entity::TRIBE_STATUS_APPROVED . "'" . " && `{$licenseTribeAlias}`.`surcharge` IS NULL, 0, `{$licenseTribeAlias}`.`surcharge`)" ); } }