123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace Ppb\Db\Table\Row;
- use Ppb\Form\Element\Range,
- Ppb\Service;
- class Accounting extends AbstractAccounting
- {
-
- const REFUND_ALLOWED = 'allowed';
- const REFUND_REQUESTED = 'requested';
- const REFUND_REFUNDED = 'refunded';
- const REFUND_DECLINED = 'declined';
-
- public function caption()
- {
- $translate = $this->getTranslate();
- return ($this->getData('amount') > 0) ? $translate->_('Debit') : $translate->_('Credit');
- }
-
- public function canRequestRefund()
- {
- $settings = $this->getSettings();
- if ($settings['enable_sale_fee_refunds']) {
- $days = (time() - strtotime($this->getData('created_at'))) / (60 * 60 * 24);
- $range = \Ppb\Utility::unserialize($settings['sale_fee_refunds_range']);
- $from = isset($range[Range::RANGE_FROM]) ? doubleval($range[Range::RANGE_FROM]) : 0;
- $to = isset($range[Range::RANGE_TO]) ? doubleval($range[Range::RANGE_TO]) : null;
- $user = $this->getUser();
- if (
- ($from == 0 || $from < $days) &&
- ($to == 0 || $to > $days) &&
- $this->getData('user_id') == $user['id'] &&
- $this->getData('refund_flag') == self::REFUND_ALLOWED
- ) {
- return true;
- }
- }
- return false;
- }
-
- public function canProcessRefund()
- {
- if ($this->getData('refund_flag') == self::REFUND_REQUESTED) {
- $user = $this->getUser();
- if ($user->getData('role') == 'Admin') {
- return true;
- }
- }
- return false;
- }
-
- public function makeRefundRequest()
- {
- if ($this->canRequestRefund()) {
- $this->save(array(
- 'refund_flag' => self::REFUND_REQUESTED,
- ));
-
- return true;
- }
- return false;
- }
-
- public function acceptRefundRequest($override = false)
- {
- if ($this->canProcessRefund() || $override) {
- $this->save(array(
- 'refund_flag' => self::REFUND_REFUNDED,
- ));
-
-
-
- $user = $this->findParentRow('\Ppb\Db\Table\Users');
- $user->save(array(
- 'balance' => ($user['balance'] - $this->getData('amount'))
- ));
- $name = sprintf('Sale Transaction Refund - "%s"', $this->displayName());
- $settings = $this->getSettings();
- $accountingService = new Service\Accounting();
- $accountingService->save(array(
- 'name' => $name,
- 'amount' => (-1) * $this->getData('amount'),
- 'user_id' => $this->getData('user_id'),
- 'currency' => $settings['currency'],
- ));
- return true;
- }
- return false;
- }
-
- public function rejectRefundRequest()
- {
- if ($this->canProcessRefund()) {
- $this->save(array(
- 'refund_flag' => self::REFUND_DECLINED,
- ));
-
- return true;
- }
- return false;
- }
- }
|