Fixed some math.

This commit is contained in:
James Cole 2015-05-24 08:00:40 +02:00
parent 724db6c34c
commit 288546c2b9
12 changed files with 64 additions and 218 deletions

View File

@ -15,14 +15,14 @@ class Budget
{ {
/** @var Collection */ /** @var Collection */
protected $budgetLines; protected $budgetLines;
/** @var float */ /** @var string */
protected $budgeted = 0; protected $budgeted = '0';
/** @var float */ /** @var string */
protected $left = 0; protected $left = '0';
/** @var float */ /** @var string */
protected $overspent = 0; protected $overspent = '0';
/** @var float */ /** @var string */
protected $spent = 0; protected $spent = '0';
/** /**
* *
@ -45,7 +45,9 @@ class Budget
*/ */
public function addBudgeted($add) public function addBudgeted($add)
{ {
$this->budgeted += floatval($add); $add = strval(round($add, 2));
bcscale(2);
$this->budgeted = bcadd($this->budgeted, $add);
} }
/** /**
@ -53,7 +55,9 @@ class Budget
*/ */
public function addLeft($add) public function addLeft($add)
{ {
$this->left += floatval($add); $add = strval(round($add, 2));
bcscale(2);
$this->left = bcadd($this->left, $add);
} }
/** /**
@ -61,7 +65,9 @@ class Budget
*/ */
public function addOverspent($add) public function addOverspent($add)
{ {
$this->overspent += floatval($add); $add = strval(round($add, 2));
bcscale(2);
$this->overspent = bcadd($this->overspent, $add);
} }
/** /**
@ -69,7 +75,9 @@ class Budget
*/ */
public function addSpent($add) public function addSpent($add)
{ {
$this->spent += floatval($add); $add = strval(round($add, 2));
bcscale(2);
$this->spent = bcadd($this->spent, $add);
} }
/** /**
@ -81,7 +89,7 @@ class Budget
} }
/** /**
* @return float * @return string
*/ */
public function getBudgeted() public function getBudgeted()
{ {
@ -89,7 +97,7 @@ class Budget
} }
/** /**
* @param float $budgeted * @param string $budgeted
*/ */
public function setBudgeted($budgeted) public function setBudgeted($budgeted)
{ {
@ -97,7 +105,7 @@ class Budget
} }
/** /**
* @return float * @return string
*/ */
public function getLeft() public function getLeft()
{ {
@ -105,7 +113,7 @@ class Budget
} }
/** /**
* @param float $left * @param string $left
*/ */
public function setLeft($left) public function setLeft($left)
{ {
@ -113,7 +121,7 @@ class Budget
} }
/** /**
* @return float * @return string
*/ */
public function getOverspent() public function getOverspent()
{ {
@ -121,15 +129,15 @@ class Budget
} }
/** /**
* @param float $overspent * @param string $overspent
*/ */
public function setOverspent($overspent) public function setOverspent($overspent)
{ {
$this->overspent = $overspent; $this->overspent = strval(round($overspent, 2));
} }
/** /**
* @return float * @return string
*/ */
public function getSpent() public function getSpent()
{ {
@ -137,11 +145,11 @@ class Budget
} }
/** /**
* @param float $spent * @param string $spent
*/ */
public function setSpent($spent) public function setSpent($spent)
{ {
$this->spent = $spent; $this->spent = strval(round($spent, 2));
} }

View File

@ -24,8 +24,8 @@ class Category
/** @var Collection */ /** @var Collection */
protected $categories; protected $categories;
/** @var float */ /** @var string */
protected $total = 0; protected $total = '0';
/** /**
* *
@ -50,7 +50,9 @@ class Category
*/ */
public function addTotal($add) public function addTotal($add)
{ {
$this->total += floatval($add); $add = strval(round($add, 2));
bcscale(2);
$this->total = bcadd($this->total, $add);
} }
/** /**
@ -69,11 +71,11 @@ class Category
} }
/** /**
* @return float * @return string
*/ */
public function getTotal() public function getTotal()
{ {
return $this->total; return strval(round($this->total, 2));
} }

View File

@ -17,8 +17,8 @@ class Expense
{ {
/** @var Collection */ /** @var Collection */
protected $expenses; protected $expenses;
/** @var float */ /** @var string */
protected $total; protected $total = '0';
/** /**
* *
@ -37,14 +37,15 @@ class Expense
$accountId = $entry->account_id; $accountId = $entry->account_id;
if (!$this->expenses->has($accountId)) { if (!$this->expenses->has($accountId)) {
$newObject = new stdClass; $newObject = new stdClass;
$newObject->amount = floatval($entry->amount); $newObject->amount = strval(round($entry->amount, 2));
$newObject->name = $entry->name; $newObject->name = $entry->name;
$newObject->count = 1; $newObject->count = 1;
$newObject->id = $accountId; $newObject->id = $accountId;
$this->expenses->put($accountId, $newObject); $this->expenses->put($accountId, $newObject);
} else { } else {
$existing = $this->expenses->get($accountId); bcscale(2);
$existing->amount += floatval($entry->amount); $existing = $this->expenses->get($accountId);
$existing->amount = bcadd($existing->amount, $entry->amount);
$existing->count++; $existing->count++;
$this->expenses->put($accountId, $existing); $this->expenses->put($accountId, $existing);
} }
@ -55,7 +56,9 @@ class Expense
*/ */
public function addToTotal($add) public function addToTotal($add)
{ {
$this->total += floatval($add); $add = strval(round($add, 2));
bcscale(2);
$this->total = bcadd($this->total, $add);
} }
/** /**
@ -73,10 +76,10 @@ class Expense
} }
/** /**
* @return float * @return string
*/ */
public function getTotal() public function getTotal()
{ {
return $this->total; return strval(round($this->total, 2));
} }
} }

View File

@ -38,14 +38,15 @@ class Income
$accountId = $entry->account_id; $accountId = $entry->account_id;
if (!$this->incomes->has($accountId)) { if (!$this->incomes->has($accountId)) {
$newObject = new stdClass; $newObject = new stdClass;
$newObject->amount = floatval($entry->amount); $newObject->amount = strval(round($entry->amount, 2));
$newObject->name = $entry->name; $newObject->name = $entry->name;
$newObject->count = 1; $newObject->count = 1;
$newObject->id = $accountId; $newObject->id = $accountId;
$this->incomes->put($accountId, $newObject); $this->incomes->put($accountId, $newObject);
} else { } else {
bcscale(2);
$existing = $this->incomes->get($accountId); $existing = $this->incomes->get($accountId);
$existing->amount += floatval($entry->amount); $existing->amount = bcadd($existing->amount, $entry->amount);
$existing->count++; $existing->count++;
$this->incomes->put($accountId, $existing); $this->incomes->put($accountId, $existing);
} }
@ -56,7 +57,9 @@ class Income
*/ */
public function addToTotal($add) public function addToTotal($add)
{ {
$this->total += floatval($add); $add = strval(round($add, 2));
bcscale(2);
$this->total = bcadd($this->total, $add);
} }
/** /**
@ -78,7 +81,7 @@ class Income
*/ */
public function getTotal() public function getTotal()
{ {
return $this->total; return strval(round($this->total, 2));
} }

View File

@ -151,24 +151,6 @@ class Account extends Model
return $value; return $value;
} }
/**
* @param $value
*
* @codeCoverageIgnore
* @return float|int
*/
public function getVirtualBalanceAttribute($value)
{
// if (is_null($this->virtual_balance_encrypted)) {
// return $value;
// }
// $value = intval(Crypt::decrypt($this->virtual_balance_encrypted));
// $value = $value / 100;
return $value;
}
/** /**
* @codeCoverageIgnore * @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\HasMany * @return \Illuminate\Database\Eloquent\Relations\HasMany
@ -229,10 +211,7 @@ class Account extends Model
*/ */
public function setVirtualBalanceAttribute($value) public function setVirtualBalanceAttribute($value)
{ {
// $value = intval($value * 100); $this->attributes['virtual_balance'] = strval(round($value, 2));
// $this->attributes['virtual_balance_encrypted'] = Crypt::encrypt($value);
// $this->attributes['virtual_balance'] = ($value / 100);
$this->attributes['virtual_balance'] = round($value, 2);
} }
/** /**

View File

@ -17,39 +17,6 @@ class Bill extends Model
protected $hidden = ['amount_min_encrypted', 'amount_max_encrypted', 'name_encrypted', 'match_encrypted']; protected $hidden = ['amount_min_encrypted', 'amount_max_encrypted', 'name_encrypted', 'match_encrypted'];
/**
* @param $value
*
* @return float|int
*/
public function getAmountMaxAttribute($value)
{
// if (is_null($this->amount_max_encrypted)) {
// return $value;
// }
// $value = intval(Crypt::decrypt($this->amount_max_encrypted));
// $value = $value / 100;
return $value;
}
/**
*
* @param $value
*
* @return float|int
*/
public function getAmountMinAttribute($value)
{
// if (is_null($this->amount_min_encrypted)) {
// return $value;
// }
// $value = intval(Crypt::decrypt($this->amount_min_encrypted));
// $value = $value / 100;
return $value;
}
/** /**
* @return array * @return array
*/ */
@ -93,11 +60,7 @@ class Bill extends Model
*/ */
public function setAmountMaxAttribute($value) public function setAmountMaxAttribute($value)
{ {
// save in cents: $this->attributes['amount_max'] = strval(round($value, 2));
// $value = intval($value * 100);
// $this->attributes['amount_max_encrypted'] = Crypt::encrypt($value);
// $this->attributes['amount_max'] = ($value / 100);
$this->attributes['amount_max'] = round($value, 2);
} }
/** /**
@ -105,11 +68,7 @@ class Bill extends Model
*/ */
public function setAmountMinAttribute($value) public function setAmountMinAttribute($value)
{ {
// save in cents: $this->attributes['amount_min'] = strval(round($value, 2));
// $value = intval($value * 100);
// $this->attributes['amount_min_encrypted'] = Crypt::encrypt($value);
// $this->attributes['amount_min'] = ($value / 100);
$this->attributes['amount_min'] = round($value, 2);
} }
/** /**

View File

@ -21,22 +21,6 @@ class BudgetLimit extends Model
return $this->belongsTo('FireflyIII\Models\Budget'); return $this->belongsTo('FireflyIII\Models\Budget');
} }
/**
* @param $value
*
* @return float|int
*/
public function getAmountAttribute($value)
{
// if (is_null($this->amount_encrypted)) {
// return $value;
// }
// $value = intval(Crypt::decrypt($this->amount_encrypted));
// $value = $value / 100;
return $value;
}
/** /**
* @return array * @return array
*/ */
@ -58,11 +42,7 @@ class BudgetLimit extends Model
*/ */
public function setAmountAttribute($value) public function setAmountAttribute($value)
{ {
// save in cents: $this->attributes['amount'] = strval(round($value, 2));
// $value = intval($value * 100);
// $this->attributes['amount_encrypted'] = Crypt::encrypt($value);
// $this->attributes['amount'] = ($value / 100);
$this->attributes['amount'] = round($value, 2);
} }
} }

