Example #1
0
 public function sell($sh_id, $qty = 0)
 {
     global $session;
     $database = new Database();
     $sh_id = $database->escape_value($sh_id);
     $qty = (int) $qty;
     if ($this->isFound()) {
         $StockCompany = new StockCompany($this->sc_id);
         /*
         Check if the user has the stock or not
         */
         $holder_stocks = new HolderStocks($sh_id);
         $holder = new StockHolder($sh_id);
         if ($holder_stocks->isFound()) {
             if ($holder_stocks->hasStock($this->sc_id)) {
                 $index = array_search($this->sc_id, array_column($holder_stocks->holder_stocks, 'sc_id'));
                 $qty_having = $holder_stocks->holder_stocks[$index]['s_qty'];
                 if ($qty <= $qty_having) {
                     $stock_cur_price = Stock::getCurPrice($this->sc_id);
                     //This removes stocks from holder's list
                     $holder_stocks->removeStock($this->sc_id, $qty, $stock_cur_price);
                     //Updates the price of the holder
                     $holder->amtUpdate($holder->cur_amt + $stock_cur_price * $qty);
                     return true;
                 } else {
                     $session->message("You Dont have {$qty} stocks of {$StockCompany->c_name} to sell, You only have {$qty_having} stocks");
                     return false;
                 }
             } else {
                 $session->message("You Dont have {$StockCompany->c_name} stocks to sell");
                 return false;
             }
         } else {
             return false;
         }
     } else {
         return false;
     }
 }