<link rel="stylesheet" href="css/new_mxerp.css">
<?php ob_start();
$PageSecurity = 4;
$PageName = 'Issue Slip';
include('includes/DefinePOClass.php');
$Prefix_SQL = "Select prefix_id,prefix_code,prefix_name from prefix_tbl Where prefix_name ='".$PageName."'";
$Prefix_Result = DB_query($Prefix_SQL,$db);
$Prefix_Row = DB_fetch_array($Prefix_Result);
$Prefix_code = $Prefix_Row['prefix_code'];
$Prefix_date = date("Y");
$Prefix_date++;
$CurrentFinancialYear = date("Y")."-".$Prefix_date; 
if(isset($_GET['issueid'])  && isset($_GET['issueid']) !='')
{$title = _('Update Issue Slip');}
else
{$title = _('Add Issue Slip');}
include('includes/SQL_CommonFunctions.inc'); 
$IssueListFormId=$px->GetFormId('Issue_show.php');
$_SESSION['modid'];
if (empty($_GET['identifier'])) {
 $identifier=date('U');
} else {
 $identifier=$_GET['identifier'];

}
///////////////////////////////////////////// Add IUssue Slip  Form  By Amit Kumar////////////////////////////////////////
		 $date = date($_SESSION['DefaultDateFormat']);
