Cleanup transaction journal edit. #215

This commit is contained in:
James Cole
2016-03-20 17:12:34 +01:00
parent cc26ce4143
commit 201790ff8d
9 changed files with 116 additions and 59 deletions

View File

@@ -102,7 +102,7 @@ class BalanceLine
return $this->getBudget()->name; return $this->getBudget()->name;
} }
if ($this->getRole() == self::ROLE_DEFAULTROLE) { if ($this->getRole() == self::ROLE_DEFAULTROLE) {
return trans('firefly.noBudget'); return trans('firefly.no_budget');
} }
if ($this->getRole() == self::ROLE_TAGROLE) { if ($this->getRole() == self::ROLE_TAGROLE) {
return trans('firefly.coveredWithTags'); return trans('firefly.coveredWithTags');

View File

@@ -198,7 +198,7 @@ class BudgetController extends Controller
} }
$noBudgetExpenses = $repository->getWithoutBudgetSum($accounts, $start, $end); $noBudgetExpenses = $repository->getWithoutBudgetSum($accounts, $start, $end);
$allEntries->push([trans('firefly.noBudget'), '0', '0', $noBudgetExpenses, '0', '0']); $allEntries->push([trans('firefly.no_budget'), '0', '0', $noBudgetExpenses, '0', '0']);
$data = $this->generator->frontpage($allEntries); $data = $this->generator->frontpage($allEntries);
$cache->store($data); $cache->store($data);
@@ -258,7 +258,7 @@ class BudgetController extends Controller
// basic information: // basic information:
$year = $currentStart->year; $year = $currentStart->year;
$entry['name'] = $budget->name ?? (string)trans('firefly.noBudget'); $entry['name'] = $budget->name ?? (string)trans('firefly.no_budget');
$spent = 0; $spent = 0;
// this might be a good moment to collect no budget stuff. // this might be a good moment to collect no budget stuff.
if (is_null($budget->id)) { if (is_null($budget->id)) {

View File

@@ -7,7 +7,6 @@ use Config;
use ExpandedForm; use ExpandedForm;
use FireflyIII\Events\TransactionJournalStored; use FireflyIII\Events\TransactionJournalStored;
use FireflyIII\Events\TransactionJournalUpdated; use FireflyIII\Events\TransactionJournalUpdated;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface; use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Http\Requests\JournalFormRequest; use FireflyIII\Http\Requests\JournalFormRequest;
use FireflyIII\Models\PiggyBank; use FireflyIII\Models\PiggyBank;
@@ -16,7 +15,9 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Input; use Input;
use Preferences; use Preferences;
@@ -57,7 +58,7 @@ class TransactionController extends Controller
$uploadSize = min($maxFileSize, $maxPostSize); $uploadSize = min($maxFileSize, $maxPostSize);
$accounts = ExpandedForm::makeSelectList($repository->getAccounts(['Default account', 'Asset account'])); $accounts = ExpandedForm::makeSelectList($repository->getAccounts(['Default account', 'Asset account']));
$budgets = ExpandedForm::makeSelectList(Auth::user()->budgets()->get()); $budgets = ExpandedForm::makeSelectList(Auth::user()->budgets()->get());
$budgets[0] = trans('form.noBudget'); $budgets[0] = trans('firefly.no_budget');
$piggyBanks = Auth::user()->piggyBanks()->orderBy('order', 'ASC')->get(); $piggyBanks = Auth::user()->piggyBanks()->orderBy('order', 'ASC')->get();
/** @var PiggyBank $piggy */ /** @var PiggyBank $piggy */
foreach ($piggyBanks as $piggy) { foreach ($piggyBanks as $piggy) {
@@ -131,53 +132,45 @@ class TransactionController extends Controller
} }
/** /**
* Shows the view to edit a transaction.
*
* @param ARI $repository
* @param TransactionJournal $journal * @param TransactionJournal $journal
* *
* @return $this * @return mixed
* @throws FireflyException
*/ */
public function edit(ARI $repository, TransactionJournal $journal) public function edit(TransactionJournal $journal)
{ {
$maxFileSize = Steam::phpBytes(ini_get('upload_max_filesize')); /** @var ARI $accountRepository */
$maxPostSize = Steam::phpBytes(ini_get('post_max_size')); $accountRepository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');
$uploadSize = min($maxFileSize, $maxPostSize); /** @var BudgetRepositoryInterface $budgetRepository */
$what = strtolower(TransactionJournal::transactionTypeStr($journal)); $budgetRepository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
$accounts = ExpandedForm::makeSelectList($repository->getAccounts(['Default account', 'Asset account'])); /** @var PiggyBankRepositoryInterface $piggyRepository */
$budgets = ExpandedForm::makeSelectList(Auth::user()->budgets()->get()); $piggyRepository = app('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface');
$budgets[0] = trans('form.noBudget');
$piggies = ExpandedForm::makeSelectList(Auth::user()->piggyBanks()->get()); $accountList = ExpandedForm::makeSelectList($accountRepository->getAccounts(['Default account', 'Asset account']));
$piggies[0] = trans('form.noPiggybank'); $budgetList = ExpandedForm::makeSelectList($budgetRepository->getActiveBudgets());
$subTitle = trans('breadcrumbs.edit_journal', ['description' => $journal->description]); $piggyBankList = ExpandedForm::makeSelectList($piggyRepository->getPiggyBanks());
$preFilled = [ $budgetList[0] = trans('firefly.no_budget');
'date' => $journal->date->format('Y-m-d'), $piggyBankList[0] = trans('form.noPiggybank');
'interest_date' => $journal->interest_date ? $journal->interest_date->format('Y-m-d') : '', $maxFileSize = Steam::phpBytes(ini_get('upload_max_filesize'));
'book_date' => $journal->book_date ? $journal->book_date->format('Y-m-d') : '', $maxPostSize = Steam::phpBytes(ini_get('post_max_size'));
'process_date' => $journal->process_date ? $journal->process_date->format('Y-m-d') : '', $uploadSize = min($maxFileSize, $maxPostSize);
'category' => '', $what = strtolower(TransactionJournal::transactionTypeStr($journal));
'budget_id' => 0, $subTitle = trans('breadcrumbs.edit_journal', ['description' => $journal->description]);
'piggy_bank_id' => 0,
'tags' => join(',', $journal->tags->pluck('tag')->toArray()),
$preFilled = [
'date' => TransactionJournal::dateAsString($journal),
'interest_date' => TransactionJournal::dateAsString($journal, 'interest_date'),
'book_date' => TransactionJournal::dateAsString($journal, 'book_date'),
'process_date' => TransactionJournal::dateAsString($journal, 'process_date'),
'category' => TransactionJournal::categoryAsString($journal),
'budget_id' => TransactionJournal::budgetId($journal),
'piggy_bank_id' => TransactionJournal::piggyBankId($journal),
'tags' => join(',', $journal->tags->pluck('tag')->toArray()),
'account_from_id' => TransactionJournal::sourceAccount($journal)->id,
'account_to_id' => TransactionJournal::destinationAccount($journal)->id,
'amount' => TransactionJournal::amountPositive($journal),
]; ];
$category = $journal->categories()->first();
if (!is_null($category)) {
$preFilled['category'] = $category->name;
}
$budget = $journal->budgets()->first();
if (!is_null($budget)) {
$preFilled['budget_id'] = $budget->id;
}
if ($journal->piggyBankEvents()->count() > 0) {
$preFilled['piggy_bank_id'] = $journal->piggyBankEvents()->orderBy('date', 'DESC')->first()->piggy_bank_id;
}
$preFilled['amount'] = TransactionJournal::amountPositive($journal);
if ($journal->isWithdrawal()) { if ($journal->isWithdrawal()) {
$preFilled['account_id'] = TransactionJournal::sourceAccount($journal)->id; $preFilled['account_id'] = TransactionJournal::sourceAccount($journal)->id;
if (TransactionJournal::destinationAccountTypeStr($journal) != 'Cash account') { if (TransactionJournal::destinationAccountTypeStr($journal) != 'Cash account') {
@@ -190,8 +183,6 @@ class TransactionController extends Controller
} }
} }
$preFilled['account_from_id'] = TransactionJournal::sourceAccount($journal)->id;
$preFilled['account_to_id'] = TransactionJournal::destinationAccount($journal)->id;
Session::flash('preFilled', $preFilled); Session::flash('preFilled', $preFilled);
Session::flash('gaEventCategory', 'transactions'); Session::flash('gaEventCategory', 'transactions');
@@ -204,7 +195,9 @@ class TransactionController extends Controller
Session::forget('transactions.edit.fromUpdate'); Session::forget('transactions.edit.fromUpdate');
return view('transactions.edit', compact('journal', 'uploadSize', 'accounts', 'what', 'budgets', 'piggies', 'subTitle'))->with('data', $preFilled); return view('transactions.edit', compact('journal', 'uploadSize', 'accountList', 'what', 'budgetList', 'piggyBankList', 'subTitle'))->with(
'data', $preFilled
);
} }
/** /**

View File

@@ -10,6 +10,7 @@
namespace FireflyIII\Support\Models; namespace FireflyIII\Support\Models;
use Carbon\Carbon;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
@@ -79,6 +80,56 @@ class TransactionJournalSupport extends Model
return $amount; return $amount;
} }
/**
* @param TransactionJournal $journal
*
* @return int
*/
public static function budgetId(TransactionJournal $journal): int
{
$budget = $journal->budgets()->first();
if (!is_null($budget)) {
return $budget->id;
}
return 0;
}
/**
* @param TransactionJournal $journal
*
* @return string
*/
public static function categoryAsString(TransactionJournal $journal): string
{
$category = $journal->categories()->first();
if (!is_null($category)) {
return $category->name;
}
return '';
}
/**
* @param TransactionJournal $journal
* @param string $dateField
*
* @return string
*/
public static function dateAsString(TransactionJournal $journal, string $dateField = ''): string
{
if ($dateField === '') {
return $journal->date->format('Y-m-d');
}
if (!is_null($journal->$dateField) && $journal->$dateField instanceof Carbon) {
return $journal->$dateField->format('Y-m-d');
}
return '';
}
/** /**
* @param TransactionJournal $journal * @param TransactionJournal $journal
* *
@@ -147,6 +198,20 @@ class TransactionJournalSupport extends Model
return false; return false;
} }
/**
* @param TransactionJournal $journal
*
* @return int
*/
public static function piggyBankId(TransactionJournal $journal): int
{
if ($journal->piggyBankEvents()->count() > 0) {
return $journal->piggyBankEvents()->orderBy('date', 'DESC')->first()->piggy_bank_id;
}
return 0;
}
/** /**
* @param TransactionJournal $journal * @param TransactionJournal $journal
* *

View File

@@ -650,7 +650,7 @@ return [
'earned' => 'Earned', 'earned' => 'Earned',
'overspent' => 'Overspent', 'overspent' => 'Overspent',
'left' => 'Left', 'left' => 'Left',
'noBudget' => '(no budget)', 'no_budget' => '(no budget)',
'maxAmount' => 'Maximum amount', 'maxAmount' => 'Maximum amount',
'minAmount' => 'Minumum amount', 'minAmount' => 'Minumum amount',
'billEntry' => 'Current bill entry', 'billEntry' => 'Current bill entry',

View File

@@ -74,7 +74,6 @@ return [
'add_new_deposit' => 'Add a new deposit', 'add_new_deposit' => 'Add a new deposit',
'add_new_transfer' => 'Add a new transfer', 'add_new_transfer' => 'Add a new transfer',
'noPiggybank' => '(no piggy bank)', 'noPiggybank' => '(no piggy bank)',
'noBudget' => '(no budget)',
'title' => 'Title', 'title' => 'Title',
'notes' => 'Notes', 'notes' => 'Notes',
'filename' => 'File name', 'filename' => 'File name',

View File

@@ -100,7 +100,7 @@
<div class="row"> <div class="row">
<div class="col-lg-12"> <div class="col-lg-12">
<label class="checkbox-inline"> <label class="checkbox-inline">
<input type="checkbox" class="budget-checkbox" name="budgets[]" value="0"> {{ 'noBudget'|_ }} <input type="checkbox" class="budget-checkbox" name="budgets[]" value="0"> {{ 'no_budget'|_ }}
</label> </label>
{% for budget in budgets %} {% for budget in budgets %}
<label class="checkbox-inline"> <label class="checkbox-inline">

View File

@@ -22,7 +22,7 @@
{% if budgetLine.getBudget.id %} {% if budgetLine.getBudget.id %}
<a href="{{ route('budgets.show',budgetLine.getBudget.id) }}">{{ budgetLine.getBudget.name }}</a> <a href="{{ route('budgets.show',budgetLine.getBudget.id) }}">{{ budgetLine.getBudget.name }}</a>
{% else %} {% else %}
<em>{{ 'noBudget'|_ }}</em> <em>{{ 'no_budget'|_ }}</em>
{% endif %} {% endif %}
</td> </td>
<td> <td>

View File

@@ -24,7 +24,7 @@
<!-- SHOW ACCOUNT (FROM) ONLY FOR WITHDRAWALS AND DEPOSITS --> <!-- SHOW ACCOUNT (FROM) ONLY FOR WITHDRAWALS AND DEPOSITS -->
{% if what == 'deposit' or what == 'withdrawal' %} {% if what == 'deposit' or what == 'withdrawal' %}
{{ ExpandedForm.select('account_id',accounts,data['account_id']) }} {{ ExpandedForm.select('account_id',accountList,data['account_id']) }}
{% endif %} {% endif %}
<!-- SHOW EXPENSE ACCOUNT ONLY FOR WITHDRAWALS --> <!-- SHOW EXPENSE ACCOUNT ONLY FOR WITHDRAWALS -->
@@ -39,8 +39,8 @@
<!-- ONLY SHOW FROM/TO ACCOUNT WHEN CREATING TRANSFER --> <!-- ONLY SHOW FROM/TO ACCOUNT WHEN CREATING TRANSFER -->
{% if what == 'transfer' %} {% if what == 'transfer' %}
{{ ExpandedForm.select('account_from_id',accounts,data['account_from_id']) }} {{ ExpandedForm.select('account_from_id',accountList,data['account_from_id']) }}
{{ ExpandedForm.select('account_to_id',accounts,data['account_to_id']) }} {{ ExpandedForm.select('account_to_id',accountList,data['account_to_id']) }}
{% endif %} {% endif %}
<!-- ALWAYS SHOW AMOUNT --> <!-- ALWAYS SHOW AMOUNT -->
@@ -61,7 +61,7 @@
<div class="box-body"> <div class="box-body">
<!-- BUDGET ONLY WHEN CREATING A WITHDRAWAL --> <!-- BUDGET ONLY WHEN CREATING A WITHDRAWAL -->
{% if what == 'withdrawal' %} {% if what == 'withdrawal' %}
{{ ExpandedForm.select('budget_id',budgets,data['budget_id']) }} {{ ExpandedForm.select('budget_id',budgetList,data['budget_id']) }}
{% endif %} {% endif %}
<!-- CATEGORY ALWAYS --> <!-- CATEGORY ALWAYS -->
@@ -80,8 +80,8 @@
{{ ExpandedForm.text('tags') }} {{ ExpandedForm.text('tags') }}
<!-- RELATE THIS TRANSFER TO A PIGGY BANK --> <!-- RELATE THIS TRANSFER TO A PIGGY BANK -->
{% if what == 'withdrawal' and piggies|length > 0 %} {% if what == 'transfer' and piggyBankList|length > 0 %}
{{ ExpandedForm.select('piggy_bank_id',piggies,data['piggy_bank_id']) }} {{ ExpandedForm.select('piggy_bank_id',piggyBankList,data['piggy_bank_id']) }}
{% endif %} {% endif %}
<!-- ATTACHMENTS --> <!-- ATTACHMENTS -->