Files
firefly-iii/app/Http/Controllers/Account/EditController.php

203 lines
8.9 KiB
PHP
Raw Normal View History

<?php
/**
* EditController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program 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 Affero General Public License for more details.
*
* 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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Account;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\AccountFormRequest;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2019-06-22 10:25:34 +02:00
use FireflyIII\Support\Http\Controllers\ModelInformation;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\View\View;
/**
* Class EditController
*/
class EditController extends Controller
{
2021-04-06 17:00:16 +02:00
use ModelInformation;
2021-03-28 11:46:23 +02:00
2023-01-03 06:48:53 +01:00
private AttachmentHelperInterface $attachments;
private AccountRepositoryInterface $repository;
/**
* EditController constructor.
*/
public function __construct()
{
parent::__construct();
// translations:
$this->middleware(
function ($request, $next) {
app('view')->share('mainTitleIcon', 'fa-credit-card');
2023-12-22 17:28:42 +01:00
app('view')->share('title', (string) trans('firefly.accounts'));
2023-01-03 06:48:53 +01:00
$this->repository = app(AccountRepositoryInterface::class);
$this->attachments = app(AttachmentHelperInterface::class);
return $next($request);
}
);
}
/**
2023-12-22 20:12:38 +01:00
* Edit account overview. It's complex, but it just has a lot of if/then/else.
*
* @SuppressWarnings(PHPMD.NPathComplexity)
*
2023-12-20 19:35:52 +01:00
* @return Factory|Redirector|RedirectResponse|View
*/
public function edit(Request $request, Account $account, AccountRepositoryInterface $repository)
{
2019-08-17 08:29:35 +02:00
if (!$this->isEditableAccount($account)) {
return $this->redirectAccountToAccount($account);
2019-08-17 08:29:35 +02:00
}
2019-06-22 10:25:34 +02:00
$objectType = config('firefly.shortNamesByFullName')[$account->accountType->type];
2023-12-22 17:28:42 +01:00
$subTitle = (string) trans(sprintf('firefly.edit_%s_account', $objectType), ['name' => $account->name]);
2019-06-22 10:25:34 +02:00
$subTitleIcon = config(sprintf('firefly.subIconsByIdentifier.%s', $objectType));
$roles = $this->getRoles();
$liabilityTypes = $this->getLiabilityTypes();
$location = $repository->getLocation($account);
2023-11-05 09:40:45 +01:00
$latitude = null !== $location ? $location->latitude : config('firefly.default_location.latitude');
$longitude = null !== $location ? $location->longitude : config('firefly.default_location.longitude');
$zoomLevel = null !== $location ? $location->zoom_level : config('firefly.default_location.zoom_level');
$hasLocation = null !== $location;
$locations = [
'location' => [
2023-12-10 06:51:59 +01:00
'latitude' => null !== old('location_latitude') ? old('location_latitude') : $latitude,
2023-11-05 09:40:45 +01:00
'longitude' => null !== old('location_longitude') ? old('location_longitude') : $longitude,
'zoom_level' => null !== old('location_zoom_level') ? old('location_zoom_level') : $zoomLevel,
'has_location' => $hasLocation || 'true' === old('location_has_location'),
],
];
2018-08-05 18:59:15 +02:00
$liabilityDirections = [
'debit' => trans('firefly.liability_direction_debit'),
'credit' => trans('firefly.liability_direction_credit'),
];
2018-08-05 18:59:15 +02:00
// interest calculation periods:
$interestPeriods = [
2023-12-22 17:28:42 +01:00
'daily' => (string) trans('firefly.interest_calc_daily'),
'monthly' => (string) trans('firefly.interest_calc_monthly'),
'yearly' => (string) trans('firefly.interest_calc_yearly'),
2018-08-05 18:59:15 +02:00
];
// put previous url in session if not redirect from store (not "return_to_edit").
if (true !== session('accounts.edit.fromUpdate')) {
2022-04-12 18:19:30 +02:00
$this->rememberPreviousUrl('accounts.edit.url');
}
$request->session()->forget('accounts.edit.fromUpdate');
2023-12-22 17:28:42 +01:00
$openingBalanceAmount = (string) $repository->getOpeningBalanceAmount($account);
if ('0' === $openingBalanceAmount) {
2021-04-27 07:55:54 +02:00
$openingBalanceAmount = '';
}
$openingBalanceDate = $repository->getOpeningBalanceDate($account);
$currency = $this->repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency();
// include this account in net-worth charts?
$includeNetWorth = $repository->getMetaValue($account, 'include_net_worth');
$includeNetWorth = null === $includeNetWorth ? true : '1' === $includeNetWorth;
// code to handle active-checkboxes
2022-12-29 19:41:57 +01:00
$hasOldInput = null !== $request->old('_token');
2022-12-24 05:48:04 +01:00
$virtualBalance = null === $account->virtual_balance ? '0' : $account->virtual_balance;
2022-12-29 19:41:57 +01:00
$preFilled = [
2019-06-22 05:51:32 +02:00
'account_number' => $repository->getMetaValue($account, 'account_number'),
'account_role' => $repository->getMetaValue($account, 'account_role'),
'cc_type' => $repository->getMetaValue($account, 'cc_type'),
'cc_monthly_payment_date' => $repository->getMetaValue($account, 'cc_monthly_payment_date'),
'BIC' => $repository->getMetaValue($account, 'BIC'),
2023-12-22 17:28:42 +01:00
'opening_balance_date' => substr((string) $openingBalanceDate, 0, 10),
2019-06-22 05:51:32 +02:00
'liability_type_id' => $account->account_type_id,
2022-12-24 05:06:39 +01:00
'opening_balance' => app('steam')->bcround($openingBalanceAmount, $currency->decimal_places),
'liability_direction' => $this->repository->getMetaValue($account, 'liability_direction'),
2022-12-24 05:48:04 +01:00
'virtual_balance' => app('steam')->bcround($virtualBalance, $currency->decimal_places),
2019-06-22 05:51:32 +02:00
'currency_id' => $currency->id,
2023-12-22 17:28:42 +01:00
'include_net_worth' => $hasOldInput ? (bool) $request->old('include_net_worth') : $includeNetWorth,
2019-06-22 05:51:32 +02:00
'interest' => $repository->getMetaValue($account, 'interest'),
'interest_period' => $repository->getMetaValue($account, 'interest_period'),
'notes' => $this->repository->getNoteText($account),
2023-12-22 17:28:42 +01:00
'active' => $hasOldInput ? (bool) $request->old('active') : $account->active,
];
2022-03-29 14:58:06 +02:00
if ('' === $openingBalanceAmount) {
$preFilled['opening_balance'] = '';
}
$request->session()->flash('preFilled', $preFilled);
2023-12-22 17:28:42 +01:00
return view('accounts.edit', compact('account', 'currency', 'subTitle', 'subTitleIcon', 'locations', 'liabilityDirections', 'objectType', 'roles', 'preFilled', 'liabilityTypes', 'interestPeriods'));
}
2021-03-28 11:46:23 +02:00
/**
* Update the account.
*
2023-12-20 19:35:52 +01:00
* @return $this|Redirector|RedirectResponse
*/
public function update(AccountFormRequest $request, Account $account)
{
2019-08-17 08:29:35 +02:00
if (!$this->isEditableAccount($account)) {
return $this->redirectAccountToAccount($account);
2019-08-17 08:29:35 +02:00
}
$data = $request->getAccountData();
$this->repository->update($account, $data);
2023-12-22 17:28:42 +01:00
$request->session()->flash('success', (string) trans('firefly.updated_account', ['name' => $account->name]));
// store new attachment(s):
2023-12-20 19:35:52 +01:00
/** @var null|array $files */
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
2020-05-07 06:44:01 +02:00
if (null !== $files && !auth()->user()->hasRole('demo')) {
$this->attachments->saveAttachmentsForModel($account, $files);
}
if (null !== $files && auth()->user()->hasRole('demo')) {
2023-12-22 17:28:42 +01:00
session()->flash('info', (string) trans('firefly.no_att_demo_user'));
2020-05-07 06:44:01 +02:00
}
if (count($this->attachments->getMessages()->get('attachments')) > 0) {
$request->session()->flash('info', $this->attachments->getMessages()->get('attachments'));
}
// redirect
2022-04-12 18:19:30 +02:00
$redirect = redirect($this->getPreviousUrl('accounts.edit.url'));
2023-12-22 17:28:42 +01:00
if (1 === (int) $request->get('return_to_edit')) {
// set value so edit routine will not overwrite URL:
$request->session()->put('accounts.edit.fromUpdate', true);
$redirect = redirect(route('accounts.edit', [$account->id]))->withInput(['return_to_edit' => 1]);
}
2020-10-24 07:18:37 +02:00
app('preferences')->mark();
return $redirect;
}
}