firefly-iii/app/controllers/TransactionController.php

583 lines
21 KiB
PHP
Raw Normal View History

2014-07-14 23:58:08 -05:00
<?php
use FireflyIII\Exception\FireflyException;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
2014-07-14 23:58:08 -05:00
2014-07-15 15:16:29 -05:00
/**
* Class TransactionController
*
2014-07-15 15:16:29 -05:00
*/
class TransactionController extends BaseController
{
2014-07-14 23:58:08 -05:00
2014-07-15 15:16:29 -05:00
/**
* Construct a new transaction controller with two of the most often used helpers.
*
2014-07-15 15:16:29 -05:00
*/
public function __construct()
{
View::share('title', 'Transactions');
View::share('mainTitleIcon', 'fa-repeat');
2014-07-14 23:58:08 -05:00
}
2014-12-06 10:53:25 -06:00
/**
*
* TODO this needs cleaning up and thinking over.
*
* @param TransactionJournal $journal
*
* @return array|\Illuminate\Http\JsonResponse
*/
2014-12-04 13:38:45 -06:00
public function alreadyRelated(TransactionJournal $journal)
{
$ids = [];
/** @var TransactionGroup $group */
foreach ($journal->transactiongroups()->get() as $group) {
/** @var TransactionJournal $jrnl */
foreach ($group->transactionjournals()->get() as $jrnl) {
if ($jrnl->id != $journal->id) {
$ids[] = $jrnl->id;
}
}
}
$unique = array_unique($ids);
2014-12-13 14:59:02 -06:00
if (count($unique) > 0) {
2014-12-04 13:38:45 -06:00
2014-12-13 15:54:52 -06:00
/** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $repository */
$repository = App::make('FireflyIII\Database\TransactionJournal\TransactionJournal');
2014-12-13 14:59:02 -06:00
$set = $repository->getByIds($unique);
2014-12-04 13:38:45 -06:00
$set->each(
function (TransactionJournal $journal) {
$journal->amount = mf($journal->getAmount());
}
);
return Response::json($set->toArray());
} else {
return (new Collection)->toArray();
}
}
/**
* Shows the view helping the user to create a new transaction journal.
*
* @param string $what
*
* @return \Illuminate\View\View
*/
public function create($what = 'deposit')
{
2014-12-13 15:54:52 -06:00
/** @var \FireflyIII\Database\Account\Account $accountRepository */
$accountRepository = App::make('FireflyIII\Database\Account\Account');
2014-12-13 15:54:52 -06:00
/** @var \FireflyIII\Database\Budget\Budget $budgetRepository */
$budgetRepository = App::make('FireflyIII\Database\Budget\Budget');
2014-12-13 15:54:52 -06:00
/** @var \FireflyIII\Database\PiggyBank\PiggyBank $piggyRepository */
$piggyRepository = App::make('FireflyIII\Database\PiggyBank\PiggyBank');
2014-12-13 15:54:52 -06:00
/** @var \FireflyIII\Database\PiggyBank\RepeatedExpense $repRepository */
$repRepository = App::make('FireflyIII\Database\PiggyBank\RepeatedExpense');
// get asset accounts with names and id's .
2014-11-21 04:12:22 -06:00
$assetAccounts = FFForm::makeSelectList($accountRepository->getAssetAccounts());
// get budgets as a select list.
2014-11-21 04:12:22 -06:00
$budgets = FFForm::makeSelectList($budgetRepository->get());
$budgets[0] = '(no budget)';
// get the piggy banks.
$list = $piggyRepository->get()->merge($repRepository->get());
$piggies = FFForm::makeSelectList($list);
$piggies[0] = '(no piggy bank)';
2014-11-26 14:11:13 -06:00
asort($piggies);
2014-12-07 08:37:53 -06:00
$preFilled = Session::has('preFilled') ? Session::get('preFilled') : [];
2014-10-08 14:04:31 -05:00
$respondTo = ['account_id', 'account_from_id'];
foreach ($respondTo as $r) {
if (!is_null(Input::get($r))) {
2014-12-07 08:37:53 -06:00
$preFilled[$r] = Input::get($r);
2014-10-08 14:04:31 -05:00
}
}
2014-12-07 08:37:53 -06:00
Session::put('preFilled', $preFilled);
2014-10-08 14:04:31 -05:00
2014-11-12 08:34:32 -06:00
return View::make('transactions.create')->with('accounts', $assetAccounts)->with('budgets', $budgets)->with('what', $what)->with('piggies', $piggies)
->with('subTitle', 'Add a new ' . $what);
}
2014-08-10 08:01:46 -05:00
/**
* Shows the form that allows a user to delete a transaction journal.
*
2014-08-10 08:01:46 -05:00
* @param TransactionJournal $transactionJournal
*
* @return $this
*/
public function delete(TransactionJournal $transactionJournal)
{
$type = strtolower($transactionJournal->transactionType->type);
return View::make('transactions.delete')->with('journal', $transactionJournal)->with(
'subTitle', 'Delete ' . $type . ' "' . $transactionJournal->description . '"'
);
}
2014-08-10 08:01:46 -05:00
/**
* @param TransactionJournal $transactionJournal
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(TransactionJournal $transactionJournal)
{
$type = $transactionJournal->transactionType->type;
2014-12-13 15:54:52 -06:00
/** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $repository */
$repository = App::make('FireflyIII\Database\TransactionJournal\TransactionJournal');
$repository->destroy($transactionJournal);
2014-12-06 10:34:39 -06:00
$return = 'withdrawal';
switch ($type) {
case 'Deposit':
2014-12-06 10:34:39 -06:00
$return = 'deposit';
break;
case 'Transfer':
2014-12-06 10:34:39 -06:00
$return = 'transfers';
break;
}
2014-12-06 10:34:39 -06:00
return Redirect::route('transactions.index', $return);
}
2014-12-06 10:53:25 -06:00
/**
* TODO this needs cleaning up and thinking over.
*
* @return \Illuminate\Http\JsonResponse
*/
2014-12-04 13:38:45 -06:00
public function doRelate()
{
$id = intval(Input::get('id'));
$sister = intval(Input::get('relateTo'));
2014-12-13 15:54:52 -06:00
/** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $repository */
$repository = App::make('FireflyIII\Database\TransactionJournal\TransactionJournal');
2014-12-04 13:38:45 -06:00
$journal = $repository->find($id);
$sis = $repository->find($sister);
if ($journal && $sis) {
$group = new TransactionGroup;
$group->relation = 'balance';
$group->user_id = $repository->getUser()->id;
$group->save();
$group->transactionjournals()->save($journal);
$group->transactionjournals()->save($sis);
return Response::json(true);
}
return Response::json(false);
}
/**
* Shows the view to edit a transaction.
*
2014-08-10 08:01:46 -05:00
* @param TransactionJournal $journal
*
2014-08-10 08:01:46 -05:00
* @return $this
*/
public function edit(TransactionJournal $journal)
{
/*
* All the repositories we need:
*/
2014-12-13 15:54:52 -06:00
/** @var \FireflyIII\Database\Account\Account $accountRepository */
$accountRepository = App::make('FireflyIII\Database\Account\Account');
2014-12-13 15:54:52 -06:00
/** @var \FireflyIII\Database\Budget\Budget $budgetRepository */
$budgetRepository = App::make('FireflyIII\Database\Budget\Budget');
2014-12-13 15:54:52 -06:00
/** @var \FireflyIII\Database\PiggyBank\PiggyBank $piggyRepository */
$piggyRepository = App::make('FireflyIII\Database\PiggyBank\PiggyBank');
// type is useful for display:
$what = strtolower($journal->transactiontype->type);
// get asset accounts with names and id's.
2014-12-15 11:16:48 -06:00
2014-12-14 13:40:02 -06:00
$budgets = FFForm::makeSelectList($budgetRepository->get(), true);
2014-12-15 11:16:48 -06:00
$accounts = FFForm::makeSelectList($accountRepository->getAssetAccounts());
2014-12-14 13:40:02 -06:00
$piggies = FFForm::makeSelectList($piggyRepository->get(), true);
2014-08-19 06:24:11 -05:00
2014-12-15 11:16:48 -06:00
/*
* Data to properly display the edit form.
*/
2014-12-07 08:37:53 -06:00
$preFilled = [
'date' => $journal->date->format('Y-m-d'),
'category' => '',
'budget_id' => 0,
2014-12-14 13:40:02 -06:00
'piggybank_id' => 0
];
/*
* Fill in the category.
*/
$category = $journal->categories()->first();
if (!is_null($category)) {
2014-12-07 08:37:53 -06:00
$preFilled['category'] = $category->name;
}
/*
* Switch on the type of transaction edited by the user and fill in other
* relevant fields:
*/
switch ($what) {
2014-09-21 08:40:41 -05:00
case 'withdrawal':
if (floatval($journal->transactions[0]->amount) < 0) {
// transactions[0] is the asset account that paid for the withdrawal.
2014-12-07 08:37:53 -06:00
$preFilled['account_id'] = $journal->transactions[0]->account->id;
$preFilled['expense_account'] = $journal->transactions[1]->account->name;
$preFilled['amount'] = floatval($journal->transactions[1]->amount);
} else {
// transactions[1] is the asset account that paid for the withdrawal.
2014-12-07 08:37:53 -06:00
$preFilled['account_id'] = $journal->transactions[1]->account->id;
$preFilled['expense_account'] = $journal->transactions[0]->account->name;
$preFilled['amount'] = floatval($journal->transactions[0]->amount);
}
$budget = $journal->budgets()->first();
if (!is_null($budget)) {
2014-12-07 08:37:53 -06:00
$preFilled['budget_id'] = $budget->id;
}
break;
2014-09-21 08:40:41 -05:00
case 'deposit':
if (floatval($journal->transactions[0]->amount) < 0) {
// transactions[0] contains the account the money came from.
2014-12-07 08:37:53 -06:00
$preFilled['account_id'] = $journal->transactions[1]->account->id;
$preFilled['revenue_account'] = $journal->transactions[0]->account->name;
$preFilled['amount'] = floatval($journal->transactions[1]->amount);
} else {
// transactions[1] contains the account the money came from.
2014-12-07 08:37:53 -06:00
$preFilled['account_id'] = $journal->transactions[0]->account->id;
$preFilled['revenue_account'] = $journal->transactions[1]->account->name;
$preFilled['amount'] = floatval($journal->transactions[0]->amount);
}
break;
2014-09-21 08:40:41 -05:00
case 'transfer':
if (floatval($journal->transactions[0]->amount) < 0) {
// zero = from account.
2014-12-07 08:37:53 -06:00
$preFilled['account_from_id'] = $journal->transactions[0]->account->id;
$preFilled['account_to_id'] = $journal->transactions[1]->account->id;
$preFilled['amount'] = floatval($journal->transactions[1]->amount);
} else {
// one = from account
2014-12-07 08:37:53 -06:00
$preFilled['account_from_id'] = $journal->transactions[1]->account->id;
$preFilled['account_to_id'] = $journal->transactions[0]->account->id;
$preFilled['amount'] = floatval($journal->transactions[0]->amount);
}
if ($journal->piggybankevents()->count() > 0) {
2014-12-07 08:37:53 -06:00
$preFilled['piggybank_id'] = $journal->piggybankevents()->first()->piggybank_id;
}
break;
}
/*
* Show the view.
*/
2014-11-12 15:36:02 -06:00
return View::make('transactions.edit')->with('journal', $journal)->with('accounts', $accounts)->with(
'what', $what
2014-12-07 08:37:53 -06:00
)->with('budgets', $budgets)->with('data', $preFilled)->with('piggies', $piggies)->with(
'subTitle', 'Edit ' . $what . ' "' . $journal->description . '"'
);
}
2014-11-12 15:36:02 -06:00
/**
* @param $what
*
* @return $this
*/
public function index($what)
{
2014-12-13 15:54:52 -06:00
/** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $repository */
$repository = App::make('FireflyIII\Database\TransactionJournal\TransactionJournal');
2014-11-14 03:28:18 -06:00
2014-11-12 15:36:02 -06:00
switch ($what) {
case 'expenses':
case 'withdrawal':
$subTitleIcon = 'fa-long-arrow-left';
$subTitle = 'Expenses';
2014-11-14 03:28:18 -06:00
$journals = $repository->getWithdrawalsPaginated(50);
2014-11-12 15:36:02 -06:00
break;
case 'revenue':
case 'deposit':
$subTitleIcon = 'fa-long-arrow-right';
$subTitle = 'Revenue, income and deposits';
2014-11-14 03:28:18 -06:00
$journals = $repository->getDepositsPaginated(50);
2014-11-12 15:36:02 -06:00
break;
case 'transfer':
case 'transfers':
$subTitleIcon = 'fa-arrows-h';
$subTitle = 'Transfers';
2014-11-14 03:28:18 -06:00
$journals = $repository->getTransfersPaginated(50);
2014-11-12 15:36:02 -06:00
break;
}
return View::make('transactions.index', compact('subTitle', 'what', 'subTitleIcon', 'journals'));
2014-11-12 15:36:02 -06:00
}
2014-12-06 10:53:25 -06:00
/**
* @param TransactionJournal $journal
*
* @return \Illuminate\View\View
*/
public function relate(TransactionJournal $journal)
{
$groups = $journal->transactiongroups()->get();
$members = new Collection;
/** @var TransactionGroup $group */
foreach ($groups as $group) {
/** @var TransactionJournal $jrnl */
foreach ($group->transactionjournals()->get() as $jrnl) {
if ($jrnl->id != $journal->id) {
$members->push($jrnl);
}
}
}
return View::make('transactions.relate', compact('journal', 'members'));
}
2014-12-06 10:53:25 -06:00
/**
* TODO this needs cleaning up and thinking over.
*
* @param TransactionJournal $journal
*
* @return \Illuminate\Http\JsonResponse
*/
2014-12-04 13:38:45 -06:00
public function relatedSearch(TransactionJournal $journal)
{
$search = e(trim(Input::get('searchValue')));
2014-12-13 15:54:52 -06:00
/** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $repository */
$repository = App::make('FireflyIII\Database\TransactionJournal\TransactionJournal');
2014-12-04 13:38:45 -06:00
$result = $repository->searchRelated($search, $journal);
$result->each(
function (TransactionJournal $j) {
$j->amount = mf($j->getAmount());
}
);
return Response::json($result->toArray());
}
/**
2014-08-10 08:01:46 -05:00
* @param TransactionJournal $journal
*
2014-08-10 08:01:46 -05:00
* @return $this
*/
2014-11-14 03:28:18 -06:00
public function show(TransactionJournal $journal)
{
2014-11-22 00:30:46 -06:00
$journal->transactions->each(
function (\Transaction $t) use ($journal) {
2014-11-22 00:30:46 -06:00
$t->before = floatval(
$t->account->transactions()->leftJoin(
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
)->where('transaction_journals.date', '<=', $journal->date->format('Y-m-d'))->where(
'transaction_journals.created_at', '<=', $journal->created_at->format('Y-m-d H:i:s')
)->where('transaction_journals.id', '!=', $journal->id)->sum('transactions.amount')
);
$t->after = $t->before + $t->amount;
2014-11-22 00:30:46 -06:00
}
);
$members = new Collection;
/** @var TransactionGroup $group */
2014-12-04 13:38:45 -06:00
foreach ($journal->transactiongroups()->get() as $group) {
/** @var TransactionJournal $jrnl */
2014-12-04 13:38:45 -06:00
foreach ($group->transactionjournals()->get() as $jrnl) {
if ($jrnl->id != $journal->id) {
$members->push($jrnl);
}
}
}
2014-11-22 00:30:46 -06:00
return View::make('transactions.show', compact('journal', 'members'))->with(
2014-09-21 08:40:41 -05:00
'subTitle', $journal->transactionType->type . ' "' . $journal->description . '"'
);
}
/**
* @param $what
*
* @return $this|\Illuminate\Http\RedirectResponse
* @throws FireflyException
*/
2014-11-12 08:34:32 -06:00
public function store($what)
{
2014-11-12 15:36:02 -06:00
$data = Input::except('_token');
$data['what'] = $what;
2014-11-12 08:34:32 -06:00
$data['currency'] = 'EUR';
2014-12-13 15:54:52 -06:00
/** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $repository */
$repository = App::make('FireflyIII\Database\TransactionJournal\TransactionJournal');
2014-11-12 08:34:32 -06:00
switch ($data['post_submit_action']) {
default:
throw new FireflyException('Cannot handle post_submit_action "' . e($data['post_submit_action']) . '"');
break;
case 'create_another':
case 'store':
$messages = $repository->validate($data);
/** @var MessageBag $messages ['errors'] */
if ($messages['errors']->count() > 0) {
Session::flash('warnings', $messages['warnings']);
Session::flash('successes', $messages['successes']);
Session::flash('error', 'Could not save transaction: ' . $messages['errors']->first());
2014-11-12 15:36:02 -06:00
2014-11-12 08:34:32 -06:00
return Redirect::route('transactions.create', $what)->withInput()->withErrors($messages['errors']);
}
// store!
$journal = $repository->store($data);
2014-11-12 08:34:32 -06:00
Session::flash('success', 'New transaction stored!');
/*
* Trigger a search for the related (if selected)
* piggy bank and store an event.
*/
2014-12-14 13:40:02 -06:00
Event::fire('transactionJournal.store', [$journal, Input::get('piggybank_id')]); // new and used.
/*
* Also trigger on both transactions.
*/
/** @var Transaction $transaction */
foreach ($journal->transactions as $transaction) {
Event::fire('transaction.store', [$transaction]);
}
2014-11-12 08:34:32 -06:00
if ($data['post_submit_action'] == 'create_another') {
return Redirect::route('transactions.create', $what)->withInput();
2014-11-12 08:34:32 -06:00
} else {
2014-11-12 15:36:02 -06:00
return Redirect::route('transactions.index', $what);
2014-11-12 08:34:32 -06:00
}
break;
case 'validate_only':
$messageBags = $repository->validate($data);
Session::flash('warnings', $messageBags['warnings']);
Session::flash('successes', $messageBags['successes']);
Session::flash('errors', $messageBags['errors']);
2014-11-12 15:36:02 -06:00
return Redirect::route('transactions.create', $what)->withInput();
2014-11-12 08:34:32 -06:00
break;
}
}
2014-12-06 10:53:25 -06:00
/**
* TODO this needs cleaning up and thinking over.
*
* @param TransactionJournal $journal
*
* @return \Illuminate\Http\JsonResponse
* @throws Exception
*/
2014-12-05 14:39:16 -06:00
public function unrelate(TransactionJournal $journal)
{
$groups = $journal->transactiongroups()->get();
$relatedTo = intval(Input::get('relation'));
/** @var TransactionGroup $group */
foreach ($groups as $group) {
foreach ($group->transactionjournals()->get() as $jrnl) {
if ($jrnl->id == $relatedTo) {
// remove from group:
$group->transactionjournals()->detach($relatedTo);
}
}
if ($group->transactionjournals()->count() == 1) {
$group->delete();
}
}
2014-12-06 10:34:39 -06:00
2014-12-05 14:39:16 -06:00
return Response::json(true);
}
2014-09-28 02:20:25 -05:00
2014-08-10 08:01:46 -05:00
/**
* @param TransactionJournal $journal
*
2014-12-13 14:59:02 -06:00
* @return $this
2014-09-21 09:22:18 -05:00
* @throws FireflyException
2014-08-10 08:01:46 -05:00
*/
2014-11-12 08:34:32 -06:00
public function update(TransactionJournal $journal)
{
2014-12-13 15:54:52 -06:00
/** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $repos */
$repos = App::make('FireflyIII\Database\TransactionJournal\TransactionJournal');
2014-11-13 10:01:09 -06:00
$data = Input::except('_token');
$data['currency'] = 'EUR';
$data['what'] = strtolower($journal->transactionType->type);
switch (Input::get('post_submit_action')) {
case 'update':
case 'return_to_edit':
$messageBag = $repos->update($journal, $data);
if ($messageBag->count() == 0) {
// has been saved, return to index:
Session::flash('success', 'Transaction updated!');
2014-11-18 02:41:43 -06:00
Event::fire('transactionJournal.update', [$journal]); // new and used.
2014-11-13 10:01:09 -06:00
/*
* Also trigger on both transactions.
*/
/** @var Transaction $transaction */
foreach ($journal->transactions as $transaction) {
Event::fire('transaction.update', [$transaction]);
}
2014-11-13 10:01:09 -06:00
if (Input::get('post_submit_action') == 'return_to_edit') {
return Redirect::route('transactions.edit', $journal->id)->withInput();
} else {
return Redirect::route('transactions.index', $data['what']);
}
} else {
2014-12-06 05:12:55 -06:00
Session::flash('error', 'Could not update transaction: ' . $journal->getErrors()->first());
2014-11-13 10:01:09 -06:00
return Redirect::route('transactions.edit', $journal->id)->withInput()->withErrors(
2014-12-06 05:12:55 -06:00
$journal->getErrors()
2014-11-13 10:01:09 -06:00
);
}
break;
case 'validate_only':
$messageBags = $repos->validate($data);
Session::flash('warnings', $messageBags['warnings']);
Session::flash('successes', $messageBags['successes']);
Session::flash('errors', $messageBags['errors']);
return Redirect::route('transactions.edit', $journal->id)->withInput();
break;
default:
throw new FireflyException('Method ' . Input::get('post_submit_action') . ' not implemented yet.');
break;
}
}
}