2017-11-14 23:29:49 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ReconcileController.php
|
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This file is part of Firefly III.
|
|
|
|
*
|
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 07:41:58 -06:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2017-11-14 23:29:49 -06:00
|
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\Account;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2017-11-22 10:49:06 -06:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2017-11-14 23:29:49 -06:00
|
|
|
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
2018-02-23 08:13:01 -06:00
|
|
|
use FireflyIII\Http\Requests\ReconciliationStoreRequest;
|
|
|
|
use FireflyIII\Http\Requests\ReconciliationUpdateRequest;
|
2017-11-14 23:29:49 -06:00
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
use FireflyIII\Models\AccountType;
|
2017-11-22 09:54:49 -06:00
|
|
|
use FireflyIII\Models\Transaction;
|
2017-11-24 14:51:07 -06:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
|
|
|
use FireflyIII\Models\TransactionType;
|
2017-11-22 10:49:06 -06:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2017-11-14 23:29:49 -06:00
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
2017-11-22 09:54:49 -06:00
|
|
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
2017-11-14 23:29:49 -06:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Collection;
|
2018-02-23 08:13:01 -06:00
|
|
|
use Log;
|
2017-11-14 23:29:49 -06:00
|
|
|
use Preferences;
|
2017-11-22 10:49:06 -06:00
|
|
|
use Session;
|
2017-11-14 23:29:49 -06:00
|
|
|
|
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class ReconcileController.
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-11-25 08:20:53 -06:00
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
2017-11-14 23:29:49 -06:00
|
|
|
*/
|
|
|
|
class ReconcileController extends Controller
|
|
|
|
{
|
2018-02-25 12:09:05 -06:00
|
|
|
/** @var JournalRepositoryInterface */
|
|
|
|
private $repository;
|
|
|
|
|
2017-11-14 23:29:49 -06:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
// translations:
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
2017-12-16 12:46:36 -06:00
|
|
|
app('view')->share('mainTitleIcon', 'fa-credit-card');
|
|
|
|
app('view')->share('title', trans('firefly.accounts'));
|
2018-02-25 12:09:05 -06:00
|
|
|
$this->repository = app(JournalRepositoryInterface::class);
|
2017-11-14 23:29:49 -06:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-24 14:51:07 -06:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
*/
|
|
|
|
public function edit(TransactionJournal $journal)
|
|
|
|
{
|
2017-12-22 11:32:43 -06:00
|
|
|
if (TransactionType::RECONCILIATION !== $journal->transactionType->type) {
|
2017-11-24 14:51:07 -06:00
|
|
|
return redirect(route('transactions.edit', [$journal->id]));
|
|
|
|
}
|
|
|
|
// view related code
|
|
|
|
$subTitle = trans('breadcrumbs.edit_journal', ['description' => $journal->description]);
|
|
|
|
|
|
|
|
// journal related code
|
2018-02-25 12:09:05 -06:00
|
|
|
$pTransaction = $this->repository->getFirstPosTransaction($journal);
|
2017-11-24 14:51:07 -06:00
|
|
|
$preFilled = [
|
2018-02-25 12:09:05 -06:00
|
|
|
'date' => $this->repository->getJournalDate($journal, null),
|
|
|
|
'category' => $this->repository->getJournalCategoryName($journal),
|
2017-11-24 14:51:07 -06:00
|
|
|
'tags' => join(',', $journal->tags->pluck('tag')->toArray()),
|
|
|
|
'amount' => $pTransaction->amount,
|
|
|
|
];
|
|
|
|
|
|
|
|
Session::flash('preFilled', $preFilled);
|
|
|
|
|
|
|
|
// put previous url in session if not redirect from store (not "return_to_edit").
|
|
|
|
if (true !== session('reconcile.edit.fromUpdate')) {
|
|
|
|
$this->rememberPreviousUri('reconcile.edit.uri');
|
|
|
|
}
|
|
|
|
Session::forget('reconcile.edit.fromUpdate');
|
|
|
|
|
|
|
|
return view(
|
|
|
|
'accounts.reconcile.edit',
|
2017-11-25 08:20:53 -06:00
|
|
|
compact('journal', 'subTitle')
|
2017-11-24 14:51:07 -06:00
|
|
|
)->with('data', $preFilled);
|
|
|
|
}
|
|
|
|
|
2017-11-14 23:29:49 -06:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @param Account $account
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2017-11-22 14:12:27 -06:00
|
|
|
*
|
2017-11-22 10:49:06 -06:00
|
|
|
* @throws FireflyException
|
2017-12-17 07:30:53 -06:00
|
|
|
* @throws \Throwable
|
2017-11-14 23:29:49 -06:00
|
|
|
*/
|
|
|
|
public function overview(Request $request, Account $account, Carbon $start, Carbon $end)
|
|
|
|
{
|
2017-11-22 14:12:27 -06:00
|
|
|
if (AccountType::ASSET !== $account->accountType->type) {
|
2017-11-22 10:49:06 -06:00
|
|
|
throw new FireflyException(sprintf('Account %s is not an asset account.', $account->name));
|
|
|
|
}
|
2017-11-22 09:54:49 -06:00
|
|
|
$startBalance = $request->get('startBalance');
|
|
|
|
$endBalance = $request->get('endBalance');
|
|
|
|
$transactionIds = $request->get('transactions') ?? [];
|
|
|
|
$clearedIds = $request->get('cleared') ?? [];
|
|
|
|
$amount = '0';
|
|
|
|
$clearedAmount = '0';
|
|
|
|
$route = route('accounts.reconcile.submit', [$account->id, $start->format('Ymd'), $end->format('Ymd')]);
|
|
|
|
// get sum of transaction amounts:
|
2018-02-25 12:09:05 -06:00
|
|
|
$transactions = $this->repository->getTransactionsById($transactionIds);
|
|
|
|
$cleared = $this->repository->getTransactionsById($clearedIds);
|
2017-11-22 13:20:44 -06:00
|
|
|
$countCleared = 0;
|
2017-11-22 09:54:49 -06:00
|
|
|
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($transactions as $transaction) {
|
|
|
|
$amount = bcadd($amount, $transaction->amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($cleared as $transaction) {
|
2017-11-22 13:20:44 -06:00
|
|
|
if ($transaction->transactionJournal->date <= $end) {
|
|
|
|
$clearedAmount = bcadd($clearedAmount, $transaction->amount);
|
2017-11-22 14:12:27 -06:00
|
|
|
++$countCleared;
|
2017-11-22 13:20:44 -06:00
|
|
|
}
|
2017-11-22 09:54:49 -06:00
|
|
|
}
|
2017-11-22 10:49:06 -06:00
|
|
|
$difference = bcadd(bcadd(bcsub($startBalance, $endBalance), $clearedAmount), $amount);
|
|
|
|
$diffCompare = bccomp($difference, '0');
|
2017-12-16 12:48:31 -06:00
|
|
|
$return = [
|
2017-11-22 09:54:49 -06:00
|
|
|
'post_uri' => $route,
|
2017-12-16 12:48:31 -06:00
|
|
|
'html' => view(
|
|
|
|
'accounts.reconcile.overview', compact(
|
2017-12-17 07:06:14 -06:00
|
|
|
'account', 'start', 'diffCompare', 'difference', 'end', 'clearedIds', 'transactionIds', 'clearedAmount',
|
|
|
|
'startBalance', 'endBalance', 'amount',
|
|
|
|
'route', 'countCleared'
|
|
|
|
)
|
2017-12-16 12:48:31 -06:00
|
|
|
)->render(),
|
2017-11-14 23:29:49 -06:00
|
|
|
];
|
|
|
|
|
2018-03-10 13:30:09 -06:00
|
|
|
return response()->json($return);
|
2017-11-14 23:29:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
* @param Carbon|null $start
|
|
|
|
* @param Carbon|null $end
|
|
|
|
*
|
2017-11-25 08:20:53 -06:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @throws FireflyException
|
2017-11-14 23:29:49 -06:00
|
|
|
*/
|
|
|
|
public function reconcile(Account $account, Carbon $start = null, Carbon $end = null)
|
|
|
|
{
|
2017-11-15 05:25:49 -06:00
|
|
|
if (AccountType::INITIAL_BALANCE === $account->accountType->type) {
|
2017-11-14 23:29:49 -06:00
|
|
|
return $this->redirectToOriginalAccount($account);
|
|
|
|
}
|
2017-11-22 10:49:06 -06:00
|
|
|
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)]));
|
|
|
|
}
|
2017-11-14 23:29:49 -06:00
|
|
|
/** @var CurrencyRepositoryInterface $currencyRepos */
|
|
|
|
$currencyRepos = app(CurrencyRepositoryInterface::class);
|
|
|
|
$currencyId = intval($account->getMeta('currency_id'));
|
2018-02-23 08:13:01 -06:00
|
|
|
$currency = $currencyRepos->findNull($currencyId);
|
2017-11-15 05:25:49 -06:00
|
|
|
if (0 === $currencyId) {
|
2017-12-17 11:23:10 -06:00
|
|
|
$currency = app('amount')->getDefaultCurrency(); // @codeCoverageIgnore
|
2017-11-14 23:29:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// no start or end:
|
|
|
|
$range = Preferences::get('viewRange', '1M')->data;
|
|
|
|
|
|
|
|
// get start and end
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null === $start && null === $end) {
|
2017-12-17 07:06:14 -06:00
|
|
|
$start = clone session('start', app('navigation')->startOfPeriod(new Carbon, $range));
|
|
|
|
$end = clone session('end', app('navigation')->endOfPeriod(new Carbon, $range));
|
2017-11-14 23:29:49 -06:00
|
|
|
}
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null === $end) {
|
2017-12-17 07:06:14 -06:00
|
|
|
$end = app('navigation')->endOfPeriod($start, $range);
|
2017-11-14 23:29:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
$startDate = clone $start;
|
|
|
|
$startDate->subDays(1);
|
|
|
|
$startBalance = round(app('steam')->balance($account, $startDate), $currency->decimal_places);
|
|
|
|
$endBalance = round(app('steam')->balance($account, $end), $currency->decimal_places);
|
|
|
|
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $account->accountType->type);
|
|
|
|
$subTitle = trans('firefly.reconcile_account', ['account' => $account->name]);
|
|
|
|
|
|
|
|
// various links
|
|
|
|
$transactionsUri = route('accounts.reconcile.transactions', [$account->id, '%start%', '%end%']);
|
|
|
|
$overviewUri = route('accounts.reconcile.overview', [$account->id, '%start%', '%end%']);
|
|
|
|
$indexUri = route('accounts.reconcile', [$account->id, '%start%', '%end%']);
|
|
|
|
|
2017-12-16 12:48:31 -06:00
|
|
|
return view(
|
|
|
|
'accounts.reconcile.index', compact(
|
2017-12-17 07:06:14 -06:00
|
|
|
'account', 'currency', 'subTitleIcon', 'start', 'end', 'subTitle', 'startBalance', 'endBalance', 'transactionsUri',
|
|
|
|
'overviewUri', 'indexUri'
|
|
|
|
)
|
2017-12-16 12:48:31 -06:00
|
|
|
);
|
2017-11-14 23:29:49 -06:00
|
|
|
}
|
|
|
|
|
2017-11-24 14:51:07 -06:00
|
|
|
/**
|
2018-02-25 12:09:05 -06:00
|
|
|
* @param TransactionJournal $journal
|
2017-11-25 08:20:53 -06:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
|
2017-11-24 14:51:07 -06:00
|
|
|
*/
|
2018-02-25 12:09:05 -06:00
|
|
|
public function show(TransactionJournal $journal)
|
2017-11-24 14:51:07 -06:00
|
|
|
{
|
2018-02-23 08:13:01 -06:00
|
|
|
|
2017-12-22 11:32:43 -06:00
|
|
|
if (TransactionType::RECONCILIATION !== $journal->transactionType->type) {
|
2017-11-24 14:51:07 -06:00
|
|
|
return redirect(route('transactions.show', [$journal->id]));
|
|
|
|
}
|
|
|
|
$subTitle = trans('firefly.reconciliation') . ' "' . $journal->description . '"';
|
|
|
|
|
|
|
|
// get main transaction:
|
2018-02-25 12:09:05 -06:00
|
|
|
$transaction = $this->repository->getAssetTransaction($journal);
|
2017-12-17 11:23:10 -06:00
|
|
|
$account = $transaction->account;
|
2017-11-24 14:51:07 -06:00
|
|
|
|
2017-12-17 11:23:10 -06:00
|
|
|
return view('accounts.reconcile.show', compact('journal', 'subTitle', 'transaction', 'account'));
|
2017-11-24 14:51:07 -06:00
|
|
|
}
|
|
|
|
|
2017-11-22 10:49:06 -06:00
|
|
|
/**
|
2018-02-23 08:13:01 -06:00
|
|
|
* @param ReconciliationStoreRequest $request
|
2018-03-11 10:24:07 -05:00
|
|
|
* @param JournalRepositoryInterface $repository
|
2018-02-23 08:13:01 -06:00
|
|
|
* @param Account $account
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2017-11-25 08:20:53 -06:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2017-11-22 10:49:06 -06:00
|
|
|
*/
|
2018-02-28 13:23:45 -06:00
|
|
|
public function submit(ReconciliationStoreRequest $request, JournalRepositoryInterface $repository, Account $account, Carbon $start, Carbon $end)
|
2017-11-22 10:49:06 -06:00
|
|
|
{
|
2018-02-23 08:13:01 -06:00
|
|
|
Log::debug('In ReconcileController::submit()');
|
|
|
|
$data = $request->getAll();
|
|
|
|
|
2017-11-22 10:49:06 -06:00
|
|
|
/** @var Transaction $transaction */
|
2018-02-23 08:13:01 -06:00
|
|
|
foreach ($data['transactions'] as $transactionId) {
|
2018-02-28 13:23:45 -06:00
|
|
|
$repository->reconcileById(intval($transactionId));
|
2017-11-22 10:49:06 -06:00
|
|
|
}
|
2018-02-23 08:13:01 -06:00
|
|
|
Log::debug('Reconciled all transactions.');
|
2017-11-22 10:49:06 -06:00
|
|
|
|
|
|
|
// create reconciliation transaction (if necessary):
|
2018-02-23 08:13:01 -06:00
|
|
|
if ('create' === $data['reconcile']) {
|
|
|
|
// get "opposing" account.
|
2017-11-22 10:49:06 -06:00
|
|
|
/** @var AccountRepositoryInterface $accountRepos */
|
|
|
|
$accountRepos = app(AccountRepositoryInterface::class);
|
|
|
|
$reconciliation = $accountRepos->getReconciliation($account);
|
2018-02-23 08:13:01 -06:00
|
|
|
|
|
|
|
|
|
|
|
$difference = $data['difference'];
|
|
|
|
$source = $reconciliation;
|
|
|
|
$destination = $account;
|
|
|
|
if (bccomp($difference, '0') === 1) {
|
|
|
|
// amount is positive. Add it to reconciliation?
|
|
|
|
$source = $account;
|
|
|
|
$destination = $reconciliation;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// data for journal
|
|
|
|
$description = trans(
|
|
|
|
'firefly.reconcilliation_transaction_title',
|
|
|
|
['from' => $start->formatLocalized($this->monthAndDayFormat), 'to' => $end->formatLocalized($this->monthAndDayFormat)]
|
|
|
|
);
|
|
|
|
$journalData = [
|
|
|
|
'type' => 'Reconciliation',
|
|
|
|
'description' => $description,
|
|
|
|
'user' => auth()->user()->id,
|
|
|
|
'date' => $data['end'],
|
|
|
|
'bill_id' => null,
|
|
|
|
'bill_name' => null,
|
|
|
|
'piggy_bank_id' => null,
|
|
|
|
'piggy_bank_name' => null,
|
|
|
|
'tags' => null,
|
|
|
|
'interest_date' => null,
|
|
|
|
'transactions' => [[
|
|
|
|
'currency_id' => intval($account->getMeta('currency_id')),
|
|
|
|
'currency_code' => null,
|
|
|
|
'description' => null,
|
|
|
|
'amount' => app('steam')->positive($difference),
|
|
|
|
'source_id' => $source->id,
|
|
|
|
'source_name' => null,
|
|
|
|
'destination_id' => $destination->id,
|
|
|
|
'destination_name' => null,
|
|
|
|
'reconciled' => true,
|
|
|
|
'identifier' => 0,
|
|
|
|
'foreign_currency_id' => null,
|
|
|
|
'foreign_currency_code' => null,
|
|
|
|
'foreign_amount' => null,
|
|
|
|
'budget_id' => null,
|
|
|
|
'budget_name' => null,
|
|
|
|
'category_id' => null,
|
|
|
|
'category_name' => null,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'notes' => join(', ', $data['transactions']),
|
2017-11-22 10:49:06 -06:00
|
|
|
];
|
2018-02-23 08:13:01 -06:00
|
|
|
|
2018-02-28 13:23:45 -06:00
|
|
|
$journal = $repository->store($journalData);
|
2017-11-22 10:49:06 -06:00
|
|
|
}
|
2018-02-23 08:13:01 -06:00
|
|
|
Log::debug('End of routine.');
|
2017-11-22 13:20:44 -06:00
|
|
|
|
2017-11-22 10:49:06 -06:00
|
|
|
Session::flash('success', trans('firefly.reconciliation_stored'));
|
|
|
|
|
|
|
|
return redirect(route('accounts.show', [$account->id]));
|
|
|
|
}
|
|
|
|
|
2017-11-14 23:29:49 -06:00
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return mixed
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @throws \Throwable
|
|
|
|
* @throws FireflyException
|
2017-11-14 23:29:49 -06:00
|
|
|
*/
|
|
|
|
public function transactions(Account $account, Carbon $start, Carbon $end)
|
|
|
|
{
|
2017-11-15 05:25:49 -06:00
|
|
|
if (AccountType::INITIAL_BALANCE === $account->accountType->type) {
|
2017-11-14 23:29:49 -06:00
|
|
|
return $this->redirectToOriginalAccount($account);
|
|
|
|
}
|
|
|
|
|
2017-11-24 11:05:58 -06:00
|
|
|
$startDate = clone $start;
|
|
|
|
$startDate->subDays(1);
|
|
|
|
|
|
|
|
/** @var CurrencyRepositoryInterface $currencyRepos */
|
|
|
|
$currencyRepos = app(CurrencyRepositoryInterface::class);
|
|
|
|
$currencyId = intval($account->getMeta('currency_id'));
|
2018-02-28 13:23:45 -06:00
|
|
|
$currency = $currencyRepos->findNull($currencyId);
|
2017-11-24 11:05:58 -06:00
|
|
|
if (0 === $currencyId) {
|
2017-12-17 11:23:10 -06:00
|
|
|
$currency = app('amount')->getDefaultCurrency(); // @codeCoverageIgnore
|
2017-11-24 11:05:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
$startBalance = round(app('steam')->balance($account, $startDate), $currency->decimal_places);
|
|
|
|
$endBalance = round(app('steam')->balance($account, $end), $currency->decimal_places);
|
|
|
|
|
2017-11-14 23:29:49 -06:00
|
|
|
// get the transactions
|
|
|
|
$selectionStart = clone $start;
|
|
|
|
$selectionStart->subDays(3);
|
|
|
|
$selectionEnd = clone $end;
|
|
|
|
$selectionEnd->addDays(3);
|
|
|
|
|
|
|
|
// grab transactions:
|
|
|
|
/** @var JournalCollectorInterface $collector */
|
|
|
|
$collector = app(JournalCollectorInterface::class);
|
|
|
|
$collector->setAccounts(new Collection([$account]))
|
|
|
|
->setRange($selectionStart, $selectionEnd)->withBudgetInformation()->withOpposingAccount()->withCategoryInformation();
|
|
|
|
$transactions = $collector->getJournals();
|
|
|
|
$html = view('accounts.reconcile.transactions', compact('account', 'transactions', 'start', 'end', 'selectionStart', 'selectionEnd'))->render();
|
|
|
|
|
2018-03-10 13:30:09 -06:00
|
|
|
return response()->json(['html' => $html, 'startBalance' => $startBalance, 'endBalance' => $endBalance]);
|
2017-11-14 23:29:49 -06:00
|
|
|
}
|
2017-11-24 14:51:07 -06:00
|
|
|
|
|
|
|
/**
|
2018-02-23 08:13:01 -06:00
|
|
|
* @param ReconciliationUpdateRequest $request
|
|
|
|
* @param TransactionJournal $journal
|
2017-11-24 14:51:07 -06:00
|
|
|
*
|
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
*/
|
2018-02-25 12:09:05 -06:00
|
|
|
public function update(ReconciliationUpdateRequest $request, TransactionJournal $journal)
|
2017-11-24 14:51:07 -06:00
|
|
|
{
|
2017-12-22 11:32:43 -06:00
|
|
|
if (TransactionType::RECONCILIATION !== $journal->transactionType->type) {
|
2017-11-24 14:51:07 -06:00
|
|
|
return redirect(route('transactions.show', [$journal->id]));
|
|
|
|
}
|
2017-12-22 11:32:43 -06:00
|
|
|
if (0 === bccomp('0', $request->get('amount'))) {
|
2017-11-24 14:51:07 -06:00
|
|
|
Session::flash('error', trans('firefly.amount_cannot_be_zero'));
|
|
|
|
|
|
|
|
return redirect(route('accounts.reconcile.edit', [$journal->id]))->withInput();
|
|
|
|
}
|
|
|
|
// update journal using account repository. Keep it consistent.
|
2018-02-23 08:13:01 -06:00
|
|
|
$submitted = $request->getJournalData();
|
|
|
|
|
|
|
|
// amount pos neg influences the accounts:
|
2018-02-28 13:23:45 -06:00
|
|
|
$source = $this->repository->getJournalSourceAccounts($journal)->first();
|
|
|
|
$destination = $this->repository->getJournalDestinationAccounts($journal)->first();
|
2018-02-23 08:13:01 -06:00
|
|
|
if (bccomp($submitted['amount'], '0') === 1) {
|
|
|
|
// amount is positive, switch accounts:
|
|
|
|
list($source, $destination) = [$destination, $source];
|
|
|
|
|
|
|
|
}
|
|
|
|
// expand data with journal data:
|
|
|
|
$data = [
|
|
|
|
'type' => $journal->transactionType->type,
|
|
|
|
'description' => $journal->description,
|
|
|
|
'user' => $journal->user_id,
|
|
|
|
'date' => $journal->date,
|
|
|
|
'bill_id' => null,
|
|
|
|
'bill_name' => null,
|
|
|
|
'piggy_bank_id' => null,
|
|
|
|
'piggy_bank_name' => null,
|
|
|
|
'tags' => $submitted['tags'],
|
|
|
|
'interest_date' => null,
|
|
|
|
'book_date' => null,
|
|
|
|
'transactions' => [[
|
2018-02-23 09:21:28 -06:00
|
|
|
'currency_id' => intval($journal->transaction_currency_id),
|
2018-02-23 08:13:01 -06:00
|
|
|
'currency_code' => null,
|
|
|
|
'description' => null,
|
|
|
|
'amount' => app('steam')->positive($submitted['amount']),
|
|
|
|
'source_id' => $source->id,
|
|
|
|
'source_name' => null,
|
|
|
|
'destination_id' => $destination->id,
|
|
|
|
'destination_name' => null,
|
|
|
|
'reconciled' => true,
|
|
|
|
'identifier' => 0,
|
|
|
|
'foreign_currency_id' => null,
|
|
|
|
'foreign_currency_code' => null,
|
|
|
|
'foreign_amount' => null,
|
|
|
|
'budget_id' => null,
|
|
|
|
'budget_name' => null,
|
|
|
|
'category_id' => null,
|
|
|
|
'category_name' => $submitted['category'],
|
|
|
|
],
|
|
|
|
],
|
2018-02-25 12:09:05 -06:00
|
|
|
'notes' => $this->repository->getNoteText($journal),
|
2018-02-23 08:13:01 -06:00
|
|
|
];
|
|
|
|
|
2018-02-25 12:09:05 -06:00
|
|
|
$this->repository->update($journal, $data);
|
2017-11-24 14:51:07 -06:00
|
|
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
if (1 === intval($request->get('return_to_edit'))) {
|
|
|
|
Session::put('reconcile.edit.fromUpdate', true);
|
|
|
|
|
|
|
|
return redirect(route('accounts.reconcile.edit', [$journal->id]))->withInput(['return_to_edit' => 1]);
|
|
|
|
}
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
|
|
|
// redirect to previous URL.
|
|
|
|
return redirect($this->getPreviousUri('reconcile.edit.uri'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
*
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
private function redirectToOriginalAccount(Account $account)
|
|
|
|
{
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
$transaction = $account->transactions()->first();
|
|
|
|
if (null === $transaction) {
|
2017-12-17 11:23:10 -06:00
|
|
|
throw new FireflyException(sprintf('Expected a transaction. Account #%d has none. BEEP, error.', $account->id)); // @codeCoverageIgnore
|
2017-11-24 14:51:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
$journal = $transaction->transactionJournal;
|
|
|
|
/** @var Transaction $opposingTransaction */
|
|
|
|
$opposingTransaction = $journal->transactions()->where('transactions.id', '!=', $transaction->id)->first();
|
|
|
|
|
|
|
|
if (null === $opposingTransaction) {
|
|
|
|
throw new FireflyException('Expected an opposing transaction. This account has none. BEEP, error.'); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect(route('accounts.show', [$opposingTransaction->account_id]));
|
|
|
|
}
|
2017-11-15 03:52:29 -06:00
|
|
|
}
|