<link rel="stylesheet" href="css/new_mxerp.css">
<link rel="stylesheet" type="text/css" href="css/professional/table_frize.css">

<?php ob_start();
$PageSecurity = 4;

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 Finished Material');}
else
{$title = _('Add Finished Material');}
include('includes/SQL_CommonFunctions.inc'); 
$_SESSION['modid'];
if (empty($_GET['identifier'])) {
 $identifier=date('U');
} else {
 $identifier=$_GET['identifier'];

}
$date = date($_SESSION['DefaultDateFormat']);
if(isset($_POST['Save']))
{

	
	$sitename=$s->GetFieldValue('locations',"loccode='".$_REQUEST['site']."'",'prefix');
	$sqlqut=DB_query("select * from inv_scrap where site='".$_REQUEST['site']."'",$db);
	if(DB_num_rows($sqlqut)>0)
	{
	$SQLMax=DB_query("select MAX(slip_no) as QutNo from inv_scrap where site='".$_REQUEST['site']."'",$db);
	$MaxQut=DB_fetch_array($SQLMax);
	$MXNo=substr($MaxQut['QutNo'],-2);
	$FinMx=$MXNo+1;
	$FinalCode=$sitename."/"."SCP"."/".str_pad($FinMx,5,0,STR_PAD_LEFT);
	}
	else
	{
	$FinalCode=$sitename."/"."SCP"."/".'00001';
	}
	 
  //print_r($_REQUEST); exit;
     $sql = "INSERT INTO inv_scrap set
						site='".$_REQUEST['site']."',
						slip_no='".$FinalCode."',
						store='".$_REQUEST['store']."',  
						job_card_id='".$_REQUEST['job_card_no']."',  
						remarks='".$_REQUEST['remarks']."',					
                  date='".date('Y-m-d')."',
						created_by='".$_SESSION['UserID']."',  
						created_date='".FormatDateForSQL(date('d/m/Y'))."' ,
						companyID='".$_SESSION['ACGCompanyId']."'";

	$insertResult = DB_query($sql,$db,$ErrMsg);
     $Last_id=DB_Last_Insert_ID($db,'inv_scrap',$ErrMsg);
    // echo $Last_id;
			 
if($Last_id!='')
	{
		
		for($i=0;$i<count($_REQUEST['itemdataid']);$i++)
		{
			$sql2 = "INSERT INTO inv_scrap_details set
								parentid='".$Last_id."', 
								mst_itemid='".$_REQUEST['itemdataid'][$i]."',
								qty='".$_REQUEST['reciveqty'][$i]."'";
			
			 $insertResult2 = DB_query($sql2,$db,$ErrMsg); 
			 
			 
				
				
		}
         prnMsg( _('Record Insert Successfully'),'success');
     echo "<script>location.href='index.php?route=Inventory/transation/scrap_process_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
     unset($_REQUEST);
}
	 }


 ?>

<div class="body_contain" style="width:1220px;">
<div class="tab_div">

 <h1>Production Waste & Scrap Process</h1>
<form name="form1" action="" method="post">
<div align="center">
<table width="100%" border="0" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
<tr class="row1">
    <td class="tabledata1" width="15%"><strong>Site</strong></td>
    <td width="35%"><?php
	if($_SESSION['AssignedBranch']!='')
	  {	  	
  $ss="Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
	  	 $site_sql=DB_query($ss,$db);
	  }
	  else
	  {
		$ss="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$site_sql=DB_query($ss,$db);
	  }
	?> 

	<select name="site" id="site" style="width:150px;" onchange="showsitename158(this.value)">
	<option value="0">Select</option>
       <?php 
					while($Row=DB_fetch_array($site_sql)){?>
             <option value="<?php echo $Row['loccode']?>"<?php echo($_SESSION['Site']==$Row['loccode'] ? 'selected':'');?> ><?php echo $Row['locationname']?></option>
                                <?php  } ?>
    </select></td>
    <td class="tabledata1" width="15%"><strong>Production Slip No.</strong></td>
    <td id="ISSUE"><input name="production_slip_no" type="text" id="return_slipno2" value="<?php if($_REQUEST['return_slipno']=='') {echo "Auto Generate";} else {echo $_REQUEST['return_slipno'];} ?>" size="15" style="width:150px;" readonly />  </td>  
	</tr>
  <tr class="row1">
    <td class="tabledata1"><strong>Godown</strong></td>
    <td><div id='hid-Godown-div'>
	<?php
	     $sql_store ='select distinct StoreCode, StoreName from storetype';
		 $resultStkLocs = DB_query($sql_store,$db);
		?>
      <select name="store" id="store" style="width:150px;">
        <option value="0">Select</option>
        <?php 
		while ($myrow=DB_fetch_array($resultStkLocs))
		{?>
        <option value="<?php echo $myrow['StoreCode'];?>"<?php echo($_REQUEST['store']==$myrow['StoreCode']?'selected':'')?>><?php echo $myrow['StoreName'];?></option>
        <?php 
		}
		?>
      </select></div></td>
    <td class="tabledata1"><strong>Job Card</strong>  </td>
    <td><div id="workorder">
      
	<select name="job_card_no" id="job_card_no" onchange="Get_oderdata(this.value)" style="width:150px;">
		<option value="0">Select</option>
		<?php       
    $floor_sql1=DB_query("Select * from qc_shop_floor where CompanyId='".$_SESSION['ACGCompanyId']."' order by id ASC",$db);
              
                while($floor_row1=DB_fetch_array($floor_sql1))
			{?>
	
     <option value="<?php echo $floor_row1['jobid']."-".$floor_row1['scrap_item_id'];?>"<?php echo $work_order_no==$floor_row1['jobid']."-".$floor_row1['scrap_item_id']?'selected':''; ?>><?php echo $s->getField('job_card','id', $floor_row1['jobid'],'job_card_no');?></option>
	<?php 	
                   }            
                        
	?></select>
	</div>
	
	</td>
  </tr>
  
  
  
   
