<?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_inventoryarm_invoices;
use App\Models\move_inventoryccms;
use App\Models\move_inventoryccms_invoices;
use App\Models\move_inventory_invoices;
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\outward_ccms;
use App\Models\ccms_vendor;
use App\Models\total_inward;
use App\Models\total_armsclamps;
use App\Models\total_ccms;
use App\Models\PreliminarysurveyModel;
use App\Models\outward_armsclamps;
use App\Models\eesl;

use DB;
use Validator;
use File;
use Redirect;
use Illuminate\Support\Facades\Storage;

class InwardController extends Controller
{

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


    public function index(Request $request)
{

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

          }
    
     DB::enableQueryLog();
       $area_dis=inward::with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(org_rcv_qty_18w) as 18w"),DB::raw("SUM(org_rcv_qty_35w) as 35w"),DB::raw("SUM(org_rcv_qty_70w) as 70w"),DB::raw("SUM(org_rcv_qty_110w) as 110w"),DB::raw("SUM(org_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(org_bufferstck_received_qty_18w) as buffer_stk_rcv_18"),DB::raw("SUM(org_bufferstck_received_qty_35w) as buffer_stk_rcv_35"),DB::raw("SUM(org_bufferstck_received_qty_70w) as buffer_stk_rcv_70"),DB::raw("SUM(org_bufferstck_received_qty_110w) as buffer_stk_rcv_110"),DB::raw("SUM(org_bufferstck_received_qty_190w) as buffer_stk_rcv_190"),DB::raw("SUM(org_drivers_received_qty_18w) as org_drivers_received_qty_18w"),DB::raw("SUM(org_drivers_received_qty_35w) as org_drivers_received_qty_35w"),DB::raw("SUM(org_drivers_received_qty_70w) as org_drivers_received_qty_70w"),DB::raw("SUM(org_drivers_received_qty_110w) as org_drivers_received_qty_110w"),DB::raw("SUM(org_drivers_received_qty_190w) as org_drivers_received_qty_190w"),DB::raw("SUM(org_spd_received_qty_18w) as org_spd_received_qty_18w"),DB::raw("SUM(org_spd_received_qty_35w) as org_spd_received_qty_35w"),DB::raw("SUM(org_spd_received_qty_70w) as org_spd_received_qty_70w"),DB::raw("SUM(org_spd_received_qty_110w) as org_spd_received_qty_110w"),DB::raw("SUM(org_spd_received_qty_190w) as org_spd_received_qty_190w"),DB::raw("SUM(org_tiltadap_rcv_qty) as org_tiltadap_rcv_qty"),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);
  
   
    })->where('jilla_id','!=','EESL')->groupby('jilla_id')->get()->toArray();
    //dd($area_dis);

    DB::enableQueryLog();
       $area_balance=inward::with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->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_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"),DB::raw("SUM(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"),'maker_name','jilla_id')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->where('jilla_id','!=','EESL')->groupby('jilla_id')->get()->toArray();
    //dd($area_balance);



    DB::enableQueryLog();
       $area_disc=inward::with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(org_rcv_qty_18w) as 18w"),DB::raw("SUM(org_rcv_qty_35w) as 35w"),DB::raw("SUM(org_rcv_qty_70w) as 70w"),DB::raw("SUM(org_rcv_qty_110w) as 110w"),DB::raw("SUM(org_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(org_bufferstck_received_qty_18w) as buffer_stk_rcv_18"),DB::raw("SUM(org_bufferstck_received_qty_35w) as buffer_stk_rcv_35"),DB::raw("SUM(org_bufferstck_received_qty_70w) as buffer_stk_rcv_70"),DB::raw("SUM(org_bufferstck_received_qty_110w) as buffer_stk_rcv_110"),DB::raw("SUM(org_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"),
    DB::raw("SUM(rcv_qty_18w) as c_18w"),DB::raw("SUM(rcv_qty_35w) as c_35w"),DB::raw("SUM(rcv_qty_70w) as c_70w"),DB::raw("SUM(rcv_qty_110w) as c_110w"),DB::raw("SUM(rcv_qty_190w) as c_190w"),DB::raw("SUM(bufferstck_received_qty_18w) as c_buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as c_buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as c_buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as c_buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as c_buffer_stk_rcv_190"),DB::raw("SUM(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"),'maker_name','jilla_id')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->where('jilla_id','!=','EESL')->groupby('maker_name')->get()->toArray();
    //dd( $area_disc);

    $inventory = move_inventory::with(['from_district','to_district'])->select(DB::raw("SUM(rcv_qty_18w) as c_18w"),DB::raw("SUM(rcv_qty_35w) as c_35w"),DB::raw("SUM(rcv_qty_70w) as c_70w"),DB::raw("SUM(rcv_qty_110w) as c_110w"),DB::raw("SUM(rcv_qty_190w) as c_190w"),DB::raw("SUM(bufferstck_received_qty_18w) as c_buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as c_buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as c_buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as c_buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as c_buffer_stk_rcv_190"))->orderby('created_at','desc')->get();
   // dd($inventory);

     $inventory_eesl = move_inventory::with(['from_district','to_district'])->select(DB::raw("SUM(rcv_qty_18w) as c_18w"),DB::raw("SUM(rcv_qty_35w) as c_35w"),DB::raw("SUM(rcv_qty_70w) as c_70w"),DB::raw("SUM(rcv_qty_110w) as c_110w"),DB::raw("SUM(rcv_qty_190w) as c_190w"),DB::raw("SUM(bufferstck_received_qty_18w) as c_buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as c_buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as c_buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as c_buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as c_buffer_stk_rcv_190"),DB::raw("SUM(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"))->where('to_jilla_id','=','EESL')->orderby('created_at','desc')->get();

     $transfer_inventory = move_inventory::select(DB::raw("SUM(rcv_qty_18w) as c_18w"),DB::raw("SUM(rcv_qty_35w) as c_35w"),DB::raw("SUM(rcv_qty_70w) as c_70w"),DB::raw("SUM(rcv_qty_110w) as c_110w"),DB::raw("SUM(rcv_qty_190w) as c_190w"),DB::raw("SUM(bufferstck_received_qty_18w) as c_buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as c_buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as c_buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as c_buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as c_buffer_stk_rcv_190"),DB::raw("SUM(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"),'maker_name')->where('to_jilla_id','=','EESL')->groupby('maker_name')->get()->toArray();
    // dd($transfer_inventory);

     return view('backend.inwards_view',compact('area_dis','area_disc','inventory','transfer_inventory','area_balance','inventory_eesl'));
    
 
}

    public function inwards_withmaker_list(Request $request)
{

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

          }
    $districts=inward::select('jilla_id')->groupby('jilla_id')->get();
foreach ($districts as $d) {
     DB::enableQueryLog();
       $area_dis_withmaker[]=inward::with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->select("maker_name",DB::raw("count(jilla_id) as count"),DB::raw("SUM(org_rcv_qty_18w) as 18w"),DB::raw("SUM(org_rcv_qty_35w) as 35w"),DB::raw("SUM(org_rcv_qty_70w) as 70w"),DB::raw("SUM(org_rcv_qty_110w) as 110w"),DB::raw("SUM(org_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(org_bufferstck_received_qty_18w) as buffer_stk_rcv_18"),DB::raw("SUM(org_bufferstck_received_qty_35w) as buffer_stk_rcv_35"),DB::raw("SUM(org_bufferstck_received_qty_70w) as buffer_stk_rcv_70"),DB::raw("SUM(org_bufferstck_received_qty_110w) as buffer_stk_rcv_110"),DB::raw("SUM(org_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"),DB::raw("SUM(rcv_qty_18w) as c_18w"),DB::raw("SUM(rcv_qty_35w) as c_35w"),DB::raw("SUM(rcv_qty_70w) as c_70w"),DB::raw("SUM(rcv_qty_110w) as c_110w"),DB::raw("SUM(rcv_qty_190w) as c_190w"),DB::raw("SUM(bufferstck_received_qty_18w) as c_buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as c_buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as c_buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as c_buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as c_buffer_stk_rcv_190"),'jilla_id')->where(function($q) use ($request){    
    
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
  
   
    })->where('jilla_id',$d['jilla_id'])->where('jilla_id','!=','EESL')->groupby('jilla_id')->groupby('maker_name')->get()->toArray();
    
   }
    //dd( $area_dis_withmaker);

   $districts=inward::select('maker_name')->groupby('maker_name')->get();
foreach ($districts as $d) {
     DB::enableQueryLog();
       $area_dis_withmakers[]=inward::with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->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('jilla_id','!=','EESL')->where('maker_name',$d['maker_name'])->groupby('maker_name')->get()->toArray();
    
   }
   //dd($area_dis_withmakers);

   $inventory = move_inventory::with(['from_district','to_district'])->select(DB::raw("SUM(rcv_qty_18w) as c_18w"),DB::raw("SUM(rcv_qty_35w) as c_35w"),DB::raw("SUM(rcv_qty_70w) as c_70w"),DB::raw("SUM(rcv_qty_110w) as c_110w"),DB::raw("SUM(rcv_qty_190w) as c_190w"),DB::raw("SUM(bufferstck_received_qty_18w) as c_buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as c_buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as c_buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as c_buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as c_buffer_stk_rcv_190"))->orderby('created_at','desc')->get();

   $inventory_eesl = move_inventory::with(['from_district','to_district'])->select(DB::raw("SUM(rcv_qty_18w) as c_18w"),DB::raw("SUM(rcv_qty_35w) as c_35w"),DB::raw("SUM(rcv_qty_70w) as c_70w"),DB::raw("SUM(rcv_qty_110w) as c_110w"),DB::raw("SUM(rcv_qty_190w) as c_190w"),DB::raw("SUM(bufferstck_received_qty_18w) as c_buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as c_buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as c_buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as c_buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as c_buffer_stk_rcv_190"))->where('to_jilla_id','=','EESL')->orderby('created_at','desc')->get();
   $transfer_inventory = move_inventory::select(DB::raw("SUM(rcv_qty_18w) as c_18w"),DB::raw("SUM(rcv_qty_35w) as c_35w"),DB::raw("SUM(rcv_qty_70w) as c_70w"),DB::raw("SUM(rcv_qty_110w) as c_110w"),DB::raw("SUM(rcv_qty_190w) as c_190w"),DB::raw("SUM(bufferstck_received_qty_18w) as c_buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as c_buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as c_buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as c_buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as c_buffer_stk_rcv_190"),'maker_name')->where('to_jilla_id','=','EESL')->groupby('maker_name')->get()->toArray();


     return view('backend.inwards_withmaker_list',compact('area_dis_withmaker','area_dis_withmakers','inventory','inventory_eesl','transfer_inventory'));
    
 
}

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

    $inward= inward::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.inwards_list',compact('inward','makeslist'));
}

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

     return view('backend.inwards_store',compact('jilla','makeslist'));
}


    public function store(Request $request){
        
       

          $rules = array(
    'led_wattage_18' => 'required_without_all:led_wattage_35,led_wattage_70,led_wattage_110,led_wattage_190',

     'rcv_qty_18w' => 'required_without_all:rcv_qty_35w,rcv_qty_70w,rcv_qty_110w,rcv_qty_190w',

     'bufferstck_invoice_qty_18w' => 'required_without_all:bufferstck_invoice_qty_35w,bufferstck_invoice_qty_70w,bufferstck_invoice_qty_110w,bufferstck_invoice_qty_190w',

    'bufferstck_received_qty_18w' => 'required_without_all:bufferstck_received_qty_35w,bufferstck_received_qty_170w,bufferstck_received_qty_110w,bufferstck_received_qty_19w',

'drivers_received_qty_18w' => 'required_without_all:drivers_received_qty_35w,drivers_received_qty_70w,drivers_received_qty_110w,drivers_received_qty_190w',

'spd_received_qty_18w' => 'required_without_all:spd_received_qty_35w,spd_received_qty_70w,spd_received_qty_110w,spd_received_qty_190w',

'spd_invoice_qty_18w' => 'required_without_all:spd_invoice_qty_35w,spd_invoice_qty_70w,spd_invoice_qty_110w,spd_invoice_qty_190w',

);
    $messages = [
   'required_without_all' => '*At least one field is required in each invoice and received qunatity'
];     
$validation = Validator::make( $request->all(), $rules, $messages);


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

     $inward_info = new inward();
     //dd($inward_info);
         $inward_info->jilla_id = $request->input('jilla_id');
         $inward_info->maker_name = $request->input('maker_name');

         // $inward_info->num_drivers = $request->input('num_drivers');
         // $inward_info->num_drv_rcv_qty = $request->input('num_drv_rcv_qty');
         // $inward_info->num_spd = $request->input('num_spd');
         // $inward_info->num_spd_rcv_qty = $request->input('num_spd_rcv_qty');
          $inward_info->tiltable_adapters = $request->input('tiltable_adapters');
         $inward_info->tiltadap_rcv_qty = $request->input('tiltadap_rcv_qty');
         $inward_info->led_wattage_18 = $request->input('led_wattage_18');
         $inward_info->rcv_qty_18w = $request->input('rcv_qty_18w');
         $inward_info->led_wattage_35 = $request->input('led_wattage_35');
         $inward_info->rcv_qty_35w = $request->input('rcv_qty_35w');
         $inward_info->led_wattage_70 = $request->input('led_wattage_70');
         $inward_info->rcv_qty_70w = $request->input('rcv_qty_70w');
         $inward_info->led_wattage_110 = $request->input('led_wattage_110');
         $inward_info->rcv_qty_110w = $request->input('rcv_qty_110w');
         $inward_info->led_wattage_190 = $request->input('led_wattage_190');
         $inward_info->rcv_qty_190w = $request->input('rcv_qty_190w');
         // $inward_info->buffer_stock = $request->input('buffer_stock');
         // $inward_info->bufstck_rcv_qty = $request->input('bufstck_rcv_qty');
         $inward_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);
          $inward_info->files=implode(',', $insert);



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

        $inward_info->drivers_received_qty_18w = $request->input('drivers_received_qty_18w');
           $inward_info->drivers_received_qty_35w = $request->input('drivers_received_qty_35w');
           $inward_info->drivers_received_qty_70w = $request->input('drivers_received_qty_70w');
        $inward_info->drivers_received_qty_110w = $request->input('drivers_received_qty_110w');
        $inward_info->drivers_received_qty_190w = $request->input('drivers_received_qty_190w');
           $inward_info->drivers_invoice_qty_18w = $request->input('drivers_invoice_qty_18w'); 
           $inward_info->drivers_invoice_qty_35w = $request->input('drivers_invoice_qty_35w');
           $inward_info->drivers_invoice_qty_70w = $request->input('drivers_invoice_qty_70w');
           $inward_info->drivers_invoice_qty_110w = $request->input('drivers_invoice_qty_110w');
           $inward_info->drivers_invoice_qty_190w = $request->input('drivers_invoice_qty_190w');
            $inward_info->spd_received_qty_18w = $request->input('spd_received_qty_18w');
           $inward_info->spd_received_qty_35w = $request->input('spd_received_qty_35w');
           $inward_info->spd_received_qty_70w = $request->input('spd_received_qty_70w');
        $inward_info->spd_received_qty_110w = $request->input('spd_received_qty_110w');
        $inward_info->spd_received_qty_190w = $request->input('spd_received_qty_190w');
           $inward_info->spd_invoice_qty_18w = $request->input('spd_invoice_qty_18w'); 
           $inward_info->spd_invoice_qty_35w = $request->input('spd_invoice_qty_35w');
           $inward_info->spd_invoice_qty_70w = $request->input('spd_invoice_qty_70w');
           $inward_info->spd_invoice_qty_110w = $request->input('spd_invoice_qty_110w');
           $inward_info->spd_invoice_qty_190w = $request->input('spd_invoice_qty_190w');

            
           $inward_info->org_rcv_qty_18w = $request->input('rcv_qty_18w');
           $inward_info->org_rcv_qty_35w = $request->input('rcv_qty_35w');
           $inward_info->org_rcv_qty_190w = $request->input('rcv_qty_190w');
           $inward_info->org_rcv_qty_110w = $request->input('rcv_qty_110w');
           $inward_info->org_rcv_qty_70w = $request->input('rcv_qty_70w');

            $inward_info->org_bufferstck_received_qty_18w = $request->input('bufferstck_received_qty_18w');
           $inward_info->org_bufferstck_received_qty_35w = $request->input('bufferstck_received_qty_35w');
           $inward_info->org_bufferstck_received_qty_70w = $request->input('bufferstck_received_qty_70w');
           $inward_info->org_bufferstck_received_qty_110w = $request->input('bufferstck_received_qty_110w');
           $inward_info->org_bufferstck_received_qty_190w = $request->input('bufferstck_received_qty_190w');
          

        $inward_info->org_drivers_received_qty_18w = $request->input('drivers_received_qty_18w');
           $inward_info->org_drivers_received_qty_35w = $request->input('drivers_received_qty_35w');
           $inward_info->org_drivers_received_qty_70w = $request->input('drivers_received_qty_70w');
        $inward_info->org_drivers_received_qty_110w = $request->input('drivers_received_qty_110w');
        $inward_info->org_drivers_received_qty_190w = $request->input('drivers_received_qty_190w');
       
            $inward_info->org_spd_received_qty_18w = $request->input('spd_received_qty_18w');
           $inward_info->org_spd_received_qty_35w = $request->input('spd_received_qty_35w');
           $inward_info->org_spd_received_qty_70w = $request->input('spd_received_qty_70w');
        $inward_info->org_spd_received_qty_110w = $request->input('spd_received_qty_110w');
        $inward_info->org_spd_received_qty_190w = $request->input('spd_received_qty_190w');
        $inward_info->org_tiltadap_rcv_qty = $request->input('tiltadap_rcv_qty');
      


        $inward_info->invoice_date = $request->input('invoice_date');
        $inward_info->invoice_number = $request->input('invoice_number');
        $inward_info->total_rcv_qty = $request->input('total_rcv_qty');
        $inward_info->total_rcv_qty_copy = $request->input('total_rcv_qty');
         $inward_info->remarks = $request->input('remarks');
          $inward_info->date_of_receiving = $request->input('date_of_receiving');
         		
         $inward_info->save();


     $inward_info = new total_inward();
     //dd($inward_info);
         $inward_info->jilla_id = $request->input('jilla_id');
         $inward_info->maker_name = $request->input('maker_name');

         // $inward_info->num_drivers = $request->input('num_drivers');
         // $inward_info->num_drv_rcv_qty = $request->input('num_drv_rcv_qty');
         // $inward_info->num_spd = $request->input('num_spd');
         // $inward_info->num_spd_rcv_qty = $request->input('num_spd_rcv_qty');
         // $inward_info->tiltable_adapters = $request->input('tiltable_adapters');
         // $inward_info->tiltadap_rcv_qty = $request->input('tiltadap_rcv_qty');
         $inward_info->led_wattage_18 = $request->input('led_wattage_18');
         $inward_info->rcv_qty_18w = $request->input('rcv_qty_18w');
         $inward_info->led_wattage_35 = $request->input('led_wattage_35');
         $inward_info->rcv_qty_35w = $request->input('rcv_qty_35w');
         $inward_info->led_wattage_70 = $request->input('led_wattage_70');
         $inward_info->rcv_qty_70w = $request->input('rcv_qty_70w');
         $inward_info->led_wattage_110 = $request->input('led_wattage_110');
         $inward_info->rcv_qty_110w = $request->input('rcv_qty_110w');
         $inward_info->led_wattage_190 = $request->input('led_wattage_190');
         $inward_info->rcv_qty_190w = $request->input('rcv_qty_190w');
         // $inward_info->buffer_stock = $request->input('buffer_stock');
         // $inward_info->bufstck_rcv_qty = $request->input('bufstck_rcv_qty');
         $inward_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);
          $inward_info->files=implode(',', $insert);



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

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


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


        $inward_info->invoice_date = $request->input('invoice_date');
        $inward_info->invoice_number = $request->input('invoice_number');
        $inward_info->total_rcv_qty = $request->input('total_rcv_qty');
        $inward_info->total_rcv_qty_copy = $request->input('total_rcv_qty');
         $inward_info->remarks = $request->input('remarks');
          $inward_info->date_of_receiving = $request->input('date_of_receiving');
                
         $inward_info->save();
          if ($request->is("api*")) {     
            return json_encode(["error"=>false,"success"=>"Data Inserted Successfully ","status"=>$inward_info]);
        }else{
            return redirect(url('inwards_store'))->withSuccess('Added Successfully.');
        }
    }
    public function move_inventory(Request $request,$value='')
    {
        $jilla=area_master::where('parent_id',0)->get();
        $makeslist= Makeslist::get();
        // $inventory = move_inventory::with(['from_district','to_district'])->where('is_eesl',0)->orderby('created_at','desc')->get();

        $inventory=move_inventory::with(['from_district','to_district'])->select('maker_name',DB::raw("count(invoice_number) as count"),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(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(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"),'invoice_number','remarks','created_at','date_of_transfer','from_jilla_id','to_jilla_id','remarks')->where('is_eesl',0)->groupby('invoice_number')->orderby('created_at','desc')->get()->toArray();
        //dd($inventory);
        $eesl_inventory = move_inventory::with(['from_district','to_district'])->select('maker_name',DB::raw("count(invoice_number) as count"),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(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(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"),'invoice_number','remarks','created_at','date_of_transfer','from_jilla_id','to_jilla_id','remarks')->where('is_eesl',1)->groupby('invoice_number')->orderby('created_at','desc')->get()->toArray();;
        //dd($eesl_inventory);


     return view('backend.inventory.inventorylist',compact('jilla','makeslist','inventory','eesl_inventory'));
    }

    public function editinventorylist(Request $request,$value='')
    {
        $jilla=area_master::where('parent_id',0)->get();
        $makeslist= Makeslist::get();
        $inventory = move_inventory::with(['from_district','to_district'])->orderby('created_at','desc')->get();
        $eesl_inventory = move_inventory::with(['from_district','to_district'])->orderby('created_at','desc')->get();
        //dd($eesl_inventory);
     return view('backend.inventory.editinventorylist',compact('jilla','makeslist','inventory','eesl_inventory'));
    }

    public function update_editinventorylist(Request $request)
    {
        $countstore = inward::where(['jilla_id'=>$request->jilla_id,'maker_name'=>$request->maker_name])->select(DB::raw("SUM(rcv_qty_18w) as rcv_qty_18w"),DB::raw("SUM(rcv_qty_35w) as rcv_qty_35w"),DB::raw("SUM(rcv_qty_70w) as rcv_qty_70w"),DB::raw("SUM(rcv_qty_110w) as rcv_qty_110w"),DB::raw("SUM(rcv_qty_190w) as rcv_qty_190w"),DB::raw("SUM(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(bufferstck_received_qty_18w) as bufferstck_received_qty_18w"),DB::raw("SUM(bufferstck_received_qty_35w) as bufferstck_received_qty_35w"),DB::raw("SUM(bufferstck_received_qty_70w) as bufferstck_received_qty_70w"),DB::raw("SUM(bufferstck_received_qty_110w) as bufferstck_received_qty_110w"),DB::raw("SUM(bufferstck_received_qty_190w) as bufferstck_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"))->first()->toArray();

        $invoices = DB::table('inwards')
                    ->where('jilla_id','=',$request->jilla_id)
                    ->where('maker_name','=',$request->maker_name)
                    ->where('approve_status','=',0)
                    ->get();


    if(count($countstore) > 0){
        
    foreach($invoices as $in){  

        $data = array(
                'rcv_qty_18w'                 => '',
                'rcv_qty_35w'                 => '',
                'rcv_qty_70w'                 => '',
                'rcv_qty_110w'                => '',
                'rcv_qty_190w'                => '',
                'drivers_received_qty_18w'    => '',
                'drivers_received_qty_35w'    => '',
                'drivers_received_qty_70w'    => '',
                'drivers_received_qty_110w'   => '',
                'drivers_received_qty_190w'   => '',
                'bufferstck_received_qty_18w' => '',
                'bufferstck_received_qty_35w' => '',
                'bufferstck_received_qty_70w' => '',
                'bufferstck_received_qty_110w'=> '',
                'bufferstck_received_qty_190w'=> '',
                'spd_received_qty_18w'        => '',
                'spd_received_qty_35w'        => '',
                'spd_received_qty_70w'        => '',
                'spd_received_qty_110w'       => '',
                'spd_received_qty_190w'       => ''
            );   
            
    if( $request->rcv_qty_18w != '' || $request->rcv_qty_35w != '' || $request->rcv_qty_70w != '' || $request->rcv_qty_110w != '' || $request->rcv_qty_190w != '' || $request->bufferstck_received_qty_18w != '' || $request->bufferstck_received_qty_35w != '' || $request->bufferstck_received_qty_70w != '' || $request->bufferstck_received_qty_110w != '' || $request->bufferstck_received_qty_190w != '' || $request->drivers_received_qty_18w != '' || $request->drivers_received_qty_35w != '' || $request->drivers_received_qty_70w != '' || $request->drivers_received_qty_110w != '' || $request->drivers_received_qty_190w != '' || $request->spd_received_qty_18w != '' || $request->spd_received_qty_35w != '' || $request->spd_received_qty_70w != '' || $request->spd_received_qty_110w != '' || $request->spd_received_qty_190w != '' ){
            

        $inwardchec = inward::where(['jilla_id'=>$request->jilla_id,"maker_name"=>$request->maker_name,'invoice_number'=> $in->invoice_number, 'id' => $in->id])->first();

     
        if(!empty($inwardchec)){
             $inwardchec->jilla_id = $inwardchec->jilla_id;
            $inwardchec->maker_name = $inwardchec->maker_name;
             $inwardchec->invoice_number = $inwardchec->invoice_number;
             $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){   
                if($request->rcv_qty_18w <= $inwardchec->rcv_qty_18w){ 
                    $data['rcv_qty_18w'] = $request->rcv_qty_18w;
                    $request->rcv_qty_18w = '';    
                }else{  
                    $data['rcv_qty_18w'] = $inwardchec->rcv_qty_18w;
                    $request->rcv_qty_18w = $request->rcv_qty_18w - $inwardchec->rcv_qty_18w;
                }          
            
                $inwardchec->rcv_qty_18w=$inwardchec->rcv_qty_18w-$data['rcv_qty_18w'];
            }
            if($request->rcv_qty_35w){
               if($request->rcv_qty_35w <= $inwardchec->rcv_qty_35w){ 
                    $data['rcv_qty_35w'] = $request->rcv_qty_35w;
                    $request->rcv_qty_35w = '';    
                }else{  
                    $data['rcv_qty_35w'] = $inwardchec->rcv_qty_35w;
                    $request->rcv_qty_35w = $request->rcv_qty_35w - $inwardchec->rcv_qty_35w;
                }
                $inwardchec->rcv_qty_35w= $inwardchec->rcv_qty_35w-$data['rcv_qty_35w'];
            }
             if($request->rcv_qty_70w){                 
                if($request->rcv_qty_70w <= $inwardchec->rcv_qty_70w){ 
                    $data['rcv_qty_70w'] = $request->rcv_qty_70w;
                    $request->rcv_qty_70w = '';    
                }else{  
                    $data['rcv_qty_70w'] = $inwardchec->rcv_qty_70w;
                    $request->rcv_qty_70w = $request->rcv_qty_70w - $inwardchec->rcv_qty_70w;
                }
                $inwardchec->rcv_qty_70w=$inwardchec->rcv_qty_70w-$data['rcv_qty_70w'];
            }
            if($request->rcv_qty_110w){

                if($request->rcv_qty_110w <= $inwardchec->rcv_qty_110w){ 
                    $data['rcv_qty_110w'] = $request->rcv_qty_110w;
                    $request->rcv_qty_110w = '';    
                }else{  
                    $data['rcv_qty_110w'] = $inwardchec->rcv_qty_110w;
                    $request->rcv_qty_110w = $request->rcv_qty_110w - $inwardchec->rcv_qty_110w;
                }
                $inwardchec->rcv_qty_110w= $inwardchec->rcv_qty_110w-$data['rcv_qty_110w'];
            }
             if($request->rcv_qty_190w){ 
                if($request->rcv_qty_190w <= $inwardchec->rcv_qty_190w){ 
                    $data['rcv_qty_190w'] = $request->rcv_qty_190w;
                    $request->rcv_qty_190w = '';    
                }else{  
                    $data['rcv_qty_190w'] = $inwardchec->rcv_qty_190w;
                    $request->rcv_qty_190w = $request->rcv_qty_190w - $inwardchec->rcv_qty_190w;
                }                
                $inwardchec->rcv_qty_190w=$inwardchec->rcv_qty_190w-$data['rcv_qty_190w'];
            }



            
            if($request->bufferstck_received_qty_18w){
                if($request->bufferstck_received_qty_18w <= $inwardchec->bufferstck_received_qty_18w){ 
                    $data['bufferstck_received_qty_18w'] = $request->bufferstck_received_qty_18w;
                    $request->bufferstck_received_qty_18w = '';    
                }else{  
                    $data['bufferstck_received_qty_18w'] = $inwardchec->bufferstck_received_qty_18w;
                    $request->bufferstck_received_qty_18w = $request->bufferstck_received_qty_18w - $inwardchec->bufferstck_received_qty_18w;
                }
                $inwardchec->bufferstck_received_qty_18w= $inwardchec->bufferstck_received_qty_18w-$data['bufferstck_received_qty_18w'];
            }

             if($request->bufferstck_received_qty_35w){
                if($request->bufferstck_received_qty_35w <= $inwardchec->bufferstck_received_qty_35w){ 
                    $data['bufferstck_received_qty_35w'] = $request->bufferstck_received_qty_35w;
                    $request->bufferstck_received_qty_35w = '';    
                }else{  
                    $data['bufferstck_received_qty_35w'] = $inwardchec->bufferstck_received_qty_35w;
                    $request->bufferstck_received_qty_35w = $request->bufferstck_received_qty_35w - $inwardchec->bufferstck_received_qty_35w;
                }
                $inwardchec->bufferstck_received_qty_35w= $inwardchec->bufferstck_received_qty_35w-$data['bufferstck_received_qty_35w'];
            }
             if($request->bufferstck_received_qty_110w){    
                if($request->bufferstck_received_qty_110w <= $inwardchec->bufferstck_received_qty_110w){ 
                    $data['bufferstck_received_qty_110w'] = $request->bufferstck_received_qty_110w;
                    $request->bufferstck_received_qty_110w = '';    
                }else{  
                    $data['bufferstck_received_qty_110w'] = $inwardchec->bufferstck_received_qty_110w;
                    $request->bufferstck_received_qty_110w = $request->bufferstck_received_qty_110w - $inwardchec->bufferstck_received_qty_110w;
                }             
                $inwardchec->bufferstck_received_qty_110w=$inwardchec->bufferstck_received_qty_110w-$data['bufferstck_received_qty_110w'];
            }
            if($request->bufferstck_received_qty_70w){
                if($request->bufferstck_received_qty_70w <= $inwardchec->bufferstck_received_qty_70w){ 
                    $data['bufferstck_received_qty_70w'] = $request->bufferstck_received_qty_70w;
                    $request->bufferstck_received_qty_70w = '';    
                }else{  
                    $data['bufferstck_received_qty_70w'] = $inwardchec->bufferstck_received_qty_70w;
                    $request->bufferstck_received_qty_70w = $request->bufferstck_received_qty_70w - $inwardchec->bufferstck_received_qty_70w;
                }
                $inwardchec->bufferstck_received_qty_70w= $inwardchec->bufferstck_received_qty_70w-$data['bufferstck_received_qty_70w'];
            }
             if($request->bufferstck_received_qty_190w){     
                if($request->bufferstck_received_qty_190w <= $inwardchec->bufferstck_received_qty_190w){ 
                    $data['bufferstck_received_qty_190w'] = $request->bufferstck_received_qty_190w;
                    $request->bufferstck_received_qty_190w = '';    
                }else{  
                    $data['bufferstck_received_qty_190w'] = $inwardchec->bufferstck_received_qty_190w;
                    $request->bufferstck_received_qty_190w = $request->bufferstck_received_qty_190w - $inwardchec->bufferstck_received_qty_190w;
                }            
                $inwardchec->bufferstck_received_qty_190w=$inwardchec->bufferstck_received_qty_190w-$data['bufferstck_received_qty_190w'];
            }


        
            if($request->drivers_received_qty_18w){
                if($request->drivers_received_qty_18w <= $inwardchec->drivers_received_qty_18w){ 
                    $data['drivers_received_qty_18w'] = $request->drivers_received_qty_18w;
                    $request->drivers_received_qty_18w = '';    
                }else{  
                    $data['drivers_received_qty_18w'] = $inwardchec->drivers_received_qty_18w;
                    $request->drivers_received_qty_18w = $request->drivers_received_qty_18w - $inwardchec->drivers_received_qty_18w;
                }
                $inwardchec->drivers_received_qty_18w= $inwardchec->drivers_received_qty_18w-$data['drivers_received_qty_18w'];
            }

             if($request->drivers_received_qty_35w){
                if($request->drivers_received_qty_35w <= $inwardchec->drivers_received_qty_35w){ 
                    $data['drivers_received_qty_35w'] = $request->drivers_received_qty_35w;
                    $request->drivers_received_qty_35w = '';    
                }else{  
                    $data['drivers_received_qty_35w'] = $inwardchec->drivers_received_qty_35w;
                    $request->drivers_received_qty_35w = $request->drivers_received_qty_35w - $inwardchec->drivers_received_qty_35w;
                }
                $inwardchec->drivers_received_qty_35w= $inwardchec->drivers_received_qty_35w-$data['drivers_received_qty_35w'];
            }
             if($request->drivers_received_qty_70w){   
                if($request->drivers_received_qty_70w <= $inwardchec->drivers_received_qty_70w){ 
                    $data['drivers_received_qty_70w'] = $request->drivers_received_qty_70w;
                    $request->drivers_received_qty_70w = '';    
                }else{  
                    $data['drivers_received_qty_70w'] = $inwardchec->drivers_received_qty_70w;
                    $request->drivers_received_qty_70w = $request->drivers_received_qty_70w - $inwardchec->drivers_received_qty_70w;
                }              
                $inwardchec->drivers_received_qty_70w=$inwardchec->drivers_received_qty_70w-$data['drivers_received_qty_70w'];
            }
            if($request->drivers_received_qty_110w){
                if($request->drivers_received_qty_110w <= $inwardchec->drivers_received_qty_110w){ 
                    $data['drivers_received_qty_110w'] = $request->drivers_received_qty_110w;
                    $request->drivers_received_qty_110w = '';    
                }else{  
                    $data['drivers_received_qty_110w'] = $inwardchec->drivers_received_qty_110w;
                    $request->drivers_received_qty_110w = $request->drivers_received_qty_110w - $inwardchec->drivers_received_qty_110w;
                }
                $inwardchec->drivers_received_qty_110w= $inwardchec->drivers_received_qty_110w-$data['drivers_received_qty_110w'];
            }
             if($request->drivers_received_qty_190w){       
                if($request->drivers_received_qty_190w <= $inwardchec->drivers_received_qty_190w){ 
                    $data['drivers_received_qty_190w'] = $request->drivers_received_qty_190w;
                    $request->drivers_received_qty_190w = '';    
                }else{  
                    $data['drivers_received_qty_190w'] = $inwardchec->drivers_received_qty_190w;
                    $request->drivers_received_qty_190w = $request->drivers_received_qty_190w - $inwardchec->drivers_received_qty_190w;
                }          
                $inwardchec->drivers_received_qty_190w=$inwardchec->drivers_received_qty_190w-$data['drivers_received_qty_190w'];
            }



            if($request->spd_received_qty_18w){
                if($request->spd_received_qty_18w <= $inwardchec->spd_received_qty_18w){ 
                    $data['spd_received_qty_18w'] = $request->spd_received_qty_18w;
                    $request->spd_received_qty_18w = '';    
                }else{  
                    $data['spd_received_qty_18w'] = $inwardchec->spd_received_qty_18w;
                    $request->spd_received_qty_18w = $request->spd_received_qty_18w - $inwardchec->spd_received_qty_18w;
                }
                $inwardchec->spd_received_qty_18w= $inwardchec->spd_received_qty_18w-$data['spd_received_qty_18w'];
            }

             if($request->spd_received_qty_35w){
                if($request->spd_received_qty_35w <= $inwardchec->spd_received_qty_35w){ 
                    $data['spd_received_qty_35w'] = $request->spd_received_qty_35w;
                    $request->spd_received_qty_35w = '';    
                }else{  
                    $data['spd_received_qty_35w'] = $inwardchec->spd_received_qty_35w;
                    $request->spd_received_qty_35w = $request->spd_received_qty_35w - $inwardchec->spd_received_qty_35w;
                }
                $inwardchec->spd_received_qty_35w= $inwardchec->spd_received_qty_35w-$data['spd_received_qty_35w'];
            }
             if($request->spd_received_qty_70w){
                if($request->spd_received_qty_70w <= $inwardchec->spd_received_qty_70w){ 
                    $data['spd_received_qty_70w'] = $request->spd_received_qty_70w;
                    $request->spd_received_qty_70w = '';    
                }else{  
                    $data['spd_received_qty_70w'] = $inwardchec->spd_received_qty_70w;
                    $request->spd_received_qty_70w = $request->spd_received_qty_70w - $inwardchec->spd_received_qty_70w;
                }                 
                $inwardchec->spd_received_qty_70w=$inwardchec->spd_received_qty_70w-$data['spd_received_qty_70w'];
            }
            if($request->spd_received_qty_110w){

                if($request->spd_received_qty_110w <= $inwardchec->spd_received_qty_110w){ 
                    $data['spd_received_qty_110w'] = $request->spd_received_qty_110w;
                    $request->spd_received_qty_110w = '';    
                }else{  
                    $data['spd_received_qty_110w'] = $inwardchec->spd_received_qty_110w;
                    $request->spd_received_qty_110w = $request->spd_received_qty_110w - $inwardchec->spd_received_qty_110w;
                }
                $inwardchec->spd_received_qty_110w= $inwardchec->spd_received_qty_110w-$data['spd_received_qty_110w'];
            }
             if($request->spd_received_qty_190w){    
                if($request->spd_received_qty_190w <= $inwardchec->spd_received_qty_190w){ 
                    $data['spd_received_qty_190w'] = $request->spd_received_qty_190w;
                    $request->spd_received_qty_190w = '';    
                }else{  
                    $data['spd_received_qty_190w'] = $inwardchec->spd_received_qty_190w;
                    $request->spd_received_qty_190w = $request->spd_received_qty_190w - $inwardchec->spd_received_qty_190w;
                }             
                $inwardchec->spd_received_qty_190w=$inwardchec->spd_received_qty_190w-$data['spd_received_qty_190w'];
            }

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

            $move_inventory_extra_data = array(
                'from_jilla_id'               => $request->jilla_id,
                'from_invoice_number'         => $in->invoice_number,
                'to_invoice_number'           => $in->invoice_number.'_edit',
                'maker_name'                  => $request->maker_name,
                'to_jilla_id'                 => $request->to_jilla_id,
            );
            
            $move_inventory_invoice_data = array_merge($data,$move_inventory_extra_data);
            
            $inwards_edit_data = array_merge($data,['jilla_id' => $request->to_jilla_id, 'maker_name' => $request->maker_name, 'invoice_number' => $in->invoice_number.'_edit']);
            
            $res = DB::table('move_inventory_invoices')->insert($move_inventory_invoice_data);
            
            $inwards_res = inward::create($inwards_edit_data);
          }

        }

    }

    return redirect(url('editinventorylist'))->with('success',"Successfully moved Lights");

        }else{
            return redirect(url('editinventorylist'))->with('error',"This Wattage quantity not available in this district");
        }

        //return redirect(url('move_inventory'))->with('success',"Successfully moved Lights");
        

    }

    
public function update_inventory(Request $request){ 


    $inwardchecks = inward::where(['jilla_id'=>$request->jilla_id,"maker_name"=>$request->maker_name,'approve_status'=>'0'])->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();

         
            $to_jilla=$request->to_jilla_id;
            $from_jilla=$request->jilla_id;

            if($request->to_jilla_id=="EESL"){
            $totring = 'EESL';
             $frominvoice=$inwardchec->invoice_number;
            $fromtring = area_master::where('id',$request->jilla_id)->first();
            $toinvoice=$fromtring->area_name."-to-".$totring."-".time();
            $is_eesl='1';
        }else{

            $totring = area_master::where('id',$request->to_jilla_id)->first();
            $fromtring = area_master::where('id',$request->jilla_id)->first();
            $frominvoice=$inwardchec->invoice_number;
            $toinvoice=$fromtring->area_name."-to-".$totring->area_name."-".time();
            $is_eesl='0';
        }




            // dd($insert_18w,$insert_35w,$insert_70w,$insert_110w,$insert_190w,$insert_buf18w,$insert_buf35w,$insert_buf70w,$insert_buf110w,$insert_buf190w,$insert_drv18w,$insert_drv35w,$insert_drv70w,$insert_drv110w,$insert_drv190w,$insert_spd18w,$insert_spd35w,$insert_spd70w,$insert_spd110w,$insert_spd190w,$insert_tltadp);

         move_inventory_invoices::create(['from_jilla_id'=>$from_jilla, 'maker_name'=>$request->maker_name, 'from_invoice_number'=>$frominvoice,'to_invoice_number'=>$toinvoice,'to_jilla_id'=>$to_jilla,'inward_id'=>$inwardchec->id,'status'=>1,"date_of_transfer"=>date('Y-m-d',strtotime($request->date_of_receiving)),"remarks"=>$request->remarks,'is_eesl'=>$is_eesl,
                     '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)]);
    }

  //  exit();

     $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;
         }  
             $invoice='';
            $to_jilla=$request->to_jilla_id;
             $from_jilla=$request->jilla_id;

        if($request->to_jilla_id=="EESL"){
              $totring = 'EESL';
              $fromtring = area_master::where('id',$request->jilla_id)->first();
            $invoice=$fromtring->area_name."-to-".$totring."-".time();

             inward::create(['jilla_id'=>$to_jilla, 'maker_name'=>$request->maker_name,'date_of_receiving'=>$request->date_of_receiving,'remarks'=>$request->remarks,
   
                'invoice_number'=>$invoice,
                  'rcv_qty_18w'=>$request->rcv_qty_18w??0,
                    'rcv_qty_35w'=>$request->rcv_qty_35w??0,
                    'rcv_qty_70w'=>$request->rcv_qty_70w??0,
                    'rcv_qty_110w'=>$request->rcv_qty_110w??0,
                    'rcv_qty_190w'=>$request->rcv_qty_190w??0,

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

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

                     'num_spd_rcv_qty'=>$request->num_spd_rcv_qty??0,
                     'spd_received_qty_18w'=>$request->spd_received_qty_18w??0,
                     'spd_received_qty_35w'=>$request->spd_received_qty_35w??0,
                     'spd_received_qty_70w'=>$request->spd_received_qty_70w??0,
                     'spd_received_qty_110w'=>$request->spd_received_qty_110w??0,
                     'spd_received_qty_190w'=>$request->spd_received_qty_190w??0,
                     'type_of_invoice'=>1,


                    //  'org_rcv_qty_18w'=>$request->rcv_qty_18w,
                    // 'org_rcv_qty_35w'=>$request->rcv_qty_35w,
                    // 'org_rcv_qty_70w'=>$request->rcv_qty_70w,
                    // 'org_rcv_qty_110w'=>$request->rcv_qty_110w,
                    // 'org_rcv_qty_190w'=>$request->rcv_qty_190w,

                    //  'org_bufferstck_received_qty_18w'=>$request->bufferstck_received_qty_18w,
                    //  'org_bufferstck_received_qty_35w'=>$request->bufferstck_received_qty_35w,
                    //  'org_bufferstck_received_qty_70w'=>$request->bufferstck_received_qty_70w, 
                    //  'org_bufferstck_received_qty_110w'=>$request->bufferstck_received_qty_110w,
                    //  'org_bufferstck_received_qty_190w'=>$request->bufferstck_received_qty_190w,

                    //  'org_drivers_received_qty_18w'=>$request->drivers_received_qty_18w, 
                    //  'org_drivers_received_qty_35w'=>$request->drivers_received_qty_35w,
                    //  'org_drivers_received_qty_70w'=>$request->drivers_received_qty_70w,
                    //  'org_drivers_received_qty_110w'=>$request->drivers_received_qty_110w,
                    //  'org_drivers_received_qty_190w'=>$request->drivers_received_qty_190w,


                    //  'org_spd_received_qty_18w'=>$request->spd_received_qty_18w,
                    //  'org_spd_received_qty_35w'=>$request->spd_received_qty_35w,
                    //  'org_spd_received_qty_70w'=>$request->spd_received_qty_70w,
                    //  'org_spd_received_qty_110w'=>$request->spd_received_qty_110w,
                    //  'org_spd_received_qty_190w'=>$request->spd_received_qty_190w,
                     // 'total_rcv_qty'=>$request->total_rcv_qty,
                    'files'=>implode(',', $insert)]);

             move_inventory::create(['from_jilla_id'=>$from_jilla, 'maker_name'=>$request->maker_name, 'invoice_number'=>$invoice,'to_jilla_id'=>$to_jilla,'inward_id'=>$inwardchec->id,'status'=>1,"date_of_transfer"=>date('Y-m-d',strtotime($request->date_of_receiving)),"remarks"=>$request->remarks,'is_eesl'=>1,
                '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,
             "files"=>implode(',', $insert)]);
        return redirect(url('move_inventory'))->with('success',"Successfully moved Lights");

        }else{


            $totring = area_master::where('id',$request->to_jilla_id)->first();
              $fromtring = area_master::where('id',$request->jilla_id)->first();

             $invoice=$fromtring->area_name."-to-".$totring->area_name."-".time();
            
            inward::create(['jilla_id'=>$to_jilla, 'maker_name'=>$request->maker_name,'date_of_receiving'=>$request->date_of_receiving,'remarks'=>$request->remarks,

                'invoice_number'=>$invoice,

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

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

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

                     'num_spd_rcv_qty'=>$request->num_spd_rcv_qty??0,
                     'spd_received_qty_18w'=>$request->spd_received_qty_18w??0,
                     'spd_received_qty_35w'=>$request->spd_received_qty_35w??0,
                     'spd_received_qty_70w'=>$request->spd_received_qty_70w??0,
                     'spd_received_qty_110w'=>$request->spd_received_qty_110w??0,
                     'spd_received_qty_190w'=>$request->spd_received_qty_190w??0,
                     'type_of_invoice'=>1,


                    //  'org_rcv_qty_18w'=>$request->rcv_qty_18w??0,
                    // 'org_rcv_qty_35w'=>$request->rcv_qty_35w??0,
                    // 'org_rcv_qty_70w'=>$request->rcv_qty_70w??0,
                    // 'org_rcv_qty_110w'=>$request->rcv_qty_110w??0,
                    // 'org_rcv_qty_190w'=>$request->rcv_qty_190w??0,

                    //  'org_bufferstck_received_qty_18w'=>$request->bufferstck_received_qty_18w??0,
                    //  'org_bufferstck_received_qty_35w'=>$request->bufferstck_received_qty_35w??0,
                    //  'org_bufferstck_received_qty_70w'=>$request->bufferstck_received_qty_70w??0, 
                    //  'org_bufferstck_received_qty_110w'=>$request->bufferstck_received_qty_110w??0,
                    //  'org_bufferstck_received_qty_190w'=>$request->bufferstck_received_qty_190w??0,

                    //  'org_drivers_received_qty_18w'=>$request->drivers_received_qty_18w??0, 
                    //  'org_drivers_received_qty_35w'=>$request->drivers_received_qty_35w??0,
                    //  'org_drivers_received_qty_70w'=>$request->drivers_received_qty_70w??0,
                    //  'org_drivers_received_qty_110w'=>$request->drivers_received_qty_110w??0,
                    //  'org_drivers_received_qty_190w'=>$request->drivers_received_qty_190w??0,


                    //  'org_spd_received_qty_18w'=>$request->spd_received_qty_18w??0,
                    //  'org_spd_received_qty_35w'=>$request->spd_received_qty_35w??0,
                    //  'org_spd_received_qty_70w'=>$request->spd_received_qty_70w??0,
                    //  'org_spd_received_qty_110w'=>$request->spd_received_qty_110w??0,
                    //  'org_spd_received_qty_190w'=>$request->spd_received_qty_190w??0,
                     // 'total_rcv_qty'=>$request->total_rcv_qty,
                    'files'=>implode(',', $insert)]);
       
            

            move_inventory::create(['from_jilla_id'=>$from_jilla, 'maker_name'=>$request->maker_name, 'invoice_number'=>$invoice,'to_jilla_id'=>$to_jilla,'inward_id'=>$inwardchec->id,'status'=>1,"date_of_transfer"=>date('Y-m-d',strtotime($request->date_of_receiving)),"remarks"=>$request->remarks,"is_eesl"=>0,
                '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,
             "files"=>implode(',', $insert)]);
             return redirect(url('move_inventory'))->with('success',"Successfully moved Lights");
         }
        
        // else{
        //     return redirect(url('move_inventory'))->with('error',"This Wattage quantity not available in this district");
       //  }
       // } 
    }

    
     public function inwards_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=inward::with(['area_district' => function ($query) {
        $query->select('id', 'area_name');
        }])->select('maker_name',DB::raw("count(invoice_number) as count"),DB::raw("SUM(org_rcv_qty_18w) as 18w"),DB::raw("SUM(org_rcv_qty_35w) as 35w"),DB::raw("SUM(org_rcv_qty_70w) as 70w"),DB::raw("SUM(org_rcv_qty_110w) as 110w"),DB::raw("SUM(org_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(org_bufferstck_received_qty_18w) as buffer_stk_rcv_18"),DB::raw("SUM(org_bufferstck_received_qty_35w) as buffer_stk_rcv_35"),DB::raw("SUM(org_bufferstck_received_qty_70w) as buffer_stk_rcv_70"),DB::raw("SUM(org_bufferstck_received_qty_110w) as buffer_stk_rcv_110"),DB::raw("SUM(org_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"),DB::raw("SUM(org_drivers_received_qty_18w) as org_drivers_received_qty_18w"),DB::raw("SUM(org_drivers_received_qty_35w) as org_drivers_received_qty_35w"),DB::raw("SUM(org_drivers_received_qty_70w) as org_drivers_received_qty_70w"),DB::raw("SUM(org_drivers_received_qty_110w) as org_drivers_received_qty_110w"),DB::raw("SUM(org_drivers_received_qty_190w) as org_drivers_received_qty_190w"),DB::raw("SUM(org_spd_received_qty_18w) as org_spd_received_qty_18w"),DB::raw("SUM(org_spd_received_qty_35w) as org_spd_received_qty_35w"),DB::raw("SUM(org_spd_received_qty_70w) as org_spd_received_qty_70w"),DB::raw("SUM(org_spd_received_qty_110w) as org_spd_received_qty_110w"),DB::raw("SUM(org_spd_received_qty_190w) as org_spd_received_qty_190w"),DB::raw("SUM(org_tiltadap_rcv_qty) as org_tiltadap_rcv_qty"),'invoice_number','phase_no','jilla_id','approve_status','remarks','created_at','date_of_receiving')->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('invoice_number')->where('type_of_invoice','=','0')->orderby('date_of_receiving','Asc')->where('jilla_id',$request->id)->get()->toArray();
        //dd($area_invoice);

         $area_balance=inward::with(['area_district' => function ($query) {
        $query->select('id', 'area_name');
        }])->select(DB::raw("SUM(rcv_qty_18w) as n18w"),DB::raw("SUM(rcv_qty_35w) as n35w"),DB::raw("SUM(rcv_qty_70w) as n70w"),DB::raw("SUM(rcv_qty_110w) as n110w"),DB::raw("SUM(rcv_qty_190w) as n190w"),DB::raw("SUM(bufferstck_received_qty_18w) as n_buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as n_buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as n_buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as n_buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as n_buffer_stk_rcv_190"),DB::raw("SUM(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"),DB::raw("SUM(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"),'jilla_id')->groupby('jilla_id')->where('jilla_id',$request->id)->get()->toArray();
         //dd($area_balance);

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

         $maker_counts=inward::with(['area_district' => function ($query) {
        $query->select('id', 'area_name');
        }])->select('maker_name',DB::raw("count(maker_name) as countss"),DB::raw("SUM(org_rcv_qty_18w) as 18w"),DB::raw("SUM(org_rcv_qty_35w) as 35w"),DB::raw("SUM(org_rcv_qty_70w) as 70w"),DB::raw("SUM(org_rcv_qty_110w) as 110w"),DB::raw("SUM(org_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(org_bufferstck_received_qty_18w) as buffer_stk_rcv_18"),DB::raw("SUM(org_bufferstck_received_qty_35w) as buffer_stk_rcv_35"),DB::raw("SUM(org_bufferstck_received_qty_70w) as buffer_stk_rcv_70"),DB::raw("SUM(org_bufferstck_received_qty_110w) as buffer_stk_rcv_110"),DB::raw("SUM(org_bufferstck_received_qty_190w) as buffer_stk_rcv_190"),DB::raw("SUM(org_drivers_received_qty_18w) as org_drivers_received_qty_18w"),DB::raw("SUM(org_drivers_received_qty_35w) as org_drivers_received_qty_35w"),DB::raw("SUM(org_drivers_received_qty_70w) as org_drivers_received_qty_70w"),DB::raw("SUM(org_drivers_received_qty_110w) as org_drivers_received_qty_110w"),DB::raw("SUM(org_drivers_received_qty_190w) as org_drivers_received_qty_190w"),DB::raw("SUM(org_spd_received_qty_18w) as org_spd_received_qty_18w"),DB::raw("SUM(org_spd_received_qty_35w) as org_spd_received_qty_35w"),DB::raw("SUM(org_spd_received_qty_70w) as org_spd_received_qty_70w"),DB::raw("SUM(org_spd_received_qty_110w) as org_spd_received_qty_110w"),DB::raw("SUM(org_spd_received_qty_190w) as org_spd_received_qty_190w"),DB::raw("SUM(org_tiltadap_rcv_qty) as org_tiltadap_rcv_qty"),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','date_of_receiving')->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();

        // /dd($maker_counts);
       
        $district  = area_master::where('id',$request->id)->first();
       // $inventory = move_inventory::with(['from_district','to_district'])->where('from_jilla_id',$request->id)->orderby('created_at','desc')->get();
        //dd($inventory);

            $inventory=move_inventory::with(['from_district','to_district'])->select('maker_name',DB::raw("count(invoice_number) as count"),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(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(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"),'invoice_number','remarks','created_at','date_of_transfer','from_jilla_id','to_jilla_id','remarks')->groupby('invoice_number')->where('from_jilla_id',$request->id)->orderby('created_at','asc')->get()->toArray();
         //dd($inventory);

            $inventory_total=move_inventory::with(['from_district','to_district'])->select(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(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(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"),'from_jilla_id')->where('from_jilla_id',$request->id)->get()->toArray();
            //dd($inventory_total);


         $makers_inventory_total=move_inventory::with(['from_district','to_district'])->select(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(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(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"),'from_jilla_id','maker_name')->where('from_jilla_id',$request->id)->groupby('maker_name')->get()->toArray();
         //dd($makers_inventory_total);


    //      $area_dis=inward::has('area_district')->with(['area_district'=> function ($query) {
    //     $query->select('id', 'area_name');
    // }])->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();


       return view('backend.inwards_invoice_list',compact('area_invoice','district','maker_counts','inventory','area_balance','inventory_total','makers_inventory_total'));
    }

   

     public function inward_edit ($id)
    
    {
        $inward=inward::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.inward_edit',compact('inward','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();

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

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

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


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

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


            $inward->spd_received_qty_18w = $request->get('spd_received_qty_18w');
           $inward->spd_received_qty_35w = $request->get('spd_received_qty_35w');
           $inward->spd_received_qty_70w = $request->get('spd_received_qty_70w');
        $inward->spd_received_qty_110w = $request->get('spd_received_qty_110w');
        $inward->spd_received_qty_190w = $request->get('spd_received_qty_190w');
           $inward->spd_invoice_qty_18w = $request->get('spd_invoice_qty_18w'); 
           $inward->spd_invoice_qty_35w = $request->get('spd_invoice_qty_35w');
           $inward->spd_invoice_qty_70w = $request->get('spd_invoice_qty_70w');
           $inward->spd_invoice_qty_110w = $request->get('spd_invoice_qty_110w');
           $inward->spd_invoice_qty_190w = $request->get('spd_invoice_qty_190w');
           $inward->remarks = $request->get('remarks');
           
                    $inward->org_tiltadap_rcv_qty = $request->get('tiltadap_rcv_qty');
                    $inward->org_rcv_qty_18w = $request->get('rcv_qty_18w');
                    $inward->org_rcv_qty_35w = $request->get('rcv_qty_35w');
                    $inward->org_rcv_qty_70w = $request->get('rcv_qty_70w');
                    $inward->org_rcv_qty_110w = $request->get('rcv_qty_110w');
                    $inward->org_rcv_qty_190w = $request->get('rcv_qty_190w');

                     $inward->org_bufferstck_received_qty_18w = $request->get('bufferstck_received_qty_18w');
                     $inward->org_bufferstck_received_qty_35w = $request->get('bufferstck_received_qty_35w');
                     $inward->org_bufferstck_received_qty_70w = $request->get('bufferstck_received_qty_70w'); 
                     $inward->org_bufferstck_received_qty_110w = $request->get('bufferstck_received_qty_110w');
                     $inward->org_bufferstck_received_qty_190w = $request->get('bufferstck_received_qty_190w');

                     $inward->org_drivers_received_qty_18w = $request->get('drivers_received_qty_18w'); 
                     $inward->org_drivers_received_qty_35w = $request->get('drivers_received_qty_35w');
                     $inward->org_drivers_received_qty_70w = $request->get('drivers_received_qty_70w');
                     $inward->org_drivers_received_qty_110w = $request->get('drivers_received_qty_110w');
                     $inward->org_drivers_received_qty_190w = $request->get('drivers_received_qty_190w');


                     $inward->org_spd_received_qty_18w = $request->get('spd_received_qty_18w');
                     $inward->org_spd_received_qty_35w = $request->get('spd_received_qty_35w');
                     $inward->org_spd_received_qty_70w = $request->get('spd_received_qty_70w');
                     $inward->org_spd_received_qty_110w = $request->get('spd_received_qty_110w');
                     $inward->org_spd_received_qty_190w = $request->get('spd_received_qty_190w');


        $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 update_inward_status(Request $request)
    {
		// $phase = inward::where('id',$request->id)->first();	
		// if($phase){
		// 	$phase_no =$phase->phase_no+1;
		// }else{
		// 	$phase_no =1;
		// }
        //dd($request->all());
        // $inward=inward::where('id',$request->id)->update(['approve_status'=>$request->approve_status]);
       // ,'phase_no'=>$phase_no,
        //dd($inward);

         $inwardchec=inward::where('id',$request->id)->first();
          $inwardcheck=outward::where('id',$request->id)->first();
         //dd($inwardchec);

            
        if(!empty($inwardchec)){

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

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

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

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

                     'num_spd_rcv_qty'=>$inwardchec->num_spd_rcv_qty,
                     'spd_received_qty_18w'=>$inwardchec->spd_received_qty_18w,
                     'spd_received_qty_35w'=>$inwardchec->spd_received_qty_35w,
                     'spd_received_qty_70w'=>$inwardchec->spd_received_qty_70w,
                     'spd_received_qty_110w'=>$inwardchec->spd_received_qty_110w,
                     'spd_received_qty_190w'=>$inwardchec->spd_received_qty_190w,
                      'files'=>$inwardchec->files,
                      'tiltable_adapters'=>$inwardchec->tiltable_adapters,
                      'tiltadap_rcv_qty'=>$inwardchec->tiltadap_rcv_qty,
                      'approve_status'=>$request->approve_status
                     // 'total_rcv_qty'=>$inwardchec->total_rcv_qty,
                   ]);

              inwards_vendor::create(['jilla_id'=>$inwardchec->jilla_id, 'maker_name'=>$inwardchec->maker_name,'vendor_id'=>$inwardchec->vendor_id,'invoice_number'=>$inwardchec->invoice_number,'date_of_receiving'=>$inwardchec->date_of_receiving,

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

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

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

                     'num_spd_rcv_qty'=>$inwardchec->num_spd_rcv_qty,
                     'spd_received_qty_18w'=>$inwardchec->spd_received_qty_18w,
                     'spd_received_qty_35w'=>$inwardchec->spd_received_qty_35w,
                     'spd_received_qty_70w'=>$inwardchec->spd_received_qty_70w,
                     'spd_received_qty_110w'=>$inwardchec->spd_received_qty_110w,
                     'spd_received_qty_190w'=>$inwardchec->spd_received_qty_190w,
                      'files'=>$inwardchec->files,
                      'tiltable_adapters'=>$inwardchec->tiltable_adapters,
                      'tiltadap_rcv_qty'=>$inwardchec->tiltadap_rcv_qty,
                      'approve_status'=>$request->approve_status
                     // 'total_rcv_qty'=>$inwardchec->total_rcv_qty,
                   ]);


    //         $inwardchec->jilla_id = $inwardchec->jilla_id;
    //         $inwardchec->maker_name = $inwardchec->maker_name;
    //         $insert=[];

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



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

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


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

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




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

    //          if($inwardchec->spd_received_qty_35w){
    //            $inwardchec->spd_received_qty_35w= $inwardchec->spd_received_qty_35w-$inwardchec->spd_received_qty_35w;
    //         }
    //          if($inwardchec->spd_received_qty_70w){                 
    //             $inwardchec->spd_received_qty_70w=$inwardchec->spd_received_qty_70w-$inwardchec->spd_received_qty_70w;
    //         }
    //         if($inwardchec->spd_received_qty_110w){
    //            $inwardchec->spd_received_qty_110w= $inwardchec->spd_received_qty_110w-$inwardchec->spd_received_qty_110w;
    //         }
    //          if($inwardchec->spd_received_qty_190w){                 
    //             $inwardchec->spd_received_qty_190w=$inwardchec->spd_received_qty_190w-$inwardchec->spd_received_qty_190w;
    //         }
    // // if($inwardchec->total_rcv_qty){                 
    // //             $inwardchec->total_rcv_qty=$inwardchec->total_rcv_qty-$inwardchec->total_rcv_qty;
    // //         }
    // // if($inwardchec->tiltadap_rcv_qty){                 
    // //             $inwardchec->tiltadap_rcv_qty=$inwardchec->tiltadap_rcv_qty-$inwardchec->tiltadap_rcv_qty;
    // //         }
            
    //         //dd($inwardchec);
    //        // $inwardcheck->approve_status="1";
    //         $inwardchec->save();
            $inward=inward::where('id',$request->id)->update(['approve_status'=>$request->approve_status]);
            $inwards=outward::where('id',$request->id)->update(['approve_status'=>$request->approve_status]);
            // dd($inward);
       


          
  
        return redirect(url('inwards_list').'?id='.$request->invoice_number.'&&jilla_id='.$request->jilla_id)->with('success',"Status Updated Successfully");
          }
          else{
             return redirect(url('inwards_list').'?id='.$request->invoice_number.'&&jilla_id='.$request->jilla_id);

          }
      }


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

     return view('backend.armsclamps_store',compact('jilla','makeslist'));
} 


     public function arms_clampsstore(Request $request){


  //  'arms_dia_inner_32' => 'required_without_all:arms_dia_25,arms_dia_inner_40,received_arms_dia_25,received_arms_dia_inner_32,received_arms_dia_inner_40,clams_set_25,clams_set_40_2,clams_set_40_3,received_clams_set_25,received_clams_set_40_2,received_clams_set_40_3',

  // 'arms_dia_inner_40' => 'required_without_all:arms_dia_25,arms_dia_inner_32,received_arms_dia_25,received_arms_dia_inner_32,received_arms_dia_inner_40,clams_set_25,clams_set_40_2,clams_set_40_3,received_clams_set_25,received_clams_set_40_2,received_clams_set_40_3',

  //  'received_arms_dia_25' => 'required_without_all:arms_dia_25,arms_dia_inner_32,arms_dia_inner_40,received_arms_dia_inner_32,received_arms_dia_inner_40,clams_set_25,clams_set_40_2,clams_set_40_3,received_clams_set_25,received_clams_set_40_2,received_clams_set_40_3',

  //  'received_arms_dia_inner_32' => 'required_without_all:arms_dia_25,arms_dia_inner_32,arms_dia_inner_40,received_arms_dia_25,received_arms_dia_inner_40,clams_set_25,clams_set_40_2,clams_set_40_3,received_clams_set_25,received_clams_set_40_2,received_clams_set_40_3',


  //  'received_arms_dia_inner_40' => 'required_without_all:arms_dia_25,arms_dia_inner_32,arms_dia_inner_40,received_arms_dia_25,received_arms_dia_inner_32,clams_set_25,clams_set_40_2,clams_set_40_3,received_clams_set_25,received_clams_set_40_2,received_clams_set_40_3',

  $rules = array(

    'arms_dia_25' => 'required_without_all:arms_dia_inner_32,arms_dia_inner_40',

     'received_arms_dia_25' => 'required_without_all:received_arms_dia_inner_32,received_arms_dia_inner_40',

      'clams_set_25' => 'required_without_all:clams_set_25,clams_set_40_2,clams_set_40_3',

      'received_clams_set_25' => 'required_without_all:received_clams_set_40_2,received_clams_set_40_3',


);
    $messages = [
   'required_without_all' => '*At least one field is required in every invoice quantity and received quantity'
];     
$validation = Validator::make( $request->all(), $rules, $messages);


// $rules = array(
//   'arms_dia_25'                   => 'required',
//   'arms_dia_inner_32'                    => 'required',
//   'arms_dia_inner_40'                  => 'required',
//    'received_arms_dia_25'                   => 'required',
//   'received_arms_dia_inner_32'                    => 'required',
//   'received_arms_dia_inner_40'                  => 'required',
//    'clams_set_25'                   => 'required',
//   'clams_set_40_2'                    => 'required',
//   'clams_set_40_3'                  => 'required',
//      'received_clams_set_25'                   => 'required',
//   'received_clams_set_40_2'                    => 'required',
//   'received_clams_set_40_3'                  => 'required',
// );

// $keys = implode(',', array_keys($rules));
// $rules['require_one'] = "required_without_all:$keys";

// $messages = [
//   'require_one.required_without_all' => '*At least one field is required'
// ];


// $validation = Validator::make( $request->all(), $rules, $messages);



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

     $armsclamps_info = new armsclamps();
     //dd($inward_info);
         $armsclamps_info->jilla_id = $request->input('jilla_id');
         $armsclamps_info->suplied_by = $request->input('suplied_by');
         $armsclamps_info->date_of_receiving = $request->input('date_of_receiving');
         $armsclamps_info->invoice_date = $request->input('invoice_date');
         $armsclamps_info->invoice_number = $request->input('invoice_number');
         $armsclamps_info->arms_dia_25 = $request->input('arms_dia_25');
         $armsclamps_info->arms_dia_inner_32 = $request->input('arms_dia_inner_32');
         $armsclamps_info->arms_dia_inner_40 = $request->input('arms_dia_inner_40');
         $armsclamps_info->received_arms_dia_25 = $request->input('received_arms_dia_25');
         $armsclamps_info->received_arms_dia_inner_32 = $request->input('received_arms_dia_inner_32');
         $armsclamps_info->received_arms_dia_inner_40 = $request->input('received_arms_dia_inner_40');
         $armsclamps_info->clams_set_25 = $request->input('clams_set_25');
         $armsclamps_info->clams_set_40_2 = $request->input('clams_set_40_2');
         $armsclamps_info->clams_set_40_3 = $request->input('clams_set_40_3');
         $armsclamps_info->received_clams_set_25 = $request->input('received_clams_set_25');
         $armsclamps_info->received_clams_set_40_2 = $request->input('received_clams_set_40_2');
         $armsclamps_info->received_clams_set_40_3 = $request->input('received_clams_set_40_3');

         $armsclamps_info->aluminium_cable = $request->input('aluminium_cable');
         $armsclamps_info->received_aluminium_cable = $request->input('received_aluminium_cable');

         $armsclamps_info->org_received_arms_dia_25 = $request->input('received_arms_dia_25');
         $armsclamps_info->org_received_arms_dia_inner_32 = $request->input('received_arms_dia_inner_32');
         $armsclamps_info->org_received_arms_dia_inner_40 = $request->input('received_arms_dia_inner_40');
          $armsclamps_info->org_received_clams_set_25 = $request->input('received_clams_set_25');
         $armsclamps_info->org_received_clams_set_40_2 = $request->input('received_clams_set_40_2');
         $armsclamps_info->org_received_clams_set_40_3 = $request->input('received_clams_set_40_3');
          $armsclamps_info->org_received_aluminium_cable = $request->input('received_aluminium_cable');



            $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;
         }
         
        $armsclamps_info->files=implode(',', $insert);
         $armsclamps_info->remarks = $request->input('remarks');
                
         $armsclamps_info->save();

         $armsclamps_info = new total_armsclamps();
     //dd($inward_info);
         $armsclamps_info->jilla_id = $request->input('jilla_id');
         $armsclamps_info->suplied_by = $request->input('suplied_by');
         $armsclamps_info->date_of_receiving = $request->input('date_of_receiving');
         $armsclamps_info->invoice_date = $request->input('invoice_date');
         $armsclamps_info->invoice_number = $request->input('invoice_number');
         $armsclamps_info->arms_dia_25 = $request->input('arms_dia_25');
         $armsclamps_info->arms_dia_inner_32 = $request->input('arms_dia_inner_32');
         $armsclamps_info->arms_dia_inner_40 = $request->input('arms_dia_inner_40');
         $armsclamps_info->received_arms_dia_25 = $request->input('received_arms_dia_25');
         $armsclamps_info->received_arms_dia_inner_32 = $request->input('received_arms_dia_inner_32');
         $armsclamps_info->received_arms_dia_inner_40 = $request->input('received_arms_dia_inner_40');
         $armsclamps_info->clams_set_25 = $request->input('clams_set_25');
         $armsclamps_info->clams_set_40_2 = $request->input('clams_set_40_2');
         $armsclamps_info->clams_set_40_3 = $request->input('clams_set_40_3');
         $armsclamps_info->received_clams_set_25 = $request->input('received_clams_set_25');
         $armsclamps_info->received_clams_set_40_2 = $request->input('received_clams_set_40_2');
         $armsclamps_info->received_clams_set_40_3 = $request->input('received_clams_set_40_3');

         $armsclamps_info->aluminium_cable = $request->input('aluminium_cable');
         $armsclamps_info->received_aluminium_cable = $request->input('received_aluminium_cable');

            $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;
         }
         
        $armsclamps_info->files=implode(',', $insert);
         $armsclamps_info->remarks = $request->input('remarks');
                
         $armsclamps_info->save();
          if ($request->is("api*")) {     
            return json_encode(["error"=>false,"success"=>"Data Inserted Successfully ","status"=>$armsclamps_info]);
        }else{
            return redirect(url('armsclamps_store'))->withSuccess('Added Successfully.');
        }
    }

public function armsclamps_list(Request $request)
{
     

    $armsclamps_info= armsclamps::where('invoice_number',$request->id)->where('jilla_id',$request->jilla_id)->with('area_district')->get();
   // dd($armsclamps_info);
   
                if ($request->is("api*")) {
                        return json_encode($armsclamps_info);
                    }
    return view('backend.armsclamps_list',compact('armsclamps_info'));
}

public function armsclamps_view(Request $request)
        {


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

          }
         
      // $area_dis=armsclamps::with(['area_district'])->groupby('jilla_id')->get()->toArray();

     DB::enableQueryLog();
       $area_dis=armsclamps::with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->whereIn('jilla_id',$request->session()->get('districts'))->where('approve_status',1)->orWhere('approve_status',0)->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"),DB::raw("SUM(org_received_arms_dia_25) as org_arms_25"),DB::raw("SUM(org_received_arms_dia_inner_32) as org_arms_32"),DB::raw("SUM(org_received_arms_dia_inner_40) as org_arms_40"),DB::raw("SUM(org_received_clams_set_25) as org_clamps_25"),DB::raw("SUM(org_received_clams_set_40_2) as org_clamps_42"),DB::raw("SUM(org_received_clams_set_40_3) as org_clamps_43"),DB::raw("SUM(org_received_aluminium_cable) as org_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();
       $area_dis_supply=armsclamps::whereIn('jilla_id',$request->session()->get('districts'))->where('approve_status',1)->orWhere('approve_status',0)->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"),DB::raw("SUM(org_received_arms_dia_25) as org_arms_25"),DB::raw("SUM(org_received_arms_dia_inner_32) as org_arms_32"),DB::raw("SUM(org_received_arms_dia_inner_40) as org_arms_40"),DB::raw("SUM(org_received_clams_set_25) as org_clamps_25"),DB::raw("SUM(org_received_clams_set_40_2) as org_clamps_42"),DB::raw("SUM(org_received_clams_set_40_3) as org_clamps_43"),DB::raw("SUM(org_received_aluminium_cable) as org_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_supply);

         DB::enableQueryLog();
       $transfered_inventory=move_inventoryarm::select(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"))->get()->toArray();
       //dd($transfered_inventory);

       DB::enableQueryLog();
       $transfered_supplies=move_inventoryarm::select(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')->groupby('suplied_by')->get()->toArray();
       //dd($transfered_supplies);

     return view('backend.armsclamps_view',compact('area_dis','area_dis_supply','transfered_inventory','transfered_supplies'));


        }

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

         DB::enableQueryLog();
       $area_dis=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(org_received_arms_dia_25) as arms_25"),DB::raw("SUM(org_received_arms_dia_inner_32) as arms_32"),DB::raw("SUM(org_received_arms_dia_inner_40) as arms_40"),DB::raw("SUM(org_received_clams_set_25) as clamps_25"),DB::raw("SUM(org_received_clams_set_40_2) as clamps_42"),DB::raw("SUM(org_received_clams_set_40_3) as clamps_43"),DB::raw("SUM(org_received_aluminium_cable) as rcv_cbl"),'jilla_id','invoice_number','approve_status','remarks','created_at','date_of_receiving')->where(function($q) use ($request){
      if(isset($request->status)){
        $q->where('approve_status',$request->status);
      }else{

           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
      }
    })->groupby('invoice_number')->orderby('date_of_receiving','Desc')->get()->toArray();
        //dd($area_dis);

    $area_balance=armsclamps::with(['area_district' => function ($query) {
        $query->select('id', 'area_name');
        }])->select(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')->where('jilla_id',$request->id)->get()->toArray();
    //dd($area_balance);

      DB::enableQueryLog();
       $area_dis_supply=armsclamps::where('jilla_id',$request->id)->select(DB::raw("count(suplied_by) as count"),DB::raw("SUM(org_received_arms_dia_25) as arms_25"),DB::raw("SUM(org_received_arms_dia_inner_32) as arms_32"),DB::raw("SUM(org_received_arms_dia_inner_40) as arms_40"),DB::raw("SUM(org_received_clams_set_25) as clamps_25"),DB::raw("SUM(org_received_clams_set_40_2) as clamps_42"),DB::raw("SUM(org_received_clams_set_40_3) as clamps_43"),DB::raw("SUM(org_received_aluminium_cable) as rcv_cbl"),'approve_status','remarks','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_supply);

 
      $inventory_total=move_inventoryarm::with(['from_district','to_district'])->select(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"),'from_jilla_id')->where('from_jilla_id',$request->id)->get()->toArray();
      //dd($inventory_total);

       $inventory=move_inventoryarm::with(['from_district','to_district'])->select(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"),'invoice_number','remarks','created_at','date_of_transfer','from_jilla_id','to_jilla_id','remarks')->groupby('invoice_number')->where('from_jilla_id',$request->id)->orderby('created_at','asc')->get()->toArray();

        $supliedby_inventory_total=move_inventoryarm::with(['from_district','to_district'])->select(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"),'from_jilla_id','suplied_by')->where('from_jilla_id',$request->id)->groupby('suplied_by')->get()->toArray();


        $district  = area_master::where('id',$request->id)->first();
       return view('backend.armsclamps_invoice_list',compact('area_dis','district','area_dis_supply','area_balance','inventory_total','inventory','supliedby_inventory_total'));
    }

public function armsclamps_edit ($id)
    
    {
        $armsclamps_info=armsclamps::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.armsclamps_edit',compact('armsclamps_info','jilla','makeslist'));
       }


  

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

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

        $armsclamps_info = armsclamps::find($id);
        
        
        $armsclamps_info->jilla_id = $request->get('jilla_id');
         $armsclamps_info->suplied_by = $request->get('suplied_by');
         $armsclamps_info->invoice_number = $request->get('invoice_number');
         $armsclamps_info->invoice_date = $request->get('invoice_date');
         $armsclamps_info->date_of_receiving = $request->get('date_of_receiving');
          $armsclamps_info->arms_dia_25 = $request->get('arms_dia_25'); 
        $armsclamps_info->arms_dia_inner_32 = $request->get('arms_dia_inner_32');
         $armsclamps_info->arms_dia_inner_40 = $request->get('arms_dia_inner_40');  
         $armsclamps_info->received_arms_dia_25 = $request->get('received_arms_dia_25'); 
        $armsclamps_info->received_arms_dia_inner_32 = $request->get('received_arms_dia_inner_32');       
        $armsclamps_info->received_arms_dia_inner_40 = $request->get('received_arms_dia_inner_40'); 
          $armsclamps_info->clams_set_25 = $request->get('clams_set_25'); 
        $armsclamps_info->clams_set_40_2 = $request->get('clams_set_40_2');
         $armsclamps_info->clams_set_40_3 = $request->get('clams_set_40_3');  
         $armsclamps_info->received_clams_set_25 = $request->get('received_clams_set_25'); 
        $armsclamps_info->received_clams_set_40_2 = $request->get('received_clams_set_40_2');       
        $armsclamps_info->received_clams_set_40_3 = $request->get('received_clams_set_40_3');
         $armsclamps_info->aluminium_cable = $request->get('aluminium_cable');       
        $armsclamps_info->received_aluminium_cable = $request->get('received_aluminium_cable');
           $armsclamps_info->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)){
       
        $armsclamps_info->files=implode(',', $insert);
      }
        $armsclamps_info->save();    
     return redirect(url('/armsclamps_list').'?id='.$armsclamps_info->invoice_number.'&&jilla_id='.$armsclamps_info->jilla_id)->with('completed');
    
    
          }
    public function update_armsclamps_status(Request $request)
    {
        //dd($request->all());
        
        $armsclamps_info=armsclamps::where('id',$request->id)->first();

        $armsclamps_infos=outward_armsclamps::where('id',$request->id)->first();

        if(!empty($armsclamps_info)){

             outward_armsclamps::create(['jilla_id'=>$armsclamps_info->jilla_id,'invoice_number'=>$armsclamps_info->invoice_number,'date_of_receiving'=>$armsclamps_info->date_of_receiving,'suplied_by'=>$armsclamps_info->suplied_by,
                    'received_arms_dia_25'=>$armsclamps_info->received_arms_dia_25,
                    'received_arms_dia_inner_32'=>$armsclamps_info->received_arms_dia_inner_32,
                    'received_arms_dia_inner_40'=>$armsclamps_info->received_arms_dia_inner_40,
                    'received_clams_set_25'=>$armsclamps_info->received_clams_set_25,
                    'received_clams_set_40_2'=>$armsclamps_info->received_clams_set_40_2, 
                    'received_clams_set_40_3'=>$armsclamps_info->received_clams_set_40_3, 
                    'received_aluminium_cable'=>$armsclamps_info->received_aluminium_cable,
                     'remarks'=>$armsclamps_info->remarks,  
                    'files'=>$armsclamps_info->files,
                     'approve_status'=>$request->approve_status,

                    ]);

              armsclamps_vendor::create(['jilla_id'=>$armsclamps_info->jilla_id,'invoice_number'=>$armsclamps_info->invoice_number,'date_of_receiving'=>$armsclamps_info->date_of_receiving,'suplied_by'=>$armsclamps_info->suplied_by,
                    'received_arms_dia_25'=>$armsclamps_info->received_arms_dia_25,
                    'received_arms_dia_inner_32'=>$armsclamps_info->received_arms_dia_inner_32,
                    'received_arms_dia_inner_40'=>$armsclamps_info->received_arms_dia_inner_40,
                    'received_clams_set_25'=>$armsclamps_info->received_clams_set_25,
                    'received_clams_set_40_2'=>$armsclamps_info->received_clams_set_40_2, 
                    'received_clams_set_40_3'=>$armsclamps_info->received_clams_set_40_3, 
                    'received_aluminium_cable'=>$armsclamps_info->received_aluminium_cable,
                     'remarks'=>$armsclamps_info->remarks,  
                    'files'=>$armsclamps_info->files,
                     'approve_status'=>$request->approve_status,

                    ]);
               
              $armsclamps=armsclamps::where('id',$request->id)->update(['approve_status'=>$request->approve_status]);
              
  
        return redirect(url('armsclamps_list').'?id='.$request->invoice_number.'&&jilla_id='.$request->jilla_id)->with('success',"Status Updated Successfully");
          }
          else{
             return redirect(url('armsclamps_list').'?id='.$request->invoice_number.'&&jilla_id='.$request->jilla_id);

          }

    }

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

     return view('backend.ccms_type_store',compact('jilla','makeslist'));
} 


     public function ccms_type_store(Request $request){


       $rules = array(
    'ccms_ph_invoice_quantity' => 'required_without_all:ccms_ph_invoice_quantity,ccms_ph_receive_quantity',

);
    $messages = [
   'required_without_all' => '*At least one field is required'
];     
$validation = Validator::make( $request->all(), $rules, $messages);


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

     $ccms_info = new inwardccmstypes();
     //dd($inward_info);

         $ccms_info->jilla_id = $request->input('jilla_id');
         $ccms_info->phase_name = $request->input('phase_name');
         $ccms_info->date_of_receiving = $request->input('date_of_receiving');
         $ccms_info->invoice_date = $request->input('invoice_date');
         $ccms_info->invoice_number = $request->input('invoice_number');
         $ccms_info->description = $request->input('description');
         $ccms_info->received_cable = $request->input('received_cable');
         $ccms_info->ccms_ph_invoice_quantity = $request->input('ccms_ph_invoice_quantity');
         $ccms_info->ccms_ph_receive_quantity = $request->input('ccms_ph_receive_quantity');

         $ccms_info->org_received_cable = $request->input('received_cable');
         $ccms_info->org_ccms_ph_receive_quantity = $request->input('ccms_ph_receive_quantity');

         $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;
         }
         
        $ccms_info->files=implode(',', $insert);
         $ccms_info->remarks = $request->input('remarks');
        
                
         $ccms_info->save();

          $ccms_info = new total_ccms();
     //dd($inward_info);

         $ccms_info->jilla_id = $request->input('jilla_id');
         $ccms_info->phase_name = $request->input('phase_name');
         $ccms_info->date_of_receiving = $request->input('date_of_receiving');
         $ccms_info->invoice_date = $request->input('invoice_date');
         $ccms_info->invoice_number = $request->input('invoice_number');
         $ccms_info->description = $request->input('description');
         $ccms_info->received_cable = $request->input('received_cable');
         $ccms_info->ccms_ph_invoice_quantity = $request->input('ccms_ph_invoice_quantity');
         $ccms_info->ccms_ph_receive_quantity = $request->input('ccms_ph_receive_quantity');
         $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;
         }
         
        $ccms_info->files=implode(',', $insert);
         $ccms_info->remarks = $request->input('remarks');
        
                
         $ccms_info->save();
          if ($request->is("api*")) {     
            return json_encode(["error"=>false,"success"=>"Data Inserted Successfully ","status"=>$ccms_info]);
        }else{
            return redirect(url('ccmstype_store'))->withSuccess('Added Successfully.');
        }
    }

public function ccmstype_list(Request $request)
{
     

    $ccms_info= inwardccmstypes::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.ccmstypes_list',compact('ccms_info'));
}

 public function ccmstypes_view(Request $request)
        {

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

          }
          DB::enableQueryLog();
       $area_dis=inwardccmstypes::with(['area_district'=> function ($query) {
        $query->select('id', 'area_name');
    }])->select(DB::raw("count(jilla_id) as count"),DB::raw("SUM(org_ccms_ph_receive_quantity) as ccmsph"),DB::raw("SUM(org_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);

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

     $inventory=move_inventoryccms::with(['from_district','to_district'])->select(DB::raw("SUM(ccms_ph_receive_quantity) as ccmsph"),DB::raw("SUM(received_cable) as rcv_cbl"))->orderby('created_at','asc')->get()->toArray();

      $inventory_total=move_inventoryccms::with(['from_district','to_district'])->select(DB::raw("SUM(ccms_ph_receive_quantity) as ccmsph"),DB::raw("SUM(received_cable) as rcv_cbl"),'from_jilla_id')->where('from_jilla_id',$request->id)->get()->toArray();

     return view('backend.ccmstypes_view',compact('area_dis','area_balance','inventory','inventory_total'));


        }

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


        DB::enableQueryLog();
       $area_dis=inwardccmstypes::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(org_ccms_ph_receive_quantity) as ccmsph"),DB::raw("SUM(org_received_cable) as rcv_cbl"),'invoice_number','jilla_id','approve_status','created_at','date_of_receiving')->where(function($q) use ($request){
      if(isset($request->status)){
        $q->where('approve_status',$request->status);
      }else{
           $q->where('approve_status',1);
            $q->orWhere('approve_status',0);
      }
    })->groupby('invoice_number')->orderby('date_of_receiving','Desc')->get()->toArray();
        //dd($area_invoice);

    $area_balance=inwardccmstypes::with(['area_district' => function ($query) {
        $query->select('id', 'area_name');
        }])->select(DB::raw("SUM(ccms_ph_receive_quantity) as ccmsph"),DB::raw("SUM(received_cable) as rcv_cbl"),'jilla_id')->groupby('jilla_id')->where('jilla_id',$request->id)->get()->toArray();
         //dd($area_balance);

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

    $inventory=move_inventoryccms::with(['from_district','to_district'])->select(DB::raw("SUM(ccms_ph_receive_quantity) as ccmsph"),DB::raw("SUM(received_cable) as rcv_cbl"),'invoice_number','remarks','created_at','date_of_transfer','from_jilla_id','to_jilla_id','remarks')->groupby('invoice_number')->where('from_jilla_id',$request->id)->orderby('created_at','asc')->get()->toArray();

    $inventory_total=move_inventoryccms::with(['from_district','to_district'])->select(DB::raw("SUM(ccms_ph_receive_quantity) as ccmsph"),DB::raw("SUM(received_cable) as rcv_cbl"),'from_jilla_id')->where('from_jilla_id',$request->id)->get()->toArray();

        $district  = area_master::where('id',$request->id)->first();
       return view('backend.ccmstypes_invoice_list',compact('area_dis','district','inventory','inventory_total','area_balance'));
    }
    public function ccmstypes_edit($id)
    
    {
        $ccms_info=inwardccmstypes::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.ccmstypes_edit',compact('ccms_info','jilla','makeslist'));
       }


  

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

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

        $ccms_info = inwardccmstypes::find($id);
        $ccms_info->jilla_id = $request->get('jilla_id');
        $ccms_info->invoice_number = $request->get('invoice_number');
        $ccms_info->phase_name = $request->get('phase_name');
        $ccms_info->description = $request->get('description'); 
        $ccms_info->received_cable = $request->get('received_cable');
        $ccms_info->received_cable = $request->get('received_cable');
        $ccms_info->ccms_ph_invoice_quantity = $request->get('ccms_ph_invoice_quantity');  
        $ccms_info->ccms_ph_receive_quantity = $request->get('ccms_ph_receive_quantity'); 
        $ccms_info->invoice_date = $request->get('invoice_date');
        $ccms_info->date_of_receiving = $request->get('date_of_receiving');
        $ccms_info->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)){
       
        $ccms_info->files=implode(',', $insert);
      }
        $ccms_info->save();  

     return redirect(url('/ccmstypes_list').'?id='.$ccms_info->invoice_number.'&&jilla_id='.$ccms_info->jilla_id)->with('success',"Updated Successfully");
    
    
          }
    public function update_ccmstypes_status(Request $request)
    {
        //dd($request->all());
        $ccms_info=inwardccmstypes::where('id',$request->id)->first();
        $ccms_infos=outward_ccms::where('id',$request->id)->first();

         if(!empty($ccms_info)){
            outward_ccms::create(['jilla_id'=>$ccms_info->jilla_id,'maker_name'=>$ccms_info->maker_name,"ccms_ph_receive_quantity"=>$ccms_info->ccms_ph_receive_quantity,"invoice_number"=>$ccms_info->invoice_number,'status'=>1,"date_of_transfer"=>date('Y-m-d',strtotime($ccms_info->date_of_transfer)),"remarks"=>$ccms_info->remarks, 'approve_status'=>$ccms_info->approve_status, "files"=>$ccms_info->files]);

            ccms_vendor::create(['jilla_id'=>$ccms_info->jilla_id,'maker_name'=>$ccms_info->maker_name,"ccms_ph_receive_quantity"=>$ccms_info->ccms_ph_receive_quantity,"invoice_number"=>$ccms_info->invoice_number,'status'=>1,"date_of_transfer"=>date('Y-m-d',strtotime($ccms_info->date_of_transfer)), 'approve_status'=>$ccms_info->approve_status,"remarks"=>$ccms_info->remarks, "files"=>$ccms_info->files]);

           $ccms=inwardccmstypes::where('id',$request->id)->update(['approve_status'=>$request->approve_status]);

    
        return redirect(url('ccmstypes_list').'?id='.$request->invoice_number.'&&jilla_id='.$request->jilla_id)->with('success',"Status Updated Successfully");

    }else{
                    return redirect(url('ccmstypes_list').'?id='.$request->invoice_number.'&&jilla_id='.$request->jilla_id);

          }
      }

  
     public function move_inventoryarm(Request $request,$value='')
    {
        $jilla=area_master::where('parent_id',0)->whereIn('id',$request->session()->get('districts'))->get();
        $armslist= armsclamps::groupby('suplied_by')->get();
        $inventory = move_inventoryarm::with(['from_district','to_district'])->where('is_eesl',0)->orderby('created_at','desc')->get();
        $suplied_by_lists=armsclamps::groupby('suplied_by')->pluck('suplied_by');
         $eesl_inventory = move_inventoryarm::with(['from_district','to_district'])->where('is_eesl',1)->orderby('created_at','desc')->get();
     return view('backend.inventory.arm_inventorylist',compact('jilla','armslist','inventory','suplied_by_lists','eesl_inventory'));
    }


    public function update_inventoryarm(Request $request)
    {
       //  $inwardcheck = armsclamps::where(['jilla_id'=>$request->jilla_id])->where(function($q) use ($request){
       //      if($request->armsdata=="25 D"){
       //          $q->where('arms_dia_25',">=",$request->quantity);
       //      }
       //      if($request->armsdata=="32 D"){
       //          $q->where('arms_dia_inner_32',">=",$request->quantity);
       //      }
       //      if($request->armsdata=="40 D"){
       //          $q->where('arms_dia_inner_40',">=",$request->quantity);
       //      }
       //      if($request->armsdata=="25 RD"){
       //          $q->where('received_arms_dia_25',">=",$request->quantity);
       //      }
       //      if($request->armsdata=="32 RD"){
       //          $q->where('received_arms_dia_inner_32',">=",$request->quantity);
       //      }
       //      if($request->armsdata=="40 RD"){
       //          $q->where('received_arms_dia_inner_40',">=",$request->quantity);
       //      }
       //      if($request->armsdata=="25 cl"){
       //          $q->where('clams_set_25',">=",$request->quantity);
       //      }
       //      if($request->armsdata=="40_2 cl"){
       //          $q->where('clams_set_40_2',">=",$request->quantity);
       //      }
       //      if($request->armsdata=="40_3 cl"){
       //          $q->where('clams_set_40_3',">=",$request->quantity);
       //      }
       //      if($request->armsdata=="25 RDcl"){
       //          $q->where('received_clams_set_25',">=",$request->quantity);
       //      }
       //      if($request->armsdata=="40_2 RDcl"){
       //          $q->where('received_clams_set_40_2',">=",$request->quantity);
       //      }
       //      if($request->armsdata=="40_3 RDcl"){
       //          $q->where('received_clams_set_40_3',">=",$request->quantity);
       //      }

       //  })->first();
       //  $check=armsclamps::get();
       // //dd( $inwardcheck);
       //  if(!empty($inwardcheck)){
       //      if($request->armsdata=="25 D"){                 
       //          $inwardcheck->arms_dia_25=$inwardcheck->arms_dia_25-$request->quantity;
       //      }
       //      if($request->armsdata=="32 D"){
       //         $inwardcheck->arms_dia_inner_32= $inwardcheck->arms_dia_inner_32-$request->quantity;
       //      }
       //      if($request->armsdata=="40 D"){
       //         $inwardcheck->arms_dia_inner_40= $inwardcheck->arms_dia_inner_40-$request->quantity;
       //      }
       //      if($request->armsdata=="25 RD"){
       //         $inwardcheck->received_arms_dia_25= $inwardcheck->received_arms_dia_25-$request->quantity;
       //      }
       //      if($request->armsdata=="32 RD"){
       //         $inwardcheck->received_arms_dia_inner_32= $inwardcheck->received_arms_dia_inner_32-$request->quantity;
       //      }
       //      if($request->armsdata=="40 RD"){
       //         $inwardcheck->received_arms_dia_inner_40= $inwardcheck->received_arms_dia_inner_40-$request->quantity;
       //      }


       //      if($request->armsdata=="25 cl"){                 
       //          $inwardcheck->clams_set_25=$inwardcheck->clams_set_25-$request->quantity;
       //      }
       //      if($request->armsdata=="40_2 cl"){
       //         $inwardcheck->clams_set_40_2= $inwardcheck->clams_set_40_2-$request->quantity;
       //      }
       //      if($request->armsdata=="40_3 cl"){
       //         $inwardcheck->clams_set_40_3= $inwardcheck->clams_set_40_3-$request->quantity;
       //      }
       //      if($request->armsdata=="25 RDcl"){
       //         $inwardcheck->received_clams_set_25= $inwardcheck->received_clams_set_25-$request->quantity;
       //      }
       //      if($request->armsdata=="40_2 RDcl"){
       //         $inwardcheck->received_clams_set_40_2= $inwardcheck->received_clams_set_40_2-$request->quantity;
       //      }
       //      if($request->armsdata=="40_3 RDcl"){
       //         $inwardcheck->received_clams_set_40_3= $inwardcheck->received_clams_set_40_3-$request->quantity;
       //      }


           




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

       //      $inward_info = new armsclamps();
       //      $inward_info->jilla_id = $request->input('to_jilla_id');
       //      $inward_info->suplied_by = $request->input('suplied_by');

       //   if($request->armsdata=="25 D"){                 
       //          $inward_info->arms_dia_25=$request->quantity;
       //      }
       //      if($request->armsdata=="32 D"){
       //         $inward_info->arms_dia_inner_32= $request->quantity;
       //      }
       //      if($request->armsdata=="40 D"){
       //         $inward_info->arms_dia_inner_40= $request->quantity;
       //      }
       //      if($request->armsdata=="25 RD"){
       //         $inward_info->received_arms_dia_25= $request->quantity;
       //      }
       //      if($request->armsdata=="32 RD"){
       //         $inward_info->received_arms_dia_inner_32= $request->quantity;
       //      }
       //       if($request->armsdata=="40 RD"){
       //         $inward_info->received_arms_dia_inner_40= $request->quantity;
       //      }


       //       if($request->armsdata=="25 cl"){                 
       //          $inward_info->clams_set_25=$request->quantity;
       //      }
       //      if($request->armsdata=="40_2 cl"){
       //         $inward_info->clams_set_40_2= $request->quantity;
       //      }
       //      if($request->armsdata=="40_3 cl"){
       //         $inward_info->clams_set_40_3= $request->quantity;
       //      }
       //      if($request->armsdata=="25 RDcl"){
       //         $inward_info->received_clams_set_25= $request->quantity;
       //      }
       //      if($request->armsdata=="40_2 RDcl"){
       //         $inward_info->received_clams_set_40_2= $request->quantity;
       //      }
       //      if($request->armsdata=="40_3 RDcl"){
       //         $inward_info->received_clams_set_40_3= $request->quantity;
       //      }

       //       $inward_info->invoice_number=time();
       //         $inward_info->date_of_receiving=date('Y-m-d');
       //         $inward_info->invoice_date=date('Y-m-d');
          
       //      $inward_info->save();

       //       //dd($inward_info);

       //       $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;
       //       }
        $inwardchecks = armsclamps::where(['jilla_id'=>$request->jilla_id,"suplied_by"=>$request->suplied_by,'approve_status'=>'0'])->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();
         
  $to_jilla=$request->to_jilla_id;
            $from_jilla=$request->jilla_id;

            if($request->to_jilla_id=="EESL"){
            $totring = 'EESL';
             $frominvoice=$inwardchec->invoice_number;
            $fromtring = area_master::where('id',$request->jilla_id)->first();
            $toinvoice=$fromtring->area_name."-to-".$totring."-".time();
            $is_eesl='1';
        }else{

            $totring = area_master::where('id',$request->to_jilla_id)->first();
            $fromtring = area_master::where('id',$request->jilla_id)->first();
            $frominvoice=$inwardchec->invoice_number;
            $toinvoice=$fromtring->area_name."-to-".$totring->area_name."-".time();
            $is_eesl='0';
        }

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

          '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;
         }   

          $invoice='';
            $to_jilla=$request->to_jilla_id;
             $from_jilla=$request->jilla_id;

             if($request->to_jilla_id=="EESL"){
              $totring = 'EESL';
              $fromtring = area_master::where('id',$request->jilla_id)->first();
            $invoice=$fromtring->area_name."-to-".$totring."-".time();

            armsclamps::create(['jilla_id'=>$request->to_jilla_id,'suplied_by'=>$request->suplied_by,'invoice_number'=>$invoice,'date_of_receiving'=>$request->date_of_receiving,

                    'received_arms_dia_25'=>$request->received_arms_dia_25??0,
                    'received_arms_dia_inner_32'=>$request->received_arms_dia_inner_32??0,
                    'received_arms_dia_inner_40'=>$request->received_arms_dia_inner_40??0,
                    'received_clams_set_25'=>$request->received_clams_set_25??0,
                    'received_clams_set_40_2'=>$request->received_clams_set_40_2??0, 
                    'received_clams_set_40_3'=>$request->received_clams_set_40_3??0, 
                    'received_aluminium_cable'=>$request->received_aluminium_cable,

                     'org_received_arms_dia_25'=>$request->received_arms_dia_25??0,
                    'org_received_arms_dia_inner_32'=>$request->received_arms_dia_inner_32??0,
                    'org_received_arms_dia_inner_40'=>$request->received_arms_dia_inner_40??0,
                    'org_received_clams_set_25'=>$request->received_clams_set_25??0,
                    'org_received_clams_set_40_2'=>$request->received_clams_set_40_2??0, 
                    'org_received_clams_set_40_3'=>$request->received_clams_set_40_3??0, 
                    'org_received_aluminium_cable'=>$request->received_aluminium_cable??0,
                     'remarks'=>$request->remarks,  
                    'files'=>implode(',', $insert)]);


            move_inventoryarm::create(['from_jilla_id'=>$request->jilla_id,'suplied_by'=>$request->suplied_by,'invoice_number'=>$invoice,'date_of_receiving'=>$request->date_of_receiving,'is_eesl'=>1,
                '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,'to_jilla_id'=>$to_jilla,'arms_id'=>$inwardchec->id,'status'=>1,"date_of_transfer"=>date('Y-m-d',strtotime($request->date_of_receiving)),"remarks"=>$request->remarks,"files"=>implode(',', $insert)]);
            return redirect(url('move_inventoryarm'))->with('success',"Successfully moved armsclamps");

        }else{
             $totring = area_master::where('id',$request->to_jilla_id)->first();
              $fromtring = area_master::where('id',$request->jilla_id)->first();

             $invoice=$fromtring->area_name."-to-".$totring->area_name."-".time();

            armsclamps::create(['jilla_id'=>$request->to_jilla_id,'suplied_by'=>$request->suplied_by,'invoice_number'=>$invoice,'date_of_receiving'=>$request->date_of_receiving,

                    'received_arms_dia_25'=>$request->received_arms_dia_25??0,
                    'received_arms_dia_inner_32'=>$request->received_arms_dia_inner_32??0,
                    'received_arms_dia_inner_40'=>$request->received_arms_dia_inner_40??0??0,
                    'received_clams_set_25'=>$request->received_clams_set_25??0,
                    'received_clams_set_40_2'=>$request->received_clams_set_40_2??0, 
                    'received_clams_set_40_3'=>$request->received_clams_set_40_3??0, 
                    'received_aluminium_cable'=>$request->received_aluminium_cable??0,

                     'org_received_arms_dia_25'=>$request->received_arms_dia_25??0,
                    'org_received_arms_dia_inner_32'=>$request->received_arms_dia_inner_32??0,
                    'org_received_arms_dia_inner_40'=>$request->received_arms_dia_inner_40??0,
                    'org_received_clams_set_25'=>$request->received_clams_set_25??0,
                    'org_received_clams_set_40_2'=>$request->received_clams_set_40_2??0, 
                    'org_received_clams_set_40_3'=>$request->received_clams_set_40_3??0, 
                    'org_received_aluminium_cable'=>$request->received_aluminium_cable??0,
                     'remarks'=>$request->remarks,  
                    'files'=>implode(',', $insert)]);

            move_inventoryarm::create(['from_jilla_id'=>$request->jilla_id,'suplied_by'=>$request->suplied_by,'invoice_number'=>$invoice,'date_of_receiving'=>$request->date_of_receiving,"is_eesl"=>0,
                '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,'to_jilla_id'=>$to_jilla,'arms_id'=>$inwardchec->id,'status'=>1,"date_of_transfer"=>date('Y-m-d',strtotime($request->date_of_receiving)),"remarks"=>$request->remarks,"files"=>implode(',', $insert)]);
             return redirect(url('move_inventoryarm'))->with('success',"armsclamps successfully moved");
        }

   }



    public function move_inventoryccms(Request $request,$value='')
    {
        $jilla=area_master::where('parent_id',0)->whereIn('id',$request->session()->get('districts'))->get();
        //$ccmslist= inwardccmstypes::get();
        $makeslist= Makeslist::get();
        $inventory = move_inventoryccms::with(['from_district','to_district'])->where('is_eesl',0)->orderby('created_at','desc')->get();
        $eesl_inventory = move_inventoryccms::with(['from_district','to_district'])->where('is_eesl',1)->orderby('created_at','desc')->get();
     return view('backend.inventory.ccms_inventorylist',compact('jilla','makeslist','inventory','eesl_inventory'));
    }
    public function update_inventoryccms(Request $request)
    {

         $inwardchecks = inwardccmstypes::where(['jilla_id'=>$request->jilla_id,'approve_status'=>'0'])->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();

           $to_jilla=$request->to_jilla_id;
            $from_jilla=$request->jilla_id;

            if($request->to_jilla_id=="EESL"){
            $totring = 'EESL';
             $frominvoice=$inwardchec->invoice_number;
            $fromtring = area_master::where('id',$request->jilla_id)->first();
            $toinvoice=$fromtring->area_name."-to-".$totring."-".time();
            $is_eesl='1';
        }else{

            $totring = area_master::where('id',$request->to_jilla_id)->first();
            $fromtring = area_master::where('id',$request->jilla_id)->first();
            $frominvoice=$inwardchec->invoice_number;
            $toinvoice=$fromtring->area_name."-to-".$totring->area_name."-".time();
            $is_eesl='0';
        }

            move_inventoryccms_invoices::create(['from_jilla_id'=>$from_jilla, 'from_invoice_number'=>$frominvoice,'to_invoice_number'=>$toinvoice,'to_jilla_id'=>$to_jilla,'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,'is_eesl'=>$is_eesl,
             "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;
         }  
             $invoice='';
            $to_jilla=$request->to_jilla_id;
             $from_jilla=$request->jilla_id;

        if($request->to_jilla_id=="EESL"){
              $totring = 'EESL';
              $fromtring = area_master::where('id',$request->jilla_id)->first();
            $invoice=$fromtring->area_name."-to-".$totring."-".time();

             inwardccmstypes::create(['jilla_id'=>$to_jilla,'invoice_number'=>$invoice,' date_of_receiving'=>$request->date_of_transfer,

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

             move_inventoryccms::create(['from_jilla_id'=>$from_jilla,"ccms_ph_receive_quantity"=>$request->ccms_ph_receive_quantity,'to_jilla_id'=>$to_jilla,"invoice_number"=>$invoice,'ccms_id'=>$inwardchec->id,'status'=>1,"date_of_transfer"=>date('Y-m-d',strtotime($request->date_of_transfer)),"remarks"=>$request->remarks,'is_eesl'=>1, "files"=>implode(',', $insert)]);

              return redirect(url('move_inventoryccms'))->with('success',"Successfully moved ccms");

         }else{

             $totring = area_master::where('id',$request->to_jilla_id)->first();
              $fromtring = area_master::where('id',$request->jilla_id)->first();

             $invoice=$fromtring->area_name."-to-".$totring->area_name."-".time();

             inwardccmstypes::create(['jilla_id'=>$to_jilla,'invoice_number'=>$invoice,' date_of_receiving'=>$request->date_of_transfer,

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

             //dd($inward_info);

            move_inventoryccms::create(['from_jilla_id'=>$from_jilla,"ccms_ph_receive_quantity"=>$request->ccms_ph_receive_quantity,'to_jilla_id'=>$to_jilla,"invoice_number"=>$invoice,'ccms_id'=>$inwardchec->id,'status'=>1,"date_of_transfer"=>date('Y-m-d',strtotime($request->date_of_transfer)),"is_eesl"=>0,"remarks"=>$request->remarks, "files"=>implode(',', $insert)]);
             return redirect(url('move_inventoryccms'))->with('success',"");
        }
        
    }

public function store_check(Request $request){

     $countstore = inward::where(['jilla_id'=>$request->jilla_id,'maker_name'=>$request->maker_name,'invoice_number'=>$request->invoice_number])->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(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(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"))->first()->toArray();
  

     //dd($countstore);
      return response()->json($countstore);
    //dd(response()->json($countstore));
    
  // |regex:[a-zA-Z\s]
   }
   

public function ledwattageinwards(Request $request){

     $countstore = inward::where(['jilla_id'=>$request->jilla_id,"maker_name"=>$request->maker_name])->select(DB::raw("SUM(led_wattage_18) as w18w"),DB::raw("SUM(led_wattage_35) as w35w"),DB::raw("SUM(led_wattage_70) as w70w"),DB::raw("SUM(led_wattage_110) as w110w"),DB::raw("SUM(led_wattage_190) as w190w"))->first()->toArray();
     // /dd($countstore);

      return response()->json($countstore);
   
   
   }

   public function armsclampstransfer(Request $request){

     $countstore = armsclamps::where(['jilla_id'=>$request->jilla_id])->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 c40s"),DB::raw("SUM(received_clams_set_40_3) as c43s"),DB::raw("SUM(received_aluminium_cable) as cbl"))->first()->toArray();
     // /dd($countstore);

      return response()->json($countstore);
   
   
   }

   public function ccmsphtransfer(Request $request){

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

      return response()->json($countstore);
   
   
   }

public function editmoveinventory_store_check(Request $request){

    $countstore = inward::where(['jilla_id'=>$request->jilla_id,'maker_name'=>$request->maker_name,'approve_status' => '0'])->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(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(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(tiltadap_rcv_qty) as trq"))->first()->toArray();
    return response()->json($countstore);
}

public function moveinventory_store_check(Request $request){

     $countstore = inward::where(['jilla_id'=>$request->jilla_id,'maker_name'=>$request->maker_name,'approve_status' => '0'])->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(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(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(tiltadap_rcv_qty) as trq"))->first()->toArray();
     
     //dd($countstore);
      return response()->json($countstore);
   }
   

   public function moveinventory_armsclamps_store_check(Request $request){

     $countstore = armsclamps::where(['jilla_id'=>$request->jilla_id,'suplied_by'=>$request->suplied_by,'approve_status'=>'0'])->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);
   }
// |regex:[a-zA-Z\s]

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

    $inward= move_inventory::where('invoice_number',$request->id)->with('from_district','to_district')->get();
   //dd($inward);
               
    return view('backend.move_inventory_list',compact('inward','makeslist'));
}

public function moveinventory_ccms_store_check(Request $request){

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

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

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

    $ccms_info= move_inventoryccms::where('invoice_number',$request->id)->with('from_district','to_district')->get();
   //dd($inward);
               
    return view('backend.ccmsmove_inventory_list',compact('ccms_info','makeslist'));
}
   public function armsmove_inventory_list(Request $request)
{
     $makeslist= Makeslist::get();

    $armsclamps_info= move_inventoryarm::where('invoice_number',$request->id)->with('from_district','to_district')->get();
   //dd($inward);
               
    return view('backend.inventory.armsmove_inventory_list',compact('armsclamps_info','makeslist'));
}


public function moveinventory_lights_edit (Request $request,$id)
    
    {
        $move_inventory=move_inventory::findOrFail($id);
        //dd($move_inventory->from_districtdata->area_name);

         $invoice_inventorys=move_inventory::pluck('invoice_number');
        $jilla=DB::table('area_masters')
                    ->where('parent_id','=','0')
                    ->where('super_id','=','0')
                    ->where('status','=','active')
                    ->get();
    $makeslist= Makeslist::get();
        return view('backend.inventory.moveinventory_lights_edit',compact('move_inventory','jilla','makeslist','invoice_inventorys'));
       }

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

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

           $inwardchec = inward::where(['jilla_id'=>$request->from_jilla_id,"maker_name"=>$request->maker_name])->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->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();

            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();


        $move_inventory = move_inventory::find($id);
        
        $move_inventory->from_district = $request->get('from_district');
        $move_inventory->maker_name = $request->get('maker_name');
        $move_inventory->invoice_number = $request->get('invoice_number');
        $move_inventory->to_district = $request->get('to_district');
        $move_inventory->date_of_transfer = $request->get('date_of_receiving');

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

           
        $move_inventory->drivers_received_qty_18w = $request->get('drivers_received_qty_18w');
        $move_inventory->drivers_received_qty_35w = $request->get('drivers_received_qty_35w');
        $move_inventory->drivers_received_qty_70w = $request->get('drivers_received_qty_70w');
        $move_inventory->drivers_received_qty_110w = $request->get('drivers_received_qty_110w');
        $move_inventory->drivers_received_qty_190w = $request->get('drivers_received_qty_190w');
        

        $move_inventory->spd_received_qty_18w = $request->get('spd_received_qty_18w');
        $move_inventory->spd_received_qty_35w = $request->get('spd_received_qty_35w');
        $move_inventory->spd_received_qty_70w = $request->get('spd_received_qty_70w');
        $move_inventory->spd_received_qty_110w = $request->get('spd_received_qty_110w');
        $move_inventory->spd_received_qty_190w = $request->get('spd_received_qty_190w');
           $move_inventory->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)){
        $move_inventory->files=implode(',', $insert);
      }
        $move_inventory->save();    
     return redirect(url('/move_inventory_list').'?id='.$move_inventory->invoice_number)->with('complted');
 }
    
    
          }



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

    $eesl= eesl::where('invoice_number',$request->id)->with('from_district','to_district')->get();
   //dd($inward);
               
    return view('backend.inventory.move_inventory_eesl_list',compact('eesl','makeslist'));
}
 public function move_inventory_makename(Request $request)
    {
        $maker_name=DB::table('inwards')
                    ->where('jilla_id','=',$request->jilla_id)
                    ->where('approve_status','=',0)
                    ->groupby('maker_name')
                    ->get();
        // var_dump($jillaid);
        // exit();

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

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

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



    public function inwards_invoices_balance_list(Request $request)
    {

        $area_invoice=inward::with(['area_district' => function ($query) {
        $query->select('id', 'area_name');
        }])->select('maker_name',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_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"),DB::raw("SUM(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"),'invoice_number','phase_no','jilla_id','approve_status','remarks','created_at','date_of_receiving')->where(function($q) use ($request){    
      if(isset($request->status)){
        $q->where('approve_status',$request->status);
      }else{

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

        //dd($area_invoice);
     $area_balance=inward::with(['area_district' => function ($query) {
        $query->select('id', 'area_name');
        }])->select(DB::raw("SUM(rcv_qty_18w) as n18w"),DB::raw("SUM(rcv_qty_35w) as n35w"),DB::raw("SUM(rcv_qty_70w) as n70w"),DB::raw("SUM(rcv_qty_110w) as n110w"),DB::raw("SUM(rcv_qty_190w) as n190w"),DB::raw("SUM(bufferstck_received_qty_18w) as n_buffer_stk_rcv_18"),DB::raw("SUM(bufferstck_received_qty_35w) as n_buffer_stk_rcv_35"),DB::raw("SUM(bufferstck_received_qty_70w) as n_buffer_stk_rcv_70"),DB::raw("SUM(bufferstck_received_qty_110w) as n_buffer_stk_rcv_110"),DB::raw("SUM(bufferstck_received_qty_190w) as n_buffer_stk_rcv_190"),DB::raw("SUM(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"),'jilla_id')->groupby('jilla_id')->where('jilla_id',$request->id)->get()->toArray();
      

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

         $maker_counts=inward::with(['area_district' => function ($query) {
        $query->select('id', 'area_name');
        }])->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"),DB::raw("SUM(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"),'invoice_number','phase_no','jilla_id','approve_status','remarks','date_of_receiving')->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();

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

            $inventory=move_inventory::with(['from_district','to_district'])->select('maker_name',DB::raw("count(invoice_number) as count"),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(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(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"),'invoice_number','remarks','created_at','date_of_transfer','from_jilla_id','to_jilla_id','remarks')->groupby('invoice_number')->where('from_jilla_id',$request->id)->orderby('created_at','asc')->get()->toArray();
         //dd($inventory);

            $inventory_total=move_inventory::with(['from_district','to_district'])->select(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(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(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"),'from_jilla_id')->where('from_jilla_id',$request->id)->get()->toArray();
            //dd($inventory_total);


         $makers_inventory_total=move_inventory::with(['from_district','to_district'])->select(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(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(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"),'from_jilla_id','maker_name')->where('from_jilla_id',$request->id)->groupby('maker_name')->get()->toArray();
         //dd($makers_inventory_total);


         $incoming_inventory_total=move_inventory::with(['from_district','to_district'])->select(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(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(drivers_received_qty_18w) as drivers_received_qty_18w"),DB::raw("SUM(drivers_received_qty_35w) as drivers_received_qty_35w"),DB::raw("SUM(drivers_received_qty_70w) as drivers_received_qty_70w"),DB::raw("SUM(drivers_received_qty_110w) as drivers_received_qty_110w"),DB::raw("SUM(drivers_received_qty_190w) as drivers_received_qty_190w"),DB::raw("SUM(spd_received_qty_18w) as spd_received_qty_18w"),DB::raw("SUM(spd_received_qty_35w) as spd_received_qty_35w"),DB::raw("SUM(spd_received_qty_70w) as spd_received_qty_70w"),DB::raw("SUM(spd_received_qty_110w) as spd_received_qty_110w"),DB::raw("SUM(spd_received_qty_190w) as spd_received_qty_190w"),DB::raw("SUM(tiltadap_rcv_qty) as tiltadap_rcv_qty"),'from_jilla_id','maker_name','invoice_number','date_of_transfer')->where('to_jilla_id',$request->id)->groupby('invoice_number')->get()->toArray();





       return view('backend.inwards_invoices_balance_list',compact('area_invoice','district','maker_counts','inventory','inventory_total','makers_inventory_total','area_balance','incoming_inventory_total'));
    }

}

 
   