First attempt at #142. Needs a lot of work still.

This commit is contained in:
James Cole
2016-04-29 20:59:28 +02:00
parent 4af8272faa
commit 0e3ccebd0b
19 changed files with 714 additions and 155 deletions

View File

@@ -11,11 +11,14 @@ namespace FireflyIII\Http\Controllers\Transaction;
use ExpandedForm;
use FireflyIII\Crud\Split\JournalInterface;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\SplitJournalFormRequest;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use Session;
/**
* Class SplitController
@@ -39,26 +42,45 @@ class SplitController extends Controller
// expect data to be in session or in post?
$journalData = session('temporary_split_data');
$currency = $currencyRepository->find(intval($journalData['amount_currency_id_amount']));
$currencies = ExpandedForm::makeSelectList($currencyRepository->get());
$assetAccounts = ExpandedForm::makeSelectList($accountRepository->getAccounts(['Default account', 'Asset account']));
$budgets = ExpandedForm::makeSelectListWithEmpty($budgetRepository->getActiveBudgets());
if (!is_array($journalData)) {
throw new FireflyException('Could not find transaction data in your session. Please go back and try again.'); // translate me.
}
// echo '<pre>';
// var_dump($journalData);
// echo '</pre>';
// exit;
return view('split.journals.from-store', compact('currency', 'assetAccounts', 'budgets'))->with('data', $journalData);
return view('split.journals.from-store', compact('currencies', 'assetAccounts', 'budgets'))->with('data', $journalData);
}
public function postJournalFromStore()
/**
* @param SplitJournalFormRequest $request
* @param JournalInterface $repository
*
* @return mixed
*/
public function postJournalFromStore(SplitJournalFormRequest $request, JournalInterface $repository)
{
$data = $request->getSplitData();
// store an empty journal first. This will be the place holder.
$journal = $repository->storeJournal($data);
// Then, store each transaction individually.
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');
Session::forget('temporary_split_data');
// this is where we originally came from.
return redirect(session('transactions.create.url'));
}
}

View File

@@ -0,0 +1,85 @@
<?php
/**
* SplitJournalFormRequest.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Http\Requests;
use Auth;
use Carbon\Carbon;
/**
* Class SplitJournalFormRequest
*
* @package FireflyIII\Http\Requests
*/
class SplitJournalFormRequest extends Request
{
/**
* @return bool
*/
public function authorize(): bool
{
// Only allow logged in users
return Auth::check();
}
/**
* @return array
*/
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 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,
'category' => $this->get('category')[$index] ?? '',
'source_account_id' => intval($this->get('source_account_id')),
'destination_account_name' => $this->get('destination_account_name')[$index] ?? ''
];
$data['transactions'][] = $transaction;
}
return $data;
}
/**
* @return array
*/
public function rules(): array
{
return [
'journal_description' => 'required|between:1,255',
'currency' => 'required|exists:transaction_currencies,id',
'source_account_id' => 'numeric|belongsToUser:accounts,id',
'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_name.*' => 'between:1,255',
'amount.*' => 'required|numeric',
'budget.*' => 'belongsToUser:budgets,id',
'category.*' => 'between:1,255',
];
}
}

View File

@@ -346,6 +346,7 @@ Route::group(
* Split controller
*/
Route::get('/transaction/split', ['uses' => 'Transaction\SplitController@journalFromStore', 'as' => 'split.journal.from-store']);
Route::post('/transaction/split', ['uses' => 'Transaction\SplitController@postJournalFromStore', 'as' => 'split.journal.from-store.post']);
/**
* Tag Controller