<?php 
session_start();
require_once("../../includes/display_internal_user_dryout.inc.php");
require_once("../../includes/functions_internal_user_dryout.inc.php");
$obj = new data();
$connect = $obj->funConnect();
$display = new display();
$display->logout_iu();
$pagename = basename($_SERVER['PHP_SELF']);
$msg = NULL;
if (isset($_REQUEST['selBType']) && $_REQUEST['selBType']!="")
{
	$selBType = $_REQUEST['selBType'];
}
else if(isset($_SESSION['selBType']))
{
	$selBType = $_SESSION['selBType'];
}
else
{
	$selBType = '';
}


function url(){
    if(isset($_SERVER['HTTPS'])){
        $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
    }
    else{
        $protocol = 'http';
    }
    
    $act_or_not=$protocol=='http'?'/act':'';
    return $protocol . "://" . $_SERVER['HTTP_HOST'] .''.$act_or_not ;
}


?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php $display->title(); ?></title>
<?php $display->favicon(); ?>
<meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
<?php $display->css(); ?>
<link href="../../css/datatables/dataTables.bootstrap.css" rel="stylesheet" type="text/css" />
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
  <script src="../js/html5shiv.js"></script>
  <script src="../js/respond.min.js"></script>
<![endif]-->
</head>
    <body class="skin-blue fixed">
        <!-- header logo: style can be found in header.less -->
        <?php $display->header_tag(); ?>
		<div class="wrapper row-offcanvas row-offcanvas-left">
            <?php $display->leftmenu(7,1); ?>
			<aside class="right-side">                
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>
                        <a href="#" class="navbar-btn sidebar-toggle" data-toggle="offcanvas" role="button" style="margin:0px;"><i class="fa fa-bars" style="font-size:20px;"></i></a>&nbsp;&nbsp;Reports&nbsp;&nbsp;&nbsp;&nbsp;<small><div class="headline text-center" id="time" style="color:#000; font-weight:bold; font-size:12px;"></div></small>
                    </h1>
                    <ol class="breadcrumb">
                         <li><a href="../"><i class="fa fa-home"></i> Home</a></li>
                         <li>Reports</li>
						 <li>Chartering</li>
						 <li class="active">Laytime Audit Report</li>
                    </ol>
                </section>

                <!-- Main content -->
                <section class="content">
				<!--   content put here..................-->
				<div class="box box-primary">
				<h3 style=" text-align:center;">Laytime Audit Report  </h3>

                <div class="row invoice-info cp_date_form" style="margin-left:10px;">

                
                        <div class="col-sm-3 invoice-col">
						Date From<span style="font-size:10px; font-style:italic;">  (CP Date)</span>
							<address>
								<input type="text" name="txtFrom_Date" id="txtFrom_Date" class="form-control" value="" placeholder="dd-mm-yyyy">
							 </address>
						</div>
						
						<div class="col-sm-3 invoice-col">
						Date To<span style="font-size:10px; font-style:italic;">  (CP Date)</span>
							<address>
								<input type="text" name="txtTo_Date" id="txtTo_Date" class="form-control" value="" placeholder="dd-mm-yyyy">
							 </address>
						</div>
                        
                        <div class="col-sm-3 invoice-col">
						&nbsp;
                            <address>
                              <button type="button" class="btn btn-primary btn-flat" onClick="getData();" >Search</button>
                            </address>
                        </div><!-- /.col -->
					</div>
				<form name="frm1" id="frm1" enctype="multipart/form-data" action="<?php echo $pagename;?>"/>
					<div class="row invoice-info" style="margin-left:10px;">
                        
					</div>
					<div align="right">
                        <a href="#" title="Pdf" onClick="getExcel();"><button class="btn btn-default" style="margin-right: 5px;" type="button"><i class="fa fa-download"></i>Generate Excel</button></a>
					</div>
					<div style="height:10px;">
						<input name="txttblid" id="txttblid" type="hidden" value="" />
						<input name="txttblstatus" id="txttblstatus" type="hidden" value="" />
						<input type="hidden" name="action" value="submit" />
					</div>
                    
					<div id="DivOwner" class="box-body table-responsive" style="overflow:auto;">
						<table id="sf_list" class="table table-bordered table-striped dataTable">
							<thead>
							    <tr>
                                    <th rowspan="2" align="left" valign="top" width="10%">SR NO</th>
                                    <th rowspan="2" align="left" valign="top" width="10%">VESSEL NAME</th>
                                    <th rowspan="2" align="left" valign="top" width="10%">VOY NO</th>
                                    <th rowspan="2" align="left" valign="top" width="10%">VESSEL- REDELIVERY DATE</th>
                                    <th rowspan="2" align="left" valign="top" width="10%">PORT</th>
                                    <th colspan="2" align="left" valign="top" width="10%">APPROVER -1 [MAKER]</th>
                                    <th colspan="4" align="left" valign="top" width="10%">APPROVER -2 [AUDITOR]</th>
                                    <th colspan="3" align="left" valign="top" width="10%"> CHARTERERS</th>
                                    <th colspan="2" align="left" valign="top" width="10%">COMPARISION BETWEEN APPROVER 1 & 2</th>
                                    <th colspan="2" align="left" valign="top" width="10%">COMPARISION BETWEEN APPROVER 2 [AUDITOR] & CHARTERER</th>                                    
                                 </tr>
                                <tr>
                                        <th  align="left" valign="top" width="10%">COMPUTATION [A]</th>
                                        <th  align="left" valign="top" width="10%">COMPUTATION AMT</th>
                                        <th  align="left" valign="top" width="10%">COMPUTATION [B]</th>
                                        <th  align="left" valign="top" width="10%">COMPUTATION AMT</th>
                                        <th  align="left" valign="top" width="10%">EFFECT DUE TO MISTAKE [ON BASIS OF A & B]</th>
                                        <th  align="left" valign="top" width="10%">AUDITORS REMARKS</th>
                                        <th  align="left" valign="top" width="10%">COMPUTATION</th>
                                        <th  align="left" valign="top" width="10%">AMT CONFIRMED</th>
                                        <th  align="left" valign="top" width="10%">EFFECTED DUE TO MISTAKE MADE</th>
                                        <th  align="left" valign="top" width="10%">MONETARY EFFECT DUE TO INCORRECT COMPUTATION - AMOUNT</th>
                                        <th  align="left" valign="top" width="10%">FINAL EFFECT</th>
                                        <th  align="left" valign="top" width="10%">COMPUTATION</th>
                                        <th  align="left" valign="top" width="10%">AMOUNT</th>                                        
                                </tr>

                           
							</thead>
							<tbody>
								

                                <?php                    
                                $sql_q=mysql_query("SELECT DISTINCT LAYTIME_ID,  laytime_master.COMID,laytime_master.PORTID,laytime_master.TTL_DEMURRAGE,laytime_master.TTL_DESPATCH,freight_cost_estimete_master.TRANS_DATE as TRANS_DATE  from laytime_master inner join freight_cost_estimete_master on freight_cost_estimete_master.COMID=laytime_master.COMID WHERE   freight_cost_estimete_master.TRANS_DATE BETWEEN '".$_GET['CP_DATE_TO']."' AND '".$_GET['CP_DATE_FROM']."'    order by LAYTIME_ID   DESC");                            
                                $i=0;
                                while($rows=mysql_fetch_assoc($sql_q)){
                                       
                                        $comid=$rows['COMID'];
                                      $vessel_name=  $obj->getVesselIMOData($obj->getCompareEstimateData($comid,"VESSEL_IMO_ID"),"VESSEL_NAME");
                                     $voyage_no= $obj->getCompareEstimateData($rows['COMID'],"VOYAGE_NO");
                                     $port_name=strstr($obj->getPortNameBasedOnID($rows['PORTID']), '/', true);

                                     $dem_des= (int) $rows['TTL_DEMURRAGE']>0?'DEMURRAGE':'DESPATCH';
                                     $dem_des_amt=(int) $rows['TTL_DEMURRAGE']>0?$rows['TTL_DEMURRAGE']:$rows['TTL_DESPATCH'];
                                

                                    $app1_data=mysql_fetch_assoc(mysql_query("SELECT * FROM `laytime_master_app1` WHERE LAYTIME_ID = '".$rows['LAYTIME_ID']."'  AND PORTID='".$rows['PORTID']."'"));
                                    if($app1_data['LAYTIME_ID'] == ''){
                                        $app1_dem_des='';
                                        $app1_dem_des_amt='';
                                    }else{
                                        
                                        $app1_dem_des= (int) $app1_data['TTL_DEMURRAGE']>0?'DEMURRAGE':'DESPATCH';
                                        $app1_dem_des_amt=(int) $app1_data['TTL_DEMURRAGE']>0?$app1_data['TTL_DEMURRAGE']:$app1_data['TTL_DESPATCH'];
                                    }
                            
                                    $app2_data=mysql_fetch_assoc(mysql_query("SELECT * FROM `laytime_master_audit` WHERE LAYTIME_ID = '".$rows['LAYTIME_ID']."'  AND PORTID='".$rows['PORTID']."'"));
                                    if($app2_data['LAYTIME_ID'] == ''){
                                        $app2_dem_des='';
                                        $app2_dem_des_amt='';

                                        
                                    }else{
                                        
                                        $app2_dem_des= (int) $app2_data['TTL_DEMURRAGE']>0?'DEMURRAGE':'DESPATCH';
                                        $app2_dem_des_amt=(int) $app2_data['TTL_DEMURRAGE']>0?$app2_data['TTL_DEMURRAGE']:$app2_data['TTL_DESPATCH'];                                
                                        
                                    }
                                    $des_dem_diff='';
                                    $des_dem_f_diff='';
                                    $des_dem_mistake="";
                                    if ($app1_data['LAYTIME_ID'] != '' && $app2_data['LAYTIME_ID']  != '') {

                                 
                                                $des_dem_diff= (float) ($app2_dem_des_amt -  $app1_dem_des_amt);
                                               

                                                if($des_dem_diff>0 && $app1_dem_des == 'DEMURRAGE' && $app2_dem_des == 'DEMURRAGE'){
                                                    $des_dem_mistake = 'DEMURRAGE UNDERCALCULATED';
                                                }
                                                
                                                if(($des_dem_diff<0) && $app1_dem_des == 'DEMURRAGE' && $app2_dem_des == 'DEMURRAGE'){
                                                    $des_dem_mistake = 'DEMURRAGE OVERCALCULATED';
                                                }
                                                if(($des_dem_diff>0) && $app1_dem_des == 'DESPATCH' && $app2_dem_des == 'DESPATCH'){
                                                    $des_dem_mistake = 'DESPATCH UNDERCALCULATED';

                                                }
                                                
                                                if(($des_dem_diff<0) && $app1_dem_des == 'DESPATCH' && $app2_dem_des == 'DESPATCH'){

                                                    $des_dem_mistake = 'DESPATCH OVERCALCULATED';
                                                }
                                                 if($app2_dem_des =='DEMURRAGE' && $app1_dem_des =='DESPATCH'){
                                                    $des_dem_mistake = 'CALCULATED DEMM INSTEAD OF DESPATCH';
                                                }
                                                 if($app2_dem_des =='DESPATCH' && $app1_dem_des =='DEMURRAGE'){
                                                    $des_dem_mistake = 'CALCULATED DESPATCH INSTEAD OF DEMM';
                                                }
                                               
                                 
                                            }
                                              $des_dem_f_diff=""  ;
                                              $final_effect=""  ;

                                            //   $dem_des= (int) $rows['TTL_DEMURRAGE']>0?'DEMURRAGE':'DESPATCH';
                                    //  $dem_des_amt=(int) $rows['TTL_DEMURRAGE']>0?$rows['TTL_DEMURRAGE']:$rows['TTL_DESPATCH'];
                                            if($app2_data['LAYTIME_ID']  != '') {
                                               
                                                $des_dem_f_diff = (float) ($dem_des_amt -  $app2_dem_des_amt);

                                                if($des_dem_f_diff >0 && $dem_des == 'DEMURRAGE' && $app2_dem_des == 'DEMURRAGE'){
                                                    $des_dem_mistake = 'DEMURRAGE UNDERCALCULATED';
                                                    $final_effect="Loss";
                                                }
                                                
                                                if(($des_dem_f_diff <0) && $dem_des == 'DEMURRAGE' && $app2_dem_des == 'DEMURRAGE'){
                                                    $des_dem_mistake = 'DEMURRAGE OVERCALCULATED';
                                                    $final_effect="";
                                                }
                                                if($des_dem_f_diff >0 && $dem_des == 'DETENTION' && $app2_dem_des == 'DETENTION'){
                                                    $des_dem_mistake = 'DETENTION UNDERCALCULATED';
                                                    $final_effect="Loss";
                                                }
                                                
                                                if(($des_dem_f_diff <0) && $dem_des == 'DETENTION' && $app2_dem_des == 'DETENTION'){
                                                    $des_dem_mistake = 'DETENTION OVERCALCULATED';
                                                    $final_effect=""  ;

                                                }
                                                if(($des_dem_f_diff >0) && $dem_des == 'DESPATCH' && $app2_dem_des == 'DESPATCH'){
                                                    $des_dem_mistake = 'DESPATCH UNDERCALCULATED';
                                                    $final_effect=""  ;

                                                }
                                                
                                                if(($des_dem_f_diff <0) && $dem_des == 'DESPATCH' && $app2_dem_des == 'DESPATCH'){

                                                    $des_dem_mistake = 'DESPATCH OVERCALCULATED';
                                                    $final_effect="Loss";

                                                }
                                                 if($app2_dem_des =='DEMURRAGE' && $dem_des =='DESPATCH'){
                                                    $des_dem_mistake = 'CALCULATED DEMM INSTEAD OF DESPATCH';
                                                    $final_effect=""  ;

                                                }
                                                 if($app2_dem_des =='DESPATCH' && $dem_des =='DEMURRAGE'){
                                                    $des_dem_mistake = 'CALCULATED DESPATCH INSTEAD OF DEMM';
                                                    $final_effect="Loss";
                                                }

                                            }

                                     
                                        ?>
                                        <tr  data-aodemdes="<?=$app1_dem_des?>" data-atdemdes="<?=$app2_dem_des?>" data_laytime="<?=$rows['LAYTIME_ID']?>" data_portid="<?=$rows['PORTID']?>">
                                             <td  align="left" valign="top" width="10%"><?= ++$i ?></td>   
                                             <td  align="left" valign="top" width="10%"><?= $vessel_name ?></td>   
                                             <td  align="left" valign="top" width="10%"><?=  $voyage_no ?></td>  
                                             
                                             <?php
                                            $sheet_id = mysql_fetch_assoc(mysql_query("select COST_SHEETID from cost_sheet_name_master where COMID='".$rows['COMID']."' and MODULEID='".$_SESSION['moduleid']."' and MCOMPANYID='".$_SESSION['company']."'"))['COST_SHEETID'];

                                            $obj->viewFreightCostEstimationTempleteRecordsNew($sheet_id);
                                            $redel_date=$obj->getFun246()=='1970-01-01 08:00:00'?'':date('d-m-Y H:i',strtotime($obj->getFun246()));
                                             ?>
                                             <td  align="left" valign="top" width="10%"><?=$redel_date?></td>   
                                             <td  align="left" valign="top" width="10%"><?= $port_name ?></td>   

                                             <!-- approval 1 -->

                                             <?php
                                                  
                                                        
                                                        
                                               

                                             ?>
                                             <td  align="left" valign="top" width="10%"><?= $app1_dem_des ?></td>   
                                             <td  align="left" valign="top" width="10%"><?= $app1_dem_des_amt ?></td>   
                                             <!-- end approval 1 -->
                                             <td  align="left" valign="top" width="10%"><?= $app2_dem_des ?></td>   
                                             <td  align="left" valign="top" width="10%"><?= $app2_dem_des_amt  ?></td>   
                                             <td  align="left" valign="top" width="10%"><?= $des_dem_mistake ?></td>   
                                             <td  align="left" valign="top" width="10%"><?= " " ?></td>   
                                             <td  align="left" valign="top" width="10%"><?= $dem_des ?></td>   
                                             <td  align="left" valign="top" width="10%"><?= $dem_des_amt ?></td>   
                                             <td  align="left" valign="top" width="10%"><?= $des_dem_mistake ?></td>   
                                             <td  align="left" valign="top" width="10%"><?= $des_dem_diff ?></td>   
                                             <td  align="left" valign="top" width="10%"><?= $final_effect ?></td>   
                                             <td  align="left" valign="top" width="10%"><?= $dem_des ?></td>   
                                             <td  align="left" valign="top" width="10%"><?= $des_dem_f_diff ?></td>   
                                             
                                                
                                             
                                            
                                        </tr> 

                                        <?php
                                }
                                ?>
                                

							</tbody>
							
						</table>
					</div>
				</form>
				</div>
				<!--   content ends here..................-->
                </section><!-- /.content -->
            </aside><!-- /.right-side -->
			
        </div><!-- ./wrapper -->


        
