/
Tax.php
1565 lines (1416 loc) · 66 KB
/
Tax.php
1
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magento.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magento.com for more information.
*
* @category Mage
* @package Mage_Tax
* @copyright Copyright (c) 2006-2017 X.commerce, Inc. and affiliates (http://www.magento.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Tax totals calculation model
*/
class Mage_Tax_Model_Sales_Total_Quote_Tax extends Mage_Sales_Model_Quote_Address_Total_Abstract
{
/**
* Tax module helper
*
* @var Mage_Tax_Helper_Data
*/
protected $_helper;
/**
* Tax calculation model
*
* @var Mage_Tax_Model_Calculation
*/
protected $_calculator;
/**
* Tax configuration object
*
* @var Mage_Tax_Model_Config
*/
protected $_config;
/**
* Flag which is initialized when collect method is start.
* Is used for checking if store tax and customer tax requests are similar
*
* @var bool
*/
protected $_areTaxRequestsSimilar = false;
/**
* Array for the rounding deltas
*
* @var array
*/
protected $_roundingDeltas = array();
/**
* Array for the base rounding deltas
*
* @var array
*/
protected $_baseRoundingDeltas = array();
/**
* @var Mage_Core_Model_Store
*/
protected $_store;
/**
* Hidden taxes array
*
* @var array
*/
protected $_hiddenTaxes = array();
/**
* Weee helper class
*
* @var Mage_Weee_Helper_Data
*/
protected $_weeeHelper;
/**
* Class constructor
*/
public function __construct()
{
$this->setCode('tax');
$this->_helper = Mage::helper('tax');
$this->_calculator = Mage::getSingleton('tax/calculation');
$this->_config = Mage::getSingleton('tax/config');
$this->_weeeHelper = Mage::helper('weee');
}
/**
* Round the total amounts in address
*
* @param Mage_Sales_Model_Quote_Address $address
* @return Mage_Tax_Model_Sales_Total_Quote_Tax
*/
protected function _roundTotals(Mage_Sales_Model_Quote_Address $address)
{
// initialize the delta to a small number to avoid non-deterministic behavior with rounding of 0.5
$totalDelta = 0.000001;
$baseTotalDelta = 0.000001;
/*
* The order of rounding is import here.
* Tax is rounded first, to be consistent with unit based calculation.
* Hidden tax and shipping_hidden_tax are rounded next, which are really part of tax.
* Shipping is rounded before subtotal to minimize the chance that subtotal is
* rounded differently because of the delta.
* Here is an example: 19.2% tax rate, subtotal = 49.95, shipping = 9.99, discount = 20%
* subtotalExclTax = 41.90436, tax = 7.7238, hidden_tax = 1.609128, shippingPriceExclTax = 8.38087
* shipping_hidden_tax = 0.321826, discount = -11.988
* The grand total is 47.952 ~= 47.95
* The rounded values are:
* tax = 7.72, hidden_tax = 1.61, shipping_hidden_tax = 0.32,
* shipping = 8.39 (instead of 8.38 from simple rounding), subtotal = 41.9, discount = -11.99
* The grand total calculated from the rounded value is 47.95
* If we simply round each value and add them up, the result is 47.94, which is one penny off
*/
$totalCodes = array('tax', 'hidden_tax', 'shipping_hidden_tax', 'shipping', 'subtotal', 'weee', 'discount');
foreach ($totalCodes as $totalCode) {
$exactAmount = $address->getTotalAmount($totalCode);
$baseExactAmount = $address->getBaseTotalAmount($totalCode);
if (!$exactAmount && !$baseExactAmount) {
continue;
}
$roundedAmount = $this->_calculator->round($exactAmount + $totalDelta);
$baseRoundedAmount = $this->_calculator->round($baseExactAmount + $baseTotalDelta);
$address->setTotalAmount($totalCode, $roundedAmount);
$address->setBaseTotalAmount($totalCode, $baseRoundedAmount);
$totalDelta = $exactAmount + $totalDelta - $roundedAmount;
$baseTotalDelta = $baseExactAmount + $baseTotalDelta - $baseRoundedAmount;
}
return $this;
}
/**
* Collect tax totals for quote address
*
* @param Mage_Sales_Model_Quote_Address $address
* @return Mage_Tax_Model_Sales_Total_Quote
*/
public function collect(Mage_Sales_Model_Quote_Address $address)
{
parent::collect($address);
$this->_roundingDeltas = array();
$this->_baseRoundingDeltas = array();
$this->_hiddenTaxes = array();
$address->setShippingTaxAmount(0);
$address->setBaseShippingTaxAmount(0);
$this->_store = $address->getQuote()->getStore();
$customer = $address->getQuote()->getCustomer();
if ($customer) {
$this->_calculator->setCustomer($customer);
}
if (!$address->getAppliedTaxesReset()) {
$address->setAppliedTaxes(array());
}
$items = $this->_getAddressItems($address);
if (!count($items)) {
return $this;
}
$request = $this->_calculator->getRateRequest(
$address,
$address->getQuote()->getBillingAddress(),
$address->getQuote()->getCustomerTaxClassId(),
$this->_store
);
if ($this->_config->priceIncludesTax($this->_store)) {
if ($this->_helper->isCrossBorderTradeEnabled($this->_store)) {
$this->_areTaxRequestsSimilar = true;
} else {
$this->_areTaxRequestsSimilar = $this->_calculator->compareRequests(
$this->_calculator->getRateOriginRequest($this->_store),
$request
);
}
}
switch ($this->_config->getAlgorithm($this->_store)) {
case Mage_Tax_Model_Calculation::CALC_UNIT_BASE:
$this->_unitBaseCalculation($address, $request);
break;
case Mage_Tax_Model_Calculation::CALC_ROW_BASE:
$this->_rowBaseCalculation($address, $request);
break;
case Mage_Tax_Model_Calculation::CALC_TOTAL_BASE:
$this->_totalBaseCalculation($address, $request);
break;
default:
break;
}
$this->_addAmount($address->getExtraTaxAmount());
$this->_addBaseAmount($address->getBaseExtraTaxAmount());
$this->_calculateShippingTax($address, $request);
$this->_processHiddenTaxes();
//round total amounts in address
$this->_roundTotals($address);
return $this;
}
/**
* Process hidden taxes for items and shippings (in accordance with hidden tax type)
*
* @return void
*/
protected function _processHiddenTaxes()
{
$this->_getAddress()->setTotalAmount('hidden_tax', 0);
$this->_getAddress()->setBaseTotalAmount('hidden_tax', 0);
$this->_getAddress()->setTotalAmount('shipping_hidden_tax', 0);
$this->_getAddress()->setBaseTotalAmount('shipping_hidden_tax', 0);
foreach ($this->_hiddenTaxes as $taxInfoItem) {
if (isset($taxInfoItem['item'])) {
// Item hidden taxes
$item = $taxInfoItem['item'];
$rateKey = $taxInfoItem['rate_key'];
$hiddenTax = $taxInfoItem['value'];
$baseHiddenTax = $taxInfoItem['base_value'];
$inclTax = $taxInfoItem['incl_tax'];
$qty = $taxInfoItem['qty'];
$hiddenTax = $this->_calculator->round($hiddenTax);
$baseHiddenTax = $this->_calculator->round($baseHiddenTax);
$item->setHiddenTaxAmount(max(0, $qty * $hiddenTax));
$item->setBaseHiddenTaxAmount(max(0, $qty * $baseHiddenTax));
$this->_getAddress()->addTotalAmount('hidden_tax', $item->getHiddenTaxAmount());
$this->_getAddress()->addBaseTotalAmount('hidden_tax', $item->getBaseHiddenTaxAmount());
} else {
// Shipping hidden taxes
$rateKey = $taxInfoItem['rate_key'];
$hiddenTax = $taxInfoItem['value'];
$baseHiddenTax = $taxInfoItem['base_value'];
$inclTax = $taxInfoItem['incl_tax'];
$hiddenTax = $this->_calculator->round($hiddenTax);
$baseHiddenTax = $this->_calculator->round($baseHiddenTax);
$this->_getAddress()->addTotalAmount('shipping_hidden_tax', $hiddenTax);
$this->_getAddress()->addBaseTotalAmount('shipping_hidden_tax', $baseHiddenTax);
$this->_getAddress()->setShippingHiddenTaxAmount(max(0, $hiddenTax));
$this->_getAddress()->setBaseShippingHiddenTaxAmount(max(0, $baseHiddenTax));
}
}
}
/**
* Check if price include tax should be used for calculations.
* We are using price include tax just in case when catalog prices are including tax
* and customer tax request is same as store tax request
*
* @param $store
* @return bool
*/
protected function _usePriceIncludeTax($store)
{
if ($this->_config->priceIncludesTax($store) || $this->_config->getNeedUsePriceExcludeTax()) {
return $this->_areTaxRequestsSimilar;
}
return false;
}
/**
*
* @param Mage_Sales_Model_Quote_Address $address
* @param float $rate
* @param array $appliedRates
* @param string $taxId
*/
protected function _calculateShippingTaxByRate(
Mage_Sales_Model_Quote_Address $address, $rate, $appliedRates, $taxId = null)
{
$inclTax = $address->getIsShippingInclTax();
$shipping = $address->getShippingTaxable();
$baseShipping = $address->getBaseShippingTaxable();
$rateKey = ($taxId == null) ? (string)$rate : $taxId;
$hiddenTax = null;
$baseHiddenTax = null;
switch ($this->_helper->getCalculationSequence($this->_store)) {
case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_EXCL:
case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_INCL:
$tax = $this->_calculator->calcTaxAmount($shipping, $rate, $inclTax, false);
$baseTax = $this->_calculator->calcTaxAmount($baseShipping, $rate, $inclTax, false);
break;
case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_EXCL:
case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_INCL:
$discountAmount = $address->getShippingDiscountAmount();
$baseDiscountAmount = $address->getBaseShippingDiscountAmount();
$tax = $this->_calculator->calcTaxAmount(
$shipping - $discountAmount,
$rate,
$inclTax,
false
);
$baseTax = $this->_calculator->calcTaxAmount(
$baseShipping - $baseDiscountAmount,
$rate,
$inclTax,
false
);
break;
}
if ($this->_config->getAlgorithm($this->_store) == Mage_Tax_Model_Calculation::CALC_TOTAL_BASE) {
$tax = $this->_deltaRound($tax, $rateKey, $inclTax);
$baseTax = $this->_deltaRound($baseTax, $rateKey, $inclTax, 'base');
$this->_addAmount(max(0, $tax));
$this->_addBaseAmount(max(0, $baseTax));
} else {
$tax = $this->_calculator->round($tax);
$baseTax = $this->_calculator->round($baseTax);
$this->_addAmount(max(0, $tax));
$this->_addBaseAmount(max(0, $baseTax));
}
if ($inclTax && !empty($discountAmount)) {
$taxBeforeDiscount = $this->_calculator->calcTaxAmount(
$shipping,
$rate,
$inclTax,
false
);
$baseTaxBeforeDiscount = $this->_calculator->calcTaxAmount(
$baseShipping,
$rate,
$inclTax,
false
);
if ($this->_config->getAlgorithm($this->_store) == Mage_Tax_Model_Calculation::CALC_TOTAL_BASE) {
$taxBeforeDiscount =
$this->_deltaRound($taxBeforeDiscount, $rateKey, $inclTax, 'tax_before_discount');
$baseTaxBeforeDiscount =
$this->_deltaRound($baseTaxBeforeDiscount, $rateKey, $inclTax, 'tax_before_discount_base');
} else {
$taxBeforeDiscount = $this->_calculator->round($taxBeforeDiscount);
$baseTaxBeforeDiscount = $this->_calculator->round($baseTaxBeforeDiscount);
}
$hiddenTax = max(0, $taxBeforeDiscount - max(0, $tax));
$baseHiddenTax = max(0, $baseTaxBeforeDiscount - max(0, $baseTax));
$this->_hiddenTaxes[] = array(
'rate_key' => $rateKey,
'value' => $hiddenTax,
'base_value' => $baseHiddenTax,
'incl_tax' => $inclTax,
);
}
$address->setShippingTaxAmount($address->getShippingTaxAmount() + max(0, $tax));
$address->setBaseShippingTaxAmount($address->getBaseShippingTaxAmount() + max(0, $baseTax));
$this->_saveAppliedTaxes($address, $appliedRates, $tax, $baseTax, $rate);
}
/**
* Tax caclulation for shipping price
*
* @param Mage_Sales_Model_Quote_Address $address
* @param Varien_Object $taxRateRequest
* @return Mage_Tax_Model_Sales_Total_Quote
*/
protected function _calculateShippingTax(Mage_Sales_Model_Quote_Address $address, $taxRateRequest)
{
$taxRateRequest->setProductClassId($this->_config->getShippingTaxClass($this->_store));
$rate = $this->_calculator->getRate($taxRateRequest);
$inclTax = $address->getIsShippingInclTax();
$address->setShippingTaxAmount(0);
$address->setBaseShippingTaxAmount(0);
$address->setShippingHiddenTaxAmount(0);
$address->setBaseShippingHiddenTaxAmount(0);
$appliedRates = $this->_calculator->getAppliedRates($taxRateRequest);
if ($inclTax) {
$this->_calculateShippingTaxByRate($address, $rate, $appliedRates);
} else {
foreach ($appliedRates as $appliedRate) {
$taxRate = $appliedRate['percent'];
$taxId = $appliedRate['id'];
$this->_calculateShippingTaxByRate($address, $taxRate, array($appliedRate), $taxId);
}
}
return $this;
}
/**
* Calculate address tax amount based on one unit price and tax amount
*
* @param Mage_Sales_Model_Quote_Address $address
* @return Mage_Tax_Model_Sales_Total_Quote
*/
protected function _unitBaseCalculation(Mage_Sales_Model_Quote_Address $address, $taxRateRequest)
{
$items = $this->_getAddressItems($address);
$itemTaxGroups = array();
$store = $address->getQuote()->getStore();
$catalogPriceInclTax = $this->_config->priceIncludesTax($store);
foreach ($items as $item) {
if ($item->getParentItem()) {
continue;
}
if ($item->getHasChildren() && $item->isChildrenCalculated()) {
foreach ($item->getChildren() as $child) {
$this->_unitBaseProcessItemTax(
$address, $child, $taxRateRequest, $itemTaxGroups, $catalogPriceInclTax);
}
$this->_recalculateParent($item);
} else {
$this->_unitBaseProcessItemTax(
$address, $item, $taxRateRequest, $itemTaxGroups, $catalogPriceInclTax);
}
}
if ($address->getQuote()->getTaxesForItems()) {
$itemTaxGroups += $address->getQuote()->getTaxesForItems();
}
$address->getQuote()->setTaxesForItems($itemTaxGroups);
return $this;
}
/**
*
* @param Mage_Sales_Model_Quote_Address $address
* @param Mage_Sales_Model_Quote_Item_Abstract $item
* @param Varien_Object $taxRateRequest
* @param array $itemTaxGroups
* @param boolean $catalogPriceInclTax
*/
protected function _unitBaseProcessItemTax(
$address, $item, $taxRateRequest, &$itemTaxGroups, $catalogPriceInclTax
)
{
$taxRateRequest->setProductClassId($item->getProduct()->getTaxClassId());
$rate = $this->_calculator->getRate($taxRateRequest);
$item->setTaxAmount(0);
$item->setBaseTaxAmount(0);
$item->setHiddenTaxAmount(0);
$item->setBaseHiddenTaxAmount(0);
$item->setTaxPercent($rate);
$item->setDiscountTaxCompensation(0);
$rowTotalInclTax = $item->getRowTotalInclTax();
$recalculateRowTotalInclTax = false;
if (!isset($rowTotalInclTax)) {
$qty = $item->getTotalQty();
$item->setRowTotalInclTax($this->_store->roundPrice($item->getTaxableAmount() * $qty));
$item->setBaseRowTotalInclTax(
$this->_store->roundPrice($item->getBaseTaxableAmount() * $qty));
$recalculateRowTotalInclTax = true;
}
$appliedRates = $this->_calculator->getAppliedRates($taxRateRequest);
$item->setTaxRates($appliedRates);
if ($catalogPriceInclTax) {
$this->_calcUnitTaxAmount($item, $rate);
$this->_saveAppliedTaxes(
$address, $appliedRates, $item->getTaxAmount(), $item->getBaseTaxAmount(), $rate);
} else {
//need to calculate each tax separately
$taxGroups = array();
foreach ($appliedRates as $appliedTax) {
$taxId = $appliedTax['id'];
$taxRate = $appliedTax['percent'];
$this->_calcUnitTaxAmount($item, $taxRate, $taxGroups, $taxId, $recalculateRowTotalInclTax);
$this->_saveAppliedTaxes(
$address, array($appliedTax), $taxGroups[$taxId]['tax'], $taxGroups[$taxId]['base_tax'], $taxRate);
}
//We need to calculate weeeAmountInclTax using multiple tax rate here
//because the _calculateWeeeTax and _calculateRowWeeeTax only take one tax rate
if ($this->_weeeHelper->isEnabled() && $this->_weeeHelper->isTaxable()) {
$this->_calculateWeeeAmountInclTax($item, $appliedRates, false);
$this->_calculateWeeeAmountInclTax($item, $appliedRates, true);
}
}
if ($rate > 0) {
$itemTaxGroups[$item->getId()] = $appliedRates;
}
$this->_addAmount($item->getTaxAmount());
$this->_addBaseAmount($item->getBaseTaxAmount());
return;
}
/**
* Calculate unit tax anount based on unit price
*
* @param Mage_Sales_Model_Quote_Item_Abstract $item
* @param float $rate
* @param array $taxGroups
* @param string $taxId
* @param boolean $recalculateRowTotalInclTax
* @return Mage_Tax_Model_Sales_Total_Quote
*/
protected function _calcUnitTaxAmount(
$item, $rate, &$taxGroups = null, $taxId = null, $recalculateRowTotalInclTax = false
)
{
$qty = $item->getTotalQty();
$inclTax = $item->getIsPriceInclTax();
$price = $item->getTaxableAmount();
$basePrice = $item->getBaseTaxableAmount();
$rateKey = ($taxId == null) ? (string)$rate : $taxId;
$isWeeeEnabled = $this->_weeeHelper->isEnabled();
$isWeeeTaxable = $this->_weeeHelper->isTaxable();
$hiddenTax = null;
$baseHiddenTax = null;
$weeeTax = null;
$baseWeeeTax = null;
$unitTaxBeforeDiscount = null;
$weeeTaxBeforeDiscount = null;
$baseUnitTaxBeforeDiscount = null;
$baseWeeeTaxBeforeDiscount = null;
switch ($this->_config->getCalculationSequence($this->_store)) {
case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_EXCL:
case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_INCL:
$unitTaxBeforeDiscount = $this->_calculator->calcTaxAmount($price, $rate, $inclTax, false);
$baseUnitTaxBeforeDiscount = $this->_calculator->calcTaxAmount($basePrice, $rate, $inclTax, false);
if ($isWeeeEnabled && $isWeeeTaxable) {
$weeeTaxBeforeDiscount = $this->_calculateWeeeTax(0, $item, $rate, false);
$unitTaxBeforeDiscount += $weeeTaxBeforeDiscount;
$baseWeeeTaxBeforeDiscount = $this->_calculateWeeeTax(0, $item, $rate);
$baseUnitTaxBeforeDiscount += $baseWeeeTaxBeforeDiscount;
}
$unitTaxBeforeDiscount = $unitTax = $this->_calculator->round($unitTaxBeforeDiscount);
$baseUnitTaxBeforeDiscount = $baseUnitTax = $this->_calculator->round($baseUnitTaxBeforeDiscount);
break;
case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_EXCL:
case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_INCL:
$discountAmount = $item->getDiscountAmount() / $qty;
$baseDiscountAmount = $item->getBaseDiscountAmount() / $qty;
//We want to remove weee
if ($isWeeeEnabled && $this->_weeeHelper->includeInSubtotal()) {
$discountAmount = $discountAmount - $item->getWeeeDiscount() / $qty;
$baseDiscountAmount = $baseDiscountAmount - $item->getBaseWeeeDiscount() / $qty;
}
$unitTaxBeforeDiscount = $this->_calculator->calcTaxAmount($price, $rate, $inclTax, false);
$unitTaxDiscount = $this->_calculator->calcTaxAmount($discountAmount, $rate, $inclTax, false);
$unitTax = $this->_calculator->round(max($unitTaxBeforeDiscount - $unitTaxDiscount, 0));
$baseUnitTaxBeforeDiscount = $this->_calculator->calcTaxAmount($basePrice, $rate, $inclTax, false);
$baseUnitTaxDiscount = $this->_calculator->calcTaxAmount($baseDiscountAmount, $rate, $inclTax, false);
$baseUnitTax = $this->_calculator->round(max($baseUnitTaxBeforeDiscount - $baseUnitTaxDiscount, 0));
if ($isWeeeEnabled && $this->_weeeHelper->isTaxable()) {
$weeeTax = $this->_calculateRowWeeeTax($item->getWeeeDiscount(), $item, $rate, false);
$weeeTax = $weeeTax / $qty;
$unitTax += $weeeTax;
$baseWeeeTax = $this->_calculateRowWeeeTax($item->getBaseWeeeDiscount(), $item, $rate);
$baseWeeeTax = $baseWeeeTax / $qty;
$baseUnitTax += $baseWeeeTax;
}
$unitTax = $this->_calculator->round($unitTax);
$baseUnitTax = $this->_calculator->round($baseUnitTax);
//Calculate the weee taxes before discount
$weeeTaxBeforeDiscount = 0;
$baseWeeeTaxBeforeDiscount = 0;
if ($isWeeeTaxable) {
$weeeTaxBeforeDiscount = $this->_calculateWeeeTax(0, $item, $rate, false);
$unitTaxBeforeDiscount += $weeeTaxBeforeDiscount;
$baseWeeeTaxBeforeDiscount = $this->_calculateWeeeTax(0, $item, $rate);
$baseUnitTaxBeforeDiscount += $baseWeeeTaxBeforeDiscount;
}
$unitTaxBeforeDiscount = max(0, $this->_calculator->round($unitTaxBeforeDiscount));
$baseUnitTaxBeforeDiscount = max(0, $this->_calculator->round($baseUnitTaxBeforeDiscount));
if ($inclTax && $discountAmount > 0) {
$hiddenTax = $unitTaxBeforeDiscount - $unitTax;
$baseHiddenTax = $baseUnitTaxBeforeDiscount - $baseUnitTax;
$this->_hiddenTaxes[] = array(
'rate_key' => $rateKey,
'qty' => $qty,
'item' => $item,
'value' => $hiddenTax,
'base_value' => $baseHiddenTax,
'incl_tax' => $inclTax,
);
} elseif ($discountAmount > $price) { // case with 100% discount on price incl. tax
$hiddenTax = $discountAmount - $price;
$baseHiddenTax = $baseDiscountAmount - $basePrice;
$this->_hiddenTaxes[] = array(
'rate_key' => $rateKey,
'qty' => $qty,
'item' => $item,
'value' => $hiddenTax,
'base_value' => $baseHiddenTax,
'incl_tax' => $inclTax,
);
}
// calculate discount compensation
// We need the discount compensation when dont calculate the hidden taxes
// (when product does not include taxes)
if (!$item->getNoDiscount() && $item->getWeeeTaxApplied()) {
$item->setDiscountTaxCompensation($item->getDiscountTaxCompensation() +
$unitTaxBeforeDiscount * $qty - max(0, $unitTax) * $qty);
}
break;
}
$rowTax = $this->_store->roundPrice(max(0, $qty * $unitTax));
$baseRowTax = $this->_store->roundPrice(max(0, $qty * $baseUnitTax));
$item->setTaxAmount($item->getTaxAmount() + $rowTax);
$item->setBaseTaxAmount($item->getBaseTaxAmount() + $baseRowTax);
if (is_array($taxGroups)) {
$taxGroups[$rateKey]['tax'] = max(0, $rowTax);
$taxGroups[$rateKey]['base_tax'] = max(0, $baseRowTax);
}
$rowTotalInclTax = $item->getRowTotalInclTax();
if (!isset($rowTotalInclTax) || $recalculateRowTotalInclTax) {
if ($this->_config->priceIncludesTax($this->_store)) {
$item->setRowTotalInclTax($price * $qty);
$item->setBaseRowTotalInclTax($basePrice * $qty);
} else {
$item->setRowTotalInclTax(
$item->getRowTotalInclTax() + ($unitTaxBeforeDiscount - $weeeTaxBeforeDiscount) * $qty);
$item->setBaseRowTotalInclTax(
$item->getBaseRowTotalInclTax() +
($baseUnitTaxBeforeDiscount - $baseWeeeTaxBeforeDiscount) * $qty);
}
}
return $this;
}
/**
* Calculate address total tax based on row total
*
* @param Mage_Sales_Model_Quote_Address $address
* @param Varien_Object $taxRateRequest
* @return Mage_Tax_Model_Sales_Total_Quote
*/
protected function _rowBaseCalculation(Mage_Sales_Model_Quote_Address $address, $taxRateRequest)
{
$items = $this->_getAddressItems($address);
$itemTaxGroups = array();
$store = $address->getQuote()->getStore();
$catalogPriceInclTax = $this->_config->priceIncludesTax($store);
foreach ($items as $item) {
if ($item->getParentItem()) {
continue;
}
if ($item->getHasChildren() && $item->isChildrenCalculated()) {
foreach ($item->getChildren() as $child) {
$this->_rowBaseProcessItemTax(
$address, $child, $taxRateRequest, $itemTaxGroups, $catalogPriceInclTax);
}
$this->_recalculateParent($item);
} else {
$this->_rowBaseProcessItemTax(
$address, $item, $taxRateRequest, $itemTaxGroups, $catalogPriceInclTax);
}
}
if ($address->getQuote()->getTaxesForItems()) {
$itemTaxGroups += $address->getQuote()->getTaxesForItems();
}
$address->getQuote()->setTaxesForItems($itemTaxGroups);
return $this;
}
/**
*
* @param Mage_Sales_Model_Quote_Address $address
* @param Mage_Sales_Model_Quote_Item_Abstract $item
* @param Varien_Object $taxRateRequest
* @param array $itemTaxGroups
* @param boolean $catalogPriceInclTax
*/
protected function _rowBaseProcessItemTax($address, $item, $taxRateRequest, &$itemTaxGroups, $catalogPriceInclTax)
{
$taxRateRequest->setProductClassId($item->getProduct()->getTaxClassId());
$rate = $this->_calculator->getRate($taxRateRequest);
$item->setTaxAmount(0);
$item->setBaseTaxAmount(0);
$item->setHiddenTaxAmount(0);
$item->setBaseHiddenTaxAmount(0);
$item->setTaxPercent($rate);
$item->setDiscountTaxCompensation(0);
$rowTotalInclTax = $item->getRowTotalInclTax();
$recalculateRowTotalInclTax = false;
if (!isset($rowTotalInclTax)) {
$item->setRowTotalInclTax($item->getTaxableAmount());
$item->setBaseRowTotalInclTax($item->getBaseTaxableAmount());
$recalculateRowTotalInclTax = true;
}
$appliedRates = $this->_calculator->getAppliedRates($taxRateRequest);
$item->setTaxRates($appliedRates);
if ($catalogPriceInclTax) {
$this->_calcRowTaxAmount($item, $rate);
$this->_saveAppliedTaxes(
$address, $appliedRates, $item->getTaxAmount(), $item->getBaseTaxAmount(), $rate);
} else {
//need to calculate each tax separately
$taxGroups = array();
foreach ($appliedRates as $appliedTax) {
$taxId = $appliedTax['id'];
$taxRate = $appliedTax['percent'];
$this->_calcRowTaxAmount($item, $taxRate, $taxGroups, $taxId, $recalculateRowTotalInclTax);
$this->_saveAppliedTaxes(
$address, array($appliedTax), $taxGroups[$taxId]['tax'], $taxGroups[$taxId]['base_tax'], $taxRate);
}
//We need to calculate weeeAmountInclTax using multiple tax rate here
//because the _calculateWeeeTax and _calculateRowWeeeTax only take one tax rate
if ($this->_weeeHelper->isEnabled() && $this->_weeeHelper->isTaxable()) {
$this->_calculateWeeeAmountInclTax($item, $appliedRates, false);
$this->_calculateWeeeAmountInclTax($item, $appliedRates, true);
}
}
if ($rate > 0) {
$itemTaxGroups[$item->getId()] = $appliedRates;
}
$this->_addAmount($item->getTaxAmount());
$this->_addBaseAmount($item->getBaseTaxAmount());
return;
}
/**
* Calculate item tax amount based on row total
*
* @param Mage_Sales_Model_Quote_Item_Abstract $item
* @param float $rate
* @param array $taxGroups
* @param string $taxId
* @param boolean $recalculateRowTotalInclTax
* @return Mage_Tax_Model_Sales_Total_Quote
*/
protected function _calcRowTaxAmount(
$item, $rate, &$taxGroups = null, $taxId = null, $recalculateRowTotalInclTax = false
)
{
$inclTax = $item->getIsPriceInclTax();
$subtotal = $taxSubtotal = $item->getTaxableAmount();
$baseSubtotal = $baseTaxSubtotal = $item->getBaseTaxableAmount();
$rateKey = ($taxId == null) ? (string)$rate : $taxId;
$isWeeeEnabled = $this->_weeeHelper->isEnabled();
$isWeeeTaxable = $this->_weeeHelper->isTaxable();
$hiddenTax = null;
$baseHiddenTax = null;
$weeeTax = null;
$baseWeeeTax = null;
$rowTaxBeforeDiscount = null;
$baseRowTaxBeforeDiscount = null;
$weeeRowTaxBeforeDiscount = null;
$baseWeeeRowTaxBeforeDiscount = null;
switch ($this->_helper->getCalculationSequence($this->_store)) {
case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_EXCL:
case Mage_Tax_Model_Calculation::CALC_TAX_BEFORE_DISCOUNT_ON_INCL:
$rowTaxBeforeDiscount = $this->_calculator->calcTaxAmount($subtotal, $rate, $inclTax, false);
$baseRowTaxBeforeDiscount = $this->_calculator->calcTaxAmount($baseSubtotal, $rate, $inclTax, false);
if ($isWeeeEnabled && $isWeeeTaxable) {
$weeeRowTaxBeforeDiscount = $this->_calculateRowWeeeTax(0, $item, $rate, false);
$rowTaxBeforeDiscount += $weeeRowTaxBeforeDiscount;
$baseWeeeRowTaxBeforeDiscount = $this->_calculateRowWeeeTax(0, $item, $rate);
$baseRowTaxBeforeDiscount += $baseWeeeRowTaxBeforeDiscount;
}
$rowTaxBeforeDiscount = $rowTax = $this->_calculator->round($rowTaxBeforeDiscount);
$baseRowTaxBeforeDiscount = $baseRowTax = $this->_calculator->round($baseRowTaxBeforeDiscount);
break;
case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_EXCL:
case Mage_Tax_Model_Calculation::CALC_TAX_AFTER_DISCOUNT_ON_INCL:
$discountAmount = $item->getDiscountAmount();
$baseDiscountAmount = $item->getBaseDiscountAmount();
if ($isWeeeEnabled && $this->_weeeHelper->includeInSubtotal()) {
$discountAmount = $discountAmount - $item->getWeeeDiscount();
$baseDiscountAmount = $baseDiscountAmount - $item->getBaseWeeeDiscount();
}
$rowTax = $this->_calculator->calcTaxAmount(
max($subtotal - $discountAmount, 0),
$rate,
$inclTax
);
$baseRowTax = $this->_calculator->calcTaxAmount(
max($baseSubtotal - $baseDiscountAmount, 0),
$rate,
$inclTax
);
if ($isWeeeEnabled && $this->_weeeHelper->isTaxable()) {
$weeeTax = $this->_calculateRowWeeeTax($item->getWeeeDiscount(), $item, $rate, false);
$rowTax += $weeeTax;
$baseWeeeTax = $this->_calculateRowWeeeTax($item->getBaseWeeeDiscount(), $item, $rate);
$baseRowTax += $baseWeeeTax;
}
$rowTax = $this->_calculator->round($rowTax);
$baseRowTax = $this->_calculator->round($baseRowTax);
//Calculate the Row Tax before discount
$rowTaxBeforeDiscount = $this->_calculator->calcTaxAmount(
$subtotal,
$rate,
$inclTax,
false
);
$baseRowTaxBeforeDiscount = $this->_calculator->calcTaxAmount(
$baseSubtotal,
$rate,
$inclTax,
false
);
//Calculate the Weee taxes before discount
$weeeRowTaxBeforeDiscount = 0;
$baseWeeeRowTaxBeforeDiscount = 0;
if ($isWeeeTaxable) {
$weeeRowTaxBeforeDiscount = $this->_calculateRowWeeeTax(0, $item, $rate, false);
$rowTaxBeforeDiscount += $weeeRowTaxBeforeDiscount;
$baseWeeeRowTaxBeforeDiscount = $this->_calculateRowWeeeTax(0, $item, $rate);
$baseRowTaxBeforeDiscount += $baseWeeeRowTaxBeforeDiscount;
}
$rowTaxBeforeDiscount = max(0, $this->_calculator->round($rowTaxBeforeDiscount));
$baseRowTaxBeforeDiscount = max(0, $this->_calculator->round($baseRowTaxBeforeDiscount));
if ($inclTax && $discountAmount > 0) {
$hiddenTax = $rowTaxBeforeDiscount - $rowTax;
$baseHiddenTax = $baseRowTaxBeforeDiscount - $baseRowTax;
$this->_hiddenTaxes[] = array(
'rate_key' => $rateKey,
'qty' => 1,
'item' => $item,
'value' => $hiddenTax,
'base_value' => $baseHiddenTax,
'incl_tax' => $inclTax,
);
} elseif ($discountAmount > $subtotal) { // case with 100% discount on price incl. tax
$hiddenTax = $discountAmount - $subtotal;
$baseHiddenTax = $baseDiscountAmount - $baseSubtotal;
$this->_hiddenTaxes[] = array(
'rate_key' => $rateKey,
'qty' => 1,
'item' => $item,
'value' => $hiddenTax,
'base_value' => $baseHiddenTax,
'incl_tax' => $inclTax,
);
}
// calculate discount compensation
if (!$item->getNoDiscount() && $item->getWeeeTaxApplied()) {
$item->setDiscountTaxCompensation($item->getDiscountTaxCompensation() +
$rowTaxBeforeDiscount - max(0, $rowTax));
}
break;
}
$item->setTaxAmount($item->getTaxAmount() + max(0, $rowTax));
$item->setBaseTaxAmount($item->getBaseTaxAmount() + max(0, $baseRowTax));
if (is_array($taxGroups)) {
$taxGroups[$rateKey]['tax'] = max(0, $rowTax);
$taxGroups[$rateKey]['base_tax'] = max(0, $baseRowTax);
}
$rowTotalInclTax = $item->getRowTotalInclTax();
if (!isset($rowTotalInclTax) || $recalculateRowTotalInclTax) {
if ($this->_config->priceIncludesTax($this->_store)) {
$item->setRowTotalInclTax($subtotal);
$item->setBaseRowTotalInclTax($baseSubtotal);
} else {
$item->setRowTotalInclTax(
$item->getRowTotalInclTax() + $rowTaxBeforeDiscount - $weeeRowTaxBeforeDiscount);
$item->setBaseRowTotalInclTax($item->getBaseRowTotalInclTax() +
$baseRowTaxBeforeDiscount - $baseWeeeRowTaxBeforeDiscount);
}
}
return $this;
}
/**
* Calculate address total tax based on address subtotal
*
* @param Mage_Sales_Model_Quote_Address $address
* @param Varien_Object $taxRateRequest
* @return Mage_Tax_Model_Sales_Total_Quote
*/
protected function _totalBaseCalculation(Mage_Sales_Model_Quote_Address $address, $taxRateRequest)
{
$items = $this->_getAddressItems($address);
$store = $address->getQuote()->getStore();
$taxGroups = array();
$itemTaxGroups = array();
$catalogPriceInclTax = $this->_config->priceIncludesTax($store);
foreach ($items as $item) {
if ($item->getParentItem()) {
continue;
}
if ($item->getHasChildren() && $item->isChildrenCalculated()) {
foreach ($item->getChildren() as $child) {
$this->_totalBaseProcessItemTax(
$child, $taxRateRequest, $taxGroups, $itemTaxGroups, $catalogPriceInclTax);
}
$this->_recalculateParent($item);
} else {
$this->_totalBaseProcessItemTax(
$item, $taxRateRequest, $taxGroups, $itemTaxGroups, $catalogPriceInclTax);
}
}
if ($address->getQuote()->getTaxesForItems()) {
$itemTaxGroups += $address->getQuote()->getTaxesForItems();
}
$address->getQuote()->setTaxesForItems($itemTaxGroups);
foreach ($taxGroups as $taxId => $data) {
if ($catalogPriceInclTax) {
$rate = (float)$taxId;
} else {
$rate = $data['applied_rates'][0]['percent'];
}
$inclTax = $data['incl_tax'];
$totalTax = array_sum($data['tax']);
$baseTotalTax = array_sum($data['base_tax']);
$this->_addAmount($totalTax);
$this->_addBaseAmount($baseTotalTax);
$totalTaxRounded = $this->_calculator->round($totalTax);
$baseTotalTaxRounded = $this->_calculator->round($totalTaxRounded);
$this->_saveAppliedTaxes($address, $data['applied_rates'], $totalTaxRounded, $baseTotalTaxRounded, $rate);
}
return $this;
}
/**
*
* @param Mage_Sales_Model_Quote_Item_Abstract $item
* @param Varien_Object $taxRateRequest
* @param array $taxGroups
* @param array $itemTaxGroups
* @param boolean $catalogPriceInclTax
*/
protected function _totalBaseProcessItemTax(
$item, $taxRateRequest, &$taxGroups, &$itemTaxGroups, $catalogPriceInclTax
)
{
$taxRateRequest->setProductClassId($item->getProduct()->getTaxClassId());
$rate = $this->_calculator->getRate($taxRateRequest);
$item->setTaxAmount(0);
$item->setBaseTaxAmount(0);
$item->setHiddenTaxAmount(0);
$item->setBaseHiddenTaxAmount(0);
$item->setTaxPercent($rate);
$item->setDiscountTaxCompensation(0);
$rowTotalInclTax = $item->getRowTotalInclTax();
$recalculateRowTotalInclTax = false;
if (!isset($rowTotalInclTax)) {
$item->setRowTotalInclTax($item->getTaxableAmount());
$item->setBaseRowTotalInclTax($item->getBaseTaxableAmount());
$recalculateRowTotalInclTax = true;
}
$appliedRates = $this->_calculator->getAppliedRates($taxRateRequest);
if ($catalogPriceInclTax) {
$taxGroups[(string)$rate]['applied_rates'] = $appliedRates;
$taxGroups[(string)$rate]['incl_tax'] = $item->getIsPriceInclTax();
$this->_aggregateTaxPerRate($item, $rate, $taxGroups);
} else {
//need to calculate each tax separately
foreach ($appliedRates as $appliedTax) {
$taxId = $appliedTax['id'];