/**
  * @param Observer $observer
  * @return void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function execute(Observer $observer)
 {
     if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && $this->weeeHelper->isEnabled()) {
         /** @var \Magento\Customer\Model\Data\Customer $customer */
         $customer = $observer->getData('customer');
         /** @var \Magento\Customer\Api\Data\AddressInterface[] $addresses */
         $addresses = $customer->getAddresses();
         if (isset($addresses)) {
             $defaultShippingFound = false;
             $defaultBillingFound = false;
             foreach ($addresses as $address) {
                 if ($address->isDefaultBilling()) {
                     $defaultBillingFound = true;
                     $this->customerSession->setDefaultTaxBillingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegion()->getRegionId() : null, 'postcode' => $address->getPostcode()]);
                 }
                 if ($address->isDefaultShipping()) {
                     $defaultShippingFound = true;
                     $this->customerSession->setDefaultTaxShippingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegion()->getRegionId() : null, 'postcode' => $address->getPostcode()]);
                 }
                 if ($defaultShippingFound && $defaultBillingFound) {
                     break;
                 }
             }
         }
     }
 }
 /**
  * Returns which product price to use as a basis for the Weee's final price
  *
  * @param  int|null $storeId
  * @return string
  */
 protected function getWhichCalcPriceToUse($storeId = null)
 {
     $calcPrice = 'finalPrice';
     if ($this->weeeData->geDisplayExcl($storeId) || $this->weeeData->geDisplayExlDescIncl($storeId) || $this->taxData->priceIncludesTax() && $this->taxData->displayPriceExcludingTax()) {
         $calcPrice = 'basePrice';
     }
     return $calcPrice;
 }
 /**
  * @param Observer $observer
  * @return void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function execute(Observer $observer)
 {
     if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && $this->weeeHelper->isEnabled()) {
         /** @var $customerAddress Address */
         $address = $observer->getCustomerAddress();
         // Check if the address is either the default billing, shipping, or both
         if ($address->getIsPrimaryBilling() || $address->getIsDefaultBilling()) {
             $this->customerSession->setDefaultTaxBillingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]);
         }
         if ($address->getIsPrimaryShipping() || $address->getIsDefaultShipping()) {
             $this->customerSession->setDefaultTaxShippingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]);
         }
     }
 }
Example #4
0
 /**
  * Check if weee total amount should be included
  * array(
  *  $index => array(
  *      'amount'   => $amount,
  *      'label'    => $label,
  *      'font_size'=> $font_size
  *  )
  * )
  * @return array
  */
 public function getTotalsForDisplay()
 {
     /** @var $items \Magento\Sales\Model\Order\Item[] */
     $items = $this->getSource()->getAllItems();
     $store = $this->getSource()->getStore();
     $weeeTotal = $this->_weeeData->getTotalAmounts($items, $store);
     // If we have no Weee, check if we still need to display it
     if (!$weeeTotal && !filter_var($this->getDisplayZero(), FILTER_VALIDATE_BOOLEAN)) {
         return [];
     }
     // Display the Weee total amount
     $fontSize = $this->getFontSize() ? $this->getFontSize() : 7;
     $totals = [['amount' => $this->getOrder()->formatPriceTxt($weeeTotal), 'label' => __($this->getTitle()) . ':', 'font_size' => $fontSize]];
     return $totals;
 }
Example #5
0
 /**
  * Modify the options config for the front end to resemble the weee final price
  *
  * @param   \Magento\Framework\Event\Observer $observer
  * @return  $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function getPriceConfiguration(\Magento\Framework\Event\Observer $observer)
 {
     if ($this->_weeeData->isEnabled()) {
         $priceConfigObj = $observer->getData('configObj');
         $priceConfig = $priceConfigObj->getConfig();
         try {
             if (is_array($priceConfig)) {
                 foreach ($priceConfig as $keyConfigs => $configs) {
                     if (is_array($configs)) {
                         foreach ($configs as $keyConfig => $config) {
                             $calcPrice = 'finalPrice';
                             if ($this->_taxData->priceIncludesTax() && $this->_taxData->displayPriceExcludingTax()) {
                                 $calcPrice = 'basePrice';
                             }
                             if (array_key_exists('prices', $configs)) {
                                 $priceConfig[$keyConfigs]['prices']['weeePrice'] = ['amount' => $configs['prices'][$calcPrice]['amount']];
                             } else {
                                 foreach ($configs as $keyConfig => $config) {
                                     $priceConfig[$keyConfigs][$keyConfig]['prices']['weeePrice'] = ['amount' => $config['prices'][$calcPrice]['amount']];
                                 }
                             }
                         }
                     }
                 }
             }
             $priceConfigObj->setConfig($priceConfig);
         } catch (Exception $e) {
             return $this;
         }
     }
     return $this;
 }
Example #6
0
 /**
  * Change default JavaScript templates for options rendering
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  */
 public function updateProductOptions(\Magento\Framework\Event\Observer $observer)
 {
     $response = $observer->getEvent()->getResponseObject();
     $options = $response->getAdditionalOptions();
     /** @var \Magento\Catalog\Model\Product $product */
     $product = $this->_registry->registry('current_product');
     if (!$product) {
         return $this;
     }
     if ($this->_weeeData->isEnabled() && !$this->_weeeData->geDisplayIncl($product->getStoreId()) && !$this->_weeeData->geDisplayExcl($product->getStoreId())) {
         // only do processing on bundle product
         if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
             if (!array_key_exists('optionTemplate', $options)) {
                 $options['optionTemplate'] = '<%- data.label %>' . '<% if (data.finalPrice.value) { %>' . ' +<%- data.finalPrice.formatted %>' . '<% } %>';
             }
             foreach ($this->_weeeData->getWeeAttributesForBundle($product) as $weeAttribute) {
                 $options['optionTemplate'] .= sprintf(' <%% if (data.weeePrice' . $weeAttribute->getCode() . ') { %%>' . '  (' . $weeAttribute->getName() . ':<%%= data.weeePrice' . $weeAttribute->getCode() . '.formatted %%>)' . '<%% } %%>');
             }
             if ($this->_weeeData->geDisplayExlDescIncl($product->getStoreId())) {
                 $options['optionTemplate'] .= sprintf(' <%% if (data.weeePrice) { %%>' . '<%%= data.weeePrice.formatted %%>' . '<%% } %%>');
             }
         }
     }
     $response->setAdditionalOptions($options);
     return $this;
 }
