API works for multi-account piggies, the rest throws an exception

This commit is contained in:
James Cole
2024-12-01 18:32:05 +01:00
parent cdf1ebf3f7
commit d740814f88
8 changed files with 82 additions and 71 deletions

View File

@@ -72,7 +72,7 @@ class ShowController extends Controller
// types to get, page size:
$pageSize = $this->parameters->get('limit');
// get list of budgets. Count it and split it.
// get list of piggy banks. Count it and split it.
$collection = $this->repository->getPiggyBanks();
$count = $collection->count();
$piggyBanks = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);

View File

@@ -67,8 +67,8 @@ class AddTimezonesToDates extends Command
CurrencyExchangeRate::class => ['date'], // done
InvitedUser::class => ['expires'],
PiggyBankEvent::class => ['date'],
PiggyBankRepetition::class => ['startdate', 'targetdate'],
PiggyBank::class => ['startdate', 'targetdate'], // done
PiggyBankRepetition::class => ['start_date', 'target_date'],
PiggyBank::class => ['start_date', 'target_date'], // done
Recurrence::class => ['first_date', 'repeat_until', 'latest_date'],
Tag::class => ['date'],
TransactionJournal::class => ['date'],

View File

@@ -78,6 +78,11 @@ class PiggyBank extends Model
throw new NotFoundHttpException();
}
public function transactionCurrency(): BelongsTo
{
return $this->belongsTo(TransactionCurrency::class);
}
public function account(): BelongsTo
{
return $this->belongsTo(Account::class);

View File

@@ -45,6 +45,7 @@ trait ModifiesPiggyBanks
public function addAmountToRepetition(PiggyBankRepetition $repetition, string $amount, TransactionJournal $journal): void
{
throw new FireflyException('[a] Piggy bank repetitions are EOL.');
app('log')->debug(sprintf('addAmountToRepetition: %s', $amount));
if (-1 === bccomp($amount, '0')) {
app('log')->debug('Remove amount.');

View File

@@ -115,16 +115,18 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
*/
public function getCurrentAmount(PiggyBank $piggyBank): string
{
$rep = $this->getRepetition($piggyBank);
if (null === $rep) {
return '0';
$sum = '0';
foreach ($piggyBank->accounts as $account) {
$amount = (string) $account->pivot->current_amount;
$amount = '' === $amount ? '0' : $amount;
$sum = bcadd($sum, $amount);
}
return $rep->current_amount;
return $sum;
}
public function getRepetition(PiggyBank $piggyBank): ?PiggyBankRepetition
{
throw new FireflyException('[b] Piggy bank repetitions are EOL.');
return $piggyBank->piggyBankRepetitions()->first();
}
@@ -140,6 +142,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
*/
public function getExactAmount(PiggyBank $piggyBank, PiggyBankRepetition $repetition, TransactionJournal $journal): string
{
throw new FireflyException('[c] Piggy bank repetitions are EOL.');
app('log')->debug(sprintf('Now in getExactAmount(%d, %d, %d)', $piggyBank->id, $repetition->id, $journal->id));
$operator = null;
@@ -272,16 +275,17 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
public function getPiggyBanks(): Collection
{
return $this->user // @phpstan-ignore-line (phpstan does not recognize objectGroups)
->piggyBanks()
return PiggyBank
::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
->where('accounts.user_id', auth()->user()->id)
->with(
[
'account',
'objectGroups',
]
)
->orderBy('order', 'ASC')->get()
;
->orderBy('piggy_banks.order', 'ASC')->get();
}
/**
@@ -290,15 +294,12 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
public function getSuggestedMonthlyAmount(PiggyBank $piggyBank): string
{
$savePerMonth = '0';
$repetition = $this->getRepetition($piggyBank);
if (null === $repetition) {
return $savePerMonth;
}
if (null !== $piggyBank->target_date && $repetition->current_amount < $piggyBank->target_amount) {
$currentAmount = $this->getCurrentAmount($piggyBank);
if (null !== $piggyBank->target_date && $currentAmount < $piggyBank->target_amount) {
$now = today(config('app.timezone'));
$startDate = null !== $piggyBank->start_date && $piggyBank->start_date->gte($now) ? $piggyBank->start_date : $now;
$diffInMonths = (int) $startDate->diffInMonths($piggyBank->target_date);
$remainingAmount = bcsub($piggyBank->target_amount, $repetition->current_amount);
$remainingAmount = bcsub($piggyBank->target_amount, $currentAmount);
// more than 1 month to go and still need money to save:
if ($diffInMonths > 0 && 1 === bccomp($remainingAmount, '0')) {
@@ -342,8 +343,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
$search->whereLike('piggy_banks.name', sprintf('%%%s%%', $query));
}
$search->orderBy('piggy_banks.order', 'ASC')
->orderBy('piggy_banks.name', 'ASC')
;
->orderBy('piggy_banks.name', 'ASC');
return $search->take($limit)->get();
}

View File

@@ -54,14 +54,11 @@ class PiggyBankTransformer extends AbstractTransformer
*/
public function transform(PiggyBank $piggyBank): array
{
$account = $piggyBank->account;
$user = $piggyBank->accounts()->first()->user;
// set up repositories
$this->accountRepos->setUser($account->user);
$this->piggyRepos->setUser($account->user);
// get currency from account, or use default.
$currency = $this->accountRepos->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
$this->accountRepos->setUser($user);
$this->piggyRepos->setUser($user);
// note
$notes = $this->piggyRepos->getNoteText($piggyBank);
@@ -80,6 +77,7 @@ class PiggyBankTransformer extends AbstractTransformer
}
// get currently saved amount:
$currency = $piggyBank->transactionCurrency;
$currentAmount = app('steam')->bcround($this->piggyRepos->getCurrentAmount($piggyBank), $currency->decimal_places);
// Amounts, depending on 0.0 state of target amount
@@ -101,8 +99,9 @@ class PiggyBankTransformer extends AbstractTransformer
'id' => (string) $piggyBank->id,
'created_at' => $piggyBank->created_at->toAtomString(),
'updated_at' => $piggyBank->updated_at->toAtomString(),
'account_id' => (string)$piggyBank->account_id,
'account_name' => $piggyBank->account->name,
'accounts' => $this->renderAccounts($piggyBank),
//'account_id' => (string)$piggyBank->account_id,
//'account_name' => $piggyBank->account->name,
'name' => $piggyBank->name,
'currency_id' => (string) $currency->id,
'currency_code' => $currency->code,
@@ -129,4 +128,17 @@ class PiggyBankTransformer extends AbstractTransformer
],
];
}
private function renderAccounts(PiggyBank $piggyBank): array
{
$return = [];
foreach ($piggyBank->accounts as $account) {
$return[] = [
'id' => $account->id,
'name' => $account->name,
// TODO add balance, add left to save.
];
}
return $return;
}
}

View File

@@ -115,6 +115,7 @@ class PiggyBankTransformer extends AbstractTransformer
// grab repetitions (for current amount):
$repetitions = PiggyBankRepetition::whereIn('piggy_bank_id', $piggyBanks)->get();
throw new FireflyException('[d] Piggy bank repetitions are EOL.');
/** @var PiggyBankRepetition $repetition */
foreach ($repetitions as $repetition) {

View File

@@ -332,14 +332,6 @@ class User extends Authenticatable
return $this->hasMany(ObjectGroup::class);
}
/**
* Link to piggy banks.
*/
public function piggyBanks(): HasManyThrough
{
return $this->hasManyThrough(PiggyBank::class, Account::class);
}
/**
* Link to preferences.
*/