1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Tax_Model_Resource_Calculation_Rule_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: 38: 39:
40: protected function _construct()
41: {
42: $this->_init('tax/calculation_rule');
43: }
44:
45: 46: 47: 48: 49: 50:
51: public function joinCalculationData($alias)
52: {
53: $this->getSelect()->joinLeft(
54: array($alias => $this->getTable('tax/tax_calculation')),
55: "main_table.tax_calculation_rule_id = {$alias}.tax_calculation_rule_id",
56: array()
57: );
58: $this->getSelect()->group('main_table.tax_calculation_rule_id');
59:
60: return $this;
61: }
62:
63: 64: 65: 66: 67: 68: 69: 70: 71: 72:
73: protected function _add($itemTable, $primaryJoinField, $secondaryJoinField, $titleField, $dataField)
74: {
75: $children = array();
76: foreach ($this as $rule) {
77: $children[$rule->getId()] = array();
78: }
79: if (!empty($children)) {
80: $joinCondition = sprintf('item.%s = calculation.%s', $secondaryJoinField, $primaryJoinField);
81: $select = $this->getConnection()->select()
82: ->from(
83: array('calculation' => $this->getTable('tax/tax_calculation')),
84: array('calculation.tax_calculation_rule_id')
85: )
86: ->join(
87: array('item' => $this->getTable($itemTable)),
88: $joinCondition,
89: array("item.{$titleField}", "item.{$secondaryJoinField}")
90: )
91: ->where('calculation.tax_calculation_rule_id IN (?)', array_keys($children))
92: ->distinct(true);
93:
94: $data = $this->getConnection()->fetchAll($select);
95: foreach ($data as $row) {
96: $children[$row['tax_calculation_rule_id']][$row[$secondaryJoinField]] = $row[$titleField];
97: }
98: }
99:
100: foreach ($this as $rule) {
101: if (isset($children[$rule->getId()])) {
102: $rule->setData($dataField, array_keys($children[$rule->getId()]));
103: }
104: }
105:
106: return $this;
107: }
108:
109: 110: 111: 112: 113:
114: public function addProductTaxClassesToResult()
115: {
116: return $this->_add('tax_class', 'product_tax_class_id', 'class_id', 'class_name', 'product_tax_classes');
117: }
118:
119: 120: 121: 122: 123:
124: public function addCustomerTaxClassesToResult()
125: {
126: return $this->_add('tax_class', 'customer_tax_class_id', 'class_id', 'class_name', 'customer_tax_classes');
127: }
128:
129: 130: 131: 132: 133:
134: public function addRatesToResult()
135: {
136: return $this->_add('tax_calculation_rate', 'tax_calculation_rate_id', 'tax_calculation_rate_id', 'code', 'tax_rates');
137: }
138:
139: 140: 141: 142: 143: 144: 145: 146:
147: public function setClassTypeFilter($type, $id)
148: {
149: switch ($type) {
150: case Mage_Tax_Model_Class::TAX_CLASS_TYPE_PRODUCT:
151: $field = 'cd.product_tax_class_id';
152: break;
153: case Mage_Tax_Model_Class::TAX_CLASS_TYPE_CUSTOMER:
154: $field = 'cd.customer_tax_class_id';
155: break;
156: default:
157: Mage::throwException('Invalid type supplied');
158: }
159:
160: $this->joinCalculationData('cd');
161: $this->addFieldToFilter($field, $id);
162: return $this;
163: }
164: }
165: