mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
These budget charts are the worst, I'm telling you.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<?php namespace FireflyIII\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Watson\Validating\ValidatingTrait;
|
||||
@@ -9,20 +9,20 @@ use Watson\Validating\ValidatingTrait;
|
||||
/**
|
||||
* FireflyIII\Models\Transaction
|
||||
*
|
||||
* @property integer $id
|
||||
* @property \Carbon\Carbon $created_at
|
||||
* @property \Carbon\Carbon $updated_at
|
||||
* @property \Carbon\Carbon $deleted_at
|
||||
* @property integer $account_id
|
||||
* @property integer $transaction_journal_id
|
||||
* @property string $description
|
||||
* @property float $amount
|
||||
* @property-read Account $account
|
||||
* @property-read TransactionJournal $transactionJournal
|
||||
* @property integer $id
|
||||
* @property \Carbon\Carbon $created_at
|
||||
* @property \Carbon\Carbon $updated_at
|
||||
* @property \Carbon\Carbon $deleted_at
|
||||
* @property integer $account_id
|
||||
* @property integer $transaction_journal_id
|
||||
* @property string $description
|
||||
* @property float $amount
|
||||
* @property-read Account $account
|
||||
* @property-read TransactionJournal $transactionJournal
|
||||
* @method static \Illuminate\Database\Query\Builder|Transaction after($date)
|
||||
* @method static \Illuminate\Database\Query\Builder|Transaction before($date)
|
||||
* @property float $before
|
||||
* @property float $after
|
||||
* @property float $before
|
||||
* @property float $after
|
||||
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Transaction whereId($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Transaction whereCreatedAt($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Transaction whereUpdatedAt($value)
|
||||
@@ -32,12 +32,13 @@ use Watson\Validating\ValidatingTrait;
|
||||
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Transaction whereDescription($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Transaction whereAmount($value)
|
||||
* @mixin \Eloquent
|
||||
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Budget[] $budgets
|
||||
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Budget[] $budgets
|
||||
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Category[] $categories
|
||||
*/
|
||||
class Transaction extends Model
|
||||
{
|
||||
|
||||
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
|
||||
protected $fillable = ['account_id', 'transaction_journal_id', 'description', 'amount'];
|
||||
protected $hidden = ['encrypted'];
|
||||
protected $rules
|
||||
@@ -47,10 +48,30 @@ class Transaction extends Model
|
||||
'description' => 'between:1,255',
|
||||
'amount' => 'required|numeric',
|
||||
];
|
||||
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
|
||||
|
||||
use SoftDeletes, ValidatingTrait;
|
||||
|
||||
/**
|
||||
* @param Builder $query
|
||||
* @param string $table
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isJoined(Builder $query, string $table):bool
|
||||
{
|
||||
$joins = $query->getQuery()->joins;
|
||||
if (is_null($joins)) {
|
||||
return false;
|
||||
}
|
||||
foreach ($joins as $join) {
|
||||
if ($join->table === $table) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
@@ -59,54 +80,6 @@ class Transaction extends Model
|
||||
return $this->belongsTo('FireflyIII\Models\Account');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*
|
||||
* @return float|int
|
||||
*/
|
||||
public function getAmountAttribute($value)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param EloquentBuilder $query
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function scopeAfter(EloquentBuilder $query, Carbon $date)
|
||||
{
|
||||
return $query->where('transaction_journals.date', '>=', $date->format('Y-m-d 00:00:00'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param EloquentBuilder $query
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function scopeBefore(EloquentBuilder $query, Carbon $date)
|
||||
{
|
||||
return $query->where('transaction_journals.date', '<=', $date->format('Y-m-d 00:00:00'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*/
|
||||
public function setAmountAttribute($value)
|
||||
{
|
||||
$this->attributes['amount'] = strval(round($value, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function transactionJournal()
|
||||
{
|
||||
return $this->belongsTo('FireflyIII\Models\TransactionJournal');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
*/
|
||||
@@ -122,4 +95,74 @@ class Transaction extends Model
|
||||
{
|
||||
return $this->belongsToMany('FireflyIII\Models\Category');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*
|
||||
* @return float|int
|
||||
*/
|
||||
public function getAmountAttribute($value)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param Builder $query
|
||||
* @param Carbon $date
|
||||
*/
|
||||
public function scopeAfter(Builder $query, Carbon $date)
|
||||
{
|
||||
if (!self::isJoined($query, 'transaction_journals')) {
|
||||
$query->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id');
|
||||
}
|
||||
$query->where('transaction_journals.date', '>=', $date->format('Y-m-d 00:00:00'));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param Builder $query
|
||||
* @param Carbon $date
|
||||
*
|
||||
*/
|
||||
public function scopeBefore(Builder $query, Carbon $date)
|
||||
{
|
||||
if (!self::isJoined($query, 'transaction_journals')) {
|
||||
$query->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id');
|
||||
}
|
||||
$query->where('transaction_journals.date', '<=', $date->format('Y-m-d 00:00:00'));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param Builder $query
|
||||
* @param array $types
|
||||
*/
|
||||
public function scopeTransactionTypes(Builder $query, array $types)
|
||||
{
|
||||
if (!self::isJoined($query, 'transaction_journals')) {
|
||||
$query->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id');
|
||||
}
|
||||
|
||||
if (!self::isJoined($query, 'transaction_types')) {
|
||||
$query->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id');
|
||||
}
|
||||
$query->whereIn('transaction_types.type', $types);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*/
|
||||
public function setAmountAttribute($value)
|
||||
{
|
||||
$this->attributes['amount'] = strval(round($value, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function transactionJournal()
|
||||
{
|
||||
return $this->belongsTo('FireflyIII\Models\TransactionJournal');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user