mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-08 16:44:31 -04:00
Template Service
This commit is contained in:
parent
c83e94d3af
commit
dc871af5eb
@ -12,9 +12,10 @@
|
|||||||
namespace App\Services\Template;
|
namespace App\Services\Template;
|
||||||
|
|
||||||
use App\Models\Design;
|
use App\Models\Design;
|
||||||
|
use App\Utils\HtmlEngine;
|
||||||
use App\Utils\VendorHtmlEngine;
|
use App\Utils\VendorHtmlEngine;
|
||||||
use App\Utils\PaymentHtmlEngine;
|
use App\Utils\PaymentHtmlEngine;
|
||||||
use Illuminate\Database\Eloquent\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
class TemplateService
|
class TemplateService
|
||||||
{
|
{
|
||||||
@ -46,7 +47,7 @@ class TemplateService
|
|||||||
'recurring_id',
|
'recurring_id',
|
||||||
'subscription_id'
|
'subscription_id'
|
||||||
];
|
];
|
||||||
|
|
||||||
public function __construct(public Design $template)
|
public function __construct(public Design $template)
|
||||||
{
|
{
|
||||||
$this->template = $template;
|
$this->template = $template;
|
||||||
@ -73,7 +74,7 @@ class TemplateService
|
|||||||
* @param array $data - the payload to be passed into the template
|
* @param array $data - the payload to be passed into the template
|
||||||
* @return self
|
* @return self
|
||||||
*/
|
*/
|
||||||
private function build(array $data): self
|
public function build(array $data): self
|
||||||
{
|
{
|
||||||
$this->compose()
|
$this->compose()
|
||||||
->parseNinjaBlocks($data)
|
->parseNinjaBlocks($data)
|
||||||
@ -97,7 +98,7 @@ class TemplateService
|
|||||||
{
|
{
|
||||||
$data = $this->preProcessDataBlocks($data);
|
$data = $this->preProcessDataBlocks($data);
|
||||||
$replacements = [];
|
$replacements = [];
|
||||||
|
nlog($data);
|
||||||
$contents = $this->document->getElementsByTagName('ninja');
|
$contents = $this->document->getElementsByTagName('ninja');
|
||||||
|
|
||||||
foreach ($contents as $content) {
|
foreach ($contents as $content) {
|
||||||
@ -131,15 +132,17 @@ class TemplateService
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses all variables in the document
|
* Parses all variables in the document
|
||||||
*
|
* @param array $data
|
||||||
* @return self
|
* @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) {
|
foreach($variables as $key => $variable) {
|
||||||
$html = strtr($this->getHtml(), $variable['labels']);
|
$html = strtr($html, $variable['labels']);
|
||||||
$html = strtr($html, $variable['values']);
|
$html = strtr($html, $variable['values']);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,18 +191,18 @@ class TemplateService
|
|||||||
*/
|
*/
|
||||||
private function resolveHtmlEngine(array $data): array
|
private function resolveHtmlEngine(array $data): array
|
||||||
{
|
{
|
||||||
return collect($data)->map(function ($key, $value) {
|
return collect($data)->map(function ($value, $key) {
|
||||||
|
|
||||||
$processed[$key] = [];
|
$processed = [];
|
||||||
|
|
||||||
match ($key) {
|
match ($key) {
|
||||||
'invoices' => $processed[$key] = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
|
'invoices' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
|
||||||
'quotes' => $processed[$key] = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
|
'quotes' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
|
||||||
'credits' => $processed[$key] = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
|
'credits' => $processed = (new HtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
|
||||||
'payments' => $processed[$key] = (new PaymentHtmlEngine($value->first(), $value->first()->client->contacts()->first()))->generateLabelsAndValues(),
|
'payments' => $processed = (new PaymentHtmlEngine($value->first(), $value->first()->client->contacts()->first()))->generateLabelsAndValues(),
|
||||||
'tasks' => $processed[$key] = [],
|
'tasks' => $processed = [],
|
||||||
'projects' => $processed[$key] = [],
|
'projects' => $processed = [],
|
||||||
'purchase_orders' => $processed[$key] = (new VendorHtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
|
'purchase_orders' => $processed = (new VendorHtmlEngine($value->first()->invitations()->first()))->generateLabelsAndValues(),
|
||||||
};
|
};
|
||||||
|
|
||||||
return $processed;
|
return $processed;
|
||||||
@ -210,18 +213,18 @@ class TemplateService
|
|||||||
|
|
||||||
private function preProcessDataBlocks($data): array
|
private function preProcessDataBlocks($data): array
|
||||||
{
|
{
|
||||||
return collect($data)->map(function ($key, $value){
|
return collect($data)->map(function ($value, $key){
|
||||||
|
|
||||||
$processed[$key] = [];
|
$processed = [];
|
||||||
|
|
||||||
match ($key) {
|
match ($key) {
|
||||||
'invoices' => $processed[$key] = $this->processInvoices($value),
|
'invoices' => $processed = $this->processInvoices($value),
|
||||||
'quotes' => $processed[$key] = $this->processQuotes($value),
|
'quotes' => $processed = $this->processQuotes($value),
|
||||||
'credits' => $processed[$key] = $this->processCredits($value),
|
'credits' => $processed = $this->processCredits($value),
|
||||||
'payments' => $processed[$key] = $this->processPayments($value),
|
'payments' => $processed = $this->processPayments($value),
|
||||||
'tasks' => $processed[$key] = $this->processTasks($value),
|
'tasks' => $processed = $this->processTasks($value),
|
||||||
'projects' => $processed[$key] = $this->processProjects($value),
|
'projects' => $processed = $this->processProjects($value),
|
||||||
'purchase_orders' => $processed[$key] = $this->processPurchaseOrders($value),
|
'purchase_orders' => $processed = $this->processPurchaseOrders($value),
|
||||||
};
|
};
|
||||||
|
|
||||||
return $processed;
|
return $processed;
|
||||||
@ -231,7 +234,9 @@ class TemplateService
|
|||||||
|
|
||||||
private function processInvoices($invoices): Collection
|
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
|
private function processQuotes($quotes): Collection
|
||||||
@ -269,7 +274,7 @@ class TemplateService
|
|||||||
|
|
||||||
private function processTasks($tasks): Collection
|
private function processTasks($tasks): Collection
|
||||||
{
|
{
|
||||||
return $task->makeHidden([
|
return $tasks->makeHidden([
|
||||||
'id',
|
'id',
|
||||||
'user_id',
|
'user_id',
|
||||||
'assigned_user_id',
|
'assigned_user_id',
|
||||||
@ -298,7 +303,7 @@ class TemplateService
|
|||||||
|
|
||||||
private function processPurchaseOrders($purchase_orders): array
|
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){
|
// return $purchase_orders->map(function ($purchase_order){
|
||||||
// })->toArray();
|
// })->toArray();
|
||||||
|
@ -20,7 +20,6 @@ use Tests\MockAccountData;
|
|||||||
use App\Services\PdfMaker\PdfMaker;
|
use App\Services\PdfMaker\PdfMaker;
|
||||||
use Illuminate\Support\Facades\App;
|
use Illuminate\Support\Facades\App;
|
||||||
use App\Jobs\Entity\CreateEntityPdf;
|
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 PdfDesignModel;
|
||||||
use App\Services\PdfMaker\Design as PdfMakerDesign;
|
use App\Services\PdfMaker\Design as PdfMakerDesign;
|
||||||
use App\Services\Template\TemplateService;
|
use App\Services\Template\TemplateService;
|
||||||
@ -51,6 +50,7 @@ class TemplateTest extends TestCase
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
|
{% for entity in invoices %}
|
||||||
{% for item in entity.line_items|filter(item => item.type_id == "1") %}
|
{% for item in entity.line_items|filter(item => item.type_id == "1") %}
|
||||||
<tr class="border-b dark:border-neutral-500">
|
<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.product_key }}</td>
|
||||||
@ -60,6 +60,7 @@ class TemplateTest extends TestCase
|
|||||||
<td class="whitespace-nowrap px-6 py-4 font-medium">0</td>
|
<td class="whitespace-nowrap px-6 py-4 font-medium">0</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</ninja>
|
</ninja>
|
||||||
@ -78,7 +79,7 @@ class TemplateTest extends TestCase
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testTemplateService()
|
public function testTemplateServiceBuild()
|
||||||
{
|
{
|
||||||
$design_model = Design::find(2);
|
$design_model = Design::find(2);
|
||||||
|
|
||||||
@ -89,6 +90,25 @@ class TemplateTest extends TestCase
|
|||||||
$replicated_design->is_custom = true;
|
$replicated_design->is_custom = true;
|
||||||
$replicated_design->save();
|
$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->assertNotNull($replicated_design->service());
|
||||||
$this->assertInstanceOf(TemplateService::class, $replicated_design->service());
|
$this->assertInstanceOf(TemplateService::class, $replicated_design->service());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user