Extend invoices and invoice items to keep more information.
This commit is contained in:
@@ -23,6 +23,13 @@ class Invoice extends Model
|
||||
'status',
|
||||
'sum',
|
||||
'tax',
|
||||
'project_id',
|
||||
'project_count',
|
||||
'is_template',
|
||||
'currency_code',
|
||||
'due_date',
|
||||
'cash_discount',
|
||||
'cash_discount_date',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -36,6 +43,25 @@ class Invoice extends Model
|
||||
'localized_state'
|
||||
];
|
||||
|
||||
/**
|
||||
* Set the project_count variable automatically, if a project is related to this invoice.
|
||||
*/
|
||||
public static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
self::creating(function (Invoice $invoice) {
|
||||
if (is_null($invoice->project_id)) {
|
||||
return true;
|
||||
}
|
||||
if ($invoice->type != '326') {
|
||||
return true;
|
||||
}
|
||||
$projectMax = Invoice::where('project_id', '=', $invoice->project_id)->max('project_count') + 1;
|
||||
$invoice->project_count = $projectMax;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the invoice state as translated string.
|
||||
*/
|
||||
@@ -107,4 +133,9 @@ class Invoice extends Model
|
||||
{
|
||||
return $this->hasMany(Payment::class);
|
||||
}
|
||||
|
||||
public function project(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Project::class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user