<?php

session_start();

//echo $costcent;

if($_SESSION['dbName']==""){
header("location:../index.php");
}



include("config.inc.php");

include("function.inc.php");
//echo $actionmoded;
//exit;
$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");


//Set Time Execution
ini_set('max_execution_time', $config['ReportTimeExecution']);



//Read From Module Table
$querymodule="Select ifNull(Module_TransferWithOneWh,0) As Module_TransferWithOneWh,ifNull(Module_NegativeQuantity,0) As Module_NegativeQuantity,ifnull(Module_ZeroQuantity,0) as Module_ZeroQuantity 
				,ifNull(Module_DoubleUnit,0) As Module_DoubleUnit ,ifnull(Module_Unit1Method,0), ifNull(Module_TrequestAutoApprove,0) As Module_TrequestAutoApprove,
				ifNull(auto_approve,0) As auto_approve	From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

	$querylog=" select ifNull(FI_TransferRequest,0) As FI_TransferRequest ,login_belongstogrp ,ifNull(Login_negativeQty,0) As Login_negativeQty".
			  " 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);
											 }
		
if ($TransType=='')
	$TransType = 'TR';
	
$AllowSave = 1;
//Check User Access For Date Modification/ Lock Period
	$MainCompanyName=explode('_',$_SESSION['dbName']);

	$sub_query= " Select ifNull(AllowLockedPeriod,0) As AllowLockedPeriod, LockedPeriod As LockedPeriodDate, ifNull(LockedPeriodDays,0) As LockedPeriodDays ".
				" From ".$_SESSION['dbLabel'].".ListUserDir  ".
				" Where Usercode='".$_SESSION['useraccount']."' ".
				" And Upper(CompanyName) = Upper('".$MainCompanyName[0]."') ".
				" And Upper(Diryear) = Upper('".$MainCompanyName[1]."') ";
	//echo $sub_query."<br>";			  
	$sub_result = mysql_query($sub_query,$connection) or die(mysql_error());
	$rowsperiod = mysql_num_rows($sub_result);

	while($sub_rows=mysql_fetch_array($sub_result)){
		extract($sub_rows);
	}	
	
	if ($rowsperiod > 0 && $AllowLockedPeriod == 1) { 
		if (!(empty($LockedPeriodDate)) && $LockedPeriodDate != '' && insertMySQLDate($transferrequest_date) < $LockedPeriodDate) $ExceedLockPeriod = 1;
		if (dateDiff(insertMySQLDate(displayToday()),insertMySQLDate($transferrequest_date)) > $LockedPeriodDays) $ExceedLockPeriod = 1;
		
		//if ($AllowSave == 0) $message='Save not allowed. Exceed Lock Period.';
		if ($ExceedLockPeriod == 1) {
			$AllowSave = 0;
			$message='Save not allowed. Exceed Lock Period.';
		}
	}
	

	
$transferdtrequest_remark = ereg_replace("'", "\'", trim($transferrequestdt_remark));
$p_transferdtrequest_remark = ereg_replace("'", "\'", trim($p_transferrequestdt_remark));



		
		if ($p_item_quantity=='')
			$p_item_quantity=0;
		
		if ($p_item_sellingprice1=='')
			$p_item_sellingprice1=0;


		$fromproject1 = $fromproject;
		$toproject1 = $toproject;
		
		if ($STrqstDtPackagingno=='')
			$STrqstDtPackagingno=0;
		if ($STrqstDtNbrunit=='')
			$STrqstDtNbrunit=0;

$costcent = "".$costcent;			
if ($costcent == '')
		$costcent1 = 'null';
	else
		$costcent1 = "'".$costcent."'";

$tocostcent = "".$tocostcent;	
if ($tocostcent == '')
		$tocostcent1 = 'null';
	else
		$tocostcent1 = "'".$tocostcent."'";

$fromproject = "".$fromproject;
if ($fromproject == '')
		$fromproject = 'null';
else
	$fromproject = "'".$fromproject."'";

$toproject = "".$toproject;	
if ($toproject == '')
		$toproject = 'null';
