Bill.php
4.52 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace App\Models\Expense;
use App\Models\Model;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Media;
use App\Traits\Recurring;
use Bkwld\Cloner\Cloneable;
use Sofa\Eloquence\Eloquence;
use Date;
class Bill extends Model
{
use Cloneable, Currencies, DateTime, Eloquence, Media, Recurring;
protected $table = 'bills';
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['attachment', 'discount'];
protected $dates = ['deleted_at', 'billed_at', 'due_at'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'bill_number', 'order_number', 'bill_status_code', 'billed_at', 'due_at', 'amount', 'currency_code', 'currency_rate', 'vendor_id', 'vendor_name', 'vendor_email', 'vendor_tax_number', 'vendor_phone', 'vendor_address', 'notes', 'category_id', 'parent_id'];
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['bill_number', 'vendor_name', 'amount', 'status.name', 'billed_at', 'due_at', 'bill_status_code'];
/**
* Searchable rules.
*
* @var array
*/
protected $searchableColumns = [
'bill_number' => 10,
'order_number' => 10,
'vendor_name' => 10,
'vendor_email' => 5,
'vendor_phone' => 2,
'vendor_address' => 1,
'notes' => 2,
];
/**
* Clonable relationships.
*
* @var array
*/
public $cloneable_relations = ['items', 'recurring', 'totals'];
public function category()
{
return $this->belongsTo('App\Models\Setting\Category');
}
public function currency()
{
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
}
public function histories()
{
return $this->hasMany('App\Models\Expense\BillHistory');
}
public function items()
{
return $this->hasMany('App\Models\Expense\BillItem');
}
public function payments()
{
return $this->hasMany('App\Models\Expense\BillPayment');
}
public function recurring()
{
return $this->morphOne('App\Models\Common\Recurring', 'recurable');
}
public function status()
{
return $this->belongsTo('App\Models\Expense\BillStatus', 'bill_status_code', 'code');
}
public function totals()
{
return $this->hasMany('App\Models\Expense\BillTotal');
}
public function vendor()
{
return $this->belongsTo('App\Models\Expense\Vendor');
}
public function scopeDue($query, $date)
{
return $query->where('due_at', '=', $date);
}
public function scopeLatest($query)
{
return $query->orderBy('paid_at', 'desc');
}
public function scopeAccrued($query)
{
return $query->where('bill_status_code', '<>', 'draft');
}
public function scopePaid($query)
{
return $query->where('bill_status_code', '=', 'paid');
}
public function scopeNotPaid($query)
{
return $query->where('bill_status_code', '<>', 'paid');
}
public function onCloning($src, $child = null)
{
$this->bill_status_code = 'draft';
}
/**
* Convert amount to double.
*
* @param string $value
* @return void
*/
public function setAmountAttribute($value)
{
$this->attributes['amount'] = (double) $value;
}
/**
* Convert currency rate to double.
*
* @param string $value
* @return void
*/
public function setCurrencyRateAttribute($value)
{
$this->attributes['currency_rate'] = (double) $value;
}
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value)
{
if (!empty($value) && !$this->hasMedia('attachment')) {
return $value;
} elseif (!$this->hasMedia('attachment')) {
return false;
}
return $this->getMedia('attachment')->last();
}
/**
* Get the discount percentage.
*
* @return string
*/
public function getDiscountAttribute()
{
$percent = 0;
$discount = $this->totals()->where('code', 'discount')->value('amount');
if ($discount) {
$sub_total = $this->totals()->where('code', 'sub_total')->value('amount');
$percent = number_format((($discount * 100) / $sub_total), 0);
}
return $percent;
}
}