2020-06-30 19:06:05 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\Bill;
|
2021-03-28 11:46:23 +02:00
|
|
|
|
2020-06-30 19:06:05 +02:00
|
|
|
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
|
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
|
|
|
use FireflyIII\Http\Requests\BillUpdateRequest;
|
|
|
|
use FireflyIII\Models\Bill;
|
|
|
|
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
2021-03-21 09:15:40 +01:00
|
|
|
use Illuminate\Contracts\View\Factory;
|
2021-09-18 10:26:12 +02:00
|
|
|
use Illuminate\Contracts\View\View;
|
2020-06-30 19:06:05 +02:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use Illuminate\Http\Request;
|
2023-12-30 10:14:35 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2020-06-30 19:06:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class EditController
|
|
|
|
*/
|
|
|
|
class EditController extends Controller
|
|
|
|
{
|
|
|
|
private AttachmentHelperInterface $attachments;
|
|
|
|
private BillRepositoryInterface $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BillController constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
2024-02-22 20:11:09 +01:00
|
|
|
app('view')->share('title', (string)trans('firefly.bills'));
|
2020-06-30 19:06:05 +02:00
|
|
|
app('view')->share('mainTitleIcon', 'fa-calendar-o');
|
|
|
|
$this->attachments = app(AttachmentHelperInterface::class);
|
|
|
|
$this->repository = app(BillRepositoryInterface::class);
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2021-03-28 11:46:23 +02:00
|
|
|
|
2020-06-30 19:06:05 +02:00
|
|
|
/**
|
|
|
|
* Edit a bill.
|
|
|
|
*
|
2021-09-18 10:26:12 +02:00
|
|
|
* @return Factory|View
|
2020-06-30 19:06:05 +02:00
|
|
|
*/
|
|
|
|
public function edit(Request $request, Bill $bill)
|
|
|
|
{
|
2024-01-01 15:24:15 +01:00
|
|
|
$periods = [];
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2020-06-30 19:06:05 +02:00
|
|
|
/** @var array $billPeriods */
|
2024-01-01 15:24:15 +01:00
|
|
|
$billPeriods = config('firefly.bill_periods');
|
2020-06-30 19:06:05 +02:00
|
|
|
|
|
|
|
foreach ($billPeriods as $current) {
|
2024-02-22 20:11:09 +01:00
|
|
|
$periods[$current] = (string)trans('firefly.'.$current);
|
2020-06-30 19:06:05 +02:00
|
|
|
}
|
|
|
|
|
2024-02-22 20:11:09 +01:00
|
|
|
$subTitle = (string)trans('firefly.edit_bill', ['name' => $bill->name]);
|
2020-06-30 19:06:05 +02:00
|
|
|
|
|
|
|
// put previous url in session if not redirect from store (not "return_to_edit").
|
|
|
|
if (true !== session('bills.edit.fromUpdate')) {
|
2022-04-12 18:19:30 +02:00
|
|
|
$this->rememberPreviousUrl('bills.edit.url');
|
2020-06-30 19:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$currency = app('amount')->getDefaultCurrency();
|
2022-12-24 05:06:39 +01:00
|
|
|
$bill->amount_min = app('steam')->bcround($bill->amount_min, $currency->decimal_places);
|
|
|
|
$bill->amount_max = app('steam')->bcround($bill->amount_max, $currency->decimal_places);
|
2020-06-30 19:06:05 +02:00
|
|
|
$rules = $this->repository->getRulesForBill($bill);
|
|
|
|
$defaultCurrency = app('amount')->getDefaultCurrency();
|
|
|
|
|
|
|
|
// code to handle active-checkboxes
|
2024-01-01 15:24:15 +01:00
|
|
|
$hasOldInput = null !== $request->old('_token');
|
2020-06-30 19:06:05 +02:00
|
|
|
|
2024-01-01 15:24:15 +01:00
|
|
|
$preFilled = [
|
2022-03-28 12:23:46 +02:00
|
|
|
'bill_end_date' => $bill->end_date,
|
|
|
|
'extension_date' => $bill->extension_date,
|
2020-06-30 19:06:05 +02:00
|
|
|
'notes' => $this->repository->getNoteText($bill),
|
|
|
|
'transaction_currency_id' => $bill->transaction_currency_id,
|
2024-02-22 20:11:09 +01:00
|
|
|
'active' => $hasOldInput ? (bool)$request->old('active') : $bill->active,
|
2023-11-05 09:40:45 +01:00
|
|
|
'object_group' => null !== $bill->objectGroups->first() ? $bill->objectGroups->first()->title : '',
|
2020-06-30 19:06:05 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$request->session()->flash('preFilled', $preFilled);
|
|
|
|
$request->session()->forget('bills.edit.fromUpdate');
|
|
|
|
|
2022-01-29 14:11:12 +01:00
|
|
|
return view('bills.edit', compact('subTitle', 'periods', 'rules', 'bill', 'defaultCurrency', 'preFilled'));
|
2020-06-30 19:06:05 +02:00
|
|
|
}
|
2021-03-28 11:46:23 +02:00
|
|
|
|
2020-06-30 19:06:05 +02:00
|
|
|
/**
|
|
|
|
* Update a bill.
|
|
|
|
*/
|
|
|
|
public function update(BillUpdateRequest $request, Bill $bill): RedirectResponse
|
|
|
|
{
|
|
|
|
$billData = $request->getBillData();
|
|
|
|
$bill = $this->repository->update($bill, $billData);
|
|
|
|
|
2024-01-01 08:17:15 +01:00
|
|
|
Log::channel('audit')->info(sprintf('Updated bill #%d.', $bill->id), $billData);
|
|
|
|
|
2024-02-22 20:11:09 +01:00
|
|
|
$request->session()->flash('success', (string)trans('firefly.updated_bill', ['name' => $bill->name]));
|
2020-06-30 19:06:05 +02:00
|
|
|
app('preferences')->mark();
|
|
|
|
|
2023-12-20 19:35:52 +01:00
|
|
|
/** @var null|array $files */
|
2024-01-01 15:24:15 +01:00
|
|
|
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
|
2020-06-30 19:06:05 +02:00
|
|
|
if (null !== $files && !auth()->user()->hasRole('demo')) {
|
|
|
|
$this->attachments->saveAttachmentsForModel($bill, $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-02-22 20:11:09 +01:00
|
|
|
session()->flash('info', (string)trans('firefly.no_att_demo_user'));
|
2020-06-30 19:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// flash messages
|
|
|
|
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-30 19:06:05 +02:00
|
|
|
}
|
2022-04-12 18:19:30 +02:00
|
|
|
$redirect = redirect($this->getPreviousUrl('bills.edit.url'));
|
2020-06-30 19:06:05 +02:00
|
|
|
|
2024-02-22 20:11:09 +01:00
|
|
|
if (1 === (int)$request->get('return_to_edit')) {
|
2020-06-30 19:06:05 +02:00
|
|
|
$request->session()->put('bills.edit.fromUpdate', true);
|
|
|
|
|
|
|
|
$redirect = redirect(route('bills.edit', [$bill->id]))->withInput(['return_to_edit' => 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $redirect;
|
|
|
|
}
|
|
|
|
}
|