From 4ec6bcc8c7330965f612eb09b6797723bbcc7014 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 30 Apr 2016 09:48:39 +0200 Subject: [PATCH] More code for split and small bug fix in attachment helper. --- app/Crud/Split/Journal.php | 8 ++- app/Helpers/Attachments/AttachmentHelper.php | 1 + .../Transaction/SplitController.php | 7 ++- app/Http/Requests/JournalFormRequest.php | 2 +- app/Http/Requests/SplitJournalFormRequest.php | 27 +++++---- .../Journal/JournalRepository.php | 2 +- database/seeds/DatabaseSeeder.php | 4 ++ database/seeds/SplitDataSeeder.php | 55 +++++++++++++++++++ resources/lang/en_US/firefly.php | 10 +++- .../views/split/journals/from-store.twig | 37 ++++++++++--- 10 files changed, 126 insertions(+), 27 deletions(-) create mode 100644 database/seeds/SplitDataSeeder.php diff --git a/app/Crud/Split/Journal.php b/app/Crud/Split/Journal.php index 9d742e7ff5..9315ba9193 100644 --- a/app/Crud/Split/Journal.php +++ b/app/Crud/Split/Journal.php @@ -85,6 +85,7 @@ class Journal implements JournalInterface 'account_id' => $sourceAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $transaction['amount'] * -1, + 'description' => $transaction['description'], ] ); @@ -94,6 +95,7 @@ class Journal implements JournalInterface 'account_id' => $destinationAccount->id, 'transaction_journal_id' => $journal->id, 'amount' => $transaction['amount'], + 'description' => $transaction['description'], ] ); @@ -133,8 +135,8 @@ class Journal implements JournalInterface list($sourceAccount, $destinationAccount) = $this->storeDepositAccounts($transaction); break; case TransactionType::TRANSFER: - $sourceAccount = Account::where('user_id', $this->user->id)->where('id', $data['account_from_id'])->first(); - $destinationAccount = Account::where('user_id', $this->user->id)->where('id', $data['account_to_id'])->first(); + $sourceAccount = Account::where('user_id', $this->user->id)->where('id', $transaction['source_account_id'])->first(); + $destinationAccount = Account::where('user_id', $this->user->id)->where('id', $transaction['destination_account_id'])->first(); break; default: throw new FireflyException('Cannot handle ' . e($type)); @@ -150,7 +152,7 @@ class Journal implements JournalInterface */ private function storeDepositAccounts(array $data): array { - $destinationAccount = Account::where('user_id', $this->user->id)->where('id', $data['account_destination_id'])->first(['accounts.*']); + $destinationAccount = Account::where('user_id', $this->user->id)->where('id', $data['destination_account_id'])->first(['accounts.*']); if (strlen($data['source_account_name']) > 0) { $fromType = AccountType::where('type', 'Revenue account')->first(); diff --git a/app/Helpers/Attachments/AttachmentHelper.php b/app/Helpers/Attachments/AttachmentHelper.php index 6fc6e3af0e..3433584545 100644 --- a/app/Helpers/Attachments/AttachmentHelper.php +++ b/app/Helpers/Attachments/AttachmentHelper.php @@ -251,6 +251,7 @@ class AttachmentHelper implements AttachmentHelperInterface $this->processFile($entry, $model); } } + return true; } diff --git a/app/Http/Controllers/Transaction/SplitController.php b/app/Http/Controllers/Transaction/SplitController.php index 6a677f5b61..71eabcd273 100644 --- a/app/Http/Controllers/Transaction/SplitController.php +++ b/app/Http/Controllers/Transaction/SplitController.php @@ -18,6 +18,7 @@ use FireflyIII\Http\Requests\SplitJournalFormRequest; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; +use Log; use Session; /** @@ -40,8 +41,9 @@ class SplitController extends Controller /** @var BudgetRepositoryInterface $budgetRepository */ $budgetRepository = app(BudgetRepositoryInterface::class); + // expect data to be in session or in post? - $journalData = session('temporary_split_data'); + $journalData = session('temporary_split_data'); $currencies = ExpandedForm::makeSelectList($currencyRepository->get()); $assetAccounts = ExpandedForm::makeSelectList($accountRepository->getAccounts(['Default account', 'Asset account'])); $budgets = ExpandedForm::makeSelectListWithEmpty($budgetRepository->getActiveBudgets()); @@ -49,6 +51,9 @@ class SplitController extends Controller throw new FireflyException('Could not find transaction data in your session. Please go back and try again.'); // translate me. } + Log::debug('Journal data', $journalData); + + return view('split.journals.from-store', compact('currencies', 'assetAccounts', 'budgets'))->with('data', $journalData); diff --git a/app/Http/Requests/JournalFormRequest.php b/app/Http/Requests/JournalFormRequest.php index 5b5a7905d7..8b8886b76b 100644 --- a/app/Http/Requests/JournalFormRequest.php +++ b/app/Http/Requests/JournalFormRequest.php @@ -39,7 +39,7 @@ class JournalFormRequest extends Request 'description' => $this->get('description'), 'source_account_id' => intval($this->get('source_account_id')), 'source_account_name' => $this->get('source_account_name') ?? '', - 'account_destination_id' => intval($this->get('account_destination_id')), + 'destination_account_id' => intval($this->get('destination_account_id')), 'destination_account_name' => $this->get('destination_account_name') ?? '', 'amount' => round($this->get('amount'), 2), 'user' => Auth::user()->id, diff --git a/app/Http/Requests/SplitJournalFormRequest.php b/app/Http/Requests/SplitJournalFormRequest.php index 3cb66f26f8..fc16ae7f87 100644 --- a/app/Http/Requests/SplitJournalFormRequest.php +++ b/app/Http/Requests/SplitJournalFormRequest.php @@ -35,25 +35,28 @@ class SplitJournalFormRequest extends Request public function getSplitData(): array { $data = [ - 'description' => $this->get('journal_description'), - 'currency_id' => intval($this->get('currency')), - 'source_account_id' => intval($this->get('source_account_id')), - 'date' => new Carbon($this->get('date')), - 'what' => $this->get('what'), - 'interest_date' => $this->get('interest_date') ? new Carbon($this->get('interest_date')) : null, - 'book_date' => $this->get('book_date') ? new Carbon($this->get('book_date')) : null, - 'process_date' => $this->get('process_date') ? new Carbon($this->get('process_date')) : null, - 'transactions' => [], + 'description' => $this->get('journal_description'), + 'currency_id' => intval($this->get('currency')), + 'source_account_id' => intval($this->get('source_account_id')), + 'source_account_name' => $this->get('source_account_name'), + 'date' => new Carbon($this->get('date')), + 'what' => $this->get('what'), + 'interest_date' => $this->get('interest_date') ? new Carbon($this->get('interest_date')) : null, + 'book_date' => $this->get('book_date') ? new Carbon($this->get('book_date')) : null, + 'process_date' => $this->get('process_date') ? new Carbon($this->get('process_date')) : null, + 'transactions' => [], ]; // description is leading because it is one of the mandatory fields. foreach ($this->get('description') as $index => $description) { $transaction = [ 'description' => $description, 'amount' => round($this->get('amount')[$index], 2), - 'budget_id' => $this->get('budget')[$index] ? $this->get('budget')[$index] : 0, + 'budget_id' => $this->get('budget')[$index] ? intval($this->get('budget')[$index]) : 0, 'category' => $this->get('category')[$index] ?? '', 'source_account_id' => intval($this->get('source_account_id')), - 'destination_account_name' => $this->get('destination_account_name')[$index] ?? '' + 'source_account_name' => $this->get('source_account_name'), + 'destination_account_id' => $this->get('destination_account_id')[$index] ? intval($this->get('destination_account_id')[$index]) : 0, + 'destination_account_name' => $this->get('destination_account_name')[$index] ?? '', ]; $data['transactions'][] = $transaction; } @@ -70,12 +73,14 @@ class SplitJournalFormRequest extends Request 'journal_description' => 'required|between:1,255', 'currency' => 'required|exists:transaction_currencies,id', 'source_account_id' => 'numeric|belongsToUser:accounts,id', + 'source_account_name.*' => 'between:1,255', 'what' => 'required|in:withdrawal,deposit,transfer', 'date' => 'required|date', 'interest_date' => 'date', 'book_date' => 'date', 'process_date' => 'date', 'description.*' => 'required|between:1,255', + 'destination_account_id.*' => 'numeric|belongsToUser:accounts,id', 'destination_account_name.*' => 'between:1,255', 'amount.*' => 'required|numeric', 'budget.*' => 'belongsToUser:budgets,id', diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php index c539eee3bc..7e17636398 100644 --- a/app/Repositories/Journal/JournalRepository.php +++ b/app/Repositories/Journal/JournalRepository.php @@ -431,7 +431,7 @@ class JournalRepository implements JournalRepositoryInterface */ protected function storeDepositAccounts(array $data): array { - $destinationAccount = Account::where('user_id', $this->user->id)->where('id', $data['account_destination_id'])->first(['accounts.*']); + $destinationAccount = Account::where('user_id', $this->user->id)->where('id', $data['destination_account_id'])->first(['accounts.*']); if (strlen($data['source_account_name']) > 0) { $fromType = AccountType::where('type', 'Revenue account')->first(); diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index bf4ec69932..45131e047b 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -26,6 +26,10 @@ class DatabaseSeeder extends Seeder if (App::environment() == 'testing' || App::environment() == 'local') { $this->call('TestDataSeeder'); } + // set up basic test data (as little as possible): + if (App::environment() == 'split') { + $this->call('SplitDataSeeder'); + } } } diff --git a/database/seeds/SplitDataSeeder.php b/database/seeds/SplitDataSeeder.php new file mode 100644 index 0000000000..623ca15178 --- /dev/null +++ b/database/seeds/SplitDataSeeder.php @@ -0,0 +1,55 @@ + 'It means that the amount of money you\'ve spent is divided between several destination expense accounts, budgets or categories.', 'split_intro_three_withdrawal' => 'For example: you could split your :total groceries so you pay :split_one from your "daily groceries" budget and :split_two from your "cigarettes" budget.', 'split_table_intro_withdrawal' => 'Split your withdrawal in as many things as you want. By default the transaction will not split, there is just one entry. Add as many splits as you want to, below. Remember that you should not deviate from your total amount. If you do, Firefly will warn you but not correct you.', - 'add_another_split' => 'Add another split', 'store_splitted_withdrawal' => 'Store splitted withdrawal', + + 'split_title_deposit' => 'Split your new deposit', + 'split_intro_one_deposit' => 'Firefly supports the "splitting" of a deposit.', + 'split_intro_two_deposit' => 'It means that the amount of money you\'ve earned is divided between several source revenue accounts or categories.', + 'split_intro_three_deposit' => 'For example: you could split your :total salary so you get :split_one as your base salary and :split_two as a reimbursment for expenses made.', + 'split_table_intro_deposit' => 'Split your deposit in as many things as you want. By default the transaction will not split, there is just one entry. Add as many splits as you want to, below. Remember that you should not deviate from your total amount. If you do, Firefly will warn you but not correct you.', + 'store_splitted_deposit' => 'Store splitted deposit', + 'add_another_split' => 'Add another split', + ]; diff --git a/resources/views/split/journals/from-store.twig b/resources/views/split/journals/from-store.twig index 8226468056..12cf3aad19 100644 --- a/resources/views/split/journals/from-store.twig +++ b/resources/views/split/journals/from-store.twig @@ -24,7 +24,11 @@ {{ ('split_intro_two_'~data.what)|_ }}

