diff --git a/app/Http/Controllers/Auth/Users.php b/app/Http/Controllers/Auth/Users.php index 3a6665917..2207fdde0 100644 --- a/app/Http/Controllers/Auth/Users.php +++ b/app/Http/Controllers/Auth/Users.php @@ -203,4 +203,27 @@ class Users extends Controller // Redirect to invoices return redirect('incomes/invoices'); } + + /** + * Mark items out of stock notifications are read and redirect to items page. + * + * @param User $user + * + * @return Response + */ + public function readItemsOutOfStock(User $user) + { + // Mark item notifications as read + foreach ($user->unreadNotifications as $notification) { + // Not an item notification + if ($notification->getAttribute('type') != 'App\Notifications\Item\Item') { + continue; + } + + $notification->markAsRead(); + } + + // Redirect to items + return redirect('items/items'); + } } diff --git a/app/Http/Controllers/Expenses/Bills.php b/app/Http/Controllers/Expenses/Bills.php index deac2d44e..add0bcdc4 100644 --- a/app/Http/Controllers/Expenses/Bills.php +++ b/app/Http/Controllers/Expenses/Bills.php @@ -153,6 +153,10 @@ class Bills extends Controller $item_object = Item::find($item['item_id']); $item_sku = $item_object->sku; + + // Increase stock (item bought) + $item_object->quantity++; + $item_object->save(); } $tax = $tax_id = 0; diff --git a/app/Http/Controllers/Incomes/Invoices.php b/app/Http/Controllers/Incomes/Invoices.php index 6d4e6258f..765ddc94d 100644 --- a/app/Http/Controllers/Incomes/Invoices.php +++ b/app/Http/Controllers/Incomes/Invoices.php @@ -21,6 +21,7 @@ use App\Models\Setting\Category; use App\Models\Setting\Currency; use App\Models\Setting\Tax; use App\Notifications\Income\Invoice as Notification; +use App\Notifications\Item\Item as ItemNotification; use App\Traits\Currencies; use App\Traits\DateTime; use App\Traits\Uploads; @@ -161,6 +162,21 @@ class Invoices extends Controller $item_object = Item::find($item['item_id']); $item_sku = $item_object->sku; + + // Decrease stock (item sold) + $item_object->quantity--; + $item_object->save(); + + // Notify users if out of stock + if ($item_object->quantity == 0) { + foreach ($item_object->company->users as $user) { + if (!$user->can('read-notifications')) { + continue; + } + + $user->notify(new ItemNotification($item_object)); + } + } } $tax = $tax_id = 0; diff --git a/app/Http/ViewComposers/Header.php b/app/Http/ViewComposers/Header.php index 81d19afc3..656aee05b 100644 --- a/app/Http/ViewComposers/Header.php +++ b/app/Http/ViewComposers/Header.php @@ -20,6 +20,7 @@ class Header $bills = []; $invoices = []; + $items = []; $notifications = 0; $company = null; @@ -47,6 +48,10 @@ class Header $invoices[$data['invoice_id']] = $data['amount']; $notifications++; break; + case 'App\Notifications\Item\Item': + $items[$data['item_id']] = $data['name']; + $notifications++; + break; } } @@ -57,6 +62,7 @@ class Header 'notifications' => $notifications, 'bills' => $bills, 'invoices' => $invoices, + 'items' => $items, 'company' => $company, 'updates' => $updates, ]); diff --git a/app/Notifications/Item/Item.php b/app/Notifications/Item/Item.php new file mode 100644 index 000000000..ce1971bd3 --- /dev/null +++ b/app/Notifications/Item/Item.php @@ -0,0 +1,64 @@ +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) + { + return (new MailMessage) + ->line(trans('items.notification.message', ['name' => $this->item->name])) + ->action(trans('items.notification.button'), url('items/items', $this->item->id, true)); + } + + /** + * 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/resources/lang/en-GB/header.php b/resources/lang/en-GB/header.php index 42038fd3d..6f57b7989 100644 --- a/resources/lang/en-GB/header.php +++ b/resources/lang/en-GB/header.php @@ -8,6 +8,7 @@ return [ 'counter' => '{0} You have no notification|{1} You have :count notification|[2,*] You have :count notifications', 'overdue_invoices' => '{1} :count overdue invoice|[2,*] :count overdue invoices', 'upcoming_bills' => '{1} :count upcoming bill|[2,*] :count upcoming bills', + 'items_stock' => '{1} :count item out of stock|[2,*] :count items out of stock', 'view_all' => 'View All' ], diff --git a/resources/lang/en-GB/items.php b/resources/lang/en-GB/items.php index 8a7cf1852..eba79c452 100644 --- a/resources/lang/en-GB/items.php +++ b/resources/lang/en-GB/items.php @@ -7,4 +7,9 @@ return [ 'purchase_price' => 'Purchase Price', 'sku' => 'SKU', + 'notification' => [ + 'message' => 'You are receiving this email because the :name is running out of stock.', + 'button' => 'View Now', + ], + ]; diff --git a/resources/views/partials/admin/header.blade.php b/resources/views/partials/admin/header.blade.php index da0e10981..da4735265 100644 --- a/resources/views/partials/admin/header.blade.php +++ b/resources/views/partials/admin/header.blade.php @@ -116,6 +116,13 @@ @endif + @if (count($items)) +