Exemplo n.º 1
0
 /**
  * Fetch discount
  *
  * @param \Magento\Quote\Model\Quote\Address $address
  * @return $this
  */
 public function fetchTotals(\Magento\Quote\Model\Quote\Address $address)
 {
     $amount = $address->getDiscountAmount();
     if ($amount != 0) {
         $title = __('Discount');
         $couponCode = $address->getQuote()->getCouponCode();
         if (strlen($couponCode)) {
             $title .= sprintf(' (%s)', $couponCode);
         }
         $address->addTotal(['code' => 'discount', 'title' => $title, 'value' => -$amount]);
     }
     return $this;
 }
Exemplo n.º 2
0
 /**
  * @param Product $product
  * @param null|false|\Magento\Quote\Model\Quote\Address $shipping
  * @param null|false|\Magento\Quote\Model\Quote\Address $billing
  * @param Website $website
  * @param bool $calculateTax
  * @param bool $round
  * @return \Magento\Framework\DataObject[]
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function getProductWeeeAttributes($product, $shipping = null, $billing = null, $website = null, $calculateTax = null, $round = true)
 {
     $result = [];
     $websiteId = $this->_storeManager->getWebsite($website)->getId();
     /** @var \Magento\Store\Model\Store $store */
     $store = $this->_storeManager->getWebsite($website)->getDefaultGroup()->getDefaultStore();
     $allWeee = $this->getWeeeTaxAttributeCodes($store);
     if (!$allWeee) {
         return $result;
     }
     /** @var \Magento\Tax\Model\Calculation $calculator */
     $calculator = $this->_calculationFactory->create();
     $customerId = $this->_customerSession->getCustomerId();
     if ($shipping && $shipping->getCountryId()) {
         $customerTaxClass = $shipping->getQuote()->getCustomerTaxClassId();
     } else {
         // if customer logged use it default shipping and billing address
         if ($customerId) {
             $shipping = $this->accountManagement->getDefaultShippingAddress($customerId);
             $billing = $this->accountManagement->getDefaultBillingAddress($customerId);
             $customerTaxClass = null;
         } else {
             $shippingAddressArray = $this->_customerSession->getDefaultTaxShippingAddress();
             $billingAddressArray = $this->_customerSession->getDefaultTaxBillingAddress();
             if (!empty($billingAddressArray)) {
                 $billing = new \Magento\Framework\DataObject($billingAddressArray);
             }
             if (!empty($shippingAddressArray)) {
                 $shipping = new \Magento\Framework\DataObject($shippingAddressArray);
             }
             $customerTaxClass = $this->_customerSession->getCustomerTaxClassId();
         }
     }
     $rateRequest = $calculator->getRateRequest($shipping, $billing, $customerTaxClass, $store, $customerId);
     $defaultRateRequest = $calculator->getDefaultRateRequest($store);
     $productAttributes = $this->getResource()->fetchWeeeTaxCalculationsByEntity($rateRequest->getCountryId(), $rateRequest->getRegionId(), $websiteId, $store->getId(), $product->getId());
     foreach ($productAttributes as $attribute) {
         $value = $attribute['weee_value'];
         if ($value) {
             $taxAmount = $amount = 0;
             $amount = $value;
             $amountExclTax = $value;
             if ($calculateTax && $this->weeeConfig->isTaxable($store)) {
                 /** @var \Magento\Tax\Model\Calculation $calculator */
                 $defaultPercent = $calculator->getRate($defaultRateRequest->setProductClassId($product->getTaxClassId()));
                 $currentPercent = $calculator->getRate($rateRequest->setProductClassId($product->getTaxClassId()));
                 if ($this->_taxData->priceIncludesTax($store)) {
                     $amountInclTax = $value / (100 + $defaultPercent) * (100 + $currentPercent);
                     if ($round) {
                         $amountInclTax = $this->priceCurrency->round($amountInclTax);
                     }
                     $taxAmount = $amountInclTax - $amountInclTax / (100 + $currentPercent) * 100;
                     if ($round) {
                         $taxAmount = $this->priceCurrency->round($taxAmount);
                     }
                     $amountExclTax = $amountInclTax - $taxAmount;
                 } else {
                     $appliedRates = $this->_calculationFactory->create()->getAppliedRates($rateRequest);
                     if (count($appliedRates) > 1) {
                         $taxAmount = 0;
                         foreach ($appliedRates as $appliedRate) {
                             $taxRate = $appliedRate['percent'];
                             if ($round) {
                                 $taxAmount += $this->priceCurrency->round($value * $taxRate / 100);
                             } else {
                                 $taxAmount += $value * $taxRate / 100;
                             }
                         }
                     } else {
                         if ($round) {
                             $taxAmount = $this->priceCurrency->round($value * $currentPercent / 100);
                         } else {
                             $taxAmount = $value * $currentPercent / 100;
                         }
                     }
                 }
             }
             $one = new \Magento\Framework\DataObject();
             $one->setName($attribute['label_value'] ? __($attribute['label_value']) : __($attribute['frontend_label']))->setAmount($amount)->setTaxAmount($taxAmount)->setAmountExclTax($amountExclTax)->setCode($attribute['attribute_code']);
             $result[] = $one;
         }
     }
     return $result;
 }
