2 namespace Auction\Service;
10 class Bid extends \Auction\DataBin\Bid
13 protected $_increments = null;
16 * Retrieve next higher auto-increment rule.
17 * @param unknown $price
18 * @return \Auction\Entity\Increment
20 public function _getNextIncrement($price)
22 if (!$this->_increments)
24 $this->_increments = $this->em
25 ->getRepository('\Auction\Entity\Increment')
26 ->findBy(array(), array('to' => 'ASC'));
28 foreach ($this->_increments as $incr)
30 if ($incr->from >= $price)
40 * ConditionalContract: Update.
43 * @param object|null $jobRecord
44 * @param object|null $record
45 * @param \Workspace\Contract\AbstractBase $contract
48 public function noBidOnExpiredAuction($meta, $jobRecord, $record, \Workspace\Contract\AbstractBase $contract)
50 if ('Active' != $jobRecord->jobState
51 || $jobRecord->endDate->getTimestamp() > time())
53 throw new \Exception('Auction already closed.');
58 * ExecuteAfter: Create.
59 * Handle bidding details.
61 * @param object|null $jobRecord
62 * @param object|null $record
63 * @param \Workspace\Utility\ServiceInputParams $contract
66 public function updateStatus($meta, $auction, $record, \Workspace\Utility\ServiceInputParams $contract)
68 #-> Collect some info.
70 ->getRepository('\Auction\Entity\Basket')
72 'auction' => $auction->id,
73 'company' => $record->company->id
77 $basket = new \Auction\Entity\Basket();
78 $basket->fromArray(array(
79 'auction' => $auction,
80 'company' => $record->company,
81 'profile' => $record->profile
83 $this->em->persist($basket);
84 $this->em->flush($basket);
86 elseif (true == $basket->archived)
88 $basket->archived = false;
89 $this->em->flush($basket);
91 if ($auction->reservePrice > $record->amount)
93 $record->status = 'Archived';
94 $this->em->flush($record);
95 throw new \Exception('Your bid was placed but you were just outbid.');
98 $newCurrentBid = false;
103 #-> Lockdown some working space.
105 $this->em->getConnection()->beginTransaction();
106 $rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder($this->em);
107 $rsm->addRootEntityFromClassMetadata('\Auction\Entity\Auction', 'auction');
108 $query = $this->em->createNativeQuery("SELECT * FROM auction WHERE id = :id FOR UPDATE", $rsm);
109 $query->setParameter("id", $auction->id);
110 $jobRecord = $query->getOneOrNullResult();
111 $cBidId = $jobRecord->currentBid
112 ? $jobRecord->currentBid->id
114 $aBidId = $jobRecord->currentBid && $jobRecord->currentBid->autoBid
115 ? $jobRecord->currentBid->autoBid->id
117 $record = $this->em->find('\Auction\Entity\Bid', $record->id);
120 if ('Active' != $jobRecord->jobState
121 || time() > $jobRecord->endDate->getTimestamp())
123 $record->status = 'Archived';
124 $this->em->flush($record);
125 $this->em->getConnection()->commit();
126 throw new \Exception('Your bid could not be placed. This auction item have been closed.');
131 $nextRule = $this->_getNextIncrement(
132 $jobRecord->currentBidPrice != 0.0
133 ? $jobRecord->currentBidPrice
134 : $jobRecord->reservePrice
136 $incr = $jobRecord->bidIncrement;
140 $jobRecord->numberOfBids++;
141 //$record->amount = floor($record->amount / $incr) * $incr;
142 if ($nextRule && $record->amount >= $nextRule->from)
144 $jobRecord->bidIncrement = $nextRule->amount;
145 $incr = $nextRule->amount;
147 if (is_null($jobRecord->currentBid))
149 #-> First bid on this item.
150 $newCurrentBid = true;
153 elseif ($jobRecord->currentBidPrice >= $record->amount
154 || $jobRecord->currentBid->company->id == $record->company->id)
156 #-> Late arrival or useless hack.
157 $record->status = 'Archived';
158 $this->em->flush($record);
159 $this->em->getConnection()->commit();
160 if ($jobRecord->currentBid->company->id == $record->company->id)
162 throw new \Exception('Your bid was not successfull. Your dealership already has the winning bid.');
166 throw new \Exception('Your bid was placed but you were just outbid.');
173 #-> Competing bids, figure it out.
174 //$this->em->merge($jobRecord->currentBid);
177 if (!is_null($jobRecord->currentBid->autoBid))
179 #-> Bid against existing auto-bid.
180 if ($record->amount > $jobRecord->currentBid->autoBid->amount)
182 $newCurrentBid = true;
184 $oldBid = $jobRecord->currentBid;
185 $jobRecord->currentBid->status = 'Archived';
186 $jobRecord->currentBid->autoBid->status = 'Archived';
187 $this->em->flush($jobRecord->currentBid);
188 $this->em->flush($jobRecord->currentBid->autoBid);
190 // $jobRecord->numberOfBids++;
192 elseif ($record->amount <= $jobRecord->currentBid->autoBid->amount
193 && $jobRecord->currentBid->autoBid->amount < ($record->amount + $incr))
195 $atBid = new \Auction\Entity\Bid();
196 $atBid->fromArray(array(
197 'auction' => $jobRecord,
198 'company' => $jobRecord->currentBid->company,
199 'profile' => $jobRecord->currentBid->profile,
200 'autoBid' => $jobRecord->currentBid->autoBid,
201 'amount' => $record->amount
203 $this->em->persist($atBid);
204 $this->em->flush($atBid);
205 $jobRecord->numberOfBids++;
206 $newCurrentBid = true;
209 $record->status = 'Archived';
210 $this->em->flush($record);
215 $atBid = new \Auction\Entity\Bid();
216 $atBid->fromArray(array(
217 'auction' => $jobRecord,
218 'company' => $jobRecord->currentBid->company,
219 'profile' => $jobRecord->currentBid->profile,
220 'autoBid' => $jobRecord->currentBid->autoBid,
221 'amount' => $record->amount + $incr
223 $this->em->persist($atBid);
224 $this->em->flush($atBid);
225 $jobRecord->numberOfBids++;
226 $newCurrentBid = true;
229 $record->status = 'Archived';
230 $this->em->flush($record);
237 if ($record->amount > $jobRecord->currentBidPrice)
239 $newCurrentBid = true;
241 $oldBid = $jobRecord->currentBid;
242 $jobRecord->currentBid->status = 'Archived';
243 $this->em->flush($jobRecord->currentBid);
245 // $jobRecord->numberOfBids++;
250 $record->status = 'Archived';
251 $this->em->flush($record);
253 // $jobRecord->numberOfBids++;
257 #-> Update auction with latest.
260 $jobRecord->currentBid = $newBid;
261 $jobRecord->currentBidPrice = $newBid->amount;
262 if ($nextRule && $newBid->amount >= $nextRule->from)
264 $jobRecord->bidIncrement = $nextRule->amount;
265 $incr = $nextRule->amount;
270 if (time() > ($jobRecord->endDate->getTimestamp() - 300))
272 #-> Move end date out with 5 minutes.
273 $jobRecord->endDate = new \DateTime(
274 date('Y-m-d H:i:s', $jobRecord->endDate->getTimestamp() + 300)
276 error_log($jobRecord->endDate->getTimestamp());
280 $this->em->flush($atBid);
282 $this->em->flush($jobRecord);
285 $this->em->getConnection()->commit();
287 #-> Notify clients with updated auction data.
288 $vehicle = $jobRecord->stock->type->model->make->name
289 . ', ' . $jobRecord->stock->type->model->name
290 . ', ' . $jobRecord->stock->type->name
291 . ' (' . $jobRecord->stock->vehicleYear->name . ')';
294 #-> Update stock entry.
295 $jobRecord->stock->highestBid = $jobRecord->currentBidPrice;
298 #-> Chat to ape comet server.
299 \Utility\Comms\Ape::broadcast('LiveAuction', array(
300 'id' => $jobRecord->id,
301 'current_bid' => $jobRecord->currentBid->toArray(array(), array(), 1),
302 'current_bid_id' => $jobRecord->currentBid->id,
303 'current_bid_price' => $jobRecord->currentBidPrice,
304 'number_of_bids' => $jobRecord->numberOfBids,
305 'expire_datetime' => $jobRecord->endDate->format(\Utility\Definitions\Locale::getDateTimeFormat()),
306 'winner' => $newBid->company->id,
307 'looser' => !is_null($oldBid) ? $oldBid->company->id : 0,
308 'basket' => $basket->toArray(array(), array(), 1),
309 'vehicle' => $vehicle
313 #-> Outbid notification.
314 if ($newCurrentBid && $doNotify)
317 $currPrefix = \Utility\Definitions\Locale::getCurrencyPrefix() . ' ';
318 $oNotify = new \Utility\Comms\Notification();
319 $oNotify->sendFromTemplate(
321 $oldBid->company->id, $oldBid->profile->id,
322 null, $oldBid->profile->mobile,
326 'first_name' => $oldBid->profile->firstName,
327 'family_name' => $oldBid->profile->familyName,
328 'vehicle' => $vehicle,
329 'seller' => $jobRecord->company->name,
330 'price' => $currPrefix . ($jobRecord->currentBidPrice + $incr),
331 'bid' => $currPrefix . $oldBid->amount,
332 'auction_expiry_date' => $jobRecord->endDate->format(
333 \Utility\Definitions\Locale::getDateTimeFormat()
339 if ($newCurrentBid && $record->company->id != $newBid->company->id)
341 throw new \Exception('Your bid was placed but you were just outbid.');