Example #1
0
 /**
  * Map an item to item data object
  *
  * @param ItemBuilder $itemBuilder
  * @param AbstractItem $item
  * @param bool $priceIncludesTax
  * @param bool $useBaseCurrency
  * @param string $parentCode
  * @return ItemDataObject
  */
 public function mapItem(ItemBuilder $itemBuilder, AbstractItem $item, $priceIncludesTax, $useBaseCurrency, $parentCode = null)
 {
     if (!$item->getTaxCalculationItemId()) {
         $sequence = 'sequence-' . $this->getNextIncrement();
         $item->setTaxCalculationItemId($sequence);
     }
     $itemBuilder->setCode($item->getTaxCalculationItemId());
     $itemBuilder->setQuantity($item->getQty());
     $itemBuilder->setTaxClassKey($itemBuilder->getTaxClassKeyBuilder()->setType(TaxClassKey::TYPE_ID)->setValue($item->getProduct()->getTaxClassId())->create());
     $itemBuilder->setTaxIncluded($priceIncludesTax);
     $itemBuilder->setType(self::ITEM_TYPE_PRODUCT);
     if ($useBaseCurrency) {
         if (!$item->getBaseTaxCalculationPrice()) {
             $item->setBaseTaxCalculationPrice($item->getBaseCalculationPriceOriginal());
         }
         $itemBuilder->setUnitPrice($item->getBaseTaxCalculationPrice());
         $itemBuilder->setDiscountAmount($item->getBaseDiscountAmount());
     } else {
         if (!$item->getTaxCalculationPrice()) {
             $item->setTaxCalculationPrice($item->getCalculationPriceOriginal());
         }
         $itemBuilder->setUnitPrice($item->getTaxCalculationPrice());
         $itemBuilder->setDiscountAmount($item->getDiscountAmount());
     }
     $itemBuilder->setParentCode($parentCode);
     return $itemBuilder->create();
 }
