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).
* *
@ -110,8 +111,7 @@ class TemplateEngine
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']);
@ -226,7 +224,6 @@ class TemplateEngine
$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!*/
@ -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());