From 0fd228811606018c371966fcdbf8a8373e8ac410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=BCneyt=20=C5=9Eent=C3=BCrk?= Date: Wed, 30 Jun 2021 10:51:52 +0300 Subject: [PATCH] Transaction event and notification changes.. --- .../TransactionPrinting.php | 2 +- .../TransactionSent.php | 2 +- app/Http/Controllers/Portal/Payments.php | 4 +- app/Http/Controllers/Purchases/Payments.php | 6 +- app/Http/Controllers/Sales/Revenues.php | 6 +- app/Notifications/Purchase/Payment.php | 117 ++++++++++++++++++ app/Notifications/Sale/Revenue.php | 2 +- app/Traits/Transactions.php | 20 +++ 8 files changed, 148 insertions(+), 11 deletions(-) rename app/Events/{Transaction => Banking}/TransactionPrinting.php (90%) rename app/Events/{Transaction => Banking}/TransactionSent.php (89%) create mode 100644 app/Notifications/Purchase/Payment.php diff --git a/app/Events/Transaction/TransactionPrinting.php b/app/Events/Banking/TransactionPrinting.php similarity index 90% rename from app/Events/Transaction/TransactionPrinting.php rename to app/Events/Banking/TransactionPrinting.php index 602e02f60..0fde309b7 100644 --- a/app/Events/Transaction/TransactionPrinting.php +++ b/app/Events/Banking/TransactionPrinting.php @@ -1,6 +1,6 @@ template_path, compact('revenue')); @@ -82,7 +82,7 @@ class Payments extends Controller */ public function pdfPayment(Transaction $payment, Request $request) { - event(new \App\Events\Transaction\TransactionPrinting($payment)); + event(new \App\Events\Banking\TransactionPrinting($payment)); $currency_style = true; diff --git a/app/Http/Controllers/Purchases/Payments.php b/app/Http/Controllers/Purchases/Payments.php index f03bc37e0..47b53f3b4 100644 --- a/app/Http/Controllers/Purchases/Payments.php +++ b/app/Http/Controllers/Purchases/Payments.php @@ -275,7 +275,7 @@ class Payments extends Controller // Notify the customer $payment->contact->notify(new Notification($payment, 'payment_new_customer', true)); - event(new \App\Events\Transaction\TransactionSent($payment)); + event(new \App\Events\Banking\TransactionSent($payment)); flash(trans('documents.messages.email_sent', ['type' => trans_choice('general.payments', 1)]))->success(); @@ -291,7 +291,7 @@ class Payments extends Controller */ public function printPayment(Transaction $payment) { - event(new \App\Events\Transaction\TransactionPrinting($payment)); + event(new \App\Events\Banking\TransactionPrinting($payment)); $view = view($payment->template_path, compact('payment')); @@ -307,7 +307,7 @@ class Payments extends Controller */ public function pdfPayment(Transaction $payment) { - event(new \App\Events\Transaction\TransactionPrinting($payment)); + event(new \App\Events\Banking\TransactionPrinting($payment)); $currency_style = true; diff --git a/app/Http/Controllers/Sales/Revenues.php b/app/Http/Controllers/Sales/Revenues.php index 3b8d01b8d..01e6062ee 100644 --- a/app/Http/Controllers/Sales/Revenues.php +++ b/app/Http/Controllers/Sales/Revenues.php @@ -276,7 +276,7 @@ class Revenues extends Controller // Notify the customer $revenue->contact->notify(new Notification($revenue, 'revenue_new_customer', true)); - event(new \App\Events\Transaction\TransactionSent($revenue)); + event(new \App\Events\Banking\TransactionSent($revenue)); flash(trans('documents.messages.email_sent', ['type' => trans_choice('general.revenues', 1)]))->success(); @@ -292,7 +292,7 @@ class Revenues extends Controller */ public function printRevenue(Transaction $revenue) { - event(new \App\Events\Transaction\TransactionPrinting($revenue)); + event(new \App\Events\Banking\TransactionPrinting($revenue)); $view = view($revenue->template_path, compact('revenue')); @@ -308,7 +308,7 @@ class Revenues extends Controller */ public function pdfRevenue(Transaction $revenue) { - event(new \App\Events\Transaction\TransactionPrinting($revenue)); + event(new \App\Events\Banking\TransactionPrinting($revenue)); $currency_style = true; diff --git a/app/Notifications/Purchase/Payment.php b/app/Notifications/Purchase/Payment.php new file mode 100644 index 000000000..20afda5d4 --- /dev/null +++ b/app/Notifications/Purchase/Payment.php @@ -0,0 +1,117 @@ +payment = $payment; + $this->template = EmailTemplate::alias($template_alias)->first(); + $this->attach_pdf = $attach_pdf; + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $notifiable + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail($notifiable) + { + $message = $this->initMessage(); + + // Attach the PDF file + if ($this->attach_pdf) { + $message->attach($this->storeTransactionPdfAndGetPath($this->payment), [ + 'mime' => 'application/pdf', + ]); + } + + return $message; + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + 'template_alias' => $this->template->alias, + 'payment_id' => $this->payment->id, + 'customer_name' => $this->payment->contact->name, + 'amount' => $this->payment->amount, + 'payment_date' => company_date($this->payment->paid_at), + ]; + } + + public function getTags() + { + return [ + '{payment_amount}', + '{payment_date}', + '{payment_admin_link}', + '{customer_name}', + '{company_name}', + '{company_email}', + '{company_tax_number}', + '{company_phone}', + '{company_address}', + ]; + } + + public function getTagsReplacement() + { + return [ + money($this->payment->amount, $this->payment->currency_code, true), + company_date($this->payment->paid_at), + route('payments.show', $this->payment->id), + $this->payment->contact->name, + $this->payment->company->name, + $this->payment->company->email, + $this->payment->company->tax_number, + $this->payment->company->phone, + nl2br(trim($this->payment->company->address)), + ]; + } +} diff --git a/app/Notifications/Sale/Revenue.php b/app/Notifications/Sale/Revenue.php index 764f5f048..c041cc16b 100644 --- a/app/Notifications/Sale/Revenue.php +++ b/app/Notifications/Sale/Revenue.php @@ -60,7 +60,7 @@ class Revenue extends Notification // Attach the PDF file if ($this->attach_pdf) { - $message->attach($this->storeDocumentPdfAndGetPath($this->revenue), [ + $message->attach($this->storeTransactionPdfAndGetPath($this->revenue), [ 'mime' => 'application/pdf', ]); } diff --git a/app/Traits/Transactions.php b/app/Traits/Transactions.php index 8b090871f..faef5cb18 100644 --- a/app/Traits/Transactions.php +++ b/app/Traits/Transactions.php @@ -88,4 +88,24 @@ trait Transactions return $key; } + + public function storeTransactionPdfAndGetPath($transaction) + { + event(new \App\Events\Banking\TransactionPrinting($transaction)); + + $view = view($transaction->template_path, ['revenue' => $transaction, 'transaction' => $transaction])->render(); + $html = mb_convert_encoding($view, 'HTML-ENTITIES', 'UTF-8'); + + $pdf = app('dompdf.wrapper'); + $pdf->loadHTML($html); + + $file_name = $this->getTransactionFileName($transaction); + + $pdf_path = storage_path('app/temp/' . $file_name); + + // Save the PDF file into temp folder + $pdf->save($pdf_path); + + return $pdf_path; + } }