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/Listeners/Update/V21/Version2119.php b/app/Listeners/Update/V21/Version2119.php new file mode 100644 index 000000000..22fe958d0 --- /dev/null +++ b/app/Listeners/Update/V21/Version2119.php @@ -0,0 +1,56 @@ +skipThisUpdate($event)) { + return; + } + + $this->updateEmailTemplate(); + + Artisan::call('cache:clear'); + } + + protected function updateEmailTemplate() + { + $company_id = company_id(); + + $companies = Company::cursor(); + + foreach ($companies as $company) { + $company->makeCurrent(); + + EmailTemplate::create([ + 'company_id' => $company->id, + 'alias' => 'payment_new_vendor', + 'class' => 'App\Notifications\Purchase\Payment', + 'name' => 'settings.email.templates.payment_new_vendor', + 'subject' => trans('email_templates.payment_new_vendor.subject'), + 'body' => trans('email_templates.payment_new_vendor.body'), + ]); + } + + company($company_id)->makeCurrent(); + } +} diff --git a/app/Notifications/Purchase/Payment.php b/app/Notifications/Purchase/Payment.php new file mode 100644 index 000000000..c325c97f6 --- /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, + 'vendor_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}', + '{vendor_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/Providers/Event.php b/app/Providers/Event.php index 772e7662e..8fc0a0a0c 100644 --- a/app/Providers/Event.php +++ b/app/Providers/Event.php @@ -35,6 +35,7 @@ class Event extends Provider 'App\Listeners\Update\V21\Version2116', 'App\Listeners\Update\V21\Version2117', 'App\Listeners\Update\V21\Version2118', + 'App\Listeners\Update\V21\Version2119', ], 'Illuminate\Auth\Events\Login' => [ 'App\Listeners\Auth\Login', 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; + } } diff --git a/database/seeds/EmailTemplates.php b/database/seeds/EmailTemplates.php index c9eccdebf..f2550fe12 100644 --- a/database/seeds/EmailTemplates.php +++ b/database/seeds/EmailTemplates.php @@ -77,6 +77,11 @@ class EmailTemplates extends Seeder 'class' => 'App\Notifications\Sale\Revenue', 'name' => 'settings.email.templates.revenue_new_customer', ], + [ + 'alias' => 'payment_new_vendor', + 'class' => 'App\Notifications\Purchase\Payment', + 'name' => 'settings.email.templates.payment_new_vendor', + ], ]; foreach ($templates as $template) { diff --git a/resources/lang/en-GB/email_templates.php b/resources/lang/en-GB/email_templates.php index 526c9c0d0..718d00657 100644 --- a/resources/lang/en-GB/email_templates.php +++ b/resources/lang/en-GB/email_templates.php @@ -51,4 +51,9 @@ return [ 'subject' => '{revenue_date} payment created', 'body' => 'Dear {customer_name},

We have prepared the following payment.

You can see the payment details from the following link: {revenue_date}.

Feel free to contact us with any questions..

Best Regards,
{company_name}', ], + + 'payment_new_vendor' => [ + 'subject' => '{revenue_date} payment created', + 'body' => 'Dear {vendor_name},

We have prepared the following payment.

You can see the payment details from the following link: {payment_date}.

Feel free to contact us with any questions..

Best Regards,
{company_name}', + ], ]; diff --git a/resources/views/purchases/payments/show.blade.php b/resources/views/purchases/payments/show.blade.php index 3a0dcb955..661712a3b 100644 --- a/resources/views/purchases/payments/show.blade.php +++ b/resources/views/purchases/payments/show.blade.php @@ -3,7 +3,7 @@ @section('title', trans('payments.payment_made')) @section('new_button') - + @endsection @section('content')