| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * EditController.php | 
					
						
							| 
									
										
										
										
											2020-01-31 07:32:04 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +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-14 11:16:12 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +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-14 11:16:12 +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-14 11:16:12 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Http\Controllers\Account; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 08:58:55 +01:00
										 |  |  | use FireflyIII\Helpers\Attachments\AttachmentHelperInterface; | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  | use FireflyIII\Http\Controllers\Controller; | 
					
						
							|  |  |  | use FireflyIII\Http\Requests\AccountFormRequest; | 
					
						
							|  |  |  | use FireflyIII\Models\Account; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2019-06-22 10:25:34 +02:00
										 |  |  | use FireflyIII\Support\Http\Controllers\ModelInformation; | 
					
						
							| 
									
										
										
										
											2019-08-17 08:29:35 +02:00
										 |  |  | use FireflyIII\Support\Http\Controllers\UserNavigation; | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  | use Illuminate\Contracts\View\Factory; | 
					
						
							|  |  |  | use Illuminate\Http\RedirectResponse; | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  | use Illuminate\Routing\Redirector; | 
					
						
							|  |  |  | use Illuminate\View\View; | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Class EditController | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class EditController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-08-17 08:29:35 +02:00
										 |  |  |     use ModelInformation, UserNavigation; | 
					
						
							| 
									
										
										
										
											2018-07-20 20:53:48 +02:00
										 |  |  |     /** @var CurrencyRepositoryInterface The currency repository */ | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |     private $currencyRepos; | 
					
						
							| 
									
										
										
										
											2018-07-20 20:53:48 +02:00
										 |  |  |     /** @var AccountRepositoryInterface The account repository */ | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |     private $repository; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 08:58:55 +01:00
										 |  |  |     /** @var AttachmentHelperInterface Helper for attachments. */ | 
					
						
							|  |  |  |     private $attachments; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-20 20:53:48 +02:00
										 |  |  |      * EditController constructor. | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::__construct(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // translations:
 | 
					
						
							|  |  |  |         $this->middleware( | 
					
						
							|  |  |  |             function ($request, $next) { | 
					
						
							|  |  |  |                 app('view')->share('mainTitleIcon', 'fa-credit-card'); | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |                 app('view')->share('title', (string) trans('firefly.accounts')); | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 $this->repository    = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  |                 $this->currencyRepos = app(CurrencyRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2020-03-19 08:58:55 +01:00
										 |  |  |                 $this->attachments = app(AttachmentHelperInterface::class); | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 return $next($request); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-20 20:53:48 +02:00
										 |  |  |      * Edit account overview. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |      * @param Request                    $request | 
					
						
							|  |  |  |      * @param Account                    $account | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |      * @param AccountRepositoryInterface $repository | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |      * @return Factory|View | 
					
						
							| 
									
										
										
										
											2018-07-14 11:45:05 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     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); // @codeCoverageIgnore
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-22 10:25:34 +02:00
										 |  |  |         $objectType     = config('firefly.shortNamesByFullName')[$account->accountType->type]; | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +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(); | 
					
						
							| 
									
										
										
										
											2019-12-30 12:12:08 +01:00
										 |  |  |         $location       = $repository->getLocation($account); | 
					
						
							|  |  |  |         $latitude       = $location ? $location->latitude : config('firefly.default_location.latitude'); | 
					
						
							|  |  |  |         $longitude      = $location ? $location->longitude : config('firefly.default_location.longitude'); | 
					
						
							|  |  |  |         $zoomLevel      = $location ? $location->zoom_level : config('firefly.default_location.zoom_level'); | 
					
						
							|  |  |  |         $hasLocation    = null !== $location; | 
					
						
							|  |  |  |         $locations      = [ | 
					
						
							|  |  |  |             'location' => [ | 
					
						
							|  |  |  |                 'latitude'     => old('location_latitude') ?? $latitude, | 
					
						
							|  |  |  |                 'longitude'    => old('location_longitude') ?? $longitude, | 
					
						
							|  |  |  |                 'zoom_level'   => old('location_zoom_level') ?? $zoomLevel, | 
					
						
							|  |  |  |                 'has_location' => $hasLocation || 'true' === old('location_has_location'), | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2018-08-05 18:59:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // interest calculation periods:
 | 
					
						
							|  |  |  |         $interestPeriods = [ | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +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
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |         // put previous url in session if not redirect from store (not "return_to_edit").
 | 
					
						
							|  |  |  |         if (true !== session('accounts.edit.fromUpdate')) { | 
					
						
							|  |  |  |             $this->rememberPreviousUri('accounts.edit.uri'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $request->session()->forget('accounts.edit.fromUpdate'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |         $openingBalanceAmount = (string) $repository->getOpeningBalanceAmount($account); | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |         $openingBalanceDate   = $repository->getOpeningBalanceDate($account); | 
					
						
							| 
									
										
										
										
											2019-06-23 05:53:01 +02:00
										 |  |  |         $currency             = $this->repository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency(); | 
					
						
							| 
									
										
										
										
											2018-08-26 18:40:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // include this account in net-worth charts?
 | 
					
						
							|  |  |  |         $includeNetWorth = $repository->getMetaValue($account, 'include_net_worth'); | 
					
						
							|  |  |  |         $includeNetWorth = null === $includeNetWorth ? true : '1' === $includeNetWorth; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |         // code to handle active-checkboxes
 | 
					
						
							|  |  |  |         $hasOldInput = null !== $request->old('_token'); | 
					
						
							|  |  |  |         $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'), | 
					
						
							|  |  |  |             'opening_balance_date'    => $openingBalanceDate, | 
					
						
							|  |  |  |             'liability_type_id'       => $account->account_type_id, | 
					
						
							|  |  |  |             'opening_balance'         => $openingBalanceAmount, | 
					
						
							|  |  |  |             'virtual_balance'         => $account->virtual_balance, | 
					
						
							|  |  |  |             'currency_id'             => $currency->id, | 
					
						
							|  |  |  |             'include_net_worth'       => $includeNetWorth, | 
					
						
							|  |  |  |             'interest'                => $repository->getMetaValue($account, 'interest'), | 
					
						
							|  |  |  |             'interest_period'         => $repository->getMetaValue($account, 'interest_period'), | 
					
						
							|  |  |  |             'notes'                   => $this->repository->getNoteText($account), | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |             'active'                  => $hasOldInput ? (bool) $request->old('active') : $account->active, | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $request->session()->flash('preFilled', $preFilled); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-05 18:59:15 +02:00
										 |  |  |         return view( | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |             'accounts.edit', | 
					
						
							|  |  |  |             compact( | 
					
						
							|  |  |  |                 'account', | 
					
						
							|  |  |  |                 'currency', | 
					
						
							|  |  |  |                 'subTitle', | 
					
						
							|  |  |  |                 'subTitleIcon', | 
					
						
							|  |  |  |                 'locations', | 
					
						
							|  |  |  |                 'objectType', | 
					
						
							|  |  |  |                 'roles', | 
					
						
							|  |  |  |                 'preFilled', | 
					
						
							|  |  |  |                 'liabilityTypes', | 
					
						
							|  |  |  |                 'interestPeriods' | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2018-08-05 18:59:15 +02:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-20 20:53:48 +02:00
										 |  |  |      * Update the account. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |      * @param AccountFormRequest $request | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |      * @param Account            $account | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |      * @return $this|RedirectResponse|Redirector | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function update(AccountFormRequest $request, Account $account) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-17 08:29:35 +02:00
										 |  |  |         if (!$this->isEditableAccount($account)) { | 
					
						
							|  |  |  |             return $this->redirectAccountToAccount($account); // @codeCoverageIgnore
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |         $data = $request->getAccountData(); | 
					
						
							|  |  |  |         $this->repository->update($account, $data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |         $request->session()->flash('success', (string) trans('firefly.updated_account', ['name' => $account->name])); | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |         app('preferences')->mark(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 09:01:35 +01:00
										 |  |  |         // store new attachment(s):
 | 
					
						
							| 
									
										
										
										
											2020-03-19 08:58:55 +01:00
										 |  |  |         /** @var array $files */ | 
					
						
							|  |  |  |         $files = $request->hasFile('attachments') ? $request->file('attachments') : null; | 
					
						
							|  |  |  |         $this->attachments->saveAttachmentsForModel($account, $files); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (count($this->attachments->getMessages()->get('attachments')) > 0) { | 
					
						
							|  |  |  |             $request->session()->flash('info', $this->attachments->getMessages()->get('attachments')); // @codeCoverageIgnore
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // redirect
 | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |         $redirect = redirect($this->getPreviousUri('accounts.edit.uri')); | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |         if (1 === (int) $request->get('return_to_edit')) { | 
					
						
							| 
									
										
										
										
											2018-07-14 11:16:12 +02:00
										 |  |  |             // 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]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $redirect; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-22 20:32:02 +02:00
										 |  |  | } |