<?php
namespace App\Http\Controllers\v2019;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Auth\User ;
use App\Http\Controllers\Auth\authentication;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Action\Common;

use App\Traits\ReferenceData;
use Session;
class LogisticsController extends Controller
{
	use ReferenceData {
        getReferenceData as protected refData;
    }
	
    public $appType = 'SM' ;
	public function __construct(Request $request) {
        if($request->has('debug') && $request->input('debug') == true) {
            $this->debug = true;			
        }
		if($request->has('appType') && $request->input('appType') == true) {
            $this->appType = $request->input('appType');		
        } elseif($request->has('filters')){
			 $filter = $request->input('filters');
			 if(!empty($filter['app_type'])){
					$this->appType = 'CM';
			 }
		} elseif($request->has('app_type') && $request->input('app_type') == true) {
            $this->appType = $request->input('app_type');		
        }
        elseif($request->has('selected_market') && $request->input('selected_market') =='coal') {            
                $this->appType = 'CM';		
        }
    }
	
	public function logisticSectionStatusForAPP(Request $request)
    {
		$resourceType = "app";
		if($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		$token='';
		if($request->has('accessToken')){
			$token = $request->input('accessToken');
			$auth = new authentication();
			$user_id = $auth->authToken($token,$this->appType);
		}
		//
		//$url = "http://50.116.49.118/theitbooth/logistics?deviceType=ios&mobile=1&accessToken=".$token;
		$url = BASEURL."logistics?deviceType=ios&mobile=1&accessToken=".$token;
		if($user_id){
			$user = new User($user_id, $this->appType);
			$isAccessible =  $user->isAccessible($resourceType, $section='Logistics');//Railway_Movement
			
			if ($isAccessible==-1) {
				$logisticsSectionStatus = "active";
			}else{
				$logisticsSectionStatus = "upgrade";
			}			
		}else{
			$logisticsSectionStatus = "upgrade";
		}
		
		
		$result['logisticsSectionStatus'] = $logisticsSectionStatus;
		$result['logisticsSectionUrl'] = $url;
		
		$code = 1001;
		$message = "Success";
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}
	
	public function getRailwayMovement(Request $request)
    {      
		
		$result = array();
		$resourceType = "web";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		if ($request->has('commodityID')){
			$commodityID = $request->input('commodityID');
			$filters['commodityID'] = $commodityID;
		}/* else{
			$code = 2014;
			$message = "Invalid! commodity missing.";			
			return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
		} */
		
		if ($request->has('regionID')){
			$regionID = $request->input('regionID');
			if(!empty($regionID))$filters['regionID'] = $regionID;
		}/*else{
			$code = 2014;
			$message = "Invalid! region missing.";			
			return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
		}*/		
		$onDate='';
		if ($request->has('onDate')){
			$onDate = strtolower($request->input('onDate'));
			$filters['onDate'] = $onDate;
		}
		
		if ($request->has('fromDate')){
			$fromDate = strtolower($request->input('fromDate'));
			$filters['fromDate'] = $fromDate;
		}
		
		if ($request->has('toDate')){
			$toDate = strtolower($request->input('toDate'));
			$filters['toDate'] = $toDate;
		}
		
		if ($request->has('division')){
			$filters['division'] = $request->input('division');
		}
		
		if ($request->has('unloadStn')){
			$filters['unloadStn'] = $request->input('unloadStn');
		}
		
		if ($request->has('loadStn')){
			$filters['loadStn'] = $request->input('loadStn');
		}
		
		if ($request->has('consignee')){
			$filters['consignee'] = $request->input('consignee');
		}
		
		if ($request->has('consignor')){
			$filters['consignor'] = $request->input('consignor');
		}
		
		if ($request->has('zone')){
			$filters['zone'] = $request->input('zone');
		}
		
		if ($request->has('commodity')){
			$filters['commodity'] = $request->input('commodity');
		}
		
		$isDownload=0;
		if ($request->has('isDownload')){
			$isDownload = $request->input('isDownload');			
		}
		
		$filters['isDownload'] = $isDownload;
		
		if ($request->has('debug')){
			$filters['debug'] = $request->input('debug');
		}
		$selected_market='';
		if ($request->has('selected_market')){
			$selected_market = $request->input('selected_market');
			$filters['selected_market'] = $selected_market;
		}
		
		$code = 1001;				
		$message = "Success";
		$accessType='pre';//for visitor/upgrade///		
		
		$user_id=0;
		
		$token='';///call from mobileapp as iframe
		//if($request->has('accessToken') && ($request->input('accessToken')!='null' || $request->input('accessToken')!=null)){
		if($request->has('accessToken') && $request->has('accessToken')!=''){
			$token = $request->input('accessToken');
			$auth = new authentication();
			$user_id = $auth->authToken($token,$this->appType);
			$resourceType='app';
		}elseif ($request->has('memid') && $request->input('memid')!=''){								
			
			//$memid = $request->input('memid');
			    $memid = htmlentities($request->input('memid'), ENT_QUOTES);

                if( !is_numeric($memid) ){
                    return new JsonResponse(array("status"=>array("code" => 2001, "message" => "Improper input values")));
                }

			
			$auth = new authentication();
			$user_id = $auth->authUserId($memid);//for web user
		}
		//echo "userid:" . $user_id; dd();
		//return $request->input('accessToken');
		///
		if (($request->has('memid') && $request->input('memid')!='') || $token!=''){
			
			if ($user_id) {
				
				if($user_id){
					$user = new User($user_id, $this->appType);
					///$isAccessible = $user->getTakenServicesByUser($item='prices', $at_place=$resourceType);
					$isAccessible =  $user->isAccessible($resourceType, $section='Logistics','','','',$selected_market);//Railway_Movement
					
					if ($isAccessible==-1) {
						$accessType = 'post';
					}else{
						$accessType = 'pre';
					}
				}
				
			}else{
				$code = 2013;
				$message = "Unauthorized action.";
				return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
			}
		}
		
		//if($isDownload==1 && $accessType=='pre'){
		if($accessType=='pre'){
		    if($isDownload==1){
    			$code = 2013;
    			$message = "Unauthorized action.";
    			return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
		    } else {
        		$result = \ApiQuery::getRailwayMovementRows( $user_id, $resourceType, $filters, $accessType, $this->appType);
		    }
		} else
		    $result = \ApiQuery::getRailwayMovementRows( $user_id, $resourceType, $filters, $accessType, $this->appType);
		
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    }  	
	
	public function getRailwayMovementFilter(Request $request)
    { 
		$result = array();
		$resourceType = "web";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		include_once "/home/steelmin/public_html/api/storage/v2019/Json/Web/RailwayMovementFilter.php";
		
		$code = 1001;
		$message = "Success";
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}
	
	public function getVesselLineupFilter(Request $request)
    { 
		$result = array();
		$resourceType = "web";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));

