mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-16 18:25:00 -06:00
Build some transaction handling.
This commit is contained in:
parent
9e2f7af59b
commit
953d68c3a2
@ -2,7 +2,6 @@
|
||||
|
||||
|
||||
use FireflyIII\Exception\FireflyException;
|
||||
use FireflyIII\Exception\NotImplementedException;
|
||||
use Illuminate\Support\MessageBag;
|
||||
|
||||
/**
|
||||
@ -319,47 +318,51 @@ class TransactionController extends BaseController
|
||||
*/
|
||||
public function update(TransactionJournal $journal)
|
||||
{
|
||||
throw new NotImplementedException;
|
||||
// switch (Input::get('post_submit_action')) {
|
||||
// case 'update':
|
||||
// case 'return_to_edit':
|
||||
// $what = strtolower($journal->transactionType->type);
|
||||
// $messageBag = $this->_helper->update($journal, Input::all());
|
||||
// if ($messageBag->count() == 0) {
|
||||
// // has been saved, return to index:
|
||||
// Session::flash('success', 'Transaction updated!');
|
||||
// Event::fire('journals.update', [$journal]);
|
||||
//
|
||||
// if (Input::get('post_submit_action') == 'return_to_edit') {
|
||||
// return Redirect::route('transactions.edit', $journal->id)->withInput();
|
||||
// } else {
|
||||
// return Redirect::route('transactions.index.' . $what);
|
||||
// }
|
||||
// } else {
|
||||
// Session::flash('error', 'Could not update transaction: ' . $journal->errors()->first());
|
||||
//
|
||||
// return Redirect::route('transactions.edit', $journal->id)->withInput()->withErrors(
|
||||
// $journal->errors()
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// break;
|
||||
// case 'validate_only':
|
||||
// $data = Input::all();
|
||||
// $data['what'] = strtolower($journal->transactionType->type);
|
||||
// $messageBags = $this->_helper->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;
|
||||
// }
|
||||
//
|
||||
/** @var \FireflyIII\Database\TransactionJournal $repos */
|
||||
$repos = App::make('FireflyIII\Database\TransactionJournal');
|
||||
|
||||
$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!');
|
||||
// Event::fire('journals.update', [$journal]);
|
||||
|
||||
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 {
|
||||
Session::flash('error', 'Could not update transaction: ' . $journal->errors()->first());
|
||||
|
||||
return Redirect::route('transactions.edit', $journal->id)->withInput()->withErrors(
|
||||
$journal->errors()
|
||||
);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -192,8 +192,8 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface
|
||||
public function openingBalanceTransaction(\Account $account)
|
||||
{
|
||||
return \TransactionJournal::withRelevantData()->accountIs($account)->leftJoin(
|
||||
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
|
||||
)->where('transaction_types.type', 'Opening balance')->first(['transaction_journals.*']);
|
||||
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
|
||||
)->where('transaction_types.type', 'Opening balance')->first(['transaction_journals.*']);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -469,4 +469,19 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface
|
||||
return \Account::firstOrCreate($data);
|
||||
|
||||
}
|
||||
|
||||
public function firstRevenueAccountOrCreate($name)
|
||||
{
|
||||
/** @var \FireflyIII\Database\AccountType $accountTypeRepos */
|
||||
$accountTypeRepos = \App::make('FireflyIII\Database\AccountType');
|
||||
|
||||
$accountType = $accountTypeRepos->findByWhat('revenue');
|
||||
|
||||
$data = ['user_id' => $this->getUser()->id, 'account_type_id' => $accountType->id, 'name' => $name, 'active' => 1];
|
||||
|
||||
return \Account::firstOrCreate($data);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -92,6 +92,14 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
|
||||
break;
|
||||
case 'opening':
|
||||
break;
|
||||
case 'deposit':
|
||||
$data['to'] = $accountRepository->find($data['account_id']);
|
||||
$data['from'] = $accountRepository->firstRevenueAccountOrCreate($data['revenue_account']);
|
||||
break;
|
||||
case 'transfer':
|
||||
$data['from'] = $accountRepository->find($data['account_from_id']);
|
||||
$data['to'] = $accountRepository->find($data['account_to_id']);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new FireflyException('Cannot save transaction journal with accounts based on $what "' . $data['what'] . '".');
|
||||
@ -118,6 +126,10 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
|
||||
throw new FireflyException($validate['errors']->first());
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO store budget and category.
|
||||
*/
|
||||
|
||||
$journal->completed = 1;
|
||||
$journal->save();
|
||||
|
||||
@ -132,8 +144,81 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
|
||||
*/
|
||||
public function update(Ardent $model, array $data)
|
||||
{
|
||||
// TODO: Implement update() method.
|
||||
throw new NotImplementedException;
|
||||
var_dump($data);
|
||||
/** @var \FireflyIII\Database\TransactionType $typeRepository */
|
||||
$typeRepository = \App::make('FireflyIII\Database\TransactionType');
|
||||
|
||||
/** @var \FireflyIII\Database\Account $accountRepository */
|
||||
$accountRepository = \App::make('FireflyIII\Database\Account');
|
||||
|
||||
/** @var \FireflyIII\Database\TransactionCurrency $currencyRepository */
|
||||
$currencyRepository = \App::make('FireflyIII\Database\TransactionCurrency');
|
||||
|
||||
/** @var \FireflyIII\Database\Transaction $transactionRepository */
|
||||
$transactionRepository = \App::make('FireflyIII\Database\Transaction');
|
||||
|
||||
$journalType = $typeRepository->findByWhat($data['what']);
|
||||
$currency = $currencyRepository->findByCode($data['currency']);
|
||||
|
||||
$model->transactionType()->associate($journalType);
|
||||
$model->transactionCurrency()->associate($currency);
|
||||
$model->user()->associate($this->getUser());
|
||||
$model->description = $data['description'];
|
||||
$model->date = $data['date'];
|
||||
|
||||
/*
|
||||
* This must be enough to store the journal:
|
||||
*/
|
||||
if (!$model->validate()) {
|
||||
\Log::error($model->errors()->all());
|
||||
throw new FireflyException('store() transaction journal failed, but it should not!');
|
||||
}
|
||||
$model->save();
|
||||
|
||||
/*
|
||||
* Still need to find the accounts related to the transactions.
|
||||
* This depends on the type of transaction.
|
||||
*/
|
||||
switch ($data['what']) {
|
||||
case 'withdrawal':
|
||||
$data['from'] = $accountRepository->find($data['account_id']);
|
||||
$data['to'] = $accountRepository->firstExpenseAccountOrCreate($data['expense_account']);
|
||||
break;
|
||||
case 'opening':
|
||||
break;
|
||||
case 'deposit':
|
||||
$data['to'] = $accountRepository->find($data['account_id']);
|
||||
$data['from'] = $accountRepository->firstRevenueAccountOrCreate($data['revenue_account']);
|
||||
break;
|
||||
case 'transfer':
|
||||
$data['from'] = $accountRepository->find($data['account_from_id']);
|
||||
$data['to'] = $accountRepository->find($data['account_to_id']);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new FireflyException('Cannot save transaction journal with accounts based on $what "' . $data['what'] . '".');
|
||||
break;
|
||||
}
|
||||
/*
|
||||
* Now we can update the transactions related to this journal.
|
||||
*/
|
||||
$amount = floatval($data['amount']);
|
||||
/** @var \Transaction $transaction */
|
||||
foreach ($model->transactions()->get() as $transaction) {
|
||||
if (floatval($transaction->amount) > 0) {
|
||||
// the TO transaction.
|
||||
$transaction->account()->associate($data['to']);
|
||||
$transaction->amount = $amount;
|
||||
} else {
|
||||
$transaction->account()->associate($data['from']);
|
||||
$transaction->amount = $amount * -1;
|
||||
}
|
||||
if (!$transaction->validate()) {
|
||||
throw new FireflyException('Could not validate transaction while saving.');
|
||||
}
|
||||
$transaction->save();
|
||||
}
|
||||
return new MessageBag;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -391,8 +476,8 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
|
||||
$sum = \DB::table('transactions')->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')->leftJoin(
|
||||
'transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id'
|
||||
)->where('amount', '>', 0)->where('transaction_types.type', '=', 'Withdrawal')->where('transaction_journals.date', '>=', $date->format('Y-m-d'))->where(
|
||||
'transaction_journals.date', '<=', $end->format('Y-m-d')
|
||||
)->sum('transactions.amount');
|
||||
'transaction_journals.date', '<=', $end->format('Y-m-d')
|
||||
)->sum('transactions.amount');
|
||||
$sum = floatval($sum);
|
||||
|
||||
return $sum;
|
||||
@ -412,8 +497,8 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
|
||||
$sum = \DB::table('transactions')->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')->leftJoin(
|
||||
'transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id'
|
||||
)->where('amount', '>', 0)->where('transaction_types.type', '=', 'Deposit')->where('transaction_journals.date', '>=', $date->format('Y-m-d'))->where(
|
||||
'transaction_journals.date', '<=', $end->format('Y-m-d')
|
||||
)->sum('transactions.amount');
|
||||
'transaction_journals.date', '<=', $end->format('Y-m-d')
|
||||
)->sum('transactions.amount');
|
||||
$sum = floatval($sum);
|
||||
|
||||
return $sum;
|
||||
|
@ -113,6 +113,9 @@ class TransactionType implements TransactionTypeInterface, CUD, CommonDatabaseCa
|
||||
case 'withdrawal':
|
||||
return \TransactionType::whereType('Withdrawal')->first();
|
||||
break;
|
||||
case 'deposit':
|
||||
return \TransactionType::whereType('Deposit')->first();
|
||||
break;
|
||||
default:
|
||||
throw new FireflyException('Cannot find transaction type described as "' . e($what) . '".');
|
||||
break;
|
||||
|
Loading…
Reference in New Issue
Block a user