diff --git a/app/Console/Commands/BillReminder.php b/app/Console/Commands/BillReminder.php index b90ac320d..8727550f5 100644 --- a/app/Console/Commands/BillReminder.php +++ b/app/Console/Commands/BillReminder.php @@ -4,7 +4,6 @@ namespace App\Console\Commands; use App\Models\Common\Company; use App\Models\Purchase\Bill; -use App\Notifications\Purchase\Bill as Notification; use App\Utilities\Overrider; use Date; use Illuminate\Console\Command; @@ -60,7 +59,7 @@ class BillReminder extends Command foreach ($days as $day) { $day = (int) trim($day); - $this->remind($day, $company); + $this->remind($day); } } @@ -69,7 +68,7 @@ class BillReminder extends Command setting()->forgetAll(); } - protected function remind($day, $company) + protected function remind($day) { // Get due date $date = Date::today()->addDays($day)->toDateString(); @@ -78,14 +77,8 @@ class BillReminder extends Command $bills = Bill::with('contact')->accrued()->notPaid()->due($date)->cursor(); foreach ($bills as $bill) { - // Notify all users assigned to this company - foreach ($company->users as $user) { - if (!$user->can('read-notifications')) { - continue; - } - - $user->notify(new Notification($bill, 'bill_remind_admin')); - } + event(new \App\Events\Purchase\BillReminding($bill)); + } } } diff --git a/app/Console/Commands/InvoiceReminder.php b/app/Console/Commands/InvoiceReminder.php index 1dff9fbc0..d2b9879a6 100644 --- a/app/Console/Commands/InvoiceReminder.php +++ b/app/Console/Commands/InvoiceReminder.php @@ -4,7 +4,6 @@ namespace App\Console\Commands; use App\Models\Common\Company; use App\Models\Sale\Invoice; -use App\Notifications\Sale\Invoice as Notification; use App\Utilities\Overrider; use Date; use Illuminate\Console\Command; @@ -60,7 +59,7 @@ class InvoiceReminder extends Command foreach ($days as $day) { $day = (int) trim($day); - $this->remind($day, $company); + $this->remind($day); } } @@ -69,7 +68,7 @@ class InvoiceReminder extends Command setting()->forgetAll(); } - protected function remind($day, $company) + protected function remind($day) { // Get due date $date = Date::today()->subDays($day)->toDateString(); @@ -78,19 +77,7 @@ class InvoiceReminder extends Command $invoices = Invoice::with('contact')->accrued()->notPaid()->due($date)->cursor(); foreach ($invoices as $invoice) { - // Notify the customer - if ($invoice->contact && !empty($invoice->contact_email)) { - $invoice->contact->notify(new Notification($invoice, 'invoice_remind_customer')); - } - - // Notify all users assigned to this company - foreach ($company->users as $user) { - if (!$user->can('read-notifications')) { - continue; - } - - $user->notify(new Notification($invoice, 'invoice_remind_admin')); - } + event(new \App\Events\Sale\InvoiceReminding($invoice)); } } } diff --git a/app/Events/Purchase/BillReminding.php b/app/Events/Purchase/BillReminding.php new file mode 100644 index 000000000..ee6d931c8 --- /dev/null +++ b/app/Events/Purchase/BillReminding.php @@ -0,0 +1,22 @@ +bill = $bill; + } +} diff --git a/app/Events/Sale/InvoiceReminding.php b/app/Events/Sale/InvoiceReminding.php new file mode 100644 index 000000000..76e7bdfb0 --- /dev/null +++ b/app/Events/Sale/InvoiceReminding.php @@ -0,0 +1,22 @@ +invoice = $invoice; + } +} diff --git a/app/Listeners/Purchase/SendBillRemindingNotification.php b/app/Listeners/Purchase/SendBillRemindingNotification.php new file mode 100644 index 000000000..51f4cb60e --- /dev/null +++ b/app/Listeners/Purchase/SendBillRemindingNotification.php @@ -0,0 +1,29 @@ +bill; + + // Notify all users assigned to this company + foreach ($bill->company->users as $user) { + if (!$user->can('read-notifications')) { + continue; + } + + $user->notify(new Notification($bill, 'bill_remind_admin')); + } + } +} diff --git a/app/Listeners/Sale/SendInvoiceRemindingNotification.php b/app/Listeners/Sale/SendInvoiceRemindingNotification.php new file mode 100644 index 000000000..ba1ba99fc --- /dev/null +++ b/app/Listeners/Sale/SendInvoiceRemindingNotification.php @@ -0,0 +1,34 @@ +invoice; + + // Notify the customer + if ($invoice->contact && !empty($invoice->contact_email)) { + $invoice->contact->notify(new Notification($invoice, 'invoice_remind_customer')); + } + + // Notify all users assigned to this company + foreach ($invoice->company->users as $user) { + if (!$user->can('read-notifications')) { + continue; + } + + $user->notify(new Notification($invoice, 'invoice_remind_admin')); + } + } +} diff --git a/app/Providers/Event.php b/app/Providers/Event.php index d888952f6..780fbb3a5 100644 --- a/app/Providers/Event.php +++ b/app/Providers/Event.php @@ -39,6 +39,9 @@ class Event extends Provider 'App\Events\Purchase\BillRecurring' => [ 'App\Listeners\Purchase\SendBillRecurringNotification', ], + 'App\Events\Purchase\BillReminding' => [ + 'App\Listeners\Purchase\SendBillRemindingNotification', + ], 'App\Events\Sale\PaymentReceived' => [ 'App\Listeners\Sale\CreateInvoiceTransaction', 'App\Listeners\Sale\SendInvoicePaymentNotification', @@ -59,6 +62,9 @@ class Event extends Provider 'App\Events\Sale\InvoiceRecurring' => [ 'App\Listeners\Sale\SendInvoiceRecurringNotification', ], + 'App\Events\Sale\InvoiceReminding' => [ + 'App\Listeners\Sale\SendInvoiceRemindingNotification', + ], 'App\Events\Menu\AdminCreated' => [ 'App\Listeners\Menu\AddAdminItems', ],