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

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

$access_name = ereg_replace("'", "\'", $access_name);

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

}	
else {$Category_CreationDate = 'null';}
*/
$mode = $_GET['mode'];
$access_fromdate = date('Y-m-d', strtotime($access_fromdate));
if($access_todate=="") 
	$access_todate="NULL"; 
else
	$access_todate = "'".date('Y-m-d', strtotime($access_todate))."'";
$access_id = $_GET['accId'];


if($mode=='insert'){
	
	$query_insert="Insert into ".$HTTP_SESSION_VARS['dbLabel'].".access (access_type, access_code, access_name, access_password, access_remark, access_fromdate,
	            access_todate, access_inactivereason, access_group, access_shift,access_language, access_active,
				access_belongstogrp, access_DefaultComp, access_DefaultYear, cashregister, admin, access_free_items,
				access_menu_sorder, access_menu_sales, access_menu_sreturn, access_menu_receipt, access_menu_aging, access_menu_comments, access_menu_deliverystatus, access_menu_routend, access_menu_routsummary, 
				access_menu_routweekly, access_menu_synchro, access_sync_sendsorder, access_sync_getdata, access_menu_clientinfo, ACCESS_RECEIPT_BASE2, printer_brand, printer_name, access_change_sorder_price, access_show_sorder_cost, access_change_sale_price, access_show_sale_cost)
			values (".GetSQLValueString($access_type,'int').", '".$access_code."', '".$accName."', '".$accPass."', '".$access_remark."', '".$access_fromdate."'
				, ".$access_todate.", '".$access_inactivereason."', ".GetSQLValueString($access_group,'int').", ".GetSQLValueString($access_shift,'int')."
				, ".GetSQLValueString($access_language,'int').", ";
				if(isset($_POST['access_active'])) 
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				$query_insert .= " ".GetSQLValueString($access_belongstogrp,'int').", '".$access_DefaultComp."'
				, '".$access_DefaultYear."', '".$cashregister."', ";
				if(isset($_POST['admin']))
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_free_items']))
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_menu_sorder'])) 
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_menu_sales'])) 
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_menu_sreturn']) )
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_menu_receipt']) )
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_menu_aging'])) 
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_menu_comments'])) 
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_menu_deliverystatus']))
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_menu_routend']))
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_menu_routsummary'])) 
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_menu_routweekly'])) 
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_menu_synchro']) )
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_sync_sendsorder']) )
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_sync_getdata'])) 
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_menu_clientinfo'])) 
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['ACCESS_RECEIPT_BASE2'])) 
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				$query_insert .= "'".$PrinterBrand."', '".$PrinterName."', "; 
				if(isset($_POST['access_change_sorder_price'])) 
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_show_sorder_cost'])) 
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_change_sale_price'])) 
					$query_insert .= '1, ';
				else 
					$query_insert .= '0, ';
				if(isset($_POST['access_show_sale_cost'])) 
					$query_insert .= '1 ';
				else 
					$query_insert .= '0 ';
				$query_insert .= ")";
				
	//echo $query_insert;exit;
	$result_insert  = mysql_query($query_insert, $connection);
	//$access_id= mysql_insert_id($connection);
	$message = mysql_error();
}

if($mode=='delete'){	
	$query_delete="delete from ".$HTTP_SESSION_VARS['dbLabel'].".access where access_id='".$access_id."'";
	//echo$query_delete;exit;
	$result_delete = mysql_query($query_delete, $connection);
	$message_delete = mysql_error();
}