Example #7
0
    /**
     * @param \Magento\Framework\App\ActionInterface $subject
     * @param callable $proceed
     * @param \Magento\Framework\App\RequestInterface $request
     * @return mixed
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function aroundExecute(
        \Magento\Framework\App\ActionInterface $subject,
        \Closure $proceed,
        \Magento\Framework\App\RequestInterface $request
    ) {
        if (!$this->weeeHelper->isEnabled() ||
            !$this->customerSession->isLoggedIn() ||
            !$this->moduleManager->isEnabled('Magento_PageCache') ||
            !$this->cacheConfig->isEnabled()) {
            return $proceed($request);
        }

        $basedOn = $this->taxHelper->getTaxBasedOn();
        if ($basedOn != 'shipping' && $basedOn != 'billing') {
            return $proceed($request);
        }

        $weeeTaxRegion = $this->getWeeeTaxRegion($basedOn);
        $websiteId = $this->storeManager->getStore()->getWebsiteId();
        $countryId = $weeeTaxRegion['countryId'];
        $regionId = $weeeTaxRegion['regionId'];

        if (!$countryId && !$regionId) {
            // country and region does not exist
            return $proceed($request);
        } else if ($countryId && !$regionId) {
            // country exist and region does not exist
            $regionId = 0;
            $exist = $this->weeeTax->isWeeeInLocation(
                $countryId,
                $regionId,
                $websiteId
            );
        } else {
            // country and region exist
            $exist = $this->weeeTax->isWeeeInLocation(
                $countryId,
                $regionId,
                $websiteId
            );
            if (!$exist) {
                // just check the country for weee
                $regionId = 0;
                $exist = $this->weeeTax->isWeeeInLocation(
                    $countryId,
                    $regionId,
                    $websiteId
                );
            }
        }

        if ($exist) {
            $this->httpContext->setValue(
                'weee_tax_region',
                ['countryId' => $countryId, 'regionId' => $regionId],
                0
            );
        }
        return $proceed($request);
    }
Example #8
0
 /**
  * @param $creditmemoItemData array
  * @return \Magento\Sales\Model\Order\Creditmemo\Item|\PHPUnit_Framework_MockObject_MockObject
  */
 protected function getInvoiceItem($creditmemoItemData)
 {
     /** @var \Magento\Sales\Model\Order\Item|\PHPUnit_Framework_MockObject_MockObject $orderItem */
     $orderItem = $this->getMock('\\Magento\\Sales\\Model\\Order\\Item', ['isDummy', '__wakeup'], [], '', false);
     foreach ($creditmemoItemData['order_item'] as $key => $value) {
         $orderItem->setData($key, $value);
     }
     $this->weeeData->expects($this->once())->method('getRowWeeeTaxInclTax')->with($orderItem)->will($this->returnValue($orderItem->getRowWeeeTaxInclTax()));
     $this->weeeData->expects($this->once())->method('getBaseRowWeeeTaxInclTax')->with($orderItem)->will($this->returnValue($orderItem->getBaseRowWeeeTaxInclTax()));
     $this->weeeData->expects($this->once())->method('getWeeeAmountInvoiced')->with($orderItem)->will($this->returnValue($orderItem->getWeeeAmountInvoiced()));
     $this->weeeData->expects($this->once())->method('getBaseWeeeAmountInvoiced')->with($orderItem)->will($this->returnValue($orderItem->getBaseWeeeAmountInvoiced()));
     $this->weeeData->expects($this->once())->method('getWeeeTaxAmountInvoiced')->with($orderItem)->will($this->returnValue($orderItem->getWeeeTaxAmountInvoiced()));
     $this->weeeData->expects($this->once())->method('getBaseWeeeTaxAmountInvoiced')->with($orderItem)->will($this->returnValue($orderItem->getBaseWeeeTaxAmountInvoiced()));
     $this->weeeData->expects($this->once())->method('getWeeeAmountRefunded')->with($orderItem)->will($this->returnValue($orderItem->getWeeeAmountRefunded()));
     $this->weeeData->expects($this->once())->method('getBaseWeeeAmountRefunded')->with($orderItem)->will($this->returnValue($orderItem->getBaseWeeeAmountRefunded()));
     $this->weeeData->expects($this->once())->method('getWeeeTaxAmountRefunded')->with($orderItem)->will($this->returnValue($orderItem->getWeeeTaxAmountRefunded()));
     $this->weeeData->expects($this->once())->method('getBaseWeeeTaxAmountRefunded')->with($orderItem)->will($this->returnValue($orderItem->getBaseWeeeTaxAmountRefunded()));
     /** @var \Magento\Sales\Model\Order\Invoice\Item|\PHPUnit_Framework_MockObject_MockObject $invoiceItem */
     $invoiceItem = $this->getMock('\\Magento\\Sales\\Model\\Order\\Invoice\\Item', ['getOrderItem', 'isLast', '__wakeup'], [], '', false);
     $invoiceItem->expects($this->any())->method('getOrderItem')->will($this->returnValue($orderItem));
     $invoiceItem->expects($this->any())->method('isLast')->will($this->returnValue($creditmemoItemData['is_last']));
     foreach ($creditmemoItemData['data_fields'] as $key => $value) {
         $invoiceItem->setData($key, $value);
     }
     $this->weeeData->expects($this->any())->method('getApplied')->will($this->returnCallback(function ($item) {
         return $item->getAppliedWeee();
     }));
     $this->weeeData->expects($this->any())->method('setApplied')->will($this->returnCallback(function ($item, $weee) {
         return $item->setAppliedWeee($weee);
     }));
     return $invoiceItem;
 }
