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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ class Invoiceitem extends Model
|
||||
*/
|
||||
protected $fillable = [
|
||||
'invoice_id',
|
||||
'article_number',
|
||||
'amount',
|
||||
'discount',
|
||||
'tax',
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Models;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class Project extends Model
|
||||
@@ -33,8 +34,20 @@ class Project extends Model
|
||||
protected $appends = [
|
||||
'start',
|
||||
'end',
|
||||
'customer_email',
|
||||
'customer_name',
|
||||
];
|
||||
|
||||
|
||||
public function getCustomerNameAttribute(): string
|
||||
{
|
||||
return $this->customer->name;
|
||||
}
|
||||
public function getCustomerEmailAttribute(): string
|
||||
{
|
||||
return $this->customer->email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the end_date attribute in local format.
|
||||
*/
|
||||
@@ -58,4 +71,9 @@ class Project extends Model
|
||||
{
|
||||
return $this->belongsTo(Customer::class);
|
||||
}
|
||||
|
||||
public function invoices(): HasMany
|
||||
{
|
||||
return $this->hasMany(Invoice::class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user