firefly-iii/app/Http/Controllers/BillController.php

409 lines
14 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* BillController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2016-05-20 01:57:45 -05:00
namespace FireflyIII\Http\Controllers;
2015-02-25 08:19:14 -06:00
2018-07-26 21:46:21 -05:00
use Carbon\Carbon;
2017-11-25 13:54:42 -06:00
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
2015-02-25 08:19:14 -06:00
use FireflyIII\Http\Requests\BillFormRequest;
2019-01-27 09:42:28 -06:00
use FireflyIII\Models\Attachment;
2015-02-25 08:19:14 -06:00
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2018-04-14 02:59:04 -05:00
use FireflyIII\TransactionRules\TransactionMatcher;
2019-01-27 09:42:28 -06:00
use FireflyIII\Transformers\AttachmentTransformer;
use FireflyIII\Transformers\BillTransformer;
2018-07-08 05:28:42 -05:00
use Illuminate\Http\RedirectResponse;
2016-12-28 06:02:56 -06:00
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use League\Fractal\Manager;
use League\Fractal\Resource\Item;
use League\Fractal\Serializer\DataArraySerializer;
2018-02-17 05:24:29 -06:00
use Symfony\Component\HttpFoundation\ParameterBag;
2015-02-25 08:19:14 -06:00
use URL;
/**
2017-11-15 05:25:49 -06:00
* Class BillController.
2018-07-14 08:22:21 -05:00
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2015-02-25 08:19:14 -06:00
*/
class BillController extends Controller
{
2017-11-25 13:54:42 -06:00
/** @var AttachmentHelperInterface Helper for attachments. */
private $attachments;
2018-07-21 01:55:32 -05:00
/** @var BillRepositoryInterface Bill repository */
2018-04-29 00:45:38 -05:00
private $billRepository;
2017-11-25 13:54:42 -06:00
2015-04-02 15:39:31 -05:00
/**
2018-07-22 01:10:16 -05:00
* BillController constructor.
2015-04-02 15:39:31 -05:00
*/
2015-02-25 08:19:14 -06:00
public function __construct()
{
2015-04-28 08:26:30 -05:00
parent::__construct();
2016-10-29 00:44:46 -05:00
2017-11-25 13:54:42 -06:00
$maxFileSize = app('steam')->phpBytes(ini_get('upload_max_filesize'));
$maxPostSize = app('steam')->phpBytes(ini_get('post_max_size'));
$uploadSize = min($maxFileSize, $maxPostSize);
app('view')->share('uploadSize', $uploadSize);
2017-11-25 13:54:42 -06:00
2016-10-29 00:44:46 -05:00
$this->middleware(
function ($request, $next) {
2018-07-15 02:38:49 -05:00
app('view')->share('title', (string)trans('firefly.bills'));
2017-12-16 12:46:36 -06:00
app('view')->share('mainTitleIcon', 'fa-calendar-o');
2018-04-29 00:45:38 -05:00
$this->attachments = app(AttachmentHelperInterface::class);
$this->billRepository = app(BillRepositoryInterface::class);
2016-10-29 00:44:46 -05:00
return $next($request);
}
);
2015-02-25 08:19:14 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Create a new bill.
*
2018-04-29 00:45:38 -05:00
* @param Request $request
2017-02-17 13:14:22 -06:00
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-25 08:19:14 -06:00
*/
2018-04-29 00:45:38 -05:00
public function create(Request $request)
2015-02-25 08:19:14 -06:00
{
2016-05-20 10:28:07 -05:00
$periods = [];
2018-04-29 00:45:38 -05:00
/** @var array $billPeriods */
$billPeriods = config('firefly.bill_periods');
foreach ($billPeriods as $current) {
2018-04-06 22:58:59 -05:00
$periods[$current] = strtolower((string)trans('firefly.repeat_freq_' . $current));
2016-05-20 10:28:07 -05:00
}
2018-07-15 02:38:49 -05:00
$subTitle = (string)trans('firefly.create_new_bill');
2018-04-07 15:23:16 -05:00
$defaultCurrency = app('amount')->getDefaultCurrency();
2015-06-09 10:56:08 -05:00
2015-04-28 01:12:12 -05:00
// put previous url in session if not redirect from store (not "create another").
2017-11-15 05:25:49 -06:00
if (true !== session('bills.create.fromStore')) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('bills.create.uri');
2015-04-28 01:12:12 -05:00
}
2017-02-17 13:14:22 -06:00
$request->session()->forget('bills.create.fromStore');
2015-04-28 01:12:12 -05:00
2018-04-29 00:45:38 -05:00
return view('bills.create', compact('periods', 'subTitle', 'defaultCurrency'));
2015-02-25 08:19:14 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Delete a bill.
*
2017-12-29 02:05:35 -06:00
* @param Bill $bill
2015-02-25 08:19:14 -06:00
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-25 08:19:14 -06:00
*/
2017-12-29 02:05:35 -06:00
public function delete(Bill $bill)
2015-02-25 08:19:14 -06:00
{
2015-04-28 01:12:12 -05:00
// put previous url in session
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('bills.delete.uri');
2018-07-15 02:38:49 -05:00
$subTitle = (string)trans('firefly.delete_bill', ['name' => $bill->name]);
2015-04-28 01:12:12 -05:00
2015-05-05 03:23:01 -05:00
return view('bills.delete', compact('bill', 'subTitle'));
2015-02-25 08:19:14 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Destroy a bill.
*
2018-04-29 00:45:38 -05:00
* @param Request $request
* @param Bill $bill
2015-02-25 08:19:14 -06:00
*
2018-07-08 05:28:42 -05:00
* @return RedirectResponse|\Illuminate\Routing\Redirector
2015-02-25 08:19:14 -06:00
*/
2018-04-29 00:45:38 -05:00
public function destroy(Request $request, Bill $bill)
2015-02-25 08:19:14 -06:00
{
2017-02-05 01:26:54 -06:00
$name = $bill->name;
2018-04-29 00:45:38 -05:00
$this->billRepository->destroy($bill);
2015-04-05 11:20:06 -05:00
2018-04-02 08:10:40 -05:00
$request->session()->flash('success', (string)trans('firefly.deleted_bill', ['name' => $name]));
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
2015-02-25 08:19:14 -06:00
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('bills.delete.uri'));
2015-02-25 08:19:14 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Edit a bill.
*
2018-04-29 00:45:38 -05:00
* @param Request $request
* @param Bill $bill
2015-02-25 08:19:14 -06:00
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-25 08:19:14 -06:00
*/
2018-04-29 00:45:38 -05:00
public function edit(Request $request, Bill $bill)
2015-02-25 08:19:14 -06:00
{
2016-05-20 10:28:07 -05:00
$periods = [];
2018-04-29 00:45:38 -05:00
/** @var array $billPeriods */
$billPeriods = config('firefly.bill_periods');
foreach ($billPeriods as $current) {
2018-07-15 02:38:49 -05:00
$periods[$current] = (string)trans('firefly.' . $current);
2016-05-20 10:28:07 -05:00
}
2018-04-29 00:45:38 -05:00
2018-07-15 02:38:49 -05:00
$subTitle = (string)trans('firefly.edit_bill', ['name' => $bill->name]);
2015-02-25 08:19:14 -06:00
2015-04-28 01:12:12 -05:00
// put previous url in session if not redirect from store (not "return_to_edit").
2017-11-15 05:25:49 -06:00
if (true !== session('bills.edit.fromUpdate')) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('bills.edit.uri');
2015-04-28 01:12:12 -05:00
}
2017-08-26 03:00:08 -05:00
2017-10-05 04:49:06 -05:00
$currency = app('amount')->getDefaultCurrency();
$bill->amount_min = round((float)$bill->amount_min, $currency->decimal_places);
$bill->amount_max = round((float)$bill->amount_max, $currency->decimal_places);
2018-04-08 09:27:52 -05:00
$defaultCurrency = app('amount')->getDefaultCurrency();
2017-08-26 03:00:08 -05:00
2018-06-18 14:07:09 -05:00
// code to handle active-checkboxes
$hasOldInput = null !== $request->old('_token');
2017-11-25 13:54:42 -06:00
$preFilled = [
2018-04-29 00:45:38 -05:00
'notes' => $this->billRepository->getNoteText($bill),
'transaction_currency_id' => $bill->transaction_currency_id,
2018-06-18 14:07:09 -05:00
'active' => $hasOldInput ? (bool)$request->old('active') : $bill->active,
2017-11-25 13:54:42 -06:00
];
$request->session()->flash('preFilled', $preFilled);
2017-02-17 13:14:22 -06:00
$request->session()->forget('bills.edit.fromUpdate');
2015-04-28 01:12:12 -05:00
2018-04-29 00:45:38 -05:00
return view('bills.edit', compact('subTitle', 'periods', 'bill', 'defaultCurrency', 'preFilled'));
2015-02-25 08:19:14 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Show all bills.
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-25 08:19:14 -06:00
*/
2018-04-29 00:45:38 -05:00
public function index()
2015-02-25 08:19:14 -06:00
{
2018-02-17 05:24:29 -06:00
$start = session('start');
$end = session('end');
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
2018-04-29 00:45:38 -05:00
$paginator = $this->billRepository->getPaginator($pageSize);
2018-02-17 05:24:29 -06:00
$parameters = new ParameterBag();
$parameters->set('start', $start);
$parameters->set('end', $end);
2018-12-17 00:09:44 -06:00
/** @var BillTransformer $transformer */
$transformer = app(BillTransformer::class);
$transformer->setParameters($parameters);
/** @var Collection $bills */
$bills = $paginator->getCollection()->map(
function (Bill $bill) use ($transformer) {
2018-08-12 03:06:20 -05:00
$return = $transformer->transform($bill);
$return['currency'] = $bill->transactionCurrency;
return $return;
2015-02-25 08:19:14 -06:00
}
);
2018-04-06 22:58:59 -05:00
$bills = $bills->sortBy(
function (array $bill) {
return (int)!$bill['active'] . strtolower($bill['name']);
}
);
2015-02-25 08:19:14 -06:00
2018-04-08 10:36:37 -05:00
// add info about rules:
2018-04-29 00:45:38 -05:00
$rules = $this->billRepository->getRulesForBills($paginator->getCollection());
2018-04-08 10:36:37 -05:00
$bills = $bills->map(
function (array $bill) use ($rules) {
$bill['rules'] = $rules[$bill['id']] ?? [];
return $bill;
}
);
$paginator->setPath(route('bills.index'));
return view('bills.index', compact('bills', 'paginator'));
2015-02-25 08:19:14 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Rescan bills for transactions.
*
2018-04-29 00:45:38 -05:00
* @param Request $request
* @param Bill $bill
2015-02-25 08:19:14 -06:00
*
2018-07-08 05:28:42 -05:00
* @return RedirectResponse|\Illuminate\Routing\Redirector
2018-04-14 02:59:04 -05:00
* @throws \FireflyIII\Exceptions\FireflyException
2015-02-25 08:19:14 -06:00
*/
2018-04-29 00:45:38 -05:00
public function rescan(Request $request, Bill $bill)
2015-02-25 08:19:14 -06:00
{
2018-07-08 05:08:53 -05:00
if (false === $bill->active) {
2018-04-02 08:10:40 -05:00
$request->session()->flash('warning', (string)trans('firefly.cannot_scan_inactive_bill'));
2015-02-25 08:19:14 -06:00
}
2018-07-08 05:08:53 -05:00
if (true === $bill->active) {
$set = $this->billRepository->getRulesForBill($bill);
$total = 0;
foreach ($set as $rule) {
// simply fire off all rules?
/** @var TransactionMatcher $matcher */
$matcher = app(TransactionMatcher::class);
2018-12-21 03:11:18 -06:00
$matcher->setSearchLimit(100000); // large upper limit
$matcher->setTriggeredLimit(100000); // large upper limit
2018-07-08 05:08:53 -05:00
$matcher->setRule($rule);
$matchingTransactions = $matcher->findTransactionsByRule();
$total += $matchingTransactions->count();
$this->billRepository->linkCollectionToBill($bill, $matchingTransactions);
}
2015-02-25 08:19:14 -06:00
2018-04-14 02:59:04 -05:00
2018-07-08 05:08:53 -05:00
$request->session()->flash('success', (string)trans('firefly.rescanned_bill', ['total' => $total]));
app('preferences')->mark();
}
2015-02-25 08:19:14 -06:00
2015-07-06 09:27:21 -05:00
return redirect(URL::previous());
2015-02-25 08:19:14 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Show a bill.
*
2018-04-29 00:45:38 -05:00
* @param Request $request
* @param Bill $bill
2015-02-25 08:19:14 -06:00
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2015-02-25 08:19:14 -06:00
*/
2018-04-29 00:45:38 -05:00
public function show(Request $request, Bill $bill)
2015-02-25 08:19:14 -06:00
{
2018-04-14 02:59:04 -05:00
// add info about rules:
2018-08-05 11:59:15 -05:00
$rules = $this->billRepository->getRulesForBill($bill);
$subTitle = $bill->name;
2018-07-26 21:46:21 -05:00
/** @var Carbon $start */
2018-08-05 11:59:15 -05:00
$start = session('start');
2018-07-26 21:46:21 -05:00
/** @var Carbon $end */
2017-12-02 00:10:36 -06:00
$end = session('end');
$year = $start->year;
2018-04-02 08:10:40 -05:00
$page = (int)$request->get('page');
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
2018-04-29 00:45:38 -05:00
$yearAverage = $this->billRepository->getYearAverage($bill, $start);
$overallAverage = $this->billRepository->getOverallAverage($bill);
$manager = new Manager();
$manager->setSerializer(new DataArraySerializer());
2018-04-06 22:58:59 -05:00
$manager->parseIncludes(['attachments', 'notes']);
// Make a resource out of the data and
2018-02-17 05:24:29 -06:00
$parameters = new ParameterBag();
$parameters->set('start', $start);
$parameters->set('end', $end);
2018-12-17 00:09:44 -06:00
/** @var BillTransformer $transformer */
$transformer = app(BillTransformer::class);
$transformer->setParameters($parameters);
$resource = new Item($bill, $transformer, 'bill');
2018-08-12 03:06:20 -05:00
$object = $manager->createData($resource)->toArray();
$object['data']['currency'] = $bill->transactionCurrency;
// use collector:
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
2016-12-28 04:34:00 -06:00
$collector->setAllAssetAccounts()->setBills(new Collection([$bill]))->setLimit($pageSize)->setPage($page)->withBudgetInformation()
2016-12-28 06:05:40 -06:00
->withCategoryInformation();
$transactions = $collector->getPaginatedTransactions();
2017-11-05 14:16:20 -06:00
$transactions->setPath(route('bills.show', [$bill->id]));
2019-01-27 09:42:28 -06:00
// transform any attachments as well.
$collection = $this->billRepository->getAttachments($bill);
$attachments = new Collection;
if ($collection->count() > 0) {
/** @var AttachmentTransformer $transformer */
$transformer = app(AttachmentTransformer::class);
$attachments = $collection->each(
function (Attachment $attachment) use ($transformer) {
return $transformer->transform($attachment);
}
);
}
2015-02-25 08:19:14 -06:00
2019-01-27 09:42:28 -06:00
return view('bills.show', compact('attachments', 'transactions', 'rules', 'yearAverage', 'overallAverage', 'year', 'object', 'bill', 'subTitle'));
2015-02-25 08:19:14 -06:00
}
2018-07-08 05:08:53 -05:00
2015-02-25 08:19:14 -06:00
/**
2018-07-22 01:10:16 -05:00
* Store a new bill.
*
2018-04-29 00:45:38 -05:00
* @param BillFormRequest $request
2015-05-03 05:54:39 -05:00
*
2018-07-08 05:28:42 -05:00
* @return RedirectResponse
2018-07-14 08:22:21 -05:00
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2015-02-25 08:19:14 -06:00
*/
2018-07-08 05:28:42 -05:00
public function store(BillFormRequest $request): RedirectResponse
2015-02-25 08:19:14 -06:00
{
2018-07-10 13:21:34 -05:00
$billData = $request->getBillData();
$billData['active'] = true;
$bill = $this->billRepository->store($billData);
2018-04-03 12:12:59 -05:00
if (null === $bill) {
$request->session()->flash('error', (string)trans('firefly.bill_store_error'));
return redirect(route('bills.create'))->withInput();
}
2018-04-02 08:10:40 -05:00
$request->session()->flash('success', (string)trans('firefly.stored_new_bill', ['name' => $bill->name]));
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
2015-02-25 08:19:14 -06:00
2017-11-25 13:54:42 -06:00
/** @var array $files */
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
$this->attachments->saveAttachmentsForModel($bill, $files);
2018-04-27 23:23:13 -05:00
if (\count($this->attachments->getMessages()->get('attachments')) > 0) {
$request->session()->flash('info', $this->attachments->getMessages()->get('attachments')); // @codeCoverageIgnore
2017-11-25 13:54:42 -06:00
}
2018-08-05 11:59:15 -05:00
return redirect(route('rules.create-from-bill', [$bill->id]));
2015-02-25 08:19:14 -06:00
}
/**
2018-07-22 01:10:16 -05:00
* Update a bill.
*
2018-04-29 00:45:38 -05:00
* @param BillFormRequest $request
* @param Bill $bill
2015-02-25 08:19:14 -06:00
*
2018-07-08 05:28:42 -05:00
* @return RedirectResponse
2015-02-25 08:19:14 -06:00
*/
2018-07-08 05:28:42 -05:00
public function update(BillFormRequest $request, Bill $bill): RedirectResponse
2015-02-25 08:19:14 -06:00
{
2015-03-29 04:51:26 -05:00
$billData = $request->getBillData();
2018-04-29 00:45:38 -05:00
$bill = $this->billRepository->update($bill, $billData);
2015-02-25 08:19:14 -06:00
2018-04-02 08:10:40 -05:00
$request->session()->flash('success', (string)trans('firefly.updated_bill', ['name' => $bill->name]));
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
2015-02-25 08:19:14 -06:00
2017-11-25 13:54:42 -06:00
/** @var array $files */
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
$this->attachments->saveAttachmentsForModel($bill, $files);
// flash messages
2018-04-27 23:23:13 -05:00
if (\count($this->attachments->getMessages()->get('attachments')) > 0) {
$request->session()->flash('info', $this->attachments->getMessages()->get('attachments')); // @codeCoverageIgnore
2017-11-25 13:54:42 -06:00
}
2018-07-08 05:08:53 -05:00
$redirect = redirect($this->getPreviousUri('bills.edit.uri'));
2017-11-25 13:54:42 -06:00
2018-04-02 08:10:40 -05:00
if (1 === (int)$request->get('return_to_edit')) {
2017-03-18 05:02:02 -05:00
// @codeCoverageIgnoreStart
2017-02-17 13:14:22 -06:00
$request->session()->put('bills.edit.fromUpdate', true);
2015-04-28 01:12:12 -05:00
2018-07-08 05:08:53 -05:00
$redirect = redirect(route('bills.edit', [$bill->id]))->withInput(['return_to_edit' => 1]);
2017-03-18 05:02:02 -05:00
// @codeCoverageIgnoreEnd
2015-04-28 01:12:12 -05:00
}
2018-07-08 05:08:53 -05:00
return $redirect;
2015-02-25 08:19:14 -06:00
}
}