Should cover models.

This commit is contained in:
James Cole
2015-05-24 11:13:46 +02:00
parent 8f2f912cdf
commit 6c71f68ed8
9 changed files with 555 additions and 116 deletions

View File

@@ -1,6 +1,5 @@
<?php namespace FireflyIII\Models;
use App;
use Crypt;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
@@ -51,12 +50,6 @@ class Account extends Model
}
// create it!
$account = Account::create($fields);
if (is_null($account->id)) {
// could not create account:
App::abort(500, 'Could not create new account with data: ' . json_encode($fields) . ' because ' . json_encode($account->getErrors()));
}
return $account;

View File

@@ -42,11 +42,6 @@ class Category extends Model
}
// create it!
$category = Category::create($fields);
if (is_null($category->id)) {
// could not create account:
App::abort(500, 'Could not create new category with data: ' . json_encode($fields));
}
return $category;

View File

@@ -57,12 +57,6 @@ class Tag extends Model
$fields['tagMode'] = 'nothing';
$fields['description'] = isset($fields['description']) && !is_null($fields['description']) ? $fields['description'] : '';
$tag = Tag::create($fields);
if (is_null($tag->id)) {
// could not create account:
App::abort(500, 'Could not create new tag with data: ' . json_encode($fields) . ' because ' . json_encode($tag->getErrors()));
}
return $tag;

View File

@@ -59,15 +59,15 @@ class TransactionJournal extends Model
}
/**
* @return float
* @return string
*/
public function getActualAmountAttribute()
{
$amount = 0;
$amount = '0';
/** @var Transaction $t */
foreach ($this->transactions as $t) {
if ($t->amount > 0) {
$amount = floatval($t->amount);
$amount = $t->amount;
}
}
@@ -79,11 +79,12 @@ class TransactionJournal extends Model
*/
public function getAmountAttribute()
{
$amount = 0;
$amount = '0';
bcscale(2);
/** @var Transaction $t */
foreach ($this->transactions as $t) {
if ($t->amount > 0) {
$amount = floatval($t->amount);
$amount = $t->amount;
}
}
@@ -93,16 +94,16 @@ class TransactionJournal extends Model
if ($this->tags->count() == 0) {
return $amount;
}
// if journal is part of advancePayment AND journal is a withdrawal,
// then journal is being repaid by other journals, so the actual amount will lower:
/** @var Tag $advancePayment */
$advancePayment = $this->tags()->where('tagMode', 'advancePayment')->first();
if ($advancePayment && $this->transactionType->type == 'Withdrawal') {
// loop other deposits, remove from our amount.
$others = $advancePayment->transactionJournals()->transactionTypes(['Deposit'])->get();
foreach ($others as $other) {
$amount -= $other->actualAmount;
$amount = bcsub($amount, $other->actualAmount);
}
return $amount;
@@ -111,25 +112,24 @@ class TransactionJournal extends Model
// if this journal is part of an advancePayment AND the journal is a deposit,
// then the journal amount is correcting a withdrawal, and the amount is zero:
if ($advancePayment && $this->transactionType->type == 'Deposit') {
return 0;
return '0';
}
// is balancing act?
$balancingAct = $this->tags()->where('tagMode', 'balancingAct')->first();
if ($balancingAct) {
// this is the transfer
if ($balancingAct) {
// this is the expense:
if ($this->transactionType->type == 'Withdrawal') {
$transfer = $balancingAct->transactionJournals()->transactionTypes(['Transfer'])->first();
if ($transfer) {
$amount -= $transfer->actualAmount;
$amount = bcsub($amount, $transfer->actualAmount);
return $amount;
}
}
}
} // @codeCoverageIgnore
} // @codeCoverageIgnore
return $amount;
}
@@ -180,16 +180,16 @@ class TransactionJournal extends Model
*/
public function getCorrectedActualAmountAttribute()
{
$amount = 0;
$amount = '0';
$type = $this->transactionType->type;
/** @var Transaction $t */
foreach ($this->transactions as $t) {
if ($t->amount > 0 && $type != 'Withdrawal') {
$amount = floatval($t->amount);
$amount = $t->amount;
break;
}
if ($t->amount < 0 && $type == 'Withdrawal') {
$amount = floatval($t->amount);
$amount = $t->amount;
break;
}
}