1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Stubs for refactor for PDF generation

This commit is contained in:
David Bomba 2022-12-23 13:22:01 +11:00
parent 6400ece913
commit 1d28a98a55
6 changed files with 274 additions and 15 deletions

View File

@ -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());

View File

@ -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

View File

@ -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;

View File

@ -0,0 +1,158 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Pdf;
use App\Models\Client;
use App\Models\Credit;
use App\Models\CreditInvitation;
use App\Models\Design;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
use App\Models\PurchaseOrder;
use App\Models\PurchaseOrderInvitation;
use App\Models\Quote;
use App\Models\QuoteInvitation;
use App\Models\RecurringInvoice;
use App\Models\RecurringInvoiceInvitation;
use App\Models\Vendor;
use App\Services\Pdf\PdfService;
use App\Utils\Traits\MakesHash;
class PdfConfiguration
{
use MakesHash;
public Design $design;
public ?Client $client;
public ?Vendor $vendor;
public object $settings;
public $settings_object;
public $entity;
public string $entity_string;
public $service;
public function __construct(PdfService $service)
{
$this->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;
}
}

View File

@ -0,0 +1,42 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Pdf;
use App\Services\Pdf\PdfConfiguration;
class PdfService
{
public $invitation;
public PdfConfiguration $config;
public function __construct($invitation)
{
$this->invitation = $invitation;
$this->config = (new PdfConfiguration($this))->init();
}
public function getPdf()
{
}
public function getHtml()
{
}
}

View File

@ -0,0 +1,68 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace Tests\Pdf;
use App\Services\Pdf\PdfConfiguration;
use App\Services\Pdf\PdfService;
use Beganovich\Snappdf\Snappdf;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
* @covers App\Services\Pdf\PdfService
*/
class PdfServiceTest extends TestCase
{
use MockAccountData;
protected function setUp(): void
{
parent::setUp();
$this->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);
}
}