else
	$toproject = "'".$toproject."'";

		if ($AllowSave==1) 
		if ($transferrequest_id!='' and $actionmode!='search' and ($actionmoded=='d' or $actionmoded=='i'))
		{
		
		$transferrequest_date = $_GET["value7"];	
		$fromproject = $_GET["value8"];
		$toproject = $_GET["value11"];	
		$costcent = $_GET["value9"];
		$tocostcent = $_GET["value12"];
		$documentrefno = $_GET["value10"];
		$fromwarehouse = $_GET["value13"];
		$towarehouse = $_GET["value14"];
		$drivername = $_GET["value15"];
		$transfer_remark = $_GET["value16"];
		$vehicle_no = $_GET["value18"];
		
		
		
		$upd = "Update temp_transferrequest set 
					 transferrequest_date = CONCAT('".displayParadoxDate(insertDate($transferrequest_date))." ', RIGHT(NOW(), 8))
					,Transferrequest_Remark = '".ereg_replace("'","\'",$transfer_remark)."'
					,Project_code1 = ".GetSQLValueString($fromproject,'text')."
					,Project_code2 = ".GetSQLValueString($toproject,'text')."			
					,Costcenter_Code = ".GetSQLValueString($costcent,'text')."
					,ToCostcenter_Code = ".GetSQLValueString($tocostcent,'text')."
					,Document_RefNo = ".GetSQLValueString($documentrefno,'text')."
					,Driver_Name = ".GetSQLValueString($drivername,'text')."
					,vehicle_no = ".GetSQLValueString($vehicle_no,'text')."";
					if ($Module_TransferWithOneWh==1)
					$upd=$upd." ,Transferrequest_WarehouseFrom = ".GetSQLValueString($fromwarehouse,'text')."
					,Transferrequest_WarehouseTo = ".GetSQLValueString($towarehouse,'text')."";
					
					$upd = $upd." where transferrequest_id = ".$transferrequest_id
					;
		
	//echo $upd;
		$result1=mysql_query($upd,$connection) or die(mysql_error());
					
		if ($Module_TransferWithOneWh==1){			
		$upd = "Update temp_transferrequestdt set ";
		$upd = $upd."TransferrequestDt_WarehouseFrom = '".$fromwarehouse."'";
		$upd = $upd.",TransferrequestDt_Warehouseto = '".$towarehouse."'";
		$upd = $upd."where transferrequest_id = ".$transferrequest_id;
		$result1=mysql_query($upd,$connection) or die(mysql_error());
		}
		
		
		}		

	if ($actionmoded=='mi')
	{
		if ($AllowSave==0) {
			header ("Location: ./Form_TransferRequest.php?actionmode=post&message=".$message."&transferrequest_id=".$transferrequest_id."&TransType=".$TransType);	
			exit;
			}
		$fromwarehouse = $_GET["fromwarehouse"];
		$towarehouse = $_GET["towarehouse"];
		
		$upd_Tr = "Update temp_transferrequest set 
					 transferrequest_date = CONCAT('".displayParadoxDate(insertDate($transferrequest_date))." ', RIGHT(NOW(), 8))
					,Transferrequest_Remark = '".ereg_replace("'","\'",$transferrequest_remark)."'
					,Project_code1 = ".$fromproject."
					,Project_code2 = ".$toproject."			
					,Costcenter_Code = ".$costcent1."
					,ToCostcenter_Code = ".$tocostcent1."
					,Driver_Name = '".$drivername."'
					,Document_RefNo = '".$documentrefno."'
					,vehicle_no = '".$vehicle_no."' ";
				
				if ($Module_TransferWithOneWh==1)
				$upd_Tr = $upd_Tr." ,Transferrequest_WarehouseFrom = '".$fromwarehouse."'
										,Transferrequest_WarehouseTo = '".$towarehouse."' ";
				
				$upd_Tr=$upd_Tr."	where transferrequest_id = ".$transferrequest_id
					;
		
		
		$upd_TrDT = "Update Temp_TransferRequestdt set 
					transferrequest_date =CONCAT('".displayParadoxDate(insertDate($transferrequest_date))." ', RIGHT(NOW(), 8))";
					if ($Module_TransferWithOneWh==1){
					$upd_TrDT =	$upd_TrDT.",TransferrequestDt_WarehouseFrom= '".$fromwarehouse."' ";
					$upd_TrDT =	$upd_TrDT.",TransferrequestDt_WarehouseTo = '".$towarehouse."' ";
					}
		$upd_TrDT =	$upd_TrDT.",Project_code1 = ".$fromproject."
					,Project_code2 = ".$toproject."			
					,Costcent_code = ".$costcent1."
					,ToCostcent_code = ".$tocostcent1."
					where transferrequest_id = ".$transferrequest_id;
		
		
		fbegin();			
		$result1=mysql_query($upd_Tr,$connection) or die(mysql_error());
		$result2=mysql_query($upd_TrDT,$connection) or die(mysql_error());
		
		
			
		if ($result1 and $result2  ) {
				fcommit();
				} else{
				$message  = mysql_error(); 
				frollback();
			}
			
			$query = "Select Count(*) From Transferrequest Where Transferrequest_id = ".$transferrequest_id;
				$result1 = selectData($connection, $query);
				$Ref = $result1[0];
			
			fbegin();
            $query_checkDelivery  = "select count(*) as TransferrequestExist from transferrequest where transferrequest_id = '$transferrequest_id'";
					$result_checkDelivery = mysql_query($query_checkDelivery, $connection); 
					$row_checkDelivery    = mysql_fetch_array($result_checkDelivery);
					extract($row_checkDelivery);
					if($TransferrequestExist > 0){
						$transType = "U";
					}
					else{
						$transType = "I";
					}
					$company = explode("_", $_SESSION['dbName']);
					$currCompany = strtoupper($company[0]);
			
			$resultcheckTrans = 0;
			$querycheckTrans = "SELECT  1 from temp_transferrequest where transferrequest_id = ".$transferrequest_id;
			//echo $querycheckTrans;
			//exit;
			$resultcheckTrans = selectData($connection, $querycheckTrans);
			$resultcheckTrans = $resultcheckTrans[0];	
			
			if($auto_approve == 1){
				$queryup = "update temp_transferrequestdt set TransferrequestDt_status = 'a',TransferrequestDt_approvedlevel=1 where  transferrequest_id = ".$transferrequest_id;
				$resup = mysql_query($queryup,$connection);
			}
			
			if ($resultcheckTrans==1) {
			$query = "SELECT CopyData_TempTransferRequest('".$SMode."', ".$transferrequest_id.",null,'".$_SESSION['useraccount']."') FROM Dual ";
			//echo $query;
			//exit;
			$result = selectData($connection, $query);
			$result = $result[0];	
			
			if ($result != 0){
				fcommit();
				
				
				
				If ($Ref == 0 or $Ref == '') // // if adding new transfer only
				{
					fbegin();	
					if ($TransType=='Tr')
						$TrRef = get_nextsequence('TransferRequest_reference','',$connection);
					else if ($TransType=='Rm')
						$TrRef = get_nextsequence('RmTransferRequest_reference','',$connection);				
					
					if ($TrRef!='')
						fcommit();
					else
						frollback();
					
					$updseq="update transferrequest set transferrequest_reference = ".$TrRef." where transferrequest_id = ".$transferrequest_id;
					$result = selectData($connection, $updseq);
						
					if ($Module_TrequestAutoApprove == 1){
						$updseq="update transferrequestdt set TransferrequestDt_status = 'A', TransferrequestDt_approvedlevel = 1 where transferrequest_id = ".$transferrequest_id;
						$result = selectData($connection, $updseq);	
					}	
				}		
				if($config['log_file'] == 1){
						$query_insertToHistory  = "call ".$config['dbName_history'].".InsertToHistory('$transType', '$transferrequest_id', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'transfer_request', '$currCompany')";
					//echo $query_insertToHistory;exit;
					$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
						}			
						
				} 
				else{
				$message  = 'Error In CopyData_TempTransferRequest Function'; 
				frollback();
				
			}
			} // end of check if found in transfer
		if ($message!='')
		$message = ereg_replace(' ','%20',$message);
		if ($message!='')
		header ("Location: ./Form_TransferRequest.php?actionmode=post&message=".$message."&transferrequest_id=".$transferrequest_id."&TransType=".$TransType);
		else
		//header ("Location: ./Form_Transfer.php?actionmode=post&transfer_id=".$transfer_id."&TransType=".$TransType);
		header ("Location: ./Form_TransferRequest.php?TransType=".$TransType."&actionmode=post&transferrequest_id=".$transferrequest_id);
		exit;		
	}
	else			
