ListingSetup.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ DmJ+dQXcaHD5ZVtmYsSddO+g8baJA8BTKTA6PA6jJyE=
  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.10 [rev.7.10.02]
  11. */
  12. /**
  13. * listing setup fee class
  14. */
  15. namespace Ppb\Service\Fees;
  16. use Ppb\Service,
  17. Ppb\Db\Table\Row\Listing as ListingModel,
  18. Ppb\Db\Table\Row\Voucher;
  19. class ListingSetup extends Service\Fees
  20. {
  21. /**
  22. *
  23. * standard fees
  24. *
  25. * @var array
  26. */
  27. protected $_fees = array(
  28. self::SETUP => 'Listing Setup',
  29. self::HPFEAT => 'Home Page Featuring',
  30. self::CATFEAT => 'Category Pages Featuring',
  31. self::HIGHLIGHTED => 'Highlighted Item',
  32. self::SUBTITLE => 'Listing Subtitle',
  33. self::IMAGES => 'Listing Images',
  34. self::MEDIA => 'Listing Media',
  35. self::DIGITAL_DOWNLOADS => 'Digital Downloads',
  36. self::ADDL_CATEGORY => 'Additional Category Listing',
  37. self::BUYOUT => 'Buy Out',
  38. self::RESERVE => 'Reserve Price',
  39. self::MAKE_OFFER => 'Make Offer',
  40. self::ITEM_SWAP => 'Item Swap'
  41. );
  42. /**
  43. *
  44. * media upload fees, with corresponding keys for files that can be uploaded free of charge
  45. *
  46. * @var array
  47. */
  48. protected $_mediaFees = array(
  49. self::IMAGES => self::NB_FREE_IMAGES,
  50. self::MEDIA => self::NB_FREE_MEDIA,
  51. self::DIGITAL_DOWNLOADS => self::NB_FREE_DOWNLOADS,
  52. );
  53. /**
  54. *
  55. * listing object
  56. *
  57. * @var \Ppb\Db\Table\Row\Listing
  58. */
  59. protected $_listing;
  60. /**
  61. *
  62. * saved listing object (used for when editing a listing)
  63. *
  64. * @var \Ppb\Db\Table\Row\Listing
  65. */
  66. protected $_savedListing;
  67. /**
  68. *
  69. * total amount to be paid after the calculate method is called
  70. *
  71. * @var float
  72. */
  73. protected $_totalAmount;
  74. /**
  75. *
  76. * payment redirect path
  77. *
  78. * @var array
  79. */
  80. protected $_redirect = array(
  81. 'module' => 'listings',
  82. 'controller' => 'listing',
  83. 'action' => 'confirm',
  84. );
  85. /**
  86. *
  87. * class constructor
  88. *
  89. * @param \Ppb\Db\Table\Row\Listing $listing listing object
  90. * @param integer|string|\Ppb\Db\Table\Row\User $user the user that will be paying
  91. */
  92. public function __construct(ListingModel $listing = null, $user = null)
  93. {
  94. parent::__construct();
  95. $settings = $this->getSettings();
  96. $feesSettings = array(
  97. Service\Fees::ADDL_CATEGORY => $settings['addl_category_listing'],
  98. Service\Fees::BUYOUT => $settings['enable_buyout'],
  99. Service\Fees::RESERVE => $settings['enable_auctions'],
  100. Service\Fees::DIGITAL_DOWNLOADS => $settings['digital_downloads_max'],
  101. Service\Fees::MAKE_OFFER => $settings['enable_make_offer'],
  102. Service\Fees::IMAGES => $settings['images_max'],
  103. Service\Fees::MEDIA => $settings['videos_max'],
  104. Service\Fees::SUBTITLE => $settings['enable_subtitle'],
  105. );
  106. foreach ($this->_fees as $feeName => $feeDescription) {
  107. if (array_key_exists($feeName, $feesSettings) && ($feesSettings[$feeName] == 0)) {
  108. unset($this->_fees[$feeName]);
  109. }
  110. }
  111. if ($listing !== null) {
  112. $amount = ($listing['listing_type'] == 'product') ?
  113. $listing['buyout_price'] : max(array($listing['start_price'], $listing['reserve_price']));
  114. $this->setListing($listing)
  115. ->setAmount($amount)
  116. ->setCategoryId($listing['category_id'])
  117. ->setCurrency($listing['currency']);
  118. }
  119. if ($user !== null) {
  120. $this->setUser($user);
  121. }
  122. }
  123. /**
  124. *
  125. * set listing model
  126. *
  127. * @param \Ppb\Db\Table\Row\Listing $listing
  128. *
  129. * @return \Ppb\Service\Fees\ListingSetup
  130. */
  131. public function setListing(ListingModel $listing)
  132. {
  133. $this->_listing = $listing;
  134. return $this;
  135. }
  136. /**
  137. *
  138. * set saved listing model
  139. *
  140. * @param \Ppb\Db\Table\Row\Listing $savedListing
  141. *
  142. * @return \Ppb\Service\Fees\ListingSetup
  143. */
  144. public function setSavedListing(ListingModel $savedListing)
  145. {
  146. $this->_savedListing = $savedListing;
  147. return $this;
  148. }
  149. /**
  150. *
  151. * calculate and return an array containing all fees to be applied when creating a listing
  152. * also apply the voucher and add it as a separate row
  153. *
  154. * @return array
  155. */
  156. public function calculate()
  157. {
  158. $data = array();
  159. $this->_totalAmount = 0;
  160. $settings = $this->getSettings();
  161. foreach ($this->_fees as $key => $value) {
  162. if ($this->_applyFee($key)) {
  163. $feeAmount = $this->getFeeAmount($key);
  164. if ($feeAmount > 0 || $settings['display_free_fees']) {
  165. $data[] = array(
  166. 'key' => $key,
  167. 'name' => $value,
  168. 'amount' => $feeAmount,
  169. 'tax_rate' => $this->getTaxType()->getData('amount'),
  170. 'currency' => $settings['currency'],
  171. );
  172. }
  173. $this->_totalAmount += $feeAmount;
  174. }
  175. }
  176. if (($voucher = $this->getVoucher()) instanceof Voucher) {
  177. $totalAmount = $this->_applyVoucher($this->_totalAmount, $settings['currency']);
  178. if ($totalAmount != $this->_totalAmount) {
  179. $voucherAmount = $totalAmount - $this->_totalAmount;
  180. $data[] = array(
  181. 'key' => 'voucher_reduction',
  182. 'name' => $voucher->description(),
  183. 'amount' => $voucherAmount,
  184. 'tax_rate' => $this->getTaxType()->getData('amount'),
  185. 'currency' => $settings['currency'],
  186. );
  187. $this->_totalAmount = $totalAmount;
  188. }
  189. }
  190. return $data;
  191. }
  192. /**
  193. *
  194. * get redirect array, but attach listing_id variable if it is set
  195. *
  196. * @return array
  197. */
  198. public function getRedirect()
  199. {
  200. $redirect = $this->_redirect;
  201. if (!empty($this->_transactionDetails['data']['listing_id'])) {
  202. $redirect['params']['id'] = $this->_transactionDetails['data']['listing_id'];
  203. }
  204. return $redirect;
  205. }
  206. /**
  207. *
  208. * activate the affected listing
  209. * the callback will also process the listing in case a payment has been reversed etc.
  210. *
  211. * @param bool $ipn true if payment is completed, false otherwise
  212. * @param array $post array keys: {listing_id}
  213. *
  214. * @return \Ppb\Service\Fees\ListingSetup
  215. */
  216. public function callback($ipn, array $post)
  217. {
  218. $listingsService = new Service\Listings();
  219. $listing = $listingsService->findBy('id', $post['listing_id']);
  220. $flag = ($ipn) ? 1 : 0;
  221. $listing->updateActive($flag);
  222. return $this;
  223. }
  224. /**
  225. *
  226. * get the amount of a certain fee
  227. * calculates based on preferred seller feature
  228. * - in case of an image fee, calculate based on the number of images uploaded
  229. * (plus apply the free images setting as well)
  230. *
  231. * @param string $name
  232. * @param float $amount
  233. * @param int $categoryId
  234. * @param null $currency
  235. *
  236. * @return float|null return the fee amount or null if no fee applies for the selected action
  237. */
  238. public function getFeeAmount($name = null, $amount = null, $categoryId = null, $currency = null)
  239. {
  240. $feeAmount = parent::getFeeAmount($name, $amount, $categoryId, $currency);
  241. if (array_key_exists($name, $this->_mediaFees)) {
  242. $savedListingValue = (!empty($this->_savedListing[$name])) ? $this->_savedListing[$name] : null;
  243. $savedListingCategoryId = (!empty($this->_savedListing['category_id'])) ? $this->_savedListing['category_id'] : null;
  244. $counter = (is_numeric($this->_listing[$name])) ?
  245. intval($this->_listing[$name]) : count(array_filter((array)\Ppb\Utility::unserialize($this->_listing[$name])));
  246. $counterFree = intval($this->getFeeAmount($this->_mediaFees[$name]));
  247. $counterSaved = ($this->_listing['category_id'] == $savedListingCategoryId) ?
  248. count(\Ppb\Utility::unserialize($savedListingValue)) : 0;
  249. if ($counterSaved > $counterFree) {
  250. $counter -= $counterSaved;
  251. }
  252. else {
  253. $counter -= $counterFree;
  254. }
  255. $feeAmount *= ($counter <= 0) ? 0 : $counter;
  256. }
  257. return $feeAmount;
  258. }
  259. /**
  260. *
  261. * get total amount to be paid resulted from the calculate() method
  262. *
  263. * @return float
  264. */
  265. public function getTotalAmount()
  266. {
  267. return $this->_totalAmount;
  268. }
  269. /**
  270. *
  271. * check whether to apply the requested fee
  272. * do not apply any fees if the listing is a draft
  273. *
  274. * @param string $name the name of the fee
  275. *
  276. * @return bool
  277. */
  278. protected function _applyFee($name)
  279. {
  280. $savedListingValue = (!empty($this->_savedListing[$name])) ? $this->_savedListing[$name] : null;
  281. $savedListingCategoryId = (!empty($this->_savedListing['category_id'])) ? $this->_savedListing['category_id'] : null;
  282. if ($this->_listing['draft']) {
  283. return false;
  284. }
  285. else if (in_array($name, $this->_feesTiers) && (!$this->_savedListing instanceof ListingModel)) {
  286. return true;
  287. }
  288. else if (array_key_exists($name, $this->_mediaFees)) {
  289. $counter = count(array_filter((array)$this->_listing[$name]));
  290. if ($this->_savedListing instanceof ListingModel) {
  291. $counterSavedListing = count(array_filter((array)$savedListingValue));
  292. if (
  293. ($counter > $counterSavedListing) ||
  294. ($this->_listing['category_id'] != $savedListingCategoryId)
  295. ) {
  296. return true;
  297. }
  298. }
  299. else if ($counter > 0) {
  300. return true;
  301. }
  302. }
  303. else if (
  304. ($this->_listing[$name] && !$savedListingValue) ||
  305. ($this->_listing['category_id'] != $savedListingCategoryId)
  306. ) {
  307. $value = $this->_listing[$name];
  308. if (is_numeric($value)) {
  309. $value = floatval($value);
  310. }
  311. return (!empty($value)) ? true : false;
  312. }
  313. return false;
  314. }
  315. }