<?php
/* rgbBalance is a class for maintaining the balance, i.e. the bank account balance in rgb units.
*/
class rgbBalance {
var $balanceId;
var $balanceDateTime; # moment of last change in DB
var $balanceRedValue;
var $balanceGreenValue;
var $balanceBlueValue;
var $balancelastTransactionId;
var $balanceUserId;
var $balanceType; # system or live
function rgbBalance() {
$this->balanceId = null;
$this->balanceDateTime = null;
$this->balanceRedValue = new rgbDimension;
$this->balanceGreenValue = new rgbDimension;
$this->balanceBlueValue = new rgbDimension;
$this->balanceUserId = null;
$this->balanceType = null;
}
function insert() {
// we need the UserId and a a balance type else we dont have a job
if (!isset($this->balanceUserId) || !isset($this->balanceType)) {
return false;
}
$myDb = $GLOBALS['db'];
$sql = sprintf("insert into rgbBalances (
balanceUserId, balanceType) values (
%s, '%s')",
$this->balanceUserId, $this->balanceType
);
$q = mysql_query($sql,$myDb);
if(!$q) {
pc_debug("mysql error: $sql ". mysql_error() ,__FILE__,__LINE__);
return false;
}
pc_debug("balance insert done; $sql",__FILE__,__LINE__);
}
function loadBalance() {
global $db;
// if (!is_numeric($this->getUserid())) return false;
$sql = sprintf("select balanceId, balanceDateTime, balanceRedValue, balanceGreenValue, balanceBlueValue,
balanceLastTransferId, balanceUserId, balanceType from rgbBalances
where balanceUserId = '%s'
and balanceType = '%s'",
$this->getUserId(),
"running"
);
if(! $q = mysql_query($sql,$db)) {
pc_debug("mysql error: $sql". mysql_error(),__FILE__,__LINE__);
}
$res = mysql_fetch_assoc($q);
$this->balanceId = $res["balanceId"];
$this->balanceDateTime = $res["balanceDateTime"];
$this->balanceRedValue->setValue($res["balanceRedValue"]);
$this->balanceGreenValue->setValue($res["balanceGreenValue"]);
$this->balanceBlueValue->setValue($res["balanceBlueValue"]);
$this->balanceUserId = $res["balanceUserId"];
$this->balanceType = $res["balanceType"];
pc_debug("loaded balance " . $this->balanceId . "with $sql. " , __FILE__,__LINE__ );
return true;
}
function getType() {
return $this->balanceType;
}
function setType($myType) {
$this->balanceType = $myType;
}
function getUserId() {
return $this->balanceUserId;
}
function setUserId($myId) {
$this->balanceUserId = $myId;
}
/* garbage
* function getBalanceRedValue() {
* return (float)$this->balanceRedValue;
* }
* function setBalanceRedValue($value) {
* $this->balanceRedValue = $value;
* }
* function getBalanceRedRoundedValue() {
* // always round to floor, fraction is added after.
* return (int) round($this->getBalanceRedValue());
* }
* function getBalanceRedFraction() {
* $fraction = $this->getBalanceRedValue() - $this->getBalanceRedRoundedValue();
* return (float) $fraction;
* }
* function getBalanceRed8th() {
* return (string) $this->getBalanceRedFraction() * 8 . "/8";
* }
*
* function getBalanceGreenValue() {
* return (float)$this->balanceGreenValue;
* }
*
* function setBalanceGreenValue($value) {
* $this->balanceGreenValue = $value;
* }
*
* function getBalanceGreenRoundedValue() {
* // always round to floor, fraction is added after.
* return (int) round($this->getBalanceGreenValue());
* }
* function getBalanceGreenFraction() {
* $fraction = $this->getBalanceGreenValue() - $this->getBalanceGreenRoundedValue();
* return (float) $fraction;
* }
* function getBalanceGreen8th() {
* return (string) $this->getBalanceGreenFraction() * 8 . "/8";
* }
*
*
*
* function getBalanceBlueValue() {
* return (float)$this->balanceBlueValue;
* }
*
* function setBalanceBlueValue($value) {
* $this->balanceBlueValue = $value;
* }
*
* function getBalanceBlueRoundedValue() {
* // always round to floor, fraction is added after.
* return (int) round($this->getBalanceBlueValue());
* }
* function getBalanceBlueFraction() {
* $fraction = $this->getBalanceBlueValue() - $this->getBalanceBlueRoundedValue();
* return (float) $fraction;
* }
* function getBalanceBlue8th() {
* return (string) $this->getBalanceBlueFraction() * 8 . "/8";
* }
*/
function getBalanceDateTime() {
return $this->balanceDateTime;
}
function showBalanceDateTime() {
$dt = strtotime($this->getBalanceDateTime());
return strftime('%d %b %G,%H:%M ',$dt);
}
}