Vouchers.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ 6B2zh4FpFs5fx92jXzfQMaFTXZQU34mtXyFH1P2hYic=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2017 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.9 [rev.7.9.01]
  11. */
  12. /**
  13. * vouchers table service class
  14. * - percentage vouchers apply on each fee row specifically
  15. * - flat amount vouchers apply on the total (not on each row)
  16. */
  17. namespace Ppb\Service;
  18. use Cube\Db\Expr,
  19. Ppb\Db\Table;
  20. class Vouchers extends AbstractService
  21. {
  22. /**
  23. *
  24. * class constructor
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. $this->setTable(
  30. new Table\Vouchers());
  31. }
  32. /**
  33. *
  34. * create or update a newsletter
  35. *
  36. * @param array $data
  37. *
  38. * @return $this
  39. */
  40. public function save($data)
  41. {
  42. $row = null;
  43. $data = $this->_prepareSaveData($data);
  44. if (array_key_exists('id', $data)) {
  45. $select = $this->_table->select()
  46. ->where("id = ?", $data['id']);
  47. unset($data['id']);
  48. $row = $this->_table->fetchRow($select);
  49. }
  50. if (count($row) > 0) {
  51. $data['updated_at'] = new Expr('now()');
  52. $this->_table->update($data, "id='{$row['id']}'");
  53. }
  54. else {
  55. $data['created_at'] = new Expr('now()');
  56. $this->_table->insert($data);
  57. }
  58. return $this;
  59. }
  60. /**
  61. *
  62. * find voucher by code and owner
  63. * (if null get an admin voucher)
  64. *
  65. * @param string $voucherCode
  66. * @param int $userId
  67. *
  68. * @return \Ppb\Db\Table\Row\Voucher|null
  69. */
  70. public function findBy($voucherCode, $userId = null)
  71. {
  72. $select = $this->_table->select()
  73. ->where('code = ?', strval($voucherCode));
  74. if ($userId) {
  75. $select->where('user_id = ?', $userId);
  76. }
  77. else {
  78. $select->where('user_id is null');
  79. }
  80. return $this->_table->fetchRow($select);
  81. }
  82. /**
  83. *
  84. * prepare listing data for when saving to the table
  85. * if listing is scheduled, 'closed' = 1
  86. *
  87. * important: the daylight saving changes will automatically be calculated when setting the end time!
  88. *
  89. * @param array $data
  90. *
  91. * @return array
  92. */
  93. protected function _prepareSaveData($data = array())
  94. {
  95. if ($data['uses_remaining'] == '') {
  96. $data['uses_remaining'] = null;
  97. }
  98. if ($data['expiration_date'] == '') {
  99. $data['expiration_date'] = null;
  100. }
  101. if (empty($data['assigned_listings'])) {
  102. $data['assigned_listings'] = '';
  103. }
  104. if (empty($data['id'])) {
  105. unset($data['id']);
  106. }
  107. return parent::_prepareSaveData($data);
  108. }
  109. /**
  110. *
  111. * delete a voucher row from the table
  112. *
  113. * @param int $id the id of the row to be deleted
  114. * @param int $userId the id of owner of the row
  115. *
  116. * @return int returns the number of affected rows
  117. */
  118. public function delete($id, $userId = null)
  119. {
  120. $adapter = $this->_table->getAdapter();
  121. $where[] = $adapter->quoteInto('id = ?', $id);
  122. if ($userId !== null) {
  123. $where[] = $adapter->quoteInto('user_id = ?', $userId);
  124. }
  125. return $this->_table->delete($where);
  126. }
  127. }