/**
  * 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);
 }