From 15ac33cb4666ed5ca08496cb1dce628e3e7b4d39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Wed, 21 Apr 2021 00:54:14 +0300 Subject: [PATCH] make document notification agnostic #1991 --- app/Console/Commands/RecurringCheck.php | 6 +- app/Events/Document/DocumentRecurring.php | 6 +- .../SendDocumentRecurringNotification.php | 16 ++- config/type.php | 118 ++++++++++-------- 4 files changed, 81 insertions(+), 65 deletions(-) diff --git a/app/Console/Commands/RecurringCheck.php b/app/Console/Commands/RecurringCheck.php index e19e44d79..7e00f4855 100644 --- a/app/Console/Commands/RecurringCheck.php +++ b/app/Console/Commands/RecurringCheck.php @@ -133,11 +133,7 @@ class RecurringCheck extends Command case 'App\Models\Document\Document': event(new DocumentCreated($clone, request())); - if ($clone->type === Document::INVOICE_TYPE) { - event(new DocumentRecurring($clone, InvoiceNotification::class)); - } elseif ($clone->type === Document::BILL_TYPE) { - event(new DocumentRecurring($clone, BillNotification::class)); - } + event(new DocumentRecurring($clone)); break; case 'App\Models\Banking\Transaction': diff --git a/app/Events/Document/DocumentRecurring.php b/app/Events/Document/DocumentRecurring.php index 4fb5edf83..72fa68a7f 100644 --- a/app/Events/Document/DocumentRecurring.php +++ b/app/Events/Document/DocumentRecurring.php @@ -8,14 +8,14 @@ use App\Models\Document\Document; class DocumentRecurring extends Event { public $document; - public $notification; /** * Create a new event instance. + * + * @param $document */ - public function __construct(Document $document, string $notification) + public function __construct(Document $document) { $this->document = $document; - $this->notification = $notification; } } diff --git a/app/Listeners/Document/SendDocumentRecurringNotification.php b/app/Listeners/Document/SendDocumentRecurringNotification.php index 885e40686..5b13e2dac 100644 --- a/app/Listeners/Document/SendDocumentRecurringNotification.php +++ b/app/Listeners/Document/SendDocumentRecurringNotification.php @@ -3,7 +3,6 @@ namespace App\Listeners\Document; use App\Events\Document\DocumentRecurring as Event; -use App\Models\Document\Document; class SendDocumentRecurringNotification { @@ -16,13 +15,24 @@ class SendDocumentRecurringNotification public function handle(Event $event) { $document = $event->document; - $notification = $event->notification; + $config = config('type.' . $document->type . '.notification'); + + if (empty($config) || empty($config['class'])) { + return; + } + + $notification = $config['class']; // Notify the customer - if ($document->type === Document::INVOICE_TYPE && $document->contact && !empty($document->contact_email)) { + if ($config['notify_contact'] && $document->contact && !empty($document->contact_email)) { $document->contact->notify(new $notification($document, "{$document->type}_recur_customer")); } + // Check if should notify users + if (!$config['notify_user']) { + return; + } + // Notify all users assigned to this company foreach ($document->company->users as $user) { if (!$user->can('read-notifications')) { diff --git a/config/type.php b/config/type.php index c552cd587..d9648f2a7 100644 --- a/config/type.php +++ b/config/type.php @@ -6,119 +6,129 @@ return [ // Documents Document::INVOICE_TYPE => [ - 'alias' => '', // core empty but module write own alias - 'group' => 'sales', // controller folder name for permission and route + 'alias' => '', // core empty but module write own alias + 'group' => 'sales', // controller folder name for permission and route 'route' => [ - 'prefix' => 'invoices', // core use with group + prefix, module ex. estimates - 'parameter' => 'invoice', // sales/invoices/{parameter}/edit - //'create' => 'invoices.create', // if you change route, you can write full path + 'prefix' => 'invoices', // core use with group + prefix, module ex. estimates + 'parameter' => 'invoice', // sales/invoices/{parameter}/edit + //'create' => 'invoices.create', // if you change route, you can write full path ], 'permission' => [ - 'prefix' => 'invoices', // this controller file name. - //'create' => 'create-sales-invoices', // if you change action permission key, you can write full permission + 'prefix' => 'invoices', // this controller file name. + //'create' => 'create-sales-invoices', // if you change action permission key, you can write full permission ], 'translation' => [ - 'prefix' => 'invoices', // this translation file name. - 'add_contact' => 'general.customers', // - 'issued_at' => 'invoices.invoice_date', - 'due_at' => 'invoices.due_date', + 'prefix' => 'invoices', // this translation file name. + 'add_contact' => 'general.customers', // + 'issued_at' => 'invoices.invoice_date', + 'due_at' => 'invoices.due_date', ], 'setting' => [ - 'prefix' => 'invoice', + 'prefix' => 'invoice', + ], + 'category_type' => 'income', + 'transaction_type' => 'income', + 'contact_type' => 'customer', // use contact type + 'hide' => [], // for document items + 'class' => [], + 'notification' => [ + 'class' => 'App\Notifications\Sale\Invoice', + 'notify_contact' => true, + 'notify_user' => true, ], - 'category_type' => 'income', - 'transaction_type' => 'income', - 'contact_type' => 'customer', // use contact type - 'hide' => [], // for document items - 'class' => [], ], Document::BILL_TYPE => [ - 'alias' => '', - 'group' => 'purchases', + 'alias' => '', + 'group' => 'purchases', 'route' => [ - 'prefix' => 'bills', - 'parameter' => 'bill', - //'create' => 'bilss.create', + 'prefix' => 'bills', + 'parameter' => 'bill', + //'create' => 'bilss.create', ], 'permission' => [ - 'prefix' => 'bills', - //'create' => 'create-purchases-bills', + 'prefix' => 'bills', + //'create' => 'create-purchases-bills', ], 'translation' => [ - 'prefix' => 'bills', - 'issued_at' => 'bills.bill_date', - 'due_at' => 'bills.due_date', + 'prefix' => 'bills', + 'issued_at' => 'bills.bill_date', + 'due_at' => 'bills.due_date', ], 'setting' => [ - 'prefix' => 'bill', + 'prefix' => 'bill', + ], + 'category_type' => 'expense', + 'transaction_type' => 'expense', + 'contact_type' => 'vendor', + 'hide' => [], + 'notification' => [ + 'class' => 'App\Notifications\Purchase\Bill', + 'notify_contact' => false, + 'notify_user' => true, ], - 'category_type' => 'expense', - 'transaction_type' => 'expense', - 'contact_type' => 'vendor', - 'hide' => [], ], // Contacts 'customer' => [ - 'group' => 'sales', + 'group' => 'sales', 'permission' => [ - 'prefix' => 'customers', - //'create' => 'create-sales-customers', + 'prefix' => 'customers', + //'create' => 'create-sales-customers', ], ], 'vendor' => [ - 'group' => 'purchases', + 'group' => 'purchases', 'permission' => [ - 'prefix' => 'vendors', - //'create' => 'create-purchases-vendors', + 'prefix' => 'vendors', + //'create' => 'create-purchases-vendors', ], ], // Transactions 'income' => [ - 'group' => 'sales', + 'group' => 'sales', 'permission' => [ - 'prefix' => 'revenues', - //'create' => 'create-sales-revenues', + 'prefix' => 'revenues', + //'create' => 'create-sales-revenues', ], - 'contact_type' => 'customer', + 'contact_type' => 'customer', ], 'expense' => [ - 'group' => 'purchases', + 'group' => 'purchases', 'permission' => [ - 'prefix' => 'payments', - //'create' => 'create-purchases-payments', + 'prefix' => 'payments', + //'create' => 'create-purchases-payments', ], - 'contact_type' => 'vendor', + 'contact_type' => 'vendor', ], // Categories 'category' => [ 'income' => [ - 'alias' => '', + 'alias' => '', 'translation' => [ - 'prefix' => 'general', + 'prefix' => 'general', ], ], 'expense' => [ - 'alias' => '', + 'alias' => '', 'translation' => [ - 'prefix' => 'general', + 'prefix' => 'general', ], ], 'item' => [ - 'alias' => '', + 'alias' => '', 'translation' => [ - 'prefix' => 'general', + 'prefix' => 'general', ], ], 'other' => [ - 'alias' => '', + 'alias' => '', 'translation' => [ - 'prefix' => 'general', + 'prefix' => 'general', ], ], ],