mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-11-30 12:43:57 -06:00
Updated the transaction everything so views and forms work with the new transaction controller.
This commit is contained in:
parent
0ae9ff4575
commit
a1ba340ead
@ -20,6 +20,7 @@ class TransactionController extends BaseController
|
||||
public function __construct(TJRI $repository)
|
||||
{
|
||||
$this->_repository = $repository;
|
||||
View::share('title', 'Transactions');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -29,15 +30,20 @@ class TransactionController extends BaseController
|
||||
*/
|
||||
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 */
|
||||
$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.
|
||||
/** @var \Firefly\Storage\Budget\BudgetRepositoryInterface $budgetRepository */
|
||||
$budgetRepository = App::make('Firefly\Storage\Budget\BudgetRepositoryInterface');
|
||||
$budgets = $budgetRepository->getAsSelectList();
|
||||
$budgets = $toolkit->makeSelectList($budgetRepository->get());
|
||||
$budgets[0] = '(no budget)';
|
||||
|
||||
// get the piggy banks.
|
||||
@ -45,9 +51,9 @@ class TransactionController extends BaseController
|
||||
$piggyRepository = App::make('Firefly\Storage\Piggybank\PiggybankRepositoryInterface');
|
||||
$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
|
||||
)->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);
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
@ -186,8 +257,14 @@ class TransactionController extends BaseController
|
||||
*/
|
||||
public function store($what)
|
||||
{
|
||||
|
||||
$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!');
|
||||
|
||||
// if reminder present, deactivate it:
|
||||
@ -204,14 +281,23 @@ class TransactionController extends BaseController
|
||||
if (Input::get('create') == '1') {
|
||||
return Redirect::route('transactions.create', [$what])->withInput();
|
||||
} 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 {
|
||||
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(
|
||||
$journal->errors()
|
||||
);
|
||||
return Redirect::route('transactions.create', [$what])->withInput()->withErrors($journal->errors());
|
||||
}
|
||||
|
||||
|
||||
|
@ -24,134 +24,6 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
||||
$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
|
||||
*
|
||||
@ -160,12 +32,12 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
||||
public function find($journalId)
|
||||
{
|
||||
return $this->_user->transactionjournals()->with(
|
||||
['transactions' => function ($q) {
|
||||
return $q->orderBy('amount', 'ASC');
|
||||
}, 'transactioncurrency', 'transactiontype', 'components', 'transactions.account',
|
||||
'transactions.account.accounttype']
|
||||
['transactions' => function ($q) {
|
||||
return $q->orderBy('amount', 'ASC');
|
||||
}, 'transactioncurrency', 'transactiontype', 'components', 'transactions.account',
|
||||
'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 Carbon $date
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
@ -186,31 +68,33 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
||||
{
|
||||
$accountID = $account->id;
|
||||
$query = $this->_user->transactionjournals()->with(
|
||||
[
|
||||
'transactions',
|
||||
'transactions.account',
|
||||
'transactioncurrency',
|
||||
'transactiontype'
|
||||
]
|
||||
[
|
||||
'transactions',
|
||||
'transactions.account',
|
||||
'transactioncurrency',
|
||||
'transactiontype'
|
||||
]
|
||||
)
|
||||
->distinct()
|
||||
->leftJoin('transactions', 'transactions.transaction_journal_id', '=',
|
||||
'transaction_journals.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
||||
->where('transactions.account_id', $accountID)
|
||||
->where('transaction_journals.date', $date->format('Y-m-d'))
|
||||
->orderBy('transaction_journals.date', 'DESC')
|
||||
->orderBy('transaction_journals.id', 'DESC')
|
||||
->get(['transaction_journals.*']);
|
||||
->distinct()
|
||||
->leftJoin(
|
||||
'transactions', 'transactions.transaction_journal_id', '=',
|
||||
'transaction_journals.id'
|
||||
)
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
||||
->where('transactions.account_id', $accountID)
|
||||
->where('transaction_journals.date', $date->format('Y-m-d'))
|
||||
->orderBy('transaction_journals.date', 'DESC')
|
||||
->orderBy('transaction_journals.id', 'DESC')
|
||||
->get(['transaction_journals.*']);
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Account $account
|
||||
* @param int $count
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param int $count
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
@ -218,28 +102,31 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
||||
{
|
||||
$accountID = $account->id;
|
||||
$query = $this->_user->transactionjournals()->with(
|
||||
[
|
||||
'transactions',
|
||||
'transactioncurrency',
|
||||
'transactiontype'
|
||||
]
|
||||
[
|
||||
'transactions',
|
||||
'transactioncurrency',
|
||||
'transactiontype'
|
||||
]
|
||||
)
|
||||
->leftJoin('transactions', 'transactions.transaction_journal_id', '=',
|
||||
'transaction_journals.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
||||
->where('accounts.id', $accountID)
|
||||
->where('date', '>=', $start->format('Y-m-d'))
|
||||
->where('date', '<=', $end->format('Y-m-d'))
|
||||
->orderBy('transaction_journals.date', 'DESC')
|
||||
->orderBy('transaction_journals.id', 'DESC')
|
||||
->take($count)
|
||||
->get(['transaction_journals.*']);
|
||||
->leftJoin(
|
||||
'transactions', 'transactions.transaction_journal_id', '=',
|
||||
'transaction_journals.id'
|
||||
)
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
||||
->where('accounts.id', $accountID)
|
||||
->where('date', '>=', $start->format('Y-m-d'))
|
||||
->where('date', '<=', $end->format('Y-m-d'))
|
||||
->orderBy('transaction_journals.date', 'DESC')
|
||||
->orderBy('transaction_journals.id', 'DESC')
|
||||
->take($count)
|
||||
->get(['transaction_journals.*']);
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \User $user
|
||||
*
|
||||
* @return mixed|void
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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()
|
||||
->orderBy('transaction_journals.date', 'DESC')
|
||||
->orderBy('transaction_journals.id', 'DESC');
|
||||
->transactionTypes([$type->type])
|
||||
->orderBy('transaction_journals.date', 'DESC')
|
||||
->orderBy('transaction_journals.id', 'DESC');
|
||||
if (!is_null($start)) {
|
||||
$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'));
|
||||
}
|
||||
|
||||
$result = $query->paginate($count);
|
||||
$result = $query->select(['transaction_journals.*'])->paginate($count);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $what
|
||||
* @param $data
|
||||
@ -299,12 +191,25 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
||||
|
||||
switch ($what) {
|
||||
case 'withdrawal':
|
||||
$fromAccount = $accountRepository->find(intval($data['account_id']));
|
||||
$toAccount = $accountRepository->createOrFindBeneficiary($data['beneficiary']);
|
||||
$fromAccount = $accountRepository->find(intval($data['account_id']));
|
||||
$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;
|
||||
|
||||
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']));
|
||||
break;
|
||||
case 'transfer':
|
||||
@ -353,15 +258,15 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
||||
$transaction->piggybank()->associate($piggyBank);
|
||||
$transaction->save();
|
||||
\Event::fire(
|
||||
'piggybanks.createRelatedTransfer', [$piggyBank, $transactionJournal, $transaction]
|
||||
'piggybanks.createRelatedTransfer', [$piggyBank, $transactionJournal, $transaction]
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($connected === false) {
|
||||
\Session::flash(
|
||||
'warning', 'Piggy bank "' . e($piggyBank->name)
|
||||
. '" is not set to draw money from any of the accounts in this transfer'
|
||||
'warning', 'Piggy bank "' . e($piggyBank->name)
|
||||
. '" is not set to draw money from any of the accounts in this transfer'
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -380,6 +285,142 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
||||
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 $data
|
||||
@ -496,8 +537,8 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
||||
}
|
||||
if ($connected === false) {
|
||||
\Session::flash(
|
||||
'warning', 'Piggy bank "' . e($piggyBank->name)
|
||||
. '" is not set to draw money from any of the accounts in this transfer'
|
||||
'warning', 'Piggy bank "' . e($piggyBank->name)
|
||||
. '" is not set to draw money from any of the accounts in this transfer'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ interface TransactionJournalRepositoryInterface
|
||||
* @param \Account $toAccount
|
||||
* @param $description
|
||||
* @param $amount
|
||||
* @param Carbon $date
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
@ -27,8 +27,10 @@ interface TransactionJournalRepositoryInterface
|
||||
*/
|
||||
public function get();
|
||||
|
||||
|
||||
/**
|
||||
* @param \User $user
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function overruleUser(\User $user);
|
||||
@ -49,6 +51,13 @@ interface TransactionJournalRepositoryInterface
|
||||
*/
|
||||
public function update(\TransactionJournal $journal, $data);
|
||||
|
||||
/**
|
||||
* @param $type
|
||||
*
|
||||
* @return \TransactionType
|
||||
*/
|
||||
public function getTransactionType($type);
|
||||
|
||||
/**
|
||||
* @param $journalId
|
||||
*
|
||||
@ -58,9 +67,9 @@ interface TransactionJournalRepositoryInterface
|
||||
|
||||
/**
|
||||
* @param \Account $account
|
||||
* @param int $count
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param int $count
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
@ -68,17 +77,20 @@ interface TransactionJournalRepositoryInterface
|
||||
|
||||
/**
|
||||
* @param \Account $account
|
||||
* @param Carbon $date
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getByAccountAndDate(\Account $account, Carbon $date);
|
||||
|
||||
/**
|
||||
* @param int $count
|
||||
* @param \TransactionType $type
|
||||
* @param int $count
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @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);
|
||||
|
||||
}
|
@ -95,6 +95,18 @@ use LaravelBook\Ardent\Builder;
|
||||
* 'Budget[] $budgets
|
||||
* @property-read \Illuminate\Database\Eloquent\Collection|\
|
||||
* '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
|
||||
{
|
||||
@ -153,7 +165,11 @@ class TransactionJournal extends Ardent
|
||||
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)) {
|
||||
$query->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id');
|
||||
|
@ -1,21 +1,10 @@
|
||||
@extends('layouts.default')
|
||||
@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="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">
|
||||
@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
|
||||
@if($what == 'deposit')
|
||||
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')
|
||||
<div class="form-group">
|
||||
<label for="account_id" class="col-sm-4 control-label">
|
||||
@if($what == 'deposit')
|
||||
Receiving account
|
||||
@endif
|
||||
@if($what == 'withdrawal')
|
||||
Paid from account
|
||||
@endif
|
||||
Asset account
|
||||
</label>
|
||||
<div class="col-sm-8">
|
||||
{{Form::select('account_id',$accounts,Input::old('account_id') ?: Input::get('account_id'),['class' => 'form-control'])}}
|
||||
@ -65,23 +49,33 @@
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<!-- SHOW BENEFICIARY (ACCOUNT TO) ONLY FOR WITHDRAWALS AND DEPOSITS -->
|
||||
@if($what == 'deposit' || $what == 'withdrawal')
|
||||
<!-- SHOW EXPENSE ACCOUNT ONLY FOR WITHDRAWALS -->
|
||||
@if($what == 'withdrawal')
|
||||
<div class="form-group">
|
||||
<label for="beneficiary" class="col-sm-4 control-label">
|
||||
@if($what == 'deposit')
|
||||
Paying beneficiary
|
||||
@endif
|
||||
@if($what == 'withdrawal')
|
||||
Beneficiary
|
||||
<label for="expense_account" class="col-sm-4 control-label">Expense account</label>
|
||||
<div class="col-sm-8">
|
||||
<input type="text" name="expense_account" value="{{{Input::old('expense_account')}}}" autocomplete="off" class="form-control" placeholder="Expense account" />
|
||||
@if($errors->has('expense_account'))
|
||||
<p class="text-danger">{{$errors->first('expense_account')}}</p>
|
||||
@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
|
||||
</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>
|
||||
<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'))
|
||||
<p class="text-danger">{{$errors->first('beneficiary')}}</p>
|
||||
<p class="text-danger">{{$errors->first('revenue_account')}}</p>
|
||||
@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
|
||||
</div>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user