<?php

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



include("config.inc.php");
include("Functions_Sales.php");
include("Forms_DataGrid_Delivery.php");

//echo $HTTP_SESSION_VARS['dbName']." ".$HTTP_SESSION_VARS['dbLabel'];
 

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


$querylog="SELECT FI_itemtransfer  as loginaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')	";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");

	
//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, ifNull(Client_ValidateProject,0) as Client_ValidateProject, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error()); 
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_Project,0) As Module_Project, ifNull(Module_CostCenter,0) As Module_CostCenter ".
             " From Module "; 
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}


$querydflwh="select warehouse_code as defaultwarehouse from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' and ifNull(default_warehouse,0) = 1 ";
$resultdflwh=mysql_query($querydflwh,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflwh)){
  extract($rows);
}

//Projects List
$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$_SESSION['dbLabel'].".loginstk on project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%') ".
			  " where user_account='".$_SESSION['useraccount']."' ".
			  " order by project.project_code"; 
$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
$projectrows   = mysql_num_rows($projectresult);
if ($projectrows == 0)
    $queryproject=" select distinct project_code, project_name from project order by project_code ";
			  
//$resultfromproject=mysql_query($queryproject,$connection) or die(mysql_error());


//Costcenter List
$querycostcent=" select distinct costcent.costcent_code, costcent_name ".
               " from costcent left join ".$_SESSION['dbLabel'].".loginstk on costcent.costcent_code like concat(loginstk.costcent_code,'%') ".
 			   " where user_account='".$_SESSION['useraccount']."' ".
 			   " order by costcent.costcent_code";
			  
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentrows   = mysql_num_rows($costcentresult);
if ($costcentrows == 0)
    $querycostcent=" select distinct costcent_code, costcent_name from costcent order by costcent_code ";			  
//$resultcostcent=mysql_query($querycostcent,$connection) or die(mysql_error());


$querycurrency="select Currency_code from currency order by currency_code";
$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error());


$HideMenu   = $_POST["HideMenu"]; if ($HideMenu == ""){$HideMenu = $_GET["HideMenu"];} 
$actionmode = $_POST["actionmode"]; if ($actionmode == ""){$actionmode = $_GET["actionmode"];}   
$mainaction = $_POST["mainaction"]; if ($mainaction == ""){$mainaction = $_GET["mainaction"];}   



if (empty($mainaction))	$mainaction = 'insert';

$message = ereg_replace("'"," ",$message);

print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">


<script type="text/javascript" language="javascript">

function showlistitem(str){

	window.open('select_item.php?item='+str,'mywindow','width=900,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function ItemTrans(){
	var fromnbr = document.getElementById("txtfromserialnb").value;
	var tonbr = document.getElementById("txttoserialnb").value;
	var qty = document.getElementById("txtRequestedQty").value;
	var verror = 0;
	
	if(document.getElementById("fromitem").value==''){
		alert('Please Choose an Item!');
		verror = 1;
		return 0;
	} else if(document.getElementById("txtRequestedQty").value==''){
		alert('Please Enter Some Quantities!');
		verror = 1;
		return 0;
	} else if(fromnbr != '' && tonbr != ''){
		fromnbr = parseFloat(fromnbr);
		tonbr = parseFloat(tonbr); 
		if( qty != parseFloat((tonbr-fromnbr)+1) ){
			alert('Number of Serial Numbers Doesn\'t match with the Requested Quantity!');
			verror = 1;
			return 0;
		}
	}
	if(verror == 0) {
	document.itemtrans.action = 'Form_itemTransfer_Actions.php?actionmode=submitdetails';
	document.itemtrans.submit();
	}
}

function ClearPage(){
	document.itemtrans.fromitem.value='';
	document.itemtrans.fromitemdesc.value='';
	document.itemtrans.txtRequestedQty.value='';
	document.itemtrans.message.value='';
	document.itemtrans.action = 'Form_itemTransfer.php';
	document.itemtrans.submit();
}

function SaveTrans(){
	if (confirm ( "Are you sure you want to Save?"))
	{
		document.itemtrans.action='Form_itemTransfer_Actions.php?actionmode=saveadjustment';
		document.itemtrans.submit();
	}	
}

function CancelPage(){
	if (confirm ( "Are you sure you want to Cancel?"))
	{
	document.itemtrans.action = 'Form_itemTransfer_Actions.php?actionmode=cancel';
	document.itemtrans.submit();
	}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Items Transfer</title>




<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>

</head>
<body >
<form name='itemtrans' method='post' action=''   enctype='multipart/form-data'>
<input type='hidden' name='HideMenu' value='$HideMenu'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='message' value='$message' size='200'>
<input type='hidden' name='txtadjid' value='$txtadjid'>
HERE;

//$message='';

	
//Initializations

	
//Get Default Project
$querydflproject="select project_code as defaultproject from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' and default_project=1 ";
$resultdflproject=mysql_query($querydflproject,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflproject)){
	extract($rows);
}
	
//Get Default Coscenter
$querydflcostcent="select costcent_code as defaultcostcent from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' and default_costcenter=1 ";
$resultdflcostcent=mysql_query($querydflcostcent,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflcostcent)){
	extract($rows);
}