if(isset($_POST['Submit']))
{
	//echo "<pre>";print_r($_POST); 	exit;
	 $_POST['PRDate']=Date($_SESSION['DefaultDateFormat']);
	$_POST['DatePrinted']=Date($_SESSION['DefaultDateFormat']);
	ConvertSQLDate($_SESSION['MR'.$identifier]->Orig_OrderDate);
/////////////////////////////////////////////////  Add ISSUE Slip Number With Financial Year And Module //////////////////////////////////////////	
	 $sql="select id, issue_slipno from addissue where site='".$_REQUEST['sitecode']."' order by id desc limit 1";
	 $result = DB_query($sql,$db);
	  $numrows = DB_num_rows($result);
	 if($numrows>0)
	 {
			 $i=0;
			 $strmax = "";
			 $myrow=DB_Fetch_array($result);
			 
			 if($myrow['issue_slipno'] !='')
			 {
			         $val= substr($myrow['issue_slipno'],19);
					   $strChar = substr($myrow['issue_slipno'],0,19);
					 if($val!="")
					 {
						 $arr[$i] = $val;
						 $i++;
					}
			
			if(is_array($arr))
			{ 
				$maxval =  max($arr);
			}
			else
			{
				$maxval=0;
			}
		}
		else
		{
			$strChar = $Prefix_code."/".$_REQUEST['sitecode']."/".$CurrentFinancialYear."/";
		}
	  }
	  $strChar = $Prefix_code."/".$_REQUEST['sitecode']."/".$CurrentFinancialYear."/";
	  
	$strFinal = $maxval+1;
	$strBothFinal = $strChar.str_pad($strFinal,4,"0",STR_PAD_LEFT);
	
	$qty=$_REQUEST['itemcode'];
	$qty1=count($qty);
	
	$sql = "INSERT INTO addissue set
						id='',  
						site='".$_REQUEST['sitecode']."',  
						issue_slipno='".$strBothFinal."',  
						issuedate='".FormatDateForSQL($_REQUEST['issuedate'])."',  
						store='".$_REQUEST['storecode']."',  
						issueto='".$_REQUEST['issueto']."',  
						received_by='".$_REQUEST['received_by']."',
						department='".$_REQUEST['department']."',
						work_order_no='".$_REQUEST['work_order_no']."',
						remarks='".$_REQUEST['remarks']."',
						createdby='".$_SESSION['UserID']."',  
						createddate='".FormatDateForSQL(date('d/m/Y'))."' ,
						CompanyId='".$_SESSION['ACGCompanyId']."'";
						
				$ErrMsg = _('An error occurred Record Note Save') . '. ' . _('The error says');
				$insertResult = DB_query($sql,$db,$ErrMsg);
			 	$Lastreq_id=DB_Last_Insert_ID($db,'addissue',$ErrMsg);
			 
				if($Lastreq_id!='')
				{
					$totalqty=0;
				for($i=0;$i<$qty1;$i++)
					 {
			        	 	 $unitID=$s->getField('unitsofmeasure','unitname',$_REQUEST['uom'][$i],'unitid'); 
					
					$sql2 = "INSERT INTO issueqty set
								id='".$_REQUEST[''][$i]."', 
								issue_id='".$Lastreq_id."', 
								itemcode='".$_REQUEST['itemcode'][$i]."', 
								itemdesc='".$_REQUEST['desc'][$i]."', 
								uom='".$unitID."',    
								Stockbalance='".$_REQUEST['Stockbalance'][$i]."', 
								issueqty='".$_REQUEST['issueqty'][$i]."', 
								averageamt='".$_REQUEST['averageamt'][$i]."',
								totalamount='".$_REQUEST['totalamount'][$i]."' ";
					 	
					 $totalqty=$totalqty+$_REQUEST['issueqty'][$i];
					  $ErrMsg = _('An error occurred Record Note Save') . '. ' . _('The error says');
				      $insertResult2 = DB_query($sql2,$db,$ErrMsg);
					 
					  $stock=DB_query("Select * from stock_maintenance where itemcode='".$_REQUEST['itemcode'][$i]."'",$db);
					$stock_row=DB_fetch_array($stock);
					 $condition1="itemcode='".$_REQUEST['itemcode'][$i]."' && site='".$_REQUEST['sitecode']."' && store='".$_REQUEST['storecode']."'";  
					
					 $current_bal=$stock_row['stockbalance']-$_REQUEST['issueqty'][$i];
					
					//print_r($current_bal); exit;
					//echo $_REQUEST['itemcode'][$i];
					$balance_arr['site'] = $_REQUEST['sitecode'];
					$balance_arr['store'] = $_REQUEST['storecode'];  
					$balance_arr['itemcode']= $_REQUEST['itemcode'][$i];
					
					$balance_arr['stockbalance']=$current_bal;
					$balance_arr['created_by']=$_SESSION['UserID'];
					//print_r($balance_arr);
					//exit;
					$update1=$s->EditRecord(stock_maintenance, $balance_arr, $condition1);
					$row1=DB_query($update1,$db);
					
					if(!$row1)
					{
	  					 die('Error'.DB_error());
					}
				 }
			}
				
$updateQty=DB_query("Update addissue set totalqty='".$totalqty."' where id='".$Lastreq_id."'",$db);
					
					
if($insertResult == true)
{
	 prnMsg( _('Record Inserted Successfully'),'success');
	echo "<script>location.href='index.php?route=Issue_show&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
 }
 ?>
<script language="javascript" type="text/javascript">
function Remaningqtn(str)
{
alert(str);
var StockBalance=document.getElementById("Stockblance_"+i).value;	
var Issuequantity=document.getElementById("issuebalanc_"+i).value;	
var RemaningBalance =parseFloat(StockBalance)-parseFloat(Issuequantity;)
document.getElementById("remaningbalance_"+i).value =RemaningBalance;
}

var countries=new ddtabcontent("countrytabs")
countries.setpersist(true)
countries.setselectedClassTarget("link") //"link" or "linkparent"
countries.init();
function calTotal(obj)
{
	alert("manvi");
	var rowCount = document.getElementById('dataTable').rows.length;
	var arr = obj.id.split("_");
	var rowx1=0;
	if (arr[1]==undefined)
	var nm='';
	else
	var nm='_'+arr[1];
	for(var i=1;i<rowCount;i++)
	{
		
		 if(parseInt(document.getElementById('reqiredbalance'+nm).value) <= document.getElementById('issuebalanc'+nm).value))
		  {
			  alert('Please Enter Less then value In Required Quantity');
			  document.getElementById('issuebalanc'+nm).focus();
			  return false;
	  }else{	
			  rowx1=parseInt(document.getElementById('Stockblance'+nm).value) - parseInt(document.getElementById('issuebalanc'+nm).value);
			if(isNaN(rowx1))
			{
				document.getElementById('remaningbalance'+nm).value=parseInt(0);
			}
			else
			 {
				document.getElementById('remaningbalance'+nm).value=rowx1;
			 }
		}
	}
}


function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	return xmlHttp;
}


 var xmlHttp
function changestorename(str)
{ 
 
 // alert('hi');
   xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		  {
		    alert ("Your browser does not support AJAX!");
		    return;
		  } 
	var url="storename.php";
	url=url+"?store_code="+str;
	//alert(url);
	//url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=NumberChanged1;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function NumberChanged1() 
{ 
	if (xmlHttp.readyState==4)
	{ 
	   document.getElementById("txtid").innerHTML=xmlHttp.responseText;
	}
}
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	return xmlHttp;
}
</script>

<div class="body_contain">
<div class="tab_div">


