fixed invoice pdf attachment
This commit is contained in:
parent
2e695865bc
commit
5079a0bb30
@ -14,7 +14,6 @@ use App\Jobs\Document\UpdateDocument;
|
||||
use App\Models\Document\Document;
|
||||
use App\Notifications\Sale\Invoice as Notification;
|
||||
use App\Traits\Documents;
|
||||
use File;
|
||||
|
||||
class Invoices extends Controller
|
||||
{
|
||||
@ -268,33 +267,8 @@ class Invoices extends Controller
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
$invoice = $this->prepareInvoice($invoice);
|
||||
|
||||
$view = view($invoice->template_path, compact('invoice'))->render();
|
||||
$html = mb_convert_encoding($view, 'HTML-ENTITIES');
|
||||
|
||||
$pdf = app('dompdf.wrapper');
|
||||
$pdf->loadHTML($html);
|
||||
|
||||
$file_name = $this->getDocumentFileName($invoice);
|
||||
|
||||
$file = storage_path('app/temp/' . $file_name);
|
||||
|
||||
$invoice->pdf_path = $file;
|
||||
|
||||
// Save the PDF file into temp folder
|
||||
$pdf->save($file);
|
||||
|
||||
// Notify the customer
|
||||
$invoice->contact->notify(new Notification($invoice, 'invoice_new_customer'));
|
||||
|
||||
// Delete temp file
|
||||
File::delete($file);
|
||||
|
||||
unset($invoice->paid);
|
||||
unset($invoice->template_path);
|
||||
unset($invoice->pdf_path);
|
||||
unset($invoice->reconciled);
|
||||
$invoice->contact->notify(new Notification($invoice, 'invoice_new_customer', true));
|
||||
|
||||
event(new \App\Events\Document\DocumentSent($invoice));
|
||||
|
||||
@ -312,7 +286,7 @@ class Invoices extends Controller
|
||||
*/
|
||||
public function printInvoice(Document $invoice)
|
||||
{
|
||||
$invoice = $this->prepareInvoice($invoice);
|
||||
event(new \App\Events\Document\DocumentPrinting($invoice));
|
||||
|
||||
$view = view($invoice->template_path, compact('invoice'));
|
||||
|
||||
@ -328,7 +302,7 @@ class Invoices extends Controller
|
||||
*/
|
||||
public function pdfInvoice(Document $invoice)
|
||||
{
|
||||
$invoice = $this->prepareInvoice($invoice);
|
||||
event(new \App\Events\Document\DocumentPrinting($invoice));
|
||||
|
||||
$currency_style = true;
|
||||
|
||||
@ -368,29 +342,4 @@ class Invoices extends Controller
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
protected function prepareInvoice(Document $invoice)
|
||||
{
|
||||
$paid = 0;
|
||||
|
||||
foreach ($invoice->transactions as $item) {
|
||||
$amount = $item->amount;
|
||||
|
||||
if ($invoice->currency_code != $item->currency_code) {
|
||||
$item->default_currency_code = $invoice->currency_code;
|
||||
|
||||
$amount = $item->getAmountConvertedFromDefault();
|
||||
}
|
||||
|
||||
$paid += $amount;
|
||||
}
|
||||
|
||||
$invoice->paid = $paid;
|
||||
|
||||
$invoice->template_path = 'sales.invoices.print_' . setting('invoice.template');
|
||||
|
||||
event(new \App\Events\Document\DocumentPrinting($invoice));
|
||||
|
||||
return $invoice;
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ class SendDocumentPaymentNotification
|
||||
|
||||
// Notify the customer
|
||||
if ($document->contact && !empty($document->contact_email)) {
|
||||
$document->contact->notify(new Notification($document, $transaction, "{$document->type}_payment_customer"));
|
||||
$document->contact->notify(new Notification($document, $transaction, "{$document->type}_payment_customer"), true);
|
||||
}
|
||||
|
||||
// Notify all users assigned to this company
|
||||
|
@ -363,6 +363,11 @@ class Document extends Model
|
||||
return $amount;
|
||||
}
|
||||
|
||||
public function getTemplatePathAttribute($value = null)
|
||||
{
|
||||
return $value ?: 'sales.invoices.print_' . setting('invoice.template');
|
||||
}
|
||||
|
||||
protected static function newFactory(): Factory
|
||||
{
|
||||
return DocumentFactory::new();
|
||||
|
@ -4,10 +4,13 @@ namespace App\Notifications\Portal;
|
||||
|
||||
use App\Abstracts\Notification;
|
||||
use App\Models\Common\EmailTemplate;
|
||||
use App\Traits\Documents;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
|
||||
class PaymentReceived extends Notification
|
||||
{
|
||||
use Documents;
|
||||
|
||||
/**
|
||||
* The bill model.
|
||||
*
|
||||
@ -29,18 +32,29 @@ class PaymentReceived extends Notification
|
||||
*/
|
||||
public $template;
|
||||
|
||||
/**
|
||||
* Should attach pdf or not.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $attach_pdf;
|
||||
|
||||
/**
|
||||
* Create a notification instance.
|
||||
*
|
||||
* @param object $invoice
|
||||
* @param object $transaction
|
||||
* @param object $template_alias
|
||||
* @param object $attach_pdf
|
||||
*/
|
||||
public function __construct($invoice = null, $transaction = null, $template = null)
|
||||
public function __construct($invoice = null, $transaction = null, $template_alias = null, $attach_pdf = false)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->invoice = $invoice;
|
||||
$this->transaction = $transaction;
|
||||
$this->template = EmailTemplate::alias($template)->first();
|
||||
$this->template = EmailTemplate::alias($template_alias)->first();
|
||||
$this->attach_pdf = $attach_pdf;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -53,9 +67,9 @@ class PaymentReceived extends Notification
|
||||
{
|
||||
$message = $this->initMessage();
|
||||
|
||||
// Attach the PDF file if available
|
||||
if (isset($this->invoice->pdf_path)) {
|
||||
$message->attach($this->invoice->pdf_path, [
|
||||
// Attach the PDF file
|
||||
if ($this->attach_pdf) {
|
||||
$message->attach($this->storeInvoicePdfAndGetPath($this->invoice), [
|
||||
'mime' => 'application/pdf',
|
||||
]);
|
||||
}
|
||||
|
@ -25,14 +25,14 @@ class Bill extends Notification
|
||||
* Create a notification instance.
|
||||
*
|
||||
* @param object $bill
|
||||
* @param object $template
|
||||
* @param object $template_alias
|
||||
*/
|
||||
public function __construct($bill = null, $template = null)
|
||||
public function __construct($bill = null, $template_alias = null)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->bill = $bill;
|
||||
$this->template = EmailTemplate::alias($template)->first();
|
||||
$this->template = EmailTemplate::alias($template_alias)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -4,10 +4,13 @@ namespace App\Notifications\Sale;
|
||||
|
||||
use App\Abstracts\Notification;
|
||||
use App\Models\Common\EmailTemplate;
|
||||
use App\Traits\Documents;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
|
||||
class Invoice extends Notification
|
||||
{
|
||||
use Documents;
|
||||
|
||||
/**
|
||||
* The invoice model.
|
||||
*
|
||||
@ -22,18 +25,27 @@ class Invoice extends Notification
|
||||
*/
|
||||
public $template;
|
||||
|
||||
/**
|
||||
* Should attach pdf or not.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $attach_pdf;
|
||||
|
||||
/**
|
||||
* Create a notification instance.
|
||||
*
|
||||
* @param object $invoice
|
||||
* @param object $template
|
||||
* @param object $template_alias
|
||||
* @param object $attach_pdf
|
||||
*/
|
||||
public function __construct($invoice = null, $template = null)
|
||||
public function __construct($invoice = null, $template_alias = null, $attach_pdf = false)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->invoice = $invoice;
|
||||
$this->template = EmailTemplate::alias($template)->first();
|
||||
$this->template = EmailTemplate::alias($template_alias)->first();
|
||||
$this->attach_pdf = $attach_pdf;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -46,9 +58,9 @@ class Invoice extends Notification
|
||||
{
|
||||
$message = $this->initMessage();
|
||||
|
||||
// Attach the PDF file if available
|
||||
if (isset($this->invoice->pdf_path)) {
|
||||
$message->attach($this->invoice->pdf_path, [
|
||||
// Attach the PDF file
|
||||
if ($this->attach_pdf) {
|
||||
$message->attach($this->storeInvoicePdfAndGetPath($this->invoice), [
|
||||
'mime' => 'application/pdf',
|
||||
]);
|
||||
}
|
||||
|
@ -123,4 +123,24 @@ trait Documents
|
||||
|
||||
return $key;
|
||||
}
|
||||
|
||||
public function storeInvoicePdfAndGetPath($invoice)
|
||||
{
|
||||
event(new \App\Events\Document\DocumentPrinting($invoice));
|
||||
|
||||
$view = view($invoice->template_path, ['invoice' => $invoice])->render();
|
||||
$html = mb_convert_encoding($view, 'HTML-ENTITIES', 'UTF-8');
|
||||
|
||||
$pdf = app('dompdf.wrapper');
|
||||
$pdf->loadHTML($html);
|
||||
|
||||
$file_name = $this->getDocumentFileName($invoice);
|
||||
|
||||
$pdf_path = storage_path('app/temp/' . $file_name);
|
||||
|
||||
// Save the PDF file into temp folder
|
||||
$pdf->save($pdf_path);
|
||||
|
||||
return $pdf_path;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user