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