Example #1
0
 /**
  * create a new auction listing in souk.
  * pass in an array or a listing object.
  * You can combine any of these parameters to create a listing:
  *    $listing = $souk->auction( array(
  *                                   'price'=>10, 
  *                                   'item_id'=>32, 
  *                                   'quantity'=>5, 
  *                                   'bid'=>100,
  *                                   'step'=>10,
  *                                   'price'=>1000,
  *                                   'expires'=>currentTime() + 86400) );
  *
  * @return   Souk\Listing
  */
 public function auction($l, array $data = NULL)
 {
     // wrap in try catch so we can handle db transactions
     try {
         // create a transaction if we don't have one already.
         Transaction::start();
         // validate the listing
         $listing = Souk\Util::validateListing($l);
         // if no seller passed in, use the current user id.
         if (!isset($listing->seller)) {
             $listing->seller = $this->user();
         }
         // make sure the seller is a positive int.
         if (!Souk\Util::validatePositiveInteger($listing->seller)) {
             throw new Exception('seller invalid', $listing->seller);
         }
         // write the listing into the db.
         $this->createListing($listing);
         // commit transaction if it was created internally.
         Transaction::commit();
         // all done.
         return $listing;
         // something bad happened.
     } catch (Exception $e) {
         // roll back the transaction.
         Transaction::rollback();
         // wrap the exception in our own exception so we can control the message.
         $e = new Exception('cannot auction: ' . $e->getMessage(), $e->__toString());
         // throw the exception up the chain.
         throw $e;
     }
 }
 /**
  * @see Stockpile_Interface::subtract();
  */
 public function subtract($item_id, $quantity = 1, array $data = NULL)
 {
     Transaction::start();
     try {
         if (!$quantity instanceof Stockpile_HybridQuantity) {
             $quantity = $this->get($item_id)->grab($quantity);
         }
         if ($quantity->value() < 1) {
             throw $this->handle(new Stockpile_Exception('cannot subtract: invalid quantity', $quantity));
         }
         if ($quantity->tally() > 0) {
             $tally = $this->core->subtract($item_id, $quantity->tally(), $data);
         } else {
             $tally = $this->core->get($item_id, $with_lock = TRUE);
         }
         if (count($quantity->all()) > 0) {
             $serial = $this->serial->subtract($item_id, $quantity->all(), $data);
         } else {
             $serial = $this->serial->get($item_id);
         }
         if (!Transaction::commit()) {
             throw new Exception('database error');
         }
         return $this->quantity(array('tally' => $tally, 'serial' => $serial));
     } catch (\Exception $e) {
         if (Transaction::inProgress()) {
             Transaction::rollback();
         }
         $e = new Exception('cannot subtract: ' . $e->getMessage(), $e->__toString());
         throw $e;
     }
 }