Example #9
0
 /**
  * Process bundle options selection for prepare view json
  *
  * @param   \Magento\Framework\Event\Observer $observer
  * @return  $this
  */
 public function updateBundleProductOptions(\Magento\Framework\Event\Observer $observer)
 {
     if (!$this->_weeeData->isEnabled()) {
         return $this;
     }
     $response = $observer->getEvent()->getResponseObject();
     $selection = $observer->getEvent()->getSelection();
     $options = $response->getAdditionalOptions();
     $_product = $this->_registry->registry('current_product');
     $typeDynamic = \Magento\Bundle\Block\Adminhtml\Catalog\Product\Edit\Tab\Attributes\Extend::DYNAMIC;
     if (!$_product || $_product->getPriceType() != $typeDynamic) {
         return $this;
     }
     $amount = $this->_weeeData->getAmount($selection);
     $attributes = $this->_weeeData->getProductWeeeAttributes($_product, null, null, null, $this->_weeeData->isTaxable());
     $amountInclTaxes = $this->_weeeData->getAmountInclTaxes($attributes);
     $taxes = $amountInclTaxes - $amount;
     $options['plusDisposition'] = $amount;
     $options['plusDispositionTax'] = $taxes < 0 ? 0 : $taxes;
     // Exclude Weee amount from excluding tax amount
     if (!$this->_weeeData->typeOfDisplay(array(0, 1, 4))) {
         $options['exclDisposition'] = true;
     }
     $response->setAdditionalOptions($options);
     return $this;
 }
Example #10
0
 public function testAfterAddressSave()
 {
     $this->moduleManagerMock->expects($this->once())->method('isEnabled')->with('Magento_PageCache')->willReturn(true);
     $this->cacheConfigMock->expects($this->once())->method('isEnabled')->willReturn(true);
     $this->weeeHelperMock->expects($this->any())->method('isEnabled')->willReturn(true);
     $address = $this->objectManager->getObject('Magento\\Customer\\Model\\Address');
     $address->setIsDefaultShipping(true);
     $address->setIsDefaultBilling(true);
     $address->setIsPrimaryBilling(true);
     $address->setIsPrimaryShipping(true);
     $address->setCountryId(1);
     $address->setData('postcode', 11111);
     $this->customerSessionMock->expects($this->once())->method('setDefaultTaxBillingAddress')->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]);
     $this->customerSessionMock->expects($this->once())->method('setDefaultTaxShippingAddress')->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]);
     $this->observerMock->expects($this->once())->method('getCustomerAddress')->willReturn($address);
     $this->session->afterAddressSave($this->observerMock);
 }
Example #11
0
 /**
  * Create the weee ("FPT") totals summary
  *
  * @return $this
  */
 public function initTotals()
 {
     /** @var $items \Magento\Sales\Model\Order\Item[] */
     $items = $this->getSource()->getAllItems();
     $store = $this->getSource()->getStore();
     $weeeTotal = $this->weeeData->getTotalAmounts($items, $store);
     if ($weeeTotal) {
         // Add our total information to the set of other totals
         $total = new \Magento\Framework\DataObject(['code' => $this->getNameInLayout(), 'label' => __('FPT'), 'value' => $weeeTotal]);
         if ($this->getBeforeCondition()) {
             $this->getParentBlock()->addTotalBefore($total, $this->getBeforeCondition());
         } else {
             $this->getParentBlock()->addTotal($total, $this->getAfterCondition());
         }
     }
     return $this;
 }
 public function testExecute()
 {
     $this->moduleManagerMock->expects($this->once())->method('isEnabled')->with('Magento_PageCache')->willReturn(true);
     $this->cacheConfigMock->expects($this->once())->method('isEnabled')->willReturn(true);
     $this->weeeHelperMock->expects($this->any())->method('isEnabled')->willReturn(true);
     $customerMock = $this->getMockBuilder('Magento\\Customer\\Model\\Data\\Customer')->disableOriginalConstructor()->getMock();
     $this->observerMock->expects($this->once())->method('getData')->with('customer')->willReturn($customerMock);
     $address = $this->objectManager->getObject('Magento\\Customer\\Model\\Data\\Address');
     $address->setIsDefaultShipping(true);
     $address->setIsDefaultBilling(true);
     $address->setCountryId(1);
     $address->setPostCode(11111);
     $addresses = [$address];
     $customerMock->expects($this->once())->method('getAddresses')->willReturn($addresses);
     $this->customerSessionMock->expects($this->once())->method('setDefaultTaxBillingAddress')->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]);
     $this->customerSessionMock->expects($this->once())->method('setDefaultTaxShippingAddress')->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]);
     $this->session->execute($this->observerMock);
 }
 /**
  * Returns which product price to use as a basis for the Weee's final price
  *
  * @param  int|null $storeId
  * @param  array|null $weeeAttributesForBundle
  * @return string
  */
 protected function getWhichCalcPriceToUse($storeId = null, $weeeAttributesForBundle = null)
 {
     $calcPrice = 'finalPrice';
     if (!empty($weeeAttributesForBundle)) {
         if ($this->weeeData->isDisplayExcl($storeId) || $this->weeeData->isDisplayExclDescIncl($storeId) || $this->taxData->priceIncludesTax() && $this->taxData->displayPriceExcludingTax()) {
             $calcPrice = 'basePrice';
         }
     }
     return $calcPrice;
 }
