mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-23 22:35:03 +00:00
Build some transaction handling.
This commit is contained in:
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
|
|
||||||
use FireflyIII\Exception\FireflyException;
|
use FireflyIII\Exception\FireflyException;
|
||||||
use FireflyIII\Exception\NotImplementedException;
|
|
||||||
use Illuminate\Support\MessageBag;
|
use Illuminate\Support\MessageBag;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -319,47 +318,51 @@ class TransactionController extends BaseController
|
|||||||
*/
|
*/
|
||||||
public function update(TransactionJournal $journal)
|
public function update(TransactionJournal $journal)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException;
|
/** @var \FireflyIII\Database\TransactionJournal $repos */
|
||||||
// switch (Input::get('post_submit_action')) {
|
$repos = App::make('FireflyIII\Database\TransactionJournal');
|
||||||
// case 'update':
|
|
||||||
// case 'return_to_edit':
|
$data = Input::except('_token');
|
||||||
// $what = strtolower($journal->transactionType->type);
|
$data['currency'] = 'EUR';
|
||||||
// $messageBag = $this->_helper->update($journal, Input::all());
|
$data['what'] = strtolower($journal->transactionType->type);
|
||||||
// if ($messageBag->count() == 0) {
|
|
||||||
// // has been saved, return to index:
|
|
||||||
// Session::flash('success', 'Transaction updated!');
|
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]);
|
// Event::fire('journals.update', [$journal]);
|
||||||
//
|
|
||||||
// if (Input::get('post_submit_action') == 'return_to_edit') {
|
if (Input::get('post_submit_action') == 'return_to_edit') {
|
||||||
// return Redirect::route('transactions.edit', $journal->id)->withInput();
|
return Redirect::route('transactions.edit', $journal->id)->withInput();
|
||||||
// } else {
|
} else {
|
||||||
// return Redirect::route('transactions.index.' . $what);
|
return Redirect::route('transactions.index', $data['what']);
|
||||||
// }
|
}
|
||||||
// } else {
|
} else {
|
||||||
// Session::flash('error', 'Could not update transaction: ' . $journal->errors()->first());
|
Session::flash('error', 'Could not update transaction: ' . $journal->errors()->first());
|
||||||
//
|
|
||||||
// return Redirect::route('transactions.edit', $journal->id)->withInput()->withErrors(
|
return Redirect::route('transactions.edit', $journal->id)->withInput()->withErrors(
|
||||||
// $journal->errors()
|
$journal->errors()
|
||||||
// );
|
);
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// break;
|
break;
|
||||||
// case 'validate_only':
|
case 'validate_only':
|
||||||
// $data = Input::all();
|
$messageBags = $repos->validate($data);
|
||||||
// $data['what'] = strtolower($journal->transactionType->type);
|
|
||||||
// $messageBags = $this->_helper->validate($data);
|
Session::flash('warnings', $messageBags['warnings']);
|
||||||
//
|
Session::flash('successes', $messageBags['successes']);
|
||||||
// Session::flash('warnings', $messageBags['warnings']);
|
Session::flash('errors', $messageBags['errors']);
|
||||||
// Session::flash('successes', $messageBags['successes']);
|
|
||||||
// Session::flash('errors', $messageBags['errors']);
|
return Redirect::route('transactions.edit', $journal->id)->withInput();
|
||||||
//
|
break;
|
||||||
// return Redirect::route('transactions.edit', $journal->id)->withInput();
|
default:
|
||||||
// break;
|
throw new FireflyException('Method ' . Input::get('post_submit_action') . ' not implemented yet.');
|
||||||
// default:
|
break;
|
||||||
// throw new FireflyException('Method ' . Input::get('post_submit_action') . ' not implemented yet.');
|
}
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -469,4 +469,19 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface
|
|||||||
return \Account::firstOrCreate($data);
|
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;
|
break;
|
||||||
case 'opening':
|
case 'opening':
|
||||||
break;
|
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:
|
default:
|
||||||
throw new FireflyException('Cannot save transaction journal with accounts based on $what "' . $data['what'] . '".');
|
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());
|
throw new FireflyException($validate['errors']->first());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TODO store budget and category.
|
||||||
|
*/
|
||||||
|
|
||||||
$journal->completed = 1;
|
$journal->completed = 1;
|
||||||
$journal->save();
|
$journal->save();
|
||||||
|
|
||||||
@@ -132,8 +144,81 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
|
|||||||
*/
|
*/
|
||||||
public function update(Ardent $model, array $data)
|
public function update(Ardent $model, array $data)
|
||||||
{
|
{
|
||||||
// TODO: Implement update() method.
|
var_dump($data);
|
||||||
throw new NotImplementedException;
|
/** @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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -113,6 +113,9 @@ class TransactionType implements TransactionTypeInterface, CUD, CommonDatabaseCa
|
|||||||
case 'withdrawal':
|
case 'withdrawal':
|
||||||
return \TransactionType::whereType('Withdrawal')->first();
|
return \TransactionType::whereType('Withdrawal')->first();
|
||||||
break;
|
break;
|
||||||
|
case 'deposit':
|
||||||
|
return \TransactionType::whereType('Deposit')->first();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new FireflyException('Cannot find transaction type described as "' . e($what) . '".');
|
throw new FireflyException('Cannot find transaction type described as "' . e($what) . '".');
|
||||||
break;
|
break;
|
||||||
|
Reference in New Issue
Block a user