'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'); } }