1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

- Pass $invoice to the getInvoiceDeliveryNote() in InvoiceService

- GenerateDeliveryNote action for InvoiceService
- New delivery-note-table element in the \Services\PdfMaker\Design.php
- $item & $description variables in the HtmlEngine
This commit is contained in:
Benjamin Beganović 2020-11-09 14:30:50 +01:00
parent a79447d4cf
commit 8adab67251
5 changed files with 166 additions and 8 deletions

View File

@ -841,10 +841,10 @@ class InvoiceController extends BaseController
*/
public function deliveryNote(ShowInvoiceRequest $request, Invoice $invoice)
{
$file_path = $invoice->service()->getInvoiceDeliveryNote($invoice, $invoice->invitations->first()->contact);
$file = base_path("storage/app/public/{$file_path}");
$file_path = $invoice->service()->getInvoiceDeliveryNote($invoice->invitations->first()->contact);
return response()->download($file_path, basename($file_path));
return response()->download($file, basename($file));
}
}

View File

@ -0,0 +1,95 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Services\Invoice;
use App\Models\ClientContact;
use App\Models\Design;
use App\Models\Invoice;
use App\Services\PdfMaker\Design as PdfMakerDesign;
use App\Services\PdfMaker\PdfMaker as PdfMakerService;
use App\Utils\HtmlEngine;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\Pdf\PdfMaker;
use Illuminate\Support\Facades\Storage;
class GenerateDeliveryNote
{
use MakesHash, PdfMaker;
/**
* @var \App\Models\Invoice
*/
private $invoice;
/**
* @var \App\Models\ClientContact
*/
private $contact;
/**
* @var mixed
*/
private $disk;
public function __construct(Invoice $invoice, ClientContact $contact = null, $disk = null)
{
$this->invoice = $invoice;
$this->contact = $contact;
$this->disk = $disk ?? config('filesystems.default');
}
public function run()
{
$design_id = $this->invoice->design_id
? $this->invoice->design_id
: $this->decodePrimaryKey($this->invoice->client->getSetting('invoice_design_id'));
$file_path = sprintf('%s%s_delivery_note.pdf', $this->invoice->client->invoice_filepath(), $this->invoice->number);
$design = Design::find($design_id);
$html = new HtmlEngine($this->invoice->invitations->first());
if ($design->is_custom) {
$options = ['custom_partials' => json_decode(json_encode($design->design), true)];
$template = new PdfMakerDesign(PdfMakerDesign::CUSTOM, $options);
} else {
$template = new PdfMakerDesign(strtolower($design->name));
}
$state = [
'template' => $template->elements([
'client' => $this->invoice->client,
'entity' => $this->invoice,
'pdf_variables' => (array) $this->invoice->company->settings->pdf_variables,
'contact' => $this->contact,
], 'delivery_note'),
'variables' => $html->generateLabelsAndValues(),
];
$maker = new PdfMakerService($state);
$maker
->design($template)
->build();
Storage::makeDirectory($this->invoice->client->invoice_filepath(), 0775);
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML());
Storage::disk($this->disk)->put($file_path, $pdf);
return $file_path;
}
}

View File

@ -143,9 +143,9 @@ class InvoiceService
return (new GetInvoicePdf($this->invoice, $contact))->run();
}
public function getInvoiceDeliveryNote($contact = null)
public function getInvoiceDeliveryNote(\App\Models\Invoice $invoice, \App\Models\ClientContact $contact = null)
{
//stubbed
return (new GenerateDeliveryNote($invoice, $contact))->run();
}
public function sendEmail($contact = null)

View File