Example #14
0
 /**
  * Fetch the Weee total amount for display in totals block when building the initial quote
  *
  * @param   \Magento\Sales\Model\Quote\Address $address
  * @return  $this
  */
 public function fetch(\Magento\Sales\Model\Quote\Address $address)
 {
     /** @var $items \Magento\Sales\Model\Order\Item[] */
     $items = $this->_getAddressItems($address);
     $store = $address->getQuote()->getStore();
     $weeeTotal = $this->_weeeData->getTotalAmounts($items, $store);
     if ($weeeTotal) {
         $address->addTotal(array('code' => $this->getCode(), 'title' => __('FPT'), 'value' => $weeeTotal, 'area' => null));
     }
     return $this;
 }
 /**
  * Obtain amount
  *
  * @param SaleableInterface $saleableItem
  * @return float
  */
 protected function getAmount(SaleableInterface $saleableItem)
 {
     $weeeTaxAmount = 0;
     $attributes = $this->weeeHelper->getProductWeeeAttributes($saleableItem, null, null, null, true, false);
     if ($attributes != null) {
         foreach ($attributes as $attribute) {
             $weeeTaxAmount += $attribute->getData('tax_amount');
         }
     }
     $weeeTaxAmount = $this->priceCurrency->convert($weeeTaxAmount);
     return $weeeTaxAmount;
 }
Example #16
0
 /**
  * Reset information about FPT for shopping cart item
  *
  * @param   \Magento\Sales\Model\Quote\Item\AbstractItem $item
  * @return  void
  */
 protected function _resetItemData($item)
 {
     $this->weeeData->setApplied($item, array());
     $item->setBaseWeeeTaxDisposition(0);
     $item->setWeeeTaxDisposition(0);
     $item->setBaseWeeeTaxRowDisposition(0);
     $item->setWeeeTaxRowDisposition(0);
     $item->setBaseWeeeTaxAppliedAmount(0);
     $item->setBaseWeeeTaxAppliedRowAmnt(0);
     $item->setWeeeTaxAppliedAmount(0);
     $item->setWeeeTaxAppliedRowAmount(0);
 }
Example #17
0
 public function testGetBaseTotalAmounts()
 {
     $item1BaseWeee = 4;
     $item2BaseWeee = 3;
     $expected = $item1BaseWeee + $item2BaseWeee;
     $itemProductSimple1 = $this->getMock('\\Magento\\Quote\\Model\\Quote\\Item', ['getBaseWeeeTaxAppliedRowAmount'], [], '', false);
     $itemProductSimple2 = $this->getMock('\\Magento\\Quote\\Model\\Quote\\Item', ['getBaseWeeeTaxAppliedRowAmount'], [], '', false);
     $items = [$itemProductSimple1, $itemProductSimple2];
     $itemProductSimple1->expects($this->any())->method('getBaseWeeeTaxAppliedRowAmount')->willReturn($item1BaseWeee);
     $itemProductSimple2->expects($this->any())->method('getBaseWeeeTaxAppliedRowAmount')->willReturn($item2BaseWeee);
     $this->assertEquals($expected, $this->helperData->getBaseTotalAmounts($items));
 }
Example #18
0
 /**
  * Reset information about Tax and Wee on FPT for shopping cart item
  *
  * @param   \Magento\Quote\Model\Quote\Item\AbstractItem $item
  * @return  void
  */
 protected function resetItemData($item)
 {
     $this->weeeData->setApplied($item, []);
     $item->setAssociatedTaxables([]);
     $item->setBaseWeeeTaxDisposition(0);
     $item->setWeeeTaxDisposition(0);
     $item->setBaseWeeeTaxRowDisposition(0);
     $item->setWeeeTaxRowDisposition(0);
     $item->setBaseWeeeTaxAppliedAmount(0);
     $item->setBaseWeeeTaxAppliedRowAmnt(0);
     $item->setWeeeTaxAppliedAmount(0);
     $item->setWeeeTaxAppliedRowAmount(0);
 }