Exemplo n.º 3
0
 /**
  * Remove item
  *
  * @param Address $address
  * @param  AddressItem|Item $item
  * @return $this
  */
 protected function _removeItem($address, $item)
 {
     if ($item instanceof Item) {
         $address->removeItem($item->getId());
         if ($address->getQuote()) {
             $address->getQuote()->removeItem($item->getId());
         }
     } elseif ($item instanceof AddressItem) {
         $address->removeItem($item->getId());
         if ($address->getQuote()) {
             $address->getQuote()->removeItem($item->getQuoteItemId());
         }
     }
     return $this;
 }
Exemplo n.º 4
0
 /**
  * Add rule discount description label to address object
  *
  * @param Address $address
  * @param \Magento\SalesRule\Model\Rule $rule
  * @return $this
  */
 public function addDiscountDescription($address, $rule)
 {
     $description = $address->getDiscountDescriptionArray();
     $ruleLabel = $rule->getStoreLabel($address->getQuote()->getStore());
     $label = '';
     if ($ruleLabel) {
         $label = $ruleLabel;
     } else {
         if (strlen($address->getCouponCode())) {
             $label = $address->getCouponCode();
         }
     }
     if (strlen($label)) {
         $description[$rule->getId()] = $label;
     }
     $address->setDiscountDescriptionArray($description);
     return $this;
 }
