firefly-iii/app/Support/Models/TransactionJournalTrait.php

177 lines
5.0 KiB
PHP
Raw Normal View History

2016-03-02 05:52:36 -06:00
<?php
/**
* TransactionJournalTrait.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-03-02 05:52:36 -06:00
*/
2017-03-24 09:15:12 -05:00
declare(strict_types=1);
2016-03-02 12:41:22 -06:00
namespace FireflyIII\Support\Models;
2016-03-02 05:52:36 -06:00
2016-03-20 11:12:34 -05:00
use Carbon\Carbon;
use FireflyIII\Models\Transaction;
2017-06-05 04:20:38 -05:00
use FireflyIII\Models\TransactionType;
use FireflyIII\Support\CacheProperties;
2016-03-02 12:41:22 -06:00
use Illuminate\Database\Eloquent\Builder;
2017-06-05 04:12:50 -05:00
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
2016-03-02 12:41:22 -06:00
2016-03-02 05:52:36 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class TransactionJournalTrait.
2016-03-02 05:52:36 -06:00
*
2017-06-05 04:12:50 -05:00
* @property int $id
* @property Carbon $date
* @property string $transaction_type_type
* @property TransactionType $transactionType
2016-03-02 05:52:36 -06:00
*/
trait TransactionJournalTrait
2016-03-02 05:52:36 -06:00
{
2018-01-25 11:41:27 -06:00
/**
* @param Builder $query
* @param string $table
*
* @return bool
*/
public static function isJoined(Builder $query, string $table): bool
{
$joins = $query->getQuery()->joins;
if (null === $joins) {
return false;
}
foreach ($joins as $join) {
if ($join->table === $table) {
return true;
}
}
return false;
}
/**
2018-02-28 14:32:59 -06:00
* @deprecated
* @return Collection
*/
public function destinationAccountList(): Collection
{
$cache = new CacheProperties;
$cache->addProperty($this->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('destination-account-list');
if ($cache->has()) {
2017-03-04 04:20:57 -06:00
return $cache->get(); // @codeCoverageIgnore
}
$transactions = $this->transactions()->where('amount', '>', 0)->orderBy('transactions.account_id')->with('account')->get();
$list = new Collection;
/** @var Transaction $t */
foreach ($transactions as $t) {
$list->push($t->account);
}
2017-03-04 08:29:20 -06:00
$list = $list->unique('id');
$cache->store($list);
return $list;
}
2016-05-15 08:24:23 -05:00
/**
2018-02-28 14:32:59 -06:00
* @deprecated
2016-05-15 08:24:23 -05:00
* @return Collection
*/
public function destinationTransactionList(): Collection
2016-05-15 08:24:23 -05:00
{
$cache = new CacheProperties;
$cache->addProperty($this->id);
2016-05-15 08:24:23 -05:00
$cache->addProperty('transaction-journal');
$cache->addProperty('destination-transaction-list');
if ($cache->has()) {
2017-03-04 04:20:57 -06:00
return $cache->get(); // @codeCoverageIgnore
2016-05-15 08:24:23 -05:00
}
$list = $this->transactions()->where('amount', '>', 0)->with('account')->get();
2016-05-15 08:24:23 -05:00
$cache->store($list);
return $list;
}
/**
2018-02-28 14:32:59 -06:00
* @deprecated
* @return Collection
*/
public function sourceAccountList(): Collection
{
$cache = new CacheProperties;
$cache->addProperty($this->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('source-account-list');
if ($cache->has()) {
2017-03-04 04:20:57 -06:00
return $cache->get(); // @codeCoverageIgnore
}
$transactions = $this->transactions()->where('amount', '<', 0)->orderBy('transactions.account_id')->with('account')->get();
$list = new Collection;
/** @var Transaction $t */
foreach ($transactions as $t) {
$list->push($t->account);
}
2017-03-04 08:29:20 -06:00
$list = $list->unique('id');
$cache->store($list);
return $list;
}
2016-05-14 06:51:33 -05:00
/**
2018-02-28 14:32:59 -06:00
* @deprecated
2016-05-14 06:51:33 -05:00
* @return Collection
*/
public function sourceTransactionList(): Collection
2016-05-14 06:51:33 -05:00
{
$cache = new CacheProperties;
$cache->addProperty($this->id);
2016-05-14 06:51:33 -05:00
$cache->addProperty('transaction-journal');
$cache->addProperty('source-transaction-list');
if ($cache->has()) {
2017-03-04 04:20:57 -06:00
return $cache->get(); // @codeCoverageIgnore
2016-05-14 06:51:33 -05:00
}
$list = $this->transactions()->where('amount', '<', 0)->with('account')->get();
2016-05-14 06:51:33 -05:00
$cache->store($list);
return $list;
}
/**
2018-02-28 14:32:59 -06:00
* @deprecated
* @return string
*/
public function transactionTypeStr(): string
{
2016-03-02 13:19:39 -06:00
$cache = new CacheProperties;
$cache->addProperty($this->id);
2016-03-02 13:19:39 -06:00
$cache->addProperty('transaction-journal');
$cache->addProperty('type-string');
if ($cache->has()) {
2017-03-04 04:20:57 -06:00
return $cache->get(); // @codeCoverageIgnore
2016-03-02 13:19:39 -06:00
}
$typeStr = $this->transaction_type_type ?? $this->transactionType->type;
2016-03-02 13:19:39 -06:00
$cache->store($typeStr);
return $typeStr;
}
2017-06-05 04:12:50 -05:00
/**
* @return HasMany
*/
abstract public function transactions(): HasMany;
2016-03-03 02:38:16 -06:00
}