Rename fields for piggy bank

This commit is contained in:
James Cole
2024-11-30 16:02:30 +01:00
parent 92190bbc54
commit 21a6927279
15 changed files with 79 additions and 79 deletions

View File

@@ -114,7 +114,7 @@ class PiggyBankController extends Controller
'%s (%s / %s)', '%s (%s / %s)',
$piggy->name, $piggy->name,
app('amount')->formatAnything($currency, $currentAmount, false), app('amount')->formatAnything($currency, $currentAmount, false),
app('amount')->formatAnything($currency, $piggy->targetamount, false), app('amount')->formatAnything($currency, $piggy->target_amount, false),
), ),
'currency_id' => (string)$currency->id, 'currency_id' => (string)$currency->id,
'currency_name' => $currency->name, 'currency_name' => $currency->name,

View File

@@ -173,7 +173,7 @@ class CorrectAmounts extends Command
/** @var PiggyBankRepetition $item */ /** @var PiggyBankRepetition $item */
foreach ($set as $item) { foreach ($set as $item) {
$item->currentamount = app('steam')->positive($item->current_amount); $item->current_amount = app('steam')->positive($item->current_amount);
$item->save(); $item->save();
} }
$this->friendlyInfo(sprintf('Corrected %d piggy bank repetition amount(s).', $count)); $this->friendlyInfo(sprintf('Corrected %d piggy bank repetition amount(s).', $count));
@@ -191,7 +191,7 @@ class CorrectAmounts extends Command
/** @var PiggyBank $item */ /** @var PiggyBank $item */
foreach ($set as $item) { foreach ($set as $item) {
$item->targetamount = app('steam')->positive($item->target_amount); $item->target_amount = app('steam')->positive($item->target_amount);
$item->save(); $item->save();
} }
$this->friendlyInfo(sprintf('Corrected %d piggy bank amount(s).', $count)); $this->friendlyInfo(sprintf('Corrected %d piggy bank amount(s).', $count));

View File

@@ -128,7 +128,7 @@ class ApplyRules extends Command
$ruleEngine->addOperator(['type' => 'account_id', 'value' => $list]); $ruleEngine->addOperator(['type' => 'account_id', 'value' => $list]);
// add the date as a filter: // add the date as a filter:
$ruleEngine->addOperator(['type' => 'date_after', 'value' => $this->startDate->format('Y-m-d')]); $ruleEngine->addOperator(['type' => 'date_after', 'value' => $this->start_date->format('Y-m-d')]);
$ruleEngine->addOperator(['type' => 'date_before', 'value' => $this->endDate->format('Y-m-d')]); $ruleEngine->addOperator(['type' => 'date_before', 'value' => $this->endDate->format('Y-m-d')]);
// start running rules. // start running rules.
@@ -296,7 +296,7 @@ class ApplyRules extends Command
[$inputEnd, $inputStart] = [$inputStart, $inputEnd]; [$inputEnd, $inputStart] = [$inputStart, $inputEnd];
} }
$this->startDate = $inputStart; $this->start_date = $inputStart;
$this->endDate = $inputEnd; $this->endDate = $inputEnd;
} }

View File

@@ -37,10 +37,10 @@ class PiggyBankObserver
app('log')->debug('Observe "created" of a piggy bank.'); app('log')->debug('Observe "created" of a piggy bank.');
$repetition = new PiggyBankRepetition(); $repetition = new PiggyBankRepetition();
$repetition->piggyBank()->associate($piggyBank); $repetition->piggyBank()->associate($piggyBank);
$repetition->start_date = $piggyBank->startdate; $repetition->start_date = $piggyBank->start_date;
$repetition->start_date_tz = $piggyBank->startdate->format('e'); $repetition->start_date_tz = $piggyBank->start_date->format('e');
$repetition->target_date = $piggyBank->targetdate; $repetition->target_date = $piggyBank->target_date;
$repetition->target_date_tz = $piggyBank->targetdate?->format('e'); $repetition->target_date_tz = $piggyBank->target_date?->format('e');
$repetition->current_amount = '0'; $repetition->current_amount = '0';
$repetition->save(); $repetition->save();
} }

View File

