Example #1
0
 public function fetchRate(Mage_Tax_Model_Rate_Data $request)
 {
     // initialize select from rate_data
     $select = $this->_getReadAdapter()->select();
     // get maximum rate from found
     $select->from(array('data' => $this->getMainTable()), array('value' => 'max(rate_value)'));
     if ($request->getRateTypeId()) {
         $select->where('data.rate_type_id=?', $request->getRateTypeId());
     }
     // join rule table with conditions
     if ($request->getCustomerClassId() && $request->getProductClassId()) {
         $select->join(array('rule' => $this->getTable('tax_rule')), 'rule.tax_rate_type_id=data.rate_type_id', array());
         $select->where('rule.tax_customer_class_id=?', $request->getCustomerClassId());
         $select->where('rule.tax_product_class_id=?', $request->getProductClassId());
     }
     // join rate table with conditions
     $select->join(array('rate' => $this->getTable('tax_rate')), 'rate.tax_rate_id=data.tax_rate_id', array());
     $select->where('rate.tax_country_id=?', $request->getCountryId());
     $select->where('rate.tax_region_id is null or rate.tax_region_id=0 or rate.tax_region_id=?', $request->getRegionId());
     $select->where("rate.tax_postcode is null or rate.tax_postcode in ('','*') or rate.tax_postcode=?", $request->getPostcode());
     // for future county handling
     if ($request->getCountyId()) {
         // TODO: make it play nice with zip
         $select->where('rate.tax_county_id is null or rate.tax_county_id=?', $request->getCountyId());
     }
     $select->order('tax_region_id desc')->order('tax_postcode desc');
     $rows = $this->_getReadAdapter()->fetchAll($select);
     return $rows ? $rows[0]['value'] : 0;
 }