1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00

Stubbing mock data for templates

This commit is contained in:
David Bomba 2023-09-27 20:36:08 +10:00
parent 99e9723fea
commit 98dd01e1d5
4 changed files with 133 additions and 27 deletions

View File

@ -16,6 +16,7 @@ use App\Utils\Ninja;
use App\Models\Quote;
use App\Models\Client;
use App\Models\Credit;
use App\Models\Vendor;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\Project;
@ -24,6 +25,7 @@ use App\Libraries\MultiDB;
use App\Factory\QuoteFactory;
use App\Jobs\Util\PreviewPdf;
use App\Models\ClientContact;
use App\Models\PurchaseOrder;
use App\Services\Pdf\PdfMock;
use App\Factory\CreditFactory;
use App\Factory\InvoiceFactory;
@ -51,7 +53,6 @@ use App\Http\Requests\Preview\DesignPreviewRequest;
use App\Services\PdfMaker\Design as PdfDesignModel;
use App\Services\PdfMaker\Design as PdfMakerDesign;
use App\Http\Requests\Preview\PreviewInvoiceRequest;
use App\Models\PurchaseOrder;
class PreviewController extends BaseController
{
@ -358,17 +359,19 @@ class PreviewController extends BaseController
/** @var \App\Models\Company $company */
$company = $user->company();
// $template = request()->input('design');
$design_object = json_decode(json_encode(request()->input('design')),1);
$client_id = Invoice::whereHas('payments')->company()->where('is_deleted', 0)->orderBy('id','desc')->first()->client_id;
$vendor_id = PurchaseOrder::query()->company()->where('is_deleted', 0)->orderBy('id', 'desc')->first()->vendor_id;
$data = [
'invoices' => Invoice::whereHas('payments')->with('client','payments')->company()->orderBy('id','desc')->take(4)->get(),
'quotes' => Quote::query()->company()->with('client')->orderBy('id','desc')->take(4)->get(),
'credits' => Credit::query()->company()->with('client')->orderBy('id','desc')->take(4)->get(),
'payments' => Payment::query()->company()->with('client')->orderBy('id','desc')->take(4)->get(),
'purchase_orders' => PurchaseOrder::query()->with('vendor')->company()->orderBy('id','desc')->take(5)->get(),
'tasks' => Task::query()->with('client','invoice')->company()->orderBy('id','desc')->take(2)->get(),
'projects' => Project::query()->with('tasks','client')->company()->orderBy('id','desc')->take(2)->get(),
'invoices' => Invoice::whereHas('payments')->company()->with('client','payments')->where('client_id', $client_id)->orderBy('id','desc')->take(4)->get(),
'quotes' => Quote::query()->company()->with('client')->where('client_id', $client_id)->orderBy('id','desc')->take(4)->get(),
'credits' => Credit::query()->company()->with('client')->where('client_id', $client_id)->orderBy('id','desc')->take(4)->get(),
'payments' => Payment::query()->company()->with('client')->where('client_id', $client_id)->orderBy('id','desc')->take(4)->get(),
'purchase_orders' => PurchaseOrder::query()->company()->with('vendor')->where('vendor_id', $vendor_id)->orderBy('id','desc')->take(5)->get(),
'tasks' => Task::query()->company()->with('client','invoice')->where('client_id', $client_id)->orderBy('id','desc')->take(2)->get(),
'projects' => Project::query()->company()->with('tasks','client')->where('client_id', $client_id)->orderBy('id','desc')->take(2)->get(),
];
$ts = (new TemplateService());

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,69 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Template;
use App\Models\Company;
use App\Services\Pdf\PdfMock;
class TemplateMock
{
use MockTrait;
private Company $company;
public function _invoke(Company $company): array
{
$this->company = $company;
$variables = collect(['invoices', 'quotes', 'credits', 'payments', 'purchase_orders'])->map(function ($type) {
$this->createVariables($type);
})->toArray();
$variables = array_merge($variables, [
'invoices' => $this->createTemplate('invoices'),
'quotes' => $this->createTemplate('quotes'),
'credits' => $this->createTemplate('credits'),
'tasks' => $this->createTemplate('tasks'),
'projects' => $this->createTemplate('projects'),
'payments' => $this->createTemplate('payments'),
'purchase_orders' => $this->createTemplate('purchase_orders'),
]);
return $variables;
}
/**
* ['entity_type','design','settings_type','settings']
*
* @param string $type
* @return array
*/
private function createVariables(string $type): array
{
$data = [
'entity_type' => rtrim($type,"s"),
'design' => '',
'settings_type' => 'company',
'settings' => $this->company->settings,
];
$mock = (new PdfMock($data, $this->company))->build();
return [$type => $mock->getStubVariables()];
}
private function createTemplate(string $type): array
{
}
}

View File

@ -43,6 +43,8 @@ class TemplateService
private string $compiled_html = '';
private array $data = [];
public function __construct(public ?Design $template = null)
{
$this->template = $template;
@ -78,30 +80,43 @@ class TemplateService
public function build(array $data): self
{
$this->compose()
->parseNinjaBlocks($data)
->processData($data)
->parseNinjaBlocks()
->parseVariables($data);
return $this;
}
public function mock(): self
{
return $this;
}
public function getHtml(): string
{
return $this->compiled_html;
}
private function processData($data): self
{
$this->data = $this->preProcessDataBlocks($data);
nlog(json_encode($this->data));
return $this;
}
/**
* Parses all Ninja tags in the document
*
* @param array $data
*
* @return self
*/
private function parseNinjaBlocks(array $data): self
private function parseNinjaBlocks(): self
{
$data = $this->preProcessDataBlocks($data);
$replacements = [];
// nlog($data);
$contents = $this->document->getElementsByTagName('ninja');
foreach ($contents as $content) {
@ -109,9 +124,7 @@ class TemplateService
$template = $content->ownerDocument->saveHTML($content);
$template = $this->twig->createTemplate(html_entity_decode($template));
$template = $template->render($data);
// nlog($template);
$template = $template->render($this->data);
$f = $this->document->createDocumentFragment();
$f->appendXML($template);
@ -196,7 +209,7 @@ class TemplateService
* @return self
*/
public function setTemplate(array $partials): self
{nlog($partials);
{
$html = '';
$html .= $partials['design']['includes'];
@ -262,28 +275,27 @@ class TemplateService
})->toArray();
}
private function processInvoices($invoices): array
public function processInvoices($invoices): array
{
$it = new InvoiceTransformer();
$it->setDefaultIncludes(['client','payments']);
$it->setDefaultIncludes(['client','payments', 'credits']);
$manager = new Manager();
$manager->parseIncludes(['client','payments','payments.type']);
$manager->parseIncludes(['client','payments','payments.type','credits']);
$resource = new \League\Fractal\Resource\Collection($invoices, $it, null);
$invoices = $manager->createData($resource)->toArray();
// nlog($invoices);
foreach($invoices['data'] as $key => $invoice)
{
$invoices['data'][$key]['client'] = $invoice['client']['data'] ?? [];
$invoices['data'][$key]['client']['contacts'] = $invoice['client']['data']['contacts']['data'] ?? [];
$invoices['data'][$key]['payments'] = $invoice['payments']['data'] ?? [];
$invoices['data'][$key]['credits'] = $invoice['credits']['data'] ?? [];
if($invoice['payments']['data'] ?? false) {
foreach($invoice['payments']['data'] as $keyx => $payment) {
$invoices['data'][$key]['payments'][$keyx]['paymentables']= $payment['paymentables']['data'] ?? [];
$invoices['data'][$key]['payments'][$keyx]['type']= $payment['type']['data'] ?? [];
$invoices['data'][$key]['payments'][$keyx]['paymentables'] = $payment['paymentables']['data'] ?? [];
$invoices['data'][$key]['payments'][$keyx]['type'] = $payment['type']['data'] ?? [];
}
}