@ -102,6 +102,10 @@ class Design extends BaseDesign
'id' => 'entity-details',
'elements' => $this->entityDetails(),
],
'delivery-note-table' => [
'id' => 'delivery-note-table',
'elements' => $this->deliveryNoteTable(),
],
'product-table' => [
'id' => 'product-table',
'elements' => $this->productTable(),
@ -151,10 +155,26 @@ class Design extends BaseDesign
public function clientDetails(): array
{
$variables = $this->context['pdf_variables']['client_details'];
$elements = [];
if ($this->type == 'delivery_note') {
$elements = [
['element' => 'p', 'content' => $this->entity->client->name, 'show_empty' => false],
['element' => 'p', 'content' => $this->entity->client->shipping_address1, 'show_empty' => false],
['element' => 'p', 'content' => $this->entity->client->shipping_address2, 'show_empty' => false],
['element' => 'p', 'content' => "{$this->entity->client->shipping_city} {$this->entity->client->shipping_state} {$this->entity->client->shipping_postal_code}", 'show_empty' => false],
['element' => 'p', 'content' => optional($this->entity->client->shipping_country)->name, 'show_empty' => false],
];
if (!is_null($this->context['contact'])) {
$elements[] = ['element' => 'p', 'content' => $this->context['contact']->email, 'show_empty' => false];
}
return $elements;
}
$variables = $this->context['pdf_variables']['client_details'];
foreach ($variables as $variable) {
$elements[] = ['element' => 'p', 'content' => $variable, 'show_empty' => false];
}
@ -192,6 +212,20 @@ class Design extends BaseDesign
return $elements;
}
public function deliveryNoteTable(): array
{
$elements = [
['element' => 'thead', 'elements' => [
['element' => 'th', 'content' => '$item_label'],
['element' => 'th', 'content' => '$description_label'],
['element' => 'th', 'content' => '$product.quantity_label'],
]],
['element' => 'tbody', 'elements' => $this->buildTableBody('delivery_note')],
];
return $elements;
}
/**
* Parent method for building products table.
*
@ -207,6 +241,10 @@ class Design extends BaseDesign
return [];
}
if ($this->type == 'delivery_note') {
return [];
}
return [
['element' => 'thead', 'elements' => $this->buildTableHeader('product')],
['element' => 'tbody', 'elements' => $this->buildTableBody('$product')],
@ -228,6 +266,10 @@ class Design extends BaseDesign
return [];
}
if ($this->type == 'delivery_note') {
return [];
}
return [
['element' => 'thead', 'elements' => $this->buildTableHeader('task')],
['element' => 'tbody', 'elements' => $this->buildTableBody('$task')],
@ -269,6 +311,20 @@ class Design extends BaseDesign
return [];
}
if ($type == 'delivery_note') {
foreach ($items as $row) {
$element = ['element' => 'tr', 'elements' => []];
$element['elements'][] = ['element' => 'td', 'content' => $row['delivery_note.product_key']];
$element['elements'][] = ['element' => 'td', 'content' => $row['delivery_note.notes']];
$element['elements'][] = ['element' => 'td', 'content' => $row['delivery_note.quantity']];
$elements[] = $element;
}
return $elements;
}
foreach ($items as $row) {
$element = ['element' => 'tr', 'elements' => []];
@ -331,6 +387,10 @@ class Design extends BaseDesign
public function tableTotals(): array
{
if ($this->type == 'delivery_note') {
return [];
}
$variables = $this->context['pdf_variables']['total_columns'];
$elements = [

View File

@ -333,6 +333,9 @@ class HtmlEngine
$data['$primary_color'] = ['value' => $this->settings->primary_color, 'label' => ''];
$data['$secondary_color'] = ['value' => $this->settings->secondary_color, 'label' => ''];
$data['$item'] = ['value' => '', 'label' => ctrans('texts.item')];
$data['$description'] = ['value' => '', 'label' => ctrans('texts.description')];
// $data['custom_label1'] = ['value' => '', 'label' => ctrans('texts.')];
// $data['custom_label2'] = ['value' => '', 'label' => ctrans('texts.')];
// $data['custom_label3'] = ['value' => '', 'label' => ctrans('texts.')];