From 0334f6ce869ddca91b3d983af935a9fb92426834 Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Fri, 23 Nov 2018 15:56:06 +0100 Subject: [PATCH] added project variables for invoice templates (#439) --- src/Invoice/Renderer/RendererTrait.php | 39 +++++++--- .../Invoice/Renderer/AbstractRendererTest.php | 1 + tests/Invoice/Renderer/DebugRendererTest.php | 26 ++++--- var/docs/invoices.md | 73 +++++++++++-------- 4 files changed, 89 insertions(+), 50 deletions(-) diff --git a/src/Invoice/Renderer/RendererTrait.php b/src/Invoice/Renderer/RendererTrait.php index 9ea675b5..d423fcff 100644 --- a/src/Invoice/Renderer/RendererTrait.php +++ b/src/Invoice/Renderer/RendererTrait.php @@ -73,7 +73,10 @@ trait RendererTrait */ protected function modelToReplacer(InvoiceModel $model) { - return [ + $customer = $model->getCustomer(); + $project = $model->getQuery()->getProject(); + + $values = [ 'invoice.due_date' => $this->getFormattedDateTime($model->getDueDate()), 'invoice.date' => $this->getFormattedDateTime($model->getInvoiceDate()), 'invoice.number' => $model->getNumberGenerator()->getInvoiceNumber(), @@ -95,16 +98,32 @@ trait RendererTrait 'query.end' => $this->getFormattedDateTime($model->getQuery()->getEnd()), 'query.month' => $this->getFormattedMonthName($model->getQuery()->getBegin()), 'query.year' => $model->getQuery()->getBegin()->format('Y'), - - 'customer.address' => $model->getCustomer()->getAddress(), - 'customer.name' => $model->getCustomer()->getName(), - 'customer.contact' => $model->getCustomer()->getContact(), - 'customer.company' => $model->getCustomer()->getCompany(), - 'customer.number' => $model->getCustomer()->getNumber(), - 'customer.country' => $model->getCustomer()->getCountry(), - 'customer.homepage' => $model->getCustomer()->getHomepage(), - 'customer.comment' => $model->getCustomer()->getComment(), ]; + + if (null !== $project) { + $values = array_merge($values, [ + 'project.id' => $project->getId(), + 'project.name' => $project->getName(), + 'project.comment' => $project->getComment(), + 'project.order_number' => $project->getOrderNumber(), + ]); + } + + if (null !== $customer) { + $values = array_merge($values, [ + 'customer.id' => $customer->getId(), + 'customer.address' => $customer->getAddress(), + 'customer.name' => $customer->getName(), + 'customer.contact' => $customer->getContact(), + 'customer.company' => $customer->getCompany(), + 'customer.number' => $customer->getNumber(), + 'customer.country' => $customer->getCountry(), + 'customer.homepage' => $customer->getHomepage(), + 'customer.comment' => $customer->getComment(), + ]); + } + + return $values; } /** diff --git a/tests/Invoice/Renderer/AbstractRendererTest.php b/tests/Invoice/Renderer/AbstractRendererTest.php index 0c61bd5c..e41845ec 100644 --- a/tests/Invoice/Renderer/AbstractRendererTest.php +++ b/tests/Invoice/Renderer/AbstractRendererTest.php @@ -159,6 +159,7 @@ abstract class AbstractRendererTest extends KernelTestCase $query->setActivity($activity); $query->setBegin(new \DateTime()); $query->setEnd(new \DateTime()); + $query->setProject($project); $model = new InvoiceModel(); $model->setCustomer($customer); diff --git a/tests/Invoice/Renderer/DebugRendererTest.php b/tests/Invoice/Renderer/DebugRendererTest.php index 9f419961..e06a3dea 100644 --- a/tests/Invoice/Renderer/DebugRendererTest.php +++ b/tests/Invoice/Renderer/DebugRendererTest.php @@ -17,14 +17,14 @@ class DebugRendererTest extends AbstractRendererTest { public function getTestModel() { - yield [$this->getInvoiceModel(), '1,947.99', 5, 5, 1, 2, 2]; - yield [$this->getInvoiceModelOneEntry(), '293.27', 1, 1, 0, 1, 0]; + yield [$this->getInvoiceModel(), '1,947.99', 5, 5, 1, 2, 2, true]; + yield [$this->getInvoiceModelOneEntry(), '293.27', 1, 1, 0, 1, 0, false]; } /** * @dataProvider getTestModel */ - public function testRender(InvoiceModel $model, $expectedRate, $expectedRows, $expectedDescriptions, $expectedUser1, $expectedUser2, $expectedUser3) + public function testRender(InvoiceModel $model, $expectedRate, $expectedRows, $expectedDescriptions, $expectedUser1, $expectedUser2, $expectedUser3, $hasProject) { $document = new InvoiceDocument(new \SplFileInfo(__DIR__ . '/DebugRenderer.php')); $sut = new DebugRenderer(); @@ -32,7 +32,7 @@ class DebugRendererTest extends AbstractRendererTest $response = $sut->render($document, $model); $data = json_decode($response->getContent(), true); - $this->assertModelStructure($data['model']); + $this->assertModelStructure($data['model'], $hasProject); $rows = $data['entries']; $this->assertEquals($expectedRows, count($rows)); @@ -43,7 +43,7 @@ class DebugRendererTest extends AbstractRendererTest // TODO check values or formats? } - protected function assertModelStructure(array $model) + protected function assertModelStructure(array $model, $hasProject = true) { $keys = [ 'invoice.due_date', @@ -65,6 +65,7 @@ class DebugRendererTest extends AbstractRendererTest 'query.end', 'query.month', 'query.year', + 'customer.id', 'customer.address', 'customer.name', 'customer.contact', @@ -75,17 +76,20 @@ class DebugRendererTest extends AbstractRendererTest 'customer.comment', ]; - foreach ($keys as $key) { - $this->assertArrayHasKey($key, $model); + if ($hasProject) { + $keys = array_merge($keys, [ + 'project.id', + 'project.name', + 'project.comment', + 'project.order_number', + ]); } - $expectedKeys = array_merge([], $keys); - sort($expectedKeys); $givenKeys = array_keys($model); + sort($keys); sort($givenKeys); - $this->assertEquals(count($keys), count($givenKeys)); - $this->assertEquals($expectedKeys, $givenKeys); + $this->assertEquals($keys, $givenKeys); } protected function assertEntryStructure(array $model) diff --git a/var/docs/invoices.md b/var/docs/invoices.md index ecefb80e..8247053a 100644 --- a/var/docs/invoices.md +++ b/var/docs/invoices.md @@ -93,35 +93,50 @@ See below in `Template variables` to find out which variables you can use in you You can use the following global variables in your templates: -| Key | Description | Example | -|---|---|---| -| ${invoice.due_date} | The due date for the invoice payment | | -| ${invoice.date} | The creation date of this invoice | | -| ${invoice.number} | The generated invoice number | | -| ${invoice.currency} | The invoice currency | | -| ${invoice.total_time} | The total working time (entries with a fixed rate are always calculated with 1) | | -| ${invoice.total} | The invoices total (including tax) | | -| ${invoice.subtotal} | The invoices subtotal (excluding tax) | | -| ${invoice.vat} | The VAT in percent for this invoice | | -| ${invoice.tax} | The tax of the invoice amount | | -| ${template.name} | The invoice name, as configured in your template | | -| ${template.company} | The company name, as configured in your template | | -| ${template.address} | The invoicing address, as configured in your template | | -| ${template.title} | The invoice title, as configured in your template | | -| ${template.payment_terms} | Your payment terms, usage might differ from template to template | | -| ${template.due_days} | The amount of days for the payment, starting with the day of creating the invoice | | -| ${query.begin} | The query begin as formatted short date | | -| ${query.end} | The query end as formatted short date | | -| ${query.month} | The month for this query (begin date) | | -| ${query.year} | The year for this query (begin date) | | -| ${customer.address} | The customer address | | -| ${customer.name} | The customer name | | -| ${customer.contact} | The customer contact | | -| ${customer.company} | The customer company | | -| ${customer.number} | The customer number | | -| ${customer.country} | The customer country | | -| ${customer.homepage} | The customer homepage | | -| ${customer.comment} | The customer comment | | +| Key | Description | +|---|---| +| ${invoice.due_date} | The due date for the invoice payment | +| ${invoice.date} | The creation date of this invoice | +| ${invoice.number} | The generated invoice number | +| ${invoice.currency} | The invoice currency | +| ${invoice.total_time} | The total working time (entries with a fixed rate are always calculated with 1) | +| ${invoice.total} | The invoices total (including tax) | +| ${invoice.subtotal} | The invoices subtotal (excluding tax) | +| ${invoice.vat} | The VAT in percent for this invoice | +| ${invoice.tax} | The tax of the invoice amount | +| ${template.name} | The invoice name, as configured in your template | +| ${template.company} | The company name, as configured in your template | +| ${template.address} | The invoicing address, as configured in your template | +| ${template.title} | The invoice title, as configured in your template | +| ${template.payment_terms} | Your payment terms, usage might differ from template to template | +| ${template.due_days} | The amount of days for the payment, starting with the day of creating the invoice | +| ${query.begin} | The query begin as formatted short date | +| ${query.end} | The query end as formatted short date | +| ${query.month} | The month for this query (begin date) | +| ${query.year} | The year for this query (begin date) | + +If a customer was selected the following values exist as well: + +| Key | Description | +|---|---| +| ${customer.id} | The customer ID | +| ${customer.address} | The customer address | +| ${customer.name} | The customer name | +| ${customer.contact} | The customer contact | +| ${customer.company} | The customer company | +| ${customer.number} | The customer number | +| ${customer.country} | The customer country | +| ${customer.homepage} | The customer homepage | +| ${customer.comment} | The customer comment | + +If a project was selected the following values exist as well: + +| Key | Description | +|---|---| +| ${project.id} | The project ID | +| ${project.name} | The project name | +| ${project.comment} | The project name | +| ${project.order_number} | The project order number | ### Timesheet entry variables