Example #19
0
 /**
  * Collect Weee amounts for the credit memo
  *
  * @param Creditmemo $creditmemo
  * @return $this
  */
 public function collect(Creditmemo $creditmemo)
 {
     $store = $creditmemo->getStore();
     $totalTax = 0;
     $baseTotalTax = 0;
     $weeeTaxAmount = 0;
     $baseWeeeTaxAmount = 0;
     foreach ($creditmemo->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $weeeAmountExclTax = ($this->_weeeData->getWeeeTaxInclTax($item) - $this->_weeeData->getTotalTaxAppliedForWeeeTax($item)) * $item->getQty();
         $totalTax += $weeeAmountExclTax;
         $baseWeeeAmountExclTax = ($this->_weeeData->getBaseWeeeTaxInclTax($item) - $this->_weeeData->getBaseTotalTaxAppliedForWeeeTax($item)) * $item->getQty();
         $baseTotalTax += $baseWeeeAmountExclTax;
         $item->setWeeeTaxAppliedRowAmount($weeeAmountExclTax);
         $item->setBaseWeeeTaxAppliedRowAmount($baseWeeeAmountExclTax);
         $weeeTaxAmount += $this->_weeeData->getWeeeTaxInclTax($item) * $item->getQty();
         $baseWeeeTaxAmount += $this->_weeeData->getBaseWeeeTaxInclTax($item) * $item->getQty();
         $newApplied = array();
         $applied = $this->_weeeData->getApplied($item);
         foreach ($applied as $one) {
             $one['base_row_amount'] = $one['base_amount'] * $item->getQty();
             $one['row_amount'] = $one['amount'] * $item->getQty();
             $one['base_row_amount_incl_tax'] = $one['base_amount_incl_tax'] * $item->getQty();
             $one['row_amount_incl_tax'] = $one['amount_incl_tax'] * $item->getQty();
             $newApplied[] = $one;
         }
         $this->_weeeData->setApplied($item, $newApplied);
         $item->setWeeeTaxRowDisposition($item->getWeeeTaxDisposition() * $item->getQty());
         $item->setBaseWeeeTaxRowDisposition($item->getBaseWeeeTaxDisposition() * $item->getQty());
     }
     if ($this->_weeeData->includeInSubtotal($store)) {
         $creditmemo->setSubtotal($creditmemo->getSubtotal() + $totalTax);
         $creditmemo->setBaseSubtotal($creditmemo->getBaseSubtotal() + $baseTotalTax);
     }
     $creditmemo->setSubtotalInclTax($creditmemo->getSubtotalInclTax() + $weeeTaxAmount);
     $creditmemo->setBaseSubtotalInclTax($creditmemo->getBaseSubtotalInclTax() + $baseWeeeTaxAmount);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $totalTax);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseTotalTax);
     return $this;
 }
Example #20
0
 public function testGetBaseTotalAmount()
 {
     $baseRowTotal = 100;
     $baseTaxAmount = 10;
     $baseDiscountTaxCompensationAmount = 2;
     $baseDiscountAmount = 20;
     $baseWeeeAmount = 5;
     $expectedValue = $baseRowTotal + $baseTaxAmount + $baseDiscountTaxCompensationAmount - $baseDiscountAmount + $baseWeeeAmount;
     $itemMock = $this->getMockBuilder('\\Magento\\Sales\\Model\\Order\\Item')->disableOriginalConstructor()->setMethods(['getBaseRowTotal', 'getBaseTaxAmount', 'getBaseDiscountTaxCompensationAmount', 'getBaseDiscountAmount', '__wakeup'])->getMock();
     $itemMock->expects($this->once())->method('getBaseRowTotal')->will($this->returnValue($baseRowTotal));
     $itemMock->expects($this->once())->method('getBaseTaxAmount')->will($this->returnValue($baseTaxAmount));
     $itemMock->expects($this->once())->method('getBaseDiscountTaxCompensationAmount')->will($this->returnValue($baseDiscountTaxCompensationAmount));
     $itemMock->expects($this->once())->method('getBaseDiscountAmount')->will($this->returnValue($baseDiscountAmount));
     $this->weeeHelper->expects($this->once())->method('getBaseRowWeeeTaxInclTax')->with($itemMock)->will($this->returnValue($baseWeeeAmount));
     $this->assertEquals($expectedValue, $this->renderer->getBaseTotalAmount($itemMock));
 }
Example #21
0
 public function testGetWeeAttributesForBundle()
 {
     $weeObject = new \Magento\Framework\Object(['code' => 'fpt', 'amount' => '15.0000']);
     $testArray = ['fpt' => $weeObject];
     $this->weeeTax->expects($this->any())->method('getProductWeeeAttributes')->will($this->returnValue([$weeObject]));
     $productSimple = $this->getMock('\\Magento\\Catalog\\Model\\Product\\Type\\Simple', [], [], '', false);
     $productInstance = $this->getMock('\\Magento\\Bundle\\Model\\Product\\Type', [], [], '', false);
     $productInstance->expects($this->any())->method('getSelectionsCollection')->will($this->returnValue([$productSimple]));
     $store = $this->getMock('\\Magento\\Store\\Model\\Store', [], [], '', false);
     $product = $this->getMock('\\Magento\\Bundle\\Model\\Product', ['getTypeInstance', 'getStoreId', 'getStore', 'getTypeId'], [], '', false);
     $product->expects($this->any())->method('getTypeInstance')->will($this->returnValue($productInstance));
     $product->expects($this->any())->method('getStoreId')->will($this->returnValue(1));
     $product->expects($this->any())->method('getStore')->will($this->returnValue($store));
     $product->expects($this->any())->method('getTypeId')->will($this->returnValue('bundle'));
     $registry = $this->getMock('Magento\\Framework\\Registry', [], [], '', false);
     $registry->expects($this->any())->method('registry')->with('current_product')->will($this->returnValue($product));
     $this->assertEquals($testArray, $this->_helperData->getWeeAttributesForBundle($product));
 }
