Exemplo n.º 1
0
 /**
  * Creates a QuoteDetails item data object by calling setters.
  *
  * @param array $dataArray
  * @return Item
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 private function generateItemWithSetters($dataArray)
 {
     $this->quoteDetailsItemBuilder->populateWithArray([]);
     if (array_key_exists(Item::KEY_CODE, $dataArray)) {
         $this->quoteDetailsItemBuilder->setCode($dataArray[Item::KEY_CODE]);
     }
     if (array_key_exists(Item::KEY_TYPE, $dataArray)) {
         $this->quoteDetailsItemBuilder->setType($dataArray[Item::KEY_TYPE]);
     }
     if (array_key_exists(Item::KEY_TAX_CLASS_KEY, $dataArray)) {
         $this->quoteDetailsItemBuilder->setTaxClassKey($this->taxClassKeyBuilder->setType($dataArray[Item::KEY_TAX_CLASS_KEY][TaxClassKey::KEY_TYPE])->setValue($dataArray[Item::KEY_TAX_CLASS_KEY][TaxClassKey::KEY_VALUE])->create());
     }
     if (array_key_exists(Item::KEY_DISCOUNT_AMOUNT, $dataArray)) {
         $this->quoteDetailsItemBuilder->setDiscountAmount($dataArray[Item::KEY_DISCOUNT_AMOUNT]);
     }
     if (array_key_exists(Item::KEY_QUANTITY, $dataArray)) {
         $this->quoteDetailsItemBuilder->setQuantity($dataArray[Item::KEY_QUANTITY]);
     }
     if (array_key_exists(Item::KEY_PARENT_CODE, $dataArray)) {
         $this->quoteDetailsItemBuilder->setParentCode($dataArray[Item::KEY_PARENT_CODE]);
     }
     if (array_key_exists(Item::KEY_SHORT_DESCRIPTION, $dataArray)) {
         $this->quoteDetailsItemBuilder->setShortDescription($dataArray[Item::KEY_SHORT_DESCRIPTION]);
     }
     if (array_key_exists(Item::KEY_UNIT_PRICE, $dataArray)) {
         $this->quoteDetailsItemBuilder->setUnitPrice($dataArray[Item::KEY_UNIT_PRICE]);
     }
     if (array_key_exists(Item::KEY_TAX_INCLUDED, $dataArray)) {
         $this->quoteDetailsItemBuilder->setTaxIncluded($dataArray[Item::KEY_TAX_INCLUDED]);
     }
     return $this->quoteDetailsItemBuilder->create();
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 protected function _setDataValues(array $data)
 {
     if (array_key_exists(QuoteDetails::KEY_BILLING_ADDRESS, $data)) {
         $data[QuoteDetails::KEY_BILLING_ADDRESS] = $this->addressBuilder->populateWithArray($data[QuoteDetails::KEY_BILLING_ADDRESS])->create();
     }
     if (array_key_exists(QuoteDetails::KEY_SHIPPING_ADDRESS, $data)) {
         $data[QuoteDetails::KEY_SHIPPING_ADDRESS] = $this->addressBuilder->populateWithArray($data[QuoteDetails::KEY_SHIPPING_ADDRESS])->create();
     }
     if (array_key_exists(QuoteDetails::KEY_ITEMS, $data)) {
         $items = [];
         foreach ($data[QuoteDetails::KEY_ITEMS] as $itemArray) {
             $items[] = $this->itemBuilder->populateWithArray($itemArray)->create();
         }
         $data[QuoteDetails::KEY_ITEMS] = $items;
     }
     return parent::_setDataValues($data);
 }
Exemplo n.º 3
0
 /**
  * Map item extra taxables
  *
  * @param ItemBuilder $itemBuilder
  * @param AbstractItem $item
  * @param bool $priceIncludesTax
  * @param bool $useBaseCurrency
  * @return ItemDataObject[]
  */
 public function mapItemExtraTaxables(ItemBuilder $itemBuilder, AbstractItem $item, $priceIncludesTax, $useBaseCurrency)
 {
     $itemDataObjects = [];
     $extraTaxables = $item->getAssociatedTaxables();
     if (!$extraTaxables) {
         return [];
     }
     foreach ($extraTaxables as $extraTaxable) {
         $extraTaxableIncludesTax = isset($extraTaxable['price_includes_tax']) ? $extraTaxable['price_includes_tax'] : $priceIncludesTax;
         $itemBuilder->setCode($extraTaxable[self::KEY_ASSOCIATED_TAXABLE_CODE]);
         $itemBuilder->setType($extraTaxable[self::KEY_ASSOCIATED_TAXABLE_TYPE]);
         $itemBuilder->setQuantity($extraTaxable[self::KEY_ASSOCIATED_TAXABLE_QUANTITY]);
         $itemBuilder->setTaxClassKey($itemBuilder->getTaxClassKeyBuilder()->setType(TaxClassKey::TYPE_ID)->setValue($extraTaxable[self::KEY_ASSOCIATED_TAXABLE_TAX_CLASS_ID])->create());
         if ($useBaseCurrency) {
             $unitPrice = $extraTaxable[self::KEY_ASSOCIATED_TAXABLE_BASE_UNIT_PRICE];
         } else {
             $unitPrice = $extraTaxable[self::KEY_ASSOCIATED_TAXABLE_UNIT_PRICE];
         }
         $itemBuilder->setUnitPrice($unitPrice);
         $itemBuilder->setTaxIncluded($extraTaxableIncludesTax);
         $itemBuilder->setAssociatedItemCode($item->getTaxCalculationItemId());
         $itemDataObjects[] = $itemBuilder->create();
     }
     return $itemDataObjects;
 }
