<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use App\Models\inward;
use App\Models\area_master;
use App\Models\Makeslist;
use App\Models\move_inventory;
use App\Models\move_inventoryarm;
use App\Models\move_inventoryccms;
use App\Models\armsclamps;
use App\Models\armsclamps_vendor;
use App\Models\inwardccmstypes;
use App\Models\inwards_vendor;
use App\Models\gramuser;
use App\Models\outward;
use App\Models\total_inward;
use App\Models\outward_armsclamps;
use App\Models\outward_ccms;
use App\Models\subvendor_ccms;
use App\Models\subvendor_ccms_invoices;
use App\Models\ccms_vendor;
use App\Models\subvendor_armsclamps;
use App\Models\subvendor_armsclamps_invoices;
use Redirect;
use App\Models\subvendor_lights;
use App\Models\subvendor_lights_invoices;
use DB;
use Validator;
use Illuminate\Support\Facades\Storage;


class OutwardController extends Controller
{

    public function __construct(Request $request) { 
   //dd(Session::get('districts'));
    if(is_null(Session::get('districts'))){
       // Redirect::to('/')->send();
 } 
}


    public function outwards_view(Request $request)
{
    // dd(Session::get('districts'));

     if($request->url!='dashboard'){
            Session::forget('dashboardurl');

          }

     DB::enableQueryLog();
       $area_dis=outward::with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->whereIn('jilla_id',$request->session()->get('districts'))->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),DB::raw("SUM(bufferstck_received_qty_18w) as buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as buffer_stk_rcv_190"),'jilla_id')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->groupby('jilla_id')->get()->toArray();

     DB::enableQueryLog();
       $area_disc=inwards_vendor::with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->whereIn('jilla_id',$request->session()->get('districts'))->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),DB::raw("SUM(bufferstck_received_qty_18w) as buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as buffer_stk_rcv_190"),'jilla_id')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->groupby('jilla_id')->get()->toArray();
       //dd( $area_dis);

     DB::enableQueryLog();
       $maker_name_count=outward::with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->whereIn('jilla_id',$request->session()->get('districts'))->select(DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),DB::raw("SUM(bufferstck_invoice_qty_18w) as buffer_stk_18"),DB::raw("SUM(bufferstck_invoice_qty_35w) as buffer_stk_35"),DB::raw("SUM(bufferstck_invoice_qty_70w) as buffer_stk_70"),DB::raw("SUM(bufferstck_invoice_qty_110w) as buffer_stk_110"),DB::raw("SUM(bufferstck_invoice_qty_190w) as buffer_stk_190"),DB::raw("SUM(bufferstck_received_qty_18w) as buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as buffer_stk_rcv_190"),DB::raw("SUM(led_wattage_18) as invoice_18w"),DB::raw("SUM(led_wattage_35) as invoice_35w"),DB::raw("SUM(led_wattage_70) as invoice_70w"),DB::raw("SUM(led_wattage_110) as invoice_110w"),DB::raw("SUM(led_wattage_190) as invoice_190w"),'maker_name','jilla_id')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->groupby('maker_name')->get()->toArray();
    //$inventory = outward::with(['area_district','vendor_name'])->orderby('created_at','desc')->get()->toArray();
     //dd($inventory);
     return view('backend.outwards.outwards_view',compact('area_dis','area_disc','maker_name_count'));

}

 public function outwards_withmaker_list(Request $request)
{

     if($request->url!='dashboard'){
            Session::forget('dashboardurl');

          }
     $districts=outward::select('jilla_id')->groupby('jilla_id')->get();
foreach ($districts as $d) {
     DB::enableQueryLog();
       $area_dis_withmaker[]=outward::with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->whereIn('jilla_id',$request->session()->get('districts'))->select("maker_name",DB::raw("count(jilla_id) as count"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),DB::raw("SUM(bufferstck_invoice_qty_18w) as buffer_stk_18"),DB::raw("SUM(bufferstck_invoice_qty_35w) as buffer_stk_35"),DB::raw("SUM(bufferstck_invoice_qty_70w) as buffer_stk_70"),DB::raw("SUM(bufferstck_invoice_qty_110w) as buffer_stk_110"),DB::raw("SUM(bufferstck_invoice_qty_190w) as buffer_stk_190"),DB::raw("SUM(bufferstck_received_qty_18w) as buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as buffer_stk_rcv_190"),DB::raw("SUM(led_wattage_18) as invoice_18w"),DB::raw("SUM(led_wattage_35) as invoice_35w"),DB::raw("SUM(led_wattage_70) as invoice_70w"),DB::raw("SUM(led_wattage_110) as invoice_110w"),DB::raw("SUM(led_wattage_190) as invoice_190w"),'jilla_id')->where('jilla_id',$d['jilla_id'])->groupby('jilla_id')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->groupby('maker_name')->get()->toArray();
   }

   $districts=outward::select('maker_name')->groupby('maker_name')->get();
foreach ($districts as $d) {
     DB::enableQueryLog();
       $area_dis_withmakers[]=outward::with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->whereIn('jilla_id',$request->session()->get('districts'))->select("maker_name",DB::raw("count(jilla_id) as count"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),DB::raw("SUM(bufferstck_invoice_qty_18w) as buffer_stk_18"),DB::raw("SUM(bufferstck_invoice_qty_35w) as buffer_stk_35"),DB::raw("SUM(bufferstck_invoice_qty_70w) as buffer_stk_70"),DB::raw("SUM(bufferstck_invoice_qty_110w) as buffer_stk_110"),DB::raw("SUM(bufferstck_invoice_qty_190w) as buffer_stk_190"),DB::raw("SUM(bufferstck_received_qty_18w) as buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as buffer_stk_rcv_190"),DB::raw("SUM(led_wattage_18) as invoice_18w"),DB::raw("SUM(led_wattage_35) as invoice_35w"),DB::raw("SUM(led_wattage_70) as invoice_70w"),DB::raw("SUM(led_wattage_110) as invoice_110w"),DB::raw("SUM(led_wattage_190) as invoice_190w"),'maker_name')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->where('maker_name',$d['maker_name'])->groupby('maker_name')->get()->toArray();
    
   }
       //dd( $area_dis_withmaker);
    //$inventory = outward::with(['area_district','vendor_name'])->orderby('created_at','desc')->get()->toArray();
     //dd($inventory);
     return view('backend.outwards.outwards_withmaker_list',compact('area_dis_withmaker','area_dis_withmakers'));

}

public function outwards_invoice_list(Request $request)
    {
        // $area_invoice=inward::with(['area_district'])->groupby('invoice_number')->where('jilla_id',$request->id)->get()->toArray();
        //dd($area_invoice);
  


        $area_invoice=outward::with(['area_district' => function ($query) {
        $query->select('id', 'area_name');
        }])->select(DB::raw("count(invoice_number) as count"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),DB::raw("SUM(bufferstck_received_qty_18w) as buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as buffer_stk_rcv_190"),'invoice_number','jilla_id','maker_name')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->groupby('invoice_number')->where('jilla_id',$request->id)->get()->toArray();
        //dd($area_invoice);

         $maker_counts=outward::with(['area_district' => function ($query) {
        $query->select('id', 'area_name');
        }])->whereIn('jilla_id',$request->session()->get('districts'))->select('maker_name',DB::raw("count(maker_name) as countss"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),DB::raw("SUM(bufferstck_invoice_qty_18w) as buffer_stk_18"),DB::raw("SUM(bufferstck_invoice_qty_35w) as buffer_stk_35"),DB::raw("SUM(bufferstck_invoice_qty_70w) as buffer_stk_70"),DB::raw("SUM(bufferstck_invoice_qty_110w) as buffer_stk_110"),DB::raw("SUM(bufferstck_invoice_qty_190w) as buffer_stk_190"),DB::raw("SUM(bufferstck_received_qty_18w) as buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as buffer_stk_rcv_190"),DB::raw("SUM(led_wattage_18) as invoice_18w"),DB::raw("SUM(led_wattage_35) as invoice_35w"),DB::raw("SUM(led_wattage_70) as invoice_70w"),DB::raw("SUM(led_wattage_110) as invoice_110w"),DB::raw("SUM(led_wattage_190) as invoice_190w"),'invoice_number','phase_no','jilla_id','approve_status','remarks')->where(function($q) use ($request){    
      if(isset($request->status)){
        $q->where('approve_status',$request->status);
        //dd($request->status);
      }else{

           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
      }
   
    })->groupby('maker_name')->where('jilla_id',$request->id)->get()->toArray();

    //     ->where(function($q) use ($request) {
    //      if($request->session()->get('role')==6){
    //         $q->where('vendor_id',$request->session()->get('id'));
    //      }
    // })


        $balance_invoice=inwards_vendor::with(['area_district' => function ($query) {
        $query->select('id', 'area_name');
        }])->select(DB::raw("count(invoice_number) as count"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),DB::raw("SUM(bufferstck_received_qty_18w) as buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as buffer_stk_rcv_190"),'invoice_number','jilla_id','maker_name')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->groupby('invoice_number')->where('jilla_id',$request->id)->get()->toArray();

        $district  = area_master::where('id',$request->id)->first();

  


       return view('backend.outwards.outwards_invoice_list',compact('area_invoice','district','balance_invoice','maker_counts'));
    }

public function outwards_list(Request $request)
{
     $makeslist= Makeslist::get();

    $outwards= outward::where('invoice_number',$request->id)->where('jilla_id',$request->jilla_id)->with('area_district')->get();
                if ($request->is("api*")) {
                        return json_encode($jilla);
                    }
    return view('backend.outwards.outwards_list',compact('outwards','makeslist'));
}

 
    


// public function store_outwards(Request $request){


//         $validation = Validator::make(
//             $request->all(),
//             [
//                 'jilla_id' => 'required',
//                 'vendor_id' => 'required',
//                 'maker_name' => 'required',
//                 'num_drivers' => 'required',
//                 'num_spd' => 'required',
//                 'tiltable_adapters' => 'required',
//                 'led_wattage_18' => 'required',
//                 'led_wattage_35' => 'required',
//                 'led_wattage_70' => 'required',
//                 'led_wattage_110' => 'required',
//                 'led_wattage_190' => 'required',
//                 'buffer_stock' => 'required',               
//                 'invoice_date' =>  'required',
//                 'invoice_number' => 'required|unique:outwards',
//                 'total_rcv_qty'=>'required',
                
//                 'files' => 'required',
//                 'rcv_qty_18w' => 'required',
//                 'rcv_qty_35w' => 'required',
//                 'rcv_qty_70w' => 'required',
//                 'rcv_qty_110w' => 'required',
//                 'rcv_qty_190w' => 'required',
//                 'bufstck_rcv_qty' => 'required',
//                 'tiltadap_rcv_qty' => 'required',
//                 'num_drv_rcv_qty' => 'required',
//                 'num_spd_rcv_qty' => 'required',
//                 'date_of_receiving' => 'required',

//                 'bufferstck_received_qty_18w' => 'required',
//                 'bufferstck_received_qty_35w' => 'required',
//                 'bufferstck_received_qty_70w' => 'required',
//                 'bufferstck_received_qty_110w' => 'required',
//                 'bufferstck_received_qty_190w' => 'required',
//                 'bufferstck_invoice_qty_18w' => 'required',
//                 'bufferstck_invoice_qty_35w' => 'required',
//                 'bufferstck_invoice_qty_70w' => 'required',
//                 'bufferstck_invoice_qty_110w' => 'required',
//                 'bufferstck_invoice_qty_190w' => 'required',

//                 'drivers_received_qty_18w' => 'required',
//                 'drivers_received_qty_35w' => 'required',
//                 'drivers_received_qty_70w' => 'required',
//                 'drivers_received_qty_110w' => 'required',
//                 'drivers_received_qty_190w' => 'required',
//                 'drivers_invoice_qty_18w' => 'required',
//                 'drivers_invoice_qty_35w' => 'required',
//                 'drivers_invoice_qty_70w' => 'required',
//                 'drivers_invoice_qty_110w' => 'required',
//                 'drivers_invoice_qty_190w' => 'required',

//                 'spd_received_qty_18w' => 'required',
//                 'spd_received_qty_35w' => 'required',
//                 'spd_received_qty_70w' => 'required',
//                 'spd_received_qty_110w' => 'required',
//                 'spd_received_qty_190w' => 'required',
//                 'spd_invoice_qty_18w' => 'required',
//                 'spd_invoice_qty_35w' => 'required',
//                 'spd_invoice_qty_70w' => 'required',
//                 'spd_invoice_qty_110w' => 'required',
//                 'spd_invoice_qty_190w' => 'required'
                        
                        
             
//             ]
//         );

//         if ($validation->fails()) {
//              if ($request->is("api*")) {
//                 $data=["error"=>true,"error_msg"=>$validation->getMessageBag()->first()];
//                 return  json_encode($data);
//              }
            
//         else{
//             return redirect(url('outwards_store'))->with('error',$validation->getMessageBag()->first());
//         }
//     }

//      $outwards_info = new outward();
//      //dd($outwards_info);
//          $outwards_info->jilla_id = $request->input('jilla_id');
//            $outwards_info->vendor_id = $request->input('vendor_id');
//          $outwards_info->maker_name = $request->input('maker_name');

//          $outwards_info->num_drivers = $request->input('num_drivers');
//          $outwards_info->num_drv_rcv_qty = $request->input('num_drv_rcv_qty');
//          $outwards_info->num_spd = $request->input('num_spd');
//          $outwards_info->num_spd_rcv_qty = $request->input('num_spd_rcv_qty');
//          $outwards_info->tiltable_adapters = $request->input('tiltable_adapters');
//          $outwards_info->tiltadap_rcv_qty = $request->input('tiltadap_rcv_qty');
//          $outwards_info->led_wattage_18 = $request->input('led_wattage_18');
//          $outwards_info->rcv_qty_18w = $request->input('rcv_qty_18w');
//          $outwards_info->led_wattage_35 = $request->input('led_wattage_35');
//          $outwards_info->rcv_qty_35w = $request->input('rcv_qty_35w');
//          $outwards_info->led_wattage_70 = $request->input('led_wattage_70');
//          $outwards_info->rcv_qty_70w = $request->input('rcv_qty_70w');
//          $outwards_info->led_wattage_110 = $request->input('led_wattage_110');
//          $outwards_info->rcv_qty_110w = $request->input('rcv_qty_110w');
//          $outwards_info->led_wattage_190 = $request->input('led_wattage_190');
//          $outwards_info->rcv_qty_190w = $request->input('rcv_qty_190w');
//          $outwards_info->buffer_stock = $request->input('buffer_stock');
//          $outwards_info->bufstck_rcv_qty = $request->input('bufstck_rcv_qty');
//          $outwards_info->total = $request->input('total');
        
