Exemplo n.º 1
0
 /**
  * Checks if the the basket weight is ok for the service provider.
  *
  * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
  * @return boolean True if payment provider can be used, false if not
  */
 public function isAvailable(\Aimeos\MShop\Order\Item\Base\Iface $basket)
 {
     $context = $this->getContext();
     $prodMap = array();
     $basketWeight = 0;
     foreach ($basket->getProducts() as $basketItem) {
         $prodId = $basketItem->getProductId();
         // basket can contain a product several times in different basket items
         if (!isset($prodMap[$prodId])) {
             $prodMap[$prodId] = 0.0;
         }
         $prodMap[$prodId] += $basketItem->getQuantity();
     }
     $propertyManager = \Aimeos\MShop\Factory::createManager($context, 'product/property');
     $search = $propertyManager->createSearch(true);
     $expr = array($search->compare('==', 'product.property.parentid', array_keys($prodMap)), $search->compare('==', 'product.property.type.code', 'package-weight'), $search->getConditions());
     $search->setConditions($search->combine('&&', $expr));
     $search->setSlice(0, 0x7fffffff);
     // if more than 100 products are in the basket
     foreach ($propertyManager->searchItems($search) as $property) {
         $basketWeight += (double) $property->getValue() * $prodMap[$property->getParentId()];
     }
     if ($this->checkWeightScale($basketWeight) === false) {
         return false;
     }
     return $this->getProvider()->isAvailable($basket);
 }
Exemplo n.º 2
0
 /**
  * Checks if the service provider should be available.
  *
  * Tests products in basket if they have a download attribute. The method
  * returns true if "download.all" is "1" and all products contain the
  * attribute resp. if "download.all" is "0" and at least one product
  * contains no download attribute.
  *
  * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
  * @return boolean True if payment provider can be used, false if not
  */
 public function isAvailable(\Aimeos\MShop\Order\Item\Base\Iface $basket)
 {
     $val = (bool) $this->getConfigValue(array('download.all'));
     foreach ($basket->getProducts() as $product) {
         if ((bool) count($product->getAttributes('download')) !== $val) {
             return !$val;
         }
     }
     return $this->getProvider()->isAvailable($basket);
 }
Exemplo n.º 3
0
 /**
  * Checks for requirements.
  *
  * @param \Aimeos\MShop\Order\Item\Base\Iface $base Basic order of the customer
  * @return boolean True if the requirements are met, false if not
  */
 public function isAvailable(\Aimeos\MShop\Order\Item\Base\Iface $base)
 {
     if (($prodcode = $this->getConfigValue('required.productcode')) !== null) {
         foreach ($base->getProducts() as $product) {
             if ($product->getProductCode() == $prodcode) {
                 return parent::isAvailable($base);
             }
         }
         return false;
     }
     return true;
 }
Exemplo n.º 4
0
 /**
  * Checks if the the basket weight is ok for the service provider.
  *
  * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
  * @return boolean True if payment provider can be used, false if not
  */
 public function isAvailable(\Aimeos\MShop\Order\Item\Base\Iface $basket)
 {
     $prodMap = array();
     // basket can contain a product several times in different basket items
     // product IDs are only those of articles, selections and bundles, not of the variants and bundled products
     foreach ($basket->getProducts() as $basketItem) {
         $qty = $basketItem->getQuantity();
         $code = $basketItem->getProductCode();
         $prodMap[$code] = isset($prodMap[$code]) ? $prodMap[$code] + $qty : $qty;
         foreach ($basketItem->getProducts() as $prodItem) {
             $qty = $prodItem->getQuantity();
             $code = $prodItem->getProductCode();
             $prodMap[$code] = isset($prodMap[$code]) ? $prodMap[$code] + $qty : $qty;
         }
     }
     if ($this->checkWeightScale($this->getWeight($prodMap)) === false) {
         return false;
     }
     return $this->getProvider()->isAvailable($basket);
 }
