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
/**
* Invoice Ninja (https://invoiceninja.com).
*
@ -87,16 +88,16 @@ class TemplateEngine
{
return $this->setEntity()
->setSettingsObject()
->setTemplates()
->replaceValues()
->renderTemplate();
->setSettingsObject()
->setTemplates()
->replaceValues()
->renderTemplate();
}
private function setEntity()
{
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();
} else {
$this->mockEntity();
@ -107,11 +108,10 @@ class TemplateEngine
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 = $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 = $this->settings_entity->getMergedSettings();
} else {
@ -155,13 +155,11 @@ class TemplateEngine
$this->raw_body = $this->body;
$this->raw_subject = $this->subject;
if($this->entity == 'purchaseOrder'){
$this->fakerValues();
}
elseif ($this->entity_obj->client()->exists()) {
if ($this->entity_obj->client()->exists()) {
$this->entityValues($this->entity_obj->client->primary_contact()->first());
}
else {
} elseif ($this->entity_obj->vendor()->exists()) {
$this->entityValues($this->entity_obj->vendor->primary_contact()->first());
} else {
$this->fakerValues();
}
@ -184,16 +182,16 @@ class TemplateEngine
]);
$this->body = $converter->convert($this->body)->getContent();
}
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();
else
$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['values']);
@ -217,16 +215,15 @@ class TemplateEngine
$data['footer'] = '';
$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));
else{
else {
$data['signature'] = $this->settings->email_signature;
$data['settings'] = $this->settings;
$data['whitelabel'] = $this->entity_obj ? $this->entity_obj->company->account->isPaid() : true;
$data['company'] = $this->entity_obj ? $this->entity_obj->company : '';
$data['settings'] = $this->settings;
}
@ -235,7 +232,6 @@ class TemplateEngine
// In order to parse variables such as $signature in the body,
// we need to replace strings with the values from HTMLEngine.
$wrapper = strtr($wrapper, $this->labels_and_values['values']);
/*If no custom design exists, send back a blank!*/
@ -269,7 +265,7 @@ class TemplateEngine
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';
DB::connection(config('database.default'))->beginTransaction();
@ -289,7 +285,7 @@ class TemplateEngine
'send_email' => true,
]);
if (! $this->entity || $this->entity == 'invoice') {
if (!$this->entity || $this->entity == 'invoice') {
$this->entity_obj = Invoice::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => auth()->user()->company()->id,
@ -321,40 +317,37 @@ class TemplateEngine
if($this->entity == 'purchaseOrder')
{
if ($this->entity == 'purchaseOrder') {
$vendor = Vendor::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => auth()->user()->company()->id,
]);
'user_id' => auth()->user()->id,
'company_id' => auth()->user()->company()->id,
]);
$contact = VendorContact::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => auth()->user()->company()->id,
'vendor_id' => $vendor->id,
'is_primary' => 1,
'send_email' => true,
]);
'user_id' => auth()->user()->id,
'company_id' => auth()->user()->company()->id,
'vendor_id' => $vendor->id,
'is_primary' => 1,
'send_email' => true,
]);
$this->entity_obj = PurchaseOrder::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => auth()->user()->company()->id,
'vendor_id' => $vendor->id,
]);
$invitation = PurchaseOrderInvitation::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => auth()->user()->company()->id,
'purchase_order_id' => $this->entity_obj->id,
'vendor_contact_id' => $contact->id,
'user_id' => auth()->user()->id,
'company_id' => auth()->user()->company()->id,
'vendor_id' => $vendor->id,
]);
$invitation = PurchaseOrderInvitation::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => auth()->user()->company()->id,
'purchase_order_id' => $this->entity_obj->id,
'vendor_contact_id' => $contact->id,
]);
}
if($vendor)
{
if ($vendor) {
$this->entity_obj->setRelation('invitations', $invitation);
$this->entity_obj->setRelation('vendor', $vendor);
@ -362,10 +355,7 @@ class TemplateEngine
$this->entity_obj->load('vendor');
$vendor->setRelation('company', auth()->user()->company());
$vendor->load('company');
}
else
{
} else {
$this->entity_obj->setRelation('invitations', $invitation);
$this->entity_obj->setRelation('client', $client);
$this->entity_obj->setRelation('company', auth()->user()->company());