//          $insert=[];
//          if($request->hasFile('files') )
//          {
//             $path=$request->file('files')->store('public');  
//             $insert[]=$path;
//          }else{
//             $name="pdf".rand(11111111, 99999999).".pdf"; 
//             Storage::disk('public')->put($name,base64_decode($request->input('files')));
//             $insert[]="public/".$name;
//          }

//           //dd($insert);
//           $outwards_info->files=implode(',', $insert);



//            $outwards_info->bufferstck_received_qty_18w = $request->input('bufferstck_received_qty_18w');
//            $outwards_info->bufferstck_received_qty_35w = $request->input('bufferstck_received_qty_35w');
//            $outwards_info->bufferstck_received_qty_70w = $request->input('bufferstck_received_qty_70w');
//         $outwards_info->bufferstck_received_qty_110w = $request->input('bufferstck_received_qty_110w');
//         $outwards_info->bufferstck_received_qty_190w = $request->input('bufferstck_received_qty_190w');
//            $outwards_info->bufferstck_invoice_qty_18w = $request->input('bufferstck_invoice_qty_18w'); 
//            $outwards_info->bufferstck_invoice_qty_35w = $request->input('bufferstck_invoice_qty_35w');
//            $outwards_info->bufferstck_invoice_qty_70w = $request->input('bufferstck_invoice_qty_70w');
//            $outwards_info->bufferstck_invoice_qty_110w = $request->input('bufferstck_invoice_qty_110w');
//            $outwards_info->bufferstck_invoice_qty_190w = $request->input('bufferstck_invoice_qty_190w');

//         $outwards_info->drivers_received_qty_18w = $request->input('drivers_received_qty_18w');
//            $outwards_info->drivers_received_qty_35w = $request->input('drivers_received_qty_35w');
//            $outwards_info->drivers_received_qty_70w = $request->input('drivers_received_qty_70w');
//         $outwards_info->drivers_received_qty_110w = $request->input('drivers_received_qty_110w');
//         $outwards_info->drivers_received_qty_190w = $request->input('drivers_received_qty_190w');
//            $outwards_info->drivers_invoice_qty_18w = $request->input('drivers_invoice_qty_18w'); 
//            $outwards_info->drivers_invoice_qty_35w = $request->input('drivers_invoice_qty_35w');
//            $outwards_info->drivers_invoice_qty_70w = $request->input('drivers_invoice_qty_70w');
//            $outwards_info->drivers_invoice_qty_110w = $request->input('drivers_invoice_qty_110w');
//            $outwards_info->drivers_invoice_qty_190w = $request->input('drivers_invoice_qty_190w');


//             $outwards_info->spd_received_qty_18w = $request->input('spd_received_qty_18w');
//            $outwards_info->spd_received_qty_35w = $request->input('spd_received_qty_35w');
//            $outwards_info->spd_received_qty_70w = $request->input('spd_received_qty_70w');
//         $outwards_info->spd_received_qty_110w = $request->input('spd_received_qty_110w');
//         $outwards_info->spd_received_qty_190w = $request->input('spd_received_qty_190w');
//            $outwards_info->spd_invoice_qty_18w = $request->input('spd_invoice_qty_18w'); 
//            $outwards_info->spd_invoice_qty_35w = $request->input('spd_invoice_qty_35w');
//            $outwards_info->spd_invoice_qty_70w = $request->input('spd_invoice_qty_70w');
//            $outwards_info->spd_invoice_qty_110w = $request->input('spd_invoice_qty_110w');
//            $outwards_info->spd_invoice_qty_190w = $request->input('spd_invoice_qty_190w');


//         $outwards_info->invoice_date = $request->input('invoice_date');
//         $outwards_info->invoice_number = $request->input('invoice_number');
//         $outwards_info->total_rcv_qty = $request->input('total_rcv_qty');
//         $outwards_info->received_quantity = $request->input('total_rcv_qty');
//          $outwards_info->remarks = $request->input('remarks');
//           $outwards_info->date_of_receiving = $request->input('date_of_receiving');
                
//          $outwards_info->save();
//           if ($request->is("api*")) {     
//             return json_encode(["error"=>false,"success"=>"Data Inserted Successfully ","status"=>$outwards_info]);
//         }else{
//             return redirect(url('outwards_store'))->withSuccess('Added Successfully.');
//         }
//     }
public function outwards_store(Request $request)
{
   
   $jilla=DB::table('area_masters')
                    ->where('parent_id','=','0')
                    ->where('super_id','=','0')
                    ->whereIn('id',$request->session()->get('districts'))
                    ->where('status','=','active')
                    ->get();
    $makeslist= Makeslist::get();
    
    $invoicelist=DB::table('inwards')->where('invoice_number',$request->get('jilla_id'))
       ->get();
       //dd($invoicelist);
    $vendorslists=gramuser::where('role',6)->get();

     return view('backend.outwards.outwards_store',compact('jilla','makeslist','vendorslists','invoicelist'));
}

public function outwards_invoiceslist($id){

            $jillaid=DB::table('inwards')
                    ->where('jilla_id','=',$id)  
                    ->get();
         //    var_dump($jillaid);
         // exit();
    return view('backend.invoice_number',compact('jillaid'));
}
public function outwards_armsinvoiceslist($id){

            $jillaid=DB::table('armsclamps')
                    ->where('jilla_id','=',$id)  
                    ->get();
         //    var_dump($jillaid);
         // exit();
    return view('backend.invoice_number',compact('jillaid'));
}
public function outwards_ccmsinvoiceslist($id){

            $jillaid=DB::table('inwardccmstypes')
                    ->where('jilla_id','=',$id)  
                    ->get();
         //    var_dump($jillaid);
         // exit();
    return view('backend.invoice_number',compact('jillaid'));
}

     public function outwards_vendor_list(Request $request)
     {
      
        //->where('jilla_id',$request->id)
        //dd($area_invoice);
        $vendorslists=gramuser::where('role',6)->where(function ($query) use ($request) {
      if($request->session()->get('role')==6){

       $query->where('id',$request->session()->get('id'));
      }
  })->get();
    

        $district  = area_master::where('id',$request->id)->first();
       return view('backend.outwards.outwards_vendor_list',compact('district','vendorslists'));
    }

       public function outwards_view_dist(Request $request)
{

     DB::enableQueryLog();
       $area_dis=outward::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->has('vendor_name')->with(['vendor_name'=> function ($query) {
        $query->select('id', 'name');
    }])->where('vendor_id',$request->id)->whereIn('jilla_id',$request->session()->get('districts'))->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),'jilla_id','vendor_id')->groupby('jilla_id')->get()->toArray();

    //dd($area_dis);
     return view('backend.outwards.outwards_view_dist',compact('area_dis'));

}

public function store_outwards(Request $request){

     $inwardchec = inward::where(['jilla_id'=>$request->jilla_id,"maker_name"=>$request->maker_name,"invoice_number"=>$request->invoice_number])->where(function($q) use ($request){
            
              if($request->rcv_qty_18w){
                $q->where('rcv_qty_18w',">=",$request->rcv_qty_18w);
            }
            if($request->rcv_qty_35w){
                $q->where('rcv_qty_35w',">=",$request->rcv_qty_35w);
            }
              
            if($request->rcv_qty_70w){
                $q->where('rcv_qty_70w',">=",$request->rcv_qty_70w);
            }
            if($request->rcv_qty_110w){
                $q->where('rcv_qty_110w',">=",$request->rcv_qty_110w);
            }
            if($request->rcv_qty_190w){
                $q->where('rcv_qty_190w',">=",$request->rcv_qty_190w);
            }
            // if($request->tiltadap_rcv_qty){
            //     $q->where('tiltadap_rcv_qty',">=",$request->tiltadap_rcv_qty);
            // }
       

           if($request->bufstck_rcv_qty){
                $q->where('bufstck_rcv_qty',">=",$request->bufstck_rcv_qty);
            }
            if($request->bufferstck_received_qty_18w){
                $q->where('bufferstck_received_qty_18w',">=",$request->bufferstck_received_qty_18w);
            }
              
            if($request->bufferstck_received_qty_35w){
                $q->where('bufferstck_received_qty_35w',">=",$request->bufferstck_received_qty_35w);
            }
            if($request->bufferstck_received_qty_110w){
                $q->where('bufferstck_received_qty_110w',">=",$request->bufferstck_received_qty_110w);
            }
            if($request->bufferstck_received_qty_70w){
                $q->where('bufferstck_received_qty_70w',">=",$request->bufferstck_received_qty_70w);
            }
            if($request->bufferstck_received_qty_190w){
                $q->where('bufferstck_received_qty_190w',">=",$request->bufferstck_received_qty_190w);
            }

            if($request->num_drv_rcv_qty){
                $q->where('num_drv_rcv_qty',">=",$request->num_drv_rcv_qty);
            }
            if($request->drivers_received_qty_18w){
                $q->where('drivers_received_qty_18w',">=",$request->drivers_received_qty_18w);
            }
              
            if($request->drivers_received_qty_35w){
                $q->where('drivers_received_qty_35w',">=",$request->drivers_received_qty_35w);
            }
            if($request->drivers_received_qty_70w){
                $q->where('drivers_received_qty_70w',">=",$request->drivers_received_qty_70w);
            }
            if($request->drivers_received_qty_110w){
                $q->where('drivers_received_qty_110w',">=",$request->drivers_received_qty_110w);
            }
            if($request->drivers_received_qty_190w){
                $q->where('drivers_received_qty_190w',">=",$request->drivers_received_qty_190w);
            }

            if($request->num_spd_rcv_qty){
                $q->where('num_spd_rcv_qty',">=",$request->num_spd_rcv_qty);
            }
            if($request->spd_received_qty_18w){
                $q->where('spd_received_qty_18w',">=",$request->spd_received_qty_18w);
            }
              
            if($request->spd_received_qty_35w){
                $q->where('spd_received_qty_35w',">=",$request->spd_received_qty_35w);
            }
            if($request->spd_received_qty_70w){
                $q->where('spd_received_qty_70w',">=",$request->spd_received_qty_70w);
            }
            if($request->spd_received_qty_110w){
                $q->where('spd_received_qty_110w',">=",$request->spd_received_qty_110w);
            }
            if($request->spd_received_qty_190w){
                $q->where('spd_received_qty_190w',">=",$request->spd_received_qty_190w);
            }
            // if($request->total_rcv_qty){
            //     $q->where('total_rcv_qty',">=",$request->total_rcv_qty);
            // }
       
        })->first();

     //dd($inwardchec);

        //$check=inward::get();
       //dd( $inwardchec);

        if(!empty($inwardchec)){
             $inwardchec->jilla_id = $inwardchec->jilla_id;
            $inwardchec->maker_name = $inwardchec->maker_name;
             $insert=[];
          if($request->hasFile('files') )
         {
            $path=$request->file('files')->store('public');  
            $insert[]=$path;
         }else{
            $name="pdf".rand(11111111, 99999999).".pdf"; 
            Storage::disk('public')->put($name,base64_decode($request->input('files')));
            $insert[]="public/".$name;
         }


    if($request->rcv_qty_18w){                 
                $inwardchec->rcv_qty_18w=$inwardchec->rcv_qty_18w-$request->rcv_qty_18w;
            }
            if($request->rcv_qty_35w){
               $inwardchec->rcv_qty_35w= $inwardchec->rcv_qty_35w-$request->rcv_qty_35w;
            }
             if($request->rcv_qty_70w){                 
                $inwardchec->rcv_qty_70w=$inwardchec->rcv_qty_70w-$request->rcv_qty_70w;
            }
            if($request->rcv_qty_110w){
               $inwardchec->rcv_qty_110w= $inwardchec->rcv_qty_110w-$request->rcv_qty_110w;
            }
             if($request->rcv_qty_190w){                 
                $inwardchec->rcv_qty_190w=$inwardchec->rcv_qty_190w-$request->rcv_qty_190w;
            }



    if($request->bufstck_rcv_qty){
               $inwardchec->bufstck_rcv_qty= $inwardchec->bufstck_rcv_qty-$request->bufstck_rcv_qty;
            }
                  if($request->bufferstck_received_qty_18w){
               $inwardchec->bufferstck_received_qty_18w= $inwardchec->bufferstck_received_qty_18w-$request->bufferstck_received_qty_18w;
            }

             if($request->bufferstck_received_qty_35w){
               $inwardchec->bufferstck_received_qty_35w= $inwardchec->bufferstck_received_qty_35w-$request->bufferstck_received_qty_35w;
            }
             if($request->bufferstck_received_qty_110w){                 
                $inwardchec->bufferstck_received_qty_110w=$inwardchec->bufferstck_received_qty_110w-$request->bufferstck_received_qty_110w;
            }
            if($request->bufferstck_received_qty_70w){
               $inwardchec->bufferstck_received_qty_70w= $inwardchec->bufferstck_received_qty_70w-$request->bufferstck_received_qty_70w;
            }
             if($request->bufferstck_received_qty_190w){                 
                $inwardchec->bufferstck_received_qty_190w=$inwardchec->bufferstck_received_qty_190w-$request->bufferstck_received_qty_190w;
            }


           
    if($request->num_drv_rcv_qty){                 
                $inwardchec->num_drv_rcv_qty=$inwardchec->num_drv_rcv_qty-$request->num_drv_rcv_qty;
            }
            if($request->drivers_received_qty_18w){
               $inwardchec->drivers_received_qty_18w= $inwardchec->drivers_received_qty_18w-$request->drivers_received_qty_18w;
            }

             if($request->drivers_received_qty_35w){
               $inwardchec->drivers_received_qty_35w= $inwardchec->drivers_received_qty_35w-$request->drivers_received_qty_35w;
            }
             if($request->drivers_received_qty_70w){                 
                $inwardchec->drivers_received_qty_70w=$inwardchec->drivers_received_qty_70w-$request->drivers_received_qty_70w;
            }
            if($request->drivers_received_qty_110w){
               $inwardchec->drivers_received_qty_110w= $inwardchec->drivers_received_qty_110w-$request->drivers_received_qty_110w;
            }
             if($request->drivers_received_qty_190w){                 
                $inwardchec->drivers_received_qty_190w=$inwardchec->drivers_received_qty_190w-$request->drivers_received_qty_190w;
            }




    if($request->num_spd_rcv_qty){
               $inwardchec->num_spd_rcv_qty= $inwardchec->num_spd_rcv_qty-$request->num_spd_rcv_qty;
            }
             if($request->spd_received_qty_18w){
               $inwardchec->spd_received_qty_18w= $inwardchec->spd_received_qty_18w-$request->spd_received_qty_18w;
            }

             if($request->spd_received_qty_35w){
               $inwardchec->spd_received_qty_35w= $inwardchec->spd_received_qty_35w-$request->spd_received_qty_35w;
            }
             if($request->spd_received_qty_70w){                 
                $inwardchec->spd_received_qty_70w=$inwardchec->spd_received_qty_70w-$request->spd_received_qty_70w;
            }
            if($request->spd_received_qty_110w){
               $inwardchec->spd_received_qty_110w= $inwardchec->spd_received_qty_110w-$request->spd_received_qty_110w;
            }
             if($request->spd_received_qty_190w){                 
                $inwardchec->spd_received_qty_190w=$inwardchec->spd_received_qty_190w-$request->spd_received_qty_190w;
            }
    // if($request->total_rcv_qty){                 
    //             $inwardchec->total_rcv_qty=$inwardchec->total_rcv_qty-$request->total_rcv_qty;
    //         }
    // if($request->tiltadap_rcv_qty){                 
    //             $inwardchec->tiltadap_rcv_qty=$inwardchec->tiltadap_rcv_qty-$request->tiltadap_rcv_qty;
    //         }
            
            //dd($inwardchec);
            $inwardchec->save();

            outward::create(['jilla_id'=>$request->jilla_id, 'maker_name'=>$request->maker_name,'vendor_id'=>$request->vendor_id,'invoice_number'=>$request->invoice_number,'date_of_receiving'=>$request->date_of_receiving,

                    'rcv_qty_18w'=>$request->rcv_qty_18w,
                    'rcv_qty_35w'=>$request->rcv_qty_35w,
                    'rcv_qty_70w'=>$request->rcv_qty_70w,
                    'rcv_qty_110w'=>$request->rcv_qty_110w,
                    'rcv_qty_190w'=>$request->rcv_qty_190w,

                      
                     // 'tiltadap_rcv_qty'=>$request->tiltadap_rcv_qty, 
                     
                     'bufstck_rcv_qty'=>$request->bufstck_rcv_qty, 
                     'bufferstck_received_qty_18w'=>$request->bufferstck_received_qty_18w,
                     'bufferstck_received_qty_35w'=>$request->bufferstck_received_qty_35w,
                     'bufferstck_received_qty_70w'=>$request->bufferstck_received_qty_70w, 
                     'bufferstck_received_qty_110w'=>$request->bufferstck_received_qty_110w,
                     'bufferstck_received_qty_190w'=>$request->bufferstck_received_qty_190w,

                     'num_drv_rcv_qty'=>$request->num_drv_rcv_qty,
                     'drivers_received_qty_18w'=>$request->drivers_received_qty_18w, 
                     'drivers_received_qty_35w'=>$request->drivers_received_qty_35w,
                     'drivers_received_qty_70w'=>$request->drivers_received_qty_70w,
                     'drivers_received_qty_110w'=>$request->drivers_received_qty_110w,
                     'drivers_received_qty_190w'=>$request->drivers_received_qty_190w, 

                     'num_spd_rcv_qty'=>$request->num_spd_rcv_qty,
                     'spd_received_qty_18w'=>$request->spd_received_qty_18w,
                     'spd_received_qty_35w'=>$request->spd_received_qty_35w,
                     'spd_received_qty_70w'=>$request->spd_received_qty_70w,
                     'spd_received_qty_110w'=>$request->spd_received_qty_110w,
                     'spd_received_qty_190w'=>$request->spd_received_qty_190w,
                     // 'total_rcv_qty'=>$request->total_rcv_qty,
                    'files'=>implode(',', $insert)]);

             return redirect(url('outwards_store'))->withSuccess('Sucessfully lights updated towards Outwards');


             }else{

            return redirect(url('outwards_store'))->with('error',"Given lights Quantity is more  to Outward");
        }   
   }

