diff --git a/app/Http/Controllers/Sales/Invoices.php b/app/Http/Controllers/Sales/Invoices.php index 52422b2dd..3c8f32695 100644 --- a/app/Http/Controllers/Sales/Invoices.php +++ b/app/Http/Controllers/Sales/Invoices.php @@ -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; - } } diff --git a/app/Listeners/Document/SendDocumentPaymentNotification.php b/app/Listeners/Document/SendDocumentPaymentNotification.php index ee378ed35..e61b80bbe 100644 --- a/app/Listeners/Document/SendDocumentPaymentNotification.php +++ b/app/Listeners/Document/SendDocumentPaymentNotification.php @@ -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 diff --git a/app/Models/Document/Document.php b/app/Models/Document/Document.php index f103427ec..8258ca68a 100644 --- a/app/Models/Document/Document.php +++ b/app/Models/Document/Document.php @@ -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(); diff --git a/app/Notifications/Portal/PaymentReceived.php b/app/Notifications/Portal/PaymentReceived.php index daa522502..084b1acd7 100644 --- a/app/Notifications/Portal/PaymentReceived.php +++ b/app/Notifications/Portal/PaymentReceived.php @@ -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', ]); } diff --git a/app/Notifications/Purchase/Bill.php b/app/Notifications/Purchase/Bill.php index a13317183..a80296447 100644 --- a/app/Notifications/Purchase/Bill.php +++ b/app/Notifications/Purchase/Bill.php @@ -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(); } /** diff --git a/app/Notifications/Sale/Invoice.php b/app/Notifications/Sale/Invoice.php index 7c30a3521..c297c1a10 100644 --- a/app/Notifications/Sale/Invoice.php +++ b/app/Notifications/Sale/Invoice.php @@ -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', ]); } diff --git a/app/Traits/Documents.php b/app/Traits/Documents.php index 8a88034a0..aabd4e17a 100644 --- a/app/Traits/Documents.php +++ b/app/Traits/Documents.php @@ -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; + } }