Skip to content

Firefly III: a personal finances manager

Fix https://github.com/firefly-iii/firefly-iii/issues/12500

James Cole97aa44ec3f36
diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php
index 95daad09d4..0f7592a49e 100644
--- a/app/Repositories/Bill/BillRepository.php
+++ b/app/Repositories/Bill/BillRepository.php
@@ -171,10 +171,9 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
public function getActiveBills(): Collection
{
return $this->user->bills()
- ->where('active', true)
- ->orderBy('bills.name', 'ASC')
- ->get(['bills.*', DB::raw('((bills.amount_min + bills.amount_max) / 2) AS expectedAmount')])
- ;
+ ->where('active', true)
+ ->orderBy('bills.name', 'ASC')
+ ->get(['bills.*', DB::raw('((bills.amount_min + bills.amount_max) / 2) AS expectedAmount')]);
}
/**
@@ -197,11 +196,10 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
public function getBills(): Collection
{
return $this->user->bills()
- ->orderBy('order', 'ASC')
- ->orderBy('active', 'DESC')
- ->orderBy('name', 'ASC')
- ->get()
- ;
+ ->orderBy('order', 'ASC')
+ ->orderBy('active', 'DESC')
+ ->orderBy('name', 'ASC')
+ ->get();
}
public function getBillsForAccounts(Collection $accounts): Collection
@@ -225,19 +223,18 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
$ids = $accounts->pluck('id')->toArray();
return $this->user->bills()
- ->leftJoin('transaction_journals', static function (JoinClause $join): void {
- $join->on('transaction_journals.bill_id', '=', 'bills.id')->whereNull('transaction_journals.deleted_at');
- })
- ->leftJoin('transactions', static function (JoinClause $join): void {
- $join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
- })
- ->whereIn('transactions.account_id', $ids)
- ->whereNull('transaction_journals.deleted_at')
- ->orderBy('bills.active', 'DESC')
- ->orderBy('bills.name', 'ASC')
- ->groupBy($fields)
- ->get($fields)
- ;
+ ->leftJoin('transaction_journals', static function (JoinClause $join): void {
+ $join->on('transaction_journals.bill_id', '=', 'bills.id')->whereNull('transaction_journals.deleted_at');
+ })
+ ->leftJoin('transactions', static function (JoinClause $join): void {
+ $join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
+ })
+ ->whereIn('transactions.account_id', $ids)
+ ->whereNull('transaction_journals.deleted_at')
+ ->orderBy('bills.active', 'DESC')
+ ->orderBy('bills.name', 'ASC')
+ ->groupBy($fields)
+ ->get($fields);
}
/**
@@ -256,13 +253,13 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
/** @var null|Note $note */
$note = $bill->notes()->first();
- return (string) $note?->text;
+ return (string)$note?->text;
}
public function getOverallAverage(Bill $bill): array
{
/** @var JournalRepositoryInterface $repos */
- $repos = app(JournalRepositoryInterface::class);
+ $repos = app(JournalRepositoryInterface::class);
$repos->setUser($this->user);
// get and sort on currency
@@ -273,9 +270,9 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
foreach ($journals as $journal) {
/** @var Transaction $transaction */
$transaction = $journal->transactions()->where('amount', '<', 0)->first();
- $currencyId = (int) $journal->transaction_currency_id;
+ $currencyId = (int)$journal->transaction_currency_id;
$currency = $journal->transactionCurrency;
- $result[$currencyId] ??= [
+ $result[$currencyId] ??= [
'sum' => '0',
'pc_sum' => '0',
'count' => 0,
@@ -286,10 +283,10 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
];
- $result[$currencyId]['sum'] = bcadd($result[$currencyId]['sum'], (string) $transaction->amount);
+ $result[$currencyId]['sum'] = bcadd($result[$currencyId]['sum'], (string)$transaction->amount);
$result[$currencyId]['pc_sum'] = bcadd($result[$currencyId]['pc_sum'], $transaction->native_amount ?? '0');
if ($journal->foreign_currency_id === Amount::getPrimaryCurrency()->id) {
- $result[$currencyId]['pc_sum'] = bcadd($result[$currencyId]['pc_sum'], (string) $transaction->amount);
+ $result[$currencyId]['pc_sum'] = bcadd($result[$currencyId]['pc_sum'], (string)$transaction->amount);
}
++$result[$currencyId]['count'];
}
@@ -300,8 +297,8 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
* @var array $arr
*/
foreach ($result as $currencyId => $arr) {
- $result[$currencyId]['avg'] = bcdiv((string) $arr['sum'], (string) $arr['count']);
- $result[$currencyId]['pc_avg'] = bcdiv((string) $arr['pc_sum'], (string) $arr['count']);
+ $result[$currencyId]['avg'] = bcdiv((string)$arr['sum'], (string)$arr['count']);
+ $result[$currencyId]['pc_avg'] = bcdiv((string)$arr['pc_sum'], (string)$arr['count']);
}
return $result;
@@ -330,19 +327,18 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
->before($end)
->after($start)
->get([
- 'transaction_journals.id',
- 'transaction_journals.date',
- 'transaction_journals.transaction_group_id',
- 'transactions.transaction_currency_id',
- 'currency.code AS transaction_currency_code',
- 'currency.decimal_places AS transaction_currency_decimal_places',
- 'transactions.foreign_currency_id',
- 'foreign_currency.code AS foreign_currency_code',
- 'foreign_currency.decimal_places AS foreign_currency_decimal_places',
- 'transactions.amount',
- 'transactions.foreign_amount',
- ])
- ;
+ 'transaction_journals.id',
+ 'transaction_journals.date',
+ 'transaction_journals.transaction_group_id',
+ 'transactions.transaction_currency_id',
+ 'currency.code AS transaction_currency_code',
+ 'currency.decimal_places AS transaction_currency_decimal_places',
+ 'transactions.foreign_currency_id',
+ 'foreign_currency.code AS foreign_currency_code',
+ 'foreign_currency.decimal_places AS foreign_currency_decimal_places',
+ 'transactions.amount',
+ 'transactions.foreign_amount',
+ ]);
}
/**
@@ -369,7 +365,7 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
// \Illuminate\Support\Facades\Log::debug(sprintf('Currentstart (%s) has become %s.', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
- $currentStart = clone $nextExpectedMatch;
+ $currentStart = clone $nextExpectedMatch;
}
return $set;
@@ -381,11 +377,10 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
public function getRulesForBill(Bill $bill): Collection
{
return $this->user->rules()
- ->leftJoin('rule_actions', 'rule_actions.rule_id', '=', 'rules.id')
- ->where('rule_actions.action_type', 'link_to_bill')
- ->where('rule_actions.action_value', $bill->name)
- ->get(['rules.*'])
- ;
+ ->leftJoin('rule_actions', 'rule_actions.rule_id', '=', 'rules.id')
+ ->where('rule_actions.action_type', 'link_to_bill')
+ ->where('rule_actions.action_value', $bill->name)
+ ->get(['rules.*']);
}
/**
@@ -396,16 +391,15 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
*/
public function getRulesForBills(Collection $collection): array
{
- $rules = $this->user->rules()
- ->leftJoin('rule_actions', 'rule_actions.rule_id', '=', 'rules.id')
- ->where('rule_actions.action_type', 'link_to_bill')
- ->get(['rules.id', 'rules.title', 'rule_actions.action_value', 'rules.active'])
- ;
- $array = [];
+ $rules = $this->user->rules()
+ ->leftJoin('rule_actions', 'rule_actions.rule_id', '=', 'rules.id')
+ ->where('rule_actions.action_type', 'link_to_bill')
+ ->get(['rules.id', 'rules.title', 'rule_actions.action_value', 'rules.active']);
+ $array = [];
/** @var Rule $rule */
foreach ($rules as $rule) {
- $array[$rule->action_value] ??= [];
+ $array[$rule->action_value] ??= [];
$array[$rule->action_value][] = ['id' => $rule->id, 'title' => $rule->title, 'active' => $rule->active];
}
$return = [];
@@ -419,29 +413,28 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
public function getYearAverage(Bill $bill, Carbon $date): array
{
/** @var JournalRepositoryInterface $repos */
- $repos = app(JournalRepositoryInterface::class);
+ $repos = app(JournalRepositoryInterface::class);
$repos->setUser($this->user);
// get and sort on currency
- $result = [];
+ $result = [];
$journals = $bill
->transactionJournals()
- ->where('date', '>=', $date->year.'-01-01 00:00:00')
- ->where('date', '<=', $date->year.'-12-31 23:59:59')
- ->get()
- ;
+ ->where('date', '>=', $date->year . '-01-01 00:00:00')
+ ->where('date', '<=', $date->year . '-12-31 23:59:59')
+ ->get();
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
/** @var null|Transaction $transaction */
- $transaction = $journal->transactions()->where('amount', '<', 0)->first();
+ $transaction = $journal->transactions()->where('amount', '<', 0)->first();
if (null === $transaction) {
continue;
}
- $currencyId = (int) $journal->transaction_currency_id;
+ $currencyId = (int)$journal->transaction_currency_id;
$currency = $journal->transactionCurrency;
- $result[$currencyId] ??= [
+ $result[$currencyId] ??= [
'sum' => '0',
'pc_sum' => '0',
'count' => 0,
@@ -451,10 +444,10 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
];
- $result[$currencyId]['sum'] = bcadd($result[$currencyId]['sum'], (string) $transaction->amount);
+ $result[$currencyId]['sum'] = bcadd($result[$currencyId]['sum'], (string)$transaction->amount);
$result[$currencyId]['pc_sum'] = bcadd($result[$currencyId]['pc_sum'], $transaction->native_amount ?? '0');
if ($journal->foreign_currency_id === Amount::getPrimaryCurrency()->id) {
- $result[$currencyId]['pc_sum'] = bcadd($result[$currencyId]['pc_sum'], (string) $transaction->amount);
+ $result[$currencyId]['pc_sum'] = bcadd($result[$currencyId]['pc_sum'], (string)$transaction->amount);
}
++$result[$currencyId]['count'];
}
@@ -465,8 +458,8 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
* @var array $arr
*/
foreach ($result as $currencyId => $arr) {
- $result[$currencyId]['avg'] = bcdiv((string) $arr['sum'], (string) $arr['count']);
- $result[$currencyId]['pc_avg'] = bcdiv((string) $arr['pc_sum'], (string) $arr['count']);
+ $result[$currencyId]['avg'] = bcdiv((string)$arr['sum'], (string)$arr['count']);
+ $result[$currencyId]['pc_avg'] = bcdiv((string)$arr['pc_sum'], (string)$arr['count']);
}
return $result;
@@ -479,7 +472,7 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
{
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
- $journal = $bill->user->transactionJournals()->find((int) $transaction['transaction_journal_id']);
+ $journal = $bill->user->transactionJournals()->find((int)$transaction['transaction_journal_id']);
$journal->bill_id = $bill->id;
$journal->save();
Log::debug(sprintf('Linked journal #%d to bill #%d', $journal->id, $bill->id));
@@ -515,7 +508,7 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
*/
public function nextExpectedMatch(Bill $bill, Carbon $date): Carbon
{
- $cache = new CacheProperties();
+ $cache = new CacheProperties();
$cache->addProperty($bill->id);
$cache->addProperty('nextExpectedMatch');
$cache->addProperty($date);
@@ -523,17 +516,17 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
return $cache->get();
}
// find the most recent date for this bill NOT in the future. Cache this date:
- $start = clone $bill->date;
+ $start = clone $bill->date;
$start->startOfDay();
- Log::debug('nextExpectedMatch: Start is '.$start->format('Y-m-d'));
+ Log::debug('nextExpectedMatch: Start is ' . $start->format('Y-m-d'));
while ($start < $date) {
Log::debug(sprintf('$start (%s) < $date (%s)', $start->format('Y-m-d H:i:s'), $date->format('Y-m-d H:i:s')));
$start = Navigation::addPeriod($start, $bill->repeat_freq, $bill->skip);
- Log::debug('Start is now '.$start->format('Y-m-d H:i:s'));
+ Log::debug('Start is now ' . $start->format('Y-m-d H:i:s'));
}
- $end = Navigation::addPeriod($start, $bill->repeat_freq, $bill->skip);
+ $end = Navigation::addPeriod($start, $bill->repeat_freq, $bill->skip);
$end->endOfDay();
// see if the bill was paid in this period.
@@ -545,8 +538,8 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
$start = clone $end;
$end = Navigation::addPeriod($start, $bill->repeat_freq, $bill->skip);
}
- Log::debug('nextExpectedMatch: Final start is '.$start->format('Y-m-d'));
- Log::debug('nextExpectedMatch: Matching end is '.$end->format('Y-m-d'));
+ Log::debug('nextExpectedMatch: Final start is ' . $start->format('Y-m-d'));
+ Log::debug('nextExpectedMatch: Matching end is ' . $end->format('Y-m-d'));
$cache->store($start);
@@ -565,11 +558,10 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
$query = sprintf('%%%s%%', $query);
return $this->user->bills()
- ->orderBy('name', 'ASC')
- ->whereLike('name', $query)
- ->take($limit)
- ->get()
- ;
+ ->orderBy('name', 'ASC')
+ ->whereLike('name', $query)
+ ->take($limit)
+ ->get();
}
public function setObjectGroup(Bill $bill, string $objectGroupTitle): Bill
@@ -612,47 +604,46 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
foreach ($bills as $bill) {
// Log::debug(sprintf('Bill #%d ("%s")', $bill->id, $bill->name));
/** @var Collection $set */
- $set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']);
- $currency = $convertToPrimary && $bill->transactionCurrency->id !== $primary->id ? $primary : $bill->transactionCurrency;
- $return[(int) $currency->id] ??= [
- 'id' => (string) $currency->id,
+ $set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']);
+ $currency = $convertToPrimary && $bill->transactionCurrency->id !== $primary->id ? $primary : $bill->transactionCurrency;
+ $return[(int)$currency->id] ??= [
+ 'id' => (string)$currency->id,
'name' => $currency->name,
'symbol' => $currency->symbol,
'code' => $currency->code,
'decimal_places' => $currency->decimal_places,
'sum' => '0',
];
- // Log::debug(sprintf('Created a new array for currency #%d', $currency->id));
+ Log::debug(sprintf('Created a new array for currency #%d', $currency->id));
/** @var TransactionJournal $transactionJournal */
foreach ($set as $transactionJournal) {
// grab currency from journal.
- $transactionCurrency = $transactionJournal->transactionCurrency;
- $currencyId = (int) $transactionCurrency->id;
+ $transactionCurrency = $transactionJournal->transactionCurrency;
+ $currencyId = (int)$transactionCurrency->id;
$return[$currencyId] ??= [
- 'id' => (string) $transactionCurrency->id,
+ 'id' => (string)$transactionCurrency->id,
'name' => $transactionCurrency->name,
'symbol' => $transactionCurrency->symbol,
'code' => $transactionCurrency->code,
'decimal_places' => $transactionCurrency->decimal_places,
'sum' => '0',
];
- $amountFromJournal = Amount::getAmountFromJournalObject($transactionJournal);
- // Log::debug(sprintf('Created a (new) array for currency #%d', $currencyId));
- // Log::debug(sprintf('Amount to add is %s', $amountFromJournal));
+ $amountFromJournal = Amount::getAmountFromJournalObject($transactionJournal, true);
+ Log::debug(sprintf('Created a (new) array for currency #%d', $currencyId));
+ Log::debug(sprintf('Amount to add is %s', $amountFromJournal));
// get currency from transaction as well.
$return[$currencyId]['sum'] = bcadd($return[$currencyId]['sum'], $amountFromJournal);
}
}
// remove empty sets
- $final = [];
+ $final = [];
foreach ($return as $entry) {
if (0 === bccomp($entry['sum'], '0')) {
continue;
}
$final[] = $entry;
}
-
return $final;
}
@@ -667,9 +658,9 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
/** @var Bill $bill */
foreach ($bills as $bill) {
// \Illuminate\Support\Facades\Log::debug(sprintf('Processing bill #%d ("%s")', $bill->id, $bill->name));
- $dates = $this->getPayDatesInRange($bill, $start, $end);
- $count = $bill->transactionJournals()->after($start)->before($end)->count();
- $total = $dates->count() - $count;
+ $dates = $this->getPayDatesInRange($bill, $start, $end);
+ $count = $bill->transactionJournals()->after($start)->before($end)->count();
+ $total = $dates->count() - $count;
// \Illuminate\Support\Facades\Log::debug(sprintf('Pay dates: %d, count: %d, left: %d', $dates->count(), $count, $total));
// \Illuminate\Support\Facades\Log::debug('dates', $dates->toArray());
@@ -678,18 +669,18 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterface
// Log::debug(sprintf('min field is %s, max field is %s', $minField, $maxField));
if ($total > 0) {
- $currency = $convertToPrimary && $bill->transactionCurrency->id !== $primary->id ? $primary : $bill->transactionCurrency;
- $average = bcdiv(bcadd($bill->{$maxField} ?? '0', $bill->{$minField} ?? '0'), '2');
+ $currency = $convertToPrimary && $bill->transactionCurrency->id !== $primary->id ? $primary : $bill->transactionCurrency;
+ $average = bcdiv(bcadd($bill->{$maxField} ?? '0', $bill->{$minField} ?? '0'), '2');
Log::debug(sprintf('Amount to pay is %s %s (%d times)', $currency->code, $average, $total));
- $return[$currency->id] ??= [
- 'id' => (string) $currency->id,
+ $return[$currency->id] ??= [
+ 'id' => (string)$currency->id,
'name' => $currency->name,
'symbol' => $currency->symbol,
'code' => $currency->code,
'decimal_places' => $currency->decimal_places,
'sum' => '0',
];
- $return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], bcmul($average, (string) $total));
+ $return[$currency->id]['sum'] = bcadd($return[$currency->id]['sum'], bcmul($average, (string)$total));
}
}
diff --git a/app/Support/Amount.php b/app/Support/Amount.php
index d610b61b4e..838e124651 100644
--- a/app/Support/Amount.php
+++ b/app/Support/Amount.php
@@ -223,12 +223,12 @@ class Amount
* Experimental function to see if we can quickly and quietly get the amount from a journal.
* This depends on the user's default currency and the wish to have it converted.
*/
- public function getAmountFromJournalObject(TransactionJournal $journal): string
+ public function getAmountFromJournalObject(TransactionJournal $journal, bool $forceNormalAmount = false): string
{
// Log::debug(sprintf('Get amount from journal #%d', $journal->id));
$convertToPrimary = $this->convertToPrimary();
$currency = $this->getPrimaryCurrency();
- $field = $convertToPrimary && $currency->id !== $journal->transaction_currency_id ? 'native_amount' : 'amount';
+ $field = false === $forceNormalAmount && $convertToPrimary && $currency->id !== $journal->transaction_currency_id ? 'native_amount' : 'amount';
/** @var null|Transaction $sourceTransaction */
$sourceTransaction = $journal->transactions()->where('amount', '<', 0)->first();