public function outwards_edit ($id)
    
    {
        $outward=outward::findOrFail($id);
        $jilla=DB::table('area_masters')
                    ->where('parent_id','=','0')
                    ->where('super_id','=','0')
                    ->where('status','=','active')
                    ->get();
    $makeslist= Makeslist::get();
        return view('backend.outwards.outwards_edit',compact('outward','jilla','makeslist'));
       }


        public function inward_update(Request $request, $id){

          $jilla=DB::table('area_masters')
                    ->where('parent_id','=','0')
                    ->where('super_id','=','0')
                    ->where('status','=','active')
                    ->get();
          $makeslist= Makeslist::get();

        $outward = outward::find($id);
        
        $outward->jilla_id = $request->get('jilla_id');
         $outward->maker_name = $request->get('maker_name');
         $outward->invoice_number = $request->get('invoice_number');
         $outward->tiltadap_rcv_qty = $request->get('tiltadap_rcv_qty');
          $outward->tiltable_adapters = $request->input('tiltable_adapters');

          $outward->led_wattage_18 = $request->get('led_wattage_18'); 
        $outward->led_wattage_35 = $request->get('led_wattage_35');
         $outward->led_wattage_70 = $request->get('led_wattage_70');  
         $outward->led_wattage_110 = $request->get('led_wattage_110'); 
        $outward->led_wattage_190 = $request->get('led_wattage_190');       
        $outward->invoice_date = $request->get('invoice_date');
        $outward->date_of_receiving = $request->get('date_of_receiving');

           $outward->rcv_qty_18w = $request->get('rcv_qty_18w'); 
        $outward->rcv_qty_35w = $request->get('rcv_qty_35w');
         $outward->rcv_qty_70w = $request->get('rcv_qty_70w');  
         $outward->rcv_qty_110w = $request->get('rcv_qty_110w'); 
        $outward->rcv_qty_190w = $request->get('rcv_qty_190w'); 


          $outward->bufferstck_received_qty_18w = $request->get('bufferstck_received_qty_18w');
           $outward->bufferstck_received_qty_35w = $request->get('bufferstck_received_qty_35w');
           $outward->bufferstck_received_qty_70w = $request->get('bufferstck_received_qty_70w');
        $outward->bufferstck_received_qty_110w = $request->get('bufferstck_received_qty_110w');
        $outward->bufferstck_received_qty_190w = $request->get('bufferstck_received_qty_190w');
           $outward->bufferstck_invoice_qty_18w = $request->get('bufferstck_invoice_qty_18w'); 
           $outward->bufferstck_invoice_qty_35w = $request->get('bufferstck_invoice_qty_35w');
           $outward->bufferstck_invoice_qty_70w = $request->get('bufferstck_invoice_qty_70w');
           $outward->bufferstck_invoice_qty_110w = $request->get('bufferstck_invoice_qty_110w');
           $outward->bufferstck_invoice_qty_190w = $request->get('bufferstck_invoice_qty_190w');

        $outward->drivers_received_qty_18w = $request->get('drivers_received_qty_18w');
           $outward->drivers_received_qty_35w = $request->get('drivers_received_qty_35w');
           $outward->drivers_received_qty_70w = $request->get('drivers_received_qty_70w');
        $outward->drivers_received_qty_110w = $request->get('drivers_received_qty_110w');
        $outward->drivers_received_qty_190w = $request->get('drivers_received_qty_190w');
           $outward->drivers_invoice_qty_18w = $request->get('drivers_invoice_qty_18w'); 
           $outward->drivers_invoice_qty_35w = $request->get('drivers_invoice_qty_35w');
           $outward->drivers_invoice_qty_70w = $request->get('drivers_invoice_qty_70w');
           $outward->drivers_invoice_qty_110w = $request->get('drivers_invoice_qty_110w');
           $outward->drivers_invoice_qty_190w = $request->get('drivers_invoice_qty_190w');


            $outward->spd_received_qty_18w = $request->get('spd_received_qty_18w');
           $outward->spd_received_qty_35w = $request->get('spd_received_qty_35w');
           $outward->spd_received_qty_70w = $request->get('spd_received_qty_70w');
        $outward->spd_received_qty_110w = $request->get('spd_received_qty_110w');
        $outward->spd_received_qty_190w = $request->get('spd_received_qty_190w');
           $outward->spd_invoice_qty_18w = $request->get('spd_invoice_qty_18w'); 
           $outward->spd_invoice_qty_35w = $request->get('spd_invoice_qty_35w');
           $outward->spd_invoice_qty_70w = $request->get('spd_invoice_qty_70w');
           $outward->spd_invoice_qty_110w = $request->get('spd_invoice_qty_110w');
           $outward->spd_invoice_qty_190w = $request->get('spd_invoice_qty_190w');
           $outward->remarks = $request->get('remarks');
        $insert=[];
          if($request->hasFile('files') )
         {
            $path=$request->file('files')->store('public');  
            $insert[]=$path;
         }else{
               if(!empty($request->input('files'))){
            $name="pdf".rand(11111111, 99999999).".pdf"; 
            Storage::disk('public')->put($name,base64_decode($request->input('files')));
            $insert[]="public/".$name;
         }
         
         }
         //dd($insert);
         if(!empty($insert)){
        $inward->files=implode(',', $insert);
      }
        $inward->save();    
     return redirect(url('/inwards_list').'?id='.$inward->invoice_number.'&&jilla_id='.$inward->jilla_id)->with('complted');
}

public function outwards_armclampsvendor_list(Request $request)
     {
  //       $vendorslists=gramuser::where('role',6)->where(function ($query) use ($request) {
  //     if($request->session()->get('role')==6){

  //      $query->where('id',$request->session()->get('id'));
  //     }
  // })->get();

         DB::enableQueryLog();
       $area_dis=outward_armsclamps::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->where('jilla_id',$request->id)->select(DB::raw("count(invoice_number) as count"),DB::raw("SUM(received_arms_dia_25) as arms_25"),DB::raw("SUM(received_arms_dia_inner_32) as arms_32"),DB::raw("SUM(received_arms_dia_inner_40) as arms_40"),DB::raw("SUM(received_clams_set_25) as clamps_25"),DB::raw("SUM(received_clams_set_40_2) as clamps_42"),DB::raw("SUM(received_clams_set_40_3) as clamps_43"),DB::raw("SUM(received_aluminium_cable) as rcv_cbl"),'jilla_id','invoice_number','jilla_id','vendor_id')->groupby('invoice_number','vendor_id')->get()->toArray();
    //dd($area_dis);
    // ->has('vendor_name')->with(['vendor_name'=> function ($query) {
    //     $query->select('id', 'name');
    //        }])-
    //     ->has('vendor_name')->with(['vendor_name'=> function ($query) {
    //     $query->select('id', 'name');
    // }])-
       DB::enableQueryLog();
       $balance_view=armsclamps_vendor::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->where('jilla_id',$request->id)->select(DB::raw("count(invoice_number) as count"),DB::raw("SUM(received_arms_dia_25) as arms_25"),DB::raw("SUM(received_arms_dia_inner_32) as arms_32"),DB::raw("SUM(received_arms_dia_inner_40) as arms_40"),DB::raw("SUM(received_clams_set_25) as clamps_25"),DB::raw("SUM(received_clams_set_40_2) as clamps_42"),DB::raw("SUM(received_clams_set_40_3) as clamps_43"),DB::raw("SUM(received_aluminium_cable) as rcv_cbl"),'jilla_id','invoice_number','jilla_id','vendor_id')->groupby('invoice_number','vendor_id')->get()->toArray();
        DB::enableQueryLog();

       $area_dis_supply=outward_armsclamps::where('jilla_id',$request->id)->where('approve_status',1)->orWhere('approve_status',0)->select(DB::raw("count(invoice_number) as count"),DB::raw("SUM(received_arms_dia_25) as arms_25"),DB::raw("SUM(received_arms_dia_inner_32) as arms_32"),DB::raw("SUM(received_arms_dia_inner_40) as arms_40"),DB::raw("SUM(received_clams_set_25) as clamps_25"),DB::raw("SUM(received_clams_set_40_2) as clamps_42"),DB::raw("SUM(received_clams_set_40_3) as clamps_43"),DB::raw("SUM(received_aluminium_cable) as rcv_cbl"),'approve_status','remarks','suplied_by')->groupby('suplied_by')->get()->toArray();
      // dd($area_dis_supply);


   
        $district  = area_master::where('id',$request->id)->first();
       return view('backend.outwards.outwards_armclampsvendor_list',compact('area_dis','district','balance_view','area_dis_supply'));
    }

public function outwards_armsclampsview(Request $request)
{


     DB::enableQueryLog();
       $area_dis=outward_armsclamps::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->whereIn('jilla_id',$request->session()->get('districts'))->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(received_arms_dia_25) as arms_25"),DB::raw("SUM(received_arms_dia_inner_32) as arms_32"),DB::raw("SUM(received_arms_dia_inner_40) as arms_40"),DB::raw("SUM(received_clams_set_25) as clamps_25"),DB::raw("SUM(received_clams_set_40_2) as clamps_42"),DB::raw("SUM(received_clams_set_40_3) as clamps_43"),DB::raw("SUM(received_aluminium_cable) as rcv_cbl"),'jilla_id')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->groupby('jilla_id')->get()->toArray();


     DB::enableQueryLog();
       $balance_view=armsclamps_vendor::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->whereIn('jilla_id',$request->session()->get('districts'))->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(received_arms_dia_25) as arms_25"),DB::raw("SUM(received_arms_dia_inner_32) as arms_32"),DB::raw("SUM(received_arms_dia_inner_40) as arms_40"),DB::raw("SUM(received_clams_set_25) as clamps_25"),DB::raw("SUM(received_clams_set_40_2) as clamps_42"),DB::raw("SUM(received_clams_set_40_3) as clamps_43"),DB::raw("SUM(received_aluminium_cable) as rcv_cbl"),'jilla_id')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->groupby('jilla_id')->get()->toArray();

     DB::enableQueryLog();
       $area_dis_supply=outward_armsclamps::whereIn('jilla_id',$request->session()->get('districts'))->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(received_arms_dia_25) as arms_25"),DB::raw("SUM(received_arms_dia_inner_32) as arms_32"),DB::raw("SUM(received_arms_dia_inner_40) as arms_40"),DB::raw("SUM(received_clams_set_25) as clamps_25"),DB::raw("SUM(received_clams_set_40_2) as clamps_42"),DB::raw("SUM(received_clams_set_40_3) as clamps_43"),DB::raw("SUM(received_aluminium_cable) as rcv_cbl"),'suplied_by')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->groupby('suplied_by')->get()->toArray();
       //dd( $area_dis);

    // ->has('vendor_name')->with(['vendor_name'=> function ($query) {
    //     $query->select('id', 'name');
    // }])->


    //    ->where(function($q) use ($request) {
    //      if($request->session()->get('role')==6){
    //         $q->where('vendor_id',$request->session()->get('id'));
    //      }
    // })

     return view('backend.outwards.outwards_armsclampsview',compact('area_dis','balance_view','area_dis_supply'));

}
//->where('vendor_id',$request->id)
  public function admin_outwards_armsclamps(Request $request)
  {

  $vendorslists=gramuser::where('role',6)->where(function ($query) use ($request) {
      if($request->session()->get('role')==6){

       $query->where('id',$request->session()->get('id'));
      }
  })->get();
  //dd($vendorslists);
   $jilla=DB::table('area_masters')
                    ->where('parent_id','=','0')
                    ->where('super_id','=','0')
                    ->where('status','=','active')
                    ->get();
    $makeslist= Makeslist::get();
     $invoicelists=DB::table('armsclamps')->where('invoice_number',$request->get('jilla_id'))
       ->get();

     return view('backend.outwards.admin_outwards_armsclamps',compact('jilla','makeslist','vendorslists','invoicelists'));
   } 
   public function admin_outwards_armsclamps_store(Request $request){

     $inwardchec = armsclamps::where(['jilla_id'=>$request->jilla_id,'invoice_number'=>$request->invoice_number])->where(function($q) use ($request){
            
              if($request->received_arms_dia_25){
                $q->where('received_arms_dia_25',">=",$request->received_arms_dia_25);
            }
            if($request->received_arms_dia_inner_32){
                $q->where('received_arms_dia_inner_32',">=",$request->received_arms_dia_inner_32);
            }
              
            if($request->received_arms_dia_inner_40){
                $q->where('received_arms_dia_inner_40',">=",$request->received_arms_dia_inner_40);
            }
            if($request->received_clams_set_25){
                $q->where('received_clams_set_25',">=",$request->received_clams_set_25);
            }
            if($request->received_clams_set_40_2){
                $q->where('received_clams_set_40_2',">=",$request->received_clams_set_40_2);
            }
            if($request->received_clams_set_40_3){
                $q->where('received_clams_set_40_3',">=",$request->received_clams_set_40_3);
            }
            if($request->received_aluminium_cable){
                $q->where('received_aluminium_cable',">=",$request->received_aluminium_cable);
            }
       
        })->first();

     //dd($inwardchec);

        //$check=inward::get();
       //dd( $inwardchec);

        if(!empty($inwardchec)){
             $inwardchec->jilla_id = $inwardchec->jilla_id;
             $insert=[];
          if($request->hasFile('files') )
         {
            $path=$request->file('files')->store('public');  
            $insert[]=$path;
         }else{
            $name="pdf".rand(11111111, 99999999).".pdf"; 
            Storage::disk('public')->put($name,base64_decode($request->input('files')));
            $insert[]="public/".$name;
         }


            if($request->received_arms_dia_25){                 
                $inwardchec->received_arms_dia_25=$inwardchec->received_arms_dia_25-$request->received_arms_dia_25;
            }
            if($request->received_arms_dia_inner_32){
               $inwardchec->received_arms_dia_inner_32= $inwardchec->received_arms_dia_inner_32-$request->received_arms_dia_inner_32;
            }
             if($request->received_arms_dia_inner_40){                 
                $inwardchec->received_arms_dia_inner_40=$inwardchec->received_arms_dia_inner_40-$request->received_arms_dia_inner_40;
            }
            if($request->received_clams_set_25){
               $inwardchec->received_clams_set_25= $inwardchec->received_clams_set_25-$request->received_clams_set_25;
            }
             if($request->received_clams_set_40_2){                 
                $inwardchec->received_clams_set_40_2=$inwardchec->received_clams_set_40_2-$request->received_clams_set_40_2;
            }
            if($request->received_clams_set_40_3){
               $inwardchec->received_clams_set_40_3= $inwardchec->received_clams_set_40_3-$request->received_clams_set_40_3;
            }
            if($request->received_aluminium_cable){
               $inwardchec->received_aluminium_cable= $inwardchec->received_aluminium_cable-$request->received_aluminium_cable;
            }

            
            
            //dd($inwardchec);
            $inwardchec->save();

            outward_armsclamps::create(['jilla_id'=>$request->jilla_id,'vendor_id'=>$request->vendor_id,'invoice_number'=>$request->invoice_number,'date_of_receiving'=>$request->date_of_receiving,

                    'received_arms_dia_25'=>$request->received_arms_dia_25,
                    'received_arms_dia_inner_32'=>$request->received_arms_dia_inner_32,
                    'received_arms_dia_inner_40'=>$request->received_arms_dia_inner_40,
                    'received_clams_set_25'=>$request->received_clams_set_25,
                    'received_clams_set_40_2'=>$request->received_clams_set_40_2, 
                    'received_clams_set_40_3'=>$request->received_clams_set_40_3, 
                    'received_aluminium_cable'=>$request->received_aluminium_cable, 
                    'files'=>implode(',', $insert)]);

             return redirect(url('admin_outwards_armsclamps'))->withSuccess('Sucessfully armsclamps updated towards Outwards');
             }else{

            return redirect(url('admin_outwards_armsclamps'))->with('error',"Given lights Quantity is more  to Outward");
        }   
   }

   public function outwards_ccmsvendor_list(Request $request)
     {
  //        $vendorslists=gramuser::where('role',6)->where(function ($query) use ($request) {
  //     if($request->session()->get('role')==6){

  //      $query->where('id',$request->session()->get('id'));
  //     }
  // })->get();

    DB::enableQueryLog();
       $area_dis=outward_ccms::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->where('jilla_id',$request->id)->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(ccms_ph_receive_quantity) as ccmsph"),DB::raw("SUM(received_cable) as rcv_cbl"),'invoice_number','jilla_id','vendor_id')->groupby('invoice_number','vendor_id')->get()->toArray();

      DB::enableQueryLog();
       $balance_view=ccms_vendor::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->where('jilla_id',$request->id)->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(ccms_ph_receive_quantity) as ccmsph"),DB::raw("SUM(received_cable) as rcv_cbl"),'invoice_number','jilla_id')->groupby('invoice_number')->where(function($q) use ($request){        
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);   
    })->get()->toArray();

        $district  = area_master::where('id',$request->id)->first();
       return view('backend.outwards.outwards_ccmsvendor_list',compact('district','area_dis','balance_view'));
    }

      public function outwards_ccmsview(Request $request)
{

     DB::enableQueryLog();
       $area_dis=outward_ccms::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->whereIn('jilla_id',$request->session()->get('districts'))->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(ccms_ph_receive_quantity) as ccmsph"),DB::raw("SUM(received_cable) as rcv_cbl"),'jilla_id')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->groupby('jilla_id')->get()->toArray();
       //dd( $area_dis);

       DB::enableQueryLog();
       $balance_view=ccms_vendor::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->whereIn('jilla_id',$request->session()->get('districts'))->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(ccms_ph_receive_quantity) as ccmsph"),DB::raw("SUM(received_cable) as rcv_cbl"),'jilla_id')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->groupby('jilla_id')->get()->toArray();

     return view('backend.outwards.outwards_ccmsview',compact('area_dis','balance_view'));

}
   public function admin_outwards_ccms(Request $request){

   $vendorslists=gramuser::where('role',6)->get();
   $jilla=DB::table('area_masters')
                    ->where('parent_id','=','0')
                    ->where('super_id','=','0')
                    ->whereIn('id',$request->session()->get('districts'))
                    ->where('status','=','active')
                    ->get();
    $makeslist= Makeslist::get();
    $invoicelists=DB::table('inwardccmstypes')->where('invoice_number',$request->get('jilla_id'))
       ->get();



     return view('backend.outwards.admin_outwards_ccms',compact('jilla','makeslist','vendorslists','invoicelists'));
   }

   public function outwards_ccmstypes_list(Request $request)
{
     

    $ccms_info= outward_ccms::where('invoice_number',$request->id)->where('jilla_id',$request->jilla_id)->with('area_district')->get();
   
                if ($request->is("api*")) {
                        return json_encode($ccms_info);
                    }
    return view('backend.outwards.outwards_ccmstypes_list',compact('ccms_info'));
}

   public function admin_outwards_ccms_store(Request $request){

     $inwardchec = inwardccmstypes::where(['jilla_id'=>$request->jilla_id])->where(function($q) use ($request){
            
              if($request->ccms_ph_receive_quantity){
                $q->where('ccms_ph_receive_quantity',">=",$request->ccms_ph_receive_quantity);
            }
       
        })->first();

     //dd($inwardchec);

        //$check=inward::get();
       //dd( $inwardchec);

        if(!empty($inwardchec)){
             $inwardchec->jilla_id = $inwardchec->jilla_id;
             // $inwardchec->maker_name = $inwardchec->maker_name;
             $insert=[];
          if($request->hasFile('files') )
         {
            $path=$request->file('files')->store('public');  
            $insert[]=$path;
         }else{
            $name="pdf".rand(11111111, 99999999).".pdf"; 
            Storage::disk('public')->put($name,base64_decode($request->input('files')));
            $insert[]="public/".$name;
         }


            if($request->ccms_ph_receive_quantity){                 
                $inwardchec->ccms_ph_receive_quantity=$inwardchec->ccms_ph_receive_quantity-$request->ccms_ph_receive_quantity;
            }
       
            //dd($inwardchec);
            $inwardchec->save();

            outward_ccms::create(['jilla_id'=>$request->jilla_id,'date_of_receiving'=>$request->date_of_receiving,'remarks'=>$request->remarks,'received_cable'=>$request->received_cable,'vendor_id'=>$request->vendor_id,'invoice_number'=>$request->invoice_number,'phase_name'=>$request->phase_name,

                    'ccms_ph_receive_quantity'=>$request->ccms_ph_receive_quantity,
                    'files'=>implode(',', $insert)]);

             return redirect(url('admin_outwards_ccms'))->withSuccess('Sucessfully ccms updated towards Outwards');
             }else{

            return redirect(url('admin_outwards_ccms'))->with('error',"Given lights Quantity is more  to Outward");
        }   
   }

   public function armsclamps_store_check(Request $request){

     $countstore = armsclamps::where(['jilla_id'=>$request->jilla_id,'invoice_number'=>$request->invoice_number])->select(DB::raw("SUM(received_arms_dia_25) as a25d"),DB::raw("SUM(received_arms_dia_inner_32) as a32d"),DB::raw("SUM(received_arms_dia_inner_40) as a40d"),DB::raw("SUM(received_clams_set_25) as c25s"),DB::raw("SUM(received_clams_set_40_2) as c402s"),DB::raw("SUM(received_clams_set_40_3) as c403s"),DB::raw("SUM(received_aluminium_cable) as cbl"))->first()->toArray();
     //dd($countstore);
       // |regex:[a-zA-Z\s]
      return response()->json($countstore);
   }

public function ccms_store_check(Request $request){

     $countstore = inwardccmstypes::where(['jilla_id'=>$request->jilla_id,'invoice_number'=>$request->invoice_number])->select(DB::raw("SUM(ccms_ph_receive_quantity) as rccms1w"))->first()->toArray();

     //dd($countstore);
      return response()->json($countstore);
    

   }

//  VENDOR OUTWARDS STARTING FROM HERE--


public function  subvendor_ccms_list(Request $request){
   
         DB::enableQueryLog();
       $area_dis=subvendor_ccms::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->with(['subvendor_name'=> function ($query) {
        $query->select('id', 'name');
    }])->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(ccms_ph_receive_quantity) as ccmsph"),DB::raw("SUM(received_cable) as rcv_cbl"),'jilla_id')->groupby('jilla_id')->get()->toArray();
      

        $district  = area_master::where('id',$request->id)->first();
       return view('backend.outwards.subvendor_ccms_list',compact('district','area_dis'));
    }

public function subvendor_outward_ccms_list(Request $request){

     DB::enableQueryLog();
       $area_dis=subvendor_ccms::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->has('subvendor_name')->with(['subvendor_name'=> function ($query) {
        $query->select('id', 'name');
    }])->where('jilla_id',$request->id)->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(ccms_ph_receive_quantity) as ccmsph"),DB::raw("SUM(received_cable) as rcv_cbl"),'jilla_id','subvendor_id')->groupby('jilla_id','subvendor_id')->get()->toArray();
       //dd( $area_dis);
     return view('backend.outwards.vendor_outward_ccms_list',compact('area_dis'));
    }


public function vendor_outwards_ccms(Request $request){

   $subvendorslists=gramuser::where('role',7)->get();
   $jilla=DB::table('area_masters')
                    ->where('parent_id','=','0')
                    ->where('super_id','=','0')
                    ->whereIn('id',$request->session()->get('districts'))
                    ->where('status','=','active')
                    ->get();
           $taluka=DB::table('area_masters')
                    ->where('parent_id','!=','0')
                    ->where('super_id','=','0')
                    ->where('status','=','active')  
                    ->where('parent_id',$request->get('jilla_id'))
                    // ->whereIn('id',$request->session()->get('mandals'))
                    ->get();               
    $makeslist= Makeslist::get();

     return view('backend.outwards.vendor_outwards_ccms',compact('jilla','makeslist','subvendorslists','taluka'));
    }
