1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00
invoiceninja/app/Models/Expense.php

202 lines
3.8 KiB
PHP
Raw Normal View History

2016-01-06 20:52:09 +01:00
<?php namespace App\Models;
use Laracasts\Presenter\PresentableTrait;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Events\ExpenseWasCreated;
2016-01-08 19:01:00 +01:00
use App\Events\ExpenseWasUpdated;
use App\Events\ExpenseWasDeleted;
2016-01-06 20:52:09 +01:00
/**
* Class Expense
*/
2016-01-06 20:52:09 +01:00
class Expense extends EntityModel
{
2016-01-07 16:14:11 +01:00
// Expenses
2016-01-06 20:52:09 +01:00
use SoftDeletes;
use PresentableTrait;
/**
* @var array
*/
2016-02-18 11:02:30 +01:00
protected $dates = ['deleted_at'];
/**
* @var string
*/
2016-01-06 20:52:09 +01:00
protected $presenter = 'App\Ninja\Presenters\ExpensePresenter';
/**
* @var array
*/
2016-01-08 19:01:00 +01:00
protected $fillable = [
2016-01-21 21:36:49 +01:00
'client_id',
'vendor_id',
2016-02-01 23:07:09 +01:00
'expense_currency_id',
'invoice_currency_id',
2016-01-08 19:01:00 +01:00
'amount',
'foreign_amount',
2016-01-08 19:01:00 +01:00
'exchange_rate',
'private_notes',
'public_notes',
2016-01-26 21:22:33 +01:00
'bank_id',
'transaction_id',
2016-07-05 20:49:47 +02:00
'expense_category_id',
];
2016-07-05 20:49:47 +02:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function expense_category()
{
return $this->belongsTo('App\Models\ExpenseCategory');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2016-01-06 20:52:09 +01:00
public function account()
{
return $this->belongsTo('App\Models\Account');
}
/**
* @return mixed
*/
2016-01-06 20:52:09 +01:00
public function user()
{
return $this->belongsTo('App\Models\User')->withTrashed();
2016-01-06 20:52:09 +01:00
}
/**
* @return mixed
*/
2016-01-06 20:52:09 +01:00
public function vendor()
{
return $this->belongsTo('App\Models\Vendor')->withTrashed();
}
/**
* @return mixed
*/
2016-01-21 20:15:30 +01:00
public function client()
{
return $this->belongsTo('App\Models\Client')->withTrashed();
}
/**
* @return mixed
*/
2016-01-21 23:29:10 +01:00
public function invoice()
{
return $this->belongsTo('App\Models\Invoice')->withTrashed();
}
/**
* @return mixed
*/
public function documents()
{
return $this->hasMany('App\Models\Document')->orderBy('id');
}
/**
* @return mixed
*/
2016-01-06 20:52:09 +01:00
public function getName()
{
2016-01-08 19:01:00 +01:00
if($this->expense_number)
return $this->expense_number;
2016-01-08 19:01:00 +01:00
return $this->public_id;
}
/**
* @return mixed
*/
2016-01-08 19:01:00 +01:00
public function getDisplayName()
{
return $this->getName();
2016-01-06 20:52:09 +01:00
}
/**
* @return string
*/
2016-01-08 19:01:00 +01:00
public function getRoute()
{
return "/expenses/{$this->public_id}";
}
/**
* @return mixed
*/
2016-01-06 20:52:09 +01:00
public function getEntityType()
{
return ENTITY_EXPENSE;
}
/**
* @return bool
*/
public function isExchanged()
2016-01-06 20:52:09 +01:00
{
return $this->invoice_currency_id != $this->expense_currency_id;
2016-01-06 20:52:09 +01:00
}
/**
* @return float
*/
public function convertedAmount()
{
return round($this->amount * $this->exchange_rate, 2);
}
2016-05-15 12:58:11 +02:00
/**
* @return array
*/
public function toArray()
{
$array = parent::toArray();
2016-05-15 12:58:11 +02:00
2016-03-27 19:09:59 +02:00
if(empty($this->visible) || in_array('converted_amount', $this->visible))$array['converted_amount'] = $this->convertedAmount();
2016-05-15 12:58:11 +02:00
return $array;
}
2016-05-15 12:58:11 +02:00
/**
* @param $query
* @param null $bankdId
* @return mixed
*/
2016-05-15 12:58:11 +02:00
public function scopeBankId($query, $bankdId = null)
{
if ($bankdId) {
$query->whereBankId($bankId);
}
return $query;
}
2016-01-06 20:52:09 +01:00
}
Expense::creating(function ($expense) {
$expense->setNullValues();
});
Expense::created(function ($expense) {
event(new ExpenseWasCreated($expense));
});
Expense::updating(function ($expense) {
$expense->setNullValues();
});
Expense::updated(function ($expense) {
event(new ExpenseWasUpdated($expense));
});
Expense::deleting(function ($expense) {
$expense->setNullValues();
});
2016-01-06 20:52:09 +01:00
Expense::deleted(function ($expense) {
event(new ExpenseWasDeleted($expense));
});