<?php

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


include("config.inc.php");
include("function.inc.php");
unset($HTTP_SESSION_VARS['error']);
$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");

	fbegin();


$query = "select BASE1 from setup limit 1";
$result1=mysql_query($query,$connection)	or die( mysql_error());
	while($row=mysql_fetch_array($result1)){
	extract($row);
	}
	
	if (empty($costcent))
		$costcent = 'null';
	else
		$costcent = "'".$costcent."'";
	

	
	$query="select curdt_parity from curdt where currency_code='NGN' order by currency_code desc";
	$result1=mysql_query($query,$connection)	or die( mysql_error());
	while($row=mysql_fetch_array($result1)){
	extract($row);
	}
	//$query="update sequence set transfer_reference = transfer_reference + 1";
		//$result3=mysql_query($query,$connection) or die(mysql_error());
	
	/*maroun
	$query="select transfer_id+1 as transfer_id ,  transfer_reference+1 as ref  from sequence";
	$result3=mysql_query($query,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result3)){
			extract($rows);
			}
	*/
	
	$transfer_id = get_nextsequence('transfer_id','',$connection);		
	$ref = get_nextsequence('transfer_reference','',$connection);		
			
	
	$query="insert into transfer(transfer_id,transfer_reference,transfer_date,transfer_remark,
	project_code1,project_code2,transfer_warehousefrom,transfer_warehouseto,costcenter_code,journal_id,driver_name,vehicle_no,User_account) 
	values
	(".$transfer_id."
		,".$ref."
		,now()
		,'".$transfer_remark."'
		,'".$fromproject."'
		,'".$toproject."'
		,'".$fromwarehouse."'
		,'".$towarehouse."'
		,".$costcent."
		,'".$journal_id."'
		,'".$driver_name."'
		,'".$vehicle_no."'
		,'".$HTTP_SESSION_VARS['useraccount']."'
		)";
	
	$resulttr=mysql_query($query,$connection);
	$errorno  = mysql_errno(); 
	if ($errorno!=0)
		{
			$HTTP_SESSION_VARS['error'] = goback($connection);
			 exit;
		}		
		
		
						/*
						$query="update sequence set transfer_id='$transfer_id' ,transfer_reference='$ref' ";
						$resultseq=mysql_query($query,$connection) or die(mysql_error());
						*/

	for ($j=0;$j<$countload;$j++)
	{
		
		//echo $countload;
		/*maroun
		$seqquery="select transferdt_id+1 as trdtid from sequence";
		$seqresult=mysql_query($seqquery,$connection) or die(mysql_error());
			while($seqrows=mysql_fetch_array($seqresult)){
			extract($seqrows);
			}
		*/

		$trdtid = get_nextsequence('transferdt_id','',$connection);		
		
		if ($relateddt_id[$j]=='')
				$relateddt_id[$j] = 'null';
		$item_sellingprice2=$item_sellingprice1[$j]/$curdt_parity;
		//echo "tr ".$transferdt_quantity;
		
		if ($item_quantity[$j]!='' or $item_quantity[$j]!=0)
		{
		$query="insert into transferdt
		(transferdt_id,transfer_id,transfer_date,item_code,transferdt_warehousefrom,transferdt_warehouseto,transferdt_quantity,transferdt_price,transferdt_pricebase1,transferdt_pricebase2,project_code1,project_code2,costcent_code,transferdt_remark,cur_code,relateddt_id)
		values(".$trdtid."
				,".$transfer_id."
				,now()
				,'".$item_code[$j]."'
				,'".$fromwarehouse."'
				,'".$towarehouse."'
				,'".$item_quantity[$j]."'
				,'".$item_sellingprice1[$j]."',
				'".$item_sellingprice1[$j]."',
				'".$item_sellingprice2."',
				'".$fromproject."',
				'".$toproject."',
				".$costcent.",
				'".$transferdt_remark[$j]."'
				,'".$BASE1."'
				,".$relateddt_id[$j]."
				)";
				
		//echo $query;
			$trdtresult=mysql_query($query,$connection);
			$errorno  = mysql_errno(); 
				if ($errorno!=0)
					{
						$HTTP_SESSION_VARS['error'] = "(Item Code: ".$item_code[$j].") ".goback($connection);
						exit;
					}
			}		
	/*
	$query4="update sequence set transferdt_id='$trdtid'";
	$resulttrd=mysql_query($query4,$connection) or die(mysql_error());
	*/
	}
	
	