public function vendor_outwards_ccms_store(Request $request){


$inwardchecks = ccms_vendor::where(['jilla_id'=>$request->jilla_id])->get();

 $ccmsph=$request->ccms_ph_receive_quantity; 
     foreach ($inwardchecks as $inwardchec) 
    {
        if($ccmsph==0){
            break;
        }

        if($inwardchec->ccms_ph_receive_quantity <= $ccmsph){
            $insert_ccmsph =$inwardchec->ccms_ph_receive_quantity;
             //dd($insert_ccmsph);3000
             $inwardchec->ccms_ph_receive_quantity=$inwardchec->ccms_ph_receive_quantity-$inwardchec->ccms_ph_receive_quantity;
             //dd($inwardchec->ccms_ph_receive_quantity);0
             $ccmsph=$ccmsph-$insert_ccmsph;
             //dd($ccmsph);50
            }elseif($inwardchec->ccms_ph_receive_quantity >= $ccmsph){
                $inwardchec->ccms_ph_receive_quantity=$inwardchec->ccms_ph_receive_quantity-$ccmsph;
                //dd($inwardchec->ccms_ph_receive_quantity);48
                $insert_ccmsph =$ccmsph;
                //dd($insert_ccmsph);
                $ccmsph=0;
            }

        $insert=[];
          if($request->hasFile('files') )
         {
            $path=$request->file('files')->store('public');  
            $insert[]=$path;
         }else{
            $name="pdf".rand(11111111, 99999999).".pdf"; 
            Storage::disk('public')->put($name,base64_decode($request->input('files')));
            $insert[]="public/".$name;
         }         
         $inwardchec->save();
         
          $from_jilla=$request->jilla_id;
          $subvendor_id=$request->subvendor_id;
          $fromtring = area_master::where('id',$request->jilla_id)->first();
          $frominvoice=$inwardchec->invoice_number;
          $toinvoice=$fromtring->area_name."-to-".$subvendor_id."-".time();

           subvendor_ccms_invoices::create(['from_jilla_id'=>$from_jilla,'from_invoice_number'=>$frominvoice,'to_invoice_number'=>$toinvoice,'to_subvendor_id'=>$subvendor_id,'ccms_id'=>$inwardchec->id,'status'=>1,"date_of_transfer"=>date('Y-m-d',strtotime($request->date_of_receiving)),"remarks"=>$request->remarks,
            'ccms_ph_receive_quantity'=>$insert_ccmsph,
            'files'=>implode(',', $insert)]);

            }


 $insert=[];
          if($request->hasFile('files') )
         {
            $path=$request->file('files')->store('public');  
            $insert[]=$path;
         }else{
            $name="pdf".rand(11111111, 99999999).".pdf"; 
            Storage::disk('public')->put($name,base64_decode($request->input('files')));
            $insert[]="public/".$name;
         }   

          $from_jilla=$request->jilla_id;
          $subvendor_id=$request->subvendor_id;
          $fromtring = area_master::where('id',$request->jilla_id)->first();
          $frominvoice=$inwardchec->invoice_number;
          $toinvoice=$fromtring->area_name."-to-".$subvendor_id."-".time();

          subvendor_ccms::create(['jilla_id'=>$request->jilla_id,'subvendor_id'=>$request->subvendor_id,'invoice_number'=>$toinvoice,'date_of_receiving'=>$request->date_of_receiving,'mandal_id'=>$request->mandal_id,'ccms_ph_receive_quantity'=>$request->ccms_ph_receive_quantity,
            'files'=>implode(',', $insert)]);


    

             return redirect(url('vendor_outwards_ccms'))->withSuccess('Sucessfully ccms Quantity updated towards subvendor');
             } 
   


// --  Arms clamps alloting to subvendor -- 


   public function vendor_outward_armsclamps_list(Request $request)
     {
      
        // $area_invoice=subvendor_armsclamps::with(['subvendor_name'])->groupby('subvendor_id')->get()->toArray();
       DB::enableQueryLog();
       $area_dis=subvendor_armsclamps::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->with(['subvendor_name'=> function ($query) {
        $query->select('id', 'name');
    }])->whereIn('jilla_id',$request->session()->get('districts'))->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(received_arms_dia_25) as arms_25"),DB::raw("SUM(received_arms_dia_inner_32) as arms_32"),DB::raw("SUM(received_arms_dia_inner_40) as arms_40"),DB::raw("SUM(received_clams_set_25) as clamps_25"),DB::raw("SUM(received_clams_set_40_2) as clamps_42"),DB::raw("SUM(received_clams_set_40_3) as clamps_43"),DB::raw("SUM(received_aluminium_cable) as rcv_cbl"),'jilla_id')->groupby('jilla_id')->get()->toArray();
    DB::enableQueryLog();
       $area_dis_supply=subvendor_armsclamps::whereIn('jilla_id',$request->session()->get('districts'))->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(received_arms_dia_25) as arms_25"),DB::raw("SUM(received_arms_dia_inner_32) as arms_32"),DB::raw("SUM(received_arms_dia_inner_40) as arms_40"),DB::raw("SUM(received_clams_set_25) as clamps_25"),DB::raw("SUM(received_clams_set_40_2) as clamps_42"),DB::raw("SUM(received_clams_set_40_3) as clamps_43"),DB::raw("SUM(received_aluminium_cable) as rcv_cbl"),'suplied_by')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->groupby('suplied_by')->get()->toArray();


        $district  = area_master::where('id',$request->id)->first();
       return view('backend.outwards.vendor_outward_armsclamps_list ',compact('area_dis','district','area_dis_supply'));
    }

public function subvendor_armsclamps_list(Request $request)
{

     DB::enableQueryLog();
       $area_dis=subvendor_armsclamps::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->has('subvendor_name')->with(['subvendor_name'=> function ($query) {
        $query->select('id', 'name');
    }])->where('jilla_id',$request->id)->whereIn('jilla_id',$request->session()->get('districts'))->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(received_arms_dia_25) as arms_25"),DB::raw("SUM(received_arms_dia_inner_32) as arms_32"),DB::raw("SUM(received_arms_dia_inner_40) as arms_40"),DB::raw("SUM(received_clams_set_25) as clamps_25"),DB::raw("SUM(received_clams_set_40_2) as clamps_42"),DB::raw("SUM(received_clams_set_40_3) as clamps_43"),DB::raw("SUM(received_aluminium_cable) as rcv_cbl"),'jilla_id','subvendor_id')->groupby('jilla_id','subvendor_id')->get()->toArray();
       //dd( $area_dis);

      DB::enableQueryLog();
       $area_dis_supply=subvendor_armsclamps::where('jilla_id',$request->id)->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(received_arms_dia_25) as arms_25"),DB::raw("SUM(received_arms_dia_inner_32) as arms_32"),DB::raw("SUM(received_arms_dia_inner_40) as arms_40"),DB::raw("SUM(received_clams_set_25) as clamps_25"),DB::raw("SUM(received_clams_set_40_2) as clamps_42"),DB::raw("SUM(received_clams_set_40_3) as clamps_43"),DB::raw("SUM(received_aluminium_cable) as rcv_cbl"),'suplied_by')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->groupby('suplied_by')->get()->toArray();
     return view('backend.outwards.subvendor_armsclamps_list',compact('area_dis','area_dis_supply'));

}

  public function vendor_outwards_armsclamps(Request $request)
  {
 $subvendorslists=gramuser::where('role',7)->get();
  //dd($vendorslists);
   $jilla=DB::table('area_masters')
                    ->where('parent_id','=','0')
                    ->where('super_id','=','0')
                     ->whereIn('id',$request->session()->get('districts'))
                    ->where('status','=','active')
                    ->get();


        $taluka=DB::table('area_masters')
                    ->where('parent_id','!=','0')
                    ->where('super_id','=','0')
                    ->where('status','=','active')  
                    ->where('parent_id',$request->get('jilla_id'))
                    // ->whereIn('id',$request->session()->get('mandals'))
                    ->get();
    $makeslist= Makeslist::get();
    $suplied_by_lists=armsclamps_vendor::groupby('suplied_by')->pluck('suplied_by');
    //dd($suplied_by_lists);

     return view('backend.outwards.vendor_outwards_armsclamps',compact('jilla','makeslist','subvendorslists','taluka','suplied_by_lists'));
   } 
   public function vendor_outwards_armsclamps_store(Request $request){
$inwardchecks = armsclamps_vendor::where(['jilla_id'=>$request->jilla_id,"suplied_by"=>$request->suplied_by])->get();
 
    
      $arms25m=$request->received_arms_dia_25; 
      $arms32m=$request->received_arms_dia_inner_32; 
      $arms40m=$request->received_arms_dia_inner_40;
      $clamps25m=$request->received_clams_set_25; 
      $clamps42m=$request->received_clams_set_40_2;
      $clamps43m=$request->received_clams_set_40_3;
      $rcv_cbl=$request->received_aluminium_cable;

       foreach ($inwardchecks as $inwardchec) 
    {
        if($arms25m==0&&$arms32m==0&&$arms40m==0&&$clamps25m==0&&$clamps42m==0&&$clamps43m==0&&$rcv_cbl==0){
            break;
        }

         if($inwardchec->received_arms_dia_25 <= $arms25m){
            $insert_25m =$inwardchec->received_arms_dia_25;
             //dd($insert_25m);3000
             $inwardchec->received_arms_dia_25=$inwardchec->received_arms_dia_25-$inwardchec->received_arms_dia_25;
             //dd($inwardchec->received_arms_dia_25);0
             $arms25m=$arms25m-$insert_25m;
             //dd($arms25m);50
            }elseif($inwardchec->received_arms_dia_25 >= $arms25m){
                $inwardchec->received_arms_dia_25=$inwardchec->received_arms_dia_25-$arms25m;
                //dd($inwardchec->received_arms_dia_25);48
                $insert_25m =$arms25m;
                //dd($insert_25m);
                $arms25m=0;
            }
          //echo $arms25m." , ".$insert_25m." , ".$inwardchec->received_arms_dia_25; echo "</br>";
            if($inwardchec->received_arms_dia_inner_32 <= $arms32m){
            $insert_32m =$inwardchec->received_arms_dia_inner_32;
             //dd($insert_32m);3000
             $inwardchec->received_arms_dia_inner_32=$inwardchec->received_arms_dia_inner_32-$inwardchec->received_arms_dia_inner_32;
             //dd($inwardchec->received_arms_dia_inner_32);0
             $arms32m=$arms32m-$insert_32m;
             //dd($arms32m);50
            }elseif($inwardchec->received_arms_dia_inner_32 >= $arms32m){
                $inwardchec->received_arms_dia_inner_32=$inwardchec->received_arms_dia_inner_32-$arms32m;
                //dd($inwardchec->received_arms_dia_inner_32);48
                $insert_32m =$arms32m;
                //dd($insert_32m);
                $arms32m=0;
            }
            if($inwardchec->received_arms_dia_inner_40 <= $arms40m){
            $insert_40m =$inwardchec->received_arms_dia_inner_40;
             //dd($insert_40m);3000
             $inwardchec->received_arms_dia_inner_40=$inwardchec->received_arms_dia_inner_40-$inwardchec->received_arms_dia_inner_40;
             //dd($inwardchec->received_arms_dia_inner_40);0
             $arms40m=$arms40m-$insert_40m;
             //dd($arms40m);50
            }elseif($inwardchec->received_arms_dia_inner_40 >= $arms40m){
                $inwardchec->received_arms_dia_inner_40=$inwardchec->received_arms_dia_inner_40-$arms40m;
                //dd($inwardchec->received_arms_dia_inner_40);48
                $insert_40m =$arms40m;
                //dd($insert_40m);
                $arms40m=0;
            }
            if($inwardchec->received_clams_set_25 <= $clamps25m){
            $insert_c25m =$inwardchec->received_clams_set_25;
             //dd($insert_c25m);3000
             $inwardchec->received_clams_set_25=$inwardchec->received_clams_set_25-$inwardchec->received_clams_set_25;
             //dd($inwardchec->received_clams_set_25);0
             $clamps25m=$clamps25m-$insert_c25m;
             //dd($clamps25m);50
            }elseif($inwardchec->received_clams_set_25 >= $clamps25m){
                $inwardchec->received_clams_set_25=$inwardchec->received_clams_set_25-$clamps25m;
                //dd($inwardchec->received_clams_set_25);48
                $insert_c25m =$clamps25m;
                //dd($insert_c25m);
                $clamps25m=0;
            }
              if($inwardchec->received_clams_set_40_2 <= $clamps42m){
            $insert_c42m =$inwardchec->received_clams_set_40_2;
             //dd($insert_c42m);3000
             $inwardchec->received_clams_set_40_2=$inwardchec->received_clams_set_40_2-$inwardchec->received_clams_set_40_2;
             //dd($inwardchec->received_clams_set_40_2);0
             $clamps42m=$clamps42m-$insert_c42m;
             //dd($clamps42m);50
            }elseif($inwardchec->received_clams_set_40_2 >= $clamps42m){
                $inwardchec->received_clams_set_40_2=$inwardchec->received_clams_set_40_2-$clamps42m;
                //dd($inwardchec->received_clams_set_40_2);48
                $insert_c42m =$clamps42m;
                //dd($insert_c42m);
                $clamps42m=0;
            }
             if($inwardchec->received_clams_set_40_3 <= $clamps43m){
            $insert_c43m =$inwardchec->received_clams_set_40_3;
             //dd($insert_c43m);3000
             $inwardchec->received_clams_set_40_3=$inwardchec->received_clams_set_40_3-$inwardchec->received_clams_set_40_3;
             //dd($inwardchec->received_clams_set_40_3);0
             $clamps43m=$clamps43m-$insert_c43m;
             //dd($clamps43m);50
            }elseif($inwardchec->received_clams_set_40_3 >= $clamps43m){
                $inwardchec->received_clams_set_40_3=$inwardchec->received_clams_set_40_3-$clamps43m;
                //dd($inwardchec->received_clams_set_40_3);48
                $insert_c43m =$clamps43m;
                //dd($insert_c43m);
                $clamps43m=0;
            }

             if($inwardchec->received_aluminium_cable <= $rcv_cbl){
            $insert_rcvcbl =$inwardchec->received_aluminium_cable;
             //dd($insert_rcvcbl);3000
             $inwardchec->received_aluminium_cable=$inwardchec->received_aluminium_cable-$inwardchec->received_aluminium_cable;
             //dd($inwardchec->received_aluminium_cable);0
             $rcv_cbl=$rcv_cbl-$insert_rcvcbl;
             //dd($rcv_cbl);50
            }elseif($inwardchec->received_aluminium_cable >= $rcv_cbl){
                $inwardchec->received_aluminium_cable=$inwardchec->received_aluminium_cable-$rcv_cbl;
                //dd($inwardchec->received_aluminium_cable);48
                $insert_rcvcbl =$rcv_cbl;
                //dd($insert_rcvcbl);
                $rcv_cbl=0;
            }

             $insert=[];
          if($request->hasFile('files') )
         {
            $path=$request->file('files')->store('public');  
            $insert[]=$path;
         }else{
            $name="pdf".rand(11111111, 99999999).".pdf"; 
            Storage::disk('public')->put($name,base64_decode($request->input('files')));
            $insert[]="public/".$name;
         }         
         $inwardchec->save();
         
 $from_jilla=$request->jilla_id;
          $subvendor_id=$request->subvendor_id;
          $fromtring = area_master::where('id',$request->jilla_id)->first();
          $frominvoice=$inwardchec->invoice_number;
          $toinvoice=$fromtring->area_name."-to-".$subvendor_id."-".time();

         subvendor_armsclamps_invoices::create(['from_jilla_id'=>$from_jilla, 'suplied_by'=>$request->suplied_by, 'from_invoice_number'=>$frominvoice,'to_invoice_number'=>$toinvoice,'to_subvendor_id'=>$subvendor_id,'arms_id'=>$inwardchec->id,'status'=>1,"date_of_transfer"=>date('Y-m-d',strtotime($request->date_of_receiving)),"remarks"=>$request->remarks,

          'received_arms_dia_25'=>$insert_25m,
                    'received_arms_dia_inner_32'=>$insert_32m,
                    'received_arms_dia_inner_40'=>$insert_40m,
                    'received_clams_set_25'=> $insert_c25m,
                    'received_clams_set_40_2'=> $insert_c42m, 
                    'received_clams_set_40_3'=> $insert_c43m, 
                    'received_aluminium_cable'=>$rcv_cbl, 
                    'files'=>implode(',', $insert)]);
    }


 $insert=[];
          if($request->hasFile('files') )
         {
            $path=$request->file('files')->store('public');  
            $insert[]=$path;
         }else{
            $name="pdf".rand(11111111, 99999999).".pdf"; 
            Storage::disk('public')->put($name,base64_decode($request->input('files')));
            $insert[]="public/".$name;
         }   

          $from_jilla=$request->jilla_id;
          $subvendor_id=$request->subvendor_id;
          $fromtring = area_master::where('id',$request->jilla_id)->first();
          $frominvoice=$inwardchec->invoice_number;
          $toinvoice=$fromtring->area_name."-to-".$subvendor_id."-".time();

            subvendor_armsclamps::create(['jilla_id'=>$request->jilla_id,'subvendor_id'=>$request->subvendor_id,'invoice_number'=>$toinvoice,'date_of_receiving'=>$request->date_of_receiving,'mandal_id'=>$request->mandal_id,'suplied_by'=>$request->suplied_by,

                    'received_arms_dia_25'=>$request->received_arms_dia_25,
                    'received_arms_dia_inner_32'=>$request->received_arms_dia_inner_32,
                    'received_arms_dia_inner_40'=>$request->received_arms_dia_inner_40,
                    'received_clams_set_25'=>$request->received_clams_set_25,
                    'received_clams_set_40_2'=>$request->received_clams_set_40_2, 
                    'received_clams_set_40_3'=>$request->received_clams_set_40_3, 
                    'received_aluminium_cable'=>$request->received_aluminium_cable, 
                    'files'=>implode(',', $insert)]);

             return redirect(url('vendor_outwards_armsclamps'))->withSuccess('Sucessfully armsclamps updated towards Outwards');
            
   }



   // --  Lights alloting to subvendor -- 


   public function vendor_outward_lights_list(Request $request)
     {
      
        // $area_invoice=subvendor_lights::with(['subvendor_name'])->groupby('subvendor_id')->get()->toArray();

        DB::enableQueryLog();
       $area_dis=subvendor_lights::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->with(['subvendor_name'=> function ($query) {
        $query->select('id', 'name');
    }])->whereIn('jilla_id',$request->session()->get('districts'))->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),'jilla_id')
    ->groupby('jilla_id')->get()->toArray();


    $area_dis_withmaker = array();
    $withmaker = false;
    if(isset($request->with) && $request->with = 'makername'){
        $withmaker = true;
        $districts=subvendor_lights::select('jilla_id')->groupby('jilla_id')->get();
        foreach ($districts as $d) {
        DB::enableQueryLog();
           $area_dis_withmaker[] = subvendor_lights::has('area_district')->with(['area_district'=> function ($query) {
            $query->select('id', 'area_name');
        }])->with(['subvendor_name'=> function ($query) {
            $query->select('id', 'name');
        }])->whereIn('jilla_id',$request->session()->get('districts'))->select("maker_name", DB::raw("count(jilla_id) as count"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),'jilla_id')
        ->where('jilla_id',$d['jilla_id'])->groupby('jilla_id')->groupby('maker_name')->get()->toArray();
      }
    }

        //->where('jilla_id',$request->id)
        //dd($area_dis_withmaker);

        $district  = area_master::where('id',$request->id)->first();

        $maker_name_count=subvendor_lights::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->with(['subvendor_name'=> function ($query) {
        $query->select('id', 'name');
    }])->whereIn('jilla_id',$request->session()->get('districts'))->select(DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),'maker_name','jilla_id')
    ->groupby('maker_name')->get()->toArray();
      
     return view('backend.outwards.vendor_outward_lights_list',compact('area_dis','district', 'maker_name_count', 'area_dis_withmaker', 'withmaker'));
    }

