<?php
session_start();
require_once 'includes/database.php';
require_once 'includes/funcs.php';
$db=new MySQLDatabase();
$db->open_connection();
$user = $_SESSION['reusername'];

if(isset($_REQUEST['cname'])){	
$cname = strtoupper($_REQUEST['cname']);
$am = strtoupper($_REQUEST['am']);
$i = strtoupper($_REQUEST['i']);
$idate = substr($_REQUEST['idate'],6,4)."-".substr($_REQUEST['idate'],3,2)."-".substr($_REQUEST['idate'] ,0,2);
$repono = $_REQUEST['repono'];
$nar = $_REQUEST['nar'];
if($i===''){
$invno = getnewno('invno');	
																		// add inv															
$query = "INSERT INTO repoinvoices (invno, cname, repono, nar, invoicecredit, amount, idate, us, ts) VALUES (".$invno.", '".$cname."', ".$repono.", '".$nar."', 1, ".$am.", '".$idate."', '".$user."', '".date("Y-m-d H:i:s")."');";
$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
$query = "UPDATE reporders SET invno = '$invno' WHERE repono = '$repono'";
$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
$db->close_connection();
echo 'OK';  
}
else { 																		// Upd inv															
$query = "UPDATE repoinvoices SET amount = '$am', idate ='$idate', us ='$user', ts ='".date("Y-m-d H:i:s")."' WHERE repono = '$repono' ;";
$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
$db->close_connection();
echo 'OK'; 
}
}


?>