/// if client changed then update all related to client
UpdateTransaction($txtSaId,$txtClientCode,$txtProjectCode,$txtCostCentCode,$txtMaturityDate,$connection);



if ((empty($txtProjectCode)) && $Module_Project == 1)
	$txtProjectCode = $defaultproject;
if ((empty($txtCostCentCode)) && $Module_CostCenter == 1)
	$txtCostCentCode = $defaultcostcent;	

if (empty($txtCurr) )
{	
	if ($ClientCur != '')
		$txtCurr = $ClientCur;
	else
		$txtCurr = $base1;
}
if (empty($txtDate))	
	$txtDate = displayToday();
 

if($txtRequestedQty == '') $txtRequestedQty=1;

echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
if ($HideMenu != 1) include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'><input type='hidden' id='txtSaDiscount_Temp'  name='txtSaDiscount_Temp' value='$txtSaDiscount_Temp'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>Item Transfer</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";

echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='30'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px' align='left'>";

	//	if ($Module_DeliveryWithSorder == 0)
	//echo"	<input type='button' id='btnAddItem' name='btnAddItem' value='Add Item'  onclick='AddItems($Module_RestrictProjCost, $Module_RestrictCost, $Module_Project, $Module_CostCenter, $FI_SalesCheckSalesman)' $style title='Add Item' >";

	if($mainaction=='submitrecord')
	echo" <button type='button' id='btnCancel' name='btnCancel' onclick='CancelPage();return false;' title='Cancel'><span><span>Cancel</span></span></button>";
	else 
	echo" <button type='button' id='btnClear' name='btnClear' onclick='ClearPage();return false;' title='Clear Page'><span><span>Clear Page</span></span></button>";
	


echo" <button type='button' id='btnSaveTrans' name='btnSaveTrans' onclick='SaveTrans();return false;' title='Save'><span>
<span>Save</span></span></button>";

echo"	</td></tr></table>";
echo"</td></tr>";				

		
echo"<tr><td id='content-body'>
<table border='0'  align='center' valign='top' width=100% class='tablecontent'>
   <tr bgcolor='#F7F7F7'><td valign='top' align='left' colspan='10'>";	


   
print <<<HERE

		<table border="0" style='table-layout:fixed'>
		  <col width=90>
		  <col width=100>
		  <col width=100>
		  <col width=100>
		  <col width=100>
		  <col width=70>
		  <col width=70>
		  <col width=80>
		  <col width=80>
		  <col width=120>

HERE;
if($mainaction == 'submitrecord'){
 echo "<tr height=20'><td colspan='2'><span class='style1'>Item Code/Desciption</span></td><td><span class='style1'>Quantity</span></td>
	<td><span class='style1'>Price</span></td><td><span class='style1'>Total</span></td></tr>";
 
	$query = "select temp_adjustment.Item_Code, Item_Description
			from temp_adjustment
			left join items on items.item_code = temp_adjustment.Item_Code where adjustment_id=$txtadjid
			"; 
	$result = mysql_query($query,$connection);
	$row = mysql_fetch_row($result);
	$fromitem = $row[0];
	$fromitemdesc = $row[1];
	}
echo "<tr bgcolor='#F7F7F7' height='25'> <td><span class='style1'>Item </span></td>";
echo"   <td colspan='4' valign='middle' bgcolor='#F5F5F5' ><span class='style8'>";
echo"   <label>
        <input name='fromitem' type='text' id='fromitem' style='width:120px;' value='".$fromitem."' />
        <input name='fromitemdesc' type='text' id='fromitemdesc' style='width:250px;' value='".$fromitemdesc."' />
        <input name='btncode1' type='hidden' id='btncode1' value='...' style='width:20px; height:20px; border:thin;' title='Select Item From' />
        </label>
      </span>
      <label><span class='style11'><a href='javascript:void(0);' NAME='My Window Name' title=' Select Item From ' 
             onClick='showlistitem(5); '><img src='img/btnopen.jpg' width='15' height='20' border='0' /></a> </span></label>      ";

echo "</tr>";

echo "<tr bgcolor='#F7F7F7' height='25'> <td><span class='style1'>Quantity </span></td>";
echo"   <td colspan='4' valign='middle' bgcolor='#F5F5F5' ><span class='style8'>";
echo"   <label>
        <input name='txtRequestedQty' type='text' id='txtRequestedQty' style='width:120px;' value='".$txtRequestedQty."' />
        </label>
      </span></td>";