<?php $display->footer(); ?>
<?php $display->js(); ?>

<?php
    
    
?>
<script src="../../js/plugins/datatables/jquery.dataTables.js" type="text/javascript"></script>
<script src="../../js/plugins/datatables/dataTables.bootstrap.js" type="text/javascript"></script>
<script src="../../js/timer.js" type="text/javascript"></script>
<link href="../../css/jquery.alerts.css" rel="stylesheet" type="text/css" />
<link href="../../css/datepicker/datepicker.css" rel="stylesheet" type="text/css" />
<script src="../../js/bootstrap-datepicker.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript" src="../../js/jquery.alerts.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	
	$("#sf_list").dataTable();
	
	$('#txtFrom_Date,#txtTo_Date').datepicker({
		format: 'yyyy-mm-dd',
		autoclose: true
	});
});

// function getData()
// {
// 	if($("#txtFrom_Date").val() != "" && $("#txtTo_Date").val() != "")
// 	{
// 		$('#DivOwner').html("");
// 		$('#DivOwner').html('<span style="text-align:center;display:block;"><img src="../../img/loading.gif" /></span>');
// 		$.post("options.php?id=51",{txtFrom_Date:""+$("#txtFrom_Date").val()+"",txtTo_Date:""+$("#txtTo_Date").val()+"",selBType:""+$("#selBType").val()+"", selCOASpot:""+$("#selCOASpot").val()+""}, function(html) {
// 			$('#DivOwner').html("");
// 			$('#DivOwner').html(html);
			$("#sf_list").dataTable();
// 		});
// 	}else{
// 		jAlert('Please select Date From & Date To', 'Alert Dialog');
// 	}
// }

function getPdf()
{
	location.href='allPdf.php?id=30&txtFrom_Date='+$("#txtFrom_Date").val()+'&txtTo_Date='+$("#txtTo_Date").val()+'&selBType='+$("#selBType").val();
}

function getExcel()
{   
	location.href='allExcel.php?id=39&txtFrom_Date=<?=$_GET['CP_DATE_FROM']?>&txtTo_Date=<?=$_GET['CP_DATE_TO']?>';
}

function getData()
{
    if($("#txtFrom_Date").val() != "" && $("#txtTo_Date").val() != ""){
            let form_date=$("#txtFrom_Date").val();
            let form_to=$("#txtTo_Date").val();
            let server_url="<?= url()?>";
            
           
            
          
          

               
            window.location.href=  server_url+"/internal-user/dryout/laytime_audit_report.php"+"?CP_DATE_TO="+form_date+"&CP_DATE_FROM="+form_to;




            

    }else{
        jAlert('Please select Date From & Date To', 'Alert Dialog');
    }
}


</script>
		
</body>
</html>