allow to search for balance for a account ID via transaction class, added template changes

This commit is contained in:
Sebastian Grewe 2013-05-11 23:46:39 +02:00
parent 961b6cc817
commit c31bbb6f93
4 changed files with 43 additions and 7 deletions

View File

@ -7,12 +7,12 @@ if (!defined('SECURITY'))
class Transaction { class Transaction {
private $sError = ''; private $sError = '';
private $table = 'transactions'; private $table = 'transactions';
// This defines each block private $tableBlocks = 'blocks';
public $account_id;
public function __construct($debug, $mysqli) { public function __construct($debug, $mysqli, $config) {
$this->debug = $debug; $this->debug = $debug;
$this->mysqli = $mysqli; $this->mysqli = $mysqli;
$this->config = $config;
$this->debug->append("Instantiated Ledger class", 2); $this->debug->append("Instantiated Ledger class", 2);
} }
@ -90,6 +90,36 @@ class Transaction {
} }
return true; return true;
} }
public function getBalance($account_id) {
$stmt = $this->mysqli->prepare("
SELECT IFNULL(c.credit - d.debit, 0) AS balance
FROM (
SELECT account_id, sum(t.amount) AS credit
FROM $this->table AS t
LEFT JOIN $this->tableBlocks AS b ON t.block_id = b.id
WHERE type = 'Credit'
AND b.confirmations > ?
AND account_id = ? ) AS c
LEFT JOIN (
SELECT account_id, sum(amount) AS debit
FROM $this->table
WHERE type IN ('Debit_MP','Debit_AP')
AND account_id = ? ) AS d
ON c.account_id = d.account_id
");
if ($this->checkStmt($stmt)) {
$stmt->bind_param("iii", $this->config['confirmations'], $account_id, $account_id);
if (!$stmt->execute()) {
$this->debug->append("Unable to execute statement: " . $stmt->error);
$this->setErrorMessage("Fetching balance failed");
}
$result = $stmt->get_result();
$stmt->close();
return $result->fetch_object()->balance;
}
return false;
}
} }
$transaction = new Transaction($debug, $mysqli); $transaction = new Transaction($debug, $mysqli, $config);

View File

@ -23,6 +23,7 @@ define('SALT', 'LJKEHFuhgu7%&¤Hg783tr7gf¤%¤fyegfredfoGHYFGYe(%/(&%6');
$config = array( $config = array(
'difficulty' => '31', // Target difficulty for this pool 'difficulty' => '31', // Target difficulty for this pool
'reward' => '50', // Reward for finding blocks 'reward' => '50', // Reward for finding blocks
'confirmations' => '120', // Confirmations per block found to credit transactions
'wallet' => array( 'wallet' => array(
'type' => 'http', // http or https are supported 'type' => 'http', // http or https are supported
'host' => 'localhost:9332', 'host' => 'localhost:9332',

View File

@ -6,6 +6,7 @@ if (!defined('SECURITY'))
// Globally available variables // Globally available variables
$debug->append('Global smarty variables', 3); $debug->append('Global smarty variables', 3);
$aGlobal = array( $aGlobal = array(
'userdata' => $_SESSION['USERDATA']['id'] ? $user->getUserData($_SESSION['USERDATA']['id']) : array(), 'userdata' => $_SESSION['USERDATA']['id'] ? $user->getUserData($_SESSION['USERDATA']['id']) : array(),
'slogan' => $settings->getValue('slogan'), 'slogan' => $settings->getValue('slogan'),
@ -17,6 +18,10 @@ $aGlobal = array(
'statstime' => $settings->getValue('statstime'), 'statstime' => $settings->getValue('statstime'),
'motd' => $settings->getValue('motd') 'motd' => $settings->getValue('motd')
); );
// Append additional user information not from user table
$aGlobal['userdata']['balance'] = $transaction->getBalance($_SESSION['USERDATA']['id']);
// Make it available in Smarty
$smarty->assign('PATH', 'site_assets/' . THEME); $smarty->assign('PATH', 'site_assets/' . THEME);
$smarty->assign('GLOBAL', $aGlobal); $smarty->assign('GLOBAL', $aGlobal);
?> ?>

View File

@ -73,13 +73,13 @@ if (!empty($action)) {
$smarty->assign("PAGE", $page); $smarty->assign("PAGE", $page);
$smarty->assign("ACTION", $action); $smarty->assign("ACTION", $action);
// Now with all loaded and processed, setup some globals we need for smarty templates
require_once(INCLUDE_DIR . '/smarty_globals.inc.php');
// Debguger // Debguger
$debug->append("Loading debug information into template", 4); $debug->append("Loading debug information into template", 4);
$smarty->assign('DebuggerInfo', $debug->getDebugInfo()); $smarty->assign('DebuggerInfo', $debug->getDebugInfo());
// Now with all loaded and processed, setup some globals we need for smarty templates
require_once(INCLUDE_DIR . '/smarty_globals.inc.php');
// Display our page // Display our page
if (!@$supress_master) if (!@$supress_master)
$smarty->display("master.tpl", md5(serialize($_REQUEST))); $smarty->display("master.tpl", md5(serialize($_REQUEST)));