Updated the transaction everything so views and forms work with the new transaction controller.

This commit is contained in:
James Cole 2014-09-12 17:31:12 +02:00
parent 0ae9ff4575
commit a1ba340ead
5 changed files with 377 additions and 228 deletions

View File

@ -20,6 +20,7 @@ class TransactionController extends BaseController
public function __construct(TJRI $repository) public function __construct(TJRI $repository)
{ {
$this->_repository = $repository; $this->_repository = $repository;
View::share('title', 'Transactions');
} }
/** /**
@ -29,15 +30,20 @@ class TransactionController extends BaseController
*/ */
public function create($what = 'deposit') public function create($what = 'deposit')
{ {
// get accounts with names and id's. /** @var \Firefly\Helper\Toolkit\Toolkit $toolkit */
$toolkit = App::make('Firefly\Helper\Toolkit\Toolkit');
// get asset accounts with names and id's.
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accountRepository */ /** @var \Firefly\Storage\Account\AccountRepositoryInterface $accountRepository */
$accountRepository = App::make('Firefly\Storage\Account\AccountRepositoryInterface'); $accountRepository = App::make('Firefly\Storage\Account\AccountRepositoryInterface');
$accounts = $accountRepository->getActiveDefaultAsSelectList(); $accounts = $accountRepository->getOfTypes(['Asset account','Default account']);
$assetAccounts = $toolkit->makeSelectList($accounts);
// get budgets as a select list. // get budgets as a select list.
/** @var \Firefly\Storage\Budget\BudgetRepositoryInterface $budgetRepository */ /** @var \Firefly\Storage\Budget\BudgetRepositoryInterface $budgetRepository */
$budgetRepository = App::make('Firefly\Storage\Budget\BudgetRepositoryInterface'); $budgetRepository = App::make('Firefly\Storage\Budget\BudgetRepositoryInterface');
$budgets = $budgetRepository->getAsSelectList(); $budgets = $toolkit->makeSelectList($budgetRepository->get());
$budgets[0] = '(no budget)'; $budgets[0] = '(no budget)';
// get the piggy banks. // get the piggy banks.
@ -45,9 +51,9 @@ class TransactionController extends BaseController
$piggyRepository = App::make('Firefly\Storage\Piggybank\PiggybankRepositoryInterface'); $piggyRepository = App::make('Firefly\Storage\Piggybank\PiggybankRepositoryInterface');
$piggies = $piggyRepository->get(); $piggies = $piggyRepository->get();
return View::make('transactions.create')->with('accounts', $accounts)->with('budgets', $budgets)->with( return View::make('transactions.create')->with('accounts', $assetAccounts)->with('budgets', $budgets)->with(
'what', $what 'what', $what
)->with('piggies', $piggies); )->with('piggies', $piggies)->with('subTitle', 'Add a new ' . $what)->with('title', 'Transactions');
} }
/** /**
@ -146,6 +152,71 @@ class TransactionController extends BaseController
)->with('budgets', $budgets)->with('data', $data)->with('piggies', $piggies); )->with('budgets', $budgets)->with('data', $data)->with('piggies', $piggies);
} }
public function expenses()
{
$transactionType = $this->_repository->getTransactionType('Withdrawal');
$start = is_null(Input::get('startdate')) ? null : new Carbon(Input::get('startdate'));
$end = is_null(Input::get('enddate')) ? null : new Carbon(Input::get('enddate'));
if ($start <= $end && !is_null($start) && !is_null($end)) {
$journals = $this->_repository->paginate($transactionType, 25, $start, $end);
$filtered = true;
$filters = ['start' => $start, 'end' => $end];
} else {
$journals = $this->_repository->paginate($transactionType, 25);
$filtered = false;
$filters = null;
}
return View::make('transactions.index')->with('journals', $journals)->with('filtered', $filtered)->with(
'filters', $filters
);
}
public function revenue()
{
$transactionType = $this->_repository->getTransactionType('Deposit');
$start = is_null(Input::get('startdate')) ? null : new Carbon(Input::get('startdate'));
$end = is_null(Input::get('enddate')) ? null : new Carbon(Input::get('enddate'));
if ($start <= $end && !is_null($start) && !is_null($end)) {
$journals = $this->_repository->paginate($transactionType, 25, $start, $end);
$filtered = true;
$filters = ['start' => $start, 'end' => $end];
} else {
$journals = $this->_repository->paginate($transactionType, 25);
$filtered = false;
$filters = null;
}
return View::make('transactions.index')->with('journals', $journals)->with('filtered', $filtered)->with(
'filters', $filters
);
}
public function transfers()
{
$transactionType = $this->_repository->getTransactionType('Transfer');
$start = is_null(Input::get('startdate')) ? null : new Carbon(Input::get('startdate'));
$end = is_null(Input::get('enddate')) ? null : new Carbon(Input::get('enddate'));
if ($start <= $end && !is_null($start) && !is_null($end)) {
$journals = $this->_repository->paginate($transactionType, 25, $start, $end);
$filtered = true;
$filters = ['start' => $start, 'end' => $end];
} else {
$journals = $this->_repository->paginate($transactionType, 25);
$filtered = false;
$filters = null;
}
return View::make('transactions.index')->with('journals', $journals)->with('filtered', $filtered)->with(
'filters', $filters
);
}
/** /**
* @return $this|\Illuminate\View\View * @return $this|\Illuminate\View\View
*/ */
@ -186,8 +257,14 @@ class TransactionController extends BaseController
*/ */
public function store($what) public function store($what)
{ {
$journal = $this->_repository->store($what, Input::all()); $journal = $this->_repository->store($what, Input::all());
if ($journal->validate()) { if($journal->errors()->count() > 0) {
Session::flash('error', 'Could not save transaction: ' . $journal->errors()->first().'!');
return Redirect::route('transactions.create', [$what])->withInput()->withErrors($journal->errors());
}
if ($journal->validate() && !is_null($journal->id)) {
Session::flash('success', 'Transaction "' . $journal->description . '" saved!'); Session::flash('success', 'Transaction "' . $journal->description . '" saved!');
// if reminder present, deactivate it: // if reminder present, deactivate it:
@ -204,14 +281,23 @@ class TransactionController extends BaseController
if (Input::get('create') == '1') { if (Input::get('create') == '1') {
return Redirect::route('transactions.create', [$what])->withInput(); return Redirect::route('transactions.create', [$what])->withInput();
} else { } else {
return Redirect::route('transactions.index'); switch($what) {
case 'withdrawal':
return Redirect::route('transactions.expenses');
break;
case 'deposit':
return Redirect::route('transactions.revenue');
break;
case 'transfer':
return Redirect::route('transactions.transfers');
break;
}
} }
} else { } else {
Session::flash('error', 'Could not save transaction: ' . $journal->errors()->first()); Session::flash('error', 'Could not save transaction: ' . $journal->errors()->first().'!');
return Redirect::route('transactions.create', [$what])->withInput()->withErrors( return Redirect::route('transactions.create', [$what])->withInput()->withErrors($journal->errors());
$journal->errors()
);
} }

View File

@ -24,134 +24,6 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$this->_user = \Auth::user(); $this->_user = \Auth::user();
} }
/**
*
* We're building this thinking the money goes from A to B.
* If the amount is negative however, the money still goes
* from A to B but the balances are reversed.
*
* Aka:
*
* Amount = 200
* A loses 200 (-200). * -1
* B gains 200 (200). * 1
*
* Final balance: -200 for A, 200 for B.
*
* When the amount is negative:
*
* Amount = -200
* A gains 200 (200). * -1
* B loses 200 (-200). * 1
*
* @param \Account $from
* @param \Account $toAccount
* @param $description
* @param $amount
* @param \Carbon\Carbon $date
*
* @return \TransactionJournal
* @throws \Firefly\Exception\FireflyException
*/
public function createSimpleJournal(\Account $from, \Account $toAccount, $description, $amount, Carbon $date)
{
$journal = new \TransactionJournal;
$amountFrom = $amount * -1;
$amountTo = $amount;
if (round(floatval($amount), 2) == 0.00) {
$journal->errors()->add('amount', 'Amount must not be zero.');
return $journal;
}
// same account:
if ($from->id == $toAccount->id) {
$journal->errors()->add('account_id', 'Must be different accounts.');
$journal->errors()->add('account_from_id', 'Must be different accounts.');
return $journal;
}
// account types for both:
$toAT = $toAccount->accountType->type;
$fromAT = $from->accountType->type;
$journalType = null;
switch (true) {
case ($from->transactions()->count() == 0 && $toAccount->transactions()->count() == 0):
$journalType = \TransactionType::where('type', 'Opening balance')->first();
break;
case (in_array($fromAT,['Default account','Asset account']) && in_array($toAT,['Default account','Asset account'])): // both are yours:
// determin transaction type. If both accounts are new, it's an initial balance transfer.
$journalType = \TransactionType::where('type', 'Transfer')->first();
break;
case ($amount < 0):
$journalType = \TransactionType::where('type', 'Deposit')->first();
break;
// is deposit into one of your own accounts:
case ($toAT == 'Default account' || $toAT == 'Asset account'):
$journalType = \TransactionType::where('type', 'Deposit')->first();
break;
// is withdrawal from one of your own accounts:
case ($fromAT == 'Default account' || $fromAT == 'Asset account'):
$journalType = \TransactionType::where('type', 'Withdrawal')->first();
break;
}
if (is_null($journalType)) {
throw new FireflyException('Could not figure out transaction type.');
}
// always the same currency:
$currency = \TransactionCurrency::where('code', 'EUR')->first();
if (is_null($currency)) {
throw new FireflyException('No currency for journal!');
}
// new journal:
$journal->transactionType()->associate($journalType);
$journal->transactionCurrency()->associate($currency);
$journal->user()->associate($this->_user);
$journal->completed = false;
$journal->description = $description;
$journal->date = $date;
if (!$journal->validate()) {
return $journal;
}
$journal->save();
// create transactions:
$fromTransaction = new \Transaction;
$fromTransaction->account()->associate($from);
$fromTransaction->transactionJournal()->associate($journal);
$fromTransaction->description = null;
$fromTransaction->amount = $amountFrom;
if (!$fromTransaction->validate()) {
throw new FireflyException('Cannot create valid transaction (from): ' . $fromTransaction->errors()
->first());
}
$fromTransaction->save();
$toTransaction = new \Transaction;
$toTransaction->account()->associate($toAccount);
$toTransaction->transactionJournal()->associate($journal);
$toTransaction->description = null;
$toTransaction->amount = $amountTo;
if (!$toTransaction->validate()) {
throw new FireflyException('Cannot create valid transaction (to): ' . $toTransaction->errors()->first());
}
$toTransaction->save();
$journal->completed = true;
$journal->save();
return $journal;
}
/** /**
* @param $journalId * @param $journalId
* *
@ -160,12 +32,12 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
public function find($journalId) public function find($journalId)
{ {
return $this->_user->transactionjournals()->with( return $this->_user->transactionjournals()->with(
['transactions' => function ($q) { ['transactions' => function ($q) {
return $q->orderBy('amount', 'ASC'); return $q->orderBy('amount', 'ASC');
}, 'transactioncurrency', 'transactiontype', 'components', 'transactions.account', }, 'transactioncurrency', 'transactiontype', 'components', 'transactions.account',
'transactions.account.accounttype'] 'transactions.account.accounttype']
) )
->where('id', $journalId)->first(); ->where('id', $journalId)->first();
} }
/** /**
@ -176,9 +48,19 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
} }
/**
* @param $type
*
* @return \TransactionType
*/
public function getTransactionType($type)
{
return \TransactionType::whereType($type)->first();
}
/** /**
* @param \Account $account * @param \Account $account
* @param Carbon $date * @param Carbon $date
* *
* @return mixed * @return mixed
*/ */
@ -186,31 +68,33 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
{ {
$accountID = $account->id; $accountID = $account->id;
$query = $this->_user->transactionjournals()->with( $query = $this->_user->transactionjournals()->with(
[ [
'transactions', 'transactions',
'transactions.account', 'transactions.account',
'transactioncurrency', 'transactioncurrency',
'transactiontype' 'transactiontype'
] ]
) )
->distinct() ->distinct()
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', ->leftJoin(
'transaction_journals.id') 'transactions', 'transactions.transaction_journal_id', '=',
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id') 'transaction_journals.id'
->where('transactions.account_id', $accountID) )
->where('transaction_journals.date', $date->format('Y-m-d')) ->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
->orderBy('transaction_journals.date', 'DESC') ->where('transactions.account_id', $accountID)
->orderBy('transaction_journals.id', 'DESC') ->where('transaction_journals.date', $date->format('Y-m-d'))
->get(['transaction_journals.*']); ->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.id', 'DESC')
->get(['transaction_journals.*']);
return $query; return $query;
} }
/** /**
* @param \Account $account * @param \Account $account
* @param int $count * @param int $count
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* *
* @return mixed * @return mixed
*/ */
@ -218,28 +102,31 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
{ {
$accountID = $account->id; $accountID = $account->id;
$query = $this->_user->transactionjournals()->with( $query = $this->_user->transactionjournals()->with(
[ [
'transactions', 'transactions',
'transactioncurrency', 'transactioncurrency',
'transactiontype' 'transactiontype'
] ]
) )
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', ->leftJoin(
'transaction_journals.id') 'transactions', 'transactions.transaction_journal_id', '=',
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id') 'transaction_journals.id'
->where('accounts.id', $accountID) )
->where('date', '>=', $start->format('Y-m-d')) ->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
->where('date', '<=', $end->format('Y-m-d')) ->where('accounts.id', $accountID)
->orderBy('transaction_journals.date', 'DESC') ->where('date', '>=', $start->format('Y-m-d'))
->orderBy('transaction_journals.id', 'DESC') ->where('date', '<=', $end->format('Y-m-d'))
->take($count) ->orderBy('transaction_journals.date', 'DESC')
->get(['transaction_journals.*']); ->orderBy('transaction_journals.id', 'DESC')
->take($count)
->get(['transaction_journals.*']);
return $query; return $query;
} }
/** /**
* @param \User $user * @param \User $user
*
* @return mixed|void * @return mixed|void
*/ */
public function overruleUser(\User $user) public function overruleUser(\User $user)
@ -249,15 +136,19 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
} }
/** /**
* @param int $count * @param \TransactionType $type
* @param int $count
* @param Carbon $start
* @param Carbon $end
* *
* @return mixed * @return mixed
*/ */
public function paginate($count = 25, Carbon $start = null, Carbon $end = null) public function paginate(\TransactionType $type, $count = 25, Carbon $start = null, Carbon $end = null)
{ {
$query = $this->_user->transactionjournals()->WithRelevantData() $query = $this->_user->transactionjournals()->WithRelevantData()
->orderBy('transaction_journals.date', 'DESC') ->transactionTypes([$type->type])
->orderBy('transaction_journals.id', 'DESC'); ->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.id', 'DESC');
if (!is_null($start)) { if (!is_null($start)) {
$query->where('transaction_journals.date', '>=', $start->format('Y-m-d')); $query->where('transaction_journals.date', '>=', $start->format('Y-m-d'));
} }
@ -265,11 +156,12 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$query->where('transaction_journals.date', '<=', $end->format('Y-m-d')); $query->where('transaction_journals.date', '<=', $end->format('Y-m-d'));
} }
$result = $query->paginate($count); $result = $query->select(['transaction_journals.*'])->paginate($count);
return $result; return $result;
} }
/** /**
* @param $what * @param $what
* @param $data * @param $data
@ -299,12 +191,25 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
switch ($what) { switch ($what) {
case 'withdrawal': case 'withdrawal':
$fromAccount = $accountRepository->find(intval($data['account_id'])); $fromAccount = $accountRepository->find(intval($data['account_id']));
$toAccount = $accountRepository->createOrFindBeneficiary($data['beneficiary']); $expenseAccountType = $accountRepository->findAccountType('Expense account');
$set = [
'name' => $data['expense_account'],
'account_type_id' => $expenseAccountType->id,
'user_id' => $this->_user->id,
'active' => 1];
$toAccount = $accountRepository->firstOrCreate($set);
break; break;
case 'deposit': case 'deposit':
$fromAccount = $accountRepository->createOrFindBeneficiary($data['beneficiary']); $revenueAccountType = $accountRepository->findAccountType('Revenue account');
$set = [
'name' => $data['revenue_account'],
'account_type_id' => $revenueAccountType->id,
'user_id' => $this->_user->id,
'active' => 1];
$fromAccount = $accountRepository->firstOrCreate($set);
$toAccount = $accountRepository->find(intval($data['account_id'])); $toAccount = $accountRepository->find(intval($data['account_id']));
break; break;
case 'transfer': case 'transfer':
@ -353,15 +258,15 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$transaction->piggybank()->associate($piggyBank); $transaction->piggybank()->associate($piggyBank);
$transaction->save(); $transaction->save();
\Event::fire( \Event::fire(
'piggybanks.createRelatedTransfer', [$piggyBank, $transactionJournal, $transaction] 'piggybanks.createRelatedTransfer', [$piggyBank, $transactionJournal, $transaction]
); );
break; break;
} }
} }
if ($connected === false) { if ($connected === false) {
\Session::flash( \Session::flash(
'warning', 'Piggy bank "' . e($piggyBank->name) 'warning', 'Piggy bank "' . e($piggyBank->name)
. '" is not set to draw money from any of the accounts in this transfer' . '" is not set to draw money from any of the accounts in this transfer'
); );
} }
} }
@ -380,6 +285,142 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
return $transactionJournal; return $transactionJournal;
} }
/**
*
* We're building this thinking the money goes from A to B.
* If the amount is negative however, the money still goes
* from A to B but the balances are reversed.
*
* Aka:
*
* Amount = 200
* A loses 200 (-200). * -1
* B gains 200 (200). * 1
*
* Final balance: -200 for A, 200 for B.
*
* When the amount is negative:
*
* Amount = -200
* A gains 200 (200). * -1
* B loses 200 (-200). * 1
*
* @param \Account $from
* @param \Account $toAccount
* @param $description
* @param $amount
* @param \Carbon\Carbon $date
*
* @return \TransactionJournal
* @throws \Firefly\Exception\FireflyException
*/
public function createSimpleJournal(\Account $fromAccount, \Account $toAccount, $description, $amount, Carbon $date)
{
$journal = new \TransactionJournal;
$journal->completed = false;
$journal->description = $description;
$journal->date = $date;
$amountFrom = $amount * -1;
$amountTo = $amount;
if (round(floatval($amount), 2) == 0.00) {
$journal->errors()->add('amount', 'Amount must not be zero.');
return $journal;
}
// account types for both:
$toAT = $toAccount->accountType->type;
$fromAT = $fromAccount->accountType->type;
$journalType = null;
switch (true) {
case ($fromAccount->transactions()->count() == 0 && $toAccount->transactions()->count() == 0):
$journalType = \TransactionType::where('type', 'Opening balance')->first();
break;
case (in_array($fromAT, ['Default account', 'Asset account'])
&& in_array(
$toAT, ['Default account', 'Asset account']
)): // both are yours:
// determin transaction type. If both accounts are new, it's an initial balance transfer.
$journalType = \TransactionType::where('type', 'Transfer')->first();
break;
case ($amount < 0):
$journalType = \TransactionType::where('type', 'Deposit')->first();
break;
// is deposit into one of your own accounts:
case ($toAT == 'Default account' || $toAT == 'Asset account'):
$journalType = \TransactionType::where('type', 'Deposit')->first();
break;
// is withdrawal from one of your own accounts:
case ($fromAT == 'Default account' || $fromAT == 'Asset account'):
$journalType = \TransactionType::where('type', 'Withdrawal')->first();
break;
}
if (is_null($journalType)) {
throw new FireflyException('Could not figure out transaction type.');
}
// always the same currency:
$currency = \TransactionCurrency::where('code', 'EUR')->first();
if (is_null($currency)) {
throw new FireflyException('No currency for journal!');
}
// new journal:
$journal->transactionType()->associate($journalType);
$journal->transactionCurrency()->associate($currency);
$journal->user()->associate($this->_user);
// same account:
if ($fromAccount->id == $toAccount->id) {
$journal->errors()->add('account_to_id', 'Must be different from the "account from".');
$journal->errors()->add('account_from_id', 'Must be different from the "account to".');
return $journal;
}
if (!$journal->validate()) {
return $journal;
}
$journal->save();
// create transactions:
$fromTransaction = new \Transaction;
$fromTransaction->account()->associate($fromAccount);
$fromTransaction->transactionJournal()->associate($journal);
$fromTransaction->description = null;
$fromTransaction->amount = $amountFrom;
if (!$fromTransaction->validate()) {
throw new FireflyException('Cannot create valid transaction (from): ' . $fromTransaction->errors()
->first());
}
$fromTransaction->save();
$toTransaction = new \Transaction;
$toTransaction->account()->associate($toAccount);
$toTransaction->transactionJournal()->associate($journal);
$toTransaction->description = null;
$toTransaction->amount = $amountTo;
if (!$toTransaction->validate()) {
throw new FireflyException('Cannot create valid transaction (to): ' . $toTransaction->errors()->first());
}
$toTransaction->save();
$journal->completed = true;
$journal->save();
return $journal;
}
/** /**
* @param \TransactionJournal $journal * @param \TransactionJournal $journal
* @param $data * @param $data
@ -496,8 +537,8 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
} }
if ($connected === false) { if ($connected === false) {
\Session::flash( \Session::flash(
'warning', 'Piggy bank "' . e($piggyBank->name) 'warning', 'Piggy bank "' . e($piggyBank->name)
. '" is not set to draw money from any of the accounts in this transfer' . '" is not set to draw money from any of the accounts in this transfer'
); );
} }
} }

View File

@ -16,7 +16,7 @@ interface TransactionJournalRepositoryInterface
* @param \Account $toAccount * @param \Account $toAccount
* @param $description * @param $description
* @param $amount * @param $amount
* @param Carbon $date * @param Carbon $date
* *
* @return mixed * @return mixed
*/ */
@ -27,8 +27,10 @@ interface TransactionJournalRepositoryInterface
*/ */
public function get(); public function get();
/** /**
* @param \User $user * @param \User $user
*
* @return mixed * @return mixed
*/ */
public function overruleUser(\User $user); public function overruleUser(\User $user);
@ -49,6 +51,13 @@ interface TransactionJournalRepositoryInterface
*/ */
public function update(\TransactionJournal $journal, $data); public function update(\TransactionJournal $journal, $data);
/**
* @param $type
*
* @return \TransactionType
*/
public function getTransactionType($type);
/** /**
* @param $journalId * @param $journalId
* *
@ -58,9 +67,9 @@ interface TransactionJournalRepositoryInterface
/** /**
* @param \Account $account * @param \Account $account
* @param int $count * @param int $count
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* *
* @return mixed * @return mixed
*/ */
@ -68,17 +77,20 @@ interface TransactionJournalRepositoryInterface
/** /**
* @param \Account $account * @param \Account $account
* @param Carbon $date * @param Carbon $date
* *
* @return mixed * @return mixed
*/ */
public function getByAccountAndDate(\Account $account, Carbon $date); public function getByAccountAndDate(\Account $account, Carbon $date);
/** /**
* @param int $count * @param \TransactionType $type
* @param int $count
* @param Carbon $start
* @param Carbon $end
* *
* @return mixed * @return mixed
*/ */
public function paginate($count = 25, Carbon $start = null, Carbon $end = null); public function paginate(\TransactionType $type, $count = 25, Carbon $start = null, Carbon $end = null);
} }

