First code for #159 popups

This commit is contained in:
James Cole
2016-04-01 16:06:55 +02:00
parent fa38c975b6
commit 144a6130f2
11 changed files with 295 additions and 208 deletions

View File

@@ -0,0 +1,106 @@
<?php
/**
* ReportController.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Http\Controllers\Popup;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Support\Binder\AccountList;
use Illuminate\Http\Request;
use InvalidArgumentException;
use Response;
/**
* Class ReportController
*
* @package FireflyIII\Http\Controllers\Popup
*/
class ReportController extends Controller
{
/**
* @param Request $request
*
* @throws FireflyException
*/
public function info(Request $request)
{
$attributes = $request->get('attributes');
$attributes = $this->parseAttributes($attributes);
$html = '';
switch ($attributes['location']) {
default:
throw new FireflyException('Firefly cannot handle "' . e($attributes['location']) . '" ');
case 'budget-spent-amount':
$html = $this->budgetSpentAmount($attributes);
break;
}
return Response::json(['html' => $html]);
}
/**
* @param array $attributes
*
* @return string
* @throws FireflyException
*/
private function budgetSpentAmount(array $attributes): string
{
// need to find the budget
// then search for expenses in the given period
// list them in some table format.
/** @var BudgetRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
$budget = $repository->find(intval($attributes['budgetId']));
if (is_null($budget->id)) {
throw new FireflyException('Could not find a budget for value "' . e($attributes['budgetId']) . '".');
}
// get all expenses in budget in period:
$journals = $repository->getExpenses($budget, $attributes['accounts'], $attributes['startDate'], $attributes['endDate']);
$view = view('popup.report.budget-spent-amount', compact('journals'))->render();
return $view;
}
/**
* @param array $attributes
*
* @return array
* @throws FireflyException
*/
private function parseAttributes(array $attributes): array
{
$attributes['location'] = $attributes['location'] ?? '';
$attributes['accounts'] = AccountList::routeBinder($attributes['accounts'] ?? '', '');
try {
$attributes['startDate'] = Carbon::createFromFormat('Ymd', $attributes['startDate']);
} catch (InvalidArgumentException $e) {
throw new FireflyException('Could not parse start date "' . e($attributes['startDate']) . '".');
}
try {
$attributes['endDate'] = Carbon::createFromFormat('Ymd', $attributes['endDate']);
} catch (InvalidArgumentException $e) {
throw new FireflyException('Could not parse start date "' . e($attributes['endDate']) . '".');
}
return $attributes;
}
}

View File

@@ -378,6 +378,14 @@ Route::group(
Route::post('/transaction/destroy/{tj}', ['uses' => 'TransactionController@destroy', 'as' => 'transactions.destroy']);
Route::post('/transaction/reorder', ['uses' => 'TransactionController@reorder', 'as' => 'transactions.reorder']);
/**
* POPUP Controllers
*/
/**
* Report popup
*/
Route::get('/popup/report', ['uses' => 'Popup\ReportController@info', 'as' => 'popup.report']);
}
);

View File

@@ -372,6 +372,30 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
return $data;
}
/**
* Returns all expenses for the given budget and the given accounts, in the given period.
*
* @param Budget $budget
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getExpenses(Budget $budget, Collection $accounts, Carbon $start, Carbon $end):Collection
{
$ids = $accounts->pluck('id')->toArray();
$set = $budget->transactionjournals()
->before($end)
->after($start)
->expanded()
->where('transaction_types.type', 'Withdrawal')
->whereIn('source_account.id', $ids)
->get(TransactionJournal::QUERYFIELDS);
return $set;
}
/**
* Returns the expenses for this budget grouped per day, with the date
* in "date" (a string, not a Carbon) and the amount in "dailyAmount".

View File

@@ -132,6 +132,18 @@ interface BudgetRepositoryInterface
*/
public function getCurrentRepetition(Budget $budget, Carbon $start, Carbon $end);
/**
* Returns all expenses for the given budget and the given accounts, in the given period.
*
* @param Budget $budget
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getExpenses(Budget $budget, Collection $accounts, Carbon $start, Carbon $end):Collection;
/**
* Returns the expenses for this budget grouped per day, with the date
* in "date" (a string, not a Carbon) and the amount in "dailyAmount".