2016-03-02 05:52:36 -06:00
|
|
|
<?php
|
|
|
|
/**
|
2017-03-04 00:18:35 -06:00
|
|
|
* 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.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-03-02 05:52:36 -06:00
|
|
|
*/
|
|
|
|
|
2016-05-20 05:41:23 -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-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;
|
2016-03-02 13:11:28 -06:00
|
|
|
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-05-06 03:32:26 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2016-03-02 12:41:22 -06:00
|
|
|
|
2016-03-02 05:52:36 -06:00
|
|
|
/**
|
2017-03-04 00:18:35 -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
|
|
|
*/
|
2017-03-04 00:18:35 -06:00
|
|
|
trait TransactionJournalTrait
|
2016-03-02 05:52:36 -06:00
|
|
|
{
|
2016-03-02 13:11:28 -06:00
|
|
|
/**
|
|
|
|
* @return string
|
2016-04-29 14:36:59 -05:00
|
|
|
* @throws FireflyException
|
2016-03-02 13:11:28 -06:00
|
|
|
*/
|
2017-03-04 00:18:35 -06:00
|
|
|
public function amount(): string
|
2016-03-02 13:11:28 -06:00
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
2017-03-04 00:18:35 -06:00
|
|
|
$cache->addProperty($this->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()) {
|
2017-03-04 04:20:57 -06:00
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
2016-03-02 13:11:28 -06:00
|
|
|
}
|
|
|
|
|
2016-05-05 11:59:46 -05:00
|
|
|
// saves on queries:
|
2017-03-04 00:18:35 -06:00
|
|
|
$amount = $this->transactions()->where('amount', '>', 0)->get()->sum('amount');
|
2016-05-05 11:59:46 -05:00
|
|
|
|
2017-03-04 00:18:35 -06:00
|
|
|
if ($this->isWithdrawal()) {
|
2016-04-30 12:50:42 -05:00
|
|
|
$amount = $amount * -1;
|
2016-03-02 13:11:28 -06:00
|
|
|
}
|
2016-04-30 12:50:42 -05:00
|
|
|
$amount = strval($amount);
|
2016-03-02 13:11:28 -06:00
|
|
|
$cache->store($amount);
|
|
|
|
|
|
|
|
return $amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-03-04 00:18:35 -06:00
|
|
|
public function amountPositive(): string
|
2016-03-02 13:11:28 -06:00
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
2017-03-04 00:18:35 -06:00
|
|
|
$cache->addProperty($this->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()) {
|
2017-03-04 04:20:57 -06:00
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
2016-03-02 13:11:28 -06:00
|
|
|
}
|
|
|
|
|
2016-05-12 03:38:44 -05:00
|
|
|
// saves on queries:
|
2017-03-04 00:18:35 -06:00
|
|
|
$amount = $this->transactions()->where('amount', '>', 0)->get()->sum('amount');
|
2016-05-12 03:38:44 -05:00
|
|
|
|
|
|
|
$amount = strval($amount);
|
2016-03-02 13:11:28 -06:00
|
|
|
$cache->store($amount);
|
|
|
|
|
|
|
|
return $amount;
|
|
|
|
}
|
|
|
|
|
2016-03-20 11:12:34 -05:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
2017-03-04 00:18:35 -06:00
|
|
|
public function budgetId(): int
|
2016-03-20 11:12:34 -05:00
|
|
|
{
|
2017-03-04 00:18:35 -06:00
|
|
|
$budget = $this->budgets()->first();
|
2016-03-20 11:12:34 -05:00
|
|
|
if (!is_null($budget)) {
|
|
|
|
return $budget->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-03-04 00:18:35 -06:00
|
|
|
public function categoryAsString(): string
|
2016-03-20 11:12:34 -05:00
|
|
|
{
|
2017-03-04 00:18:35 -06: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
|
|
|
|
*/
|
2017-03-04 00:18:35 -06:00
|
|
|
public function dateAsString(string $dateField = ''): string
|
2016-03-20 11:12:34 -05:00
|
|
|
{
|
|
|
|
if ($dateField === '') {
|
2017-03-04 00:18:35 -06:00
|
|
|
return $this->date->format('Y-m-d');
|
2016-03-20 11:12:34 -05:00
|
|
|
}
|
2017-03-04 00:18:35 -06:00
|
|
|
if (!is_null($this->$dateField) && $this->$dateField instanceof Carbon) {
|
2016-09-09 04:19:40 -05:00
|
|
|
// make field NULL
|
2017-03-04 00:18:35 -06:00
|
|
|
$carbon = clone $this->$dateField;
|
|
|
|
$this->$dateField = null;
|
|
|
|
$this->save();
|
2016-09-09 04:19:40 -05:00
|
|
|
|
|
|
|
// create meta entry
|
2017-03-04 00:18:35 -06:00
|
|
|
$this->setMeta($dateField, $carbon);
|
2016-09-09 04:19:40 -05:00
|
|
|
|
|
|
|
// return that one instead.
|
|
|
|
return $carbon->format('Y-m-d');
|
|
|
|
}
|
2017-03-04 00:18:35 -06:00
|
|
|
$metaField = $this->getMeta($dateField);
|
2016-09-09 04:19:40 -05:00
|
|
|
if (!is_null($metaField)) {
|
|
|
|
$carbon = new Carbon($metaField);
|
|
|
|
|
|
|
|
return $carbon->format('Y-m-d');
|
2016-03-20 11:12:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-06 03:32:26 -05:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2017-03-04 00:18:35 -06:00
|
|
|
public function destinationAccountList(): Collection
|
2016-05-06 03:32:26 -05:00
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
2017-03-04 00:18:35 -06:00
|
|
|
$cache->addProperty($this->id);
|
2016-05-06 03:32:26 -05:00
|
|
|
$cache->addProperty('transaction-journal');
|
|
|
|
$cache->addProperty('destination-account-list');
|
|
|
|
if ($cache->has()) {
|
2017-03-04 04:20:57 -06:00
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
2016-05-06 03:32:26 -05:00
|
|
|
}
|
2017-03-04 00:18:35 -06:00
|
|
|
$transactions = $this->transactions()->where('amount', '>', 0)->orderBy('transactions.account_id')->with('account')->get();
|
2016-05-06 03:32:26 -05:00
|
|
|
$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
|
|
|
|
*/
|
2017-03-04 00:18:35 -06:00
|
|
|
public function destinationTransactionList(): Collection
|
2016-05-15 08:24:23 -05:00
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
2017-03-04 00:18:35 -06:00
|
|
|
$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
|
|
|
}
|
2017-03-04 00:18:35 -06: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
|
|
|
|
*/
|
2017-03-04 00:18:35 -06:00
|
|
|
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
|
|
|
|
*/
|
2017-03-04 00:18:35 -06:00
|
|
|
public function piggyBankId(): int
|
2016-03-20 11:12:34 -05:00
|
|
|
{
|
2017-03-04 00:18:35 -06: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;
|
|
|
|
}
|
|
|
|
|
2016-04-29 14:36:59 -05:00
|
|
|
/**
|
2016-05-06 03:32:26 -05:00
|
|
|
* @return Collection
|
|
|
|
*/
|
2017-03-04 00:18:35 -06:00
|
|
|
public function sourceAccountList(): Collection
|
2016-05-06 03:32:26 -05:00
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
2017-03-04 00:18:35 -06:00
|
|
|
$cache->addProperty($this->id);
|
2016-05-06 03:32:26 -05:00
|
|
|
$cache->addProperty('transaction-journal');
|
|
|
|
$cache->addProperty('source-account-list');
|
|
|
|
if ($cache->has()) {
|
2017-03-04 04:20:57 -06:00
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
2016-05-06 03:32:26 -05:00
|
|
|
}
|
2017-03-04 00:18:35 -06:00
|
|
|
$transactions = $this->transactions()->where('amount', '<', 0)->orderBy('transactions.account_id')->with('account')->get();
|
2016-05-06 03:32:26 -05:00
|
|
|
$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
|
|
|
|
*/
|
2017-03-04 00:18:35 -06:00
|
|
|
public function sourceTransactionList(): Collection
|
2016-05-14 06:51:33 -05:00
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
2017-03-04 00:18:35 -06:00
|
|
|
$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
|
|
|
}
|
2017-03-04 00:18:35 -06:00
|
|
|
$list = $this->transactions()->where('amount', '<', 0)->with('account')->get();
|
2016-05-14 06:51:33 -05:00
|
|
|
$cache->store($list);
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2016-03-02 13:11:28 -06:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-03-04 00:18:35 -06:00
|
|
|
public function transactionTypeStr(): string
|
2016-03-02 13:11:28 -06:00
|
|
|
{
|
2016-03-02 13:19:39 -06:00
|
|
|
$cache = new CacheProperties;
|
2017-03-04 00:18:35 -06:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2017-03-04 00:18:35 -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-02 13:11:28 -06:00
|
|
|
}
|
2016-03-03 02:38:16 -06:00
|
|
|
}
|