public function subvendor_lights_list(Request $request)
{

    $jilla_id = $request->id;

      DB::enableQueryLog();
       $area_dis=subvendor_lights::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->has('subvendor_name')->with(['subvendor_name'=> function ($query) {
        $query->select('id', 'name');
    }])->where('jilla_id',$request->id)->select(DB::raw("count(invoice_number) as count"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),'jilla_id','subvendor_id','invoice_number')->groupby('subvendor_id')->get()->toArray();
       //dd( $area_dis);

    $area_dis_withmaker = array();
    $withmaker = false;
    if(isset($request->with) && $request->with = 'makername'){
        $withmaker = true;
        $districts=subvendor_lights::select('subvendor_id')->groupby('subvendor_id')->get();
        foreach ($districts as $d) {
            //dd($d->subvendor_id);
        DB::enableQueryLog();
           $area_dis_withmaker[] = subvendor_lights::has('area_district')->with(['area_district'=> function ($query) {
            $query->select('id', 'area_name');
        }])->with(['subvendor_name'=> function ($query) {
            $query->select('id', 'name');
        }])->where('jilla_id',$request->id)->select("maker_name", DB::raw("count(jilla_id) as count"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),'jilla_id','subvendor_id','invoice_number')
        ->where('subvendor_id',$d['subvendor_id'])->groupby('maker_name')->get()->toArray();
      }
    }



       $maker_name_count=subvendor_lights::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->with(['subvendor_name'=> function ($query) {
        $query->select('id', 'name');
    }])->where('jilla_id',$request->id)->select(DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),'maker_name','jilla_id', 'subvendor_id')
    ->groupby('maker_name')->get()->toArray();

    $area_dis_withmaker = array();
    $withmaker = false;
    if(isset($request->with) && $request->with = 'makername'){
        $withmaker = true;
        $subvendors=subvendor_lights::select('subvendor_id')->where('jilla_id', $request->id)->groupby('subvendor_id')->get();
        foreach ($subvendors as $d) {
        DB::enableQueryLog();
           $area_dis_withmaker[] = subvendor_lights::has('area_district')->with(['area_district'=> function ($query) {
            $query->select('id', 'area_name');
        }])->has('subvendor_name')->with(['subvendor_name'=> function ($query) {
            $query->select('id', 'name');
        }])->where('jilla_id',$request->id)->select("maker_name", DB::raw("count(jilla_id) as count"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),'jilla_id','subvendor_id', 'invoice_number')
        ->where('subvendor_id',$d['subvendor_id'])->groupby('subvendor_id')->groupby('maker_name')->get()->toArray();
      }
    }

     return view('backend.outwards.subvendor_lights_list',compact('jilla_id','area_dis', 'maker_name_count', 'area_dis_withmaker', 'withmaker'));

}

public function subvendor_lights_list_mandals(Request $request)
{

    $jilla_id = $request->id;
    $subvendor_id = $request->subvendor_id;
      DB::enableQueryLog();
       $area_dis=subvendor_lights::has('area_mandal')->with(['area_mandal'=> function ($query) {
        $query->select('id', 'area_name');
    }])->has('subvendor_name')->with(['subvendor_name'=> function ($query) {
        $query->select('id', 'name');
    }])->where('jilla_id',$request->id)->where('subvendor_id',$request->subvendor_id)->select(DB::raw("count(invoice_number) as count"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),'jilla_id','subvendor_id','invoice_number','mandal_id')->groupby('mandal_id')->get()->toArray();
       //dd( $area_dis);

     $area_dis_withmaker = array();
    $withmaker = false;
    if(isset($request->with) && $request->with = 'makername'){
        $withmaker = true;
        $districts=subvendor_lights::select('mandal_id')->groupby('mandal_id')->get();
       // dd($districts);
        foreach ($districts as $d) {
            //dd($d['mandal_id']);
        DB::enableQueryLog();
           $area_dis_withmaker[] = subvendor_lights::has('area_mandal')->with(['area_mandal'=> function ($query) {
            $query->select('id', 'area_name');
        }])->with(['subvendor_name'=> function ($query) {
            $query->select('id', 'name');
        }])->where('jilla_id',$request->id)->where('subvendor_id',$request->subvendor_id)->select("maker_name", DB::raw("count(jilla_id) as count"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),'jilla_id','mandal_id','subvendor_id')
        ->where('mandal_id',$d['mandal_id'])->groupby('mandal_id')->groupby('maker_name')->get()->toArray();
      }
    }

//dd($area_dis_withmaker);


    $maker_name_count=subvendor_lights::has('area_district')->with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->with(['subvendor_name'=> function ($query) {
        $query->select('id', 'name');
    }])->where('jilla_id',$request->id)->where('subvendor_id',$request->subvendor_id)->select(DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),'maker_name','jilla_id', 'mandal_id')
    ->groupby('maker_name')->get()->toArray();

    $area_dis_withmaker = array();
    $withmaker = false;
    if(isset($request->with) && $request->with = 'makername'){
        $withmaker = true;
        $mandals=subvendor_lights::select('mandal_id')->where('jilla_id', $request->id)->where('subvendor_id',$request->subvendor_id)->groupby('mandal_id')->get();
        foreach ($mandals as $d) {
        DB::enableQueryLog();
           $area_dis_withmaker[] = subvendor_lights::has('area_mandal')->with(['area_mandal'=> function ($query) {
            $query->select('id', 'area_name');
        }])->has('subvendor_name')->with(['subvendor_name'=> function ($query) {
            $query->select('id', 'name');
        }])->where('jilla_id',$request->id)->where('subvendor_id',$request->subvendor_id)->select("maker_name", DB::raw("count(jilla_id) as count"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),'jilla_id','subvendor_id','invoice_number','mandal_id')
        ->where('mandal_id',$d['mandal_id'])->groupby('mandal_id')->groupby('maker_name')->get()->toArray();
      }
    }

     return view('backend.outwards.subvendor_lights_list_mandals',compact('area_dis', 'maker_name_count', 'area_dis_withmaker', 'withmaker','jilla_id','subvendor_id'));

}

public function vendor_outwards_lights(Request $request)
  {
 $subvendorslists=gramuser::where('role',7)->get();
  //dd($vendorslists);
   $jilla=DB::table('area_masters')
                    ->where('parent_id','=','0')
                    ->where('super_id','=','0')
                     ->whereIn('id',$request->session()->get('districts'))
                    ->where('status','=','active')
                    ->get();


        $taluka=DB::table('area_masters')
                    ->where('parent_id','!=','0')
                    ->where('super_id','=','0')
                    ->where('status','=','active')  
                    ->where('parent_id',$request->get('jilla_id'))
                    // ->whereIn('id',$request->session()->get('mandals'))
                    ->get();
       
    $makeslist= Makeslist::get();

    $invoice_numbers= DB::table('outwards')
                    ->where('jilla_id',$request->get('jilla_id'))  
                    ->where('maker_name',$request->get('maker_name'))
                    ->get();
             $invoice_numbere= DB::table('outwards')->get();       

     return view('backend.outwards.vendor_outwards_lights',compact('jilla','makeslist','subvendorslists','taluka','invoice_numbere'));
   }


