addSpecificVendorsToQueue($libraryItemData['id'], $libraryItemData['vendors']); break; case LibraryObj::SEND_TO_VENDORS_WITH_NAICS: $this->addNaicsVendorsToQueue($libraryItemData['id'], $libraryItemData['naicsCodes']); break; case LibraryObj::SEND_TO_ALL_APPROVED_VENDORS: $this->addAllApprovedVendorsToQueue($libraryItemData['id']); break; case LibraryObj::SEND_TO_ALL_VENDORS: $this->addAllVendorsToQueue($libraryItemData['id']); break; default: throw new Exception('Invalid library Send To option: ' . $libraryItemData['sendTo']); } return $this; } public function addSpecificVendorsToQueue($libraryId, $vendors) { if (empty($vendors)) { throw new Exception('vendors is undefined'); } $this->addVendorsToQueue($libraryId, $vendors); return $this; } public function addAllVendorsToQueue($libraryId) { $this->addVendorsToQueue($libraryId, []); return $this; } public function addNaicsVendorsToQueue($libraryId, $naicsCodes) { if (empty($naicsCodes)) { throw new Exception('naicsCodes is undefined'); } $select = $this->_db->select(); $select->from($this->_getPair('LicenseNaics', 'licn'), [ 'vendorId' => new Zend_Db_Expr('lic.vendorId'), 'libraryId' => new Zend_Db_Expr($libraryId), 'status' => new Zend_Db_Expr($this->_db->quote(self::STATUS_NEW)), 'added' => new Zend_Db_Expr($this->_db->quote($added = date('Y-m-d H:i:s'))), 'changed' => new Zend_Db_Expr($this->_db->quote($added)), ]); $select->joinLeft( $this->_getPair('License', 'lic'), '`lic`.`id` = `licn`.`licenseId`', [] ); $select->where("`licn`.`naicsCode` IN (?)", $naicsCodes); $select->group('lic.vendorId'); $this->_insertToQueue($select); return $this; } public function addAllApprovedVendorsToQueue($libraryId) { $select = $this->_db->select(); $select->from($this->_getPair('License', 'lic'), [ 'vendorId' => new Zend_Db_Expr('lic.vendorId'), 'libraryId' => new Zend_Db_Expr($libraryId), 'status' => new Zend_Db_Expr($this->_db->quote(self::STATUS_NEW)), 'added' => new Zend_Db_Expr($this->_db->quote($added = date('Y-m-d H:i:s'))), 'changed' => new Zend_Db_Expr($this->_db->quote($added)), ]); $select->joinLeft( $this->_getPair('LicenseTribe', 'lict'), '`lic`.`id` = `lict`.`licenseId`', [] ); $select->where("`lict`.`status` = ?", LicenseEntity::TRIBE_STATUS_APPROVED); $select->group('lic.vendorId'); $this->_insertToQueue($select); return $this; } public function addVendorsToQueue($libraryId, $vendors = null) { $select = $this->_db->select(); $select->from($this->_getPair('Vendor', 'v'), [ 'id', 'libraryId' => new Zend_Db_Expr($libraryId), 'status' => new Zend_Db_Expr($this->_db->quote(self::STATUS_NEW)), 'added' => new Zend_Db_Expr($this->_db->quote($added = date('Y-m-d H:i:s'))), 'changed' => new Zend_Db_Expr($this->_db->quote($added)), ]); if ($vendors) { $select->where('id IN (?)', $vendors); } $this->_insertToQueue($select); return $this; } protected function _insertToQueue (Zend_Db_Select $select) { $this->_db->exec( 'INSERT INTO ' . $this->_db->quoteIdentifier(Qs_Db::getTableName($this->_tableAlias)) . ' (`vendorId`, `libraryId`, `status`, `added`, `changed`) ' . $select ); return $this; } public function markSend(array $key) { return $this->_getTable()->updateByKey(['status' => self::STATUS_SENT], $key); } public function clearOldHistory($clearOlderThan) { return $this->_getTable()->delete('`added` < ' . $this->_db->quote($clearOlderThan . ' 00:00:00')); } public function deleteMailingData($libraryId) { return $this->_getTable()->delete('`libraryId` = ' . $this->_db->quote($libraryId, Zend_Db::INT_TYPE)); } }