</table>
</div>


 <div id="item">
 </div>
 




<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <tr class="row1">
    <td class="tabledata1" width="15%">Remarks:</td>
    <td class="tabledata1"><textarea name="remarks" id="remarks" cols="" rows="" style="width:100%; height:60px;"></textarea></td>
  </tr>
</table>



<p align="center"><input type="submit" name="Save" value="Save" class="search" /></p>
</form>
</div>
</div>




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

function addRow(tableID) 
{	
	if(document)
		var table = document.getElementById(tableID);
		
		var rowCount = table.rows.length;
		var row = table.insertRow(rowCount);
		document.getElementById("hiditemcount").value=rowCount;
		var colCount = table.rows[0].cells.length;
		for(var i=0; i<colCount; i++) {	 
            
			var newcell = row.insertCell(i);
			if(i==0)
	newcell.className='headerl_1';	
	else if(i==1)
	newcell.className='headerl_2';
	else if(i==2)
	newcell.className='headerl_3';
	else if(i==3)
	newcell.className='headerl_4';
	else
	newcell.className='col1';
           	//newcell.style.background='#e7e7e7';
			//  newcell.className='tabledata';
			   newcell.innerHTML = table.rows[0].cells[i].innerHTML;
				switch(newcell.childNodes[0].type) 
				{
				case "text":
					newcell.childNodes[0].value = "";
					var nod_id = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
										
				break;

				case "hidden":

					newcell.childNodes[0].value = "";

					var nod_id_hidden = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;

				break;

				case "button":

					var nod_id = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;

				break;

				case "checkbox":

				newcell.childNodes[0].checked = false;
				var nod_id_chk = newcell.childNodes[0].id;
				newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
				

				break;

				case "select-one":
				
				newcell.childNodes[0].selectedIndex = 0;
				var nod_id = newcell.childNodes[0].id;
				newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
				break;	
			}
		}
	}
