From 497494040147d72e68b599e8c5cc2519f86ed108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Ba=C5=9F?= Date: Sat, 29 Sep 2018 17:37:55 +0300 Subject: [PATCH 1/2] Out-of-stock reminder settings --- app/Http/Controllers/Incomes/Invoices.php | 17 ++++++ app/Listeners/Updates/Version130.php | 33 +++++++++++ app/Notifications/Common/Item.php | 2 +- app/Notifications/Common/ItemReminder.php | 69 +++++++++++++++++++++++ app/Providers/EventServiceProvider.php | 1 + resources/lang/en-GB/items.php | 5 +- resources/lang/en-GB/settings.php | 2 + 7 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 app/Listeners/Updates/Version130.php create mode 100644 app/Notifications/Common/ItemReminder.php diff --git a/app/Http/Controllers/Incomes/Invoices.php b/app/Http/Controllers/Incomes/Invoices.php index 4fb908994..75bad69d0 100644 --- a/app/Http/Controllers/Incomes/Invoices.php +++ b/app/Http/Controllers/Incomes/Invoices.php @@ -23,6 +23,7 @@ use App\Models\Setting\Currency; use App\Models\Setting\Tax; use App\Notifications\Income\Invoice as Notification; use App\Notifications\Common\Item as ItemNotification; +use App\Notifications\Common\ItemReminder as ItemReminderNotification; use App\Traits\Currencies; use App\Traits\DateTime; use App\Traits\Incomes; @@ -186,6 +187,22 @@ class Invoices extends Controller $item_object->quantity -= $item['quantity']; $item_object->save(); + if (setting('general.send_item_reminder')) { + $item_stocks = explode(',', setting('general.schedule_item_stocks')); + + foreach ($item_stocks as $item_stock) { + if ($item_object->quantity == $item_stock) { + foreach ($item_object->company->users as $user) { + if (!$user->can('read-notifications')) { + continue; + } + + $user->notify(new ItemReminderNotification($item_object)); + } + } + } + } + // Notify users if out of stock if ($item_object->quantity == 0) { foreach ($item_object->company->users as $user) { diff --git a/app/Listeners/Updates/Version130.php b/app/Listeners/Updates/Version130.php new file mode 100644 index 000000000..29ad25529 --- /dev/null +++ b/app/Listeners/Updates/Version130.php @@ -0,0 +1,33 @@ +check($event)) { + return; + } + + // Set new Item Reminder settings + setting(['general.send_item_reminder' => '0'); + setting(['general.schedule_item_stocks' => '3,5,7']); + + setting()->save(); + } +} diff --git a/app/Notifications/Common/Item.php b/app/Notifications/Common/Item.php index 208edb6e9..09bdd1d52 100644 --- a/app/Notifications/Common/Item.php +++ b/app/Notifications/Common/Item.php @@ -44,7 +44,7 @@ class Item extends Notification public function toMail($notifiable) { $message = (new MailMessage) - ->line(trans('items.notification.message', ['name' => $this->item->name])) + ->line(trans('items.notification.message.out_of_stock', ['name' => $this->item->name])) ->action(trans('items.notification.button'), url('items/items', $this->item->id, true)); // Override per company as Laravel doesn't read config diff --git a/app/Notifications/Common/ItemReminder.php b/app/Notifications/Common/ItemReminder.php new file mode 100644 index 000000000..168a78d08 --- /dev/null +++ b/app/Notifications/Common/ItemReminder.php @@ -0,0 +1,69 @@ +item = $item; + } + + /** + * Get the notification's channels. + * + * @param mixed $notifiable + * @return array|string + */ + public function via($notifiable) + { + return ['mail', 'database']; + } + + /** + * Build the mail representation of the notification. + * + * @param mixed $notifiable + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail($notifiable) + { + $message = (new MailMessage) + ->line(trans('items.notification.message.reminder', ['name' => $this->item->name])) + ->action(trans('items.notification.button'), url('items/items', $this->item->id, true)); + + // Override per company as Laravel doesn't read config + $message->from(config('mail.from.address'), config('mail.from.name')); + + return $message; + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + 'item_id' => $this->item->id, + 'name' => $this->item->name, + ]; + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 9c1cb6664..64a9eae9d 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -28,6 +28,7 @@ class EventServiceProvider extends ServiceProvider 'App\Listeners\Updates\Version129', 'App\Listeners\Updates\Version1210', 'App\Listeners\Updates\Version1211', + 'App\Listeners\Updates\Version130', ], 'Illuminate\Auth\Events\Login' => [ 'App\Listeners\Auth\Login', diff --git a/resources/lang/en-GB/items.php b/resources/lang/en-GB/items.php index eba79c452..8474e742d 100644 --- a/resources/lang/en-GB/items.php +++ b/resources/lang/en-GB/items.php @@ -8,7 +8,10 @@ return [ 'sku' => 'SKU', 'notification' => [ - 'message' => 'You are receiving this email because the :name is running out of stock.', + 'message' => [ + 'reminder' => 'You are receiving this email because the :name is remind of stock.', + 'out_of_stock' => 'You are receiving this email because the :name is running out of stock.', + ], 'button' => 'View Now', ], diff --git a/resources/lang/en-GB/settings.php b/resources/lang/en-GB/settings.php index 20e7cc22b..39b49a5de 100644 --- a/resources/lang/en-GB/settings.php +++ b/resources/lang/en-GB/settings.php @@ -66,6 +66,8 @@ return [ 'bill_days' => 'Send Before Due Days', 'cron_command' => 'Cron Command', 'schedule_time' => 'Hour To Run', + 'send_item_reminder'=> 'Item Reminder', + 'item_stocks' => 'Send Item Stock', ], 'appearance' => [ 'tab' => 'Appearance', From 30e7a65803e20d2e1ccdda1585f52d3de323d22b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Ba=C5=9F?= Date: Sat, 29 Sep 2018 17:48:37 +0300 Subject: [PATCH 2/2] Language change --- resources/lang/en-GB/settings.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/lang/en-GB/settings.php b/resources/lang/en-GB/settings.php index 39b49a5de..0b4bec21b 100644 --- a/resources/lang/en-GB/settings.php +++ b/resources/lang/en-GB/settings.php @@ -66,8 +66,8 @@ return [ 'bill_days' => 'Send Before Due Days', 'cron_command' => 'Cron Command', 'schedule_time' => 'Hour To Run', - 'send_item_reminder'=> 'Item Reminder', - 'item_stocks' => 'Send Item Stock', + 'send_item_reminder'=> 'Send Item Reminder', + 'item_stocks' => 'Send When Item Stock', ], 'appearance' => [ 'tab' => 'Appearance',