Exemplo n.º 1
0
 /**
  * @param AbstractItem $item
  * @return float
  */
 public function getBasePriceInclTax($item)
 {
     $qty = $item->getQty() ? $item->getQty() : ($item->getQtyOrdered() ? $item->getQtyOrdered() : 1);
     $taxAmount = $item->getBaseTaxAmount() + $item->getBaseDiscountTaxCompensation();
     $price = floatval($qty) ? ($item->getBaseRowTotal() + $taxAmount) / $qty : 0;
     return $this->priceCurrency->round($price);
 }
Exemplo n.º 2
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();
 }
Exemplo n.º 3
0
 /**
  * Request shipping rates for entire address or specified address item
  * Returns true if current selected shipping method code corresponds to one of the found rates
  *
  * @param \Magento\Sales\Model\Quote\Item\AbstractItem $item
  * @return bool
  */
 public function requestShippingRates(\Magento\Sales\Model\Quote\Item\AbstractItem $item = null)
 {
     /** @var $request \Magento\Sales\Model\Quote\Address\RateRequest */
     $request = $this->_rateRequestFactory->create();
     $request->setAllItems($item ? array($item) : $this->getAllItems());
     $request->setDestCountryId($this->getCountryId());
     $request->setDestRegionId($this->getRegionId());
     $request->setDestRegionCode($this->getRegionCode());
     $request->setDestStreet($this->getStreetFull());
     $request->setDestCity($this->getCity());
     $request->setDestPostcode($this->getPostcode());
     $request->setPackageValue($item ? $item->getBaseRowTotal() : $this->getBaseSubtotal());
     $packageWithDiscount = $item ? $item->getBaseRowTotal() - $item->getBaseDiscountAmount() : $this->getBaseSubtotalWithDiscount();
     $request->setPackageValueWithDiscount($packageWithDiscount);
     $request->setPackageWeight($item ? $item->getRowWeight() : $this->getWeight());
     $request->setPackageQty($item ? $item->getQty() : $this->getItemQty());
     /**
      * Need for shipping methods that use insurance based on price of physical products
      */
     $packagePhysicalValue = $item ? $item->getBaseRowTotal() : $this->getBaseSubtotal() - $this->getBaseVirtualAmount();
     $request->setPackagePhysicalValue($packagePhysicalValue);
     $request->setFreeMethodWeight($item ? 0 : $this->getFreeMethodWeight());
     /**
      * Store and website identifiers need specify from quote
      */
     /*$request->setStoreId($this->_storeManager->getStore()->getId());
       $request->setWebsiteId($this->_storeManager->getStore()->getWebsiteId());*/
     $request->setStoreId($this->getQuote()->getStore()->getId());
     $request->setWebsiteId($this->getQuote()->getStore()->getWebsiteId());
     $request->setFreeShipping($this->getFreeShipping());
     /**
      * Currencies need to convert in free shipping
      */
     $request->setBaseCurrency($this->getQuote()->getStore()->getBaseCurrency());
     $request->setPackageCurrency($this->getQuote()->getStore()->getCurrentCurrency());
     $request->setLimitCarrier($this->getLimitCarrier());
     $request->setBaseSubtotalInclTax($this->getBaseSubtotalInclTax());
     $result = $this->_rateCollector->create()->collectRates($request)->getResult();
     $found = false;
     if ($result) {
         $shippingRates = $result->getAllRates();
         foreach ($shippingRates as $shippingRate) {
             $rate = $this->_addressRateFactory->create()->importShippingRate($shippingRate);
             if (!$item) {
                 $this->addShippingRate($rate);
             }
             if ($this->getShippingMethod() == $rate->getCode()) {
                 if ($item) {
                     $item->setBaseShippingAmount($rate->getPrice());
                 } else {
                     /**
                      * possible bug: this should be setBaseShippingAmount(),
                      * see \Magento\Sales\Model\Quote\Address\Total\Shipping::collect()
                      * where this value is set again from the current specified rate price
                      * (looks like a workaround for this bug)
                      */
                     $this->setShippingAmount($rate->getPrice());
                 }
                 $found = true;
             }
         }
     }
     return $found;
 }
Exemplo n.º 4
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));
 }
Exemplo n.º 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();
 }