Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Protected Member Functions
AbstractAggregateCalculator Class Reference
Inheritance diagram for AbstractAggregateCalculator:
AbstractCalculator RowBaseCalculator TotalBaseCalculator

Protected Member Functions

 calculateWithTaxNotInPrice (QuoteDetailsItemInterface $item, $quantity, $round=true)
 
 roundAmount ( $amount, $rate=null, $direction=null, $type=self::KEY_REGULAR_DELTA_ROUNDING, $round=true, $item=null)
 
- Protected Member Functions inherited from AbstractCalculator
 calculateWithTaxInPrice (QuoteDetailsItemInterface $item, $quantity, $round=true)
 
 calculateWithTaxNotInPrice (QuoteDetailsItemInterface $item, $quantity, $round=true)
 
 getAddressRateRequest ()
 
 isSameRateAsStore ($rate, $storeRate)
 
 deltaRound ($price, $rate, $direction, $type=self::KEY_REGULAR_DELTA_ROUNDING, $round=true)
 
 calculatePriceInclTax ($storePriceInclTax, $storeRate, $customerRate, $round=true)
 

Additional Inherited Members

- Public Member Functions inherited from AbstractCalculator
 __construct (TaxClassManagementInterface $taxClassService, TaxDetailsItemInterfaceFactory $taxDetailsItemDataObjectFactory, AppliedTaxInterfaceFactory $appliedTaxDataObjectFactory, AppliedTaxRateInterfaceFactory $appliedTaxRateDataObjectFactory, Calculation $calculationTool, \Magento\Tax\Model\Config $config, $storeId, \Magento\Framework\DataObject $addressRateRequest=null)
 
 setBillingAddress (CustomerAddress $billingAddress)
 
 setShippingAddress (CustomerAddress $shippingAddress)
 
 setCustomerTaxClassId ($customerTaxClassId)
 
 setCustomerId ($customerId)
 
 calculate (QuoteDetailsItemInterface $item, $quantity, $round=true)
 
- Data Fields inherited from AbstractCalculator
const KEY_REGULAR_DELTA_ROUNDING = 'regular'
 
const KEY_APPLIED_TAX_DELTA_ROUNDING = 'applied_tax_amount'
 
const KEY_TAX_BEFORE_DISCOUNT_DELTA_ROUNDING = 'tax_before_discount'
 
- Protected Attributes inherited from AbstractCalculator
 $taxDetailsItemDataObjectFactory
 
 $calculationTool
 
 $storeId
 
 $customerTaxClassId
 
 $customerId
 
 $shippingAddress
 
 $billingAddress
 
 $config
 
 $roundingDeltas
 
 $taxClassManagement
 
 $appliedTaxDataObjectFactory
 
 $appliedTaxRateDataObjectFactory
 

Detailed Description

Definition at line 10 of file AbstractAggregateCalculator.php.

Member Function Documentation

◆ calculateWithTaxNotInPrice()

calculateWithTaxNotInPrice ( QuoteDetailsItemInterface  $item,
  $quantity,
  $round = true 
)
protected

Definition at line 91 of file AbstractAggregateCalculator.php.

92  {
93  $taxRateRequest = $this->getAddressRateRequest()->setProductClassId(
94  $this->taxClassManagement->getTaxClassId($item->getTaxClassKey())
95  );
96  $rate = $this->calculationTool->getRate($taxRateRequest);
97  $appliedRates = $this->calculationTool->getAppliedRates($taxRateRequest);
98 
99  $applyTaxAfterDiscount = $this->config->applyTaxAfterDiscount($this->storeId);
100  $discountAmount = $item->getDiscountAmount();
101  $discountTaxCompensationAmount = 0;
102 
103  // Calculate $rowTotal
104  $price = $this->calculationTool->round($item->getUnitPrice());
105  $rowTotal = $price * $quantity;
106  $rowTaxes = [];
107  $rowTaxesBeforeDiscount = [];
108  $appliedTaxes = [];
109  //Apply each tax rate separately
110  foreach ($appliedRates as $appliedRate) {
111  $taxId = $appliedRate['id'];
112  $taxRate = $appliedRate['percent'];
113  $rowTaxPerRate = $this->calculationTool->calcTaxAmount($rowTotal, $taxRate, false, false);
114  $deltaRoundingType = self::KEY_REGULAR_DELTA_ROUNDING;
115  if ($applyTaxAfterDiscount) {
117  }
118  $rowTaxPerRate = $this->roundAmount($rowTaxPerRate, $taxId, false, $deltaRoundingType, $round, $item);
119  $rowTaxAfterDiscount = $rowTaxPerRate;
120 
121  //Handle discount
122  if ($applyTaxAfterDiscount) {
123  //TODO: handle originalDiscountAmount
124  $taxableAmount = max($rowTotal - $discountAmount, 0);
125  $rowTaxAfterDiscount = $this->calculationTool->calcTaxAmount(
126  $taxableAmount,
127  $taxRate,
128  false,
129  false
130  );
131  $rowTaxAfterDiscount = $this->roundAmount(
132  $rowTaxAfterDiscount,
133  $taxId,
134  false,
135  self::KEY_REGULAR_DELTA_ROUNDING,
136  $round,
137  $item
138  );
139  }
140  $appliedTaxes[$taxId] = $this->getAppliedTax(
141  $rowTaxAfterDiscount,
142  $appliedRate
143  );
144 
145  $rowTaxes[] = $rowTaxAfterDiscount;
146  $rowTaxesBeforeDiscount[] = $rowTaxPerRate;
147  }
148  $rowTax = array_sum($rowTaxes);
149  $rowTaxBeforeDiscount = array_sum($rowTaxesBeforeDiscount);
150  $rowTotalInclTax = $rowTotal + $rowTaxBeforeDiscount;
151  $priceInclTax = $rowTotalInclTax / $quantity;
152 
153  if ($round) {
154  $priceInclTax = $this->calculationTool->round($priceInclTax);
155  }
156 
157  return $this->taxDetailsItemDataObjectFactory->create()
158  ->setCode($item->getCode())
159  ->setType($item->getType())
160  ->setRowTax($rowTax)
161  ->setPrice($price)
162  ->setPriceInclTax($priceInclTax)
163  ->setRowTotal($rowTotal)
164  ->setRowTotalInclTax($rowTotalInclTax)
165  ->setDiscountTaxCompensationAmount($discountTaxCompensationAmount)
166  ->setAssociatedItemCode($item->getAssociatedItemCode())
167  ->setTaxPercent($rate)
168  ->setAppliedTaxes($appliedTaxes);
169  }
$price
roundAmount( $amount, $rate=null, $direction=null, $type=self::KEY_REGULAR_DELTA_ROUNDING, $round=true, $item=null)
$taxRate
Definition: tax_rule.php:12

◆ roundAmount()

roundAmount (   $amount,
  $rate = null,
  $direction = null,
  $type = self::KEY_REGULAR_DELTA_ROUNDING,
  $round = true,
  $item = null 
)
abstractprotected

Round amount

Parameters
float$amount
null | float$rate
null | bool$direction
string$type
bool$round
QuoteDetailsItemInterface$item
Returns
float

The documentation for this class was generated from the following file: