<?php
require_once 'includes/database.php';
require_once 'includes/funcs.php';
session_start();
$us = $_SESSION['reusername'];
$db=new MySQLDatabase();
$db->open_connection();
if (isset($_REQUEST['newdoc'])){
            $rmks = mysql_real_escape_string($db->connection, $_REQUEST['rmks']); 
            $sid = mysql_real_escape_string($db->connection, $_REQUEST['s']);
            $am = mysql_real_escape_string($db->connection, $_REQUEST['am']);
			$insno = mysql_real_escape_string($db->connection, $_REQUEST['ino']);
			$billno = mysql_real_escape_string($db->connection, $_REQUEST['cn']);
			$ts = date("Y-m-d H:i:s");					
            $idate = substr($_REQUEST['idate'],6,4)."-".substr($_REQUEST['idate'],3,2)."-".substr($_REQUEST['idate'] ,0,2);  
			$sql = mysqli_query($db->connection, "SELECT * FROM insrecs WHERE insno = '$insno';") or die (mysqli_error($db->connection) );
			if ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){ $cname = $row['cname'];}
			$query = "INSERT INTO insbills (billno, idate, insno, cname, supp, amount, invoicecredit, rmks, us, ts) VALUES (".$billno.", '".$idate."', ".$insno.", '".$cname."', ".$sid.", ".$am.", 0, '".$rmks."', '".$us."', '".$ts."');"; 
			//echo $query;
            if(!mysqli_query($db->connection, $query)){
                echo 'nothing';  
             }else{   
                echo 'Saved Credit Note No.'.$billno;
                   }                                                                                  
    $db->close_connection();
}   
?>	