<?php 
$title = _('Plant Process Entry');
if($_REQUEST['Save'])
{


	$prefix_sql=DB_query("Select * from prefix_tbl where prefix_name='".$title."'",$db);
	$prefix_row=DB_fetch_array($prefix_sql);
	$prefixf=$prefix_row['prefix_code'];
   $prefix1=$_REQUEST['site'];
	$financial=substr($_SESSION['YearStart'],-2)."-".substr($_SESSION['YearEnd1'],-2);
	  $sql="Select doc_no from plant_process where  site='".$_REQUEST['site']."'
	      	and INSTR(`doc_no`, '$financial') > 0 order by id desc limit 0,1";

	$result=DB_query($sql,$db);
	$myrow=DB_fetch_array($result);
	//echo DB_num_rows($result);
	if(DB_num_rows($result)>0)
	{
	//$Reqno=$myrow['Reqno'];
	//$req_part1=substr($Reqno,-4);
	$ExplBill=explode("/",$myrow['doc_no']);
	$maxval = $ExplBill[3];
	//echo $req_part1;
	$req_final=$maxval+1;
	//echo "<br>".$req_final;
	}
	else
	{
	$req_final="1";
	}
	
	$string=$prefix1."/".$prefixf."/".$financial."/";
	
	
	     $final_string=$string.str_pad($req_final,4,"0",STR_PAD_LEFT);
       
	
	
	
	$data['doc_no']=$final_string;
   $data['site']=$_REQUEST['site'];
	$data['date']= FormatDateForSQL($_REQUEST['date']);
	$data['shift']=$_REQUEST['shift'];
	$data['shift_incharge']=$_REQUEST['shift_incharge'];
	$data['prepared_by']=$_REQUEST['prepared_by'];
	$data['Created_by'] = $_SESSION['UserID'];
	$data['Created_date'] = date('Y-m-d');
	$data['CompanyId'] = $_SESSION['ACGCompanyId'];
	
	$s->RecordInsert('plant_process',$data);			//INSERT INTO PARANT TABLE
	$last_id=DB_Last_Insert_ID($db,'plant_process','id');// GET LAST INSERT ID
	
	foreach($_REQUEST['prdprocess'] as $key=>$value)
   {
    $data2['parentid']=$last_id;
    $data2['processid']=$value;
   $s->RecordInsert('plant_process_details1',$data2);			//INSERT INTO PARANT TABLE
	$last_id1=DB_Last_Insert_ID($db,'plant_process_details1','id');// GET LAST INSERT ID
   foreach($_REQUEST[$value] as $key1=>$value1)
   {
   
    $data3['parentid']=$last_id1;
    $data3['boxid']=$value1[box_id];
    $data3['box_value']=$value1[boxvalue];
    
    
    $s->RecordInsert('plant_process_details2',$data3);			//INSERT INTO PARANT TABLE
   
  
    
    
    
   }
 
   
   
    
   }
   
   
   
   
}







?>




