topnotch_website/app/Models/InvoiceReport.php

61 lines
1.4 KiB
PHP
Raw Normal View History

2024-11-29 12:39:20 -05:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2024-12-02 12:21:01 -05:00
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
2024-11-29 12:39:20 -05:00
class InvoiceReport extends Model
{
use HasFactory;
protected $fillable = [
2024-12-02 12:21:01 -05:00
'internal_id',
2024-11-29 12:39:20 -05:00
'customer_id',
'date_start',
'date_end',
'filter_paid',
'subtotal',
'pst',
'gst',
'total',
];
2024-12-02 12:21:01 -05:00
public static function boot(): void
{
parent::boot();
static::created(function ($model) {
$model->attributes['internal_id'] = 'TN-INR-'.$model->id;
$invoices = Invoice::whereBetween('date', [$model->date_start, $model->date_end])
->where('customer_id', $model->customer_id)->get()->pluck('id')->toArray();
$model->invoices()->sync($invoices);
$model->save();
});
// todo: sync all invoices within time and customer constraints
}
2024-11-29 12:39:20 -05:00
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class);
}
2024-12-02 12:21:01 -05:00
public function invoices(): BelongsToMany
{
return $this->BelongsToMany(Invoice::class);
}
public function orders(): HasManyThrough
{
return $this->hasManyThrough(Order::class, Invoice::class);
}
2024-11-29 12:39:20 -05:00
}