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

View File

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

View File

@@ -95,6 +95,7 @@ class DebugRendererTest extends AbstractRendererTest
protected function assertEntryStructure(array $model) protected function assertEntryStructure(array $model)
{ {
$keys = [ $keys = [
'entry.row',
'entry.description', 'entry.description',
'entry.amount', 'entry.amount',
'entry.rate', '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`. 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! **Important:** You have to add one of the variables - either `${entry.description}` or `${entry.row}` - in one table row,
This row will then be cloned for every timesheet entry. 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. 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 | | Key | Description | Example |
|---|---|---| |---|---|---|
| ${entry.row} | An empty string, used as template row for docx | |
| ${entry.description} | The entries description | _foo bar_ | | ${entry.description} | The entries description | _foo bar_ |
| ${entry.amount} | The format duration/amount for this entry | 02:47 h | | ${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 | | ${entry.rate} | The rate for one unit of the entry (normally one hour) | 100 |