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

363 lines
13 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
2016-07-05 01:57:45 -05:00
use Carbon\Carbon;
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\Models\TransactionJournal;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2016-12-28 06:02:56 -06:00
use Illuminate\Http\Request;
2017-12-28 02:53:21 -06:00
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Preferences;
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
*/
2017-02-17 13:14:22 -06:00
public function create(Request $request)
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.create_new_bill');
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
2015-05-05 03:23:01 -05:00
return view('bills.create', compact('periods', 'subTitle'));
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);
2017-02-17 13:14:22 -06:00
$request->session()->flash('success', strval(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
*/
2017-02-17 13:14:22 -06: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 = [];
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);
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
2015-05-05 03:23:01 -05:00
return view('bills.edit', compact('subTitle', 'periods', 'bill'));
2015-02-25 08:19:14 -06:00
}
/**
* @param BillRepositoryInterface $repository
*
* @return View
2015-02-25 08:19:14 -06:00
*/
2017-12-28 02:53:21 -06:00
public function index(Request $request, BillRepositoryInterface $repository)
2015-02-25 08:19:14 -06:00
{
/** @var Carbon $start */
2016-03-29 08:55:14 -05:00
$start = session('start');
/** @var Carbon $end */
2017-12-28 02:53:21 -06:00
$end = session('end');
$page = 0 === intval($request->get('page')) ? 1 : intval($request->get('page'));
$pageSize = intval(Preferences::get('listPageSize', 50)->data);
$collection = $repository->getBills();
$total = $collection->count();
$collection = $collection->slice(($page - 1) * $pageSize, $pageSize);
$collection->each(
2016-03-29 08:55:14 -05:00
function (Bill $bill) use ($repository, $start, $end) {
2016-10-23 07:56:05 -05:00
// paid in this period?
$bill->paidDates = $repository->getPaidDatesInRange($bill, $start, $end);
2016-10-29 00:44:46 -05:00
$bill->payDates = $repository->getPayDatesInRange($bill, $start, $end);
2017-12-02 00:10:36 -06:00
$lastPaidDate = $this->lastPaidDate($repository->getPaidDatesInRange($bill, $start, $end), $start);
2016-10-23 07:56:05 -05:00
if ($bill->paidDates->count() >= $bill->payDates->count()) {
2017-12-02 00:10:36 -06:00
// if all bills have been been paid, jump to next period.
$lastPaidDate = $end;
2016-10-23 07:56:05 -05:00
}
2017-12-02 00:10:36 -06:00
$bill->nextExpectedMatch = $repository->nextExpectedMatch($bill, $lastPaidDate);
2015-02-25 08:19:14 -06:00
}
);
2017-12-28 02:53:21 -06:00
// paginate bills
2017-12-29 02:05:35 -06:00
$bills = new LengthAwarePaginator($collection, $total, $pageSize, $page);
2017-12-28 02:53:21 -06:00
$bills->setPath(route('bills.index'));
2015-02-25 08:19:14 -06:00
2015-02-27 04:09:23 -06:00
return view('bills.index', compact('bills'));
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 rescan(Request $request, BillRepositoryInterface $repository, Bill $bill)
2015-02-25 08:19:14 -06:00
{
2017-11-15 05:25:49 -06:00
if (0 === intval($bill->active)) {
2017-02-17 13:14:22 -06:00
$request->session()->flash('warning', strval(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
}
2015-04-05 11:20:06 -05:00
$journals = $repository->getPossiblyRelatedJournals($bill);
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
$repository->scan($bill, $journal);
2015-02-25 08:19:14 -06:00
}
2017-02-17 13:14:22 -06:00
$request->session()->flash('success', strval(trans('firefly.rescanned_bill')));
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
{
2016-07-05 01:57:45 -05:00
/** @var Carbon $date */
2017-12-16 12:48:31 -06:00
$date = session('start');
2017-12-02 00:10:36 -06:00
/** @var Carbon $end */
$end = session('end');
2016-07-05 01:57:45 -05:00
$year = $date->year;
2017-07-30 06:42:30 -05:00
$page = intval($request->get('page'));
$pageSize = intval(Preferences::get('listPageSize', 50)->data);
2016-07-05 01:57:45 -05:00
$yearAverage = $repository->getYearAverage($bill, $date);
$overallAverage = $repository->getOverallAverage($bill);
// 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]));
2017-12-02 00:10:36 -06:00
$bill->paidDates = $repository->getPaidDatesInRange($bill, $date, $end);
$bill->payDates = $repository->getPayDatesInRange($bill, $date, $end);
$lastPaidDate = $this->lastPaidDate($repository->getPaidDatesInRange($bill, $date, $end), $date);
if ($bill->paidDates->count() >= $bill->payDates->count()) {
// if all bills have been been paid, jump to next period.
$lastPaidDate = $end;
}
$bill->nextExpectedMatch = $repository->nextExpectedMatch($bill, $lastPaidDate);
2015-02-25 08:19:14 -06:00
$hideBill = true;
2015-05-05 03:23:01 -05:00
$subTitle = e($bill->name);
2015-02-25 08:19:14 -06:00
2017-11-05 14:16:20 -06:00
return view('bills.show', compact('transactions', 'yearAverage', 'overallAverage', 'year', 'hideBill', '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
*/
public function store(BillFormRequest $request, BillRepositoryInterface $repository)
{
2015-03-29 04:51:26 -05:00
$billData = $request->getBillData();
$bill = $repository->store($billData);
2017-10-19 11:08:50 -05:00
$request->session()->flash('success', strval(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
}
2017-11-15 05:25:49 -06:00
if (1 === intval($request->get('create_another'))) {
2017-03-18 05:02:02 -05:00
// @codeCoverageIgnoreStart
2017-02-17 13:14:22 -06:00
$request->session()->put('bills.create.fromStore', true);
2015-04-28 01:12:12 -05:00
2015-07-06 09:27:21 -05:00
return redirect(route('bills.create'))->withInput();
2017-03-18 05:02:02 -05:00
// @codeCoverageIgnoreEnd
2015-03-03 10:40:17 -06:00
}
2015-04-28 01:12:12 -05:00
// redirect to previous URL.
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('bills.create.uri'));
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
2017-10-19 11:08:50 -05:00
$request->session()->flash('success', strval(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
}
2017-11-15 05:25:49 -06:00
if (1 === intval($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
}
2017-12-02 00:10:36 -06:00
/**
* Returns the latest date in the set, or start when set is empty.
*
* @param Collection $dates
* @param Carbon $default
*
* @return Carbon
*/
private function lastPaidDate(Collection $dates, Carbon $default): Carbon
{
2017-12-22 11:32:43 -06:00
if (0 === $dates->count()) {
return $default; // @codeCoverageIgnore
2017-12-02 00:10:36 -06:00
}
$latest = $dates->first();
/** @var Carbon $date */
foreach ($dates as $date) {
if ($date->gte($latest)) {
$latest = $date;
}
}
return $latest;
}
2015-02-25 08:19:14 -06:00
}