public function testGetDefaultRateRequest() { $customerDataSet = $this->customerRepository->getById(self::FIXTURE_CUSTOMER_ID); $address = $this->addressRepository->getById(self::FIXTURE_ADDRESS_ID); $rateRequest = $this->_model->getRateRequest(null, null, null, null, $customerDataSet->getId()); $this->assertNotNull($rateRequest); $this->assertEquals($address->getCountryId(), $rateRequest->getCountryId()); $this->assertEquals($address->getRegion()->getRegionId(), $rateRequest->getRegionId()); $this->assertEquals($address->getPostcode(), $rateRequest->getPostcode()); $customerTaxClassId = $this->groupRepository->getById($customerDataSet->getGroupId())->getTaxClassId(); $this->assertEquals($customerTaxClassId, $rateRequest->getCustomerClassId()); }
/** * test for method afterPrepareAdjustmentConfig */ public function testAfterPrepareAdjustmentConfig() { $this->productMock->expects($this->once())->method('getTaxClassId')->will($this->returnValue('tax-class-id')); $this->calculationMock->expects($this->exactly(2))->method('getRateRequest')->will($this->returnValue($this->rateRequestMock)); $this->calculationMock->expects($this->exactly(2))->method('getRate')->with($this->equalTo($this->rateRequestMock))->will($this->returnValue(99.09999999999999)); $this->productMock->expects($this->once())->method('getPriceInfo')->will($this->returnValue($this->priceInfoMock)); $this->priceInfoMock->expects($this->once())->method('getAdjustment')->with($this->equalTo(\Magento\Tax\Pricing\Adjustment::ADJUSTMENT_CODE))->will($this->returnValue($this->adjustmentMock)); $this->adjustmentMock->expects($this->once())->method('isIncludedInBasePrice')->will($this->returnValue(true)); $this->taxHelperMock->expects($this->once())->method('displayPriceIncludingTax')->will($this->returnValue(true)); $this->taxHelperMock->expects($this->once())->method('displayBothPrices')->will($this->returnValue(true)); $expected = ['product' => $this->productMock, 'defaultTax' => 99.09999999999999, 'currentTax' => 99.09999999999999, 'customerId' => 1, 'includeTax' => true, 'showIncludeTax' => true, 'showBothPrices' => true]; $this->assertEquals($expected, $this->plugin->afterPrepareAdjustmentConfig($this->attributePriceMock, ['product' => $this->productMock, 'defaultTax' => 0, 'currentTax' => 0, 'customerId' => 1])); }
/** * Get array of arrays with tax information for display in PDF * array( * $index => array( * 'amount' => $amount, * 'label' => $label, * 'font_size'=> $font_size * ) * ) * * @return array * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function getFullTaxInfo() { $fontSize = $this->getFontSize() ? $this->getFontSize() : 7; $taxClassAmount = $this->_taxHelper->getCalculatedTaxes($this->getOrder()); if (!empty($taxClassAmount)) { foreach ($taxClassAmount as &$tax) { $percent = $tax['percent'] ? ' (' . $tax['percent'] . '%)' : ''; $tax['amount'] = $this->getAmountPrefix() . $this->getOrder()->formatPriceTxt($tax['tax_amount']); $tax['label'] = __($tax['title']) . $percent . ':'; $tax['font_size'] = $fontSize; } } else { /** @var $orders \Magento\Tax\Model\Resource\Sales\Order\Tax\Collection */ $orders = $this->_taxOrdersFactory->create(); $rates = $orders->loadByOrder($this->getOrder())->toArray(); $fullInfo = $this->_taxCalculation->reproduceProcess($rates['items']); $tax_info = []; if ($fullInfo) { foreach ($fullInfo as $info) { if (isset($info['hidden']) && $info['hidden']) { continue; } $_amount = $info['amount']; foreach ($info['rates'] as $rate) { $percent = $rate['percent'] ? ' (' . $rate['percent'] . '%)' : ''; $tax_info[] = ['amount' => $this->getAmountPrefix() . $this->getOrder()->formatPriceTxt($_amount), 'label' => __($rate['title']) . $percent . ':', 'font_size' => $fontSize]; } } } $taxClassAmount = $tax_info; } return $taxClassAmount; }
/** * Calculate the row total for an item * * @param QuoteDetailsItem $item * @return float */ protected function calcRowTotal(QuoteDetailsItem $item) { $qty = $this->getTotalQuantity($item); // Round unit price before multiplying to prevent losing 1 cent on subtotal $total = $this->calculator->round($item->getUnitPrice()) * $qty; return $this->calculator->round($total); }
/** * Get full information about taxes applied to order * * @return array */ public function getFullTaxInfo() { $source = $this->getSource(); if (!$source instanceof \Magento\Sales\Model\Order\Invoice && !$source instanceof \Magento\Sales\Model\Order\Creditmemo) { $source = $this->getOrder(); } $taxClassAmount = []; if (empty($source)) { return $taxClassAmount; } $taxClassAmount = $this->_taxHelper->getCalculatedTaxes($source); if (empty($taxClassAmount)) { $rates = $this->_taxOrderFactory->create()->getCollection()->loadByOrder($source)->toArray(); $taxClassAmount = $this->_taxCalculation->reproduceProcess($rates['items']); } return $taxClassAmount; }
/** * Given a store price that includes tax at the store rate, this function will back out the store's tax, and add in * the customer's tax. Returns this new price which is the customer's price including tax. * * @param float $storePriceInclTax * @param float $storeRate * @param float $customerRate * @return float */ protected function calculatePriceInclTax($storePriceInclTax, $storeRate, $customerRate) { $storeTax = $this->calculationTool->calcTaxAmount($storePriceInclTax, $storeRate, true, false); $priceExclTax = $storePriceInclTax - $storeTax; $customerTax = $this->calculationTool->calcTaxAmount($priceExclTax, $customerRate, false, false); $customerPriceInclTax = $this->calculationTool->round($priceExclTax + $customerTax); return $customerPriceInclTax; }
/** * Get full information about taxes applied to order * * @return array */ public function getFullTaxInfo() { /** @var $source \Magento\Sales\Model\Order */ $source = $this->getOrder(); $taxClassAmount = array(); if ($source instanceof \Magento\Sales\Model\Order) { $taxClassAmount = $this->_taxHelper->getCalculatedTaxes($source); if (empty($taxClassAmount)) { $rates = $this->_taxOrderFactory->create()->getCollection()->loadByOrder($source)->toArray(); $taxClassAmount = $this->_taxCalculation->reproduceProcess($rates['items']); } } return $taxClassAmount; }
/** * @return void */ public function saveCalculationData() { $ctc = $this->getData('customer_tax_class_ids'); $ptc = $this->getData('product_tax_class_ids'); $rates = $this->getData('tax_rate_ids'); $this->_calculation->deleteByRuleId($this->getId()); foreach ($ctc as $c) { foreach ($ptc as $p) { foreach ($rates as $r) { $dataArray = ['tax_calculation_rule_id' => $this->getId(), 'tax_calculation_rate_id' => $r, 'customer_tax_class_id' => $c, 'product_tax_class_id' => $p]; $this->_calculation->setData($dataArray)->save(); } } } }
/** * Calculate row information for item based on children calculation * * @param TaxDetailsItemInterface[] $children * @param int $quantity * @return TaxDetailsItemInterface */ protected function calculateParent($children, $quantity) { $rowTotal = 0.0; $rowTotalInclTax = 0.0; $rowTax = 0.0; $taxableAmount = 0.0; foreach ($children as $child) { $rowTotal += $child->getRowTotal(); $rowTotalInclTax += $child->getRowTotalInclTax(); $rowTax += $child->getRowTax(); $taxableAmount += $child->getTaxableAmount(); } $price = $this->calculationTool->round($rowTotal / $quantity); $priceInclTax = $this->calculationTool->round($rowTotalInclTax / $quantity); $taxDetailsItemDataObject = $this->taxDetailsItemDataObjectFactory->create()->setPrice($price)->setPriceInclTax($priceInclTax)->setRowTotal($rowTotal)->setRowTotalInclTax($rowTotalInclTax)->setRowTax($rowTax); return $taxDetailsItemDataObject; }
/** * @return array */ public function getFullTaxInfo() { $rates = $this->_orderTaxCollectionFactory->create()->loadByOrder($this)->toArray(); return $this->_taxCalculation->reproduceProcess($rates['items']); }
/** * Apply Tax Rate * * @param int $classId * @param null $shippingAddress * @param null $billingAddress * @param null $customerTaxClass * @param int|null $customerId * @return float */ protected function applyRate($classId, $shippingAddress = null, $billingAddress = null, $customerTaxClass = null, $customerId = null) { $rateRequest = $this->calculation->getRateRequest($shippingAddress, $billingAddress, $customerTaxClass, null, $customerId); $rateRequest->setProductClassId($classId); return $this->calculation->getRate($rateRequest); }
/** * Calculate item fixed tax and prepare information for discount and regular taxation * * @param \Magento\Sales\Model\Quote\Address $address * @param \Magento\Sales\Model\Quote\Item\AbstractItem $item * @return void|$this */ protected function _process(\Magento\Sales\Model\Quote\Address $address, $item) { if (!$this->_weeeData->isEnabled($this->_store)) { return $this; } $attributes = $this->_weeeData->getProductWeeeAttributes($item->getProduct(), $address, $address->getQuote()->getBillingAddress(), $this->_store->getWebsiteId()); $applied = array(); $productTaxes = array(); $defaultRateRequest = $this->_calculator->getRateOriginRequest($this->_store); $rateRequest = $this->_calculator->getRateRequest($address, $address->getQuote()->getBillingAddress(), $address->getQuote()->getCustomerTaxClassId(), $this->_store); $totalValueInclTax = 0; $baseTotalValueInclTax = 0; $totalRowValueInclTax = 0; $baseTotalRowValueInclTax = 0; $totalValueExclTax = 0; $baseTotalValueExclTax = 0; $totalRowValueExclTax = 0; $baseTotalRowValueExclTax = 0; $priceIncludesTax = $this->_taxData->priceIncludesTax($this->_store); $calculationAlgorithm = $this->_taxData->getCalculationAgorithm($this->_store); $defaultPercent = $currentPercent = 0; //when FPT is not taxable foreach ($attributes as $key => $attribute) { $title = $attribute->getName(); $baseValue = $attribute->getAmount(); $value = $this->_store->convertPrice($baseValue); $value = $this->_store->roundPrice($value); if ($this->_weeeData->isTaxable($this->_store)) { $defaultPercent = $this->_calculator->getRate($defaultRateRequest->setProductClassId($item->getProduct()->getTaxClassId())); $currentPercent = $this->_calculator->getRate($rateRequest->setProductClassId($item->getProduct()->getTaxClassId())); } if ($priceIncludesTax) { //Make sure that price including tax is rounded first $baseValueInclTax = $baseValue / (100 + $defaultPercent) * (100 + $currentPercent); $baseValueInclTax = $this->_store->roundPrice($baseValueInclTax); $valueInclTax = $value / (100 + $defaultPercent) * (100 + $currentPercent); $valueInclTax = $this->_store->roundPrice($valueInclTax); $baseValueExclTax = $baseValueInclTax / (100 + $currentPercent) * 100; $valueExclTax = $valueInclTax / (100 + $currentPercent) * 100; if ($calculationAlgorithm == Calculation::CALC_UNIT_BASE) { $baseValueExclTax = $this->_store->roundPrice($baseValueExclTax); $valueExclTax = $this->_store->roundPrice($valueExclTax); } } else { $valueExclTax = $value; $baseValueExclTax = $baseValue; $valueInclTax = $valueExclTax * (100 + $currentPercent) / 100; $baseValueInclTax = $baseValueExclTax * (100 + $currentPercent) / 100; if ($calculationAlgorithm == Calculation::CALC_UNIT_BASE) { $baseValueInclTax = $this->_store->roundPrice($baseValueInclTax); $valueInclTax = $this->_store->roundPrice($valueInclTax); } } $rowValueInclTax = $this->_store->roundPrice($valueInclTax * $item->getTotalQty()); $baseRowValueInclTax = $this->_store->roundPrice($baseValueInclTax * $item->getTotalQty()); $rowValueExclTax = $this->_store->roundPrice($valueExclTax * $item->getTotalQty()); $baseRowValueExclTax = $this->_store->roundPrice($baseValueExclTax * $item->getTotalQty()); //Now, round the unit price just in case $valueExclTax = $this->_store->roundPrice($valueExclTax); $baseValueExclTax = $this->_store->roundPrice($baseValueExclTax); $valueInclTax = $this->_store->roundPrice($valueInclTax); $baseValueInclTax = $this->_store->roundPrice($baseValueInclTax); $totalValueInclTax += $valueInclTax; $baseTotalValueInclTax += $baseValueInclTax; $totalRowValueInclTax += $rowValueInclTax; $baseTotalRowValueInclTax += $baseRowValueInclTax; $totalValueExclTax += $valueExclTax; $baseTotalValueExclTax += $baseValueExclTax; $totalRowValueExclTax += $rowValueExclTax; $baseTotalRowValueExclTax += $baseRowValueExclTax; $productTaxes[] = array('title' => $title, 'base_amount' => $baseValueExclTax, 'amount' => $valueExclTax, 'row_amount' => $rowValueExclTax, 'base_row_amount' => $baseRowValueExclTax, 'base_amount_incl_tax' => $baseValueInclTax, 'amount_incl_tax' => $valueInclTax, 'row_amount_incl_tax' => $rowValueInclTax, 'base_row_amount_incl_tax' => $baseRowValueInclTax); //This include FPT as applied tax, since tax on FPT is calculated separately, we use value excluding tax $applied[] = array('id' => $attribute->getCode(), 'percent' => null, 'hidden' => $this->_weeeData->includeInSubtotal($this->_store), 'rates' => array(array('base_real_amount' => $baseRowValueExclTax, 'base_amount' => $baseRowValueExclTax, 'amount' => $rowValueExclTax, 'code' => $attribute->getCode(), 'title' => $title, 'percent' => null, 'position' => 1, 'priority' => -1000 + $key))); } $item->setWeeeTaxAppliedAmount($totalValueExclTax)->setBaseWeeeTaxAppliedAmount($baseTotalValueExclTax)->setWeeeTaxAppliedRowAmount($totalRowValueExclTax)->setBaseWeeeTaxAppliedRowAmnt($baseTotalRowValueExclTax); $item->setWeeeTaxAppliedAmountInclTax($totalValueInclTax)->setBaseWeeeTaxAppliedAmountInclTax($baseTotalValueInclTax)->setWeeeTaxAppliedRowAmountInclTax($totalRowValueInclTax)->setBaseWeeeTaxAppliedRowAmntInclTax($baseTotalRowValueInclTax); if ($priceIncludesTax) { $this->_processTaxSettings($item, $totalValueInclTax, $baseTotalValueInclTax, $totalRowValueInclTax, $baseTotalRowValueInclTax); } else { $this->_processTaxSettings($item, $totalValueExclTax, $baseTotalValueExclTax, $totalRowValueExclTax, $baseTotalRowValueExclTax); } $this->_processTotalAmount($address, $totalRowValueExclTax, $baseTotalRowValueExclTax, $totalRowValueInclTax, $baseTotalRowValueInclTax); $this->_weeeData->setApplied($item, array_merge($this->_weeeData->getApplied($item), $productTaxes)); //Update the applied taxes for the quote if ($applied) { $this->_saveAppliedTaxes($address, $applied, $item->getWeeeTaxAppliedAmount(), $item->getBaseWeeeTaxAppliedAmount(), null); } }