View File

@ -22,22 +22,6 @@ class LimitRepetition extends Model
return $this->belongsTo('FireflyIII\Models\BudgetLimit'); return $this->belongsTo('FireflyIII\Models\BudgetLimit');
} }
/**
* @param $value
*
* @return float|int
*/
public function getAmountAttribute($value)
{
// if (is_null($this->amount_encrypted)) {
// return $value;
// }
// $value = intval(Crypt::decrypt($this->amount_encrypted));
// $value = $value / 100;
return $value;
}
/** /**
* @return array * @return array
*/ */
@ -51,11 +35,7 @@ class LimitRepetition extends Model
*/ */
public function setAmountAttribute($value) public function setAmountAttribute($value)
{ {
// save in cents: $this->attributes['amount'] = strval(round($value, 2));
// $value = intval($value * 100);
// $this->attributes['amount_encrypted'] = Crypt::encrypt($value);
// $this->attributes['amount'] = ($value / 100);
$this->attributes['amount'] = round($value, 2);
} }
} }

View File

@ -89,22 +89,6 @@ class PiggyBank extends Model
return intval($value) == 1; return intval($value) == 1;
} }
/**
* @param $value
*
* @return float|int
*/
public function getTargetamountAttribute($value)
{
// if (is_null($this->targetamount_encrypted)) {
// return $value;
// }
// $value = intval(Crypt::decrypt($this->targetamount_encrypted));
// $value = $value / 100;
return $value;
}
/** /**
* @return \Illuminate\Database\Eloquent\Relations\HasMany * @return \Illuminate\Database\Eloquent\Relations\HasMany
*/ */
@ -136,10 +120,6 @@ class PiggyBank extends Model
*/ */
public function setTargetamountAttribute($value) public function setTargetamountAttribute($value)
{ {
// save in cents: $this->attributes['targetamount'] = strval(round($value, 2));
// $value = intval($value * 100);
// $this->attributes['targetamount_encrypted'] = Crypt::encrypt($value);
// $this->attributes['targetamount'] = ($value / 100);
$this->attributes['targetamount'] = round($value, 2);
} }
} }

