This commit is contained in:
James Cole
2018-04-28 21:54:48 +02:00
parent 7f459df9e9
commit fdc9467218
7 changed files with 79 additions and 38 deletions

View File

@@ -104,6 +104,22 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
return bccomp($amount, $savedSoFar) <= 0;
}
/**
* Correct order of piggies in case of issues.
*/
public function correctOrder(): void
{
$set = $this->user->piggyBanks()->orderBy('order', 'ASC')->get();
$current = 1;
foreach ($set as $piggyBank) {
if ((int)$piggyBank->order !== $current) {
$piggyBank->order = $current;
$piggyBank->save();
}
$current++;
}
}
/**
* @param PiggyBank $piggyBank
* @param string $amount
@@ -327,7 +343,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
/** @var PiggyBank $current */
foreach ($piggies as $current) {
$repetition = $this->getRepetition($current);
if(null !== $repetition) {
if (null !== $repetition) {
$balance = bcsub($balance, $repetition->currentamount);
}
}
@@ -379,14 +395,10 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
*
* @return bool
*/
public function setOrder(int $piggyBankId, int $order): bool
public function setOrder(PiggyBank $piggyBank, int $order): bool
{
$piggyBank = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', $this->user->id)
->where('piggy_banks.id', $piggyBankId)->first(['piggy_banks.*']);
if ($piggyBank) {
$piggyBank->order = $order;
$piggyBank->save();
}
$piggyBank->order = $order;
$piggyBank->save();
return true;
}