From e1f579c7fdea9a50d2ab647b24a838c95b4e1a41 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 13 May 2025 16:00:41 +0200 Subject: [PATCH] Apply changes when creating invoices for projects. --- .../Controllers/Api/InvoiceController.php | 3 ++- app/Models/Invoice.php | 19 ------------------- app/Models/Project.php | 12 ++++++++++++ resources/views/invoice/create.blade.php | 6 +++--- resources/views/invoice/edit.blade.php | 2 +- resources/views/invoice/show.blade.php | 8 ++++---- 6 files changed, 22 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/Api/InvoiceController.php b/app/Http/Controllers/Api/InvoiceController.php index fc763de..b0b9892 100644 --- a/app/Http/Controllers/Api/InvoiceController.php +++ b/app/Http/Controllers/Api/InvoiceController.php @@ -43,9 +43,9 @@ class InvoiceController extends Controller 'address_id' => 'required|integer|exists:addresses,id', 'delivery_id' => 'nullable|integer|exists:addresses,id', 'project_id' => 'nullable|integer|exists:projects,id', + 'project_count' => 'nullable|integer', 'currency_code' => 'required|string', 'type' => [Rule::enum(InvoiceTypeCode::class)], - 'project_count' => 'nullable|integer', 'tax' => 'required|numeric', 'sum' => 'required|numeric', 'due_date' => 'required|date', @@ -73,6 +73,7 @@ class InvoiceController extends Controller 'address_id' => 'required|integer|exists:addresses,id', 'delivery_id' => 'nullable|integer|exists:addresses,id', 'project_id' => 'nullable|integer|exists:projects,id', + 'project_count' => 'nullable|integer', 'currency_code' => 'required|string', 'type' => [Rule::enum(InvoiceTypeCode::class)], 'tax' => 'required|numeric', diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index e95a1bc..edc52dd 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -43,25 +43,6 @@ 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. */ diff --git a/app/Models/Project.php b/app/Models/Project.php index 9fde576..230fd8e 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -36,6 +36,7 @@ class Project extends Model 'end', 'customer_email', 'customer_name', + 'next_invoice_count', ]; @@ -56,6 +57,14 @@ class Project extends Model return (is_null($this->start_date)) ? '' : Carbon::createFromFormat('Y-m-d', $this->start_date)->format('d.m.Y'); } + /** + * Get the next invoice number for this project. + */ + public function getNextInvoiceCountAttribute(): int + { + return $this->invoices()->max('project_count') + 1 ?? 1; + } + /** * Get the end_date attribute in local format. */ @@ -72,6 +81,9 @@ class Project extends Model return $this->belongsTo(Customer::class)->withTrashed(); } + /** + * Get the invoices for this project. + */ public function invoices(): HasMany { return $this->hasMany(Invoice::class); diff --git a/resources/views/invoice/create.blade.php b/resources/views/invoice/create.blade.php index f19b706..395ade2 100644 --- a/resources/views/invoice/create.blade.php +++ b/resources/views/invoice/create.blade.php @@ -110,7 +110,7 @@