@@ -75,7 +75,7 @@ class PiggyBankController extends Controller
$locale = app('steam')->getLocale(); $locale = app('steam')->getLocale();
// get first event or start date of piggy bank or today // get first event or start date of piggy bank or today
$startDate = $piggyBank->startdate ?? today(config('app.timezone')); $startDate = $piggyBank->start_date ?? today(config('app.timezone'));
/** @var null|PiggyBankEvent $firstEvent */ /** @var null|PiggyBankEvent $firstEvent */
$firstEvent = $set->first(); $firstEvent = $set->first();

View File

@@ -50,15 +50,15 @@ class FrontpageController extends Controller
if (1 === bccomp($amount, '0')) { if (1 === bccomp($amount, '0')) {
// percentage! // percentage!
$pct = 0; $pct = 0;
if (0 !== bccomp($piggyBank->targetamount, '0')) { if (0 !== bccomp($piggyBank->target_amount, '0')) {
$pct = (int)bcmul(bcdiv($amount, $piggyBank->targetamount), '100'); $pct = (int)bcmul(bcdiv($amount, $piggyBank->target_amount), '100');
} }
$entry = [ $entry = [
'id' => $piggyBank->id, 'id' => $piggyBank->id,
'name' => $piggyBank->name, 'name' => $piggyBank->name,
'amount' => $amount, 'amount' => $amount,
'target' => $piggyBank->targetamount, 'target' => $piggyBank->target_amount,
'percentage' => $pct, 'percentage' => $pct,
]; ];

View File

@@ -72,8 +72,8 @@ class AmountController extends Controller
$leftOnAccount = $this->piggyRepos->leftOnAccount($piggyBank, today(config('app.timezone'))); $leftOnAccount = $this->piggyRepos->leftOnAccount($piggyBank, today(config('app.timezone')));
$savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank); $savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank);
$maxAmount = $leftOnAccount; $maxAmount = $leftOnAccount;
if (0 !== bccomp($piggyBank->targetamount, '0')) { if (0 !== bccomp($piggyBank->target_amount, '0')) {
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar); $leftToSave = bcsub($piggyBank->target_amount, $savedSoFar);
$maxAmount = min($leftOnAccount, $leftToSave); $maxAmount = min($leftOnAccount, $leftToSave);
} }
$currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency(); $currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency();
@@ -94,8 +94,8 @@ class AmountController extends Controller
$savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank); $savedSoFar = $this->piggyRepos->getCurrentAmount($piggyBank);
$maxAmount = $leftOnAccount; $maxAmount = $leftOnAccount;
if (0 !== bccomp($piggyBank->targetamount, '0')) { if (0 !== bccomp($piggyBank->target_amount, '0')) {
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar); $leftToSave = bcsub($piggyBank->target_amount, $savedSoFar);
$maxAmount = min($leftOnAccount, $leftToSave); $maxAmount = min($leftOnAccount, $leftToSave);
} }
$currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency(); $currency = $this->accountRepos->getAccountCurrency($piggyBank->account) ?? app('amount')->getDefaultCurrency();

View File