Exemplo n.º 5
0
 /**
  * @param Product $product
  * @param null|false|\Magento\Quote\Model\Quote\Address $shipping
  * @param null|false|\Magento\Quote\Model\Quote\Address $billing
  * @param Website $website
  * @param bool $calculateTax
  * @return \Magento\Framework\Object[]
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function getProductWeeeAttributes($product, $shipping = null, $billing = null, $website = null, $calculateTax = null)
 {
     $result = [];
     $websiteId = $this->_storeManager->getWebsite($website)->getId();
     /** @var \Magento\Store\Model\Store $store */
     $store = $this->_storeManager->getWebsite($website)->getDefaultGroup()->getDefaultStore();
     $allWeee = $this->getWeeeTaxAttributeCodes($store);
     if (!$allWeee) {
         return $result;
     }
     /** @var \Magento\Tax\Model\Calculation $calculator */
     $calculator = $this->_calculationFactory->create();
     if ($shipping && $shipping->getCountryId()) {
         $customerTaxClass = $shipping->getQuote()->getCustomerTaxClassId();
     } else {
         // if customer logged use it default shipping and billing address
         if ($customerId = $this->_customerSession->getCustomerId()) {
             $shipping = $this->accountManagement->getDefaultShippingAddress($customerId);
             $billing = $this->accountManagement->getDefaultBillingAddress($customerId);
             $customerTaxClass = null;
         } else {
             $shippingAddressArray = $this->_customerSession->getDefaultTaxShippingAddress();
             $billingAddressArray = $this->_customerSession->getDefaultTaxBillingAddress();
             if (!empty($billingAddressArray)) {
                 $billing = new \Magento\Framework\Object($billingAddressArray);
             }
             if (!empty($shippingAddressArray)) {
                 $shipping = new \Magento\Framework\Object($shippingAddressArray);
             }
             $customerTaxClass = $this->_customerSession->getCustomerTaxClassId();
         }
     }
     $rateRequest = $calculator->getRateRequest($shipping, $billing, $customerTaxClass, $store);
     $defaultRateRequest = $calculator->getDefaultRateRequest($store);
     $productAttributes = $product->getTypeInstance()->getSetAttributes($product);
     foreach ($productAttributes as $code => $attribute) {
         if (in_array($code, $allWeee)) {
             $attributeSelect = $this->getResource()->getReadConnection()->select();
             $attributeSelect->from($this->getResource()->getTable('weee_tax'), 'value')->where('attribute_id = ?', (int) $attribute->getId())->where('website_id IN(?)', [$websiteId, 0])->where('country = ?', $rateRequest->getCountryId())->where('state IN(?)', [$rateRequest->getRegionId(), 0])->where('entity_id = ?', (int) $product->getId())->limit(1);
             $order = ['state ' . \Magento\Framework\DB\Select::SQL_DESC, 'website_id ' . \Magento\Framework\DB\Select::SQL_DESC];
             $attributeSelect->order($order);
             $value = $this->getResource()->getReadConnection()->fetchOne($attributeSelect);
             if ($value) {
                 $taxAmount = $amount = 0;
                 $amount = $value;
                 if ($calculateTax && $this->weeeConfig->isTaxable($store)) {
                     /** @var \Magento\Tax\Model\Calculation $calculator */
                     $defaultPercent = $calculator->getRate($defaultRateRequest->setProductClassId($product->getTaxClassId()));
                     $currentPercent = $calculator->getRate($rateRequest->setProductClassId($product->getTaxClassId()));
                     if ($this->_taxData->priceIncludesTax($store)) {
                         $amountInclTax = $value / (100 + $defaultPercent) * (100 + $currentPercent);
                         //round the "golden price"
                         $amountInclTax = $this->priceCurrency->round($amountInclTax);
                         $taxAmount = $amountInclTax - $amountInclTax / (100 + $currentPercent) * 100;
                         $taxAmount = $this->priceCurrency->round($taxAmount);
                     } else {
                         $appliedRates = $this->_calculationFactory->create()->getAppliedRates($rateRequest);
                         if (count($appliedRates) > 1) {
                             $taxAmount = 0;
                             foreach ($appliedRates as $appliedRate) {
                                 $taxRate = $appliedRate['percent'];
                                 $taxAmount += $this->priceCurrency->round($value * $taxRate / 100);
                             }
                         } else {
                             $taxAmount = $this->priceCurrency->round($value * $currentPercent / 100);
                         }
                         $taxAmount = $this->priceCurrency->round($value * $currentPercent / 100);
                     }
                 }
                 $one = new \Magento\Framework\Object();
                 $one->setName(__($attribute->getFrontend()->getLabel()))->setAmount($amount)->setTaxAmount($taxAmount)->setCode($attribute->getAttributeCode());
                 $result[] = $one;
             }
         }
     }
     return $result;
 }
Exemplo n.º 6
0
 /**
  * Convert address discount description array to string
  *
  * @param Address $address
  * @param string $separator
  * @return $this
  */
 public function prepareDescription($address, $separator = ', ')
 {
     $descriptionArray = $address->getDiscountDescriptionArray();
     if (!$descriptionArray && $address->getQuote()->getItemVirtualQty() > 0) {
         $descriptionArray = $address->getQuote()->getBillingAddress()->getDiscountDescriptionArray();
     }
     $description = $descriptionArray && is_array($descriptionArray) ? implode($separator, array_unique($descriptionArray)) : '';
     $address->setDiscountDescription($description);
     return $this;
 }
