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

221 lines
6.5 KiB
PHP
Raw Normal View History

2015-02-25 08:19:14 -06:00
<?php namespace FireflyIII\Http\Controllers;
2015-04-05 11:20:06 -05:00
use Config;
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;
2015-03-10 11:26:31 -05:00
use Input;
use Preferences;
2015-02-25 08:19:14 -06:00
use Redirect;
use Session;
use URL;
use View;
/**
* Class BillController
*
* @package FireflyIII\Http\Controllers
*/
class BillController extends Controller
{
2015-04-02 15:39:31 -05:00
/**
2015-05-23 13:49:57 -05:00
* @codeCoverageIgnore
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();
2015-05-14 06:17:53 -05:00
View::share('title', trans('firefly.bills'));
2015-02-25 08:19:14 -06:00
View::share('mainTitleIcon', 'fa-calendar-o');
}
/**
* @return \Illuminate\View\View
2015-02-25 08:19:14 -06:00
*/
public function create()
{
2015-06-09 10:56:08 -05:00
$periods = Config::get('firefly.periods_to_text');
$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").
if (Session::get('bills.create.fromStore') !== true) {
Session::put('bills.create.url', URL::previous());
}
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 \Illuminate\View\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
Session::put('bills.delete.url', URL::previous());
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
{
2015-04-05 11:20:06 -05:00
$repository->destroy($bill);
2015-02-25 08:19:14 -06:00
Session::flash('success', 'The bill was deleted.');
Preferences::mark();
2015-02-25 08:19:14 -06:00
2015-04-28 01:12:12 -05:00
return Redirect::to(Session::get('bills.delete.url'));
2015-02-25 08:19:14 -06:00
}
/**
* @param Bill $bill
*
* @return \Illuminate\View\View
2015-02-25 08:19:14 -06:00
*/
public function edit(Bill $bill)
{
2015-05-05 03:23:01 -05:00
$periods = Config::get('firefly.periods_to_text');
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").
if (Session::get('bills.edit.fromUpdate') !== true) {
Session::put('bills.edit.url', URL::previous());
}
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 \Illuminate\View\View
*/
public function index(BillRepositoryInterface $repository)
{
2015-04-05 11:20:06 -05:00
$bills = $repository->getBills();
2015-02-25 08:19:14 -06:00
$bills->each(
2015-06-06 16:09:12 -05:00
function (Bill $bill) use ($repository) {
2015-02-25 08:19:14 -06:00
$bill->nextExpectedMatch = $repository->nextExpectedMatch($bill);
2015-04-05 11:20:06 -05:00
$bill->lastFoundMatch = $repository->lastFoundMatch($bill);
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) {
Session::flash('warning', 'Inactive bills cannot be scanned.');
2015-04-05 11:20:06 -05:00
return Redirect::to(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
2015-02-25 08:19:14 -06:00
Session::flash('success', 'Rescanned everything.');
Preferences::mark();
2015-02-25 08:19:14 -06:00
return Redirect::to(URL::previous());
}
/**
2015-05-03 05:54:39 -05:00
* @param BillRepositoryInterface $repository
* @param Bill $bill
2015-02-25 08:19:14 -06:00
*
* @return \Illuminate\View\View
2015-02-25 08:19:14 -06:00
*/
2015-05-03 05:54:39 -05:00
public function show(BillRepositoryInterface $repository, Bill $bill)
2015-02-25 08:19:14 -06:00
{
2015-04-07 11:26:14 -05:00
$journals = $repository->getJournals($bill);
2015-02-25 08:19:14 -06:00
$bill->nextExpectedMatch = $repository->nextExpectedMatch($bill);
$hideBill = true;
2015-05-05 03:23:01 -05:00
$subTitle = e($bill->name);
2015-02-25 08:19:14 -06:00
2015-05-05 03:23:01 -05:00
return view('bills.show', compact('journals', '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);
2015-02-25 08:19:14 -06:00
Session::flash('success', 'Bill "' . e($bill->name) . '" stored.');
Preferences::mark();
2015-02-25 08:19:14 -06:00
2015-03-03 10:40:17 -06:00
if (intval(Input::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-03-03 10:40:17 -06:00
return Redirect::route('bills.create')->withInput();
}
2015-04-28 01:12:12 -05:00
// redirect to previous URL.
return Redirect::to(Session::get('bills.create.url'));
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
Session::flash('success', 'Bill "' . e($bill->name) . '" updated.');
Preferences::mark();
2015-02-25 08:19:14 -06:00
2015-04-28 01:12:12 -05:00
if (intval(Input::get('return_to_edit')) === 1) {
// set value so edit routine will not overwrite URL:
Session::put('bills.edit.fromUpdate', true);
2015-05-26 13:37:01 -05:00
return Redirect::route('bills.edit', [$bill->id])->withInput(['return_to_edit' => 1]);
2015-04-28 01:12:12 -05:00
}
// redirect to previous URL.
return Redirect::to(Session::get('bills.edit.url'));
2015-02-25 08:19:14 -06:00
}
}