This should just about finished split transaction editing.

This commit is contained in:
James Cole 2016-05-13 07:33:04 +02:00
parent d7ab482ae1
commit 2d8449ed68
7 changed files with 165 additions and 37 deletions

View File

@ -64,6 +64,13 @@ class Journal implements JournalInterface
);
$journal->save();
foreach ($data['transactions'] as $transaction) {
$this->storeTransaction($journal, $transaction);
}
$journal->completed = true;
$journal->save();
return $journal;
}
@ -116,6 +123,38 @@ class Journal implements JournalInterface
}
/**
* @param TransactionJournal $journal
* @param array $data
*
* @return TransactionJournal
*/
public function updateJournal(TransactionJournal $journal, array $data): TransactionJournal
{
echo '<pre>';
print_r($data);
$journal->description = $data['journal_description'];
$journal->transaction_currency_id = $data['journal_currency_id'];
$journal->date = $data['date'];
$journal->interest_date = $data['interest_date'];
$journal->book_date = $data['book_date'];
$journal->process_date = $data['process_date'];
$journal->save();
// delete original transactions, and recreate them.
$journal->transactions()->delete();
foreach ($data['transactions'] as $transaction) {
$this->storeTransaction($journal, $transaction);
}
$journal->completed = true;
$journal->save();
return $journal;
}
/**
* @param string $type
* @param array $transaction

View File

@ -36,4 +36,12 @@ interface JournalInterface
* @return Collection
*/
public function storeTransaction(TransactionJournal $journal, array $transaction): Collection;
/**
* @param TransactionJournal $journal
* @param array $data
*
* @return TransactionJournal
*/
public function updateJournal(TransactionJournal $journal, array $data): TransactionJournal;
}

View File