if ($actionmoded=='i')
	{
		
	
		$transferrequest_date = $_GET["value7"];
		
		$fromproject = $_GET["value8"];
		$toproject = $_GET["value11"];
		
		$costcent = $_GET["value9"];
		$tocostcent = $_GET["value12"];
		
		$documentrefno = $_GET["value10"];

		$fromwarehouse = $_GET["value13"];
		$towarehouse = $_GET["value14"];
		
		$driver_name = $_GET["value15"];
		$transferrequest_remark = $_GET["value16"];
		
		
		
		
		// for detail
		$p_item_code = $_GET["value1"];
		$p_item_quantity = $_GET["value2"];
		$p_item_sellingprice1 = $_GET["value3"];
		$fromwarehousedt = $_GET["value4"];
		$towarehousedt = $_GET["value5"];
		$p_transferdt_remark = $_GET["value6"];
		$Item_Unit2Qty = $_GET["value17"];
		
		$STrqstDtBatchno 	 = $_GET["STrqstDtBatchno"];
		$STrqstDtPackagingno = $_GET["STrqstDtPackagingno"];
		$STrqstDtNbrunit 	 = $_GET["STrqstDtNbrunit"];
		
		$TrqstId = $_GET["TrqstId"];									
		$STrqstDtId = $_GET["STrqstDtId"];									
		$transferrequest_id = $_GET["transferrequest_id"];									
									
		if ($TrqstId=='')
			{
				fbegin();
				$transferrequest_id = get_nextsequence('transferrequest_id','',$connection);	
				fcommit();
				
				fbegin();
				
				$query = "insert into temp_transferrequest(Transferrequest_Id,Transferrequest_Date,Transferrequest_Remark,Project_code1,Project_code2,Transferrequest_WarehouseFrom,Transferrequest_WarehouseTo,Driver_Name,Vehicle_No,Logged_Code,costcenter_code,Tocostcenter_code,Transferrequest_BelongsToGroup,User_account,Document_RefNo,Transferrequest_Type)
						 values
							(".$transferrequest_id."
							,CONCAT('".displayParadoxDate(insertDate($transferrequest_date))." ', RIGHT(NOW(), 8)),'".ereg_replace("'","\'",$transferrequest_remark)."',".GetSQLValueString($fromproject,'text').",".GetSQLValueString($toproject,'text').",".GetSQLValueString($fromwarehouse,'text').",".GetSQLValueString($towarehouse,'text').",'".$driver_name."','".$vehicle_no."','".$_SESSION['useraccount']."',".GetSQLValueString($costcent,'text').",".GetSQLValueString($tocostcent,'text').",".$login_belongstogrp.",'".$_SESSION['useraccount']."','".$documentrefno."','".$TransType."')";
			

			//echo $query;exit;
			$result=mysql_query($query,$connection) or die(mysql_error());
				if ($result)
					{
					$transferrequest_id = $transferrequest_id;
					fcommit();
					}
				else
					frollback();
					
			}
			
			$itemcode = $p_item_code;				
			$queryitem = " SELECT Count(*) FROM Items WHERE Item_Code = '".$itemcode."'";
			$resultitem = selectData($connection, $queryitem);
		
		// if item not exists then checks barcode
		if ($resultitem[0] == 0) {
			$sub_query = " SELECT Item_Code FROM Items WHERE item_barcode = '".$itemcode."' or item_barcode2 = '".$itemcode."' or item_barcode3 = '".$itemcode."'";
			
			$sub_result = selectData($connection, $sub_query);
			$itemcode = $sub_result[0];
			$p_item_code = $sub_result[0];
			
			if ($itemcode == '' || empty($itemcode)) {
				$ErrorExists = 1;
				$message  = "Insert not allowed. Item not found."; 
			}	
		}
					
					
					
					/*{
						//$checkqty_query =  "select QtyExistsWSorder_TEMP('".$itemcode."','".$fromwarehouse."',now(),0,'TR',null,null)";
						$checkqty_query =  " select QtyExistsWSorder_temp('".$itemcode."','".$fromwarehouse."','".displayParadoxDate(insertDate($transferrequest_date))." 23:59:59','".$transferrequest_id."','TR',null,null,0)  - ifnull(q1.qty,0)
							  from (select
									ifnull(sum(TransferrequestDt_Quantity),0) as qty
									from temp_transferrequestdt where transferrequest_id='".$transferrequest_id."' and item_code = '".$itemcode."'
									)q1";
							
						
						$checkqtyresult=mysql_query($checkqty_query,$connection) or die(mysql_error());
						$checkqtyrows=mysql_fetch_array($checkqtyresult);
						
						//echo $checkqtyrows[0]." ".$p_item_quantity." ".$Module_NegativeQuantity." ".$Login_negativeQty;
						
						if (($checkqtyrows[0] < $p_item_quantity) and (($Module_NegativeQuantity==0) or ($Login_negativeQty==0)) )
						
							{			
										$transferrequest_remark = ereg_replace(" ","%20",$transferrequest_remark);			
										$transferrequestdt_remark = ereg_replace(" ","%20",$transferrequestdt_remark);			
										$fromwarehouse = ereg_replace("&","%26",$fromwarehouse);			
										$towarehouse = ereg_replace("&","%26",$towarehouse);			
										$driver_name = ereg_replace(" ","%20",$driver_name);
										$documentrefno = ereg_replace(" ","%20",$documentrefno);
										$message = "Quantity%20entered%20can%20not%20exceeds%20".$checkqtyrows[0];
											//echo $checkqty_query;
											//exit;
								//header ("Location: ./Form_TransferRequestItems.php?fromproject=$fromproject1&toproject=$toproject1&fromwarehouse=$fromwarehouse&costcent=$costcent&tocostcent=$tocostcent&towarehouse=$towarehouse&transferrequestdt_remark=$transferrequestdt_remark&actionmode=$actionmode&transferrequest_id=$transferrequest_id&transferrequest_remark=$transferrequest_remark&transferrequest_date=$transferrequest_date&drivername=$driver_name&documentrefno=$documentrefno&vehicle_no=$vehicle_no&message=$message&td=$td&item_withserial=$item_withserial&transferrequestdt_id=$transferrequestdt_id&transit=$transit&TransType=$TransType");
								exit;
							}
						}*/
						$itemexists=0;
						
									
								$query="select 1 from warehouse where warehouse_production=1 and warehouse_code = '".$fromwarehouse."'";
								$result=mysql_query($query,$connection) or die(mysql_error());
								$isprod = mysql_num_rows($result);
						if ($isprod=='')  
									$isprod=0;
							
						if ($td!='')
							{
							
					
//echo $query;
								//exit;
								
								$query = "select transferrequestdt.item_code,item_withserialno
								from transferrequestdt ,items
								where
								transferrequestdt.item_code = items.item_code
								and transfer_id in (select transferrequest_id from transferrequest where transferrequest_id in(".$td."))
								and if($isprod=1,0=0,item_withserialno = 0)
								and items.item_code = '".$itemcode."'
								and item_active = 1
								";
								//echo $query;
								//exit;
								$result=mysql_query($query,$connection) or die(mysql_error());
								$itemexists = mysql_num_rows($result);
							}
							

						$query = "select item_code as item_code, item_description as item_description,item_sellingprice1 as item_sellingprice1,ifnull(Item_Production,0) as Item_Production,ifnull(Item_SemiFinished,0) as Item_SemiFinished 
									,Item_Unit
									from items where item_code = '".$p_item_code."'
									and if($isprod=1,0=0,(item_withserialno=0 or  item_withserialno=''))";
						
						$result=mysql_query($query,$connection) or die(mysql_error());
						while($rows=mysql_fetch_array($result)){
								extract($rows);
								}
								
						$query1 = "select ifnull(Item_Production,0) as Item_Production,ifnull(Item_SemiFinished,0) as Item_SemiFinished from items where item_code = '".$p_item_code."'
									";
						
						$result1=mysql_query($query1,$connection) or die(mysql_error());
						while($rows1=mysql_fetch_array($result1)){
								extract($rows1);
								}
								
						
						 if (($Item_Production==1)	and ($Item_SemiFinished==0))
								{
									$ItemcodeFinished = "'".$p_item_code."'";
									$ItemcodeSemiFinished = 'Null';
								}	
						else
						 if (($Item_Production==0)	and ($Item_SemiFinished==1))
								{
									$ItemcodeSemiFinished = "'".$p_item_code."'";
									$ItemcodeFinished = 'Null';
								}
						else				
								{
									$ItemcodeSemiFinished = 'Null';
									$ItemcodeFinished = 'Null';
								}
						
						$query = " SELECT ifNull(Item_Pack, 0) ".
								" FROM Items WHERE Item_Code = '".$p_item_code."'";
						$result   = selectData($connection, $query);
						$ItemPack = $result[0];
						//echo $query;
							// Case Not Pack Item

			//if ($lstItemType == "Trading" || $lstItemType == "PromoTrading" || $lstItemType == "Production" || $lstItemType == "NonStock" || $lstItemType == "All") {
			if ($ItemPack==0){
						
							$PTransferrequestdt_id = get_nextsequence('transferrequestdt_id','',$connection);	
									$query = "select BASE1 from setup limit 1";
									$result1=mysql_query($query,$connection)	or die( mysql_error());
										while($row=mysql_fetch_array($result1)){
										extract($row);
										}
										
								$query="select curdt_parity from curdt where currency_code='".$BASE1."'  order by curdt_date desc";
								//echo $query;
								$result1=mysql_query($query,$connection)	or die( mysql_error());
								
								while($row=mysql_fetch_array($result1)){
								extract($row);
								}
								$item_sellingprice2=$p_item_sellingprice1/$curdt_parity;
						
						
						if ($Item_Unit=='')
							$Item_Unit = 1;
							
						if ($Item_Unit2Qty=='')
							$Item_Unit = 0;
							
						fbegin();  
						$query = "insert into temp_transferrequestdt(transferrequestdt_id,transferrequest_id,transferrequest_date,item_code,transferrequestdt_quantity,TransferrequestDt_Price,transferrequestdt_pricebase1,transferrequestdt_pricebase2
								,TransferrequestDt_WarehouseFrom,TransferrequestDt_WarehouseTo,transferrequestdt_remark,Logged_code,costcent_code,tocostcent_code,Item_CodeFinished,Item_CodeSemiFinished
								,project_code1,project_code2,TransferrequestDt_BatchNo,TransferrequestDt_packagingNo,TransferrequestDt_nbrUnit, transferrequestdt_heatno ";
								
								if ($Module_DoubleUnit==1)
								$query = $query."
								,Item_UnitCoef
								,Item_Unit2Qty ";
								
								
								$query = $query.") values(
							  '".$PTransferrequestdt_id."'
							  ,'".$transferrequest_id."'
							  ,CONCAT('".displayParadoxDate(insertDate($transferrequest_date))." ', RIGHT(NOW(), 8))
							   ,'".$p_item_code."'						  
							  ,'".$p_item_quantity."'
							  ,".$p_item_sellingprice1."
							  ,".$p_item_sellingprice1."
							  ,".$item_sellingprice2."
							   ,".GetSQLValueString($fromwarehousedt,'text')."
							  ,".GetSQLValueString($towarehousedt,'text')."
							  ,'".$p_transferdt_remark."'
							  ,'".$_SESSION['useraccount']."'
							  ,".GetSQLValueString($costcent,'text')."
							  ,".GetSQLValueString($tocostcent,'text')."
							  ,".$ItemcodeFinished."
							  ,".$ItemcodeSemiFinished."
							 ,".GetSQLValueString($fromproject,'text')."
							  ,".GetSQLValueString($toproject,'text').",".GetSQLValueString($STrqstDtBatchno,'text').",
							  ".GetSQLValueString($STrqstDtPackagingno,'double').",".GetSQLValueString($STrqstDtNbrunit,'double').", ".GetSQLValueString($STrqstDtHeatno,'text')."  ";
							
							if ($Module_DoubleUnit==1)
								$query = $query.",'".$Item_Unit."' ,".$Item_Unit2Qty." ";
							   
					   
								$query = $query.")";
						//echo $query;
						}
						else 
						{
				
				fbegin();
				$txtItemCode = $p_item_code;
				$txtPurDtQty = $p_item_quantity;
				
				if ($lstItemType == "FinishGoodPack" || $lstItemType == "SemiFinishPack") {
					$querypackdt = " SELECT PackDt1.Item_Code, PackDt1.Item_CodePackDt, PackDt1.PackDt_Quantity, PackDt1.PackDt_CostPercentage ".
								   " FROM PackDt, PackDt As PackDt1, Items ".
								   " WHERE PackDt.Item_Code = '".$txtItemCode."'  ".
								   " AND PackDt.Item_CodePackDt = PackDt1.Item_Code ".
								   " AND PackDt.Item_CodePackDt = Items.Item_Code ".
								   " AND ifNull(Items.Item_SemiFinished,0) = 1 ".
								   " UNION ".
								   " SELECT PackDt.Item_Code, PackDt.Item_CodePackDt, PackDt.PackDt_Quantity, PackDt.PackDt_CostPercentage ".
								   " FROM PackDt, Items ".
								   " WHERE PackDt.Item_Code = '".$txtItemCode."'  ".
								   " AND PackDt.Item_CodePackDt = Items.Item_Code ".
								   " AND ifNull(Items.Item_SemiFinished,0) = 0 ";				
				} else {
					$querypackdt = " SELECT Item_Code, Item_CodePackDt,PackDt_Quantity,PackDt_CostPercentage ".
								   " FROM PackDt ".
								   " WHERE Item_Code = '".$txtItemCode."'  ";
				}
				
				//echo $querypackdt;
				//	exit;
				$resultpackdt=mysql_query($querypackdt, $connection) or die(mysql_error());
				while($rowspackdt=mysql_fetch_array($resultpackdt)){			   
				
					if ($lstItemType == "FinishGoodPack" || $lstItemType == "SemiFinishPack")	$vItemCodePacked = '';
					else $vItemCodePacked = $rowspackdt[0];
					
					$vItemCode       = $rowspackdt[1];
					$vPackDtQty      = $rowspackdt[2];
					
					$itemquery = "select Item_SellingPrice1,Item_SellingPrice1Base1,Item_SellingPrice1Base2 from  items where item_code='".$rowspackdt[0]."'";					
					$resultitemPrice=mysql_query($itemquery, $connection) or die(mysql_error());
					$rowsprice=mysql_fetch_array($resultitemPrice);
					$p_item_sellingprice1  = $rowsprice[0];
					$p_item_sellingpriceBase1  = $rowsprice[1];
					$p_item_sellingpriceBase2  = $rowsprice[2];
					
					$vPurDtId = get_nextsequence('transferrequestdt_id','',$connection);				
					$vPurDtQty = $txtPurDtQty*$vPackDtQty;
					
					$query = "insert into temp_transferrequestdt(transferrequestdt_id,transferrequest_id,transferrequest_date,item_code,transferrequestdt_quantity,TransferrequestDt_Price,transferrequestdt_pricebase1,transferrequestdt_pricebase2
					,TransferrequestDt_WarehouseFrom,TransferrequestDt_WarehouseTo,transferrequestdt_remark,Logged_code,costcent_code,tocostcent_code
					,Item_CodePacked,Item_qtyPacked ,Item_CodeFinished,Item_CodeSemiFinished,project_code1,project_code2,TransferrequestDt_BatchNo,TransferrequestDt_packagingNo,TransferrequestDt_nbrUnit
					) values(
							  '".$vPurDtId."'
							  ,'".$transferrequest_id."'
							  ,CONCAT('".displayParadoxDate(insertDate($transferrequest_date))." ', RIGHT(NOW(), 8))
							   ,'".$vItemCode."'						  
							  ,'".$vPurDtQty."'
							  ,'".$p_item_sellingprice1."'
							  ,'".$p_item_sellingpriceBase1."'
							  ,'".$p_item_sellingpriceBase2."'
							   ,'".$fromwarehouse."'
							  ,'".$towarehouse."'
							  ,'".$p_transferdt_remark."'
							  ,'".$_SESSION['useraccount']."'
							  ,".$costcent1."
							  ,".$tocostcent1."
							  ,  if('".$vItemCodePacked."'='', Null, '".$vItemCodePacked."')
							  ,".$txtPurDtQty."
							  ,".$ItemcodeFinished."
							  ,".$ItemcodeSemiFinished."
							  ,".GetSQLValueString($fromproject,'text')."
							  ,".GetSQLValueString($toproject,'text')."
							  ,".GetSQLValueString($STrqstDtBatchno,'text').",
							  ".GetSQLValueString($STrqstDtPackagingno,'double').",".GetSQLValueString($STrqstDtNbrunit,'double')."  
							   );";
						
					$result=mysql_query($query,$connection) or die(mysql_error());						
					//echo $query;
				} // End Loop Of Packdt
					
						if ($result)
							fcommit();
						else
							frollback();
		}
	
		
						// after adding semi finished pack and finished good pack
												
			if ($ItemPack==0){
					{
						if ($p_item_code!='' )
						$result=mysql_query($query,$connection) or die(mysql_error());
						else
						if ($p_item_code!='' )
						$message='Error:Item%20not%20exists.';
						//else
						//if ($p_item_code=='' )
						//$message='Error:Item%20with%20serial.';
						}
					
						if ($result)
							fcommit();
						else
							frollback();
						}
						
						$transferrequest_remark = ereg_replace(" ","%20",$transferrequest_remark);			
						$transferrequestdt_remark = ereg_replace(" ","%20",$transferrequestdt_remark);			
						$fromwarehouse = ereg_replace("&","%26",$fromwarehouse);			
						$towarehouse = ereg_replace("&","%26",$towarehouse);			
						$driver_name = ereg_replace(" ","%20",$driver_name);
						$documentrefno = ereg_replace(" ","%20",$documentrefno);

		echo $message;				
		//header ("Location: ./Form_TransferRequestItems.php?fromproject=$fromproject1&toproject=$toproject1&costcent=$costcent&tocostcent=$tocostcent&fromwarehouse=$fromwarehouse&towarehouse=$towarehouse&transferdtrequest_remark=$transferdtrequest_remark&actionmode=$actionmode&transfer_id=$transfer_id&transfer_remark=$transfer_remark&transfer_date=$transfer_date&drivername=$driver_name&documentrefno=$documentrefno&vehicle_no=$vehicle_no&message=$message&td=$td&item_withserial=$item_withserial&transferdtrequest_id=$transferdtrequest_id&transit=$transit&TransType=$TransType");
		echo "[BRK]";
		
		echo $transferrequest_id;
		
		exit;
	}

	else
if ($actionmoded=='d' ) { 
		if ($transferrequestdt_id!=''){ 
			if ($item_withserial==0)
				{
				$Del = "delete from temp_transferrequestdt where transferrequestdt_id = ".$transferrequestdt_id;
				$result=mysql_query($Del,$connection) or die(mysql_error());
				}			
}
exit;
}
else if ($actionmode=='checkitemcode'){

$vitemcode = $_GET["vitemcode"];
$vitemdesc = $_GET["vitemdesc"];
$codedesc = $_GET["codedesc"];

if ($codedesc==1)
	$query = " select item_description from items where item_code = '".$vitemcode ."' and  ifNull(item_active,0) = 1 ";
	else
	$query = " select item_code from items where item_description = '".$vitemdesc ."' and  ifNull(item_active,0) = 1 ";
	
$result=mysql_query($query,$connection)  or die(mysql_error());
$row=mysql_fetch_row($result);
if ($row[0]!='') $itemcode = $row[0];
else $itemcode = 0;

echo "[BRK]";
echo $itemcode;
echo "[BRK]";	

exit;
}
else if ($actionmode=='checkwarehousecode'){

$vwarehousecode = $_GET["vwarehousecode"];

$query = " select warehouse_code from warehouse where warehouse_code = '".$vwarehousecode ."' ";
$result=mysql_query($query,$connection)  or die(mysql_error());
$row=mysql_fetch_row($result);

echo "[BRK]";
if ($row[0]!='')
	echo $row[0];
else	
	echo 0;
echo "[BRK]";	
}
	else
