added project variables for invoice templates (#439)

This commit is contained in:
Kevin Papst
2018-11-23 15:56:06 +01:00
committed by GitHub
parent 025558e58e
commit 0334f6ce86
4 changed files with 89 additions and 50 deletions

View File

@@ -73,7 +73,10 @@ trait RendererTrait
*/ */
protected function modelToReplacer(InvoiceModel $model) protected function modelToReplacer(InvoiceModel $model)
{ {
return [ $customer = $model->getCustomer();
$project = $model->getQuery()->getProject();
$values = [
'invoice.due_date' => $this->getFormattedDateTime($model->getDueDate()), 'invoice.due_date' => $this->getFormattedDateTime($model->getDueDate()),
'invoice.date' => $this->getFormattedDateTime($model->getInvoiceDate()), 'invoice.date' => $this->getFormattedDateTime($model->getInvoiceDate()),
'invoice.number' => $model->getNumberGenerator()->getInvoiceNumber(), 'invoice.number' => $model->getNumberGenerator()->getInvoiceNumber(),
@@ -95,16 +98,32 @@ trait RendererTrait
'query.end' => $this->getFormattedDateTime($model->getQuery()->getEnd()), 'query.end' => $this->getFormattedDateTime($model->getQuery()->getEnd()),
'query.month' => $this->getFormattedMonthName($model->getQuery()->getBegin()), 'query.month' => $this->getFormattedMonthName($model->getQuery()->getBegin()),
'query.year' => $model->getQuery()->getBegin()->format('Y'), '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;
} }
/** /**

View File

@@ -159,6 +159,7 @@ abstract class AbstractRendererTest extends KernelTestCase
$query->setActivity($activity); $query->setActivity($activity);
$query->setBegin(new \DateTime()); $query->setBegin(new \DateTime());
$query->setEnd(new \DateTime()); $query->setEnd(new \DateTime());
$query->setProject($project);
$model = new InvoiceModel(); $model = new InvoiceModel();
$model->setCustomer($customer); $model->setCustomer($customer);

View File

@@ -17,14 +17,14 @@ class DebugRendererTest extends AbstractRendererTest
{ {
public function getTestModel() public function getTestModel()
{ {
yield [$this->getInvoiceModel(), '1,947.99', 5, 5, 1, 2, 2]; yield [$this->getInvoiceModel(), '1,947.99', 5, 5, 1, 2, 2, true];
yield [$this->getInvoiceModelOneEntry(), '293.27', 1, 1, 0, 1, 0]; yield [$this->getInvoiceModelOneEntry(), '293.27', 1, 1, 0, 1, 0, false];
} }
/** /**
* @dataProvider getTestModel * @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')); $document = new InvoiceDocument(new \SplFileInfo(__DIR__ . '/DebugRenderer.php'));
$sut = new DebugRenderer(); $sut = new DebugRenderer();
@@ -32,7 +32,7 @@ class DebugRendererTest extends AbstractRendererTest
$response = $sut->render($document, $model); $response = $sut->render($document, $model);
$data = json_decode($response->getContent(), true); $data = json_decode($response->getContent(), true);
$this->assertModelStructure($data['model']); $this->assertModelStructure($data['model'], $hasProject);
$rows = $data['entries']; $rows = $data['entries'];
$this->assertEquals($expectedRows, count($rows)); $this->assertEquals($expectedRows, count($rows));
@@ -43,7 +43,7 @@ class DebugRendererTest extends AbstractRendererTest
// TODO check values or formats? // TODO check values or formats?
} }
protected function assertModelStructure(array $model) protected function assertModelStructure(array $model, $hasProject = true)
{ {
$keys = [ $keys = [
'invoice.due_date', 'invoice.due_date',
@@ -65,6 +65,7 @@ class DebugRendererTest extends AbstractRendererTest
'query.end', 'query.end',
'query.month', 'query.month',
'query.year', 'query.year',
'customer.id',
'customer.address', 'customer.address',
'customer.name', 'customer.name',
'customer.contact', 'customer.contact',
@@ -75,17 +76,20 @@ class DebugRendererTest extends AbstractRendererTest
'customer.comment', 'customer.comment',
]; ];
foreach ($keys as $key) { if ($hasProject) {
$this->assertArrayHasKey($key, $model); $keys = array_merge($keys, [
'project.id',
'project.name',
'project.comment',
'project.order_number',
]);
} }
$expectedKeys = array_merge([], $keys);
sort($expectedKeys);
$givenKeys = array_keys($model); $givenKeys = array_keys($model);
sort($keys);
sort($givenKeys); sort($givenKeys);
$this->assertEquals(count($keys), count($givenKeys)); $this->assertEquals($keys, $givenKeys);
$this->assertEquals($expectedKeys, $givenKeys);
} }
protected function assertEntryStructure(array $model) protected function assertEntryStructure(array $model)

View File

@@ -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: You can use the following global variables in your templates:
| Key | Description | Example | | Key | Description |
|---|---|---| |---|---|
| ${invoice.due_date} | The due date for the invoice payment | | | ${invoice.due_date} | The due date for the invoice payment |
| ${invoice.date} | The creation date of this invoice | | | ${invoice.date} | The creation date of this invoice |
| ${invoice.number} | The generated invoice number | | | ${invoice.number} | The generated invoice number |
| ${invoice.currency} | The invoice currency | | | ${invoice.currency} | The invoice currency |
| ${invoice.total_time} | The total working time (entries with a fixed rate are always calculated with 1) | | | ${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.total} | The invoices total (including tax) |
| ${invoice.subtotal} | The invoices subtotal (excluding tax) | | | ${invoice.subtotal} | The invoices subtotal (excluding tax) |
| ${invoice.vat} | The VAT in percent for this invoice | | | ${invoice.vat} | The VAT in percent for this invoice |
| ${invoice.tax} | The tax of the invoice amount | | | ${invoice.tax} | The tax of the invoice amount |
| ${template.name} | The invoice name, as configured in your template | | | ${template.name} | The invoice name, as configured in your template |
| ${template.company} | The company 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.address} | The invoicing address, as configured in your template |
| ${template.title} | The invoice title, 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.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 | | | ${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.begin} | The query begin as formatted short date |
| ${query.end} | The query end as formatted short date | | | ${query.end} | The query end as formatted short date |
| ${query.month} | The month for this query (begin date) | | | ${query.month} | The month for this query (begin date) |
| ${query.year} | The year for this query (begin date) | | | ${query.year} | The year for this query (begin date) |
| ${customer.address} | The customer address | |
| ${customer.name} | The customer name | | If a customer was selected the following values exist as well:
| ${customer.contact} | The customer contact | |
| ${customer.company} | The customer company | | | Key | Description |
| ${customer.number} | The customer number | | |---|---|
| ${customer.country} | The customer country | | | ${customer.id} | The customer ID |
| ${customer.homepage} | The customer homepage | | | ${customer.address} | The customer address |
| ${customer.comment} | The customer comment | | | ${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 ### Timesheet entry variables