diff --git a/app/Jobs/Entity/CreateEntityPdf.php b/app/Jobs/Entity/CreateEntityPdf.php index fe4b0ccccd4c..0f388ade0b3c 100644 --- a/app/Jobs/Entity/CreateEntityPdf.php +++ b/app/Jobs/Entity/CreateEntityPdf.php @@ -111,6 +111,7 @@ class CreateEntityPdf implements ShouldQueue /* Init a new copy of the translator*/ $t = app('translator'); + /* Set the locale*/ App::setLocale($this->client->locale()); diff --git a/app/Models/Client.php b/app/Models/Client.php index 7b09fa68f80e..5f33ed84c4bc 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -324,21 +324,6 @@ class Client extends BaseModel implements HasLocalePreference return $this->service()->updateBalance($amount); } - /** - * Adjusts client "balances" when a client - * makes a payment that goes on file, but does - * not effect the client.balance record. - * - * @param float $amount Adjustment amount - * @return Client - */ - // public function processUnappliedPayment($amount) :Client - // { - // return $this->service()->updatePaidToDate($amount) - // ->adjustCreditBalance($amount) - // ->save(); - // } - /** * Returns the entire filtered set * of settings which have been merged from diff --git a/app/Models/Vendor.php b/app/Models/Vendor.php index abde54bf77c2..48812f028cac 100644 --- a/app/Models/Vendor.php +++ b/app/Models/Vendor.php @@ -171,6 +171,11 @@ class Vendor extends BaseModel return ''; } + public function getMergedSettings() :object + { + return $this->company->settings; + } + public function purchase_order_filepath($invitation) { $contact_key = $invitation->contact->contact_key; diff --git a/app/Services/Pdf/PdfConfiguration.php b/app/Services/Pdf/PdfConfiguration.php new file mode 100644 index 000000000000..0cb663f5c11b --- /dev/null +++ b/app/Services/Pdf/PdfConfiguration.php @@ -0,0 +1,158 @@ +service = $service; + + } + + public function init() + { + + $this->setEntityType() + ->setEntityProperties() + ->setDesign(); + + return $this; + + } + + private function setEntityType() + { + + if ($this->service->invitation instanceof InvoiceInvitation) { + $this->entity = $this->service->invitation->invoice; + $this->entity_string = 'invoice'; + } elseif ($this->service->invitation instanceof QuoteInvitation) { + $this->entity = $this->service->invitation->quote; + $this->entity_string = 'quote'; + } elseif ($this->service->invitation instanceof CreditInvitation) { + $this->entity = $this->service->invitation->credit; + $this->entity_string = 'credit'; + } elseif ($this->service->invitation instanceof RecurringInvoiceInvitation) { + $this->entity = $this->service->invitation->recurring_invoice; + $this->entity_string = 'recurring_invoice'; + } elseif ($this->service->invitation instanceof PurchaseOrderInvitation) { + $this->entity = $this->service->invitation->purchase_order; + $this->entity_string = 'purchase_order'; + } else { + throw new \Exception('Unable to resolve entity', 500); + } + + return $this; + } + + private function setEntityProperties() + { + $entity_design_id = ''; + + if ($this->entity instanceof Invoice) { + + $this->client = $this->entity->client; + $this->path = $this->client->invoice_filepath($this->service->invitation); + $this->entity_design_id = 'invoice_design_id'; + $this->settings = $this->client->getMergedSettings(); + $this->settings_object = $this->client; + + } elseif ($this->entity instanceof Quote) { + + $this->client = $this->entity->client; + $this->path = $this->client->quote_filepath($this->service->invitation); + $this->entity_design_id = 'quote_design_id'; + $this->settings = $this->client->getMergedSettings(); + $this->settings_object = $this->client; + + } elseif ($this->entity instanceof Credit) { + + $this->client = $this->entity->client; + $this->path = $this->client->credit_filepath($this->service->invitation); + $this->entity_design_id = 'credit_design_id'; + $this->settings = $this->client->getMergedSettings(); + $this->settings_object = $this->client; + + } elseif ($this->entity instanceof RecurringInvoice) { + + $this->client = $this->entity->client; + $this->path = $this->client->recurring_invoice_filepath($this->service->invitation); + $this->entity_design_id = 'invoice_design_id'; + $this->settings = $this->client->getMergedSettings(); + $this->settings_object = $this->client; + + } elseif ($this->entity instanceof PurchaseOrder) { + + $this->vendor = $this->entity->vendor; + $this->path = $this->vendor->purchase_order_filepath($this->service->invitation); + $this->entity_design_id = 'invoice_design_id'; + $this->entity_design_id = 'purchase_order_design_id'; + $this->settings = $this->vendor->getMergedSettings(); + $this->settings_object = $this->client; + + } + else + throw new \Exception('Unable to resolve entity properties type', 500); + + $this->path = $this->path.$this->entity->numberFormatter().'.pdf'; + + return $this; + } + + private function setDesign() + { + + $design_id = $this->entity->design_id ? : $this->decodePrimaryKey($this->settings_object->getSetting($this->entity_design_id)); + + $this->design = Design::find($design_id ?: 2); + + return $this; + + } + +} \ No newline at end of file diff --git a/app/Services/Pdf/PdfService.php b/app/Services/Pdf/PdfService.php new file mode 100644 index 000000000000..306bc92f0f9d --- /dev/null +++ b/app/Services/Pdf/PdfService.php @@ -0,0 +1,42 @@ +invitation = $invitation; + + $this->config = (new PdfConfiguration($this))->init(); + + } + + public function getPdf() + { + + } + + public function getHtml() + { + + } + +} \ No newline at end of file diff --git a/tests/Pdf/PdfServiceTest.php b/tests/Pdf/PdfServiceTest.php new file mode 100644 index 000000000000..30371619b10e --- /dev/null +++ b/tests/Pdf/PdfServiceTest.php @@ -0,0 +1,68 @@ +makeTestData(); + } + + public function testInitOfClass() + { + + $invitation = $this->invoice->invitations->first(); + + $service = new PdfService($invitation); + + $this->assertInstanceOf(PdfService::class, $service); + + } + + public function testEntityResolution() + { + + $invitation = $this->invoice->invitations->first(); + + $service = new PdfService($invitation); + + $this->assertInstanceOf(PdfConfiguration::class, $service->config); + + + } + + public function testDefaultDesign() + { + $invitation = $this->invoice->invitations->first(); + + $service = new PdfService($invitation); + + $this->assertEquals(2, $service->config->design->id); + + } +} \ No newline at end of file