123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- <?php
- namespace Ppb\Db\Table\Row;
- use Ppb\Service;
- class Offer extends AbstractRow
- {
-
- const STATUS_PENDING = 'pending';
- const STATUS_ACCEPTED = 'accepted';
- const STATUS_DECLINED = 'declined';
- const STATUS_WITHDRAWN = 'withdrawn';
- const STATUS_COUNTER = 'counter';
-
- public static $statuses = array(
- self::STATUS_PENDING => 'Pending',
- self::STATUS_ACCEPTED => 'Accepted',
- self::STATUS_DECLINED => 'Declined',
- self::STATUS_WITHDRAWN => 'Withdrawn',
- self::STATUS_COUNTER => 'Counter Offer',
- );
-
- public function canAccept(Listing $listing = null)
- {
- if (!$listing instanceof Listing) {
-
- $listing = $this->findParentRow('\Ppb\Db\Table\Listings');
- }
- $user = $this->getUser();
- $quantity = $this->getData('quantity');
- $productAttributes = \Ppb\Utility::unserialize($this->getData('product_attributes'));
- if (
- $listing->canAcceptOffer($quantity, $productAttributes) &&
- $this->getData('receiver_id') == $user['id'] &&
- $this->getData('status') == self::STATUS_PENDING
- ) {
- return true;
- }
- return false;
- }
-
- public function canCounter(Listing $listing = null)
- {
- return $this->canAccept($listing);
- }
-
- public function canWithdraw()
- {
- $user = $this->getUser();
- return ($this->getData('user_id') == $user['id'] &&
- $this->getData('status') == self::STATUS_PENDING) ? true : false;
- }
-
- public function canDecline()
- {
- $user = $this->getUser();
- return ($this->getData('receiver_id') == $user['id'] &&
- $this->getData('status') == self::STATUS_PENDING) ? true : false;
- }
-
- public function getBuyerId(Listing $listing = null)
- {
- if (!$listing instanceof Listing) {
-
- $listing = $this->findParentRow('\Ppb\Db\Table\Listings');
- }
- return ($this->getData('user_id') == $listing['user_id']) ?
- $this->getData('receiver_id') : $this->getData('user_id');
- }
-
- public function accept()
- {
-
- $listing = $this->findParentRow('\Ppb\Db\Table\Listings', null, $this->getTable()->select()->forUpdate());
- if ($this->canAccept($listing)) {
- $this->save(array(
- 'status' => self::STATUS_ACCEPTED,
- ));
-
- $mail = new \Listings\Model\Mail\UserNotification();
- $mail->offerAccepted($listing, $this)->send();
- $service = new Service\Sales();
- $data = array(
- 'buyer_id' => $this->getBuyerId($listing),
- 'seller_id' => $listing['user_id'],
- 'listings' => array(
- array(
- 'listing_id' => $listing['id'],
- 'price' => $this->getData('amount'),
- 'quantity' => (int)$this->getData('quantity'),
- 'product_attributes' => $this->getData('product_attributes'),
- ),
- ),
- );
- $service->save($data);
- return true;
- }
- return false;
- }
-
- public function counter()
- {
-
- $listing = $this->findParentRow('\Ppb\Db\Table\Listings');
- if ($this->canCounter($listing)) {
- $this->save(array(
- 'status' => self::STATUS_COUNTER,
- ));
- return true;
- }
- return false;
- }
-
- public function decline()
- {
-
- $listing = $this->findParentRow('\Ppb\Db\Table\Listings');
- if ($this->canDecline()) {
- $this->save(array(
- 'status' => self::STATUS_DECLINED,
- ));
-
- $mail = new \Listings\Model\Mail\UserNotification();
- $mail->offerDeclined($listing, $this)->send();
- return true;
- }
- return false;
- }
-
- public function withdraw()
- {
- if ($this->canWithdraw()) {
-
- $listing = $this->findParentRow('\Ppb\Db\Table\Listings');
- $this->save(array(
- 'status' => self::STATUS_WITHDRAWN,
- ));
-
- $mail = new \Listings\Model\Mail\UserNotification();
- $mail->offerWithdrawn($listing, $this)->send();
- return true;
- }
- return false;
- }
- }
|