<?php

session_start();


if($HTTP_SESSION_VARS['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($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");



$Category_CreationDate = $_POST['Category_CreationDate']; if($Category_CreationDate == '') $Category_CreationDate = $_GET['Category_CreationDate'];
$Category_Code = $_POST['Category_Code']; if($Category_Code == '') $Category_Code = $_GET['Category_Code'];
$Category_name = $_POST['Category_name']; if($Category_name == '') $Category_name = $_GET['Category_name'];
$Category_Price1 = $_POST['Category_Price1']; if($Category_Price1 == '') $Category_Price1 = $_GET['Category_Price1'];
$Category_Price2 = $_POST['Category_Price2']; if($Category_Price2 == '') $Category_Price2 = $_GET['Category_Price2'];
$Category_Price3 = $_POST['Category_Price3']; if($Category_Price3 == '') $Category_Price3 = $_GET['Category_Price3'];
$Category_Price4 = $_POST['Category_Price4']; if($Category_Price4 == '') $Category_Price4 = $_GET['Category_Price4'];
$Category_Price5 = $_POST['Category_Price5']; if($Category_Price5 == '') $Category_Price5 = $_GET['Category_Price5'];
$Category_Price6 = $_POST['Category_Price6']; if($Category_Price6 == '') $Category_Price6 = $_GET['Category_Price6'];
$txtKeyword = $_POST['txtKeyword']; if($txtKeyword == '') $txtKeyword = $_GET['txtKeyword'];
$IncrementId     = $_POST["IncrementId"]; if ($IncrementId == ""){$IncrementId = $_GET["IncrementId"];}  

$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 from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
    while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}
$category_length	= $_POST["category_length"]; if ($category_length == ""){$category_length = $_GET["category_length"];} 
if (empty($category_length)) $category_length = 0;

//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);
}

$Category_Code = ereg_replace("'", "\'", $Category_Code);
$Category_name = ereg_replace("'", "\'", $Category_name);

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

}	
else {$Category_CreationDate = 'null';}

$Category_Code=trim($Category_Code);
$category_inactivedate = date('Y-m-d', strtotime($category_inactivedate));

if($mode=='insert'){
		
	$query="Insert into Category (Category_Code, Category_name, Category_Price1, Category_Price2,Category_Price3,Category_Price4,Category_Price5,Category_Price6, Category_BelongsToGroup, Category_Posted, Owner_Branch, category_length, Category_namearabic,category_active)
			values ('".$Category_Code."', '".$Category_name."'
				, ".GetSQLValueString($Category_Price1,'double')."
				, ".GetSQLValueString($Category_Price2,'double')."
				,".GetSQLValueString($Category_Price3,'double')."
				,".GetSQLValueString($Category_Price4,'double')."
				,".GetSQLValueString($Category_Price5,'double')."
				,".GetSQLValueString($Category_Price6,'double')."
				, $FI_BelongsToGrp
				
				, '".$Setup_Branch."'
				, ".GetSQLValueString($category_length,'double').", '$Category_namearabic',";
				if($category_active==1) 
					$query .= "'1', NULL)";
				else
					$query .= "'0', '$category_inactivedate')";
			
  //echo $query;exit;
	
	$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', '$Category_Code', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'category', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	$Increment= mysql_insert_id($connection);
	$message = mysql_error();
	
	

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

}

if($mode=='delete')
{	
	$query = "select Category_Code,Category_name from Category where Increment='".$IncrementId."'";
	$result=mysql_query($query,$connection);
	$rows=mysql_fetch_row($result);
	$Category_Code=$rows[0];
	$Category_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', '$Category_Code', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'category', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	$query="delete from Category where Increment='".$IncrementId."'";
	//echo$query;exit;
	
	
	
	
	$result  = mysql_query($query, $connection);
	$message = mysql_error();

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


if($mode=='edit'){
	$query_update=" update Category set Category_Code='".$Category_Code."', Category_namearabic = '$Category_namearabic', ".
				  " Category_name='".$Category_name."', ".
				  "	Category_Price1= ".GetSQLValueString($Category_Price1,'double').", ".
				  "	Category_Price2= ".GetSQLValueString($Category_Price2,'double').", ".
				  "	Category_Price3= ".GetSQLValueString($Category_Price3,'double').", ".
				  "	Category_Price4= ".GetSQLValueString($Category_Price4,'double').", ".
				  "	Category_Price5= ".GetSQLValueString($Category_Price5,'double').", ".
				  "	Category_Price6= ".GetSQLValueString($Category_Price6,'double').",".
				  "	category_length= ".GetSQLValueString($category_length,'double').",".
                  " Category_CreationDate=$Category_CreationDate ,";
					if($category_active==1)
						$query_update .= "category_active='$category_active', category_inactivedate=NULL ";
					else
						$query_update .= "category_active='$category_active', category_inactivedate='$category_inactivedate' ";
					$query_update .= "where Increment='".$Increment."'";
	//echo $query_update;exit;		
		

	$resultedit = mysql_query($query_update, $connection);
	if($config['log_file'] == 1){
		$transType = "U";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
		$query_insertToHistory  = "call ".$config['dbName_history'].".InsertToHistory('$transType', '$Category_Code', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'category', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	$message    = mysql_error();

	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','CATEGORY','',$Category_Code,$Setup_Branch);
		}
	}

}

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

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

mysql_close($connection);



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

HERE;


?>
		           