View File

@ -95,6 +95,18 @@ use LaravelBook\Ardent\Builder;
* 'Budget[] $budgets * 'Budget[] $budgets
* @property-read \Illuminate\Database\Eloquent\Collection|\ * @property-read \Illuminate\Database\Eloquent\Collection|\
* 'Category[] $categories * 'Category[] $categories
* @property integer $recurring_transaction_id
* @property-read \Illuminate\Database\Eloquent\Collection|\
* 'Budget[] $budgets
* @property-read \Illuminate\Database\Eloquent\Collection|\
* 'Category[] $categories
* @property-read \RecurringTransaction $recurringTransaction
* @method static \Illuminate\Database\Query\Builder|\TransactionJournal whereRecurringTransactionId($value)
* @property-read \Illuminate\Database\Eloquent\Collection|\
* 'Budget[] $budgets
* @property-read \Illuminate\Database\Eloquent\Collection|\
* 'Category[] $categories
* @method static \TransactionJournal accountIs($account)
*/ */
class TransactionJournal extends Ardent class TransactionJournal extends Ardent
{ {
@ -153,7 +165,11 @@ class TransactionJournal extends Ardent
return ['created_at', 'updated_at', 'date']; return ['created_at', 'updated_at', 'date'];
} }
public function scopeAccount(Builder $query, \Account $account) /**
* @param Builder $query
* @param Account $account
*/
public function scopeAccountIs(Builder $query, \Account $account)
{ {
if (!isset($this->joinedTransactions)) { if (!isset($this->joinedTransactions)) {
$query->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id'); $query->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id');

View File

@ -1,21 +1,10 @@
@extends('layouts.default') @extends('layouts.default')
@section('content') @section('content')
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<h1>Firefly
<small>Add a new {{$what}}</small>
</h1>
</div>
</div>
<div class="row"> <div class="row">
<div class="col-lg-6 col-md-12 col-sm-12"> <div class="col-lg-6 col-md-12 col-sm-12">
<p class="text-info">
Technically speaking, withdrawals, deposits and transfers are all transactions, moving money from
account <em>A</em> to account <em>B</em>.
</p>
<p class="text-info"> <p class="text-info">
@if($what == 'withdrawal') @if($what == 'withdrawal')
A withdrawal is when you spend money on something, moving an amount to a <em>beneficiary</em>. Some text about moving from asset accounts to expense accounts
@endif @endif
@if($what == 'deposit') @if($what == 'deposit')
A deposit is when you earn money, moving an amount from a beneficiary into your own account. A deposit is when you earn money, moving an amount from a beneficiary into your own account.
@ -49,12 +38,7 @@
@if($what == 'deposit' || $what == 'withdrawal') @if($what == 'deposit' || $what == 'withdrawal')
<div class="form-group"> <div class="form-group">
<label for="account_id" class="col-sm-4 control-label"> <label for="account_id" class="col-sm-4 control-label">
@if($what == 'deposit') Asset account
Receiving account
@endif
@if($what == 'withdrawal')
Paid from account
@endif
</label> </label>
<div class="col-sm-8"> <div class="col-sm-8">
{{Form::select('account_id',$accounts,Input::old('account_id') ?: Input::get('account_id'),['class' => 'form-control'])}} {{Form::select('account_id',$accounts,Input::old('account_id') ?: Input::get('account_id'),['class' => 'form-control'])}}
@ -65,23 +49,33 @@
</div> </div>
@endif @endif
<!-- SHOW BENEFICIARY (ACCOUNT TO) ONLY FOR WITHDRAWALS AND DEPOSITS --> <!-- SHOW EXPENSE ACCOUNT ONLY FOR WITHDRAWALS -->
@if($what == 'deposit' || $what == 'withdrawal') @if($what == 'withdrawal')
<div class="form-group"> <div class="form-group">
<label for="beneficiary" class="col-sm-4 control-label"> <label for="expense_account" class="col-sm-4 control-label">Expense account</label>
@if($what == 'deposit') <div class="col-sm-8">
Paying beneficiary <input type="text" name="expense_account" value="{{{Input::old('expense_account')}}}" autocomplete="off" class="form-control" placeholder="Expense account" />
@endif @if($errors->has('expense_account'))
@if($what == 'withdrawal') <p class="text-danger">{{$errors->first('expense_account')}}</p>
Beneficiary @else
<span class="help-block">This field will auto-complete your existing expense accounts (if any), but you can type freely to create new ones.</span>
@endif @endif
</div>
</div>
@endif
<!-- SHOW REVENUE ACCOUNT ONLY FOR DEPOSITS -->
@if($what == 'deposit')
<div class="form-group">
<label for="revenue_account" class="col-sm-4 control-label">
Revenue account
</label> </label>
<div class="col-sm-8"> <div class="col-sm-8">
<input type="text" name="beneficiary" value="{{{Input::old('beneficiary')}}}" autocomplete="off" class="form-control" placeholder="Beneficiary" /> <input type="text" name="revenue_account" value="{{{Input::old('revenue_account')}}}" autocomplete="off" class="form-control" placeholder="Revenue account" />
@if($errors->has('beneficiary')) @if($errors->has('beneficiary'))
<p class="text-danger">{{$errors->first('beneficiary')}}</p> <p class="text-danger">{{$errors->first('revenue_account')}}</p>
@else @else
<span class="help-block">This field will auto-complete your existing beneficiaries (if any), but you can type freely to create new ones.</span> <span class="help-block">This field will auto-complete your existing revenue accounts (if any), but you can type freely to create new ones.</span>
@endif @endif
</div> </div>
</div> </div>