Various code cleanup.

This commit is contained in:
James Cole
2023-11-05 19:41:37 +01:00
parent a0564751d6
commit 1d2e95f5af
136 changed files with 1171 additions and 514 deletions

View File

@@ -363,7 +363,7 @@ class AccountRepository implements AccountRepositoryInterface
return null;
}
return (string)$transaction->amount;
return $transaction->amount;
}
/**
@@ -635,6 +635,7 @@ class AccountRepository implements AccountRepositoryInterface
*/
public function oldestJournal(Account $account): ?TransactionJournal
{
/** @var TransactionJournal|null $first */
$first = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->orderBy('transaction_journals.date', 'ASC')
@@ -643,7 +644,7 @@ class AccountRepository implements AccountRepositoryInterface
->orderBy('transaction_journals.id', 'ASC')
->first(['transaction_journals.id']);
if (null !== $first) {
return TransactionJournal::find((int)$first->id);
return TransactionJournal::find($first->id);
}
return null;

View File

@@ -633,8 +633,8 @@ class BillRepository implements BillRepositoryInterface
/** @var Transaction|null $sourceTransaction */
$sourceTransaction = $transactionJournal->transactions()->where('amount', '<', 0)->first();
if (null !== $sourceTransaction) {
$amount = (string)$sourceTransaction->amount;
if ((int)$sourceTransaction->foreign_currency_id === (int)$currency->id) {
$amount = $sourceTransaction->amount;
if ((int)$sourceTransaction->foreign_currency_id === $currency->id) {
// use foreign amount instead!
$amount = (string)$sourceTransaction->foreign_amount;
}

View File

@@ -134,12 +134,13 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
public function getAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end): string
{
$amount = '0';
/** @var AvailableBudget|null $availableBudget */
$availableBudget = $this->user->availableBudgets()
->where('transaction_currency_id', $currency->id)
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))->first();
if (null !== $availableBudget) {
$amount = (string)$availableBudget->amount;
$amount = $availableBudget->amount;
}
return $amount;

View File

@@ -114,24 +114,24 @@ class BudgetRepository implements BudgetRepositoryInterface
];
// same period
if ($limit->start_date->isSameDay($start) && $limit->end_date->isSameDay($end)) {
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], (string)$limit->amount);
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], $limit->amount);
app('log')->debug(sprintf('Add full amount [1]: %s', $limit->amount));
continue;
}
// limit is inside of date range
if ($start->lte($limit->start_date) && $end->gte($limit->end_date)) {
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], (string)$limit->amount);
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], $limit->amount);
app('log')->debug(sprintf('Add full amount [2]: %s', $limit->amount));
continue;
}
$total = $limit->start_date->diffInDays($limit->end_date) + 1; // include the day itself.
$days = $this->daysInOverlap($limit, $start, $end);
$amount = bcmul(bcdiv((string)$limit->amount, (string)$total), (string)$days);
$amount = bcmul(bcdiv($limit->amount, (string)$total), (string)$days);
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], $amount);
app('log')->debug(
sprintf(
'Amount per day: %s (%s over %d days). Total amount for %d days: %s',
bcdiv((string)$limit->amount, (string)$total),
bcdiv($limit->amount, (string)$total),
$limit->amount,
$total,
$days,
@@ -230,24 +230,24 @@ class BudgetRepository implements BudgetRepositoryInterface
];
// same period
if ($limit->start_date->isSameDay($start) && $limit->end_date->isSameDay($end)) {
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], (string)$limit->amount);
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], $limit->amount);
app('log')->debug(sprintf('Add full amount [1]: %s', $limit->amount));
continue;
}
// limit is inside of date range
if ($start->lte($limit->start_date) && $end->gte($limit->end_date)) {
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], (string)$limit->amount);
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], $limit->amount);
app('log')->debug(sprintf('Add full amount [2]: %s', $limit->amount));
continue;
}
$total = $limit->start_date->diffInDays($limit->end_date) + 1; // include the day itself.
$days = $this->daysInOverlap($limit, $start, $end);
$amount = bcmul(bcdiv((string)$limit->amount, (string)$total), (string)$days);
$amount = bcmul(bcdiv($limit->amount, (string)$total), (string)$days);
$return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], $amount);
app('log')->debug(
sprintf(
'Amount per day: %s (%s over %d days). Total amount for %d days: %s',
bcdiv((string)$limit->amount, (string)$total),
bcdiv($limit->amount, (string)$total),
$limit->amount,
$total,
$days,
@@ -489,8 +489,8 @@ class BudgetRepository implements BudgetRepositoryInterface
$budgets = $this->getBudgets();
/** @var Budget $budget */
foreach ($budgets as $budget) {
DB::table('budget_transaction')->where('budget_id', (int)$budget->id)->delete();
DB::table('budget_transaction_journal')->where('budget_id', (int)$budget->id)->delete();
DB::table('budget_transaction')->where('budget_id', $budget->id)->delete();
DB::table('budget_transaction_journal')->where('budget_id', $budget->id)->delete();
RecurrenceTransactionMeta::where('name', 'budget_id')->where('value', (string)$budget->id)->delete();
RuleAction::where('action_type', 'set_budget')->where('action_value', (string)$budget->id)->delete();
$budget->delete();

View File

@@ -58,7 +58,7 @@ class OperationsRepository implements OperationsRepositoryInterface
foreach ($budget->budgetlimits as $limit) {
$diff = $limit->start_date->diffInDays($limit->end_date);
$diff = 0 === $diff ? 1 : $diff;
$amount = (string)$limit->amount;
$amount = $limit->amount;
$perDay = bcdiv($amount, (string)$diff);
$total = bcadd($total, $perDay);
$count++;

View File

@@ -119,7 +119,7 @@ trait ModifiesPiggyBanks
{
$today = today(config('app.timezone'));
$leftOnAccount = $this->leftOnAccount($piggyBank, $today);
$savedSoFar = (string)$this->getRepetition($piggyBank)->currentamount;
$savedSoFar = $this->getRepetition($piggyBank)->currentamount;
$maxAmount = $leftOnAccount;
$leftToSave = null;
if (0 !== bccomp($piggyBank->targetamount, '0')) {

View File

@@ -146,7 +146,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
return '0';
}
return (string)$rep->currentamount;
return $rep->currentamount;
}
/**
@@ -225,11 +225,11 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
// currency of the account + the piggy bank currency are almost the same.
// which amount from the transaction matches?
$amount = null;
if ((int)$source->transaction_currency_id === (int)$currency->id) {
if ((int)$source->transaction_currency_id === $currency->id) {
app('log')->debug('Use normal amount');
$amount = app('steam')->$operator($source->amount); // @phpstan-ignore-line
}
if ((int)$source->foreign_currency_id === (int)$currency->id) {
if ((int)$source->foreign_currency_id === $currency->id) {
app('log')->debug('Use foreign amount');
$amount = app('steam')->$operator($source->foreign_amount); // @phpstan-ignore-line
}
@@ -240,10 +240,10 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
}
app('log')->debug(sprintf('The currency is %s and the amount is %s', $currency->code, $amount));
$room = bcsub((string)$piggyBank->targetamount, (string)$repetition->currentamount);
$room = bcsub($piggyBank->targetamount, $repetition->currentamount);
$compare = bcmul($repetition->currentamount, '-1');
if (bccomp((string)$piggyBank->targetamount, '0') === 0) {
if (bccomp($piggyBank->targetamount, '0') === 0) {
// amount is zero? then the "room" is positive amount of we wish to add or remove.
$room = app('steam')->positive($amount);
app('log')->debug(sprintf('Room is now %s', $room));

View File

@@ -376,8 +376,8 @@ class RuleRepository implements RuleRepositoryInterface
*/
public function setOrder(Rule $rule, int $newOrder): void
{
$oldOrder = (int)$rule->order;
$groupId = (int)$rule->rule_group_id;
$oldOrder = $rule->order;
$groupId = $rule->rule_group_id;
$maxOrder = $this->maxOrder($rule->ruleGroup);
$newOrder = $newOrder > $maxOrder ? $maxOrder + 1 : $newOrder;
app('log')->debug(sprintf('New order will be %d', $newOrder));

View File

@@ -151,7 +151,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
$count = 1;
/** @var Rule $entry */
foreach ($set as $entry) {
if ((int)$entry->order !== $count) {
if ($entry->order !== $count) {
app('log')->debug(sprintf('Rule #%d was on spot %d but must be on spot %d', $entry->id, $entry->order, $count));
$entry->order = $count;
$entry->save();

View File

@@ -175,7 +175,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
$result = [];
/** @var Attachment $attachment */
foreach ($set as $attachment) {
$journalId = (int)$attachment->attachable_id;
$journalId = $attachment->attachable_id;
$result[$journalId] = $result[$journalId] ?? [];
$current = $attachment->toArray();
$current['file_exists'] = true;
@@ -417,7 +417,7 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface
if (null !== $currencyPreference) {
$currency = TransactionCurrency::where('id', $currencyPreference->data)->first();
}
$journalId = (int)$row->transaction_journal_id;
$journalId = $row->transaction_journal_id;
$return[$journalId] = $return[$journalId] ?? [];
$return[$journalId][] = [

View File

@@ -225,7 +225,7 @@ class UserGroupRepository implements UserGroupRepositoryInterface
// if it's 1:
if (1 === $membershipCount) {
$lastUserId = (int)$userGroup->groupMemberships()->distinct()->first(['group_memberships.user_id'])->user_id;
$lastUserId = $userGroup->groupMemberships()->distinct()->first(['group_memberships.user_id'])->user_id;
// if this is also the user we're editing right now, and we remove all of their roles:
if ($lastUserId === (int)$user->id && 0 === count($data['roles'])) {
app('log')->debug('User is last in this group, refuse to act');

View File

@@ -81,7 +81,7 @@ class BillRepository implements BillRepositoryInterface
/** @var Collection $set */
$set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']);
$currency = $bill->transactionCurrency;
$currencyId = (int)$bill->transaction_currency_id;
$currencyId = $bill->transaction_currency_id;
$return[$currencyId] = $return[$currencyId] ?? [
'currency_id' => (string)$currency->id,
@@ -103,18 +103,18 @@ class BillRepository implements BillRepositoryInterface
/** @var Transaction|null $sourceTransaction */
$sourceTransaction = $transactionJournal->transactions()->where('amount', '<', 0)->first();
if (null !== $sourceTransaction) {
$amount = (string)$sourceTransaction->amount;
if ((int)$sourceTransaction->foreign_currency_id === (int)$currency->id) {
$amount = $sourceTransaction->amount;
if ((int)$sourceTransaction->foreign_currency_id === $currency->id) {
// use foreign amount instead!
$amount = (string)$sourceTransaction->foreign_amount;
}
// convert to native currency
$nativeAmount = $amount;
if ($currencyId !== (int)$default->id) {
if ($currencyId !== $default->id) {
// get rate and convert.
$nativeAmount = $converter->convert($currency, $default, $transactionJournal->date, $amount);
}
if ((int)$sourceTransaction->foreign_currency_id === (int)$default->id) {
if ((int)$sourceTransaction->foreign_currency_id === $default->id) {
// ignore conversion, use foreign amount
$nativeAmount = (string)$sourceTransaction->foreign_amount;
}
@@ -154,7 +154,7 @@ class BillRepository implements BillRepositoryInterface
if ($total > 0) {
$currency = $bill->transactionCurrency;
$currencyId = (int)$bill->transaction_currency_id;
$currencyId = $bill->transaction_currency_id;
$average = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2');
$nativeAverage = $converter->convert($currency, $default, $start, $average);
$return[$currencyId] = $return[$currencyId] ?? [

View File

@@ -53,7 +53,7 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
->where('end_date', $end->format('Y-m-d'))->get();
/** @var AvailableBudget $availableBudget */
foreach ($availableBudgets as $availableBudget) {
$currencyId = (int)$availableBudget->transaction_currency_id;
$currencyId = $availableBudget->transaction_currency_id;
$return[$currencyId] = $return[$currencyId] ?? [
'currency_id' => $currencyId,
'currency_code' => $availableBudget->transactionCurrency->code,

View File

@@ -113,7 +113,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
// is being used in accounts (as integer)
$meta = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
->whereNull('accounts.deleted_at')
->where('account_meta.name', 'currency_id')->where('account_meta.data', json_encode((int)$currency->id))->count();
->where('account_meta.name', 'currency_id')->where('account_meta.data', json_encode($currency->id))->count();
if ($meta > 0) {
app('log')->info(sprintf('Used in %d accounts as currency_id, return true. ', $meta));
@@ -181,10 +181,10 @@ class CurrencyRepository implements CurrencyRepositoryInterface
$local = $this->get();
return $all->map(static function (TransactionCurrency $current) use ($local) {
$hasId = $local->contains(static function (TransactionCurrency $entry) use ($current) {
return (int)$entry->id === (int)$current->id;
return $entry->id === $current->id;
});
$isDefault = $local->contains(static function (TransactionCurrency $entry) use ($current) {
return 1 === (int)$entry->pivot->group_default && (int)$entry->id === (int)$current->id;
return 1 === (int)$entry->pivot->group_default && $entry->id === $current->id;
});
$current->userEnabled = $hasId;
$current->userDefault = $isDefault;