out of stock notification
This commit is contained in:
@ -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');
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
]);
|
||||
|
64
app/Notifications/Item/Item.php
Normal file
64
app/Notifications/Item/Item.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications\Item;
|
||||
|
||||
use Illuminate\Notifications\Notification;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
|
||||
class Item extends Notification
|
||||
{
|
||||
/**
|
||||
* The item model.
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
public $item;
|
||||
|
||||
/**
|
||||
* Create a notification instance.
|
||||
*
|
||||
* @param object $item
|
||||
*/
|
||||
public function __construct($item)
|
||||
{
|
||||
$this->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,
|
||||
];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user