if ($actionmoded=='e' and $transferrequestdt_id!='')
	{
	// for detail
		$itemcode = $_GET["value1"];
		$p_item_quantity = $_GET["value2"];
		$p_item_sellingprice1 = $_GET["value3"];
		$fromwarehousedt = $_GET["value4"];
		$towarehousedt = $_GET["value5"];
		$p_transferdt_remark = $_GET["value6"];
		$Item_Unit2Qty = $_GET["value17"];
		$STrqstDtBatchno = $_GET["STrqstDtBatchno"];
		$STrqstDtPackagingno = $_GET["STrqstDtPackagingno"];
		$STrqstDtNbrunit = $_GET["STrqstDtNbrunit"];
		$STrqstDtHeatno = $_GET["STrqstDtHeatno"];
	
		$upd = "update temp_transferrequestdt set 
				Item_code = '".$itemcode."'
				,TransferrequestDt_Quantity= '".$p_item_quantity."'
				,TransferrequestDt_Price= '".$p_item_sellingprice1."' ";
				
	if ($Module_DoubleUnit==1)
		$upd = $upd .",Item_Unit2Qty= '".$Item_Unit2Qty."' ";
		$upd = $upd ." , TransferrequestDt_BatchNo = ".GetSQLValueString($STrqstDtBatchno,'text')."
					   , TransferrequestDt_packagingNo = ".GetSQLValueString($STrqstDtPackagingno,'double')."
					   , TransferrequestDt_nbrUnit = ".GetSQLValueString($STrqstDtNbrunit,'double')."  
					   , Transferrequestdt_remark= ".GetSQLValueString($p_transferdt_remark,'text')." ";
				
		if ($Module_TransferWithOneWh==0)
		$upd = $upd." ,TransferrequestDt_WarehouseFrom = ".GetSQLValueString($fromwarehousedt,'text')."
					,TransferrequestDt_WarehouseTo = ".GetSQLValueString($towarehousedt,'text')." ";

				
		$upd = $upd." ,  transferrequestdt_heatno = ".GetSQLValueString($STrqstDtHeatno,'text')."  where transferrequestdt_id = ".$transferrequestdt_id;


		$result=mysql_query($upd,$connection) or die(mysql_error());
		
	
		
		$transferrequest_remark = ereg_replace(" ","%20",$transferrequest_remark);			
						$transferrequestdt_remark = ereg_replace(" ","%20",$transferrequestdt_remark);			
						$fromwarehouse = ereg_replace("&","%26",$fromwarehouse);			
						$towarehouse = ereg_replace("&","%26",$towarehouse);			
						$driver_name = ereg_replace(" ","%20",$driver_name);	
						$documentrefno = ereg_replace(" ","%20",$documentrefno);	
	//header ("Location: ./Form_TransferRequestItems.php?fromproject=$fromproject1&toproject=$toproject1&fromwarehouse=$fromwarehouse&towarehouse=$towarehouse&transferdt_remark=$transferdt_remark&actionmode=$actionmode&transfer_id=$transfer_id&transfer_remark=$transfer_remark&transfer_date=$transfer_date&drivername=$driver_name&documentrefno=$documentrefno&vehicle_no=$vehicle_no&transferdt_id=$transferdt_id&actionmoded=$actionmoded&item_withserial=$item_withserial&transferdt_id=$transferdt_id&message=$message&transit=$transit&TransType=$TransType");
	}