public function vendor_outwards_lights_store(Request $request){


   $inwardchecks = inwards_vendor::where(['jilla_id'=>$request->jilla_id,"maker_name"=>$request->maker_name])->get();

    $led18w=$request->rcv_qty_18w; $led35w=$request->rcv_qty_35w; $led70w=$request->rcv_qty_70w; $led110w=$request->rcv_qty_110w; $led190w=$request->rcv_qty_190w;

    $buf18w=$request->bufferstck_received_qty_18w;$buf35w=$request->bufferstck_received_qty_35w;$buf70w=$request->bufferstck_received_qty_70w;$buf110w=$request->bufferstck_received_qty_110w;$buf190w=$request->bufferstck_received_qty_190w;

     $drv18w=$request->drivers_received_qty_18w;$drv35w=$request->drivers_received_qty_35w;$drv70w=$request->drivers_received_qty_70w;$drv110w=$request->drivers_received_qty_110w;$drv190w=$request->drivers_received_qty_190w;

     $spd18w=$request->spd_received_qty_18w;$spd35w=$request->spd_received_qty_35w;$spd70w=$request->spd_received_qty_70w;$spd110w=$request->spd_received_qty_110w;$spd190w=$request->spd_received_qty_190w;

     $tltadp=$request->tiltadap_rcv_qty;

     //dd($inwardchecks);

    foreach ($inwardchecks as $inwardchec) 
    {
        if($led18w==0&&$led35w==0&&$led70w==0&&$led110w==0&&$led190w==0&&$led35w==0&&$buf18w==0&&$buf35w==0&&$buf70w==0&&$buf110w==0&&$buf190w==0&&$drv18w==0&&$drv35w==0&&$drv70w==0&&$drv190w==0&&$drv110w==0&&$spd18w==0&&$spd35w==0&&$spd70w==0&&$spd110w==0&&$spd190w==0&&$tltadp==0){
            break;
        }

        if($inwardchec->rcv_qty_18w <= $led18w){
            $insert_18w =$inwardchec->rcv_qty_18w;
             //dd($insert_18w);3000
             $inwardchec->rcv_qty_18w=$inwardchec->rcv_qty_18w-$inwardchec->rcv_qty_18w;
             //dd($inwardchec->rcv_qty_18w);0
             $led18w=$led18w-$insert_18w;
             //dd($led18w);50
            }elseif($inwardchec->rcv_qty_18w >= $led18w){
                $inwardchec->rcv_qty_18w=$inwardchec->rcv_qty_18w-$led18w;
                //dd($inwardchec->rcv_qty_18w);48
                $insert_18w =$led18w;
                //dd($insert_18w);
                $led18w=0;
            }
            //echo $led18w." , ".$insert_18w." , ".$inwardchec->rcv_qty_18w; echo "</br>";
        if($inwardchec->rcv_qty_35w <= $led35w){
             $insert_35w =$inwardchec->rcv_qty_35w;
             $inwardchec->rcv_qty_35w=$inwardchec->rcv_qty_35w-$inwardchec->rcv_qty_35w;
             $led35w=$led35w-$insert_35w;
             
            }elseif($inwardchec->rcv_qty_35w >= $led35w){
             $inwardchec->rcv_qty_35w=$inwardchec->rcv_qty_35w-$led35w;
             $insert_35w =$led35w;
             $led35w=0;
            }
         if($inwardchec->rcv_qty_70w <= $led70w){
            $insert_70w =$inwardchec->rcv_qty_70w;
             $inwardchec->rcv_qty_70w=$inwardchec->rcv_qty_70w-$inwardchec->rcv_qty_70w;
             $led70w=$led70w-$insert_70w;
             
            }elseif($inwardchec->rcv_qty_70w >= $led70w){
             $inwardchec->rcv_qty_70w=$inwardchec->rcv_qty_70w-$led70w;
             $insert_70w =$led70w;
             $led70w=0;
            }
        if($inwardchec->rcv_qty_110w <= $led110w){
             $insert_110w =$inwardchec->rcv_qty_110w;
             $inwardchec->rcv_qty_110w=$inwardchec->rcv_qty_110w-$inwardchec->rcv_qty_110w;
             $led110w=$led110w-$insert_110w;
            
            }elseif($inwardchec->rcv_qty_110w >= $led110w){
             $inwardchec->rcv_qty_110w=$inwardchec->rcv_qty_110w-$led110w;
             $insert_110w =$led110w;
             $led110w=0;
            }
        if($inwardchec->rcv_qty_190w <= $led190w){
            $insert_190w =$inwardchec->rcv_qty_190w;
             $inwardchec->rcv_qty_190w=$inwardchec->rcv_qty_190w-$inwardchec->rcv_qty_190w;
             $led190w=$led190w-$insert_190w;
             
            }elseif($inwardchec->rcv_qty_190w >= $led190w){
             $inwardchec->rcv_qty_190w=$inwardchec->rcv_qty_190w-$led190w;
              $insert_190w =$led190w;
             $led190w=0;
            }

        if($inwardchec->bufferstck_received_qty_18w <= $buf18w){
            $insert_buf18w =$inwardchec->bufferstck_received_qty_18w;
             $inwardchec->bufferstck_received_qty_18w=$inwardchec->bufferstck_received_qty_18w-$inwardchec->bufferstck_received_qty_18w;
             $buf18w=$buf18w-$insert_buf18w;
            }elseif($inwardchec->bufferstck_received_qty_18w >= $buf18w){
             $inwardchec->bufferstck_received_qty_18w=$inwardchec->bufferstck_received_qty_18w-$buf18w;
              $insert_buf18w =$buf18w;
              // dd($insert_buf18w);
             $buf18w=0;
            }
        if($inwardchec->bufferstck_received_qty_35w <= $buf35w){
            $insert_buf35w =$inwardchec->bufferstck_received_qty_35w;
             $inwardchec->bufferstck_received_qty_35w=$inwardchec->bufferstck_received_qty_35w-$inwardchec->bufferstck_received_qty_35w;
             $buf35w=$buf35w-$insert_buf35w;
              
            }elseif($inwardchec->bufferstck_received_qty_35w >= $buf35w){
             $inwardchec->bufferstck_received_qty_35w=$inwardchec->bufferstck_received_qty_35w-$buf35w;
             $insert_buf35w =$buf35w;
             $buf35w=0;
            }
        if($inwardchec->bufferstck_received_qty_70w <= $buf70w){
            $insert_buf70w =$inwardchec->bufferstck_received_qty_70w;
             $inwardchec->bufferstck_received_qty_70w=$inwardchec->bufferstck_received_qty_70w-$inwardchec->bufferstck_received_qty_70w;
             $buf70w=$buf70w-$insert_buf70w;
              
            }elseif($inwardchec->bufferstck_received_qty_70w >= $buf70w){
             $inwardchec->bufferstck_received_qty_70w=$inwardchec->bufferstck_received_qty_70w-$buf70w;
             $insert_buf70w =$buf70w;
             $buf70w=0;
            }                   
        if($inwardchec->bufferstck_received_qty_110w <= $buf110w){
             $insert_buf110w =$inwardchec->bufferstck_received_qty_110w;
             $inwardchec->bufferstck_received_qty_110w=$inwardchec->bufferstck_received_qty_110w-$inwardchec->bufferstck_received_qty_110w;
             $buf110w=$buf110w-$insert_buf110w;
             
            }elseif($inwardchec->bufferstck_received_qty_110w >= $buf110w){
             $inwardchec->bufferstck_received_qty_110w=$inwardchec->bufferstck_received_qty_110w-$buf110w;
             $insert_buf110w =$buf110w;
             $buf110w=0;
            }
        if($inwardchec->bufferstck_received_qty_190w <= $buf190w){
            $insert_buf190w =$inwardchec->bufferstck_received_qty_190w;
             $inwardchec->bufferstck_received_qty_190w=$inwardchec->bufferstck_received_qty_190w-$inwardchec->bufferstck_received_qty_190w;
             $buf190w=$buf190w-$insert_buf190w;
             
            }elseif($inwardchec->bufferstck_received_qty_190w >= $buf190w){
             $inwardchec->bufferstck_received_qty_190w=$inwardchec->bufferstck_received_qty_190w-$buf190w;
             $insert_buf190w =$buf190w;
             $buf190w=0;
            }
        if($inwardchec->drivers_received_qty_18w <= $drv18w){
            $insert_drv18w =$inwardchec->drivers_received_qty_18w;
             $inwardchec->drivers_received_qty_18w=$inwardchec->drivers_received_qty_18w-$inwardchec->drivers_received_qty_18w;
             $drv18w=$drv18w-$insert_drv18w;
             
            }elseif($inwardchec->drivers_received_qty_18w >= $drv18w){
             $inwardchec->drivers_received_qty_18w=$inwardchec->drivers_received_qty_18w-$drv18w;
             $insert_drv18w =$drv18w;
             $drv18w=0;
            }
        if($inwardchec->drivers_received_qty_35w <= $drv35w){
             $insert_drv35w =$inwardchec->drivers_received_qty_35w;
             $inwardchec->drivers_received_qty_35w=$inwardchec->drivers_received_qty_35w-$inwardchec->drivers_received_qty_35w;
             $drv35w=$drv35w-$insert_drv35w;
            
            }elseif($inwardchec->drivers_received_qty_35w >= $drv35w){
             $inwardchec->drivers_received_qty_35w=$inwardchec->drivers_received_qty_35w-$drv35w;
              $insert_drv35w =$drv35w;
             $drv35w=0;
            }

        if($inwardchec->drivers_received_qty_70w <= $drv70w){
             $insert_drv70w =$inwardchec->drivers_received_qty_70w;
             $inwardchec->drivers_received_qty_70w=$inwardchec->drivers_received_qty_70w-$inwardchec->drivers_received_qty_70w;
             $drv70w=$drv70w-$insert_drv70w;
            
            }elseif($inwardchec->drivers_received_qty_70w >= $drv70w){
             $inwardchec->drivers_received_qty_70w=$inwardchec->drivers_received_qty_70w-$drv70w;
              $insert_drv70w =$drv70w;
             $drv70w=0;
            }
        if($inwardchec->drivers_received_qty_110w <= $drv110w){
            $insert_drv110w =$inwardchec->drivers_received_qty_110w;
             $inwardchec->drivers_received_qty_110w=$inwardchec->drivers_received_qty_110w-$inwardchec->drivers_received_qty_110w;
             $drv110w=$drv110w-$insert_drv110w;
             
            }elseif($inwardchec->drivers_received_qty_110w >= $drv110w){
             $inwardchec->drivers_received_qty_110w=$inwardchec->drivers_received_qty_110w-$drv110w;
              $insert_drv110w =$drv110w;
             $drv110w=0;
            } 
        if($inwardchec->drivers_received_qty_190w <= $drv190w){
            $insert_drv190w =$inwardchec->drivers_received_qty_190w;
             $inwardchec->drivers_received_qty_190w=$inwardchec->drivers_received_qty_190w-$inwardchec->drivers_received_qty_190w;
             $drv190w=$drv190w-$insert_drv190w;
             
            }elseif($inwardchec->drivers_received_qty_190w >= $drv190w){
             $inwardchec->drivers_received_qty_190w=$inwardchec->drivers_received_qty_190w-$drv190w;
              $insert_drv190w =$drv190w;
             $drv190w=0;
            }
        if($inwardchec->spd_received_qty_18w <= $spd18w){
            $insert_spd18w =$inwardchec->spd_received_qty_18w;
             $inwardchec->spd_received_qty_18w=$inwardchec->spd_received_qty_18w-$inwardchec->spd_received_qty_18w;
             $spd18w=$spd18w-$insert_spd18w;
             
            }elseif($inwardchec->spd_received_qty_18w >= $spd18w){
             $inwardchec->spd_received_qty_18w=$inwardchec->spd_received_qty_18w-$spd18w;
              $insert_spd18w =$spd18w;
             $spd18w=0;
            }
        if($inwardchec->spd_received_qty_35w <= $spd35w){
             $insert_spd35w =$inwardchec->spd_received_qty_35w;
             $inwardchec->spd_received_qty_35w=$inwardchec->spd_received_qty_35w-$inwardchec->spd_received_qty_35w;
             $spd35w=$spd35w-$insert_spd35w;
            
            }elseif($inwardchec->spd_received_qty_35w >= $spd35w){
             $inwardchec->spd_received_qty_35w=$inwardchec->spd_received_qty_35w-$spd35w;
             $insert_spd35w =$spd35w;
             $spd35w=0;
            }                                
        if($inwardchec->spd_received_qty_70w <= $spd70w){
            $insert_spd70w =$inwardchec->spd_received_qty_70w;
             $inwardchec->spd_received_qty_70w=$inwardchec->spd_received_qty_70w-$inwardchec->spd_received_qty_70w;
             $spd70w=$spd70w-$insert_spd70w;
             
            }elseif($inwardchec->spd_received_qty_70w >= $spd70w){
             $inwardchec->spd_received_qty_70w=$inwardchec->spd_received_qty_70w-$spd70w;
            $insert_spd70w =$spd70w;
             $spd70w=0;
            }                 
        if($inwardchec->spd_received_qty_110w <= $spd110w){
            $insert_spd110w =$inwardchec->spd_received_qty_110w;
             $inwardchec->spd_received_qty_110w=$inwardchec->spd_received_qty_110w-$inwardchec->spd_received_qty_110w;
             $spd110w=$spd110w-$insert_spd110w;
              
            }elseif($inwardchec->spd_received_qty_110w >= $spd110w){
             $inwardchec->spd_received_qty_110w=$inwardchec->spd_received_qty_110w-$spd110w;
              $insert_spd110w =$spd110w;
             $spd110w=0;
            } 
        if($inwardchec->spd_received_qty_190w <= $spd190w){
            $insert_spd190w =$inwardchec->spd_received_qty_190w;
             $inwardchec->spd_received_qty_190w=$inwardchec->spd_received_qty_190w-$inwardchec->spd_received_qty_190w;
             $spd190w=$spd190w-$insert_spd190w;
             
            }elseif($inwardchec->spd_received_qty_190w >= $spd190w){
             $inwardchec->spd_received_qty_190w=$inwardchec->spd_received_qty_190w-$spd190w;
              $insert_spd190w =$spd190w;
             $spd190w=0;
            }
            if($inwardchec->tiltadap_rcv_qty <= $tltadp){
                $insert_tltadp =$inwardchec->tiltadap_rcv_qty;
             $inwardchec->tiltadap_rcv_qty=$inwardchec->tiltadap_rcv_qty-$inwardchec->tiltadap_rcv_qty;
             $tltadp=$tltadp-$inwardchec->tiltadap_rcv_qty;
              
            }elseif($inwardchec->tiltadap_rcv_qty >= $tltadp){
             $inwardchec->tiltadap_rcv_qty=$inwardchec->tiltadap_rcv_qty-$tltadp;
             $insert_tltadp =$tltadp;
             $tltadp=0;
            }

          $insert=[];
          if($request->hasFile('files') )
         {
            $path=$request->file('files')->store('public');  
            $insert[]=$path;
         }else{
            $name="pdf".rand(11111111, 99999999).".pdf"; 
            Storage::disk('public')->put($name,base64_decode($request->input('files')));
            $insert[]="public/".$name;
         }         
         $inwardchec->save();

          $from_jilla=$request->jilla_id;
          $subvendor_id=$request->subvendor_id;
          $fromtring = area_master::where('id',$request->jilla_id)->first();
          $frominvoice=$inwardchec->invoice_number;
          $toinvoice=$fromtring->area_name."-to-".$subvendor_id."-".time();


         subvendor_lights_invoices::create(['from_jilla_id'=>$from_jilla, 'maker_name'=>$request->maker_name, 'from_invoice_number'=>$frominvoice,'to_invoice_number'=>$toinvoice,'to_subvendor_id'=>$subvendor_id,'inward_id'=>$inwardchec->id,'status'=>1,"date_of_transfer"=>date('Y-m-d',strtotime($request->date_of_receiving)),"remarks"=>$request->remarks,
                     'rcv_qty_18w'=>$insert_18w,
                     'rcv_qty_35w'=>$insert_35w,
                     'rcv_qty_70w'=>$insert_70w,
                     'rcv_qty_110w'=>$insert_110w,
                     'rcv_qty_190w'=>$insert_190w,                      
                     'bufferstck_received_qty_18w'=>$insert_buf18w,
                     'bufferstck_received_qty_35w'=>$insert_buf35w,
                     'bufferstck_received_qty_70w'=>$insert_buf70w, 
                     'bufferstck_received_qty_110w'=>$insert_buf110w,
                     'bufferstck_received_qty_190w'=>$insert_buf190w,
                     'drivers_received_qty_18w'=>$insert_drv18w, 
                     'drivers_received_qty_35w'=>$insert_drv35w,
                     'drivers_received_qty_70w'=>$insert_drv70w,
                     'drivers_received_qty_110w'=>$insert_drv110w,
                     'drivers_received_qty_190w'=>$insert_drv190w, 
                     'spd_received_qty_18w'=>$insert_spd18w,
                     'spd_received_qty_35w'=>$insert_spd35w,
                     'spd_received_qty_70w'=>$insert_spd70w,
                     'spd_received_qty_110w'=>$insert_spd110w,
                     'spd_received_qty_190w'=>$insert_spd190w,
                     'tiltadap_rcv_qty'=>$insert_tltadp,
             "files"=>implode(',', $insert)]);
    }
            
          $insert=[];
          if($request->hasFile('files') )
         {
            $path=$request->file('files')->store('public');  
            $insert[]=$path;
         }else{
            $name="pdf".rand(11111111, 99999999).".pdf"; 
            Storage::disk('public')->put($name,base64_decode($request->input('files')));
            $insert[]="public/".$name;
         }         
       
          $from_jilla=$request->jilla_id;
          $subvendor_id=$request->subvendor_id;
          $fromtring = area_master::where('id',$request->jilla_id)->first();
          $frominvoice=$inwardchec->invoice_number;
          $toinvoice=$fromtring->area_name."-to-".$subvendor_id."-".time();

            subvendor_lights::create(['jilla_id'=>$request->jilla_id, 'maker_name'=>$request->maker_name,'subvendor_id'=>$subvendor_id,'invoice_number'=>$toinvoice,'date_of_receiving'=>$request->date_of_receiving,'mandal_id'=>$request->mandal_id,

                    'rcv_qty_18w'=>$request->rcv_qty_18w,
                    'rcv_qty_35w'=>$request->rcv_qty_35w,
                    'rcv_qty_70w'=>$request->rcv_qty_70w,
                    'rcv_qty_110w'=>$request->rcv_qty_110w,
                    'rcv_qty_190w'=>$request->rcv_qty_190w,

                      
                     'tiltadap_rcv_qty'=>$request->tiltadap_rcv_qty, 
                     
                     'bufstck_rcv_qty'=>$request->bufstck_rcv_qty, 
                     'bufferstck_received_qty_18w'=>$request->bufferstck_received_qty_18w,
                     'bufferstck_received_qty_35w'=>$request->bufferstck_received_qty_35w,
                     'bufferstck_received_qty_70w'=>$request->bufferstck_received_qty_70w, 
                     'bufferstck_received_qty_110w'=>$request->bufferstck_received_qty_110w,
                     'bufferstck_received_qty_190w'=>$request->bufferstck_received_qty_190w,

                     'num_drv_rcv_qty'=>$request->num_drv_rcv_qty,
                     'drivers_received_qty_18w'=>$request->drivers_received_qty_18w, 
                     'drivers_received_qty_35w'=>$request->drivers_received_qty_35w,
                     'drivers_received_qty_70w'=>$request->drivers_received_qty_70w,
                     'drivers_received_qty_110w'=>$request->drivers_received_qty_110w,
                     'drivers_received_qty_190w'=>$request->drivers_received_qty_190w, 

                     'num_spd_rcv_qty'=>$request->num_spd_rcv_qty,
                     'spd_received_qty_18w'=>$request->spd_received_qty_18w,
                     'spd_received_qty_35w'=>$request->spd_received_qty_35w,
                     'spd_received_qty_70w'=>$request->spd_received_qty_70w,
                     'spd_received_qty_110w'=>$request->spd_received_qty_110w,
                     'spd_received_qty_190w'=>$request->spd_received_qty_190w,
                     'total_rcv_qty'=>$request->total_rcv_qty,
                    'files'=>implode(',', $insert)]);

             return redirect(url('vendor_outwards_lights'))->withSuccess('Sucessfully lights updated towards Outwards');


      
   }
   public function subvendor_lights_store_check(Request $request){

     $countstore = inwards_vendor::where(['jilla_id'=>$request->jilla_id,"maker_name"=>$request->maker_name])->select(DB::raw("SUM(rcv_qty_18w) as w18w"),DB::raw("SUM(rcv_qty_35w) as w35w"),DB::raw("SUM(rcv_qty_70w) as w70w"),DB::raw("SUM(rcv_qty_110w) as w110w"),DB::raw("SUM(rcv_qty_190w) as w190w"),DB::raw("SUM(bufferstck_received_qty_18w) as b18w"),DB::raw("SUM(bufferstck_received_qty_35w) as b35w"),DB::raw("SUM(bufferstck_received_qty_70w) as b70w"),DB::raw("SUM(bufferstck_received_qty_110w) as b110w"),DB::raw("SUM(bufferstck_received_qty_190w) as b190w"),DB::raw("SUM(drivers_received_qty_18w) as d18w"),DB::raw("SUM(drivers_received_qty_35w) as d35w"),DB::raw("SUM(drivers_received_qty_70w) as d70w"),DB::raw("SUM(drivers_received_qty_110w) as d110w"),DB::raw("SUM(drivers_received_qty_190w) as d190w"),DB::raw("SUM(spd_received_qty_18w) as s18w"),DB::raw("SUM(spd_received_qty_35w) as s35w"),DB::raw("SUM(spd_received_qty_70w) as s70w"),DB::raw("SUM(spd_received_qty_110w) as s110w"),DB::raw("SUM(spd_received_qty_190w) as s190w"),DB::raw("SUM(tiltadap_rcv_qty) as tltd"))->first()->toArray();


     //dd($countstore);
      return response()->json($countstore);
    
  // |regex:[a-zA-Z\s]
   }
    public function subvendor_armsclamps_store_check(Request $request){

     $countstore = armsclamps_vendor::where(['jilla_id'=>$request->jilla_id,'suplied_by'=>$request->suplied_by])->select(DB::raw("SUM(received_arms_dia_25) as a25d"),DB::raw("SUM(received_arms_dia_inner_32) as a32d"),DB::raw("SUM(received_arms_dia_inner_40) as a40d"),DB::raw("SUM(received_clams_set_25) as c25s"),DB::raw("SUM(received_clams_set_40_2) as c402s"),DB::raw("SUM(received_clams_set_40_3) as c403s"),DB::raw("SUM(received_aluminium_cable) as cbl"))->first()->toArray();
     //dd($countstore);
       // |regex:[a-zA-Z\s]
      return response()->json($countstore);
   }

   public function subvendor_ccms_store_check(Request $request){

     $countstore = ccms_vendor::where(['jilla_id'=>$request->jilla_id])->select(DB::raw("SUM(ccms_ph_receive_quantity) as rccms1w"))->first()->toArray();

     // dd($countstore);
      return response()->json($countstore);
    

   }

   public function invoicenumber(Request $request)
    {
        $invoicenumber=DB::table('inwards')
                    ->where('jilla_id','=',$request->jilla_id)
                    ->where('maker_name','=',$request->maker_name)
                    ->where('approve_status','=',0)
                    ->get();
        // var_dump($jillaid);
        // exit();

         $str="<option value=''>select</option>";
foreach($invoicenumber as $j)
{
    $str.="<option value='$j->invoice_number'>".$j->invoice_number."</option>";
}
return $str;           
                
    }

    public function armsinvoicenumber(Request $request)
    {
        $invoicenumber=DB::table('armsclamps')
                    ->where('jilla_id','=',$request->jilla_id)
                    ->where('approve_status','=',0)
                    ->get();
         // var_dump($invoicenumber);
         // exit();

         $str="<option value=''>select</option>";
         foreach($invoicenumber as $j)
         {
        $str.="<option value='$j->invoice_number'>".$j->invoice_number."</option>";
          }
         return $str;           
                
    }


   public function ccmsinvoicenumber(Request $request)
    {
        $invoicenumber=DB::table('inwardccmstypes')
                    ->where('jilla_id','=',$request->jilla_id)
                    ->get();
        // var_dump($jillaid);
        // exit();

         $str="<option value=''>select</option>";
         foreach($invoicenumber as $j)
             {
      $str.="<option value='$j->invoice_number'>".$j->invoice_number."</option>";
          }
         return $str;           
                
    }

