/src/classes/XLite/Module/CDev/SalesTax/Model/Tax.php

https://github.com/litecommerce/core · PHP · 125 lines · 35 code · 12 blank · 78 comment · 2 complexity · 09a907d976de92f25aa1af5db7058668 MD5 · raw file

  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3. /**
  4. * LiteCommerce
  5. *
  6. * NOTICE OF LICENSE
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0)
  9. * that is bundled with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://opensource.org/licenses/osl-3.0.php
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to licensing@litecommerce.com so we can send you a copy immediately.
  15. *
  16. * PHP version 5.3.0
  17. *
  18. * @category LiteCommerce
  19. * @author Creative Development LLC <info@cdev.ru>
  20. * @copyright Copyright (c) 2011-2012 Creative Development LLC <info@cdev.ru>. All rights reserved
  21. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @link http://www.litecommerce.com/
  23. */
  24. namespace XLite\Module\CDev\SalesTax\Model;
  25. /**
  26. * Tax
  27. *
  28. *
  29. * @Entity
  30. * @Table (name="sales_taxes")
  31. */
  32. class Tax extends \XLite\Model\Base\I18n
  33. {
  34. /**
  35. * Product unique ID
  36. *
  37. * @var integer
  38. *
  39. * @Id
  40. * @GeneratedValue (strategy="AUTO")
  41. * @Column (type="uinteger")
  42. */
  43. protected $id;
  44. /**
  45. * Eenabled
  46. *
  47. * @var boolean
  48. *
  49. * @Column (type="boolean")
  50. */
  51. protected $enabled = false;
  52. /**
  53. * Tax rates (relation)
  54. *
  55. * @var \Doctrine\Common\Collections\ArrayCollection
  56. *
  57. * @OneToMany (targetEntity="XLite\Module\CDev\SalesTax\Model\Tax\Rate", mappedBy="tax", cascade={"all"})
  58. * @OrderBy ({"position" = "ASC"})
  59. */
  60. protected $rates;
  61. /**
  62. * Constructor
  63. *
  64. * @param array $data Entity properties OPTIONAL
  65. *
  66. * @return void
  67. */
  68. public function __construct(array $data = array())
  69. {
  70. $this->rates = new \Doctrine\Common\Collections\ArrayCollection();
  71. parent::__construct($data);
  72. }
  73. /**
  74. * Get filtered rates by zones and membership
  75. *
  76. * @param array $zones Zone id list
  77. * @param \XLite\Model\Membership $membership Membership OPTIONAL
  78. * @param \Doctrine\Common\Collections\ArrayCollection $productClasses Product classes OPTIONAL
  79. *
  80. * @return array
  81. */
  82. public function getFilteredRates(
  83. array $zones,
  84. \XLite\Model\Membership $membership = null,
  85. \Doctrine\Common\Collections\ArrayCollection $productClasses = null
  86. ) {
  87. $rates = array();
  88. foreach ($this->getRates() as $rate) {
  89. if ($rate->isApplied($zones, $membership, $productClasses) && !isset($rates[$rate->getPosition()])) {
  90. $rates[$rate->getPosition()] = $rate;
  91. }
  92. }
  93. ksort($rates);
  94. return $rates;
  95. }
  96. /**
  97. * Get filtered rate by zones and membership
  98. *
  99. * @param array $zones Zone id list
  100. * @param \XLite\Model\Membership $membership Membership OPTIONAL
  101. * @param \Doctrine\Common\Collections\ArrayCollection $productClasses Product classes OPTIONAL
  102. *
  103. * @return \XLite\Module\CDev\SalesTax\Model\Tax\Rate
  104. */
  105. public function getFilteredRate(
  106. array $zones,
  107. \XLite\Model\Membership $membership = null,
  108. \Doctrine\Common\Collections\ArrayCollection $productClasses = null
  109. ) {
  110. $rates = $this->getFilteredRates($zones, $membership, $productClasses);
  111. return array_shift($rates);
  112. }
  113. }