/** * Constructor * Find the balance from the most recent transaction. * If no balance is found, create an initial transaction. * * @param object &$db Database * @param integer $uid User ID * @return void */ public function __construct(&$db, $uid) { $this->_db = $db; $this->uid = $uid; $BA = new Account($this->_db); if ($BA->load_uid($this->uid)) { $this->balance = $BA->balance; $this->earnings = $BA->earnings; $this->credit = $BA->credit; } else { // no points are given initially $this->balance = 0; $this->earnings = 0; $this->credit = 0; $this->_saveBalance('creation'); } }
/** * Remove all user blog entries for the given user ID * * Method is called after user data is deleted from the database * * @param array $user Holds the user data * @param boolean $success True if user was succesfully stored in the database * @param string $msg Message * @return boolean */ public function onMemberAfterDelete($user, $success, $msg) { if (!$success) { return false; } $userId = \Hubzero\Utility\Arr::getValue($user, 'id', 0, 'int'); if ($userId) { try { $entry = \Hubzero\Bank\Account::oneByUserId($user['id']); if (!$entry->destroy()) { throw new Exception($entry->getError()); } $transactions = \Hubzero\Bank\Transaction::all()->whereEquals('uid', $user['id']); foreach ($transactions->rows() as $row) { if (!$row->destroy()) { throw new Exception($row->getError()); } } } catch (Exception $e) { return false; } } return true; }
/** * Save the current balance * * @param string $type Record type (inserting or updating) * @return boolean True on success */ public function _saveBalance($type) { if ($type == 'creation') { $model = Account::blank(); } else { $model = Account::oneByUserId($this->uid); } $model->set(['uid' => $this->uid, 'balance' => $this->balance, 'earnings' => $this->earnings, 'credit' => $this->credit]); if (!$model->save()) { $this->setError($model->getError()); return false; } return true; }
/** * Save an entry * * @return void */ public function saveTask() { // Check for request forgeries Request::checkToken(); $account = Request::getVar('account', array(), 'post'); $row = \Hubzero\Bank\Account::blank()->set($account); $row->set('uid', intval($row->get('uid'))); $row->set('balance', intval($row->get('balance'))); $row->set('earnings', intval($row->get('earnings'))); $data = Request::getVar('transaction', array(), 'post'); if (isset($data['amount']) && intval($data['amount']) > 0) { $data['uid'] = $row->get('uid'); $data['created'] = Date::toSql(); $data['amount'] = intval($data['amount']); if (!isset($data['category']) || !$data['category']) { $data['category'] = 'general'; } if (!isset($data['description']) || !$data['description']) { $data['description'] = 'Reason unspecified'; } if (!isset($data['type']) || !$data['type']) { $data['type'] = ''; } switch ($data['type']) { case 'withdraw': $row->balance -= $data['amount']; break; case 'deposit': $row->balance += $data['amount']; $row->earnings += $data['amount']; break; case 'creation': $row->balance = $data['amount']; $row->earnings = $data['amount']; break; } $data['balance'] = $row->balance; $BT = Transaction::blank()->set($data); if (!$BT->save()) { App::abort(500, $row->getError()); return; } } if (!$row->save()) { App::abort(500, $row->getError()); return; } App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller . '&task=edit&uid=' . $row->uid, false), Lang::txt('User info saved')); }