support template variable entry.row for docx if description is not used (#504)

This commit is contained in:
Kevin Papst
2019-01-18 16:19:02 +01:00
committed by GitHub
parent 64330e6cc4
commit 1ad440f488
4 changed files with 15 additions and 3 deletions

View File

@@ -13,6 +13,7 @@ use App\Entity\InvoiceDocument;
use App\Invoice\RendererInterface;
use App\Model\InvoiceModel;
use PhpOffice\PhpWord\Escaper\Xml;
use PhpOffice\PhpWord\Exception\Exception as OfficeException;
use PhpOffice\PhpWord\TemplateProcessor;
use Symfony\Component\HttpFoundation\File\Stream;
use Symfony\Component\HttpFoundation\Response;
@@ -40,7 +41,12 @@ class DocxRenderer extends AbstractRenderer implements RendererInterface
$template->setValue($search, $replace);
}
try {
$template->cloneRow('entry.description', count($model->getCalculator()->getEntries()));
} catch (OfficeException $ex) {
$template->cloneRow('entry.row', count($model->getCalculator()->getEntries()));
}
$i = 1;
foreach ($model->getCalculator()->getEntries() as $entry) {
$values = $this->timesheetToArray($entry);

View File

@@ -161,6 +161,7 @@ trait RendererTrait
$end = $timesheet->getEnd();
return [
'entry.row' => '',
'entry.description' => $description,
'entry.amount' => $amount,
'entry.rate' => $this->getFormattedMoney($hourlyRate),

View File

@@ -95,6 +95,7 @@ class DebugRendererTest extends AbstractRendererTest
protected function assertEntryStructure(array $model)
{
$keys = [
'entry.row',
'entry.description',
'entry.amount',
'entry.rate',

View File

@@ -67,8 +67,11 @@ GitHub to find out which variables can be used or debug it with:
Docx templates are powered by [PHPWord](https://github.com/PHPOffice/PHPWord) and its `TemplateProcessor`.
**Important:** The variable `${entry.description}` has to be set in one table row, otherwise no timesheet records will be rendered!
This row will then be cloned for every timesheet entry.
**Important:** You have to add one of the variables - either `${entry.description}` or `${entry.row}` - in one table row,
otherwise the rendering process will fail! The row containing this variable will be cloned for every included timesheet record.
If you do not use `${entry.description}` then a fallback for `${entry.row}` is used and will be removed in the rendering process,
it will not show up in the generated invoice.
See below in `Template variables` to find out which variables you can use in your template.
@@ -144,6 +147,7 @@ For each timesheet entry you can use the variables from the following table.
| Key | Description | Example |
|---|---|---|
| ${entry.row} | An empty string, used as template row for docx | |
| ${entry.description} | The entries description | _foo bar_ |
| ${entry.amount} | The format duration/amount for this entry | 02:47 h |
| ${entry.rate} | The rate for one unit of the entry (normally one hour) | 100 |