diff --git a/app/Http/Controllers/Account/ShowController.php b/app/Http/Controllers/Account/ShowController.php index e562a17f3a..6b6caffb64 100644 --- a/app/Http/Controllers/Account/ShowController.php +++ b/app/Http/Controllers/Account/ShowController.php @@ -167,34 +167,34 @@ class ShowController extends Controller if (!$this->isEditableAccount($account)) { return $this->redirectAccountToAccount($account); } - $location = $this->repository->getLocation($account); - $isLiability = $this->repository->isLiability($account); - $attachments = $this->repository->getAttachments($account); - $objectType = config(sprintf('firefly.shortNamesByFullName.%s', $account->accountType->type)); - $end = today(config('app.timezone')); - $today = today(config('app.timezone')); + $location = $this->repository->getLocation($account); + $isLiability = $this->repository->isLiability($account); + $attachments = $this->repository->getAttachments($account); + $objectType = config(sprintf('firefly.shortNamesByFullName.%s', $account->accountType->type)); + $end = today(config('app.timezone')); + $today = today(config('app.timezone')); $accountCurrency = $this->repository->getAccountCurrency($account); - $start = $this->repository->oldestJournalDate($account) ?? today(config('app.timezone'))->startOfMonth(); - $subTitleIcon = config('firefly.subIconsByIdentifier.'.$account->accountType->type); - $page = (int) $request->get('page'); - $pageSize = (int) app('preferences')->get('listPageSize', 50)->data; - $currency = $this->repository->getAccountCurrency($account) ?? Amount::getDefaultCurrency(); - $subTitle = (string) trans('firefly.all_journals_for_account', ['name' => $account->name]); - $periods = new Collection(); + $start = $this->repository->oldestJournalDate($account) ?? today(config('app.timezone'))->startOfMonth(); + $subTitleIcon = config('firefly.subIconsByIdentifier.'.$account->accountType->type); + $page = (int) $request->get('page'); + $pageSize = (int) app('preferences')->get('listPageSize', 50)->data; + $currency = $this->repository->getAccountCurrency($account) ?? Amount::getDefaultCurrency(); + $subTitle = (string) trans('firefly.all_journals_for_account', ['name' => $account->name]); + $periods = new Collection(); /** @var GroupCollectorInterface $collector */ - $collector = app(GroupCollectorInterface::class); + $collector = app(GroupCollectorInterface::class); $collector->setAccounts(new Collection([$account]))->setLimit($pageSize)->setPage($page)->withAccountInformation()->withCategoryInformation(); // this search will not include transaction groups where this asset account (or liability) // is just part of ONE of the journals. To force this: $collector->setExpandGroupSearch(true); - $groups = $collector->getPaginatedGroups(); + $groups = $collector->getPaginatedGroups(); $groups->setPath(route('accounts.show.all', [$account->id])); - $chartUrl = route('chart.account.period', [$account->id, $start->format('Y-m-d'), $end->format('Y-m-d')]); - $showAll = true; - $balances = Steam::filterAccountBalance(Steam::finalAccountBalance($account, $end), $account, $this->convertToNative, $accountCurrency); + $chartUrl = route('chart.account.period', [$account->id, $start->format('Y-m-d'), $end->format('Y-m-d')]); + $showAll = true; + $balances = Steam::filterAccountBalance(Steam::finalAccountBalance($account, $end), $account, $this->convertToNative, $accountCurrency); return view( 'accounts.show', diff --git a/app/Http/Controllers/Bill/IndexController.php b/app/Http/Controllers/Bill/IndexController.php index c2bf354790..cded2adb12 100644 --- a/app/Http/Controllers/Bill/IndexController.php +++ b/app/Http/Controllers/Bill/IndexController.php @@ -71,29 +71,30 @@ class IndexController extends Controller { $this->cleanupObjectGroups(); $this->repository->correctOrder(); - $start = session('start'); - $end = session('end'); - $collection = $this->repository->getBills(); - $total = $collection->count(); + $start = session('start'); + $end = session('end'); + $collection = $this->repository->getBills(); + $total = $collection->count(); - $parameters = new ParameterBag(); + $parameters = new ParameterBag(); // sub one day from temp start so the last paid date is one day before it should be. - $tempStart = clone $start; + $tempStart = clone $start; // 2023-06-23 do not sub one day from temp start, fix is in BillTransformer::payDates instead // $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 = app(BillTransformer::class); $transformer->setParameters($parameters); // loop all bills, convert to array and add rules and stuff. - $rules = $this->repository->getRulesForBills($collection); + $rules = $this->repository->getRulesForBills($collection); // make bill groups: - $bills = [ + $bills = [ 0 => [ // the index is the order, not the ID. 'object_group_id' => 0, 'object_group_title' => (string) trans('firefly.default_group_title_name'), @@ -126,9 +127,9 @@ class IndexController extends Controller ksort($bills); // summarise per currency / per group. - $sums = $this->getSums($bills); - $totals = $this->getTotals($sums); - $today = now()->startOfDay(); + $sums = $this->getSums($bills); + $totals = $this->getTotals($sums); + $today = now()->startOfDay(); return view('bills.index', compact('bills', 'sums', 'total', 'totals', 'today')); } diff --git a/app/Http/Controllers/Chart/AccountController.php b/app/Http/Controllers/Chart/AccountController.php index d425816335..d4f95e0334 100644 --- a/app/Http/Controllers/Chart/AccountController.php +++ b/app/Http/Controllers/Chart/AccountController.php @@ -418,8 +418,8 @@ class AccountController extends Controller public function period(Account $account, Carbon $start, Carbon $end): JsonResponse { Log::debug('Now in period()'); - $chartData = []; - $cache = new CacheProperties(); + $chartData = []; + $cache = new CacheProperties(); $cache->addProperty('chart.account.period'); $cache->addProperty($start); $cache->addProperty($end); @@ -430,23 +430,23 @@ class AccountController extends Controller } // collect and filter balances for the entire period. - $step = $this->calculateStep($start, $end); + $step = $this->calculateStep($start, $end); Log::debug(sprintf('Step is %s', $step)); - $locale = app('steam')->getLocale(); - $return = []; + $locale = app('steam')->getLocale(); + $return = []; // fix for issue https://github.com/firefly-iii/firefly-iii/issues/8041 // have to make sure this chart is always based on the balance at the END of the period. // This period depends on the size of the chart - $current = clone $start; - $current = app('navigation')->endOfX($current, $step, null); - $format = (string) trans('config.month_and_day_js', [], $locale); - $accountCurrency = $this->accountRepository->getAccountCurrency($account); + $current = clone $start; + $current = app('navigation')->endOfX($current, $step, null); + $format = (string) trans('config.month_and_day_js', [], $locale); + $accountCurrency = $this->accountRepository->getAccountCurrency($account); Log::debug('One'); - $range = Steam::finalAccountBalanceInRange($account, $start, $end, $this->convertToNative); + $range = Steam::finalAccountBalanceInRange($account, $start, $end, $this->convertToNative); Log::debug('Two'); - $range = Steam::filterAccountBalances($range, $account, $this->convertToNative, $accountCurrency); + $range = Steam::filterAccountBalances($range, $account, $this->convertToNative, $accountCurrency); Log::debug('Three'); // temp, get end balance. @@ -455,29 +455,29 @@ class AccountController extends Controller Log::debug('END temp get end balance done'); $previous = array_values($range)[0]; - $accountCurrency = $accountCurrency ?? $this->defaultCurrency; // do this AFTER getting the balances. + $accountCurrency ??= $this->defaultCurrency; // do this AFTER getting the balances. Log::debug('Start chart loop.'); - $newRange = []; - $expectedIndex = 0; + $newRange = []; + $expectedIndex = 0; Log::debug('Balances exist at:'); foreach ($range as $key => $value) { $newRange[] = ['date' => $key, 'info' => $value]; Log::debug(sprintf(' - %s', $key)); } - $carbon = Carbon::createFromFormat('Y-m-d', $newRange[0]['date']); + $carbon = Carbon::createFromFormat('Y-m-d', $newRange[0]['date']); while ($end->gte($current)) { $momentBalance = $previous; - $theDate = $current->format('Y-m-d'); + $theDate = $current->format('Y-m-d'); while ($carbon->lte($current) && array_key_exists($expectedIndex, $newRange)) { $momentBalance = $newRange[$expectedIndex]['info']; Log::debug(sprintf('Expected index is %d!, date is %s, current is %s', $expectedIndex, $carbon->format('Y-m-d'), $current->format('Y-m-d'))); - $carbon = Carbon::createFromFormat('Y-m-d', $newRange[$expectedIndex]['date']); - $expectedIndex++; + $carbon = Carbon::createFromFormat('Y-m-d', $newRange[$expectedIndex]['date']); + ++$expectedIndex; } - $return = $this->updateChartKeys($return, $momentBalance); - $previous = $momentBalance; + $return = $this->updateChartKeys($return, $momentBalance); + $previous = $momentBalance; Log::debug(sprintf('Now at %s', $theDate), $momentBalance); @@ -487,15 +487,15 @@ class AccountController extends Controller $label = $current->isoFormat($format); $return[$key]['entries'][$label] = $amount; } - $current = app('navigation')->addPeriod($current, $step, 0); + $current = app('navigation')->addPeriod($current, $step, 0); // here too, to fix #8041, the data is corrected to the end of the period. - $current = app('navigation')->endOfX($current, $step, null); + $current = app('navigation')->endOfX($current, $step, null); Log::debug(sprintf('Next moment is %s', $current->format('Y-m-d'))); } Log::debug('End of chart loop.'); // second loop (yes) to create nice array with info! Yay! - $chartData = []; + $chartData = []; foreach ($return as $key => $info) { if (3 === strlen($key)) { @@ -518,7 +518,7 @@ class AccountController extends Controller $chartData[] = $info; } - $data = $this->generator->multiSet($chartData); + $data = $this->generator->multiSet($chartData); $cache->store($data); return response()->json($data); diff --git a/app/Http/Controllers/Json/ReconcileController.php b/app/Http/Controllers/Json/ReconcileController.php index 70e4843c66..e27bda139a 100644 --- a/app/Http/Controllers/Json/ReconcileController.php +++ b/app/Http/Controllers/Json/ReconcileController.php @@ -123,7 +123,7 @@ 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 ?? '0', $endBalance ?? '0'), $clearedAmount?? '0'), $amount); + $difference = bcadd(bcadd(bcsub($startBalance ?? '0', $endBalance ?? '0'), $clearedAmount ?? '0'), $amount); $diffCompare = bccomp($difference, '0'); $countCleared = count($clearedJournals); $reconSum = bcadd(bcadd($startBalance ?? '0', $amount ?? '0'), $clearedAmount ?? '0'); diff --git a/app/Models/Bill.php b/app/Models/Bill.php index 712e9e2f58..34b934a62a 100644 --- a/app/Models/Bill.php +++ b/app/Models/Bill.php @@ -47,19 +47,19 @@ class Bill extends Model protected $casts = [ - 'created_at' => 'datetime', - 'updated_at' => 'datetime', - 'deleted_at' => 'datetime', - 'date' => SeparateTimezoneCaster::class, - 'end_date' => SeparateTimezoneCaster::class, - 'extension_date' => SeparateTimezoneCaster::class, - 'skip' => 'int', - 'automatch' => 'boolean', - 'active' => 'boolean', - 'name_encrypted' => 'boolean', - 'match_encrypted' => 'boolean', - 'amount_min' => 'string', - 'amount_max' => 'string', + 'created_at' => 'datetime', + 'updated_at' => 'datetime', + 'deleted_at' => 'datetime', + 'date' => SeparateTimezoneCaster::class, + 'end_date' => SeparateTimezoneCaster::class, + 'extension_date' => SeparateTimezoneCaster::class, + 'skip' => 'int', + 'automatch' => 'boolean', + 'active' => 'boolean', + 'name_encrypted' => 'boolean', + 'match_encrypted' => 'boolean', + 'amount_min' => 'string', + 'amount_max' => 'string', 'native_amount_min' => 'string', 'native_amount_max' => 'string', ]; diff --git a/app/Support/Steam.php b/app/Support/Steam.php index c075cf3ade..7d5ca78599 100644 --- a/app/Support/Steam.php +++ b/app/Support/Steam.php @@ -111,25 +111,26 @@ class Steam // sums up the balance changes per day, for foreign, native and normal amounts. - $set = $account->transactions() - ->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') - ->where('transaction_journals.date', '>=', $start->format('Y-m-d H:i:s')) - ->where('transaction_journals.date', '<=', $end->format('Y-m-d H:i:s')) - ->groupBy('transaction_journals.date') - ->groupBy('transactions.transaction_currency_id') - ->groupBy('transactions.foreign_currency_id') - ->orderBy('transaction_journals.date', 'ASC') - ->whereNull('transaction_journals.deleted_at') - ->get( - [ // @phpstan-ignore-line - 'transaction_journals.date', - 'transactions.transaction_currency_id', - 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'), - ] - ); + $set = $account->transactions() + ->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') + ->where('transaction_journals.date', '>=', $start->format('Y-m-d H:i:s')) + ->where('transaction_journals.date', '<=', $end->format('Y-m-d H:i:s')) + ->groupBy('transaction_journals.date') + ->groupBy('transactions.transaction_currency_id') + ->groupBy('transactions.foreign_currency_id') + ->orderBy('transaction_journals.date', 'ASC') + ->whereNull('transaction_journals.deleted_at') + ->get( + [ // @phpstan-ignore-line + 'transaction_journals.date', + 'transactions.transaction_currency_id', + 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'), + ] + ) + ; $currentBalance = $startBalance; @@ -148,22 +149,22 @@ class Steam Log::debug(sprintf('Processing transaction(s) on date %s', $carbon->format('Y-m-d H:i:s'))); // if convert to native, if NOT convert to native. - if($convertToNative) { - Log::debug(sprintf('Amount is %s %s, foreign amount is %s, native amount is %s', $entryCurrency->code, $this->bcround($modified,2), $this->bcround($foreignModified,2), $this->bcround($nativeModified,2))); + if ($convertToNative) { + Log::debug(sprintf('Amount is %s %s, foreign amount is %s, native amount is %s', $entryCurrency->code, $this->bcround($modified, 2), $this->bcround($foreignModified, 2), $this->bcround($nativeModified, 2))); // if the currency is the default currency add to native balance + currency balance - if($entry->transaction_currency_id === $defaultCurrency->id) { + if ($entry->transaction_currency_id === $defaultCurrency->id) { Log::debug('Add amount to native.'); - $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $modified); + $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $modified); } // add to native balance. - if($entry->foreign_currency_id !== $defaultCurrency->id) { + if ($entry->foreign_currency_id !== $defaultCurrency->id) { // this check is not necessary, because if the foreign currency is the same as the default currency, the native amount is zero. // so adding this would mean nothing. - $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeModified); + $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeModified); } - if($entry->foreign_currency_id === $defaultCurrency->id) { - $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $foreignModified); + if ($entry->foreign_currency_id === $defaultCurrency->id) { + $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $foreignModified); } // add to balance if is the same. if ($entry->transaction_currency_id === $accountCurrency?->id) { @@ -329,7 +330,7 @@ class Steam if ($native->id === $accountCurrency?->id) { $return['balance'] = bcadd('' === (string) $account->virtual_balance ? '0' : $account->virtual_balance, $return['balance']); } - Log::debug(sprintf('balance is (%s only) %s (with virtual balance)', $native->code, $this->bcround($return['balance'],2))); + Log::debug(sprintf('balance is (%s only) %s (with virtual balance)', $native->code, $this->bcround($return['balance'], 2))); // native balance $return['native_balance'] = (string) $account->transactions() @@ -340,7 +341,7 @@ class Steam ; // plus native virtual balance. $return['native_balance'] = bcadd('' === (string) $account->native_virtual_balance ? '0' : $account->native_virtual_balance, $return['native_balance']); - Log::debug(sprintf('native_balance is (all transactions to %s) %s (with virtual balance)', $native->code,$this->bcround( $return['native_balance']))); + Log::debug(sprintf('native_balance is (all transactions to %s) %s (with virtual balance)', $native->code, $this->bcround($return['native_balance']))); // plus foreign transactions in THIS currency. $sum = (string) $account->transactions() diff --git a/app/Transformers/BillTransformer.php b/app/Transformers/BillTransformer.php index 2229d94ace..a9de92351d 100644 --- a/app/Transformers/BillTransformer.php +++ b/app/Transformers/BillTransformer.php @@ -59,23 +59,23 @@ class BillTransformer extends AbstractTransformer */ public function transform(Bill $bill): array { - $defaultCurrency = $this->parameters->get('defaultCurrency') ?? Amount::getDefaultCurrency(); + $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; + $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; @@ -85,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')); } @@ -94,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 ( @@ -132,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')); } @@ -174,7 +174,7 @@ class BillTransformer extends AbstractTransformer 'links' => [ [ 'rel' => 'self', - 'uri' => '/bills/' . $bill->id, + 'uri' => '/bills/'.$bill->id, ], ], ]; @@ -196,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(); @@ -212,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. @@ -221,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,