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

382 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-04-07 15:23:16 -05:00
use ExpandedForm;
2017-11-25 13:54:42 -06:00
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
2016-12-28 04:34:00 -06:00
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
2015-02-25 08:19:14 -06:00
use FireflyIII\Http\Requests\BillFormRequest;
use FireflyIII\Models\Bill;
2017-11-25 13:54:42 -06:00
use FireflyIII\Models\Note;
2015-02-25 08:19:14 -06:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2018-04-07 15:23:16 -05:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
2018-04-14 02:59:04 -05:00
use FireflyIII\TransactionRules\TransactionMatcher;
use FireflyIII\Transformers\BillTransformer;
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;
use Preferences;
2018-02-17 05:24:29 -06:00
use Symfony\Component\HttpFoundation\ParameterBag;
2015-02-25 08:19:14 -06:00
use URL;
use View;
/**
2017-11-15 05:25:49 -06:00
* Class BillController.
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;
2015-04-02 15:39:31 -05:00
/**
2016-02-04 00:27:03 -06:00
*
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);
View::share('uploadSize', $uploadSize);
2016-10-29 00:44:46 -05:00
$this->middleware(
function ($request, $next) {
2017-12-16 12:46:36 -06:00
app('view')->share('title', trans('firefly.bills'));
app('view')->share('mainTitleIcon', 'fa-calendar-o');
2017-11-25 13:54:42 -06:00
$this->attachments = app(AttachmentHelperInterface::class);
2016-10-29 00:44:46 -05:00
return $next($request);
}
);
2015-02-25 08:19:14 -06:00
}
/**
2017-02-17 13:14:22 -06:00
* @param Request $request
*
* @return View
2015-02-25 08:19:14 -06:00
*/
2018-04-07 15:23:16 -05:00
public function create(Request $request, CurrencyRepositoryInterface $repository)
2015-02-25 08:19:14 -06:00
{
2016-05-20 10:28:07 -05:00
$periods = [];
foreach (config('firefly.bill_periods') 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-04-07 15:23:16 -05:00
$subTitle = trans('firefly.create_new_bill');
$defaultCurrency = app('amount')->getDefaultCurrency();
$currencies = ExpandedForm::makeSelectList($repository->get());
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-07 15:23:16 -05:00
return view('bills.create', compact('periods', 'subTitle', 'currencies', 'defaultCurrency'));
2015-02-25 08:19:14 -06:00
}
/**
2017-12-29 02:05:35 -06:00
* @param Bill $bill
2015-02-25 08:19:14 -06:00
*
* @return 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');
2015-05-25 15:16:00 -05:00
$subTitle = 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
}
/**
2017-02-17 13:14:22 -06:00
* @param Request $request
2015-05-03 05:54:39 -05:00
* @param BillRepositoryInterface $repository
* @param Bill $bill
2015-02-25 08:19:14 -06:00
*
2017-02-17 13:14:22 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-02-25 08:19:14 -06:00
*/
2017-02-17 13:14:22 -06:00
public function destroy(Request $request, BillRepositoryInterface $repository, Bill $bill)
2015-02-25 08:19:14 -06:00
{
2017-02-05 01:26:54 -06:00
$name = $bill->name;
2015-04-05 11:20:06 -05:00
$repository->destroy($bill);
2018-04-02 08:10:40 -05:00
$request->session()->flash('success', (string)trans('firefly.deleted_bill', ['name' => $name]));
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
}
/**
2017-02-17 13:14:22 -06:00
* @param Request $request
* @param Bill $bill
2015-02-25 08:19:14 -06:00
*
* @return View
2015-02-25 08:19:14 -06:00
*/
2018-04-08 09:27:52 -05:00
public function edit(Request $request, CurrencyRepositoryInterface $repository, Bill $bill)
2015-02-25 08:19:14 -06:00
{
2016-05-20 10:28:07 -05:00
$periods = [];
foreach (config('firefly.bill_periods') as $current) {
$periods[$current] = trans('firefly.' . $current);
}
2015-06-09 10:56:08 -05:00
$subTitle = 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();
2017-08-26 03:00:08 -05:00
$bill->amount_min = round($bill->amount_min, $currency->decimal_places);
$bill->amount_max = round($bill->amount_max, $currency->decimal_places);
2018-04-08 09:27:52 -05:00
$defaultCurrency = app('amount')->getDefaultCurrency();
$currencies = ExpandedForm::makeSelectList($repository->get());
2017-08-26 03:00:08 -05:00
2017-11-25 13:54:42 -06:00
$preFilled = [
'notes' => '',
];
/** @var Note $note */
$note = $bill->notes()->first();
if (null !== $note) {
$preFilled['notes'] = $note->text;
}
$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-08 09:27:52 -05:00
return view('bills.edit', compact('subTitle', 'periods', 'bill', 'defaultCurrency', 'currencies'));
2015-02-25 08:19:14 -06:00
}
/**
* @param BillRepositoryInterface $repository
*
* @return View
2015-02-25 08:19:14 -06:00
*/
public function index(BillRepositoryInterface $repository)
2015-02-25 08:19:14 -06:00
{
2018-02-17 05:24:29 -06:00
$start = session('start');
$end = session('end');
2018-04-02 08:10:40 -05:00
$pageSize = (int)Preferences::get('listPageSize', 50)->data;
2018-02-17 05:24:29 -06:00
$paginator = $repository->getPaginator($pageSize);
$parameters = new ParameterBag();
$parameters->set('start', $start);
$parameters->set('end', $end);
$transformer = new BillTransformer($parameters);
/** @var Collection $bills */
$bills = $paginator->getCollection()->map(
function (Bill $bill) use ($transformer) {
return $transformer->transform($bill);
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:
$rules = $repository->getRulesForBills($paginator->getCollection());
$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
}
/**
2017-02-17 13:14:22 -06:00
* @param Request $request
2015-05-03 05:54:39 -05:00
* @param BillRepositoryInterface $repository
* @param Bill $bill
2015-02-25 08:19:14 -06:00
*
2017-02-17 13:14:22 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2018-04-14 02:59:04 -05:00
* @throws \FireflyIII\Exceptions\FireflyException
2015-02-25 08:19:14 -06:00
*/
2017-02-17 13:14:22 -06:00
public function rescan(Request $request, BillRepositoryInterface $repository, Bill $bill)
2015-02-25 08:19:14 -06:00
{
2018-04-02 08:10:40 -05:00
if (0 === (int)$bill->active) {
$request->session()->flash('warning', (string)trans('firefly.cannot_scan_inactive_bill'));
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-04-14 02:59:04 -05:00
$set = $repository->getRulesForBill($bill);
$total = 0;
foreach ($set as $rule) {
// simply fire off all rules?
/** @var TransactionMatcher $matcher */
$matcher = app(TransactionMatcher::class);
$matcher->setLimit(100000); // large upper limit
$matcher->setRange(100000); // large upper limit
$matcher->setRule($rule);
$matchingTransactions = $matcher->findTransactionsByRule();
$total += $matchingTransactions->count();
$repository->linkCollectionToBill($bill, $matchingTransactions);
2015-02-25 08:19:14 -06:00
}
2018-04-14 02:59:04 -05:00
$request->session()->flash('success', (string)trans('firefly.rescanned_bill', ['total' => $total]));
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
}
/**
2016-12-28 06:02:56 -06:00
* @param Request $request
2015-05-03 05:54:39 -05:00
* @param BillRepositoryInterface $repository
* @param Bill $bill
2015-02-25 08:19:14 -06:00
*
* @return View
2015-02-25 08:19:14 -06:00
*/
2016-12-28 06:02:56 -06:00
public function show(Request $request, BillRepositoryInterface $repository, Bill $bill)
2015-02-25 08:19:14 -06:00
{
2018-04-14 02:59:04 -05:00
// add info about rules:
$rules = $repository->getRulesForBill($bill);
$subTitle = $bill->name;
$start = session('start');
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)Preferences::get('listPageSize', 50)->data;
$yearAverage = $repository->getYearAverage($bill, $start);
2016-07-05 01:57:45 -05:00
$overallAverage = $repository->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);
$resource = new Item($bill, new BillTransformer($parameters), 'bill');
$object = $manager->createData($resource)->toArray();
// use collector:
2016-12-28 04:34:00 -06:00
/** @var JournalCollectorInterface $collector */
2017-02-05 09:16:15 -06:00
$collector = app(JournalCollectorInterface::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();
2017-11-05 14:16:20 -06:00
$transactions = $collector->getPaginatedJournals();
$transactions->setPath(route('bills.show', [$bill->id]));
2015-02-25 08:19:14 -06:00
2018-04-14 02:59:04 -05:00
return view('bills.show', compact('transactions', 'rules', 'yearAverage', 'overallAverage', 'year', 'object', 'bill', 'subTitle'));
2015-02-25 08:19:14 -06:00
}
/**
2015-05-03 05:54:39 -05:00
* @param BillFormRequest $request
* @param BillRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse
2015-02-25 08:19:14 -06:00
*/
2018-04-07 15:23:16 -05:00
public function store(BillFormRequest $request, BillRepositoryInterface $repository, RuleGroupRepositoryInterface $ruleGroupRepository)
2015-02-25 08:19:14 -06:00
{
2015-03-29 04:51:26 -05:00
$billData = $request->getBillData();
$bill = $repository->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]));
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
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-04-08 09:27:52 -05:00
// do return to original bill form?
$return = 'false';
if (1 === (int)$request->get('create_another')) {
$return = 'true';
}
2018-04-07 15:23:16 -05:00
// find first rule group, or create one:
$count = $ruleGroupRepository->count();
if ($count === 0) {
$data = [
'title' => (string)trans('firefly.rulegroup_for_bills_title'),
'description' => (string)trans('firefly.rulegroup_for_bills_description'),
];
$group = $ruleGroupRepository->store($data);
2015-03-03 10:40:17 -06:00
}
2018-04-07 15:23:16 -05:00
if ($count > 0) {
$group = $ruleGroupRepository->getActiveGroups(auth()->user())->first();
}
// redirect to page that will create a new rule.
2018-04-08 09:27:52 -05:00
$params = http_build_query(['fromBill' => $bill->id, 'return' => $return]);
return redirect(route('rules.create', [$group->id]) . '?' . $params);
2015-02-25 08:19:14 -06:00
}
/**
2015-05-03 05:54:39 -05:00
* @param BillFormRequest $request
* @param BillRepositoryInterface $repository
* @param Bill $bill
2015-02-25 08:19:14 -06:00
*
* @return \Illuminate\Http\RedirectResponse
2015-02-25 08:19:14 -06:00
*/
2015-05-03 05:54:39 -05:00
public function update(BillFormRequest $request, BillRepositoryInterface $repository, Bill $bill)
2015-02-25 08:19:14 -06:00
{
2015-03-29 04:51:26 -05:00
$billData = $request->getBillData();
$bill = $repository->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]));
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
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-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
2015-07-06 09:27:21 -05:00
return 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
}
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('bills.edit.uri'));
2015-02-25 08:19:14 -06:00
}
}