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

276 lines
8.6 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* BillController.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2016-05-20 01:57:45 -05:00
declare(strict_types = 1);
namespace FireflyIII\Http\Controllers;
2015-02-25 08:19:14 -06:00
2016-07-05 01:57:45 -05:00
use Carbon\Carbon;
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;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2016-12-28 06:02:56 -06:00
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Preferences;
2015-02-25 08:19:14 -06:00
use Session;
use URL;
use View;
/**
* Class BillController
*
* @package FireflyIII\Http\Controllers
*/
class BillController extends Controller
{
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
$this->middleware(
function ($request, $next) {
View::share('title', trans('firefly.bills'));
View::share('mainTitleIcon', 'fa-calendar-o');
return $next($request);
}
);
2015-02-25 08:19:14 -06:00
}
/**
* @return View
2015-02-25 08:19:14 -06:00
*/
public function create()
{
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-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 "create another").
2016-02-04 00:27:03 -06:00
if (session('bills.create.fromStore') !== true) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('bills.create.uri');
2015-04-28 01:12:12 -05:00
}
Session::forget('bills.create.fromStore');
2015-05-25 01:12:31 -05:00
Session::flash('gaEventCategory', 'bills');
Session::flash('gaEventAction', 'create');
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
}
/**
* @param Bill $bill
*
* @return View
2015-02-25 08:19:14 -06:00
*/
public function delete(Bill $bill)
{
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 01:12:31 -05:00
Session::flash('gaEventCategory', 'bills');
Session::flash('gaEventAction', 'delete');
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
}
/**
2015-05-03 05:54:39 -05:00
* @param BillRepositoryInterface $repository
* @param Bill $bill
2015-02-25 08:19:14 -06:00
*
* @return \Illuminate\Http\RedirectResponse
*/
2015-05-03 05:54:39 -05:00
public function destroy(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);
2016-03-20 05:38:01 -05:00
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
}
/**
* @param Bill $bill
*
* @return View
2015-02-25 08:19:14 -06:00
*/
public function edit(Bill $bill)
{
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").
2016-02-04 00:27:03 -06:00
if (session('bills.edit.fromUpdate') !== true) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('bills.edit.uri');
2015-04-28 01:12:12 -05:00
}
Session::forget('bills.edit.fromUpdate');
2015-05-25 01:12:31 -05:00
Session::flash('gaEventCategory', 'bills');
Session::flash('gaEventAction', 'edit');
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
*/
public function index(BillRepositoryInterface $repository)
{
/** @var Carbon $start */
2016-03-29 08:55:14 -05:00
$start = session('start');
/** @var Carbon $end */
2016-09-16 05:07:45 -05:00
$end = session('end');
2016-03-29 08:55:14 -05:00
2015-04-05 11:20:06 -05:00
$bills = $repository->getBills();
2015-02-25 08:19:14 -06:00
$bills->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);
2016-10-23 07:56:05 -05:00
$lastDate = clone $start;
if ($bill->paidDates->count() >= $bill->payDates->count()) {
$lastDate = $end;
}
$bill->nextExpectedMatch = $repository->nextExpectedMatch($bill, $lastDate);
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
}
/**
2015-05-03 05:54:39 -05:00
* @param BillRepositoryInterface $repository
* @param Bill $bill
2015-02-25 08:19:14 -06:00
*
2015-05-03 05:54:39 -05:00
* @return \Illuminate\Http\RedirectResponse
2015-02-25 08:19:14 -06:00
*/
2015-05-03 05:54:39 -05:00
public function rescan(BillRepositoryInterface $repository, Bill $bill)
2015-02-25 08:19:14 -06:00
{
if (intval($bill->active) == 0) {
2016-03-20 05:38:01 -05:00
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
}
2015-04-05 11:20:06 -05:00
2016-03-20 05:38:01 -05:00
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 */
$date = session('start');
$year = $date->year;
2016-12-28 06:02:56 -06:00
$page = intval($request->get('page')) == 0 ? 1 : intval($request->get('page'));
$pageSize = intval(Preferences::get('transactionPageSize', 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();
2016-11-05 05:47:21 -05:00
$journals = $collector->getPaginatedJournals();
2016-04-21 03:23:19 -05:00
$journals->setPath('/bills/show/' . $bill->id);
2016-10-20 14:40:45 -05:00
$bill->nextExpectedMatch = $repository->nextExpectedMatch($bill, new Carbon);
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
2016-07-05 01:57:45 -05:00
return view('bills.show', compact('journals', '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);
2016-03-20 05:38:01 -05:00
Session::flash('success', strval(trans('firefly.stored_new_bill', ['name' => e($bill->name)])));
Preferences::mark();
2015-02-25 08:19:14 -06:00
2016-12-28 06:02:56 -06:00
if (intval($request->get('create_another')) === 1) {
2015-04-28 01:12:12 -05:00
// set value so create routine will not overwrite URL:
Session::put('bills.create.fromStore', true);
2015-07-06 09:27:21 -05:00
return redirect(route('bills.create'))->withInput();
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
2016-03-20 05:38:01 -05:00
Session::flash('success', strval(trans('firefly.updated_bill', ['name' => e($bill->name)])));
Preferences::mark();
2015-02-25 08:19:14 -06:00
2016-12-28 06:02:56 -06:00
if (intval($request->get('return_to_edit')) === 1) {
2015-04-28 01:12:12 -05:00
// set value so edit routine will not overwrite URL:
Session::put('bills.edit.fromUpdate', true);
2015-07-06 09:27:21 -05:00
return redirect(route('bills.edit', [$bill->id]))->withInput(['return_to_edit' => 1]);
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
}
}