@ -12,6 +12,7 @@ namespace FireflyIII\Http\Controllers\Transaction;
use ExpandedForm;
use FireflyIII\Crud\Split\JournalInterface;
use FireflyIII\Events\TransactionJournalUpdated;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\SplitJournalFormRequest;
@ -21,8 +22,12 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use Illuminate\Http\Request;
use Input;
use Log;
use Preferences;
use Session;
use Steam;
use URL;
use View;
/**
@ -63,16 +68,24 @@ class SplitController extends Controller
/** @var BudgetRepositoryInterface $budgetRepository */
$budgetRepository = app(BudgetRepositoryInterface::class);
$uploadSize = min(Steam::phpBytes(ini_get('upload_max_filesize')), Steam::phpBytes(ini_get('post_max_size')));
$currencies = ExpandedForm::makeSelectList($currencyRepository->get());
$assetAccounts = ExpandedForm::makeSelectList($accountRepository->getAccounts(['Default account', 'Asset account']));
$budgets = ExpandedForm::makeSelectListWithEmpty($budgetRepository->getActiveBudgets());
$preFilled = $this->arrayFromJournal($request, $journal);
// get the transactions:
Session::flash('gaEventCategory', 'transactions');
Session::flash('gaEventAction', 'edit-split-' . $preFilled['what']);
// put previous url in session if not redirect from store (not "return_to_edit").
if (session('transactions.edit-split.fromUpdate') !== true) {
Session::put('transactions.edit-split.url', URL::previous());
}
Session::forget('transactions.edit-split.fromUpdate');
return view(
'split.journals.edit',
compact('currencies', 'preFilled', 'amount', 'sourceAccounts', 'destinationAccounts', 'assetAccounts', 'budgets', 'what', 'journal')
compact('currencies', 'preFilled', 'amount', 'sourceAccounts', 'uploadSize', 'destinationAccounts', 'assetAccounts', 'budgets', 'journal')
);
}
@ -105,7 +118,6 @@ class SplitController extends Controller
$assetAccounts = ExpandedForm::makeSelectList($accountRepository->getAccounts(['Default account', 'Asset account']));
$budgets = ExpandedForm::makeSelectListWithEmpty($budgetRepository->getActiveBudgets());
return view('split.journals.from-store', compact('currencies', 'assetAccounts', 'budgets', 'preFilled'));
@ -129,13 +141,6 @@ class SplitController extends Controller
if (is_null($journal->id)) {
throw new FireflyException('Could not store transaction.');
}
foreach ($data['transactions'] as $transaction) {
$transactions = $repository->storeTransaction($journal, $transaction);
}
// TODO move to repository.
$journal->completed = true;
$journal->save();
// forget temp journal data
Session::forget('temporary_split_data');
@ -145,12 +150,38 @@ class SplitController extends Controller
}
/**
* @param TransactionJournal $journal
* @param SplitJournalFormRequest $request
* @param JournalInterface $repository
*
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function update(SplitJournalFormRequest $request, JournalInterface $repository)
public function update(TransactionJournal $journal, SplitJournalFormRequest $request, JournalInterface $repository)
{
echo 'ok';
$data = $request->getSplitData();
$journal = $repository->updateJournal($journal, $data);
event(new TransactionJournalUpdated($journal));
// update, get events by date and sort DESC
$type = strtolower($journal->transaction_type_type ?? TransactionJournal::transactionTypeStr($journal));
Session::flash('success', strval(trans('firefly.updated_' . $type, ['description' => e($data['journal_description'])])));
Preferences::mark();
if (intval(Input::get('return_to_edit')) === 1) {
// set value so edit routine will not overwrite URL:
Session::put('transactions.edit-split.fromUpdate', true);
return redirect(route('split.journal.edit', [$journal->id]))->withInput(['return_to_edit' => 1]);
}
// redirect to previous URL.
return redirect(session('transactions.edit-split.url'));
// update all:
}
/**
@ -187,25 +218,37 @@ class SplitController extends Controller
$index = 0;
/** @var Transaction $transaction */
foreach ($journal->transactions()->get() as $transaction) {
//if ($journal->isWithdrawal() && $transaction->account_id !== $firstSourceId) {
$array['description'][] = $transaction->description;
$array['destination_account_id'][] = $transaction->account_id;
$array['destination_account_name'][] = $transaction->account->name;
$array['amount'][] = $transaction->amount;
//}
$budget = $transaction->budgets()->first();
$category = $transaction->categories()->first();
$budgetId = 0;
$categoryName = '';
if (!is_null($budget)) {
$budgetId = $budget->id;
}
$category = $transaction->categories()->first();
$categoryName = '';
if (!is_null($category)) {
$categoryName = $category->name;
}
$array['budget_id'][] = $budgetId;
$budgetId = $request->old('budget_id')[$index] ?? $budgetId;
$categoryName = $request->old('category')[$index] ?? $categoryName;
$amount = $request->old('amount')[$index] ?? $transaction->amount;
$description = $request->old('description')[$index] ?? $transaction->description;
$destinationName = $request->old('destination_account_name')[$index] ??$transaction->account->name;
if ($journal->isWithdrawal() && $transaction->account_id !== $firstSourceId) {
$array['description'][] = $description;
$array['destination_account_id'][] = $transaction->account_id;
$array['destination_account_name'][] = $destinationName;
$array['amount'][] = $amount;
$array['budget_id'][] = intval($budgetId);
$array['category'][] = $categoryName;
// only add one when "valid" transaction
$index++;
}
}
return $array;

View File

