PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/code/order/SilvercartShoppingCart.php

https://bitbucket.org/silvercart/silvercart/
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

  1. <?php
  2. /**
  3. * Copyright 2010, 2011 pixeltricks GmbH
  4. *
  5. * This file is part of SilverCart.
  6. *
  7. * SilverCart is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SilverCart is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SilverCart. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @package Silvercart
  21. * @subpackage Order
  22. */
  23. /**
  24. * abstract for shopping cart
  25. *
  26. * @package Silvercart
  27. * @subpackage Order
  28. * @author Sascha Koehler <skoehler@pixeltricks.de>
  29. * @copyright 2010 pixeltricks GmbH
  30. * @since 22.11.2010
  31. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  32. */
  33. class SilvercartShoppingCart extends DataObject {
  34. /**
  35. * Contains all registered modules that get called when the shoppingcart
  36. * is displayed.
  37. *
  38. * @var array
  39. *
  40. * @author Sascha Koehler <skoehler@pixeltricks.de>
  41. * @copyright 2011 pixeltricks GmbH
  42. * @since 21.01.2011
  43. */
  44. public static $registeredModules = array();
  45. /**
  46. * Singular-Beschreibung zur Darstellung im Backend.
  47. *
  48. * @var string
  49. *
  50. * @author Sascha Koehler <skoehler@pixeltricks.de>
  51. * @copyright 2010 pixeltricks GmbH
  52. * @since 22.11.2010
  53. */
  54. public static $singular_name = "cart";
  55. /**
  56. * Plural-Beschreibung zur Darstellung im Backend.
  57. *
  58. * @var string
  59. *
  60. * @author Sascha Koehler <skoehler@pixeltricks.de>
  61. * @copyright 2010 pixeltricks GmbH
  62. * @since 22.11.2010
  63. */
  64. public static $plural_name = "carts";
  65. /**
  66. * 1:n relations
  67. *
  68. * @var array
  69. *
  70. * @author Sascha Koehler <skoehler@pixeltricks.de>
  71. * @copyright 2010 pixeltricks GmbH
  72. * @since 22.11.2010
  73. */
  74. public static $has_many = array(
  75. 'SilvercartShoppingCartPositions' => 'SilvercartShoppingCartPosition'
  76. );
  77. /**
  78. * defines n:m relations
  79. *
  80. * @var array configure relations
  81. *
  82. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  83. * @copyright 2010 pixeltricks GmbH
  84. * @since 16.12.10
  85. */
  86. public static $many_many = array(
  87. 'SilvercartProducts' => 'SilvercartProduct'
  88. );
  89. /**
  90. * Indicates wether the registered modules should be loaded.
  91. *
  92. * @var boolean
  93. *
  94. * @author Sascha Koehler <skoehler@pixeltricks.de>
  95. * @since 27.04.2011
  96. */
  97. public static $loadModules = true;
  98. /**
  99. * Indicates wether the registered modules should be loaded.
  100. *
  101. * @var boolean
  102. *
  103. * @author Sascha Koehler <skoehler@pixeltricks.de>
  104. * @since 27.04.2011
  105. */
  106. public static $createForms = true;
  107. /**
  108. * Contains the ID of the payment method the customer has chosen.
  109. *
  110. * @var Int
  111. *
  112. * @author Sascha Koehler <skoehler@pixeltricks.de>
  113. * @copyright 2011 pixeltricks GmbH
  114. * @since 07.02.2011
  115. */
  116. protected $paymentMethodID;
  117. /**
  118. * Contains the ID of the shipping method the customer has chosen.
  119. *
  120. * @var Int
  121. *
  122. * @author Sascha Koehler <skoehler@pixeltricks.de>
  123. * @copyright 2011 pixeltricks GmbH
  124. * @since 07.02.2011
  125. */
  126. protected $shippingMethodID;
  127. /**
  128. * Contains the calculated charges and discounts for product values for
  129. * caching purposes.
  130. *
  131. * @var DataObject
  132. *
  133. * @author Sascha Koehler <skoehler@pixeltricks.de>
  134. * @since 15.12.2011
  135. */
  136. protected $chargesAndDiscountsForProducts = null;
  137. /**
  138. * Contains the calculated charges and discounts for the shopping cart
  139. * total for caching purposes.
  140. *
  141. * @var DataObject
  142. *
  143. * @author Sascha Koehler <skoehler@pixeltricks.de>
  144. * @since 15.12.2011
  145. */
  146. protected $chargesAndDiscountsForTotal = null;
  147. /**
  148. * Contains hashes for caching.
  149. *
  150. * @var array
  151. *
  152. * @author Sascha Koehler <skoehler@pixeltricks.de>
  153. * @since 23.01.2012
  154. */
  155. protected $cacheHashes = array();
  156. /**
  157. * default constructor
  158. *
  159. * @param array $record array of field values
  160. * @param bool $isSingleton true if this is a singleton() object
  161. *
  162. * @return void
  163. *
  164. * @author Sebastian Diel <sdiel@pixeltricks.de>
  165. * @since 15.02.2011
  166. */
  167. public function __construct($record = null, $isSingleton = false) {
  168. parent::__construct($record, $isSingleton);
  169. if (array_key_exists('url', $_REQUEST)) {
  170. if (stripos($_REQUEST['url'], '/dev/build') !== false) {
  171. return;
  172. }
  173. }
  174. // Initialize shopping cart position object, so that it can inject
  175. // its forms into the controller.
  176. if (!self::$loadModules) {
  177. SilvercartShoppingCartPosition::setCreateForms(false);
  178. }
  179. foreach ($this->SilvercartShoppingCartPositions() as $cartPosition) {
  180. if ($cartPosition->SilvercartProduct()->ID == 0) {
  181. $cartPosition->delete();
  182. }
  183. }
  184. $this->SilvercartShippingMethodID = 0;
  185. $this->SilvercartPaymentMethodID = 0;
  186. // Check if unit test are performed: The call to Member:currentUserID()
  187. // would fail
  188. //
  189. // Check if the installation is complete. If it's not complete we
  190. // can't call the method "Member::currentUser()", since it tries to
  191. // get the decorated fields from SilvercartCustomer that are not
  192. // yet created in the database
  193. if (array_key_exists('QUERY_STRING', $_SERVER) && (strpos($_SERVER['QUERY_STRING'], 'dev/tests') !== false || strpos($_SERVER['QUERY_STRING'], 'dev/build') !== false)) {
  194. return true;
  195. }
  196. if (!SapphireTest::is_running_test() &&
  197. SilvercartConfig::isInstallationCompleted() &&
  198. Member::currentUserID() &&
  199. self::$loadModules) {
  200. $this->callMethodOnRegisteredModules(
  201. 'performShoppingCartConditionsCheck',
  202. array(
  203. $this,
  204. Member::currentUser()
  205. )
  206. );
  207. $this->callMethodOnRegisteredModules(
  208. 'ShoppingCartInit'
  209. );
  210. }
  211. }
  212. /**
  213. * Indicates wether the cart has charges and discounts for the product
  214. * values.
  215. *
  216. * @return boolean
  217. *
  218. * @author Sascha Koehler <skoehler@pixeltricks.de>
  219. * @since 15.12.2011
  220. */
  221. public function HasChargesAndDiscountsForProducts() {
  222. if ($this->ChargesAndDiscountsForProducts()) {
  223. return true;
  224. }
  225. return false;
  226. }
  227. /**
  228. * Indicates wether the cart has charges and discounts for the total
  229. * shopping cart value.
  230. *
  231. * @return boolean
  232. *
  233. * @author Sascha Koehler <skoehler@pixeltricks.de>
  234. * @since 15.12.2011
  235. */
  236. public function HasChargesAndDiscountsForTotal() {
  237. if ($this->ChargesAndDiscountsForTotal()) {
  238. return true;
  239. }
  240. return false;
  241. }
  242. /**
  243. * Returns the charges and discounts for product values.
  244. *
  245. * @param string $priceType 'gross' or 'net'
  246. *
  247. * @return void
  248. *
  249. * @author Sascha Koehler <skoehler@pixeltricks.de>
  250. * @since 14.12.2011
  251. */
  252. public function ChargesAndDiscountsForProducts($priceType = false) {
  253. $cacheHash = md5($priceType);
  254. $cacheKey = 'ChargesAndDiscountsForProducts_'.$cacheHash;
  255. if (array_key_exists($cacheKey, $this->cacheHashes)) {
  256. return $this->cacheHashes[$cacheKey];
  257. }
  258. $paymentMethodObj = DataObject::get_by_id(
  259. 'SilvercartPaymentMethod', $this->SilvercartPaymentMethodID
  260. );
  261. if ($paymentMethodObj) {
  262. $handlingCostPayment = $paymentMethodObj->getChargesAndDiscountsForProducts($this, $priceType);
  263. if ($handlingCostPayment === false) {
  264. return false;
  265. } else {
  266. $taxes = $this->getTaxRatesWithoutFeesAndCharges('SilvercartVoucher');
  267. $silvercartTax = $this->getMostValuableTaxRate($taxes);
  268. $chargesAndDiscounts = new DataObject(
  269. array(
  270. 'Name' => $paymentMethodObj->sumModificationLabel,
  271. 'sumModificationImpact' => $paymentMethodObj->sumModificationImpact,
  272. 'PriceFormatted' => $handlingCostPayment->Nice(),
  273. 'Price' => $handlingCostPayment,
  274. 'SilvercartTax' => $silvercartTax
  275. )
  276. );
  277. $this->chargesAndDiscountsForProducts = $chargesAndDiscounts;
  278. $this->cacheHashes[$cacheKey] = $this->chargesAndDiscountsForProducts;
  279. return $chargesAndDiscounts;
  280. }
  281. }
  282. return false;
  283. }
  284. /**
  285. * Returns the charges and discounts for the shopping cart total.
  286. *
  287. * @param string $priceType 'gross' or 'net'
  288. *
  289. * @return void
  290. *
  291. * @author Sascha Koehler <skoehler@pixeltricks.de>
  292. * @since 15.12.2011
  293. */
  294. public function ChargesAndDiscountsForTotal($priceType = false) {
  295. $cacheHash = md5($priceType);
  296. $cacheKey = 'ChargesAndDiscountsForTotal_'.$cacheHash;
  297. if (array_key_exists($cacheKey, $this->cacheHashes)) {
  298. return $this->cacheHashes[$cacheKey];
  299. }
  300. $paymentMethodObj = DataObject::get_by_id(
  301. 'SilvercartPaymentMethod', $this->SilvercartPaymentMethodID
  302. );
  303. if ($paymentMethodObj) {
  304. $handlingCostPayment = $paymentMethodObj->getChargesAndDiscountsForTotal($this, $priceType);
  305. if ($handlingCostPayment === false) {
  306. return false;
  307. } else {
  308. $taxes = $this->getTaxRatesWithFees();
  309. $silvercartTax = $this->getMostValuableTaxRate($taxes);
  310. $handlingCostPaymentRounded = $handlingCostPayment;
  311. $handlingCostPaymentRounded->setAmount(
  312. round($handlingCostPayment->getAmount(), 2)
  313. );
  314. $chargesAndDiscounts = new DataObject(
  315. array(
  316. 'Name' => $paymentMethodObj->sumModificationLabel,
  317. 'sumModificationImpact' => $paymentMethodObj->sumModificationImpact,
  318. 'PriceFormatted' => $handlingCostPayment->Nice(),
  319. 'Price' => $handlingCostPayment,
  320. 'SilvercartTax' => $silvercartTax
  321. )
  322. );
  323. $this->chargesAndDiscountsForTotal = $chargesAndDiscounts;
  324. $this->cacheHashes[$cacheKey] = $this->chargesAndDiscountsForTotal;
  325. return $chargesAndDiscounts;
  326. }
  327. }
  328. return false;
  329. }
  330. /**
  331. * Set wether the registered modules should be loaded and handled.
  332. *
  333. * @param boolean $doLoad set wether to load the modules or not
  334. *
  335. * @return void
  336. *
  337. * @author Sascha Koehler <skoehler@pixeltricks.de>
  338. * @copyright 2011 pixeltricks GmbH
  339. * @since 27.04.2011
  340. */
  341. public static function setLoadShoppingCartModules($doLoad) {
  342. self::$loadModules = $doLoad;
  343. }
  344. /**
  345. * Set wether the shopping cart forms should be drawn.
  346. *
  347. * @param boolean $doCreate set wether to create the forms or not
  348. *
  349. * @return void
  350. *
  351. * @author Sascha Koehler <skoehler@pixeltricks.de>
  352. * @copyright 2011 pixeltricks GmbH
  353. * @since 27.04.2011
  354. */
  355. public static function setCreateShoppingCartForms($doCreate) {
  356. self::$createForms = $doCreate;
  357. }
  358. /**
  359. * adds a product to the cart
  360. *
  361. * @param array $formData the sended form data
  362. *
  363. * @return bool
  364. *
  365. * @author Sascha Koehler <skoehler@pixeltricks.de>
  366. * @copyright 2010 pixeltricks GmbH
  367. * @since 21.12.2010
  368. */
  369. public static function addProduct($formData) {
  370. $error = true;
  371. $member = Member::currentUser();
  372. if (!$member) {
  373. $member = SilvercartCustomer::createAnonymousCustomer();
  374. }
  375. $overwriteAddProduct = SilvercartPlugin::call($member->getCart(), 'overwriteAddProduct', array($formData), false, 'boolean');
  376. if ($overwriteAddProduct) {
  377. $error = false;
  378. } else {
  379. if ($formData['productID'] && $formData['productQuantity']) {
  380. $cart = $member->getCart();
  381. if ($cart) {
  382. $product = DataObject::get_by_id('SilvercartProduct', $formData['productID'], 'Created');
  383. if ($product) {
  384. $quantity = (int) $formData['productQuantity'];
  385. if ($quantity > 0) {
  386. $product->addToCart($cart->ID, $quantity);
  387. $error = false;
  388. }
  389. }
  390. }
  391. }
  392. }
  393. return !$error;
  394. }
  395. /**
  396. * empties cart
  397. *
  398. * @return void
  399. *
  400. * @author Sascha Koehler <skoehler@pixeltricks.de>
  401. * @copyright 2010 pixeltricks GmbH
  402. * @since 22.11.2010
  403. */
  404. public function delete() {
  405. $positions = $this->SilvercartShoppingCartPositions();
  406. foreach ($positions as $position) {
  407. $position->delete();
  408. }
  409. }
  410. /**
  411. * returns quantity of all products in the cart
  412. *
  413. * @param int $productId if set only product quantity of this product is returned
  414. *
  415. * @return int
  416. *
  417. * @author Sascha Koehler <skoehler@pixeltricks.de>
  418. * @since 22.11.10
  419. */
  420. public function getQuantity($productId = null) {
  421. $positions = $this->SilvercartShoppingCartPositions();
  422. $quantity = 0;
  423. foreach ($positions as $position) {
  424. if ($productId === null ||
  425. $position->SilvercartProduct()->ID === $productId) {
  426. $quantity += $position->Quantity;
  427. }
  428. }
  429. return $quantity;
  430. }
  431. /**
  432. * Returns the price of the cart positions + fees, including taxes.
  433. *
  434. * @param array $excludeShoppingCartPositions Positions that shall not be counted;
  435. * can contain the ID or the className of the position
  436. * @param boolean $excludeCharges Indicates wether charges and discounts should be calculated
  437. *
  438. * @return string a price amount
  439. *
  440. * @author Sascha Koehler <skoehler@pixeltricks.de>
  441. * @copyright 2011 pixeltricks GmbH
  442. * @since 04.02.2011
  443. */
  444. public function getTaxableAmountGrossWithFees($excludeShoppingCartPositions = false, $excludeCharges = false) {
  445. $member = Member::currentUser();
  446. $shippingMethod = DataObject::get_by_id('SilvercartShippingMethod', $this->SilvercartShippingMethodID);
  447. $paymentMethod = DataObject::get_by_id('SilvercartPaymentMethod', $this->SilvercartPaymentMethodID);
  448. $amountTotal = $this->getTaxableAmountGrossWithoutFees(null, $excludeShoppingCartPositions, $excludeCharges)->getAmount();
  449. if ($shippingMethod) {
  450. $shippingFee = $shippingMethod->getShippingFee();
  451. if ($shippingFee !== false) {
  452. $shippingFeeAmount = $shippingFee->Price->getAmount();
  453. $amountTotal = $shippingFeeAmount + $amountTotal;
  454. }
  455. }
  456. if ($paymentMethod) {
  457. $paymentFee = $paymentMethod->SilvercartHandlingCost();
  458. if ($paymentFee !== false) {
  459. $paymentFeeAmount = $paymentFee->amount->getAmount();
  460. $amountTotal = $paymentFeeAmount + $amountTotal;
  461. }
  462. }
  463. $amountTotalObj = new Money;
  464. $amountTotalObj->setAmount($amountTotal);
  465. $amountTotalObj->setCurrency(SilvercartConfig::DefaultCurrency());
  466. return $amountTotalObj;
  467. }
  468. /**
  469. * Returns the net price of the cart positions + fees, including taxes.
  470. *
  471. * @param array $excludeShoppingCartPositions Positions that shall not be counted;
  472. * can contain the ID or the className of the position
  473. * @param boolean $excludeCharges Indicates wether charges and discounts should be calculated
  474. *
  475. * @return string a price amount
  476. *
  477. * @author Sascha Koehler <skoehler@pixeltricks.de>
  478. * @copyright 2012 pixeltricks GmbH
  479. * @since 20.01.2012
  480. */
  481. public function getTaxableAmountNetWithFees($excludeShoppingCartPositions = false, $excludeCharges = false) {
  482. $member = Member::currentUser();
  483. $shippingMethod = DataObject::get_by_id('SilvercartShippingMethod', $this->SilvercartShippingMethodID);
  484. $paymentMethod = DataObject::get_by_id('SilvercartPaymentMethod', $this->SilvercartPaymentMethodID);
  485. $amountTotal = round($this->getTaxableAmountNetWithoutFees(null, $excludeShoppingCartPositions, $excludeCharges)->getAmount(), 2);
  486. if ($shippingMethod) {
  487. $shippingFee = $shippingMethod->getShippingFee();
  488. if ($shippingFee !== false) {
  489. $shippingFeeAmount = $shippingFee->getPriceAmount();
  490. $amountTotal = $shippingFeeAmount + $amountTotal;
  491. }
  492. }
  493. if ($paymentMethod) {
  494. $paymentFee = $paymentMethod->SilvercartHandlingCost();
  495. if ($paymentFee !== false) {
  496. $paymentFeeAmount = $paymentFee->getPriceAmount();
  497. $amountTotal = $paymentFeeAmount + $amountTotal;
  498. }
  499. }
  500. if (round($amountTotal, 2) === -0.00) {
  501. $amountTotal *= -1;
  502. }
  503. $amountTotalObj = new Money;
  504. $amountTotalObj->setAmount($amountTotal);
  505. $amountTotalObj->setCurrency(SilvercartConfig::DefaultCurrency());
  506. return $amountTotalObj;
  507. }
  508. /**
  509. * Returns the price of the cart positions, including taxes.
  510. *
  511. * @param array $excludeModules An array of registered modules that shall not
  512. * be taken into account.
  513. * @param array $excludeShoppingCartPosition Positions that shall not be counted;
  514. * can contain the ID or the className of the position
  515. * @param boolean $excludeCharges Indicates wether charges and discounts should be calculated
  516. *
  517. * @return Money a price amount
  518. *
  519. * @author Sascha Koehler <skoehler@pixeltricks.de>
  520. * @copyright 2011 pixeltricks GmbH
  521. * @since 15.12.2011
  522. */
  523. public function getTaxableAmountGrossWithoutFees($excludeModules = array(), $excludeShoppingCartPosition = false, $excludeCharges = false) {
  524. $amount = $this->getTaxableAmountGrossWithoutFeesAndCharges($excludeModules, $excludeShoppingCartPosition)->getAmount();
  525. // Handling costs for payment and shipment
  526. if (!$excludeCharges &&
  527. $this->ChargesAndDiscountsForProducts()) {
  528. $amount += $this->ChargesAndDiscountsForProducts()->Price->getAmount();
  529. }
  530. $amountObj = new Money;
  531. $amountObj->setAmount($amount);
  532. $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
  533. return $amountObj;
  534. }
  535. /**
  536. * Returns the price of the cart positions.
  537. *
  538. * @param array $excludeModules An array of registered modules that shall not
  539. * be taken into account.
  540. * @param array $excludeShoppingCartPosition Positions that shall not be counted;
  541. * can contain the ID or the className of the position
  542. * @param boolean $excludeCharges Indicates wether charges and discounts should be calculated
  543. *
  544. * @return Money a price amount
  545. *
  546. * @author Sascha Koehler <skoehler@pixeltricks.de>
  547. * @copyright 2011 pixeltricks GmbH
  548. * @since 15.12.2011
  549. */
  550. public function getTaxableAmountNetWithoutFees($excludeModules = array(), $excludeShoppingCartPosition = false, $excludeCharges = false) {
  551. $amount = $this->getTaxableAmountNetWithoutFeesAndCharges($excludeModules, $excludeShoppingCartPosition)->getAmount();
  552. // Handling costs for payment and shipment
  553. if (!$excludeCharges &&
  554. $this->ChargesAndDiscountsForProducts()) {
  555. $amount += $this->ChargesAndDiscountsForProducts()->Price->getAmount();
  556. }
  557. if (round($amount, 2) === -0.00) {
  558. $amount *= -1;
  559. }
  560. $amountObj = new Money;
  561. $amountObj->setAmount($amount);
  562. $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
  563. return $amountObj;
  564. }
  565. /**
  566. * Returns the price of the cart positions without modules.
  567. *
  568. * The price type is automatically determined by the
  569. * SilvercartShoppinCartPosition.
  570. *
  571. * @return Money a price amount
  572. *
  573. * @author Sascha Koehler <skoehler@pixeltricks.de>
  574. * @copyright 2011 pixeltricks GmbH
  575. * @since 15.12.2011
  576. */
  577. public function getTaxableAmountGrossWithoutModules() {
  578. $amount = 0;
  579. // products
  580. foreach ($this->SilvercartShoppingCartPositions() as $position) {
  581. $amount += $position->getPrice(false, 'gross')->getAmount();
  582. }
  583. $amountObj = new Money;
  584. $amountObj->setAmount($amount);
  585. $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
  586. return $amountObj;
  587. }
  588. /**
  589. * Returns the price of the cart positions without modules.
  590. *
  591. * The price type is automatically determined by the
  592. * SilvercartShoppinCartPosition.
  593. *
  594. * @return Money a price amount
  595. *
  596. * @author Sascha Koehler <skoehler@pixeltricks.de>
  597. * @copyright 2011 pixeltricks GmbH
  598. * @since 15.12.2011
  599. */
  600. public function getTaxableAmountNetWithoutModules() {
  601. $amount = 0;
  602. // products
  603. foreach ($this->SilvercartShoppingCartPositions() as $position) {
  604. $amount += $position->getPrice(false, 'net')->getAmount();
  605. }
  606. if (round($amount, 2) === -0.00) {
  607. $amount *= -1;
  608. }
  609. $amountObj = new Money;
  610. $amountObj->setAmount($amount);
  611. $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
  612. return $amountObj;
  613. }
  614. /**
  615. * Returns the price of the cart positions, including taxes.
  616. *
  617. * @param array $excludeModules An array of registered modules that shall not
  618. * be taken into account.
  619. * @param array $excludeShoppingCartPosition Positions that shall not be counted;
  620. * can contain the ID or the className of the position
  621. *
  622. * @return Money a price amount
  623. *
  624. * @author Sascha Koehler <skoehler@pixeltricks.de>
  625. * @copyright 2011 pixeltricks GmbH
  626. * @since 15.12.2011
  627. */
  628. public function getTaxableAmountGrossWithoutFeesAndCharges($excludeModules = array(), $excludeShoppingCartPosition = false) {
  629. if (!is_array($excludeModules)) {
  630. $excludeModules = array($excludeModules);
  631. }
  632. if (!is_array($excludeShoppingCartPosition)) {
  633. $excludeShoppingCartPosition = array($excludeShoppingCartPosition);
  634. }
  635. $cacheHash = md5(
  636. implode(',', $excludeModules).
  637. implode(',', $excludeShoppingCartPosition)
  638. );
  639. $cacheKey = 'getTaxableAmountGrossWithoutFeesAndCharges_'.$cacheHash;
  640. if (array_key_exists($cacheKey, $this->cacheHashes)) {
  641. return $this->cacheHashes[$cacheKey];
  642. }
  643. $amountObj = $this->getTaxableAmountGrossWithoutModules();
  644. $amount = $amountObj->getAmount();
  645. $registeredModules = $this->callMethodOnRegisteredModules(
  646. 'ShoppingCartPositions',
  647. array(
  648. $this,
  649. Member::currentUser(),
  650. true,
  651. $excludeShoppingCartPosition,
  652. false
  653. ),
  654. $excludeModules,
  655. $excludeShoppingCartPosition
  656. );
  657. // Registered Modules
  658. if ($registeredModules) {
  659. foreach ($registeredModules as $moduleName => $modulePositions) {
  660. foreach ($modulePositions as $modulePosition) {
  661. $amount += (float) $modulePosition->PriceTotal;
  662. }
  663. }
  664. }
  665. $amountObj->setAmount($amount);
  666. $this->cacheHashes[$cacheKey] = $amountObj;
  667. return $amountObj;
  668. }
  669. /**
  670. * Returns the price of the cart positions.
  671. *
  672. * @param array $excludeModules An array of registered modules that shall not
  673. * be taken into account.
  674. * @param array $excludeShoppingCartPosition Positions that shall not be counted;
  675. * can contain the ID or the className of the position
  676. *
  677. * @return Money a price amount
  678. *
  679. * @author Sascha Koehler <skoehler@pixeltricks.de>
  680. * @copyright 2011 pixeltricks GmbH
  681. * @since 15.12.2011
  682. */
  683. public function getTaxableAmountNetWithoutFeesAndCharges($excludeModules = array(), $excludeShoppingCartPosition = false) {
  684. if (!is_array($excludeModules)) {
  685. $excludeModules = array($excludeModules);
  686. }
  687. if (!is_array($excludeShoppingCartPosition)) {
  688. $excludeShoppingCartPosition = array($excludeShoppingCartPosition);
  689. }
  690. $cacheHash = md5(
  691. implode(',', $excludeModules).
  692. implode(',', $excludeShoppingCartPosition)
  693. );
  694. $cacheKey = 'getTaxableAmountGrossWithoutFeesAndCharges_'.$cacheHash;
  695. if (array_key_exists($cacheKey, $this->cacheHashes)) {
  696. return $this->cacheHashes[$cacheKey];
  697. }
  698. $amountObj = $this->getTaxableAmountNetWithoutModules();
  699. $amount = $amountObj->getAmount();
  700. $registeredModules = $this->callMethodOnRegisteredModules(
  701. 'ShoppingCartPositions',
  702. array(
  703. $this,
  704. Member::currentUser(),
  705. true,
  706. $excludeShoppingCartPosition,
  707. false
  708. ),
  709. $excludeModules,
  710. $excludeShoppingCartPosition
  711. );
  712. // Registered Modules
  713. if ($registeredModules) {
  714. foreach ($registeredModules as $moduleName => $modulePositions) {
  715. foreach ($modulePositions as $modulePosition) {
  716. $amount += (float) $modulePosition->PriceNetTotal;
  717. }
  718. }
  719. }
  720. if (round($amount, 2) === -0.00) {
  721. $amount *= -1;
  722. }
  723. $amountObj->setAmount($amount);
  724. $this->cacheHashes[$cacheKey] = $amountObj;
  725. return $amountObj;
  726. }
  727. /**
  728. * Returns the total amount of all taxes.
  729. *
  730. * @param boolean $excludeCharges Indicates wether to exlude charges and discounts
  731. *
  732. * @return Money a price amount
  733. *
  734. * @author Sascha Koehler <skoehler@pixeltricks.de>
  735. * @copyright 2011 pixeltricks GmbH
  736. * @since 07.04.2011
  737. */
  738. public function getTaxTotal($excludeCharges = false) {
  739. $taxRates = $this->getTaxRatesWithFees();
  740. if (!$excludeCharges &&
  741. $this->HasChargesAndDiscountsForTotal()) {
  742. foreach ($this->ChargesAndDiscountsForTotal() as $charge) {
  743. if ($charge->SilvercartTax === false) {
  744. continue;
  745. }
  746. $taxRate = $taxRates->find('Rate', $charge->SilvercartTax->Rate);
  747. if ($taxRate) {
  748. $amount = $charge->Price->getAmount();
  749. if (SilvercartConfig::PriceType() == 'gross') {
  750. $rateAmount = $amount - ($amount / (100 + $charge->SilvercartTax->Rate) * 100);
  751. } else {
  752. $rateAmount = ($amount / 100 * (100 + $charge->SilvercartTax->Rate)) - $amount;
  753. }
  754. $taxRate->AmountRaw += $rateAmount;
  755. if (round($taxRate->AmountRaw, 2) === -0.00) {
  756. $taxRate->AmountRaw *= -1;
  757. }
  758. $taxRate->Amount->setAmount($taxRate->AmountRaw);
  759. }
  760. }
  761. }
  762. return $taxRates;
  763. }
  764. /**
  765. * Returns the non taxable amount of positions in the shopping cart.
  766. * Those can originate from registered modules only.
  767. *
  768. * @param array $excludeModules An array of registered modules that shall not
  769. * be taken into account.
  770. * @param array $excludeShoppingCartPosition Positions that shall not be counted
  771. *
  772. * @return Money
  773. *
  774. * @author Sascha Koehler <skoehler@pixeltricks.de>
  775. * @copyright 2011 pixeltricks GmbH
  776. * @since 04.02.2011
  777. */
  778. public function getNonTaxableAmount($excludeModules = array(), $excludeShoppingCartPosition = false) {
  779. $amount = 0;
  780. $registeredModules = $this->callMethodOnRegisteredModules(
  781. 'ShoppingCartPositions',
  782. array(
  783. $this,
  784. Member::currentUser(),
  785. false,
  786. $excludeShoppingCartPosition
  787. ),
  788. $excludeModules,
  789. $excludeShoppingCartPosition
  790. );
  791. // Registered Modules
  792. foreach ($registeredModules as $moduleName => $modulePositions) {
  793. foreach ($modulePositions as $modulePosition) {
  794. $amount += (float) $modulePosition->PriceTotal;
  795. }
  796. }
  797. $amountObj = new Money;
  798. $amountObj->setAmount($amount);
  799. $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
  800. return $amountObj;
  801. }
  802. /**
  803. * Returns the handling costs for the chosen payment method.
  804. *
  805. * @return Money
  806. *
  807. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  808. * @copyright 2011 pixeltricks GmbH
  809. * @since 26.1.2011
  810. */
  811. public function HandlingCostPayment() {
  812. $handlingCostPayment = 0;
  813. $paymentMethodObj = DataObject::get_by_id(
  814. 'SilvercartPaymentMethod', $this->SilvercartPaymentMethodID
  815. );
  816. if ($paymentMethodObj) {
  817. $handlingCostPaymentObj = $paymentMethodObj->getHandlingCost();
  818. } else {
  819. $handlingCostPaymentObj = new Money();
  820. $handlingCostPaymentObj->setAmount(0);
  821. $handlingCostPaymentObj->setCurrency(SilvercartConfig::DefaultCurrency());
  822. }
  823. if (SilvercartConfig::PriceType() == 'net') {
  824. $taxRate = $this->getMostValuableTaxRate($this->getTaxRatesWithoutFeesAndCharges('SilvercartVoucher'));
  825. $handlingCostPayment = round(($handlingCostPaymentObj->getAmount() / (100 + $taxRate->Rate) * 100), 2);
  826. $handlingCostPaymentObj->setAmount($handlingCostPayment);
  827. }
  828. return $handlingCostPaymentObj;
  829. }
  830. /**
  831. * Returns the handling costs for the chosen shipping method.
  832. *
  833. * @return Money
  834. *
  835. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  836. * @copyright 2011 pixeltricks GmbH
  837. * @since 26.1.2011
  838. */
  839. public function HandlingCostShipment() {
  840. $handlingCostShipment = 0;
  841. $selectedShippingMethod = DataObject::get_by_id(
  842. 'SilvercartShippingMethod', $this->SilvercartShippingMethodID
  843. );
  844. if ($selectedShippingMethod) {
  845. $handlingCostShipmentObj = $selectedShippingMethod->getShippingFee()->Price;
  846. } else {
  847. $handlingCostShipmentObj = new Money();
  848. $handlingCostShipmentObj->setAmount($handlingCostShipment);
  849. $handlingCostShipmentObj->setCurrency(SilvercartConfig::DefaultCurrency());
  850. }
  851. if (SilvercartConfig::PriceType() == 'net') {
  852. $taxRate = $this->getMostValuableTaxRate($this->getTaxRatesWithoutFeesAndCharges('SilvercartVoucher'));
  853. $handlingCostShipment = round(($handlingCostShipmentObj->getAmount() / (100 + $taxRate->Rate) * 100), 2);
  854. $handlingCostShipmentObj->setAmount($handlingCostShipment);
  855. }
  856. return $handlingCostShipmentObj;
  857. }
  858. /**
  859. * Returns the shipping method title.
  860. *
  861. * @return string
  862. *
  863. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  864. * @copyright 2011 pixeltricks GmbH
  865. * @since 26.1.2011
  866. */
  867. public function CarrierAndShippingMethodTitle() {
  868. $title = '';
  869. $selectedShippingMethod = DataObject::get_by_id(
  870. 'SilvercartShippingMethod', $this->SilvercartShippingMethodID
  871. );
  872. if ($selectedShippingMethod) {
  873. $title = $selectedShippingMethod->SilvercartCarrier()->Title . "-" . $selectedShippingMethod->Title;
  874. }
  875. return $title;
  876. }
  877. /**
  878. * Returns the payment method object.
  879. *
  880. * @return PaymentMethod
  881. *
  882. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  883. * @copyright 2011 pixeltricks GmbH
  884. * @since 26.1.2011
  885. */
  886. public function getPayment() {
  887. $paymentMethodObj = DataObject::get_by_id(
  888. 'SilvercartPaymentMethod', $this->SilvercartPaymentMethodID
  889. );
  890. return $paymentMethodObj;
  891. }
  892. /**
  893. * Returns the minimum order value.
  894. *
  895. * @return mixed Money
  896. *
  897. * @author Sascha Koehler <skoehler@pixeltricks.de>
  898. * @copyright 2011 pixeltricks GmbH
  899. * @since 09.06.2011
  900. */
  901. public function MinimumOrderValue() {
  902. $minimumOrderValue = new Money();
  903. if (SilvercartConfig::UseMinimumOrderValue() &&
  904. SilvercartConfig::MinimumOrderValue()) {
  905. $minimumOrderValue->setAmount(SilvercartConfig::MinimumOrderValue()->getAmount());
  906. $minimumOrderValue->setCurrency(SilvercartConfig::MinimumOrderValue()->getCurrency());
  907. }
  908. return $minimumOrderValue->Nice();
  909. }
  910. /**
  911. * Indicates wether the minimum order value is reached.
  912. *
  913. * @return bool
  914. *
  915. * @author Sascha Koehler <skoehler@pixeltricks.de>
  916. * @copyright 2011 pixeltricks GmbH
  917. * @since 09.06.2011
  918. */
  919. public function IsMinimumOrderValueReached() {
  920. if (SilvercartConfig::UseMinimumOrderValue() &&
  921. SilvercartConfig::MinimumOrderValue() &&
  922. SilvercartConfig::MinimumOrderValue()->getAmount() > $this->getAmountTotalWithoutFees()->getAmount()) {
  923. return false;
  924. }
  925. return true;
  926. }
  927. /**
  928. * In case stock management is enabled: Find out if all positions quantities
  929. * are still in stock
  930. *
  931. * @return bool Can this cart be checkt out?
  932. *
  933. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  934. * @since 18.7.2011
  935. */
  936. public function isAvailableInStock() {
  937. $positions = $this->SilvercartShoppingCartPositions();
  938. if ($positions) {
  939. $isCheckoutable = true;
  940. foreach ($positions as $position) {
  941. if ($position->Quantity > $position->SilvercartProduct()->StockQuantity) {
  942. $isCheckoutable = false;
  943. break;
  944. }
  945. }
  946. return $isCheckoutable;
  947. } else {
  948. return false;
  949. }
  950. }
  951. /**
  952. * Returns the end sum of the cart (taxable positions + nontaxable
  953. * positions + fees).
  954. *
  955. * @param array $excludeModules An array of registered modules that shall not
  956. * be taken into account.
  957. * @param array $excludeShoppingCartPositions Positions that shall not be counted
  958. * @param boolean $excludeCharges Indicates wether to exlude charges and discounts
  959. *
  960. * @return string a price amount
  961. *
  962. * @author Sascha Koehler <skoehler@pixeltricks.de>
  963. * @copyright 2011 pixeltricks GmbH
  964. * @since 04.02.2011
  965. */
  966. public function getAmountTotal($excludeModules = array(), $excludeShoppingCartPositions = false, $excludeCharges = false) {
  967. if (SilvercartConfig::PriceType() == 'gross') {
  968. return $this->getAmountTotalGross($excludeModules, $excludeShoppingCartPositions, $excludeCharges);
  969. } else {
  970. return $this->getAmountTotalNet($excludeModules, $excludeShoppingCartPositions, $excludeCharges);
  971. }
  972. }
  973. /**
  974. * Returns the end sum of the cart (taxable positions + nontaxable
  975. * positions + fees).
  976. *
  977. * @param array $excludeModules An array of registered modules that shall not
  978. * be taken into account.
  979. * @param array $excludeShoppingCartPositions Positions that shall not be counted
  980. * @param boolean $excludeCharges Indicates wether to exlude charges and discounts
  981. *
  982. * @return string a price amount
  983. *
  984. * @author Sascha Koehler <skoehler@pixeltricks.de>
  985. * @copyright 2011 pixeltricks GmbH
  986. * @since 04.02.2011
  987. */
  988. public function getAmountTotalGross($excludeModules = array(), $excludeShoppingCartPositions = false, $excludeCharges = false) {
  989. $amount = $this->getTaxableAmountGrossWithFees($excludeShoppingCartPositions)->getAmount();
  990. $amount += $this->getNonTaxableAmount($excludeModules, $excludeShoppingCartPositions)->getAmount();
  991. // Handling costs for payment and shipment
  992. if (!$excludeCharges &&
  993. $this->HasChargesAndDiscountsForTotal()) {
  994. $amount += $this->ChargesAndDiscountsForTotal('gross')->Price->getAmount();
  995. }
  996. $amountObj = new Money;
  997. $amountObj->setAmount($amount);
  998. $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
  999. return $amountObj;
  1000. }
  1001. /**
  1002. * Returns the end sum of the cart (taxable positions + nontaxable
  1003. * positions + fees) excluding vat.
  1004. *
  1005. * @param array $excludeModules An array of registered modules that shall not
  1006. * be taken into account.
  1007. * @param array $excludeShoppingCartPositions Positions that shall not be counted
  1008. * @param boolean $excludeCharges Indicates wether to exlude charges and discounts
  1009. *
  1010. * @return string a price amount
  1011. *
  1012. * @author Sascha Koehler <skoehler@pixeltricks.de>
  1013. * @copyright 2011 pixeltricks GmbH
  1014. * @since 04.02.2011
  1015. */
  1016. public function getAmountTotalNet($excludeModules = array(), $excludeShoppingCartPositions = false, $excludeCharges = false) {
  1017. $amountObj = $this->getAmountTotalNetWithoutVat($excludeModules, $excludeShoppingCartPositions, $excludeCharges);
  1018. $amount = $amountObj->getAmount();
  1019. foreach ($this->getTaxTotal($excludeCharges) as $tax) {
  1020. $amount += $tax->Amount->getAmount();
  1021. }
  1022. $amountObj->setAmount($amount);
  1023. return $amountObj;
  1024. }
  1025. /**
  1026. * Returns the end sum of the cart (taxable positions + nontaxable
  1027. * positions + fees) excluding vat.
  1028. *
  1029. * @param array $excludeModules An array of registered modules that shall not
  1030. * be taken into account.
  1031. * @param array $excludeShoppingCartPositions Positions that shall not be counted
  1032. * @param boolean $excludeCharges Indicates wether to exlude charges and discounts
  1033. *
  1034. * @return string a price amount
  1035. *
  1036. * @author Sascha Koehler <skoehler@pixeltricks.de>
  1037. * @copyright 2011 pixeltricks GmbH
  1038. * @since 04.02.2011
  1039. */
  1040. public function getAmountTotalNetWithoutVat($excludeModules = array(), $excludeShoppingCartPositions = false, $excludeCharges = false) {
  1041. $amount = $this->getTaxableAmountNetWithFees($excludeShoppingCartPositions)->getAmount();
  1042. $amount += $this->getNonTaxableAmount($excludeModules, $excludeShoppingCartPositions)->getAmount();
  1043. // Handling costs for payment and shipment
  1044. if (!$excludeCharges &&
  1045. $this->HasChargesAndDiscountsForTotal()) {
  1046. $amount += $this->ChargesAndDiscountsForTotal('net')->Price->getAmount();
  1047. }
  1048. if (round($amount, 2) === -0.00) {
  1049. $amount *= -1;
  1050. }
  1051. $amountObj = new Money;
  1052. $amountObj->setAmount($amount);
  1053. $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
  1054. return $amountObj;
  1055. }
  1056. /**
  1057. * Returns the end sum of the cart (taxable positions + nontaxable
  1058. * positions + fees) without any taxes.
  1059. *
  1060. * @param array $excludeModules An array of registered modules that shall not
  1061. * be taken into account.
  1062. * @param array $excludeShoppingCartPositions Positions that shall not be counted
  1063. * @param boolean $excludeCharges Indicates wether to exlude charges and discounts
  1064. *
  1065. * @return string a price amount
  1066. *
  1067. * @author Sascha Koehler <skoehler@pixeltricks.de>
  1068. * @copyright 2011 pixeltricks GmbH
  1069. * @since 04.02.2011
  1070. */
  1071. public function getAmountTotalWithoutTaxes($excludeModules = array(), $excludeShoppingCartPositions = false, $excludeCharges = false) {
  1072. $amount = $this->getTaxableAmountGrossWithFees($excludeShoppingCartPositions)->getAmount();
  1073. $amount += $this->getNonTaxableAmount($excludeModules, $excludeShoppingCartPositions)->getAmount();
  1074. // Handling costs for payment and shipment
  1075. if (!$excludeCharges &&
  1076. $this->ChargesAndDiscountsForTotal()) {
  1077. $amount += $this->ChargesAndDiscountsForTotal()->Price->getAmount();
  1078. }
  1079. if (round($amount, 2) === -0.00) {
  1080. $amount *= -1;
  1081. }
  1082. $amountObj = new Money;
  1083. $amountObj->setAmount($amount);
  1084. $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
  1085. return $amountObj;
  1086. }
  1087. /**
  1088. * Returns the end sum of the cart without fees (taxable positions +
  1089. * nontaxable positions).
  1090. *
  1091. * @param array $excludeModules An array of registered modules that shall not
  1092. * be taken into account.
  1093. * @param array $excludeShoppingCartPositions Positions that shall not be counted
  1094. * @param boolean $excludeCharges Indicates wether to exlude charges and discounts
  1095. *
  1096. * @return string a price amount
  1097. *
  1098. * @author Sascha Koehler <skoehler@pixeltricks.de>
  1099. * @copyright 2011 pixeltricks GmbH
  1100. * @since 12.05.2011
  1101. */
  1102. public function getAmountTotalGrossWithoutFees($excludeModules = array(), $excludeShoppingCartPositions = false, $excludeCharges = false) {
  1103. $amount = $this->getTaxableAmountGrossWithoutFees($excludeModules, $excludeShoppingCartPositions, $excludeCharges)->getAmount();
  1104. $amount += $this->getNonTaxableAmount($excludeModules, $excludeShoppingCartPositions)->getAmount();
  1105. if (round($amount, 2) === -0.00) {
  1106. $amount *= -1;
  1107. }
  1108. $amountObj = new Money;
  1109. $amountObj->setAmount($amount);
  1110. $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
  1111. return $amountObj;
  1112. }
  1113. /**
  1114. * Returns the end sum of the cart without fees (taxable positions +
  1115. * nontaxable positions).
  1116. *
  1117. * @param array $excludeModules An array of registered modules that shall not
  1118. * be taken into account.
  1119. * @param array $excludeShoppingCartPositions Positions that shall not be counted
  1120. * @param boolean $excludeCharges Indicates wether to exlude charges and discounts
  1121. *
  1122. * @return string a price amount
  1123. *
  1124. * @author Sascha Koehler <skoehler@pixeltricks.de>
  1125. * @copyright 2011 pixeltricks GmbH
  1126. * @since 12.05.2011
  1127. */
  1128. public function getAmountTotalNetWithoutFees($excludeModules = array(), $excludeShoppingCartPositions = false, $excludeCharges = false) {
  1129. $amount = $this->getTaxableAmountNetWithoutFees($excludeModules, $excludeShoppingCartPositions, $excludeCharges)->getAmount();
  1130. $amount += $this->getNonTaxableAmount($excludeModules, $excludeShoppingCartPositions)->getAmount();
  1131. if (round($amount, 2) === -0.00) {
  1132. $amount *= -1;
  1133. }
  1134. $amountObj = new Money;
  1135. $amountObj->setAmount($amount);
  1136. $amountObj->setCurrency(SilvercartConfig::DefaultCurrency());
  1137. return $amountObj;
  1138. }
  1139. /**
  1140. * Returns the tax rates for shipping and payment fees.
  1141. *
  1142. * @return DataObjectSet
  1143. *
  1144. * @author Sascha Koehler <skoehler@pixeltricks.de>
  1145. * @since 23.01.2012
  1146. */
  1147. public function getTaxRatesForFees() {
  1148. $taxes = new DataObjectSet;
  1149. $taxAmount = 0;
  1150. $shippingMethod = DataObject::get_by_id('SilvercartShippingMethod', $this->SilvercartShippingMethodID);
  1151. $paymentMethod = DataObject::get_by_id('SilvercartPaymentMethod', $this->SilvercartPaymentMethodID);
  1152. if ($shippingMethod) {
  1153. $shippingFee = $shippingMethod->getShippingFee();
  1154. if ($shippingFee) {
  1155. $taxAmount += $shippingFee->getTaxAmount();
  1156. }
  1157. }
  1158. if ($paymentMethod) {
  1159. $paymentFee = $paymentMethod->SilvercartHandlingCost();
  1160. if ($paymentFee) {
  1161. $taxAmount += $paymentFee->getTaxAmount();
  1162. }
  1163. }
  1164. $taxRate = $this->getMostValuableTaxRate($this->getTaxRatesWithoutFeesAndCharges())->Rate;
  1165. if (!$taxes->find('Rate', $taxRate)) {
  1166. $taxes->push(
  1167. new DataObject(
  1168. array(
  1169. 'Rate' => $taxRate,
  1170. 'AmountRaw' => $taxAmount,
  1171. )
  1172. )
  1173. );
  1174. }
  1175. foreach ($taxes as $tax) {
  1176. $taxObj = new Money;
  1177. $taxObj->setAmount($tax->AmountRaw);
  1178. $taxObj->setCurrency(SilvercartConfig::DefaultCurrency());
  1179. $tax->Amount = $taxObj;
  1180. }
  1181. return $taxes;
  1182. }
  1183. /**
  1184. * Returns tax amounts included in the shoppingcart separated by tax rates
  1185. * with fee taxes.
  1186. *
  1187. * @return DataObjectSet
  1188. *
  1189. * @author Sascha Koehler <skoehler@pixeltricks.de>
  1190. * @copyright 2011 pixeltricks GmbH
  1191. * @since 04.02.2011
  1192. */
  1193. public function getTaxRatesWithFees() {
  1194. $taxes = $this->getTaxRatesWithoutFees();
  1195. $shippingMethod = DataObject::get_by_id('SilvercartShippingMethod', $this->SilvercartShippingMethodID);
  1196. $paymentMethod = DataObject::get_by_id('SilvercartPaymentMethod', $this->SilvercartPaymentMethodID);
  1197. if ($shippingMethod) {
  1198. $shippingFee = $shippingMethod->getShippingFee();
  1199. if ($shippingFee) {
  1200. if ($shippingFee->SilvercartTax()) {
  1201. $taxRate = $shippingFee->getTaxRate();
  1202. if ( $taxRate &&
  1203. !$taxes->find('Rate', $taxRate)) {
  1204. $taxes->push(
  1205. new DataObject(
  1206. array(
  1207. 'Rate' => $taxRate,
  1208. 'AmountRaw' => 0.0,
  1209. )
  1210. )
  1211. );
  1212. }
  1213. $taxSection = $taxes->find('Rate', $taxRate);
  1214. $taxSection->AmountRaw += $shippingFee->getTaxAmount();
  1215. }
  1216. }
  1217. }
  1218. if ($paymentMethod) {
  1219. $paymentFee = $paymentMethod->SilvercartHandlingCost();
  1220. if ($paymentFee) {
  1221. if ($paymentFee->SilvercartTax()) {
  1222. $taxRate = $paymentFee->SilvercartTax()->getTaxRate();
  1223. if ( $taxRate &&
  1224. !$taxes->find('Rate', $taxRate)) {
  1225. $taxes->push(
  1226. new DataObject(
  1227. array(
  1228. 'Rate' => $taxRate,
  1229. 'AmountRaw' => 0.0,
  1230. )
  1231. )
  1232. );
  1233. }
  1234. $taxSection = $taxes->find('Rate', $taxRate);
  1235. $taxSection->AmountRaw += $paymentFee->getTaxAmount();
  1236. }
  1237. }
  1238. }
  1239. foreach ($taxes as $tax) {
  1240. $taxObj = new Money;
  1241. $taxObj->setAmount(round($tax->

Large files files are truncated, but you can click here to view the full file