SaleTransaction.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ J5seVzNoHGFFCwpOgepiWo+yiOOd8aol8XIc0n0gj7g=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2016 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.7
  11. */
  12. /**
  13. * sale transaction fee class
  14. */
  15. namespace Ppb\Service\Fees;
  16. use Ppb\Service,
  17. Ppb\Db\Table\Row\Sale as SaleModel;
  18. class SaleTransaction extends Service\Fees
  19. {
  20. /**
  21. *
  22. * fees to be included
  23. *
  24. * @var array
  25. */
  26. protected $_fees = array(
  27. self::SALE => 'Sale Transaction Fee',
  28. );
  29. /**
  30. *
  31. * sale object
  32. *
  33. * @var \Ppb\Db\Table\Row\Sale
  34. */
  35. protected $_sale;
  36. /**
  37. *
  38. * total amount to be paid after the calculate method is called
  39. *
  40. * @var float
  41. */
  42. protected $_totalAmount;
  43. /**
  44. *
  45. * payment redirect path
  46. *
  47. * @var array
  48. */
  49. protected $_redirect = array(
  50. 'module' => 'members',
  51. 'controller' => 'invoices',
  52. 'action' => 'browse',
  53. );
  54. /**
  55. *
  56. * class constructor
  57. *
  58. * @param \Ppb\Db\Table\Row\Sale $sale
  59. * @param integer|string|\Ppb\Db\Table\Row\User $user the user that will be paying
  60. */
  61. public function __construct(SaleModel $sale = null, $user = null)
  62. {
  63. parent::__construct();
  64. if ($sale !== null) {
  65. $this->setSale($sale);
  66. }
  67. if ($user !== null) {
  68. $this->setUser($user);
  69. }
  70. }
  71. /**
  72. *
  73. * set sale model
  74. * also, based on the sale model, set the total amount that will be used to calculate the fees against
  75. * the amount will be the total amount without tax, postage and insurance
  76. *
  77. * @param \Ppb\Db\Table\Row\Sale $sale
  78. *
  79. * @return $this
  80. */
  81. public function setSale(SaleModel $sale)
  82. {
  83. $this->_sale = $sale;
  84. $this->setAmount(
  85. $sale->calculateTotal(true));
  86. return $this;
  87. }
  88. /**
  89. *
  90. * get sale model
  91. *
  92. * @return \Ppb\Db\Table\Row\Sale
  93. */
  94. public function getSale()
  95. {
  96. return $this->_sale;
  97. }
  98. /**
  99. *
  100. * calculate and return an array containing all fees to be applied for the sale in question
  101. *
  102. * @return array
  103. */
  104. public function calculate()
  105. {
  106. $data = array();
  107. $this->_totalAmount = 0;
  108. $settings = $this->getSettings();
  109. foreach ($this->_fees as $key => $value) {
  110. $feeAmount = $this->getFeeAmount($key);
  111. if ($feeAmount > 0 || $settings['display_free_fees']) {
  112. $data[] = array(
  113. 'key' => $key,
  114. 'name' => array(
  115. 'string' => 'Sale Transaction Fee - Sale ID: #%s',
  116. 'args' => array($this->_sale['id']),
  117. ),
  118. 'amount' => $feeAmount,
  119. 'tax_rate' => $this->getTaxType()->getData('amount'),
  120. 'currency' => $settings['currency'],
  121. );
  122. }
  123. $this->_totalAmount += $feeAmount;
  124. }
  125. return $data;
  126. }
  127. /**
  128. *
  129. * get redirect array, and attach type and sale_id variables
  130. *
  131. * @return array
  132. */
  133. public function getRedirect()
  134. {
  135. $settings = $this->getSettings();
  136. $redirect = $this->_redirect;
  137. $redirect['params']['type'] = ($settings['sale_fee_payer'] == 'buyer') ? 'bought' : 'sold';
  138. if (!empty($this->_transactionDetails['data']['sale_id'])) {
  139. $redirect['params']['sale_id'] = $this->_transactionDetails['data']['sale_id'];
  140. }
  141. return $redirect;
  142. }
  143. /**
  144. *
  145. * activate the affected sale
  146. * the callback will also process the listing in case a payment has been reversed etc
  147. *
  148. * @param bool $ipn true if payment is completed, false otherwise
  149. * @param array $post array keys: {sale_id}
  150. *
  151. * @return \Ppb\Service\Fees\SaleTransaction
  152. */
  153. public function callback($ipn, array $post)
  154. {
  155. $salesService = new Service\Sales();
  156. $sale = $salesService->findBy('id', $post['sale_id']);
  157. $flag = ($ipn) ? 1 : 0;
  158. $sale->save(array(
  159. 'active' => $flag,
  160. ));
  161. return $this;
  162. }
  163. /**
  164. *
  165. * apply sale transaction related fees. for the sale transaction fee, it will be calculated for each listing so that
  166. * category specific fees are used properly.
  167. *
  168. * @param string $name
  169. * @param float $amount
  170. * @param int $categoryId
  171. * @param null $currency
  172. *
  173. * @return float|null return the fee amount or null if no fee applies for the selected action
  174. */
  175. public function getFeeAmount($name = null, $amount = null, $categoryId = null, $currency = null)
  176. {
  177. if ($name == self::SALE) {
  178. $sale = $this->getSale();
  179. $salesListings = $sale->findDependentRowset('\Ppb\Db\Table\SalesListings');
  180. $feeAmount = 0;
  181. $currency = $sale->getData('currency');
  182. /** @var \Ppb\Db\Table\Row\SaleListing $saleListing */
  183. foreach ($salesListings as $saleListing) {
  184. $listing = $saleListing->findParentRow('\Ppb\Db\Table\Listings');
  185. $amount = $saleListing->calculateTotal(false);
  186. $categoryId = $listing->getData('category_id');
  187. $feeAmount += parent::getFeeAmount($name, $amount, $categoryId, $currency);
  188. }
  189. }
  190. else {
  191. $feeAmount = parent::getFeeAmount($name, $amount, $categoryId, $currency);
  192. }
  193. return $feeAmount;
  194. }
  195. /**
  196. *
  197. * get total amount to be paid resulted from the calculate() method
  198. *
  199. * @return float
  200. */
  201. public function getTotalAmount()
  202. {
  203. return $this->_totalAmount;
  204. }
  205. /**
  206. *
  207. * only apply preferred sellers reduction if it should apply to sale fees
  208. *
  209. * @param float $amount
  210. *
  211. * @return float
  212. */
  213. protected function _applyPreferredSellersReduction($amount)
  214. {
  215. $settings = $this->getSettings();
  216. if ($settings['preferred_sellers_apply_sale']) {
  217. $amount = parent::_applyPreferredSellersReduction($amount);
  218. }
  219. return $amount;
  220. }
  221. }