2017-06-26 06:16:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
//use App\Events\ExpenseWasCreated;
|
|
|
|
//use App\Events\ExpenseWasUpdated;
|
2017-06-26 11:45:42 +02:00
|
|
|
use App\Models\Traits\HasRecurrence;
|
2017-06-26 06:16:29 +02:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
use Laracasts\Presenter\PresentableTrait;
|
|
|
|
use Utils;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Expense.
|
|
|
|
*/
|
|
|
|
class RecurringExpense extends EntityModel
|
|
|
|
{
|
|
|
|
// Expenses
|
|
|
|
use SoftDeletes;
|
|
|
|
use PresentableTrait;
|
2017-06-26 11:45:42 +02:00
|
|
|
use HasRecurrence;
|
2017-06-26 06:16:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $dates = ['deleted_at'];
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $presenter = 'App\Ninja\Presenters\ExpensePresenter';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
|
|
|
'client_id',
|
|
|
|
'vendor_id',
|
|
|
|
'expense_currency_id',
|
|
|
|
//'invoice_currency_id',
|
|
|
|
//'exchange_rate',
|
|
|
|
'amount',
|
|
|
|
'private_notes',
|
|
|
|
'public_notes',
|
|
|
|
'expense_category_id',
|
|
|
|
'tax_rate1',
|
|
|
|
'tax_name1',
|
|
|
|
'tax_rate2',
|
|
|
|
'tax_name2',
|
2017-06-26 10:10:51 +02:00
|
|
|
'should_be_invoiced',
|
2017-06-26 15:25:11 +02:00
|
|
|
//'start_date',
|
|
|
|
//'end_date',
|
2017-06-26 10:10:51 +02:00
|
|
|
'frequency_id',
|
2017-06-26 06:16:29 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function expense_category()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\ExpenseCategory')->withTrashed();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function account()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Account');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\User')->withTrashed();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function vendor()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Vendor')->withTrashed();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function client()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Client')->withTrashed();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
if ($this->public_notes) {
|
|
|
|
return Utils::truncateString($this->public_notes, 16);
|
|
|
|
} else {
|
|
|
|
return '#' . $this->public_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getDisplayName()
|
|
|
|
{
|
|
|
|
return $this->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRoute()
|
|
|
|
{
|
|
|
|
return "/recurring_expenses/{$this->public_id}/edit";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getEntityType()
|
|
|
|
{
|
|
|
|
return ENTITY_RECURRING_EXPENSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function amountWithTax()
|
|
|
|
{
|
2018-01-23 19:32:11 +01:00
|
|
|
return $this->amount + Utils::calculateTaxes($this->amount, $this->tax_rate1, $this->tax_rate2);
|
2017-06-26 06:16:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RecurringExpense::creating(function ($expense) {
|
|
|
|
$expense->setNullValues();
|
|
|
|
});
|
|
|
|
|
|
|
|
RecurringExpense::created(function ($expense) {
|
|
|
|
//event(new ExpenseWasCreated($expense));
|
|
|
|
});
|
|
|
|
|
|
|
|
RecurringExpense::updating(function ($expense) {
|
|
|
|
$expense->setNullValues();
|
|
|
|
});
|
|
|
|
|
|
|
|
RecurringExpense::updated(function ($expense) {
|
|
|
|
//event(new ExpenseWasUpdated($expense));
|
|
|
|
});
|
|
|
|
|
|
|
|
RecurringExpense::deleting(function ($expense) {
|
|
|
|
$expense->setNullValues();
|
|
|
|
});
|