<?php 
session_start();
require_once 'includes/database.php';
require_once 'includes/funcs.php';
if (!isset($_SESSION['reusername'])) { echo 'Sorry You Dont Have permissions to View this Page!'; exit();}
else {$user = $_SESSION['reusername'];}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <script type="text/javascript" src="js/jquery-1.10.2.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.10.4.custom.js"></script>
        <link rel="stylesheet" href="css/allcss.css" type="text/css" />
        <link rel="stylesheet" href="css/jquery-ui-1.10.4.custom.css" type="text/css" />
        <style>
            html, body {margin:0; padding:0; height:100%; width:100%; min-width:100%; min-height:100%; overflow:scroll;}
#regform {
text-align: left;
width:90%;
margin: 5px; padding-left:5px; padding-right:5px;
border: 1px solid #666; 
background: #FFD47F;
box-shadow: 0px 0px 5px rgba(0,0,0,0.5); 
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
font-weight:bold;
font-size:14px;
color:#2C2C57; 
z-index: 100;
text-wrap: none;
}

.ui-autocomplete {
            max-height: 300px;
            overflow-y: auto;
            /* prevent horizontal scrollbar */
            overflow-x: hidden;
            /* add padding to account for vertical scrollbar */
            padding-right: 10px;
            font-size:12px;
        } 
</style>

<script>        
$(document).ready(function() { 

$("#btnaddcrinv").click(function(e) {
e.preventDefault();
 
if( $('#idate').val()===""  || $('#am').val()==="" || $('#insbkno').val()==="" ){
        $(".validateTips3").html("<font size='2' color='red'><center>All Fields Are Required!</center></font>");
        } 
else { 
    if($("#rmks").val()!== null) { var vrmk = $("#rmks").val();} else {  var vrmk = ".";}
	var insinvno = ($('#insbkno').val());
	var myino = insinvno.slice(0, (insinvno.indexOf("-"))-1); 
    var jqxhrposts = $.get("cuscredsav.php?c="+$('#invno').val()+"&newdoc=true&idate="+$('#idate').val()+"&am="+$('#am').val()+"&rmks="+vrmk+"&insno="+myino+"&levy="+$('#levy').val()+"&sduty="+$('#sduty').val()+"&prem="+$('#prem').val(), function(data) {
						alert(data);
						if (data==="nothing"){ $(".validateTips3").html("<font size='2' color='red'><center>Error in creating Credit Note/Invoice!</center></font>"); }
						 else { window.close(); }                         
                          } );
                          
    }                    
});
 });
</script>
<?php
 if($_REQUEST['c'] === ""){
	$idate = date('d-m-Y');
	$insbkno = '';
	$amount = '';
	$rmks = '';
	$invno = '';
 }
 else {	$invno = $_REQUEST['c'];
	require_once 'includes/database.php';
	$db=new MySQLDatabase();
	$db->open_connection();
	$query = "SELECT insinvoices.amount, insinvoices.rmks, insinvoices.levy, insinvoices.sduty, insinvoices.prem, insinvoices.idate, insinvoices.insno, insrecs.insurer, insrecs.regno FROM insinvoices, insrecs WHERE insinvoices.invno = '$invno' AND insinvoices.insno = insrecs.insno;";
	$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
	if ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){ 
		$amount = $row['amount'];
		$rmks = $row['rmks'];		$levy = $row['levy'];		$sduty = $row['sduty'];		$prem = $row['prem'];	
		$idate = substr($row['idate'],8,2)."-".substr($row['idate'],5,2)."-".substr($row['idate'] ,0,4);
		$insbkno = $row['insno'].' - '.$idate.' - '.$row['insurer'].' - '.$row['regno'];
	}
 }
?>   
    </head><body>
<p class="titletr">Customer Credit Note <?php echo str_repeat('&nbsp;', 120); ?><a href="#"><img src="images/close.png" onclick="window.close();"> </a></p>       
<form id="regform" method="post" onkeypress="return event.keyCode!=13" action="inscuscred.php" >
                    <center>
                    <p id="validatecrinv" class="validateTips3">All fields are required</p>    
                    <table cellspacing="5px" id="lladdbox">
					
                    <tr><td align="left"><label for="rdate"> Doc. Date &nbsp;</label></td>
                    <td><input type="text" id="idate" name="idate" required="required" value="<?php echo $idate; ?>" size="20"/> </td>
                    </tr>     
					<tr><td colspan="2" align="left"><label for="insbkno"> Booking No. &nbsp;&nbsp;</label> <input type="text" id="insbkno" name="insbkno" value="<?php echo $insbkno; ?>" size="55"/>  </td>
                    </tr> 
                    <tr><td align="left"><label for="prem"> Premium &nbsp;</label></td>
                    <td><input type="text" id="prem" name="prem" value="<?php echo $prem; ?>" size="10"> &nbsp;&nbsp; </td>
                    </tr>
                    <tr><td align="left"><label for="levy"> Levy &nbsp;</label></td>
                    <td><input type="text" id="levy" name="levy" value="<?php echo $levy; ?>" size="10"> &nbsp;&nbsp; </td>
                    </tr>
                    <tr><td align="left"><label for="sduty"> Stamp Duty &nbsp;</label></td>
                    <td><input type="text" id="sduty" name="sduty" value="<?php echo $sduty; ?>" size="10"> &nbsp;&nbsp; </td>
                    </tr>				 
                    <tr><td align="left"><label for="am"> Amount &nbsp;</label></td>
                    <td><input type="text" id="am" name="am" value="<?php echo $amount; ?>" size="20"> &nbsp;&nbsp; </td>
                    </tr>
                    <tr><td align="left"><label for="rmks"> Remarks &nbsp;</label></td>
                    <td><input type="text" id="rmks" name="rmks" value="<?php echo $rmks; ?>" size="40"> <input type="hidden" id="invno" name="invno" value="<?php echo $invno; ?>"/> </td>
                    </tr>

                    <br />
                    <tr><td colspan=2 align="center"><input type="submit" id="btnaddcrinv" name="btnaddcrinv"  class="text ui-widget-content ui-corner-all" value=" Save Document " style="width:160px;font-weight:bold; "/></td>
                    </td></tr>
                    </table></center>
                    </form>
    </body>
</html>
<script type="text/javascript">
$("#idate").datepicker({ changeMonth: true, changeYear: true, dateFormat: "dd-mm-yy"});    
$('#insbkno').autocomplete({ source: "allinsbooks.php", minLength: 2  });

</script>

