<?php
namespace App\Http\Controllers\API;
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;
class MasterController extends Controller 
{
    public $successStatus = 200;
   
    /********
    ** vessel freight filter
    * ******/
    public function vessel_freight()
    {
        // get data from table
        $res = Eximmaster::select('item')->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])->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])->groupBy('unload_country')->get();
                foreach($unload as $ku =>$uvalue){
                    $data[$key][$value->item][$lvalue->load_country]['unload'][]=$uvalue->unload_country;
                    $data[$key][$value->item][$lvalue->load_country]['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')->groupBy('load_country')->get();
        foreach($load as $ke =>$val){
            $unload = Eximmaster::select('unload_country','f_grade')->where(['load_country'=>$val->load_country])->groupBy('unload_country')->get();
            foreach($unload as $ku =>$uvalue){
                $all['all'][$val->load_country]['unload'][]=$uvalue->unload_country;
                $all['all'][$val->load_country]['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()
    {
        // get data from table
        $commodity = Logisticdata::select('commodity')->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])->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])->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])->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])->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');
        $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()]);
        }
        // DB::enableQueryLog();
        // get data from table 
        $getData = Logisticdata::select('id','status as vessul_status','cargo as sub_commodiy','load_port','unload_port','load_country','unload_country','type_of_ships as vessel_size')
                    ->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->orderBy('id','DESC')->get();
        // dd(DB::getQueryLog());
        // this is for to set response of data
        $response=['vessel_size'=>[],'vessul_status'=>[],'sub_commodiy'=>[],'load_port'=>[],'unload_port'=>[],'country'=>[]];
        foreach($getData as $key =>$value){
            if (!in_array($value->vessel_size,$response['vessel_size'])){
                $response['vessel_size'][]=$value->vessel_size;
            }
            if (!in_array($value->vessul_status,$response['vessul_status'])){
                $response['vessul_status'][]=$value->vessul_status;
            }
            if (!in_array($value->sub_commodiy,$response['sub_commodiy'])){
                $response['sub_commodiy'][]=$value->sub_commodiy;
            }
            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(!empty($response)){
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$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');
        $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'=>[]];
        $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',DB::raw('SUM(qty) AS 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['country_wise']=="Export" || $data['country_wise']=="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();
            $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;
                }
            }
            // 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']=="shipper"){
            // get data from table
            $getData = Logisticdata::select('status','shipper',DB::raw('SUM(qty) AS 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['country_wise']=="Export" || $data['country_wise']=="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)->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;
                }
            }
            // 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',DB::raw('SUM(qty) AS qty'));
            }else{
                $getData = Logisticdata::select('id','status','load_country as country',DB::raw('SUM(qty) AS 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['country_wise']=="Export" || $data['country_wise']=="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)->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;
                }
            }
            // 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',DB::raw('SUM(qty) AS qty'));
            }else{
                $getData = Logisticdata::select('id','status','unload_port as port',DB::raw('SUM(qty) AS 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->where('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['country_wise']=="Export" || $data['country_wise']=="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)->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;
                }
            }
            // print_r($response);die;
            // 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;
                            }
                        }
                    }
                }
            }
        }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['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['country_wise']=="Export" || $data['country_wise']=="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]=($response[$value->status]+$value->qty);
            }
        }
        // print_r($response);die;
        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['country_wise']=="Export" || $data['country_wise']=="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['country_wise']=="Export" || $data['country_wise']=="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['country_wise']=="Export" || $data['country_wise']=="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['country_wise']=="Export" || $data['country_wise']=="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['country_wise']=="Export" || $data['country_wise']=="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['country_wise']=="Export" || $data['country_wise']=="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['country_wise']=="Export" || $data['country_wise']=="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['country_wise']=="Export" || $data['country_wise']=="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['country_wise']=="Export" || $data['country_wise']=="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['country_wise']=="Export" || $data['country_wise']=="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()
    {
        // get data from table
        $commodity = LogisticshipData::select('commodity')->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])->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])->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])->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])->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');
        $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(['success' => false,'error' => $validator->messages()]);
        }
        // get data from table on behalf of filter
        $getData = LogisticshipData::select('id','status as vessul_status','cargo as sub_commodiy','load_port','unload_port','load_country','unload_country','type_of_ships as vessel_size')
                    ->where([
                        // 'commodity'=>$data['commodity'], 
                        // 'selected_country'=>$data['country'],
                        '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->orderBy('id','DESC')->get();
        // set data of response
        $response=['vessel_size'=>[],'vessul_status'=>[],'sub_commodiy'=>[],'load_port'=>[],'unload_port'=>[],'country'=>[]];
        foreach($getData as $key =>$value){
            if (!in_array($value->vessel_size,$response['vessel_size'])){
                $response['vessel_size'][]=$value->vessel_size;
            }
            if (!in_array($value->vessul_status,$response['vessul_status'])){
                $response['vessul_status'][]=$value->vessul_status;
            }
            if (!in_array($value->sub_commodiy,$response['sub_commodiy'])){
                $response['sub_commodiy'][]=$value->sub_commodiy;
            }
            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(!empty($response)){
            return response()->json(['status'=>1001,'message'=>'Data Found successfully','data'=>$response],$this->successStatus); 
        }else{
            return response()->json(['status'=>401,'message'=>'No Data Found'], 401);
        }
    }

    /********
    ** vessel lineup sailed graph
    * ******/
    public function vessel_lineup_sailed_graph(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['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']=="shipper"){
            // get data from table
            $getData = LogisticshipData::select('id','f_year','f_month','qtrYear','formated_date','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']]);
        // 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->where('type_of_ships',$vessel_size);
        }
        if($data['vessel_status']!=""){
            $vessel_status= explode(',',$data['vessel_status']);
            $getData = $getData->where('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['country_wise']=="Export" || $data['country_wise']=="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->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['country_wise']=="Export" || $data['country_wise']=="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['country_wise']=="Export" || $data['country_wise']=="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['country_wise']=="Export" || $data['country_wise']=="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['country_wise']=="Export" || $data['country_wise']=="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['country_wise']=="Export" || $data['country_wise']=="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')->groupBy('commodity')->get();
        $data=[];
        $all=[];
        
        //this is for to set filter value of item
        foreach($commodity as $key => $value){
            if($value->commodity=="ORES"){
                $data[$key]['Iron Ore'][] = 'India';
            }else{
                $data[$key][$value->commodity][] = 'India';
            }
        }
        //this is for to set all filter value
        if(count($commodity)>1){
            $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(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']);
        }
        $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);
        }
    }
    /********
    ** vessel lineup sailed graph
    * ******/
    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)
        $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']=="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($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;
                    }
                    
                }
            }
            // 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;
    }
}