From e6a75bf16bfd610770f34eba175b3f793ccfa735 Mon Sep 17 00:00:00 2001 From: Daniel Stock Date: Tue, 21 Oct 2025 17:12:50 +0200 Subject: [PATCH] =?UTF-8?q?Added=20customer=5Fnr=20to=20database=20and=20i?= =?UTF-8?q?nvoice=20templates.=20Used=20standard=20DATEV=20format(10000=20?= =?UTF-8?q?=E2=80=93=2069999)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #31 --- app/Http/Controllers/CustomerController.php | 18 +++++++++++++++++- app/Http/Controllers/InvoiceController.php | 5 +++-- app/Models/Customer.php | 1 + database/factories/CustomerFactory.php | 2 ++ ...025_09_17_000000_create_customers_table.php | 1 + resources/js/types/index.d.ts | 1 + resources/views/invoice.blade.php | 9 ++++++++- 7 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/CustomerController.php b/app/Http/Controllers/CustomerController.php index 7740906..62b5e94 100644 --- a/app/Http/Controllers/CustomerController.php +++ b/app/Http/Controllers/CustomerController.php @@ -20,4 +20,20 @@ public function index() return ApiDataTransformer::snakeToCamel($customerArray); }); } -} \ No newline at end of file + + /** + * Generate a random available customer number + */ + public static function generateCustomerNumber() + { + $newNumber = null; + while (!$newNumber) { + // DATEV standard + $randomNumber = rand(10000, 69999); + $customer = Customer::firstWhere('customer_nr', $randomNumber); + if ($customer) { + return $randomNumber; + } + } + } +} diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 19cb1ac..531e33f 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -243,8 +243,9 @@ public function exportXml($id) // string $name __BT-44, From MINIMUM__ The full name of the buyer // string|null $id __BT-46, From BASIC WL__ An identifier of the buyer. In many systems, buyer identification is key information. Multiple buyer IDs can be assigned or specified. They can be differentiated by using different identification schemes. If no scheme is given, it should be known to the buyer and buyer, e.g. a previously exchanged, seller-assigned identifier of the buyer // string|null $description __BT-X-334, From EXTENDED__ Further legal information about the buyer - // TODO: use invoice customerNr - $document->setDocumentBuyer($invoice->customer->company_name, "###KUNDENNUMMER"); + if ($invoice->customer->company_name && $invoice->customer->customer_nr) { + $document->setDocumentBuyer($invoice->customer->company_name, $invoice->customer->customer_nr); + } // Set contact of the buyer party // diff --git a/app/Models/Customer.php b/app/Models/Customer.php index 273e10c..5121d83 100644 --- a/app/Models/Customer.php +++ b/app/Models/Customer.php @@ -16,6 +16,7 @@ class Customer extends Model protected $fillable = [ 'type', 'company_name', + 'customer_nr', 'vat_id', 'tax_id', // Tax identification number 'global_id', // Global Location Number (GLN) or other identification diff --git a/database/factories/CustomerFactory.php b/database/factories/CustomerFactory.php index 35d544d..614e944 100644 --- a/database/factories/CustomerFactory.php +++ b/database/factories/CustomerFactory.php @@ -3,6 +3,7 @@ namespace Database\Factories; use App\Models\PaymentTerms; use Illuminate\Database\Eloquent\Factories\Factory; +use App\Http\Controllers\CustomerController; class CustomerFactory extends Factory { @@ -13,6 +14,7 @@ public function definition() return [ 'type' => $this->faker->randomElement(['private', 'business']), 'company_name' => $this->faker->company(), + 'customer_nr' => CustomerController::generateCustomerNumber(), 'vat_id' => $this->faker->numerify('DE##########'), 'tax_id' => $this->faker->numerify('DE##########'), 'global_id' => $this->faker->numerify('############'), diff --git a/database/migrations/2025_09_17_000000_create_customers_table.php b/database/migrations/2025_09_17_000000_create_customers_table.php index 23b351c..d934626 100644 --- a/database/migrations/2025_09_17_000000_create_customers_table.php +++ b/database/migrations/2025_09_17_000000_create_customers_table.php @@ -11,6 +11,7 @@ public function up() $table->id(); $table->enum('type', ['private', 'business']); $table->string('company_name', 100)->nullable(); + $table->integer('customer_nr', false, true)->nullable(); $table->string('vat_id', 50)->nullable(); $table->string('tax_id', 50)->nullable(); $table->string('global_id', 50)->nullable(); diff --git a/resources/js/types/index.d.ts b/resources/js/types/index.d.ts index f7a5103..8ee4540 100644 --- a/resources/js/types/index.d.ts +++ b/resources/js/types/index.d.ts @@ -92,6 +92,7 @@ export interface Customer { id: number; type: string; companyName: string | null; + customerNr: number | null; vatId: string | null; contacts: Contact[]; email?: string; diff --git a/resources/views/invoice.blade.php b/resources/views/invoice.blade.php index 3d0de72..40b31a7 100644 --- a/resources/views/invoice.blade.php +++ b/resources/views/invoice.blade.php @@ -1,4 +1,3 @@ - @@ -81,6 +80,14 @@ {{ $invoice['nr'] }} + @if($invoice['customer']['customerNr']) + + Kunden-Nr. + + {{ $invoice['customer']['customerNr'] }} + + + @endif Datum