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 $size_id;
public $design;
/**
* Cast object values and return entire class
* prevents missing properties from not being returned

View File

@ -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) [],
];

View File

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

View File

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

View File

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

View File

@ -1,5 +1,6 @@
<?php
use App\DataMapper\ClientSettings;
use App\DataMapper\DefaultSettings;
use App\Models\Account;
use App\Models\Client;
@ -8,6 +9,7 @@ use App\Models\CompanyToken;
use App\Models\User;
use App\Models\UserAccount;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Log;
class RandomDataSeeder extends Seeder
{
@ -101,8 +103,7 @@ class RandomDataSeeder extends Seeder
/** 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\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();

View File

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