<?php
session_start();
 header("Content-type: text/html; charset=windows-1256");
if($_SESSION['dbName']==""){
header("location:../index.php");
}
include("config.inc.php");

include("Functions_UserAccess.php");
include("function.inc.php");
include("function2.inc.php");
$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'])
		or die("Could not connect to DB");
 mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");
		
		$actionmode=$_GET ['actionmode'];
$transaction= $_GET ['transaction'];
$debit= $_GET ['debitnumber'];
$type =$_GET['type'];
$synchro=$_GET['synchro'];

$olddebit= $_GET ['olddebitnumber'];

if ($actionmode=="insert"){
	
	$insert="insert into closingtemplate values ('A',$debit,'$debit',$synchro,'$type')";
	
	mysql_query($insert, $connection) or die(mysql_error());
	draw_closingtemplate($connection);
}

if ($actionmode=="delete"){

	$delete = "delete from closingtemplate where Debit_LedgerNumber=$debit  ";
			mysql_query($delete, $connection) or die(mysql_error());
			
			draw_closingtemplate($connection);
			
}	
 if ($actionmode=="update"){

	 $update="update closingtemplate SET Debit_LedgerNumber=$debit,synchronized=$synchro,transaction_type='$type'
			where Debit_LedgerNumber=$olddebit ";
	
			mysql_query($update, $connection) or die(mysql_error());
			
			draw_closingtemplate($connection);
			

}
if ($actionmode=="SV_closingperiod"){
	 
	$querybelongs = "select distinct journal_belongstogroup from journal";
	$resultBelongs = mysql_query($querybelongs, $connection);

	while($rowBelongs = mysql_fetch_array($resultBelongs)){
		$fromdate = $_GET['fromdate'];
		$todate = $_GET['todate'];
		$debitledger = $_GET['debitledger'];

		$fromdate = explodeword($fromdate);
		$fromdate = $fromdate." 00:00:00";
		$fromdate = "'".$fromdate."'";
		$todate = explodeword($todate);
		$todate = $todate." 23:59:59";
		$todate = "'".$todate."'"; 
		$resultvars = mysql_query("set @id :=0 ", $connection);
		// $JournalId = get_nextsequence('journal_id','',$connection);
		$query="call StockVariation_Closing(".$fromdate.",".$todate.",".$debitledger.",'".$Base1."','".$Base2."','".$_SESSION['dbName']."','".$_SESSION['dbLabel']."','".$_SESSION['useraccount']."','".$_SESSION['username']."','".$rowBelongs[0]."',@id) ";
		// echo $query;exit;
		$result=mysql_query($query,$connection) or die(mysql_error($connection));
}
	if($result)
		header('Location: form_utilities.php?message=SV Closed Successfully');
	else
		header('Location: form_utilities.php?message='.mysql_error());
}
if ($actionmode=="year_closing"){
	 
	 
	$querybelongs = "select distinct journal_belongstogroup from journal";
	$resultBelongs = mysql_query($querybelongs, $connection);

	while($rowBelongs = mysql_fetch_array($resultBelongs)){
	$fromdate = $_GET['fromdate'];
	$todate = $_GET['todate'];
	$debitledger = $_GET['debitledger'];

	$fromdate = explodeword($fromdate);
	$fromdate = $fromdate." 00:00:00";
	$fromdate = "'".$fromdate."'";
	$todate = explodeword($todate);
	$todate = $todate." 23:59:59";
	$todate = "'".$todate."'"; 
	$resultvars = mysql_query("set @id :=0 ", $connection);
	// $JournalId = get_nextsequence('journal_id','',$connection);
	$query="call Closing_Year(".$fromdate.",".$todate.",'".$Base1."','".$Base2."','".$_SESSION['dbName']."','".$_SESSION['dbLabel']."','".$_SESSION['useraccount']."','".$_SESSION['username']."','".$FI_BelongsToGrp."',".$debitledger.",@id) ";
	$result=mysql_query($query,$connection) or die(mysql_error($connection));

	}
	if($result)
	header('Location: form_utilities.php?message=Year Closed Successfully');
else
	header('Location: form_utilities.php?message='.mysql_error());
}


function explodeword($date)
{

$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
return $trim;
}


