| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2020-06-30 19:05:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * EditController.php | 
					
						
							|  |  |  |  * Copyright (c) 2020 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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Http\Controllers\PiggyBank; | 
					
						
							| 
									
										
										
										
											2021-03-28 11:46:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  | use FireflyIII\Helpers\Attachments\AttachmentHelperInterface; | 
					
						
							|  |  |  | use FireflyIII\Http\Controllers\Controller; | 
					
						
							| 
									
										
										
										
											2020-06-30 19:05:35 +02:00
										 |  |  | use FireflyIII\Http\Requests\PiggyBankUpdateRequest; | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  | use FireflyIII\Models\PiggyBank; | 
					
						
							|  |  |  | use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; | 
					
						
							|  |  |  | use Illuminate\Contracts\View\Factory; | 
					
						
							|  |  |  | use Illuminate\Http\RedirectResponse; | 
					
						
							|  |  |  | use Illuminate\Routing\Redirector; | 
					
						
							| 
									
										
										
										
											2023-12-30 10:14:35 +01:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  | use Illuminate\View\View; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class EditController | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class EditController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     private AttachmentHelperInterface    $attachments; | 
					
						
							|  |  |  |     private PiggyBankRepositoryInterface $piggyRepos; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * PiggyBankController constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::__construct(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->middleware( | 
					
						
							|  |  |  |             function ($request, $next) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 app('view')->share('title', (string) trans('firefly.piggyBanks')); | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  |                 app('view')->share('mainTitleIcon', 'fa-bullseye'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-05 07:31:26 +01:00
										 |  |  |                 $this->attachments = app(AttachmentHelperInterface::class); | 
					
						
							|  |  |  |                 $this->piggyRepos  = app(PiggyBankRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  |                 return $next($request); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-04 19:18:26 +02:00
										 |  |  |     public function resetHistory(PiggyBank $piggyBank): RedirectResponse | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-06-04 15:40:29 +02:00
										 |  |  |         $this->piggyRepos->resetHistory($piggyBank); | 
					
						
							|  |  |  |         session()->flash('success', (string) trans('firefly.piggy_history_reset')); | 
					
						
							| 
									
										
										
										
											2025-06-04 19:18:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-04 15:40:29 +02:00
										 |  |  |         return redirect(route('piggy-banks.show', [$piggyBank->id])); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Edit a piggy bank. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Factory|View | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function edit(PiggyBank $piggyBank) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $subTitle     = (string) trans('firefly.update_piggy_title', ['name' => $piggyBank->name]); | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  |         $subTitleIcon = 'fa-pencil'; | 
					
						
							|  |  |  |         $note         = $piggyBank->notes()->first(); | 
					
						
							|  |  |  |         // Flash some data to fill the form.
 | 
					
						
							| 
									
										
										
										
											2024-11-30 16:02:30 +01:00
										 |  |  |         $targetDate   = $piggyBank->target_date?->format('Y-m-d'); | 
					
						
							|  |  |  |         $startDate    = $piggyBank->start_date?->format('Y-m-d'); | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $preFilled    = [ | 
					
						
							| 
									
										
										
										
											2025-04-20 21:12:34 +02:00
										 |  |  |             'name'                    => $piggyBank->name, | 
					
						
							| 
									
										
										
										
											2025-04-20 12:41:46 +02:00
										 |  |  |             'transaction_currency_id' => (int) $piggyBank->transaction_currency_id, | 
					
						
							| 
									
										
										
										
											2025-04-20 21:12:34 +02:00
										 |  |  |             'target_amount'           => app('steam')->bcround($piggyBank->target_amount, $piggyBank->transactionCurrency->decimal_places), | 
					
						
							|  |  |  |             'target_date'             => $targetDate, | 
					
						
							|  |  |  |             'start_date'              => $startDate, | 
					
						
							|  |  |  |             'accounts'                => [], | 
					
						
							|  |  |  |             'object_group'            => null !== $piggyBank->objectGroups->first() ? $piggyBank->objectGroups->first()->title : '', | 
					
						
							|  |  |  |             'notes'                   => null === $note ? '' : $note->text, | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2024-12-14 21:55:42 +01:00
										 |  |  |         foreach ($piggyBank->accounts as $account) { | 
					
						
							| 
									
										
										
										
											2024-12-14 17:32:03 +01:00
										 |  |  |             $preFilled['accounts'][] = $account->id; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-11-30 16:02:30 +01:00
										 |  |  |         if (0 === bccomp($piggyBank->target_amount, '0')) { | 
					
						
							| 
									
										
										
										
											2024-12-14 17:32:03 +01:00
										 |  |  |             $preFilled['target_amount'] = ''; | 
					
						
							| 
									
										
										
										
											2022-03-27 18:30:46 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  |         session()->flash('preFilled', $preFilled); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // put previous url in session if not redirect from store (not "return_to_edit").
 | 
					
						
							|  |  |  |         if (true !== session('piggy-banks.edit.fromUpdate')) { | 
					
						
							| 
									
										
										
										
											2022-04-12 18:19:30 +02:00
										 |  |  |             $this->rememberPreviousUrl('piggy-banks.edit.url'); | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         session()->forget('piggy-banks.edit.fromUpdate'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |         return view('piggy-banks.edit', compact('subTitle', 'subTitleIcon', 'piggyBank', 'preFilled')); | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-28 11:46:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Update a piggy bank. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |      * @return Redirector|RedirectResponse | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-06-30 19:05:35 +02:00
										 |  |  |     public function update(PiggyBankUpdateRequest $request, PiggyBank $piggyBank) | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         $data      = $request->getPiggyBankData(); | 
					
						
							|  |  |  |         $piggyBank = $this->piggyRepos->update($piggyBank, $data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         session()->flash('success', (string) trans('firefly.updated_piggy_bank', ['name' => $piggyBank->name])); | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  |         app('preferences')->mark(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // store new attachment(s):
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         /** @var null|array $files */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $files     = $request->hasFile('attachments') ? $request->file('attachments') : null; | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  |         if (null !== $files && !auth()->user()->hasRole('demo')) { | 
					
						
							|  |  |  |             $this->attachments->saveAttachmentsForModel($piggyBank, $files); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (null !== $files && auth()->user()->hasRole('demo')) { | 
					
						
							| 
									
										
										
										
											2024-01-09 20:48:17 +01:00
										 |  |  |             Log::channel('audit')->warning(sprintf('The demo user is trying to upload attachments in %s.', __METHOD__)); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             session()->flash('info', (string) trans('firefly.no_att_demo_user')); | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (count($this->attachments->getMessages()->get('attachments')) > 0) { | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |             $request->session()->flash('info', $this->attachments->getMessages()->get('attachments')); | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $redirect  = redirect($this->getPreviousUrl('piggy-banks.edit.url')); | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         if (1 === (int) $request->get('return_to_edit')) { | 
					
						
							| 
									
										
										
										
											2020-06-07 11:30:02 +02:00
										 |  |  |             session()->put('piggy-banks.edit.fromUpdate', true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $redirect = redirect(route('piggy-banks.edit', [$piggyBank->id])); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $redirect; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |