diff --git a/app/Http/Controllers/Incomes/Invoices.php b/app/Http/Controllers/Incomes/Invoices.php index 2a7698fcc..6d4e6258f 100644 --- a/app/Http/Controllers/Incomes/Invoices.php +++ b/app/Http/Controllers/Incomes/Invoices.php @@ -20,11 +20,13 @@ use App\Models\Item\Item; use App\Models\Setting\Category; use App\Models\Setting\Currency; use App\Models\Setting\Tax; +use App\Notifications\Income\Invoice as Notification; use App\Traits\Currencies; use App\Traits\DateTime; use App\Traits\Uploads; use App\Utilities\Modules; use Date; +use File; class Invoices extends Controller { @@ -407,6 +409,48 @@ class Invoices extends Controller return redirect()->back(); } + /** + * Download the PDF file of invoice. + * + * @param Invoice $invoice + * + * @return Response + */ + public function emailInvoice(Invoice $invoice) + { + $invoice = $this->prepareInvoice($invoice); + + $html = view($invoice->template_path, compact('invoice'))->render(); + + $pdf = \App::make('dompdf.wrapper'); + $pdf->loadHTML($html); + + $file = storage_path('app/temp/invoice_'.time().'.pdf'); + + $invoice->pdf_path = $file; + + // Save the PDF file into temp folder + $pdf->save($file); + + // Notify the customer + $invoice->customer->notify(new Notification($invoice)); + + // Delete temp file + File::delete($file); + + unset($invoice->paid); + unset($invoice->template_path); + unset($invoice->pdf_path); + + // Mark invoice as sent + $invoice->invoice_status_code = 'sent'; + $invoice->save(); + + flash(trans('invoices.messages.email_sent'))->success(); + + return redirect()->back(); + } + /** * Print the invoice. * @@ -416,19 +460,7 @@ class Invoices extends Controller */ public function printInvoice(Invoice $invoice) { - $paid = 0; - - foreach ($invoice->payments as $item) { - $item->default_currency_code = $invoice->currency_code; - - $paid += $item->getDynamicConvertedAmount(); - } - - $invoice->paid = $paid; - - $invoice->template_path = 'incomes.invoices.invoice'; - - event(new InvoicePrinting($invoice)); + $invoice = $this->prepareInvoice($invoice); return view($invoice->template_path, compact('invoice')); } @@ -442,25 +474,15 @@ class Invoices extends Controller */ public function pdfInvoice(Invoice $invoice) { - $paid = 0; - - foreach ($invoice->payments as $item) { - $item->default_currency_code = $invoice->currency_code; - - $paid += $item->getDynamicConvertedAmount(); - } - - $invoice->paid = $paid; - - $invoice->template_path = 'incomes.invoices.invoice'; - - event(new InvoicePrinting($invoice)); + $invoice = $this->prepareInvoice($invoice); $html = view($invoice->template_path, compact('invoice'))->render(); $pdf = \App::make('dompdf.wrapper'); $pdf->loadHTML($html); + //$pdf->setPaper('A4', 'portrait'); + $file_name = 'invoice_'.time().'.pdf'; return $pdf->download($file_name); @@ -591,6 +613,25 @@ class Invoices extends Controller return redirect('incomes/invoices'); } + protected function prepareInvoice(Invoice $invoice) + { + $paid = 0; + + foreach ($invoice->payments as $item) { + $item->default_currency_code = $invoice->currency_code; + + $paid += $item->getDynamicConvertedAmount(); + } + + $invoice->paid = $paid; + + $invoice->template_path = 'incomes.invoices.invoice'; + + event(new InvoicePrinting($invoice)); + + return $invoice; + } + protected function addTotals($invoice, $request, $taxes, $sub_total, $tax_total) { $sort_order = 1; diff --git a/app/Notifications/Income/Invoice.php b/app/Notifications/Income/Invoice.php index 2ca68b092..354786e9a 100644 --- a/app/Notifications/Income/Invoice.php +++ b/app/Notifications/Income/Invoice.php @@ -46,9 +46,18 @@ class Invoice extends Notification implements ShouldQueue */ public function toMail($notifiable) { - return (new MailMessage) - ->line('You are receiving this email because you have an upcoming ' . money($this->invoice->amount, $this->invoice->currency_code, true) . ' invoice to ' . $this->invoice->customer->name . ' customer.') - ->action('Pay Now', url('customers/invoices', $this->invoice->id, true)); + $message = (new MailMessage) + ->line(trans('invoices.notification.message', ['amount' => money($this->invoice->amount, $this->invoice->currency_code, true), 'customer' => $this->invoice->customer->name])) + ->action(trans('invoices.notification.button'), url('customers/invoices', $this->invoice->id, true)); + + // Attach the PDF file if available + if (isset($this->invoice->pdf_path)) { + $message->attach($this->invoice->pdf_path, [ + 'mime' => 'application/pdf', + ]); + } + + return $message; } /** diff --git a/resources/lang/en-GB/invoices.php b/resources/lang/en-GB/invoices.php index b1f0f7a55..7f52a170f 100644 --- a/resources/lang/en-GB/invoices.php +++ b/resources/lang/en-GB/invoices.php @@ -37,7 +37,13 @@ return [ ], 'messages' => [ + 'email_sent' => 'Invoice email has been sent successfully!', 'marked_sent' => 'Invoice marked as sent successfully!', ], + 'notification' => [ + 'message' => 'You are receiving this email because you have an upcoming :amount invoice to :customer customer.', + 'button' => 'Pay Now', + ], + ]; diff --git a/resources/views/incomes/invoices/show.blade.php b/resources/views/incomes/invoices/show.blade.php index 8a07b1325..7d88541a0 100644 --- a/resources/views/incomes/invoices/show.blade.php +++ b/resources/views/incomes/invoices/show.blade.php @@ -161,7 +161,7 @@ @permission('update-incomes-invoices')
  • {{ trans('invoices.mark_sent') }}
  • @endpermission -
  • {{ trans('invoices.send_mail') }}
  • +
  • {{ trans('invoices.send_mail') }}
  • {{ trans('invoices.download_pdf') }}