Commit ae761b2a by Angel MAS

notify products stock

parent 6e5a34af
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
namespace Onestartup\Shop\Libs; namespace Onestartup\Shop\Libs;
use Onestartup\Shop\Model\ProductShop as Product; use Onestartup\Shop\Model\ProductShop as Product;
use Onestartup\Shop\Notifications\StockNotify;
use App\User;
class Util class Util
{ {
...@@ -24,12 +26,19 @@ class Util ...@@ -24,12 +26,19 @@ class Util
public function stock_change($sale, $old) public function stock_change($sale, $old)
{ {
$products = $sale->products; $products = $sale->products;
$admin = User::first();
if ($sale->status == 2) { if ($sale->status == 2) {
foreach ($products as $product) { foreach ($products as $product) {
$aux = Product::find($product->id); $aux = Product::find($product->id);
$aux->infoSale()->decrement('quantity', $product->pivot->quantity); $aux->infoSale()->decrement('quantity', $product->pivot->quantity);
$info = $aux->infoSale;
if ($info->quantity <= $info->reserve_amount) {
$admin->notify(new StockNotify($product));
}
} }
} else { } else {
......
<?php
namespace Onestartup\Shop\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Onestartup\Shop\Model\ProductShop as Product;
class StockNotify extends Notification
{
use Queueable;
protected $product;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Product $product)
{
$this->product = $product;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hola apreciable administrador')
->subject('Verificar existencia de producto: '.$this->product->name)
->line('El producto '.$this->product->name.' esta agotado o proximo a agotarse')
->line('Verifica la existencia del producto y actualiza su información')
->action('Ver producto', route('admin.shop.product.edit', $this->product->id));
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment