<?php

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

include("config.inc.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");



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

	$query="update transfer
	set transfer_remark = '".$transfer_remark."',
	project_code1 = '".$fromproject."',
	project_code2 = '".$toproject."',
	transfer_warehousefrom = '".$fromwarehouse."',
	transfer_warehouseto = '".$towarehouse."',
	costcenter_code = ".$costcent.",
	driver_name = '".$driver_name."',
	vehicle_no = '".$vehicle_no."'  
	where transfer_id = ".$transfer_id;
	//echo $query."<br>"."<br>";
	//exit;
	$resulttr=mysql_query($query,$connection) or die(mysql_error());
	
	//echo $countload."<br>";
	
	
	
	for ($j=0;$j<$countload;$j++)
	{
		//echo $transferdtid[$j]."<br>";
		if ($transferdtid[$j]!='')
		{
			$query="update transferdt
			set transferdt_warehousefrom = '".$fromwarehouse."'
			,transferdt_warehouseto = '".$towarehouse."'
			,transferdt_quantity = '".$item_quantity[$j]."'
			,transferdt_remark = '".$transferdt_remark[$j]."'
			where transferdt_id = ".$transferdtid[$j];
			//echo $query."<br>";
			
			$trdtresult=mysql_query($query,$connection) or die(mysql_error());
		}
		
	
			
		
	
	}
	

		$purserialquery = "update purchasesserialno set warehouse_code = '".$towarehouse."'
			where transferdt_id in
			(select transferdt_id from transferdt where transfer_id = ".$transfer_id.")";
			$purserialresult=mysql_query($purserialquery,$connection) or die(mysql_error());
	
	/*
	
					//// //// TABLE USERTD01
					$query = "select id, transfer_id, transferdt_id, item_code, item_description, transferdt_quantity, TransferDt_Price, relateddt_id, TransferDt_WarehouseFrom, TransferDt_WarehouseTo, serialno_code,transferdt_remark
							FROM ".$HTTP_SESSION_VARS['tabletd'];
					$result=mysql_query($query,$connection) or die(mysql_error());
					while($row=mysql_fetch_array($result)){
					
						$query="select serial_id from sequence";
						$result10=mysql_query($query,$connection) or die(mysql_error());
						while($rows=mysql_fetch_array($result10)){
						extract($rows);
						}

						$serial_id = $serial_id + 1;
			
						$trdquery="select transferdt_id as seco from transferdt where item_code = '".$row[3]."' and transfer_id = ".$transfer_id;
							//echo $trdquery."<br>";
							$trdresult10=mysql_query($trdquery,$connection) or die(mysql_error());
							$tdrows=mysql_fetch_array($trdresult10);
							
				$query="insert into purchasesserialno(serial_id,journal_type,purchasedt_id,item_code,serialno_code,warehouse_code,transferdt_id) 
						values('".$serial_id."'
								,'TR'
								,'".$tdrows[0]."'
								,'".$row[3]."','".$row[10]."','".$towarehouse."','".$tdrows[0]."')";
						//echo $query."<br>";
						$resultpur=mysql_query($query,$connection) or die(mysql_error());
						$query="update sequence set serial_id='$serial_id'";
						$resultincserial=mysql_query($query,$connection) or die(mysql_error());
						}
	}
	else
		{
		
		$serialarray = $HTTP_SESSION_VARS["serialarray"] ;
		$count = count($serialarray);
		
		if ($count!='')
			{
			for($sr=0;$sr<$count;$sr++)
				{
					if ($serialarray[$sr][0]!='')
						{
						$query="select serial_id from sequence";
						$result10=mysql_query($query,$connection) or die(mysql_error());
						while($rows=mysql_fetch_array($result10)){
						extract($rows);
						}

						$serial_id = $serial_id + 1;
						//echo $serialarrayload[$sr][0]." ".$serialarrayload[$sr][1]."<br>";
							$trdquery="select transferdt_id as seco from transferdt where item_code = '".$serialarray[$sr][1]."' and transfer_id = ".$transfer_id;
							//echo $trdquery."<br>";
							$trdresult10=mysql_query($trdquery,$connection) or die(mysql_error());
							$tdrows=mysql_fetch_array($trdresult10);
							
							if ($tdrows[0]=='')
								$tdrows[0] = 'null';
							else
								$tdrows[0] = "'".$tdrows[0]."'";
								
						//echo "tdrows ".$tdrows[0];
						$query="insert into purchasesserialno(serial_id,journal_type,purchasedt_id,item_code,serialno_code,warehouse_code,transferdt_id) 
						values('".$serial_id."'
								,'TR'
								,".$tdrows[0]."
								,'".$serialarray[$sr][1]."','".$serialarray[$sr][0]."','".$towarehouse."',".$tdrows[0].")";
						//echo $query."<br>";
								$resultpur=mysql_query($query,$connection) or die(mysql_error());
						$query="update sequence set serial_id='$serial_id'";
						$resultincserial=mysql_query($query,$connection) or die(mysql_error());
						}
				}
			}	
		
		}
if ($process=='load')
			if(!$resulttr or !$resulttrd or !$resultpur or !$resultincserial or !$trdresult10)
					{
					frollback();
					}
					else
					{
					fcommit();				
					}
		else
			if ($HTTP_SESSION_VARS["serialarray"]!='')
				{
				if (!$resultincserial)
					frollback();
				else
					fcommit();
					if (isset($HTTP_SESSION_VARS['tabletd']))
					$query="drop table if exists `".$HTTP_SESSION_VARS['tabletd']."`";
					$result=mysql_query($query,$connection) or die(mysql_error());
				}
					
		else			
			if(!$resulttr or !$resulttrd )
					{
						
					frollback();
					}
					else
					{					
					if (isset($HTTP_SESSION_VARS['tabletd']))
					$query="drop table if exists `".$HTTP_SESSION_VARS['tabletd']."`";
					$result=mysql_query($query,$connection) or die(mysql_error());
					fcommit();				
					}
	*/
	
	//fcommit();			
	function fbegin()
{
	mysql_query("START TRANSACTION");
}
function fcommit(){
	mysql_query("COMMIT");
}
function frollback()
{
	mysql_query("ROLLBACK");
}

header ("Location: transfertrans.php?transferid=".$transfer_id."&edit=yes&actionmode=view");
?>