function deleteRowOrder(tableID) 
{
	try 
	{
		var table = document.getElementById(tableID);
		//alert(table);
		var rowCount = table.rows.length;
		//alert(rowCount);
		for(var i=0; i<rowCount; i++) 
			{
				var row = table.rows[i];
				var chkbox = row.cells[0].childNodes[0];
				if(null != chkbox && true == chkbox.checked) 
				{
					if(rowCount <= 1) 
					{
						alert("Cannot delete all the rows.");
						break;
					}
				table.deleteRow(i);
				rowCount--;
				i--;
				}
				
			}
	document.getElementById("hiditemcount").value=parseInt(rowCount)-1;		
	}
	catch(e) 
	{
		alert(e);
	}

}
function issuedetails(str)
{
	var branch=document.getElementById("site").value;
	var godown=document.getElementById("store").value;
	
if (str.length==0)
  {
  document.getElementById("ISSUE").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("ISSUE").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","JobWorkItems.php?q="+str+"&site="+branch+"&store="+godown,true);
xmlhttp.send();

}
function openPopupProfile(i)
{ 
        window.open("issu_searchpopupfinish.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 opendata(str,id)
{

   var sitecode=document.getElementById("site").value;
   var date=document.getElementById("recieved_date").value;
   
  if (str.length == 0)
    {    	
     document.getElementById("lightbox1").innerHTML="";
     document.getElementById("lightbox1").style.border="0px";
     return;
    }	
	if(sitecode=='0')
	{
	alert("Must Select Site");
	document.getElementById("site").focus();
	return;
	}
	else if(date=='')
	{
	alert("Must Select Date");
	document.getElementById("recieved_date").focus();
	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("lightbox1").style.display='block';	
	document.getElementById("lightbox1").innerHTML=xmlhttp.responseText;	
   // document.getElementById("textitem").innerHTML=data[1];	
    }
  }
  var store=document.getElementById("store").value;
 

  xmlhttp.open("GET","issu_searchpopupfinish.php?site="+str+"&data="+id+"&store="+store+"&site1="+sitecode+"&date="+date,true);
	xmlhttp.send();	
	
}


function hidedata()
{
document.getElementById("lightbox1").style.display='none';
}

function getItemsList1(id,code,shade,netrate,mrprate,rate,ctr)
{ 
    var ctr1=ctr.split("_");
    var i=ctr1[1]
	var itemcount=parseInt(document.getElementById('hiditemcount').value)+1;
	var edit_cnt=document.getElementById('edit_cnt').value;
	if(edit_cnt==''){var counter=1;} else if(edit_cnt>itemcount){var counter=edit_cnt;
	var itemcount=parseFloat(counter)+parseFloat(itemcount);
	} else  {var counter=edit_cnt;}
	  
	if(ctr1[1]==undefined)
	{
	//if(document.getElementById('itemdataid').value=='')
	//{
	
	document.getElementById('itemdataid').value = id;
	document.getElementById('itemcode').value = code;
	document.getElementById('descriptions').value = shade;
	document.getElementById('uom').value = netrate;
	document.getElementById('stock').value = mrprate;
	//}
	}
	else
	{
	//if(document.getElementById('itemdataid_'+i).value=='')
	//{		
	document.getElementById('itemdataid_'+i).value = id;
	document.getElementById('itemcode_'+i).value = code;
	document.getElementById('descriptions_'+i).value = shade;
	document.getElementById('uom_'+i).value = netrate;
	document.getElementById('stock_'+i).value = mrprate;
	//}	
	}	
	document.getElementById("lightbox1").style.display='none';
	//var data= addRow('dataTable')
	
}

function showsitename158(str)
{

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)
    {
	var data=xmlhttp.responseText.split("__");	
	
	document.getElementById("hid-Godown-div").innerHTML=data[0];
   // document.getElementById("workorder").innerHTML=data[1];
	
    }
  }
  xmlhttp.open("GET","GoDownIssueSlip10.php?site="+str,true);
	xmlhttp.send();

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

	
}

function Get_oderdata(str)
{	
if (str=="")
  {
  document.getElementById("item").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("item").innerHTML=xmlhttp.responseText;
	
    }
  }
  var sitecode=document.getElementById("site").value;
         var godown=document.getElementById("store").value;
        
	
  
xmlhttp.open("GET","get_scrapitem.php?salid="+str+"&sitecode="+sitecode+"&godown="+godown,true);
xmlhttp.send();	
	
}


function Get_oderdata1(str)
{
		
	
if (str=="")
  {
  document.getElementById("dept").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("jobcarddate1").innerHTML=xmlhttp.responseText;
	
    }
  }
  
xmlhttp.open("GET","get_joborderdata.php?salid="+str,true);
xmlhttp.send();	
	
}

function GetItemQty(itemid)
{
	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)
	{
	
		//var data=xmlhttp.responseText.split("__");
           
		
		document.getElementById("showitemdataid").innerHTML=xmlhttp.responseText;
		
	}
	}
		//xmlhttp.open("GET","GoDownIssueSlip7.php?site="+str,true);
	var sitecode=document.getElementById("site").value;
         var godown=document.getElementById("store").value;
        
	xmlhttp.open("GET","GetItemQtyall.php?itemid="+itemid+"&sitecode="+sitecode+"&godown="+godown,true);
	
	xmlhttp.send();	
}









function calamt(str,str1)
{
var k=str.split("_");
if(k[1]==undefined)
{
if(parseFloat(document.getElementById("reciveqty").value)>parseFloat(document.getElementById("stock").value))
{
alert("Receive Qty Should Be less then Stock Qty");
document.getElementById("reciveqty").value='';
}

}
else
{
var i=k[1];
if(parseFloat(document.getElementById("reciveqty_"+i).value)>parseFloat(document.getElementById("stock_"+i).value))
{
alert("Receive Qty Should Be less then Stock Qty");
document.getElementById("reciveqty_"+i).value='';

}
}


}



</script>
<style type="text/css">
.body_lightbox1 {
width:100%;
background-color:#e7e7e7;
position:absolute;
left:280px;
top:95px;
padding-left:5px;
padding-right:5px;
border:#666666 solid 3px;
}
</style>
