Skip to content

Firefly III: a personal finances manager

Code for optional fields #301

James Cole176752e2193d
diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php
index 1f1707919f..d4661a42e4 100644
--- a/app/Http/Controllers/TransactionController.php
+++ b/app/Http/Controllers/TransactionController.php
@@ -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);
}
diff --git a/app/Http/Requests/JournalFormRequest.php b/app/Http/Requests/JournalFormRequest.php
index 43105e214a..88e0895673 100644
--- a/app/Http/Requests/JournalFormRequest.php
+++ b/app/Http/Requests/JournalFormRequest.php
@@ -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) {
diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php
index 15cbf6a7fe..912e3c0482 100644
--- a/app/Models/TransactionJournal.php
+++ b/app/Models/TransactionJournal.php
@@ -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();
+ }
+
+ 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);
}
- return '';
+ // 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;
}
/**
@@ -355,43 +389,67 @@ class TransactionJournal extends TransactionJournalSupport
}
/**
- * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
+ * @param string $name
+ * @param $value
+ *
+ * @return TransactionJournalMeta
*/
- public function tags()
+ public function setMeta(string $name, $value): TransactionJournalMeta
{
- return $this->belongsToMany('FireflyIII\Models\Tag');
+ 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\BelongsTo
+ * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
- public function transactionCurrency()
+ public function tags()
{
- return $this->belongsTo('FireflyIII\Models\TransactionCurrency');
+ return $this->belongsToMany('FireflyIII\Models\Tag');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
- public function transactionType()
+ public function transactionCurrency()
{
- return $this->belongsTo('FireflyIII\Models\TransactionType');
+ return $this->belongsTo('FireflyIII\Models\TransactionCurrency');
}
/**
- * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
+ * @return HasMany
*/
- public function transactiongroups()
+ public function transactionJournalMeta(): HasMany
{
- return $this->belongsToMany('FireflyIII\Models\TransactionGroup');
+ return $this->hasMany('FireflyIII\Models\TransactionJournalMeta');
}
/**
- * @return HasMany
+ * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
- public function transactionjournalmeta(): HasMany
+ public function transactionType()
{
- return $this->hasMany('FireflyIII\Models\TransactionJournalMeta');
+ return $this->belongsTo('FireflyIII\Models\TransactionType');
}
/**
@@ -409,4 +467,5 @@ class TransactionJournal extends TransactionJournalSupport
{
return $this->belongsTo('FireflyIII\User');
}
+
}
diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php
index 3631c5da4b..189a34e01b 100644
--- a/app/Repositories/Journal/JournalRepository.php
+++ b/app/Repositories/Journal/JournalRepository.php
@@ -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;
}
diff --git a/app/Support/Models/TransactionJournalSupport.php b/app/Support/Models/TransactionJournalSupport.php
index 1469715340..7b59c20d2d 100644
--- a/app/Support/Models/TransactionJournalSupport.php
+++ b/app/Support/Models/TransactionJournalSupport.php
@@ -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;
}
-
-
}
diff --git a/app/Support/Steam.php b/app/Support/Steam.php
index 5158d2ab03..f32fdbaf51 100644
--- a/app/Support/Steam.php
+++ b/app/Support/Steam.php
@@ -24,6 +24,9 @@ use FireflyIII\Models\Transaction;
*/
class Steam
{
+
+
+
/**
*
* @param \FireflyIII\Models\Account $account
diff --git a/resources/views/transactions/show.twig b/resources/views/transactions/show.twig
index 4aa107f307..57d9d785bb 100644
--- a/resources/views/transactions/show.twig
+++ b/resources/views/transactions/show.twig
@@ -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">