Invoice templates

This commit is contained in:
David Bomba 2019-09-04 11:45:53 +10:00
parent e60bcf2d23
commit cd40ba95e6
7 changed files with 258 additions and 185 deletions

View File

@ -167,7 +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', 'design' => 'views/pdf/design1.blade.php',
'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\Blade;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Spatie\Browsershot\Browsershot; use Spatie\Browsershot\Browsershot;
@ -72,7 +73,7 @@ class CreateInvoicePdf implements ShouldQueue
//->showBrowserHeaderAndFooter() //->showBrowserHeaderAndFooter()
//->headerHtml($header) //->headerHtml($header)
//->footerHtml($footer) //->footerHtml($footer)
->waitUntilNetworkIdle()->pdf(); ->waitUntilNetworkIdle(false)->pdf();
//->margins(10,10,10,10) //->margins(10,10,10,10)
//->savePdf('test.pdf'); //->savePdf('test.pdf');
} }
@ -88,8 +89,57 @@ class CreateInvoicePdf implements ShouldQueue
*/ */
private function generateInvoiceHtml($design, $invoice) :string private function generateInvoiceHtml($design, $invoice) :string
{ {
return view($design, array_merge($invoice->makeLabels(), $invoice->makeValues()))->render(); $variables = array_merge($invoice->makeLabels(), $invoice->makeValues());
$design = str_replace(array_keys($variables), array_values($variables), $design);
//
$data['invoice'] = $invoice;
return $this->renderView($design, $data);
//return view($design, $data)->render();
} }
/**
* Parses the blade file string and processes the template variables
*
* @param string $string The Blade file string
* @param array $data The array of template variables
* @return string The return HTML string
*
*/
private function renderView($string, $data) :string
{
if (!$data) {
$data = [];
}
$data['__env'] = app(\Illuminate\View\Factory::class);
$php = Blade::compileString($string);
$obLevel = ob_get_level();
ob_start();
extract($data, EXTR_SKIP);
try {
eval('?' . '>' . $php);
} catch (\Exception $e) {
while (ob_get_level() > $obLevel) {
ob_end_clean();
}
throw $e;
} catch (\Throwable $e) {
while (ob_get_level() > $obLevel) {
ob_end_clean();
}
throw new \FatalThrowableError($e);
}
return ob_get_clean();
}
} }

View File

@ -20,7 +20,9 @@ use App\Utils\Traits\NumberFormatter;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Laracasts\Presenter\PresentableTrait; use Laracasts\Presenter\PresentableTrait;
class Invoice extends BaseModel class Invoice extends BaseModel
@ -242,11 +244,11 @@ class Invoice extends BaseModel
/** /**
* Returns the template for the invoice * Returns the template for the invoice
* *
* @return string Either the template view, OR the template HTML stirng * @return string Either the template view, OR the template HTML string
*/ */
public function design() :string public function design() :string
{ {
return $this->settings->design ?: 'pdf.design1'; return File::exists(resource_path($this->settings->design)) ? File::get(resource_path($this->settings->design)) : File::get(resource_path('views/pdf/design1.blade.php'));
} }
} }

View File

