|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use App\Events\StudentDeleting; |
|
6
|
|
|
use App\Events\StudentUpdated; |
|
7
|
|
|
use App\Events\InvoiceDeleting; |
|
8
|
|
|
use Backpack\CRUD\app\Models\Traits\CrudTrait; |
|
9
|
|
|
use Carbon\Carbon; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
11
|
|
|
use Spatie\Activitylog\Traits\LogsActivity; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @mixin IdeHelperInvoice |
|
15
|
|
|
*/ |
|
16
|
|
|
class Invoice extends Model |
|
17
|
|
|
{ |
|
18
|
|
|
use CrudTrait; |
|
|
|
|
|
|
19
|
|
|
use LogsActivity; |
|
20
|
|
|
|
|
21
|
|
|
protected $guarded = ['id']; |
|
22
|
|
|
|
|
23
|
|
|
protected static bool $logUnguarded = true; |
|
24
|
|
|
|
|
25
|
|
|
protected $appends = ['total_price_with_currency', 'formatted_date']; |
|
26
|
|
|
|
|
27
|
|
|
protected $casts = [ |
|
28
|
|
|
'date' => 'date', |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
protected $dispatchesEvents = [ |
|
32
|
|
|
'deleting' => InvoiceDeleting::class, |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
public function invoiceDetails() |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->hasMany(InvoiceDetail::class)->orderByRaw("CASE WHEN product_type like '%Enrollment' THEN 10 WHEN product_type like '%Fee' THEN 5 ELSE 0 END desc"); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function taxes() |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->hasMany(InvoiceDetail::class)->where('product_type', Tax::class); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function scheduledPayments() |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->hasMany(InvoiceDetail::class)->where('product_type', ScheduledPayment::class); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function payments() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->hasMany(Payment::class); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function paidTotal() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->payments->sum('value'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function enrollments() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->hasMany(InvoiceDetail::class)->where('product_type', Enrollment::class); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function comments() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->morphMany(Comment::class, 'commentable'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function invoiceType() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->belongsTo(InvoiceType::class); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function setNumber() |
|
76
|
|
|
{ |
|
77
|
|
|
// retrieve the last entry for the same type / year, and increment |
|
78
|
|
|
$count = self::whereInvoiceTypeId($this->invoice_type_id)->whereYear('created_at', $this->created_at->year)->orderByDesc('invoice_number')->first()->invoice_number; |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
$this->update(['invoice_number' => $count + 1]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getInvoiceReferenceAttribute() |
|
84
|
|
|
{ |
|
85
|
|
|
if (config('invoicing.invoice_numbering') === 'manual') { |
|
86
|
|
|
return $this->receipt_number; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->invoiceType->name.$this->created_at->format('y').'-'.$this->invoice_number; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getInvoiceSeriesAttribute() : string |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->invoiceType->name.$this->created_at->format('y'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getTotalPriceWithCurrencyAttribute() |
|
98
|
|
|
{ |
|
99
|
|
|
if (config('app.currency_position') === 'before') { |
|
100
|
|
|
return config('app.currency_symbol').' '.$this->totalPrice(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $this->totalPrice().' '.config('app.currency_symbol'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function totalPrice() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->invoiceDetails()->sum('price') / 100; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function getTotalPriceAttribute() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->totalPrice(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function getFormattedNumberAttribute() |
|
117
|
|
|
{ |
|
118
|
|
|
if (config('invoicing.invoice_numbering') === 'manual') { |
|
119
|
|
|
return $this->receipt_number; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return 'FC'.$this->receipt_number; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function getFormattedDateAttribute() |
|
126
|
|
|
{ |
|
127
|
|
|
return Carbon::parse($this->date)->locale(app()->getLocale())->isoFormat('Do MMMM YYYY'); |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function getBalanceAttribute() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->totalPrice() - $this->paidTotal(); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|