<?php

session_start();

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

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

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



print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="default.css" type="text/css" />
<script type="text/javascript" language="javascript">


</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

</head>
<body>
<form name='insertsales' method=''>
<input type='text' name='SMode' value='$SMode'>
<input type='text' name='STempId' value='$STempId'>
<input type='text' name='SJvAlltypes' value='$SJvAlltypes'>
<input type='text' name='SJvExclude'  value='$SJvExclude'>
<input type='text' name='SJvTypes'    value='$SJvTypes'>
<input type='text' name='SJvdb'       value='$SJvdb'>
<input type='text' name='SJvcr'       value='$SJvcr'>
<input type='text' name='SJvLedgers'  value='$SJvLedgers'>
<input type='text' name='SGroupbyLevel3Name' value='$SGroupbyLevel3Name'>
<input type='text' name='SGroupbyLevel3Value' value='$SGroupbyLevel3Value'>
HERE;

$message='';

//Submit Record Mode

if ($SMode == "insert" || $SMode == 'edit'){
	$vTemplateName  = $templatename;
	$vReportName    = $reportname;
	$vUserName      = $HTTP_SESSION_VARS['useraccount'];
	$vGroupbyLevel1Name  = ereg_replace("'", "\'", trim($groupbylevel1name));
	$vGroupbyLevel2Name  = ereg_replace("'", "\'", trim($groupbylevel2name));
	$vGroupbyLevel3Name  = ereg_replace("'", "\'", trim($SGroupbyLevel3Name));
	$vGroupbyLevel1Value = ereg_replace("'", "\'", trim($groupbylevel1value));
	$vGroupbyLevel2Value = ereg_replace("'", "\'", trim($groupbylevel2value));
	$vGroupbyLevel3Value = ereg_replace("'", "\'", trim($SGroupbyLevel3Value));
	$vFromLedgerNumber = ereg_replace("'", "\'", trim($fromledger));
	$vToLedgerNumber   = ereg_replace("'", "\'", trim($toledger));
	$vFromClientCode   = ereg_replace("'", "\'", trim($fromclient));
	$vToClientCode     = ereg_replace("'", "\'", trim($toclient));
	$vFromSupplierCode = ereg_replace("'", "\'", trim($fromsupplier));
	$vToSupplierCode   = ereg_replace("'", "\'", trim($tosupplier));
	$vFromSalesmanCode = ereg_replace("'", "\'", trim($fromsalesman));
	$vToSalesmanCode   = ereg_replace("'", "\'", trim($tosalesman));
	$vFromItemCode     = ereg_replace("'", "\'", trim($fromitem));
	$vToItemCode       = ereg_replace("'", "\'", trim($toitem));
	$vFromWarehouseCode = ereg_replace("'", "\'", trim($fromwarehouse));
	$vToWarehouseCode   = ereg_replace("'", "\'", trim($towarehouse));
	$vFromGroupCode     = ereg_replace("'", "\'", trim($fromgroup));
	$vToGroupCode       = ereg_replace("'", "\'", trim($togroup));
	$vFromSubgroupCode  = ereg_replace("'", "\'", trim($fromsubgroup));
	$vToSubgroupCode    = ereg_replace("'", "\'", trim($tosubgroup));
	$vFromProjectCode   = ereg_replace("'", "\'", trim($fromproject));
	$vToProjectCode     = ereg_replace("'", "\'", trim($toproject));
	$vFromCostcentCode  = ereg_replace("'", "\'", trim($fromcostcent));
	$vToCostcentCode    = ereg_replace("'", "\'", trim($tocostcent));
	$vFromMasterProjectCode   = ereg_replace("'", "\'", trim($frommasterproject));
	$vToMasterProjectCode     = ereg_replace("'", "\'", trim($tomasterproject));
	$vFromMasterCostcentCode  = ereg_replace("'", "\'", trim($frommastercostcent));
	$vToMasterCostcentCode    = ereg_replace("'", "\'", trim($tomastercostcent));
	$vFromAreaCode    = ereg_replace("'", "\'", trim($fromarea));
	
	$vJournalTypeAlltypes  = $SJvAlltypes;
	$vJournalTypeAlldebit  = $SJvAlldebit;
	$vJournalTypeAllcredit = $SJvAllcredit;
	$vJournalTypeExclude  = $SJvExclude;
	$vJournalTypeTypes    = ereg_replace("'", "\'", trim($SJvTypes));
	$vJournalTypedb       = $SJvdb;
	$vJournalTypecr       = $SJvcr;
	$vJournalLedgers      = ereg_replace("'", "\'", trim($SJvLedgers));
	
	
	//Insert Case
	if ($STempId == ""){
	
		/*$sub_query = " Select Lpad(ifNull(Max(Substr(Template_Name,length(Template_Name)-1)),0) + 1, 2, '0') ".
					 " From ReportsOptions_Templates ".
					 " Where Upper(Report_Name) = Upper('".$vReportName."') ".
					 " And User_Account = '".$vUserName."' ";
					  
		$sub_result = mysql_query($sub_query, $connection);
		$sub_row    = mysql_fetch_row($sub_result);
		$vTemplateName = $vUserName.'-'.strtoupper($vReportName).'-'.$sub_row[0];	*/
	
		fbegin();
		$query = " INSERT INTO ReportsOptions_Templates (Template_Name, Report_Name, Groupby_Level1Value, Groupby_Level2Value, Groupby_Level3Value, Groupby_Level1Name, ".
				 " Groupby_Level2Name, Groupby_Level3Name, JournalType_alltypes, JournalType_exclude, JournalType_types, JournalType_db, JournalType_cr, FromLedger_Number, ".
				 " ToLedger_Number, FromClient_Code, ToClient_Code, FromSupplier_Code, ToSupplier_Code, FromSalesman_Code, ToSalesman_Code, FromItem_Code, ToItem_Code, ".
				 " FromWarehouse_Code, ToWarehouse_Code, FromGroup_Code, ToGroup_Code, FromSubgroup_Code, ToSubgroup_Code, FromProject_Code, ToProject_Code, FromMasterProject_Code, ".
				 " ToMasterProject_Code, FromCostcent_Code, ToCostcent_Code, FromMasterCostcent_Code, ToMasterCostcent_Code, FromArea_Code, ToArea_Code, AllTypesDebit, AllTypesCredit, ".
				 " LedgersPerType, User_Account  ) ".
				 " VALUES ( '".$vTemplateName."', '".$vReportName."', '".$vGroupbyLevel1Value."', '".$vGroupbyLevel2Value."', '".$vGroupbyLevel3Value."', '".$vGroupbyLevel1Name."', ".
				 "          '".$vGroupbyLevel2Name."', '".$vGroupbyLevel3Name."', $vJournalTypeAlltypes, '".$vJournalTypeExclude."', '".$vJournalTypeTypes."', '".$vJournalTypedb."', ".
				 "          '".$vJournalTypecr."', '".$vFromLedgerNumber."', '".$vToLedgerNumber."', '".$vFromClientCode."', '".$vToClientCode."', '".$vFromSupplierCode."', '".$vToSupplierCode."', ".
				 "          '".$vFromSalesmanCode."', '".$vToSalesmanCode."', '".$vFromItemCode."', '".$vToItemCode."', '".$vFromWarehouseCode."', '".$vToWarehouseCode."', ".
				 "          '".$vFromGroupCode."', '".$vToGroupCode."', '".$vFromSubgroupCode."', '".$vToSubgroupCode."', '".$vFromProjectCode."', '".$vToProjectCode."', ".
				 "          '".$vFromMasterProjectCode."', '".$vToMasterProjectCode."', '".$vFromCostcentCode."', '".$vToCostcentCode."', '".$vFromMasterCostcentCode."', ".
				 "          '".$vToMasterCostcentCode."', '".$vFromAreaCode."', Null, $vJournalTypeAlldebit, $vJournalTypeAllcredit, '".$vJournalLedgers."', '".$vUserName."' )";
					 
			 
		$result=mysql_query($query,$connection);
		//echo $query."<br>";exit;	
		
		if ($result){
			$STempId = mysql_insert_id();
			fcommit();
		}else{
			$STempId = "";
			$message  = mysql_error(); 
			frollback();
		}
	
	} //End Insert Case
	
	//Edit Case
	if ($STempId != "" && $vReportName != "" ){
		
		fbegin();
		$query = " UPDATE ReportsOptions_Templates SET ".
				 " Template_Name = '".$vTemplateName."', ".
				 " Groupby_Level1Name = '".$vGroupbyLevel1Name."', Groupby_Level2Name = '".$vGroupbyLevel2Name."', Groupby_Level3Name = '".$vGroupbyLevel3Name."', ".
				 " Groupby_Level1Value = '".$vGroupbyLevel1Value."', Groupby_Level2Value = '".$vGroupbyLevel2Value."', Groupby_Level3Value = '".$vGroupbyLevel3Value."', ".
				 " JournalType_alltypes = $vJournalTypeAlltypes, JournalType_exclude = '".$vJournalTypeExclude."', JournalType_types = '".$vJournalTypeTypes."', ".
				 " JournalType_db = '".$vJournalTypedb."', JournalType_cr = '".$vJournalTypecr."', FromLedger_Number = '".$vFromLedgerNumber."', ToLedger_Number = '".$vToLedgerNumber."', ".
				 " FromClient_Code = '".$vFromClientCode."', ToClient_Code = '".$vToClientCode."', FromSupplier_Code = '".$vFromSupplierCode."', ".
				 " ToSupplier_Code = '".$vToSupplierCode."', FromSalesman_Code = '".$vFromSalesmanCode."', ToSalesman_Code = '".$vToSalesmanCode."', ".
				 " FromItem_Code = '".$vFromItemCode."', ToItem_Code = '".$vToItemCode."', FromWarehouse_Code = '".$vFromWarehouseCode."', ".
				 " ToWarehouse_Code = '".$vToWarehouseCode."', FromGroup_Code = '".$vFromGroupCode."', ToGroup_Code = '".$vToGroupCode."', ".
				 " FromSubgroup_Code = '".$vFromSubgroupCode."', ToSubgroup_Code = '".$vToSubgroupCode."', FromProject_Code = '".$vFromProjectCode."', ".
				 " ToProject_Code = '".$vToProjectCode."', FromMasterProject_Code = '".$vFromMasterProjectCode."', ToMasterProject_Code = '".$vToMasterProjectCode."', ".
				 " FromCostcent_Code = '".$vFromCostcentCode."', ToCostcent_Code = '".$vToCostcentCode."', FromMasterCostcent_Code = '".$vFromMasterCostcentCode."', ".
				 " ToMasterCostcent_Code = '".$vToMasterCostcentCode."', FromArea_Code = '".$vFromAreaCode."', ToArea_Code = Null, ".
				 " AllTypesDebit = $vJournalTypeAlldebit, AllTypesCredit = $vJournalTypeAllcredit, LedgersPerType = '".$vJournalLedgers."' ".
				 " WHERE Template_Id = '".$STempId."' ";
			 
		$result=mysql_query($query,$connection);
		//echo $query."<br>";exit;	
		
		if ($result){
			fcommit();
		}else{
			$message  = mysql_error(); 
			frollback();
		}

	} //End Edit Case
	

} 




//Delete Record Mode 
else if ($SMode == "delete" && $STempId != ''){

	$AllowDelete = 1;
	$message  = '';
	
		
	//If No Constraints Exists On Delete
	if ($AllowDelete == 1) {
	
		$query = " DELETE FROM ReportsOptions_Templates ".
				 " WHERE Template_Id = '".$STempId."' ";
				 
		if (!updateData($connection, $query, true))		 
			$message  = mysql_error(); 
			
		
	}
	
	
}



$message = ereg_replace(' ','%20',$message);

//echo $mode.$message; exit;

if($SMode == "insert" || $SMode == "edit") 
	header ("Location: ./Form_ReportsOptions.php?mainaction=edit&templateid=".$STempId."&message=".$message);
else if ($SMode == "delete" )	
	header ("Location: ./Form_ReportsOptionsSearch.php?message=".$message);

	  	  
function fbegin()
{
	mysql_query("START TRANSACTION");
}
function fcommit(){
	mysql_query("COMMIT");
}
function frollback()
{
	mysql_query("ROLLBACK");
}



print <<<HERE
</form>
</body>

HERE;


?>