Voucher.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ SDBAJe5DoJpQG+XJULdLXxMPhsMMpI7tpSkecE7aB24=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.2
  11. */
  12. /**
  13. * vouchers table row object model
  14. */
  15. namespace Ppb\Db\Table\Row;
  16. use Ppb\Service;
  17. class Voucher extends AbstractRow
  18. {
  19. /**
  20. *
  21. * check if the voucher is valid
  22. *
  23. * @param int $listingId if provided, check if the listing id matches
  24. *
  25. * @return bool
  26. */
  27. public function isValid($listingId = null)
  28. {
  29. $usesRemaining = $this->getData('uses_remaining');
  30. $expirationDate = $this->getData('expiration_date');
  31. if ($listingId !== null) {
  32. $assignedListings = array_filter(array_map('intval', explode(',', $this->getData('assigned_listings'))));
  33. if (count($assignedListings) && !in_array($listingId, $assignedListings)) {
  34. return false;
  35. }
  36. }
  37. if (($usesRemaining === null || $usesRemaining > 0) &&
  38. ($expirationDate === null || strtotime($expirationDate) > time())
  39. ) {
  40. return true;
  41. }
  42. return false;
  43. }
  44. /**
  45. *
  46. * apply the voucher to a certain amount and return the updated amount
  47. * if listing id is provided, check and apply by listing id
  48. *
  49. * @param float $amount
  50. * @param string $currency
  51. * @param int $listingId
  52. *
  53. * @return float
  54. */
  55. public function apply($amount, $currency = null, $listingId = null)
  56. {
  57. if ($this->isValid($listingId)) {
  58. $reductionAmount = $this->getData('reduction_amount');
  59. switch ($this->getData('reduction_type')) {
  60. case 'flat':
  61. $settings = $this->getSettings();
  62. if ($currency !== null && $currency != $settings['currency']) {
  63. $currenciesService = new Service\Table\Currencies();
  64. $reductionAmount = $currenciesService->convertAmount($reductionAmount, $settings['currency'],
  65. $currency);
  66. }
  67. $amount -= $reductionAmount;
  68. if ($amount < 0) {
  69. $amount = 0;
  70. }
  71. break;
  72. case 'percent':
  73. $amount -= $amount * $reductionAmount / 100;
  74. break;
  75. }
  76. }
  77. return $amount;
  78. }
  79. /**
  80. *
  81. * update uses remaining column
  82. *
  83. * @return $this
  84. */
  85. public function updateUses()
  86. {
  87. $usesRemaining = $this->getData('uses_remaining');
  88. if ($usesRemaining > 0) {
  89. $this->save(array(
  90. 'uses_remaining' => ($usesRemaining - 1),
  91. ));
  92. }
  93. return $this;
  94. }
  95. /**
  96. *
  97. * output a description for the voucher
  98. *
  99. * @return string
  100. */
  101. public function description()
  102. {
  103. $translate = $this->getTranslate();
  104. return $translate->_('Voucher') . ' - ' . $this->getData('code');
  105. }
  106. }