Exemplo n.º 4
0
 /**
  * Get product price with all tax settings processing
  *
  * @param   \Magento\Catalog\Model\Product $product
  * @param   float $price inputted product price
  * @param   bool $includingTax return price include tax flag
  * @param   null|Address $shippingAddress
  * @param   null|Address $billingAddress
  * @param   null|int $ctc customer tax class
  * @param   null|string|bool|int|Store $store
  * @param   bool $priceIncludesTax flag what price parameter contain tax
  * @param   bool $roundPrice
  * @return  float
  */
 public function getTaxPrice($product, $price, $includingTax = null, $shippingAddress = null, $billingAddress = null, $ctc = null, $store = null, $priceIncludesTax = null, $roundPrice = true)
 {
     if (!$price) {
         return $price;
     }
     $store = $this->_storeManager->getStore($store);
     if ($this->_taxConfig->needPriceConversion($store)) {
         if (is_null($priceIncludesTax)) {
             $priceIncludesTax = $this->_taxConfig->priceIncludesTax($store);
         }
         $shippingAddressDataObject = null;
         if ($shippingAddress instanceof \Magento\Customer\Model\Address\AbstractAddress) {
             $shippingAddressDataObject = $this->_addressConverter->createAddressFromModel($shippingAddress, null, null);
         }
         $billingAddressDataObject = null;
         if ($billingAddress instanceof \Magento\Customer\Model\Address\AbstractAddress) {
             $billingAddressDataObject = $this->_addressConverter->createAddressFromModel($billingAddress, null, null);
         }
         $item = $this->_quoteDetailsItemBuilder->setQuantity(1)->setCode($product->getSku())->setShortDescription($product->getShortDescription())->setTaxClassKey($this->_taxClassKeyBuilder->setType(TaxClassKey::TYPE_ID)->setValue($product->getTaxClassId())->create())->setTaxIncluded($priceIncludesTax)->setType('product')->setUnitPrice($price)->create();
         $quoteDetails = $this->_quoteDetailsBuilder->setShippingAddress($shippingAddressDataObject)->setBillingAddress($billingAddressDataObject)->setCustomerTaxClassKey($this->_taxClassKeyBuilder->setType(TaxClassKey::TYPE_ID)->setValue($ctc)->create())->setItems([$item])->setCustomerId($this->_customerSession->getCustomerId())->create();
         $storeId = null;
         if ($store) {
             $storeId = $store->getId();
         }
         $taxDetails = $this->_taxCalculationService->calculateTax($quoteDetails, $storeId);
         $items = $taxDetails->getItems();
         $taxDetailsItem = array_shift($items);
         if (!is_null($includingTax)) {
             if ($includingTax) {
                 $price = $taxDetailsItem->getPriceInclTax();
             } else {
                 $price = $taxDetailsItem->getPrice();
             }
         } else {
             switch ($this->_taxConfig->getPriceDisplayType($store)) {
                 case Config::DISPLAY_TYPE_EXCLUDING_TAX:
                 case Config::DISPLAY_TYPE_BOTH:
                     $price = $taxDetailsItem->getPrice();
                     break;
                 case Config::DISPLAY_TYPE_INCLUDING_TAX:
                     $price = $taxDetailsItem->getPriceInclTax();
                     break;
                 default:
                     break;
             }
         }
     }
     if ($roundPrice) {
         return $store->roundPrice($price);
     } else {
         return $price;
     }
 }
Exemplo n.º 5
0
 /**
  * Map extra taxables associated with quote
  *
  * @param ItemBuilder $itemBuilder
  * @param Address $address
  * @param bool $useBaseCurrency
  * @return ItemDataObject[]
  */
 public function mapQuoteExtraTaxables(ItemBuilder $itemBuilder, Address $address, $useBaseCurrency)
 {
     $itemDataObjects = [];
     $extraTaxables = $address->getAssociatedTaxables();
     if (!$extraTaxables) {
         return [];
     }
     foreach ($extraTaxables as $extraTaxable) {
         $itemBuilder->setCode($extraTaxable[self::KEY_ASSOCIATED_TAXABLE_CODE]);
         $itemBuilder->setType($extraTaxable[self::KEY_ASSOCIATED_TAXABLE_TYPE]);
         $itemBuilder->setQuantity($extraTaxable[self::KEY_ASSOCIATED_TAXABLE_QUANTITY]);
         $itemBuilder->setTaxClassKey($itemBuilder->getTaxClassKeyBuilder()->setType(TaxClassKey::TYPE_ID)->setValue($extraTaxable[self::KEY_ASSOCIATED_TAXABLE_TAX_CLASS_ID])->create());
         if ($useBaseCurrency) {
             $unitPrice = $extraTaxable[self::KEY_ASSOCIATED_TAXABLE_BASE_UNIT_PRICE];
         } else {
             $unitPrice = $extraTaxable[self::KEY_ASSOCIATED_TAXABLE_UNIT_PRICE];
         }
         $itemBuilder->setUnitPrice($unitPrice);
         $itemBuilder->setTaxIncluded($extraTaxable[self::KEY_ASSOCIATED_TAXABLE_PRICE_INCLUDES_TAX]);
         $itemBuilder->setAssociatedItemCode($extraTaxable[self::KEY_ASSOCIATED_TAXABLE_ASSOCIATION_ITEM_CODE]);
         $itemDataObjects[] = $itemBuilder->create();
     }
     return $itemDataObjects;
 }
Exemplo n.º 6
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();
 }