1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Template Service

This commit is contained in:
David Bomba 2023-09-22 22:35:43 +10:00
parent c83e94d3af
commit dc871af5eb
2 changed files with 57 additions and 32 deletions

View File

@ -12,9 +12,10 @@
namespace App\Services\Template;
use App\Models\Design;
use App\Utils\HtmlEngine;
use App\Utils\VendorHtmlEngine;
use App\Utils\PaymentHtmlEngine;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Collection;
class TemplateService
{
@ -46,7 +47,7 @@ class TemplateService
'recurring_id',
'subscription_id'
];
public function __construct(public Design $template)
{
$this->template = $template;
@ -73,7 +74,7 @@ class TemplateService
* @param array $data - the payload to be passed into the template
* @return self
*/
private function build(array $data): self
public function build(array $data): self
{
$this->compose()
->parseNinjaBlocks($data)
@ -97,7 +98,7 @@ class TemplateService
{
$data = $this->preProcessDataBlocks($data);
$replacements = [];
nlog($data);
$contents = $this->document->getElementsByTagName('ninja');
foreach ($contents as $content) {
@ -131,15 +132,17 @@ class TemplateService
/**
* Parses all variables in the document
*
* @param array $data
* @return self
*/
private function parseVariables(): self
private function parseVariables(array $data): self
{
$variables = $this->resolveHtmlEngine();
$variables = $this->resolveHtmlEngine($data);
$html = $this->getHtml();
foreach($variables as $key => $variable) {
$html = strtr($this->getHtml(), $variable['labels']);
$html = strtr($html, $variable['labels']);
$html = strtr($html, $variable['values']);
}
@ -188,18 +191,18 @@ class TemplateService
*/
private function resolveHtmlEngine(array $data): array
{
return collect($data)->map(function ($key, $value) {
$processed[$key] = [];
return collect($data)->map(function ($value, $key) {
$processed = [];
match ($key) {
'invoices' => $processed[$key] = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
'quotes' => $processed[$key] = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
'credits' => $processed[$key] = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
'payments' => $processed[$key] = (new PaymentHtmlEngine($value->first(), $value->first()->client->contacts()->first()))->generateLabelsAndValues(),
'tasks' => $processed[$key] = [],
'projects' => $processed[$key] = [],
'purchase_orders' => $processed[$key] = (new VendorHtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
'invoices' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
'quotes' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
'credits' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
'payments' => $processed = (new PaymentHtmlEngine($value->first(), $value->first()->client->contacts()->first()))->generateLabelsAndValues(),
'tasks' => $processed = [],
'projects' => $processed = [],
'purchase_orders' => $processed = (new VendorHtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
};
return $processed;
@ -210,18 +213,18 @@ class TemplateService
private function preProcessDataBlocks($data): array
{
return collect($data)->map(function ($key, $value){
return collect($data)->map(function ($value, $key){
$processed[$key] = [];
$processed = [];
match ($key) {
'invoices' => $processed[$key] = $this->processInvoices($value),
'quotes' => $processed[$key] = $this->processQuotes($value),
'credits' => $processed[$key] = $this->processCredits($value),
'payments' => $processed[$key] = $this->processPayments($value),
'tasks' => $processed[$key] = $this->processTasks($value),
'projects' => $processed[$key] = $this->processProjects($value),
'purchase_orders' => $processed[$key] = $this->processPurchaseOrders($value),
'invoices' => $processed = $this->processInvoices($value),
'quotes' => $processed = $this->processQuotes($value),
'credits' => $processed = $this->processCredits($value),
'payments' => $processed = $this->processPayments($value),
'tasks' => $processed = $this->processTasks($value),
'projects' => $processed = $this->processProjects($value),
'purchase_orders' => $processed = $this->processPurchaseOrders($value),
};
return $processed;
@ -231,7 +234,9 @@ class TemplateService
private function processInvoices($invoices): Collection
{
return $invoices->makeHidden($this->standard_excludes);
return $invoices->map(function($invoice){
return $invoice->makeHidden($this->standard_excludes);
});
}
private function processQuotes($quotes): Collection
@ -269,7 +274,7 @@ class TemplateService
private function processTasks($tasks): Collection
{
return $task->makeHidden([
return $tasks->makeHidden([
'id',
'user_id',
'assigned_user_id',
@ -298,7 +303,7 @@ class TemplateService
private function processPurchaseOrders($purchase_orders): array
{
return $projects->makeHidden($this->purchase_excludes);
return $purchase_orders->makeHidden($this->purchase_excludes);
// return $purchase_orders->map(function ($purchase_order){
// })->toArray();

View File

@ -20,7 +20,6 @@ use Tests\MockAccountData;
use App\Services\PdfMaker\PdfMaker;
use Illuminate\Support\Facades\App;
use App\Jobs\Entity\CreateEntityPdf;
use App\Services\PdfMaker\Design as DesignMaker;
use App\Services\PdfMaker\Design as PdfDesignModel;
use App\Services\PdfMaker\Design as PdfMakerDesign;
use App\Services\Template\TemplateService;
@ -51,6 +50,7 @@ class TemplateTest extends TestCase
</tr>
</thead>
<tbody>
{% for entity in invoices %}
{% 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>
@ -60,6 +60,7 @@ class TemplateTest extends TestCase
<td class="whitespace-nowrap px-6 py-4 font-medium">0</td>
</tr>
{% endfor %}
{% endfor %}
</tbody>
</table>
</ninja>
@ -78,7 +79,7 @@ class TemplateTest extends TestCase
}
public function testTemplateService()
public function testTemplateServiceBuild()
{
$design_model = Design::find(2);
@ -89,6 +90,25 @@ class TemplateTest extends TestCase
$replicated_design->is_custom = true;
$replicated_design->save();
$data = [];
$data['invoices'] = collect([$this->invoice]);
$ts = $replicated_design->service()->build($data);
nlog($ts->getHtml());
$this->assertNotNull($ts->getHtml());
}
public function testTemplateService()
{
$design_model = Design::find(2);
$replicated_design = $design_model->replicate();
$design = $replicated_design->design;
$design->body .= $this->body;
$replicated_design->design = $design;
$replicated_design->is_custom = true;
$replicated_design->save();
$this->assertNotNull($replicated_design->service());
$this->assertInstanceOf(TemplateService::class, $replicated_design->service());