		if($request->has('filters') && !empty($request->input('filters'))){
		    $filters = $request->input('filters');
		    
			if( isset($filters['app_type']) && $filters['app_type'] == 'CM'){
				include_once "/home/steelmin/public_html/api/storage/v2019/Json/Web/VesselLineupFilter_CM.php";
			} else 
				include_once "/home/steelmin/public_html/api/storage/v2019/Json/Web/VesselLineupFilter.php";
		}elseif($this->appType=='CM'){
			include_once "/home/steelmin/public_html/api/storage/v2019/Json/Web/VesselLineupFilter_CM.php";
		} else 
			include_once "/home/steelmin/public_html/api/storage/v2019/Json/Web/VesselLineupFilter.php";
		
		$ids = array();
		$excel_upoad_date_time_arr = DB::table('tbl_logistic_data')->select("excel_upoad_date_time")->orderBY('id','desc')->first();
		
		$excel_upoad_date_time = $excel_upoad_date_time_arr["excel_upoad_date_time"];
		
		/*$tbl_logistic_arr = DB::table('tbl_logistic_data')->where("excel_upoad_date_time","=",$excel_upoad_date_time)->groupBy('selected_country')->groupBy('commodity')->get();
	
		
		$commodities=array();
		foreach($tbl_logistic_arr as $k=> $v){
			$result['region'][] = array(
									"isSelectable" => "y",
									"id" => $v['selected_country'],
									"name" => $v['selected_country']
								 );
			 
			$commodities[$v['commodity']] = $v['commodity'];
		}
		
		foreach($commodities as $k=> $v){
			$result['commodities'][] = array(
										"isSelectable" => "y",
										"id" => strtoupper($v),
										"name" => strtoupper($v)
									);
		}
		
		
		$result["statusFilterValues"] = array(
			"Anchorage","Berth", "Expected"
        );
		$result["movementFilterValues"] = array(
			"Import","Export","Coastal Load"
        );*/
		
