<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
get_pagesecurity('6','creates','R');     
$_SESSION['cuPages']='opr_lcgen.php';   
$cuPage='opr_lcgen.php';    
$aid=$_SESSION['uid'];    
}else{
header('Location:../index.php');
exit;    
}
$mhead='lc';
$menuh='LC Setup';
$phead='lccre';
$page='Generate LC';
$dtnow = date("Y-m-d h:i:s", time());
?>
<?php
include('../layout/head.php');
include('../layout/header.php');
include('../layout/side_nav.php');
?>
<?php 
if(isset($_POST['save_lcn'])){
	$lcno = strtoupper(remove_junk(escape($_POST['lcno'])));
    $pino = strtoupper(remove_junk(escape($_POST['pino'])));
    $doi = remove_junk(escape($_POST['doi']));
    $expdate = remove_junk(escape($_POST['expdate']));
    $ldos = remove_junk(escape($_POST['ldos']));
    $apbank = ucwords(remove_junk(escape($_POST['apbank'])));
    $bnbank = ucwords(remove_junk(escape($_POST['bnbank'])));
    $usd = remove_junk(escape($_POST['usd']));
	$expdays = remove_junk(escape($_POST['expdays']));
    if($expdays!=''){$expdays=0;}
	if(isset($_POST['lcno'])){
	$ducode = mysqli_query($con,"SELECT * FROM tbl_lcopen WHERE lcno = '$lcno'");
	}
	if(isset($_POST['pino'])){
	$dupi = mysqli_query($con,"SELECT * FROM tbl_lcopen WHERE pino = '$pino'");
	}
    if($dupi->num_rows > 0) {
    save_msg('i','PI no alrady exists!');
	echo "<script>window.location='opr_lcgen.php'</script>";
	}
    
	if($ducode->num_rows > 0) {
		save_msg('i','LC No alrady exists!');
		echo "<script>window.location='opr_lcgen.php'</script>";
	}else{
    $sql="INSERT INTO tbl_lcopen(lcno,pino,doi,expdate,expdays,ldos,apbank,bnbank,usd,uid,date) VALUES ('$lcno','$pino','$doi','$expdate','$expdays','$ldos','$apbank','$bnbank','$usd','$aid','$dtnow')";    
	$result = mysqli_query($con,$sql) or die(mysqli_error($con));
    $efid=mysqli_affected_rows($con);     
    if($efid>0){
    $act =remove_junk(escape('LC No: '.$lcno));    
    write_activity($aid,'LCN','LC No has been Added',$act);    
    save_msg('s','Data Successfully Saved!');
	}else{
    save_msg('w','Data fail to Saved!');    
    }
    echo "<script>window.location='opr_lcgen.php'</script>";     
    }
}
?>
<div class="content-wrapper">
<?php include('../layout/page_head.php'); ?>
<!-- Main content -->
<section class="content">