else if ($actionmode=='search' and $transferrequest_id!='')
	
{
	 
	$message='';
	$message1='';
	$message2='';
	fbegin();
	//Copy Data From Main Tables To Temp Tables
	$query = " INSERT INTO Temp_transferRequest(Transferrequest_Id, Transferrequest_Reference, Transferrequest_Date, Transferrequest_Remark, Transferrequest_Posted, Transferrequest_Edit, 
				Project_code1, Project_code2, Costcenter_Code,ToCostcenter_Code, Journal_id, TransferrequestDt_ID, SendReceive_Reference, Transferrequest_BelongsToGroup, 
				Synchronized, Adjustment_id, Driver_Name, Document_RefNo , 
				Vehicle_No, f_Date, Requestedby, User_account, Company_Name, Transferrequest_WarehouseFrom, Transferrequest_WarehouseTo, Logged_Code,Transferrequest_Type, MReq_id)
				Select Transferrequest_Id, Transferrequest_Reference, Transferrequest_Date, Transferrequest_Remark, Transferrequest_Posted, Transferrequest_Edit, Project_code1, Project_code2, Costcenter_Code, ToCostcenter_Code, Journal_id, TransferrequestDt_ID, SendReceive_Reference, Transferrequest_BelongsToGroup, Synchronized, 
				Adjustment_id, Driver_Name,Document_RefNo, Vehicle_No, f_Date, Requestedby, User_account, Company_Name, Transferrequest_WarehouseFrom, Transferrequest_WarehouseTo
				, '".$_SESSION['useraccount']."','".$TransType."', MReq_id FROM Transferrequest ".
				" WHERE Transferrequest_id = ".$transferrequest_id;
				
	//echo $query; exit;
	if (!updateData($connection, $query, true))
      $message  = mysql_error(); 	
			 
	$query = " INSERT INTO temp_transferrequestdt (TransferrequestDt_id, Transferrequest_Id, Transferrequest_Date, Item_Barcode, Item_code, Size_Measure, TransferrequestDt_WarehouseFrom, TransferrequestDt_WarehouseTo, TransferrequestDt_Quantity, TransferrequestDt_ExpireDate, TransferrequestDt_Price, TransferrequestDt_PriceBase1, TransferrequestDt_PriceBase2, Transferrequest_Posted, Project_Code1, Project_Code2, Costcent_Code, 
				Transferrequestdt_remark, Color_Code, Cur_code, Transferrequestdt_posted, PreProduction_Serial, Item_CodePacked, Synchronized, Item_qtyPacked, TransferrequestDt_DirectStkDbt, TransferrequestDt_DirectStkCrd, TransferrequestDt_DirectStkCur, TransferrequestDt_PricePerUnit, TransferrequestDt_QtyUnit, 
				Date, Item_ManufacturedCode, Item_UnitCoef, Item_Unit2Qty, relateddt_id, Logged_Code,Item_codefinished,Item_codesemifinished,Approval_date,TransferrequestDt_BatchNo,TransferrequestDt_packagingNo,TransferrequestDt_nbrUnit, transferrequestdt_heatno)
				select TransferrequestDt_id, Transferrequest_Id, Transferrequest_Date, Item_Barcode, Item_code, Size_Measure, TransferrequestDt_WarehouseFrom, TransferrequestDt_WarehouseTo, TransferrequestDt_Quantity, TransferrequestDt_ExpireDate, TransferrequestDt_Price, TransferrequestDt_PriceBase1, TransferrequestDt_PriceBase2, Transferrequest_Posted, Project_Code1, Project_Code2, Costcent_Code, Transferrequestdt_remark, Color_Code, Cur_code, 
				Transferrequestdt_posted, PreProduction_Serial, Item_CodePacked, Synchronized, Item_qtyPacked, TransferrequestDt_DirectStkDbt, TransferrequestDt_DirectStkCrd, TransferrequestDt_DirectStkCur, TransferrequestDt_PricePerUnit, TransferrequestDt_QtyUnit, Date, Item_ManufacturedCode, Item_UnitCoef, Item_Unit2Qty, relateddt_id
				, '".$_SESSION['useraccount']."' ,Item_codefinished,Item_codesemifinished,Approval_date,TransferrequestDt_BatchNo,TransferrequestDt_packagingNo,TransferrequestDt_nbrUnit, transferrequestdt_heatno
				FROM transferrequestdt  WHERE transferrequest_id = ".$transferrequest_id;
