<?php
namespace App\Http\Controllers\v2019;
use Illuminate\Http\Request; 
use App\Http\Controllers\Controller; 
use Illuminate\Support\Facades\Auth; 
use Illuminate\Support\Facades\DB;
use Validator;
use Mail;
use App\Eximmaster;
use App\Logisticdata;
use App\LogisticshipData;
use App\Railwayprogramme;
use App\Abbreviation;
use DateTime;
use App\Traits\VesselLineUpGraphTrait;
use App\Services\VesselLineUpGraphService;

class MasterController extends Controller 
{
    use VesselLineUpGraphTrait;
	public $successStatus = 200;
	
/*
	START: code done by freelancer: mutasim 
	DATE: 17-sept-2022
*/

   public function vessel_lineup_working_graph(Request $request, VesselLineUpGraphService $graphService)
    {
        $data = $request->only('type_wise', 'commodity', 'country', 'trade_type', 'vessel_status', 'sub_commodity', 'vessel_size', 'country_wise', 'load_port', 'unload_port', 'company','selected_market');
        $validator = Validator::make($data, [
            'commodity' => 'required',
            'country' => 'required',
            'trade_type' => 'required',
            'selected_market'=>'required'
        ]);

        if ($validator->fails()) {
            return response()->json(['status' => 401, 'error' => $validator->messages()]);
        }
        if ($request->has('selected_market')){
			$selected_market = $request->input('selected_market');			
		}
        $check_market = '';
		if($selected_market=='coal')
            $check_market = 'CM';
        elseif($selected_market=='ferrous')
            $check_market = 'SM';

        $getData = DB::table('tbl_logistic_data')->where(['movement' => $data['trade_type']])->where('sm_cm', $check_market);

        $dataMapping = config('data-mapping.vessel_line_up.working');
        $getData = $graphService->applySelectFilter($dataMapping['select_condition'], $data, $getData, 'working');

        $filterDataMapping = config('data-mapping.vessel_line_up.common_filters');
        $getData = $graphService->applyFilters($filterDataMapping, $data, $getData);

        $response = $graphService->prepareWorkingData($data, $getData->whereIn('status', ['Anchorage', 'Berth', 'Expected'])->orderBy('status'));

        return $graphService->returnGraphResult($response, $data);
    }

    public function vessel_lineup_sailed_graph(Request $request, VesselLineUpGraphService $graphService)
    {
        $data = $request->only('date_formate', 'from_date', 'to_date', 'type_wise', 'commodity', 'country', 'trade_type', 'vessel_status', 'sub_commodity', 'vessel_size', 'country_wise', 'load_port', 'unload_port', 'company','selected_market');
        $validator = Validator::make($data, [
            'date_formate' => 'required',
            'from_date' => 'required',
            'to_date' => 'required',
            'commodity' => 'required',
            'country' => 'required',
            'trade_type' => 'required',
            'selected_market'=> 'required'
        ]);

        if ($validator->fails()) {
            return response()->json(['status' => 401, 'error' => $validator->messages()]);
        }
        
        if ($request->has('selected_market')){
			$selected_market = $request->input('selected_market');			
		}
        $check_market = '';
		if($selected_market=='coal')
            $check_market = 'CM';
        elseif($selected_market=='ferrous')
            $check_market = 'SM';

        $getData = DB::table('tbl_logistics_shipped_data')->where(['movement' => $data['trade_type'], 'sm_cm' => $check_market]);
        $dataMapping = config('data-mapping.vessel_line_up.sailed');

        $getData = $graphService->applySelectFilter($dataMapping['select_condition'], $data, $getData, 'sailed');

        $getData = $getData->whereBetween('formated_date', $this->getDateFromFormat($data['date_formate'], $data['from_date'], $data['to_date']))->orderBy('formated_date');

        $filterDataMapping = config('data-mapping.vessel_line_up.common_filters');
        $getData = $graphService->applyFilters($filterDataMapping, $data, $getData);

        $response = $graphService->prepareSaildData($data, $getData);

        //return $response;

        return $graphService->returnGraphResult($response, $data);
    }
 /*
	END: code done by freelancer: mutasim 
	DATE: 17-sept-2022
*/
  