<div class="row">
<div class="col-md-8">
<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">Add Letter of Credit (LC)</h3>
</div>
<div class="box-body">
<?php echo display_msg();?>  
<form action="opr_lcgen.php" onsubmit="return validate()" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<div class="col-md-12 popup_details_div">    
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-10">    
<div class="row">
<div class="col-md-4">
<div class="form-group" >
<label>LC No</label>
<input type="text" maxlength="25" class="form-control" name="lcno" id="lcno" placeholder="e.g. 1741192300167" autocomplete="off">    
</div>    
</div>
<div class="col-md-4">
<div class="form-group" >
<label>PI No</label>
<input type="text" maxlength="25" class="form-control" name="pino" id="pino" placeholder="e.g. XHL20191203003" autocomplete="off">    
</div>
</div>
<div class="col-md-4">
<div class="form-group" >
<label>Date of Issue</label>
<div class="input-group">
<span class="input-group-addon">
<span class="fa fa-calendar"></span>
</span>
<input type="text" class="form-control datetimepicker" name="doi" id="doi" placeholder="Date of Issue" autocomplete="off" readonly>
</div>
</div>
</div>    
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group" >
<label>Date of Expiry</label>
<div class="input-group">
<span class="input-group-addon">
<span class="fa fa-calendar"></span>
</span>
<input type="text" class="form-control datetimepicker" name="expdate" id="expdate" placeholder="Date of Expiry" autocomplete="off" readonly>
</div>
</div>    
</div>
<div class="col-md-4">
<div class="form-group" >
<label>Draft At</label>
<input type="text" maxlength="3" class="form-control" min="0" value="0" name="expdays" id="expdays" nkeypress="return isNumberKey(event)" placeholder="e.g. 90" autocomplete="off">    
</div>    
</div>
<div class="col-md-4">
<div class="form-group" >
<label>Latest Date of Shipment</label>
<div class="input-group">
<span class="input-group-addon">
<span class="fa fa-calendar"></span>
</span>
<input type="text" class="form-control datetimepicker" name="ldos" id="ldos" placeholder="Latest Date of Shipment" autocomplete="off" readonly>
</div>
</div>    
</div>    
</div>    
<div class="row">
<div class="col-md-4">
<div class="form-group" >
<label>Applicant Bank</label>
<textarea class="form-control" name="apbank" id="apbank" maxlength="150" rows="5" placeholder="e.g. Mercantile Bank Limited"></textarea>   
</div>
</div> 
<div class="col-md-4">
<div class="form-group" >
<label>Beneficiary Bank</label>
<textarea class="form-control" name="bnbank" id="bnbank" maxlength="150" rows="5" placeholder="e.g. Mercantile Bank Limited"></textarea>   
</div>
</div> 
<div class="col-md-4">
<div class="form-group" >
<label>USD Amount</label>
<input type="text" maxlength="20" class="form-control" min="0" value="0" name="usd" id="usd"  placeholder="e.g. 90" autocomplete="off">    
</div>    
</div>   
</div>
    
</div>    
<div class="col-md-1"></div>
</div>
</div>
    
<div class="clearfix" ></div>
<div class="col-md-12 nopadding widgets_area"></div>    
<div class="row"style="margin-top: 15px" >
<div class="col-md-8"></div>
<div class="col-md-4 text-right" >
<input type="submit" name="save_lcn" id="submit" class="btn btn-flat bg-purple btn-sm " value="Save"/> <a href="opr_lclist.php" class="btn btn-flat bg-gray  ">Close</a>
</div> 
</div>     
</form>    
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-md-12">
<div class="box box-solid">
<div class="box-header">
<h3 class="box-title">History </h3>
</div>
<!-- /.box-header -->
<div class="box-body" >
<?php if($_SESSION['utype']=='1'){echo read_activity($aid,'LCN','A');}else{echo read_activity($aid,'LCN','U');}?>
</div>
</div>
</div>
</div>
</div>
</div>

<?php include('../layout/quick.php');?>     
</section>
<!-- /.main content -->    
</div>

<?php 
include('../layout/footer.php');
?>
<!-- page script -->
<script type="text/javascript">
$(document).ready(function () {
var lcno = new LiveValidation('lcno');
lcno.add(Validate.Presence);
var pino = new LiveValidation('pino');
pino.add(Validate.Presence);
var doi = new LiveValidation('doi');
doi.add(Validate.Presence);
var expdate = new LiveValidation('expdate');
expdate.add(Validate.Presence);
var ldos = new LiveValidation('ldos');
ldos.add(Validate.Presence);
var apbank = new LiveValidation('apbank');
apbank.add(Validate.Presence);    
});
    
$(document).on('keydown', '#pino', function() {
$('#pino' ).autocomplete({
source: function( request, response ) {
$.ajax({
url: 'axe_cart.php',
type: 'post',
dataType: 'json',
data: {
search: request.term, getpi:1
},
success: function(data) {
response(data);
}
});
},
select: function (event, ui) {
var te=ui.item.label;
$(this).val(te); // display the selected text
return false;
}
});    
});    
</script>    
<!-- /page script -->
</html>    