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: class Mage_Tax_Model_Observer
33: {
34: 35: 36: 37: 38:
39: public function salesEventConvertQuoteAddressToOrder(Varien_Event_Observer $observer)
40: {
41: $address = $observer->getEvent()->getAddress();
42: $order = $observer->getEvent()->getOrder();
43:
44: $taxes = $address->getAppliedTaxes();
45: if (is_array($taxes)) {
46: if (is_array($order->getAppliedTaxes())) {
47: $taxes = array_merge($order->getAppliedTaxes(), $taxes);
48: }
49: $order->setAppliedTaxes($taxes);
50: $order->setConvertingFromQuote(true);
51: }
52: }
53:
54: 55: 56: 57: 58:
59: public function salesEventOrderAfterSave(Varien_Event_Observer $observer)
60: {
61: $order = $observer->getEvent()->getOrder();
62:
63: if (!$order->getConvertingFromQuote() || $order->getAppliedTaxIsSaved()) {
64: return;
65: }
66:
67: $getTaxesForItems = $order->getQuote()->getTaxesForItems();
68: $taxes = $order->getAppliedTaxes();
69:
70: $ratesIdQuoteItemId = array();
71: if (!is_array($getTaxesForItems)) {
72: $getTaxesForItems = array();
73: }
74: foreach ($getTaxesForItems as $quoteItemId => $taxesArray) {
75: foreach ($taxesArray as $rates) {
76: if (count($rates['rates']) == 1) {
77: $ratesIdQuoteItemId[$rates['id']][] = array(
78: 'id' => $quoteItemId,
79: 'percent' => $rates['percent'],
80: 'code' => $rates['rates'][0]['code']
81: );
82: } else {
83: $percentDelta = $rates['percent'];
84: $percentSum = 0;
85: foreach ($rates['rates'] as $rate) {
86: $ratesIdQuoteItemId[$rates['id']][] = array(
87: 'id' => $quoteItemId,
88: 'percent' => $rate['percent'],
89: 'code' => $rate['code']
90: );
91: $percentSum += $rate['percent'];
92: }
93:
94: if ($percentDelta != $percentSum) {
95: $delta = $percentDelta - $percentSum;
96: foreach ($ratesIdQuoteItemId[$rates['id']] as &$rateTax) {
97: if ($rateTax['id'] == $quoteItemId) {
98: $rateTax['percent'] = (($rateTax['percent'] / $percentSum) * $delta)
99: + $rateTax['percent'];
100: }
101: }
102: }
103: }
104: }
105: }
106:
107: foreach ($taxes as $id => $row) {
108: foreach ($row['rates'] as $tax) {
109: if (is_null($row['percent'])) {
110: $baseRealAmount = $row['base_amount'];
111: } else {
112: if ($row['percent'] == 0 || $tax['percent'] == 0) {
113: continue;
114: }
115: $baseRealAmount = $row['base_amount'] / $row['percent'] * $tax['percent'];
116: }
117: $hidden = (isset($row['hidden']) ? $row['hidden'] : 0);
118: $data = array(
119: 'order_id' => $order->getId(),
120: 'code' => $tax['code'],
121: 'title' => $tax['title'],
122: 'hidden' => $hidden,
123: 'percent' => $tax['percent'],
124: 'priority' => $tax['priority'],
125: 'position' => $tax['position'],
126: 'amount' => $row['amount'],
127: 'base_amount' => $row['base_amount'],
128: 'process' => $row['process'],
129: 'base_real_amount' => $baseRealAmount,
130: );
131:
132: $result = Mage::getModel('tax/sales_order_tax')->setData($data)->save();
133:
134: if (isset($ratesIdQuoteItemId[$id])) {
135: foreach ($ratesIdQuoteItemId[$id] as $quoteItemId) {
136: if ($quoteItemId['code'] == $tax['code']) {
137: $item = $order->getItemByQuoteItemId($quoteItemId['id']);
138: if ($item) {
139: $data = array(
140: 'item_id' => $item->getId(),
141: 'tax_id' => $result->getTaxId(),
142: 'tax_percent' => $quoteItemId['percent']
143: );
144: Mage::getModel('tax/sales_order_tax_item')->setData($data)->save();
145: }
146: }
147: }
148: }
149: }
150: }
151:
152: $order->setAppliedTaxIsSaved(true);
153: }
154:
155: 156: 157: 158: 159: 160:
161: public function prepareCatalogIndexPriceSelect(Varien_Event_Observer $observer)
162: {
163: $table = $observer->getEvent()->getTable();
164: $response = $observer->getEvent()->getResponseObject();
165: $select = $observer->getEvent()->getSelect();
166: $storeId = $observer->getEvent()->getStoreId();
167:
168: $additionalCalculations = $response->getAdditionalCalculations();
169: $calculation = Mage::helper('tax')->getPriceTaxSql(
170: $table . '.min_price', $table.'.tax_class_id'
171: );
172:
173: if (!empty($calculation)) {
174: $additionalCalculations[] = $calculation;
175: $response->setAdditionalCalculations($additionalCalculations);
176: 177: 178:
179:
180: }
181:
182: return $this;
183: }
184:
185: 186: 187: 188: 189: 190:
191: public function addTaxPercentToProductCollection($observer)
192: {
193: $helper = Mage::helper('tax');
194: $collection = $observer->getEvent()->getCollection();
195: $store = $collection->getStoreId();
196: if (!$helper->needPriceConversion($store)) {
197: return $this;
198: }
199:
200: if ($collection->requireTaxPercent()) {
201: $request = Mage::getSingleton('tax/calculation')->getRateRequest();
202: foreach ($collection as $item) {
203: if (null === $item->getTaxClassId()) {
204: $item->setTaxClassId($item->getMinimalTaxClassId());
205: }
206: if (!isset($classToRate[$item->getTaxClassId()])) {
207: $request->setProductClassId($item->getTaxClassId());
208: $classToRate[$item->getTaxClassId()] = Mage::getSingleton('tax/calculation')->getRate($request);
209: }
210: $item->setTaxPercent($classToRate[$item->getTaxClassId()]);
211: }
212:
213: }
214: return $this;
215: }
216:
217: 218: 219: 220: 221: 222:
223: public function aggregateSalesReportTaxData($schedule)
224: {
225: Mage::app()->getLocale()->emulate(0);
226: $currentDate = Mage::app()->getLocale()->date();
227: $date = $currentDate->subHour(25);
228: Mage::getResourceModel('tax/report_tax')->aggregate($date);
229: Mage::app()->getLocale()->revert();
230: return $this;
231: }
232:
233: 234: 235: 236: 237: 238:
239: public function quoteCollectTotalsBefore(Varien_Event_Observer $observer)
240: {
241:
242: $quote = $observer->getEvent()->getQuote();
243: foreach ($quote->getAllAddresses() as $address) {
244: $address->setExtraTaxAmount(0);
245: $address->setBaseExtraTaxAmount(0);
246: }
247: return $this;
248: }
249: }
250: