Fixes for sending purchase orders when using a custom template

This commit is contained in:
David Bomba 2022-09-09 09:12:43 +10:00
parent 98127fbab4
commit d069d6f0e1

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* Invoice Ninja (https://invoiceninja.com). * Invoice Ninja (https://invoiceninja.com).
* *
@ -96,7 +97,7 @@ class TemplateEngine
private function setEntity() private function setEntity()
{ {
if (strlen($this->entity) > 1 && strlen($this->entity_id) > 1) { if (strlen($this->entity) > 1 && strlen($this->entity_id) > 1) {
$class = 'App\Models\\'.ucfirst(Str::camel($this->entity)); $class = 'App\Models\\' . ucfirst(Str::camel($this->entity));
$this->entity_obj = $class::withTrashed()->where('id', $this->decodePrimaryKey($this->entity_id))->company()->first(); $this->entity_obj = $class::withTrashed()->where('id', $this->decodePrimaryKey($this->entity_id))->company()->first();
} else { } else {
$this->mockEntity(); $this->mockEntity();
@ -107,11 +108,10 @@ class TemplateEngine
private function setSettingsObject() private function setSettingsObject()
{ {
if($this->entity == 'purchaseOrder' || $this->entity == 'purchase_order'){ if ($this->entity == 'purchaseOrder' || $this->entity == 'purchase_order') {
$this->settings_entity = auth()->user()->company(); $this->settings_entity = auth()->user()->company();
$this->settings = $this->settings_entity->settings; $this->settings = $this->settings_entity->settings;
} } elseif ($this->entity_obj->client()->exists()) {
elseif ($this->entity_obj->client()->exists()) {
$this->settings_entity = $this->entity_obj->client; $this->settings_entity = $this->entity_obj->client;
$this->settings = $this->settings_entity->getMergedSettings(); $this->settings = $this->settings_entity->getMergedSettings();
} else { } else {
@ -155,13 +155,11 @@ class TemplateEngine
$this->raw_body = $this->body; $this->raw_body = $this->body;
$this->raw_subject = $this->subject; $this->raw_subject = $this->subject;
if($this->entity == 'purchaseOrder'){ if ($this->entity_obj->client()->exists()) {
$this->fakerValues();
}
elseif ($this->entity_obj->client()->exists()) {
$this->entityValues($this->entity_obj->client->primary_contact()->first()); $this->entityValues($this->entity_obj->client->primary_contact()->first());
} } elseif ($this->entity_obj->vendor()->exists()) {
else { $this->entityValues($this->entity_obj->vendor->primary_contact()->first());
} else {
$this->fakerValues(); $this->fakerValues();
} }
@ -184,16 +182,16 @@ class TemplateEngine
]); ]);
$this->body = $converter->convert($this->body)->getContent(); $this->body = $converter->convert($this->body)->getContent();
} }
private function entityValues($contact) private function entityValues($contact)
{ {
if($this->entity == 'purchaseOrder') if (in_array($this->entity, ['purchaseOrder', 'purchase_order']))
$this->labels_and_values = (new VendorHtmlEngine($this->entity_obj->invitations->first()))->generateLabelsAndValues(); $this->labels_and_values = (new VendorHtmlEngine($this->entity_obj->invitations->first()))->generateLabelsAndValues();
else else
$this->labels_and_values = (new HtmlEngine($this->entity_obj->invitations->first()))->generateLabelsAndValues(); $this->labels_and_values = (new HtmlEngine($this->entity_obj->invitations->first()))->generateLabelsAndValues();
$this->body = strtr($this->body, $this->labels_and_values['labels']); $this->body = strtr($this->body, $this->labels_and_values['labels']);
$this->body = strtr($this->body, $this->labels_and_values['values']); $this->body = strtr($this->body, $this->labels_and_values['values']);
@ -217,16 +215,15 @@ class TemplateEngine
$data['footer'] = ''; $data['footer'] = '';
$data['logo'] = auth()->user()->company()->present()->logo(); $data['logo'] = auth()->user()->company()->present()->logo();
if($this->entity_obj->client()->exists()) if ($this->entity_obj->client()->exists())
$data = array_merge($data, Helpers::sharedEmailVariables($this->entity_obj->client)); $data = array_merge($data, Helpers::sharedEmailVariables($this->entity_obj->client));
else{ else {
$data['signature'] = $this->settings->email_signature; $data['signature'] = $this->settings->email_signature;
$data['settings'] = $this->settings; $data['settings'] = $this->settings;
$data['whitelabel'] = $this->entity_obj ? $this->entity_obj->company->account->isPaid() : true; $data['whitelabel'] = $this->entity_obj ? $this->entity_obj->company->account->isPaid() : true;
$data['company'] = $this->entity_obj ? $this->entity_obj->company : ''; $data['company'] = $this->entity_obj ? $this->entity_obj->company : '';
$data['settings'] = $this->settings; $data['settings'] = $this->settings;
} }
@ -235,7 +232,6 @@ class TemplateEngine
// In order to parse variables such as $signature in the body, // In order to parse variables such as $signature in the body,
// we need to replace strings with the values from HTMLEngine. // we need to replace strings with the values from HTMLEngine.
$wrapper = strtr($wrapper, $this->labels_and_values['values']); $wrapper = strtr($wrapper, $this->labels_and_values['values']);
/*If no custom design exists, send back a blank!*/ /*If no custom design exists, send back a blank!*/
@ -269,7 +265,7 @@ class TemplateEngine
private function mockEntity() private function mockEntity()
{ {
if(!$this->entity && $this->template && str_contains($this->template, 'purchase_order')) if (!$this->entity && $this->template && str_contains($this->template, 'purchase_order'))
$this->entity = 'purchaseOrder'; $this->entity = 'purchaseOrder';
DB::connection(config('database.default'))->beginTransaction(); DB::connection(config('database.default'))->beginTransaction();
@ -289,7 +285,7 @@ class TemplateEngine
'send_email' => true, 'send_email' => true,
]); ]);
if (! $this->entity || $this->entity == 'invoice') { if (!$this->entity || $this->entity == 'invoice') {
$this->entity_obj = Invoice::factory()->create([ $this->entity_obj = Invoice::factory()->create([
'user_id' => auth()->user()->id, 'user_id' => auth()->user()->id,
'company_id' => auth()->user()->company()->id, 'company_id' => auth()->user()->company()->id,
@ -321,8 +317,7 @@ class TemplateEngine
if($this->entity == 'purchaseOrder') if ($this->entity == 'purchaseOrder') {
{
$vendor = Vendor::factory()->create([ $vendor = Vendor::factory()->create([
'user_id' => auth()->user()->id, 'user_id' => auth()->user()->id,
@ -350,11 +345,9 @@ class TemplateEngine
'purchase_order_id' => $this->entity_obj->id, 'purchase_order_id' => $this->entity_obj->id,
'vendor_contact_id' => $contact->id, 'vendor_contact_id' => $contact->id,
]); ]);
} }
if($vendor) if ($vendor) {
{
$this->entity_obj->setRelation('invitations', $invitation); $this->entity_obj->setRelation('invitations', $invitation);
$this->entity_obj->setRelation('vendor', $vendor); $this->entity_obj->setRelation('vendor', $vendor);
@ -362,10 +355,7 @@ class TemplateEngine
$this->entity_obj->load('vendor'); $this->entity_obj->load('vendor');
$vendor->setRelation('company', auth()->user()->company()); $vendor->setRelation('company', auth()->user()->company());
$vendor->load('company'); $vendor->load('company');
} else {
}
else
{
$this->entity_obj->setRelation('invitations', $invitation); $this->entity_obj->setRelation('invitations', $invitation);
$this->entity_obj->setRelation('client', $client); $this->entity_obj->setRelation('client', $client);
$this->entity_obj->setRelation('company', auth()->user()->company()); $this->entity_obj->setRelation('company', auth()->user()->company());