<?php 
session_start();
require_once 'includes/database.php';
include '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;z-index: 100;}

        </style>
<script type="text/javascript"> 
function suminvams() {                      // on selecting
    var tot=0;
    var tp = document.getElementsByName('topay');
    for(var i=0;i<tp.length;i++){ 
        var avwc = tp[i].value;
        var av1 = avwc.replace(/[+-][^0-9.]/g,'');   
        var aa = parseFloat(av1);
        if(aa>0) { tot += aa;}  
        else { tp[i].value = 0;}
     };   
     document.getElementById('am').value = tot;
 };
    
      
$(document).ready(function() {  

$("#btnaddinv").click(function(e) {
e.preventDefault();

if( $('#cname').val() === ''  ){ document.getElementById('valerr').value="Customer Name Cannot be Blank!";	}
else if( $('#idate').val() === ''  ){ document.getElementById('valerr').value="Date Cannot be Blank!";	}
else if( $('#am').val() === '' || $('#am').val() === '0' ){ document.getElementById('valerr').value="Amount Must be Greater than 0!";	}
else if( $('#cname').val() !== '' && $('#idate').val() !== '' && $('#am').val() !== '0') {
		if($('#inv').val() === '0'){ var ic = 0;}
		else { var ic = 1;} 
		var savinvdet = $.get("savothinv.php?cname="+$('#cname').val()+"&idate="+$('#idate').val()+"&rmks="+$('#rmks').val()+"&am="+$('#am').val()+"&ic="+ic+"&i="+$('#invno').val(), function(datainv){
			var tp = document.getElementsByName('topay');
			var ino = datainv.trim();	
			document.getElementById('inno').value = ino;
			alert('Invoice No. '+$('#inno').val()+ ' Generated!');
			window.location.replace("invprint.php?i="+$('#inno').val()+"&sec=o","INVOICE","width=900px,height=600px,scrollbar=1");
		 });
	 	}
});

});
</script>
  
</head><body>
<?php 
if($_REQUEST['e'] === "0"){
	$cname = '';
	$idate = date("d-m-Y");
	$am = '0';
	$rmks = '';
	$ic = 1;
	$invno = 0;
}
else {
	$invno = $_REQUEST['i'];
	require_once 'includes/database.php';

	$db=new MySQLDatabase();
	$db->open_connection();
	$query = "SELECT * FROM othinvoices WHERE invno = '$invno';";
	$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
	if ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
		$cname = $row['cname'];
		$idate = substr($row['idate'],8,2)."-".substr($row['idate'],5,2)."-".substr($row['idate'] ,0,4);
		$ic = $row['invoicecredit'];
		$am = $row['amount'];
		$rmks = $row['rmks'];
	}
	else { echo '<script type="text/javascript"> window.close();</script>';}
	$db->close_connection();
}
?> 
<p class="titletr"> <?php echo ($_REQUEST['e'] === "0") ? 'New Invoice/Credit Note' : 'Edit Invoice/Credit Note No.'.$invno;?>  &nbsp; <input type="text" id="valerr" name="valerr" value="" size="25"> <?php echo str_repeat('&nbsp;', 10); ?> <a href="#"><img src="images/close.png" onclick="window.close();"> </a>  </p>       
<form id="invform" method="post" onkeypress="return event.keyCode!=13" enctype="multipart/form-data" action="#" >
                    <center>
                    <table cellspacing="5px" id="regaddbox">
					<tr><td align="left"><label for="idate"> Date &nbsp;</label></td>
						<td><input type="text" id="idate" name="idate" value="<?php echo $idate; ?>" size="10"/> </td>
                    </tr>
                    <tr><td align="left"><label for="cname">Customer Name &nbsp; </label></td>
						<td><input type="text" id="cname" name="cname" value= "<?php echo $cname; ?>" required="required" size="40"/></td>
                    </tr>
					<tr><td colspan=2 align="center" class="patchbx">
						<input type="radio"  id="inv" name="trinvc" <?php if($ic) { echo 'checked value="1"'; } else { echo 'value="0"';} ?>  /><b>Invoice</b>&nbsp;&nbsp;&nbsp;
						<input type="radio"  id="cr" name="trinvc" <?php if(!$ic) { echo 'checked value="1"'; } else { echo 'value="0"';} ?> /><b>Credit Note</b>
						<input type="hidden" id="ic" name="ic" value="<?php echo $ic; ?>"/> <input type="hidden" id="invno" name="invno" value="<?php echo $invno; ?>"/><input type="hidden" id="e" name="e" value="<?php echo $_REQUEST['e']; ?>"/></td></tr>
					
					<tr><td align="left"><label for="am"> Amount &nbsp;</label></td>
                    <td><input type="text" id="am" name="am" value="<?php echo $am; ?>" size="10" required="required"/></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="inno" name="inno" value="0"/></td>
                    </tr>
                    <tr><td colspan=2> </td></tr><br />
                    <tr><td colspan=2 align="center">
						<input type="button" class="text ui-widget-content ui-corner-all" style="width:160px;font-weight:bold;" id="btnaddinv" name="btnaddinv" value=" Save Invoice "/>
                    </td></tr>
                    </table></center>
        </form>
</body>
</html>

<script type="text/javascript">   
$('#cname').autocomplete({ source: <?php echo allcustomers(); ?>, minLength: 2  });                                                                
$('#idate').datepicker({ changeMonth: true, changeYear: true, dateFormat: "dd-mm-yy"}); 


</script>



