Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Tax.php
Go to the documentation of this file.
1 <?php
11 
13 
19 {
25  protected $_config;
26 
30  protected $_order;
31 
35  protected $_source;
36 
42  public function __construct(
43  \Magento\Framework\View\Element\Template\Context $context,
44  \Magento\Tax\Model\Config $taxConfig,
45  array $data = []
46  ) {
47  $this->_config = $taxConfig;
48  parent::__construct($context, $data);
49  }
50 
56  public function displayFullSummary()
57  {
58  return $this->_config->displaySalesFullSummary($this->getOrder()->getStore());
59  }
60 
66  public function getSource()
67  {
68  return $this->_source;
69  }
70 
76  public function initTotals()
77  {
79  $parent = $this->getParentBlock();
80  $this->_order = $parent->getOrder();
81  $this->_source = $parent->getSource();
82 
83  $store = $this->getStore();
84  $allowTax = $this->_source->getTaxAmount() > 0 || $this->_config->displaySalesZeroTax($store);
85  $grandTotal = (double)$this->_source->getGrandTotal();
86  if (!$grandTotal || $allowTax && !$this->_config->displaySalesTaxWithGrandTotal($store)) {
87  $this->_addTax();
88  }
89 
90  $this->_initSubtotal();
91  $this->_initShipping();
92  $this->_initDiscount();
93  $this->_initGrandTotal();
94  return $this;
95  }
96 
103  protected function _addTax($after = 'discount')
104  {
105  $taxTotal = new \Magento\Framework\DataObject(['code' => 'tax', 'block_name' => $this->getNameInLayout()]);
106  $this->getParentBlock()->addTotal($taxTotal, $after);
107  return $this;
108  }
109 
115  public function getStore()
116  {
117  return $this->_order->getStore();
118  }
119 
124  protected function _initSubtotal()
125  {
126  $store = $this->getStore();
127  $parent = $this->getParentBlock();
128  $subtotal = $parent->getTotal('subtotal');
129  if (!$subtotal) {
130  return $this;
131  }
132  if ($this->_config->displaySalesSubtotalBoth($store)) {
133  $subtotal = (double)$this->_source->getSubtotal();
134  $baseSubtotal = (double)$this->_source->getBaseSubtotal();
135  $subtotalIncl = (double)$this->_source->getSubtotalInclTax();
136  $baseSubtotalIncl = (double)$this->_source->getBaseSubtotalInclTax();
137 
138  if (!$subtotalIncl || !$baseSubtotalIncl) {
139  // Calculate the subtotal if it is not set
140  $subtotalIncl = $subtotal
141  + $this->_source->getTaxAmount()
142  - $this->_source->getShippingTaxAmount();
143  $baseSubtotalIncl = $baseSubtotal
144  + $this->_source->getBaseTaxAmount()
145  - $this->_source->getBaseShippingTaxAmount();
146 
147  if ($this->_source instanceof Order) {
148  // Adjust for the discount tax compensation
149  foreach ($this->_source->getAllItems() as $item) {
150  $subtotalIncl += $item->getDiscountTaxCompensationAmount();
151  $baseSubtotalIncl += $item->getBaseDiscountTaxCompensationAmount();
152  }
153  }
154  }
155 
156  $subtotalIncl = max(0, $subtotalIncl);
157  $baseSubtotalIncl = max(0, $baseSubtotalIncl);
158  $totalExcl = new \Magento\Framework\DataObject(
159  [
160  'code' => 'subtotal_excl',
161  'value' => $subtotal,
162  'base_value' => $baseSubtotal,
163  'label' => __('Subtotal (Excl.Tax)'),
164  ]
165  );
166  $totalIncl = new \Magento\Framework\DataObject(
167  [
168  'code' => 'subtotal_incl',
169  'value' => $subtotalIncl,
170  'base_value' => $baseSubtotalIncl,
171  'label' => __('Subtotal (Incl.Tax)'),
172  ]
173  );
174  $parent->addTotal($totalExcl, 'subtotal');
175  $parent->addTotal($totalIncl, 'subtotal_excl');
176  $parent->removeTotal('subtotal');
177  } elseif ($this->_config->displaySalesSubtotalInclTax($store)) {
178  $subtotalIncl = (double)$this->_source->getSubtotalInclTax();
179  $baseSubtotalIncl = (double)$this->_source->getBaseSubtotalInclTax();
180 
181  if (!$subtotalIncl) {
182  $subtotalIncl = $this->_source->getSubtotal() +
183  $this->_source->getTaxAmount() -
184  $this->_source->getShippingTaxAmount();
185  }
186  if (!$baseSubtotalIncl) {
187  $baseSubtotalIncl = $this->_source->getBaseSubtotal() +
188  $this->_source->getBaseTaxAmount() -
189  $this->_source->getBaseShippingTaxAmount();
190  }
191 
192  $total = $parent->getTotal('subtotal');
193  if ($total) {
194  $total->setValue(max(0, $subtotalIncl));
195  $total->setBaseValue(max(0, $baseSubtotalIncl));
196  }
197  }
198  return $this;
199  }
200 
204  protected function _initShipping()
205  {
206  $store = $this->getStore();
207  $parent = $this->getParentBlock();
208  $shipping = $parent->getTotal('shipping');
209  if (!$shipping) {
210  return $this;
211  }
212 
213  if ($this->_config->displaySalesShippingBoth($store)) {
214  $shipping = (double)$this->_source->getShippingAmount();
215  $baseShipping = (double)$this->_source->getBaseShippingAmount();
216  $shippingIncl = (double)$this->_source->getShippingInclTax();
217  if (!$shippingIncl) {
218  $shippingIncl = $shipping + (double)$this->_source->getShippingTaxAmount();
219  }
220  $baseShippingIncl = (double)$this->_source->getBaseShippingInclTax();
221  if (!$baseShippingIncl) {
222  $baseShippingIncl = $baseShipping + (double)$this->_source->getBaseShippingTaxAmount();
223  }
224 
225  $totalExcl = new \Magento\Framework\DataObject(
226  [
227  'code' => 'shipping',
228  'value' => $shipping,
229  'base_value' => $baseShipping,
230  'label' => __('Shipping & Handling (Excl.Tax)'),
231  ]
232  );
233  $totalIncl = new \Magento\Framework\DataObject(
234  [
235  'code' => 'shipping_incl',
236  'value' => $shippingIncl,
237  'base_value' => $baseShippingIncl,
238  'label' => __('Shipping & Handling (Incl.Tax)'),
239  ]
240  );
241  $parent->addTotal($totalExcl, 'shipping');
242  $parent->addTotal($totalIncl, 'shipping');
243  } elseif ($this->_config->displaySalesShippingInclTax($store)) {
244  $shippingIncl = $this->_source->getShippingInclTax();
245  if (!$shippingIncl) {
246  $shippingIncl = $this->_source->getShippingAmount() + $this->_source->getShippingTaxAmount();
247  }
248  $baseShippingIncl = $this->_source->getBaseShippingInclTax();
249  if (!$baseShippingIncl) {
250  $baseShippingIncl = $this->_source->getBaseShippingAmount() +
251  $this->_source->getBaseShippingTaxAmount();
252  }
253  $total = $parent->getTotal('shipping');
254  if ($total) {
255  $total->setValue($shippingIncl);
256  $total->setBaseValue($baseShippingIncl);
257  }
258  }
259  return $this;
260  }
261 
265  protected function _initDiscount()
266  {
267  }
268 
272  protected function _initGrandTotal()
273  {
274  $store = $this->getStore();
275  $parent = $this->getParentBlock();
276  $grandototal = $parent->getTotal('grand_total');
277  if (!$grandototal || !(double)$this->_source->getGrandTotal()) {
278  return $this;
279  }
280 
281  if ($this->_config->displaySalesTaxWithGrandTotal($store)) {
282  $grandtotal = $this->_source->getGrandTotal();
283  $baseGrandtotal = $this->_source->getBaseGrandTotal();
284  $grandtotalExcl = $grandtotal - $this->_source->getTaxAmount();
285  $baseGrandtotalExcl = $baseGrandtotal - $this->_source->getBaseTaxAmount();
286  $grandtotalExcl = max($grandtotalExcl, 0);
287  $baseGrandtotalExcl = max($baseGrandtotalExcl, 0);
288  $totalExcl = new \Magento\Framework\DataObject(
289  [
290  'code' => 'grand_total',
291  'strong' => true,
292  'value' => $grandtotalExcl,
293  'base_value' => $baseGrandtotalExcl,
294  'label' => __('Grand Total (Excl.Tax)'),
295  ]
296  );
297  $totalIncl = new \Magento\Framework\DataObject(
298  [
299  'code' => 'grand_total_incl',
300  'strong' => true,
301  'value' => $grandtotal,
302  'base_value' => $baseGrandtotal,
303  'label' => __('Grand Total (Incl.Tax)'),
304  ]
305  );
306  $parent->addTotal($totalExcl, 'grand_total');
307  $this->_addTax('grand_total');
308  $parent->addTotal($totalIncl, 'tax');
309  }
310  return $this;
311  }
312 
316  public function getOrder()
317  {
318  return $this->_order;
319  }
320 
324  public function getLabelProperties()
325  {
326  return $this->getParentBlock()->getLabelProperties();
327  }
328 
332  public function getValueProperties()
333  {
334  return $this->getParentBlock()->getValueProperties();
335  }
336 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Tax\Model\Config $taxConfig, array $data=[])
Definition: Tax.php:42
__()
Definition: __.php:13
_addTax($after='discount')
Definition: Tax.php:103