@ -35,6 +35,7 @@ class SplitJournalFormRequest extends Request
public function getSplitData(): array
{
$data = [
'id' => $this->get('id') ?? 0,
'journal_description' => $this->get('journal_description'),
'journal_currency_id' => intval($this->get('journal_currency_id')),
'journal_source_account_id' => intval($this->get('journal_source_account_id')),

View File

@ -25,6 +25,7 @@ return [
'journal_currency_id' => 'Currency',
'journal_amount' => 'Amount',
'journal_asset_source_account' => 'Asset account (source)',
'journal_source_account_id' => 'Asset account (source)',
'account_from_id' => 'From account',
'account_to_id' => 'To account',
'asset_destination_account' => 'Asset account (destination)',

View File

@ -46,19 +46,17 @@
{{ ExpandedForm.select('journal_currency_id', currencies, preFilled.transaction_currency_id) }}
{{ ExpandedForm.staticText('journal_amount', preFilled.journal_amount|formatAmount ) }}
<input type="hidden" name="journal_amount" value="{{ preFilled.journal_amount }}"/>
<!-- show static source if withdrawal or transfer -->
{% if what == 'withdrawal' or what == 'transfer' %}
{{ ExpandedForm.select('journal_asset_source_account_id', assetAccounts, preFilled.sourceAccounts.first.id) }}
<!-- show source if withdrawal or transfer -->
{% if preFilled.what == 'withdrawal' or preFilled.what == 'transfer' %}
{{ ExpandedForm.select('journal_source_account_id', assetAccounts, preFilled.journal_source_account_id) }}
{% endif %}
<!-- show static source if deposit: -->
{% if what == 'deposit' %}
{{ ExpandedForm.staticText('revenue_account', sourceAccounts.first.name) }}
<input type="hidden" name="journal_source_account_name" value="{{ preFilled.sourceAccounts.first.name }}"/>
{% if preFilled.what == 'deposit' %}
{{ ExpandedForm.text('journal_source_account_name', preFilled.journal_source_account_name) }}
{% endif %}
<!-- show static destination if transfer -->
{% if what == 'transfer' %}
{{ ExpandedForm.staticText('asset_destination_account', assetAccounts[preFilled.destinationAccounts.first.id]) }}
<input type="hidden" name="journal_destination_account_id" value="{{ preFilled.destinationAccounts.first.id }}"/>
{% if preFilled.what == 'transfer' %}
{{ ExpandedForm.select('journal_destination_account_id', assetAccounts, preFilled.journal_destination_account_id) }}
{% endif %}
</div>
</div>
@ -82,6 +80,7 @@
{{ ExpandedForm.date('process_date', journal.process_date) }}
</div>
</div>
</div>
</div>
<div class="row">
@ -151,7 +150,15 @@
</td>
{% if preFilled.what == 'withdrawal' %}
<td>
{{ Form.select('budget_id[]', budgets, preFilled.budget_id[index], {class: 'form-control'}) }}
<select class="form-control" name="budget_id[]">
{% for key, budget in budgets %}
<option label="{{ budget }}" value="{{ key }}"
{% if preFilled.budget_id[index] == key %}
selected="selected"
{% endif %}
>{{ budget }}</option>
{% endfor %}
</select>
</td>
{% endif %}
<td>
@ -170,11 +177,40 @@
<br/>
<a href="#" class="btn btn-default btn-do-split"><i class="fa fa-plus-circle"></i> {{ 'add_another_split'|_ }}</a>
</p>
<p class="pull-right">
<!--<p class="pull-right">
<button type="submit" id="transaction-btn" class="btn btn-success pull-right">
{{ ('update_splitted_'~preFilled.what)|_ }}
</button>
</p>
-->
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'optionalFields'|_ }}</h3>
</div>
<div class="box-body">
<!-- ATTACHMENTS -->
{{ ExpandedForm.file('attachments[]', {'multiple': 'multiple','helpText': trans('firefly.upload_max_file_size', {'size': uploadSize|filesize}) }) }}
</div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<!-- panel for options -->
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'options'|_ }}</h3>
</div>
<div class="box-body">
{{ ExpandedForm.optionsList('update','split-transaction') }}
</div>
<div class="box-footer">
<button type="submit" class="pull-right btn btn-success">{{ ('update_' ~ preFilled.what)|_ }}</button>
</div>
</div>
</div>

View File

@ -74,7 +74,7 @@
<!-- show source drop down box if withdrawal or transfer -->
{% if preFilled.what == 'withdrawal' or preFilled.what == 'transfer' %}
{{ ExpandedForm.select('journal_asset_source_account_id', assetAccounts, preFilled.journal_source_account_id) }}
{{ ExpandedForm.select('journal_source_account_id', assetAccounts, preFilled.journal_source_account_id) }}
{% endif %}
<!-- show source text input if deposit: -->
{% if preFilled.what == 'deposit' %}