Working on PDF generation with mocked Invoice Data. - Finishing saving to file location

This commit is contained in:
David Bomba 2019-08-29 22:47:45 +10:00
parent 2c65a6305c
commit 10272a1eeb
7 changed files with 39 additions and 18 deletions

View File

@ -90,7 +90,7 @@ class ClientSettings extends BaseSettings
public $industry_id; public $industry_id;
public $size_id; public $size_id;
public $design;
/** /**
* Cast object values and return entire class * Cast object values and return entire class
* prevents missing properties from not being returned * prevents missing properties from not being returned

View File

@ -112,6 +112,8 @@ class CompanySettings extends BaseSettings
public $counter_padding; public $counter_padding;
public $default_gateway; public $default_gateway;
public $design;
/** /**
* Cast object values and return entire class * Cast object values and return entire class
* prevents missing properties from not being returned * prevents missing properties from not being returned
@ -165,6 +167,7 @@ class CompanySettings extends BaseSettings
'credit_number_prefix' => '', 'credit_number_prefix' => '',
'client_number_prefix' => '', 'client_number_prefix' => '',
'auto_archive_invoice' => 'FALSE', 'auto_archive_invoice' => 'FALSE',
'design' => 'pdf.design1',
'translations' => (object) [], 'translations' => (object) [],
]; ];

View File

@ -14,6 +14,7 @@ namespace App\Listeners\Invoice;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Spatie\Browsershot\Browsershot; use Spatie\Browsershot\Browsershot;
@ -39,17 +40,21 @@ class CreateInvoicePdf
{ {
$invoice = $event->invoice; $invoice = $event->invoice;
$path = $invoice->client->client_hash . '/invoices/'; $path = 'public/' . $invoice->client->client_hash . '/invoices/';
$file = $path . $invoice->invoice_number . '.pdf'; $file = $path . $invoice->invoice_number . '.pdf';
Log::error($file);
//get invoice template //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 //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 //create pdf
$this->makePdf(null,null,$html,$file) $pdf = $this->makePdf(null,null,$html, $file);
// $path = Storage::putFile($file, $pdf);
//store pdf //store pdf
//$path = Storage::putFile('public/' . $path, $this->file); //$path = Storage::putFile('public/' . $path, $this->file);
@ -57,31 +62,32 @@ class CreateInvoicePdf
} }
private function makePdf($header, $footer, $html, $pdf) : void private function makePdf($header, $footer, $html, $pdf)
{ {
Browsershot::html($html) return Browsershot::html($html)
//->showBrowserHeaderAndFooter() //->showBrowserHeaderAndFooter()
//->headerHtml($header) //->headerHtml($header)
//->footerHtml($footer) //->footerHtml($footer)
->waitUntilNetworkIdle() ->waitUntilNetworkIdle()
//->margins(10,10,10,10) //->margins(10,10,10,10)
->savePdf($pdf); ->savePdf('test.pdf');
} }
/** /**
* Generate the HTML invoice parsing variables * Generate the HTML invoice parsing variables
* and generating the final invoice HTML * 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 * @param Collection $invoice The invoice object
* @return string The invoice string in HTML format * @return string The invoice string in HTML format
*/ */
private function generateInvoiceHtml($template, $invoice) :string private function generateInvoiceHtml($design, $invoice) :string
{ {
//swap labels //swap labels
$data['invoice'] = $invoice;
$html = view(.$template, $invoice)->render(); return view($design, $data)->render();
return view('pdf.stub', $html); //return view('pdf.stub', $html)->render();
} }
} }

View File

@ -236,4 +236,15 @@ class Invoice extends BaseModel
break; 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';
}
} }

View File

@ -36,7 +36,7 @@ class InvoicePresenter extends EntityPresenter
return $this->client->present()->address(); return $this->client->present()->address();
} }
pubic function shipping_address() public function shipping_address()
{ {
return $this->client->present()->shipping_address(); return $this->client->present()->shipping_address();
} }

View File

@ -1,5 +1,6 @@
<?php <?php
use App\DataMapper\ClientSettings;
use App\DataMapper\DefaultSettings; use App\DataMapper\DefaultSettings;
use App\Models\Account; use App\Models\Account;
use App\Models\Client; use App\Models\Client;
@ -8,6 +9,7 @@ use App\Models\CompanyToken;
use App\Models\User; use App\Models\User;
use App\Models\UserAccount; use App\Models\UserAccount;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Log;
class RandomDataSeeder extends Seeder class RandomDataSeeder extends Seeder
{ {
@ -101,8 +103,7 @@ class RandomDataSeeder extends Seeder
/** Recurring Invoice Factory */ /** Recurring Invoice Factory */
factory(\App\Models\RecurringInvoice::class,20)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); factory(\App\Models\RecurringInvoice::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]); 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(); $clients = Client::all();

View File

@ -4,6 +4,7 @@ namespace Tests\Unit;
use App\DataMapper\ClientSettings; use App\DataMapper\ClientSettings;
use App\DataMapper\CompanySettings; use App\DataMapper\CompanySettings;
use Illuminate\Support\Facades\Log;
use Tests\TestCase; use Tests\TestCase;
/** /**
@ -35,15 +36,14 @@ class CompareObjectTest extends TestCase
$this->client_settings->{$key} = $this->company_settings->{$key}; $this->client_settings->{$key} = $this->company_settings->{$key};
} }
return $this->client_settings; return $this->client_settings;
} }
public function testProperties() 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->timezone_id, 15);
$this->assertEquals($build_client_settings->currency_id, 1); $this->assertEquals($build_client_settings->currency_id, 1);