00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class Mage_Tax_Model_Mysql4_Calculation_Rule_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
00034 {
00035 protected function _construct()
00036 {
00037 $this->_init('tax/calculation_rule');
00038 }
00039
00040 public function joinCalculationData($alias)
00041 {
00042 $this->getSelect()->joinLeft(array($alias=>$this->getTable('tax_calculation')), "main_table.tax_calculation_rule_id = {$alias}.tax_calculation_rule_id", array());
00043 $this->getSelect()->group('main_table.tax_calculation_rule_id');
00044 }
00045
00046 protected function _add($itemTable, $primaryJoinField, $secondaryJoinField, $titleField, $dataField)
00047 {
00048 $children = array();
00049 foreach ($this as $rule) {
00050 $children[$rule->getId()] = array();
00051 }
00052 if (!empty($children)) {
00053 $select = $this->getConnection()->select()
00054 ->from(array('calculation'=>$this->getTable('tax_calculation')), array('calculation.tax_calculation_rule_id'))
00055 ->join(
00056 array('item'=>$this->getTable($itemTable)),
00057 "item.{$secondaryJoinField} = calculation.{$primaryJoinField}",
00058 array("item.{$titleField}"))
00059 ->where('calculation.tax_calculation_rule_id IN (?)', array_keys($children))
00060 ->distinct(true);
00061
00062 $data = $this->getConnection()->fetchAll($select);
00063 foreach ($data as $row) {
00064 $children[$row['tax_calculation_rule_id']][] = $row[$titleField];
00065 }
00066 }
00067 foreach ($this as $rule) {
00068 if (isset($children[$rule->getId()])) {
00069 $rule->setData($dataField, $children[$rule->getId()]);
00070 }
00071 }
00072 return $this;
00073 }
00074
00075 public function addProductTaxClassesToResult()
00076 {
00077 return $this->_add('tax_class', 'product_tax_class_id', 'class_id', 'class_name', 'product_tax_classes');
00078 }
00079 public function addCustomerTaxClassesToResult()
00080 {
00081 return $this->_add('tax_class', 'customer_tax_class_id', 'class_id', 'class_name', 'customer_tax_classes');
00082 }
00083 public function addRatesToResult()
00084 {
00085 return $this->_add('tax_calculation_rate', 'tax_calculation_rate_id', 'tax_calculation_rate_id', 'code', 'tax_rates');
00086 }
00087
00088 public function setClassTypeFilter($type, $id)
00089 {
00090 switch ($type) {
00091 case 'PRODUCT':
00092 $field = 'cd.product_tax_class_id';
00093 break;
00094 case 'CUSTOMER':
00095 $field = 'cd.customer_tax_class_id';
00096 break;
00097 default:
00098 Mage::throwException('Invalid type supplied');
00099 }
00100
00101 $this->joinCalculationData('cd');
00102 $this->addFieldToFilter($field, $id);
00103 return $this;
00104 }
00105 }