From 2bc8064eeb3d9055a25ff7241da248547e41b6ad Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 6 Mar 2020 23:18:32 +1100 Subject: [PATCH] Fixes for setting entity defaults (#3440) --- app/Jobs/Credit/CreateCreditPdf.php | 2 +- app/Jobs/Invoice/CreateInvoicePdf.php | 7 ++++--- app/Jobs/Quote/CreateQuotePdf.php | 3 +-- app/Models/Client.php | 28 +++++---------------------- app/Repositories/BaseRepository.php | 10 +++------- 5 files changed, 14 insertions(+), 36 deletions(-) diff --git a/app/Jobs/Credit/CreateCreditPdf.php b/app/Jobs/Credit/CreateCreditPdf.php index 31f03b875f..94b52d2181 100644 --- a/app/Jobs/Credit/CreateCreditPdf.php +++ b/app/Jobs/Credit/CreateCreditPdf.php @@ -76,7 +76,7 @@ class CreateCreditPdf implements ShouldQueue { $file_path = $path . $this->credit->number . '.pdf'; - $design = Design::find($this->credit->client->getSetting('credit_design_id')); + $design = Design::find($this->decodePrimaryKey($this->credit->client->getSetting('credit_design_id'))); if($design->is_custom){ $credit_design = new Custom($design->design); diff --git a/app/Jobs/Invoice/CreateInvoicePdf.php b/app/Jobs/Invoice/CreateInvoicePdf.php index ce757b4f40..367b2c8407 100644 --- a/app/Jobs/Invoice/CreateInvoicePdf.php +++ b/app/Jobs/Invoice/CreateInvoicePdf.php @@ -19,9 +19,10 @@ use App\Models\ClientContact; use App\Models\Company; use App\Models\Design; use App\Models\Invoice; -use App\Utils\Traits\Pdf\PdfMaker; +use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesInvoiceHtml; use App\Utils\Traits\NumberFormatter; +use App\Utils\Traits\Pdf\PdfMaker; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; @@ -33,7 +34,7 @@ use Spatie\Browsershot\Browsershot; class CreateInvoicePdf implements ShouldQueue { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker; + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash; public $invoice; @@ -74,7 +75,7 @@ class CreateInvoicePdf implements ShouldQueue { $file_path = $path . $this->invoice->number . '.pdf'; - $design = Design::find($this->invoice->client->getSetting('invoice_design_id')); + $design = Design::find($this->decodePrimaryKey($this->invoice->client->getSetting('invoice_design_id'))); if($design->is_custom){ $invoice_design = new Custom($design->design); diff --git a/app/Jobs/Quote/CreateQuotePdf.php b/app/Jobs/Quote/CreateQuotePdf.php index 023aafbe72..5d595f3dd4 100644 --- a/app/Jobs/Quote/CreateQuotePdf.php +++ b/app/Jobs/Quote/CreateQuotePdf.php @@ -76,8 +76,7 @@ class CreateQuotePdf implements ShouldQueue { $path = $this->quote->client->quote_filepath(); - - $design = Design::find($this->quote->client->getSetting('quote_design_id')); + $design = Design::find($this->decodePrimaryKey($this->quote->client->getSetting('quote_design_id'))); if($design->is_custom){ $quote_design = new Custom($design->design); diff --git a/app/Models/Client.php b/app/Models/Client.php index 3f3eadadbf..0bf88057b5 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -466,31 +466,13 @@ class Client extends BaseModel implements HasLocalePreference return $this->company->company_key . '/'; } - - public function setInvoiceDefaults($invoice) :Invoice + public function setCompanyDefaults($data, $entity_name) { - $invoice->terms = $this->getSetting('invoice_terms'); - $invoice->footer = $this->getSetting('invoice_footer'); - $invoice->public_notes = isset($this->public_notes) ? $this->public_notes : ''; + $data['terms'] = $this->getSetting($entity_name.'_terms'); + $data['footer'] =$this->getSetting($entity_name.'_footer'); + $data['public_notes'] = isset($this->public_notes) ? $this->public_notes : ''; - return $invoice; + return $data; } - public function setQuoteDefaults($quote) :Quote - { - $quote->terms = $this->getSetting('quote_terms'); - $quote->footer = $this->getSetting('quote_footer'); - $quote->public_notes = isset($this->public_notes) ? $this->public_notes : ''; - - return $quote; - } - - public function setCreditDefaults($credit) :Credit - { - $credit->terms = $this->getSetting('credit_terms'); - $credit->footer = $this->getSetting('credit_footer'); - $credit->public_notes = isset($this->public_notes) ? $this->public_notes : ''; - - return $credit; - } } diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 8fe5798ba2..99e17d3a5f 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -194,19 +194,15 @@ class BaseRepository $resource = explode('\\', $class->name)[2]; /** This will extract 'Invoice' from App\Models\Invoice */ $lcfirst_resource_id = lcfirst($resource) . '_id'; - //if new, set defaults! - if(!$model->id) { - $methodName = "set" . $resource . "Defaults"; - $model = $client->{$methodName}($model); - } - - if ($class->name == Invoice::class || $class->name == Quote::class) $state['starting_amount'] = $model->amount; if (!$model->id) { + $data = $client->setCompanyDefaults($data,lcfirst($resource)); $model->uses_inclusive_taxes = $client->getSetting('inclusive_taxes'); } + +\Log::error(print_r($data,1)); $model->fill($data); $model->save();