//	echo $query;
//	exit;
		if (!updateData($connection, $query, true))
			$message1  = mysql_error(); 			
			
	
			
		if ($message!='' or $message1!='')
		frollback();
		else
		fcommit();

		
		if ($message!='')
			$message = ereg_replace(' ','%20',$message);
		if ($message!='')
			header ("Location: ./Form_TransferRequestSearch.php?PMode=1&message=".$message."&transferrequest_id=".$transferrequest_id."&TransType=".$TransType);
		else
			header ("Location: ./Form_TransferRequestSearch.php?PMode=1&TransType=".$TransType);
		exit;	

}
	else if ($actionmode=='deletepending' and $pendingtransferrequest_id!='')	
	{
		fbegin();
		
		$query ="delete from temp_transferrequestdt where transferrequest_id = ".$pendingtransferrequest_id;
		$result2=mysql_query($query,$connection) or die(mysql_error());
					
		$query ="delete from temp_transferrequest where transferrequest_id = ".$pendingtransferrequest_id;
		$result3=mysql_query($query,$connection) or die(mysql_error());
		
		
		if (!result2 or !result3)
		frollback();
		else
		fcommit();
		
		header ("Location: ./Form_TransferRequestSearchPending.php?TransType=".$TransType);
		
	}
else if ($actionmode=='checkcostcentercode'){

$vcostcentercode = $_GET["vcostcentercode"];

$query = " select costcent_code from costcent where costcent_code = '".$vcostcentercode ."' ";
$result=mysql_query($query,$connection)  or die(mysql_error());
$row=mysql_fetch_row($result);

echo "[BRK]";
if ($row[0]!='')
	echo $row[0];
else	
	echo 0;
echo "[BRK]";	
}
else if ($actionmode=='updateitemstatus'){

	$PRqstid = $_GET["PRqstid"];
	$PRqstDtid = $_GET["PRqstDtid"];
	$objval = $_GET["objval"];
	$approved = $_GET["approved"];
	$reason = $_GET["reason"];

	echo "[BRK]";

	if ($_GET["vreason"]!='')
		$vreason = $_GET["vreason"];
	
	if($PRqstid!=''){
		$query = " UPDATE transferrequest SET rejectedreason = '$vreason' where transferrequest_id = $PRqstid";
		if (!updateData($connection, $query, true)) 
			$message  = mysql_error();
								
		$query = " UPDATE transferrequestdt SET rejectedreason = '$vreason' where transferrequest_id = $PRqstid";
		if (!updateData($connection, $query, true)) 
			$message  = mysql_error();						
	}
	
	
	if($PRqstid!='') {
		if ($objval == 'p') {
			$query = " UPDATE transferrequestdt SET  TransferrequestDt_status = null, rejectedreason = null, Approval_date=null ".
					 " WHERE transferrequest_id = $PRqstid ";
		} else {
			$query = " UPDATE transferrequestdt SET  TransferrequestDt_status = '$objval', Approval_date = if('$objval'='r',null,now()) ".
					 " WHERE transferrequest_id = $PRqstid ";
		}
	}
	else {
		if ($objval == 'p') {
			$query = " UPDATE transferrequestdt SET  TransferrequestDt_status = null,rejectedreason = null,TransferrequestDt_approvedlevel = '$approved' ".
					 " , Approval_date = null ".
					 " WHERE transferrequestdt_id = $PRqstDtid ";
		} else {
			$query = " UPDATE transferrequestdt SET  TransferrequestDt_status = '$objval',rejectedreason = '".$reason."',TransferrequestDt_approvedlevel = '$approved' ".
					 " , Approval_date = if('$objval'='r',null,now()) ".
					 " WHERE transferrequestdt_id = $PRqstDtid ";
		}
	}
	
	if (!updateData($connection, $query, true)) 
		$message  = mysql_error();
		
	echo $message;
	echo "[BRK]";
}
else if ($actionmode=='delete')
		{
			
		fbegin();
		
		if($config['log_file'] == 1){
			$company = explode("_", $_SESSION['dbName']);
			$currCompany = strtoupper($company[0]);
						$transType = "D";
						$query_insertToHistory  = "call ".$config['dbName_history'].".InsertToHistory('$transType', '$transferrequest_id', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'transfer_request', '$currCompany')";
					//echo $query_insertToHistory;exit;
					$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
						}
		
		
		$query ="delete from transferrequestdt where transferrequest_id = ".$transferrequest_id;
		$result2=mysql_query($query,$connection) or die(mysql_error());
		
		$query ="delete from transferrequest where transferrequest_id = ".$transferrequest_id;
		$result3=mysql_query($query,$connection) or die(mysql_error());
		
		
		if (!result2 or !result3)
		frollback();
		else
		fcommit();
		
		header ("Location: ./Form_TransferRequestSearch.php?TransType=".$TransType);
		}

