2 namespace Auction\Service;
10 class AutoBid extends \Auction\DataBin\AutoBid
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)
39 * ExecuteAfter: Create.
40 * Handle bidding details.
42 * @param object|null $jobRecord
43 * @param object|null $record
44 * @param \Workspace\Utility\ServiceInputParams $contract
47 public function updateStatus($meta, $auction, $record, \Workspace\Utility\ServiceInputParams $contract)
49 #-> Collect some info.
51 ->getRepository('\Auction\Entity\Basket')
53 'auction' => $auction->id,
54 'company' => $record->company->id
58 $basket = new \Auction\Entity\Basket();
59 $basket->fromArray(array(
60 'auction' => $auction,
61 'company' => $record->company,
62 'profile' => $record->profile
64 $this->em->persist($basket);
65 $this->em->flush($basket);
67 elseif (true == $basket->archived)
69 $basket->archived = false;
70 $this->em->flush($basket);
72 if ($auction->reservePrice > $record->amount)
74 $record->status = 'Archived';
75 $this->em->flush($record);
76 throw new \Exception('Your bid was placed but you were just outbid.');
79 $newCurrentBid = false;
84 #-> Lockdown some working space.
86 $this->em->getConnection()->beginTransaction();
87 $rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder($this->em);
88 $rsm->addRootEntityFromClassMetadata('\Auction\Entity\Auction', 'auction');
89 $query = $this->em->createNativeQuery("SELECT * FROM auction WHERE id = :id FOR UPDATE", $rsm);
90 $query->setParameter("id", $auction->id);
91 $jobRecord = $query->getOneOrNullResult();
92 $cBidId = $jobRecord->currentBid
93 ? $jobRecord->currentBid->id
95 $aBidId = $jobRecord->currentBid && $jobRecord->currentBid->autoBid
96 ? $jobRecord->currentBid->autoBid->id
98 $record = $this->em->find('\Auction\Entity\AutoBid', $record->id);
101 if ('Active' != $jobRecord->jobState
102 || time() > $jobRecord->endDate->getTimestamp())
104 $record->status = 'Archived';
105 $this->em->flush($record);
106 $this->em->getConnection()->commit();
107 throw new \Exception('Your bid could not be placed. This auction item have been closed.');
112 $base = $jobRecord->currentBidPrice != 0.0
113 ? $jobRecord->currentBidPrice
114 : $jobRecord->reservePrice;
115 $nextRule = $this->_getNextIncrement($base);
116 $incr = $jobRecord->bidIncrement;
119 // $jobRecord->numberOfBids++;
120 //$record->amount = floor($record->amount / $incr) * $incr;
121 //$record->amount = ($record->amount / $incr) * $incr;
122 if ($nextRule && $base >= $nextRule->from)
124 $jobRecord->bidIncrement = $nextRule->amount;
125 $incr = $nextRule->amount;
126 $nextRule = $this->_getNextIncrement($base);
129 if (is_null($jobRecord->currentBid))
131 #-> First bid on this item.
132 $atBid = new \Auction\Entity\Bid();
133 $atBid->fromArray(array(
134 'auction' => $jobRecord,
135 'company' => \Utility\Registry::resolveCompanyContext(null),
136 'profile' => \Utility\Registry::resolveProfileContext(null),
137 'autoBid' => $record,
138 'amount' => $jobRecord->reservePrice + $incr
140 $this->em->persist($atBid);
141 $this->em->flush($atBid);
142 $newCurrentBid = true;
144 $jobRecord->numberOfBids++;
146 elseif ($jobRecord->currentBidPrice + $incr >= $record->amount
147 || $jobRecord->currentBid->company->id == $record->company->id)
149 #-> Late arrival or useless hack.
150 $record->status = 'Archived';
151 $this->em->flush($record);
152 if ($jobRecord->currentBid->company->id == $record->company->id)
154 throw new \Exception('Your bid was not successfull. Your dealership already has the winning bid.');
158 throw new \Exception('Your bid was too low, please place a bid higher than R' . ($jobRecord->currentBidPrice + $incr));
162 // $jobRecord->numberOfBids++;
166 #-> Competing bids, figure it out.
168 if (!is_null($jobRecord->currentBid->autoBid))
170 #-> Auto-bid against existing auto-bid.
171 $price = $jobRecord->currentBidPrice;
172 $prevBid = $jobRecord->currentBid->autoBid->amount;
173 $newBid = $record->amount;
175 $bidder = 0; // 0:prev auto, 1:new auto
176 //error_log('Prev max: ' . $prevBid);
177 //error_log('New max: ' . $newBid);
181 $bidder = (0 == $bidder)
184 /* error_log((0 == $bidder)
185 ? 'Previous bidder bids @ ' . $price
186 : 'New bidder bids @ ' . $price); */
187 $autoMax = (0 == $bidder)
190 if ($price > $autoMax)
193 //error_log('somebody is over max');
196 if (1 == $bidder || $price <= $prevBid)
198 //error_log('Prev wins');
199 /* $atBid = new \Auction\Entity\Bid();
200 $atBid->fromArray(array(
201 'auction' => $jobRecord,
202 'company' => $jobRecord->currentBid->company,
203 'profile' => $jobRecord->currentBid->profile,
204 'autoBid' => $jobRecord->currentBid->autoBid,
207 $this->em->persist($atBid);
208 $this->em->flush($atBid); */
209 $newCurrentBid = true;
212 $record->status = 'Archived';
213 $this->em->flush($record);
217 //error_log('New wins');
218 /* $atBid = new \Auction\Entity\Bid();
219 $atBid->fromArray(array(
220 'auction' => $jobRecord,
221 'company' => $record->company,
222 'profile' => $record->profile,
223 'autoBid' => $record,
226 $this->em->persist($atBid);
227 $this->em->flush($atBid); */
228 $newCurrentBid = true;
230 $oldBid = $jobRecord->currentBid;
231 $jobRecord->currentBid->autoBid->status = 'Archived';
232 $jobRecord->currentBid->status = 'Archived';
233 // $jobRecord->numberOfBids++;
234 $this->em->flush($jobRecord->currentBid->autoBid);
235 $this->em->flush($jobRecord->currentBid);
243 $atBid = new \Auction\Entity\Bid();
244 $atBid->fromArray(array(
245 'auction' => $jobRecord,
246 'company' => $record->company,
247 'profile' => $record->profile,
248 'autoBid' => $record,
251 $this->em->persist($atBid);
252 $this->em->flush($atBid);
257 $atBid = new \Auction\Entity\Bid();
258 $atBid->fromArray(array(
259 'auction' => $jobRecord,
260 'company' => $jobRecord->currentBid->company,
261 'profile' => $jobRecord->currentBid->profile,
262 'autoBid' => $jobRecord->currentBid->autoBid,
265 $this->em->persist($atBid);
266 $this->em->flush($atBid);
269 $jobRecord->numberOfBids++;
272 if ($nextRule && $price >= $nextRule->from)
274 $jobRecord->bidIncrement = $nextRule->amount;
275 $incr = $nextRule->amount;
276 $nextRule = $this->_getNextIncrement($price);
277 //$prevBid = floor($jobRecord->currentBid->autoBid->amount / $incr) * $incr;
278 $prevBid = $jobRecord->currentBid->autoBid->amount;
279 //$newBid = floor($record->amount / $incr) * $incr;
280 $newBid = $record->amount;
281 // $jobRecord->numberOfBids++;
288 #-> Auto-bid against bid.
289 $price = $jobRecord->currentBidPrice + $incr;
290 if ($record->amount >= $price)
292 $atBid = new \Auction\Entity\Bid();
293 $atBid->fromArray(array(
294 'auction' => $jobRecord,
295 'company' => $record->company,
296 'profile' => $record->profile,
297 'autoBid' => $record,
300 $this->em->persist($atBid);
301 $this->em->flush($atBid);
302 $newCurrentBid = true;
304 $oldBid = $jobRecord->currentBid;
305 $jobRecord->currentBid->status = 'Archived';
306 $this->em->flush($jobRecord->currentBid);
307 $jobRecord->numberOfBids++;
312 $record->status = 'Archived';
313 $this->em->flush($record);
316 // $jobRecord->numberOfBids++;
320 #-> Update auction with latest.
323 $jobRecord->currentBid = $newBid;
324 $jobRecord->currentBidPrice = $newBid->amount;
325 if ($nextRule && $newBid->amount >= $nextRule->from)
327 $jobRecord->bidIncrement = $nextRule->amount;
328 $incr = $nextRule->amount;
333 if (time() > ($jobRecord->endDate->getTimestamp() - 300))
335 #-> Move end date out with 5 minutes.
336 $jobRecord->endDate = new \DateTime(
337 date('Y-m-d H:i:s', $jobRecord->endDate->getTimestamp() + 300)
339 error_log($jobRecord->endDate->getTimestamp());
341 $this->em->flush($jobRecord);
344 $this->em->getConnection()->commit();
345 //\Utility\Doctrine::unlockTables();
347 #-> Notify clients with updated auction data.
348 $vehicle = $jobRecord->stock->type->model->make->name
349 . ', ' . $jobRecord->stock->type->model->name
350 . ', ' . $jobRecord->stock->type->name
351 . ' (' . $jobRecord->stock->vehicleYear->name . ')';
354 #-> Update stock entry.
355 $jobRecord->stock->highestBid = $jobRecord->currentBidPrice;
358 #-> Chat to ape comet server.
359 \Utility\Comms\Ape::broadcast('LiveAuction', array(
360 'id' => $jobRecord->id,
361 'current_bid' => $jobRecord->currentBid->toArray(array(), array(), 1),
362 'current_bid_id' => $jobRecord->currentBid->id,
363 'current_bid_price' => $jobRecord->currentBidPrice,
364 'number_of_bids' => $jobRecord->numberOfBids,
365 'expire_datetime' => $jobRecord->endDate->format(\Utility\Definitions\Locale::getDateTimeFormat()),
366 'winner' => $newBid->company->id,
367 'looser' => !is_null($oldBid) ? $oldBid->company->id : 0,
368 'basket' => $basket->toArray(array(), array(), 1),
369 'vehicle' => $vehicle
373 #-> Outbid notification.
374 if ($newCurrentBid && $doNotify)
377 $currPrefix = \Utility\Definitions\Locale::getCurrencyPrefix() . ' ';
378 $oNotify = new \Utility\Comms\Notification();
379 $oNotify->sendFromTemplate(
381 $oldBid->company->id, $oldBid->profile->id,
382 null, $oldBid->profile->mobile,
386 'first_name' => $oldBid->profile->firstName,
387 'family_name' => $oldBid->profile->familyName,
388 'vehicle' => $vehicle,
389 'seller' => $jobRecord->company->name,
390 'price' => $currPrefix . ($jobRecord->currentBidPrice + $incr),
391 'bid' => $currPrefix . $oldBid->amount,
392 'auction_expiry_date' => $jobRecord->endDate->format(
393 \Utility\Definitions\Locale::getDateTimeFormat()
399 if ($newCurrentBid && $record->company->id != $newBid->company->id)
401 throw new \Exception('Your bid was placed but you were just outbid.');