1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Template Service

This commit is contained in:
David Bomba 2023-09-22 23:59:03 +10:00
parent 91bd2e18f5
commit 1ad4c10538
2 changed files with 68 additions and 25 deletions

View File

@ -32,6 +32,7 @@ use App\Transformers\PaymentTransformer;
use App\Transformers\ProjectTransformer;
use App\Transformers\PurchaseOrderTransformer;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Serializer\JsonApiSerializer;
class TemplateService
{
@ -40,30 +41,6 @@ class TemplateService
private string $compiled_html = '';
private array $standard_excludes = [
'id',
'client_id',
'assigned_user_id',
'project_id',
'vendor_id',
'design_id',
'company_id',
'recurring_id',
'subscription_id'
];
private array $purchase_excludes = [
'id',
'vendor_id',
'assigned_user_id',
'project_id',
'vendor_id',
'design_id',
'company_id',
'recurring_id',
'subscription_id'
];
public function __construct(public Design $template)
{
$this->template = $template;
@ -114,7 +91,7 @@ class TemplateService
{
$data = $this->preProcessDataBlocks($data);
$replacements = [];
nlog($data);
$contents = $this->document->getElementsByTagName('ninja');
foreach ($contents as $content) {
@ -253,6 +230,7 @@ nlog($data);
$it = new InvoiceTransformer();
$it->setDefaultIncludes(['client']);
$manager = new Manager();
// $manager->setSerializer(new JsonApiSerializer());
$resource = new \League\Fractal\Resource\Collection($invoices, $it, Invoice::class);
$i = $manager->createData($resource)->toArray();
return $i['data'];

View File

@ -67,6 +67,40 @@ class TemplateTest extends TestCase
';
private string $nested_body = '
<ninja>
$company.name
<table class="min-w-full text-left text-sm font-light">
<thead class="border-b font-medium dark:border-neutral-500">
<tr class="text-sm leading-normal">
<th scope="col" class="px-6 py-4">Item #</th>
<th scope="col" class="px-6 py-4">Description</th>
<th scope="col" class="px-6 py-4">Ordered</th>
<th scope="col" class="px-6 py-4">Delivered</th>
<th scope="col" class="px-6 py-4">Outstanding</th>
</tr>
</thead>
<tbody>
{% for entity in invoices %}
Client Name: {{ entity.client.name }}
Client Name with variables = $client.name
{% for item in entity.line_items|filter(item => item.type_id == "1") %}
<tr class="border-b dark:border-neutral-500">
<td class="whitespace-nowrap px-6 py-4 font-medium">{{ item.product_key }}</td>
<td class="whitespace-nowrap px-6 py-4 font-medium">{{ item.notes }}</td>
<td class="whitespace-nowrap px-6 py-4 font-medium">{{ item.quantity }}</td>
<td class="whitespace-nowrap px-6 py-4 font-medium">{{ item.quantity }}</td>
<td class="whitespace-nowrap px-6 py-4 font-medium">0</td>
</tr>
{% endfor %}
{% endfor %}
</tbody>
</table>
</ninja>
';
protected function setUp() :void
{
parent::setUp();
@ -79,6 +113,37 @@ class TemplateTest extends TestCase
}
public function testDoubleEntityNestedDataTemplateServiceBuild()
{
$design_model = Design::find(2);
$replicated_design = $design_model->replicate();
$design = $replicated_design->design;
$design->body .= $this->nested_body;
$replicated_design->design = $design;
$replicated_design->is_custom = true;
$replicated_design->save();
$i2 = Invoice::factory()
->for($this->client)
->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'status_id' => Invoice::STATUS_SENT,
'design_id' => $replicated_design->id,
'balance' => 100,
]);
$data = [];
$data['invoices'] = collect([$this->invoice, $i2]);
$ts = $replicated_design->service()->build($data);
nlog("results = ");
nlog($ts->getHtml());
$this->assertNotNull($ts->getHtml());
}
public function testDoubleEntityTemplateServiceBuild()
{
$design_model = Design::find(2);