2016-03-02 05:52:36 -06:00
|
|
|
<?php
|
|
|
|
/**
|
2016-03-02 12:41:22 -06:00
|
|
|
* TransactionJournalSupport.php
|
2016-03-02 05:52:36 -06:00
|
|
|
* Copyright (C) 2016 Sander Dorigo
|
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
|
|
|
* of the MIT license. See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
2016-03-02 12:41:22 -06:00
|
|
|
namespace FireflyIII\Support\Models;
|
2016-03-02 05:52:36 -06:00
|
|
|
|
2016-03-02 12:41:22 -06:00
|
|
|
|
2016-03-02 13:11:28 -06:00
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
use FireflyIII\Models\Transaction;
|
|
|
|
use FireflyIII\Models\TransactionJournal;
|
|
|
|
use FireflyIII\Support\CacheProperties;
|
2016-03-02 12:41:22 -06:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2016-03-02 05:52:36 -06:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2016-03-02 12:41:22 -06:00
|
|
|
|
2016-03-02 05:52:36 -06:00
|
|
|
/**
|
2016-03-02 12:41:22 -06:00
|
|
|
* Class TransactionJournalSupport
|
2016-03-02 05:52:36 -06:00
|
|
|
*
|
2016-03-02 12:41:22 -06:00
|
|
|
* @package FireflyIII\Support\Models
|
2016-03-02 05:52:36 -06:00
|
|
|
*/
|
2016-03-02 12:41:22 -06:00
|
|
|
class TransactionJournalSupport extends Model
|
2016-03-02 05:52:36 -06:00
|
|
|
{
|
2016-03-02 13:11:28 -06:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function amount(TransactionJournal $journal): string
|
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($journal->id);
|
2016-03-02 13:19:39 -06:00
|
|
|
$cache->addProperty('transaction-journal');
|
2016-03-02 13:11:28 -06:00
|
|
|
$cache->addProperty('amount');
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
|
|
|
bcscale(2);
|
|
|
|
$transaction = $journal->transactions->sortByDesc('amount')->first();
|
|
|
|
$amount = $transaction->amount;
|
|
|
|
if ($journal->isWithdrawal()) {
|
|
|
|
$amount = bcmul($amount, '-1');
|
|
|
|
}
|
|
|
|
$cache->store($amount);
|
|
|
|
|
|
|
|
return $amount;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function amountPositive(TransactionJournal $journal): string
|
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($journal->id);
|
2016-03-02 13:19:39 -06:00
|
|
|
$cache->addProperty('transaction-journal');
|
2016-03-02 13:11:28 -06:00
|
|
|
$cache->addProperty('amount-positive');
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
|
|
|
$amount = '0';
|
|
|
|
/** @var Transaction $t */
|
|
|
|
foreach ($journal->transactions as $t) {
|
|
|
|
if ($t->amount > 0) {
|
|
|
|
$amount = $t->amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$cache->store($amount);
|
|
|
|
|
|
|
|
return $amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
public static function destinationAccount(TransactionJournal $journal): Account
|
|
|
|
{
|
2016-03-02 13:19:39 -06:00
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($journal->id);
|
|
|
|
$cache->addProperty('transaction-journal');
|
|
|
|
$cache->addProperty('destination-account');
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
$transaction = $journal->transactions()->where('amount', '>', 0)->first();
|
|
|
|
if (!is_null($transaction)) {
|
|
|
|
$account = $transaction->account;
|
|
|
|
$cache->store($account);
|
|
|
|
} else {
|
|
|
|
$account = new Account;
|
|
|
|
}
|
2016-03-02 13:11:28 -06:00
|
|
|
|
2016-03-02 13:19:39 -06:00
|
|
|
return $account;
|
2016-03-02 13:11:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function destinationAccountTypeStr(TransactionJournal $journal): string
|
|
|
|
{
|
2016-03-02 13:19:39 -06:00
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($journal->id);
|
|
|
|
$cache->addProperty('transaction-journal');
|
|
|
|
$cache->addProperty('destination-account-type-str');
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
2016-03-02 13:11:28 -06:00
|
|
|
$account = self::destinationAccount($journal);
|
|
|
|
$type = $account->accountType ? $account->accountType->type : '(unknown)';
|
2016-03-02 13:19:39 -06:00
|
|
|
$cache->store($type);
|
2016-03-02 13:11:28 -06:00
|
|
|
|
|
|
|
return $type;
|
|
|
|
}
|
|
|
|
|
2016-03-02 05:52:36 -06:00
|
|
|
/**
|
2016-03-02 12:41:22 -06:00
|
|
|
* @param Builder $query
|
|
|
|
* @param string $table
|
2016-03-02 05:52:36 -06:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-03-02 12:41:22 -06:00
|
|
|
public static function isJoined(Builder $query, string $table):bool
|
2016-03-02 05:52:36 -06:00
|
|
|
{
|
|
|
|
$joins = $query->getQuery()->joins;
|
2016-03-02 12:41:22 -06:00
|
|
|
if (is_null($joins)) {
|
2016-03-02 05:52:36 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
foreach ($joins as $join) {
|
2016-03-02 12:41:22 -06:00
|
|
|
if ($join->table === $table) {
|
2016-03-02 05:52:36 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-03-02 12:41:22 -06:00
|
|
|
|
2016-03-02 05:52:36 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-02 12:41:22 -06:00
|
|
|
|
2016-03-02 13:11:28 -06:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
public static function sourceAccount(TransactionJournal $journal): Account
|
|
|
|
{
|
2016-03-02 13:19:39 -06:00
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($journal->id);
|
|
|
|
$cache->addProperty('transaction-journal');
|
|
|
|
$cache->addProperty('source-account');
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
$transaction = $journal->transactions()->where('amount', '<', 0)->first();
|
|
|
|
if (!is_null($transaction)) {
|
|
|
|
$account = $transaction->account;
|
|
|
|
$cache->store($account);
|
|
|
|
} else {
|
|
|
|
$account = new Account;
|
|
|
|
}
|
2016-03-02 13:11:28 -06:00
|
|
|
|
2016-03-02 13:19:39 -06:00
|
|
|
return $account;
|
2016-03-02 13:11:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function sourceAccountTypeStr(TransactionJournal $journal): string
|
|
|
|
{
|
2016-03-02 13:19:39 -06:00
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($journal->id);
|
|
|
|
$cache->addProperty('transaction-journal');
|
|
|
|
$cache->addProperty('source-account-type-str');
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
2016-03-02 13:11:28 -06:00
|
|
|
$account = self::sourceAccount($journal);
|
|
|
|
$type = $account->accountType ? $account->accountType->type : '(unknown)';
|
2016-03-02 13:19:39 -06:00
|
|
|
$cache->store($type);
|
2016-03-02 13:11:28 -06:00
|
|
|
|
|
|
|
return $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function transactionTypeStr(TransactionJournal $journal): string
|
|
|
|
{
|
2016-03-02 13:19:39 -06:00
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($journal->id);
|
|
|
|
$cache->addProperty('transaction-journal');
|
|
|
|
$cache->addProperty('type-string');
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
|
|
|
$typeStr = $journal->transaction_type_type ?? $journal->transactionType->type;
|
|
|
|
$cache->store($typeStr);
|
|
|
|
|
|
|
|
return $typeStr;
|
2016-03-02 13:11:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-02 05:52:36 -06:00
|
|
|
}
|