Files
firefly-iii/app/Http/Controllers/Bill/CreateController.php

134 lines
4.4 KiB
PHP
Raw Normal View History

2020-06-30 19:06:05 +02:00
<?php
/**
* CreateController.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\Exceptions\FireflyException;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\BillStoreRequest;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2021-03-21 09:15:40 +01:00
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
2020-06-30 19:06:05 +02:00
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
2021-04-07 07:28:43 +02:00
use Log;
2020-06-30 19:06:05 +02:00
/**
* Class CreateController
*/
class CreateController extends Controller
{
private AttachmentHelperInterface $attachments;
private BillRepositoryInterface $repository;
/**
* BillController constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
2022-03-29 14:58:06 +02: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);
}
);
}
/**
* Create a new bill.
*
* @param Request $request
*
* @return Factory|View
2020-06-30 19:06:05 +02:00
*/
public function create(Request $request)
{
$periods = [];
/** @var array $billPeriods */
$billPeriods = config('firefly.bill_periods');
foreach ($billPeriods as $current) {
2022-03-29 14:58:06 +02:00
$periods[$current] = (string) trans('firefly.repeat_freq_' . $current);
2020-06-30 19:06:05 +02:00
}
2022-03-29 14:58:06 +02:00
$subTitle = (string) trans('firefly.create_new_bill');
2020-06-30 19:06:05 +02:00
$defaultCurrency = app('amount')->getDefaultCurrency();
// put previous url in session if not redirect from store (not "create another").
if (true !== session('bills.create.fromStore')) {
2022-04-12 18:19:30 +02:00
$this->rememberPreviousUrl('bills.create.url');
2020-06-30 19:06:05 +02:00
}
$request->session()->forget('bills.create.fromStore');
return view('bills.create', compact('periods', 'subTitle', 'defaultCurrency'));
2020-06-30 19:06:05 +02:00
}
/**
* Store a new bill.
*
* @param BillStoreRequest $request
*
* @return RedirectResponse
*
*/
public function store(BillStoreRequest $request): RedirectResponse
{
2022-03-29 14:58:06 +02:00
$billData = $request->getBillData();
2022-03-28 12:23:46 +02:00
2020-06-30 19:06:05 +02:00
$billData['active'] = true;
try {
$bill = $this->repository->store($billData);
} catch (FireflyException $e) {
Log::error($e->getMessage());
2022-03-29 14:58:06 +02:00
$request->session()->flash('error', (string) trans('firefly.bill_store_error'));
2020-06-30 19:06:05 +02:00
return redirect(route('bills.create'))->withInput();
}
2022-03-29 14:58:06 +02:00
$request->session()->flash('success', (string) trans('firefly.stored_new_bill', ['name' => $bill->name]));
2020-06-30 19:06:05 +02:00
app('preferences')->mark();
/** @var array $files */
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
if (null !== $files && !auth()->user()->hasRole('demo')) {
$this->attachments->saveAttachmentsForModel($bill, $files);
}
if (null !== $files && auth()->user()->hasRole('demo')) {
2022-03-29 14:58:06 +02:00
session()->flash('info', (string) trans('firefly.no_att_demo_user'));
2020-06-30 19:06:05 +02:00
}
if (count($this->attachments->getMessages()->get('attachments')) > 0) {
$request->session()->flash('info', $this->attachments->getMessages()->get('attachments'));
2020-06-30 19:06:05 +02:00
}
return redirect(route('rules.create-from-bill', [$bill->id]));
}
}