/code/order/SilvercartShoppingCart.php
PHP | 1897 lines | 908 code | 253 blank | 736 comment | 125 complexity | 417ebf6265b7a21b53850fc70c0bd4d4 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * Copyright 2010, 2011 pixeltricks GmbH
- *
- * This file is part of SilverCart.
- *
- * SilverCart is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * SilverCart is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with SilverCart. If not, see <http://www.gnu.org/licenses/>.
- *
- * @package Silvercart
- * @subpackage Order
- */
- /**
- * abstract for shopping cart
- *
- * @package Silvercart
- * @subpackage Order
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2010 pixeltricks GmbH
- * @since 22.11.2010
- * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
- */
- class SilvercartShoppingCart extends DataObject {
- /**
- * Contains all registered modules that get called when the shoppingcart
- * is displayed.
- *
- * @var array
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 21.01.2011
- */
- public static $registeredModules = array();
- /**
- * Singular-Beschreibung zur Darstellung im Backend.
- *
- * @var string
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2010 pixeltricks GmbH
- * @since 22.11.2010
- */
- public static $singular_name = "cart";
- /**
- * Plural-Beschreibung zur Darstellung im Backend.
- *
- * @var string
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2010 pixeltricks GmbH
- * @since 22.11.2010
- */
- public static $plural_name = "carts";
- /**
- * 1:n relations
- *
- * @var array
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2010 pixeltricks GmbH
- * @since 22.11.2010
- */
- public static $has_many = array(
- 'SilvercartShoppingCartPositions' => 'SilvercartShoppingCartPosition'
- );
- /**
- * defines n:m relations
- *
- * @var array configure relations
- *
- * @author Roland Lehmann <rlehmann@pixeltricks.de>
- * @copyright 2010 pixeltricks GmbH
- * @since 16.12.10
- */
- public static $many_many = array(
- 'SilvercartProducts' => 'SilvercartProduct'
- );
- /**
- * Indicates wether the registered modules should be loaded.
- *
- * @var boolean
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @since 27.04.2011
- */
- public static $loadModules = true;
- /**
- * Indicates wether the registered modules should be loaded.
- *
- * @var boolean
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @since 27.04.2011
- */
- public static $createForms = true;
- /**
- * Contains the ID of the payment method the customer has chosen.
- *
- * @var Int
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 07.02.2011
- */
- protected $paymentMethodID;
- /**
- * Contains the ID of the shipping method the customer has chosen.
- *
- * @var Int
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 07.02.2011
- */
- protected $shippingMethodID;
- /**
- * Contains the calculated charges and discounts for product values for
- * caching purposes.
- *
- * @var DataObject
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @since 15.12.2011
- */
- protected $chargesAndDiscountsForProducts = null;
-
- /**
- * Contains the calculated charges and discounts for the shopping cart
- * total for caching purposes.
- *
- * @var DataObject
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @since 15.12.2011
- */
- protected $chargesAndDiscountsForTotal = null;
- /**
- * Contains hashes for caching.
- *
- * @var array
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @since 23.01.2012
- */
- protected $cacheHashes = array();
- /**
- * default constructor
- *
- * @param array $record array of field values
- * @param bool $isSingleton true if this is a singleton() object
- *
- * @return void
- *
- * @author Sebastian Diel <sdiel@pixeltricks.de>
- * @since 15.02.2011
- */
- public function __construct($record = null, $isSingleton = false) {
- parent::__construct($record, $isSingleton);
- if (array_key_exists('url', $_REQUEST)) {
- if (stripos($_REQUEST['url'], '/dev/build') !== false) {
- return;
- }
- }
- // Initialize shopping cart position object, so that it can inject
- // its forms into the controller.
- if (!self::$loadModules) {
- SilvercartShoppingCartPosition::setCreateForms(false);
- }
-
- foreach ($this->SilvercartShoppingCartPositions() as $cartPosition) {
- if ($cartPosition->SilvercartProduct()->ID == 0) {
- $cartPosition->delete();
- }
- }
-
- $this->SilvercartShippingMethodID = 0;
- $this->SilvercartPaymentMethodID = 0;
- // Check if unit test are performed: The call to Member:currentUserID()
- // would fail
- //
- // Check if the installation is complete. If it's not complete we
- // can't call the method "Member::currentUser()", since it tries to
- // get the decorated fields from SilvercartCustomer that are not
- // yet created in the database
- if (array_key_exists('QUERY_STRING', $_SERVER) && (strpos($_SERVER['QUERY_STRING'], 'dev/tests') !== false || strpos($_SERVER['QUERY_STRING'], 'dev/build') !== false)) {
- return true;
- }
- if (!SapphireTest::is_running_test() &&
- SilvercartConfig::isInstallationCompleted() &&
- Member::currentUserID() &&
- self::$loadModules) {
- $this->callMethodOnRegisteredModules(
- 'performShoppingCartConditionsCheck',
- array(
- $this,
- Member::currentUser()
- )
- );
- $this->callMethodOnRegisteredModules(
- 'ShoppingCartInit'
- );
- }
- }
- /**
- * Indicates wether the cart has charges and discounts for the product
- * values.
- *
- * @return boolean
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @since 15.12.2011
- */
- public function HasChargesAndDiscountsForProducts() {
- if ($this->ChargesAndDiscountsForProducts()) {
- return true;
- }
-
- return false;
- }
-
- /**
- * Indicates wether the cart has charges and discounts for the total
- * shopping cart value.
- *
- * @return boolean
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @since 15.12.2011
- */
- public function HasChargesAndDiscountsForTotal() {
- if ($this->ChargesAndDiscountsForTotal()) {
- return true;
- }
-
- return false;
- }
-
- /**
- * Returns the charges and discounts for product values.
- *
- * @param string $priceType 'gross' or 'net'
- *
- * @return void
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @since 14.12.2011
- */
- public function ChargesAndDiscountsForProducts($priceType = false) {
- $cacheHash = md5($priceType);
- $cacheKey = 'ChargesAndDiscountsForProducts_'.$cacheHash;
- if (array_key_exists($cacheKey, $this->cacheHashes)) {
- return $this->cacheHashes[$cacheKey];
- }
- $paymentMethodObj = DataObject::get_by_id(
- 'SilvercartPaymentMethod', $this->SilvercartPaymentMethodID
- );
- if ($paymentMethodObj) {
- $handlingCostPayment = $paymentMethodObj->getChargesAndDiscountsForProducts($this, $priceType);
-
- if ($handlingCostPayment === false) {
- return false;
- } else {
- $taxes = $this->getTaxRatesWithoutFeesAndCharges('SilvercartVoucher');
- $silvercartTax = $this->getMostValuableTaxRate($taxes);
- $chargesAndDiscounts = new DataObject(
- array(
- 'Name' => $paymentMethodObj->sumModificationLabel,
- 'sumModificationImpact' => $paymentMethodObj->sumModificationImpact,
- 'PriceFormatted' => $handlingCostPayment->Nice(),
- 'Price' => $handlingCostPayment,
- 'SilvercartTax' => $silvercartTax
- )
- );
- $this->chargesAndDiscountsForProducts = $chargesAndDiscounts;
- $this->cacheHashes[$cacheKey] = $this->chargesAndDiscountsForProducts;
- return $chargesAndDiscounts;
- }
- }
- return false;
- }
-
- /**
- * Returns the charges and discounts for the shopping cart total.
- *
- * @param string $priceType 'gross' or 'net'
- *
- * @return void
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @since 15.12.2011
- */
- public function ChargesAndDiscountsForTotal($priceType = false) {
- $cacheHash = md5($priceType);
- $cacheKey = 'ChargesAndDiscountsForTotal_'.$cacheHash;
- if (array_key_exists($cacheKey, $this->cacheHashes)) {
- return $this->cacheHashes[$cacheKey];
- }
- $paymentMethodObj = DataObject::get_by_id(
- 'SilvercartPaymentMethod', $this->SilvercartPaymentMethodID
- );
- if ($paymentMethodObj) {
- $handlingCostPayment = $paymentMethodObj->getChargesAndDiscountsForTotal($this, $priceType);
-
- if ($handlingCostPayment === false) {
- return false;
- } else {
- $taxes = $this->getTaxRatesWithFees();
- $silvercartTax = $this->getMostValuableTaxRate($taxes);
- $handlingCostPaymentRounded = $handlingCostPayment;
- $handlingCostPaymentRounded->setAmount(
- round($handlingCostPayment->getAmount(), 2)
- );
- $chargesAndDiscounts = new DataObject(
- array(
- 'Name' => $paymentMethodObj->sumModificationLabel,
- 'sumModificationImpact' => $paymentMethodObj->sumModificationImpact,
- 'PriceFormatted' => $handlingCostPayment->Nice(),
- 'Price' => $handlingCostPayment,
- 'SilvercartTax' => $silvercartTax
- )
- );
- $this->chargesAndDiscountsForTotal = $chargesAndDiscounts;
- $this->cacheHashes[$cacheKey] = $this->chargesAndDiscountsForTotal;
- return $chargesAndDiscounts;
- }
- }
- return false;
- }
- /**
- * Set wether the registered modules should be loaded and handled.
- *
- * @param boolean $doLoad set wether to load the modules or not
- *
- * @return void
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 27.04.2011
- */
- public static function setLoadShoppingCartModules($doLoad) {
- self::$loadModules = $doLoad;
- }
- /**
- * Set wether the shopping cart forms should be drawn.
- *
- * @param boolean $doCreate set wether to create the forms or not
- *
- * @return void
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 27.04.2011
- */
- public static function setCreateShoppingCartForms($doCreate) {
- self::$createForms = $doCreate;
- }
- /**
- * adds a product to the cart
- *
- * @param array $formData the sended form data
- *
- * @return bool
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2010 pixeltricks GmbH
- * @since 21.12.2010
- */
- public static function addProduct($formData) {
- $error = true;
- $member = Member::currentUser();
-
- if (!$member) {
- $member = SilvercartCustomer::createAnonymousCustomer();
- }
-
- $overwriteAddProduct = SilvercartPlugin::call($member->getCart(), 'overwriteAddProduct', array($formData), false, 'boolean');
-
- if ($overwriteAddProduct) {
- $error = false;
- } else {
- if ($formData['productID'] && $formData['productQuantity']) {
- $cart = $member->getCart();
- if ($cart) {
- $product = DataObject::get_by_id('SilvercartProduct', $formData['productID'], 'Created');
- if ($product) {
- $quantity = (int) $formData['productQuantity'];
- if ($quantity > 0) {
- $product->addToCart($cart->ID, $quantity);
- $error = false;
- }
- }
- }
- }
- }
-
- return !$error;
- }
- /**
- * empties cart
- *
- * @return void
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2010 pixeltricks GmbH
- * @since 22.11.2010
- */
- public function delete() {
- $positions = $this->SilvercartShoppingCartPositions();
- foreach ($positions as $position) {
- $position->delete();
- }
- }
- /**
- * returns quantity of all products in the cart
- *
- * @param int $productId if set only product quantity of this product is returned
- *
- * @return int
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @since 22.11.10
- */
- public function getQuantity($productId = null) {
- $positions = $this->SilvercartShoppingCartPositions();
- $quantity = 0;
- foreach ($positions as $position) {
- if ($productId === null ||
- $position->SilvercartProduct()->ID === $productId) {
- $quantity += $position->Quantity;
- }
- }
- return $quantity;
- }
- /**
- * Returns the price of the cart positions + fees, including taxes.
- *
- * @param array $excludeShoppingCartPositions Positions that shall not be counted;
- * can contain the ID or the className of the position
- * @param boolean $excludeCharges Indicates wether charges and discounts should be calculated
- *
- * @return string a price amount
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 04.02.2011
- */
- public function getTaxableAmountGrossWithFees($excludeShoppingCartPositions = false, $excludeCharges = false) {
- $member = Member::currentUser();
- $shippingMethod = DataObject::get_by_id('SilvercartShippingMethod', $this->SilvercartShippingMethodID);
- $paymentMethod = DataObject::get_by_id('SilvercartPaymentMethod', $this->SilvercartPaymentMethodID);
- $amountTotal = $this->getTaxableAmountGrossWithoutFees(null, $excludeShoppingCartPositions, $excludeCharges)->getAmount();
- if ($shippingMethod) {
- $shippingFee = $shippingMethod->getShippingFee();
- if ($shippingFee !== false) {
- $shippingFeeAmount = $shippingFee->Price->getAmount();
- $amountTotal = $shippingFeeAmount + $amountTotal;
- }
- }
- if ($paymentMethod) {
- $paymentFee = $paymentMethod->SilvercartHandlingCost();
- if ($paymentFee !== false) {
- $paymentFeeAmount = $paymentFee->amount->getAmount();
- $amountTotal = $paymentFeeAmount + $amountTotal;
- }
- }
-
- $amountTotalObj = new Money;
- $amountTotalObj->setAmount($amountTotal);
- $amountTotalObj->setCurrency(SilvercartConfig::DefaultCurrency());
- return $amountTotalObj;
- }
- /**
- * Returns the net price of the cart positions + fees, including taxes.
- *
- * @param array $excludeShoppingCartPositions Positions that shall not be counted;
- * can contain the ID or the className of the position
- * @param boolean $excludeCharges Indicates wether charges and discounts should be calculated
- *
- * @return string a price amount
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2012 pixeltricks GmbH
- * @since 20.01.2012
- */
- public function getTaxableAmountNetWithFees($excludeShoppingCartPositions = false, $excludeCharges = false) {
- $member = Member::currentUser();
- $shippingMethod = DataObject::get_by_id('SilvercartShippingMethod', $this->SilvercartShippingMethodID);
- $paymentMethod = DataObject::get_by_id('SilvercartPaymentMethod', $this->SilvercartPaymentMethodID);
- $amountTotal = round($this->getTaxableAmountNetWithoutFees(null, $excludeShoppingCartPositions, $excludeCharges)->getAmount(), 2);
- if ($shippingMethod) {
- $shippingFee = $shippingMethod->getShippingFee();
- if ($shippingFee !== false) {
- $shippingFeeAmount = $shippingFee->getPriceAmount();
- $amountTotal = $shippingFeeAmount + $amountTotal;
- }
- }
- if ($paymentMethod) {
- $paymentFee = $paymentMethod->SilvercartHandlingCost();
- if ($paymentFee !== false) {
- $paymentFeeAmount = $paymentFee->getPriceAmount();
- $amountTotal = $paymentFeeAmount + $amountTotal;
- }
- }
- if (round($amountTotal, 2) === -0.00) {
- $amountTotal *= -1;
- }
-
- $amountTotalObj = new Money;
- $amountTotalObj->setAmount($amountTotal);
- $amountTotalObj->setCurrency(SilvercartConfig::DefaultCurrency());
- return $amountTotalObj;
- }
-
- /**
- * Returns the price of the cart positions, including taxes.
- *
- * @param array $excludeModules An array of registered modules that shall not
- * be taken into account.
- * @param array $excludeShoppingCartPosition Positions that shall not be counted;
- * can contain the ID or the className of the position
- * @param boolean $excludeCharges Indicates wether charges and discounts should be calculated
- *
- * @return Money a price amount
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 15.12.2011
- */
- public function getTaxableAmountGrossWithoutFees($excludeModules = array(), $excludeShoppingCartPosition = false, $excludeCharges = false) {
- $amount = $this->getTaxableAmountGrossWithoutFeesAndCharges($excludeModules, $excludeShoppingCartPosition)->getAmount();
-
- // Handling costs for payment and shipment
- if (!$excludeCharges &&
- $this->ChargesAndDiscountsForProducts()) {
-
- $amount += $this->ChargesAndDiscountsForProducts()->Price->getAmount();
- }
-
- $amountObj = new Money;
- $amountObj->setAmount($amount);
- $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
- return $amountObj;
- }
- /**
- * Returns the price of the cart positions.
- *
- * @param array $excludeModules An array of registered modules that shall not
- * be taken into account.
- * @param array $excludeShoppingCartPosition Positions that shall not be counted;
- * can contain the ID or the className of the position
- * @param boolean $excludeCharges Indicates wether charges and discounts should be calculated
- *
- * @return Money a price amount
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 15.12.2011
- */
- public function getTaxableAmountNetWithoutFees($excludeModules = array(), $excludeShoppingCartPosition = false, $excludeCharges = false) {
- $amount = $this->getTaxableAmountNetWithoutFeesAndCharges($excludeModules, $excludeShoppingCartPosition)->getAmount();
-
- // Handling costs for payment and shipment
- if (!$excludeCharges &&
- $this->ChargesAndDiscountsForProducts()) {
-
- $amount += $this->ChargesAndDiscountsForProducts()->Price->getAmount();
- }
-
- if (round($amount, 2) === -0.00) {
- $amount *= -1;
- }
- $amountObj = new Money;
- $amountObj->setAmount($amount);
- $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
- return $amountObj;
- }
-
- /**
- * Returns the price of the cart positions without modules.
- *
- * The price type is automatically determined by the
- * SilvercartShoppinCartPosition.
- *
- * @return Money a price amount
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 15.12.2011
- */
- public function getTaxableAmountGrossWithoutModules() {
- $amount = 0;
- // products
- foreach ($this->SilvercartShoppingCartPositions() as $position) {
- $amount += $position->getPrice(false, 'gross')->getAmount();
- }
- $amountObj = new Money;
- $amountObj->setAmount($amount);
- $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
- return $amountObj;
- }
-
- /**
- * Returns the price of the cart positions without modules.
- *
- * The price type is automatically determined by the
- * SilvercartShoppinCartPosition.
- *
- * @return Money a price amount
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 15.12.2011
- */
- public function getTaxableAmountNetWithoutModules() {
- $amount = 0;
- // products
- foreach ($this->SilvercartShoppingCartPositions() as $position) {
- $amount += $position->getPrice(false, 'net')->getAmount();
- }
- if (round($amount, 2) === -0.00) {
- $amount *= -1;
- }
- $amountObj = new Money;
- $amountObj->setAmount($amount);
- $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
- return $amountObj;
- }
- /**
- * Returns the price of the cart positions, including taxes.
- *
- * @param array $excludeModules An array of registered modules that shall not
- * be taken into account.
- * @param array $excludeShoppingCartPosition Positions that shall not be counted;
- * can contain the ID or the className of the position
- *
- * @return Money a price amount
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 15.12.2011
- */
- public function getTaxableAmountGrossWithoutFeesAndCharges($excludeModules = array(), $excludeShoppingCartPosition = false) {
- if (!is_array($excludeModules)) {
- $excludeModules = array($excludeModules);
- }
- if (!is_array($excludeShoppingCartPosition)) {
- $excludeShoppingCartPosition = array($excludeShoppingCartPosition);
- }
- $cacheHash = md5(
- implode(',', $excludeModules).
- implode(',', $excludeShoppingCartPosition)
- );
- $cacheKey = 'getTaxableAmountGrossWithoutFeesAndCharges_'.$cacheHash;
- if (array_key_exists($cacheKey, $this->cacheHashes)) {
- return $this->cacheHashes[$cacheKey];
- }
- $amountObj = $this->getTaxableAmountGrossWithoutModules();
- $amount = $amountObj->getAmount();
- $registeredModules = $this->callMethodOnRegisteredModules(
- 'ShoppingCartPositions',
- array(
- $this,
- Member::currentUser(),
- true,
- $excludeShoppingCartPosition,
- false
- ),
- $excludeModules,
- $excludeShoppingCartPosition
- );
- // Registered Modules
- if ($registeredModules) {
- foreach ($registeredModules as $moduleName => $modulePositions) {
- foreach ($modulePositions as $modulePosition) {
- $amount += (float) $modulePosition->PriceTotal;
- }
- }
- }
- $amountObj->setAmount($amount);
- $this->cacheHashes[$cacheKey] = $amountObj;
-
- return $amountObj;
- }
- /**
- * Returns the price of the cart positions.
- *
- * @param array $excludeModules An array of registered modules that shall not
- * be taken into account.
- * @param array $excludeShoppingCartPosition Positions that shall not be counted;
- * can contain the ID or the className of the position
- *
- * @return Money a price amount
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 15.12.2011
- */
- public function getTaxableAmountNetWithoutFeesAndCharges($excludeModules = array(), $excludeShoppingCartPosition = false) {
- if (!is_array($excludeModules)) {
- $excludeModules = array($excludeModules);
- }
- if (!is_array($excludeShoppingCartPosition)) {
- $excludeShoppingCartPosition = array($excludeShoppingCartPosition);
- }
- $cacheHash = md5(
- implode(',', $excludeModules).
- implode(',', $excludeShoppingCartPosition)
- );
- $cacheKey = 'getTaxableAmountGrossWithoutFeesAndCharges_'.$cacheHash;
- if (array_key_exists($cacheKey, $this->cacheHashes)) {
- return $this->cacheHashes[$cacheKey];
- }
- $amountObj = $this->getTaxableAmountNetWithoutModules();
- $amount = $amountObj->getAmount();
- $registeredModules = $this->callMethodOnRegisteredModules(
- 'ShoppingCartPositions',
- array(
- $this,
- Member::currentUser(),
- true,
- $excludeShoppingCartPosition,
- false
- ),
- $excludeModules,
- $excludeShoppingCartPosition
- );
- // Registered Modules
- if ($registeredModules) {
- foreach ($registeredModules as $moduleName => $modulePositions) {
- foreach ($modulePositions as $modulePosition) {
- $amount += (float) $modulePosition->PriceNetTotal;
- }
- }
- }
- if (round($amount, 2) === -0.00) {
- $amount *= -1;
- }
- $amountObj->setAmount($amount);
- $this->cacheHashes[$cacheKey] = $amountObj;
-
- return $amountObj;
- }
- /**
- * Returns the total amount of all taxes.
- *
- * @param boolean $excludeCharges Indicates wether to exlude charges and discounts
- *
- * @return Money a price amount
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 07.04.2011
- */
- public function getTaxTotal($excludeCharges = false) {
- $taxRates = $this->getTaxRatesWithFees();
- if (!$excludeCharges &&
- $this->HasChargesAndDiscountsForTotal()) {
- foreach ($this->ChargesAndDiscountsForTotal() as $charge) {
- if ($charge->SilvercartTax === false) {
- continue;
- }
- $taxRate = $taxRates->find('Rate', $charge->SilvercartTax->Rate);
- if ($taxRate) {
- $amount = $charge->Price->getAmount();
- if (SilvercartConfig::PriceType() == 'gross') {
- $rateAmount = $amount - ($amount / (100 + $charge->SilvercartTax->Rate) * 100);
- } else {
- $rateAmount = ($amount / 100 * (100 + $charge->SilvercartTax->Rate)) - $amount;
- }
- $taxRate->AmountRaw += $rateAmount;
- if (round($taxRate->AmountRaw, 2) === -0.00) {
- $taxRate->AmountRaw *= -1;
- }
- $taxRate->Amount->setAmount($taxRate->AmountRaw);
- }
- }
- }
- return $taxRates;
- }
- /**
- * Returns the non taxable amount of positions in the shopping cart.
- * Those can originate from registered modules only.
- *
- * @param array $excludeModules An array of registered modules that shall not
- * be taken into account.
- * @param array $excludeShoppingCartPosition Positions that shall not be counted
- *
- * @return Money
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 04.02.2011
- */
- public function getNonTaxableAmount($excludeModules = array(), $excludeShoppingCartPosition = false) {
- $amount = 0;
- $registeredModules = $this->callMethodOnRegisteredModules(
- 'ShoppingCartPositions',
- array(
- $this,
- Member::currentUser(),
- false,
- $excludeShoppingCartPosition
- ),
- $excludeModules,
- $excludeShoppingCartPosition
- );
- // Registered Modules
- foreach ($registeredModules as $moduleName => $modulePositions) {
- foreach ($modulePositions as $modulePosition) {
- $amount += (float) $modulePosition->PriceTotal;
- }
- }
- $amountObj = new Money;
- $amountObj->setAmount($amount);
- $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
- return $amountObj;
- }
- /**
- * Returns the handling costs for the chosen payment method.
- *
- * @return Money
- *
- * @author Roland Lehmann <rlehmann@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 26.1.2011
- */
- public function HandlingCostPayment() {
- $handlingCostPayment = 0;
- $paymentMethodObj = DataObject::get_by_id(
- 'SilvercartPaymentMethod', $this->SilvercartPaymentMethodID
- );
- if ($paymentMethodObj) {
- $handlingCostPaymentObj = $paymentMethodObj->getHandlingCost();
- } else {
- $handlingCostPaymentObj = new Money();
- $handlingCostPaymentObj->setAmount(0);
- $handlingCostPaymentObj->setCurrency(SilvercartConfig::DefaultCurrency());
- }
- if (SilvercartConfig::PriceType() == 'net') {
- $taxRate = $this->getMostValuableTaxRate($this->getTaxRatesWithoutFeesAndCharges('SilvercartVoucher'));
- $handlingCostPayment = round(($handlingCostPaymentObj->getAmount() / (100 + $taxRate->Rate) * 100), 2);
- $handlingCostPaymentObj->setAmount($handlingCostPayment);
- }
- return $handlingCostPaymentObj;
- }
- /**
- * Returns the handling costs for the chosen shipping method.
- *
- * @return Money
- *
- * @author Roland Lehmann <rlehmann@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 26.1.2011
- */
- public function HandlingCostShipment() {
- $handlingCostShipment = 0;
- $selectedShippingMethod = DataObject::get_by_id(
- 'SilvercartShippingMethod', $this->SilvercartShippingMethodID
- );
- if ($selectedShippingMethod) {
- $handlingCostShipmentObj = $selectedShippingMethod->getShippingFee()->Price;
- } else {
- $handlingCostShipmentObj = new Money();
- $handlingCostShipmentObj->setAmount($handlingCostShipment);
- $handlingCostShipmentObj->setCurrency(SilvercartConfig::DefaultCurrency());
- }
- if (SilvercartConfig::PriceType() == 'net') {
- $taxRate = $this->getMostValuableTaxRate($this->getTaxRatesWithoutFeesAndCharges('SilvercartVoucher'));
- $handlingCostShipment = round(($handlingCostShipmentObj->getAmount() / (100 + $taxRate->Rate) * 100), 2);
- $handlingCostShipmentObj->setAmount($handlingCostShipment);
- }
- return $handlingCostShipmentObj;
- }
- /**
- * Returns the shipping method title.
- *
- * @return string
- *
- * @author Roland Lehmann <rlehmann@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 26.1.2011
- */
- public function CarrierAndShippingMethodTitle() {
- $title = '';
- $selectedShippingMethod = DataObject::get_by_id(
- 'SilvercartShippingMethod', $this->SilvercartShippingMethodID
- );
- if ($selectedShippingMethod) {
- $title = $selectedShippingMethod->SilvercartCarrier()->Title . "-" . $selectedShippingMethod->Title;
- }
- return $title;
- }
- /**
- * Returns the payment method object.
- *
- * @return PaymentMethod
- *
- * @author Roland Lehmann <rlehmann@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 26.1.2011
- */
- public function getPayment() {
- $paymentMethodObj = DataObject::get_by_id(
- 'SilvercartPaymentMethod', $this->SilvercartPaymentMethodID
- );
- return $paymentMethodObj;
- }
- /**
- * Returns the minimum order value.
- *
- * @return mixed Money
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 09.06.2011
- */
- public function MinimumOrderValue() {
- $minimumOrderValue = new Money();
- if (SilvercartConfig::UseMinimumOrderValue() &&
- SilvercartConfig::MinimumOrderValue()) {
- $minimumOrderValue->setAmount(SilvercartConfig::MinimumOrderValue()->getAmount());
- $minimumOrderValue->setCurrency(SilvercartConfig::MinimumOrderValue()->getCurrency());
- }
- return $minimumOrderValue->Nice();
- }
- /**
- * Indicates wether the minimum order value is reached.
- *
- * @return bool
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 09.06.2011
- */
- public function IsMinimumOrderValueReached() {
- if (SilvercartConfig::UseMinimumOrderValue() &&
- SilvercartConfig::MinimumOrderValue() &&
- SilvercartConfig::MinimumOrderValue()->getAmount() > $this->getAmountTotalWithoutFees()->getAmount()) {
- return false;
- }
- return true;
- }
-
- /**
- * In case stock management is enabled: Find out if all positions quantities
- * are still in stock
- *
- * @return bool Can this cart be checkt out?
- *
- * @author Roland Lehmann <rlehmann@pixeltricks.de>
- * @since 18.7.2011
- */
- public function isAvailableInStock() {
- $positions = $this->SilvercartShoppingCartPositions();
- if ($positions) {
- $isCheckoutable = true;
- foreach ($positions as $position) {
- if ($position->Quantity > $position->SilvercartProduct()->StockQuantity) {
- $isCheckoutable = false;
- break;
- }
- }
- return $isCheckoutable;
- } else {
- return false;
- }
- }
- /**
- * Returns the end sum of the cart (taxable positions + nontaxable
- * positions + fees).
- *
- * @param array $excludeModules An array of registered modules that shall not
- * be taken into account.
- * @param array $excludeShoppingCartPositions Positions that shall not be counted
- * @param boolean $excludeCharges Indicates wether to exlude charges and discounts
- *
- * @return string a price amount
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 04.02.2011
- */
- public function getAmountTotal($excludeModules = array(), $excludeShoppingCartPositions = false, $excludeCharges = false) {
- if (SilvercartConfig::PriceType() == 'gross') {
- return $this->getAmountTotalGross($excludeModules, $excludeShoppingCartPositions, $excludeCharges);
- } else {
- return $this->getAmountTotalNet($excludeModules, $excludeShoppingCartPositions, $excludeCharges);
- }
- }
- /**
- * Returns the end sum of the cart (taxable positions + nontaxable
- * positions + fees).
- *
- * @param array $excludeModules An array of registered modules that shall not
- * be taken into account.
- * @param array $excludeShoppingCartPositions Positions that shall not be counted
- * @param boolean $excludeCharges Indicates wether to exlude charges and discounts
- *
- * @return string a price amount
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 04.02.2011
- */
- public function getAmountTotalGross($excludeModules = array(), $excludeShoppingCartPositions = false, $excludeCharges = false) {
- $amount = $this->getTaxableAmountGrossWithFees($excludeShoppingCartPositions)->getAmount();
- $amount += $this->getNonTaxableAmount($excludeModules, $excludeShoppingCartPositions)->getAmount();
-
- // Handling costs for payment and shipment
- if (!$excludeCharges &&
- $this->HasChargesAndDiscountsForTotal()) {
-
- $amount += $this->ChargesAndDiscountsForTotal('gross')->Price->getAmount();
- }
-
- $amountObj = new Money;
- $amountObj->setAmount($amount);
- $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
- return $amountObj;
- }
- /**
- * Returns the end sum of the cart (taxable positions + nontaxable
- * positions + fees) excluding vat.
- *
- * @param array $excludeModules An array of registered modules that shall not
- * be taken into account.
- * @param array $excludeShoppingCartPositions Positions that shall not be counted
- * @param boolean $excludeCharges Indicates wether to exlude charges and discounts
- *
- * @return string a price amount
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 04.02.2011
- */
- public function getAmountTotalNet($excludeModules = array(), $excludeShoppingCartPositions = false, $excludeCharges = false) {
- $amountObj = $this->getAmountTotalNetWithoutVat($excludeModules, $excludeShoppingCartPositions, $excludeCharges);
- $amount = $amountObj->getAmount();
- foreach ($this->getTaxTotal($excludeCharges) as $tax) {
- $amount += $tax->Amount->getAmount();
- }
- $amountObj->setAmount($amount);
- return $amountObj;
- }
- /**
- * Returns the end sum of the cart (taxable positions + nontaxable
- * positions + fees) excluding vat.
- *
- * @param array $excludeModules An array of registered modules that shall not
- * be taken into account.
- * @param array $excludeShoppingCartPositions Positions that shall not be counted
- * @param boolean $excludeCharges Indicates wether to exlude charges and discounts
- *
- * @return string a price amount
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 04.02.2011
- */
- public function getAmountTotalNetWithoutVat($excludeModules = array(), $excludeShoppingCartPositions = false, $excludeCharges = false) {
- $amount = $this->getTaxableAmountNetWithFees($excludeShoppingCartPositions)->getAmount();
- $amount += $this->getNonTaxableAmount($excludeModules, $excludeShoppingCartPositions)->getAmount();
- // Handling costs for payment and shipment
- if (!$excludeCharges &&
- $this->HasChargesAndDiscountsForTotal()) {
-
- $amount += $this->ChargesAndDiscountsForTotal('net')->Price->getAmount();
- }
- if (round($amount, 2) === -0.00) {
- $amount *= -1;
- }
- $amountObj = new Money;
- $amountObj->setAmount($amount);
- $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
- return $amountObj;
- }
- /**
- * Returns the end sum of the cart (taxable positions + nontaxable
- * positions + fees) without any taxes.
- *
- * @param array $excludeModules An array of registered modules that shall not
- * be taken into account.
- * @param array $excludeShoppingCartPositions Positions that shall not be counted
- * @param boolean $excludeCharges Indicates wether to exlude charges and discounts
- *
- * @return string a price amount
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 04.02.2011
- */
- public function getAmountTotalWithoutTaxes($excludeModules = array(), $excludeShoppingCartPositions = false, $excludeCharges = false) {
- $amount = $this->getTaxableAmountGrossWithFees($excludeShoppingCartPositions)->getAmount();
- $amount += $this->getNonTaxableAmount($excludeModules, $excludeShoppingCartPositions)->getAmount();
-
- // Handling costs for payment and shipment
- if (!$excludeCharges &&
- $this->ChargesAndDiscountsForTotal()) {
-
- $amount += $this->ChargesAndDiscountsForTotal()->Price->getAmount();
- }
- if (round($amount, 2) === -0.00) {
- $amount *= -1;
- }
-
- $amountObj = new Money;
- $amountObj->setAmount($amount);
- $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
- return $amountObj;
- }
- /**
- * Returns the end sum of the cart without fees (taxable positions +
- * nontaxable positions).
- *
- * @param array $excludeModules An array of registered modules that shall not
- * be taken into account.
- * @param array $excludeShoppingCartPositions Positions that shall not be counted
- * @param boolean $excludeCharges Indicates wether to exlude charges and discounts
- *
- * @return string a price amount
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 12.05.2011
- */
- public function getAmountTotalGrossWithoutFees($excludeModules = array(), $excludeShoppingCartPositions = false, $excludeCharges = false) {
- $amount = $this->getTaxableAmountGrossWithoutFees($excludeModules, $excludeShoppingCartPositions, $excludeCharges)->getAmount();
- $amount += $this->getNonTaxableAmount($excludeModules, $excludeShoppingCartPositions)->getAmount();
- if (round($amount, 2) === -0.00) {
- $amount *= -1;
- }
- $amountObj = new Money;
- $amountObj->setAmount($amount);
- $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
- return $amountObj;
- }
- /**
- * Returns the end sum of the cart without fees (taxable positions +
- * nontaxable positions).
- *
- * @param array $excludeModules An array of registered modules that shall not
- * be taken into account.
- * @param array $excludeShoppingCartPositions Positions that shall not be counted
- * @param boolean $excludeCharges Indicates wether to exlude charges and discounts
- *
- * @return string a price amount
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 12.05.2011
- */
- public function getAmountTotalNetWithoutFees($excludeModules = array(), $excludeShoppingCartPositions = false, $excludeCharges = false) {
- $amount = $this->getTaxableAmountNetWithoutFees($excludeModules, $excludeShoppingCartPositions, $excludeCharges)->getAmount();
- $amount += $this->getNonTaxableAmount($excludeModules, $excludeShoppingCartPositions)->getAmount();
- if (round($amount, 2) === -0.00) {
- $amount *= -1;
- }
- $amountObj = new Money;
- $amountObj->setAmount($amount);
- $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
- return $amountObj;
- }
-
- /**
- * Returns the tax rates for shipping and payment fees.
- *
- * @return DataObjectSet
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @since 23.01.2012
- */
- public function getTaxRatesForFees() {
- $taxes = new DataObjectSet;
- $taxAmount = 0;
- $shippingMethod = DataObject::get_by_id('SilvercartShippingMethod', $this->SilvercartShippingMethodID);
- $paymentMethod = DataObject::get_by_id('SilvercartPaymentMethod', $this->SilvercartPaymentMethodID);
- if ($shippingMethod) {
- $shippingFee = $shippingMethod->getShippingFee();
- if ($shippingFee) {
- $taxAmount += $shippingFee->getTaxAmount();
- }
- }
- if ($paymentMethod) {
- $paymentFee = $paymentMethod->SilvercartHandlingCost();
- if ($paymentFee) {
- $taxAmount += $paymentFee->getTaxAmount();
- }
- }
- $taxRate = $this->getMostValuableTaxRate($this->getTaxRatesWithoutFeesAndCharges())->Rate;
- if (!$taxes->find('Rate', $taxRate)) {
- $taxes->push(
- new DataObject(
- array(
- 'Rate' => $taxRate,
- 'AmountRaw' => $taxAmount,
- )
- )
- );
- }
- foreach ($taxes as $tax) {
- $taxObj = new Money;
- $taxObj->setAmount($tax->AmountRaw);
- $taxObj->setCurrency(SilvercartConfig::DefaultCurrency());
- $tax->Amount = $taxObj;
- }
- return $taxes;
- }
- /**
- * Returns tax amounts included in the shoppingcart separated by tax rates
- * with fee taxes.
- *
- * @return DataObjectSet
- *
- * @author Sascha Koehler <skoehler@pixeltricks.de>
- * @copyright 2011 pixeltricks GmbH
- * @since 04.02.2011
- */
- public function getTaxRatesWithFees() {
- $taxes = $this->getTaxRatesWithoutFees();
- $shippingMethod = DataObject::get_by_id('SilvercartShippingMethod', $this->SilvercartShippingMethodID);
- $paymentMethod = DataObject::get_by_id('SilvercartPaymentMethod', $this->SilvercartPaymentMethodID);
- if ($shippingMethod) {
- $shippingFee = $shippingMethod->getShippingFee();
- if ($shippingFee) {
- if ($shippingFee->SilvercartTax()) {
- $taxRate = $shippingFee->getTaxRate();
- if ( $taxRate &&
- !$taxes->find('Rate', $taxRate)) {
- $taxes->push(
- new DataObject(
- array(
- 'Rate' => $taxRate,
- 'AmountRaw' => 0.0,
- )
- )
- );
- }
- $taxSection = $taxes->find('Rate', $taxRate);
- $taxSection->AmountRaw += $shippingFee->getTaxAmount();
- }
- }
- }
- if ($paymentMethod) {
- $paymentFee = $paymentMethod->SilvercartHandlingCost();
- if ($paymentFee) {
- if ($paymentFee->SilvercartTax()) {
- $taxRate = $paymentFee->SilvercartTax()->getTaxRate();
- if ( $taxRate &&
- !$taxes->find('Rate', $taxRate)) {
- $taxes->push(
- new DataObject(
- array(
- 'Rate' => $taxRate,
- 'AmountRaw' => 0.0,
- )
- )
- );
- }
- $taxSection = $taxes->find('Rate', $taxRate);
- $taxSection->AmountRaw += $paymentFee->getTaxAmount();
- }
- }
- }
- foreach ($taxes as $tax) {
- $taxObj = new Money;
- $taxObj->setAmount(round($tax->…
Large files files are truncated, but you can click here to view the full file