<?php 


session_start();
require_once("../../includes/display_internal_user_risk.inc.php");
require_once("../../includes/functions_internal_user_riskmangement.inc.php");
$obj = new data();
$connect = $obj->funConnect();
$display = new display();
$display->logout_iu();
$id = $_REQUEST['id'];
if (@$_REQUEST['action'] == 'submit')
 {
 	$msg = $obj->updateFfaHedgeListRecords();
	header('Location:./ffahedge.php?msg='.$msg);
 }
$obj->viewFfaHedgeRecords($id);
$pagename = basename($_SERVER['PHP_SELF'])."?id=".$id;
?>
<!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(); ?>
<!-- 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]-->
<style>
.animated {
				vertical-align: top; 
				transition: height 0.2s;
				-webkit-transition: height 0.2s; 
				-moz-transition: height 0.2s; 
			}

form.cmxform label.error, label.error {
		/* remove the next line when you have trouble in IE6 with labels in list */
		font-family:Verdana, Arial, Helvetica, sans-serif;
		font-size:10px;
		color: red;
		font-style:normal;
		font-weight:lighter;
		margin:5px;
		vertical-align:top;
		}

		*{
			margin: 0px;
			padding: 0px;
			box-sizing: border-box;
		}
</style>
</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(6); ?>
			<aside class="right-side">                
                <!-- Content Header (Page header) -->
                 <section class="content-header">
                    <h1>
                        <i class="fa fa-folder-open"></i>&nbsp;FFA HEDGE&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 class="active">FFA&nbsp;&nbsp;&nbsp;&nbsp; > FFA HEDGE</li>
                    </ol>
                </section>

                <!-- Main content -->
                <section class="content invoice">
				<!--   content put here..................-->
				<div align="right"><a href="ffahedge.php"><button class="btn btn-info btn-flat">Back</button></a></div>
				<div style="height:10px;">&nbsp;</div>
				<form role="form" name="frm1" id="frm1" enctype="multipart/form-data" action="<?php echo $pagename;?>" method="post">			
				<div class="row">
                        <div class="col-xs-12">
                            <h2 class="page-header">
                             UPDATE HEDGE 
                            </h2>                            
                        </div><!-- /.col -->
                    </div>
					
					<div class="box-body">
                    
                       

					<div class="form-group" style="width:49%; float:left;">
							<label for="txtVName" >Sell/buy</label>
							<select  name="selSellBuy" onchange="setCalculationForffa()" class="form-control" id="selSellBuy" onChange="">
							
							<option value="">Please select a option</option>
							<option value="sell">Sell</option>
							<option value="buy">Buy</option>
							</select>   
		
						</div>
                    
                        <div class="form-group" style="width:49%; float:left;padding-left:1%;">
						<label for="txtVName" >Date</label>
							<input type="text" class="form-control" id="txtffaConDate" name="txtffaConDate" onchange="setCalculationForffa()" placeholder="Date" autocomplete="off" value="<?= date("d-m-Y",strtotime($obj->getfun2()));?>">                                     
						</div>

                        <div class="form-group" style="width:49%; float:left;">
							<label for="txtVName" >Close Trade Ref</label>
							<input type="text" class="form-control" id="txtFfaClosingRef" name="txtFfaClosingRef" placeholder="Close Trade Ref" autocomplete="off" value="<?= $obj->getfun3()?>" onKeyUp="setCalculationForffa()">                        </div>
                    
                        <div class="form-group" style="width:49%; float:left;padding-left:1%;">
						<label for="txtVName" >Open Trade Ref.</label>
							<input type="text" class="form-control" id="txtRefNo" name="txtRefNo" placeholder="Trade Ref No." autocomplete="off" value="<?= $obj->getfun4()?>">                      
						</div>





					    <div class="form-group" style="width:49%; float:left;">
							<label for="txtVName" >Clearing Ref No.
						</label>
							<input type="text" class="form-control" id="txtClearRefNo" name="txtClearRefNo" placeholder="Clearing Ref No." autocomplete="off" value="<?= $obj->getfun5()?>">                        </div>
                    
                        <div class="form-group" style="width:49%; float:left;padding-left:1%;">
						<label for="txtVName" >Clearing Fee factor

						</label>
							<input type="text" class="form-control" id="txtCloseFeeFac" name="txtCloseFeeFac" placeholder="Closing Ref.
						" autocomplete="off" value="<?= $obj->getfun6()?>">                      
						</div>






						
						<div class="form-group" style="width:49%; float:left;">
							<label for="txtVCode" >Contract</label>
							<select  name="selCon" class="form-control" id="selCon" onChange="">
							
							<?php 
							echo $obj->getContractForFFA($obj->getfun7());
							?>
							</select>   
						 </div><br/>

						 <div class="form-group" style="width:49%;  padding-left:1%;float:left;" id="drycargo1">
							<label for="txtCraneSize">Settlement

						</label>

						<br>
									<input type="checkbox" name="chkSettlement" <?= ($obj->getfun27()==1)?'checked="checked"':'';?> id="chkSettlement" value="1" class="checkbox" onclick="setCalculationForffa()"/>
						</div>
					 		<div style="clear:both;"></div>	
                        
                        <div class="form-group" style="width:49%; float:left;">
							<label for="txtYBDate" >Period</label>

							<input type="text" class="form-control" id="txtPeriod" name="txtPeriod" placeholder="Period" autocomplete="off" value="<?= $obj->getfun8()?>">     
							<?php 
							// $obj->getBusinessTypeList1();
							?>
							</select>   
						                        
						</div>


                            
						
						<div class="form-group" style="width:49%; float:left;padding-left:1%;">
							<label for="selHullType" >No of Days</label>
						
							
							
							<select  name="txtSizeDpm" class="form-control" id="txtSizeDpm" onChange="">
							
							<?php 
							echo $obj->getMultipleof5($obj->getfun9());
							?>
							</select>   


						</div>
						<div class="form-group" style="width:49%;  float:left;">
						<label for="txtDWT">Size (Total)
							</label>



						<select  name="txtSizeTotal" class="form-control" id="txtSizeTotal" onChange="setCalculationForffa()">
							
							<?php 
							echo $obj->getMultipleof5($obj->getfun10());
							?>
							</select>   


						</div>

						<div class="form-group" style="width:49%; float:left;padding-left:1%;" id="drycargo7">
							<label for="txtVesselGroup">Type
						</label>
						<select  name="selType" class="form-control" id="selType" onChange="setCalculationForffa()"  >
									<option value="">-----Select From List-----</option>
									<option value="1">Spread trade</option>

									<option value="2">Outright</option>

							</select> 	
						</div>

						<div style="clear:both;"></div>	



                      

						<div class="form-group sellFfa" style="width:49%; display:none;  float:left;">
						<label for="txtCoating" >Sell ($)
						</label>
						<input type="text" class="form-control" id="txtSell" onKeyUp="setCalculationForffa()" name="txtSell" placeholder="Sell ($)
						" autocomplete="off" value="<?= $obj->getfun12()?>">       
						</div>


  
                        <div class="form-group buyFfa" style="width:49%; display:none; float:left;padding-left:1%;">
							<label for="txtDWT">Buy ($)
							</label>
							<input type="text" onKeyUp="setCalculationForffa()" class="form-control" id="txtBuy" name="txtBuy" placeholder="Buy ($)
							" autocomplete="off" value="<?= $obj->getfun11()?>">
						</div>

	

						<div style="clear:both;"></div>

						<div class="form-group" style="width:49%; float:left;">
							<label for="txtOwner">Broker
							</label>
							<select  name="selBroker" class="form-control" id="selBroker" >
							
							<?php 
							$obj->getVendorCodeListNew($obj->getfun13());
							?>
							</select> 							  
						</div>


						<div class="form-group" style="width:49%;  padding-left:1%;float:left;" id="drycargo2">



							<label for="txtDraft" >Clearing Fees
							</label>
							<input type="text" class="form-control" id="txtClearingFee" name="txtClearingFee" placeholder="Clearing Fees
						" autocomplete="off" readonly value="<?= $obj->getfun14()?>">     
							                         
						</div>
						

					
						<div class="form-group" style="width:24%; float:left;">



								<label for="txtClearingGst" >Clearing GST percentage

								</label>
									<input type="text" class="form-control" id="txtClearingGstPer" name="txtClearingGstPer" placeholder="Clearing GST percentage" value="<?= $obj->getfun28()?>" autocomplete="off" onKeyUp="setCalculationForffa()">                         
								</div>




								<div class="form-group" style="width:25%; float:left;">



								<label for="txtClearingGst" >Total Clearing GST Amount

								</label>
								<input type="text" class="form-control" id="txtClearingGst" name="txtClearingGst" placeholder="Total Clearing GST Amount" autocomplete="off" value="<?= $obj->getfun15()?>" onKeyUp="setCalculationForffa()" readonly>                         
								</div>


						<div class="form-group" style="width:49%;  padding-left:1%;float:left;" id="drycargo1">
							<label for="txtCraneSize">Brokerage
						</label>
							<input type="text" class="form-control" id="txtBrokerage" name="txtBrokerage" placeholder="Brokerage
						" autocomplete="off" readonly value="<?= $obj->getfun16()?>">
						</div>
						

						

                        <div style="clear:both;"></div>

						
						<div class="form-group" style="width:49%; float:left;" id="drycargo4">
							<label for="txtVesselCode" >Gross P/L
						</label>
							<input type="text" class="form-control" id="txtGrossPl" name="txtGrossPl" placeholder="Gross P/L
						" autocomplete="off" readonly tabindex="-1" value="<?= $obj->getfun17()?>">                                         
						</div>
						
						<div class="form-group" style="width:49%;  padding-left:1%;float:left;" id="drycargo3">
						<label for="txtVesselCode" >Net P/L

						</label>
							<input type="text" class="form-control" id="txtNetpl" name="txtNetpl" placeholder="Net P/L

						" autocomplete="off" readonly tabindex="-1" value="<?= $obj->getfun18()?>">  
						</div>
						

                        
                        <div class="form-group" style="width:49%; float:left;" id="drycargo5">
						<label for="txtVesselGroup">Executed For
						</label>
							<select  name="selCreatedBy" class="form-control" id="selCreatedBy" onChange=""  >
	
							<?php 
							$obj->getLoginDetailsForFFA($obj->getfun19());
							?>
							</select> 	
						</div>

						<div class="form-group" style="width:49%;  padding-left:1%;float:left;" id="drycargo5">
						<label for="txtVesselGroup">Entity

						</label>
							<select  name="selEntity" class="form-control" id="selEntity" onChange=""  >
							
							<?php 
							$obj->getEntityForFFA($obj->getfun20());
							?>
							</select> 	
						</div>
						
						<div class="form-group" style="width:49%; float:left;" id="drycargo6">
						<label for="txtVesselGroup">Exchange


							</label>
							<select  name="selExchange" class="form-control" id="selExchange" onChange=""  >
						
							<?php 
							$obj->getExchangeForFFA($obj->getfun21());
							?>
							</select> 	
						</div>
                        
                        <div class="form-group" style="width:49%;  padding-left:1%;float:left;" id="drycargo7">
							<label for="txtVesselGroup">Vessel hedge
						</label>
						<select  name="selVesselCargo" class="form-control" id="selVesselCargo" onChange=""  >
					
							<?php 
							$obj->getVesselTypeListMemberWise('',$obj->getfun22());
							?>
							</select> 	
						</div>
						<div class="form-group" style="width:49%; float:left;" id="drycargo7">
							<label for="txtVesselGroup">Cargo hedge
						</label>
						<select  name="selCharterters" class="form-control" id="selCharterters" onChange=""  >
					
							<?php 
							$obj->getVendorListNewUpdate();
							?>
							</select> 	
						</div>
					
                        
						<div class="form-group" style="width:49%;  padding-left:1%;float:left;">
							<label for="txtDraft" >Fixture No.
						</label>
							<input type="text" class="form-control" id="txtFixtureNo" name="txtFixtureNo" placeholder="Fixture No.
						" autocomplete="off" value="<?= $obj->getfun24()?>">    
						
						<input type="hidden" id="ffaConId" name="ffaConId" value="<?= $_REQUEST['id']?>">   

						</div>

						<div style="clear:both;"></div>




						<div class="form-group" style="width:49%; float:left;" id="drycargo1">
							<label for="txtCraneSize">Brokerage(%)
						</label>
							<input type="text" class="form-control" id="txtBrokeragePer" name="txtBrokeragePer" placeholder="Brokerage(%)
						" autocomplete="off" value="<?= $obj->getfun26()?>" onKeyUp="setCalculationForffa()">
						</div>
					

						

						<div class="form-group" style="width:49%;  padding-left:1%;float:left;" id="drycargo8">
						<label for="txtVesselGroup">Remarks</label>
							<textarea name="txtRemarks" id="txtRemarks" class="form-control" cols="42" rows="4"><?= $obj->getfun23()?></textarea>
						</div>



						
						<div style="clear:both;"></div>


						

						
						
					</div><!-- /.box-body -->
                        

                    
                  
                    
				<div class="box-footer" align="right">
							<button type="submit" class="btn btn-primary btn-flat" onClick="return getValidate(0);">Submit</button>
							<input type="hidden" name="action" value="submit" /><input type="hidden" name="upstatus" id="upstatus" value="" />
				</div>
         
				
					
				</form>
				
				<!--   content ends here..................-->
                </section><!-- /.content -->
            </aside><!-- /.right-side -->
			
        </div><!-- ./wrapper -->
