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

Template stubs

This commit is contained in:
David Bomba 2023-09-27 21:44:09 +10:00
parent 7281f0f281
commit f655064fa5
3 changed files with 36 additions and 27 deletions

View File

@ -361,23 +361,23 @@ class PreviewController extends BaseController
$design_object = json_decode(json_encode(request()->input('design')),1); $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; // $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; // $vendor_id = PurchaseOrder::query()->company()->where('is_deleted', 0)->orderBy('id', 'desc')->first()->vendor_id;
$data = [ // $data = [
'invoices' => Invoice::whereHas('payments')->company()->with('client','payments')->where('client_id', $client_id)->orderBy('id','desc')->take(4)->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(), // '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(), // '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(), // '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(), // '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(), // '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(), // 'projects' => Project::query()->company()->with('tasks','client')->where('client_id', $client_id)->orderBy('id','desc')->take(2)->get(),
]; // ];
$ts = (new TemplateService()); $ts = (new TemplateService());
$ts->setCompany($company) $ts->setCompany($company)
->setTemplate($design_object) ->setTemplate($design_object)
->build($data); ->mock();
$html = $ts->getHtml(); $html = $ts->getHtml();

View File

@ -44,16 +44,14 @@ class TemplateMock
return $this->createVariables($type); return $this->createVariables($type);
})->toArray(); })->toArray();
$this->engines = [ $this->engines['invoices'] = json_decode($this->invoice_data, true);
'invoices' => json_decode($this->invoice_data, true), $this->engines['quotes'] = json_decode($this->quote_data, true);
'quotes' => json_decode($this->quote_data, true), $this->engines['credits'] = json_decode($this->credit_data, true);
'credits' => json_decode($this->credit_data, true), $this->engines['tasks'] = json_decode($this->task_data, true);
'tasks' => json_decode($this->task_data, true), $this->engines['projects'] = json_decode($this->project_data, true);
'projects' => json_decode($this->project_data, true), $this->engines['payments'] = json_decode($this->payment_data, true);
'payments' => json_decode($this->payment_data, true), $this->engines['purchase_orders'] = json_decode($this->purchase_order_data, true);
'purchase_orders' => json_decode($this->purchase_order_data, true),
];
return $this; return $this;
} }

View File

@ -46,6 +46,8 @@ class TemplateService
private array $data = []; private array $data = [];
private array $variables = [];
public ?Company $company; public ?Company $company;
public function __construct(public ?Design $template = null) public function __construct(public ?Design $template = null)
@ -85,18 +87,28 @@ class TemplateService
$this->compose() $this->compose()
->processData($data) ->processData($data)
->parseNinjaBlocks() ->parseNinjaBlocks()
->parseVariables($data); ->processVariables($data)
->parseVariables();
return $this; return $this;
} }
private function processVariables($data): self
{
$this->variables = $this->resolveHtmlEngine($data);
return $this;
}
public function mock(): self public function mock(): self
{ {
$tm = new TemplateMock($this->company); $tm = new TemplateMock($this->company);
$tm->init();
$this->data = $tm->engines; $this->data = $tm->engines;
$this->variables = $tm->variables[0];
$this->parseNinjaBlocks() $this->parseNinjaBlocks()
->parseVariables($tm->variables); ->parseVariables();
return $this; return $this;
} }
@ -153,13 +165,12 @@ class TemplateService
* @param array $data * @param array $data
* @return self * @return self
*/ */
private function parseVariables(array $data): self private function parseVariables(): self
{ {
$variables = $this->resolveHtmlEngine($data);
$html = $this->getHtml(); $html = $this->getHtml();
foreach($variables as $key => $variable) { foreach($this->variables as $key => $variable) {
if(isset($variable['labels']) && isset($variable['values'])) if(isset($variable['labels']) && isset($variable['values']))
{ {