/**
  *                             .                                  PORTION_MAX_EXPORT_TIME     .
  */
 function sendMessagesPortion()
 {
     global $application;
     loadCoreFile('ascHtmlMimeMail.php');
     $mailer = new ascHtmlMimeMail();
     $from = $this->_currentMessage['letter_from_name'] . ' <' . $this->_currentMessage['letter_from_email'] . '>';
     $mailer->setFrom($from);
     $mailer->setSubject($this->_currentMessage['letter_subject']);
     $html = "<html><head><title>{$this->_currentMessage['letter_subject']}</title></head><body>{$this->_currentMessage['letter_html']}</body></html>";
     $mailer->setHtml($html);
     $start_time = $this->microtime_float();
     $sent_count = 0;
     //
     //                           $max_to_send
     //
     $table = 'newsletter_temp';
     $tables = $this->getTables();
     //          PORTION_MAX_MESSAGES_NUM          ,                  (_sentCountTotal + 1)
     //                                 ,     PORTION_MAX_MESSAGES_NUM
     $query = new DB_Select();
     $query->addSelectTable($table);
     $query->addSelectField($tables[$table]['columns']['recipient_value']);
     $query->addWhereOpenSection();
     $query->WhereValue($tables[$table]['columns']['recipient_num'], DB_GTE, $this->_sentCountTotal + 1);
     $query->WhereAND();
     $query->WhereValue($tables[$table]['columns']['recipient_num'], DB_LTE, $this->_sentCountTotal + PORTION_MAX_MESSAGES_NUM);
     $query->addWhereCloseSection();
     $res = $application->db->getDB_Result($query);
     $addr_num = count($res);
     while ($this->microtime_float() - $start_time < PORTION_MAX_EXPORT_TIME && $sent_count < $addr_num) {
         //
         //
         //
         $mailer->send(array($res[$sent_count]['recipient_value']));
         $sent_count++;
         // :
         /*debug*/
         //usleep(200000);
     }
     $this->_sentCountTotal += $sent_count;
     $sending_status = 'PROCESSING';
     if ($this->_sentCountTotal >= $this->_totalRecipients) {
         $this->_sentCountTotal = $this->_totalRecipients;
         //
         //
         //
         $table = 'newsletter_temp';
         $tables = $this->getTables();
         $query = new DB_Delete($table);
         $application->db->PrepareSQL($query);
         $application->db->DB_Exec();
         $this->updateMessage($this->_currentMessage['letter_id'], array('letter_sent_date' => date('Y-m-d G:i:s')));
         $sending_status = 'COMPLETED';
     }
     return array('errors' => '', 'warnings' => '', 'sent_total' => $this->_sentCountTotal, 'sending_status' => $sending_status);
 }
 /**
  * Returns a tax formulas/rates list for given country, state and
  * tax class.
  * Warning: the entries from
  * state_id = STATE_ID_ALL
  * and
  * tax_class_id = TAX_CLASS_ID_ANY
  * will be returned independently of $state_id and $tax_class_id.
  */
 function getTaxRatesList($country_id = -1, $state_id = -1, $tax_class_id = -1, $tax_name_id = -1)
 {
     global $application;
     $tables = $this->getTables();
     $tr = $tables['tax_rates']['columns'];
     $ptc = $tables['product_tax_classes']['columns'];
     $tn = $tables['tax_names']['columns'];
     $query = new DB_Select();
     $query->addSelectField($tr['id'], 'Id');
     $query->addSelectField($tr['c_id'], 'c_id');
     $query->addSelectField($tr['s_id'], 's_id');
     $query->addSelectField($ptc['name'], 'ProductTaxClass');
     $query->addSelectField($ptc['id'], 'tax_class_id');
     $query->addLeftJoin('product_tax_classes', $ptc['id'], DB_EQ, $tr['ptc_id']);
     $query->addLeftJoin('tax_names', $tn['id'], DB_EQ, $tr['tn_id']);
     $query->setMultiLangAlias('_name', 'tax_names', $tn['name'], $tn['id'], 'Taxes');
     $query->addSelectField($query->getMultiLangAlias('_name'), 'TaxName');
     $query->addSelectField($tn['id'], 'tax_name_id');
     $query->addSelectField($tr['rate'], 'Rate');
     $query->addSelectField($tr['formula'], 'Formula');
     $query->addSelectField($tr['applicable'], 'Applicable');
     $query->addSelectField($tr['rates_set'], 'rates_set');
     $query->WhereValue('', '', '1');
     if ($country_id != -1 && $country_id != TAXES_COUNTRY_NOT_NEEDED_ID) {
         $query->WhereAnd();
         $query->addWhereOpenSection();
         $query->WhereValue($tr['c_id'], DB_EQ, $country_id);
         $query->WhereOR();
         $query->WhereValue($tr['c_id'], DB_EQ, TAXES_COUNTRY_NOT_NEEDED_ID);
         $query->addWhereCloseSection();
     }
     if ($state_id != -1 && $state_id != TAXES_STATE_NOT_NEEDED_ID) {
         $query->WhereAnd();
         $query->addWhereOpenSection();
         $query->WhereValue($tr['s_id'], DB_EQ, $state_id);
         $query->WhereOR();
         $query->WhereValue($tr['s_id'], DB_EQ, STATE_ID_ALL);
         $query->WhereOR();
         $query->WhereValue($tr['s_id'], DB_EQ, TAXES_STATE_NOT_NEEDED_ID);
         $query->addWhereCloseSection();
     }
     if ($tax_class_id != -1) {
         $query->WhereAnd();
         $query->addWhereOpenSection();
         $query->WhereValue($tr['ptc_id'], DB_EQ, $tax_class_id);
         $query->WhereOR();
         $query->WhereValue($tr['ptc_id'], DB_EQ, TAX_CLASS_ID_ANY);
         $query->addWhereCloseSection();
     }
     if ($tax_name_id != -1) {
         $query->WhereAnd();
         $query->WhereValue($tr['tn_id'], DB_EQ, $tax_name_id);
     }
     return $application->db->getDB_Result($query);
 }