Example #2
0
 /**
  * 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)
 {
     $attributes = $this->weeeData->getProductWeeeAttributes($item->getProduct(), $address, $address->getQuote()->getBillingAddress(), $this->_store->getWebsiteId());
     $productTaxes = array();
     $totalValueInclTax = 0;
     $baseTotalValueInclTax = 0;
     $totalRowValueInclTax = 0;
     $baseTotalRowValueInclTax = 0;
     $totalValueExclTax = 0;
     $baseTotalValueExclTax = 0;
     $totalRowValueExclTax = 0;
     $baseTotalRowValueExclTax = 0;
     $associatedTaxables = $item->getAssociatedTaxables();
     if (!$associatedTaxables) {
         $associatedTaxables = [];
     }
     foreach ($attributes as $key => $attribute) {
         $title = $attribute->getName();
         $baseValueExclTax = $baseValueInclTax = $attribute->getAmount();
         $valueExclTax = $valueInclTax = $this->_store->roundPrice($this->_store->convertPrice($baseValueExclTax));
         $rowValueInclTax = $rowValueExclTax = $this->_store->roundPrice($valueInclTax * $item->getTotalQty());
         $baseRowValueInclTax = $this->_store->roundPrice($baseValueInclTax * $item->getTotalQty());
         $baseRowValueExclTax = $baseRowValueInclTax;
         $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);
         if ($this->weeeData->isTaxable($this->_store)) {
             $itemTaxCalculationId = $item->getTaxCalculationItemId();
             $weeeItemCode = self::ITEM_CODE_WEEE_PREFIX . $this->getNextIncrement();
             $weeeItemCode .= '-' . $title;
             $associatedTaxables[] = [CommonTaxCollector::KEY_ASSOCIATED_TAXABLE_TYPE => self::ITEM_TYPE, CommonTaxCollector::KEY_ASSOCIATED_TAXABLE_CODE => $weeeItemCode, CommonTaxCollector::KEY_ASSOCIATED_TAXABLE_UNIT_PRICE => $valueExclTax, CommonTaxCollector::KEY_ASSOCIATED_TAXABLE_BASE_UNIT_PRICE => $baseValueExclTax, CommonTaxCollector::KEY_ASSOCIATED_TAXABLE_QUANTITY => $item->getQty(), CommonTaxCollector::KEY_ASSOCIATED_TAXABLE_TAX_CLASS_ID => $item->getProduct()->getTaxClassId()];
             $this->weeeCodeToItemMap[$weeeItemCode] = $item;
         }
     }
     $item->setAssociatedTaxables($associatedTaxables);
     $item->setWeeeTaxAppliedAmount($totalValueExclTax)->setBaseWeeeTaxAppliedAmount($baseTotalValueExclTax)->setWeeeTaxAppliedRowAmount($totalRowValueExclTax)->setBaseWeeeTaxAppliedRowAmnt($baseTotalRowValueExclTax);
     $item->setWeeeTaxAppliedAmountInclTax($totalValueInclTax)->setBaseWeeeTaxAppliedAmountInclTax($baseTotalValueInclTax)->setWeeeTaxAppliedRowAmountInclTax($totalRowValueInclTax)->setBaseWeeeTaxAppliedRowAmntInclTax($baseTotalRowValueInclTax);
     $this->processTotalAmount($address, $totalRowValueExclTax, $baseTotalRowValueExclTax, $totalRowValueInclTax, $baseTotalRowValueInclTax);
     $this->weeeData->setApplied($item, array_merge($this->weeeData->getApplied($item), $productTaxes));
 }
Example #3
0
 /**
  * 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);
     }
 }
Example #4
0
 /**
  * Convert quote item to order item
  *
  * @param   \Magento\Sales\Model\Quote\Item\AbstractItem $item
  * @return  \Magento\Sales\Model\Order\Item
  */
 public function itemToOrderItem(\Magento\Sales\Model\Quote\Item\AbstractItem $item)
 {
     $orderItem = $this->_orderItemFactory->create()->setStoreId($item->getStoreId())->setQuoteItemId($item->getId())->setQuoteParentItemId($item->getParentItemId())->setProductId($item->getProductId())->setProductType($item->getProductType())->setQtyBackordered($item->getBackorders())->setProduct($item->getProduct())->setBaseOriginalPrice($item->getBaseOriginalPrice());
     $options = $item->getProductOrderOptions();
     if (!$options) {
         $options = $item->getProduct()->getTypeInstance()->getOrderOptions($item->getProduct());
     }
     $orderItem->setProductOptions($options);
     $this->_objectCopyService->copyFieldsetToTarget('sales_convert_quote_item', 'to_order_item', $item, $orderItem);
     if ($item->getParentItem()) {
         $orderItem->setQtyOrdered($orderItem->getQtyOrdered() * $item->getParentItem()->getQty());
     }
     if (!$item->getNoDiscount()) {
         $this->_objectCopyService->copyFieldsetToTarget('sales_convert_quote_item', 'to_order_item_discount', $item, $orderItem);
     }
     return $orderItem;
 }
Example #5
0
 /**
  * Map an item to item data object
  *
  * @param ItemBuilder $itemBuilder
  * @param AbstractItem $item
  * @param bool $priceIncludesTax
  * @param bool $useBaseCurrency
  * @param string $parentCode
  * @return ItemDataObject
  */
 protected function mapItem(ItemBuilder $itemBuilder, AbstractItem $item, $priceIncludesTax, $useBaseCurrency, $parentCode = null)
 {
     if (!$item->getSequence()) {
         $sequence = 'sequence-' . $this->getNextIncrement();
         $item->setSequence($sequence);
     }
     $itemBuilder->setCode($item->getSequence());
     $itemBuilder->setQuantity($item->getQty());
     $itemBuilder->setTaxClassId($item->getProduct()->getTaxClassId());
     $itemBuilder->setTaxIncluded($priceIncludesTax);
     $itemBuilder->setType('product');
     //TODO: find a place to define constants
     if ($item->getParentItem()) {
         $itemBuilder->setParentCode($item->getParentItem()->getId());
     }
     if ($useBaseCurrency) {
         if (!$item->getBaseTaxCalculationPrice()) {
             $item->setBaseTaxCalculationPrice($item->getBaseCalculationPriceOriginal());
         }
         $itemBuilder->setUnitPrice($item->getBaseTaxCalculationPrice());
         $itemBuilder->setDiscountAmount($item->getBaseDiscountAmount());
     } else {
         if (!$item->getTaxCalculationPrice()) {
             $item->setTaxCalculationPrice($item->getCalculationPriceOriginal());
         }
         $itemBuilder->setUnitPrice($item->getTaxCalculationPrice());
         $itemBuilder->setDiscountAmount($item->getDiscountAmount());
     }
     $itemBuilder->setParentCode($parentCode);
     return $itemBuilder->create();
 }