diff --git a/app/Handlers/Events/StoredJournalEventHandler.php b/app/Handlers/Events/StoredJournalEventHandler.php index c0e5e88014..8839287e4f 100644 --- a/app/Handlers/Events/StoredJournalEventHandler.php +++ b/app/Handlers/Events/StoredJournalEventHandler.php @@ -140,7 +140,7 @@ class StoredJournalEventHandler */ private function getExactAmount(TransactionJournal $journal, PiggyBank $piggyBank, PiggyBankRepetition $repetition): string { - $amount = TransactionJournal::amountPositive($journal); + $amount = $journal->amountPositive(); $sources = TransactionJournal::sourceAccountList($journal)->pluck('id')->toArray(); $room = bcsub(strval($piggyBank->targetamount), strval($repetition->currentamount)); $compare = bcmul($repetition->currentamount, '-1'); diff --git a/app/Http/Controllers/Popup/ReportController.php b/app/Http/Controllers/Popup/ReportController.php index a2f3505923..8f4ae3e23d 100644 --- a/app/Http/Controllers/Popup/ReportController.php +++ b/app/Http/Controllers/Popup/ReportController.php @@ -275,7 +275,7 @@ class ReportController extends Controller $journals = $journals->filter( function (Transaction $transaction) use ($report) { // get the destinations: - $destinations = TransactionJournal::destinationAccountList($transaction->transactionJournal)->pluck('id')->toArray(); + $destinations = $transaction->transactionJournal->destinationAccountList()->pluck('id')->toArray(); // do these intersect with the current list? return !empty(array_intersect($report, $destinations)); diff --git a/app/Http/Controllers/TagController.php b/app/Http/Controllers/TagController.php index b746961419..fdedc1d952 100644 --- a/app/Http/Controllers/TagController.php +++ b/app/Http/Controllers/TagController.php @@ -159,8 +159,8 @@ class TagController extends Controller /* * Can this tag become another type? */ - $allowAdvance = Tag::tagAllowAdvance($tag); - $allowToBalancingAct = Tag::tagAllowBalancing($tag); + $allowAdvance = $tag->tagAllowAdvance(); + $allowToBalancingAct = $tag->tagAllowBalancing(); // edit tag options: if ($allowAdvance === false) { diff --git a/app/Http/Controllers/Transaction/ConvertController.php b/app/Http/Controllers/Transaction/ConvertController.php index 3fb1e41de7..de0ea9de03 100644 --- a/app/Http/Controllers/Transaction/ConvertController.php +++ b/app/Http/Controllers/Transaction/ConvertController.php @@ -64,11 +64,13 @@ class ConvertController extends Controller */ public function index(TransactionType $destinationType, TransactionJournal $journal) { + // @codeCoverageIgnoreStart if ($this->isOpeningBalance($journal)) { return $this->redirectToAccount($journal); } + // @codeCoverageIgnoreEnd - $positiveAmount = TransactionJournal::amountPositive($journal); + $positiveAmount = $journal->amountPositive(); $assetAccounts = ExpandedForm::makeSelectList($this->accounts->getActiveAccountsByType([AccountType::DEFAULT, AccountType::ASSET])); $sourceType = $journal->transactionType; $subTitle = trans('firefly.convert_to_' . $destinationType->type, ['description' => $journal->description]); @@ -89,8 +91,8 @@ class ConvertController extends Controller } // get source and destination account: - $sourceAccount = TransactionJournal::sourceAccountList($journal)->first(); - $destinationAccount = TransactionJournal::destinationAccountList($journal)->first(); + $sourceAccount = $journal->sourceAccountList()->first(); + $destinationAccount = $journal->destinationAccountList()->first(); return view( 'transactions.convert', @@ -165,8 +167,8 @@ class ConvertController extends Controller { /** @var AccountRepositoryInterface $accountRepository */ $accountRepository = app(AccountRepositoryInterface::class); - $sourceAccount = TransactionJournal::sourceAccountList($journal)->first(); - $destinationAccount = TransactionJournal::destinationAccountList($journal)->first(); + $sourceAccount = $journal->sourceAccountList()->first(); + $destinationAccount = $journal->destinationAccountList()->first(); $sourceType = $journal->transactionType; $joined = $sourceType->type . '-' . $destinationType->type; switch ($joined) { @@ -210,8 +212,8 @@ class ConvertController extends Controller { /** @var AccountRepositoryInterface $accountRepository */ $accountRepository = app(AccountRepositoryInterface::class); - $sourceAccount = TransactionJournal::sourceAccountList($journal)->first(); - $destinationAccount = TransactionJournal::destinationAccountList($journal)->first(); + $sourceAccount = $journal->sourceAccountList()->first(); + $destinationAccount = $journal->destinationAccountList()->first(); $sourceType = $journal->transactionType; $joined = $sourceType->type . '-' . $destinationType->type; switch ($joined) { diff --git a/app/Http/Controllers/Transaction/MassController.php b/app/Http/Controllers/Transaction/MassController.php index 5a61b060f4..b3565a2079 100644 --- a/app/Http/Controllers/Transaction/MassController.php +++ b/app/Http/Controllers/Transaction/MassController.php @@ -130,8 +130,8 @@ class MassController extends Controller * @var TransactionJournal $journal */ foreach ($journals as $index => $journal) { - $sources = TransactionJournal::sourceAccountList($journal); - $destinations = TransactionJournal::destinationAccountList($journal); + $sources = $journal->sourceAccountList($journal); + $destinations = $journal->destinationAccountList($journal); if ($sources->count() > 1) { $messages[] = trans('firefly.cannot_edit_multiple_source', ['description' => $journal->description, 'id' => $journal->id]); continue; @@ -156,9 +156,9 @@ class MassController extends Controller // set some values to be used in the edit routine: $filtered->each( function (TransactionJournal $journal) { - $journal->amount = TransactionJournal::amountPositive($journal); - $sources = TransactionJournal::sourceAccountList($journal); - $destinations = TransactionJournal::destinationAccountList($journal); + $journal->amount = $journal->amountPositive(); + $sources = $journal->sourceAccountList(); + $destinations = $journal->destinationAccountList(); $journal->transaction_count = $journal->transactions()->count(); if (!is_null($sources->first())) { $journal->source_account_id = $sources->first()->id; diff --git a/app/Http/Controllers/Transaction/SingleController.php b/app/Http/Controllers/Transaction/SingleController.php index d895f776f1..c58edf55f2 100644 --- a/app/Http/Controllers/Transaction/SingleController.php +++ b/app/Http/Controllers/Transaction/SingleController.php @@ -83,8 +83,8 @@ class SingleController extends Controller public function cloneTransaction(TransactionJournal $journal) { - $source = TransactionJournal::sourceAccountList($journal)->first(); - $destination = TransactionJournal::destinationAccountList($journal)->first(); + $source = $journal->sourceAccountList()->first(); + $destination = $journal->destinationAccountList()->first(); $budget = $journal->budgets()->first(); $budgetId = is_null($budget) ? 0 : $budget->id; $category = $journal->categories()->first(); @@ -98,7 +98,7 @@ class SingleController extends Controller 'source_account_name' => $source->name, 'destination_account_id' => $destination->id, 'destination_account_name' => $destination->name, - 'amount' => TransactionJournal::amountPositive($journal), + 'amount' => $journal->amountPositive(), 'date' => $journal->date->format('Y-m-d'), 'budget_id' => $budgetId, 'category' => $categoryName, @@ -195,7 +195,7 @@ class SingleController extends Controller return $this->redirectToAccount($transactionJournal); } // @codeCoverageIgnoreEnd - $type = TransactionJournal::transactionTypeStr($transactionJournal); + $type = $transactionJournal->transactionTypeStr(); Session::flash('success', strval(trans('firefly.deleted_' . strtolower($type), ['description' => e($transactionJournal->description)]))); $repository->delete($transactionJournal); @@ -224,7 +224,7 @@ class SingleController extends Controller return redirect(route('transactions.split.edit', [$journal->id])); } - $what = strtolower(TransactionJournal::transactionTypeStr($journal)); + $what = strtolower($journal->transactionTypeStr()); $assetAccounts = ExpandedForm::makeSelectList($this->accounts->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])); $budgetList = ExpandedForm::makeSelectListWithEmpty($this->budgets->getBudgets()); @@ -233,27 +233,27 @@ class SingleController extends Controller // journal related code - $sourceAccounts = TransactionJournal::sourceAccountList($journal); - $destinationAccounts = TransactionJournal::destinationAccountList($journal); + $sourceAccounts = $journal->sourceAccountList(); + $destinationAccounts = $journal->destinationAccountList(); $optionalFields = Preferences::get('transaction_journal_optional_fields', [])->data; $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), + 'date' => $journal->dateAsString(), + 'interest_date' => $journal->dateAsString( 'interest_date'), + 'book_date' => $journal->dateAsString('book_date'), + 'process_date' => $journal->dateAsString('process_date'), + 'category' => $journal->categoryAsString(), + 'budget_id' => $journal->budgetId(), 'tags' => join(',', $journal->tags->pluck('tag')->toArray()), 'source_account_id' => $sourceAccounts->first()->id, 'source_account_name' => $sourceAccounts->first()->edit_name, 'destination_account_id' => $destinationAccounts->first()->id, 'destination_account_name' => $destinationAccounts->first()->edit_name, - 'amount' => TransactionJournal::amountPositive($journal), + 'amount' => $journal->amountPositive(), // new custom fields: - 'due_date' => TransactionJournal::dateAsString($journal, 'due_date'), - 'payment_date' => TransactionJournal::dateAsString($journal, 'payment_date'), - 'invoice_date' => TransactionJournal::dateAsString($journal, 'invoice_date'), + 'due_date' => $journal->dateAsString('due_date'), + 'payment_date' => $journal->dateAsString('payment_date'), + 'invoice_date' => $journal->dateAsString('invoice_date'), 'interal_reference' => $journal->getMeta('internal_reference'), 'notes' => $journal->getMeta('notes'), ]; @@ -369,7 +369,7 @@ class SingleController extends Controller event(new UpdatedTransactionJournal($journal)); // update, get events by date and sort DESC - $type = strtolower(TransactionJournal::transactionTypeStr($journal)); + $type = strtolower($journal->transactionTypeStr()); Session::flash('success', strval(trans('firefly.updated_' . $type, ['description' => e($data['description'])]))); Preferences::mark(); diff --git a/app/Http/Controllers/Transaction/SplitController.php b/app/Http/Controllers/Transaction/SplitController.php index b1c75d150a..a12e8d77aa 100644 --- a/app/Http/Controllers/Transaction/SplitController.php +++ b/app/Http/Controllers/Transaction/SplitController.php @@ -209,18 +209,18 @@ class SplitController extends Controller */ private function arrayFromJournal(Request $request, TransactionJournal $journal): array { - $sourceAccounts = TransactionJournal::sourceAccountList($journal); - $destinationAccounts = TransactionJournal::destinationAccountList($journal); + $sourceAccounts = $journal->sourceAccountList(); + $destinationAccounts = $journal->destinationAccountList(); $array = [ 'journal_description' => $request->old('journal_description', $journal->description), - 'journal_amount' => TransactionJournal::amountPositive($journal), + 'journal_amount' => $journal->amountPositive(), 'sourceAccounts' => $sourceAccounts, 'journal_source_account_id' => $request->old('journal_source_account_id', $sourceAccounts->first()->id), 'journal_source_account_name' => $request->old('journal_source_account_name', $sourceAccounts->first()->name), 'journal_destination_account_id' => $request->old('journal_destination_account_id', $destinationAccounts->first()->id), 'currency_id' => $request->old('currency_id', $journal->transaction_currency_id), 'destinationAccounts' => $destinationAccounts, - 'what' => strtolower(TransactionJournal::transactionTypeStr($journal)), + 'what' => strtolower($journal->transactionTypeStr()), 'date' => $request->old('date', $journal->date), 'tags' => join(',', $journal->tags->pluck('tag')->toArray()), @@ -265,8 +265,8 @@ class SplitController extends Controller // set initial category and/or budget: if (count($transactions) === 1 && $index === 0) { - $set['budget_id'] = TransactionJournal::budgetId($journal); - $set['category'] = TransactionJournal::categoryAsString($journal); + $set['budget_id'] = $journal->budgetId(); + $set['category'] = $journal->categoryAsString(); } $return[] = $set; diff --git a/app/Models/Tag.php b/app/Models/Tag.php index 769ea2c5af..3ad3178855 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -14,7 +14,8 @@ declare(strict_types = 1); namespace FireflyIII\Models; use Crypt; -use FireflyIII\Support\Models\TagSupport; +use FireflyIII\Support\Models\TagTrait; +use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Watson\Validating\ValidatingTrait; @@ -24,8 +25,10 @@ use Watson\Validating\ValidatingTrait; * * @package FireflyIII\Models */ -class Tag extends TagSupport +class Tag extends Model { + use ValidatingTrait, SoftDeletes, TagTrait; + /** * The attributes that should be casted to native types. * @@ -44,7 +47,6 @@ class Tag extends TagSupport protected $fillable = ['user_id', 'tag', 'date', 'description', 'longitude', 'latitude', 'zoomLevel', 'tagMode']; protected $rules = ['tag' => 'required|between:1,200',]; - use ValidatingTrait, SoftDeletes; /** * @param array $fields @@ -103,7 +105,7 @@ class Tag extends TagSupport $sum = '0'; /** @var TransactionJournal $journal */ foreach ($tag->transactionjournals as $journal) { - bcadd($sum, TransactionJournal::amount($journal)); + bcadd($sum, $journal->amount()); } return $sum; diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php index 1808955271..64d3899a03 100644 --- a/app/Models/TransactionJournal.php +++ b/app/Models/TransactionJournal.php @@ -16,8 +16,9 @@ namespace FireflyIII\Models; use Carbon\Carbon; use Crypt; use FireflyIII\Support\CacheProperties; -use FireflyIII\Support\Models\TransactionJournalSupport; +use FireflyIII\Support\Models\TransactionJournalTrait; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; +use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\SoftDeletes; use Log; @@ -30,9 +31,9 @@ use Watson\Validating\ValidatingTrait; * * @package FireflyIII\Models */ -class TransactionJournal extends TransactionJournalSupport +class TransactionJournal extends Model { - use SoftDeletes, ValidatingTrait; + use SoftDeletes, ValidatingTrait, TransactionJournalTrait; /** * The attributes that should be casted to native types. diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php index 43874982fa..2e5f692bc4 100644 --- a/app/Repositories/Bill/BillRepository.php +++ b/app/Repositories/Bill/BillRepository.php @@ -251,7 +251,7 @@ class BillRepository implements BillRepositoryInterface $count = strval($journals->count()); /** @var TransactionJournal $journal */ foreach ($journals as $journal) { - $sum = bcadd($sum, TransactionJournal::amountPositive($journal)); + $sum = bcadd($sum, $journal->amountPositive()); } $avg = '0'; if ($journals->count() > 0) { @@ -370,7 +370,7 @@ class BillRepository implements BillRepositoryInterface $count = strval($journals->count()); /** @var TransactionJournal $journal */ foreach ($journals as $journal) { - $sum = bcadd($sum, TransactionJournal::amountPositive($journal)); + $sum = bcadd($sum, $journal->amountPositive()); } $avg = '0'; if ($journals->count() > 0) { @@ -478,15 +478,15 @@ class BillRepository implements BillRepositoryInterface if (false === $journal->isWithdrawal()) { return false; } - $destinationAccounts = TransactionJournal::destinationAccountList($journal); - $sourceAccounts = TransactionJournal::sourceAccountList($journal); + $destinationAccounts = $journal->destinationAccountList(); + $sourceAccounts = $journal->sourceAccountList(); $matches = explode(',', $bill->match); $description = strtolower($journal->description) . ' '; $description .= strtolower(join(' ', $destinationAccounts->pluck('name')->toArray())); $description .= strtolower(join(' ', $sourceAccounts->pluck('name')->toArray())); $wordMatch = $this->doWordMatch($matches, $description); - $amountMatch = $this->doAmountMatch(TransactionJournal::amountPositive($journal), $bill->amount_min, $bill->amount_max); + $amountMatch = $this->doAmountMatch($journal->amountPositive(), $bill->amount_min, $bill->amount_max); /* diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php index 3e19b968a9..cf022037a7 100644 --- a/app/Repositories/Tag/TagRepository.php +++ b/app/Repositories/Tag/TagRepository.php @@ -375,8 +375,8 @@ class TagRepository implements TagRepositoryInterface */ private function matchAll(TransactionJournal $journal, Tag $tag): bool { - $journalSources = join(',', array_unique(TransactionJournal::sourceAccountList($journal)->pluck('id')->toArray())); - $journalDestinations = join(',', array_unique(TransactionJournal::destinationAccountList($journal)->pluck('id')->toArray())); + $journalSources = join(',', array_unique($journal->sourceAccountList()->pluck('id')->toArray())); + $journalDestinations = join(',', array_unique($journal->destinationAccountList()->pluck('id')->toArray())); $match = true; $journals = $tag->transactionJournals()->get(['transaction_journals.*']); @@ -387,8 +387,8 @@ class TagRepository implements TagRepositoryInterface Log::debug(sprintf('Now existingcomparing new journal #%d to existing journal #%d', $journal->id, $existing->id)); // $checkAccount is the source_account for a withdrawal // $checkAccount is the destination_account for a deposit - $existingSources = join(',', array_unique(TransactionJournal::sourceAccountList($existing)->pluck('id')->toArray())); - $existingDestinations = join(',', array_unique(TransactionJournal::destinationAccountList($existing)->pluck('id')->toArray())); + $existingSources = join(',', array_unique($journal->sourceAccountList()->pluck('id')->toArray())); + $existingDestinations = join(',', array_unique($journal->destinationAccountList()->pluck('id')->toArray())); if ($existing->isWithdrawal() && $existingSources !== $journalDestinations) { /* diff --git a/app/Rules/Triggers/AmountExactly.php b/app/Rules/Triggers/AmountExactly.php index e7984deb02..9e69692bfe 100644 --- a/app/Rules/Triggers/AmountExactly.php +++ b/app/Rules/Triggers/AmountExactly.php @@ -58,7 +58,7 @@ final class AmountExactly extends AbstractTrigger implements TriggerInterface */ public function triggered(TransactionJournal $journal): bool { - $amount = $journal->destination_amount ?? TransactionJournal::amountPositive($journal); + $amount = $journal->destination_amount ?? $journal->amountPositive(); $compare = $this->triggerValue; $result = bccomp($amount, $compare); if ($result === 0) { diff --git a/app/Rules/Triggers/AmountLess.php b/app/Rules/Triggers/AmountLess.php index 1c856c6796..ba1f1cc709 100644 --- a/app/Rules/Triggers/AmountLess.php +++ b/app/Rules/Triggers/AmountLess.php @@ -58,7 +58,7 @@ final class AmountLess extends AbstractTrigger implements TriggerInterface */ public function triggered(TransactionJournal $journal): bool { - $amount = $journal->destination_amount ?? TransactionJournal::amountPositive($journal); + $amount = $journal->destination_amount ?? $journal->amountPositive(); $compare = $this->triggerValue; $result = bccomp($amount, $compare); if ($result === -1) { diff --git a/app/Rules/Triggers/AmountMore.php b/app/Rules/Triggers/AmountMore.php index d8a7ce1643..34b61d9e0e 100644 --- a/app/Rules/Triggers/AmountMore.php +++ b/app/Rules/Triggers/AmountMore.php @@ -64,7 +64,7 @@ final class AmountMore extends AbstractTrigger implements TriggerInterface */ public function triggered(TransactionJournal $journal): bool { - $amount = $journal->destination_amount ?? TransactionJournal::amountPositive($journal); + $amount = $journal->destination_amount ?? $journal->amountPositive(); $compare = $this->triggerValue; $result = bccomp($amount, $compare); if ($result === 1) { diff --git a/app/Rules/Triggers/ToAccountContains.php b/app/Rules/Triggers/ToAccountContains.php index d80c9ab4d5..816ff82c57 100644 --- a/app/Rules/Triggers/ToAccountContains.php +++ b/app/Rules/Triggers/ToAccountContains.php @@ -66,7 +66,7 @@ final class ToAccountContains extends AbstractTrigger implements TriggerInterfac $toAccountName = ''; /** @var Account $account */ - foreach (TransactionJournal::destinationAccountList($journal) as $account) { + foreach ($journal->destinationAccountList() as $account) { $toAccountName .= strtolower($account->name); } diff --git a/app/Rules/Triggers/ToAccountEnds.php b/app/Rules/Triggers/ToAccountEnds.php index f4fd676422..b75595ecf2 100644 --- a/app/Rules/Triggers/ToAccountEnds.php +++ b/app/Rules/Triggers/ToAccountEnds.php @@ -66,7 +66,7 @@ final class ToAccountEnds extends AbstractTrigger implements TriggerInterface $toAccountName = ''; /** @var Account $account */ - foreach (TransactionJournal::destinationAccountList($journal) as $account) { + foreach ($journal->destinationAccountList() as $account) { $toAccountName .= strtolower($account->name); } diff --git a/app/Rules/Triggers/ToAccountIs.php b/app/Rules/Triggers/ToAccountIs.php index 0a2fdd8ca9..a5faa3e6a9 100644 --- a/app/Rules/Triggers/ToAccountIs.php +++ b/app/Rules/Triggers/ToAccountIs.php @@ -66,7 +66,7 @@ final class ToAccountIs extends AbstractTrigger implements TriggerInterface $toAccountName = ''; /** @var Account $account */ - foreach (TransactionJournal::destinationAccountList($journal) as $account) { + foreach ($journal->destinationAccountList() as $account) { $toAccountName .= strtolower($account->name); } diff --git a/app/Rules/Triggers/ToAccountStarts.php b/app/Rules/Triggers/ToAccountStarts.php index f5f162c283..63e912b4fc 100644 --- a/app/Rules/Triggers/ToAccountStarts.php +++ b/app/Rules/Triggers/ToAccountStarts.php @@ -66,7 +66,7 @@ final class ToAccountStarts extends AbstractTrigger implements TriggerInterface $toAccountName = ''; /** @var Account $account */ - foreach (TransactionJournal::destinationAccountList($journal) as $account) { + foreach ($journal->destinationAccountList() as $account) { $toAccountName .= strtolower($account->name); } diff --git a/app/Support/Amount.php b/app/Support/Amount.php index 9434352b80..fd9103b6a4 100644 --- a/app/Support/Amount.php +++ b/app/Support/Amount.php @@ -187,7 +187,7 @@ class Amount { $currency = $journal->transactionCurrency; - return $this->formatAnything($currency, TransactionJournal::amount($journal), $coloured); + return $this->formatAnything($currency, $journal->amount(), $coloured); } /** diff --git a/app/Support/Binder/JournalList.php b/app/Support/Binder/JournalList.php index b36af5149c..a6f811d741 100644 --- a/app/Support/Binder/JournalList.php +++ b/app/Support/Binder/JournalList.php @@ -39,7 +39,11 @@ class JournalList implements BinderInterface $object = TransactionJournal::whereIn('transaction_journals.id', $ids) ->expanded() ->where('transaction_journals.user_id', auth()->user()->id) - ->get(TransactionJournal::queryFields()); + ->get([ + 'transaction_journals.*', + 'transaction_types.type AS transaction_type_type', + 'transaction_currencies.code AS transaction_currency_code', + ]); if ($object->count() > 0) { return $object; diff --git a/app/Support/Models/TagSupport.php b/app/Support/Models/TagTrait.php similarity index 65% rename from app/Support/Models/TagSupport.php rename to app/Support/Models/TagTrait.php index f053ef42cf..83d59ad292 100644 --- a/app/Support/Models/TagSupport.php +++ b/app/Support/Models/TagTrait.php @@ -1,10 +1,8 @@ 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; } diff --git a/app/Support/Models/TransactionJournalSupport.php b/app/Support/Models/TransactionJournalTrait.php similarity index 54% rename from app/Support/Models/TransactionJournalSupport.php rename to app/Support/Models/TransactionJournalTrait.php index 513fec2623..14a6dfbb3d 100644 --- a/app/Support/Models/TransactionJournalSupport.php +++ b/app/Support/Models/TransactionJournalTrait.php @@ -1,10 +1,8 @@ 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; diff --git a/app/Support/Twig/General.php b/app/Support/Twig/General.php index 5d339960fa..688f37a942 100644 --- a/app/Support/Twig/General.php +++ b/app/Support/Twig/General.php @@ -310,7 +310,7 @@ class General extends Twig_Extension { return new Twig_SimpleFunction( 'getAmount', function (TransactionJournal $journal): string { - return TransactionJournal::amount($journal); + return $journal->amount(); } ); } diff --git a/app/Support/Twig/Journal.php b/app/Support/Twig/Journal.php index 757264a240..6b70876014 100644 --- a/app/Support/Twig/Journal.php +++ b/app/Support/Twig/Journal.php @@ -47,7 +47,7 @@ class Journal extends Twig_Extension return $cache->get(); } - $list = TransactionJournal::destinationAccountList($journal); + $list = $journal->destinationAccountList(); $array = []; /** @var Account $entry */ foreach ($list as $entry) { diff --git a/tests/Feature/Controllers/Transaction/ConvertControllerTest.php b/tests/Feature/Controllers/Transaction/ConvertControllerTest.php index 47f8fb0070..dbb44283f0 100644 --- a/tests/Feature/Controllers/Transaction/ConvertControllerTest.php +++ b/tests/Feature/Controllers/Transaction/ConvertControllerTest.php @@ -12,7 +12,10 @@ declare(strict_types = 1); namespace Tests\Feature\Controllers\Transaction; +use FireflyIII\Models\AccountType; use FireflyIII\Models\TransactionJournal; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use Illuminate\Support\Collection; use Tests\TestCase; /** @@ -28,9 +31,12 @@ class ConvertControllerTest extends TestCase */ public function testIndexDepositTransfer() { - $deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first(); + // mock stuff: + $repository = $this->mock(AccountRepositoryInterface::class); + $repository->shouldReceive('getActiveAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->once()->andReturn(new Collection); $this->be($this->user()); + $deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first(); $response = $this->get(route('transactions.convert.index', ['transfer', $deposit->id])); $response->assertStatus(200); $response->assertSee('Convert a deposit into a transfer'); @@ -77,7 +83,7 @@ class ConvertControllerTest extends TestCase */ public function testIndexWithdrawalDeposit() { - $withdrawal= TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first(); + $withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first(); $this->be($this->user()); $response = $this->get(route('transactions.convert.index', ['deposit', $withdrawal->id])); $response->assertStatus(200); @@ -89,7 +95,7 @@ class ConvertControllerTest extends TestCase */ public function testIndexWithdrawalTransfer() { - $withdrawal= TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first(); + $withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first(); $this->be($this->user()); $response = $this->get(route('transactions.convert.index', ['transfer', $withdrawal->id])); $response->assertStatus(200); @@ -98,10 +104,12 @@ class ConvertControllerTest extends TestCase /** * @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::postIndex + * @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getSourceAccount + * @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getDestinationAccount */ public function testPostIndex() { - $withdrawal= TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first(); + $withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first(); // convert a withdrawal to a transfer. Requires the ID of another asset account. $data = [ 'destination_account_asset' => 2,