diff --git a/app/DataMapper/ClientSettings.php b/app/DataMapper/ClientSettings.php index d0f289751820..55348aeb271b 100644 --- a/app/DataMapper/ClientSettings.php +++ b/app/DataMapper/ClientSettings.php @@ -90,7 +90,7 @@ class ClientSettings extends BaseSettings public $industry_id; public $size_id; - + public $design; /** * Cast object values and return entire class * prevents missing properties from not being returned diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index 10094d494351..8e7a8c16cd52 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -112,6 +112,8 @@ class CompanySettings extends BaseSettings public $counter_padding; public $default_gateway; + + public $design; /** * Cast object values and return entire class * prevents missing properties from not being returned @@ -165,6 +167,7 @@ class CompanySettings extends BaseSettings 'credit_number_prefix' => '', 'client_number_prefix' => '', 'auto_archive_invoice' => 'FALSE', + 'design' => 'pdf.design1', 'translations' => (object) [], ]; diff --git a/app/Listeners/Invoice/CreateInvoicePdf.php b/app/Listeners/Invoice/CreateInvoicePdf.php index 18649a739415..11e3c6d7c0bf 100644 --- a/app/Listeners/Invoice/CreateInvoicePdf.php +++ b/app/Listeners/Invoice/CreateInvoicePdf.php @@ -14,6 +14,7 @@ namespace App\Listeners\Invoice; use App\Utils\Traits\MakesHash; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; use Spatie\Browsershot\Browsershot; @@ -39,49 +40,54 @@ class CreateInvoicePdf { $invoice = $event->invoice; - $path = $invoice->client->client_hash . '/invoices/'; + $path = 'public/' . $invoice->client->client_hash . '/invoices/'; $file = $path . $invoice->invoice_number . '.pdf'; + + Log::error($file); //get invoice template - $html = $this->generateInvoiceHtml($invoice->settings->template, $invoice) + $html = $this->generateInvoiceHtml($invoice->design(), $invoice); //todo - move this to the client creation stage so we don't keep hitting this unnecessarily - Storage::makeDirectory('public/' . $path, 0755); + Storage::makeDirectory($path, 0755); //create pdf - $this->makePdf(null,null,$html,$file) + $pdf = $this->makePdf(null,null,$html, $file); + // $path = Storage::putFile($file, $pdf); + //store pdf //$path = Storage::putFile('public/' . $path, $this->file); //$url = Storage::url($path); } - private function makePdf($header, $footer, $html, $pdf) : void + private function makePdf($header, $footer, $html, $pdf) { - Browsershot::html($html) + return Browsershot::html($html) //->showBrowserHeaderAndFooter() //->headerHtml($header) //->footerHtml($footer) ->waitUntilNetworkIdle() //->margins(10,10,10,10) - ->savePdf($pdf); + ->savePdf('test.pdf'); } /** * Generate the HTML invoice parsing variables * and generating the final invoice HTML * - * @param string $template either the path to the design template, OR the full design template string + * @param string $design either the path to the design template, OR the full design template string * @param Collection $invoice The invoice object * @return string The invoice string in HTML format */ - private function generateInvoiceHtml($template, $invoice) :string + private function generateInvoiceHtml($design, $invoice) :string { //swap labels - - $html = view(.$template, $invoice)->render(); + $data['invoice'] = $invoice; - return view('pdf.stub', $html); + return view($design, $data)->render(); + + //return view('pdf.stub', $html)->render(); } } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 38298b43bb56..c1ab91a143fe 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -236,4 +236,15 @@ class Invoice extends BaseModel break; } } + + /** + * Returns the template for the invoice + * + * @return string Either the template view, OR the template HTML stirng + */ + public function design() :string + { + return $this->settings->design ?: 'pdf.design1'; + } + } \ No newline at end of file diff --git a/app/Models/Presenters/InvoicePresenter.php b/app/Models/Presenters/InvoicePresenter.php index ba58d1b59794..50f58e31460c 100644 --- a/app/Models/Presenters/InvoicePresenter.php +++ b/app/Models/Presenters/InvoicePresenter.php @@ -36,7 +36,7 @@ class InvoicePresenter extends EntityPresenter return $this->client->present()->address(); } - pubic function shipping_address() + public function shipping_address() { return $this->client->present()->shipping_address(); } diff --git a/database/seeds/RandomDataSeeder.php b/database/seeds/RandomDataSeeder.php index 32aa4e33c2f9..2c09eca8dc2d 100644 --- a/database/seeds/RandomDataSeeder.php +++ b/database/seeds/RandomDataSeeder.php @@ -1,5 +1,6 @@ create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); - factory(\App\Models\Payment::class,20)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); - + factory(\App\Models\Payment::class,20)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id, 'settings' => ClientSettings::buildClientSettings($company->settings, $client->settings)]); $clients = Client::all(); diff --git a/tests/Unit/CompareObjectTest.php b/tests/Unit/CompareObjectTest.php index e81f9dbc197c..0f291e9c9c2e 100644 --- a/tests/Unit/CompareObjectTest.php +++ b/tests/Unit/CompareObjectTest.php @@ -4,6 +4,7 @@ namespace Tests\Unit; use App\DataMapper\ClientSettings; use App\DataMapper\CompanySettings; +use Illuminate\Support\Facades\Log; use Tests\TestCase; /** @@ -35,15 +36,14 @@ class CompareObjectTest extends TestCase $this->client_settings->{$key} = $this->company_settings->{$key}; } - return $this->client_settings; } public function testProperties() { - $build_client_settings = $this->buildClientSettings(); + $build_client_settings = $this->buildClientSettings(); $this->assertEquals($build_client_settings->timezone_id, 15); $this->assertEquals($build_client_settings->currency_id, 1);