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


$SubGroup_policy     = $_POST["SubGroup_policy"]; if ($SubGroup_policy == ""){$SubGroup_policy = $_GET["SubGroup_policy"];}  
$buttondimwidth     = $_POST["buttondimwidth"]; if ($buttondimwidth == ""){$buttondimwidth = $_GET["buttondimwidth"];}  



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

$HideMenu     = $_POST["HideMenu"]; if ($HideMenu == ""){$HideMenu = $_GET["HideMenu"];}   
$actionmode   = $_GET["actionmode"]; if ($actionmode == ""){$actionmode = $_POST["actionmode"];}   
$mode   	  = $_GET["mode"]; if ($mode == ""){$mode = $_POST["mode"];}   
$message 	  = $_GET["message"]; if ($message == ""){$message = $_POST["message"];}
$Increment    = $_GET["Increment"]; if ($Increment == ""){$Increment = $_POST["Increment"];} 
$IncrementId  = $_GET["IncrementId"]; if ($IncrementId == ""){$IncrementId = $_POST["IncrementId"];} 

$txtKeyword  = $_POST["txtKeyword"]; if ($txtKeyword == ""){$txtKeyword = $_GET["txtKeyword"];}   
$vTabFocus   = $_POST["vTabFocus"]; if ($vTabFocus == ""){$vTabFocus = $_GET["vTabFocus"];} 


$Subgroup_CreationDate	= $_POST["Subgroup_CreationDate"]; if ($Subgroup_CreationDate == ""){$Subgroup_CreationDate = $_GET["Subgroup_CreationDate"];} 
$SubGroup_Code    	= $_POST["SubGroup_Code"]; if ($SubGroup_Code == ""){$SubGroup_Code = $_GET["SubGroup_Code"];} 
$SubGroup_Name 		= $_POST["SubGroup_Name"]; if ($SubGroup_Name == ""){$SubGroup_Name = $_GET["SubGroup_Name"];} 
$group_code 		= $_POST["group_code"]; if ($group_code == ""){$group_code = $_GET["group_code"];} 
$group_name 		= $_POST["group_name"]; if ($group_name == ""){$group_name = $_GET["group_name"];} 
$SubGroup_Parent	= $_POST["SubGroup_Parent"]; if ($SubGroup_Parent == ""){$SubGroup_Parent = $_GET["SubGroup_Parent"];} 
$SubGroup_Type		= $_POST["SubGroup_Type"]; if ($SubGroup_Type == ""){$SubGroup_Type = $_GET["SubGroup_Type"];} 
$SubGroup_Color		= $_POST["SubGroup_Color"]; if ($SubGroup_Color == ""){$SubGroup_Color = $_GET["SubGroup_Color"];} 
$subgroup_order		= $_POST["subgroup_order"]; if ($subgroup_order == ""){$subgroup_order = $_GET["subgroup_order"];} 
$subgroup_length	= $_POST["subgroup_length"]; if ($subgroup_length == ""){$subgroup_length = $_GET["subgroup_length"];} 
$SubGroup_policy	= $_POST["SubGroup_policy"]; if ($SubGroup_policy == ""){$SubGroup_policy = $_GET["SubGroup_policy"];} 
    
$SubGroup_Code=ereg_replace("'", "\'", $SubGroup_Code);
$SubGroup_Name=ereg_replace("'", "\'", $SubGroup_Name);
$SubGroup_Parent=ereg_replace("'", "\'", $SubGroup_Parent);
$SubGroup_Type=ereg_replace("'", "\'", $SubGroup_Type);
$group_code=ereg_replace("'", "\'", $group_code);

if (empty($subgroup_length)) $subgroup_length = 0;



if(empty($group_code))
  $group_code= 'null' ;
else
  $group_code="'$group_code'";

if($SubGroup_Parent=='')
  $SubGroup_Parent='null';
else
  $SubGroup_Parent="'$SubGroup_Parent'";
  
if($SubGroup_Type=='')
  $SubGroup_Type='null';
else
  $SubGroup_Type="'$SubGroup_Type'";
  
if($SubGroup_Type=='')
  $SubGroup_Color='null';