View File

@ -15,22 +15,6 @@ class PiggyBankEvent extends Model
protected $fillable = ['piggy_bank_id', 'transaction_journal_id', 'date', 'amount']; protected $fillable = ['piggy_bank_id', 'transaction_journal_id', 'date', 'amount'];
protected $hidden = ['amount_encrypted']; protected $hidden = ['amount_encrypted'];
/**
* @param $value
*
* @return float|int
*/
public function getAmountAttribute($value)
{
// if (is_null($this->amount_encrypted)) {
// return $value;
// }
// $value = intval(Crypt::decrypt($this->amount_encrypted));
// $value = $value / 100;
return $value;
}
/** /**
* @return array * @return array
*/ */
@ -52,11 +36,7 @@ class PiggyBankEvent extends Model
*/ */
public function setAmountAttribute($value) public function setAmountAttribute($value)
{ {
// save in cents: $this->attributes['amount'] = strval(round($value, 2));
// $value = intval($value * 100);
// $this->attributes['amount_encrypted'] = Crypt::encrypt($value);
// $this->attributes['amount'] = ($value / 100);
$this->attributes['amount'] = round($value, 2);
} }
/** /**

View File

@ -16,22 +16,6 @@ class PiggyBankRepetition extends Model
protected $fillable = ['piggy_bank_id', 'startdate', 'targetdate', 'currentamount']; protected $fillable = ['piggy_bank_id', 'startdate', 'targetdate', 'currentamount'];
protected $hidden = ['currentamount_encrypted']; protected $hidden = ['currentamount_encrypted'];
/**
* @param $value
*
* @return float|int
*/
public function getCurrentamountAttribute($value)
{
// if (is_null($this->currentamount_encrypted)) {
// return $value;
// }
// $value = intval(Crypt::decrypt($this->currentamount_encrypted));
// $value = $value / 100;
return $value;
}
/** /**
* @return array * @return array
*/ */
@ -88,11 +72,7 @@ class PiggyBankRepetition extends Model
*/ */
public function setCurrentamountAttribute($value) public function setCurrentamountAttribute($value)
{ {
// save in cents: $this->attributes['currentamount'] = strval(round($value, 2));
// $value = intval($value * 100);
// $this->attributes['currentamount_encrypted'] = Crypt::encrypt($value);
// $this->attributes['currentamount'] = ($value / 100);
$this->attributes['currentamount'] = round($value, 2);
} }
} }

View File

@ -42,12 +42,6 @@ class Transaction extends Model
public function getAmountAttribute($value) public function getAmountAttribute($value)
{ {
return $value; return $value;
// if (is_null($this->amount_encrypted)) {
// return $value;
// }
// $value = floatval(Crypt::decrypt($this->amount_encrypted));
//
// return $value;
} }
/** /**
@ -85,9 +79,7 @@ class Transaction extends Model
*/ */
public function setAmountAttribute($value) public function setAmountAttribute($value)
{ {
// save in cents: $this->attributes['amount'] = strval(round($value, 2));
// $this->attributes['amount_encrypted'] = Crypt::encrypt($value);
$this->attributes['amount'] = round($value, 2);
} }
/** /**