if($mode=='edit'){
	if($access_todate=="")
		$access_todate="null";
	$query_update=" update ".$HTTP_SESSION_VARS['dbLabel'].".access set access_type=".GetSQLValueString($access_type,'int').", access_code='".$access_code."', access_name='".$accName."', access_password='".$accPass."', access_remark='".$access_remark."'
				, access_fromdate='".$access_fromdate."', access_todate=".$access_todate.", access_inactivereason='".$access_inactivereason."', access_group=".GetSQLValueString($access_group,'int').", access_shift=".GetSQLValueString($access_shift,'int')."
				, access_language=".GetSQLValueString($access_language,'int').", ";
				if(isset($_POST['access_active'])) 
					$query_update .= "access_active='1', ";
				else 
					$query_update .= "access_active=0, ";
				$query_update .= "access_belongstogrp=".GetSQLValueString($access_belongstogrp,'int').", access_DefaultComp='".$access_DefaultComp."'
				, access_DefaultYear='".$access_DefaultYear."', cashregister='".$cashregister."', printer_brand='".$PrinterBrand."', printer_name='".$PrinterName."',";
				if(isset($_POST['admin'])) 
					$query_update .= 'admin=1, ';
				else 
					$query_update .= 'admin=0, ';
				if(isset($_POST['access_free_items'])) 
					$query_update .= 'access_free_items=1, ';
				else 
					$query_update .= 'access_free_items=0, ';
				if(isset($_POST['access_menu_sorder'])) 
					$query_update .= 'access_menu_sorder=1, ';
				else 
					$query_update .= 'access_menu_sorder=0, ';
				if(isset($_POST['access_menu_sales'])) 
					$query_update .= 'access_menu_sales=1, ';
				else 
					$query_update .= 'access_menu_sales=0, ';
				if(isset($_POST['access_menu_sreturn']))
					$query_update .= 'access_menu_sreturn=1, ';
				else 
					$query_update .= 'access_menu_sreturn=0, ';
				if(isset($_POST['access_menu_receipt'])) 
					$query_update .= 'access_menu_receipt=1, ';
				else 
					$query_update .= 'access_menu_receipt=0, ';
				if(isset($_POST['access_menu_aging'])) 
					$query_update .= 'access_menu_aging=1, ';
				else 
					$query_update .= 'access_menu_aging=0, ';
				if(isset($_POST['access_menu_comments'])) 
					$query_update .= 'access_menu_comments=1, ';
				else 
					$query_update .= 'access_menu_comments=0, ';
					if(isset($_POST['access_menu_deliverystatus'])) 
					$query_update .= 'access_menu_deliverystatus=1, ';
				else 
					$query_update .= 'access_menu_deliverystatus=0, ';
				if(isset($_POST['access_menu_routend'])) 
					$query_update .= 'access_menu_routend=1, ';
				else 
					$query_update .= 'access_menu_routend=0, ';
				if(isset($_POST['access_menu_routsummary'])) 
					$query_update .= 'access_menu_routsummary=1, ';
				else 
					$query_update .= 'access_menu_routsummary=0, ';
				if(isset($_POST['access_menu_routweekly'])) 
					$query_update .= 'access_menu_routweekly=1, ';
				else 
					$query_update .= 'access_menu_routweekly=0, ';
				if(isset($_POST['access_menu_synchro']))
					$query_update .= 'access_menu_synchro=1, ';
				else 
					$query_update .= 'access_menu_synchro=0, ';
				if(isset($_POST['access_sync_sendsorder'])) 
					$query_update .= 'access_sync_sendsorder=1, ';
				else 
					$query_update .= 'access_sync_sendsorder=0, ';
				if(isset($_POST['access_sync_getdata'])) 
					$query_update .= 'access_sync_getdata=1, ';
				else 
					$query_update .= 'access_sync_getdata=0, ';
				if(isset($_POST['access_menu_clientinfo'])) 
					$query_update .= 'access_menu_clientinfo=1, ';
				else 
					$query_update .= 'access_menu_clientinfo=0, ';
				if(isset($_POST['access_change_sorder_price'])) 
					$query_update .= 'access_change_sorder_price=1, ';
				else 
					$query_update .= 'access_change_sorder_price=0, ';
				if(isset($_POST['access_show_sorder_cost'])) 
					$query_update .= 'access_show_sorder_cost=1, ';
				else 
					$query_update .= 'access_show_sorder_cost=0, ';
				if(isset($_POST['access_change_sale_price'])) 
					$query_update .= 'access_change_sale_price=1, ';
				else 
					$query_update .= 'access_change_sale_price=0, ';
				if(isset($_POST['access_show_sale_cost'])) 
					$query_update .= 'access_show_sale_cost=1, ';
				else 
					$query_update .= 'access_show_sale_cost=0, ';
				if(isset($_POST['ACCESS_RECEIPT_BASE2'])) 
					$query_update .= 'ACCESS_RECEIPT_BASE2=1 ';
				else 
					$query_update .= 'ACCESS_RECEIPT_BASE2=0 ';
					
				$query_update .= "where access_id='".$access_id."'";
	//echo $query_update;exit;		

	$result_update = mysql_query($query_update, $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_MobileAccess.php?mode=insert&message=".$message);
else
	if($mode=='edit')	
		header ("Location: ./Form_MobileAccess.php?mode=view&accId=".$access_id."&message=".$message);
		else
			if($mode=='insert')		
				header ("Location: ./Form_MobileAccess.php?mode=insert&message=".$message);
mysql_close($connection);



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

HERE;


?>
		           