Code for optional fields #301

Signed-off-by: James Cole <thegrumpydictator@gmail.com>
This commit is contained in:
James Cole
2016-09-09 11:19:40 +02:00
parent 8d19f60091
commit 176752e219
7 changed files with 212 additions and 54 deletions

View File

@@ -44,6 +44,12 @@ class TransactionController extends Controller
parent::__construct();
View::share('title', trans('firefly.transactions'));
View::share('mainTitleIcon', 'fa-repeat');
$maxFileSize = Steam::phpBytes(ini_get('upload_max_filesize'));
$maxPostSize = Steam::phpBytes(ini_get('post_max_size'));
$uploadSize = min($maxFileSize, $maxPostSize);
View::share('uploadSize', $uploadSize);
}
/**
@@ -136,17 +142,20 @@ class TransactionController extends Controller
if ($count > 2) {
return redirect(route('split.journal.edit', [$journal->id]));
}
$budgetRepository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
$piggyRepository = app('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface');
$crud = app('FireflyIII\Crud\Account\AccountCrudInterface');
$assetAccounts = ExpandedForm::makeSelectList($crud->getAccountsByType(['Default account', 'Asset account']));
$budgetList = ExpandedForm::makeSelectListWithEmpty($budgetRepository->getActiveBudgets());
$piggyBankList = ExpandedForm::makeSelectListWithEmpty($piggyRepository->getPiggyBanks());
$maxFileSize = Steam::phpBytes(ini_get('upload_max_filesize'));
$maxPostSize = Steam::phpBytes(ini_get('post_max_size'));
$uploadSize = min($maxFileSize, $maxPostSize);
$what = strtolower(TransactionJournal::transactionTypeStr($journal));
$subTitle = trans('breadcrumbs.edit_journal', ['description' => $journal->description]);
// code to get list data:
$budgetRepository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
$piggyRepository = app('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface');
$crud = app('FireflyIII\Crud\Account\AccountCrudInterface');
$assetAccounts = ExpandedForm::makeSelectList($crud->getAccountsByType(['Default account', 'Asset account']));
$budgetList = ExpandedForm::makeSelectListWithEmpty($budgetRepository->getActiveBudgets());
$piggyBankList = ExpandedForm::makeSelectListWithEmpty($piggyRepository->getPiggyBanks());
// view related code
$subTitle = trans('breadcrumbs.edit_journal', ['description' => $journal->description]);
$what = strtolower(TransactionJournal::transactionTypeStr($journal));
// journal related code
$sourceAccounts = TransactionJournal::sourceAccountList($journal);
$destinationAccounts = TransactionJournal::destinationAccountList($journal);
$optionalFields = Preferences::get('transaction_journal_optional_fields', [])->data;
@@ -164,6 +173,12 @@ class TransactionController extends Controller
'destination_account_id' => $destinationAccounts->first()->id,
'destination_account_name' => $destinationAccounts->first()->name,
'amount' => TransactionJournal::amountPositive($journal),
// new custom fields:
'due_date' => TransactionJournal::dateAsString($journal, 'due_date'),
'payment_date' => TransactionJournal::dateAsString($journal, 'payment_date'),
'interal_reference' => $journal->getMeta('internal_reference'),
'notes' => $journal->getMeta('notes'),
];
if ($journal->isWithdrawal() && $destinationAccounts->first()->accountType->type == AccountType::CASH) {
@@ -187,7 +202,7 @@ class TransactionController extends Controller
return view(
'transactions.edit',
compact('journal', 'optionalFields', 'uploadSize', 'assetAccounts', 'what', 'budgetList', 'piggyBankList', 'subTitle')
compact('journal', 'optionalFields', 'assetAccounts', 'what', 'budgetList', 'piggyBankList', 'subTitle')
)->with('data', $preFilled);
}

View File

@@ -60,6 +60,13 @@ class JournalFormRequest extends Request
'category' => $this->get('category') ?? '',
'tags' => explode(',', $tags),
'piggy_bank_id' => $this->get('piggy_bank_id') ? intval($this->get('piggy_bank_id')) : 0,
// new custom fields here:
'due_date' => $this->get('due_date') ? new Carbon($this->get('due_date')) : null,
'payment_date' => $this->get('payment_date') ? new Carbon($this->get('payment_date')) : null,
'internal_reference' => $this->get('internal_reference'),
'notes' => $this->get('notes'),
];
}
@@ -81,6 +88,12 @@ class JournalFormRequest extends Request
'category' => 'between:1,255',
'amount_currency_id_amount' => 'required|exists:transaction_currencies,id',
'piggy_bank_id' => 'numeric',
// new custom fields here:
'due_date' => 'date',
'payment_date' => 'date',
'internal_reference' => 'min:1,max:255',
'notes' => 'min:1,max:65536',
];
switch ($what) {

View File

@@ -14,10 +14,13 @@ namespace FireflyIII\Models;
use Auth;
use Carbon\Carbon;
use Crypt;
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Models\TransactionJournalSupport;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Log;
use Preferences;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Watson\Validating\ValidatingTrait;
@@ -171,6 +174,18 @@ class TransactionJournal extends TransactionJournalSupport
return $this->belongsToMany('FireflyIII\Models\Category');
}
/**
* @param string $name
*
* @return bool
*/
public function deleteMeta(string $name):bool
{
$this->transactionJournalMeta()->where('name', $name)->delete();
return true;
}
/**
*
* @param $value
@@ -188,19 +203,38 @@ class TransactionJournal extends TransactionJournalSupport
/**
*
* @param string $fieldName
* @param string $name
*
* @return string
*/
public function getMeta($fieldName)
public function getMeta(string $name)
{
foreach ($this->transactionjournalmeta as $meta) {
if ($meta->name == $fieldName) {
return $meta->data;
}
$value = null;
$cache = new CacheProperties;
$cache->addProperty('journal-meta');
$cache->addProperty($this->id);
$cache->addProperty($name);
if ($cache->has()) {
return $cache->get();
}
return '';
Log::debug(sprintf('Looking for journal #%d meta field "%s".', $this->id, $name));
$entry = $this->transactionJournalMeta()->where('name', $name)->first();
if (!is_null($entry)) {
$value = $entry->data;
// cache:
$cache->store($value);
}
// convert to Carbon if name is _date
if (!is_null($value) && substr($name, -5) === '_date') {
$value = new Carbon($value);
// cache:
$cache->store($value);
}
return $value;
}
/**
@@ -354,6 +388,38 @@ class TransactionJournal extends TransactionJournalSupport
$this->attributes['encrypted'] = true;
}
/**
* @param string $name
* @param $value
*
* @return TransactionJournalMeta
*/
public function setMeta(string $name, $value): TransactionJournalMeta
{
if (is_null($value)) {
$this->deleteMeta($name);
return new TransactionJournalMeta();
}
if ($value instanceof Carbon) {
$value = $value->toW3cString();
}
Log::debug(sprintf('Going to set "%s" with value "%s"', $name, json_encode($value)));
$entry = $this->transactionJournalMeta()->where('name', $name)->first();
if (is_null($entry)) {
$entry = new TransactionJournalMeta();
$entry->transactionJournal()->associate($this);
$entry->name = $name;
}
$entry->data = $value;
$entry->save();
Preferences::mark();
return $entry;
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
@@ -370,6 +436,14 @@ class TransactionJournal extends TransactionJournalSupport
return $this->belongsTo('FireflyIII\Models\TransactionCurrency');
}
/**
* @return HasMany
*/
public function transactionJournalMeta(): HasMany
{
return $this->hasMany('FireflyIII\Models\TransactionJournalMeta');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
@@ -378,22 +452,6 @@ class TransactionJournal extends TransactionJournalSupport
return $this->belongsTo('FireflyIII\Models\TransactionType');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function transactiongroups()
{
return $this->belongsToMany('FireflyIII\Models\TransactionGroup');
}
/**
* @return HasMany
*/
public function transactionjournalmeta(): HasMany
{
return $this->hasMany('FireflyIII\Models\TransactionJournalMeta');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
@@ -409,4 +467,5 @@ class TransactionJournal extends TransactionJournalSupport
{
return $this->belongsTo('FireflyIII\User');
}
}

View File

@@ -41,6 +41,9 @@ class JournalRepository implements JournalRepositoryInterface
/** @var User */
private $user;
/** @var array */
private $validMetaFields = ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date', 'internal_reference', 'notes'];
/**
* JournalRepository constructor.
*
@@ -344,8 +347,15 @@ class JournalRepository implements JournalRepositoryInterface
$this->saveTags($journal, $data['tags']);
}
return $journal;
foreach ($data as $key => $value) {
if (in_array($key, $this->validMetaFields)) {
$journal->setMeta($key, $value);
continue;
}
Log::debug(sprintf('Could not store meta field "%s" with value "%s" for journal #%d', json_encode($key), json_encode($value), $journal->id));
}
return $journal;
}
@@ -370,14 +380,17 @@ class JournalRepository implements JournalRepositoryInterface
'description' => $data['description'],
'completed' => 0,
'date' => $data['date'],
'interest_date' => $data['interest_date'],
'book_date' => $data['book_date'],
'process_date' => $data['process_date'],
]
);
$journal->save();
return $journal;
$result = $journal->save();
if ($result) {
return $journal;
}
return new TransactionJournal();
}
/**
@@ -392,10 +405,6 @@ class JournalRepository implements JournalRepositoryInterface
$journal->transaction_currency_id = $data['amount_currency_id_amount'];
$journal->description = $data['description'];
$journal->date = $data['date'];
$journal->interest_date = $data['interest_date'];
$journal->book_date = $data['book_date'];
$journal->process_date = $data['process_date'];
// unlink all categories, recreate them:
$journal->categories()->detach();
@@ -439,6 +448,20 @@ class JournalRepository implements JournalRepositoryInterface
$this->updateTags($journal, $data['tags']);
}
// update meta fields:
$result = $journal->save();
if ($result) {
foreach ($data as $key => $value) {
if (in_array($key, $this->validMetaFields)) {
$journal->setMeta($key, $value);
continue;
}
Log::debug(sprintf('Could not store meta field "%s" with value "%s" for journal #%d', json_encode($key), json_encode($value), $journal->id));
}
return $journal;
}
return $journal;
}

View File

@@ -29,6 +29,7 @@ use Illuminate\Support\Collection;
*/
class TransactionJournalSupport extends Model
{
/**
* @param TransactionJournal $journal
*
@@ -123,7 +124,22 @@ class TransactionJournalSupport extends Model
return $journal->date->format('Y-m-d');
}
if (!is_null($journal->$dateField) && $journal->$dateField instanceof Carbon) {
return $journal->$dateField->format('Y-m-d');
// make field NULL
$carbon = clone $journal->$dateField;
$journal->$dateField = null;
$journal->save();
// create meta entry
$journal->setMeta($dateField, $carbon);
// return that one instead.
return $carbon->format('Y-m-d');
}
$metaField = $journal->getMeta($dateField);
if (!is_null($metaField)) {
$carbon = new Carbon($metaField);
return $carbon->format('Y-m-d');
}
return '';
@@ -288,6 +304,4 @@ class TransactionJournalSupport extends Model
return $typeStr;
}
}

View File

@@ -24,6 +24,9 @@ use FireflyIII\Models\Transaction;
*/
class Steam
{
/**
*
* @param \FireflyIII\Models\Account $account

View File

@@ -21,22 +21,38 @@
<td>{{ trans('list.date') }}</td>
<td>{{ journal.date.formatLocalized(monthAndDayFormat) }}</td>
</tr>
{% if journal.interest_date %}
{% if journal.getMeta('interest_date') %}
<tr>
<td>{{ trans('list.interest_date') }}</td>
<td>{{ journal.interest_date.formatLocalized(monthAndDayFormat) }}</td>
<td>{{ journal.getMeta('interest_date').formatLocalized(monthAndDayFormat) }}</td>
</tr>
{% endif %}
{% if journal.book_date %}
{% if journal.getMeta('book_date') %}
<tr>
<td>{{ trans('list.book_date') }}</td>
<td>{{ journal.book_date.formatLocalized(monthAndDayFormat) }}</td>
<td>{{ journal.getMeta('book_date').formatLocalized(monthAndDayFormat) }}</td>
</tr>
{% endif %}
{% if journal.process_date %}
{% if journal.getMeta('process_date') %}
<tr>
<td>{{ trans('list.process_date') }}</td>
<td>{{ journal.process_date.formatLocalized(monthAndDayFormat) }}</td>
<td>{{ journal.getMeta('process_date').formatLocalized(monthAndDayFormat) }}</td>
</tr>
{% endif %}
{% if journal.getMeta('due_date') %}
<tr>
<td>{{ trans('list.due_date') }}</td>
<td>{{ journal.getMeta('due_date').formatLocalized(monthAndDayFormat) }}</td>
</tr>
{% endif %}
{% if journal.getMeta('payment_date') %}
<tr>
<td>{{ trans('list.payment_date') }}</td>
<td>{{ journal.getMeta('payment_date').formatLocalized(monthAndDayFormat) }}</td>
</tr>
{% endif %}
@@ -94,6 +110,21 @@
</td>
</tr>
{% endif %}
{% if journal.getMeta('internal_reference') %}
<tr>
<td>{{ trans('list.interal_reference') }}</td>
<td>{{ journal.getMeta('internal_reference') }}</td>
</tr>
{% endif %}
{% if journal.getMeta('notes') %}
<tr>
<td>{{ trans('list.notes') }}</td>
<td>{{ journal.getMeta('notes')|nl2br }}</td>
</tr>
{% endif %}
</table>
</div>
<div class="box-footer">