<?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 = mysqli_real_escape_string($db->connection, $_REQUEST['rmks']); 
            $rtype = mysqli_real_escape_string($db->connection, $_REQUEST['rtype']);
            $am = mysqli_real_escape_string($db->connection, $_REQUEST['am']);
			$cname = mysqli_real_escape_string($db->connection, $_REQUEST['cname']);
			$ts = date("Y-m-d H:i:s");			 
			$docno = getnewno('credno'); 
            $ts = date("Y-m-d H:i:s");
            $idate = substr($_REQUEST['idate'],6,4)."-".substr($_REQUEST['idate'],3,2)."-".substr($_REQUEST['idate'] ,0,2);  
			if($rtype==="INSURANCE"){
				$query = "INSERT INTO insinvoices (invno, idate, cname, amount, invoicecredit, rmks, us, ts) VALUES (".$docno.", '".$idate."', '".$cname."', ".$am.", 0, '".$rmks."', '".$us."', '".$ts."');";
			 }
			elseif($rtype==="REPOSESSION"){
				$query = "INSERT INTO repoinvoices (invno, idate, cname, amount, invoicecredit, rmks, us, ts) VALUES (".$docno.", '".$idate."', '".$cname."', ".$am.", 0, '".$rmks."', '".$us."', '".$ts."');";
			 } 
			elseif($rtype==="TRACKER"){
				$query = "INSERT INTO trackinvoices (invno, idate, cname, amount, invoicecredit, rmks, us, ts) VALUES (".$docno.", '".$idate."', '".$cname."', ".$am.", 0, '".$rmks."', '".$us."', '".$ts."');";
			 }  
			//echo $query;
            if(!mysqli_query($db->connection, $query)){
                echo 'nothing';  
             }else{   
                echo 'Saved with Doc. No.'.$docno;
                   }                                                                                  
    $db->close_connection();
}   
?>	