99 lines
2.5 KiB
PHP
99 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Avoir extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'client_id',
|
|
'invoice_id',
|
|
'group_id',
|
|
'avoir_number',
|
|
'status',
|
|
'avoir_date',
|
|
'due_date',
|
|
'currency',
|
|
'total_ht',
|
|
'total_tva',
|
|
'total_ttc',
|
|
'reason_type',
|
|
'reason_description',
|
|
'e_invoice_status',
|
|
'refund_status',
|
|
'refund_date',
|
|
'refund_method',
|
|
'compensation_invoice_id',
|
|
'compensation_amount',
|
|
];
|
|
|
|
protected $casts = [
|
|
'avoir_date' => 'date',
|
|
'due_date' => 'date',
|
|
'refund_date' => 'date',
|
|
'total_ht' => 'decimal:2',
|
|
'total_tva' => 'decimal:2',
|
|
'total_ttc' => 'decimal:2',
|
|
'compensation_amount' => 'decimal:2',
|
|
];
|
|
|
|
protected static function booted()
|
|
{
|
|
static::creating(function ($avoir) {
|
|
// Auto-generate avoir number if not provided
|
|
if (empty($avoir->avoir_number)) {
|
|
$prefix = 'AV-' . now()->format('Ym') . '-';
|
|
$lastAvoir = self::where('avoir_number', 'like', $prefix . '%')
|
|
->orderBy('avoir_number', 'desc')
|
|
->first();
|
|
|
|
if ($lastAvoir) {
|
|
// Extract numeric part
|
|
preg_match('/(\d+)$/', $lastAvoir->avoir_number, $matches);
|
|
$newNumber = $matches ? intval($matches[1]) + 1 : 1;
|
|
} else {
|
|
$newNumber = 1;
|
|
}
|
|
|
|
$avoir->avoir_number = $prefix . str_pad((string)$newNumber, 4, '0', STR_PAD_LEFT);
|
|
}
|
|
});
|
|
}
|
|
|
|
public function client()
|
|
{
|
|
return $this->belongsTo(Client::class);
|
|
}
|
|
|
|
public function invoice()
|
|
{
|
|
return $this->belongsTo(Invoice::class);
|
|
}
|
|
|
|
public function group()
|
|
{
|
|
return $this->belongsTo(ClientGroup::class, 'group_id');
|
|
}
|
|
|
|
public function lines()
|
|
{
|
|
return $this->hasMany(AvoirLine::class);
|
|
}
|
|
|
|
public function compensationInvoice()
|
|
{
|
|
return $this->belongsTo(Invoice::class, 'compensation_invoice_id');
|
|
}
|
|
|
|
public function history()
|
|
{
|
|
return $this->hasMany(DocumentStatusHistory::class, 'document_id')
|
|
->where('document_type', 'avoir')
|
|
->orderBy('changed_at', 'desc');
|
|
}
|
|
}
|