- {{ trans(('firefly.split_intro_three_'~data.what), {total: 20|formatAmount, split_one: 15|formatAmount, split_two: 5|formatAmount})|raw }} + {% if data.what =='deposit' %} + {{ trans(('firefly.split_intro_three_'~data.what), {total: 500|formatAmount, split_one: 425|formatAmount, split_two: 75|formatAmount})|raw }} + {% else %} + {{ trans(('firefly.split_intro_three_'~data.what), {total: 20|formatAmount, split_one: 15|formatAmount, split_two: 5|formatAmount})|raw }} + {% endif %}

- {% if data.what == 'deposit' or data.what == 'transfer' %} + + {% if data.what == 'deposit' %} + {{ ExpandedForm.staticText('revenue_account', data.source_account_name) }} + + {% endif %} + + {% if data.what == 'transfer' %} {{ ExpandedForm.staticText('asset_destination_account', assetAccounts[data.destination_account_id]) }} {% endif %} @@ -113,11 +122,14 @@ {{ trans('list.split_number') }} {{ trans('list.description') }} + - {% if data.what == 'withdrawal' %} + + {% if data.what == 'withdrawal' or data.what == 'deposit' %} {{ trans('list.destination') }} {% endif %} {{ trans('list.amount') }} @@ -134,16 +146,23 @@ #1 {{ Form.input('text', 'description[]', data.description, {class: 'form-control'}) }} - {% if data.what == 'deposit' %} - - {{ Form.input('text', 'source_account_name[]', data.source_account_name, {class: 'form-control'}) }} - - {% endif %} + + {% if data.what == 'withdrawal' %} {{ Form.input('text', 'destination_account_name[]', data.destination_account_name, {class: 'form-control'}) }} {% endif %} + + + {% if data.what == 'deposit' %} + + {{ Form.select('destination_account_id[]', assetAccounts, data.destination_account_id, {class: 'form-control'}) }} + + {% endif %} + + + {{ Form.input('number', 'amount[]', data.amount, {class: 'form-control', autocomplete: 'off', step: 'any', min:'0.01'}) }}