mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-19 19:01:58 +00:00
Create reconciliation transaction.
This commit is contained in:
@@ -23,11 +23,13 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Http\Controllers\Account;
|
namespace FireflyIII\Http\Controllers\Account;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
||||||
use FireflyIII\Http\Controllers\Controller;
|
use FireflyIII\Http\Controllers\Controller;
|
||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
use FireflyIII\Models\AccountType;
|
use FireflyIII\Models\AccountType;
|
||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@@ -35,6 +37,7 @@ use Illuminate\Support\Collection;
|
|||||||
use Navigation;
|
use Navigation;
|
||||||
use Preferences;
|
use Preferences;
|
||||||
use Response;
|
use Response;
|
||||||
|
use Session;
|
||||||
use View;
|
use View;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -67,9 +70,13 @@ class ReconcileController extends Controller
|
|||||||
* @param Carbon $end
|
* @param Carbon $end
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Http\JsonResponse
|
* @return \Illuminate\Http\JsonResponse
|
||||||
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
public function overview(Request $request, Account $account, Carbon $start, Carbon $end)
|
public function overview(Request $request, Account $account, Carbon $start, Carbon $end)
|
||||||
{
|
{
|
||||||
|
if ($account->accountType->type !== AccountType::ASSET) {
|
||||||
|
throw new FireflyException(sprintf('Account %s is not an asset account.', $account->name));
|
||||||
|
}
|
||||||
$startBalance = $request->get('startBalance');
|
$startBalance = $request->get('startBalance');
|
||||||
$endBalance = $request->get('endBalance');
|
$endBalance = $request->get('endBalance');
|
||||||
$transactionIds = $request->get('transactions') ?? [];
|
$transactionIds = $request->get('transactions') ?? [];
|
||||||
@@ -93,6 +100,11 @@ class ReconcileController extends Controller
|
|||||||
$clearedAmount = bcadd($clearedAmount, $transaction->amount);
|
$clearedAmount = bcadd($clearedAmount, $transaction->amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// final difference:
|
||||||
|
//{% set diff = (startBalance - endBalance) + clearedAmount + amount %}
|
||||||
|
$difference = bcadd(bcadd(bcsub($startBalance, $endBalance), $clearedAmount), $amount);
|
||||||
|
$diffCompare = bccomp($difference, '0');
|
||||||
|
|
||||||
$return = [
|
$return = [
|
||||||
'is_zero' => false,
|
'is_zero' => false,
|
||||||
'post_uri' => $route,
|
'post_uri' => $route,
|
||||||
@@ -100,7 +112,10 @@ class ReconcileController extends Controller
|
|||||||
];
|
];
|
||||||
$return['html'] = view(
|
$return['html'] = view(
|
||||||
'accounts.reconcile.overview',
|
'accounts.reconcile.overview',
|
||||||
compact('account', 'start', 'end', 'clearedIds', 'transactionIds', 'clearedAmount', 'startBalance', 'endBalance', 'amount', 'route')
|
compact(
|
||||||
|
'account', 'start', 'diffCompare', 'difference', 'end', 'clearedIds', 'transactionIds', 'clearedAmount', 'startBalance', 'endBalance', 'amount',
|
||||||
|
'route'
|
||||||
|
)
|
||||||
)->render();
|
)->render();
|
||||||
|
|
||||||
return Response::json($return);
|
return Response::json($return);
|
||||||
@@ -118,6 +133,11 @@ class ReconcileController extends Controller
|
|||||||
if (AccountType::INITIAL_BALANCE === $account->accountType->type) {
|
if (AccountType::INITIAL_BALANCE === $account->accountType->type) {
|
||||||
return $this->redirectToOriginalAccount($account);
|
return $this->redirectToOriginalAccount($account);
|
||||||
}
|
}
|
||||||
|
if (AccountType::ASSET !== $account->accountType->type) {
|
||||||
|
Session::flash('error', trans('firefly.must_be_asset_account'));
|
||||||
|
|
||||||
|
return redirect(route('accounts.index', [config('firefly.shortNamesByFullName.' . $account->accountType->type)]));
|
||||||
|
}
|
||||||
/** @var CurrencyRepositoryInterface $currencyRepos */
|
/** @var CurrencyRepositoryInterface $currencyRepos */
|
||||||
$currencyRepos = app(CurrencyRepositoryInterface::class);
|
$currencyRepos = app(CurrencyRepositoryInterface::class);
|
||||||
$currencyId = intval($account->getMeta('currency_id'));
|
$currencyId = intval($account->getMeta('currency_id'));
|
||||||
@@ -170,6 +190,52 @@ class ReconcileController extends Controller
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Account $account
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*/
|
||||||
|
public function submit(Request $request, Account $account, Carbon $start, Carbon $end)
|
||||||
|
{
|
||||||
|
/** @var JournalRepositoryInterface $repository */
|
||||||
|
$repository = app(JournalRepositoryInterface::class);
|
||||||
|
$transactions = $repository->getTransactionsById($request->get('transactions'));
|
||||||
|
/** @var Transaction $transaction */
|
||||||
|
foreach ($transactions as $transaction) {
|
||||||
|
$repository->reconcile($transaction); // mark as reconciled.
|
||||||
|
}
|
||||||
|
|
||||||
|
// create reconciliation transaction (if necessary):
|
||||||
|
if ($request->get('reconcile') === 'create') {
|
||||||
|
/** @var AccountRepositoryInterface $accountRepos */
|
||||||
|
$accountRepos = app(AccountRepositoryInterface::class);
|
||||||
|
$reconciliation = $accountRepos->getReconciliation($account);
|
||||||
|
$difference = $request->get('difference');
|
||||||
|
|
||||||
|
// store journal between these two.
|
||||||
|
$data = [
|
||||||
|
'what' => 'Reconciliation',
|
||||||
|
'source' => $account,
|
||||||
|
'destination' => $reconciliation,
|
||||||
|
'category' => '',
|
||||||
|
'budget_id' => 0,
|
||||||
|
'amount' => $difference,
|
||||||
|
'currency_id' => $account->getMeta('currency_id'),
|
||||||
|
'description' => 'Reconciliation [period]',
|
||||||
|
'date' => $request->get('end'),
|
||||||
|
];
|
||||||
|
$journal = $repository->store($data);
|
||||||
|
// reconcile this transaction too:
|
||||||
|
$transaction = $journal->transactions()->first();
|
||||||
|
$repository->reconcile($transaction);
|
||||||
|
}
|
||||||
|
Session::flash('success', trans('firefly.reconciliation_stored'));
|
||||||
|
|
||||||
|
return redirect(route('accounts.show', [$account->id]));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Account $account
|
* @param Account $account
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
@@ -199,13 +265,4 @@ class ReconcileController extends Controller
|
|||||||
|
|
||||||
return Response::json(['html' => $html]);
|
return Response::json(['html' => $html]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Account $account
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
*/
|
|
||||||
public function submit(Request $request, Account $account, Carbon $start, Carbon $end) {
|
|
||||||
var_dump($request->all());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -109,6 +109,15 @@ interface AccountRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function getCashAccount(): Account;
|
public function getCashAccount(): Account;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find or create the opposing reconciliation account.
|
||||||
|
*
|
||||||
|
* @param Account $account
|
||||||
|
*
|
||||||
|
* @return Account|null
|
||||||
|
*/
|
||||||
|
public function getReconciliation(Account $account): ?Account;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the date of the very last transaction in this account.
|
* Returns the date of the very last transaction in this account.
|
||||||
*
|
*
|
||||||
|
@@ -22,6 +22,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace FireflyIII\Repositories\Account;
|
namespace FireflyIII\Repositories\Account;
|
||||||
|
|
||||||
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
use FireflyIII\Models\AccountType;
|
use FireflyIII\Models\AccountType;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
@@ -222,4 +223,37 @@ trait FindAccountsTrait
|
|||||||
|
|
||||||
return $account;
|
return $account;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Account $account
|
||||||
|
*
|
||||||
|
* @return Account|null
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
public function getReconciliation(Account $account): ?Account
|
||||||
|
{
|
||||||
|
if ($account->accountType->type !== AccountType::ASSET) {
|
||||||
|
throw new FireflyException(sprintf('%s is not an asset account.', $account->name));
|
||||||
|
}
|
||||||
|
$name = $account->name . ' reconciliation';
|
||||||
|
$type = AccountType::where('type', AccountType::RECONCILIATION)->first();
|
||||||
|
$accounts = $this->user->accounts()->where('account_type_id', $type->id)->get();
|
||||||
|
/** @var Account $account */
|
||||||
|
foreach ($accounts as $account) {
|
||||||
|
if ($account->name === $name) {
|
||||||
|
return $account;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// assume nothing was found. create it!
|
||||||
|
$data = [
|
||||||
|
'accountType' => 'reconcile',
|
||||||
|
'name' => $name,
|
||||||
|
'iban' => null,
|
||||||
|
'virtualBalance' => null,
|
||||||
|
'active' => true,
|
||||||
|
];
|
||||||
|
$account = $this->storeAccount($data);
|
||||||
|
|
||||||
|
return $account;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -67,6 +67,11 @@ trait SupportJournalsTrait
|
|||||||
$accounts['source'] = Account::where('user_id', $user->id)->where('id', $data['source_account_id'])->first();
|
$accounts['source'] = Account::where('user_id', $user->id)->where('id', $data['source_account_id'])->first();
|
||||||
$accounts['destination'] = Account::where('user_id', $user->id)->where('id', $data['destination_account_id'])->first();
|
$accounts['destination'] = Account::where('user_id', $user->id)->where('id', $data['destination_account_id'])->first();
|
||||||
break;
|
break;
|
||||||
|
case TransactionType::RECONCILIATION:
|
||||||
|
$accounts['source'] = $data['source'];
|
||||||
|
$accounts['destination'] = $data['destination'];
|
||||||
|
unset($data['source'], $data['destination']);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new FireflyException(sprintf('Did not recognise transaction type "%s".', $type->type));
|
throw new FireflyException(sprintf('Did not recognise transaction type "%s".', $type->type));
|
||||||
}
|
}
|
||||||
@@ -229,6 +234,9 @@ trait SupportJournalsTrait
|
|||||||
$check = 'destination';
|
$check = 'destination';
|
||||||
}
|
}
|
||||||
switch ($transactionType->type) {
|
switch ($transactionType->type) {
|
||||||
|
case TransactionType::RECONCILIATION:
|
||||||
|
// do nothing.
|
||||||
|
break;
|
||||||
case TransactionType::DEPOSIT:
|
case TransactionType::DEPOSIT:
|
||||||
case TransactionType::WITHDRAWAL:
|
case TransactionType::WITHDRAWAL:
|
||||||
// continue:
|
// continue:
|
||||||
|
@@ -113,6 +113,7 @@ return [
|
|||||||
'opening' => 'Initial balance account',
|
'opening' => 'Initial balance account',
|
||||||
'initial' => 'Initial balance account',
|
'initial' => 'Initial balance account',
|
||||||
'import' => 'Import account',
|
'import' => 'Import account',
|
||||||
|
'reconcile' => 'Reconciliation account',
|
||||||
],
|
],
|
||||||
'shortNamesByFullName' =>
|
'shortNamesByFullName' =>
|
||||||
[
|
[
|
||||||
|
@@ -654,6 +654,9 @@ return [
|
|||||||
'create_neg_reconcile_transaction' => 'Clear the selected transactions, and create a correction removing :amount from this asset account.',
|
'create_neg_reconcile_transaction' => 'Clear the selected transactions, and create a correction removing :amount from this asset account.',
|
||||||
'reconcile_do_nothing' => 'Clear the selected transactions, but do not correct.',
|
'reconcile_do_nothing' => 'Clear the selected transactions, but do not correct.',
|
||||||
'reconcile_go_back' => 'You can always edit or delete a correction later.',
|
'reconcile_go_back' => 'You can always edit or delete a correction later.',
|
||||||
|
'must_be_asset_account' => 'You can only reconcile asset accounts',
|
||||||
|
'reconciliation_stored' => 'Reconciliation stored',
|
||||||
|
'reconcile_this_account' => 'Reconcile this account',
|
||||||
'confirm_reconciliation' => 'Confirm reconciliation',
|
'confirm_reconciliation' => 'Confirm reconciliation',
|
||||||
'submitted_start_balance' => 'Submitted start balance',
|
'submitted_start_balance' => 'Submitted start balance',
|
||||||
'selected_transactions' => 'Selected transactions (:count)',
|
'selected_transactions' => 'Selected transactions (:count)',
|
||||||
|
@@ -35,38 +35,42 @@
|
|||||||
<td>{{ 'submitted_end_balance'|_ }} (date)</td>
|
<td>{{ 'submitted_end_balance'|_ }} (date)</td>
|
||||||
<td>{{ endBalance|formatAmount }}</td>
|
<td>{{ endBalance|formatAmount }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% set diff = (startBalance - endBalance) + clearedAmount + amount %}
|
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ 'difference'|_ }}</td>
|
<td>{{ 'difference'|_ }}</td>
|
||||||
<td>{{ diff|formatAmount }}</td>
|
<td>
|
||||||
|
{{ difference|formatAmount }}
|
||||||
|
<input type="hidden" name="difference" value="{{ difference }}" />
|
||||||
|
</td>
|
||||||
|
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
<p>
|
<p>
|
||||||
{% if diff > 0 %}
|
{% if diffCompare > 0 %}
|
||||||
{{ 'reconcile_has_more'|_ }}
|
{{ 'reconcile_has_more'|_ }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if diff < 0 %}
|
{% if diffCompare < 0 %}
|
||||||
{{ 'reconcile_has_less'|_ }}
|
{{ 'reconcile_has_less'|_ }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</p>
|
</p>
|
||||||
{% if diff == 0 %}
|
{% if diffCompare == 0 %}
|
||||||
<p>
|
<p>
|
||||||
{{ 'reconcile_is_equal'|_ }}
|
{{ 'reconcile_is_equal'|_ }}
|
||||||
</p>
|
</p>
|
||||||
<input type="hidden" name="reconcile" value="nothing">
|
<input type="hidden" name="reconcile" value="nothing">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if diff != 0 %}
|
{% if diffCompare != 0 %}
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<div class="col-lg-12">
|
<div class="col-lg-12">
|
||||||
<div class="radio">
|
<div class="radio">
|
||||||
<label>
|
<label>
|
||||||
|
|
||||||
<input type="radio" name="reconcile" value="create">
|
<input type="radio" name="reconcile" value="create">
|
||||||
{% if diff > 0 %}
|
{% if diffCompare > 0 %}
|
||||||
{{ trans('firefly.create_neg_reconcile_transaction', {amount: (diff*-1)|formatAmount})|raw }}
|
{{ trans('firefly.create_neg_reconcile_transaction', {amount: (difference*-1)|formatAmount})|raw }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if diff < 0 %}
|
{% if diffCompare < 0 %}
|
||||||
{{ trans('firefly.create_pos_reconcile_transaction', {amount: (diff*-1)|formatAmount})|raw }}
|
{{ trans('firefly.create_pos_reconcile_transaction', {amount: (difference*-1)|formatAmount})|raw }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
@@ -18,7 +18,7 @@
|
|||||||
{% for account in accounts %}
|
{% for account in accounts %}
|
||||||
<tr>
|
<tr>
|
||||||
<td class="hidden-sm hidden-xs">
|
<td class="hidden-sm hidden-xs">
|
||||||
<div class="btn-group btn-group-xs edit_tr_buttons"><a class="btn btn-default btn-xs" title="{{ 'edit'|_ }}" href="{{ route('accounts.edit',account.id) }}"><i class="fa fa-fw fa-pencil"></i></a><a class="btn btn-default btn-xs" title="{{ 'reconcile'|_ }}" href="{{ route('accounts.reconcile',account.id) }}"><i class="fa fa-fw fa-check"></i></a><a class="btn btn-danger btn-xs" title="{{ 'delete'|_ }}" href="{{ route('accounts.delete',account.id) }}"><i class="fa fa-fw fa-trash-o"></i></a></div>
|
<div class="btn-group btn-group-xs edit_tr_buttons"><a class="btn btn-default btn-xs" title="{{ 'edit'|_ }}" href="{{ route('accounts.edit',account.id) }}"><i class="fa fa-fw fa-pencil"></i></a>{% if what == 'asset' %}<a class="btn btn-default btn-xs" title="{{ 'reconcile'|_ }}" href="{{ route('accounts.reconcile',account.id) }}"><i class="fa fa-fw fa-check"></i></a>{% endif %}<a class="btn btn-danger btn-xs" title="{{ 'delete'|_ }}" href="{{ route('accounts.delete',account.id) }}"><i class="fa fa-fw fa-trash-o"></i></a></div>
|
||||||
</td>
|
</td>
|
||||||
<td data-value="{{ account.name }}"><a href="{{ route('accounts.show',account.id) }}">{{ account.name }}</a></td>
|
<td data-value="{{ account.name }}"><a href="{{ route('accounts.show',account.id) }}">{{ account.name }}</a></td>
|
||||||
{% if what == "asset" %}
|
{% if what == "asset" %}
|
||||||
|
Reference in New Issue
Block a user