use journal repository instead of direct calls.

This commit is contained in:
James Cole
2018-02-25 19:09:05 +01:00
parent 99983a5c8f
commit 1b304bf85e
18 changed files with 175 additions and 222 deletions

View File

@@ -63,66 +63,6 @@ trait TransactionJournalTrait
return false;
}
/**
* @return string
*/
public function amount(): string
{
$cache = new CacheProperties;
$cache->addProperty($this->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('amount');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
// saves on queries:
$amount = $this->transactions()->where('amount', '>', 0)->get()->sum('amount');
if ($this->isWithdrawal()) {
$amount = $amount * -1;
}
$amount = strval($amount);
$cache->store($amount);
return $amount;
}
/**
* @return string
*/
public function amountPositive(): string
{
$cache = new CacheProperties;
$cache->addProperty($this->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('amount-positive');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
// saves on queries:
$amount = $this->transactions()->where('amount', '>', 0)->get()->sum('amount');
$amount = strval($amount);
$cache->store($amount);
return $amount;
}
/**
* @return int
*/
public function budgetId(): int
{
$budget = $this->budgets()->first();
if (null !== $budget) {
return $budget->id;
}
return 0;
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
@@ -133,51 +73,6 @@ trait TransactionJournalTrait
*/
abstract public function categories(): BelongsToMany;
/**
* @return string
*/
public function categoryAsString(): string
{
$category = $this->categories()->first();
if (null !== $category) {
return $category->name;
}
return '';
}
/**
* @param string $dateField
*
* @return string
*/
public function dateAsString(string $dateField = ''): string
{
if ('' === $dateField) {
return $this->date->format('Y-m-d');
}
if (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 (null !== $metaField) {
$carbon = new Carbon($metaField);
return $carbon->format('Y-m-d');
}
return '';
}
/**
* @return Collection
*/