<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Plant Process Entry</h1>	
<form action="" method="post">

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr class="row1">
    <td class="tabledate"><strong>Branch</strong></td>
    <td ><select name="site" id="site"  style="width:100%;">
	<option value="0">Select</option>
	<?php 
	$sql_tax_service1=DB_query("select * from locations where CompanyID='".$_SESSION['ACGCompanyId']."'",$db);
	while($row12=DB_fetch_array($sql_tax_service1))
	{
	$t1='';
	if($site==$row12['loccode'])
$t1="selected";	
	  ?>
	<option value="<?php echo $row12['loccode']; ?>" <?php echo $t1; ?>><?php echo $row12['locationname']; ?></option>
	<?php } ?>
	
	</select></td>
    <td class="tabledate"><strong>Doc No</strong></td>
    <td ><input type="text" name="doc_no" id="doc_no" style="width:100%" readonly="true" value="Auto Generated"></td>
    <td class="tabledate"><strong>Date</strong></td>
    <td ><input type="text" name="date" id="date" class="datepicker" style="width:100%" /></td>
  </tr>
  <tr class="row1">
    <td class="tabledate"><strong>Shift</strong></td>
    <td class="tabledate1"><select name="shift" id="shift" style="width:100%">
      <option value="0">Select</option>
      <?php 
      $shift=DB_query("select * from production_shift where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
      while($row_shift=DB_fetch_array($shift))
      {
      ?>
      <option value="<?php echo $row_shift['id']; ?>"><?php echo $row_shift['shift_name']; ?></option>
      <?php } ?>
     </select></td>
    <td class="tabledate1"><strong>Shift In charge</strong></td>
    <td class="tabledate1"><select name="shift_incharge" id="shift_incharge" style="width:100%">
      <option value="0">Select</option>
      <?php
      $prd_sql=DB_query("select * from production_operator_master 
           where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
      while($prd_row=DB_fetch_array($prd_sql))
      {
      ?>
      <option value="<?php echo $prd_row['id']; ?>"><?php echo $prd_row['operator_name']; ?></option>
      <?php } ?>
     </select></td>
    <td class="tabledate1"><strong>Prepared By</strong></td>
    <td class="tabledate1"><input type="text" name="prepared_by" id="prepared_by" style="width:100%"></td>
  </tr>
  
</table>
 <br>
 <div style="width:1042px; overflow:auto;">
<table width="100%" border="1" cellspacing="0" cellpadding="2">
  <tr class="row1">
    <td class="tabledate" style="text-align:center;"><div style="width:40px;"><strong>Sr.No</strong></div></td>
    <td class="tabledate" style="text-align:center;"  ><div style="width:260px;"><strong>Description</strong></div></td>
    <td class="tabledate" style="text-align:center;" ><strong>Result</strong></td>
  </tr>
  <?php 
  $i=1;
  $sql_parameter=DB_query("select * from Prd_process_master where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);        
  while($row_parameter=DB_fetch_array($sql_parameter))
  {
  ?>
  <tr class="row1" >
    <td><?php echo $i; ?></td>
    <td ><?php echo $row_parameter['description']; ?><input type="hidden" name="description[]" value="<?php echo $row_parameter['description']; ?>">
     <input type="hidden" name="prdprocess[]" value="<?php echo $row_parameter['id']; ?>">
    
    </td>
    <td>
	
     <?php
    
     $box_name='';
     $count='';
     $data1=''; 
     $detailid='';
     
     $sql_1=DB_query("SELECT prd_process_master_details2.id AS id1 ,prd_process_master_details2.box_name AS box_name,
prd_process_master_details2.valuereq  AS req ,prd_process_master_details.box_req AS box_req FROM prd_process_master_details2
 INNER JOIN prd_process_master_details ON prd_process_master_details.id=prd_process_master_details2.parentid
 WHERE processid ='".$row_parameter['id']."' 
      



"
             ,$db);
     //select * from prd_process_master_details where processid ='".$row_parameter['id']."'
      $numrow=DB_num_rows($sql_1);
     if($numrow>0)
     {
      while($count1=DB_fetch_array($sql_1))
              
      {
       $count=$count1['box_req'];
       $box_name[]=$count1['box_name'];
       $detailid[$count1['box_name']][]=$count1['id1'];
      }
     }
   //print_r($detailid);
     ?>
     
    <table width="100%" border="1" cellspacing="0" cellpadding="0">
      <tr >
     <?php
   for($t=0;$t<$count;$t++)
     {
   $bx=$box_name[$t];  
  
     ?>   
    <td class="tabledata1" style="text-align:center;" ><strong><?php echo $box_name[$t]; ?>
     <input type="hidden" name="box_name[]" value="<?php echo $bx;?>" >
     <input type="hidden" name="<?php echo $row_parameter['id']; ?>[][box_id]" value="<?php echo $detailid[$bx][0];?>" >
     </strong> 
     <?php 
    $sql_3=DB_query("select * from prd_process_details3 where parentid='".$detailid[$bx][0]."' 
     and box_value!=''",$db);
   
  
     $sql_2=DB_query("select * from prd_process_details3 where parentid='".$detailid[$bx][0]."'",$db);
      $numrow1=DB_num_rows($sql_2);
      $numrow2=DB_num_rows($sql_3);
     if($numrow1>0)
     {
      if($numrow2>0)
      {
        $data1="<select name='".$row_parameter['id']."[][boxvalue]' style='width:60px;' >";
       
      }
   
    
      ?>
     <hr>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <?php
      while($details=DB_fetch_array($sql_2))
       {
       if($details['box_value']!='')
       {
   
     $data1.="<option value=".$details['box_value'].">".$details['box_value']."</option>";
     $data1.="</option>";       
        }
        else
        {
         $data1="<input type='text' name='".$row_parameter['id']."[][boxvalue]' style='width:60px;'>";
        }
       
        
       }
      
      ?>
     <tr><td style="text-align:center;"><?php echo $data1; ?></td></tr>
    
    
    
    </table>
     
      <?php } ?>
    
    
    
    
    </td>
     <?php } ?>
    
    
    
    
    
    
    
    </tr>
    </table>
     
     <?php $i++;
     }  ?>
    
    
    </td>
  </tr>
  <?php   ?>
  <tr  class="row1">
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
   </div>
 <p align="center"><input type="submit" name="Save" value="Save" class="search"></p>
</form>
</div></div>