Exemplo n.º 7
0
 /**
  * @param \Magento\Quote\Model\Quote\Address $address
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function collect(\Magento\Quote\Model\Quote\Address $address)
 {
     $quote = $address->getQuote();
     $eventArgs = ['website_id' => $this->_storeManager->getStore($quote->getStoreId())->getWebsiteId(), 'customer_group_id' => $quote->getCustomerGroupId(), 'coupon_code' => $quote->getCouponCode()];
     $address->setFreeShipping(0);
     $totalDiscountAmount = 0;
     $subtotalWithDiscount = 0;
     $baseTotalDiscountAmount = 0;
     $baseSubtotalWithDiscount = 0;
     $items = $address->getAllItems();
     if (!count($items)) {
         $address->setDiscountAmount($totalDiscountAmount);
         $address->setSubtotalWithDiscount($subtotalWithDiscount);
         $address->setBaseDiscountAmount($baseTotalDiscountAmount);
         $address->setBaseSubtotalWithDiscount($baseSubtotalWithDiscount);
         return $this;
     }
     foreach ($items as $item) {
         if ($item->getNoDiscount()) {
             $item->setDiscountAmount(0);
             $item->setBaseDiscountAmount(0);
             $item->setRowTotalWithDiscount($item->getRowTotal());
             $item->setBaseRowTotalWithDiscount($item->getRowTotal());
             $subtotalWithDiscount += $item->getRowTotal();
             $baseSubtotalWithDiscount += $item->getBaseRowTotal();
         } else {
             /**
              * Child item discount we calculate for parent
              */
             if ($item->getParentItemId()) {
                 continue;
             }
             /**
              * Composite item discount calculation
              */
             if ($item->getHasChildren() && $item->isChildrenCalculated()) {
                 foreach ($item->getChildren() as $child) {
                     $eventArgs['item'] = $child;
                     $this->_eventManager->dispatch('sales_quote_address_discount_item', $eventArgs);
                     /**
                      * Parent free shipping we apply to all children
                      */
                     if ($item->getFreeShipping()) {
                         $child->setFreeShipping($item->getFreeShipping());
                     }
                     if (!$child->getDiscountAmount() && $item->getDiscountPercent()) {
                         $child->setDiscountAmount($child->getRowTotal() * $item->getDiscountPercent());
                     }
                     $totalDiscountAmount += $child->getDiscountAmount();
                     //*$item->getQty();
                     $baseTotalDiscountAmount += $child->getBaseDiscountAmount();
                     //*$item->getQty();
                     $child->setRowTotalWithDiscount($child->getRowTotal() - $child->getDiscountAmount());
                     $child->setBaseRowTotalWithDiscount($child->getBaseRowTotal() - $child->getBaseDiscountAmount());
                     $subtotalWithDiscount += $child->getRowTotalWithDiscount();
                     $baseSubtotalWithDiscount += $child->getBaseRowTotalWithDiscount();
                 }
             } else {
                 $eventArgs['item'] = $item;
                 $this->_eventManager->dispatch('sales_quote_address_discount_item', $eventArgs);
                 $totalDiscountAmount += $item->getDiscountAmount();
                 $baseTotalDiscountAmount += $item->getBaseDiscountAmount();
                 $item->setRowTotalWithDiscount($item->getRowTotal() - $item->getDiscountAmount());
                 $item->setBaseRowTotalWithDiscount($item->getBaseRowTotal() - $item->getBaseDiscountAmount());
                 $subtotalWithDiscount += $item->getRowTotalWithDiscount();
                 $baseSubtotalWithDiscount += $item->getBaseRowTotalWithDiscount();
             }
         }
     }
     $address->setDiscountAmount($totalDiscountAmount);
     $address->setSubtotalWithDiscount($subtotalWithDiscount);
     $address->setBaseDiscountAmount($baseTotalDiscountAmount);
     $address->setBaseSubtotalWithDiscount($baseSubtotalWithDiscount);
     $address->setGrandTotal($address->getGrandTotal() - $address->getDiscountAmount());
     $address->setBaseGrandTotal($address->getBaseGrandTotal() - $address->getBaseDiscountAmount());
     return $this;
 }
Exemplo n.º 8
0
 /**
  * Declare address model
  *
  * @param \Magento\Quote\Model\Quote\Address $address
  * @return $this
  */
 public function setAddress(\Magento\Quote\Model\Quote\Address $address)
 {
     $this->_address = $address;
     $this->_quote = $address->getQuote();
     return $this;
 }
 /**
  * Populate the quote details with address information
  *
  * @param QuoteDetailsInterface $quoteDetails
  * @param QuoteAddress $address
  * @return QuoteDetailsInterface
  */
 public function populateAddressData(QuoteDetailsInterface $quoteDetails, QuoteAddress $address)
 {
     $quoteDetails->setBillingAddress($this->mapAddress($address->getQuote()->getBillingAddress()));
     $quoteDetails->setShippingAddress($this->mapAddress($address));
     return $quoteDetails;
 }
