2014-07-15 06:58:08 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
2014-12-30 21:03:42 +01:00
|
|
|
use FireflyIII\Database\TransactionJournal\TransactionJournal as Repository;
|
2014-11-12 10:54:53 +01:00
|
|
|
use FireflyIII\Exception\FireflyException;
|
2014-12-30 21:03:42 +01:00
|
|
|
use FireflyIII\Helper\TransactionJournal\HelperInterface as Helper;
|
2014-11-30 14:52:17 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2014-07-15 06:58:08 +02:00
|
|
|
|
2014-07-15 22:16:29 +02:00
|
|
|
/**
|
2014-12-31 16:17:43 +01:00
|
|
|
*
|
|
|
|
* @SuppressWarnings("CamelCase") // I'm fine with this.
|
|
|
|
*
|
2014-07-15 22:16:29 +02:00
|
|
|
* Class TransactionController
|
2014-09-09 20:00:04 +02:00
|
|
|
*
|
2014-07-15 22:16:29 +02:00
|
|
|
*/
|
2014-07-15 11:04:53 +02:00
|
|
|
class TransactionController extends BaseController
|
|
|
|
{
|
2014-07-15 06:58:08 +02:00
|
|
|
|
2014-11-12 15:22:01 +01:00
|
|
|
|
2014-12-30 21:03:42 +01:00
|
|
|
/** @var Helper */
|
|
|
|
protected $_helper;
|
|
|
|
/** @var Repository */
|
|
|
|
protected $_repository;
|
|
|
|
|
2014-07-15 22:16:29 +02:00
|
|
|
/**
|
2014-09-21 08:25:30 +02:00
|
|
|
* Construct a new transaction controller with two of the most often used helpers.
|
|
|
|
*
|
2014-12-30 21:03:42 +01:00
|
|
|
* @param Repository $repository
|
|
|
|
* @param Helper $helper
|
2014-07-15 22:16:29 +02:00
|
|
|
*/
|
2014-12-30 21:03:42 +01:00
|
|
|
public function __construct(Repository $repository, Helper $helper)
|
2014-07-15 11:04:53 +02:00
|
|
|
{
|
2014-12-30 21:03:42 +01:00
|
|
|
$this->_repository = $repository;
|
|
|
|
$this->_helper = $helper;
|
2014-09-12 17:31:12 +02:00
|
|
|
View::share('title', 'Transactions');
|
2014-09-17 08:55:51 +02:00
|
|
|
View::share('mainTitleIcon', 'fa-repeat');
|
2014-07-15 06:58:08 +02:00
|
|
|
}
|
|
|
|
|
2014-12-04 20:38:45 +01:00
|
|
|
|
2014-07-25 13:02:01 +02:00
|
|
|
/**
|
2014-09-21 08:25:30 +02:00
|
|
|
* Shows the view helping the user to create a new transaction journal.
|
|
|
|
*
|
2014-08-02 07:34:38 +02:00
|
|
|
* @param string $what
|
2014-07-25 13:02:01 +02:00
|
|
|
*
|
2014-08-02 07:34:38 +02:00
|
|
|
* @return \Illuminate\View\View
|
2014-07-25 13:02:01 +02:00
|
|
|
*/
|
2014-08-02 07:34:38 +02:00
|
|
|
public function create($what = 'deposit')
|
2014-07-15 20:58:35 +02:00
|
|
|
{
|
2014-12-30 21:03:42 +01:00
|
|
|
$accounts = FFForm::makeSelectList($this->_helper->getAssetAccounts());
|
|
|
|
$budgets = FFForm::makeSelectList($this->_helper->getBudgets());
|
|
|
|
$budgets[0] = '(no budget)';
|
|
|
|
$piggyBanks = $this->_helper->getPiggyBanks();
|
|
|
|
$repeatedExpenses = $this->_helper->getRepeatedExpenses();
|
|
|
|
$list = $piggyBanks->merge($repeatedExpenses);
|
|
|
|
$piggies = FFForm::makeSelectList($list);
|
|
|
|
$piggies[0] = '(no piggy bank)';
|
|
|
|
$preFilled = Session::has('preFilled') ? Session::get('preFilled') : [];
|
|
|
|
$respondTo = ['account_id', 'account_from_id'];
|
2015-01-01 23:12:12 +01:00
|
|
|
$subTitle = 'Add a new ' . e($what);
|
2014-09-21 08:25:30 +02:00
|
|
|
|
2014-10-08 21:04:31 +02:00
|
|
|
foreach ($respondTo as $r) {
|
|
|
|
if (!is_null(Input::get($r))) {
|
2014-12-07 15:37:53 +01:00
|
|
|
$preFilled[$r] = Input::get($r);
|
2014-10-08 21:04:31 +02:00
|
|
|
}
|
|
|
|
}
|
2014-12-07 15:37:53 +01:00
|
|
|
Session::put('preFilled', $preFilled);
|
2014-10-08 21:04:31 +02:00
|
|
|
|
2014-12-30 21:03:42 +01:00
|
|
|
asort($piggies);
|
|
|
|
|
|
|
|
|
|
|
|
return View::make('transactions.create', compact('accounts', 'budgets', 'what', 'piggies', 'subTitle'));
|
2014-07-15 20:58:35 +02:00
|
|
|
}
|
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
/**
|
2014-09-21 08:25:30 +02:00
|
|
|
* Shows the form that allows a user to delete a transaction journal.
|
|
|
|
*
|
2014-12-30 21:03:42 +01:00
|
|
|
* @param TransactionJournal $journal
|
2014-08-10 15:01:46 +02:00
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-12-30 21:03:42 +01:00
|
|
|
public function delete(TransactionJournal $journal)
|
2014-08-02 07:34:38 +02:00
|
|
|
{
|
2014-12-30 21:03:42 +01:00
|
|
|
$type = strtolower($journal->transactionType->type);
|
2015-01-01 23:12:12 +01:00
|
|
|
$subTitle = 'Delete ' . e($type) . ' "' . e($journal->description) . '"';
|
2014-09-17 08:55:51 +02:00
|
|
|
|
2014-12-30 21:03:42 +01:00
|
|
|
return View::make('transactions.delete', compact('journal', 'subTitle'));
|
2014-08-02 07:34:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $transactionJournal
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2014-08-02 07:34:38 +02:00
|
|
|
public function destroy(TransactionJournal $transactionJournal)
|
|
|
|
{
|
2014-12-30 21:03:42 +01:00
|
|
|
$type = $transactionJournal->transactionType->type;
|
2014-12-06 17:34:39 +01:00
|
|
|
$return = 'withdrawal';
|
|
|
|
|
2014-12-30 22:25:30 +01:00
|
|
|
Session::flash('success', 'Transaction "' . e($transactionJournal->description) . '" destroyed.');
|
|
|
|
|
|
|
|
$this->_repository->destroy($transactionJournal);
|
2014-11-16 10:31:19 +01:00
|
|
|
|
2014-09-21 08:25:30 +02:00
|
|
|
switch ($type) {
|
|
|
|
case 'Deposit':
|
2014-12-06 17:34:39 +01:00
|
|
|
$return = 'deposit';
|
2014-09-21 08:25:30 +02:00
|
|
|
break;
|
|
|
|
case 'Transfer':
|
2014-12-06 17:34:39 +01:00
|
|
|
$return = 'transfers';
|
2014-09-21 08:25:30 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-12-06 17:34:39 +01:00
|
|
|
|
|
|
|
return Redirect::route('transactions.index', $return);
|
2014-08-02 07:34:38 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 20:30:50 +02:00
|
|
|
/**
|
2014-09-21 08:25:30 +02:00
|
|
|
* Shows the view to edit a transaction.
|
|
|
|
*
|
2014-08-10 15:01:46 +02:00
|
|
|
* @param TransactionJournal $journal
|
2014-07-29 20:30:50 +02:00
|
|
|
*
|
2014-08-10 15:01:46 +02:00
|
|
|
* @return $this
|
2014-07-29 20:30:50 +02:00
|
|
|
*/
|
2014-08-02 07:34:38 +02:00
|
|
|
public function edit(TransactionJournal $journal)
|
2014-07-29 20:30:50 +02:00
|
|
|
{
|
2014-12-30 21:03:42 +01:00
|
|
|
$what = strtolower($journal->transactiontype->type);
|
2015-01-01 23:12:12 +01:00
|
|
|
$subTitle = 'Edit ' . e($what) . ' "' . e($journal->description) . '"';
|
2014-12-30 21:03:42 +01:00
|
|
|
$budgets = FFForm::makeSelectList($this->_helper->getBudgets(), true);
|
|
|
|
$accounts = FFForm::makeSelectList($this->_helper->getAssetAccounts());
|
|
|
|
$piggies = FFForm::makeSelectList($this->_helper->getPiggyBanks(), true);
|
|
|
|
$transactions = $journal->transactions()->orderBy('amount', 'DESC')->get();
|
|
|
|
$preFilled = [
|
|
|
|
'date' => $journal->date->format('Y-m-d'),
|
|
|
|
'category' => '',
|
|
|
|
'budget_id' => 0,
|
2014-12-24 20:55:42 +01:00
|
|
|
'piggy_bank_id' => 0
|
2014-11-18 09:37:54 +01:00
|
|
|
];
|
2014-09-21 08:25:30 +02:00
|
|
|
|
2014-08-02 07:34:38 +02:00
|
|
|
$category = $journal->categories()->first();
|
|
|
|
if (!is_null($category)) {
|
2014-12-07 15:37:53 +01:00
|
|
|
$preFilled['category'] = $category->name;
|
2014-08-02 07:34:38 +02:00
|
|
|
}
|
2014-09-21 08:25:30 +02:00
|
|
|
|
2014-12-30 21:03:42 +01:00
|
|
|
$budget = $journal->budgets()->first();
|
|
|
|
if (!is_null($budget)) {
|
|
|
|
$preFilled['budget_id'] = $budget->id;
|
|
|
|
}
|
2014-11-25 21:09:52 +01:00
|
|
|
|
2014-12-30 21:03:42 +01:00
|
|
|
if ($journal->piggyBankEvents()->count() > 0) {
|
|
|
|
$preFilled['piggy_bank_id'] = $journal->piggyBankEvents()->first()->piggy_bank_id;
|
2014-07-29 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
2014-12-30 21:03:42 +01:00
|
|
|
$preFilled['amount'] = $journal->getAmount();
|
|
|
|
$preFilled['account_id'] = $this->_helper->getAssetAccount($what, $transactions);
|
|
|
|
$preFilled['expense_account'] = $transactions[0]->account->name;
|
|
|
|
$preFilled['revenue_account'] = $transactions[1]->account->name;
|
|
|
|
$preFilled['account_from_id'] = $transactions[1]->account->id;
|
|
|
|
$preFilled['account_to_id'] = $transactions[0]->account->id;
|
2014-11-12 22:36:02 +01:00
|
|
|
|
2014-12-30 21:03:42 +01:00
|
|
|
|
|
|
|
return View::make('transactions.edit', compact('journal', 'accounts', 'what', 'budgets', 'piggies', 'subTitle'))->with('data', $preFilled);
|
2014-07-29 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 22:36:02 +01:00
|
|
|
/**
|
|
|
|
* @param $what
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function index($what)
|
|
|
|
{
|
|
|
|
switch ($what) {
|
|
|
|
case 'expenses':
|
|
|
|
case 'withdrawal':
|
|
|
|
$subTitleIcon = 'fa-long-arrow-left';
|
|
|
|
$subTitle = 'Expenses';
|
2014-12-30 21:03:42 +01:00
|
|
|
$journals = $this->_repository->getWithdrawalsPaginated(50);
|
2014-11-12 22:36:02 +01:00
|
|
|
break;
|
|
|
|
case 'revenue':
|
|
|
|
case 'deposit':
|
|
|
|
$subTitleIcon = 'fa-long-arrow-right';
|
|
|
|
$subTitle = 'Revenue, income and deposits';
|
2014-12-30 21:03:42 +01:00
|
|
|
$journals = $this->_repository->getDepositsPaginated(50);
|
2014-11-12 22:36:02 +01:00
|
|
|
break;
|
|
|
|
case 'transfer':
|
|
|
|
case 'transfers':
|
|
|
|
$subTitleIcon = 'fa-arrows-h';
|
|
|
|
$subTitle = 'Transfers';
|
2014-12-30 21:03:42 +01:00
|
|
|
$journals = $this->_repository->getTransfersPaginated(50);
|
2014-11-12 22:36:02 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-30 14:52:17 +01:00
|
|
|
return View::make('transactions.index', compact('subTitle', 'what', 'subTitleIcon', 'journals'));
|
2014-11-12 22:36:02 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-12-04 20:38:45 +01:00
|
|
|
|
2014-07-29 20:30:50 +02:00
|
|
|
/**
|
2014-08-10 15:01:46 +02:00
|
|
|
* @param TransactionJournal $journal
|
2014-07-29 20:30:50 +02:00
|
|
|
*
|
2014-08-10 15:01:46 +02:00
|
|
|
* @return $this
|
2014-07-29 20:30:50 +02:00
|
|
|
*/
|
2014-11-14 10:28:18 +01:00
|
|
|
public function show(TransactionJournal $journal)
|
|
|
|
{
|
2014-11-22 07:30:46 +01:00
|
|
|
$journal->transactions->each(
|
2014-11-25 21:09:52 +01:00
|
|
|
function (\Transaction $t) use ($journal) {
|
2014-11-22 07:30:46 +01: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')
|
|
|
|
);
|
2014-11-25 21:09:52 +01:00
|
|
|
$t->after = $t->before + $t->amount;
|
2014-11-22 07:30:46 +01:00
|
|
|
}
|
|
|
|
);
|
2014-11-30 14:52:17 +01:00
|
|
|
$members = new Collection;
|
|
|
|
/** @var TransactionGroup $group */
|
2014-12-04 20:38:45 +01:00
|
|
|
foreach ($journal->transactiongroups()->get() as $group) {
|
2014-12-30 21:03:42 +01:00
|
|
|
/** @var TransactionJournal $loopJournal */
|
|
|
|
foreach ($group->transactionjournals()->get() as $loopJournal) {
|
|
|
|
if ($loopJournal->id != $journal->id) {
|
|
|
|
$members->push($loopJournal);
|
2014-11-30 14:52:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-22 07:30:46 +01:00
|
|
|
|
2014-11-30 14:52:17 +01:00
|
|
|
return View::make('transactions.show', compact('journal', 'members'))->with(
|
2015-01-01 23:12:12 +01:00
|
|
|
'subTitle', e($journal->transactionType->type) . ' "' . e($journal->description) . '"'
|
2014-09-21 15:40:41 +02:00
|
|
|
);
|
2014-07-29 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
2014-07-25 13:02:01 +02:00
|
|
|
/**
|
2015-01-18 21:07:40 +01:00
|
|
|
* @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind.
|
|
|
|
*
|
2014-07-25 13:02:01 +02:00
|
|
|
* @param $what
|
|
|
|
*
|
2014-09-20 08:39:24 +02:00
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse
|
|
|
|
* @throws FireflyException
|
2014-07-25 13:02:01 +02:00
|
|
|
*/
|
2014-11-12 15:34:32 +01:00
|
|
|
public function store($what)
|
|
|
|
{
|
2014-12-31 16:45:12 +01:00
|
|
|
$data = Input::except('_token');
|
|
|
|
$transactionType = $this->_repository->getJournalType($what);
|
|
|
|
$transactionCurrency = $this->_repository->getJournalCurrency('EUR');
|
|
|
|
$data['transaction_type_id'] = $transactionType->id;
|
|
|
|
$data['transaction_currency_id'] = $transactionCurrency->id;
|
|
|
|
$data['completed'] = 0;
|
|
|
|
$data['what'] = $what;
|
2015-01-02 12:38:13 +01:00
|
|
|
$data['currency'] = 'EUR';
|
2014-12-30 21:03:42 +01:00
|
|
|
$messages = $this->_repository->validate($data);
|
2014-11-12 15:34:32 +01:00
|
|
|
|
2014-12-30 21:03:42 +01:00
|
|
|
Session::flash('warnings', $messages['warnings']);
|
|
|
|
Session::flash('successes', $messages['successes']);
|
|
|
|
Session::flash('errors', $messages['errors']);
|
|
|
|
if ($messages['errors']->count() > 0) {
|
|
|
|
Session::flash('error', 'Could not store transaction: ' . $messages['errors']->first());
|
2015-01-18 09:49:53 +01:00
|
|
|
return Redirect::route('transactions.create', $data['what'])->withInput();
|
2014-12-30 21:03:42 +01:00
|
|
|
}
|
2014-11-16 10:31:19 +01:00
|
|
|
|
2015-01-18 09:49:53 +01:00
|
|
|
if ($data['post_submit_action'] == 'validate_only') {
|
2014-12-30 21:03:42 +01:00
|
|
|
return Redirect::route('transactions.create', $data['what'])->withInput();
|
|
|
|
}
|
2014-11-12 15:34:32 +01:00
|
|
|
|
2014-12-30 21:03:42 +01:00
|
|
|
$journal = $this->_repository->store($data);
|
|
|
|
Event::fire('transactionJournal.store', [$journal, Input::get('piggy_bank_id')]); // new and used.
|
2015-01-18 21:07:40 +01:00
|
|
|
|
2014-12-30 21:03:42 +01:00
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($journal->transactions as $transaction) {
|
|
|
|
Event::fire('transaction.store', [$transaction]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Session::flash('success', 'Transaction "' . e($data['description']) . '" stored.');
|
|
|
|
if ($data['post_submit_action'] == 'store') {
|
|
|
|
return Redirect::route('transactions.index', $data['what']);
|
2014-11-12 15:34:32 +01:00
|
|
|
}
|
2014-12-30 21:03:42 +01:00
|
|
|
|
|
|
|
return Redirect::route('transactions.create', $data['what'])->withInput();
|
2014-07-16 21:11:43 +02:00
|
|
|
}
|
2014-07-15 20:58:35 +02:00
|
|
|
|
2014-09-28 09:20:25 +02:00
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
2014-12-13 21:59:02 +01:00
|
|
|
* @return $this
|
2014-09-21 16:22:18 +02:00
|
|
|
* @throws FireflyException
|
2014-08-10 15:01:46 +02:00
|
|
|
*/
|
2014-11-12 15:34:32 +01:00
|
|
|
public function update(TransactionJournal $journal)
|
|
|
|
{
|
2014-12-31 16:45:12 +01:00
|
|
|
$data = Input::except('_token');
|
|
|
|
$data['currency'] = 'EUR';
|
|
|
|
$data['what'] = strtolower($journal->transactionType->type);
|
|
|
|
$data['transaction_type_id'] = $journal->transaction_type_id;
|
|
|
|
$data['transaction_currency_id'] = $journal->transaction_currency_id;
|
|
|
|
$data['completed'] = 1;
|
|
|
|
$messages = $this->_repository->validate($data);
|
2014-11-13 17:01:09 +01:00
|
|
|
|
2014-12-30 21:03:42 +01:00
|
|
|
Session::flash('warnings', $messages['warnings']);
|
|
|
|
Session::flash('successes', $messages['successes']);
|
|
|
|
Session::flash('errors', $messages['errors']);
|
|
|
|
if ($messages['errors']->count() > 0) {
|
|
|
|
Session::flash('error', 'Could not update transaction: ' . $messages['errors']->first());
|
2015-01-18 21:07:40 +01:00
|
|
|
return Redirect::route('transactions.edit', $journal->id)->withInput();
|
2014-12-30 21:03:42 +01:00
|
|
|
}
|
2015-01-18 21:07:40 +01:00
|
|
|
if ($data['post_submit_action'] == 'validate_only') {
|
2014-12-30 21:03:42 +01:00
|
|
|
return Redirect::route('transactions.edit', $journal->id)->withInput();
|
|
|
|
}
|
|
|
|
$this->_repository->update($journal, $data);
|
|
|
|
Session::flash('success', 'Transaction "' . e($data['description']) . '" updated.');
|
|
|
|
Event::fire('transactionJournal.update', [$journal]); // new and used.
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($journal->transactions()->get() as $transaction) {
|
|
|
|
Event::fire('transaction.update', [$transaction]);
|
2014-11-13 17:01:09 +01:00
|
|
|
}
|
2014-12-30 21:03:42 +01:00
|
|
|
if ($data['post_submit_action'] == 'update') {
|
|
|
|
return Redirect::route('transactions.index', $data['what']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// go back to update screen.
|
|
|
|
return Redirect::route('transactions.edit', $journal->id)->withInput(['post_submit_action' => 'return_to_edit']);
|
|
|
|
|
2014-07-26 08:05:02 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-02 06:16:49 +01:00
|
|
|
}
|