Exemplo n.º 5
0
 /**
  * Tests if the shipping threshold is reached and updates the price accordingly
  *
  * @param \Aimeos\MShop\Order\Item\Base\Iface $order Basket object
  * @param \Aimeos\MShop\Price\Item\Iface $price Delivery price item
  * @param array $threshold Associative list of currency/threshold pairs
  */
 protected function checkThreshold(\Aimeos\MShop\Order\Item\Base\Iface $order, \Aimeos\MShop\Price\Item\Iface $price, array $threshold)
 {
     $currency = $price->getCurrencyId();
     if (!isset($threshold[$currency])) {
         return;
     }
     $sum = \Aimeos\MShop\Factory::createManager($this->getContext(), 'price')->createItem();
     foreach ($order->getProducts() as $product) {
         $sum->addItem($product->getPrice(), $product->getQuantity());
     }
     if ($sum->getValue() + $sum->getRebate() >= $threshold[$currency] && $price->getCosts() > '0.00') {
         $price->setRebate($price->getCosts());
         $price->setCosts('0.00');
     } else {
         if ($sum->getValue() + $sum->getRebate() < $threshold[$currency] && $price->getRebate() > '0.00') {
             $price->setCosts($price->getRebate());
             $price->setRebate('0.00');
         }
     }
 }
Exemplo n.º 6
0
 /**
  * Returns a list of tax rates and values for the given basket.
  *
  * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket containing the products, services, etc.
  * @return array Associative list of tax rates as key and corresponding amounts as value
  */
 protected function getTaxRates(\Aimeos\MShop\Order\Item\Base\Iface $basket)
 {
     $taxrates = array();
     foreach ($basket->getProducts() as $product) {
         $price = $product->getSumPrice();
         $taxrate = $price->getTaxrate();
         if (isset($taxrates[$taxrate])) {
             $taxrates[$taxrate]->addItem($price);
         } else {
             $taxrates[$taxrate] = $price->setQuantity(1);
             // sum is already calculated
         }
     }
     try {
         $price = clone $basket->getService('delivery')->getPrice();
         $taxrate = $price->getTaxrate();
         if (isset($taxrates[$taxrate])) {
             $taxrates[$taxrate]->addItem($price, $price->getQuantity());
         } else {
             $taxrates[$taxrate] = $price->setQuantity(1);
             // only single price
         }
     } catch (\Exception $e) {
     }
     // if delivery service isn't available
     try {
         $price = clone $basket->getService('payment')->getPrice();
         $taxrate = $price->getTaxrate();
         if (isset($taxrates[$taxrate])) {
             $taxrates[$taxrate]->addItem($price, $price->getQuantity());
         } else {
             $taxrates[$taxrate] = $price->setQuantity(1);
             // only single price
         }
     } catch (\Exception $e) {
     }
     // if payment service isn't available
     return $taxrates;
 }
Exemplo n.º 7
0
 /**
  * Returns a list of tax rates and values for the given basket.
  *
  * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket containing the products, services, etc.
  * @return array Associative list of tax rates as key and corresponding amounts as value
  */
 protected function getTaxRates(\Aimeos\MShop\Order\Item\Base\Iface $basket)
 {
     $taxrates = array();
     foreach ($basket->getProducts() as $product) {
         $price = $product->getPrice();
         $taxrate = $price->getTaxrate();
         if (isset($taxrates[$taxrate])) {
             $taxrates[$taxrate] += ($price->getValue() + $price->getCosts()) * $product->getQuantity();
         } else {
             $taxrates[$taxrate] = ($price->getValue() + $price->getCosts()) * $product->getQuantity();
         }
     }
     try {
         $price = $basket->getService('delivery')->getPrice();
         $taxrate = $price->getTaxrate();
         if (isset($taxrates[$taxrate])) {
             $taxrates[$taxrate] += $price->getValue() + $price->getCosts();
         } else {
             $taxrates[$taxrate] = $price->getValue() + $price->getCosts();
         }
     } catch (\Exception $e) {
     }
     // if delivery service isn't available
     try {
         $price = $basket->getService('payment')->getPrice();
         $taxrate = $price->getTaxrate();
         if (isset($taxrates[$taxrate])) {
             $taxrates[$taxrate] += $price->getValue() + $price->getCosts();
         } else {
             $taxrates[$taxrate] = $price->getValue() + $price->getCosts();
         }
     } catch (\Exception $e) {
     }
     // if payment service isn't available
     return $taxrates;
 }
