diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index bfecb4de8b..bf6e5c97ec 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -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)); } } diff --git a/app/Services/Invoice/GenerateDeliveryNote.php b/app/Services/Invoice/GenerateDeliveryNote.php new file mode 100644 index 0000000000..74170974a5 --- /dev/null +++ b/app/Services/Invoice/GenerateDeliveryNote.php @@ -0,0 +1,95 @@ +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; + } +} diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 5171752aea..820da96133 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -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) diff --git a/app/Services/PdfMaker/Design.php b/app/Services/PdfMaker/Design.php index 42dde49f3a..beffc99de9 100644 --- a/app/Services/PdfMaker/Design.php +++ b/app/Services/PdfMaker/Design.php @@ -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 = [ diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index 9eae2077ff..e669dca1d4 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -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.')];