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


//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 Setup_Branch from setup";
$result= mysql_query($query_setup,$connection) or die( mysql_error());
while($rowsetup=mysql_fetch_array($result)){
	extract($rowsetup);
}

/// Group details
$query_grouping="select inventory_ledger,Charge_ledger,Revenue_ledger,Promotion_ledger,Discount_Ledger,Vat_ledger,group_code from grouping
				where Increment='".$Increment."'";
$result_grouping= mysql_query($query_grouping, $connection);
$row=mysql_fetch_array($result_grouping);

$inventory_ledger=$row[0];
$charge_ledger=$row[1];
$revenue_ledger=$row[2];
$promotion_ledger=$row[3];
$discount_ledger=$row[4];
$vat_ledger=$row[5];	
$group_code=$row[6];
				
$update_transsetup="update transactionsetup set Trans_directStkDbt='".$inventory_ledger."'";
		
	if($code1=='SI' || $code1=='SR')
		$update_transsetup=$update_transsetup." ,Trans_RMAcc='".$revenue_ledger."'";
					
	if($code1=='SI' || $code1=='AD' || $code1=='PD' || $code1=='SE' )	
		$update_transsetup=$update_transsetup.",trans_directstkcrd='".$charge_ledger."'";		
	
	if($code1=='PI')
		$update_transsetup=$update_transsetup.",Trans_VatAccount='".$vat_ledger."'";
		
	$update_transsetup=$update_transsetup." where group_code='".$group_code."' and Journal_type='".$code1."'";	
	
	$result_update_transsetup= mysql_query($update_transsetup, $connection);		

if ($result_update_transsetup) {
	if ($Module_Synchro == 1) {
		InsertSynchroLoc($connection,'U','GROUPING','',$group_code,$Setup_Branch);
	}
}	


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

header ("Location: ./Form_Grouping_TransactionTypes.php?closewindow=1&mode=view&message=".$message);

mysql_close($connection);