<?php

header("Content-type: text/html; charset=windows-1256");
session_start();

include("config.inc.php");
include("Functions_Sales.php");
include("function.inc.php");
include("form_Customergrid.php");
//Set Time Execution For the Report
ini_set('max_execution_time', $config['ReportTimeExecution']);

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

function ConvertCurrency($FromCurr, $ToCurr, $FromDate, $Amount, $connection)
{
	$query = "SELECT ifNull(convertcurfunc('".$FromCurr."', '".$ToCurr."', '".$FromDate."', 0, 0, '', '', ifNull($Amount,0)),0) From Dual ";
	$result = selectData($connection, $query);
	//echo $query; exit;
	Return	$result[0];

}


$name = $_GET['name'];

$benef = $_GET['benef'];
$Tel = $_GET['Tel'];
$Remark = $_GET['Remark'];
$Remark = $_GET['Remark'];
$Currency = $_GET['Currency'];
$Amount = $_GET['Amount'];

$accdc = $_GET['accdc'];
$accdcinfo = $_GET['accdcinfo'];



	  	$bases = "select base1,base2 from setup;";
	$resbases = mysql_query($bases,$connection);
	$rawbases= mysql_fetch_array($resbases);
	extract ($rawbases); 
	

	 

	$coupon_amountbase1 = ConvertCurrency($Currency,$base1,date("Y-m-d H:i:s"),$Amount,$connection) ;
	$coupon_amountbase2 = ConvertCurrency($Currency,$base2,date("Y-m-d H:i:s"),$Amount,$connection) ;
	
$Coupon_Id = get_nextsequence('Coupon_Id','',$connection);	
$Coupon_Reference = get_nextsequence('Coupon_Reference','',$connection);	
	
$queryinsert = "
insert into coupon(Coupon_Id, Coupon_Reference, Coupon_Name,
 Ledger_Number, Coupon_Active, Coupon_Posted, Coupon_Tel,
 Coupon_Remark, Coupon_Amount, Coupon_AmountBase1, Coupon_AmountBase2,
 Currency_Code, Coupon_AmountConsum, Coupon_AmountConsumBase1,
 Coupon_AmountConsumBase2, Coupon_Date, Synchronized, Owner_Branch, coupon_beneficiary,coupon_debitacc,coupon_debitaccinfo)
 
 
 values ( '$Coupon_Id', '$Coupon_Reference'
 ,'$name',(select coupon_account from cashregister),'1','1','$Tel','$Remark','$Amount','$coupon_amountbase1','$coupon_amountbase2'
 ,'$Currency',null,null,null,now(),'0',null,'$benef','$accdc','$accdcinfo')



";

//echo $queryinsert;

$queryupdate = "
update coupon
set Coupon_Name = '$name',
coupon_beneficiary = '$benef',
Coupon_Tel = '$Tel',
Coupon_Remark = '$Remark',
Currency_Code = '$Currency',
Coupon_Amount = '$Amount',
Coupon_AmountBase1 = '$coupon_amountbase1',
Coupon_AmountBase2 = '$coupon_amountbase2',
coupon_debitaccinfo = '$accdcinfo',
coupon_debitacc = '$accdc'
where coupon_id = $id

";	

//echo $queryupdate; 

if ($mode == "insert")
{
$res = mysql_query($queryinsert,$connection);
createJVCoupon($connection, $Coupon_Id);	

}
else
	if ($mode == "update")
	{
		$res = mysql_query($queryupdate,$connection);
		
		createJVCoupon2($connection, $id);
	}
else if($mode == "delete"){
	
$querydel = "delete from coupon where coupon_id = $id";	
$res = mysql_query($querydel,$connection);
}

//echo $queryinsert;



	
  
?>