<?php $display->footer(); ?>
<?php $display->js(); ?>
<link href="../../css/jquery.alerts.css" rel="stylesheet" type="text/css" />
<script src='../../js/jquery.autosize.js'></script>

<link href="../../css/datepicker/datepicker.css" rel="stylesheet" type="text/css" />
<script src="../../js/plugins/datepicker/bootstrap-datepicker.js" type="text/javascript"></script>


<script language="JavaScript" type="text/javascript" src="../../js/jquery.alerts.js"></script>
<script src="../../js/timer.js" type="text/javascript"></script>
<script>
$("#selCharterters").val('<?php echo $obj->getFun29(); ?>');
</script>
<script type="text/javascript">

$(document).ready(function(){ 
	$('#selType').val('<?= $obj->getfun25()?>')
	$('#selSellBuy').val('<?= $obj->getfun1()?>')

	$('#txtffaConDate').datepicker({

                   
format: 'dd-mm-yyyy',
autoclose: true,
todayBtn: true,
minuteStep: 1
}).on('changeDate', function(){  

setCalculationForffa();
});


$('#txtPeriod').datepicker({

                   
format: 'mm-yyyy',
autoclose: true,
todayBtn: true,
minuteStep: 1
}).on('changeDate', function(){  

setCalculationForffa();
});








setCalculationForffa();


$("#selPort").val('<?php echo $obj->getFun3();?>');
$(".areasize").autosize({append: "\n"});
$("#frm1").validate({
	rules: {

		txtTName:"required",
		selVesselCargo: {
			required: function(element) {
				return $('#selCharterters').val() == "" && $('#selVesselCargo').val() ==""
			}
		},


		txtffaConDate: {
			required: function(element) {

				return $('#selCharterters').val() != "";

			}
		},

		txtFixtureNo: {
			required: function(element) {

				return $('#selCharterters').val() != "";

			}
		},



		},
	messages: {	
		txtTName:"*",
		selPort:"*"
		},
submitHandler: function(form)  {
		jAlert('<b style="font-size:25px;">Please wait . . .</b><br><br><img src="../../img/loading.gif"  />', 'Alert');
		$("#popup_container").css({"background": "rgba(255,255,255,0.1)","border": "10px solid rgba(255,255,255,0.1)","width": "100%"});
		$("#popup_content").css({"background":"none","text-align":"center"});
		$("#popup_ok,#popup_title").hide();  
		form.submit();
	}
});

});




