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

277 lines
7.1 KiB
PHP
Raw Normal View History

2016-03-02 05:52:36 -06:00
<?php
/**
* 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.
2016-03-02 05:52:36 -06: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-02 12:41:22 -06:00
2016-03-20 11:12:34 -05:00
use Carbon\Carbon;
2016-04-29 10:26:59 -05:00
use FireflyIII\Exceptions\FireflyException;
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;
use Illuminate\Support\Collection;
2016-03-02 12:41:22 -06:00
2016-03-02 05:52:36 -06:00
/**
* Class TransactionJournalTrait
2016-03-02 05:52:36 -06:00
*
2017-03-04 04:20:57 -06:00
* @property int $id
* @method Collection transactions()
* @method bool isWithdrawal()
*
2016-03-02 12:41:22 -06:00
* @package FireflyIII\Support\Models
2016-03-02 05:52:36 -06:00
*/
trait TransactionJournalTrait
2016-03-02 05:52:36 -06:00
{
/**
* @return string
* @throws FireflyException
*/
public function amount(): string
{
$cache = new CacheProperties;
$cache->addProperty($this->id);
2016-03-02 13:19:39 -06:00
$cache->addProperty('transaction-journal');
$cache->addProperty('amount');
if ($cache->has()) {
2017-03-04 04:20:57 -06:00
return $cache->get(); // @codeCoverageIgnore
}
// saves on queries:
$amount = $this->transactions()->where('amount', '>', 0)->get()->sum('amount');
if ($this->isWithdrawal()) {
2016-04-30 12:50:42 -05:00
$amount = $amount * -1;
}
2016-04-30 12:50:42 -05:00
$amount = strval($amount);
$cache->store($amount);
return $amount;
}
/**
* @return string
*/
public function amountPositive(): string
{
$cache = new CacheProperties;
$cache->addProperty($this->id);
2016-03-02 13:19:39 -06:00
$cache->addProperty('transaction-journal');
$cache->addProperty('amount-positive');
if ($cache->has()) {
2017-03-04 04:20:57 -06:00
return $cache->get(); // @codeCoverageIgnore
}
2016-05-12 03:38:44 -05:00
// saves on queries:
$amount = $this->transactions()->where('amount', '>', 0)->get()->sum('amount');
2016-05-12 03:38:44 -05:00
$amount = strval($amount);
$cache->store($amount);
return $amount;
}
2016-03-20 11:12:34 -05:00
/**
* @return int
*/
public function budgetId(): int
2016-03-20 11:12:34 -05:00
{
$budget = $this->budgets()->first();
2016-03-20 11:12:34 -05:00
if (!is_null($budget)) {
return $budget->id;
}
return 0;
}
/**
* @return string
*/
public function categoryAsString(): string
2016-03-20 11:12:34 -05:00
{
$category = $this->categories()->first();
2016-03-20 11:12:34 -05:00
if (!is_null($category)) {
return $category->name;
}
return '';
}
/**
* @param string $dateField
*
* @return string
*/
public function dateAsString(string $dateField = ''): string
2016-03-20 11:12:34 -05:00
{
if ($dateField === '') {
return $this->date->format('Y-m-d');
2016-03-20 11:12:34 -05:00
}
if (!is_null($this->$dateField) && $this->$dateField instanceof Carbon) {
// make field NULL
$carbon = clone $this->$dateField;
$this->$dateField = null;
$this->save();
// create meta entry
$this->setMeta($dateField, $carbon);
// return that one instead.
return $carbon->format('Y-m-d');
}
$metaField = $this->getMeta($dateField);
if (!is_null($metaField)) {
$carbon = new Carbon($metaField);
return $carbon->format('Y-m-d');
2016-03-20 11:12:34 -05:00
}
return '';
}
/**
* @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);
}
$cache->store($list);
return $list;
}
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;
}
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
*/
public 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-20 11:12:34 -05:00
/**
* @return int
*/
public function piggyBankId(): int
2016-03-20 11:12:34 -05:00
{
if ($this->piggyBankEvents()->count() > 0) {
return $this->piggyBankEvents()->orderBy('date', 'DESC')->first()->piggy_bank_id;
2016-03-20 11:12:34 -05:00
}
return 0;
}
/**
* @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);
}
$cache->store($list);
return $list;
}
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;
}
/**
* @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;
}
2016-03-03 02:38:16 -06:00
}