Exemplo n.º 10
0
 /**
  * Update tax related fields for shipping
  *
  * @param QuoteAddress $address
  * @param TaxDetailsItemInterface $shippingTaxDetails
  * @param TaxDetailsItemInterface $baseShippingTaxDetails
  * @return $this
  */
 protected function processShippingTaxInfo(QuoteAddress $address, $shippingTaxDetails, $baseShippingTaxDetails)
 {
     $address->setTotalAmount('shipping', $shippingTaxDetails->getRowTotal());
     $address->setBaseTotalAmount('shipping', $baseShippingTaxDetails->getRowTotal());
     $address->setTotalAmount('shipping_hidden_tax', $shippingTaxDetails->getDiscountTaxCompensationAmount());
     $address->setBaseTotalAmount('shipping_hidden_tax', $baseShippingTaxDetails->getDiscountTaxCompensationAmount());
     $address->setShippingInclTax($shippingTaxDetails->getRowTotalInclTax());
     $address->setBaseShippingInclTax($baseShippingTaxDetails->getRowTotalInclTax());
     $address->setShippingTaxAmount($shippingTaxDetails->getRowTax());
     $address->setBaseShippingTaxAmount($baseShippingTaxDetails->getRowTax());
     //Add the shipping tax to total tax amount
     $address->addTotalAmount('tax', $shippingTaxDetails->getRowTax());
     $address->addBaseTotalAmount('tax', $baseShippingTaxDetails->getRowTax());
     if ($this->_config->discountTax($address->getQuote()->getStore())) {
         $address->setShippingAmountForDiscount($shippingTaxDetails->getRowTotalInclTax());
         $address->setBaseShippingAmountForDiscount($baseShippingTaxDetails->getRowTotalInclTax());
     }
     return $this;
 }
Exemplo n.º 11
0
/**
 * 2016-04-04
 * @param AA|CA|QA|OA $a
 * @return Customer|Quote|Order|null
 */
function df_address_owner($a)
{
    return $a instanceof CA ? $a->getCustomer() : ($a instanceof QA ? $a->getQuote() : ($a instanceof OA ? $a->getOrder() : null));
}
Exemplo n.º 12
0
 /**
  * @param \Magento\Quote\Model\Quote\Address $address
  * @return $this
  */
 public function fetch(\Magento\Quote\Model\Quote\Address $address)
 {
     $applied = $address->getAppliedTaxes();
     $store = $address->getQuote()->getStore();
     $amount = $address->getTaxAmount();
     if ($amount != 0 || $this->_taxData->displayZeroTax($store)) {
         $address->addTotal(['code' => $this->getCode(), 'title' => __('Tax'), 'full_info' => $applied ? $applied : [], 'value' => $amount]);
     }
     return $this;
 }
Exemplo n.º 13
0
 /**
  * Add tax totals information to address object
  *
  * @param   Address $address
  * @return  $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function fetch(Address $address)
 {
     $applied = $address->getAppliedTaxes();
     $store = $address->getQuote()->getStore();
     $amount = $address->getTaxAmount();
     $items = $this->_getAddressItems($address);
     $discountTaxCompensation = 0;
     foreach ($items as $item) {
         $discountTaxCompensation += $item->getDiscountTaxCompensation();
     }
     $taxAmount = $amount + $discountTaxCompensation;
     $area = null;
     if ($this->_config->displayCartTaxWithGrandTotal($store) && $address->getGrandTotal()) {
         $area = 'taxes';
     }
     if ($amount != 0 || $this->_config->displayCartZeroTax($store)) {
         $address->addTotal(['code' => $this->getCode(), 'title' => __('Tax'), 'full_info' => $applied ? $applied : [], 'value' => $amount, 'area' => $area]);
     }
     $store = $address->getQuote()->getStore();
     /**
      * Modify subtotal
      */
     if ($this->_config->displayCartSubtotalBoth($store) || $this->_config->displayCartSubtotalInclTax($store)) {
         if ($address->getSubtotalInclTax() > 0) {
             $subtotalInclTax = $address->getSubtotalInclTax();
         } else {
             $subtotalInclTax = $address->getSubtotal() + $taxAmount - $address->getShippingTaxAmount();
         }
         $address->addTotal(['code' => 'subtotal', 'title' => __('Subtotal'), 'value' => $subtotalInclTax, 'value_incl_tax' => $subtotalInclTax, 'value_excl_tax' => $address->getSubtotal()]);
     }
     return $this;
 }