2018-07-12 21:32:58 +02:00
|
|
|
<?php
|
2024-11-25 04:18:55 +01:00
|
|
|
|
2018-07-12 21:32:58 +02:00
|
|
|
/**
|
|
|
|
* ReconcileController.php
|
2020-01-31 07:32:04 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-07-12 21:32:58 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-07-12 21:32:58 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2018-07-12 21:32:58 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-07-12 21:32:58 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-07-12 21:32:58 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-07-12 21:32:58 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\Json;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2025-01-03 09:05:56 +01:00
|
|
|
use FireflyIII\Enums\TransactionTypeEnum;
|
2021-09-18 10:26:12 +02:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2019-05-30 12:31:19 +02:00
|
|
|
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
2018-07-12 21:32:58 +02:00
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
|
|
|
use FireflyIII\Models\Account;
|
2019-06-22 13:09:11 +02:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2018-07-12 21:32:58 +02:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2024-12-22 19:42:06 +01:00
|
|
|
use FireflyIII\Support\Facades\Steam;
|
2018-07-12 21:32:58 +02:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Collection;
|
2024-03-06 07:01:21 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-07-12 21:32:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ReconcileController
|
|
|
|
*/
|
|
|
|
class ReconcileController extends Controller
|
|
|
|
{
|
2023-02-22 18:14:14 +01:00
|
|
|
private AccountRepositoryInterface $accountRepos;
|
2018-07-12 21:32:58 +02:00
|
|
|
|
|
|
|
/**
|
2018-07-21 08:06:24 +02:00
|
|
|
* ReconcileController constructor.
|
2018-07-12 21:32:58 +02:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
// translations:
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
|
|
|
app('view')->share('mainTitleIcon', 'fa-credit-card');
|
2024-12-22 08:43:12 +01:00
|
|
|
app('view')->share('title', (string) trans('firefly.accounts'));
|
2023-02-22 18:14:14 +01:00
|
|
|
$this->accountRepos = app(AccountRepositoryInterface::class);
|
2018-07-12 21:32:58 +02:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-21 08:06:24 +02:00
|
|
|
* Overview of reconciliation.
|
|
|
|
*
|
2022-03-29 15:10:05 +02:00
|
|
|
* @throws FireflyException
|
2023-12-22 20:12:38 +01:00
|
|
|
*/
|
2024-03-18 20:25:30 +01:00
|
|
|
public function overview(Request $request, ?Account $account = null, ?Carbon $start = null, ?Carbon $end = null): JsonResponse
|
2018-07-12 21:32:58 +02:00
|
|
|
{
|
2019-06-22 13:09:11 +02:00
|
|
|
$startBalance = $request->get('startBalance');
|
|
|
|
$endBalance = $request->get('endBalance');
|
2024-12-30 10:51:34 +01:00
|
|
|
$accountCurrency = $this->accountRepos->getAccountCurrency($account) ?? $this->defaultCurrency;
|
2019-06-22 13:09:11 +02:00
|
|
|
$amount = '0';
|
|
|
|
$clearedAmount = '0';
|
2020-01-05 08:04:55 +01:00
|
|
|
|
2022-01-20 06:23:42 +01:00
|
|
|
if (null === $start && null === $end) {
|
|
|
|
throw new FireflyException('Invalid dates submitted.');
|
|
|
|
}
|
2020-01-05 08:04:55 +01:00
|
|
|
if ($end->lt($start)) {
|
|
|
|
[$start, $end] = [$end, $start];
|
|
|
|
}
|
2024-07-10 11:40:41 +02:00
|
|
|
$end->endOfDay();
|
|
|
|
$start->startOfDay();
|
2020-01-05 08:04:55 +01:00
|
|
|
|
2019-06-22 13:09:11 +02:00
|
|
|
$route = route('accounts.reconcile.submit', [$account->id, $start->format('Ymd'), $end->format('Ymd')]);
|
|
|
|
$selectedIds = $request->get('journals') ?? [];
|
|
|
|
$clearedJournals = [];
|
|
|
|
$clearedIds = $request->get('cleared') ?? [];
|
|
|
|
$journals = [];
|
2023-12-20 19:35:52 +01:00
|
|
|
// Collect all submitted journals
|
2019-06-22 13:09:11 +02:00
|
|
|
if (count($selectedIds) > 0) {
|
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
|
|
|
$collector->setJournalIds($selectedIds);
|
2024-01-01 14:43:56 +01:00
|
|
|
$journals = $collector->getExtractedJournals();
|
2019-06-22 13:09:11 +02:00
|
|
|
}
|
2019-05-31 13:35:33 +02:00
|
|
|
|
2023-12-20 19:35:52 +01:00
|
|
|
// Collect all journals already reconciled
|
2019-06-22 13:09:11 +02:00
|
|
|
if (count($clearedIds) > 0) {
|
|
|
|
/** @var GroupCollectorInterface $collector */
|
2024-01-01 14:43:56 +01:00
|
|
|
$collector = app(GroupCollectorInterface::class);
|
2019-06-22 13:09:11 +02:00
|
|
|
$collector->setJournalIds($clearedIds);
|
|
|
|
$clearedJournals = $collector->getExtractedJournals();
|
2018-07-12 21:32:58 +02:00
|
|
|
}
|
|
|
|
|
2019-06-22 13:09:11 +02:00
|
|
|
/** @var array $journal */
|
|
|
|
foreach ($journals as $journal) {
|
|
|
|
$amount = $this->processJournal($account, $accountCurrency, $journal, $amount);
|
2018-10-27 05:10:49 +02:00
|
|
|
}
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Final amount is %s', $amount));
|
2019-06-22 13:09:11 +02:00
|
|
|
|
|
|
|
/** @var array $journal */
|
|
|
|
foreach ($clearedJournals as $journal) {
|
|
|
|
if ($journal['date'] <= $end) {
|
|
|
|
$clearedAmount = $this->processJournal($account, $accountCurrency, $journal, $clearedAmount);
|
2018-07-12 21:32:58 +02:00
|
|
|
}
|
|
|
|
}
|
2024-03-06 07:01:21 +01:00
|
|
|
Log::debug(sprintf('Start balance: "%s"', $startBalance));
|
|
|
|
Log::debug(sprintf('End balance: "%s"', $endBalance));
|
|
|
|
Log::debug(sprintf('Cleared amount: "%s"', $clearedAmount));
|
|
|
|
Log::debug(sprintf('Amount: "%s"', $amount));
|
2025-01-03 14:56:06 +01:00
|
|
|
$difference = bcadd(bcadd(bcsub($startBalance ?? '0', $endBalance ?? '0'), $clearedAmount), $amount);
|
2024-01-01 14:43:56 +01:00
|
|
|
$diffCompare = bccomp($difference, '0');
|
|
|
|
$countCleared = count($clearedJournals);
|
2025-01-03 14:56:06 +01:00
|
|
|
$reconSum = bcadd(bcadd($startBalance ?? '0', $amount), $clearedAmount);
|
2018-08-07 19:29:40 +02:00
|
|
|
|
|
|
|
try {
|
2024-03-06 07:01:21 +01:00
|
|
|
$view = view('accounts.reconcile.overview', compact('account', 'start', 'diffCompare', 'difference', 'end', 'clearedAmount', 'startBalance', 'endBalance', 'amount', 'route', 'countCleared', 'reconSum', 'selectedIds'))->render();
|
2023-12-20 19:35:52 +01:00
|
|
|
} catch (\Throwable $e) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('View error: %s', $e->getMessage()));
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($e->getTraceAsString());
|
2019-06-29 19:47:31 +02:00
|
|
|
$view = sprintf('Could not render accounts.reconcile.overview: %s', $e->getMessage());
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-11-02 06:25:37 +01:00
|
|
|
throw new FireflyException($view, 0, $e);
|
2018-08-07 19:29:40 +02:00
|
|
|
}
|
2021-04-07 07:28:43 +02:00
|
|
|
|
2024-03-06 07:01:21 +01:00
|
|
|
$return = ['post_url' => $route, 'html' => $view];
|
2018-07-12 21:32:58 +02:00
|
|
|
|
|
|
|
return response()->json($return);
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:16:01 +01:00
|
|
|
private function processJournal(Account $account, TransactionCurrency $currency, array $journal, string $amount): string
|
|
|
|
{
|
|
|
|
$toAdd = '0';
|
|
|
|
app('log')->debug(sprintf('User submitted %s #%d: "%s"', $journal['transaction_type_type'], $journal['transaction_journal_id'], $journal['description']));
|
|
|
|
|
|
|
|
// not much magic below we need to cover using tests.
|
|
|
|
|
|
|
|
if ($account->id === $journal['source_account_id']) {
|
|
|
|
if ($currency->id === $journal['currency_id']) {
|
|
|
|
$toAdd = $journal['amount'];
|
|
|
|
}
|
|
|
|
if (null !== $journal['foreign_currency_id'] && $journal['foreign_currency_id'] === $currency->id) {
|
|
|
|
$toAdd = $journal['foreign_amount'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($account->id === $journal['destination_account_id']) {
|
|
|
|
if ($currency->id === $journal['currency_id']) {
|
|
|
|
$toAdd = bcmul($journal['amount'], '-1');
|
|
|
|
}
|
|
|
|
if (null !== $journal['foreign_currency_id'] && $journal['foreign_currency_id'] === $currency->id) {
|
|
|
|
$toAdd = bcmul($journal['foreign_amount'], '-1');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
app('log')->debug(sprintf('Going to add %s to %s', $toAdd, $amount));
|
|
|
|
$amount = bcadd($amount, $toAdd);
|
|
|
|
app('log')->debug(sprintf('Result is %s', $amount));
|
|
|
|
|
|
|
|
return $amount;
|
|
|
|
}
|
|
|
|
|
2018-07-12 21:32:58 +02:00
|
|
|
/**
|
2018-07-21 08:06:24 +02:00
|
|
|
* Returns a list of transactions in a modal.
|
|
|
|
*
|
2021-05-24 08:54:58 +02:00
|
|
|
* @return JsonResponse
|
2023-12-20 19:35:52 +01:00
|
|
|
*
|
2021-09-18 10:26:12 +02:00
|
|
|
* @throws FireflyException
|
2023-12-22 20:12:38 +01:00
|
|
|
*/
|
2024-03-18 20:25:30 +01:00
|
|
|
public function transactions(Account $account, ?Carbon $start = null, ?Carbon $end = null)
|
2018-07-12 21:32:58 +02:00
|
|
|
{
|
2022-01-20 06:23:42 +01:00
|
|
|
if (null === $start || null === $end) {
|
|
|
|
throw new FireflyException('Invalid dates submitted.');
|
|
|
|
}
|
2020-01-05 08:04:55 +01:00
|
|
|
if ($end->lt($start)) {
|
|
|
|
[$end, $start] = [$start, $end];
|
|
|
|
}
|
2024-01-01 14:43:56 +01:00
|
|
|
$startDate = clone $start;
|
2019-06-22 13:09:11 +02:00
|
|
|
$startDate->subDay();
|
2024-03-03 10:13:49 +01:00
|
|
|
$end->endOfDay();
|
2018-07-12 21:32:58 +02:00
|
|
|
|
2024-12-30 10:51:34 +01:00
|
|
|
$currency = $this->accountRepos->getAccountCurrency($account) ?? $this->defaultCurrency;
|
2024-12-22 19:42:06 +01:00
|
|
|
$startBalance = Steam::finalAccountBalance($account, $startDate)['balance'];
|
|
|
|
$endBalance = Steam::finalAccountBalance($account, $end)['balance'];
|
2020-10-18 16:36:19 +02:00
|
|
|
|
2018-07-12 21:32:58 +02:00
|
|
|
// get the transactions
|
|
|
|
$selectionStart = clone $start;
|
|
|
|
$selectionStart->subDays(3);
|
2024-01-01 14:43:56 +01:00
|
|
|
$selectionEnd = clone $end;
|
2018-07-12 21:32:58 +02:00
|
|
|
$selectionEnd->addDays(3);
|
|
|
|
|
|
|
|
// grab transactions:
|
2019-05-30 12:31:19 +02:00
|
|
|
/** @var GroupCollectorInterface $collector */
|
2024-01-01 14:43:56 +01:00
|
|
|
$collector = app(GroupCollectorInterface::class);
|
2019-05-30 12:31:19 +02:00
|
|
|
|
2018-07-12 21:32:58 +02:00
|
|
|
$collector->setAccounts(new Collection([$account]))
|
2023-12-20 19:35:52 +01:00
|
|
|
->setRange($selectionStart, $selectionEnd)
|
|
|
|
->withBudgetInformation()->withCategoryInformation()->withAccountInformation()
|
|
|
|
;
|
2024-01-01 14:43:56 +01:00
|
|
|
$array = $collector->getExtractedJournals();
|
|
|
|
$journals = $this->processTransactions($account, $array);
|
2019-06-22 13:09:11 +02:00
|
|
|
|
2018-08-07 19:29:40 +02:00
|
|
|
try {
|
2022-01-29 14:11:12 +01:00
|
|
|
$html = view(
|
2020-03-17 15:01:00 +01:00
|
|
|
'accounts.reconcile.transactions',
|
|
|
|
compact('account', 'journals', 'currency', 'start', 'end', 'selectionStart', 'selectionEnd')
|
|
|
|
)->render();
|
2023-12-20 19:35:52 +01:00
|
|
|
} catch (\Throwable $e) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Could not render: %s', $e->getMessage()));
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($e->getTraceAsString());
|
2019-06-22 13:09:11 +02:00
|
|
|
$html = sprintf('Could not render accounts.reconcile.transactions: %s', $e->getMessage());
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-11-02 06:25:37 +01:00
|
|
|
throw new FireflyException($html, 0, $e);
|
2018-08-07 19:29:40 +02:00
|
|
|
}
|
2021-09-18 10:26:12 +02:00
|
|
|
|
2018-07-12 21:32:58 +02:00
|
|
|
return response()->json(['html' => $html, 'startBalance' => $startBalance, 'endBalance' => $endBalance]);
|
|
|
|
}
|
2019-06-22 13:09:11 +02:00
|
|
|
|
2020-10-18 16:36:19 +02:00
|
|
|
/**
|
|
|
|
* "fix" amounts to make it easier on the reconciliation overview:
|
|
|
|
*/
|
|
|
|
private function processTransactions(Account $account, array $array): array
|
|
|
|
{
|
|
|
|
$journals = [];
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2020-10-18 16:36:19 +02:00
|
|
|
/** @var array $journal */
|
|
|
|
foreach ($array as $journal) {
|
2024-01-01 14:43:56 +01:00
|
|
|
$inverse = false;
|
2021-04-07 07:28:43 +02:00
|
|
|
|
2025-01-03 09:09:15 +01:00
|
|
|
if (TransactionTypeEnum::DEPOSIT->value === $journal['transaction_type_type']) {
|
2020-10-18 16:36:19 +02:00
|
|
|
$inverse = true;
|
|
|
|
}
|
|
|
|
// transfer to this account? then positive amount:
|
2025-01-03 09:05:56 +01:00
|
|
|
if (TransactionTypeEnum::TRANSFER->value === $journal['transaction_type_type'] && $account->id === $journal['destination_account_id']) {
|
2020-10-18 16:36:19 +02:00
|
|
|
$inverse = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// opening balance into account? then positive amount:
|
2025-01-03 09:09:15 +01:00
|
|
|
if (TransactionTypeEnum::OPENING_BALANCE->value === $journal['transaction_type_type']
|
2020-10-18 16:36:19 +02:00
|
|
|
&& $account->id === $journal['destination_account_id']) {
|
|
|
|
$inverse = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (true === $inverse) {
|
|
|
|
$journal['amount'] = app('steam')->positive($journal['amount']);
|
|
|
|
if (null !== $journal['foreign_amount']) {
|
|
|
|
$journal['foreign_amount'] = app('steam')->positive($journal['foreign_amount']);
|
|
|
|
}
|
|
|
|
}
|
2021-04-07 07:28:43 +02:00
|
|
|
|
2020-10-18 16:36:19 +02:00
|
|
|
$journals[] = $journal;
|
|
|
|
}
|
2021-03-21 09:15:40 +01:00
|
|
|
|
2020-10-18 16:36:19 +02:00
|
|
|
return $journals;
|
|
|
|
}
|
2018-07-22 20:32:02 +02:00
|
|
|
}
|