From f655064fa530cf66b32584cdd63562667cad09f0 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 27 Sep 2023 21:44:09 +1000 Subject: [PATCH] Template stubs --- app/Http/Controllers/PreviewController.php | 24 +++++++++++----------- app/Services/Template/TemplateMock.php | 18 ++++++++-------- app/Services/Template/TemplateService.php | 21 ++++++++++++++----- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/app/Http/Controllers/PreviewController.php b/app/Http/Controllers/PreviewController.php index 49ab966632..cae588da70 100644 --- a/app/Http/Controllers/PreviewController.php +++ b/app/Http/Controllers/PreviewController.php @@ -361,23 +361,23 @@ class PreviewController extends BaseController $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; + // $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')->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(), - ]; + // $data = [ + // '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()); $ts->setCompany($company) ->setTemplate($design_object) - ->build($data); + ->mock(); $html = $ts->getHtml(); diff --git a/app/Services/Template/TemplateMock.php b/app/Services/Template/TemplateMock.php index 9ec86e030d..b1bc34a224 100644 --- a/app/Services/Template/TemplateMock.php +++ b/app/Services/Template/TemplateMock.php @@ -44,16 +44,14 @@ class TemplateMock return $this->createVariables($type); })->toArray(); - $this->engines = [ - 'invoices' => json_decode($this->invoice_data, true), - 'quotes' => json_decode($this->quote_data, true), - 'credits' => json_decode($this->credit_data, true), - 'tasks' => json_decode($this->task_data, true), - 'projects' => json_decode($this->project_data, true), - 'payments' => json_decode($this->payment_data, true), - 'purchase_orders' => json_decode($this->purchase_order_data, true), - ]; - + $this->engines['invoices'] = json_decode($this->invoice_data, true); + $this->engines['quotes'] = json_decode($this->quote_data, true); + $this->engines['credits'] = json_decode($this->credit_data, true); + $this->engines['tasks'] = json_decode($this->task_data, true); + $this->engines['projects'] = json_decode($this->project_data, true); + $this->engines['payments'] = json_decode($this->payment_data, true); + $this->engines['purchase_orders'] = json_decode($this->purchase_order_data, true); + return $this; } diff --git a/app/Services/Template/TemplateService.php b/app/Services/Template/TemplateService.php index 2f76aea671..79263cc74b 100644 --- a/app/Services/Template/TemplateService.php +++ b/app/Services/Template/TemplateService.php @@ -46,6 +46,8 @@ class TemplateService private array $data = []; + private array $variables = []; + public ?Company $company; public function __construct(public ?Design $template = null) @@ -85,18 +87,28 @@ class TemplateService $this->compose() ->processData($data) ->parseNinjaBlocks() - ->parseVariables($data); + ->processVariables($data) + ->parseVariables(); return $this; } + private function processVariables($data): self + { + $this->variables = $this->resolveHtmlEngine($data); + + return $this; + } public function mock(): self { $tm = new TemplateMock($this->company); + $tm->init(); + $this->data = $tm->engines; + $this->variables = $tm->variables[0]; $this->parseNinjaBlocks() - ->parseVariables($tm->variables); + ->parseVariables(); return $this; } @@ -153,13 +165,12 @@ class TemplateService * @param array $data * @return self */ - private function parseVariables(array $data): self + private function parseVariables(): self { - $variables = $this->resolveHtmlEngine($data); $html = $this->getHtml(); - foreach($variables as $key => $variable) { + foreach($this->variables as $key => $variable) { if(isset($variable['labels']) && isset($variable['values'])) {