Exemplo n.º 8
0
 /**
  * Saves the ordered products to the storage.
  *
  * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket containing ordered products or bundles
  */
 protected function storeProducts(\Aimeos\MShop\Order\Item\Base\Iface $basket)
 {
     $position = 1;
     $manager = $this->getSubManager('product');
     $attrManager = $manager->getSubManager('attribute');
     foreach ($basket->getProducts() as $item) {
         $baseId = $basket->getId();
         $item->setBaseId($baseId);
         if (($pos = $item->getPosition()) === null) {
             $item->setPosition($position++);
         } else {
             $position = ++$pos;
         }
         $manager->saveItem($item);
         $productId = $item->getId();
         foreach ($item->getAttributes() as $attribute) {
             $attribute->setProductId($productId);
             $attrManager->saveItem($attribute);
         }
         // if the item is a bundle, it probably contains sub-products
         foreach ($item->getProducts() as $subProduct) {
             $subProduct->setBaseId($baseId);
             $subProduct->setOrderProductId($productId);
             if (($pos = $subProduct->getPosition()) === null) {
                 $subProduct->setPosition($position++);
             } else {
                 $position = ++$pos;
             }
             $manager->saveItem($subProduct);
             $subProductId = $subProduct->getId();
             foreach ($subProduct->getAttributes() as $attribute) {
                 $attribute->setProductId($subProductId);
                 $attrManager->saveItem($attribute);
             }
         }
     }
 }
Exemplo n.º 9
0
 /**
  * Adds the product list to the XML object
  *
  * @param \Aimeos\MShop\Order\Item\Base\Iface $base Order base object
  * @param \DOMDocument $dom DOM document object with contains the XML structure
  * @param \DOMElement $orderitem DOM element which will be the parent of the new child
  * @throws DOMException If an error occures
  */
 protected function buildXMLProducts(\Aimeos\MShop\Order\Item\Base\Iface $base, \DOMDocument $dom, \DOMElement $orderitem)
 {
     $productlist = $dom->createElement('productlist');
     foreach ($base->getProducts() as $product) {
         $price = $product->getPrice();
         $total = $price->getValue() + $price->getCosts();
         $productitem = $dom->createElement('productitem');
         $this->appendChildCDATA('position', $product->getPosition(), $dom, $productitem);
         $this->appendChildCDATA('code', $product->getProductCode(), $dom, $productitem);
         $this->appendChildCDATA('name', $product->getName(), $dom, $productitem);
         $this->appendChildCDATA('quantity', $product->getQuantity(), $dom, $productitem);
         $priceitem = $dom->createElement('priceitem');
         $this->appendChildCDATA('price', number_format($price->getValue(), 2, '.', ''), $dom, $priceitem);
         $this->appendChildCDATA('shipping', number_format($price->getCosts(), 2, '.', ''), $dom, $priceitem);
         $this->appendChildCDATA('discount', number_format(0.0, 2, '.', ''), $dom, $priceitem);
         $this->appendChildCDATA('total', number_format($total, 2, '.', ''), $dom, $priceitem);
         $productitem->appendChild($priceitem);
         if ($product->getType() === 'bundle') {
             $this->buildXMLChildList($product, $product->getProducts(), $dom, $productitem);
         }
         $productlist->appendChild($productitem);
     }
     $orderitem->appendChild($productlist);
 }
Exemplo n.º 10
0
 /**
  * Migrates the products from the old basket to the current one.
  *
  * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
  * @param array $errors Associative list of previous errors
  * @param string $localeKey Unique identifier of the site, language and currency
  * @return array Associative list of errors occured
  */
 protected function copyProducts(\Aimeos\MShop\Order\Item\Base\Iface $basket, array $errors, $localeKey)
 {
     foreach ($basket->getProducts() as $pos => $product) {
         if ($product->getFlags() & \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE) {
             continue;
         }
         try {
             $attrIds = array();
             foreach ($product->getAttributes() as $attrItem) {
                 $attrIds[$attrItem->getType()][] = $attrItem->getAttributeId();
             }
             $this->addProduct($product->getProductId(), $product->getQuantity(), array(), $this->getValue($attrIds, 'variant', array()), $this->getValue($attrIds, 'config', array()), $this->getValue($attrIds, 'hidden', array()), $this->getValue($attrIds, 'custom', array()), $product->getWarehouseCode());
             $basket->deleteProduct($pos);
         } catch (\Exception $e) {
             $code = $product->getProductCode();
             $logger = $this->getContext()->getLogger();
             $str = 'Error migrating product with code "%1$s" in basket to locale "%2$s": %3$s';
             $logger->log(sprintf($str, $code, $localeKey, $e->getMessage()), \Aimeos\MW\Logger\Base::INFO);
             $errors['product'][$pos] = $e->getMessage();
         }
     }
     return $errors;
 }
