<?php
session_start();
include_once("../../includes/functions_internal_user_dryin.inc.php");
@$i = $_REQUEST['id'];
switch ($i)
{
   
	case 1:
        $ret = checkDuplicateVendorName();
				break;
	case 2:
        $ret = checkDuplicateVendorCode();
				break;
	case 3:
        $ret = checkDuplicateVendorShortName();
				break;
	case 4:
        $ret = checkPortStatus();
				break;
	case 5:
        $ret = getCOAData();
				break;
    case 6:
        $ret = checkDuplicateOpenCargo();
				break;
    case 7:
        $ret = checkDistance();
				break;
	case 8:
        $ret = checkDuplicateNomID();
				break;
	default:
		    $ret = "";
				//echo $ret;
				break; 				
}

function checkDuplicateNomID()
{
	$sug 		= new clsSettings();
	$con 		= $sug->funConnect();
	$nomid 		= $_REQUEST['txtNomID'];
	$query 		= mysql_query("select * from mapping_master where NOM_NAME= '".$nomid."'");
	$num		= mysql_num_rows($query);
	if($num > 0)
	{
		echo 'false';
	}
	else
	{
		echo 'true';
	}
}

function checkDistance()
{
	$obj = new data();
	$connect=$obj->funConnect();
	$selFPort = $_REQUEST['selFPort'];
	$selTPort = $_REQUEST['selTPort'];
	$selDType = $_REQUEST['selDType'];
	if($selDType == 1){$field = "Short_dis";}
	if($selDType == 2){$field = "nm250_dis";}
	if($selDType == 3){$field = "nm600_dis";}
	if($selDType == 4){$field = "Long_dis";}
	
	$sql = "select ".$field." from DISTANCE_MASTER where FromPortId=".$selFPort." and ToPortId=".$selTPort;
	$result = mysql_query($sql);
	$rec = mysql_num_rows($result);
	if($rec == 0)
	{
		$sql1 = "select ".$field." from DISTANCE_MASTER where FromPortId=".$selTPort." and ToPortId=".$selFPort;
		$result1 = mysql_query($sql1);
		$rec1 = mysql_num_rows($result1);
		if($rec1 == 0)
		{
			echo '';
		}
		else
		{
			$rows1 = mysql_fetch_assoc($result1);
			echo $rows1[$field];
		}
	}
	else
	{
		$rows = mysql_fetch_assoc($result);
		echo $rows[$field];
	}
}

function checkDuplicateOpenCargo()
{
$sug = new clsSettings();
$sug->funConnect();
$contract_id = $_REQUEST['txtCID'];
$query = mysql_query("select * from open_cargo_master where MODULEID='".$_SESSION['moduleid']."' AND MCOMPANYID='".$_SESSION['company']."' and CONTRACTID= '".$contract_id."'");
$num=mysql_num_rows($query);
	if($num > 0)
	{
	echo 'false';
	}
	else
	{
	echo 'true';
	}
}

