public static function model($className = __CLASS__) { return parent::model($className); }
public function sell($baseSellAmount) { ignore_user_abort(true); if (session_status() === PHP_SESSION_NONE) { session_start(); } $db = UniversalConnect::doConnect(); date_default_timezone_set('Asia/Singapore'); $userkey = $_SESSION["userkey"]; $baseSellAmount = round($baseSellAmount, 2); if ($baseSellAmount <= 0) { return false; } $baseCurr = new BaseCurrency(); if ($baseSellAmount > $baseCurr->getAmount()) { $baseSellAmount = $baseCurr->getAmount(); } $addAmount = round($baseSellAmount * $this->buyValue, 2); $db->begin_transaction(); $query = "UPDATE wallet SET amount=amount+{$addAmount} WHERE currencyid={$this->id} AND userkey={$userkey};"; $db->query($query); $query = "UPDATE wallet SET amount=amount-{$baseSellAmount} WHERE currencyid=1 AND userkey={$userkey};"; $db->query($query); //transtype: 0 for sell (USD to JPY), 1 for buy (JPY to USD) $query = "INSERT INTO transactions (transtype, userkey, currencyid, amount, rate, receiveamt, time) VALUES (0, {$userkey}, {$this->id}, {$baseSellAmount}, {$this->buyValue}, {$addAmount}, " . time() . ");"; $db->query($query); if (!$db->commit()) { $db->rollback(); die("An error occurred during transaction. Please try again later. Technical details: " . $db->error); } //calculates net worth $totalvalue = 0.0; $query = "SELECT wallet.amount, currency.sellvalue FROM wallet INNER JOIN currency ON currency.currencyid=wallet.currencyid WHERE userkey={$userkey}"; $result2 = $db->query($query) or die($db->error); while ($row2 = $result2->fetch_assoc()) { $totalvalue += round($row2["amount"] / $row2["sellvalue"], 4); } $totalvalue = round($totalvalue, 2); $query = "UPDATE users SET networth={$totalvalue} WHERE userkey={$userkey}"; $db->query($query); $db->close(); return true; }
/** * Returns a peer instance associated with this om. * * Since Peer classes are not to have any instance attributes, this method returns the * same instance for all member of this class. The method could therefore * be static, but this would prevent one from overriding the behavior. * * @return CurrencyPeer */ public function getPeer() { if (self::$peer === null) { self::$peer = new CurrencyPeer(); } return self::$peer; }