Merge pull request #90 from denisdulici/out-of-stock

Out of stock for inventory
This commit is contained in:
Denis Duliçi 2017-11-21 23:47:29 +03:00 committed by GitHub
commit 9a5a460848
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 127 additions and 0 deletions

View File

@ -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');
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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,
]);

View 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,
];
}
}

View File

@ -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'
],

View File

@ -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',
],
];

View File

@ -116,6 +116,13 @@
</a>
</li>
@endif
@if (count($items))
<li>
<a href="{{ url('auth/users/' . $user->id . '/read-items') }}">
<i class="fa fa-cubes text-red"></i> {{ trans_choice('header.notifications.items_stock', count($items), ['count' => count($items)]) }}
</a>
</li>
@endif
</ul>
</li>
<li class="footer"><a href="#">{{ trans('header.notifications.view_all') }}</a></li>

View File

@ -31,6 +31,7 @@ Route::group(['middleware' => 'language'], function () {
Route::get('logout', 'Auth\Login@destroy')->name('logout');
Route::get('users/{user}/read-bills', 'Auth\Users@readUpcomingBills');
Route::get('users/{user}/read-invoices', 'Auth\Users@readOverdueInvoices');
Route::get('users/{user}/read-items', 'Auth\Users@readItemsOutOfStock');
Route::resource('users', 'Auth\Users');
Route::resource('roles', 'Auth\Roles');
Route::resource('permissions', 'Auth\Permissions');