<?php

namespace App\Listeners;

use App\Events\SalesComplete;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use DB;
use auth;
class SalesUpdateStock
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     */
    public function handle(SalesComplete $event): void
    {

        $SalesItems = DB::SELECT("SELECT ItemCtrlNum ,Quantity FROM sales_item_dets WHERE OrderCtrlNum = ?",[$event->SalesCtrlNum]);
        //dd($SalesItems);
        foreach($SalesItems as $SalesItem){
            DB::SELECT("UPDATE stocklevels SET Quantity = Quantity - ? WHERE ItemCtrlNum = ?",[$SalesItem->Quantity, $SalesItem->ItemCtrlNum]);
            
            
            DB::SELECT("UPDATE storestocklevels
            SET     Quantity = Quantity - ?
            WHERE   ItemCtrlNum = ?
            AND     StoreCtrlNum = ? ",[$SalesItem->Quantity, $SalesItem->ItemCtrlNum,auth()->user()->store]);

            
        }
    }
}
