<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Invoice;
use App\User;
use App\Product;
use App\Numbertowordconvertsconver;
use Session;

class ComController extends Controller
{

    public function __construct()
    {
      // $this->middleware('auth');
    }
    
    
    function rem_allinv_item(Request $request){
       
        
        $this->validate($request, [
                'temp_line_id'  => 'required',
                'ref_id'  => 'required'
                
        ]);
        $temp_pro_id = $request->input('temp_line_id');
        $ref = $request->input('ref_id');
        $invoice = new Invoice();
                        $depo = $invoice->get_sales_depo($ref);
        
        $resultitem = app('db')->select("SELECT * FROM `temp_qto_items` WHERE `temp_qto_items`.`po_itm_id` = '$temp_pro_id'");
        $count = count($resultitem);
        if($count>0){
            $gt_batch = $resultitem[0]->po_itm_item_id;
                $all_data = app('db')->select("SELECT * FROM `temp_qto_items` WHERE `po_itm_item_id` = '$gt_batch' and `po_user`=$ref");
              $allqty = 0;  
                foreach ($all_data as $row) {
                    $allqty = $row->po_itm_qty+ $row->free_item;
                    $line_num = $row->po_itm_id;
                        
                        $updt = app('db')->delete("DELETE FROM `temp_qto_items` WHERE `po_itm_id`='$line_num'");
                        if($updt){
                                $data = "Removed";
                                $cal = new Invoice();
                                $cal_stock = $cal->cal_batch($row->depo_id,"temprem","0",$gt_batch,$allqty,"plus",$row->po_itm_batch,0);
                                
                        }else{
                                $temp_pro_id = $data = "error";
                        }
                }
              
        
        }else{
            $data = "Incorrect Id";
             $temp_pro_id = $data = "error";
        }
        
        $dataa = response()->json($temp_pro_id);
            return $dataa;
    }
    
    function all_stock_cal(Request $request){
       
       $this->validate($request, [
            'depo' => 'required',
            'type' => 'required',
            'inv_id' => 'required',
            'item_id' => 'required',
            'qty' => 'required',
            'add' => 'required',
            'batch' => 'required',
            'usr' => 'required',
        ]);
            
            $depo =$request->input('depo');
            $type = $request->input('type');
            $inv_id =$request->input('inv_id'); 
            $item_id = $request->input('item_id');
            $qty =$request->input('qty');
            $add =$request->input('add');
            $batch = $request->input('batch');
            $usr = $request->input('usr');
    
    $cal = new Invoice();
       $cal_stock = $cal->cal_batch($depo,$type,$inv_id,$item_id,$qty,$add,$batch,$usr);


        $data = response()->json($cal_stock);
            return $data;

    }
    
}    