if ($process=='load')	
	{
	/*
	$serialarrayload = $HTTP_SESSION_VARS["serialarrayload"] ;
		$count = count($serialarrayload);
		
		if ($count!='')
			{
			for($sr=0;$sr<$count;$sr++)
				{
					if ($serialarrayload[$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 = '".$serialarrayload[$sr][0]."' and transfer_id = ".$transfer_id;
							//echo $trdquery;
							$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]."'
								,'".$serialarrayload[$sr][0]."','".$serialarrayload[$sr][1]."','".$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(!$resulttr or !$resulttrd or !$resultpur or !$resultincserial)
			//if(!$resulttr or !$resulttrd  or !$resultincserial)
				//{
				//rollback();
			//	}
				//else
			//	{
				
			//	}
				
			}
			*/
					//// //// 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)){
					
						/*	maroun
						$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;
						*/
						$serial_id = get_nextsequence('serial_id','',$connection);		
						
						$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,flagunique) 
						values('".$serial_id."'
								,'TR'
								,'".$tdrows[0]."'
								,'".$row[3]."','".$row[10]."','".$towarehouse."','".$tdrows[0]."','".$tdrows[0]."')";
						//echo $query."<br>";
						$resultpur=mysql_query($query,$connection);
						$errorno  = mysql_errno(); 
							if ($errorno!=0)
								{
									$HTTP_SESSION_VARS['error'] = "(Item Code: ".$row[3].") ".goback($connection);
									exit;								
								}
						$query = "update purchasesserialno set serialno_used = 1 where serialno_code = '".$row[10]."' and warehouse_code = '".$fromwarehouse."'";
						$resultpurused=mysql_query($query,$connection);
						echo "<br>".$query."<br>";
						/*
						$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]!='')
						{
						/*maroun
						$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;
						*/
						$serial_id = get_nextsequence('serial_id','',$connection);		
						
						//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,flagunique) 
						values('".$serial_id."'
								,'TR'
								,".$tdrows[0]."
								,'".$serialarray[$sr][1]."','".$serialarray[$sr][0]."','".$towarehouse."',".$tdrows[0].",".$tdrows[0].")";
						//echo $query."<br>";
								$resultpur=mysql_query($query,$connection) ;
								$errorno  = mysql_errno(); 
									if ($errorno!=0)
										{
											$HTTP_SESSION_VARS['error'] = "(Item Code: ".$serialarray[$sr][1].") ".goback($connection);
											exit;
										}
						$query = "update purchasesserialno set serialno_used = 1 where serialno_code = '".$serialarray[$sr][0]."' and warehouse_code = '".$fromwarehouse."'";
						$resultpurused2=mysql_query($query,$connection);
						//echo "<br>".$query."<br>";
						/*
						$query="update sequence set serial_id='$serial_id'";
						$resultincserial=mysql_query($query,$connection) or die(mysql_error());
						*/
						}
				}
			}	
		//echo $serialarray[0][0]."<br>";
		//echo $serialarray[1][0];
		//echo $serialarray[2][0];
		
		}
if ($process=='load')
			if(!$resulttr  or !$resultpur  or !$trdresult10 or !$resultpurused  or !$resultpurused2)
					{
					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 ($HTTP_SESSION_VARS["serialarray"]!='')
				{
				if (!$resultpur)
					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  )
					{
						
					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();				
					}
			
	
	function fbegin()
{
	mysql_query("START TRANSACTION");
}
function fcommit(){
	mysql_query("COMMIT");
}
function frollback()
{
	mysql_query("ROLLBACK");
}

function goback($connection){

echo mysql_errno($connection) . ": " . mysql_error($connection) . "\n";
$HTTP_SESSION_VARS['error'] =  mysql_errno($connection) . ": " . mysql_error($connection) . "\n";;

print <<<HERE
<p>
<table border=0 width='85%'>
<tr><td align=center>
<script>
history.go(-1);
history.go(0)
</script>
</td></tr>
HERE;
//&errorno=$errorno
return $HTTP_SESSION_VARS['error']; 
 }

//exit;
header ("Location: transfertrans.php?transferid=".$transfer_id);
?>