echo "</tr>";

//check if item with serial number
if($fromitem != ''){
	$queryserial = "select ifnull(item_withserialno,0) from items where item_code='$fromitem';";
	$resserial = mysql_query($queryserial,$connection);
	$rwserial = mysql_fetch_row($resserial);
	if($rwserial[0] == 1){
		
		echo "<tr bgcolor='#F7F7F7' height='25'> <td><span class='style1'>Serial # Prefix </span></td>";
		echo"   <td colspan='4' valign='middle' bgcolor='#F5F5F5' ><span class='style8'>";
		echo"   <label>
				<input name='txtprefixserialnb' type='text' id='txtprefixserialnb' style='width:120px;' value='".$txtprefixserialnb."' />
				</label>
			  </span></td>";
		echo "</tr>";
		echo "<tr bgcolor='#F7F7F7' height='25'> <td><span class='style1'>From</span></td>";
		echo"   <td colspan='4' valign='middle' bgcolor='#F5F5F5' ><span class='style8'>";
		echo"   <label>
				<input name='txtfromserialnb' type='text' id='txtfromserialnb' style='width:120px;' value='".$txtfromserialnb."' />
				</label>
			  </span>";
		echo "<span class='style1'>To</span><span class='style8'>
				<label>
				<input name='txttoserialnb' type='text' id='txttoserialnb' style='width:120px;' value='".$txttoserialnb."' />
				</label>
			  </span>";	  
		echo  "</td>";
			  
		echo "</tr>";
		
	}	
}
echo "<tr bgcolor='#F7F7F7' height='30'> <td>";
if($txtadjid != '') $dis = 'disabled';
echo"   <button type='button' id='btnItemTrans' name='btnItemTrans' onclick='ItemTrans();return false;' title='Submit' $dis><span><span>Submit</span></span></button></td>";
 echo "</td></tr>";
 
 echo "<tr height='30'><td></td></tr>";
 if($mainaction == 'submitrecord'){
 echo "<tr height=20'><td colspan='2'><span class='style1'>Item Code/Desciption</span></td><td><span class='style1'>Quantity</span></td>
	<td><span class='style1'>Price</span></td><td><span class='style1'>Total</span></td></tr>";
	$query = "select ABS(AdjustmentDt_Quantity), item_code
			from temp_adjustmentdt where adjustment_id=$txtadjid and journal_type = 'AR' "; 
	$result = mysql_query($query,$connection);
	$rw = mysql_fetch_row($result);
	$qty = $rw[0]; 
	$itemcode = $rw[1]; 
			
	$query = "select temp_adjustmentdt.Item_Code, Item_Description, ABS(AdjustmentDt_Quantity),AdjustmentDt_Price,journal_type
			from temp_adjustmentdt
			left join items on items.item_code = temp_adjustmentdt.Item_Code where adjustment_id=$txtadjid and journal_type = 'AI'
			order by journal_type asc"; 
	$result = mysql_query($query,$connection);
	while($rws = mysql_fetch_row($result)){
		
		echo "<tr><td colspan='2'><font size='2'>$rws[0] - $rws[1]</font></td><td><font size='2'>$rws[2]</font></td>
			<td><font size='2'>".number_format($rws[3]*$rws[2]/$qty,$DecimalBase)."</font></td><td colspan='2'><font size='2'>".number_format($rws[3]*$rws[2],$DecimalBase)."</font></td></tr>";
		
	}
	 echo "<tr><td colspan='5'><hr></td></tr>";
	$query = "select temp_adjustmentdt.Item_Code, Item_Description, ABS(AdjustmentDt_Quantity),AdjustmentDt_Price,journal_type
			from temp_adjustmentdt
			left join items on items.item_code = temp_adjustmentdt.Item_Code where adjustment_id=$txtadjid and journal_type = 'AR'
			order by journal_type asc"; 
	$result = mysql_query($query,$connection);
	while($rws = mysql_fetch_row($result)){
		
		echo "<tr><td colspan='2'><font size='2'>$rws[0] - $rws[1]</font></td><td><font size='2'>$rws[2]</font></td>
			<td><font size='2'>".number_format($rws[3],$DecimalBase)."</font></td><td colspan='2'><font size='2'>".number_format($rws[3]*$rws[2],$DecimalBase)."</font></td></tr>";
		
	}
 
 }
 
echo"   </table></td></tr>"; 
 echo"   </table></td></tr><tr height='30'><td align='center' id='content-bottom' ></td></tr>";  
echo"   </table></td></tr>"; 

echo" </table>  ";//end of last table menu
	

print <<<HERE
</form>
</body>

HERE;

?>