<?php

namespace App\Listeners;

use App\Events\SalesComplete;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

use App\Models\ItemSalesTemp;
use App\Models\SalesItemHdr;
use DB;
use auth;
class SalesComp
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     */
    public function handle(SalesComplete $event): void
    {
        
       
        
        $SalesItemHdr = new SalesItemHdr();
        $ItemSalesTemp = ItemSalesTemp::WHERE('OrderCtrlNum',$event->SalesCtrlNum)->first();


        $SalesItemHdr->OrderCtrlNum = $ItemSalesTemp->OrderCtrlNum;
        $SalesItemHdr->UserId = auth()->user()->id;
        $SalesItemHdr->store = auth()->user()->store;




        DB::SELECT("INSERT INTO sales_item_dets
                    select OrderCtrlNum, itemctrlnum, quantity,itemctrlnum, quantity, price     from item_sales_temps
                    WHERE OrderCtrlNum = ?",[$event->SalesCtrlNum]);

        DB::SELECT("DELETE FROM item_sales_temps WHERE OrderCtrlNum = ?",[$event->SalesCtrlNum]);

        $SalesItemHdr->save();
        
        


    }
}