function getCOAData()
{
	$obj = new data();
	$connect=$obj->funConnect();
	$selActive1 = $_REQUEST['selActive'];
	$txtCOADate = $_REQUEST['txtCOADate'];
	$selOwner = $_REQUEST['selOwner'];
	
	if($_REQUEST['selActive'] == 3){$selActive = 1;}else{$selActive = $_REQUEST['selActive'];}
	
	//$sql = "select * from coa_master WHERE MODULEID='".$_SESSION['moduleid']."' AND MCOMPANYID='".$_SESSION['company']."' and STATUS='".$selActive."' order by COA_DATE desc";
	$sql = "SELECT * from coa_master where MODULEID='".$_SESSION['moduleid']."' AND MCOMPANYID='".$_SESSION['company']."' and STATUS='".$selActive."' ";
	if($_REQUEST['txtCOADate'] != "")
	{
	$sql.= " and COA_DATE = '".date("Y-m-d",strtotime($_REQUEST['txtCOADate']))."'  ";
	}
	if($_REQUEST['selOwner'] != "")
	{
	$sql.= " and OWNER = '".$_REQUEST['selOwner']."'  ";
	}
	$sql.="order by COA_DATE desc";
	$res = mysql_query($sql);
	$rec = mysql_num_rows($res);
	$i = 1;
	echo '<table id="coa_list" class="table table-bordered table-striped">
		<thead>
			<tr>
				<th width="13%">COA CP Date</th>
				<th width="13%">COA Number</th>
				<th width="13%">Cargo/Material Type</th>
				<th width="13%">Vessel Category</th>
				<th width="12%">No. of Shipments</th>
				<th width="12%">Performed</th>
				<th width="12%">Owner</th>
				<th width="12%">Details</th>
				
			</tr>
		</thead>
		<tbody>';
	if($rec == 0)
	{
		echo '<tbody>';
		echo '</tbody>';
	}
	else{
		while($rows = mysql_fetch_assoc($res))
			{
				if($selActive1 == 3)
				{
					if((int)$rows['NO_OF_SHIPMENT'] == (int)$obj->getPerformedVesselRec($rows['COAID']))
					{
						echo '<tr>';
						echo '<td>'.date("d-m-Y",strtotime($rows['COA_DATE'])).'</td>';
						echo '<td>'.$rows['COA_NO'].'</td>';
						echo '<td>'.$obj->getCargoNameBasedOnId($rows['CARGOTYPEID']).'</td>';
						echo '<td>'.$obj->getVesselCategoryTypeBasedOnId($rows['VESSEL_CATEGORY_ID']).'</td>';
						echo '<td>'.$rows['NO_OF_SHIPMENT'].'</td>';
						echo '<td>'.$obj->getPerformedVesselRec($rows['COAID']).'</td>';
						echo '<td>'.$obj->getVendorListNewBasedOnID($rows['OWNER']).' ('.$rows['OWNER'].')</td>';
						if($rows['STATUS'] == 1)
						{
							echo '<td align="center">';
							if($obj->getPerformedVesselRec($rows['COAID']) == 0)
							{
								echo '<a href="cancel_coa.php?id='.$rows['COAID'].'" title="Cancel COA"><i class="fa fa-times" style="color:red;"></i></a>&nbsp;|&nbsp;';
							}
							echo '<a href="monthly_coa_remarks.php?id='.$rows['COAID'].'" title="Monthly Remarks" ><i class="fa fa-edit "></i></a>&nbsp;|&nbsp;<a href="updatecoa.php?id='.$rows['COAID'].'" title="Edit Details" ><i class="fa fa-edit "></i></a></td>';	
						}
						else
						{
							echo '<td>';
							echo $rows['CANCEL_REMARKS'];
							echo '</td>';	
						}
						echo '</tr>';
						$i++;
					}
				}
				else
				{
					if((int)$rows['NO_OF_SHIPMENT'] != (int)$obj->getPerformedVesselRec($rows['COAID']))
					{
						echo '<tr>';
						echo '<td>'.date("d-m-Y",strtotime($rows['COA_DATE'])).'</td>';
						echo '<td>'.$rows['COA_NO'].'</td>';
						echo '<td>'.$obj->getCargoNameBasedOnId($rows['CARGOTYPEID']).'</td>';
						echo '<td>'.$obj->getVesselCategoryTypeBasedOnId($rows['VESSEL_CATEGORY_ID']).'</td>';
						echo '<td>'.$rows['NO_OF_SHIPMENT'].'</td>';
						echo '<td>'.$obj->getPerformedVesselRec($rows['COAID']).'</td>';
						echo '<td>'.$obj->getVendorListNewBasedOnID($rows['OWNER']).' ('.$rows['OWNER'].')</td>';
						if($rows['STATUS'] == 1)
						{
							echo '<td align="center">';
							if($obj->getPerformedVesselRec($rows['COAID']) == 0)
							{
								echo '<a href="cancel_coa.php?id='.$rows['COAID'].'" title="Cancel COA"><i class="fa fa-times" style="color:red;"></i></a>&nbsp;|&nbsp;';
							}
							echo '<a href="monthly_coa_remarks.php?id='.$rows['COAID'].'" title="Monthly Remarks"><i class="fa fa-edit "></i></a>&nbsp;|&nbsp;<a href="updatecoa.php?id='.$rows['COAID'].'" title="Edit Details" ><i class="fa fa-edit "></i></a></td>';	
						}
						else
						{
							echo '<td>';
							echo $rows['CANCEL_REMARKS'];
							echo '</td>';	
						}
						echo '</tr>';
						$i++;
					}
				}
		}
	}
	echo '</tbody>
		</table>';
}



function checkPortStatus()
{
	$obj = new data();
	$connect=$obj->funConnect();
	//$selDPort = $_REQUEST['selDPort'];
	$selTerminal = $_REQUEST['selTerminal'];
	$selCargo = explode(",",$_REQUEST['selCargo']);
	
	$query = "select * from port_information_master where PORTID='".$selCargo[1]."' and CARGOID='".$selCargo[0]."' and TERMINALID='".$selTerminal."' AND MODULEID='".$_SESSION['moduleid']."' AND MCOMPANYID='".$_SESSION['company']."' AND STATUS=1";
	$result = mysql_query($query) ;
	$num = mysql_num_rows($result);
	
		if($num > 0)
		{
		echo 'false';
		}
		else
		{
		echo 'true';
		}
	
}

function checkDuplicateVendorShortName()
{
	$sug = new clsSettings();
	$sug->funConnect();
	$s_name = trim(ereg_replace(' +',' ',$_REQUEST['txtVSName']));
	$query = mysql_query("select * from vendor_master where STATUS=1 and SHORT_NAME= '".$s_name."'");
	$num=mysql_num_rows($query);
	if($num > 0)
	{
	echo 'false';
	}
	else
	{
	echo 'true';
	}
}

function checkDuplicateVendorCode()
{
	$sug = new clsSettings();
	$sug->funConnect();
	$code = trim(ereg_replace(' +',' ',$_REQUEST['txtVCode'])); 
	$query = mysql_query("select * from vendor_master where STATUS=1 and CODE= '".$code."'");
	$num=mysql_num_rows($query);
	if($num > 0)
	{
	echo 'false';
	}
	else
	{
	echo 'true';
	}
}

function checkDuplicateVendorName()
{
	$sug = new clsSettings();
	$sug->funConnect();
	$name = trim(ereg_replace(' +',' ',$_REQUEST['txtVName']));
	$query = mysql_query("select * from vendor_master where STATUS=1 and NAME= '".$name."'");
	$num=mysql_num_rows($query);
	if($num > 0)
	{
	echo 'false';
	}
	else
	{
	echo 'true';
	}
}


?>