@ -26,6 +26,11 @@ class CompanyPresenter extends EntityPresenter
return $this->entity->name ?: ctrans('texts.untitled_account'); return $this->entity->name ?: ctrans('texts.untitled_account');
} }
public function logo()
{
return strlen($this->entity->logo > 0) ? $this->entity->logo : 'https://www.invoiceninja.com/wp-content/uploads/2019/01/InvoiceNinja-Logo-Round-300x300.png';
}
public function address() public function address()
{ {
$str = ''; $str = '';

View File

@ -39,89 +39,101 @@ trait MakesInvoiceValues
]; ];
private static $labels = [ private static $labels = [
'invoice', 'invoice_date',
'invoice_date', 'due_date',
'due_date', 'invoice_number',
'invoice_number', 'po_number',
'po_number', 'discount',
'discount', 'taxes',
'taxes', 'tax',
'tax', 'item',
'item', 'description',
'description', 'unit_cost',
'unit_cost', 'quantity',
'quantity', 'line_total',
'line_total', 'subtotal',
'subtotal', 'paid_to_date',
'paid_to_date', 'balance_due',
'balance_due', 'partial_due',
'partial_due', 'terms',
'terms', 'your_invoice',
'your_invoice', 'quote',
'quote', 'your_quote',
'your_quote', 'quote_date',
'quote_date', 'quote_number',
'quote_number', 'total',
'total', 'invoice_issued_to',
'invoice_issued_to', 'quote_issued_to',
'quote_issued_to', 'rate',
'rate', 'hours',
'hours', 'balance',
'balance', 'from',
'from', 'to',
'to', 'invoice_to',
'invoice_to', 'quote_to',
'quote_to', 'details',
'details', 'invoice_no',
'invoice_no', 'quote_no',
'quote_no', 'valid_until',
'valid_until', 'client_name',
'client_name', 'address1',
'address1', 'address2',
'address2', 'id_number',
'id_number', 'vat_number',
'vat_number', 'city_state_postal',
'city_state_postal', 'postal_city_state',
'postal_city_state', 'country',
'country', 'email',
'email', 'contact_name',
'contact_name', 'company_name',
'company_name', 'website',
'website', 'phone',
'phone', 'blank',
'blank', 'surcharge',
'surcharge', 'tax_invoice',
'tax_invoice', 'tax_quote',
'tax_quote', 'statement',
'statement', 'statement_date',
'statement_date', 'your_statement',
'your_statement', 'statement_issued_to',
'statement_issued_to', 'statement_to',
'statement_to', 'credit_note',
'credit_note', 'credit_date',
'credit_date', 'credit_number',
'credit_number', 'credit_issued_to',
'credit_issued_to', 'credit_to',
'credit_to', 'your_credit',
'your_credit', 'work_phone',
'work_phone', 'invoice_total',
'invoice_total', 'outstanding',
'outstanding', 'invoice_due_date',
'invoice_due_date', 'quote_due_date',
'quote_due_date', 'service',
'service', 'product_key',
'product_key', 'unit_cost',
'unit_cost', 'custom_value1',
'custom_value1', 'custom_value2',
'custom_value2', 'delivery_note',
'delivery_note', 'date',
'date', 'method',
'method', 'payment_date',
'payment_date', 'reference',
'reference', 'amount',
'amount', 'amount_paid',
'amount_paid', ];
];
public function makeInvoiceTemplateKeys()
{
$data = [];
foreach(self::$labels as $label)
$data[] = '$'.$label.'_label';
foreach(self::$labels as $label)
$data[] = '$'.$label;
return $data;
}
/** /**
* Iterates and translates all labels * Iterates and translates all labels
@ -133,7 +145,7 @@ trait MakesInvoiceValues
$data = []; $data = [];
foreach(self::$labels as $label) foreach(self::$labels as $label)
$data[$label . '_label'] = ctrans('texts.'.$label); $data['$'.$label . '_label'] = ctrans('texts.'.$label);
return $data; return $data;
} }
@ -147,89 +159,90 @@ trait MakesInvoiceValues
{ {
$data = []; $data = [];
$data['invoice'] = $this; $data['$invoice_date'] = $this->invoice_date;
$data['invoice_date'] = $this->invoice_date; $data['$due_date'] = $this->due_date;
$data['due_date'] = $this->due_date; $data['$invoice_number'] = $this->invoice_number;
$data['invoice_number'] = $this->invoice_number; $data['$po_number'] = $this->po_number;
$data['po_number'] = $this->po_number; // $data['$discount'] = ;
// $data['discount'] = ; // $data['$taxes'] = ;
// $data['taxes'] = ; // $data['$tax'] = ;
// $data['tax'] = ; // $data['$item'] = ;
// $data['item'] = ; // $data['$description'] = ;
// $data['description'] = ; // $data['$unit_cost'] = ;
// $data['unit_cost'] = ; // $data['$quantity'] = ;
// $data['quantity'] = ; // $data['$line_total'] = ;
// $data['line_total'] = ; // $data['$subtotal'] = ;
// $data['subtotal'] = ; // $data['$paid_to_date'] = ;
// $data['paid_to_date'] = ; $data['$balance_due'] = Number::formatMoney($this->balance, $this->client->currency(), $this->client->country, $this->client->settings);
$data['balance_due'] = Number::formatMoney($this->balance, $this->client->currency(), $this->client->country, $this->client->settings); $data['$partial_due'] = Number::formatMoney($this->partial, $this->client->currency(), $this->client->country, $this->client->settings);
$data['partial_due'] = Number::formatMoney($this->partial, $this->client->currency(), $this->client->country, $this->client->settings); $data['$terms'] = $this->terms;
$data['terms'] = $this->terms; // $data['$your_invoice'] = ;
// $data['your_invoice'] = ; // $data['$quote'] = ;
// $data['quote'] = ; // $data['$your_quote'] = ;
// $data['your_quote'] = ; // $data['$quote_date'] = ;
// $data['quote_date'] = ; // $data['$quote_number'] = ;
// $data['quote_number'] = ; $data['$total'] = Number::formatMoney($this->amount, $this->client->currency(), $this->client->country, $this->client->settings);
$data['total'] = Number::formatMoney($this->amount, $this->client->currency(), $this->client->country, $this->client->settings); // $data['$invoice_issued_to'] = ;
// $data['invoice_issued_to'] = ; // $data['$quote_issued_to'] = ;
// $data['quote_issued_to'] = ; // $data['$rate'] = ;
// $data['rate'] = ; // $data['$hours'] = ;
// $data['hours'] = ; // $data['$balance'] = ;
// $data['balance'] = ; // $data['$from'] = ;
// $data['from'] = ; // $data['$to'] = ;
// $data['to'] = ; // $data['$invoice_to'] = ;
// $data['invoice_to'] = ; // $data['$quote_to'] = ;
// $data['quote_to'] = ; // $data['$details'] = ;
// $data['details'] = ; $data['$invoice_no'] = $this->invoice_number;
$data['invoice_no'] = $this->invoice_number; // $data['$quote_no'] = ;
// $data['quote_no'] = ; // $data['$valid_until'] = ;
// $data['valid_until'] = ; $data['$client_name'] = $this->present()->clientName();
$data['client_name'] = $this->present()->clientName(); $data['$address1'] = $this->client->address1;
$data['address1'] = $this->client->address1; $data['$address2'] = $this->client->address2;
$data['address2'] = $this->client->address2; $data['$id_number'] = $this->client->id_number;
$data['id_number'] = $this->client->id_number; $data['$vat_number'] = $this->client->vat_number;
$data['vat_number'] = $this->client->vat_number; $data['$city_state_postal'] = $this->present()->cityStateZip($this->client->city, $this->client->state, $this->client->postal_code, FALSE);
$data['city_state_postal'] = $this->present()->cityStateZip($this->client->city, $this->client->state, $this->client->postal_code, FALSE); $data['$postal_city_state'] = $this->present()->cityStateZip($this->client->city, $this->client->state, $this->client->postal_code, TRUE);
$data['postal_city_state'] = $this->present()->cityStateZip($this->client->city, $this->client->state, $this->client->postal_code, TRUE); $data['$country'] = $this->client->country->name;
$data['country'] = $this->client->country->name; $data['$email'] = isset($this->client->primary_contact()->first()->email) ?: 'no primary contact set';
$data['email'] = isset($this->client->primary_contact()->first()->email) ?: 'no primary contact set'; $data['$contact_name'] = $this->client->present()->primary_contact_name();
$data['contact_name'] = $this->client->present()->primary_contact_name(); $data['$company_name'] = $this->company->present()->name();
$data['company_name'] = $this->company->name; $data['$company_address'] = $this->company->present()->address();
$data['website'] = $this->client->present()->website(); $data['$company_logo'] = $this->company->present()->logo();
$data['phone'] = $this->client->present()->phone(); $data['$website'] = $this->client->present()->website();
//$data['blank'] = ; $data['$phone'] = $this->client->present()->phone();
//$data['surcharge'] = ; //$data['$blank'] = ;
//$data['$surcharge'] = ;
/* /*
$data['tax_invoice'] = $data['$tax_invoice'] =
$data['tax_quote'] = $data['$tax_quote'] =
$data['statement'] = ; $data['$statement'] = ;
$data['statement_date'] = ; $data['$statement_date'] = ;
$data['your_statement'] = ; $data['$your_statement'] = ;
$data['statement_issued_to'] = ; $data['$statement_issued_to'] = ;
$data['statement_to'] = ; $data['$statement_to'] = ;
$data['credit_note'] = ; $data['$credit_note'] = ;
$data['credit_date'] = ; $data['$credit_date'] = ;
$data['credit_number'] = ; $data['$credit_number'] = ;
$data['credit_issued_to'] = ; $data['$credit_issued_to'] = ;
$data['credit_to'] = ; $data['$credit_to'] = ;
$data['your_credit'] = ; $data['$your_credit'] = ;
$data['work_phone'] = ; $data['$work_phone'] = ;
$data['invoice_total'] = ; $data['$invoice_total'] = ;
$data['outstanding'] = ; $data['$outstanding'] = ;
$data['invoice_due_date'] = ; $data['$invoice_due_date'] = ;
$data['quote_due_date'] = ; $data['$quote_due_date'] = ;
$data['service'] = ; $data['$service'] = ;
$data['product_key'] = ; $data['$product_key'] = ;
$data['unit_cost'] = ; $data['$unit_cost'] = ;
$data['custom_value1'] = ; $data['$custom_value1'] = ;
$data['custom_value2'] = ; $data['$custom_value2'] = ;
$data['delivery_note'] = ; $data['$delivery_note'] = ;
$data['date'] = ; $data['$date'] = ;
$data['method'] = ; $data['$method'] = ;
$data['payment_date'] = ; $data['$payment_date'] = ;
$data['reference'] = ; $data['$reference'] = ;
$data['amount'] = ; $data['$amount'] = ;
$data['amount_paid'] =; $data['$amount_paid'] =;
*/ */
return $data; return $data;

