<?php
session_start();
require_once 'includes/database.php';
include 'includes/funcs.php';
$db=new MySQLDatabase();
$db->open_connection();
$user = $_SESSION['reusername'];
$docno = 0;
if(isset($_REQUEST['insno'])){
	$insno = $_REQUEST['insno'];	$doc = $_REQUEST['doc'];	
	$cc = $_REQUEST['cname'];
	$idate = substr($_REQUEST['idate'],6,4)."-".substr($_REQUEST['idate'],3,2)."-".substr($_REQUEST['idate'] ,0,2);
	$supp = $_REQUEST['supp'];	
	$ib = $_REQUEST['ib'];	
	$i = $_REQUEST['i'];	$u = $_REQUEST['u'];
	$iam = $_REQUEST['iam'];
	if ($ib =='i') { 
		if($u =='0'){
			$invno = getnewno("invno"); 
			$query = "INSERT INTO insinvoices (cname, insno, idate, invno, amount, invoicecredit, supp, us, ts) VALUES ('".$cc."', ".$insno.", '".$idate."', '".$invno."', ".$iam.", 1, '".$supp."', '".$user."', '".date("Y-m-d H:i:s")."');";		
			$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
			if($i == '7'){ $query = "UPDATE insrecs SET amount = '$iam', idate = '$idate', invno = '$invno' WHERE insno = '$insno'";  }
			else {
				$query = "UPDATE insrecs SET inst".$i." = '$iam', instdate".$i." = '$idate', invno".$i." = '$invno' WHERE insno = '$insno'"; 
			 }
			$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );	
			$docno = $invno;	
			}
		else {	$query = "UPDATE insinvoices SET idate = '$idate', amount = '$iam',  us = '$user', ts = '".date("Y-m-d H:i:s")."' WHERE invno = '$doc';";		
				$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
				if($i == '7'){ $query = "UPDATE insrecs SET amount = '$iam', idate  = '$idate' WHERE insno = '$insno'";  }
				else { $query = "UPDATE insrecs SET inst".$i." = '$iam', instdate".$i." = '$idate' WHERE insno = '$insno'"; }
				$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
				}		
					}
	else { 	
		if($u =='0'){
			$billno = getnewno("billno"); 
			$query = "INSERT INTO insbills (cname, insno, idate, billno, amount, invoicecredit, supp, us, ts) VALUES ('".$cc."', ".$insno.", '".$idate."', ".$billno.", ".$iam.", 1, ".$supp.", '".$user."', '".date("Y-m-d H:i:s")."');";
			$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
			if($i == '8'){ $query = "UPDATE insrecs SET payam = '$iam', idate = '$idate', billno = '$billno' WHERE insno = '$insno';"; }
			else { $query = "UPDATE insrecs SET bill".$i." = '$iam', billdate".$i." = '$idate', billno".$i." = '$billno' WHERE insno = '$insno';"; }
			$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
			$docno = $billno;
			}
		else {	$query = "UPDATE insbills SET idate = '$idate', amount = '$iam',  us = '$user', ts = '".date("Y-m-d H:i:s")."' WHERE billno = '$doc';";		
				$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
				if($i == '8'){ $query = "UPDATE insrecs SET proam = '$iam', idate = '$idate' WHERE insno = '$insno'"; }
				else { $query = "UPDATE insrecs SET bill".$i." = '$iam', billdate".$i." = '$idate' WHERE insno = '$insno'"; }
				$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
				}
	}
}
$db->close_connection();
echo $docno;  



?>