		$result['excel_upoad_date_time'] = $excel_upoad_date_time;

		$code = 1001;
		$message = "Success";
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}
	
	public function getVesselLineup(Request $request)
    {      
		$result = array();
		$resourceType = "web";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		$filters= array();
		if ($request->has('commodity')){
			$commodity = $request->input('commodity');
			$filters['commodity'] = $commodity;
		}
		
		if ($request->has('region')){
			$region = $request->input('region');
			$filters['region'] = $region; 
		}
		 
		
		if ($request->has('subCommodity')){
			$filters['cargo'] = $request->input('subCommodity');  
		}elseif ($request->has('cargo')){
			$filters['cargo'] = $request->input('cargo');  
		}
		
		if ($request->has('vesselSize')){
			$filters['vesselSize'] = $request->input('vesselSize');
		}
		
		if ($request->has('loadCountry')){
			$filters['loadCountry'] = $request->input('loadCountry');
		}
		
		if ($request->has('unloadCountry')){
			$filters['unloadCountry'] = $request->input('unloadCountry');
		}
		
		if ($request->has('company')){
			$filters['company'] = $request->input('company');
		}
		
		if ($request->has('loadPort')){
			$filters['loadPort'] = $request->input('loadPort'); 
		}
		
		if ($request->has('unloadPort')){
			$filters['unloadPort'] = $request->input('unloadPort'); 
		}

		if ($request->has('source')){
			$filters['source'] = $request->input('source'); 
		}
		
		if ($request->has('status')){
			$status = $request->input('status');
			$filters['status'] = $status;
		}
		
		
		$isCoalmint=0;
		if ($request->has('isCoalmint')){
			$isCoalmint = $request->input('isCoalmint');
			$filters['isCoalmint'] = $isCoalmint; 
		}
		
		///$Movement='';
		if ($request->has('movement')){
			$movement = $request->input('movement');
			$filters['movement'] = $movement;
		}
		
		$isDownload=0;
		if ($request->has('isDownload')){
			$isDownload = $request->input('isDownload');			
		}
		
		$filters['isDownload'] = $isDownload;
		
		if ($request->has('debug')){
			$filters['debug'] = $request->input('debug');
		}
		
		$onDate='';
		if ($request->has('onDate')){
			$onDate = strtolower($request->input('onDate'));
			$filters['onDate'] = $onDate;
		}
		
		$fromDate='';
		if ($request->has('fromDate')){
			$fromDate = strtolower($request->input('fromDate'));
			$filters['fromDate'] = $fromDate;
		}
		
		if ($request->has('toDate')){
			$toDate = strtolower($request->input('toDate'));
			$filters['toDate'] = $toDate;
		}
		$selected_market='';
		if ($request->has('selected_market')){
			$selected_market = $request->input('selected_market');
			$filters['selected_market'] = $selected_market;
			if($selected_market=='coal')
                $this->appType = "CM";
		}
		
		/*if ($request->has('date_formate') && strtolower($filters['source'])=='sailed'){
			$dateFormate = strtolower($request->input('date_formate'));
			$filters['dateFormate'] = $dateFormate;
			$fdate = $request->input('from_date');
			$todate =  $request->input('to_date');
			if (!empty($fdate) && !empty($todate)){
                //added condition for from and to date 
                $from_date='';$to_date='';
                $qtr_arr['from'] = array("q1"=>1,"q2"=>4,"q3"=>7,"q4"=>10);
                $qtr_arr['to'] = array("q1"=>3,"q2"=>6,"q3"=>9,"q4"=>12);
                if($dateFormate=="yearly"){
                    $from_date = $fdate."-01-01";
                    $to_date = $todate."-12-31";
                }
                elseif($dateFormate=="quaterly"){
                    $from_tmp = explode(",",str_replace(" ","",$fdate));
                    $to_tmp = explode(",",str_replace(" ","",$todate));
                    $from_date = $from_tmp[1]."-".$qtr_arr['from'][$from_tmp[0]]."-01";
                    $to_date = date("Y-m-t",strtotime($to_tmp[1]."-".$qtr_arr['to'][$to_tmp[0]]."-01"));                       
                }
                elseif($dateFormate=="monthly"){
                    $from_date = date("Y-m-01",strtotime($fdate));
                    $to_date = date("Y-m-t",strtotime($todate));                        
                }
                elseif($dateFormate=="date"){
                    $from_date = date("Y-m-d",strtotime($fdate));
                    $to_date = date("Y-m-d",strtotime($todate));
                }
                $filters['toDate']=$to_date;
                $filters['fromDate']=$from_date;
            }
		}*/
		/* if($request->has('filters') && !empty($request->input('filters'))){
		    //echo "yes";
		    $filters = $request->input('filters');
		   // print_r($filters);
			if( isset($filters['app_type']) && $filters['app_type'] == 'CM')
				$this->appType = "CM";
		} */
		
		
		$code = 1001;				
		$message = "Success";
		$accessType='pre';//for visitor/upgrade///		
		$user_id=0;
		$token='';///call from mobileapp as iframe
		if($request->has('accessToken')){
			$token = $request->input('accessToken');
			$auth = new authentication();
			$user_id = $auth->authToken($token,$this->appType);
		}elseif ($request->has('memid') && $request->input('memid')!=''){								
			
			//$memid = $request->input('memid');
			    $memid = htmlentities($request->input('memid'), ENT_QUOTES);

                if( !is_numeric($memid) ){
                    return new JsonResponse(array("status"=>array("code" => 2001, "message" => "Improper input values")));
                }
			
			$auth = new authentication();
			$user_id = $auth->authUserId($memid);//for web user
		}
		///
		if (($request->has('memid') && $request->input('memid')!='') || ($request->has('accessToken') && $request->input('accessToken')!='')){	
			if ($user_id) {
				
				if($user_id){
					$user = new User($user_id, $this->appType);
					///$isAccessible = $user->getTakenServicesByUser($item='prices', $at_place=$resourceType);
					$isAccessible =  $user->isAccessible($resourceType, $section='Logistics','','','',$selected_market);
					
					if ($isAccessible==-1 || $isCoalmint==1) {
						$accessType = 'post';
					}else{
						$accessType = 'pre';
					}
				}
				
			}else{
				$code = 2013;
				$message = "Unauthorized action.";
				return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
			}
		}
		
		if($isDownload==1 && $accessType=='pre'){
			$code = 2013;
			$message = "Unauthorized action.";
			return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
		}
		
		$result = \ApiQuery::getVesselLineupRows( $user_id, $resourceType, $filters, $accessType, $this->appType);
		
		if( count($result) == 0)
		    $message = "No Record Found.";
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    }  	
	
	public function getVesselFreightFilter(Request $request)
    { 
		$result = array();
		$resourceType = "web";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		if($request->has('filters') && !empty($request->input('filters'))){
		    $filters = $request->input('filters');
		    
			if( isset($filters['app_type']) && $filters['app_type'] == 'CM'){
				
				$tbl_freight_arr = DB::table('tbl_exim_master')->select('f_grade', 'load_country', 'unload_country')->where("category","=",'Logistics')->where("commodity","=",'coal')->groupBy('f_grade')->groupBy('load_country')->groupBy('unload_country')->orderBy('f_grade')->orderBy('load_country')->orderBy('unload_country')->get();
				
				foreach($tbl_freight_arr as $arr):
					$result['All']['All'][$arr['unload_country']]=$arr['unload_country'];
					$result['All'][$arr['load_country']][$arr['unload_country']]=$arr['unload_country'];
					
					$result[$arr['f_grade']]['All'][$arr['unload_country']]=$arr['unload_country'];
					
					$result[$arr['f_grade']][$arr['load_country']][$arr['unload_country']]=$arr['unload_country'];
				endforeach;
				///include_once "/home/steelmin/public_html/api/storage/v2019/Json/Web/VesselFreightFilter_CM.php";
			} else 
				include_once "/home/steelmin/public_html/api/storage/v2019/Json/Web/VesselFreightFilter.php";
		}elseif($this->appType=='CM'){
			include_once "/home/steelmin/public_html/api/storage/v2019/Json/Web/VesselFreightFilter_CM.php";
		}  else 
			include_once "/home/steelmin/public_html/api/storage/v2019/Json/Web/VesselFreightFilter.php";




//		include_once "/home/steelmin/public_html/api/storage/v2019/Json/Web/VesselFreightFilter.php";
		
		$code = 1001;
		$message = "Success";
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
	}
	
	public function getVesselFreight(Request $request)
    {      
		$result = array();
		$resourceType = "web";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		$filters= array();
		if ($request->has('commodity')){
			$commodity = $request->input('commodity');
			$filters['commodity'] = $commodity;
		}
		
		if ($request->has('vesselSize')){
			$vesselSize = $request->input('vesselSize');
			$filters['vesselSize'] = $vesselSize;
		}
		if ($request->has('source')){
			$source = $request->input('source');
			$filters['source'] = $source;
		}
		/* if ($request->has('regionID')){
			$regionID = $request->input('regionID');
			$filters['regionID'] = $regionID;
		} */
		
		///$Movement='';
		if ($request->has('loadCountry')){
			$loadCountry = $request->input('loadCountry');
			$filters['loadCountry'] = $loadCountry;
		}
		
		if ($request->has('unLoadCountry')){
			$unLoadCountry = $request->input('unLoadCountry');
			$filters['unLoadCountry'] = $unLoadCountry;
		}
		
		$isDownload=0;
		if ($request->has('isDownload')){
			$isDownload = $request->input('isDownload');			
		}
		
		$filters['isDownload'] = $isDownload;
		
		if ($request->has('debug')){
			$filters['debug'] = $request->input('debug');
		}
		if ($request->has('selected_market')){
			$selected_market = $request->input('selected_market');
			$filters['selected_market'] = $selected_market;
		}
		
		/*
		if($request->has('filters') && !empty($request->input('filters'))){
		    $filters = $request->input('filters');
			if( isset($filters['app_type']) && $filters['app_type'] == 'CM')
				$this->appType = "CM";
		}*/

		
		$code = 1001;				
		$message = "Success";
		$accessType='pre';//for visitor/upgrade///		
		$user_id=0;
		$token='';///call from mobileapp as iframe
		if($request->has('accessToken')){
			$token = $request->input('accessToken');
			$auth = new authentication();
			$user_id = $auth->authToken($token,$this->appType);
		}elseif ($request->has('memid') && $request->input('memid')!=''){								
			
			//$memid = $request->input('memid');
			    $memid = htmlentities($request->input('memid'), ENT_QUOTES);

                if( !is_numeric($memid) ){
                    return new JsonResponse(array("status"=>array("code" => 2001, "message" => "Improper input values")));
                }
			
			$auth = new authentication();
			$user_id = $auth->authUserId($memid);//for web user
		}
		//
		if (($request->has('memid') && $request->input('memid')!='') || ($request->has('accessToken') && $request->input('accessToken')!='')){		
			if ($user_id) {
				
				if($user_id){
					$user = new User($user_id, $this->appType);
					///$isAccessible = $user->getTakenServicesByUser($item='prices', $at_place=$resourceType);
					$isAccessible =  $user->isAccessible($resourceType, $section='Logistics','','','',$selected_market);
					
					if ($isAccessible==-1) {
						$accessType = 'post';
					}else{
						$accessType = 'pre';
					}
				}
				
			}else{
				$code = 2013;
				$message = "Unauthorized action.";
				return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
			}
		}
		
		if($isDownload==1 && $accessType=='pre'){
			$code = 2013;
			$message = "Unauthorized action.";
			return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
		}
		
		$result = \ApiQuery::getVesselFreightRows( $user_id, $resourceType, $filters, $accessType, $this->appType);
		
		if( count($result) == 0)
		    $message = "No Record Found.";
		
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    }
	
	public function getVesselFreightGraph(Request $request)
    {      
		$result = array();
	    $resourceType = "web";		
		if ($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));		
		
		if ($request->has('itemID')) {
            $relation_id = $request->input('itemID') ;
        }else{
			return new JsonResponse(array("status"=>array("code" => 2100, "message" => "Invalid ItemId.")));
		}
		
		$user_id=0;
		if ($resourceType=='app' && $request->has('accessToken') && $request->input('accessToken')!='') {
			//$uemail = $request->input('email');//oldPassword
			$token = $request->input('accessToken');
			$user = new User();
			$auth = new authentication();
			$user_id = $auth->authToken($token,$this->appType);
			if(!$user_id)
				return new JsonResponse(array("status"=>array("code" => 2100, "message" => "Unauthorized action.")));
		}elseif ($resourceType=='web' && $request->has('memid') && $request->input('memid')!=''){
								
			//$memid = $request->input('memid');
			    $memid = htmlentities($request->input('memid'), ENT_QUOTES);

                if( !is_numeric($memid) ){
                    return new JsonResponse(array("status"=>array("code" => 2001, "message" => "Improper input values")));
                }
			
			$auth = new authentication();
			$user_id = $auth->authUserId($memid);//for web user
			if(!$user_id)
				return new JsonResponse(array("status"=>array("code" => 2100, "message" => "Unauthorized action.")));
		}
		if ($request->has('selected_market')){
			$selected_market = $request->input('selected_market');			
		}
		if($user_id){
			$user = new User($user_id, $this->appType);
			///$isAccessible = $user->getTakenServicesByUser($item='prices', $at_place=$resourceType);
			$isAccessible =  $user->isAccessible($resourceType, $section='Logistics','','','',$selected_market);//Railway_Movement
			
			if ($isAccessible==-1) {
				$randNum = 1;
			}else{
				$randNum = mt_rand(111, 111111) / 100;
			}
		}

        if( $request->has('assesment')) {
            $assesment = $request->input('assesment') ;
        }
		
		if( $request->has('currencyName')) {
            $currency = $request->input('currencyName') ;
        }
		
		$requiredfor='';
		$_logStartDate='';
		if( $request->has('requiredFor')) {
            $requiredfor = $request->input('requiredFor') ;
        }
		
		$dateFilter = array();
		
		if($request->has('fromDate') && !empty($request->input('fromDate'))){
		    $fromDate = $request->input('fromDate');
			$dateFilter['fromDate'] = $fromDate;			
		}
		
		if($request->has('toDate') && !empty($request->input('toDate'))){
		    $toDate = $request->input('toDate');
			$dateFilter['toDate'] = $toDate;			
		}
		
		$assessmentType ='';
		if($request->has('assessmentType') && !empty($request->input('assessmentType'))){
		    $assessmentType = $request->input('assessmentType');	
			$dateFilter['assessmentType'] = $assessmentType;			
		}
		
		$code = 1001;
		$message = "Success";
		$result =  \ApiQuery::getVesselFreightPricesByWeeksForGraph($relation_id, $randNum, $dateFilter);
		
		$res = array("status"=>array("code" => $code, "message" => $message), "data"=> $result);
		
	    return new JsonResponse($res);
    }
	
	
	public function getCilAuctionData(Request $request){
		
		$resourceType = "app";
		if($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		if($request->has('memid')){
			//$memid = $request->input('memid');
			    $memid = htmlentities($request->input('memid'), ENT_QUOTES);

                if( !is_numeric($memid) ){
                    return new JsonResponse(array("status"=>array("code" => 2001, "message" => "Improper input values")));
                }
			
			//$memid = _encrypt_decrypt($action='decrypt', $memid);
			$auth = new authentication();
			$user_id = $auth->authUserId($memid);//for web user
		}elseif($request->has('accessToken')){
			$token = $request->input('accessToken');
			$auth = new authentication();
			$user_id = $auth->authToken($token,$this->appType);
		}
		///echo $user_id.$this->appType;
		$userService='';
		if($user_id){
			$user = new User($user_id, $this->appType);
			$isAccessible =  $user->isAccessible($resourceType, $section='cilauction');//cilauction
			
			if ($isAccessible==-1) {
				$sessionata = session()->all();
				$userService = $sessionata['userService'];
				if($userService=='Platinum'){
					$excelLink = "public/auction/platinum.zip";
				}elseif($userService=='Platinum+'){
					$excelLink = "public/auction/platinum_plus.zip";
				} 
				///$logisticsSectionStatus = "active";
			}else{
				///$logisticsSectionStatus = "upgrade";
				$code = 2013;
				$message = "Unauthorized action. Upgrade your plan to see this section";
				return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
			}			
		}else{
			//$logisticsSectionStatus = "upgrade";
				$code = 2013;
				$message = "Unauthorized action.";
				return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
		}
		//$fromDate = date('Y-m-d',strtotime(Session::get('sectionFromDate')));
		//$toDate = date('Y-m-31',strtotime(Session::get('sectionTodate')));	
        $flag = 0;
		
//		$fromDate = date('Y-m-d',strtotime(Session::get('sectionFromDate')));
//		$toDate = date('Y-m-31',strtotime(Session::get('sectionTodate')));	
		if($request->has('filters') && !empty($request->input('filters'))){
		    $filters = $request->input('filters');
    		if( isset($filters['f_month']) && isset($filters['f_year']) ){
        		$dbresult = DB::table('tbl_cil_auction')
        				->select('*' )		
        				->where('month', '=', $filters['f_month'])
        				->where('year', '=', $filters['f_year'])
        				->get();
    		} else
    		    $flag = 1;
		} else 
		    $flag = 1;
		$selMnt = '';
		$selYr = '';
		if( $flag == 1 ){
            $dbresult = DB::table('tbl_cil_auction')
        				->select('month', 'year' )		
        				->orderBy('id', 'desc')
        				->first();
        //	print_r($dbresult);			
			$selMnt = $dbresult['month'];
			$selYr = $dbresult['year'];
        	
    		$dbresult = DB::table('tbl_cil_auction')
    				->select('*' )		
    				->where('month', '=', $dbresult['month'])
    				->where('year', '=', $dbresult['year'])
    				->get();
		}
		/* foreach($dbresult as $cType){
			$result[$cType["id"]] = $cType["name"];
		} */
	    
		return new JsonResponse(array("status"=>array("code" => 1001, "message" => "success"), "data"=> $dbresult, "excelLink"=> $excelLink, "selMnt"=> $selMnt, "selYr"=> $selYr));
    }
}
