make document notification agnostic #1991

This commit is contained in:
Denis Duliçi 2021-04-21 00:54:14 +03:00
parent 8f2f360ad6
commit 15ac33cb46
4 changed files with 81 additions and 65 deletions

View File

@ -133,11 +133,7 @@ class RecurringCheck extends Command
case 'App\Models\Document\Document': case 'App\Models\Document\Document':
event(new DocumentCreated($clone, request())); event(new DocumentCreated($clone, request()));
if ($clone->type === Document::INVOICE_TYPE) { event(new DocumentRecurring($clone));
event(new DocumentRecurring($clone, InvoiceNotification::class));
} elseif ($clone->type === Document::BILL_TYPE) {
event(new DocumentRecurring($clone, BillNotification::class));
}
break; break;
case 'App\Models\Banking\Transaction': case 'App\Models\Banking\Transaction':

View File

@ -8,14 +8,14 @@ use App\Models\Document\Document;
class DocumentRecurring extends Event class DocumentRecurring extends Event
{ {
public $document; public $document;
public $notification;
/** /**
* Create a new event instance. * Create a new event instance.
*
* @param $document
*/ */
public function __construct(Document $document, string $notification) public function __construct(Document $document)
{ {
$this->document = $document; $this->document = $document;
$this->notification = $notification;
} }
} }

View File

@ -3,7 +3,6 @@
namespace App\Listeners\Document; namespace App\Listeners\Document;
use App\Events\Document\DocumentRecurring as Event; use App\Events\Document\DocumentRecurring as Event;
use App\Models\Document\Document;
class SendDocumentRecurringNotification class SendDocumentRecurringNotification
{ {
@ -16,13 +15,24 @@ class SendDocumentRecurringNotification
public function handle(Event $event) public function handle(Event $event)
{ {
$document = $event->document; $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 // 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")); $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 // Notify all users assigned to this company
foreach ($document->company->users as $user) { foreach ($document->company->users as $user) {
if (!$user->can('read-notifications')) { if (!$user->can('read-notifications')) {

View File

@ -6,119 +6,129 @@ return [
// Documents // Documents
Document::INVOICE_TYPE => [ Document::INVOICE_TYPE => [
'alias' => '', // core empty but module write own alias 'alias' => '', // core empty but module write own alias
'group' => 'sales', // controller folder name for permission and route 'group' => 'sales', // controller folder name for permission and route
'route' => [ 'route' => [
'prefix' => 'invoices', // core use with group + prefix, module ex. estimates 'prefix' => 'invoices', // core use with group + prefix, module ex. estimates
'parameter' => 'invoice', // sales/invoices/{parameter}/edit 'parameter' => 'invoice', // sales/invoices/{parameter}/edit
//'create' => 'invoices.create', // if you change route, you can write full path //'create' => 'invoices.create', // if you change route, you can write full path
], ],
'permission' => [ 'permission' => [
'prefix' => 'invoices', // this controller file name. 'prefix' => 'invoices', // this controller file name.
//'create' => 'create-sales-invoices', // if you change action permission key, you can write full permission //'create' => 'create-sales-invoices', // if you change action permission key, you can write full permission
], ],
'translation' => [ 'translation' => [
'prefix' => 'invoices', // this translation file name. 'prefix' => 'invoices', // this translation file name.
'add_contact' => 'general.customers', // 'add_contact' => 'general.customers', //
'issued_at' => 'invoices.invoice_date', 'issued_at' => 'invoices.invoice_date',
'due_at' => 'invoices.due_date', 'due_at' => 'invoices.due_date',
], ],
'setting' => [ '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 => [ Document::BILL_TYPE => [
'alias' => '', 'alias' => '',
'group' => 'purchases', 'group' => 'purchases',
'route' => [ 'route' => [
'prefix' => 'bills', 'prefix' => 'bills',
'parameter' => 'bill', 'parameter' => 'bill',
//'create' => 'bilss.create', //'create' => 'bilss.create',
], ],
'permission' => [ 'permission' => [
'prefix' => 'bills', 'prefix' => 'bills',
//'create' => 'create-purchases-bills', //'create' => 'create-purchases-bills',
], ],
'translation' => [ 'translation' => [
'prefix' => 'bills', 'prefix' => 'bills',
'issued_at' => 'bills.bill_date', 'issued_at' => 'bills.bill_date',
'due_at' => 'bills.due_date', 'due_at' => 'bills.due_date',
], ],
'setting' => [ '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 // Contacts
'customer' => [ 'customer' => [
'group' => 'sales', 'group' => 'sales',
'permission' => [ 'permission' => [
'prefix' => 'customers', 'prefix' => 'customers',
//'create' => 'create-sales-customers', //'create' => 'create-sales-customers',
], ],
], ],
'vendor' => [ 'vendor' => [
'group' => 'purchases', 'group' => 'purchases',
'permission' => [ 'permission' => [
'prefix' => 'vendors', 'prefix' => 'vendors',
//'create' => 'create-purchases-vendors', //'create' => 'create-purchases-vendors',
], ],
], ],
// Transactions // Transactions
'income' => [ 'income' => [
'group' => 'sales', 'group' => 'sales',
'permission' => [ 'permission' => [
'prefix' => 'revenues', 'prefix' => 'revenues',
//'create' => 'create-sales-revenues', //'create' => 'create-sales-revenues',
], ],
'contact_type' => 'customer', 'contact_type' => 'customer',
], ],
'expense' => [ 'expense' => [
'group' => 'purchases', 'group' => 'purchases',
'permission' => [ 'permission' => [
'prefix' => 'payments', 'prefix' => 'payments',
//'create' => 'create-purchases-payments', //'create' => 'create-purchases-payments',
], ],
'contact_type' => 'vendor', 'contact_type' => 'vendor',
], ],
// Categories // Categories
'category' => [ 'category' => [
'income' => [ 'income' => [
'alias' => '', 'alias' => '',
'translation' => [ 'translation' => [
'prefix' => 'general', 'prefix' => 'general',
], ],
], ],
'expense' => [ 'expense' => [
'alias' => '', 'alias' => '',
'translation' => [ 'translation' => [
'prefix' => 'general', 'prefix' => 'general',
], ],
], ],
'item' => [ 'item' => [
'alias' => '', 'alias' => '',
'translation' => [ 'translation' => [
'prefix' => 'general', 'prefix' => 'general',
], ],
], ],
'other' => [ 'other' => [
'alias' => '', 'alias' => '',
'translation' => [ 'translation' => [
'prefix' => 'general', 'prefix' => 'general',
], ],
], ],
], ],