/
RecurringExpense.php
154 lines (134 loc) · 3.07 KB
/
RecurringExpense.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace App\Models;
//use App\Events\ExpenseWasCreated;
//use App\Events\ExpenseWasUpdated;
use App\Models\Traits\HasRecurrence;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laracasts\Presenter\PresentableTrait;
use Utils;
/**
* Class Expense.
*/
class RecurringExpense extends EntityModel
{
// Expenses
use SoftDeletes;
use PresentableTrait;
use HasRecurrence;
/**
* @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',
'should_be_invoiced',
//'start_date',
//'end_date',
'frequency_id',
];
/**
* @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()
{
return $this->amount + Utils::calculateTaxes($this->amount, $this->tax_rate1, $this->tax_rate2);
}
}
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();
});