From 288546c2b966a1e3ef32a06886e095696d1e10d1 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 24 May 2015 08:00:40 +0200 Subject: [PATCH] Fixed some math. --- app/Helpers/Collection/Budget.php | 52 +++++++++++++++++------------ app/Helpers/Collection/Category.php | 12 ++++--- app/Helpers/Collection/Expense.php | 19 ++++++----- app/Helpers/Collection/Income.php | 11 +++--- app/Models/Account.php | 23 +------------ app/Models/Bill.php | 45 ++----------------------- app/Models/BudgetLimit.php | 22 +----------- app/Models/LimitRepetition.php | 22 +----------- app/Models/PiggyBank.php | 22 +----------- app/Models/PiggyBankEvent.php | 22 +----------- app/Models/PiggyBankRepetition.php | 22 +----------- app/Models/Transaction.php | 10 +----- 12 files changed, 64 insertions(+), 218 deletions(-) diff --git a/app/Helpers/Collection/Budget.php b/app/Helpers/Collection/Budget.php index dd609f39f6..53548291de 100644 --- a/app/Helpers/Collection/Budget.php +++ b/app/Helpers/Collection/Budget.php @@ -15,14 +15,14 @@ class Budget { /** @var Collection */ protected $budgetLines; - /** @var float */ - protected $budgeted = 0; - /** @var float */ - protected $left = 0; - /** @var float */ - protected $overspent = 0; - /** @var float */ - protected $spent = 0; + /** @var string */ + protected $budgeted = '0'; + /** @var string */ + protected $left = '0'; + /** @var string */ + protected $overspent = '0'; + /** @var string */ + protected $spent = '0'; /** * @@ -45,7 +45,9 @@ class Budget */ 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) { - $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) { - $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) { - $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() { @@ -89,7 +97,7 @@ class Budget } /** - * @param float $budgeted + * @param string $budgeted */ public function setBudgeted($budgeted) { @@ -97,7 +105,7 @@ class Budget } /** - * @return float + * @return string */ public function getLeft() { @@ -105,7 +113,7 @@ class Budget } /** - * @param float $left + * @param string $left */ public function setLeft($left) { @@ -113,7 +121,7 @@ class Budget } /** - * @return float + * @return string */ public function getOverspent() { @@ -121,15 +129,15 @@ class Budget } /** - * @param float $overspent + * @param string $overspent */ public function setOverspent($overspent) { - $this->overspent = $overspent; + $this->overspent = strval(round($overspent, 2)); } /** - * @return float + * @return string */ public function getSpent() { @@ -137,11 +145,11 @@ class Budget } /** - * @param float $spent + * @param string $spent */ public function setSpent($spent) { - $this->spent = $spent; + $this->spent = strval(round($spent, 2)); } diff --git a/app/Helpers/Collection/Category.php b/app/Helpers/Collection/Category.php index 07a4ce7296..8de8733a9a 100644 --- a/app/Helpers/Collection/Category.php +++ b/app/Helpers/Collection/Category.php @@ -24,8 +24,8 @@ class Category /** @var Collection */ protected $categories; - /** @var float */ - protected $total = 0; + /** @var string */ + protected $total = '0'; /** * @@ -50,7 +50,9 @@ class Category */ 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() { - return $this->total; + return strval(round($this->total, 2)); } diff --git a/app/Helpers/Collection/Expense.php b/app/Helpers/Collection/Expense.php index 4a0098679a..a66c1ef86d 100644 --- a/app/Helpers/Collection/Expense.php +++ b/app/Helpers/Collection/Expense.php @@ -17,8 +17,8 @@ class Expense { /** @var Collection */ protected $expenses; - /** @var float */ - protected $total; + /** @var string */ + protected $total = '0'; /** * @@ -37,14 +37,15 @@ class Expense $accountId = $entry->account_id; if (!$this->expenses->has($accountId)) { $newObject = new stdClass; - $newObject->amount = floatval($entry->amount); + $newObject->amount = strval(round($entry->amount, 2)); $newObject->name = $entry->name; $newObject->count = 1; $newObject->id = $accountId; $this->expenses->put($accountId, $newObject); } else { - $existing = $this->expenses->get($accountId); - $existing->amount += floatval($entry->amount); + bcscale(2); + $existing = $this->expenses->get($accountId); + $existing->amount = bcadd($existing->amount, $entry->amount); $existing->count++; $this->expenses->put($accountId, $existing); } @@ -55,7 +56,9 @@ class Expense */ 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() { - return $this->total; + return strval(round($this->total, 2)); } } diff --git a/app/Helpers/Collection/Income.php b/app/Helpers/Collection/Income.php index eaf0568bc4..9dd0e279d7 100644 --- a/app/Helpers/Collection/Income.php +++ b/app/Helpers/Collection/Income.php @@ -38,14 +38,15 @@ class Income $accountId = $entry->account_id; if (!$this->incomes->has($accountId)) { $newObject = new stdClass; - $newObject->amount = floatval($entry->amount); + $newObject->amount = strval(round($entry->amount, 2)); $newObject->name = $entry->name; $newObject->count = 1; $newObject->id = $accountId; $this->incomes->put($accountId, $newObject); } else { + bcscale(2); $existing = $this->incomes->get($accountId); - $existing->amount += floatval($entry->amount); + $existing->amount = bcadd($existing->amount, $entry->amount); $existing->count++; $this->incomes->put($accountId, $existing); } @@ -56,7 +57,9 @@ class Income */ 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() { - return $this->total; + return strval(round($this->total, 2)); } diff --git a/app/Models/Account.php b/app/Models/Account.php index 23ee5500da..6d1a51f108 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -151,24 +151,6 @@ class Account extends Model 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 * @return \Illuminate\Database\Eloquent\Relations\HasMany @@ -229,10 +211,7 @@ class Account extends Model */ public function setVirtualBalanceAttribute($value) { - // $value = intval($value * 100); - // $this->attributes['virtual_balance_encrypted'] = Crypt::encrypt($value); - // $this->attributes['virtual_balance'] = ($value / 100); - $this->attributes['virtual_balance'] = round($value, 2); + $this->attributes['virtual_balance'] = strval(round($value, 2)); } /** diff --git a/app/Models/Bill.php b/app/Models/Bill.php index 934249da37..c11110142c 100644 --- a/app/Models/Bill.php +++ b/app/Models/Bill.php @@ -17,39 +17,6 @@ class Bill extends Model 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 */ @@ -93,11 +60,7 @@ class Bill extends Model */ public function setAmountMaxAttribute($value) { - // save in cents: - // $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); + $this->attributes['amount_max'] = strval(round($value, 2)); } /** @@ -105,11 +68,7 @@ class Bill extends Model */ public function setAmountMinAttribute($value) { - // save in cents: - // $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); + $this->attributes['amount_min'] = strval(round($value, 2)); } /** diff --git a/app/Models/BudgetLimit.php b/app/Models/BudgetLimit.php index 5a5bbe77d5..c945701ad2 100644 --- a/app/Models/BudgetLimit.php +++ b/app/Models/BudgetLimit.php @@ -21,22 +21,6 @@ class BudgetLimit extends Model 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 */ @@ -58,11 +42,7 @@ class BudgetLimit extends Model */ public function setAmountAttribute($value) { - // save in cents: - // $value = intval($value * 100); - // $this->attributes['amount_encrypted'] = Crypt::encrypt($value); - // $this->attributes['amount'] = ($value / 100); - $this->attributes['amount'] = round($value, 2); + $this->attributes['amount'] = strval(round($value, 2)); } } diff --git a/app/Models/LimitRepetition.php b/app/Models/LimitRepetition.php index e4a7a6ea7b..4cadd5e466 100644 --- a/app/Models/LimitRepetition.php +++ b/app/Models/LimitRepetition.php @@ -22,22 +22,6 @@ class LimitRepetition extends Model 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 */ @@ -51,11 +35,7 @@ class LimitRepetition extends Model */ public function setAmountAttribute($value) { - // save in cents: - // $value = intval($value * 100); - // $this->attributes['amount_encrypted'] = Crypt::encrypt($value); - // $this->attributes['amount'] = ($value / 100); - $this->attributes['amount'] = round($value, 2); + $this->attributes['amount'] = strval(round($value, 2)); } } diff --git a/app/Models/PiggyBank.php b/app/Models/PiggyBank.php index ee15a8c294..18e4251d76 100644 --- a/app/Models/PiggyBank.php +++ b/app/Models/PiggyBank.php @@ -89,22 +89,6 @@ class PiggyBank extends Model 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 */ @@ -136,10 +120,6 @@ class PiggyBank extends Model */ public function setTargetamountAttribute($value) { - // save in cents: - // $value = intval($value * 100); - // $this->attributes['targetamount_encrypted'] = Crypt::encrypt($value); - // $this->attributes['targetamount'] = ($value / 100); - $this->attributes['targetamount'] = round($value, 2); + $this->attributes['targetamount'] = strval(round($value, 2)); } } diff --git a/app/Models/PiggyBankEvent.php b/app/Models/PiggyBankEvent.php index 089022e00f..5cf2498889 100644 --- a/app/Models/PiggyBankEvent.php +++ b/app/Models/PiggyBankEvent.php @@ -15,22 +15,6 @@ class PiggyBankEvent extends Model protected $fillable = ['piggy_bank_id', 'transaction_journal_id', 'date', 'amount']; 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 */ @@ -52,11 +36,7 @@ class PiggyBankEvent extends Model */ public function setAmountAttribute($value) { - // save in cents: - // $value = intval($value * 100); - // $this->attributes['amount_encrypted'] = Crypt::encrypt($value); - // $this->attributes['amount'] = ($value / 100); - $this->attributes['amount'] = round($value, 2); + $this->attributes['amount'] = strval(round($value, 2)); } /** diff --git a/app/Models/PiggyBankRepetition.php b/app/Models/PiggyBankRepetition.php index 636a3a1675..fed4dfd490 100644 --- a/app/Models/PiggyBankRepetition.php +++ b/app/Models/PiggyBankRepetition.php @@ -16,22 +16,6 @@ class PiggyBankRepetition extends Model protected $fillable = ['piggy_bank_id', 'startdate', 'targetdate', 'currentamount']; 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 */ @@ -88,11 +72,7 @@ class PiggyBankRepetition extends Model */ public function setCurrentamountAttribute($value) { - // save in cents: - // $value = intval($value * 100); - // $this->attributes['currentamount_encrypted'] = Crypt::encrypt($value); - // $this->attributes['currentamount'] = ($value / 100); - $this->attributes['currentamount'] = round($value, 2); + $this->attributes['currentamount'] = strval(round($value, 2)); } } diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index ead89c4b36..0c2fe0ff04 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -42,12 +42,6 @@ class Transaction extends Model public function getAmountAttribute($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) { - // save in cents: - // $this->attributes['amount_encrypted'] = Crypt::encrypt($value); - $this->attributes['amount'] = round($value, 2); + $this->attributes['amount'] = strval(round($value, 2)); } /**