<h1>New Issue Slip</h1>
<form name="form1" action="" method="post">
<div align="center">
<table width="100%" cellpadding="3" cellspacing="0" border="1" style="border:#CCCCCC solid 1px;">
<tr>
    <td class="tabledata1">Branch</th>
    <td>
	<?php
		 $sql = "SELECT loccode, locationname FROM locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode='".$_SESSION['AssignedBranch']."'"; 
		$resultStkLocs = DB_query($sql,$db);
		?>
	<select name="sitecode" style="width:100%">
	<option value="0">Select</option>
      <?php 
			while ($myrow=DB_fetch_array($resultStkLocs))
				{
				?>
      <option value="<?php echo $myrow['loccode']; ?>"><?php echo $myrow['locationname'];?></option>
      <?  }	?>
    </select></td>
    <td class="tabledata1">Godown</th>
    <td><?php
	     $sql_store ='select distinct StoreCode, StoreName from storetype';
		 $resultStkLocs = DB_query($sql_store,$db);
		?>
      <select name="storecode" style="width:100%">
        <option value="0">Select</option>
        <?php 
		while ($myrow=DB_fetch_array($resultStkLocs))
		{?>
        <option value="<?php echo $myrow['StoreCode'];?>"><?php echo $myrow['StoreName'];?></option>
        <?php 
		}
		?>
      </select></td>
</tr>
  <tr>
    <td class="tabledata1">Issue Slip No.</th>
    <td><input name="issue_slipno" type="text" id="issue_slipno" style="width:100%;" readonly="readonly" value="Autogenerate"></td>
    <td class="tabledata1">Issue Date </th>
    <td><input name="issuedate" type="text" id="issuedate" value="<?php echo $date; ?>" class="datepicker" style="width:100%" /></td>
  </tr>
  
  
  <tr>
    <td class="tabledata1">Issue To </th>
    <td>
	<select name="issueto" id="issueto" style="width:100%" onchange="Get_issuedetails(this.value)" >
	<option value="0">Select</option>
	<?php 
	$floor_sql=DB_query("Select * from floor_master",$db);
	while($floor_row=DB_fetch_array($floor_sql))
	{?>
	<option value="<?php echo $floor_row['id'];?>"><?php echo $floor_row['floorName'];?></option>
	<?php 	
	}
	?>
    </select></td>
    
    <td class="tabledata1">Received By </th>
    <td>
	<?php 
	
		$sql="select vendor_code, vendor_name from suppliers";
			$result = DB_query($sql, $db);
			?>
			<select name="received_by" id="received_by" onchange="Get_departmentdetails(this.value)" style="width:100%">
			<option value="000">---Select---</option>
			<?php
			while($myrow = DB_fetch_array($result))
			{ 
			$p='';
			if($myrow['vendor_code']==$vendor)
			$p="selected";
			?>
			 <option  value="<?php echo $myrow['vendor_code']; ?>"<?php echo $p;?>><?php echo $myrow['vendor_name']; ?></option>
		<?php }
			?>
			</select>
	
	
	<!--<select name="received_by" id="received_by" style="width:100px" onchange="Get_departmentdetails(this.value)">
	<option value="0">Select</option>
	<?php 
	$floor_sql=DB_query("Select * from empmaster",$db);
	while($floor_row=DB_fetch_array($floor_sql))
	{?>
	<option value="<?php echo $floor_row['id'];?>"><?php echo $floor_row['emp_name'];?></option>
	<?php 	
	}
	?>
 </select> -->
    
    </td>
    
  </tr>
  <tr>
  	
    <td class="tabledata1">Department</th>
    <td><input name="department" type="text" id="department" style="width:100%" readonly="readonly"></td>
    
    <td class="tabledata1">Work Order No. </th>
    <td>
	<select name="work_order_no" id="work_order_no" style="width:100%" >
	<option value="0">Select</option>
	<?php 
	$floor_sql=DB_query("Select * from salesorder",$db);
	while($floor_row=DB_fetch_array($floor_sql))
	{?>
	<option value="<?php echo $floor_row['id'];?>"><?php echo $floor_row['orderNo'];?></option>
	<?php 	
	}
	?>
    </select></td>
    </tr
  ><tr></tr>
</table>
</div>
<p align="left">
<input type="button" onclick="addRow('dataTable')" value="Add More" />&nbsp;
<input type="button" class="crmbutton small delete"  onclick="deleteRow('dataTable')" value="Delete" />
</p>
<div align="center">
<div style="overflow:auto; width:800px; height:auto">