@@ -77,8 +77,8 @@ class EditController extends Controller
$subTitleIcon = 'fa-pencil'; $subTitleIcon = 'fa-pencil';
$note = $piggyBank->notes()->first(); $note = $piggyBank->notes()->first();
// Flash some data to fill the form. // Flash some data to fill the form.
$targetDate = $piggyBank->targetdate?->format('Y-m-d'); $targetDate = $piggyBank->target_date?->format('Y-m-d');
$startDate = $piggyBank->startdate?->format('Y-m-d'); $startDate = $piggyBank->start_date?->format('Y-m-d');
$currency = $this->accountRepository->getAccountCurrency($piggyBank->account); $currency = $this->accountRepository->getAccountCurrency($piggyBank->account);
if (null === $currency) { if (null === $currency) {
$currency = app('amount')->getDefaultCurrency(); $currency = app('amount')->getDefaultCurrency();
@@ -87,13 +87,13 @@ class EditController extends Controller
$preFilled = [ $preFilled = [
'name' => $piggyBank->name, 'name' => $piggyBank->name,
'account_id' => $piggyBank->account_id, 'account_id' => $piggyBank->account_id,
'targetamount' => app('steam')->bcround($piggyBank->targetamount, $currency->decimal_places), 'targetamount' => app('steam')->bcround($piggyBank->target_amount, $currency->decimal_places),
'targetdate' => $targetDate, 'targetdate' => $targetDate,
'startdate' => $startDate, 'startdate' => $startDate,
'object_group' => null !== $piggyBank->objectGroups->first() ? $piggyBank->objectGroups->first()->title : '', 'object_group' => null !== $piggyBank->objectGroups->first() ? $piggyBank->objectGroups->first()->title : '',
'notes' => null === $note ? '' : $note->text, 'notes' => null === $note ? '' : $note->text,
]; ];
if (0 === bccomp($piggyBank->targetamount, '0')) { if (0 === bccomp($piggyBank->target_amount, '0')) {
$preFilled['targetamount'] = ''; $preFilled['targetamount'] = '';
} }
session()->flash('preFilled', $preFilled); session()->flash('preFilled', $preFilled);

View File

@@ -42,8 +42,8 @@ class PiggyBankRepetition extends Model
= [ = [
'created_at' => 'datetime', 'created_at' => 'datetime',
'updated_at' => 'datetime', 'updated_at' => 'datetime',
'startdate' => SeparateTimezoneCaster::class, 'start_date' => SeparateTimezoneCaster::class,
'targetdate' => SeparateTimezoneCaster::class, 'target_date' => SeparateTimezoneCaster::class,
'virtual_balance' => 'string', 'virtual_balance' => 'string',
]; ];
@@ -56,7 +56,7 @@ class PiggyBankRepetition extends Model
public function scopeOnDates(EloquentBuilder $query, Carbon $start, Carbon $target): EloquentBuilder public function scopeOnDates(EloquentBuilder $query, Carbon $start, Carbon $target): EloquentBuilder
{ {
return $query->where('startdate', $start->format('Y-m-d'))->where('targetdate', $target->format('Y-m-d')); return $query->where('start_date', $start->format('Y-m-d'))->where('target_date', $target->format('Y-m-d'));
} }
/** /**
@@ -66,14 +66,14 @@ class PiggyBankRepetition extends Model
{ {
return $query->where( return $query->where(
static function (EloquentBuilder $q) use ($date): void { static function (EloquentBuilder $q) use ($date): void {
$q->where('startdate', '<=', $date->format('Y-m-d 00:00:00')); $q->where('start_date', '<=', $date->format('Y-m-d 00:00:00'));
$q->orWhereNull('startdate'); $q->orWhereNull('start_date');
} }
) )
->where( ->where(
static function (EloquentBuilder $q) use ($date): void { static function (EloquentBuilder $q) use ($date): void {
$q->where('targetdate', '>=', $date->format('Y-m-d 00:00:00')); $q->where('target_date', '>=', $date->format('Y-m-d 00:00:00'));
$q->orWhereNull('targetdate'); $q->orWhereNull('target_date');
} }
) )
; ;
@@ -82,15 +82,15 @@ class PiggyBankRepetition extends Model
/** /**
* @param mixed $value * @param mixed $value
*/ */
public function setCurrentamountAttribute($value): void public function setCurrentAmountAttribute($value): void
{ {
$this->attributes['currentamount'] = (string)$value; $this->attributes['current_amount'] = (string)$value;
} }
/** /**
* Get the amount * Get the amount
*/ */
protected function currentamount(): Attribute protected function currentAmount(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: static fn ($value) => (string)$value, get: static fn ($value) => (string)$value,

View File

@@ -59,7 +59,7 @@ trait ModifiesPiggyBanks
if (null === $repetition) { if (null === $repetition) {
return false; return false;
} }
$repetition->currentamount = bcsub($repetition->currentamount, $amount); $repetition->current_amount = bcsub($repetition->current_amount, $amount);
$repetition->save(); $repetition->save();
app('log')->debug('addAmount [a]: Trigger change for negative amount.'); app('log')->debug('addAmount [a]: Trigger change for negative amount.');
@@ -74,8 +74,8 @@ trait ModifiesPiggyBanks
if (null === $repetition) { if (null === $repetition) {
return false; return false;
} }
$currentAmount = $repetition->currentamount ?? '0'; $currentAmount = $repetition->current_amount ?? '0';
$repetition->currentamount = bcadd($currentAmount, $amount); $repetition->current_amount = bcadd($currentAmount, $amount);
$repetition->save(); $repetition->save();
app('log')->debug('addAmount [b]: Trigger change for positive amount.'); app('log')->debug('addAmount [b]: Trigger change for positive amount.');
@@ -88,11 +88,11 @@ trait ModifiesPiggyBanks
{ {
$today = today(config('app.timezone')); $today = today(config('app.timezone'));
$leftOnAccount = $this->leftOnAccount($piggyBank, $today); $leftOnAccount = $this->leftOnAccount($piggyBank, $today);
$savedSoFar = $this->getRepetition($piggyBank)->currentamount; $savedSoFar = $this->getRepetition($piggyBank)->current_amount;
$maxAmount = $leftOnAccount; $maxAmount = $leftOnAccount;
$leftToSave = null; $leftToSave = null;
if (0 !== bccomp($piggyBank->targetamount, '0')) { if (0 !== bccomp($piggyBank->target_amount, '0')) {
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar); $leftToSave = bcsub($piggyBank->target_amount, $savedSoFar);
$maxAmount = 1 === bccomp($leftOnAccount, $leftToSave) ? $leftToSave : $leftOnAccount; $maxAmount = 1 === bccomp($leftOnAccount, $leftToSave) ? $leftToSave : $leftOnAccount;
} }
@@ -114,7 +114,7 @@ trait ModifiesPiggyBanks
if (null === $repetition) { if (null === $repetition) {
return false; return false;
} }
$savedSoFar = $repetition->currentamount; $savedSoFar = $repetition->current_amount;
return bccomp($amount, $savedSoFar) <= 0; return bccomp($amount, $savedSoFar) <= 0;
} }
@@ -143,12 +143,12 @@ trait ModifiesPiggyBanks
if (null === $repetition) { if (null === $repetition) {
return $piggyBank; return $piggyBank;
} }
$max = $piggyBank->targetamount; $max = $piggyBank->target_amount;
if (1 === bccomp($amount, $max) && 0 !== bccomp($piggyBank->targetamount, '0')) { if (1 === bccomp($amount, $max) && 0 !== bccomp($piggyBank->target_amount, '0')) {
$amount = $max; $amount = $max;
} }
$difference = bcsub($amount, $repetition->currentamount); $difference = bcsub($amount, $repetition->current_amount);
$repetition->currentamount = $amount; $repetition->current_amount = $amount;
$repetition->save(); $repetition->save();
if (-1 === bccomp($difference, '0')) { if (-1 === bccomp($difference, '0')) {
@@ -213,7 +213,7 @@ trait ModifiesPiggyBanks
// repetition is auto created. // repetition is auto created.
$repetition = $this->getRepetition($piggyBank); $repetition = $this->getRepetition($piggyBank);
if (null !== $repetition && array_key_exists('current_amount', $data) && '' !== $data['current_amount']) { if (null !== $repetition && array_key_exists('current_amount', $data) && '' !== $data['current_amount']) {
$repetition->currentamount = $data['current_amount']; $repetition->current_amount = $data['current_amount'];
$repetition->save(); $repetition->save();
} }
@@ -318,13 +318,13 @@ trait ModifiesPiggyBanks
// if the piggy bank is now smaller than the current relevant rep, // if the piggy bank is now smaller than the current relevant rep,
// remove money from the rep. // remove money from the rep.
$repetition = $this->getRepetition($piggyBank); $repetition = $this->getRepetition($piggyBank);
if (null !== $repetition && $repetition->currentamount > $piggyBank->targetamount && 0 !== bccomp($piggyBank->targetamount, '0')) { if (null !== $repetition && $repetition->current_amount > $piggyBank->target_amount && 0 !== bccomp($piggyBank->target_amount, '0')) {
$difference = bcsub($piggyBank->targetamount, $repetition->currentamount); $difference = bcsub($piggyBank->target_amount, $repetition->current_amount);
// an amount will be removed, create "negative" event: // an amount will be removed, create "negative" event:
event(new ChangedAmount($piggyBank, $difference, null, null)); event(new ChangedAmount($piggyBank, $difference, null, null));
$repetition->currentamount = $piggyBank->targetamount; $repetition->current_amount = $piggyBank->target_amount;
$repetition->save(); $repetition->save();
} }
@@ -370,18 +370,18 @@ trait ModifiesPiggyBanks
$piggyBank->account_id = (int)$data['account_id']; $piggyBank->account_id = (int)$data['account_id'];
} }
if (array_key_exists('targetamount', $data) && '' !== $data['targetamount']) { if (array_key_exists('targetamount', $data) && '' !== $data['targetamount']) {
$piggyBank->targetamount = $data['targetamount']; $piggyBank->target_amount = $data['targetamount'];
} }
if (array_key_exists('targetamount', $data) && '' === $data['targetamount']) { if (array_key_exists('targetamount', $data) && '' === $data['targetamount']) {
$piggyBank->targetamount = '0'; $piggyBank->target_amount = '0';
} }
if (array_key_exists('targetdate', $data) && '' !== $data['targetdate']) { if (array_key_exists('targetdate', $data) && '' !== $data['targetdate']) {
$piggyBank->targetdate = $data['targetdate']; $piggyBank->target_date = $data['targetdate'];
$piggyBank->targetdate_tz = $data['targetdate']?->format('e'); $piggyBank->target_date_tz = $data['targetdate']?->format('e');
} }
if (array_key_exists('startdate', $data)) { if (array_key_exists('startdate', $data)) {
$piggyBank->startdate = $data['startdate']; $piggyBank->start_date = $data['startdate'];
$piggyBank->startdate_tz = $data['targetdate']?->format('e'); $piggyBank->start_date_tz = $data['targetdate']?->format('e');
} }
$piggyBank->save(); $piggyBank->save();

View File

@@ -120,7 +120,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
return '0'; return '0';
} }
return $rep->currentamount; return $rep->current_amount;
} }
public function getRepetition(PiggyBank $piggyBank): ?PiggyBankRepetition public function getRepetition(PiggyBank $piggyBank): ?PiggyBankRepetition
@@ -200,10 +200,10 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
} }
app('log')->debug(sprintf('The currency is %s and the amount is %s', $currency->code, $amount)); app('log')->debug(sprintf('The currency is %s and the amount is %s', $currency->code, $amount));
$room = bcsub($piggyBank->targetamount, $repetition->currentamount); $room = bcsub($piggyBank->target_amount, $repetition->current_amount);
$compare = bcmul($repetition->currentamount, '-1'); $compare = bcmul($repetition->current_amount, '-1');
if (0 === bccomp($piggyBank->targetamount, '0')) { if (0 === bccomp($piggyBank->target_amount, '0')) {
// amount is zero? then the "room" is positive amount of we wish to add or remove. // amount is zero? then the "room" is positive amount of we wish to add or remove.
$room = app('steam')->positive($amount); $room = app('steam')->positive($amount);
app('log')->debug(sprintf('Room is now %s', $room)); app('log')->debug(sprintf('Room is now %s', $room));
@@ -223,7 +223,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
// amount is negative and $currentamount is smaller than $amount // amount is negative and $currentamount is smaller than $amount
if (-1 === bccomp($amount, '0') && 1 === bccomp($compare, $amount)) { if (-1 === bccomp($amount, '0') && 1 === bccomp($compare, $amount)) {
app('log')->debug(sprintf('Max amount to remove is %f', $repetition->currentamount)); app('log')->debug(sprintf('Max amount to remove is %f', $repetition->current_amount));
app('log')->debug(sprintf('Cannot remove %f from piggy bank #%d ("%s")', $amount, $piggyBank->id, $piggyBank->name)); app('log')->debug(sprintf('Cannot remove %f from piggy bank #%d ("%s")', $amount, $piggyBank->id, $piggyBank->name));
app('log')->debug(sprintf('New amount is %f', $compare)); app('log')->debug(sprintf('New amount is %f', $compare));
@@ -267,7 +267,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
/** @var PiggyBank $piggy */ /** @var PiggyBank $piggy */
foreach ($set as $piggy) { foreach ($set as $piggy) {
$currentAmount = $this->getRepetition($piggy)->currentamount ?? '0'; $currentAmount = $this->getRepetition($piggy)->current_amount ?? '0';
$piggy->name = $piggy->name.' ('.app('amount')->formatAnything($currency, $currentAmount, false).')'; $piggy->name = $piggy->name.' ('.app('amount')->formatAnything($currency, $currentAmount, false).')';
} }
@@ -298,11 +298,11 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
if (null === $repetition) { if (null === $repetition) {
return $savePerMonth; return $savePerMonth;
} }
if (null !== $piggyBank->targetdate && $repetition->currentamount < $piggyBank->targetamount) { if (null !== $piggyBank->target_date && $repetition->current_amount < $piggyBank->target_amount) {
$now = today(config('app.timezone')); $now = today(config('app.timezone'));
$startDate = null !== $piggyBank->startdate && $piggyBank->startdate->gte($now) ? $piggyBank->startdate : $now; $startDate = null !== $piggyBank->start_date && $piggyBank->start_date->gte($now) ? $piggyBank->start_date : $now;
$diffInMonths = (int)$startDate->diffInMonths($piggyBank->targetdate); $diffInMonths = (int)$startDate->diffInMonths($piggyBank->target_date);
$remainingAmount = bcsub($piggyBank->targetamount, $repetition->currentamount); $remainingAmount = bcsub($piggyBank->target_amount, $repetition->current_amount);
// more than 1 month to go and still need money to save: // more than 1 month to go and still need money to save:
if ($diffInMonths > 0 && 1 === bccomp($remainingAmount, '0')) { if ($diffInMonths > 0 && 1 === bccomp($remainingAmount, '0')) {
@@ -332,7 +332,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
foreach ($piggies as $current) { foreach ($piggies as $current) {
$repetition = $this->getRepetition($current); $repetition = $this->getRepetition($current);
if (null !== $repetition) { if (null !== $repetition) {
$balance = bcsub($balance, $repetition->currentamount); $balance = bcsub($balance, $repetition->current_amount);
} }
} }

View File

@@ -456,10 +456,10 @@ class ExportDataGenerator
$piggy->account->accountType->type, $piggy->account->accountType->type,
$piggy->name, $piggy->name,
$currency?->code, $currency?->code,
$piggy->targetamount, $piggy->target_amount,
$repetition?->currentamount, $repetition?->current_amount,
$piggy->startdate?->format('Y-m-d'), $piggy->start_date?->format('Y-m-d'),
$piggy->targetdate?->format('Y-m-d'), $piggy->target_date?->format('Y-m-d'),
$piggy->order, $piggy->order,
$piggy->active, $piggy->active,
]; ];

View File

@@ -178,15 +178,15 @@ class UpdatePiggybank implements ActionInterface
$repository->setUser($journal->user); $repository->setUser($journal->user);
// how much can we add to the piggy bank? // how much can we add to the piggy bank?
if (0 !== bccomp($piggyBank->targetamount, '0')) { if (0 !== bccomp($piggyBank->target_amount, '0')) {
$toAdd = bcsub($piggyBank->targetamount, $repository->getCurrentAmount($piggyBank)); $toAdd = bcsub($piggyBank->target_amount, $repository->getCurrentAmount($piggyBank));
app('log')->debug(sprintf('Max amount to add to piggy bank is %s, amount is %s', $toAdd, $amount)); app('log')->debug(sprintf('Max amount to add to piggy bank is %s, amount is %s', $toAdd, $amount));
// update amount to fit: // update amount to fit:
$amount = -1 === bccomp($amount, $toAdd) ? $amount : $toAdd; $amount = -1 === bccomp($amount, $toAdd) ? $amount : $toAdd;
app('log')->debug(sprintf('Amount is now %s', $amount)); app('log')->debug(sprintf('Amount is now %s', $amount));
} }
if (0 === bccomp($piggyBank->targetamount, '0')) { if (0 === bccomp($piggyBank->target_amount, '0')) {
app('log')->debug('Target amount is zero, can add anything.'); app('log')->debug('Target amount is zero, can add anything.');
} }

View File

@@ -84,18 +84,18 @@ class PiggyBankTransformer extends AbstractTransformer
// Amounts, depending on 0.0 state of target amount // Amounts, depending on 0.0 state of target amount
$percentage = null; $percentage = null;
$targetAmount = $piggyBank->targetamount; $targetAmount = $piggyBank->target_amount;
$leftToSave = null; $leftToSave = null;
$savePerMonth = null; $savePerMonth = null;
if (0 !== bccomp($targetAmount, '0')) { // target amount is not 0.00 if (0 !== bccomp($targetAmount, '0')) { // target amount is not 0.00
$leftToSave = bcsub($piggyBank->targetamount, $currentAmount); $leftToSave = bcsub($piggyBank->target_amount, $currentAmount);
$percentage = (int)bcmul(bcdiv($currentAmount, $targetAmount), '100'); $percentage = (int)bcmul(bcdiv($currentAmount, $targetAmount), '100');
$targetAmount = app('steam')->bcround($targetAmount, $currency->decimal_places); $targetAmount = app('steam')->bcround($targetAmount, $currency->decimal_places);
$leftToSave = app('steam')->bcround($leftToSave, $currency->decimal_places); $leftToSave = app('steam')->bcround($leftToSave, $currency->decimal_places);
$savePerMonth = app('steam')->bcround($this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places); $savePerMonth = app('steam')->bcround($this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places);
} }
$startDate = $piggyBank->startdate?->format('Y-m-d'); $startDate = $piggyBank->start_date?->format('Y-m-d');
$targetDate = $piggyBank->targetdate?->format('Y-m-d'); $targetDate = $piggyBank->target_date?->format('Y-m-d');
return [ return [
'id' => (string)$piggyBank->id, 'id' => (string)$piggyBank->id,

View File

@@ -119,7 +119,7 @@ class PiggyBankTransformer extends AbstractTransformer
/** @var PiggyBankRepetition $repetition */ /** @var PiggyBankRepetition $repetition */
foreach ($repetitions as $repetition) { foreach ($repetitions as $repetition) {
$this->repetitions[$repetition->piggy_bank_id] = [ $this->repetitions[$repetition->piggy_bank_id] = [
'amount' => $repetition->currentamount, 'amount' => $repetition->current_amount,
]; ];
} }
@@ -178,14 +178,14 @@ class PiggyBankTransformer extends AbstractTransformer
$nativeLeftToSave = null; $nativeLeftToSave = null;
$savePerMonth = null; $savePerMonth = null;
$nativeSavePerMonth = null; $nativeSavePerMonth = null;
$startDate = $piggyBank->startdate?->format('Y-m-d'); $startDate = $piggyBank->start_date?->format('Y-m-d');
$targetDate = $piggyBank->targetdate?->format('Y-m-d'); $targetDate = $piggyBank->target_date?->format('Y-m-d');
$accountId = $piggyBank->account_id; $accountId = $piggyBank->account_id;
$accountName = $this->accounts[$accountId]['name'] ?? null; $accountName = $this->accounts[$accountId]['name'] ?? null;
$currency = $this->currencies[$accountId] ?? $this->default; $currency = $this->currencies[$accountId] ?? $this->default;
$currentAmount = app('steam')->bcround($this->repetitions[$piggyBank->id]['amount'] ?? '0', $currency->decimal_places); $currentAmount = app('steam')->bcround($this->repetitions[$piggyBank->id]['amount'] ?? '0', $currency->decimal_places);
$nativeCurrentAmount = $this->converter->convert($this->default, $currency, today(), $currentAmount); $nativeCurrentAmount = $this->converter->convert($this->default, $currency, today(), $currentAmount);
$targetAmount = $piggyBank->targetamount; $targetAmount = $piggyBank->target_amount;
$nativeTargetAmount = $this->converter->convert($this->default, $currency, today(), $targetAmount); $nativeTargetAmount = $this->converter->convert($this->default, $currency, today(), $targetAmount);
$note = $this->notes[$piggyBank->id] ?? null; $note = $this->notes[$piggyBank->id] ?? null;
$group = $this->groups[$piggyBank->id] ?? null; $group = $this->groups[$piggyBank->id] ?? null;
@@ -194,7 +194,7 @@ class PiggyBankTransformer extends AbstractTransformer
$leftToSave = bcsub($targetAmount, $currentAmount); $leftToSave = bcsub($targetAmount, $currentAmount);
$nativeLeftToSave = $this->converter->convert($this->default, $currency, today(), $leftToSave); $nativeLeftToSave = $this->converter->convert($this->default, $currency, today(), $leftToSave);
$percentage = (int)bcmul(bcdiv($currentAmount, $targetAmount), '100'); $percentage = (int)bcmul(bcdiv($currentAmount, $targetAmount), '100');
$savePerMonth = $this->getSuggestedMonthlyAmount($currentAmount, $targetAmount, $piggyBank->startdate, $piggyBank->targetdate); $savePerMonth = $this->getSuggestedMonthlyAmount($currentAmount, $targetAmount, $piggyBank->start_date, $piggyBank->target_date);
$nativeSavePerMonth = $this->converter->convert($this->default, $currency, today(), $savePerMonth); $nativeSavePerMonth = $this->converter->convert($this->default, $currency, today(), $savePerMonth);
} }
$this->converter->summarize(); $this->converter->summarize();