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

@ -31,6 +31,11 @@ return [
'contact_type' => 'customer', // use contact type 'contact_type' => 'customer', // use contact type
'hide' => [], // for document items 'hide' => [], // for document items
'class' => [], 'class' => [],
'notification' => [
'class' => 'App\Notifications\Sale\Invoice',
'notify_contact' => true,
'notify_user' => true,
],
], ],
Document::BILL_TYPE => [ Document::BILL_TYPE => [
@ -57,6 +62,11 @@ return [
'transaction_type' => 'expense', 'transaction_type' => 'expense',
'contact_type' => 'vendor', 'contact_type' => 'vendor',
'hide' => [], 'hide' => [],
'notification' => [
'class' => 'App\Notifications\Purchase\Bill',
'notify_contact' => false,
'notify_user' => true,
],
], ],
// Contacts // Contacts