Fixed some math things.

This commit is contained in:
James Cole
2015-07-26 19:42:28 +02:00
parent eef28d96f4
commit e211c9812e
9 changed files with 28 additions and 18 deletions

View File

@@ -38,7 +38,7 @@ class ChartJsBillChartGenerator implements BillChartGenerator
/** @var Bill $entry */ /** @var Bill $entry */
foreach ($unpaid as $entry) { // loop unpaid: foreach ($unpaid as $entry) { // loop unpaid:
$description = $entry[0]->name . ' (' . $entry[1]->format('jS M Y') . ')'; $description = $entry[0]->name . ' (' . $entry[1]->format('jS M Y') . ')';
$amount = ($entry[0]->amount_max + $entry[0]->amount_min) / 2; $amount = bcdiv(bcadd($entry[0]->amount_max, $entry[0]->amount_min), 2);
$unpaidDescriptions[] = $description; $unpaidDescriptions[] = $description;
$unpaidAmount = bcadd($unpaidAmount, $amount); $unpaidAmount = bcadd($unpaidAmount, $amount);
unset($amount, $description); unset($amount, $description);

View File

@@ -44,7 +44,7 @@ class GoogleBillChartGenerator implements BillChartGenerator
/** @var Bill $entry */ /** @var Bill $entry */
foreach ($unpaid as $entry) { foreach ($unpaid as $entry) {
$description = $entry[0]->name . ' (' . $entry[1]->format('jS M Y') . ')'; $description = $entry[0]->name . ' (' . $entry[1]->format('jS M Y') . ')';
$amount = ($entry[0]->amount_max + $entry[0]->amount_min) / 2; $amount = bcdiv(bcadd($entry[0]->amount_max, $entry[0]->amount_min), 2);
$unpaidDescriptions[] = $description; $unpaidDescriptions[] = $description;
$unpaidAmount = bcadd($unpaidAmount, $amount); $unpaidAmount = bcadd($unpaidAmount, $amount);
unset($amount, $description); unset($amount, $description);

View File

@@ -49,10 +49,12 @@ class UpdateJournalConnection
if (is_null($repetition)) { if (is_null($repetition)) {
return; return;
} }
$amount = $journal->amount; bcscale(2);
$diff = $amount - $event->amount; // update current repetition
$repetition->currentamount += $diff; $amount = $journal->amount;
$diff = bcsub($amount, $event->amount); // update current repetition
$repetition->currentamount = bcadd($repetition->currentamount, $diff);
$repetition->save(); $repetition->save();

View File

@@ -79,7 +79,7 @@ class ReportHelper implements ReportHelperInterface
foreach ($accounts as $account) { foreach ($accounts as $account) {
$start = bcadd($start, $account->startBalance); $start = bcadd($start, $account->startBalance);
$end = bcadd($end, $account->endBalance); $end = bcadd($end, $account->endBalance);
$diff = bcadd($diff, ($account->endBalance - $account->startBalance)); $diff = bcadd($diff, bcsub($account->endBalance, $account->startBalance));
} }
$object = new AccountCollection; $object = new AccountCollection;
@@ -255,6 +255,8 @@ class ReportHelper implements ReportHelperInterface
$repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); $repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
$set = $repository->getBudgets(); $set = $repository->getBudgets();
bcscale(2);
foreach ($set as $budget) { foreach ($set as $budget) {
$repetitions = $repository->getBudgetLimitRepetitions($budget, $start, $end); $repetitions = $repository->getBudgetLimitRepetitions($budget, $start, $end);
@@ -277,9 +279,9 @@ class ReportHelper implements ReportHelperInterface
$budgetLine->setBudget($budget); $budgetLine->setBudget($budget);
$budgetLine->setRepetition($repetition); $budgetLine->setRepetition($repetition);
$expenses = $repository->spentInPeriodCorrected($budget, $repetition->startdate, $repetition->enddate, $shared); $expenses = $repository->spentInPeriodCorrected($budget, $repetition->startdate, $repetition->enddate, $shared);
$left = $expenses < $repetition->amount ? $repetition->amount - $expenses : 0; $left = $expenses < $repetition->amount ? bcsub($repetition->amount, $expenses) : 0;
$spent = $expenses > $repetition->amount ? 0 : $expenses; $spent = $expenses > $repetition->amount ? 0 : $expenses;
$overspent = $expenses > $repetition->amount ? $expenses - $repetition->amount : 0; $overspent = $expenses > $repetition->amount ? bcsub($expenses, $repetition->amount) : 0;
$budgetLine->setLeft($left); $budgetLine->setLeft($left);
$budgetLine->setSpent($spent); $budgetLine->setSpent($spent);

View File

@@ -161,7 +161,7 @@ class JsonController extends Controller
} }
/** @var Bill $entry */ /** @var Bill $entry */
foreach ($unpaid as $entry) { foreach ($unpaid as $entry) {
$current = ($entry[0]->amount_max + $entry[0]->amount_min) / 2; $current = bcdiv(bcadd($entry[0]->amount_max, $entry[0]->amount_min), 2);
$amount = bcadd($amount, $current); $amount = bcadd($amount, $current);
} }

View File

@@ -47,10 +47,11 @@ class PiggyBankController extends Controller
*/ */
public function add(AccountRepositoryInterface $repository, PiggyBank $piggyBank) public function add(AccountRepositoryInterface $repository, PiggyBank $piggyBank)
{ {
bcscale(2);
$date = Session::get('end', Carbon::now()->endOfMonth()); $date = Session::get('end', Carbon::now()->endOfMonth());
$leftOnAccount = $repository->leftOnAccount($piggyBank->account, $date); $leftOnAccount = $repository->leftOnAccount($piggyBank->account, $date);
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount; $savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
$leftToSave = $piggyBank->targetamount - $savedSoFar; $leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = min($leftOnAccount, $leftToSave); $maxAmount = min($leftOnAccount, $leftToSave);
return view('piggy-banks.add', compact('piggyBank', 'maxAmount')); return view('piggy-banks.add', compact('piggyBank', 'maxAmount'));
@@ -173,7 +174,7 @@ class PiggyBankController extends Controller
foreach ($piggyBanks as $piggyBank) { foreach ($piggyBanks as $piggyBank) {
$piggyBank->savedSoFar = round($piggyBank->currentRelevantRep()->currentamount, 2); $piggyBank->savedSoFar = round($piggyBank->currentRelevantRep()->currentamount, 2);
$piggyBank->percentage = $piggyBank->savedSoFar != 0 ? intval($piggyBank->savedSoFar / $piggyBank->targetamount * 100) : 0; $piggyBank->percentage = $piggyBank->savedSoFar != 0 ? intval($piggyBank->savedSoFar / $piggyBank->targetamount * 100) : 0;
$piggyBank->leftToSave = $piggyBank->targetamount - $piggyBank->savedSoFar; $piggyBank->leftToSave = bcsub($piggyBank->targetamount, $piggyBank->savedSoFar);
/* /*
* Fill account information: * Fill account information:
@@ -211,7 +212,7 @@ class PiggyBankController extends Controller
if (is_array($data)) { if (is_array($data)) {
foreach ($data as $order => $id) { foreach ($data as $order => $id) {
$repository->setOrder(intval($id), (intval($order) + 1)); $repository->setOrder(intval($id), ($order + 1));
} }
} }
} }
@@ -225,13 +226,13 @@ class PiggyBankController extends Controller
*/ */
public function postAdd(PiggyBankRepositoryInterface $repository, AccountRepositoryInterface $accounts, PiggyBank $piggyBank) public function postAdd(PiggyBankRepositoryInterface $repository, AccountRepositoryInterface $accounts, PiggyBank $piggyBank)
{ {
bcscale(2);
$amount = round(Input::get('amount'), 2); $amount = round(Input::get('amount'), 2);
$date = Session::get('end', Carbon::now()->endOfMonth()); $date = Session::get('end', Carbon::now()->endOfMonth());
$leftOnAccount = $accounts->leftOnAccount($piggyBank->account, $date); $leftOnAccount = $accounts->leftOnAccount($piggyBank->account, $date);
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount; $savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
$leftToSave = $piggyBank->targetamount - $savedSoFar; $leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = round(min($leftOnAccount, $leftToSave), 2); $maxAmount = round(min($leftOnAccount, $leftToSave), 2);
bcscale(2);
if ($amount <= $maxAmount) { if ($amount <= $maxAmount) {
$repetition = $piggyBank->currentRelevantRep(); $repetition = $piggyBank->currentRelevantRep();

View File

@@ -246,10 +246,11 @@ class TransactionController extends Controller
*/ */
public function show(JournalRepositoryInterface $repository, TransactionJournal $journal) public function show(JournalRepositoryInterface $repository, TransactionJournal $journal)
{ {
bcscale(2);
$journal->transactions->each( $journal->transactions->each(
function (Transaction $t) use ($journal, $repository) { function (Transaction $t) use ($journal, $repository) {
$t->before = $repository->getAmountBefore($journal, $t); $t->before = $repository->getAmountBefore($journal, $t);
$t->after = $t->before + $t->amount; $t->after = bcadd($t->before, $t->amount);
} }
); );
$what = strtolower($journal->transactionType->type); $what = strtolower($journal->transactionType->type);

View File

@@ -236,6 +236,7 @@ class AccountRepository implements AccountRepositoryInterface
if (count($ids) > 0) { if (count($ids) > 0) {
$accounts = Auth::user()->accounts()->whereIn('id', $ids)->get(); $accounts = Auth::user()->accounts()->whereIn('id', $ids)->get();
} }
bcscale(2);
$accounts->each( $accounts->each(
function (Account $account) use ($start, $end) { function (Account $account) use ($start, $end) {
@@ -249,7 +250,7 @@ class AccountRepository implements AccountRepositoryInterface
// sum of piggy bank amounts on this account: // sum of piggy bank amounts on this account:
// diff between endBalance and piggyBalance. // diff between endBalance and piggyBalance.
// then, percentage. // then, percentage.
$difference = $account->endBalance - $account->piggyBalance; $difference = bcsub($account->endBalance, $account->piggyBalance);
$account->difference = $difference; $account->difference = $difference;
$account->percentage = $difference != 0 && $account->endBalance != 0 ? round((($difference / $account->endBalance) * 100)) : 100; $account->percentage = $difference != 0 && $account->endBalance != 0 ? round((($difference / $account->endBalance) * 100)) : 100;
@@ -277,13 +278,15 @@ class AccountRepository implements AccountRepositoryInterface
$start = clone Session::get('start', new Carbon); $start = clone Session::get('start', new Carbon);
$end = clone Session::get('end', new Carbon); $end = clone Session::get('end', new Carbon);
bcscale(2);
$accounts->each( $accounts->each(
function (Account $account) use ($start, $end) { function (Account $account) use ($start, $end) {
$account->startBalance = Steam::balance($account, $start); $account->startBalance = Steam::balance($account, $start);
$account->endBalance = Steam::balance($account, $end); $account->endBalance = Steam::balance($account, $end);
// diff (negative when lost, positive when gained) // diff (negative when lost, positive when gained)
$diff = $account->endBalance - $account->startBalance; $diff = bcsub($account->endBalance, $account->startBalance);
if ($diff < 0 && $account->startBalance > 0) { if ($diff < 0 && $account->startBalance > 0) {
// percentage lost compared to start. // percentage lost compared to start.

View File

@@ -82,10 +82,11 @@ class PiggyBankPart
*/ */
public function percentage() public function percentage()
{ {
bcscale(2);
if ($this->getCurrentamount() < $this->getCumulativeAmount()) { if ($this->getCurrentamount() < $this->getCumulativeAmount()) {
$pct = 0; $pct = 0;
// calculate halfway point? // calculate halfway point?
if ($this->getCumulativeAmount() - $this->getCurrentamount() < $this->getAmountPerBar()) { if (bcsub($this->getCumulativeAmount(), $this->getCurrentamount()) < $this->getAmountPerBar()) {
$left = $this->getCurrentamount() % $this->getAmountPerBar(); $left = $this->getCurrentamount() % $this->getAmountPerBar();
$pct = round($left / $this->getAmountPerBar() * 100); $pct = round($left / $this->getAmountPerBar() * 100);
} }