From 06049a9a2823c24e9158377165a17b07152c9c7b Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 26 Dec 2024 09:16:17 +0100 Subject: [PATCH] Update various pages, clean up some code. --- app/Http/Controllers/Bill/IndexController.php | 7 +-- .../Controllers/Json/ReconcileController.php | 4 +- app/Http/Middleware/Installer.php | 1 - app/Models/Bill.php | 4 ++ app/Support/Steam.php | 25 ++------ app/Transformers/BillTransformer.php | 59 ++++++++++--------- resources/views/bills/show.twig | 10 +++- resources/views/list/bills.twig | 4 ++ 8 files changed, 58 insertions(+), 56 deletions(-) diff --git a/app/Http/Controllers/Bill/IndexController.php b/app/Http/Controllers/Bill/IndexController.php index 871258e1af..c2bf354790 100644 --- a/app/Http/Controllers/Bill/IndexController.php +++ b/app/Http/Controllers/Bill/IndexController.php @@ -76,7 +76,6 @@ class IndexController extends Controller $collection = $this->repository->getBills(); $total = $collection->count(); - $defaultCurrency = app('amount')->getDefaultCurrency(); $parameters = new ParameterBag(); // sub one day from temp start so the last paid date is one day before it should be. $tempStart = clone $start; @@ -84,7 +83,8 @@ class IndexController extends Controller // $tempStart->subDay(); $parameters->set('start', $tempStart); $parameters->set('end', $end); - + $parameters->set('convertToNative', $this->convertToNative); + $parameters->set('defaultCurrency', $this->defaultCurrency); /** @var BillTransformer $transformer */ $transformer = app(BillTransformer::class); $transformer->setParameters($parameters); @@ -112,7 +112,7 @@ class IndexController extends Controller 'bills' => [], ]; - $currency = $bill->transactionCurrency ?? $defaultCurrency; + $currency = $bill->transactionCurrency ?? $this->defaultCurrency; $array['currency_id'] = $currency->id; $array['currency_name'] = $currency->name; $array['currency_symbol'] = $currency->symbol; @@ -122,7 +122,6 @@ class IndexController extends Controller $array['rules'] = $rules[$bill['id']] ?? []; $bills[$groupOrder]['bills'][] = $array; } - // order by key ksort($bills); diff --git a/app/Http/Controllers/Json/ReconcileController.php b/app/Http/Controllers/Json/ReconcileController.php index 72fad4d610..70e4843c66 100644 --- a/app/Http/Controllers/Json/ReconcileController.php +++ b/app/Http/Controllers/Json/ReconcileController.php @@ -123,10 +123,10 @@ class ReconcileController extends Controller Log::debug(sprintf('End balance: "%s"', $endBalance)); Log::debug(sprintf('Cleared amount: "%s"', $clearedAmount)); Log::debug(sprintf('Amount: "%s"', $amount)); - $difference = bcadd(bcadd(bcsub($startBalance, $endBalance), $clearedAmount), $amount); + $difference = bcadd(bcadd(bcsub($startBalance ?? '0', $endBalance ?? '0'), $clearedAmount?? '0'), $amount); $diffCompare = bccomp($difference, '0'); $countCleared = count($clearedJournals); - $reconSum = bcadd(bcadd($startBalance, $amount), $clearedAmount); + $reconSum = bcadd(bcadd($startBalance ?? '0', $amount ?? '0'), $clearedAmount ?? '0'); try { $view = view('accounts.reconcile.overview', compact('account', 'start', 'diffCompare', 'difference', 'end', 'clearedAmount', 'startBalance', 'endBalance', 'amount', 'route', 'countCleared', 'reconSum', 'selectedIds'))->render(); diff --git a/app/Http/Middleware/Installer.php b/app/Http/Middleware/Installer.php index 069df131c2..158976ca98 100644 --- a/app/Http/Middleware/Installer.php +++ b/app/Http/Middleware/Installer.php @@ -24,7 +24,6 @@ declare(strict_types=1); namespace FireflyIII\Http\Middleware; -use DB; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Support\System\OAuthKeys; use Illuminate\Database\QueryException; diff --git a/app/Models/Bill.php b/app/Models/Bill.php index 45fc9beb73..712e9e2f58 100644 --- a/app/Models/Bill.php +++ b/app/Models/Bill.php @@ -60,6 +60,8 @@ class Bill extends Model 'match_encrypted' => 'boolean', 'amount_min' => 'string', 'amount_max' => 'string', + 'native_amount_min' => 'string', + 'native_amount_max' => 'string', ]; protected $fillable @@ -81,6 +83,8 @@ class Bill extends Model 'extension_date', 'end_date_tz', 'extension_date_tz', + 'native_amount_min', + 'native_amount_max', ]; protected $hidden = ['amount_min_encrypted', 'amount_max_encrypted', 'name_encrypted', 'match_encrypted']; diff --git a/app/Support/Steam.php b/app/Support/Steam.php index 25c7c20766..528cf29e5d 100644 --- a/app/Support/Steam.php +++ b/app/Support/Steam.php @@ -29,6 +29,7 @@ use FireflyIII\Models\Account; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionCurrency; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; /** @@ -123,10 +124,10 @@ class Steam [ // @phpstan-ignore-line 'transaction_journals.date', 'transactions.transaction_currency_id', - \DB::raw('SUM(transactions.amount) AS modified'), + DB::raw('SUM(transactions.amount) AS modified'), 'transactions.foreign_currency_id', - \DB::raw('SUM(transactions.foreign_amount) AS modified_foreign'), - \DB::raw('SUM(transactions.native_amount) AS modified_native'), + DB::raw('SUM(transactions.foreign_amount) AS modified_foreign'), + DB::raw('SUM(transactions.native_amount) AS modified_native'), ] ); @@ -178,24 +179,6 @@ class Steam // add to GBP, as expected. $currentBalance[$entryCurrency->code] = bcadd($currentBalance[$entryCurrency->code] ?? '0', $modified); } - -// // add "modified" to amount if the currency id matches the account currency id. -// if ($entry->transaction_currency_id === $currency->id) { -// $currentBalance['balance'] = bcadd($currentBalance['balance'], $modified); -// $currentBalance[$currency->code] = bcadd($currentBalance[$currency->code], $modified); -// } -// -// // always add the native balance, even if it ends up at zero. -// $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeModified); - - // DO NOT add modified foreign to the array -// if (null !== $entry->foreign_currency_id) { -// $foreignId = $entry->foreign_currency_id; -// $currencies[$foreignId] ??= TransactionCurrency::find($foreignId); -// $foreignCurrency = $currencies[$foreignId]; -// $currentBalance[$foreignCurrency->code] ??= '0'; -// $currentBalance[$foreignCurrency->code] = bcadd($currentBalance[$foreignCurrency->code], $foreignModified); -// } $balances[$carbon->format('Y-m-d')] = $currentBalance; Log::debug('Updated entry',$currentBalance); } diff --git a/app/Transformers/BillTransformer.php b/app/Transformers/BillTransformer.php index f9a29a79db..2229d94ace 100644 --- a/app/Transformers/BillTransformer.php +++ b/app/Transformers/BillTransformer.php @@ -30,6 +30,7 @@ use FireflyIII\Models\Bill; use FireflyIII\Models\ObjectGroup; use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Bill\BillRepositoryInterface; +use FireflyIII\Support\Facades\Amount; use FireflyIII\Support\Models\BillDateCalculator; use Illuminate\Support\Collection; @@ -58,21 +59,23 @@ class BillTransformer extends AbstractTransformer */ public function transform(Bill $bill): array { - $paidData = $this->paidData($bill); - $lastPaidDate = $this->getLastPaidDate($paidData); - $start = $this->parameters->get('start') ?? today()->subYears(10); - $end = $this->parameters->get('end') ?? today()->addYears(10); - $payDates = $this->calculator->getPayDates($start, $end, $bill->date, $bill->repeat_freq, $bill->skip, $lastPaidDate); - $currency = $bill->transactionCurrency; - $notes = $this->repository->getNoteText($bill); - $notes = '' === $notes ? null : $notes; - $objectGroupId = null; - $objectGroupOrder = null; - $objectGroupTitle = null; + $defaultCurrency = $this->parameters->get('defaultCurrency') ?? Amount::getDefaultCurrency(); + + $paidData = $this->paidData($bill); + $lastPaidDate = $this->getLastPaidDate($paidData); + $start = $this->parameters->get('start') ?? today()->subYears(10); + $end = $this->parameters->get('end') ?? today()->addYears(10); + $payDates = $this->calculator->getPayDates($start, $end, $bill->date, $bill->repeat_freq, $bill->skip, $lastPaidDate); + $currency = $bill->transactionCurrency; + $notes = $this->repository->getNoteText($bill); + $notes = '' === $notes ? null : $notes; + $objectGroupId = null; + $objectGroupOrder = null; + $objectGroupTitle = null; $this->repository->setUser($bill->user); /** @var null|ObjectGroup $objectGroup */ - $objectGroup = $bill->objectGroups->first(); + $objectGroup = $bill->objectGroups->first(); if (null !== $objectGroup) { $objectGroupId = $objectGroup->id; $objectGroupOrder = $objectGroup->order; @@ -82,7 +85,7 @@ class BillTransformer extends AbstractTransformer $paidDataFormatted = []; $payDatesFormatted = []; foreach ($paidData as $object) { - $date = Carbon::createFromFormat('!Y-m-d', $object['date'], config('app.timezone')); + $date = Carbon::createFromFormat('!Y-m-d', $object['date'], config('app.timezone')); if (null === $date) { $date = today(config('app.timezone')); } @@ -91,24 +94,24 @@ class BillTransformer extends AbstractTransformer } foreach ($payDates as $string) { - $date = Carbon::createFromFormat('!Y-m-d', $string, config('app.timezone')); + $date = Carbon::createFromFormat('!Y-m-d', $string, config('app.timezone')); if (null === $date) { $date = today(config('app.timezone')); } $payDatesFormatted[] = $date->toAtomString(); } // next expected match - $nem = null; - $nemDate = null; - $nemDiff = trans('firefly.not_expected_period'); - $firstPayDate = $payDates[0] ?? null; + $nem = null; + $nemDate = null; + $nemDiff = trans('firefly.not_expected_period'); + $firstPayDate = $payDates[0] ?? null; if (null !== $firstPayDate) { $nemDate = Carbon::createFromFormat('!Y-m-d', $firstPayDate, config('app.timezone')); if (null === $nemDate) { $nemDate = today(config('app.timezone')); } - $nem = $nemDate->toAtomString(); + $nem = $nemDate->toAtomString(); // nullify again when it's outside the current view range. if ( @@ -129,7 +132,7 @@ class BillTransformer extends AbstractTransformer $current = $payDatesFormatted[0] ?? null; if (null !== $current && !$nemDate->isToday()) { - $temp2 = Carbon::createFromFormat('Y-m-d\TH:i:sP', $current); + $temp2 = Carbon::createFromFormat('Y-m-d\TH:i:sP', $current); if (null === $temp2) { $temp2 = today(config('app.timezone')); } @@ -149,6 +152,8 @@ class BillTransformer extends AbstractTransformer 'name' => $bill->name, 'amount_min' => app('steam')->bcround($bill->amount_min, $currency->decimal_places), 'amount_max' => app('steam')->bcround($bill->amount_max, $currency->decimal_places), + 'native_amount_min' => app('steam')->bcround($bill->native_amount_min, $defaultCurrency->decimal_places), + 'native_amount_max' => app('steam')->bcround($bill->native_amount_max, $defaultCurrency->decimal_places), 'date' => $bill->date->toAtomString(), 'end_date' => $bill->end_date?->toAtomString(), 'extension_date' => $bill->extension_date?->toAtomString(), @@ -169,7 +174,7 @@ class BillTransformer extends AbstractTransformer 'links' => [ [ 'rel' => 'self', - 'uri' => '/bills/'.$bill->id, + 'uri' => '/bills/' . $bill->id, ], ], ]; @@ -191,13 +196,13 @@ class BillTransformer extends AbstractTransformer // 2023-07-18 this particular date is used to search for the last paid date. // 2023-07-18 the cloned $searchDate is used to grab the correct transactions. /** @var Carbon $start */ - $start = clone $this->parameters->get('start'); - $searchStart = clone $start; + $start = clone $this->parameters->get('start'); + $searchStart = clone $start; $start->subDay(); /** @var Carbon $end */ - $end = clone $this->parameters->get('end'); - $searchEnd = clone $end; + $end = clone $this->parameters->get('end'); + $searchEnd = clone $end; // move the search dates to the start of the day. $searchStart->startOfDay(); @@ -207,7 +212,7 @@ class BillTransformer extends AbstractTransformer app('log')->debug(sprintf('Search parameters are: start: %s', $searchStart->format('Y-m-d'))); // Get from database when bill was paid. - $set = $this->repository->getPaidDatesInRange($bill, $searchStart, $searchEnd); + $set = $this->repository->getPaidDatesInRange($bill, $searchStart, $searchEnd); app('log')->debug(sprintf('Count %d entries in getPaidDatesInRange()', $set->count())); // Grab from array the most recent payment. If none exist, fall back to the start date and pretend *that* was the last paid date. @@ -216,7 +221,7 @@ class BillTransformer extends AbstractTransformer app('log')->debug(sprintf('Result of lastPaidDate is %s', $lastPaidDate->format('Y-m-d'))); // At this point the "next match" is exactly after the last time the bill was paid. - $result = []; + $result = []; foreach ($set as $entry) { $result[] = [ 'transaction_group_id' => (string) $entry->transaction_group_id, diff --git a/resources/views/bills/show.twig b/resources/views/bills/show.twig index 7edc47d58d..3df9b2cfc5 100644 --- a/resources/views/bills/show.twig +++ b/resources/views/bills/show.twig @@ -27,7 +27,15 @@ diff --git a/resources/views/list/bills.twig b/resources/views/list/bills.twig index b32ca256cd..0dc7719635 100644 --- a/resources/views/list/bills.twig +++ b/resources/views/list/bills.twig @@ -67,6 +67,10 @@ title="{{ formatAmountBySymbol(entry.amount_min, entry.currency_symbol, entry.currency_decimal_places, false)|escape }} -- {{ formatAmountBySymbol(entry.amount_max, entry.currency_symbol, entry.currency_decimal_places, false)|escape }}" > ~ {{ formatAmountBySymbol((entry.amount_max + entry.amount_min)/2, entry.currency_symbol, entry.currency_decimal_places) }} + + {% if '0' != entry.native_amount_max %} + ({{ formatAmountBySymbol((entry.native_amount_max + entry.native_amount_min)/2, defaultCurrency.symbol, defaultCurrency.decimal_places) }}) + {% endif %}
- {{ trans('firefly.match_between_amounts', {low: formatAmountByCurrency(object.data.currency,object.data.amount_min), high: formatAmountByCurrency(object.data.currency,object.data.amount_max) })|raw }} + {% set lowAmount = formatAmountByCurrency(object.data.currency,object.data.amount_min) %} + {% set highAmount = formatAmountByCurrency(object.data.currency,object.data.amount_max) %} + {% if(0 != object.data.native_amount_min) %} + {% set lowAmount = lowAmount ~ ' (' ~ formatAmountByCode(object.data.native_amount_min, defaultCurrency.code) ~ ')' %} + {% endif %} + {% if(0 != object.data.native_amount_max) %} + {% set highAmount = highAmount ~ ' (' ~ formatAmountByCode(object.data.native_amount_max, defaultCurrency.code) ~ ')' %} + {% endif %} + {{ trans('firefly.match_between_amounts', {low: lowAmount, high: highAmount })|raw }} {{ 'repeats'|_ }} {{ trans('firefly.repeat_freq_' ~object.data.repeat_freq) }}.