<?php

session_start();


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

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


$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");

//User Access
$querylog=" select ifNull(login_belongstogrp,0) As FI_BelongsToGrp from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_Synchro,0) As Module_Synchro From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

$query_setup=" Select base1 As base1, base2 as base2, Setup_Branch from setup";
$result= mysql_query($query_setup,$connection) or die( mysql_error());
while($rowsetup=mysql_fetch_array($result)){
	extract($rowsetup);
}

$Area_Code = ereg_replace("'", "\'", $Area_Code);
$Area_Name = ereg_replace("'", "\'", $Area_Name);
$Area_Country = ereg_replace("'", "\'", $Area_Country);
$cartype = ereg_replace("'", "\'", $cartype);

if (!empty($Area_CreationDate))	{
	$Area_CreationDate  = explodeword($Area_CreationDate );
	$Area_CreationDate   = "'$Area_CreationDate '";

}	
else {$Area_CreationDate = 'null';}

$Area_Code=trim($Area_Code);

if($mainmode=='insert'){
		
	$query="Insert into Area (Area_Code,Area_Name, Area_CreationDate, Area_BelongsToGroup, Owner_Branch,Area_Country,area_type)
			values ('".$Area_Code."', '".$Area_Name."', $Area_CreationDate, $FI_BelongsToGrp, '".$Setup_Branch."','$Area_Country',".GetSQLValueString($areatype,'text')." ) ";

	//echo $query;				
	
	$result  = mysql_query($query, $connection);
	$Increment= mysql_insert_id($connection);
	$message = mysql_error();
    
	if($config['log_file'] == 1){
		$transType = "I";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
		$query_insertToHistory  = "call ".$config['dbName_history'].".InsertToHistory('$transType', '$Area_Code', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'area', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'I','AREA','',$Area_Code,$Setup_Branch);
		}
	}
}

if($mainmode=='delete')
{	
	$query = "select Area_Code from Area where Increment='".$IncrementId."'";
	$result=mysql_query($query,$connection);
	$rows=mysql_fetch_row($result);
	$Area_Code=$rows[0];
	
	if($config['log_file'] == 1){
		$transType = "D";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
		$query_insertToHistory  = "call ".$config['dbName_history'].".InsertToHistory('$transType', '$Area_Code', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'area', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	
	$query="delete from Area where Increment='".$IncrementId."'";
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	$errornum= mysql_errno();
						
	if($errornum=='1451') {
		$message="Delete Not Allowed . Area is used .";
		//echo $message;
	}
	
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'D','AREA','',$Area_Code,$Setup_Branch);
		}
	}
	
}


if($mainmode=='edit'){
	$query_update=" update Area set Area_Code='".$Area_Code."', ".
				  " Area_Name='".$Area_Name."', ".
				  "	Area_CreationDate= $Area_CreationDate , ".
				  "	Area_Country= '$Area_Country',  ".
				  " area_type = ".GetSQLValueString($areatype,'text')." ".
				  "	where Increment='".$Increment."'";
	//echo $query_update;exit;		

	$resultedit = mysql_query($query_update, $connection);
	$message    = mysql_error();

	if($config['log_file'] == 1){
		$transType = "U";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
		$query_insertToHistory  = "call ".$config['dbName_history'].".InsertToHistory('$transType', '$Area_Code', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'area', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','AREA','',$Area_Code,$Setup_Branch);
		}
	}
}

if($actionmode=='submitareatransp'){
	
	if($AreaTransp_ID==''){
		$query="insert into areatransp(Transfer_Amount,Currency,Area_Code,CarType_code,Area_Tariff,area_tarrifvisit)
				values('".$amount."','".$currency."','".$Area_Code."','".$cartype."','".$area_tarif."','".$area_tarrifvisit."')";
		//echo$query;exit;		
		$result = mysql_query($query, $connection);		
	}
	else if($AreaTransp_ID!=''){
		$query="update areatransp set Transfer_Amount='".$amount."',
									Currency='".$currency."',
									Area_Code='".$Area_Code."',
									CarType_code='".$cartype."',
									Area_Tariff='".$area_tarif."',
									area_tarrifvisit='".$area_tarrifvisit."'
				where AreaTransp_ID='".$AreaTransp_ID."'";
		//echo$query;exit;		
		$result = mysql_query($query, $connection);	
	}
}

if($actionmode=='deleteareatransp'){
	$query="delete from areatransp where AreaTransp_ID='".$AreaTransp_ID."'";
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
}

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

if($mainmode=='delete')
	header ("Location: ./Form_Area.php?mainmode=insert&message=".$message);
else 
header ("Location: ./Form_Area.php?Increment=$Increment&mainmode=editmode&message=".$message);

mysql_close($connection);



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

HERE;


?>
		           