function setCalculationForffa(){


	$('#chkSettlement').on('ifChecked', function () { 

		$('#txtBrokeragePer').val(0);

	});

	$('#chkSettlement').on('ifUnchecked', function () { 

		if($('#selType').val()==1){

			$('#txtBrokeragePer').val(0.0005);

		}else{

			$('#txtBrokeragePer').val(0.001);

		}

	});


	var selSellBuy = $('#selSellBuy').val();

	if( selSellBuy==' '){selSellBuy = 0;}
	// console.log(selSellBuy);
	if(selSellBuy!=0){

			if(selSellBuy=='sell'){


				$(".sellFfa").attr("style", "width:49%; display:block;  float:left;");
				
				$(".buyFfa").attr("style", "width:49%; display:block; float:left;padding-left:1%;");

				$('#txtBuy').prop('readonly', true);

				$('#txtSell').attr('readonly', false);

				$('#txtBuy').val(0);


			}if(selSellBuy=='buy'){
				// console.log('dpk');
				$('#txtSell').prop('readonly', true);

				$('#txtBuy').attr('readonly', false);

				$('#txtSell').val(0);

				$(".sellFfa").attr("style", "width:49%; display:block;  float:left;");

				$(".buyFfa").attr("style", "width:49%; display:block; float:left;padding-left:1%;");
			}
			
		}

		var txtCloseFeeFac = $('#txtCloseFeeFac').val();

		if(isNaN(txtCloseFeeFac) || txtCloseFeeFac==''){txtCloseFeeFac = 0;}
		

		var txtSizeTotal = $('#txtSizeTotal').val();
	
	if(isNaN(txtSizeTotal) || txtSizeTotal==''){txtSizeTotal = 0;}


	 $('#txtClearingFee').val((txtSizeTotal*txtCloseFeeFac).toFixed(2));
	 


	 var txtClearingGstPer = $('#txtClearingGstPer').val();
		
		if(isNaN(txtClearingGstPer) || txtClearingGstPer==''){txtClearingGstPer = 0;}


		var ttlForttlClrGstAmt   = (txtCloseFeeFac * txtClearingGstPer / 100) * txtSizeTotal;

		$('#txtClearingGst').val(ttlForttlClrGstAmt.toFixed(4));
	 


	 var txtSell = $('#txtSell').val();

	if( txtSell==' '){txtSell = 0;}

	if(txtSell!=0){

		setTimeout(function () {
                    
                
				if($('#txtBrokeragePer').val()=="" || isNaN($('#txtBrokeragePer').val())){var txtBrokeragePer = 0;}else{var txtBrokeragePer = $("#txtBrokeragePer").val();}

				console.log(txtBrokeragePer,'dpk1');

				

					$('#txtBrokerage').val((txtSell*txtSizeTotal*txtBrokeragePer).toFixed(2));

				
			}, 1000);
	
		}


	var txtBuy = $('#txtBuy').val();

	if( txtBuy==' '){txtBuy = 0;}

	if(txtBuy!=0){

		setTimeout(function () {
                    
					if($('#txtBrokeragePer').val()=="" || isNaN($('#txtBrokeragePer').val())){var txtBrokeragePer = 0;}else{var txtBrokeragePer = $("#txtBrokeragePer").val();}
	
					
			
						$('#txtBrokerage').val((txtBuy*txtSizeTotal*txtBrokeragePer).toFixed(2));
	
					
				}, 1000);
		
		
	}



	var txtffaConDate = $('#txtffaConDate').val();

	if( txtffaConDate==' '){txtffaConDate = 0;}

	// if(txtffaConDate!=0){

	// 	$.post("options.php?id=126",{txtffaConDate:""+$("#txtffaConDate").val()+""}, function(res) {

	// 		$('#txtClearingGst').val((txtSizeTotal*res).toFixed(2));
	// });

	// }


	// if(txtBuy!=0){


		if($("#txtFfaClosingRef").val().length == 0 || $("#txtPeriod").val().length == 0) {

			// console.log('dpk')
			$('#txtNetpl').val(0.00);

			$('#txtGrossPl').val(0.00);

		}else{

			// console.log('HJK')


			$.ajax({

			cache:false,
			url:'options.php?id=130',
			type:'POST',
			data:{
				txtFfaClosingRef:$("#txtFfaClosingRef").val(),
				txtPeriod:$("#txtPeriod").val(),
				conid:$('#ffaConId').val()
			},

			success:function(res){



				// var gstClearper = res;

				console.log(res);

				if(res.trim()=== ''){

					// Response is empty
					$('#txtNetpl').val(0.00);
					$('#txtGrossPl').val(0.00);
					return false;

				}
		
			const obj = JSON.parse(res);

			var txtGrossPl = $('#txtGrossPl').val();

			if( txtGrossPl==' '){txtGrossPl = 0;}

			// console.log(res)
			 var minus1;

			 var minus2;
			 if(obj.SELL>0){

if(obj.SELL>0){
	minus2 = obj.SELL;


	minus1 =txtBuy;

}


}else{
if(obj.BUY>0){

	
minus1 = obj.BUY


minus2 = txtSell
}
	

}
console.log(txtSell)

// if(typeof minus1=='undefined'){

// 	minus1=0;
// }if(typeof minus2=='undefined'){

// 	minus2=0;
// }


console.log(minus1)
console.log(minus2)
$('#txtGrossPl').val(((parseFloat(minus2)-parseFloat(minus1))*txtSizeTotal).toFixed(2));


	


			var txtGrossPl = $('#txtGrossPl').val();

		
			if(isNaN(txtGrossPl) ||  txtGrossPl==' '){txtGrossPl = 0;}

			$('#txtGrossPl').val(txtGrossPl);

			// $('#txtNetpl').val((txtGrossPl-obj.CLEARING_FEES-obj.CLEARING_GST-obj.BROKRAGE-$('#txtClearingFee').val()-$('#txtClearingGst').val()-$('#txtBrokerage').val()).toFixed(2));



			// var netForUnwind = 

			var netForUnwind = parseFloat(txtGrossPl)-(parseFloat($('#txtClearingFee').val())+parseFloat($('#txtClearingGst').val())+parseFloat($('#txtBrokerage').val()));


			if(isNaN(netForUnwind) ||  netForUnwind==' '){netForUnwind = 0;}


			

			var txtNetpl = parseFloat(netForUnwind) - (parseFloat(obj.CLEARING_FEES/obj.SIZE_TOTAL*txtSizeTotal)+parseFloat(obj.CLEARING_GST/obj.SIZE_TOTAL*txtSizeTotal)+parseFloat(obj.BROKRAGE/obj.SIZE_TOTAL*txtSizeTotal));
			if(isNaN(txtNetpl) ||  txtNetpl==' '){txtNetpl = 0;}
		
			$('#txtNetpl').val((txtNetpl).toFixed(2));

			


		}

	});


		}

		


	// }
	

}



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