This commit is contained in:
denisdulici 2017-11-16 20:27:30 +03:00
parent 9243ed34e3
commit 79b3467bdd
4 changed files with 86 additions and 30 deletions

View File

@ -20,11 +20,13 @@ use App\Models\Item\Item;
use App\Models\Setting\Category; use App\Models\Setting\Category;
use App\Models\Setting\Currency; use App\Models\Setting\Currency;
use App\Models\Setting\Tax; use App\Models\Setting\Tax;
use App\Notifications\Income\Invoice as Notification;
use App\Traits\Currencies; use App\Traits\Currencies;
use App\Traits\DateTime; use App\Traits\DateTime;
use App\Traits\Uploads; use App\Traits\Uploads;
use App\Utilities\Modules; use App\Utilities\Modules;
use Date; use Date;
use File;
class Invoices extends Controller class Invoices extends Controller
{ {
@ -407,6 +409,48 @@ class Invoices extends Controller
return redirect()->back(); 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. * Print the invoice.
* *
@ -416,19 +460,7 @@ class Invoices extends Controller
*/ */
public function printInvoice(Invoice $invoice) public function printInvoice(Invoice $invoice)
{ {
$paid = 0; $invoice = $this->prepareInvoice($invoice);
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 view($invoice->template_path, compact('invoice')); return view($invoice->template_path, compact('invoice'));
} }
@ -442,25 +474,15 @@ class Invoices extends Controller
*/ */
public function pdfInvoice(Invoice $invoice) public function pdfInvoice(Invoice $invoice)
{ {
$paid = 0; $invoice = $this->prepareInvoice($invoice);
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));
$html = view($invoice->template_path, compact('invoice'))->render(); $html = view($invoice->template_path, compact('invoice'))->render();
$pdf = \App::make('dompdf.wrapper'); $pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($html); $pdf->loadHTML($html);
//$pdf->setPaper('A4', 'portrait');
$file_name = 'invoice_'.time().'.pdf'; $file_name = 'invoice_'.time().'.pdf';
return $pdf->download($file_name); return $pdf->download($file_name);
@ -591,6 +613,25 @@ class Invoices extends Controller
return redirect('incomes/invoices'); 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) protected function addTotals($invoice, $request, $taxes, $sub_total, $tax_total)
{ {
$sort_order = 1; $sort_order = 1;

View File

@ -46,9 +46,18 @@ class Invoice extends Notification implements ShouldQueue
*/ */
public function toMail($notifiable) public function toMail($notifiable)
{ {
return (new MailMessage) $message = (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.') ->line(trans('invoices.notification.message', ['amount' => money($this->invoice->amount, $this->invoice->currency_code, true), 'customer' => $this->invoice->customer->name]))
->action('Pay Now', url('customers/invoices', $this->invoice->id, true)); ->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;
} }
/** /**

View File

@ -37,7 +37,13 @@ return [
], ],
'messages' => [ 'messages' => [
'email_sent' => 'Invoice email has been sent successfully!',
'marked_sent' => 'Invoice marked as 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',
],
]; ];

View File

@ -161,7 +161,7 @@
@permission('update-incomes-invoices') @permission('update-incomes-invoices')
<li><a href="{{ url('incomes/invoices/' . $invoice->id . '/sent') }}">{{ trans('invoices.mark_sent') }}</a></li> <li><a href="{{ url('incomes/invoices/' . $invoice->id . '/sent') }}">{{ trans('invoices.mark_sent') }}</a></li>
@endpermission @endpermission
<li><a href="#" id="button-email">{{ trans('invoices.send_mail') }}</a></li> <li><a href="{{ url('incomes/invoices/' . $invoice->id . '/email') }}">{{ trans('invoices.send_mail') }}</a></li>
<li class="divider"></li> <li class="divider"></li>
<li><a href="{{ url('incomes/invoices/' . $invoice->id . '/pdf') }}">{{ trans('invoices.download_pdf') }}</a></li> <li><a href="{{ url('incomes/invoices/' . $invoice->id . '/pdf') }}">{{ trans('invoices.download_pdf') }}</a></li>
<li class="divider"></li> <li class="divider"></li>