<?php
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$rurl="&modid=".$modid."&frmid=".$frmid;

function CheckFinalProcess($branch_code){

$checkFinal = DB_query("select id from production_process_master where isFinal='1' and branch_code='".$branch_code."' and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
$num=DB_num_rows($checkFinal);
if($num>0)
	return 0;
else
	return 1;

}

$error = array();

if(isset($_POST['Submit']))
{

$check=DB_query("select * from production_process_master where manuf_process_name='".$_REQUEST['shift_category']."'and branch_code='".$_REQUEST['site']."' and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
$num=DB_num_rows($check);
if($num<=0)
{	

if(isset($_REQUEST['isFinal']) and $_REQUEST['isFinal'] == 'on')
 {
  $isFinal = CheckFinalProcess($_REQUEST['site']);

if($isFinal == 0)
	$error[] = "Final process is already defined.";
else
 {
 $data['manuf_process_name']=$_REQUEST['shift_category'];
 $data['branch_code']=$_REQUEST['site'];
 $data['isFinal']='1';
 $data['CompanyId']=$_SESSION['ACGCompanyId'];
 
 $s->RecordInsert('production_process_master',$data);
 $gp_last_id=DB_Last_Insert_ID($db,'production_process_master','id');
 for($i=0;$i<count($_REQUEST['frmnamerole']);$i++)
 {
 if($_REQUEST['frmnamerole'][$i])
  {
  $data1['parentid']=$gp_last_id;
  $data1['mst_itemid']=$_REQUEST['frmnamerole'][$i];
  $s->RecordInsert('production_process_master_details',$data1);
  }
  
 }
 


}
}else
 {
 
 $data['manuf_process_name']=$_REQUEST['shift_category'];
 $data['branch_code']=$_REQUEST['site'];
 $data['isFinal']='0';
 $data['CompanyId']=$_SESSION['ACGCompanyId'];
  
 $s->RecordInsert('production_process_master',$data);
  $gp_last_id=DB_Last_Insert_ID($db,'production_process_master','id');
 for($i=0;$i<count($_REQUEST['frmnamerole']);$i++)
 {
  if($_REQUEST['frmnamerole'][$i])
  {
  $data1['parentid']=$gp_last_id;
  $data1['mst_itemid']=$_REQUEST['frmnamerole'][$i];
  $s->RecordInsert('production_process_master_details',$data1);
  }
  
 }

}

}
}
if(isset($_POST['Update']))
{ 
  
$check=DB_query("select * from production_process_master where manuf_process_name='".$_REQUEST['shift_category']."' and branch_code='".$_REQUEST['site']."' and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
$num=DB_num_rows($check);
if($num>=0)
{		
if(isset($_REQUEST['isFinal']) && $_REQUEST['isFinal'] == 'on')
 $st=1;
 else
 $st=0;
  $st;
 
 $data['manuf_process_name']=$_REQUEST['shift_category'];
 $data['branch_code']=$_REQUEST['site'];
 $data['isFinal']=$st; 
 $cond="id='".$_REQUEST['Id']."'";


 $s->RecordEdit('production_process_master',$data,$cond);
 
  $gp_last_id=$_REQUEST['Id'];
  $delete=DB_query("delete from production_process_master_details where parentid='".$_REQUEST['Id']."'",$db);
 for($i=0;$i<count($_REQUEST['frmnamerole']);$i++)
 {
  if($_REQUEST['frmnamerole'][$i])
  {
  $data1['parentid']=$gp_last_id;
  $data1['mst_itemid']=$_REQUEST['frmnamerole'][$i];
  $s->RecordInsert('production_process_master_details',$data1);
  }
  
 }
 
}
$s->pageLocation("index.php?route=production_process&daction=Updated".$rurl);	
}



if($_REQUEST['Delete'])
{
	for($i=0;$i<count($_REQUEST['leadid']);$i++){
	$del="delete from production_process_master where id='".$_REQUEST['leadid'][$i]."'";
	$query=DB_query($del,$db);
	}
$s->pageLocation("index.php?route=production_process&daction=Updated".$rurl);		
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<?php if($_REQUEST['action']==''){?>
<h1><?php echo $s->GetFieldValue("modules_form","id='".$_REQUEST['frmid']."' and moduleid='".$_REQUEST['modid']."'","frm_name"); ?></h1>
<form action="" method="post" name="frm">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr>
<td colspan="5"><?php
if(count($error)>0)
{
	print_r($error);
}
?></td>
</tr>
<tr class="row1">
   <td width="154" class="tabledata1">BRANCH</td>	
	  <td width="218"> <select name="site" style="width:150px;" id="site">
	  <?php 
            $site_sql=DB_query("Select loccode, locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db);
             while($site_row=DB_fetch_array($site_sql)){
             ?>
             <option value="<?php echo $site_row['loccode'];?>"><?php echo $site_row['locationname'];?></option>
             <?php } ?>		
		</option></select></td>
		 <td width="154" class="tabledata1">Manufacturing Process</td>	
	<td width="202"><input name="shift_category" id="shift_category" style="width:150px;" type="text"></td>
	
	<td width="176" class="tabledata1">Final Process &nbsp;<input name="isFinal" id="isFinal" type="checkbox"   /></td>
   <td></td>
</tr>
<tr><td colspan="6">
  
  <div style="width:100%; float:left; border: #000 1px solid;">
   	<div style="height:150px; overflow:auto;">
			
          
          <input type="checkbox" name="assigned" id="assigned_site" value="0" onclick="FProfileMaster(document.frm.frmnamerole)">All<br />
 
		 <table  align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
					<?php 
					$i=1;
               $query1=DB_query("select * from item_master where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
					 while($row=DB_fetch_array($query1))
                     {
				     ?>
					<tr><td><input type="checkbox" name="frmnamerole[]" id="frmname_<?php echo $i; ?>" value="<?php echo $row['id']; ?>" ><input type="hidden" name="itemid[]" id="form_name_<?php echo $i; ?>" value="<?php echo $row['item_name']; ?> " />
					
					</td>
					<td><?php echo $row['item_name']; ?> </td>
					</tr>
					<?php $i++; } ?>
					</table>
          
         </div>
			<div align="right"><input name="button" type="button" class="buttonsview" value=">>" onclick="swapdata()" /></div>
		</div>	   
   
  
  
  
  
  
  
 </td>
</tr>



<tr class="row1">
	<td colspan="6 ">
		<p align="center"><input type="submit" value="Save" name="Submit" onclick="return valid();" class="search"></p>
	</td>
</tr>  
   
</table>

</form><?php }if($_REQUEST['action']=='edit' and $_REQUEST['Id']!='' ){
$sql="select * from production_process_master where id='".$_REQUEST['Id']."'";
$query=DB_query($sql,$db);
$row1=DB_fetch_array($query);



?>
<h1>Production Process</h1>
<form action="" method="post" name="frm">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
    <td class="tabledata1">BRANCH</td>	
	  <td> <select name="site" style="width:150px;" id="site">
	  <?php 
            $site_sql=DB_query("Select loccode, locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db);
             while($site_row=DB_fetch_array($site_sql)){
             	$k='';
				 if($site_row['loccode']==$row1['branch_code'])
				 {
				 $k="selected";	
				 }
             ?>
             <option value="<?php echo $site_row['loccode'];?>"<?php echo $k; ?>><?php echo $site_row['locationname'];?></option>
             <?php } ?>		
		</option></select></td>
		 <td class="tabledata1">Manufacturing Process</td>
		<td><input name="shift_category" id="shift_category" style="width:150px;" type="text" value="<?php echo $row1['manuf_process_name']; ?>"></td>
<td width="176" class="tabledata1">Final Process &nbsp;<input name="isFinal" id="isFinal" type="checkbox"
<?php if($row1['isFinal']==1) { ?> checked="checked" <?php } ?>      /></td>
</tr>


<tr><td colspan="6" id="showitme" >
  
  <div style="width:100%; float:left; border: #000 1px solid; min-height: 40%">
   	<div style="height:150px; overflow:auto;">
			
          
          <input type="checkbox" name="assigned" id="assigned_site" value="0" onclick="FProfileMaster(document.frm.frmnamerole)">All<br />
 
		 <table  align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
					<?php 
					$i=1;
               $query1=DB_query("select * from item_master where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
					 while($row=DB_fetch_array($query1))
                     {
                $itemid=$s->GetFieldValue("production_process_master_details","parentid='".$_REQUEST['Id']."'
                  and mst_itemid='".$row['id']."'","mst_itemid");
                 if($itemid=='')
                  $checked="";
                 else
                  $checked="checked";
				     ?>
					<tr><td><input type="checkbox" name="frmnamerole[]" id="frmname_<?php echo $i; ?>" value="<?php echo $row['id']; ?>" <?php echo $checked; ?> ><input type="hidden" name="itemid[]" id="form_name_<?php echo $i; ?>" value="<?php echo $row['id']; ?> " />
					
					</td>
					<td><?php echo $row['item_name']; ?> </td>
					</tr>
					<?php $i++; } ?>
					</table>
          
         </div>
			<div align="right"><input name="button" type="button" class="buttonsview" value=">>" onclick="swapdata()" /></div>
		</div>	   
   
  
  
  
  
  
  
 </td>
</tr>

<tr class="row1">
	<td colspan="4">
		<p align="center"><input type="submit" value="Update" name="Update" onclick="return valid();" class="search"></p>
	</td>
</tr>  
</table>

</form><?php } ?>
<form name="myform" method="post">
<p align="left">
	<input type="submit" name="Delete" value="Delete" class="search" onclick="return confirm('Are you sure you want to delete?')" />
</p>

<table width="100%" border="1" cellpadding="3" cellspacing="0">
<tr>
<td class="tabledata"><input name="leadid" type="checkbox"   id="leadid" onclick="LeadData()"    /></td>
<td class="tabledata">Branch</td>
<td class="tabledata">Manufacturing Process  </td>
</tr>
<?php $i=0;
$sql="select * from production_process_master where CompanyId='".$_SESSION['ACGCompanyId']."'";
$query=DB_query($sql,$db);
while($row=DB_fetch_array($query)){?>
<tr class="row1">
<td style="white-space:nowrap;"><input name="leadid[]" type="checkbox" id="leadid_<?php echo $i+1; ?>"   value="<?php  echo $row['id']?>" />
	
	<a href="index.php?route=production_process&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=edit&Id=<?php echo $row['id'];?>"><img src="images/icon_edit.gif" width="12px"  height="12" /></a>
	
	
	</td>
<td><?php $db1="select * from locations where loccode='".$row['branch_code']."'";
$query1=Db_query($db1,$db);
$row11=Db_fetch_array($query1); 
echo $row11['locationname']; ?></td>
<td><?php echo $row['manuf_process_name']; ?></td>
</tr>
<?php $i++;} ?>
</table>
</form>
</div></div>


<?php 
//include('includes/footer.inc');
?>
<script language="JavaScript">
function valid()
{
if(document.getElementById("site").value==0)
{
alert("Must Select Site Name");
document.getElementById("site").focus();
return false;	
}
if(document.getElementById("shift_category").value=='')
{
alert("Must Enter Manufacturing Process");
document.getElementById("shift_category").focus();
return false;	
}
return true;	
}	
	
function LeadData()
{	
	myCheckboxArray = document.myform.elements["leadid[]"];
	var t=myCheckboxArray.length;
	//alert(myCheckboxArray);
	if(document.getElementById('leadid').checked==true)
	{	
	if(t==undefined)
	document.getElementById('leadid_'+1).checked=true;		
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('leadid_'+i).checked=true;	
	}	
	}
	else
	{
	if(t==undefined)		
	document.getElementById('leadid_'+1).checked=false;		
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('leadid_'+i).checked=false;	
	}
	}
}		
	
   
   function showentrymonth(str)
   {
    
    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("entrymonth").innerHTML=xmlhttp.responseText;
         
		}
	}
   var entrytype=document.getElementById("entrytype").value;
	xmlhttp.open("GET","ajax_process_master_month.php?id="+str+"&entrytype="+entrytype,true);
	xmlhttp.send();
        
    
    
   }
   
   function showentrymonth1(str)
   {
     document.getElementById("entrymonth").innerHTML='';
     document.getElementById("yeartype").value='0';
        
    
    
   }
   
   
   
 
   
   

function FProfileMaster(str)
{

   myCheckboxArray = document.frm.elements["frmnamerole[]"];
	var t=myCheckboxArray.length;
	
	if(document.getElementById('assigned_site').checked==true)
	{	
	
	if(t==undefined)	
	document.getElementById('frmname_'+1).checked=true;
	for (var i = 1; i <=t; i++)
	{
	document.getElementById('frmname_'+i).checked=true;	
	}	
	}
	else
	{
	if(t==undefined)	
	document.getElementById('frmname_'+1).checked=false;
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('frmname_'+i).checked=false;	
	}
	}
	

	
}
</script>