Example #22
0
 public function testGetRecursiveAmountBundle()
 {
     $testAmountUnit1 = 1;
     $testAmountUnit2 = 2;
     $testTotalUnit = $testAmountUnit1 + $testAmountUnit2;
     $testAmountRow1 = 33;
     $testAmountRow2 = 444;
     $testTotalRow = $testAmountRow1 + $testAmountRow2;
     $itemProductSimple1 = $this->getMock('\\Magento\\Quote\\Model\\Quote\\Item', ['getWeeeTaxAppliedAmount', 'getWeeeTaxAppliedRowAmount'], [], '', false);
     $itemProductSimple2 = $this->getMock('\\Magento\\Quote\\Model\\Quote\\Item', ['getWeeeTaxAppliedAmount', 'getWeeeTaxAppliedRowAmount'], [], '', false);
     $itemProductSimple1->expects($this->any())->method('getWeeeTaxAppliedAmount')->will($this->returnValue($testAmountUnit1));
     $itemProductSimple1->expects($this->any())->method('getWeeeTaxAppliedRowAmount')->will($this->returnValue($testAmountRow1));
     $itemProductSimple2->expects($this->any())->method('getWeeeTaxAppliedAmount')->will($this->returnValue($testAmountUnit2));
     $itemProductSimple2->expects($this->any())->method('getWeeeTaxAppliedRowAmount')->will($this->returnValue($testAmountRow2));
     $itemProductBundle = $this->getMock('\\Magento\\Quote\\Model\\Quote\\Item', ['getHasChildren', 'isChildrenCalculated', 'getChildren'], [], '', false);
     $itemProductBundle->expects($this->any())->method('getHasChildren')->will($this->returnValue(true));
     $itemProductBundle->expects($this->any())->method('isChildrenCalculated')->will($this->returnValue(true));
     $itemProductBundle->expects($this->any())->method('getChildren')->will($this->returnValue([$itemProductSimple1, $itemProductSimple2]));
     $this->assertEquals($testTotalUnit, $this->helperData->getWeeeTaxAppliedAmount($itemProductBundle));
     $this->assertEquals($testTotalRow, $this->helperData->getWeeeTaxAppliedRowAmount($itemProductBundle));
 }
Example #23
0
 /**
  * Modify the options config for the front end to resemble the weee final price
  *
  * @param   \Magento\Framework\Event\Observer $observer
  * @return  $this
  */
 public function getPriceConfiguration(\Magento\Framework\Event\Observer $observer)
 {
     if ($this->_weeeData->isEnabled()) {
         $priceConfigObj = $observer->getData('configObj');
         $priceConfig = $priceConfigObj->getConfig();
         if (is_array($priceConfig)) {
             foreach ($priceConfig as $keyConfigs => $configs) {
                 if (is_array($configs)) {
                     if (array_key_exists('prices', $configs)) {
                         $priceConfig[$keyConfigs]['prices']['weeePrice'] = ['amount' => $configs['prices']['finalPrice']['amount']];
                     } else {
                         foreach ($configs as $keyConfig => $config) {
                             $priceConfig[$keyConfigs][$keyConfig]['prices']['weeePrice'] = ['amount' => $config['prices']['finalPrice']['amount']];
                         }
                     }
                 }
             }
         }
         $priceConfigObj->setConfig($priceConfig);
     }
     return $this;
 }
 /**
  * Test for method getWeeeTaxAttributes
  *
  * @param int $typeOfDisplay
  * @param array $attributes
  * @param array $expectedResult
  * @dataProvider getWeeeTaxAttributesDataProvider
  */
 public function testGetWeeeTaxAttributes($typeOfDisplay, $attributes, $expectedResult)
 {
     /** @var \Magento\Framework\Pricing\Render\Amount $amountRender */
     $amountRender = $this->getMockBuilder('Magento\\Framework\\Pricing\\Render\\Amount')->disableOriginalConstructor()->setMethods(['getSaleableItem', 'getDisplayValue', 'getAmount'])->getMock();
     /** @var \Magento\Catalog\Model\Product $saleable */
     $saleable = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['__wakeup'])->getMock();
     /** @var \Magento\Framework\Pricing\Amount\Base $baseAmount */
     $baseAmount = $this->getMockBuilder('Magento\\Framework\\Pricing\\Amount\\Base')->disableOriginalConstructor()->setMethods(['getValue'])->getMock();
     $amountRender->expects($this->any())->method('getAmount')->will($this->returnValue($baseAmount));
     $callback = function ($argument) use($typeOfDisplay) {
         if (is_array($argument)) {
             return in_array($typeOfDisplay, $argument);
         } else {
             return $argument == $typeOfDisplay;
         }
     };
     $this->weeeHelperMock->expects($this->any())->method('typeOfDisplay')->will($this->returnCallback($callback));
     $this->weeeHelperMock->expects($this->any())->method('getProductWeeeAttributesForDisplay')->will($this->returnValue($attributes));
     $amountRender->expects($this->any())->method('getSaleableItem')->will($this->returnValue($saleable));
     $this->model->render($amountRender);
     $result = $this->model->getWeeeTaxAttributes();
     $this->assertEquals($expectedResult, $result);
 }
 public function testAroundDispatchBasedOnShipping()
 {
     $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->willReturn(true);
     $this->moduleManagerMock->expects($this->once())->method('isEnabled')->with('Magento_PageCache')->willReturn(true);
     $this->cacheConfigMock->expects($this->once())->method('isEnabled')->willReturn(true);
     $this->weeeHelperMock->expects($this->once())->method('isEnabled')->willReturn(true);
     $this->taxHelperMock->expects($this->once())->method('getTaxBasedOn')->willReturn('shipping');
     $storeMock = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock();
     $storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1);
     $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($storeMock);
     $this->scopeConfigMock->expects($this->at(0))->method('getValue')->with(\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null)->willReturn('US');
     $this->scopeConfigMock->expects($this->at(1))->method('getValue')->with(\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null)->willReturn(0);
     $this->customerSessionMock->expects($this->once())->method('getDefaultTaxShippingAddress')->willReturn(['country_id' => 'US', 'region_id' => 1]);
     $this->weeeTaxMock->expects($this->once())->method('isWeeeInLocation')->with('US', 1, 1)->willReturn(true);
     $this->httpContextMock->expects($this->once())->method('setValue')->with('weee_tax_region', ['countryId' => 'US', 'regionId' => 1], 0);
     $action = $this->objectManager->getObject('Magento\\Framework\\App\\Test\\Unit\\Action\\Stub\\ActionStub');
     $request = $this->getMock('\\Magento\\Framework\\App\\Request\\Http', ['getActionName'], [], '', false);
     $expectedResult = 'expectedResult';
     $proceed = function ($request) use($expectedResult) {
         return $expectedResult;
     };
     $this->contextPlugin->aroundDispatch($action, $proceed, $request);
 }
