Large commit to get rid of a lot of static methods.

This commit is contained in:
James Cole
2017-03-04 07:18:35 +01:00
parent 33c20c8dc4
commit d9aa074330
25 changed files with 144 additions and 169 deletions

View File

@@ -1,10 +1,8 @@
<?php
/**
* TagSupport.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
* TagTrait.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
@@ -17,28 +15,26 @@ use FireflyIII\Models\Tag;
use Illuminate\Database\Eloquent\Model;
/**
* FireflyIII\Support\Models\TagSupport
* Class TagSupport
*
* @mixin \Eloquent
* @package FireflyIII\Support\Models
*/
class TagSupport extends Model
trait TagTrait
{
/**
* Can a tag become an advance payment?
*
* @param Tag $tag
*
* @return bool
*/
public static function tagAllowAdvance(Tag $tag): bool
public function tagAllowAdvance(): bool
{
/*
* If this tag is a balancing act, and it contains transfers, it cannot be
* changes to an advancePayment.
*/
if ($tag->tagMode == 'balancingAct' || $tag->tagMode == 'nothing') {
foreach ($tag->transactionjournals as $journal) {
if ($this->tagMode == 'balancingAct' || $this->tagMode == 'nothing') {
foreach ($this->transactionjournals as $journal) {
if ($journal->isTransfer()) {
return false;
}
@@ -49,7 +45,7 @@ class TagSupport extends Model
* If this tag contains more than one expenses, it cannot become an advance payment.
*/
$count = 0;
foreach ($tag->transactionjournals as $journal) {
foreach ($this->transactionjournals as $journal) {
if ($journal->isWithdrawal()) {
$count++;
}
@@ -64,23 +60,21 @@ class TagSupport extends Model
/**
* Can a tag become a balancing act?
*
* @param Tag $tag
*
* @return bool
*/
public static function tagAllowBalancing(Tag $tag): bool
public function tagAllowBalancing(): bool
{
/*
* If has more than two transactions already, cannot become a balancing act:
*/
if ($tag->transactionjournals->count() > 2) {
if ($this->transactionjournals->count() > 2) {
return false;
}
/*
* If any transaction is a deposit, cannot become a balancing act.
*/
foreach ($tag->transactionjournals as $journal) {
foreach ($this->transactionjournals as $journal) {
if ($journal->isDeposit()) {
return false;
}

View File

@@ -1,10 +1,8 @@
<?php
/**
* TransactionJournalSupport.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
* TransactionJournalTrait.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
@@ -24,24 +22,21 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
/**
* Class TransactionJournalSupport
* Class TransactionJournalTrait
*
* @package FireflyIII\Support\Models
* @mixin \Eloquent
*/
class TransactionJournalSupport extends Model
trait TransactionJournalTrait
{
/**
* @param TransactionJournal $journal
*
* @return string
* @throws FireflyException
*/
public static function amount(TransactionJournal $journal): string
public function amount(): string
{
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty($this->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('amount');
if ($cache->has()) {
@@ -49,9 +44,9 @@ class TransactionJournalSupport extends Model
}
// saves on queries:
$amount = $journal->transactions()->where('amount', '>', 0)->get()->sum('amount');
$amount = $this->transactions()->where('amount', '>', 0)->get()->sum('amount');
if ($journal->isWithdrawal()) {
if ($this->isWithdrawal()) {
$amount = $amount * -1;
}
$amount = strval($amount);
@@ -61,14 +56,12 @@ class TransactionJournalSupport extends Model
}
/**
* @param TransactionJournal $journal
*
* @return string
*/
public static function amountPositive(TransactionJournal $journal): string
public function amountPositive(): string
{
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty($this->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('amount-positive');
if ($cache->has()) {
@@ -76,7 +69,7 @@ class TransactionJournalSupport extends Model
}
// saves on queries:
$amount = $journal->transactions()->where('amount', '>', 0)->get()->sum('amount');
$amount = $this->transactions()->where('amount', '>', 0)->get()->sum('amount');
$amount = strval($amount);
$cache->store($amount);
@@ -85,13 +78,11 @@ class TransactionJournalSupport extends Model
}
/**
* @param TransactionJournal $journal
*
* @return int
*/
public static function budgetId(TransactionJournal $journal): int
public function budgetId(): int
{
$budget = $journal->budgets()->first();
$budget = $this->budgets()->first();
if (!is_null($budget)) {
return $budget->id;
}
@@ -100,13 +91,11 @@ class TransactionJournalSupport extends Model
}
/**
* @param TransactionJournal $journal
*
* @return string
*/
public static function categoryAsString(TransactionJournal $journal): string
public function categoryAsString(): string
{
$category = $journal->categories()->first();
$category = $this->categories()->first();
if (!is_null($category)) {
return $category->name;
}
@@ -115,29 +104,28 @@ class TransactionJournalSupport extends Model
}
/**
* @param TransactionJournal $journal
* @param string $dateField
*
* @return string
*/
public static function dateAsString(TransactionJournal $journal, string $dateField = ''): string
public function dateAsString(string $dateField = ''): string
{
if ($dateField === '') {
return $journal->date->format('Y-m-d');
return $this->date->format('Y-m-d');
}
if (!is_null($journal->$dateField) && $journal->$dateField instanceof Carbon) {
if (!is_null($this->$dateField) && $this->$dateField instanceof Carbon) {
// make field NULL
$carbon = clone $journal->$dateField;
$journal->$dateField = null;
$journal->save();
$carbon = clone $this->$dateField;
$this->$dateField = null;
$this->save();
// create meta entry
$journal->setMeta($dateField, $carbon);
$this->setMeta($dateField, $carbon);
// return that one instead.
return $carbon->format('Y-m-d');
}
$metaField = $journal->getMeta($dateField);
$metaField = $this->getMeta($dateField);
if (!is_null($metaField)) {
$carbon = new Carbon($metaField);
@@ -150,20 +138,18 @@ class TransactionJournalSupport extends Model
}
/**
* @param TransactionJournal $journal
*
* @return Collection
*/
public static function destinationAccountList(TransactionJournal $journal): Collection
public function destinationAccountList(): Collection
{
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty($this->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('destination-account-list');
if ($cache->has()) {
return $cache->get();
}
$transactions = $journal->transactions()->where('amount', '>', 0)->orderBy('transactions.account_id')->with('account')->get();
$transactions = $this->transactions()->where('amount', '>', 0)->orderBy('transactions.account_id')->with('account')->get();
$list = new Collection;
/** @var Transaction $t */
foreach ($transactions as $t) {
@@ -175,20 +161,18 @@ class TransactionJournalSupport extends Model
}
/**
* @param TransactionJournal $journal
*
* @return Collection
*/
public static function destinationTransactionList(TransactionJournal $journal): Collection
public function destinationTransactionList(): Collection
{
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty($this->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('destination-transaction-list');
if ($cache->has()) {
return $cache->get();
}
$list = $journal->transactions()->where('amount', '>', 0)->with('account')->get();
$list = $this->transactions()->where('amount', '>', 0)->with('account')->get();
$cache->store($list);
return $list;
@@ -200,7 +184,7 @@ class TransactionJournalSupport extends Model
*
* @return bool
*/
public static function isJoined(Builder $query, string $table): bool
public function isJoined(Builder $query, string $table): bool
{
$joins = $query->getQuery()->joins;
if (is_null($joins)) {
@@ -216,46 +200,30 @@ class TransactionJournalSupport extends Model
}
/**
* @param TransactionJournal $journal
*
* @return int
*/
public static function piggyBankId(TransactionJournal $journal): int
public function piggyBankId(): int
{
if ($journal->piggyBankEvents()->count() > 0) {
return $journal->piggyBankEvents()->orderBy('date', 'DESC')->first()->piggy_bank_id;
if ($this->piggyBankEvents()->count() > 0) {
return $this->piggyBankEvents()->orderBy('date', 'DESC')->first()->piggy_bank_id;
}
return 0;
}
/**
* @return array
*/
public static function queryFields(): array
{
return [
'transaction_journals.*',
'transaction_types.type AS transaction_type_type', // the other field is called "transaction_type_id" so this is pretty consistent.
'transaction_currencies.code AS transaction_currency_code',
];
}
/**
* @param TransactionJournal $journal
*
* @return Collection
*/
public static function sourceAccountList(TransactionJournal $journal): Collection
public function sourceAccountList(): Collection
{
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty($this->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('source-account-list');
if ($cache->has()) {
return $cache->get();
}
$transactions = $journal->transactions()->where('amount', '<', 0)->orderBy('transactions.account_id')->with('account')->get();
$transactions = $this->transactions()->where('amount', '<', 0)->orderBy('transactions.account_id')->with('account')->get();
$list = new Collection;
/** @var Transaction $t */
foreach ($transactions as $t) {
@@ -267,41 +235,37 @@ class TransactionJournalSupport extends Model
}
/**
* @param TransactionJournal $journal
*
* @return Collection
*/
public static function sourceTransactionList(TransactionJournal $journal): Collection
public function sourceTransactionList(): Collection
{
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty($this->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('source-transaction-list');
if ($cache->has()) {
return $cache->get();
}
$list = $journal->transactions()->where('amount', '<', 0)->with('account')->get();
$list = $this->transactions()->where('amount', '<', 0)->with('account')->get();
$cache->store($list);
return $list;
}
/**
* @param TransactionJournal $journal
*
* @return string
*/
public static function transactionTypeStr(TransactionJournal $journal): string
public function transactionTypeStr(): string
{
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty($this->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('type-string');
if ($cache->has()) {
return $cache->get();
}
$typeStr = $journal->transaction_type_type ?? $journal->transactionType->type;
$typeStr = $this->transaction_type_type ?? $this->transactionType->type;
$cache->store($typeStr);
return $typeStr;