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

251 lines
7.7 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
/**
* BillController.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
2016-05-20 08:57:45 +02:00
declare(strict_types = 1);
namespace FireflyIII\Http\Controllers;
2015-02-25 15:19:14 +01:00
use FireflyIII\Http\Requests\BillFormRequest;
use FireflyIII\Models\Bill;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2015-03-10 17:26:31 +01:00
use Input;
use Preferences;
2015-02-25 15:19:14 +01:00
use Session;
use URL;
use View;
/**
* Class BillController
*
* @package FireflyIII\Http\Controllers
*/
class BillController extends Controller
{
2015-04-02 22:39:31 +02:00
/**
2016-02-04 07:27:03 +01:00
*
2015-04-02 22:39:31 +02:00
*/
2015-02-25 15:19:14 +01:00
public function __construct()
{
2015-04-28 15:26:30 +02:00
parent::__construct();
2015-05-14 13:17:53 +02:00
View::share('title', trans('firefly.bills'));
2015-02-25 15:19:14 +01:00
View::share('mainTitleIcon', 'fa-calendar-o');
}
/**
* @return \Illuminate\View\View
2015-02-25 15:19:14 +01:00
*/
public function create()
{
2016-04-26 21:40:15 +02:00
$periods = config('firefly.periods_to_text');
2015-06-09 17:56:08 +02:00
$subTitle = trans('firefly.create_new_bill');
2015-02-25 15:19:14 +01:00
2015-04-28 08:12:12 +02:00
// put previous url in session if not redirect from store (not "create another").
2016-02-04 07:27:03 +01:00
if (session('bills.create.fromStore') !== true) {
2015-04-28 08:12:12 +02:00
Session::put('bills.create.url', URL::previous());
}
Session::forget('bills.create.fromStore');
2015-05-25 08:12:31 +02:00
Session::flash('gaEventCategory', 'bills');
Session::flash('gaEventAction', 'create');
2015-04-28 08:12:12 +02:00
2015-05-05 10:23:01 +02:00
return view('bills.create', compact('periods', 'subTitle'));
2015-02-25 15:19:14 +01:00
}
/**
* @param Bill $bill
*
* @return \Illuminate\View\View
2015-02-25 15:19:14 +01:00
*/
public function delete(Bill $bill)
{
2015-04-28 08:12:12 +02:00
// put previous url in session
Session::put('bills.delete.url', URL::previous());
2015-05-25 08:12:31 +02:00
Session::flash('gaEventCategory', 'bills');
Session::flash('gaEventAction', 'delete');
2015-05-25 22:16:00 +02:00
$subTitle = trans('firefly.delete_bill', ['name' => $bill->name]);
2015-04-28 08:12:12 +02:00
2015-05-05 10:23:01 +02:00
return view('bills.delete', compact('bill', 'subTitle'));
2015-02-25 15:19:14 +01:00
}
/**
2015-05-03 12:54:39 +02:00
* @param BillRepositoryInterface $repository
* @param Bill $bill
2015-02-25 15:19:14 +01:00
*
* @return \Illuminate\Http\RedirectResponse
*/
2015-05-03 12:54:39 +02:00
public function destroy(BillRepositoryInterface $repository, Bill $bill)
2015-02-25 15:19:14 +01:00
{
2016-03-20 11:38:01 +01:00
$name = $bill->name;
2015-04-05 18:20:06 +02:00
$repository->destroy($bill);
2016-03-20 11:38:01 +01:00
Session::flash('success', strval(trans('firefly.deleted_bill', ['name' => $name])));
Preferences::mark();
2015-02-25 15:19:14 +01:00
2016-02-04 07:27:03 +01:00
return redirect(session('bills.delete.url'));
2015-02-25 15:19:14 +01:00
}
/**
* @param Bill $bill
*
* @return \Illuminate\View\View
2015-02-25 15:19:14 +01:00
*/
public function edit(Bill $bill)
{
2016-04-26 21:40:15 +02:00
$periods = config('firefly.periods_to_text');
2015-06-09 17:56:08 +02:00
$subTitle = trans('firefly.edit_bill', ['name' => $bill->name]);
2015-02-25 15:19:14 +01:00
2015-04-28 08:12:12 +02:00
// put previous url in session if not redirect from store (not "return_to_edit").
2016-02-04 07:27:03 +01:00
if (session('bills.edit.fromUpdate') !== true) {
2015-04-28 08:12:12 +02:00
Session::put('bills.edit.url', URL::previous());
}
Session::forget('bills.edit.fromUpdate');
2015-05-25 08:12:31 +02:00
Session::flash('gaEventCategory', 'bills');
Session::flash('gaEventAction', 'edit');
2015-04-28 08:12:12 +02:00
2015-05-05 10:23:01 +02:00
return view('bills.edit', compact('subTitle', 'periods', 'bill'));
2015-02-25 15:19:14 +01:00
}
/**
* @param BillRepositoryInterface $repository
*
* @return \Illuminate\View\View
*/
public function index(BillRepositoryInterface $repository)
{
2016-03-29 15:55:14 +02:00
$start = session('start');
$end = session('end');
2015-04-05 18:20:06 +02:00
$bills = $repository->getBills();
2015-02-25 15:19:14 +01:00
$bills->each(
2016-03-29 15:55:14 +02:00
function (Bill $bill) use ($repository, $start, $end) {
2015-02-25 15:19:14 +01:00
$bill->nextExpectedMatch = $repository->nextExpectedMatch($bill);
2015-04-05 18:20:06 +02:00
$bill->lastFoundMatch = $repository->lastFoundMatch($bill);
2016-03-29 15:55:14 +02:00
$journals = $repository->getJournalsInRange($bill, $start, $end);
// loop journals, find average:
$average = '0';
$count = $journals->count();
if ($count > 0) {
$sum = '0';
foreach ($journals as $journal) {
$sum = bcadd($sum, TransactionJournal::amountPositive($journal));
}
$average = bcdiv($sum, strval($count));
}
$bill->lastPaidAmount = $average;
$bill->paidInPeriod = ($start <= $bill->lastFoundMatch) && ($end >= $bill->lastFoundMatch);
2015-02-25 15:19:14 +01:00
}
);
2015-02-27 11:09:23 +01:00
return view('bills.index', compact('bills'));
2015-02-25 15:19:14 +01:00
}
/**
2015-05-03 12:54:39 +02:00
* @param BillRepositoryInterface $repository
* @param Bill $bill
2015-02-25 15:19:14 +01:00
*
2015-05-03 12:54:39 +02:00
* @return \Illuminate\Http\RedirectResponse
2015-02-25 15:19:14 +01:00
*/
2015-05-03 12:54:39 +02:00
public function rescan(BillRepositoryInterface $repository, Bill $bill)
2015-02-25 15:19:14 +01:00
{
if (intval($bill->active) == 0) {
2016-03-20 11:38:01 +01:00
Session::flash('warning', strval(trans('firefly.cannot_scan_inactive_bill')));
2015-02-25 15:19:14 +01:00
2015-07-06 16:27:21 +02:00
return redirect(URL::previous());
2015-02-25 15:19:14 +01:00
}
2015-04-05 18:20:06 +02:00
$journals = $repository->getPossiblyRelatedJournals($bill);
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
$repository->scan($bill, $journal);
2015-02-25 15:19:14 +01:00
}
2015-04-05 18:20:06 +02:00
2016-03-20 11:38:01 +01:00
Session::flash('success', strval(trans('firefly.rescanned_bill')));
Preferences::mark();
2015-02-25 15:19:14 +01:00
2015-07-06 16:27:21 +02:00
return redirect(URL::previous());
2015-02-25 15:19:14 +01:00
}
/**
2015-05-03 12:54:39 +02:00
* @param BillRepositoryInterface $repository
* @param Bill $bill
2015-02-25 15:19:14 +01:00
*
* @return \Illuminate\View\View
2015-02-25 15:19:14 +01:00
*/
2015-05-03 12:54:39 +02:00
public function show(BillRepositoryInterface $repository, Bill $bill)
2015-02-25 15:19:14 +01:00
{
2016-04-25 18:43:09 +02:00
$page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page'));
$pageSize = Preferences::get('transactionPageSize', 50)->data;
$journals = $repository->getJournals($bill, $page, $pageSize);
2016-04-21 10:23:19 +02:00
$journals->setPath('/bills/show/' . $bill->id);
2015-02-25 15:19:14 +01:00
$bill->nextExpectedMatch = $repository->nextExpectedMatch($bill);
$hideBill = true;
2015-05-05 10:23:01 +02:00
$subTitle = e($bill->name);
2015-02-25 15:19:14 +01:00
2015-05-05 10:23:01 +02:00
return view('bills.show', compact('journals', 'hideBill', 'bill', 'subTitle'));
2015-02-25 15:19:14 +01:00
}
/**
2015-05-03 12:54:39 +02:00
* @param BillFormRequest $request
* @param BillRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse
2015-02-25 15:19:14 +01:00
*/
public function store(BillFormRequest $request, BillRepositoryInterface $repository)
{
2015-03-29 11:51:26 +02:00
$billData = $request->getBillData();
$bill = $repository->store($billData);
2016-03-20 11:38:01 +01:00
Session::flash('success', strval(trans('firefly.stored_new_bill', ['name' => e($bill->name)])));
Preferences::mark();
2015-02-25 15:19:14 +01:00
2015-03-03 17:40:17 +01:00
if (intval(Input::get('create_another')) === 1) {
2015-04-28 08:12:12 +02:00
// set value so create routine will not overwrite URL:
Session::put('bills.create.fromStore', true);
2015-07-06 16:27:21 +02:00
return redirect(route('bills.create'))->withInput();
2015-03-03 17:40:17 +01:00
}
2015-04-28 08:12:12 +02:00
// redirect to previous URL.
2016-02-04 07:27:03 +01:00
return redirect(session('bills.create.url'));
2015-02-25 15:19:14 +01:00
}
/**
2015-05-03 12:54:39 +02:00
* @param BillFormRequest $request
* @param BillRepositoryInterface $repository
* @param Bill $bill
2015-02-25 15:19:14 +01:00
*
* @return \Illuminate\Http\RedirectResponse
2015-02-25 15:19:14 +01:00
*/
2015-05-03 12:54:39 +02:00
public function update(BillFormRequest $request, BillRepositoryInterface $repository, Bill $bill)
2015-02-25 15:19:14 +01:00
{
2015-03-29 11:51:26 +02:00
$billData = $request->getBillData();
$bill = $repository->update($bill, $billData);
2015-02-25 15:19:14 +01:00
2016-03-20 11:38:01 +01:00
Session::flash('success', strval(trans('firefly.updated_bill', ['name' => e($bill->name)])));
Preferences::mark();
2015-02-25 15:19:14 +01:00
2015-04-28 08:12:12 +02: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-07-06 16:27:21 +02:00
return redirect(route('bills.edit', [$bill->id]))->withInput(['return_to_edit' => 1]);
2015-04-28 08:12:12 +02:00
}
// redirect to previous URL.
2016-02-04 07:27:03 +01:00
return redirect(session('bills.edit.url'));
2015-02-25 15:19:14 +01:00
}
}