Exemplo n.º 11
0
 /**
  * Checks for the product limits when the configuration contains limits per currency.
  *
  * @param \Aimeos\MShop\Order\Item\Base\Iface $order Basket object
  * @param \Aimeos\MShop\Order\Item\Base\Product\Iface $value Order product item
  * @throws \Aimeos\MShop\Plugin\Provider\Exception If one limit is exceeded
  */
 protected function checkWithCurrency(\Aimeos\MShop\Order\Item\Base\Iface $order, \Aimeos\MShop\Order\Item\Base\Product\Iface $value)
 {
     $config = $this->getItemBase()->getConfig();
     $currencyId = $value->getPrice()->getCurrencyId();
     if (isset($config['single-value-max'][$currencyId]) && $value->getPrice()->getValue() * $value->getQuantity() > (double) $config['single-value-max'][$currencyId]) {
         $msg = $this->getContext()->getI18n()->dt('mshop', 'The maximum product value is %1$s');
         throw new \Aimeos\MShop\Plugin\Provider\Exception(sprintf($msg, $config['single-value-max'][$currencyId]));
     }
     if (isset($config['total-value-max'][$currencyId])) {
         $price = clone $value->getPrice();
         $price->setValue($price->getValue() * $value->getQuantity());
         foreach ($order->getProducts() as $product) {
             $price->addItem($product->getPrice(), $product->getQuantity());
         }
         if ((double) $price->getValue() > (double) $config['total-value-max'][$currencyId]) {
             $msg = $this->getContext()->getI18n()->dt('mshop', 'The maximum value of all products is %1$s');
             throw new \Aimeos\MShop\Plugin\Provider\Exception(sprintf($msg, $config['total-value-max'][$currencyId]));
         }
     }
 }
Exemplo n.º 12
0
 /**
  * Returns a list of tax rates and their price items for the given basket.
  *
  * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket containing the products, services, etc.
  * @return array Associative list of tax rates as key and corresponding price items as value
  */
 protected function getPriceByTaxRate(\Aimeos\MShop\Order\Item\Base\Iface $basket)
 {
     $taxrates = array();
     $manager = \Aimeos\MShop\Factory::createManager($this->getContext(), 'price');
     foreach ($basket->getProducts() as $product) {
         $price = $product->getPrice();
         $taxrate = $price->getTaxRate();
         if (!isset($taxrates[$taxrate])) {
             $taxrates[$taxrate] = $manager->createItem();
         }
         $taxrates[$taxrate]->addItem($price, $product->getQuantity());
     }
     try {
         $price = $basket->getService('delivery')->getPrice();
         $taxrate = $price->getTaxRate();
         if (!isset($taxrates[$taxrate])) {
             $taxrates[$taxrate] = $manager->createItem();
         }
         $taxrates[$taxrate]->addItem($price);
     } catch (\Exception $e) {
     }
     // if delivery service isn't available
     try {
         $price = $basket->getService('payment')->getPrice();
         $taxrate = $price->getTaxRate();
         if (!isset($taxrates[$taxrate])) {
             $taxrates[$taxrate] = $manager->createItem();
         }
         $taxrates[$taxrate]->addItem($price);
     } catch (\Exception $e) {
     }
     // if payment service isn't available
     return $taxrates;
 }
Exemplo n.º 13
0
 /**
  * Returns the IDs of the products in the current basket.
  *
  * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
  * @return string[] List of product IDs
  */
 protected function getProductIdsFromBasket(\Aimeos\MShop\Order\Item\Base\Iface $basket)
 {
     $list = array();
     foreach ($basket->getProducts() as $orderProduct) {
         $list[$orderProduct->getProductId()] = true;
         foreach ($orderProduct->getProducts() as $subProduct) {
             $list[$subProduct->getProductId()] = true;
         }
     }
     return array_keys($list);
 }