else if ($actionmoded=='CopyTransferRequest')
{
			fbegin();
						
						$query =  "select null
							  ,null
							  ,CONCAT('".displayParadoxDate(insertDate($transferrequest_date))." ', RIGHT(NOW(), 8))
							   ,item_code						  
							  ,transferrequestdt_quantity
							  ,TransferrequestDt_Price
							  ,transferrequestdt_pricebase1
							  ,transferrequestdt_pricebase2
							  ,TransferrequestDt_WarehouseFrom
							  ,TransferrequestDt_WarehouseTo
							  ,transferrequestdt_remark
							  ,'".$_SESSION['useraccount']."'						  
							  ,Item_CodeFinished
							  ,Item_CodeSemiFinished
							  ,transferrequestdt_id
							  ,ifnull(Item_Unit2Qty,0)
							  ,ifnull(Item_UnitCoef,0)
							   from transferrequestdt where transferrequest_id= '".$loadedtransferrequest_id."'";
							$result=mysql_query($query,$connection);
							//echo $query;
							
							while ($row=mysql_fetch_array($result)){
							$PTransferrequestdt_id = get_nextsequence('transferrequestdt_id','',$connection);	
						
							$query1 = "insert into temp_transferrequestdt(transferrequestdt_id,transferrequest_id,transferrequest_date,item_code
								,transferrequestdt_quantity
								,TransferrequestDt_Price,transferdt_pricebase1,transferrequestdt_pricebase2
								,TransferrequestDt_WarehouseFrom,TransferrequestDt_WarehouseTo,transferrequestdt_remark
								,Logged_code,Item_CodeFinished,Item_CodeSemiFinished,relateddt_id ";
								
								if ($Module_DoubleUnit==1)
									$query1 = $query1 .",Item_Unit2Qty,Item_UnitCoef ";
								
								
								$query1 = $query1 .") values(
								'".$PTransferrequestdt_id."'
							  ,'".$transferrequest_id."'
							  ,CONCAT('".displayParadoxDate(insertDate($transferrequest_date))." ', RIGHT(NOW(), 8))
							   ,'".$row[3]."'
							  ,'".$row[4]."'
							  ,'".$row[5]."'
							  ,'".$row[6]."'
							  ,'".$row[7]."'
							   ,'".$row[8]."'
							  ,'".$row[9]."'
							  ,'".$row[10]."'
							  ,'".$_SESSION['useraccount']."'						  
							  ,'".$row[12]."'
							  ,'".$row[13]."'
							  ,'".$row[14]."' ";
							  
							  if ($Module_DoubleUnit==1)
							  $query1 = $query1 .",'".$row[15]."'
												,'".$row[16]."' ";
							  
							  $query1 = $query1 ." )"	;
							  
							$result1=mysql_query($query1,$connection) or die(mysql_error());
							
							//echo $query1."<br>";

														
							}
			
			
			
		if ($result3 and $yy==1){
		
		
				fcommit(); }
		else		
		if ($result2 and $yy!=1){
				fcommit();
			}else{
				$ErrorExists = 1;
				$message  = mysql_error(); 
				frollback();
			}
			
	if ($message =='')	
		header ("Location: ./Form_LoadItems.php?transfer_id=".$transfer_id."&costcent=".$costcent."&tocostcent=".$tocostcent);								  
	else	
		header ("Location: ./Form_LoadItems.php?message=$message$transfer_id=".$transfer_id."&costcent=".$costcent."&tocostcent=".$tocostcent);
							

}
else if ($actionmode=='updatequantity'){

$transdtid = $_GET["transdtid"];
$qty = $_GET["qty"];

$query =  " update transferrequestdt set TransferrequestDt_Quantity = ".GetSQLValueString($qty,'double')." where transferrequestdt_id = ".$transdtid;
//echo $query ;
$result=mysql_query($query,$connection) or die(mysql_error());
$message  = mysql_error(); 
echo "[BRK]";							
echo $message;
echo "[BRK]";							
}


function displayParadoxDate($data){
	return substr($data, 0, 4) ."-". substr($data, 4, 2) ."-".substr($data, 6, 2);
}
function insertDate($data){
	return substr($data, 6, 4) . substr($data, 3, 2) . substr($data, 0, 2);
}
function fbegin()
{
	mysql_query("START TRANSACTION");
}
function fcommit(){
	mysql_query("COMMIT");
}
function frollback()
{
	mysql_query("ROLLBACK");
}
function selectData($conn, $query){
	$result = mysql_query($query, $conn);
	$cur = mysql_fetch_row($result);		
	return $cur;
}
function updateData($conn, $query, $commit){
	if (mysql_query($query, $conn)){				
		return true;
	}else{
		return false;
	}
}
function insertMySQLDate($data){
	$data = insertDate($data);
	return substr($data, 0, 4) ."-". substr($data, 4, 2) ."-".substr($data, 6, 2)." ".strftime("%H").":".strftime("%M").":".strftime("%S");;
}

function dateDiff ($d1, $d2) {
	return round(abs(strtotime($d1)-strtotime($d2))/86400);
}

function displayToday(){
	return strftime("%d")."/".strftime("%m")."/".strftime("%Y");
}
	//Return Date in YYYY-MM-DD HH:MM:SS format. Supplied Format: DD/MM/YYYY


	
?>