mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Clean up method relying on old transaction journal code.
This commit is contained in:
@@ -10,6 +10,10 @@
|
||||
namespace FireflyIII\Support\Models;
|
||||
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Support\CacheProperties;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
@@ -20,6 +24,84 @@ use Illuminate\Database\Eloquent\Model;
|
||||
*/
|
||||
class TransactionJournalSupport extends Model
|
||||
{
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function amount(TransactionJournal $journal): string
|
||||
{
|
||||
$cache = new CacheProperties;
|
||||
$cache->addProperty($journal->id);
|
||||
$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);
|
||||
$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
|
||||
{
|
||||
$account = $journal->transactions()->where('amount', '>', 0)->first()->account;
|
||||
|
||||
return $account ?? new Account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function destinationAccountTypeStr(TransactionJournal $journal): string
|
||||
{
|
||||
$account = self::destinationAccount($journal);
|
||||
$type = $account->accountType ? $account->accountType->type : '(unknown)';
|
||||
|
||||
return $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder $query
|
||||
* @param string $table
|
||||
@@ -41,4 +123,40 @@ class TransactionJournalSupport extends Model
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Account
|
||||
*/
|
||||
public static function sourceAccount(TransactionJournal $journal): Account
|
||||
{
|
||||
$account = $journal->transactions()->where('amount', '<', 0)->first()->account;
|
||||
|
||||
return $account ?? new Account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function sourceAccountTypeStr(TransactionJournal $journal): string
|
||||
{
|
||||
$account = self::sourceAccount($journal);
|
||||
$type = $account->accountType ? $account->accountType->type : '(unknown)';
|
||||
|
||||
return $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function transactionTypeStr(TransactionJournal $journal): string
|
||||
{
|
||||
return $journal->transaction_type_type ?? $journal->transactionType->type;
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user