else
  $SubGroup_Color="'$SubGroup_Color'";
  
  
if($subgroup_order=='')
  $subgroup_order='null';
 
if($SubGroup_policy=='')
  $SubGroup_policy='null';
else
  $SubGroup_policy="'$SubGroup_policy'";
 
if($buttondimwidth == '') $buttondimwidth="null"; 
if($buttondimheight == '') $buttondimheight="null"; 

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

}	
else {$Subgroup_CreationDate = 'null';}


$SubGroup_Code=trim($SubGroup_Code);

$ErrorExists = 0;


if($mode=='insert' || $mode=='edit'){
	//$image = 'Null';
	if (isset($_FILES['photo']) && $_FILES['photo']['size'] > 0) { 

		$image_name = addslashes($_FILES['photo']['name']);
		$image_size = getimagesize($_FILES['photo']['tmp_name']);
		$image_type =$_FILES['photo']['type'];
		//	echo$_FILES['photo']['size'];
			
		if ($image_size == FALSE) {
			$error['photo'] = "That's not an image.";
		
		}
		else {
			if ($_FILES['photo']['size'] > 204800) {
				$error['photo'] = "Image is too large.";
			}
			else {
				$image = addslashes(file_get_contents($_FILES['photo']['tmp_name']));
			}
		}	
	}

	
	//Check For Duplicate Code
	$query_testcode="select count(*) from subgroup where subgroup_code='".$SubGroup_Code."' ";
		
	if($mode=='edit') $query_testcode=$query_testcode." and increment<>'".$Increment."'";
	//echo$query_testcode;exit;
	$result_testcode=mysql_query($query_testcode, $connection);
	$row_code=mysql_fetch_row($result_testcode);
	$num=$row_code[0];
	
	if($num>0){
		$message="Code Already exists";
		$ErrorExists = 1;
	}
		

}


if($mode=='insert' && $ErrorExists == 0){
		
	
	$query="Insert into SubGroup (SubGroup_Code,SubGroup_Name,group_code,SubGroup_BelongsToGroup,Subgroup_CreationDate,parent_code, SubGroup_Type, SubGroup_Color, SubGroup_Picture, Owner_Branch,subgroup_order,subgroup_length,button_dimwidth,button_dimheight)
			values ('".$SubGroup_Code."', '".$SubGroup_Name."', $group_code, $FI_BelongsToGrp, $Subgroup_CreationDate, $SubGroup_Parent, $SubGroup_Type, $SubGroup_Color, '$image', '".$Setup_Branch."',$subgroup_order,$subgroup_length,$buttondimwidth,$buttondimheight)";
$result  = mysql_query($query, $connection);
	$Increment= mysql_insert_id($connection);
	$message = mysql_error();
			 
			
//ECHO $query			; EXIT;
	
	if($config['log_file'] == 1){
		$transType = "I";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
		$query_insertToHistory  = "call ".$config['dbName_history'].".InsertToHistory('$transType', '$SubGroup_Code', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'subgroup', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'I','SUBGROUP','',$SubGroup_Code,$Setup_Branch);
		}
	}

}

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

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


if($mode=='edit' && $ErrorExists == 0){
	$query_update=" update SubGroup set SubGroup_Code='".$SubGroup_Code."',".
				  "	SubGroup_Name='".$SubGroup_Name."',".
				  "	group_code = $group_code ,".
				  "	parent_code = $SubGroup_Parent, ".
				  " SubGroup_Type = $SubGroup_Type, ".
				  " SubGroup_Color = $SubGroup_Color, ".
				  " subgroup_order = $subgroup_order, ".
				  " SubGroup_Picture = '$image' ".
				  " ,subgroup_length = '$subgroup_length' ".
				  " ,button_dimwidth = $buttondimwidth ".
				  " ,button_dimheight = $buttondimheight ".
				  "	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', '$SubGroup_Code', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'subgroup', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
						

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

if($mode == 'checktype'){
	$subgtype = $_GET['subgtype'];
	
	if($subgtype == 'PLU'){
		echo 1;
	} else echo 0;
	exit;
}
$message = ereg_replace(' ','%20',$message);

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

mysql_close($connection);




?>