<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#999999 solid 1px;">
  <tr>
    <th>&nbsp;</th>
    <th><strong>Pick Items</strong></th>
    <th><strong>Item Code</strong></th>
    <th><strong>Description</strong></th>
     <th><strong>UOM</strong></th>
    <th><strong>Stock Balance </strong></th>
    <th><strong>Issue Qty </strong></th>
  </tr>
  <tbody id="dataTable">
  <tr bgcolor="#FFFFFF">
  <td><input type="checkbox" name="chk" />
	  <input type="hidden" value="" id="user_shipping" name="user_shipping[]" /></td>
  <td><input type="button" name="profile_popup" value="+" id="profile_popup"  onclick="openPopupProfile(this.id)" />      </td>
  <td><input type="text" value="" tabindex=""  readonly="" id="washing_profile" name="itemcode[]"size="10" />      </td>
  <td><input type="text" name="desc[]"  id="desc" size="25" readonly/></td>
   <td><input type="text" name="uom[]" id="uom" value="" readonly/></td>
  <td><input type="text" name="Stockbalance[]"  id="Stockbalance"  size="10" readonly/></td>
  <td><input type="text" name="issueqty[]"  id="issueqty" size="10" onblur="calTotal(this)"/></td>
  </tr>
  </tbody>
</table>

</div>
</div>
<div style="overflow:auto; width:800px; margin-left:5px;">
<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#999999 solid 1px;">
<tr>
<td class="tabledata1">Remarks </td>
<td><textarea name="remarks" style="width:100%" id="remark"><?php echo $mysqlrow['remarks']; ?></textarea></td>
</tr>
</table>
</div>
<p align="center"><input type="submit" name="Submit" value="Save" /></p>
</form>





</div></div>






<script language="javascript" type="text/javascript">
function openPopupProfile(i)
{ 
		window.open("issu_searchpopup.php?rowcount="+i,"profiles_popup_window","height=425,width=640,toolbar=no,menubar=no,dependent=yes,resizable=yes");
}
 var xmlHttp

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	return xmlHttp;
}

function getsitename(str)
{
if (str.length==0)
  {
  document.getElementById("txtsite").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtsite").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_sitename.php?site="+str,true);
xmlhttp.send();
}
function getsitename22(str)
{
if (str.length==0)
  {
  document.getElementById("txtsite22").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtsite22").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_sitename.php?q2="+str,true);
xmlhttp.send();
}
function Get_issuedetails(str)
{
  if (str.length==0)
  {
  document.getElementById("issueid").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("issueid").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_IssueDetails.php?q="+str,true);
xmlhttp.send();
}

function getpartyname(str)
{
	//alert(str);
	var hidparty=document.getElementById("hidden_party").value;
	//alert(hidparty);
	if (str.length==0)
  {
  document.getElementById("pname").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("pname").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_IssuePartyName.php?name="+str+"&partytype="+hidparty,true);
xmlhttp.send();
}
	

function get_referredby(str)
{
	if (str.length==0)
  {
  document.getElementById("rname").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("rname").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_IssueEmployeeName.php?q="+str,true);
xmlhttp.send();
}

 var xmlHttp
function requisition_detail(str)
{ 
   //alert(str);
   xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		  {
		    alert ("Your browser does not support AJAX!");
		    return;
		  } 
	var url="reqdetail.php";
	url=url+"?req_number="+str;
	
	//url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=changedetail;
	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function changedetail() 
{ 
	if (xmlHttp.readyState==4)
	{ 
	   var responsestr = xmlHttp.responseText;
	   resstr = responsestr.split("@@##@");
	   document.getElementById("reqdate").value=resstr[0];
	   document.getElementById("txtdetail").innerHTML=resstr[1];
	  
	}
}
function calculateTotalAmount(str)
{
	var str_BRK=str.split('_');
	if(str_BRK[1]==undefined)
	{
		var issue=document.getElementById("issueqty").value;
		var average=document.getElementById("averageamt").value;
		var TOTAL=issue*average;
		document.getElementById("totalamount").value=TOTAL.toFixed(2);
	}
	else
	{
		var issue=document.getElementById("issueqty_"+str_BRK[1]).value;
		var average=document.getElementById("averageamt_"+str_BRK[1]).value;
		var TOTAL=issue*average;
		document.getElementById("totalamount_"+str_BRK[1]).value=TOTAL.toFixed(2);
	}
}
function Get_departmentdetails(str)
{
	

if (str=="")
  {
  document.getElementById("department").innerHTML="";
  return;
  }
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	document.getElementById("department").value=xmlhttp.responseText;
	
    }
  }
  //alert(str);
xmlhttp.open("GET","get_department.php?department1="+str,true);
xmlhttp.send();

//xmlhttp.open("GET","getSort.php?s="+str+"&cat="+cat,true);

}
</script>