<?php

session_start();

if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("function.inc.php");
include("Functions_Trans.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) or die("Could not connect to DB");
mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");


set_time_limit(0);

$message='';

if($mode == "GenerateInvoices"){

	$resultvars = mysql_query("set @Result := 0, @JvResult := 0, @SalesJVId :=0, @SalesJVRef := 0, @SalesJVDtId := 0 ", $connection);

	$sub_query = " call GenerateInvoices('".$fromledger."', '".$toledger."', '".insertMySQLDate($fromdate)."', '".insertMySQLDate($todate)."', ".
				 "                       '".$invoicecurr."', '".insertMySQLDate($invoicedate)."', 0, '". $HTTP_SESSION_VARS['dbName']."', '". $HTTP_SESSION_VARS['dbLabel']."', ".
				 "                       '". $HTTP_SESSION_VARS['useraccount']."', '". $HTTP_SESSION_VARS['username']."', @Result, @JvResult) ";
					
	//echo $sub_query; exit;
	$sub_result = mysql_query($sub_query,$connection);
    $error = mysql_error($connection);
	
	
	$sub_query1 = " select @Result, @JvResult from dual ";
	$sub_result1 = mysql_query($sub_query1,$connection) or die(mysql_error());
	$cur1 = mysql_fetch_row($sub_result1);

	
    if ($cur1[0] == -5) {
	   $message = 'Error In Procedure GenerateInvoices: '.$error;
	   //frollback();
	}   
	else if ($cur1[0] == -2) {
	   $message = 'Error In Procedure Create_SalesJVAutoJv: '.$error;
	   //frollback();
	}   
	else if  ($cur1[0] == -1)  {
	   $message = 'Error In Procedure Create_Jv: '.$error;   
	   //frollback();
	}   
	else if ($cur1[0] >= 0 && $cur1[1] >= 0) {
		$message = 'Generate invoices done successfully. ';	
		//fcommit();
	}	
	
}


$message = ereg_replace(' ','%20',$message);
$message = ereg_replace('#','%23',$message);
$message = ereg_replace('&','%26',$message);

//echo $mode.$message; exit;

header ("Location: ./Form_Invoicing.php?actionmode=Search&fromledger=".$fromledger."&toledger=".$toledger."&fromdate=".$fromdate."&todate=".$todate.
		"&invoicedate=".$invoicedate."&invoicecurr=".$invoicecurr."&message=".$message);



?>