public function move_inventory_invoicenumber(Request $request)
    {
        $invoicenumber=DB::table('inwards')
                    ->where('jilla_id','=',$request->jilla_id)
                    ->where('maker_name','=',$request->maker_name)
                    ->get();
        // var_dump($jillaid);
        // exit();

         $str="<option value=''>select</option>";
foreach($invoicenumber as $j)
{
    $str.="<option value='$j->invoice_number'>".$j->invoice_number."</option>";
}
 return $str;           
                
    }

    public function subvendor_inwardslights(Request $request)
{

     

     DB::enableQueryLog();
       $area_dis=subvendor_lights::has('area_mandal')->with(['area_mandal'=> function ($query) {
        $query->select('id', 'area_name');
    }])->has('subvendor_name')->with(['subvendor_name'=> function ($query) {
        $query->select('id', 'name');
    }])->whereIn('mandal_id',$request->session()->get('mandals'))->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),'mandal_id')->groupby('mandal_id')->get()->toArray();
       //dd( $area_dis);
    //$inventory = outward::with(['area_district','vendor_name'])->orderby('created_at','desc')->get()->toArray();
     //dd($inventory);

    // ->where(function($q) use ($request) {
    //      if($request->session()->get('role')==7){
    //         $q->where('subvendor_id',$request->session()->get('id'));
    //      }
    // })

    $area_dis_withmaker = array();
    $withmaker = false;
    if(isset($request->with) && $request->with = 'makername'){
        $withmaker = true;
        $districts=subvendor_lights::select('mandal_id')->groupby('mandal_id')->get();
       // dd($districts);
        foreach ($districts as $d) {
            //dd($d['mandal_id']);
        DB::enableQueryLog();
           $area_dis_withmaker[] = subvendor_lights::has('area_mandal')->with(['area_mandal'=> function ($query) {
            $query->select('id', 'area_name');
        }])->with(['subvendor_name'=> function ($query) {
            $query->select('id', 'name');
        }])->whereIn('mandal_id',$request->session()->get('mandals'))->select("maker_name", DB::raw("count(jilla_id) as count"),DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),'jilla_id','mandal_id')
        ->where('mandal_id',$d['mandal_id'])->groupby('mandal_id')->groupby('maker_name')->get()->toArray();
      }
    }
    
 //dd($area_dis_withmaker);

    $maker_name_count=subvendor_lights::has('area_mandal')->with(['area_mandal'=> function ($query) {
        $query->select('id', 'area_name');
    }])->has('subvendor_name')->with(['subvendor_name'=> function ($query) {
        $query->select('id', 'name');
    }])->whereIn('mandal_id',$request->session()->get('mandals'))->select(DB::raw("SUM(rcv_qty_18w) as 18w"),DB::raw("SUM(rcv_qty_35w) as 35w"),DB::raw("SUM(rcv_qty_70w) as 70w"),DB::raw("SUM(rcv_qty_110w) as 110w"),DB::raw("SUM(rcv_qty_190w) as 190w"),DB::raw("SUM(bufstck_rcv_qty) as buffer_stk"),'maker_name','mandal_id')->groupby('maker_name')->get()->toArray();

    // ->where(function($q) use ($request) {
    //     if($request->session()->get('role')==7){
    //         $q->where('subvendor_id',$request->session()->get('id'));
    //     }
    // })

     return view('backend.subvendor.subvendor_inwardslights',compact('area_dis', 'maker_name_count', 'area_dis_withmaker', 'withmaker'));

}

public function subvendor_inwardsarmsclamps(Request $request)
{

     DB::enableQueryLog();
       $area_dis=subvendor_armsclamps::has('area_mandal')->with(['area_mandal'=> function ($query) {
        $query->select('id', 'area_name');
    }])->has('subvendor_name')->with(['subvendor_name'=> function ($query) {
        $query->select('id', 'name');
    }])->whereIn('mandal_id',$request->session()->get('mandals'))->select(DB::raw("count(mandal_id) as count"),DB::raw("SUM(received_arms_dia_25) as arms_25"),DB::raw("SUM(received_arms_dia_inner_32) as arms_32"),DB::raw("SUM(received_arms_dia_inner_40) as arms_40"),DB::raw("SUM(received_clams_set_25) as clamps_25"),DB::raw("SUM(received_clams_set_40_2) as clamps_42"),DB::raw("SUM(received_clams_set_40_3) as clamps_43"),DB::raw("SUM(received_aluminium_cable) as rcv_cbl"),'mandal_id')->where(function($q) use ($request) {
         if($request->session()->get('role')==7){
            $q->where('subvendor_id',$request->session()->get('id'));
         }
    })->groupby('mandal_id')->get()->toArray();
       //dd( $area_dis);
     $area_dis_supply=subvendor_armsclamps::whereIn('mandal_id',$request->session()->get('mandals'))->select(DB::raw("count(mandal_id) as count"),DB::raw("SUM(received_arms_dia_25) as arms_25"),DB::raw("SUM(received_arms_dia_inner_32) as arms_32"),DB::raw("SUM(received_arms_dia_inner_40) as arms_40"),DB::raw("SUM(received_clams_set_25) as clamps_25"),DB::raw("SUM(received_clams_set_40_2) as clamps_42"),DB::raw("SUM(received_clams_set_40_3) as clamps_43"),DB::raw("SUM(received_aluminium_cable) as rcv_cbl"),'suplied_by')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->groupby('suplied_by')->get()->toArray();
     return view('backend.subvendor.subvendor_inwardsarmsclamps',compact('area_dis','area_dis_supply'));

}

public function subvendor_inwardsccms(Request $request)
{

     DB::enableQueryLog();
       $area_dis=subvendor_ccms::has('area_mandal')->with(['area_mandal'=> function ($query) {
        $query->select('id', 'area_name');
    }])->has('subvendor_name')->with(['subvendor_name'=> function ($query) {
        $query->select('id', 'name');
    }])->whereIn('mandal_id',$request->session()->get('mandals'))->select(DB::raw("count(mandal_id) as count"),DB::raw("SUM(ccms_ph_receive_quantity) as ccmsph"),DB::raw("SUM(received_cable) as rcv_cbl"),'mandal_id')->where(function($q) use ($request) {
         if($request->session()->get('role')==7){
            $q->where('subvendor_id',$request->session()->get('id'));
         }
    })->groupby('mandal_id')->get()->toArray();
       // /dd( $area_dis);
     return view('backend.subvendor.subvendor_inwardsccms',compact('area_dis'));

  }
}
