More stuff for splits.

This commit is contained in:
James Cole 2016-04-30 19:50:42 +02:00
parent c05c6e72c0
commit 9c5292962f
8 changed files with 37 additions and 17 deletions

View File

@ -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 FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use Log;
use Session;
@ -41,12 +42,15 @@ class SplitController extends Controller
/** @var BudgetRepositoryInterface $budgetRepository */
$budgetRepository = app(BudgetRepositoryInterface::class);
/** @var PiggyBankRepositoryInterface $piggyBankRepository */
$piggyBankRepository = app(PiggyBankRepositoryInterface::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());
$piggyBanks = ExpandedForm::makeSelectListWithEmpty($piggyBankRepository->getPiggyBanks());
if (!is_array($journalData)) {
throw new FireflyException('Could not find transaction data in your session. Please go back and try again.'); // translate me.
}
@ -54,7 +58,7 @@ class SplitController extends Controller
Log::debug('Journal data', $journalData);
return view('split.journals.from-store', compact('currencies', 'assetAccounts', 'budgets'))->with('data', $journalData);
return view('split.journals.from-store', compact('currencies', 'piggyBanks', 'assetAccounts', 'budgets'))->with('data', $journalData);
}

View File

@ -55,7 +55,9 @@ class SplitJournalFormRequest extends Request
'category' => $this->get('category')[$index] ?? '',
'source_account_id' => intval($this->get('source_account_id')),
'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_id' => isset($this->get('destination_account_id')[$index])
? intval($this->get('destination_account_id')[$index])
: intval($this->get('destination_account_id')),
'destination_account_name' => $this->get('destination_account_name')[$index] ?? '',
];
$data['transactions'][] = $transaction;

View File

@ -20,7 +20,6 @@ use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\CacheProperties;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Log;
/**
* Class TransactionJournalSupport
@ -47,27 +46,24 @@ class TransactionJournalSupport extends Model
}
if ($journal->isWithdrawal() && !is_null($journal->source_amount)) {
$cache->store($journal->source_amount);
return $journal->source_amount;
}
if ($journal->isDeposit() && !is_null($journal->destination_amount)) {
$cache->store($journal->destination_amount);
return $journal->destination_amount;
}
// breaks if > 2
$transaction = $journal->transactions->sortByDesc('amount')->first();
if (is_null($transaction)) {
Log::error('Transaction journal #' . $journal->id . ' has ZERO transactions (or they have been deleted).');
throw new FireflyException('Transaction journal #' . $journal->id . ' has ZERO transactions. Visit this page for a solution: https://git.io/vwPFY');
}
$amount = $transaction->amount;
if ($journal->isWithdrawal()) {
$amount = bcmul($amount, '-1');
$amount = $journal->transactions()->where('amount', '>', 0)->get()->sum('amount');
if ($journal->isDeposit()) {
$amount = $amount * -1;
}
$amount = strval($amount);
$cache->store($amount);
return $amount;
}
/**
@ -250,7 +246,7 @@ class TransactionJournalSupport extends Model
'source_account.id AS source_account_id',
'source_account.name AS source_account_name',
'source_acct_type.type AS source_account_type',
DB::raw('COUNT(`destination`.`id`) + COUNT(`source`.`id`) as `count_transactions`')
DB::raw('COUNT(`destination`.`id`) + COUNT(`source`.`id`) as `count_transactions`'),
];
}

View File

@ -51,5 +51,6 @@ class SplitDataSeeder extends Seeder
TestData::createCategories($user);
TestData::createExpenseAccounts($user);
TestData::createRevenueAccounts($user);
TestData::createPiggybanks($user);
}
}

View File

@ -818,6 +818,15 @@ return [
'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',
'split_title_transfer' => 'Split your new transfer',
'split_intro_one_transfer' => 'Firefly supports the "splitting" of a transfer.',
'split_intro_two_transfer' => 'It means that the amount of money you\'re moving is divided between several categories or piggy banks.',
'split_intro_three_transfer' => 'For example: you could split your :total move so you get :split_one in one piggy bank and :split_two in another.',
'split_table_intro_transfer' => 'Split your transfer 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_transfer' => 'Store splitted transfer',
'add_another_split' => 'Add another split',
];

View File

@ -27,6 +27,8 @@ return [
'asset_destination_account' => 'Asset account (destination)',
'asset_source_account' => 'Asset account (source)',
'journal_description' => 'Description',
'split_journal' => 'Split this transaction',
'split_journal_explanation' => 'Split this transaction in multiple parts',
'currency' => 'Currency',
'account_id' => 'Asset account',
'budget_id' => 'Budget',

View File

@ -36,6 +36,7 @@ return [
'book_date' => 'Book date',
'process_date' => 'Processing date',
'from' => 'From',
'piggy_bank' => 'Piggy bank',
'to' => 'To',
'budget' => 'Budget',
'category' => 'Category',

View File

@ -174,6 +174,11 @@
<td>
{{ Form.input('text', 'category[]', data.category, {class: 'form-control'}) }}
</td>
{% if data.what == 'transfer' %}
<td>
{{ Form.select('piggy_bank_id[]', piggyBanks, data.piggy_bank_id, {class: 'form-control'}) }}
</td>
{% endif %}
</tr>
</tbody>
</table>