Exemplo n.º 14
0
 /**
  * Returns an list of order data required by PayPal.
  *
  * @param \Aimeos\MShop\Order\Item\Base\Iface $orderBase Order base item
  * @return array Associative list of key/value pairs with order data required by PayPal
  */
 protected function getOrderDetails(\Aimeos\MShop\Order\Item\Base\Iface $orderBase)
 {
     $deliveryCosts = $paymentCosts = '0.00';
     $values = $this->getAuthParameter();
     try {
         $orderAddressDelivery = $orderBase->getAddress(\Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY);
         /* setting up the shipping address details (ReviewOrder) */
         $values['ADDROVERRIDE'] = 1;
         $values['PAYMENTREQUEST_0_SHIPTONAME'] = $orderAddressDelivery->getFirstName() . ' ' . $orderAddressDelivery->getLastName();
         $values['PAYMENTREQUEST_0_SHIPTOSTREET'] = $orderAddressDelivery->getAddress1() . ' ' . $orderAddressDelivery->getAddress2() . ' ' . $orderAddressDelivery->getAddress3();
         $values['PAYMENTREQUEST_0_SHIPTOCITY'] = $orderAddressDelivery->getCity();
         $values['PAYMENTREQUEST_0_SHIPTOSTATE'] = $orderAddressDelivery->getState();
         $values['PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE'] = $orderAddressDelivery->getCountryId();
         $values['PAYMENTREQUEST_0_SHIPTOZIP'] = $orderAddressDelivery->getPostal();
     } catch (\Exception $e) {
     }
     // If no address is available
     $lastPos = 0;
     foreach ($orderBase->getProducts() as $product) {
         $price = $product->getPrice();
         $lastPos = $product->getPosition() - 1;
         $values['L_PAYMENTREQUEST_0_NUMBER' . $lastPos] = $product->getId();
         $values['L_PAYMENTREQUEST_0_NAME' . $lastPos] = $product->getName();
         $values['L_PAYMENTREQUEST_0_QTY' . $lastPos] = $product->getQuantity();
         $values['L_PAYMENTREQUEST_0_AMT' . $lastPos] = $this->getAmount($price);
     }
     $price = $orderBase->getService('payment')->getPrice();
     if (($paymentCosts = $this->getAmount($price)) > '0.00') {
         $lastPos++;
         $values['L_PAYMENTREQUEST_0_NAME' . $lastPos] = $this->getContext()->getI18n()->dt('mshop', 'Payment costs');
         $values['L_PAYMENTREQUEST_0_QTY' . $lastPos] = '1';
         $values['L_PAYMENTREQUEST_0_AMT' . $lastPos] = $paymentCosts;
     }
     try {
         $orderServiceDeliveryItem = $orderBase->getService('delivery');
         $deliveryCosts = $this->getAmount($orderServiceDeliveryItem->getPrice());
         $values['L_SHIPPINGOPTIONAMOUNT0'] = (string) $deliveryCosts;
         $values['L_SHIPPINGOPTIONLABEL0'] = $orderServiceDeliveryItem->getName();
         $values['L_SHIPPINGOPTIONNAME0'] = $orderServiceDeliveryItem->getCode();
         $values['L_SHIPPINGOPTIONISDEFAULT0'] = 'true';
     } catch (\Exception $e) {
     }
     // If no delivery service is available
     $price = $orderBase->getPrice();
     $amount = $this->getAmount($price);
     $values['MAXAMT'] = $amount + 0.01;
     // @todo rounding error?
     $values['PAYMENTREQUEST_0_AMT'] = $amount;
     $values['PAYMENTREQUEST_0_ITEMAMT'] = number_format($price->getValue() + $paymentCosts, 2, '.', '');
     $values['PAYMENTREQUEST_0_SHIPPINGAMT'] = number_format($price->getCosts() - $paymentCosts, 2, '.', '');
     $values['PAYMENTREQUEST_0_INSURANCEAMT'] = '0.00';
     $values['PAYMENTREQUEST_0_INSURANCEOPTIONOFFERED'] = 'false';
     $values['PAYMENTREQUEST_0_SHIPDISCAMT'] = '0.00';
     $values['PAYMENTREQUEST_0_TAXAMT'] = $price->getTaxValue();
     $values['PAYMENTREQUEST_0_CURRENCYCODE'] = $orderBase->getPrice()->getCurrencyId();
     $values['PAYMENTREQUEST_0_PAYMENTACTION'] = $this->getConfigValue(array('paypalexpress.PaymentAction'), 'sale');
     return $values;
 }