    /********
    ** vessel freight filter
    * ******/
    public function vessel_freight()
    {
        // get data from table
        $res = Eximmaster::select('item')->whereIn('category',['LOGISTICS'])->groupBy('item')->get();
        $data=[];
        $all=[];
        //this is for to set filter value of item
        foreach($res as $key => $value){
            $load = Eximmaster::select('load_country')->where(['item'=>$value->item])->whereIn('category',['LOGISTICS'])->groupBy('load_country')->get();
            foreach($load as $ky => $lvalue){
                $unload = Eximmaster::select('unload_country','f_grade')->where(['item'=>$value->item,'load_country'=>$lvalue->load_country])->whereIn('category',['LOGISTICS'])->groupBy('unload_country','f_grade')->get();
                foreach($unload as $ku =>$uvalue){
                    $data[$key][$value->item][$lvalue->load_country]['unload'][$uvalue->unload_country]=$uvalue->unload_country;
                    $data[$key][$value->item][$lvalue->load_country]['grade'][$uvalue->f_grade]=$uvalue->f_grade;
                }
                if(count($data[$key][$value->item][$lvalue->load_country]['unload'])>1){
                    $data[$key][$value->item][$lvalue->load_country]['unload'][]='All';
                }
                if(count($data[$key][$value->item][$lvalue->load_country]['grade'])>1){
                    $data[$key][$value->item][$lvalue->load_country]['grade'][]='All';
                }
            }

        }
        //this is for to set all filter value
        $load = Eximmaster::select('load_country')->whereIn('category',['LOGISTICS'])->groupBy('load_country')->get();
        foreach($load as $ke =>$val){
            $unload = Eximmaster::select('unload_country','f_grade')->where(['load_country'=>$val->load_country])->whereIn('category',['LOGISTICS'])->groupBy('unload_country')->get();
            foreach($unload as $ku =>$uvalue){
                $all['All'][$val->load_country]['unload'][$uvalue->unload_country]=$uvalue->unload_country;
               // $all['All'][$val->load_country]['grade'][$uvalue->f_grade]=$uvalue->f_grade;
            }
             $unload = Eximmaster::select('unload_country','f_grade')->where(['load_country'=>$val->load_country])->whereIn('category',['LOGISTICS'])->groupBy('f_grade')->get();
            foreach($unload as $ku =>$uvalue){
                //$all['All'][$val->load_country]['unload'][$uvalue->unload_country]=$uvalue->unload_country;
                $all['All'][$val->load_country]['grade'][$uvalue->f_grade]=$uvalue->f_grade;
            }
            if(count($all['All'][$val->load_country]['unload'])>1){
                $all['All'][$val->load_country]['unload'][]='All';
            }
            if(count($all['All'][$val->load_country]['grade'])>1){
                $all['All'][$val->load_country]['grade'][]='All';
            }
            // $all['All'][$val->load_country]['unload'][]='All';
            // $all['All'][$val->load_country]['grade'][]='All';
        }
        // end of all filter
        $data[]=$all;
        if(!empty($data)){
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$data],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }
    /* 
    //function with  market conditions
    public function vessel_freight(Request $request)
    {
        if ($request->has('selected_market')){
			$selected_market = $request->input('selected_market');			
        }
        // get data from table
        $res = Eximmaster::select('item')->whereRaw("FIND_IN_SET('$selected_market', f_market)")->whereIn('category',['LOGISTICS'])->groupBy('item')->get();
        $data=[];
        $all=[];
        //this is for to set filter value of item
        foreach($res as $key => $value){
            $load = Eximmaster::select('load_country')->whereRaw("FIND_IN_SET('$selected_market', f_market)")->where(['item'=>$value->item])->whereIn('category',['LOGISTICS'])->groupBy('load_country')->get();
            foreach($load as $ky => $lvalue){
                $unload = Eximmaster::select('unload_country','f_grade')->whereRaw("FIND_IN_SET('$selected_market', f_market)")->where(['item'=>$value->item,'load_country'=>$lvalue->load_country])->whereIn('category',['LOGISTICS'])->groupBy('unload_country','f_grade')->get();
                foreach($unload as $ku =>$uvalue){
                    $data[$key][$value->item][$lvalue->load_country]['unload'][$uvalue->unload_country]=$uvalue->unload_country;
                    $data[$key][$value->item][$lvalue->load_country]['grade'][$uvalue->f_grade]=$uvalue->f_grade;
                }
                if(count($data[$key][$value->item][$lvalue->load_country]['unload'])>1){
                    $data[$key][$value->item][$lvalue->load_country]['unload'][]='All';
                }
                if(count($data[$key][$value->item][$lvalue->load_country]['grade'])>1){
                    $data[$key][$value->item][$lvalue->load_country]['grade'][]='All';
                }
            }

        }
        //this is for to set all filter value
        $load = Eximmaster::select('load_country')->whereRaw("FIND_IN_SET('$selected_market', f_market)")->whereIn('category',['LOGISTICS'])->groupBy('load_country')->get();
        foreach($load as $ke =>$val){
            $unload = Eximmaster::select('unload_country','f_grade')->whereRaw("FIND_IN_SET('$selected_market', f_market)")->where(['load_country'=>$val->load_country])->whereIn('category',['LOGISTICS'])->groupBy('unload_country')->get();
            foreach($unload as $ku =>$uvalue){
                $all['All'][$val->load_country]['unload'][$uvalue->unload_country]=$uvalue->unload_country;
               // $all['All'][$val->load_country]['grade'][$uvalue->f_grade]=$uvalue->f_grade;
            }
             $unload = Eximmaster::select('unload_country','f_grade')->whereRaw("FIND_IN_SET('$selected_market', f_market)")->where(['load_country'=>$val->load_country])->whereIn('category',['LOGISTICS'])->groupBy('f_grade')->get();
            foreach($unload as $ku =>$uvalue){
                //$all['All'][$val->load_country]['unload'][$uvalue->unload_country]=$uvalue->unload_country;
                $all['All'][$val->load_country]['grade'][$uvalue->f_grade]=$uvalue->f_grade;
            }
            if(count($all['All'][$val->load_country]['unload'])>1){
                $all['All'][$val->load_country]['unload'][]='All';
            }
            if(count($all['All'][$val->load_country]['grade'])>1){
                $all['All'][$val->load_country]['grade'][]='All';
            }
            // $all['All'][$val->load_country]['unload'][]='All';
            // $all['All'][$val->load_country]['grade'][]='All';
        }
        // end of all filter
        $data[]=$all;
        if(!empty($data)){
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$data],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }
*/
    /********
    ** vessel  lineup
    * ******/
    public function vessel_lineup(Request $request)
    {      
        // get requested data from post
        $data = $request->only('selected_market');
        $validator = Validator::make($data, [          
            'selected_market' => 'required',
        ]);

        //Send failed response if request is not valid
        if ($validator->fails()) {
            return response()->json(['status' => 401,'error' => $validator->messages()]);
        }
        
        if ($request->has('selected_market')){
			$selected_market = $request->input('selected_market');			
		}
		$check_market = '';
		if($selected_market=='coal')
            $check_market = 'CM';
        elseif($selected_market=='ferrous')
            $check_market = 'SM';
       
           
       // get data from table
        $commodity = Logisticdata::select('commodity')->where('sm_cm','=',$check_market)->groupBy('commodity')->get();
        $data=[];
        $all=[];
        ////DB::enableQueryLog();
        //this is for to set filter value of item
        foreach($commodity as $key => $value){
            $country = Logisticdata::select('selected_country')->where(['commodity'=>$value->commodity, 'sm_cm'=>$check_market])->groupBy('selected_country')->get();
            // $country = Logisticdata::select('selected_country')->where(['commodity'=>$value->commodity])->groupBy('load_country')->get();
            foreach($country as $ky => $lvalue){
                $movement = Logisticdata::select('movement')->where(['commodity'=>$value->commodity,'selected_country'=>$lvalue->selected_country,'sm_cm'=>$check_market])->groupBy('movement')->get();
                foreach($movement as $ku => $uvalue){
                    $data[$key][$value->commodity][$lvalue->selected_country][]=$uvalue->movement;
                }
            }
            if(count($country)>1){
                $movement = Logisticdata::select('movement')->where(['commodity'=>$value->commodity,'sm_cm'=>$check_market])->groupBy('movement')->get();
                foreach($movement as $ku => $uvalue){
                    $data[$key][$value->commodity]['All'][]=$uvalue->movement;
                }
            }
            
        }
        // dd(DB::getQueryLog());
        //this is for to set all filter value
        $country = Logisticdata::select('selected_country')->groupBy('selected_country')->get();
        foreach($country as $ke =>$val){
            $movement = Logisticdata::select('movement')->where(['selected_country'=>$val->selected_country,'sm_cm'=>$check_market])->groupBy('movement')->get();
            foreach($movement as $ku => $uvalue){
                $all['All'][$val->selected_country][]=$uvalue->movement;
            }
        }
        if(count($country)>1){
            $movement = Logisticdata::select('movement')->groupBy('movement')->get();
            foreach($movement as $ku => $uvalue){
                $all['All']['All'][]=$uvalue->movement;
            }
        }
        //all filter value end here
        $data[]=$all;
        if(!empty($data)){
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$data],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'Try Again'], 401);
        }
    }

    /********
    ** vessel  Sub lineup
    * ******/
    public function vessel_sublineup(Request $request)
    {
        // get requested data from post
        $data = $request->only('commodity', 'country', 'trade_type','selected_market');
        $validator = Validator::make($data, [
            'commodity' => 'required',
            'country' => 'required',
            'trade_type' => 'required',
            'selected_market' => 'required',
        ]);

        //Send failed response if request is not valid
        if ($validator->fails()) {
            return response()->json(['status' => 401,'error' => $validator->messages()]);
        }
         if ($request->has('selected_market')){
			$selected_market = $request->input('selected_market');			
		}
        $check_market = '';
		if($selected_market=='coal')
            $check_market = 'CM';
        elseif($selected_market=='ferrous')
            $check_market = 'SM';
       
       
        // DB::enableQueryLog();
        // get data from table 
        $getData = Logisticdata::select('id','status as vessel_status','cargo as sub_commodity','load_port','unload_port','load_country','unload_country','type_of_ships as vessel_size','shipper','receiver')
                    ->where('movement','=',$data['trade_type']);
                    if($data['commodity']!="All"){
                        $getData = $getData->where('commodity','=',$data['commodity']);
                    }
                    if($data['country']!="All"){
                        $getData = $getData->where('selected_country','=',$data['country']);
                    }
                    $getData = $getData->where('sm_cm','=',$check_market)->orderBy('id','DESC')->get();
        // dd(DB::getQueryLog());
        // this is for to set response of data
        $response=['vessel_size'=>[],'vessel_status'=>[],'sub_commodity'=>[],'load_port'=>[],'unload_port'=>[],'country'=>[],'company'=>[]];
        foreach($getData as $key =>$value){
            if (!in_array($value->vessel_size,$response['vessel_size'])){
                $response['vessel_size'][]=$value->vessel_size;
            }
            if (!in_array($value->vessel_status,$response['vessel_status'])){
                $response['vessel_status'][]=$value->vessel_status;
            }
            if (!in_array($value->sub_commodity,$response['sub_commodity'])){
                $response['sub_commodity'][]=$value->sub_commodity;
            }
            if (!in_array($value->load_port,$response['load_port'])){
                $response['load_port'][]=$value->load_port;
            }
            if (!in_array($value->unload_port,$response['unload_port'])){
                $response['unload_port'][]=$value->unload_port;
            }
            if($data['trade_type']=="Export"||$data['trade_type']=="Coastal (Load)"){
                if (!in_array($value->unload_country,$response['country'])){
                    $response['country'][]=$value->unload_country;
                }
            }else{
                if (!in_array($value->load_country,$response['country'])){
                    $response['country'][]=$value->load_country;
                }
            }
			if($data['trade_type']=="Export"||$data['trade_type']=="Coastal (Load)"){
                if (!in_array($value->shipper,$response['company'])){
                    $response['company'][]=$value->shipper;
                }
            }else{
                if (!in_array($value->receiver ,$response['company'])){
                    $response['company'][]=$value->receiver;
                }
            }
        }
        $final_response=array();
        foreach($response as $key1=>$val1){
             sort($val1);
             $final_response[$key1] = $val1;
         }
        if(!empty($final_response)){
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$final_response],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }

    /********
    ** vessel lineup graph
    * ******/
    public function vessel_lineup_graph(Request $request)
    {
        // get requested data from post
        $data = $request->only('type_wise','commodity', 'country', 'trade_type','vessel_status','sub_commodity','vessel_size','country_wise','load_port','unload_port','company');
        $validator = Validator::make($data, [
            'commodity' => 'required',
            'country' => 'required',
            'trade_type' => 'required',
        ]);
        
         if ($request->has('selected_market')){
			$selected_market = $request->input('selected_market');			
		}
		$check_market = '';
		if($selected_market=='coal')
            $check_market = 'CM';
        elseif($selected_market=='ferrous')
            $check_market = 'SM';
            
        //Send failed response if request is not valid
        if ($validator->fails()) {
            return response()->json(['status' => 401,'error' => $validator->messages()]);
        }
        $arr=['Anchorage','Berth','Expected'];
        // $response=['Anchorage'=>[],'Berth'=>[],'Expected'=>[]];
        $response=['0'=>['name'=>'Anchorage','data'=>[]],'1'=>['name'=>'Berth','data'=>[]],'2'=>['name'=>'Expected','data'=>[]]];
        

        if($data['type_wise']=="sub-commodity"){
            // get data from table
            $getData = Logisticdata::select('status','cargo','qty');
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $vessel_status= explode(',',$data['vessel_status']);
                $getData = $getData->whereIn('status',$vessel_status);
            }
            if($data['vessel_size']!=""){
            $vessel_size= explode(',',$data['vessel_size']);
            $getData = $getData->whereIn('type_of_ships',$vessel_size);
            }
            if($data['sub_commodity']!=""){
                $sub_commodity= explode(',',$data['sub_commodity']);
                $getData = $getData->whereIn('cargo',$sub_commodity);
            }
            if($data['country_wise']!=""){
                $country_wise= explode(',',$data['country_wise']);
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->whereIn('unload_country',$country_wise);
                }else{
                    $getData = $getData->whereIn('load_country',$country_wise);
                }
            }
			if($data['company']!=""){
                $company= explode(',',$data['company']);
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->whereIn('shipper',$company);
                }else{
                    $getData = $getData->whereIn('receiver',$company);
                }
            }
            if($data['load_port']!=""){
                $load_port= explode(',',$data['load_port']);
                $getData = $getData->whereIn('load_port',$load_port);
            }
            if($data['unload_port']!=""){
                $unload_port= explode(',',$data['unload_port']);
                $getData = $getData->whereIn('unload_port',$unload_port);
            }
            $getData = $getData->whereIn('status',$arr)->where('sm_cm','=',$check_market)->get();

            // $getData = $getData->whereIn('status',$arr)->groupBy('cargo')->get();
            // set response arr of api
            // foreach($response as $response_k =>$v){
            //     foreach($getData as $kd => $val){
            //         $response[$response_k]['data'][$kd]['name']=$val->cargo;
            //         $response[$response_k]['data'][$kd]['value']=0;
            //     }
            // }

            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($val->cargo==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$val->cargo;
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$val->cargo;
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            // set response of api
            foreach($getData as $key => $value){
                foreach ($response as $rkkey=> $rkvalue) {
                    if($value->status == $rkvalue['name']){
                        foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                            if($ksvalue['name']==$value->cargo){
                                $response[$rkkey]['data'][$kskey]['value']=$response[$rkkey]['data'][$kskey]['value']+$value->qty;
                            }
                        }
                    }
                }
            }
        }elseif($data['type_wise']=="vessel-size"){
            // get data from table
            $getData = Logisticdata::select('status','type_of_ships','qty');
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $vessel_status= explode(',',$data['vessel_status']);
                $getData = $getData->whereIn('status',$vessel_status);
            }
            if($data['vessel_size']!=""){
            $vessel_size= explode(',',$data['vessel_size']);
            $getData = $getData->whereIn('type_of_ships',$vessel_size);
            }
            if($data['sub_commodity']!=""){
                $sub_commodity= explode(',',$data['sub_commodity']);
                $getData = $getData->whereIn('cargo',$sub_commodity);
            }
            if($data['country_wise']!=""){
                $country_wise= explode(',',$data['country_wise']);
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->whereIn('unload_country',$country_wise);
                }else{
                    $getData = $getData->whereIn('load_country',$country_wise);
                }
            }
			if($data['company']!=""){
                $company= explode(',',$data['company']);
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->whereIn('shipper',$company);
                }else{
                    $getData = $getData->whereIn('receiver',$company);
                }
            }
            if($data['load_port']!=""){
                $load_port= explode(',',$data['load_port']);
                $getData = $getData->whereIn('load_port',$load_port);
            }
            if($data['unload_port']!=""){
                $unload_port= explode(',',$data['unload_port']);
                $getData = $getData->whereIn('unload_port',$unload_port);
            }
            $getData = $getData->whereIn('status',$arr)->where('sm_cm','=',$check_market)->get();

            // $getData = $getData->whereIn('status',$arr)->groupBy('cargo')->get();
            // set response arr of api
            // foreach($response as $response_k =>$v){
            //     foreach($getData as $kd => $val){
            //         $response[$response_k]['data'][$kd]['name']=$val->cargo;
            //         $response[$response_k]['data'][$kd]['value']=0;
            //     }
            // }

            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($val->type_of_ships==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$val->type_of_ships;
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$val->type_of_ships;
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            // set response of api
            foreach($getData as $key => $value){
                foreach ($response as $rkkey=> $rkvalue) {
                    if($value->status == $rkvalue['name']){
                        foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                            if($ksvalue['name']==$value->type_of_ships){
                                $response[$rkkey]['data'][$kskey]['value']=$response[$rkkey]['data'][$kskey]['value']+$value->qty;
                            }
                        }
                    }
                }
            }
        }elseif($data['type_wise']=="shipper"){
            // get data from table
            if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                 $getData = Logisticdata::select('status','shipper','qty');
            }
            else
                 $getData = Logisticdata::select('status','receiver as shipper','qty');
                    
             
           // $getData = Logisticdata::select('status','shipper','qty');
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $vessel_status= explode(',',$data['vessel_status']);
                $getData = $getData->whereIn('status',$vessel_status);
            }
            if($data['vessel_size']!=""){
            $vessel_size= explode(',',$data['vessel_size']);
            $getData = $getData->whereIn('type_of_ships',$vessel_size);
            }
            if($data['sub_commodity']!=""){
                $sub_commodity= explode(',',$data['sub_commodity']);
                $getData = $getData->whereIn('cargo',$sub_commodity);
            }
            if($data['country_wise']!=""){
                $country_wise= explode(',',$data['country_wise']);
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->whereIn('unload_country',$country_wise);
                }else{
                    $getData = $getData->whereIn('load_country',$country_wise);
                }
            }
			if($data['company']!=""){
                $company= explode(',',$data['company']);
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->whereIn('shipper',$company);
                }else{
                    $getData = $getData->whereIn('receiver',$company);
                }
            }
            if($data['load_port']!=""){
                $load_port= explode(',',$data['load_port']);
                $getData = $getData->whereIn('load_port',$load_port);
            }
            if($data['unload_port']!=""){
                $unload_port= explode(',',$data['unload_port']);
                $getData = $getData->whereIn('unload_port',$unload_port);
            }
            $getData = $getData->whereIn('status',$arr)->where('sm_cm','=',$check_market)->get();
            // $getData = $getData->whereIn('status',$arr)->groupBy('shipper')->get();
            // set response arr of api
            // foreach($response as $response_k =>$v){
            //     foreach($getData as $kd => $val){
            //         $response[$response_k]['data'][$kd]['name']=$val->shipper;
            //         $response[$response_k]['data'][$kd]['value']=0;
            //     }
            // }
            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($val->shipper==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$val->shipper;
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$val->shipper;
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            // set response of api
            foreach($getData as $key => $value){
                foreach ($response as $rkkey=> $rkvalue) {
                    if($value->status == $rkvalue['name']){
                        foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                            if($ksvalue['name']==$value->shipper){
                                $response[$rkkey]['data'][$kskey]['value']=$response[$rkkey]['data'][$kskey]['value']+$value->qty;
                            }
                        }
                    }
                }
            }
        }elseif($data['type_wise']=="country"){
            // get data from table
            if($data['trade_type']=="Export"||$data['trade_type']=="Coastal (Load)"){
                $getData = Logisticdata::select('id','status','unload_country as country','qty');
            }else{
                $getData = Logisticdata::select('id','status','load_country as country','qty');
            }
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $vessel_status= explode(',',$data['vessel_status']);
                $getData = $getData->whereIn('status',$vessel_status);
            }
            if($data['vessel_size']!=""){
            $vessel_size= explode(',',$data['vessel_size']);
            $getData = $getData->whereIn('type_of_ships',$vessel_size);
            }
            if($data['sub_commodity']!=""){
                $sub_commodity= explode(',',$data['sub_commodity']);
                $getData = $getData->whereIn('cargo',$sub_commodity);
            }
            if($data['country_wise']!=""){
                $country_wise= explode(',',$data['country_wise']);
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->whereIn('unload_country',$country_wise);
                }else{
                    $getData = $getData->whereIn('load_country',$country_wise);
                }
            }
			if($data['company']!=""){
                $company= explode(',',$data['company']);
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->whereIn('shipper',$company);
                }else{
                    $getData = $getData->whereIn('receiver',$company);
                }
            }
            if($data['load_port']!=""){
                $load_port= explode(',',$data['load_port']);
                $getData = $getData->whereIn('load_port',$load_port);
            }
            if($data['unload_port']!=""){
                $unload_port= explode(',',$data['unload_port']);
                $getData = $getData->whereIn('unload_port',$unload_port);
            }
            $getData = $getData->whereIn('status',$arr)->where('sm_cm','=',$check_market)->get();
            // $getData = $getData->whereIn('status',$arr)->groupBy('country')->get();
            
            // set response arr of api
            // foreach($response as $response_k =>$v){
            //     foreach($getData as $kd => $val){
            //         $response[$response_k]['data'][$kd]['name']=$val->country;
            //         $response[$response_k]['data'][$kd]['value']=0;
            //     }
            // }

            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($val->country==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$val->country;
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$val->country;
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }

            // set response of api
            foreach($getData as $key => $value){
                foreach ($response as $rkkey=> $rkvalue) {
                    if($value->status == $rkvalue['name']){
                        foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                            if($ksvalue['name']==$value->country){
                                $response[$rkkey]['data'][$kskey]['value']=$response[$rkkey]['data'][$kskey]['value']+$value->qty;
                            }
                        }
                    }
                }
            }
        }elseif($data['type_wise']=="port"){
            // get data from table
            if($data['trade_type']=="Export"||$data['trade_type']=="Coastal (Load)"){
                $getData = Logisticdata::select('id','status','load_port as port','qty');
            }else{
                $getData = Logisticdata::select('id','status','unload_port as port','qty');
            }
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_size']!=""){
                $vessel_size= explode(',',$data['vessel_size']);
                $getData = $getData->whereIn('type_of_ships',$vessel_size);
            }
            if($data['vessel_status']!=""){
                $vessel_status= explode(',',$data['vessel_status']);
                $getData = $getData->whereIn('status',$vessel_status);
            }
            if($data['sub_commodity']!=""){
                $sub_commodity= explode(',',$data['sub_commodity']);
                $getData = $getData->whereIn('cargo',$sub_commodity);
            }
            if($data['country_wise']!=""){
                $country_wise= explode(',',$data['country_wise']);
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->whereIn('unload_country',$country_wise);
                }else{
                    $getData = $getData->whereIn('load_country',$country_wise);
                }
            }
			if($data['company']!=""){
                $company= explode(',',$data['company']);
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->whereIn('shipper',$company);
                }else{
                    $getData = $getData->whereIn('receiver',$company);
                }
            }
            if($data['load_port']!=""){
                $load_port= explode(',',$data['load_port']);
                $getData = $getData->whereIn('load_port',$load_port);
            }
            if($data['unload_port']!=""){
                $unload_port= explode(',',$data['unload_port']);
                $getData = $getData->whereIn('unload_port',$unload_port);
            }
            $getData = $getData->whereIn('status',$arr)->where('sm_cm','=',$check_market)->get();
            // $getData = $getData->whereIn('status',$arr)->groupBy('port')->get();
            // set response arr of api
            // foreach($response as $response_k =>$v){
            //     foreach($getData as $kd => $val){
            //         $response[$response_k]['data'][$kd]['name']=$val->port;
            //         $response[$response_k]['data'][$kd]['value']=0;
            //     }
            // }
            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($val->port==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$val->port;
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$val->port;
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            // set response of api
            foreach($getData as $key => $value){
                foreach ($response as $rkkey=> $rkvalue) {
                    if($value->status == $rkvalue['name']){
                        foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                            if($ksvalue['name']==$value->port){
                                $response[$rkkey]['data'][$kskey]['value']=$response[$rkkey]['data'][$kskey]['value']+$value->qty;
                            }
                        }
                    }
                }
            }
            // print_r($response);die;                                            
        }else{
            // DB::enableQueryLog();
            $response=['Anchorage'=>0,'Berth'=>0,'Expected'=>0];
            // get data from table
            $getData = Logisticdata::select('status','qty');
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $vessel_status= explode(',',$data['vessel_status']);
                $getData = $getData->whereIn('status',$vessel_status);
            }            
            if($data['vessel_size']!=""){
            $vessel_size= explode(',',$data['vessel_size']);
            $getData = $getData->whereIn('type_of_ships',$vessel_size);
            }
            if($data['sub_commodity']!=""){
                $sub_commodity= explode(',',$data['sub_commodity']);
                $getData = $getData->whereIn('cargo',$sub_commodity);
            }
            if($data['country_wise']!=""){
                $country_wise= explode(',',$data['country_wise']);
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->whereIn('unload_country',$country_wise);
                }else{
                    $getData = $getData->whereIn('load_country',$country_wise);
                }
            }
			if($data['company']!=""){
                $company= explode(',',$data['company']);
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->whereIn('shipper',$company);
                }else{
                    $getData = $getData->whereIn('receiver',$company);
                }
            }
            if($data['load_port']!=""){
                $load_port= explode(',',$data['load_port']);
                $getData = $getData->whereIn('load_port',$load_port);
            }
            if($data['unload_port']!=""){
                $unload_port= explode(',',$data['unload_port']);
                $getData = $getData->whereIn('unload_port',$unload_port);
            }
            $getData = $getData->whereIn('status',$arr)->where('sm_cm','=',$check_market)->get();
            // dd(DB::getQueryLog());
            // set response of api
            foreach($getData as $value){
                $response[$value->status]=($response[$value->status]+$value->qty);
            }
        }
        if($data['type_wise']!=""){
            $newdata=[];
            $c=0;
            foreach ($response as $key => $value) {
                $newdata[$c]['name']=$value['name'];
                $newdata[$c]['data']=isset($value['data'])?array_values($value['data']):'';
                $c++;
            }
            $response=$newdata;   
        }
        if(!empty($response)){
            $datas['graph_type']=  empty($data['type_wise']) ? 'Parent graph' : 'child graph ('.$data['type_wise'].' wise)';
            $datas['graph_points']= $response;
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$datas],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }

    /********
    ** vessel lineup graph old
    * ******/

    public function vessel_lineup_graph_old2(Request $request)
    {
        // get requested data from post
        $data = $request->only('type_wise','commodity', 'country', 'trade_type','vessel_status','sub_commodity','vessel_size','country_wise','load_port','unload_port');
        $validator = Validator::make($data, [
            'commodity' => 'required',
            'country' => 'required',
            'trade_type' => 'required',
        ]);
        //Send failed response if request is not valid
        if ($validator->fails()) {
            return response()->json(['status' => 401,'error' => $validator->messages()]);
        }
        $arr=['Anchorage','Berth','Expected'];
        $response=['Anchorage'=>[],'Berth'=>[],'Expected'=>[]];

        if($data['type_wise']=="sub-commodity"){
            // get data from table
            $getData = Logisticdata::select('status','cargo','qty');
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $vessel_status= explode(',',$data['vessel_status']);
                $getData = $getData->whereIn('status',$vessel_status);
            }
            if($data['sub_commodity']!=""){
                $sub_commodity= explode(',',$data['sub_commodity']);
                $getData = $getData->whereIn('cargo',$sub_commodity);
            }
            if($data['country_wise']!=""){
                $country_wise= explode(',',$data['country_wise']);
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->whereIn('unload_country',$country_wise);
                }else{
                    $getData = $getData->whereIn('load_country',$country_wise);
                }
            }
            if($data['load_port']!=""){
                $load_port= explode(',',$data['load_port']);
                $getData = $getData->whereIn('load_port',$load_port);
            }
            if($data['unload_port']!=""){
                $unload_port= explode(',',$data['unload_port']);
                $getData = $getData->whereIn('unload_port',$unload_port);
            }
            $getData = $getData->whereIn('status',$arr)->get();
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $val){
                    $response[$response_k][$val->cargo]=0;
                }
            }
            // set response of api
            foreach($getData as $value){
                // $response[$value->status][$value->cargo]++;
                $response[$value->status][$value->cargo]=($value->qty+$response[$value->status][$value->cargo]);
            }
        }elseif($data['type_wise']=="shipper"){
            // get data from table
            $getData = Logisticdata::select('status','shipper','qty');
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $vessel_status= explode(',',$data['vessel_status']);
                $getData = $getData->whereIn('status',$vessel_status);
            }
            if($data['sub_commodity']!=""){
                $sub_commodity= explode(',',$data['sub_commodity']);
                $getData = $getData->whereIn('cargo',$sub_commodity);
            }
            if($data['country_wise']!=""){
                $country_wise= explode(',',$data['country_wise']);
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->whereIn('unload_country',$country_wise);
                }else{
                    $getData = $getData->whereIn('load_country',$country_wise);
                }
            }
            if($data['load_port']!=""){
                $load_port= explode(',',$data['load_port']);
                $getData = $getData->whereIn('load_port',$load_port);
            }
            if($data['unload_port']!=""){
                $unload_port= explode(',',$data['unload_port']);
                $getData = $getData->whereIn('unload_port',$unload_port);
            }
            $getData = $getData->whereIn('status',$arr)->get();
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $val){
                    $response[$response_k][$val->shipper]=0;
                }
            }
            // set response of api
            foreach($getData as $value){
                // $response[$value->status][$value->shipper]++;
                $response[$value->status][$value->shipper]=($value->qty+$response[$value->status][$value->shipper]);
            }
        }elseif($data['type_wise']=="country"){
            // get data from table
            if($data['trade_type']=="Export"||$data['trade_type']=="Coastal (Load)"){
                $getData = Logisticdata::select('id','status','unload_country as country');
            }else{
                $getData = Logisticdata::select('id','status','load_country as country');
            }
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $vessel_status= explode(',',$data['vessel_status']);
                $getData = $getData->whereIn('status',$vessel_status);
            }
            if($data['sub_commodity']!=""){
                $sub_commodity= explode(',',$data['sub_commodity']);
                $getData = $getData->whereIn('cargo',$sub_commodity);
            }
            if($data['country_wise']!=""){
                $country_wise= explode(',',$data['country_wise']);
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->whereIn('unload_country',$country_wise);
                }else{
                    $getData = $getData->whereIn('load_country',$country_wise);
                }
            }
            if($data['load_port']!=""){
                $load_port= explode(',',$data['load_port']);
                $getData = $getData->whereIn('load_port',$load_port);
            }
            if($data['unload_port']!=""){
                $unload_port= explode(',',$data['unload_port']);
                $getData = $getData->whereIn('unload_port',$unload_port);
            }
            $getData = $getData->whereIn('status',$arr)->get();
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $val){
                    $response[$response_k][$val->country]=0;
                }
            }
            // set response of api
            foreach($getData as $value){
                $response[$value->status][$value->country]++;
            }
        }elseif($data['type_wise']=="port"){
            // get data from table
            if($data['trade_type']=="Export"||$data['trade_type']=="Coastal (Load)"){
                $getData = Logisticdata::select('id','status','load_port as port');
            }else{
                $getData = Logisticdata::select('id','status','unload_port as port');
            }
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $vessel_status= explode(',',$data['vessel_status']);
                $getData = $getData->whereIn('status',$vessel_status);
            }
            if($data['sub_commodity']!=""){
                $sub_commodity= explode(',',$data['sub_commodity']);
                $getData = $getData->whereIn('cargo',$sub_commodity);
            }
            if($data['country_wise']!=""){
                $country_wise= explode(',',$data['country_wise']);
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->whereIn('unload_country',$country_wise);
                }else{
                    $getData = $getData->whereIn('load_country',$country_wise);
                }
            }
            if($data['load_port']!=""){
                $load_port= explode(',',$data['load_port']);
                $getData = $getData->whereIn('load_port',$load_port);
            }
            if($data['unload_port']!=""){
                $unload_port= explode(',',$data['unload_port']);
                $getData = $getData->whereIn('unload_port',$unload_port);
            }
            $getData = $getData->whereIn('status',$arr)->get();
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $val){
                    $response[$response_k][$val->port]=0;
                }
            }
            // set response of api
            foreach($getData as $value){
                $response[$value->status][$value->port]++;
            }
        }else{
            // DB::enableQueryLog();
            $response=['Anchorage'=>0,'Berth'=>0,'Expected'=>0];
            // get data from table
            $getData = Logisticdata::select('status');
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $vessel_status= explode(',',$data['vessel_status']);
                $getData = $getData->whereIn('status',$vessel_status);
            }
            if($data['sub_commodity']!=""){
                $sub_commodity= explode(',',$data['sub_commodity']);
                $getData = $getData->whereIn('cargo',$sub_commodity);
            }
            if($data['country_wise']!=""){
                $country_wise= explode(',',$data['country_wise']);
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->whereIn('unload_country',$country_wise);
                }else{
                    $getData = $getData->whereIn('load_country',$country_wise);
                }
            }
            if($data['load_port']!=""){
                $load_port= explode(',',$data['load_port']);
                $getData = $getData->whereIn('load_port',$load_port);
            }
            if($data['unload_port']!=""){
                $unload_port= explode(',',$data['unload_port']);
                $getData = $getData->whereIn('unload_port',$unload_port);
            }
            $getData = $getData->whereIn('status',$arr)->get();
            // dd(DB::getQueryLog());
            // set response of api
            foreach($getData as $value){
                $response[$value->status]++;
            }
        }
        
        if(!empty($response)){
            $datas['graph_type']=  empty($data['type_wise']) ? 'Parent graph' : 'child graph ('.$data['type_wise'].' wise)';
            $datas['graph_points']= $response;
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$datas],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }

    public function vessel_lineup_graph_old1(Request $request)
    {
        // get requested data from post
        $data = $request->only('type_wise','commodity', 'country', 'trade_type','vessel_status','sub_commodity','vessel_size','country_wise','load_port','unload_port');
        $validator = Validator::make($data, [
            'commodity' => 'required',
            'country' => 'required',
            'trade_type' => 'required',
        ]);
        //Send failed response if request is not valid
        if ($validator->fails()) {
            return response()->json(['status' => 401,'error' => $validator->messages()]);
        }
        $arr=['Anchorage','Berth','Expected'];
        $response=['Anchorage'=>[],'Berth'=>[],'Expected'=>[]];

        if($data['type_wise']=="sub-commodity"){
            // get data from table
            $getData = Logisticdata::select('status','cargo');
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $getData = $getData->where(['status'=>$data['vessel_status']]);
            }
            if($data['sub_commodity']!=""){
                $getData = $getData->where(['cargo'=>$data['sub_commodity']]);
            }
            if($data['country_wise']!=""){
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->where(['unload_country'=>$data['country_wise']]);
                }else{
                    $getData = $getData->where(['load_country'=>$data['country_wise']]);
                }
            }
            if($data['load_port']!=""){
                $getData = $getData->where(['load_port'=>$data['load_port']]);
            }
            if($data['unload_port']!=""){
                $getData = $getData->where(['unload_port'=>$data['unload_port']]);
            }
            $getData = $getData->whereIn('status',$arr)->get();
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $val){
                    $response[$response_k][$val->cargo]=0;
                }
            }
            // set response of api
            foreach($getData as $value){
                $response[$value->status][$value->cargo]++;
            }
        }elseif($data['type_wise']=="shipper"){
            // get data from table
            $getData = Logisticdata::select('status','shipper');
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $getData = $getData->where(['status'=>$data['vessel_status']]);
            }
            if($data['sub_commodity']!=""){
                $getData = $getData->where(['cargo'=>$data['sub_commodity']]);
            }
            if($data['country_wise']!=""){
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->where(['unload_country'=>$data['country_wise']]);
                }else{
                    $getData = $getData->where(['load_country'=>$data['country_wise']]);
                }
            }
            if($data['load_port']!=""){
                $getData = $getData->where(['load_port'=>$data['load_port']]);
            }
            if($data['unload_port']!=""){
                $getData = $getData->where(['unload_port'=>$data['unload_port']]);
            }
            $getData = $getData->whereIn('status',$arr)->get();
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $val){
                    $response[$response_k][$val->shipper]=0;
                }
            }
            // set response of api
            foreach($getData as $value){
                $response[$value->status][$value->shipper]++;
            }
        }elseif($data['type_wise']=="country"){
            // get data from table
            if($data['trade_type']=="Export"||$data['trade_type']=="Coastal (Load)"){
                $getData = Logisticdata::select('id','status','unload_country as country');
            }else{
                $getData = Logisticdata::select('id','status','load_country as country');
            }
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $getData = $getData->where(['status'=>$data['vessel_status']]);
            }
            if($data['sub_commodity']!=""){
                $getData = $getData->where(['cargo'=>$data['sub_commodity']]);
            }
            if($data['country_wise']!=""){
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->where(['unload_country'=>$data['country_wise']]);
                }else{
                    $getData = $getData->where(['load_country'=>$data['country_wise']]);
                }
            }
            if($data['load_port']!=""){
                $getData = $getData->where(['load_port'=>$data['load_port']]);
            }
            if($data['unload_port']!=""){
                $getData = $getData->where(['unload_port'=>$data['unload_port']]);
            }
            $getData = $getData->whereIn('status',$arr)->get();
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $val){
                    $response[$response_k][$val->country]=0;
                }
            }
            // set response of api
            foreach($getData as $value){
                $response[$value->status][$value->country]++;
            }
        }elseif($data['type_wise']=="port"){
            // get data from table
            if($data['trade_type']=="Export"||$data['trade_type']=="Coastal (Load)"){
                $getData = Logisticdata::select('id','status','load_port as port');
            }else{
                $getData = Logisticdata::select('id','status','unload_port as port');
            }
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $getData = $getData->where(['status'=>$data['vessel_status']]);
            }
            if($data['sub_commodity']!=""){
                $getData = $getData->where(['cargo'=>$data['sub_commodity']]);
            }
            if($data['country_wise']!=""){
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->where(['unload_country'=>$data['country_wise']]);
                }else{
                    $getData = $getData->where(['load_country'=>$data['country_wise']]);
                }
            }
            if($data['load_port']!=""){
                $getData = $getData->where(['load_port'=>$data['load_port']]);
            }
            if($data['unload_port']!=""){
                $getData = $getData->where(['unload_port'=>$data['unload_port']]);
            }
            $getData = $getData->whereIn('status',$arr)->get();
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $val){
                    $response[$response_k][$val->port]=0;
                }
            }
            // set response of api
            foreach($getData as $value){
                $response[$value->status][$value->port]++;
            }
        }else{
            // DB::enableQueryLog();
            $response=['Anchorage'=>0,'Berth'=>0,'Expected'=>0];
            // get data from table
            $getData = Logisticdata::select('status');
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $getData = $getData->where(['status'=>$data['vessel_status']]);
            }
            if($data['sub_commodity']!=""){
                $getData = $getData->where(['cargo'=>$data['sub_commodity']]);
            }
            if($data['country_wise']!=""){
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->where(['unload_country'=>$data['country_wise']]);
                }else{
                    $getData = $getData->where(['load_country'=>$data['country_wise']]);
                }
            }
            if($data['load_port']!=""){
                $getData = $getData->where(['load_port'=>$data['load_port']]);
            }
            if($data['unload_port']!=""){
                $getData = $getData->where(['unload_port'=>$data['unload_port']]);
            }
            $getData = $getData->whereIn('status',$arr)->get();
            // dd(DB::getQueryLog());
            // set response of api
            foreach($getData as $value){
                $response[$value->status]++;
            }
        }
        
        if(!empty($response)){
            $datas['data']['graph_type']=  empty($data['type_wise']) ? 'Parent graph' : 'child graph ('.$data['type_wise'].' wise)';
            $datas['data']['graph_points']= $response;
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$datas],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }

    /********
    ** vessel  lineup sailed
    * ******/
    public function vessel_lineup_sailed(Request $request)
    {
        
        if ($request->has('selected_market')){
			$selected_market = $request->input('selected_market');			
		}
        $check_market = '';
		if($selected_market=='coal')
            $check_market = 'CM';
        elseif($selected_market=='ferrous')
            $check_market = 'SM';
       
        // get data from table
        $commodity = LogisticshipData::select('commodity')->where('sm_cm','=',$check_market)->groupBy('commodity')->get();
        $data=[];
        $all=[];
        
        //this is for to set filter value of item
        foreach($commodity as $key => $value){
            $country = LogisticshipData::select('selected_country')->where(['commodity'=>$value->commodity,'sm_cm'=>$check_market])->groupBy('selected_country')->get();
            // $country = LogisticshipData::select('selected_country')->where(['commodity'=>$value->commodity])->groupBy('load_country')->get();
            foreach($country as $ky => $lvalue){
                $movement = LogisticshipData::select('movement')->where(['commodity'=>$value->commodity,'selected_country'=>$lvalue->selected_country,'sm_cm'=>$check_market])->groupBy('movement')->get();
                foreach($movement as $ku => $uvalue){
                    $data[$key][$value->commodity][$lvalue->selected_country][]=$uvalue->movement;
                }
            }
            if(count($country)>1){
                $movement = LogisticshipData::select('movement')->where(['commodity'=>$value->commodity,'sm_cm'=>$check_market])->groupBy('movement')->get();
                foreach($movement as $ku => $uvalue){
                    $data[$key][$value->commodity]['All'][]=$uvalue->movement;
                }
            }
        }
        //this is for to set all filter value
        $country = LogisticshipData::select('selected_country')->groupBy('selected_country')->get();
        foreach($country as $ke =>$val){
            $movement = LogisticshipData::select('movement')->where(['selected_country'=>$val->selected_country,'sm_cm'=>$check_market])->groupBy('movement')->get();
            foreach($movement as $ku => $uvalue){
                $all['All'][$val->selected_country][]=$uvalue->movement;
            }
        }
        if(count($country)>1){
            $movement = LogisticshipData::select('movement')->groupBy('movement')->get();
            foreach($movement as $ku => $uvalue){
                $all['All']['All'][]=$uvalue->movement;
            }
        }
        //all filter value end here
        $data[]=$all;
        if(!empty($data)){
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$data],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }

    /********
    ** vessel  Sublineup sailed
    * ******/
    public function vessel_sublineup_sailed(Request $request)
    {
        // requested data from post
        $data = $request->only('commodity', 'country', 'trade_type','selected_market');
        $validator = Validator::make($data, [
            'commodity' => 'required',
            'country' => 'required',
            'trade_type' => 'required',
            'selected_market' => 'required',
        ]);

        //Send failed response if request is not valid
        if ($validator->fails()) {
            return response()->json(['success' => false,'error' => $validator->messages()]);
        }
        
           if ($request->has('selected_market')){
			$selected_market = $request->input('selected_market');			
		}
        
        
        $check_market = '';
		if($selected_market=='coal')
            $check_market = 'CM';
        elseif($selected_market=='ferrous')
            $check_market = 'SM';
        // get data from table on behalf of filter
        $getData = LogisticshipData::select('id','date','status as vessel_status','cargo as sub_commodity','load_port','unload_port','load_country','unload_country','type_of_ships as vessel_size','shipper','receiver')
                    ->where([
                        // 'commodity'=>$data['commodity'], 
                        // 'selected_country'=>$data['country'],
                        'movement'=>$data['trade_type'],
						'sm_cm'=>$check_market
                    ]);
                    if($data['commodity']!="All"){
                        $getData = $getData->where('commodity','=',$data['commodity']);
                    }
                    if($data['country']!="All"){
                        $getData = $getData->where('selected_country','=',$data['country']);
                    }
                    $getData = $getData->orderBy('formated_date','DESC')->get();
        // set data of response
        $response=['vessel_size'=>[],'vessel_status'=>[],'sub_commodity'=>[],'load_port'=>[],'unload_port'=>[],'country'=>[],'company'=>[]];
      ///  $response['maxDate'] = '2021-12-31';
		foreach($getData as $key =>$value){			
			if($key==0)	
				$response['maxDate'][] = $value->date;
            if (!in_array($value->vessel_size,$response['vessel_size'])){
                $response['vessel_size'][]=$value->vessel_size;
            }
            if (!in_array($value->vessel_status,$response['vessel_status'])){
                $response['vessel_status'][]=$value->vessel_status;
            }
            if (!in_array($value->sub_commodity,$response['sub_commodity'])){
                $response['sub_commodity'][]=$value->sub_commodity;
            }
            if (!in_array($value->load_port,$response['load_port'])){
                $response['load_port'][]=$value->load_port;
            }
            if (!in_array($value->unload_port,$response['unload_port'])){
                $response['unload_port'][]=$value->unload_port;
            }
            if($data['trade_type']=="Export"||$data['trade_type']=="Coastal (Load)"){
                if (!in_array($value->unload_country,$response['country'])){
                    $response['country'][]=$value->unload_country;
                }
            }else{
                if (!in_array($value->load_country,$response['country'])){
                    $response['country'][]=$value->load_country;
                }
            }
			if($data['trade_type']=="Export"||$data['trade_type']=="Coastal (Load)"){
                if (!in_array($value->shipper,$response['company'])){
                    $response['company'][]=$value->shipper;
                }
            }else{
                if (!in_array($value->receiver ,$response['company'])){
                    $response['company'][]=$value->receiver;
                }
            }
        }
		 $final_response=array();
        foreach($response as $key1=>$val1){
             sort($val1);
             $final_response[$key1] = $val1;
         }
        if(!empty($final_response)){
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$final_response],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }

    /********
    ** vessel lineup sailed graph
    * ******/
    public function vessel_lineup_sailed_graph_17_09_2022(Request $request)
    {
        DB::enableQueryLog();
        // get requested data from post
        $data = $request->only('date_formate','from_date','to_date','type_wise','commodity', 'country', 'trade_type','vessel_status','sub_commodity','vessel_size','country_wise','load_port','unload_port','company');
        $validator = Validator::make($data, [
            'date_formate' => 'required',
            'from_date' => 'required',
            'to_date' => 'required',
            'commodity' => 'required',
            'country' => 'required',
            'trade_type' => 'required',
        ]);
        //Send failed response if request is not valid
        if ($validator->fails()) {
            return response()->json(['status' => 401,'error' => $validator->messages()]);
        }

        if($data['type_wise']=="sub-commodity"){
            // get data from table
            $getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date','cargo','qty');
        }elseif($data['type_wise']=="vessel-size"){
            // get data from table
            $getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date','type_of_ships','qty');
        }elseif($data['type_wise']=="shipper"){
            // get data from table
            //$getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date','shipper','qty');
            if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                 $getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date','shipper','qty');
            }
            else
                 $getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date','receiver as shipper','qty');
        }elseif($data['type_wise']=="country"){
            // get data from table
            if($data['trade_type']=="Export"||$data['trade_type']=="Coastal (Load)"){
                $getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date','unload_country as country','qty');
            }else{
                $getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date','load_country as country','qty');
            }
        }elseif($data['type_wise']=="port"){
            // get data from table
            if($data['trade_type']=="Export"||$data['trade_type']=="Coastal (Load)"){
                $getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date','load_port as port','qty');
            }else{
                $getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date','unload_port as port','qty');
            }
        }else{
            // get data from table
            $getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date','qty');
        }
        // -------------------------------------------------------
        $getData=$getData->where(['movement'=>$data['trade_type'],'sm_cm'=>'SM']);
        // where condition on the base of value passed
        if($data['date_formate']=="yearly"){
            // need response "2000"
            $getData->whereBetween('f_year', [$data['from_date'], $data['to_date']]);
            $response = $this->sailed_graph_response_child_set($data);
        }elseif($data['date_formate']=="quaterly"){
            // need response "Q1 2000"
            $where = $this->where_quaterly_set($data);
            $getData->whereIn('qtrYear', $where);
            $response = $this->sailed_graph_response_child_set($data);
        }elseif($data['date_formate']=="monthly"){
            // need response "Feb-2022"
            $getData->whereBetween('f_year', [date('Y',strtotime($data['from_date'])), date('Y',strtotime($data['to_date']))]);
            $where =$this->where_monthly_set($data);
            if(!empty($where)){
                $getData->whereIn('f_month', $where);
            }
            $response = $this->sailed_graph_response_child_set($data);
        }elseif($data['date_formate']=="date"){
            // need response "Y-m-d"
            $getData->whereBetween('formated_date', [$data['from_date'], $data['to_date']]);
            $response = $this->sailed_graph_response_child_set($data);
        }

        if($data['commodity']!="" && $data['commodity']!="All"){
            $getData = $getData->where(['commodity'=>$data['commodity']]);
        }
        if($data['country']!="" && $data['country']!="All"){
            $getData = $getData->where(['selected_country'=>$data['country']]);
        }
        if($data['vessel_size']!=""){
            $vessel_size= explode(',',$data['vessel_size']);
            $getData = $getData->whereIn('type_of_ships',$vessel_size);
        }
        if($data['vessel_status']!=""){
            $vessel_status= explode(',',$data['vessel_status']);
            $getData = $getData->whereIn('status',$vessel_status);
        }
        if($data['sub_commodity']!=""){
            $sub_commodity= explode(',',$data['sub_commodity']);
            $getData = $getData->whereIn('cargo',$sub_commodity);
        }
        if($data['country_wise']!=""){
            $country_wise= explode(',',$data['country_wise']);
            if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                $getData = $getData->whereIn('unload_country',$country_wise);
            }else{
                $getData = $getData->whereIn('load_country',$country_wise);
            }
        }
		if($data['company']!=""){
            $company= explode(',',$data['company']);
            if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                $getData = $getData->whereIn('shipper',$company);
            }else{
                $getData = $getData->whereIn('receiver',$company);
            }
        }
        if($data['load_port']!=""){
            $load_port= explode(',',$data['load_port']);
            $getData = $getData->whereIn('load_port',$load_port);
            
        }
        if($data['unload_port']!=""){
            $unload_port= explode(',',$data['unload_port']);
            $getData = $getData->whereIn('unload_port',$unload_port);
        }

        $getData = $getData->get();
        // dd(DB::getQueryLog());
        $response = $this->set_sailed_graph_data($data,$getData,$response);
        if($data['type_wise']!=""){
            $newdata=[];
            $c=0;
            foreach ($response as $key => $value) {
                $newdata[$c]['name']=$key;
                $newdata[$c]['data']=isset($value['data'])?array_values($value['data']):'';
                $c++;
            }
            $response=$newdata;
            
        }
        if(!empty($response)){
            $datas['graph_type']=  empty($data['type_wise']) ? 'Parent graph' : 'child graph ('.$data['type_wise'].' wise)';
            $datas['graph_points']= $response;
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$datas],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }
    /********
    ** vessel lineup sailed graph old
    * ******/
    public function vessel_lineup_sailed_graph_old(Request $request)
    {
        DB::enableQueryLog();
        // get requested data from post
        $data = $request->only('date_formate','from_date','to_date','type_wise','commodity', 'country', 'trade_type','vessel_status','sub_commodity','vessel_size','country_wise','load_port','unload_port');
        $validator = Validator::make($data, [
            'date_formate' => 'required',
            'from_date' => 'required',
            'to_date' => 'required',
            'commodity' => 'required',
            'country' => 'required',
            'trade_type' => 'required',
        ]);
        //Send failed response if request is not valid
        if ($validator->fails()) {
            return response()->json(['status' => 401,'error' => $validator->messages()]);
        }

        if($data['date_formate']!=""){
            $validator = Validator::make($data, [
                'date_formate' => 'required',
                'from_date' => 'required',
                'to_date' => 'required',
            ]);
            //Send failed response if request is not valid
            if ($validator->fails()) {
                return response()->json(['status' => 401,'error' => $validator->messages()]);
            }
        }
        if($data['type_wise']=="sub-commodity"){
            // get data from table
            $getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date','cargo');
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['date_formate']=="yearly"){
                // need response "2000"
                $getData->whereBetween('f_year', [$data['from_date'], $data['to_date']]);
                $response = $this->sailed_graph_response_child_set($data);
            }elseif($data['date_formate']=="quaterly"){
                // need response "Q1 2000"
                $where = $this->where_quaterly_set($data);
                $getData->whereIn('qtrYear', $where);
                $response = $this->sailed_graph_response_child_set($data);
            }elseif($data['date_formate']=="monthly"){
                // need response "Feb-2022"
                $getData->whereBetween('f_year', [date('Y',strtotime($data['from_date'])), date('Y',strtotime($data['to_date']))]);
                $where =$this->where_monthly_set($data);
                if(!empty($where)){
                    $getData->whereIn('f_month', $where);
                }
                $response = $this->sailed_graph_response_child_set($data);
            }elseif($data['date_formate']=="date"){
                // need response "Y-m-d"
                $getData->whereBetween('formated_date', [$data['from_date'], $data['to_date']]);
                $response = $this->sailed_graph_response_child_set($data);
            }
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $getData = $getData->where(['status'=>$data['vessel_status']]);
            }
            if($data['sub_commodity']!=""){
                $getData = $getData->where(['cargo'=>$data['sub_commodity']]);
            }
            if($data['country_wise']!=""){
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->where(['unload_country'=>$data['country_wise']]);
                }else{
                    $getData = $getData->where(['load_country'=>$data['country_wise']]);
                }
            }
            if($data['load_port']!=""){
                $getData = $getData->where(['load_port'=>$data['load_port']]);
            }
            if($data['unload_port']!=""){
                $getData = $getData->where(['unload_port'=>$data['unload_port']]);
            }
            $getData = $getData->get();
            // dd(DB::getQueryLog());
            // set response arr of 
            foreach($response as $response_k =>$v){
                foreach($getData as $val){
                    $response[$response_k][$val->cargo]=0;
                }
            }
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $value){
                    $response[$value->f_year][$value->cargo]++;
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $value){
                    $response[$value->qtrYear][$value->cargo]++;
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $value){
                    $m = date("M",strtotime($value->f_month));
                    $response[$m.'-'.$value->f_year][$value->cargo]++;
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $value){
                    $response[$value->formated_date][$value->cargo]++;
                }
            }
        }elseif($data['type_wise']=="shipper"){
            // get data from table
            $getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date','shipper');
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['date_formate']=="yearly"){
                // need response "2000"
                $getData->whereBetween('f_year', [$data['from_date'], $data['to_date']]);
                $response = $this->sailed_graph_response_child_set($data);
            }elseif($data['date_formate']=="quaterly"){
                // need response "Q1 2000"
                $where = $this->where_quaterly_set($data);
                $getData->whereIn('qtrYear', $where);
                $response = $this->sailed_graph_response_child_set($data);
            }elseif($data['date_formate']=="monthly"){
                // need response "Feb-2022"
                $getData->whereBetween('f_year', [date('Y',strtotime($data['from_date'])), date('Y',strtotime($data['to_date']))]);
                $where =$this->where_monthly_set($data);
                if(!empty($where)){
                    $getData->whereIn('f_month', $where);
                }
                $response = $this->sailed_graph_response_child_set($data);
            }elseif($data['date_formate']=="date"){
                // need response "Y-m-d"
                $getData->whereBetween('formated_date', [$data['from_date'], $data['to_date']]);
                $response = $this->sailed_graph_response_child_set($data);
            }
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $getData = $getData->where(['status'=>$data['vessel_status']]);
            }
            if($data['sub_commodity']!=""){
                $getData = $getData->where(['cargo'=>$data['sub_commodity']]);
            }
            if($data['country_wise']!=""){
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->where(['unload_country'=>$data['country_wise']]);
                }else{
                    $getData = $getData->where(['load_country'=>$data['country_wise']]);
                }
            }
            if($data['load_port']!=""){
                $getData = $getData->where(['load_port'=>$data['load_port']]);
            }
            if($data['unload_port']!=""){
                $getData = $getData->where(['unload_port'=>$data['unload_port']]);
            }
            $getData = $getData->get();
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $val){
                    $response[$response_k][$val->shipper]=0;
                }
            }
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $value){
                    $response[$value->f_year][$value->shipper]++;
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $value){
                    $response[$value->qtrYear][$value->shipper]++;
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $value){
                    $m = date("M",strtotime($value->f_month));
                    $response[$m.'-'.$value->f_year][$value->shipper]++;
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $value){
                    $response[$value->formated_date][$value->shipper]++;
                }
            }
        }elseif($data['type_wise']=="country"){
            // get data from table
            if($data['trade_type']=="Export"||$data['trade_type']=="Coastal (Load)"){
                $getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date','unload_country as country');
                // $getData = Logisticdata::select('id','status','unload_country as country');
            }else{
                $getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date','load_country as country');
                // $getData = Logisticdata::select('id','status','load_country as country');
            }
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['date_formate']=="yearly"){
                // need response "2000"
                $getData->whereBetween('f_year', [$data['from_date'], $data['to_date']]);
                $response = $this->sailed_graph_response_child_set($data);
            }elseif($data['date_formate']=="quaterly"){
                // need response "Q1 2000"
                $where = $this->where_quaterly_set($data);
                $getData->whereIn('qtrYear', $where);
                $response = $this->sailed_graph_response_child_set($data);
            }elseif($data['date_formate']=="monthly"){
                // need response "Feb-2022"
                $getData->whereBetween('f_year', [date('Y',strtotime($data['from_date'])), date('Y',strtotime($data['to_date']))]);
                $where =$this->where_monthly_set($data);
                if(!empty($where)){
                    $getData->whereIn('f_month', $where);
                }
                $response = $this->sailed_graph_response_child_set($data);
            }elseif($data['date_formate']=="date"){
                // need response "Y-m-d"
                $getData->whereBetween('formated_date', [$data['from_date'], $data['to_date']]);
                $response = $this->sailed_graph_response_child_set($data);
            }
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $getData = $getData->where(['status'=>$data['vessel_status']]);
            }
            if($data['sub_commodity']!=""){
                $getData = $getData->where(['cargo'=>$data['sub_commodity']]);
            }
            if($data['country_wise']!=""){
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->where(['unload_country'=>$data['country_wise']]);
                }else{
                    $getData = $getData->where(['load_country'=>$data['country_wise']]);
                }
            }
            if($data['load_port']!=""){
                $getData = $getData->where(['load_port'=>$data['load_port']]);
            }
            if($data['unload_port']!=""){
                $getData = $getData->where(['unload_port'=>$data['unload_port']]);
            }
            $getData = $getData->get();
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $val){
                    $response[$response_k][$val->country]=0;
                }
            }
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $value){
                    $response[$value->f_year][$value->country]++;
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $value){
                    $response[$value->qtrYear][$value->country]++;
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $value){
                    $m = date("M",strtotime($value->f_month));
                    $response[$m.'-'.$value->f_year][$value->country]++;
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $value){
                    $response[$value->formated_date][$value->country]++;
                }
            }
        }elseif($data['type_wise']=="port"){
            // get data from table
            if($data['trade_type']=="Export"||$data['trade_type']=="Coastal (Load)"){
                // $getData = Logisticdata::select('id','status','load_port as port');
                $getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date','load_port as port');
            }else{
                // $getData = Logisticdata::select('id','status','unload_port as port');
                $getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date','unload_port as port');
            }
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['date_formate']=="yearly"){
                // need response "2000"
                $getData->whereBetween('f_year', [$data['from_date'], $data['to_date']]);
                $response = $this->sailed_graph_response_child_set($data);
            }elseif($data['date_formate']=="quaterly"){
                // need response "Q1 2000"
                $where = $this->where_quaterly_set($data);
                $getData->whereIn('qtrYear', $where);
                $response = $this->sailed_graph_response_child_set($data);
            }elseif($data['date_formate']=="monthly"){
                // need response "Feb-2022"
                $getData->whereBetween('f_year', [date('Y',strtotime($data['from_date'])), date('Y',strtotime($data['to_date']))]);
                $where =$this->where_monthly_set($data);
                if(!empty($where)){
                    $getData->whereIn('f_month', $where);
                }
                $response = $this->sailed_graph_response_child_set($data);
            }elseif($data['date_formate']=="date"){
                // need response "Y-m-d"
                $getData->whereBetween('formated_date', [$data['from_date'], $data['to_date']]);
                $response = $this->sailed_graph_response_child_set($data);
            }
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $getData = $getData->where(['status'=>$data['vessel_status']]);
            }
            if($data['sub_commodity']!=""){
                $getData = $getData->where(['cargo'=>$data['sub_commodity']]);
            }
            if($data['country_wise']!=""){
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->where(['unload_country'=>$data['country_wise']]);
                }else{
                    $getData = $getData->where(['load_country'=>$data['country_wise']]);
                }
            }
            if($data['load_port']!=""){
                $getData = $getData->where(['load_port'=>$data['load_port']]);
            }
            if($data['unload_port']!=""){
                $getData = $getData->where(['unload_port'=>$data['unload_port']]);
            }
            $getData = $getData->get();
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $val){
                    $response[$response_k][$val->port]=0;
                }
            }
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $value){
                    $response[$value->f_year][$value->port]++;
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $value){
                    $response[$value->qtrYear][$value->port]++;
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $value){
                    $m = date("M",strtotime($value->f_month));
                    $response[$m.'-'.$value->f_year][$value->port]++;
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $value){
                    $response[$value->formated_date][$value->port]++;
                }
            }
        }else{
            
            $response=[];
            // get data from table
            $getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date');
            $getData=$getData->where(['movement'=>$data['trade_type']]);
            // where condition on the base of value passed
            if($data['date_formate']=="yearly"){
                // need response "2000"
                $getData->whereBetween('f_year', [$data['from_date'], $data['to_date']]);
                $response = $this->sailed_graph_response_parent_set($data);
            }elseif($data['date_formate']=="quaterly"){
                // need response "Q1 2000"
                $where = $this->where_quaterly_set($data);
                $getData->whereIn('qtrYear', $where);
                $response = $this->sailed_graph_response_parent_set($data);
            }elseif($data['date_formate']=="monthly"){
                // need response "Feb-2022"
                $getData->whereBetween('f_year', [date('Y',strtotime($data['from_date'])), date('Y',strtotime($data['to_date']))]);
                $where =$this->where_monthly_set($data);
                if(!empty($where)){
                    $getData->whereIn('f_month', $where);
                }
                $response = $this->sailed_graph_response_parent_set($data);
            }elseif($data['date_formate']=="date"){
                // need response "Y-m-d"
                $getData->whereBetween('formated_date', [$data['from_date'], $data['to_date']]);
                $response = $this->sailed_graph_response_parent_set($data);
            }
            if($data['commodity']!="" && $data['commodity']!="All"){
                $getData = $getData->where(['commodity'=>$data['commodity']]);
            }
            if($data['country']!="" && $data['country']!="All"){
                $getData = $getData->where(['selected_country'=>$data['country']]);
            }
            if($data['vessel_status']!=""){
                $getData = $getData->where(['status'=>$data['vessel_status']]);
            }
            if($data['sub_commodity']!=""){
                $getData = $getData->where(['cargo'=>$data['sub_commodity']]);
            }
            if($data['country_wise']!=""){
                if($data['trade_type']=="Export" || $data['trade_type']=="Coastal (Load)"){
                    $getData = $getData->where(['unload_country'=>$data['country_wise']]);
                }else{
                    $getData = $getData->where(['load_country'=>$data['country_wise']]);
                }
            }
            if($data['load_port']!=""){
                $getData = $getData->where(['load_port'=>$data['load_port']]);
            }
            if($data['unload_port']!=""){
                $getData = $getData->where(['unload_port'=>$data['unload_port']]);
            }
            $getData = $getData->get();
            // dd(DB::getQueryLog());
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $value){
                    $response[$value->f_year]++;
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $value){
                    $response[$value->qtrYear]++;
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $value){
                    $m = date("M",strtotime($value->f_month));
                    $response[$m.'-'.$value->f_year]++;
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $value){
                    $response[$value->formated_date]++;
                }
            }
        }
        if(!empty($response)){
            $datas['data']['graph_type']=  empty($data['type_wise']) ? 'Parent graph' : 'child graph ('.$data['type_wise'].' wise)';
            $datas['data']['graph_points']= $response;
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$datas],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }
    /********
    ** railway movement
    * ******/
    public function railway_movement()
    {
        // get data from table
        $commodity = Railwayprogramme::select('commodity','selected_country')->groupBy('commodity')->get();
        $data=[];
        $all=[];
        
        //this is for to set filter value of item
        foreach($commodity as $key => $value){
            if($value->commodity=="ORES" || $value->commodity=="RMSP"){
                $data[$key]['Iron Ore'][] = $value->selected_country;
            }else{
                $data[$key][$value->commodity][] = $value->selected_country;
            }
             $all['All'][]=$value->selected_country;
        }
		
        //this is for to set all filter value
        ///if(count($commodity)>1){
        if(count($data)>2){
            $all['All'] = array_unique($all['All']);
            //$all['All'][]='India';
            //all filter value end here
            $data[]=$all;
        }
        if(!empty($data)){
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$data],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }
    
    /********
    ** railway sub Movement
    * ******/
    public function railway_submovement_1_apr_2022(Request $request)
    {
        // requested data from post
        $data = $request->only('commodity', 'country');
        $validator = Validator::make($data, [
            'commodity' => 'required',
            'country' => 'required',
        ]);

        //Send failed response if request is not valid
        if ($validator->fails()) {
            return response()->json(['success' => false,'error' => $validator->messages()]);
        }
        // get data from table on behalf of filter
        $data['commodity']=($data['commodity']=="Iron Ore")?"ORES":$data['commodity'];
        $getData = Railwayprogramme::select('division','consignor','consignee','load_stn','unload_stn');
         
        if($data['commodity']!="All"){
            //$getData =$getData->where('commodity','=',$data['commodity']);
            if($data['commodity']=="ORES")
                $getData =$getData->whereIn('commodity',array("ORES,RMSP"))->where('selected_country','=',$data['country']); //YKV
            else
                $getData =$getData->where('commodity','=',$data['commodity'])->where('selected_country','=',$data['country']); //YKV
        }
        
        $getData =$getData->with(['divisions','consignors','consignees','load_stns','unload_stns'])->get();
        // set data of response
        $response=[];
        foreach($getData as $key => $value ){
            $response['Division'][$key]['name']=$value->divisions->f_full_form;
            $response['Division'][$key]['key']=$value->divisions->f_abbr;
            $response['Consignor'][$key]['name']=$value->consignors->f_full_form;
            $response['Consignor'][$key]['key']=$value->consignors->f_abbr;
            $response['Consignee'][$key]['name']=$value->consignees->f_full_form;
            $response['Consignee'][$key]['key']=$value->consignees->f_abbr;
            $response['Load-Siding'][$key]['name']=$value->load_stns->f_full_form;
            $response['Load-Siding'][$key]['key']=$value->load_stns->f_abbr;
            $response['Unload-Siding'][$key]['name']=$value->unload_stns->f_full_form;
            $response['Unload-Siding'][$key]['key']=$value->unload_stns->f_abbr;
        }
        
        $newresponse=[];
        foreach ($response as $key=> $value) {
            $newresponse[$key] = array_values(array_unique($value,SORT_REGULAR));
        }
        if(!empty($newresponse)){
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$newresponse],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }    
    
    
    /********
    ** railway sub Movement
    * ******/
	public function railway_submovement(Request $request)
    {
        // requested data from post
        $data = $request->only('commodity', 'country');
        $validator = Validator::make($data, [
            'commodity' => 'required',
            'country' => 'required',
        ]);

        //Send failed response if request is not valid
        if ($validator->fails()) {
            return response()->json(['success' => false,'error' => $validator->messages()]);
        }
        
        $redisKeyName = "rly_submovement_graph_" . str_replace(" ", "_", $data['country']) . "_" . str_replace(" ", "_", $data['commodity']);
        include_once "/home/steelmin/public_html/redisFn.php";
        if(isKeyExistsInRedis($redisKeyName)){
        	$result = json_decode(getKeyValueFromRedis($redisKeyName), true);
        	return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$result],$this->successStatus); 
        } 
         
        // get data from table on behalf of filter
		///DB::enableQueryLog();