Example #26
0
 /**
  * Returns display type for price accordingly to current zone
  *
  * @param int|int[]|null $compareTo
  * @param \Magento\Store\Model\Store|null $store
  * @return bool|int
  */
 protected function typeOfDisplay($compareTo = null, $store = null)
 {
     return $this->weeeHelper->typeOfDisplay($compareTo, $this->getZone(), $store);
 }
Example #27
0
 /**
  * Obtain amount
  *
  * @param SaleableInterface $saleableItem
  * @return float
  */
 protected function getAmount(SaleableInterface $saleableItem)
 {
     $weeeAmount = $this->weeeHelper->getAmountExclTax($saleableItem);
     $weeeAmount = $this->priceCurrency->convert($weeeAmount);
     return $weeeAmount;
 }
Example #28
0
 public function testGetBaseWeeeTaxAmountRefunded()
 {
     $orderItem = $this->setupOrderItem();
     $value = $this->_helperData->getBaseWeeeTaxAmountRefunded($orderItem);
     $this->assertEquals(self::BASE_TAX_AMOUNT_REFUNDED, $value);
 }
Example #29
0
 public function testGetProductWeeeAttributesForDisplay()
 {
     $store = $this->getMock('\\Magento\\Store\\Model\\Store', [], [], '', false);
     $this->product->expects($this->any())->method('getStore')->will($this->returnValue($store));
     $this->helperData->getProductWeeeAttributesForDisplay($this->product);
 }
