<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
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");



$Warehouse_code = $_GET["Warehouse_code"]; if ($Warehouse_code == ""){$Warehouse_code = $_POST["Warehouse_code"];}   
$Warehouse_name = $_GET["Warehouse_name"]; if ($Warehouse_name == ""){$Warehouse_name = $_POST["Warehouse_name"];}   
$Warehouse_CreationDate = $_GET["Warehouse_CreationDate"]; if ($Warehouse_CreationDate == ""){$Warehouse_CreationDate = $_POST["Warehouse_CreationDate"];}   
$Warehouse_code = $_GET["Warehouse_code"]; if ($Warehouse_code == ""){$Warehouse_code = $_POST["Warehouse_code"];}   


$mode = $_GET["mode"]; if ($mode == ""){$mode = $_POST["mode"];}   
$WarehouseId = $_GET["WarehouseId"]; if ($WarehouseId == ""){$WarehouseId = $_POST["WarehouseId"];}   
$Pwarehouse_code = $_GET["Pwarehouse_code"]; if ($Pwarehouse_code == ""){$Pwarehouse_code = $_POST["Pwarehouse_code"];}   
$txtWarehouseType = $_GET["txtWarehouseType"]; if ($txtWarehouseType == ""){$txtWarehouseType = $_POST["txtWarehouseType"];}   

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}



$query_user_name = "select User_Name from ".$_SESSION['dbLabel'].".users where user_account='".$_SESSION['useraccount']."'";
$result_user_name = mysql_query($query_user_name, $connection);
$rows_user_name = mysql_fetch_array($result_user_name);
extract($rows_user_name);

//User Access
$querylog=" select ifNull(login_belongstogrp,0) As FI_BelongsToGrp, ifNull(Login_location,0) As Login_location 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);
}

$Warehouse_code = ereg_replace("'", "\'", $Warehouse_code);
$Warehouse_name = ereg_replace("'", "\'", $Warehouse_name);


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

}	
else {$Warehouse_CreationDate = 'null';}

$Warehouse_code=trim($Warehouse_code);

if($mode=='insert'){
		
	fbegin();
	$Warehouse_Id = get_nextsequence('warehouse_id','',$connection);
	fcommit();
	if($Login_location == 1) {
		
		
		
	$query="Insert into Warehouse (warehouse_id,Warehouse_code,Warehouse_name,Warehouse_BelongsToGroup, Warehouse_transit, Warehouse_CreationDate, Owner_Branch, 
			warehouse_type ";
	if($Pwarehouse_code != '') 	$query=$query." ,warehouse_parentId ";	
	$query=$query." )values ('".$Warehouse_Id."','".$Warehouse_code."', '".$Warehouse_name."', $FI_BelongsToGrp, $Warehouse_transit, $Warehouse_CreationDate, '".$Setup_Branch."',
			'".$txtWarehouseType."' ";
	if($Pwarehouse_code != '') 	$query=$query." , '$Pwarehouse_code' ";			
	$query=$query." )";
	
	
	 
	} else {
		$query="Insert into Warehouse (warehouse_id,Warehouse_code,Warehouse_name,Warehouse_BelongsToGroup, Warehouse_transit, Warehouse_CreationDate, Owner_Branch )values 
		('".$Warehouse_Id."','".$Warehouse_code."', '".$Warehouse_name."', $FI_BelongsToGrp, $Warehouse_transit, $Warehouse_CreationDate, '".$Setup_Branch."' )";
	
		
	
	
	
	}
	

	//echo$query;			exit;	
	//$result_history = mysql_query($query_history, $connection_history);
	$message = mysql_error().$query_history;
	$result  = mysql_query($query, $connection);
	
	if($config['log_file'] == 1){
		$transType = "I";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
		$query_insertToHistory  = "call ".$config['dbName_history'].".InsertToHistory('$transType', '$Warehouse_code', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'warehouse', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	//$Warehouse_Id = mysql_insert_id($connection);
	$message = mysql_error();

	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'I','WAREHOUSE','',$Warehouse_code,$Setup_Branch);
		}
	}		   

}

if($mode=='delete')
{	
	$query = "select Warehouse_Code,Warehouse_name from Warehouse where Warehouse_Id='".$WarehouseId."'";
	$result=mysql_query($query,$connection);
	$rows=mysql_fetch_row($result);
	$Warehouse_Code=$rows[0];
	$Warehouse_name=$rows[1];
	
	if($config['log_file'] == 1){
		$transType = "D";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
		$query_insertToHistory  = "call ".$config['dbName_history'].".InsertToHistory('$transType', '$Warehouse_Code', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'warehouse', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	
	$query="delete from Warehouse where Warehouse_Id='".$WarehouseId."'";
	//echo$query;exit;
	 if($config['log_file'] == 1){
							
							$query_update_history="insert into Warehouse
							(Warehouse_name,Warehouse_Code
						 ,warehouse_date, warehouse_operator_id, warehouse_schema, warehouse_status)
							values('".$Warehouse_name."','".$Warehouse_Code."',
							now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
						$resultedit_history = mysql_query($query_update_history, $connection_history);
						$message = mysql_error();
						}
	
	
	$result  = mysql_query($query, $connection);
	$message = mysql_error();

	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'D','WAREHOUSE','',$Warehouse_Code,$Setup_Branch);
		}
	}
}


if($mode=='edit'){
	if($Login_location == 1)
	{
		$query_update=" update Warehouse set Warehouse_code='".$Warehouse_code."', ".
				  "	Warehouse_name='".$Warehouse_name."' , ".
				  "	Warehouse_transit = $Warehouse_transit ,".
				  " Warehouse_CreationDate= $Warehouse_CreationDate".
				  " , warehouse_type= '$txtWarehouseType' ".
				  "	where Warehouse_Id='".$Warehouse_Id."'";
				  
				 
				  
	}
else {				  
	$query_update=" update Warehouse set Warehouse_code='".$Warehouse_code."', ".
				  "	Warehouse_name='".$Warehouse_name."' , ".
				  "	Warehouse_transit = $Warehouse_transit ,".
				  " Warehouse_CreationDate= $Warehouse_CreationDate".
				  "	where Warehouse_Id='".$Warehouse_Id."'";
	//echo $query_update;exit;		
	
	
	 
	
	
}
//$resultedit_history = mysql_query($query_update_history, $connection_history);
	$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', '$Warehouse_code', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'warehouse', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','WAREHOUSE','',$Warehouse_code,$Setup_Branch);
		}
	}
}



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

if($mode=='delete')			   
	header ("Location: ./Form_Warehouse.php?mode=insert&message=".$message);
else
	header ("Location: ./Form_Warehouse.php?Warehouse_Id=$Warehouse_Id&Warehouse_code=$Warehouse_code&Warehouse_name=$Warehouse_name&mode=editmode&message=".$message);

mysql_close($connection);

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


?>