try{       
	   $data['commodity']=($data['commodity']=="Iron Ore")?"ORES":$data['commodity'];
        $getData = Railwayprogramme::select('u1.f_full_form as division','u2.f_full_form as consignor','u3.f_full_form as consignee','u4.f_full_form as load_stn','u5.f_full_form as unload_stn','u1.f_abbr as u1key','u2.f_abbr as u2key','u3.f_abbr as u3key','u4.f_abbr as u4key','u5.f_abbr as u5key',DB::raw('MAX(f_date) as maxDate'))
				->leftJoin('tbl_abbreviation as u1','u1.f_abbr','=','tbl_railway_programme.division')
				->leftJoin('tbl_abbreviation as u2','u2.f_abbr','=','tbl_railway_programme.consignor')
				->leftJoin('tbl_abbreviation as u3','u3.f_abbr','=','tbl_railway_programme.consignee')
				->leftJoin('tbl_abbreviation as u4','u4.f_abbr','=','tbl_railway_programme.load_stn')
				->leftJoin('tbl_abbreviation as u5','u5.f_abbr','=','tbl_railway_programme.unload_stn');
         
        if($data['commodity']!="All"){
            //$getData =$getData->where('commodity','=',$data['commodity']);
           if($data['commodity']=="ORES")
                $getData =$getData->whereIn('commodity',array("RMSP","ORES"))->where('selected_country','=',$data['country']); //YKV
            else
                $getData =$getData->where('commodity','=',$data['commodity'])->where('selected_country','=',$data['country']); //YKV
        }
        
        ////$getData =$getData->with(['divisions','consignors','consignees','load_stns','unload_stns'])->get();
        // set data of response
       // $getData =$getData->groupBy('division','consignor','consignee','load_stn','unload_stn')->orderByRaw('division ASC,consignor ASC,consignee ASC,load_stn ASC,unload_stn ASC')->get();
        $getData =$getData->groupBy('division','consignor','consignee','load_stn','unload_stn')->get();
		///dd(DB::getQueryLog());
		} catch(\Illuminate\Database\QueryException $ex){ 
  dd($ex->getMessage()); 
  // Note any method of class PDOException can be called on $ex.
}
		
		//print_r($getData);
		 $response=[];$max_date=[];
        foreach($getData as $key => $value ){
			////
            $response['Division'][$key]['name']=$value->division;
            $response['Division'][$key]['key']=$value->u1key;
            $response['Consignor'][$key]['name']=$value->consignor;
            $response['Consignor'][$key]['key']=$value->u2key;
            $response['Consignee'][$key]['name']=$value->consignee;
            $response['Consignee'][$key]['key']=$value->u3key;
            $response['Load-Siding'][$key]['name']=$value->load_stn;
            $response['Load-Siding'][$key]['key']=$value->u4key;
            $response['Unload-Siding'][$key]['name']=$value->unload_stn;
            $response['Unload-Siding'][$key]['key']=$value->u5key; 
			$max_date[]=$value->maxDate;
			///
        }
        /////
        $newresponse=[];
        foreach ($response as $key=> $value) {            
            $newresponse[$key] = array_values(array_unique($value,SORT_REGULAR));
            //sort the data by name
            $keys_sort = array_column($newresponse[$key], 'name');
            array_multisort($keys_sort, SORT_ASC, $newresponse[$key]);
        }
		$newresponse['maxDate']=max($max_date);
		
        if(!empty($response)){
            setInRedis($redisKeyName, json_encode($newresponse), 0);
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$newresponse],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
        
        return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
    }
	//function take much execution time on below code
    public function railway_submovement_vendor_05_04_2022(Request $request)
    {
        // requested data from post
        $data = $request->only('commodity', 'country');
        $validator = Validator::make($data, [
            'commodity' => 'required',
            'country' => 'required',
        ]);

        //Send failed response if request is not valid
        if ($validator->fails()) {
            return response()->json(['success' => false,'error' => $validator->messages()]);
        }
        
        $redisKeyName = "rly_submovement_graph_" . str_replace(" ", "_", $data['country']) . "_" . str_replace(" ", "_", $data['commodity']);
        include_once "/home/steelmin/public_html/redisFn.php";
        if(isKeyExistsInRedis($redisKeyName)){
        	$result = json_decode(getKeyValueFromRedis($redisKeyName), true);
        	return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$result],$this->successStatus); 
        }        
        
        // get data from table on behalf of filter
        $data['commodity']=($data['commodity']=="Iron Ore")?"ORES":$data['commodity'];
        $getData = Railwayprogramme::select('division','consignor','consignee','load_stn','unload_stn');
         
        if($data['commodity']!="All"){
            //$getData =$getData->where('commodity','=',$data['commodity']);
            $getData =$getData->where('commodity','=',$data['commodity'])->where('selected_country','=',$data['country']); //YKV
        }
        
        $getData =$getData->with(['divisions','consignors','consignees','load_stns','unload_stns'])->get();
        // set data of response
        $response=[];
        foreach($getData as $key => $value ){
            $response['Division'][$key]['name']=$value->divisions->f_full_form;
            $response['Division'][$key]['key']=$value->divisions->f_abbr;
            $response['Consignor'][$key]['name']=$value->consignors->f_full_form;
            $response['Consignor'][$key]['key']=$value->consignors->f_abbr;
            $response['Consignee'][$key]['name']=$value->consignees->f_full_form;
            $response['Consignee'][$key]['key']=$value->consignees->f_abbr;
            $response['Load-Siding'][$key]['name']=$value->load_stns->f_full_form;
            $response['Load-Siding'][$key]['key']=$value->load_stns->f_abbr;
            $response['Unload-Siding'][$key]['name']=$value->unload_stns->f_full_form;
            $response['Unload-Siding'][$key]['key']=$value->unload_stns->f_abbr;
        }
        
        $newresponse=[];
        foreach ($response as $key=> $value) {
            $newresponse[$key] = array_values(array_unique($value,SORT_REGULAR));
        }
        if(!empty($newresponse)){
            setInRedis($redisKeyName, json_encode($newresponse), 0);
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$newresponse],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }
    /********
    ** vessel lineup sailed graph
    * ******/
	///write new code for below function
    public function railway_movement_graph_07042022(Request $request)
    {
        //DB::enableQueryLog();
        
       
        
        // get requested data from post
        $data = $request->only('date_formate','from_date','to_date','type_wise','commodity', 'country','division','consignor','consignee','load_siding','unload_siding');
        $validator = Validator::make($data, [
            'date_formate' => 'required',
            'from_date' => 'required',
            'to_date' => 'required',
            'commodity' => 'required',
            'country' => 'required',
        ]);
        //Send failed response if request is not valid
        if ($validator->fails()) {
            return response()->json(['status' => 401,'error' => $validator->messages()]);
        }

        if ($data['commodity']=='Iron Ore') {
            $data['commodity']='ORES';
        }
       
        //type_wise =(division,load-station,unload-station,consignor,consignee)
        //DB::enableQueryLog();
        $getData = Railwayprogramme::select('*');
        /*if($data['type_wise']=="division"){
            // get data from table
            $getData = Railwayprogramme::select('tbl_railway_programme.*');
            // $getData = Railwayprogramme::select('division','consignor','consignee','load_stn','unload_stn');
        }elseif($data['type_wise']=="load-station"){
            // get data from table
            $getData = Railwayprogramme::select('tbl_railway_programme.*');
        }elseif($data['type_wise']=="unload-station"){
            // get data from table
            $getData = Railwayprogramme::select('tbl_railway_programme.*');
        }elseif($data['type_wise']=="consignor"){
            // get data from table
            $getData = Railwayprogramme::select('tbl_railway_programme.*');
        }elseif($data['type_wise']=="consignee"){
            // get data from table
            $getData = Railwayprogramme::select('*');
        }else{
            $getData = Railwayprogramme::select('*');
        }*/
        // -------------------------------------------------------
        // where condition on the base of value passed
        if($data['division']!=""){
            $division= explode(',',$data['division']);
            $getData = $getData->whereIn('division',$division);
        }
        if($data['consignor']!=""){
            $consignor= explode(',',$data['consignor']);
            $getData = $getData->whereIn('consignor',$consignor);
        }
        if($data['consignee']!=""){
            $consignee= explode(',',$data['consignee']);
            $getData = $getData->whereIn('consignee',$consignee);
        }
        if($data['load_siding']!=""){
            $load_siding= explode(',',$data['load_siding']);
            $getData = $getData->whereIn('load_stn',$load_siding);
        }
        if($data['unload_siding']!=""){
            $unload_siding= explode(',',$data['unload_siding']);
            $getData = $getData->whereIn('unload_stn',$unload_siding);
        }

        if($data['date_formate']=="yearly"){
            // need response "2000"
            $getData->whereBetween('f_year', [$data['from_date'], $data['to_date']]);
            $response = $this->sailed_graph_response_child_set($data);
        }elseif($data['date_formate']=="quaterly"){
            // need response "Q1 2000"
            $where = $this->where_quaterly_set($data);
            // print_r($where);die;
            $getData->whereIn('qtrYear', $where);
            $response = $this->sailed_graph_response_child_set($data);
        }elseif($data['date_formate']=="monthly"){
            // need response "Feb-2022"
            $getData->whereBetween('f_year', [date('Y',strtotime($data['from_date'])), date('Y',strtotime($data['to_date']))]);
            $where =$this->where_monthly_set($data);
            if(!empty($where)){
                $getData->whereIn('f_month', $where);
            }
            $response = $this->sailed_graph_response_child_set($data);
        }elseif($data['date_formate']=="date"){
            // need response "Y-m-d"
            $getData->whereBetween('f_date', [$data['from_date'], $data['to_date']]);
            $response = $this->sailed_graph_response_child_set($data);
        }

        if($data['commodity']!="" && $data['commodity']!="All"){
            $getData = $getData->where(['commodity'=>$data['commodity']]);
        }
        if($data['country']!="" && $data['country']!="All"){
            $getData = $getData->where(['selected_country'=>$data['country']]);
        }

        $getData = $getData->get();
         //dd(DB::getQueryLog());
         
         
         
         
        $response = $this->set_railway_graph_data($data,$getData,$response);
        
        if($data['type_wise']!=""){
            $newdata=[];
            $c=0;
            foreach ($response as $key => $value) {
                $newdata[$c]['name']=$key;
                $newdata[$c]['data']=array_values($value['data']);
                $c++;
            }
            $response=$newdata;
        }
        if(!empty($response)){
            $datas['graph_type']=  empty($data['type_wise']) ? 'Parent graph' : $data['type_wise'].' wise';
            $datas['graph_points']= $response;
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$datas],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }

    /********
    **  this function is for to create parent graph response array* ******/
    function sailed_graph_response_parent_set($data){
        $response=[];
        if($data['date_formate']=="yearly"){
            // need response "2000"
            $year_diff= $data['to_date']-$data['from_date'];
            for($i=0;$i<= $year_diff;$i++){
                $year_val= ($data['from_date'] +$i);
                $response[$year_val]=0;
            }
        }elseif($data['date_formate']=="quaterly"){
            // need response "Q1 2000"
            $fromdate =explode(' ',$data['from_date']);
            $todate =explode(' ',$data['to_date']);
            $quater_diff= $todate[1]-$fromdate[1];
            if($quater_diff>0){
                for($i=0;$i<= $quater_diff;$i++){
                    $quater_val = $fromdate[1] + $i;
                    if($quater_val == $fromdate[1]){
                        $fromqtrdate = explode('Q',$fromdate[0]);
                        // $toqtrdate = explode('Q',$todate[0]);
                        $qtr_diff= 4-$fromqtrdate[1];
                        for($io=0;$io<= $qtr_diff;$io++){
                            $qtr_val = $fromqtrdate[1] + $io;
                            $response['Q'.$qtr_val.' '.$quater_val]=0;
                        } 
                    }elseif($quater_val == $todate[1]){
                        $fromqtrdates = 1;
                        $toqtrdates = explode('Q',$todate[0]);
                        $qtr_diffs= $toqtrdates[1] - $fromqtrdates;
                        for($ic=0;$ic<= $qtr_diffs;$ic++){
                            $qtr_vals = $fromqtrdates + $ic;
                            $response['Q'.$qtr_vals.' '.$quater_val]=0;
                        } 
                    }else{
                        $response['Q1 '.$quater_val]=0;
                        $response['Q2 '.$quater_val]=0;
                        $response['Q3 '.$quater_val]=0;
                        $response['Q4 '.$quater_val]=0;
                    }
                }
            }else{
                $fromqtrdate = explode('Q',$fromdate[0]);
                $toqtrdate = explode('Q',$todate[0]);
                $qtr_diff= $toqtrdate[1]-$fromqtrdate[1];
                for($i=0;$i<= $qtr_diff;$i++){
                    $qtr_val = $fromqtrdate[1] + $i;
                    $response['Q'.$qtr_val.' '.$fromdate[1]]=0;
                }
            }
        }elseif($data['date_formate']=="monthly"){
            // need response "Feb-2022"
            $datetime1 = date_create($data['from_date']);
            $datetime2 = date_create($data['to_date']);
            $interval = date_diff($datetime1, $datetime2);
            $months = (($interval->format('%y')*12)+($interval->format('%m')));
            // $interval->format('%a day %m month %y year');
            if($months>0){
                for($i=0;$i<= $months;$i++){
                    $mon = date("M-Y", strtotime($data['from_date']." + $i months"));
                    $response[$mon]=0;
                }
            }
        }elseif($data['date_formate']=="date"){
            // need response "Y-m-d"
            $date1_ts = strtotime($data['from_date']);
            $date2_ts = strtotime($data['to_date']);
            $diff = $date2_ts - $date1_ts;
            for($i=0;$i<=round($diff / 86400);$i++){
                $dat_val= date('Y-m-d', strtotime($data['from_date'] . ' +'.$i.' day'));
                $response[$dat_val]=0;
            }
        }
        return $response;
    }


    /********
    **   this function is for to create child graph response array* ******/
    function sailed_graph_response_child_set($data){
        $response=[];
        if($data['date_formate']=="yearly"){
            // need response "2000"
            $year_diff= $data['to_date']-$data['from_date'];
            for($i=0;$i<= $year_diff;$i++){
                $year_val= ($data['from_date'] +$i);
                $response[$year_val]=[];
            }
        }elseif($data['date_formate']=="quaterly"){
            // need response "Q1 2000"
            $fromdate =explode(' ',$data['from_date']);
            $todate =explode(' ',$data['to_date']);
            $quater_diff= $todate[1]-$fromdate[1];
            if($quater_diff>0){
                for($i=0;$i<= $quater_diff;$i++){
                    $quater_val = $fromdate[1] + $i;
                    if($quater_val == $fromdate[1]){
                        $fromqtrdate = explode('Q',$fromdate[0]);
                        // $toqtrdate = explode('Q',$todate[0]);
                        $qtr_diff= 4-$fromqtrdate[1];
                        for($io=0;$io<= $qtr_diff;$io++){
                            $qtr_val = $fromqtrdate[1] + $io;
                            $response['Q'.$qtr_val.' '.$quater_val]=[];
                        } 
                    }elseif($quater_val == $todate[1]){
                        $fromqtrdates = 1;
                        $toqtrdates = explode('Q',$todate[0]);
                        $qtr_diffs= $toqtrdates[1] - $fromqtrdates;
                        for($ic=0;$ic<= $qtr_diffs;$ic++){
                            $qtr_vals = $fromqtrdates + $ic;
                            $response['Q'.$qtr_vals.' '.$quater_val]=[];
                        } 
                    }else{
                        $response['Q1 '.$quater_val]=[];
                        $response['Q2 '.$quater_val]=[];
                        $response['Q3 '.$quater_val]=[];
                        $response['Q4 '.$quater_val]=[];
                    }
                }
            }else{
                $fromqtrdate = explode('Q',$fromdate[0]);
                $toqtrdate = explode('Q',$todate[0]);
                $qtr_diff= $toqtrdate[1]-$fromqtrdate[1];
                for($i=0;$i<= $qtr_diff;$i++){
                    $qtr_val = $fromqtrdate[1] + $i;
                    $response['Q'.$qtr_val.' '.$fromdate[1]]=[];
                }
            }
        }elseif($data['date_formate']=="monthly"){
            // need response "Feb-2022"
            $datetime1 = date_create($data['from_date']);
            $datetime2 = date_create($data['to_date']);
            $interval = date_diff($datetime1, $datetime2);
            $months = (($interval->format('%y')*12)+($interval->format('%m')));
            // $interval->format('%a day %m month %y year');
            if($months>0){
                for($i=0;$i<= $months;$i++){
                    $mon = date("M-Y", strtotime($data['from_date']." + $i months"));
                    $response[$mon]=[];
                }
            }
        }elseif($data['date_formate']=="date"){
            // need response "Y-m-d"
            $date1_ts = strtotime($data['from_date']);
            $date2_ts = strtotime($data['to_date']);
            $diff = $date2_ts - $date1_ts;
            for($i=0;$i<=round($diff / 86400);$i++){
                $dat_val= date('Y-m-d', strtotime($data['from_date'] . ' +'.$i.' day'));
                $response[$dat_val]=[];
            }
        }
        return $response;
    }


    /********
    **   this function is for to create quaterly response array * ******/
    function where_quaterly_set($data){
        $where=[];
        $from_date = explode(' ',$data['from_date']);
        $to_date = explode(' ',$data['to_date']);
        $qtr_year_diff = $to_date[1]-$from_date[1];
        if($qtr_year_diff>0){
            for ($ic=0; $ic <= $qtr_year_diff ; $ic++) { 
                    $new_qtr_year= $from_date[1]+$ic;
                if($new_qtr_year==$from_date[1]){
                    $from_num =explode('Q',$from_date[0]);
                    $qtr_num_diff= 4-$from_num[1];
                    for($i=0;$i<=$qtr_num_diff;$i++){
                        $n= $i+$from_num[1];
                        $where[]='Q'.$n.' '.$new_qtr_year;
                    }
                }elseif($new_qtr_year==$to_date[1]){
                    $from_num =1;
                    $to_num =explode('Q',$to_date[0]);
                    $qtr_num_diff= $to_num[1]-$from_num;
                    for($i=0;$i<=$qtr_num_diff;$i++){
                        $n= $i+$from_num;
                        $where[]='Q'.$n.' '.$new_qtr_year;
                    }
                }else{
                    for($i=0;$i<=3;$i++){
                        $n= $i+1;
                        $where[]='Q'.$n.' '.$new_qtr_year;
                    }
                }
            }
        }else{
            $from_num =explode('Q',$from_date[0]);
            $to_num =explode('Q',$to_date[0]);
            $qtr_num_diff=$to_num[1]-$from_num[1];
            for($i=0;$i<=$qtr_num_diff;$i++){
                $n= $i+$from_num[1];
                $where[]='Q'.$n.' '.$from_date[1];
            }
        }
        return $where;
    }


    /********
    **   this function is for to create monthly response array * ******/
    function where_monthly_set($data){
        $datetime1 = date_create($data['from_date']);
        $datetime2 = date_create($data['to_date']);
        $interval = date_diff($datetime1, $datetime2);
        $months = (($interval->format('%y')*12)+($interval->format('%m')));
        $where=[];
        if($months < 12){
            
            for($i=0;$i<= $months;$i++){
                $mon = date("F", strtotime($data['from_date']." + $i months"));
                $where[]=$mon;
            }
        }
        return $where;
    }


    /********
    **   This function is for to set data in array* ******/
    function set_sailed_graph_data($data,$getData,$response){
        if($data['type_wise']=="sub-commodity"){
            // set response arr of 
            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($val->cargo==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$val->cargo;
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$val->cargo;
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            // print_r($response);die;
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_year == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->cargo){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->qtrYear == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->cargo){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $key => $value){
                    $m = date("M",strtotime($value->f_month)).'-'.$value->f_year;
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($m == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->cargo){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
             }elseif($data['date_formate']=="date"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->formated_date == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->cargo){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }
        }elseif($data['type_wise']=="vessel-size"){
            // set response arr of 
            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($val->type_of_ships==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$val->type_of_ships;
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$val->type_of_ships;
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            // print_r($response);die;
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_year == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->type_of_ships){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->qtrYear == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->type_of_ships){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $key => $value){
                    $m = date("M",strtotime($value->f_month)).'-'.$value->f_year;
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($m == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->type_of_ships){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
             }elseif($data['date_formate']=="date"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->formated_date == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->type_of_ships){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }
        }elseif($data['type_wise']=="shipper"){
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($val->shipper==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$val->shipper;
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$val->shipper;
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_year == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->shipper){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->qtrYear == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->shipper){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $key => $value){
                    $m = date("M",strtotime($value->f_month)).'-'.$value->f_year;
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($m == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->shipper){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->formated_date == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->shipper){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }
        }elseif($data['type_wise']=="country"){
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($val->country==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$val->country;
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$val->country;
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_year == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->country){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->qtrYear == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->country){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        $m = date("M",strtotime($value->f_month)).'-'.$value->f_year;
                        if($m == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->country){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->formated_date == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->country){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }
        }elseif($data['type_wise']=="port"){
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($val->port==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$val->port;
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$val->port;
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                }
            }
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_year == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->port){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->qtrYear == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->port){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        $m = date("M",strtotime($value->f_month)).'-'.$value->f_year;
                        if($m == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->port){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->formated_date == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->port){
                                    $response[$rkkey]['data'][$kskey]['value'] = ((int) filter_var($response[$rkkey]['data'][$kskey]['value'], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                                }
                            }
                        }
                    }
                }
            }
        }else{
            $response = $this->sailed_graph_response_parent_set($data);
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $value){
                    $response[$value->f_year] = ((int) filter_var($response[$value->f_year], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $value){
                    $response[$value->qtrYear] = ((int) filter_var($response[$value->qtrYear], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $value){
                    $m = date("M",strtotime($value->f_month));
                    $response[$m.'-'.$value->f_year] = ((int) filter_var($response[$m.'-'.$value->f_year], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $value){
                    $response[$value->formated_date] = ((int) filter_var($response[$value->formated_date], FILTER_SANITIZE_NUMBER_INT) + (int) filter_var($value->qty, FILTER_SANITIZE_NUMBER_INT));
                }
            }
        }
        return $response;
    }

    function set_sailed_graph_data_old($data,$getData,$response){
        if($data['type_wise']=="sub-commodity"){
            // set response arr of 
            foreach($response as $response_k =>$v){
                foreach($getData as $val){
                    $response[$response_k][$val->cargo]=0;
                }
            }
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $value){
                    $response[$value->f_year][$value->cargo]++;
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $value){
                    $response[$value->qtrYear][$value->cargo]++;
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $value){
                    $m = date("M",strtotime($value->f_month));
                    $response[$m.'-'.$value->f_year][$value->cargo]++;
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $value){
                    $response[$value->formated_date][$value->cargo]++;
                }
            }
        }elseif($data['type_wise']=="shipper"){
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $val){
                    $response[$response_k][$val->shipper]=0;
                }
            }
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $value){
                    $response[$value->f_year][$value->shipper]++;
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $value){
                    $response[$value->qtrYear][$value->shipper]++;
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $value){
                    $m = date("M",strtotime($value->f_month));
                    $response[$m.'-'.$value->f_year][$value->shipper]++;
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $value){
                    $response[$value->formated_date][$value->shipper]++;
                }
            }
        }elseif($data['type_wise']=="country"){
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $val){
                    $response[$response_k][$val->country]=0;
                }
            }
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $value){
                    $response[$value->f_year][$value->country]++;
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $value){
                    $response[$value->qtrYear][$value->country]++;
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $value){
                    $m = date("M",strtotime($value->f_month));
                    $response[$m.'-'.$value->f_year][$value->country]++;
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $value){
                    $response[$value->formated_date][$value->country]++;
                }
            }
        }elseif($data['type_wise']=="port"){
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $val){
                    $response[$response_k][$val->port]=0;
                }
            }
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $value){
                    $response[$value->f_year][$value->port]++;
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $value){
                    $response[$value->qtrYear][$value->port]++;
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $value){
                    $m = date("M",strtotime($value->f_month));
                    $response[$m.'-'.$value->f_year][$value->port]++;
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $value){
                    $response[$value->formated_date][$value->port]++;
                }
            }
        }else{
            $response = $this->sailed_graph_response_parent_set($data);
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $value){
                    $response[$value->f_year]++;
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $value){
                    $response[$value->qtrYear]++;
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $value){
                    $m = date("M",strtotime($value->f_month));
                    $response[$m.'-'.$value->f_year]++;
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $value){
                    $response[$value->formated_date]++;
                }
            }
        }
        return $response;
    }

    /********
    **   This function is for to set railway graph in array* ******/
    function set_railway_graph_data_07042022($data,$getData,$response){
  
        if($data['type_wise']=="division"){
			
            // set response arr of 
            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($this->railwayfullform($val->division)==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$this->railwayfullform($val->division);
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$this->railwayfullform($val->division);
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            ////echo "dddd"; die;
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_year == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->division)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->qtrYear == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->division)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $key => $value){
                    $m = date("M",strtotime($value->f_month)).'-'.$value->f_year;
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($m == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->division)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="date"){
                // print_r($getData);die;
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_date == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->division)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }

            }
        }elseif($data['type_wise']=="load-station"){
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($this->railwayfullform($val->load_stn)==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$this->railwayfullform($val->load_stn);
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$this->railwayfullform($val->load_stn);
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            // print_r($response);die;
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_year == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->load_stn)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->qtrYear == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->load_stn)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $key => $value){
                    $m = date("M",strtotime($value->f_month)).'-'.$value->f_year;
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($m == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->load_stn)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_date == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->load_stn)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }
        }elseif($data['type_wise']=="unload-station"){
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($this->railwayfullform($val->unload_stn)==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$this->railwayfullform($val->unload_stn);
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$this->railwayfullform($val->unload_stn);
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_year == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->unload_stn)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->qtrYear == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->unload_stn)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        $m = date("M",strtotime($value->f_month)).'-'.$value->f_year;
                        if($m == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->unload_stn)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_date == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->unload_stn)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }
        }elseif($data['type_wise']=="consignor"){
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($this->railwayfullform($val->consignor)==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$this->railwayfullform($val->consignor);
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$this->railwayfullform($val->consignor);
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_year == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->consignor)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->qtrYear == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->consignor)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        $m = date("M",strtotime($value->f_month)).'-'.$value->f_year;
                        if($m == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->consignor)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_date == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->consignor)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }
        }elseif($data['type_wise']=="consignee"){
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($this->railwayfullform($val->consignee)==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$this->railwayfullform($val->consignee);
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$this->railwayfullform($val->consignee);
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_year == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->consignee)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->qtrYear == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->consignee)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        $m = date("M",strtotime($value->f_month)).'-'.$value->f_year;
                        if($m == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->consignee)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_date == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$this->railwayfullform($value->consignee)){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }
        }else{
            $response = $this->sailed_graph_response_parent_set($data);
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $value){
                    $response[$value->f_year]++;
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $value){
                    $response[$value->qtrYear]++;
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $value){
                    $m = date("M",strtotime($value->f_month));
                    $response[$m.'-'.$value->f_year]++;
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $value){
                    $response[$value->f_date]++;
                }
            }
        }
        return $response;
    }

    /********
    **  this function is for to get railway full form array* ******/

    public function railwayfullform($name){
        $response = Abbreviation::where(['f_abbr'=>$name])->first(['f_full_form']);
        return $response->f_full_form;
    }
	
	
	////new function to get railway data by mohan reddy on 07Apr2022
	public function railway_movement_graph(Request $request)
    {
        //DB::enableQueryLog();
        
       
        
        // get requested data from post
        $data = $request->only('date_formate','from_date','to_date','type_wise','commodity', 'country','division','consignor','consignee','load_siding','unload_siding');
        $validator = Validator::make($data, [
            'date_formate' => 'required',
            'from_date' => 'required',
            'to_date' => 'required',
            'commodity' => 'required',
            'country' => 'required',
        ]);
        //Send failed response if request is not valid
        if ($validator->fails()) {
            return response()->json(['status' => 401,'error' => $validator->messages()]);
        }

        if ($data['commodity']=='Iron Ore') {
            $data['commodity']='ORES';
        }
       
        //type_wise =(division,load-station,unload-station,consignor,consignee)
        //DB::enableQueryLog();
       ///// $getData = Railwayprogramme::select('*');
		$getData = Railwayprogramme::select('zone','f_date','commodity','qtrYear','f_year','f_month','selected_country', 'u1.f_full_form as division','u2.f_full_form as consignor','u3.f_full_form as consignee','u4.f_full_form as load_stn','u5.f_full_form as unload_stn','u1.f_abbr as u1key','u2.f_abbr as u2key','u3.f_abbr as u3key','u4.f_abbr as u4key','u5.f_abbr as u5key')
				->leftJoin('tbl_abbreviation as u1','u1.f_abbr','=','tbl_railway_programme.division')
				->leftJoin('tbl_abbreviation as u2','u2.f_abbr','=','tbl_railway_programme.consignor')
				->leftJoin('tbl_abbreviation as u3','u3.f_abbr','=','tbl_railway_programme.consignee')
				->leftJoin('tbl_abbreviation as u4','u4.f_abbr','=','tbl_railway_programme.load_stn')
				->leftJoin('tbl_abbreviation as u5','u5.f_abbr','=','tbl_railway_programme.unload_stn');
				
        /*if($data['type_wise']=="division"){
            // get data from table
            $getData = Railwayprogramme::select('tbl_railway_programme.*');
            // $getData = Railwayprogramme::select('division','consignor','consignee','load_stn','unload_stn');
        }elseif($data['type_wise']=="load-station"){
            // get data from table
            $getData = Railwayprogramme::select('tbl_railway_programme.*');
        }elseif($data['type_wise']=="unload-station"){
            // get data from table
            $getData = Railwayprogramme::select('tbl_railway_programme.*');
        }elseif($data['type_wise']=="consignor"){
            // get data from table
            $getData = Railwayprogramme::select('tbl_railway_programme.*');
        }elseif($data['type_wise']=="consignee"){
            // get data from table
            $getData = Railwayprogramme::select('*');
        }else{
            $getData = Railwayprogramme::select('*');
        }*/
        // -------------------------------------------------------
        // where condition on the base of value passed
        if($data['division']!=""){
            $division= explode(',',$data['division']);
            $getData = $getData->whereIn('division',$division);
        }
        if($data['consignor']!=""){
            $consignor= explode(',',$data['consignor']);
            $getData = $getData->whereIn('consignor',$consignor);
        }
        if($data['consignee']!=""){
            $consignee= explode(',',$data['consignee']);
            $getData = $getData->whereIn('consignee',$consignee);
        }
        if($data['load_siding']!=""){
            $load_siding= explode(',',$data['load_siding']);
            $getData = $getData->whereIn('load_stn',$load_siding);
        }
        if($data['unload_siding']!=""){
            $unload_siding= explode(',',$data['unload_siding']);
            $getData = $getData->whereIn('unload_stn',$unload_siding);
        }

        if($data['date_formate']=="yearly"){
            // need response "2000"
            $getData->whereBetween('f_year', [$data['from_date'], $data['to_date']]);
            $response = $this->sailed_graph_response_child_set($data);
        }elseif($data['date_formate']=="quaterly"){
            // need response "Q1 2000"
            $where = $this->where_quaterly_set($data);
            // print_r($where);die;
            $getData->whereIn('qtrYear', $where);
            $response = $this->sailed_graph_response_child_set($data);
        }elseif($data['date_formate']=="monthly"){
            // need response "Feb-2022"
            $getData->whereBetween('f_year', [date('Y',strtotime($data['from_date'])), date('Y',strtotime($data['to_date']))]);
            $where =$this->where_monthly_set($data);
            if(!empty($where)){
                $getData->whereIn('f_month', $where);
            }
            $response = $this->sailed_graph_response_child_set($data);
        }elseif($data['date_formate']=="date"){
            // need response "Y-m-d"
            $getData->whereBetween('f_date', [$data['from_date'], $data['to_date']]);
            $response = $this->sailed_graph_response_child_set($data);
        }

        if($data['commodity']!="" && $data['commodity']!="All"){
            $getData = $getData->where(['commodity'=>$data['commodity']]);
        }
        if($data['country']!="" && $data['country']!="All"){
            $getData = $getData->where(['selected_country'=>$data['country']]);
        }

        $getData = $getData->get();
         //dd(DB::getQueryLog());
         
         
         
         
        $response = $this->set_railway_graph_data($data,$getData,$response);
        
        if($data['type_wise']!=""){
            $newdata=[];
            $c=0;
            foreach ($response as $key => $value) {
                $newdata[$c]['name']=$key;
                $newdata[$c]['data']=array_values($value['data']);
                $c++;
            }
            $response=$newdata;
        }
        if(!empty($response)){
            $datas['graph_type']=  empty($data['type_wise']) ? 'Parent graph' : $data['type_wise'].' wise';
            $datas['graph_points']= $response;
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$datas],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }
	
	 function set_railway_graph_data($data,$getData,$response){
		////echo '<pre>', print_r($data),  print_r($response), '</pre>';die;
        if($data['type_wise']=="division"){
            // set response arr of 
            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($val->division==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$val->division;
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$val->division;
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            ////echo "dddd"; die;
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_year == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->division){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->qtrYear == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->division){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $key => $value){
                    $m = date("M",strtotime($value->f_month)).'-'.$value->f_year;
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($m == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->division){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="date"){
                ///echo '<pre>'; print_r($response);die;
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_date == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->division){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }

            }
        }elseif($data['type_wise']=="load-station"){
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($val->load_stn==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$val->load_stn;
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$val->load_stn;
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            // print_r($response);die;
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_year == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->load_stn){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->qtrYear == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->load_stn){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $key => $value){
                    $m = date("M",strtotime($value->f_month)).'-'.$value->f_year;
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($m == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->load_stn){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_date == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->load_stn){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }
        }elseif($data['type_wise']=="unload-station"){
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($val->unload_stn==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$val->unload_stn;
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$val->unload_stn;
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_year == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->unload_stn){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->qtrYear == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->unload_stn){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        $m = date("M",strtotime($value->f_month)).'-'.$value->f_year;
                        if($m == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->unload_stn){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_date == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->unload_stn){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }
        }elseif($data['type_wise']=="consignor"){
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($val->consignor==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$val->consignor;
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$val->consignor;
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_year == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->consignor){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->qtrYear == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->consignor){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        $m = date("M",strtotime($value->f_month)).'-'.$value->f_year;
                        if($m == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->consignor){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_date == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->consignor){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }
        }elseif($data['type_wise']=="consignee"){
            // set response arr of api
            foreach($response as $response_k =>$v){
                foreach($getData as $kd => $val){
                    if($kd > 0){
                        $count=0;
                        foreach ($response[$response_k]['data'] as $unikey => $univalue) {
                            if ($val->consignee==$univalue['name']){
                                $count++;
                            }
                        }
                        if($count==0){
                            $response[$response_k]['data'][$kd]['name']=$val->consignee;
                            $response[$response_k]['data'][$kd]['value']=0;
                        }
                    }else{
                        $response[$response_k]['data'][$kd]['name']=$val->consignee;
                        $response[$response_k]['data'][$kd]['value']=0;
                    }
                    
                }
            }
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_year == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->consignee){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->qtrYear == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->consignee){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        $m = date("M",strtotime($value->f_month)).'-'.$value->f_year;
                        if($m == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->consignee){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $key => $value){
                    foreach ($response as $rkkey=> $rkvalue) {
                        if($value->f_date == $rkkey){
                            foreach ($rkvalue['data'] as $kskey => $ksvalue) {
                                if($ksvalue['name']==$value->consignee){
                                    $response[$rkkey]['data'][$kskey]['value']++;
                                }
                            }
                        }
                    }
                }
            }
        }else{
            $response = $this->sailed_graph_response_parent_set($data);
            // set response of api
            if($data['date_formate']=="yearly"){
                foreach($getData as $value){
                    $response[$value->f_year]++;
                }
            }elseif($data['date_formate']=="quaterly"){
                foreach($getData as $value){
                    $response[$value->qtrYear]++;
                }
            }elseif($data['date_formate']=="monthly"){
                foreach($getData as $value){
                    $m = date("M",strtotime($value->f_month));
                    $response[$m.'-'.$value->f_year]++;
                }
            }elseif($data['date_formate']=="date"){
                foreach($getData as $value){
                    $response[$value->f_date]++;
                }
            }
        }
        return $response;
    }
    
    //new code for railway prog stack graphs
   /* public function railway_movement_graph_13042022_new(Request $request)
    {
        // get requested data from post
        $data = $request->only('date_formate','from_date','to_date','type_wise','commodity', 'country','division','consignor','consignee','load_siding','unload_siding');
        $validator = Validator::make($data, [
            'date_formate' => 'required',
            'from_date' => 'required',
            'to_date' => 'required',
            'commodity' => 'required',
            'country' => 'required'
        ]);
        
        //Send failed response if request is not valid
        if ($validator->fails()) {
            return response()->json(['status' => 401,'error' => $validator->messages()]);
        }

        if ($data['commodity']=='Iron Ore') {
            $data['commodity']='ORES';
        }
       
        $query='';
        $q1_s="";
        $q1_l="";
        $q2_s="";
        $q2_l="";
        $subquery="";
        $wherecount=0;
        
        $qtr_arr['from'] = array("Q1"=>1,"Q2"=>4,"Q3"=>7,"Q4"=>10);
        $qtr_arr['to'] = array("Q1"=>3,"Q2"=>6,"Q3"=>9,"Q4"=>12);
        if($data['type_wise'] !=""){
             $sel_type = array();
             $f_type  ='';
             $time_col = '';
             $group_by_col = '';
                if($data['type_wise'] =="division"){
                    $sel_type = "division";
                    $f_type  ='dvsn';
                }
                elseif($data['type_wise'] =="load-station"){
                    $sel_type ='load_stn';
                    $f_type  ='sttn_from_destn';
                }
                elseif($data['type_wise'] =="unload-station"){
                     $sel_type ='unload_stn';
                     $f_type  ='sttn_from_destn';
                }
                elseif($data['type_wise'] =="consignor"){
                    $sel_type="consignor";
                    $f_type  ='CNSR_CNSG';
                }
                elseif($data['type_wise'] =="consignee"){
                    $sel_type ="consignee";
                    $f_type  ='CNSR_CNSG';
                }
                
                $from_date='';
                $to_date ='';
                 if($data['date_formate']=="yearly"){
                    $from_date = $data['from_date']."-01-01";
                    $to_date = $data['to_date']."-12-31";
                    $time_col = 'f_year  as graph_x ,f_year as order_col';
                    $group_by_col = 'f_year';
                 }
                 elseif($data['date_formate']=="quaterly"){
                    $from_tmp = explode(" ",$data['from_date']);
                    $to_tmp = explode(" ",$data['to_date']);
                    $from_date = $from_tmp[1]."-".$qtr_arr['from'][$from_tmp[0]]."-01";
                    $to_date = date("Y-m-t",strtotime($to_tmp[1]."-".$qtr_arr['to'][$to_tmp[0]]."-01"));
                    $time_col='qtrYear as graph_x,qtrYear as order_col';
                    $group_by_col = 'qtrYear';
                 }
                 elseif($data['date_formate']=="monthly"){
                     $from_date = date("Y-m-01",strtotime($data['from_date']));
                     $to_date = date("Y-m-t",strtotime($data['to_date']));
                     $time_col = "concat(f_year,'-',f_month) as graph_x,concat(f_year,'-',f_month) as order_col";
                     $group_by_col = 'f_year,f_month';
                     
                 }
                 elseif($data['date_formate']=="date"){
                    $from_date = date("Y-m-d",strtotime($data['from_date']));
                    $to_date = date("Y-m-d",strtotime($data['to_date']));
                    $time_col = "f_date  as graph_x,f_date as order_col ";
                    $group_by_col = 'f_date';
                 }
            
            $q1_l=" group by $group_by_col, $sel_type )";
            $q2_l=" group by $group_by_col) order by 2,5,4 desc";
            $date_cond = "  `tbl_railway_programme`.`f_date` >= '$from_date' and `tbl_railway_programme`.`f_date` <= '$to_date'";
            
            $subquery .= "SELECT $sel_type, count(*) as cnt FROM `tbl_railway_programme` WHERE  $date_cond ";           
            $subquery.=" group by $sel_type order by cnt desc limit 0,11";
             
             
              $q1_s=" (SELECT $time_col,
                    (SELECT `f_full_form`
                    FROM `tbl_abbreviation`
                    WHERE `f_abbr` = tbl_railway_programme.$sel_type
                        AND `f_type` = '$f_type') as graph_y,
                        count(*) AS cnt,
                        '1' AS flag
                    FROM `tbl_railway_programme`
                    WHERE ". $date_cond ;
                $q2_s="
                        UNION
                        
                        (SELECT $time_col,
                            'others' AS graph_y,
                            count(*),
                            '2' AS flag
                        FROM `tbl_railway_programme`
                        WHERE ". $date_cond ;
                
                //getting in values
                
                $subquery= DB::select($subquery);
              //  print_r($subquery);
                $subwhere='"';
                for( $i = 0; $i < count($subquery); $i++ ){
                    if ($i == count($subquery)-1) {
                        $subwhere.=$subquery[$i][$sel_type].'"';
                    }else{
                        $subwhere.=$subquery[$i][$sel_type].'","';
                    }
                }
                //echo $subwhere;
                $q1_s.=" and $sel_type in (".$subwhere.") ";
                $q2_s.=" and $sel_type not in  (".$subwhere.") ";
        }  
        else{
            $query="SELECT  qtrYear,f_year,f_month,f_date FROM `tbl_railway_programme` WHERE";
        }

        if($data['division']!=""){
            $division = "'".str_replace(",","','",$data['division'])."'";
            if ($data['type_wise'] !="division") {
                $q1_s.=" and division in (".$division.")";
                $q2_s.=" and division in  (".$division.")";
            }
        }
        if($data['consignor']!=""){
            $consignor = "'".str_replace(",","','",$data['consignor'])."'";
            if ($data['type_wise'] !="consignor") {
                $q1_s.=" and consignor in (".$consignor.")";
                $q2_s.=" and consignor in  (".$consignor.")";
            }
        }
        if($data['consignee']!=""){
            $consignee = "'".str_replace(",","','",$data['consignee'])."'";
            if ($data['type_wise'] !="consignee") {
                $q1_s.=" and consignee in (".$consignee.")";
                $q2_s.=" and consignee in  (".$consignee.")";
            }
        }
        if($data['load_siding']!=""){
            $load_siding = "'".str_replace(",","','",$data['load_siding'])."'";
            if ($data['type_wise'] !="load-station") {
                $q1_s.=" and load_stn in (".$load_siding.")";
                $q2_s.=" and load_stn in  (".$load_siding.")";
            }
        }
        if($data['unload_siding']!=""){
            $unload_siding = "'".str_replace(",","','",$data['unload_siding'])."'";
            if ($data['type_wise'] !="unload-station") {
                $q1_s.=" and unload_stn in (".$unload_siding.")";
                $q2_s.=" and unload_stn in  (".$unload_siding.")";
            }
        }
        

        if($data['commodity']!="" && $data['commodity']!="All"){
            $q1_s.=" and commodity = '".$data['commodity']."'";
            $q2_s.=" and commodity = '".$data['commodity']."'";
        }
        if($data['country']!="" && $data['country']!="All"){
            $q1_s.=" and selected_country = '".$data['country']."'";
            $q2_s.=" and selected_country = '".$data['country']."'";
        }
        
        //echo $q1_s.$q1_l.$q2_s.$q2_l;
        
        $getData =  DB::select($q1_s.$q1_l.$q2_s.$q2_l);
        $response = $this->set_railway_graph_data_13042022_new($data,$getData);

        if($data['type_wise']!=""){
            //$newdata=[];
            $newdata = array();
            $c=0;
            foreach ($response as $key => $value) {
                if (isset($value) && !empty($value)) {
                    $newdata[$c]['name']=$key;
                    $newdata[$c]['data']=array_values($value);
                    $c++;
                }
            }
            $response=$newdata;
        }
        
        if(!empty($response)){
            $datas['graph_type']=  empty($data['type_wise']) ? 'Parent graph' : $data['type_wise'].' wise';
            $datas['graph_points']= $response;
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$datas],$this->successStatus);
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }
    
    function set_railway_graph_data_13042022_new($data,$getData){
        for( $i = 0; $i < count($getData); $i++ ){
            if($data['date_formate']=="monthly")
                $m = date("M-Y",strtotime($getData[$i]['graph_x']));
            else
                $m = $getData[$i]['graph_x'];
            
            $response[$m][$i]['name']= $getData[$i]['graph_y'];
            $response[$m][$i]['value']= $getData[$i]['cnt'];
        }
        return $response;
    } 
    */
      public function railway_movement_graph_13042022_new_tarun(Request $request)
    {      
        // get requested data from post
        $data = $request->only('date_formate','from_date','to_date','type_wise','commodity', 'country','division','consignor','consignee','load_siding','unload_siding');
        $validator = Validator::make($data, [
            'date_formate' => 'required',
            'from_date' => 'required',
            'to_date' => 'required',
            'commodity' => 'required',
            'country' => 'required',
            //'type_wise' => 'required',

        ]);
        //Send failed response if request is not valid
        if ($validator->fails()) {
            return response()->json(['status' => 401,'error' => $validator->messages()]);
        }

//         if ($data['commodity']=='Iron Ore') {
//             $data['commodity']='ORES';
//         }
       
        $query='';
        $q1_s="";
        $q1_l="";
        $q2_s="";
        $q2_l="";
        $subquery="";
        $wherecount=0;
        
        $qtr_arr['from'] = array("Q1"=>1,"Q2"=>4,"Q3"=>7,"Q4"=>10);
        $qtr_arr['to'] = array("Q1"=>3,"Q2"=>6,"Q3"=>9,"Q4"=>12);
        
        $from_date='';
        $to_date ='';
        $time_col = '';
        $group_by_col = '';
        
        //making date condition based on passed input
        if($data['date_formate']=="yearly"){
            $from_date = $data['from_date']."-01-01";
            $to_date = $data['to_date']."-12-31";
            $time_col = 'f_year  as graph_x ,f_year as order_col';
            $group_by_col = 'f_year';
        }
        elseif($data['date_formate']=="quaterly"){
            $from_tmp = explode(" ",$data['from_date']);
            $to_tmp = explode(" ",$data['to_date']);
            $from_date = $from_tmp[1]."-".$qtr_arr['from'][$from_tmp[0]]."-01";
            $to_date = date("Y-m-t",strtotime($to_tmp[1]."-".$qtr_arr['to'][$to_tmp[0]]."-01"));
            $time_col='qtrYear as graph_x,qtrYear as order_col';
            $group_by_col = 'qtrYear';
        }
        elseif($data['date_formate']=="monthly"){
            $from_date = date("Y-m-01",strtotime($data['from_date']));
            $to_date = date("Y-m-t",strtotime($data['to_date']));
            $time_col = "concat(f_year,'-',f_month) as graph_x,concat(f_year,'-',f_month) as order_col";
            $group_by_col = 'f_year,f_month';
            
        }
        elseif($data['date_formate']=="date"){
            $from_date = date("Y-m-d",strtotime($data['from_date']));
            $to_date = date("Y-m-d",strtotime($data['to_date']));
            $time_col = "f_date  as graph_x,f_date as order_col ";
            $group_by_col = 'f_date';
        }
        $date_cond = " and `tbl_railway_programme`.`f_date` >= '$from_date' and `tbl_railway_programme`.`f_date` <= '$to_date'";  
        
        // making where condition on the base of value passed 
        $extra_filter ='';
        $extra_filter_subqry ='';        
        if($data['division']!=""){
            // $division= explode(',',$data['division']);
            $division = "'".str_replace(",","','",$data['division'])."'";
            if ($data['type_wise'] !="division") {
                $extra_filter.=" and division in (".$division.")";                
            }
            $extra_filter_subqry.=" and division in (".$division.")";     
        }
        if($data['consignor']!=""){
            // $consignor= explode(',',$data['consignor']);
            $consignor = "'".str_replace(",","','",$data['consignor'])."'";
            if ($data['type_wise'] !="consignor") {
               $extra_filter .=" and consignor in (".$consignor.")";                
            }
            $extra_filter_subqry .=" and consignor in (".$consignor.")";                
        }
        if($data['consignee']!=""){
            // $consignee= explode(',',$data['consignee']);
            $consignee = "'".str_replace(",","','",$data['consignee'])."'";
            if ($data['type_wise'] !="consignee") {
                $extra_filter .=" and consignee in (".$consignee.")";                
            }
            $extra_filter_subqry .=" and consignee in (".$consignee.")";                
        }
        if($data['load_siding']!=""){
            // $load_siding= explode(',',$data['load_siding']);
            $load_siding = "'".str_replace(",","','",$data['load_siding'])."'";
            if ($data['type_wise'] !="load-station") {
                $extra_filter.=" and load_stn in (".$load_siding.")";
            }
            $extra_filter_subqry.=" and load_stn in (".$load_siding.")";
        }
        if($data['unload_siding']!=""){
            // $unload_siding= explode(',',$data['unload_siding']);
            $unload_siding = "'".str_replace(",","','",$data['unload_siding'])."'";
            if ($data['type_wise'] !="unload-station") {
                $extra_filter.=" and unload_stn in (".$unload_siding.")";                
            }
            $extra_filter_subqry.=" and unload_stn in (".$unload_siding.")";                
        }
            
        if($data['type_wise'] !=""){
             $sel_type = array();
             $f_type  ='';
             
                if($data['type_wise'] =="division"){
                    $sel_type = "division";
                    $f_type  ='dvsn';
                }
                elseif($data['type_wise'] =="load-station"){
                    $sel_type ='load_stn';
                    $f_type  ='sttn_from_destn';
                }
                elseif($data['type_wise'] =="unload-station"){
                     $sel_type ='unload_stn';
                     $f_type  ='sttn_from_destn';
                }
                elseif($data['type_wise'] =="consignor"){
                    $sel_type="consignor";
                    $f_type  ='CNSR_CNSG';
                }
                elseif($data['type_wise'] =="consignee"){
                    $sel_type ="consignee";
                    $f_type  ='CNSR_CNSG';
                }
                
                
            $q1_l=" group by $group_by_col, graph_y )";
            $q2_l=" group by $group_by_col) order by 2,5,4 desc";
            
            
            $subquery = "
                        SELECT $sel_type,  
                                (
                                    SELECT `f_full_form`
                                    FROM `tbl_abbreviation`
                                    WHERE `f_abbr` = tbl_railway_programme.$sel_type
                                    AND     `f_type` = '$f_type'
                                ) as fullForm,  
                                count(*) as cnt
                        FROM `tbl_railway_programme` 
                        WHERE  1 
                                $date_cond $extra_filter_subqry
                    ";           
            $subquery.=" group by $sel_type,fullForm order by cnt desc limit 0,11";
            
             
              $q1_s=" (SELECT $time_col,
                    (SELECT `f_full_form`
                    FROM `tbl_abbreviation`
                    WHERE `f_abbr` = tbl_railway_programme.$sel_type
                        AND `f_type` = '$f_type') as graph_y,
                        count(*) AS cnt,
                        '1' AS flag,
                        $sel_type as abbrLegends
                    FROM `tbl_railway_programme`
                    WHERE 1 ". $date_cond ;
                $q2_s="
                        UNION
                        
                        (SELECT $time_col,
                            'Others' AS graph_y,
                            count(*),
                            '2' AS flag,
                            'others' as abbrLegends
                        FROM `tbl_railway_programme`
                        WHERE 1 ". $date_cond ;
                
                //getting in values
                
                $subquery= DB::select($subquery);
                $subwhere="'"; $abbrFullForm = array();
                for( $i = 0; $i < count($subquery); $i++ ){
                    if ($i == count($subquery)-1) {
                        $subwhere.=$subquery[$i][$sel_type]."'";
                    }else{
                        $subwhere.=$subquery[$i][$sel_type]."','";
                    }
                    $abbrFullForm[] = $subquery[$i]['fullForm'];
                }
                
                $q1_s.=" and $sel_type in (".$subwhere.") ";
                $q2_s.=" and $sel_type not in  (".$subwhere.") ";
               // $response = $this->sailed_graph_response_child_set($data);
                
                // where condition on the base of value passed
                if($data['commodity']!="" && $data['commodity']!="All"){
                    if($data['commodity']=='Iron Ore'){
                        $q1_s.=" and commodity in ('RMSP','ORES') ";
                        $q2_s.="  and commodity in ('RMSP','ORES') ";
                    }
                    else
                    {
                        $q1_s.=" and commodity = '".$data['commodity']."'";
                        $q2_s.=" and commodity = '".$data['commodity']."'";
                    }
                }
                if($data['country']!="" && $data['country']!="All"){
                    $q1_s.=" and selected_country = '".$data['country']."'";
                    $q2_s.=" and selected_country = '".$data['country']."'";
                }
                $q1_s.= $extra_filter;
                $q2_s.= $extra_filter;
                
                //echo $q1_s.$q1_l.$q2_s.$q2_l;
                //die;
                $getData =  DB::select($q1_s.$q1_l.$q2_s.$q2_l);
                // dd(DB::getQueryLog());
                
                $response = $this->set_railway_graph_data_13042022_new_tarun($data,$getData, $abbrFullForm);
                  //$newdata=[];
                //print_r($response['data']);
                $newdata = array();
                $c=0;
                /*foreach ($response['data'] as $key => $value) {
                    if (isset($value) && !empty($value)) {
                        $newdata[$c]['name']=$key;

                        $newdata[$c]['data']=array_values($value);
                        $c++;
                    }
                    
                } */ 
                
                
                //$response=$newdata;
        }  
        else{
               $q1_s=" SELECT $time_col,
                        count(*) AS cnt                      
                    FROM `tbl_railway_programme`
                    WHERE 1 ". $date_cond  ;                
              // where condition on the base of value passed
              $q1_s.= $extra_filter;
               if($data['commodity']!="" && $data['commodity']!="All"){
                    if($data['commodity']=='Iron Ore'){
                        $q1_s.=" and commodity in ('RMSP','ORES') ";                        
                    }
                    else 
                        $q1_s.=" and commodity = '".$data['commodity']."'";
                }
                if($data['country']!="" && $data['country']!="All"){
                    $q1_s.=" and selected_country = '".$data['country']."'";
                }
               $q1_s .= " group by " .$group_by_col.  " order by f_date ";
               $getData =  DB::select($q1_s);
              // print_r($getData);
               foreach($getData as $key => $value){
                if($data['date_formate']=="monthly")
                    $m = date("M-Y",strtotime($value[graph_x]));
                else
                    $m = $value[graph_x];
                $response[$m]=$value[cnt];
              }       
        
        }       
        if(!empty($response)){
            $datas['graph_type']=  empty($data['type_wise']) ? 'Parent graph' : $data['type_wise'].' wise';
            $datas['graph_points']= $response;
            //print_r($datas);
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$datas],$this->successStatus);
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }
 
 
  
    function set_railway_graph_data_13042022_new_tarun($data,$getData, $abbrLegends){
        $dateKey = '';
        //$abbrLegends = explode(",", str_replace("'", "", $abbrLegends));
        $tempArrLegends = $abbrLegends;
        $finres = array();
        for( $i = 0; $i < count($getData); $i++ ){
            if($data['date_formate']=="monthly")
                $m = date("M-Y",strtotime($getData[$i]['graph_x']));
            else
                $m = $getData[$i]['graph_x'];
                
             $response[$m][]= array("name"=>$getData[$i]['graph_y'],"value"=>$getData[$i]['cnt']);
            if( $dateKey == $m ){
                
                $tempArrLegends = array_diff($tempArrLegends, array($getData[$i]['graph_y']));
                //$tempArrLegends
            } else {
                if( $i > 0 ){
                    $tempArrLegends = array_values($tempArrLegends);
                    
                    //date key has chnaged
                    if( count($tempArrLegends) > 0 ){
                        for( $i1 = 0; $i1 < count($tempArrLegends); $i1++ ){
                            $response[$dateKey][]= array("name"=>$tempArrLegends[$i1],"value"=>0);
                        }
                    }
                    $finres[] = array("name"=>$dateKey,"data"=>array_values($response[$dateKey]));
                    $response = array();
                }
                $dateKey = $m;
                $tempArrLegends = $abbrLegends;
            }
        }
        return $finres;
    }
    
    public function railway_movement_graph_13042022_new(Request $request)
    {      
        // get requested data from post
        $data = $request->only('date_formate','from_date','to_date','type_wise','commodity', 'country','division','consignor','consignee','load_siding','unload_siding');
        $validator = Validator::make($data, [
            'date_formate' => 'required',
            'from_date' => 'required',
            'to_date' => 'required',
            'commodity' => 'required',
            'country' => 'required',
            //'type_wise' => 'required',

        ]);
        //Send failed response if request is not valid
        if ($validator->fails()) {
            return response()->json(['status' => 401,'error' => $validator->messages()]);
        }

//         if ($data['commodity']=='Iron Ore') {
//             $data['commodity']='ORES';
//         }
       
        $query='';
        $q1_s="";
        $q1_l="";
        $q2_s="";
        $q2_l="";
        $subquery="";
        $wherecount=0;
        
        $qtr_arr['from'] = array("Q1"=>1,"Q2"=>4,"Q3"=>7,"Q4"=>10);
        $qtr_arr['to'] = array("Q1"=>3,"Q2"=>6,"Q3"=>9,"Q4"=>12);
        
        $from_date='';
        $to_date ='';
        $time_col = '';
        $group_by_col = '';
        
        //making date condition based on passed input
        if($data['date_formate']=="yearly"){
            $from_date = $data['from_date']."-01-01";
            $to_date = $data['to_date']."-12-31";
            $time_col = 'f_year  as graph_x ,f_year as order_col';
            $group_by_col = 'f_year';
        }
        elseif($data['date_formate']=="quaterly"){
            $from_tmp = explode(" ",$data['from_date']);
            $to_tmp = explode(" ",$data['to_date']);
            $from_date = $from_tmp[1]."-".$qtr_arr['from'][$from_tmp[0]]."-01";
            $to_date = date("Y-m-t",strtotime($to_tmp[1]."-".$qtr_arr['to'][$to_tmp[0]]."-01"));
            $time_col='qtrYear as graph_x,qtrYear as order_col';
            $group_by_col = 'qtrYear';
        }
        elseif($data['date_formate']=="monthly"){
            $from_date = date("Y-m-01",strtotime($data['from_date']));
            $to_date = date("Y-m-t",strtotime($data['to_date']));
            $time_col = "concat(f_year,'-',f_month) as graph_x,concat(f_year,'-',f_month) as order_col";
            $group_by_col = 'f_year,f_month';
            
        }
        elseif($data['date_formate']=="date"){
            $from_date = date("Y-m-d",strtotime($data['from_date']));
            $to_date = date("Y-m-d",strtotime($data['to_date']));
            $time_col = "f_date  as graph_x,f_date as order_col ";
            $group_by_col = 'f_date';
        }
        $date_cond = " and `tbl_railway_programme`.`f_date` >= '$from_date' and `tbl_railway_programme`.`f_date` <= '$to_date'";  
        
        // making where condition on the base of value passed 
        $extra_filter ='';
        $extra_filter_subqry ='';        
        if($data['division']!=""){
            // $division= explode(',',$data['division']);
            $division = "'".str_replace(",","','",$data['division'])."'";
            if ($data['type_wise'] !="division") {
                $extra_filter.=" and division in (".$division.")";                
            }
            $extra_filter_subqry.=" and division in (".$division.")";     
        }
        if($data['consignor']!=""){
            // $consignor= explode(',',$data['consignor']);
            $consignor = "'".str_replace(",","','",$data['consignor'])."'";
            if ($data['type_wise'] !="consignor") {
               $extra_filter .=" and consignor in (".$consignor.")";                
            }
            $extra_filter_subqry .=" and consignor in (".$consignor.")";                
        }
        if($data['consignee']!=""){
            // $consignee= explode(',',$data['consignee']);
            $consignee = "'".str_replace(",","','",$data['consignee'])."'";
            if ($data['type_wise'] !="consignee") {
                $extra_filter .=" and consignee in (".$consignee.")";                
            }
            $extra_filter_subqry .=" and consignee in (".$consignee.")";                
        }
        if($data['load_siding']!=""){
            // $load_siding= explode(',',$data['load_siding']);
            $load_siding = "'".str_replace(",","','",$data['load_siding'])."'";
            if ($data['type_wise'] !="load-station") {
                $extra_filter.=" and load_stn in (".$load_siding.")";
            }
            $extra_filter_subqry.=" and load_stn in (".$load_siding.")";
        }
        if($data['unload_siding']!=""){
            // $unload_siding= explode(',',$data['unload_siding']);
            $unload_siding = "'".str_replace(",","','",$data['unload_siding'])."'";
            if ($data['type_wise'] !="unload-station") {
                $extra_filter.=" and unload_stn in (".$unload_siding.")";                
            }
            $extra_filter_subqry.=" and unload_stn in (".$unload_siding.")";                
        }
            
        if($data['type_wise'] !=""){
             $sel_type = array();
             $f_type  ='';
             
                if($data['type_wise'] =="division"){
                    $sel_type = "division";
                    $f_type  ='dvsn';
                }
                elseif($data['type_wise'] =="load-station"){
                    $sel_type ='load_stn';
                    $f_type  ='sttn_from_destn';
                }
                elseif($data['type_wise'] =="unload-station"){
                     $sel_type ='unload_stn';
                     $f_type  ='sttn_from_destn';
                }
                elseif($data['type_wise'] =="consignor"){
                    $sel_type="consignor";
                    $f_type  ='CNSR_CNSG';
                }
                elseif($data['type_wise'] =="consignee"){
                    $sel_type ="consignee";
                    $f_type  ='CNSR_CNSG';
                }
                
                
            $q1_l=" group by $group_by_col, graph_y )";
            $q2_l=" group by $group_by_col) order by 2,5,4 desc";
            
            
            $subquery .= "SELECT $sel_type, count(*) as cnt FROM `tbl_railway_programme` WHERE  1 $date_cond $extra_filter_subqry";           
            $subquery.=" group by $sel_type order by cnt desc limit 0,11";
            
             
              $q1_s=" (SELECT $time_col,
                    (SELECT `f_full_form`
                    FROM `tbl_abbreviation`
                    WHERE `f_abbr` = tbl_railway_programme.$sel_type
                        AND `f_type` = '$f_type') as graph_y,
                        count(*) AS cnt,
                        '1' AS flag
                    FROM `tbl_railway_programme`
                    WHERE 1 ". $date_cond ;
                $q2_s="
                        UNION
                        
                        (SELECT $time_col,
                            'Others' AS graph_y,
                            count(*),
                            '2' AS flag
                        FROM `tbl_railway_programme`
                        WHERE 1 ". $date_cond ;
                
                //getting in values
                
                $subquery= DB::select($subquery);
                $subwhere="'";
                for( $i = 0; $i < count($subquery); $i++ ){
                    if ($i == count($subquery)-1) {
                        $subwhere.=$subquery[$i][$sel_type]."'";
                    }else{
                        $subwhere.=$subquery[$i][$sel_type]."','";
                    }
                }
                //print_r($subquery);
                if($subwhere=="'"){
                    $q1_s.=" and $sel_type in (NULL) ";
                    $q2_s.=" and $sel_type in  (NULL) ";
                }
                else{
                    $q1_s.=" and $sel_type in (".$subwhere.") ";
                    $q2_s.=" and $sel_type not in  (".$subwhere.") ";
                }
//                 $q1_s.=" and $sel_type in (".$subwhere.") ";
//                 $q2_s.=" and $sel_type not in  (".$subwhere.") ";
               // $response = $this->sailed_graph_response_child_set($data);
                
                // where condition on the base of value passed
                if($data['commodity']!="" && $data['commodity']!="All"){
                    if($data['commodity']=='Iron Ore'){
                        $q1_s.=" and commodity in ('RMSP','ORES') ";
                        $q2_s.="  and commodity in ('RMSP','ORES') ";
                    }
                    else
                    {
                        $q1_s.=" and commodity = '".$data['commodity']."'";
                        $q2_s.=" and commodity = '".$data['commodity']."'";
                    }
                }
                if($data['country']!="" && $data['country']!="All"){
                    $q1_s.=" and selected_country = '".$data['country']."'";
                    $q2_s.=" and selected_country = '".$data['country']."'";
                }
                $q1_s.= $extra_filter;
                $q2_s.= $extra_filter;
                
                //echo $q1_s.$q1_l.$q2_s.$q2_l;
                //die;
                $getData =  DB::select($q1_s.$q1_l.$q2_s.$q2_l);
                // dd(DB::getQueryLog());
                
                $response = $this->set_railway_graph_data_13042022_new($data,$getData);
                  //$newdata=[];
                //print_r($response['data']);
                $newdata = array();
                $c=0;
                if(isset($response['data']) && !empty($response['data'])){
                    foreach ($response['data'] as $key => $value) {
                        if (isset($value) && !empty($value)) {
                            $newdata[$c]['name']=$key;
                            //if($c==0)
                            {   
                                $value_new=array();
                                $array_new=array();
                                $array_new = array_column($value, 'name');                         
                                $diff = array_diff($response['unique_col'],$array_new);
                                foreach($diff as $diff_val){
                                    $value_new[] = array("name"=>$diff_val,"value"=>0);
                                }
                                if(!empty($value_new))
                                    $value=array_merge($value,$value_new);
                                $newdata[$c]['data']=array_values($value);
                            }
                            //else
                            // $newdata[$c]['data']=array_values($value);                       
                        
                            $c++;
                        }
                        
                    } 
                }
              //  print_r($newdata);
                $response=$newdata;
        }  
        else{
               $q1_s=" SELECT $time_col,
                        count(*) AS cnt                      
                    FROM `tbl_railway_programme`
                    WHERE 1 ". $date_cond  ;                
              // where condition on the base of value passed
              $q1_s.= $extra_filter;
               if($data['commodity']!="" && $data['commodity']!="All"){
                    if($data['commodity']=='Iron Ore'){
                        $q1_s.=" and commodity in ('RMSP','ORES') ";                        
                    }
                    else 
                        $q1_s.=" and commodity = '".$data['commodity']."'";
                }
                if($data['country']!="" && $data['country']!="All"){
                    $q1_s.=" and selected_country = '".$data['country']."'";
                }
               $q1_s .= " group by " .$group_by_col.  " order by f_date ";
               $getData =  DB::select($q1_s);
              // print_r($getData);
               foreach($getData as $key => $value){
                if($data['date_formate']=="monthly")
                    $m = date("M-Y",strtotime($value[graph_x]));
                else
                    $m = $value[graph_x];
                $response[$m]=$value[cnt];
              }       
        
        }       
        if(!empty($response)){
            $datas['graph_type']=  empty($data['type_wise']) ? 'Parent graph' : $data['type_wise'].' wise';
            $datas['graph_points']= $response;
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$datas],$this->successStatus);
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 200);
        }
    }
    
    function set_railway_graph_data_13042022_new($data,$getData){
        /*$count=0;
       
        foreach($getData as $key => $value){
            if($data['date_formate']=="monthly")
                $m = date("M-Y",strtotime($value->graph_x));
            else
                $m = $value->graph_x;
            $response[$m][$count]['name']=$value->graph_y;
            $response[$m][$count]['value']=$value->cnt;
            $count++;
        }
        
        return $response;*/
        $final_data =array();
        if(!empty($getData)){
            $unique_col = array();
            for( $i = 0; $i < count($getData); $i++ ){
                if($data['date_formate']=="monthly")
                    $m = date("M-Y",strtotime($getData[$i]['graph_x']));
                else
                    $m = $getData[$i]['graph_x'];
                
                $response[$m][$i]['name']= $getData[$i]['graph_y'];
                $response[$m][$i]['value']= $getData[$i]['cnt'];
                
                $unique_col[$getData[$i]['graph_y']] = $getData[$i]['graph_y'];
            }
            $final_data['unique_col'] = array_unique($unique_col);
            $final_data['data'] = $response;
        }
        return $final_data;
    }
    
    
}