Example #30
0
 /**
  * Collect Weee amounts for the credit memo
  *
  * @param  Creditmemo $creditmemo
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  */
 public function collect(Creditmemo $creditmemo)
 {
     $store = $creditmemo->getStore();
     $totalWeeeAmount = 0;
     $baseTotalWeeeAmount = 0;
     $totalWeeeAmountInclTax = 0;
     $baseTotalWeeeAmountInclTax = 0;
     $totalTaxAmount = 0;
     $baseTotalTaxAmount = 0;
     foreach ($creditmemo->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         $orderItemQty = $orderItem->getQtyOrdered();
         if (!$orderItemQty || $orderItem->isDummy() || $item->getQty() < 0) {
             continue;
         }
         $ratio = $item->getQty() / $orderItemQty;
         $orderItemWeeeAmountExclTax = $orderItem->getWeeeTaxAppliedRowAmount();
         $orderItemBaseWeeeAmountExclTax = $orderItem->getBaseWeeeTaxAppliedRowAmnt();
         $weeeAmountExclTax = $creditmemo->roundPrice($orderItemWeeeAmountExclTax * $ratio);
         $baseWeeeAmountExclTax = $creditmemo->roundPrice($orderItemBaseWeeeAmountExclTax * $ratio, 'base');
         $orderItemWeeeAmountInclTax = $this->_weeeData->getRowWeeeTaxInclTax($orderItem);
         $orderItemBaseWeeeAmountInclTax = $this->_weeeData->getBaseRowWeeeTaxInclTax($orderItem);
         $weeeAmountInclTax = $creditmemo->roundPrice($orderItemWeeeAmountInclTax * $ratio);
         $baseWeeeAmountInclTax = $creditmemo->roundPrice($orderItemBaseWeeeAmountInclTax * $ratio, 'base');
         $itemTaxAmount = $weeeAmountInclTax - $weeeAmountExclTax;
         $itemBaseTaxAmount = $baseWeeeAmountInclTax - $baseWeeeAmountExclTax;
         $weeeAmountAvailable = $this->_weeeData->getWeeeAmountInvoiced($orderItem) - $this->_weeeData->getWeeeAmountRefunded($orderItem);
         $baseWeeeAmountAvailable = $this->_weeeData->getBaseWeeeAmountInvoiced($orderItem) - $this->_weeeData->getBaseWeeeAmountRefunded($orderItem);
         $weeeTaxAmountAvailable = $this->_weeeData->getWeeeTaxAmountInvoiced($orderItem) - $this->_weeeData->getWeeeTaxAmountRefunded($orderItem);
         $baseWeeeTaxAmountAvailable = $this->_weeeData->getBaseWeeeTaxAmountInvoiced($orderItem) - $this->_weeeData->getBaseWeeeTaxAmountRefunded($orderItem);
         if ($item->isLast()) {
             $weeeAmountExclTax = $weeeAmountAvailable;
             $baseWeeeAmountExclTax = $baseWeeeAmountAvailable;
             $itemTaxAmount = $weeeTaxAmountAvailable;
             $itemBaseTaxAmount = $baseWeeeTaxAmountAvailable;
         } else {
             $weeeAmountExclTax = min($weeeAmountExclTax, $weeeAmountAvailable);
             $baseWeeeAmountExclTax = min($baseWeeeAmountExclTax, $baseWeeeAmountAvailable);
             $itemTaxAmount = min($itemTaxAmount, $weeeTaxAmountAvailable);
             $itemBaseTaxAmount = min($itemBaseTaxAmount, $baseWeeeTaxAmountAvailable);
         }
         $totalWeeeAmount += $weeeAmountExclTax;
         $baseTotalWeeeAmount += $baseWeeeAmountExclTax;
         $item->setWeeeTaxAppliedRowAmount($weeeAmountExclTax);
         $item->setBaseWeeeTaxAppliedRowAmount($baseWeeeAmountExclTax);
         $totalTaxAmount += $itemTaxAmount;
         $baseTotalTaxAmount += $itemBaseTaxAmount;
         //Set the ratio of the tax amount in invoice item compared to tax amount in order item
         //This information is needed to calculate tax per tax rate later
         $orderItemTaxAmount = $orderItemWeeeAmountInclTax - $orderItemWeeeAmountExclTax;
         if ($orderItemTaxAmount != 0) {
             $taxRatio = [];
             if ($item->getTaxRatio()) {
                 $taxRatio = unserialize($item->getTaxRatio());
             }
             $taxRatio[\Magento\Weee\Model\Total\Quote\Weee::ITEM_TYPE] = $itemTaxAmount / $orderItemTaxAmount;
             $item->setTaxRatio(serialize($taxRatio));
         }
         $totalWeeeAmountInclTax += $weeeAmountInclTax;
         $baseTotalWeeeAmountInclTax += $baseWeeeAmountInclTax;
         $newApplied = [];
         $applied = $this->_weeeData->getApplied($orderItem);
         foreach ($applied as $one) {
             $title = (string) $one['title'];
             $one['base_row_amount'] = $creditmemo->roundPrice($one['base_row_amount'] * $ratio, $title . '_base');
             $one['row_amount'] = $creditmemo->roundPrice($one['row_amount'] * $ratio, $title);
             $one['base_row_amount_incl_tax'] = $creditmemo->roundPrice($one['base_row_amount_incl_tax'] * $ratio, $title . '_base');
             $one['row_amount_incl_tax'] = $creditmemo->roundPrice($one['row_amount_incl_tax'] * $ratio, $title);
             $newApplied[] = $one;
         }
         $this->_weeeData->setApplied($item, $newApplied);
         // Update order item
         $newApplied = [];
         $applied = $this->_weeeData->getApplied($orderItem);
         foreach ($applied as $one) {
             if (isset($one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED])) {
                 $one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED] = $one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED] + $baseWeeeAmountExclTax;
             } else {
                 $one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED] = $baseWeeeAmountExclTax;
             }
             if (isset($one[WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED])) {
                 $one[WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED] = $one[WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED] + $weeeAmountExclTax;
             } else {
                 $one[WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED] = $weeeAmountExclTax;
             }
             if (isset($one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED])) {
                 $one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED] = $one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED] + $itemBaseTaxAmount;
             } else {
                 $one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED] = $itemBaseTaxAmount;
             }
             if (isset($one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED])) {
                 $one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED] = $one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED] + $itemTaxAmount;
             } else {
                 $one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED] = $itemTaxAmount;
             }
             $newApplied[] = $one;
         }
         $this->_weeeData->setApplied($orderItem, $newApplied);
         $item->setWeeeTaxRowDisposition($item->getWeeeTaxDisposition() * $item->getQty());
         $item->setBaseWeeeTaxRowDisposition($item->getBaseWeeeTaxDisposition() * $item->getQty());
     }
     if ($this->_weeeData->includeInSubtotal($store)) {
         $creditmemo->setSubtotal($creditmemo->getSubtotal() + $totalWeeeAmount);
         $creditmemo->setBaseSubtotal($creditmemo->getBaseSubtotal() + $baseTotalWeeeAmount);
     }
     $creditmemo->setTaxAmount($creditmemo->getTaxAmount() + $totalTaxAmount);
     $creditmemo->setBaseTaxAmount($creditmemo->getBaseTaxAmount() + $baseTotalTaxAmount);
     $creditmemo->setSubtotalInclTax($creditmemo->getSubtotalInclTax() + $totalWeeeAmountInclTax);
     $creditmemo->setBaseSubtotalInclTax($creditmemo->getBaseSubtotalInclTax() + $baseTotalWeeeAmountInclTax);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $totalWeeeAmount + $totalTaxAmount);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseTotalWeeeAmount + $baseTotalTaxAmount);
     return $this;
 }