Accounting.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ H2ZluzpPfZhz1mQiv795bx8ElEItbGgGVSGsjYQoYTU=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.4
  11. */
  12. /**
  13. * accounting table row object model
  14. */
  15. namespace Ppb\Db\Table\Row;
  16. use Ppb\Form\Element\Range,
  17. Ppb\Service;
  18. class Accounting extends AbstractAccounting
  19. {
  20. /**
  21. * refund flags
  22. */
  23. const REFUND_ALLOWED = 'allowed';
  24. const REFUND_REQUESTED = 'requested';
  25. const REFUND_REFUNDED = 'refunded';
  26. const REFUND_DECLINED = 'declined';
  27. /**
  28. *
  29. * invoice details page caption
  30. *
  31. * @return string
  32. */
  33. public function caption()
  34. {
  35. $translate = $this->getTranslate();
  36. return ($this->getData('amount') > 0) ? $translate->_('Debit') : $translate->_('Credit');
  37. }
  38. /**
  39. *
  40. * check if a refund can be requested for the selected accounting row
  41. *
  42. * refunds can be requested by the payer only,
  43. * and only if the refund requests admin settings are met
  44. *
  45. * @return bool
  46. */
  47. public function canRequestRefund()
  48. {
  49. $settings = $this->getSettings();
  50. if ($settings['enable_sale_fee_refunds']) {
  51. $days = (time() - strtotime($this->getData('created_at'))) / (60 * 60 * 24);
  52. $range = \Ppb\Utility::unserialize($settings['sale_fee_refunds_range']);
  53. $from = isset($range[Range::RANGE_FROM]) ? doubleval($range[Range::RANGE_FROM]) : 0;
  54. $to = isset($range[Range::RANGE_TO]) ? doubleval($range[Range::RANGE_TO]) : null;
  55. $user = $this->getUser();
  56. if (
  57. ($from == 0 || $from < $days) &&
  58. ($to == 0 || $to > $days) &&
  59. $this->getData('user_id') == $user['id'] &&
  60. $this->getData('refund_flag') == self::REFUND_ALLOWED
  61. ) {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. /**
  68. *
  69. * check if the logged in user can process a refund request (admin)
  70. *
  71. * @return bool
  72. */
  73. public function canProcessRefund()
  74. {
  75. if ($this->getData('refund_flag') == self::REFUND_REQUESTED) {
  76. $user = $this->getUser();
  77. if ($user->getData('role') == 'Admin') {
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. /**
  84. *
  85. * the payer makes a refund request
  86. *
  87. * @return bool
  88. */
  89. public function makeRefundRequest()
  90. {
  91. if ($this->canRequestRefund()) {
  92. $this->save(array(
  93. 'refund_flag' => self::REFUND_REQUESTED,
  94. ));
  95. // MAIL REFUND REQUEST ADMIN NOTIFICATION
  96. return true;
  97. }
  98. return false;
  99. }
  100. /**
  101. *
  102. * admin accepts the refund request and credits the payer's account balance
  103. *
  104. * @param bool $override override settings
  105. * @return bool
  106. */
  107. public function acceptRefundRequest($override = false)
  108. {
  109. if ($this->canProcessRefund() || $override) {
  110. $this->save(array(
  111. 'refund_flag' => self::REFUND_REFUNDED,
  112. ));
  113. // MAIL REFUND ACCEPTED USER NOTIFICATION
  114. // add credit to payer account balance
  115. /** @var \Ppb\Db\Table\Row\User $user */
  116. $user = $this->findParentRow('\Ppb\Db\Table\Users');
  117. $user->save(array(
  118. 'balance' => ($user['balance'] - $this->getData('amount'))
  119. ));
  120. $name = sprintf('Sale Transaction Refund - "%s"', $this->displayName());
  121. $settings = $this->getSettings();
  122. $accountingService = new Service\Accounting();
  123. $accountingService->save(array(
  124. 'name' => $name,
  125. 'amount' => (-1) * $this->getData('amount'),
  126. 'user_id' => $this->getData('user_id'),
  127. 'currency' => $settings['currency'],
  128. ));
  129. return true;
  130. }
  131. return false;
  132. }
  133. /**
  134. *
  135. * admin rejects the refund request
  136. *
  137. * @return bool
  138. */
  139. public function rejectRefundRequest()
  140. {
  141. if ($this->canProcessRefund()) {
  142. $this->save(array(
  143. 'refund_flag' => self::REFUND_DECLINED,
  144. ));
  145. // MAIL REFUND REJECTED USER NOTIFICATION
  146. return true;
  147. }
  148. return false;
  149. }
  150. }