View File

@ -18,5 +18,6 @@ $factory->define(App\Models\Company::class, function (Faker $faker) {
'country_id' => 4, 'country_id' => 4,
'work_phone' => $faker->phoneNumber, 'work_phone' => $faker->phoneNumber,
'work_email' => $faker->safeEmail, 'work_email' => $faker->safeEmail,
'logo' => 'https://www.invoiceninja.com/wp-content/themes/invoice-ninja/images/logo.png',
]; ];
}); });

View File

@ -111,13 +111,14 @@
<table> <table>
<tr> <tr>
<td class="title"> <td class="title">
<img src="https://www.sparksuite.com/images/logo.png" style="width:100%; max-width:300px;"> <img src="$company_logo" style="width:100%; max-width:150px;">
</td> </td>
<td> <td>
{{$invoice_number_label}}: {{ $invoice->invoice_number }}<br> $invoice_number_label: $invoice_number <br>
{{$invoice_date_label}}: {{ $invoice->invoice_date }}<br> $invoice_date_label: $invoice_date <br>
{{$invoice_due_date_label}}: {{ $invoice->due_date }} $invoice_due_date_label: $due_date
</td> </td>
</tr> </tr>
</table> </table>
@ -129,19 +130,20 @@
<table> <table>
<tr> <tr>
<td> <td>
{{$client_name}}<br> $client_name<br>
{{$address1}}<br> $address1<br>
{{$address2}}<br> $address2<br>
{{$city_state_postal}}<br> $city_state_postal<br>
{{$country}}<br> $country<br>
{{$vat_number}}<br> $vat_number<br>
</td> </td>
<td> <td>
{{$company_name}}<br> $company_name<br>
{{$phone}}<br> $company_address<br>
{{$email}}<br> $phone<br>
$email<br>
</td> </td>
</tr> </tr>