Example #1
0
 public function buy($sh_id, $qty = 0)
 {
     global $session;
     $database = new Database();
     $sh_id = $database->escape_value($sh_id);
     $qty = (int) $qty;
     if ($this->found) {
         $StockCompany = new StockCompany($this->sc_id);
         /*
         Check if the user has enough money to buy the stocks
         */
         $holder = new StockHolder($sh_id);
         if ($holder->found) {
             $stock_cur_price = Stock::getCurPrice($this->sc_id);
             if ($holder->cur_amt > $stock_cur_price * $qty) {
                 if ($StockCompany->leftStocks() > $qty) {
                     $holder_stocks = new HolderStocks($sh_id);
                     //this adds the stock to holder list and also creates a transaction for it
                     $success = $holder_stocks->addStock($this->sc_id, $qty, $stock_cur_price);
                     //Updates the amount of money holder has
                     $success = $holder->amtUpdate($holder->cur_amt - $stock_cur_price * $qty);
                     return true;
                 } else {
                     $session->message("Stocks of {$StockCompany->c_name} are unavailable in the marketplace");
                     return false;
                 }
             } else {
                 $session->message('You Don\'t have enough money to buy ' . $qty . ' stocks of ' . $StockCompany->c_name);
                 return false;
             }
         }
     } else {
         //check this!
         $session->message('Stock Unavailable');
         return false;
     }
 }