Merge pull request #511 from batuhawk/1.3-dev
Out-of-stock reminder settings
This commit is contained in:
		@@ -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) {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										33
									
								
								app/Listeners/Updates/Version130.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								app/Listeners/Updates/Version130.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Listeners\Updates;
 | 
			
		||||
 | 
			
		||||
use App\Events\UpdateFinished;
 | 
			
		||||
use Artisan;
 | 
			
		||||
 | 
			
		||||
class Version130 extends Listener
 | 
			
		||||
{
 | 
			
		||||
    const ALIAS = 'core';
 | 
			
		||||
 | 
			
		||||
    const VERSION = '1.3.0';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Handle the event.
 | 
			
		||||
     *
 | 
			
		||||
     * @param  $event
 | 
			
		||||
     * @return void
 | 
			
		||||
     */
 | 
			
		||||
    public function handle(UpdateFinished $event)
 | 
			
		||||
    {
 | 
			
		||||
        // Check if should listen
 | 
			
		||||
        if (!$this->check($event)) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Set new Item Reminder settings
 | 
			
		||||
        setting(['general.send_item_reminder' => '0');
 | 
			
		||||
        setting(['general.schedule_item_stocks' => '3,5,7']);
 | 
			
		||||
 | 
			
		||||
        setting()->save();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										69
									
								
								app/Notifications/Common/ItemReminder.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								app/Notifications/Common/ItemReminder.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,69 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Notifications\Common;
 | 
			
		||||
 | 
			
		||||
use Illuminate\Notifications\Notification;
 | 
			
		||||
use Illuminate\Notifications\Messages\MailMessage;
 | 
			
		||||
 | 
			
		||||
class ItemReminder 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)
 | 
			
		||||
    {
 | 
			
		||||
        $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,
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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',
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user