Bids.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ 65zC3sODIQFqo9q7wR1qNp2EzIoopPvOfl7VbdgSBdo=
  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.7
  11. */
  12. /**
  13. * bids table service class
  14. */
  15. namespace Ppb\Service;
  16. use Ppb\Db\Table\Bids as BidsTable,
  17. Ppb\Service,
  18. Cube\Db\Expr,
  19. Cube\Controller\Front;
  20. class Bids extends AbstractService
  21. {
  22. /**
  23. *
  24. * bid increments table service
  25. *
  26. * @var \Ppb\Service\Table\BidIncrements
  27. */
  28. protected $_bidIncrements;
  29. /**
  30. *
  31. * the message output after a bid is placed
  32. *
  33. * @var string
  34. */
  35. protected $_message;
  36. /**
  37. *
  38. * class constructor
  39. */
  40. public function __construct()
  41. {
  42. parent::__construct();
  43. $this->setTable(
  44. new BidsTable());
  45. }
  46. /**
  47. *
  48. * get bid increments table service
  49. *
  50. * @return \Ppb\Service\Table\BidIncrements
  51. */
  52. public function getBidIncrements()
  53. {
  54. if (!$this->_bidIncrements instanceof Service\Table\BidIncrements) {
  55. $this->setBidIncrements(
  56. new Service\Table\BidIncrements());
  57. }
  58. return $this->_bidIncrements;
  59. }
  60. /**
  61. *
  62. * set bid increments table service
  63. *
  64. * @param \Ppb\Service\Table\BidIncrements $bidIncrements
  65. * @return \Ppb\Service\Bids
  66. */
  67. public function setBidIncrements(Service\Table\BidIncrements $bidIncrements)
  68. {
  69. $this->_bidIncrements = $bidIncrements;
  70. return $this;
  71. }
  72. /**
  73. *
  74. * set bid post output message
  75. *
  76. * @param string $message
  77. * @return $this
  78. */
  79. public function setMessage($message)
  80. {
  81. $this->_message = $message;
  82. return $this;
  83. }
  84. /**
  85. *
  86. * get bid post output message
  87. *
  88. * @return string
  89. */
  90. public function getMessage()
  91. {
  92. return $this->_message;
  93. }
  94. /**
  95. *
  96. * process that posts a new bid on an auction
  97. * the listing id will always result from a "select for update" query, so that two bids cannot be posted at the same time.
  98. *
  99. * @param array $data
  100. * @return \Ppb\Service\Bids
  101. * @throws \InvalidArgumentException
  102. */
  103. public function save($data)
  104. {
  105. if (!isset($data['listing_id'])) {
  106. throw new \InvalidArgumentException("No listing selected for the post bid operation.");
  107. }
  108. $listingsService = new Service\Listings();
  109. /** @var \Ppb\Db\Table\Row\Listing $listing */
  110. $listing = $listingsService->findBy('id', $data['listing_id']);
  111. $view = Front::getInstance()->getBootstrap()->getResource('view');
  112. $settings = $this->getSettings();
  113. $translate = $this->getTranslate();
  114. if ($listing->countDependentRowset('\Ppb\Db\Table\Bids') > 0) {
  115. $bids = $listing->getBids();
  116. /** @var \Ppb\Db\Table\Row\Bid $hBid */
  117. $hBid = $bids->getRow(0);
  118. $highBid = $hBid->toArray();
  119. if ($settings['proxy_bidding'] &&
  120. $data['amount'] > $listing['reserve_price'] &&
  121. $highBid['amount'] >= $listing['reserve_price'] &&
  122. $highBid['user_id'] == $data['user_id']
  123. ) { // update maximum bid
  124. $adapter = $this->_table->getAdapter();
  125. $this->_table->update(
  126. array(
  127. 'maximum_bid' => $data['amount'],
  128. 'outbid' => 0,
  129. 'updated_at' => new Expr('now()')
  130. ),
  131. array(
  132. $adapter->quoteInto('listing_id = ?', $listing['id']),
  133. $adapter->quoteInto('user_id = ?', $data['user_id'])
  134. ));
  135. $this->setMessage(
  136. sprintf($translate->_('Your maximum bid has been updated to %s.'),
  137. $view->amount($data['amount'], $listing['currency'])));
  138. }
  139. else {
  140. // set to outbid all previous bids
  141. $this->_setOutbidBids($listing['id']);
  142. unset($highBid['id']);
  143. if ($data['amount'] > $highBid['maximum_bid']) {
  144. if ($highBid['maximum_bid'] > $highBid['amount']) {
  145. $highBid['amount'] = $highBid['maximum_bid'];
  146. $this->_saveBid($highBid);
  147. }
  148. // MAIL OUTBID BIDDER NOTIFICATION
  149. $mail = new \Listings\Model\Mail\BuyerNotification();
  150. $mail->outbid($listing, $hBid)->send();
  151. $this->_setOutbidBids($listing['id']);
  152. $data['maximum_bid'] = $data['amount'];
  153. $data['amount'] = $listing->minimumBid($data['amount']);
  154. $this->_saveBid($data);
  155. $this->setMessage(
  156. sprintf($translate->_('Your maximum bid, in the amount of %s, has been posted successfully. You are now the highest bidder on this item, with a bid of %s.'),
  157. $view->amount($data['maximum_bid'], $listing['currency']),
  158. $view->amount(min(array($data['amount'], $data['maximum_bid'])), $listing['currency'])));
  159. // TODO: send outbid mail to the bidder having the previous high bid.
  160. }
  161. else {
  162. $data['maximum_bid'] = $data['amount'];
  163. $this->_saveBid($data);
  164. $this->_setOutbidBids($listing['id']);
  165. $highBid['amount'] = $listing->minimumBid($highBid['amount']);
  166. $this->_saveBid($highBid);
  167. $this->setMessage(
  168. sprintf($translate->_('Your maximum bid, in the amount of %s, has been posted, but is lower than the current high bidder\'s maximum bid.'),
  169. $view->amount($data['maximum_bid'], $listing['currency'])));
  170. }
  171. }
  172. }
  173. else {
  174. $data['maximum_bid'] = $data['amount'];
  175. $data['amount'] = $listing->minimumBid($data['amount']);
  176. $this->_saveBid($data);
  177. $this->setMessage(
  178. sprintf($translate->_('Your maximum bid, in the amount of %s, has been posted successfully. You are now the highest bidder on this item, with a bid of %s.'),
  179. $view->amount($data['maximum_bid'], $listing['currency']),
  180. $view->amount($data['amount'], $listing['currency'])));
  181. }
  182. if ($settings['enable_auctions_sniping']) {
  183. if ($listing->getTimeLeft() < $settings['auctions_sniping_minutes'] * 60) {
  184. $listing->save(array(
  185. 'end_time' => new Expr('now() + interval ' . intval($settings['auctions_sniping_minutes']) . ' minute'),
  186. ));
  187. }
  188. }
  189. if ($settings['enable_change_duration'] && $settings['change_duration_days'] > 0) {
  190. if ($listing->getTimeLeft() > $settings['change_duration_days'] * 86400) {
  191. $listing->save(array(
  192. 'end_time' => new Expr('now() + interval ' . intval($settings['change_duration_days']) . ' day'),
  193. ));
  194. }
  195. }
  196. return $this;
  197. }
  198. /**
  199. *
  200. * insert a prepared bid row into the bids table
  201. *
  202. * @param array $data
  203. * @return \Ppb\Service\Bids
  204. */
  205. protected function _saveBid(array $data)
  206. {
  207. $row = null;
  208. $data = $this->_prepareSaveData($data);
  209. if (array_key_exists('id', $data)) {
  210. $select = $this->_table->select()
  211. ->where("id = ?", $data['id']);
  212. unset($data['id']);
  213. $row = $this->_table->fetchRow($select);
  214. }
  215. if (count($row) > 0) {
  216. $data['updated_at'] = new Expr('now()');
  217. $this->_table->update($data, "id='{$row['id']}'");
  218. }
  219. else {
  220. $data['created_at'] = new Expr('now()');
  221. $this->_table->insert($data);
  222. }
  223. return $this;
  224. }
  225. /**
  226. *
  227. * set outbid flag to certain bids
  228. *
  229. * @param int $listingId
  230. * @return $this
  231. */
  232. protected function _setOutbidBids($listingId)
  233. {
  234. $this->_table->update(
  235. array('outbid' => 1, 'updated_at' => new Expr('now()')), "listing_id='" . (int)$listingId . "'");
  236. return $this;
  237. }
  238. /**
  239. *
  240. * prepare custom field data for when saving to the table
  241. *
  242. * @param array $data
  243. * @return array
  244. */
  245. protected function _prepareSaveData($data = array())
  246. {
  247. if (array_key_exists('amount', $data) &&
  248. array_key_exists('maximum_bid', $data)
  249. ) {
  250. if ($data['amount'] > $data['maximum_bid']) {
  251. $data['amount'] = $data['maximum_bid'];
  252. }
  253. }
  254. $data = parent::_prepareSaveData($data);
  255. return $data;
  256. }
  257. }