<?php 
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$rurl="&modid=".$modid."&frmid=".$frmid;
$sql="SELECT * FROM salesorder_details where masterId='".$_REQUEST['id']."'";
$sql2=DB_query($sql,$db);
$query=DB_fetch_array($sql2);
 $clientname=$s->getField('item_master','StockID',$query['ItemCode'],'item_name');  
?>
<form action="" method="post"> 
<table  border="0" align="center" cellpadding="0" cellspacing="0" width="100%">
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="left" valign="top" >MRP Generate</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="left" valign="top">
    <fieldset>
    <legend><strong>Item ---<?php echo ucfirst($clientname); ?></strong> </legend>
    <table border="0" align="center" cellpadding="0" cellspacing="0" width="100%">
        <tr>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td><table  border="0" cellpadding="0" cellspacing="0" width="100%">
              <tr>
                <th width="20" align="left">S.R.No</th>
                <th width="40" align="left">Part Code</th>
                <th width="80" align="left">Item Name</th>
                <th width="40" align="left">Total Qty Required</th>
                <th  width="40" align="left">Stock In Hand</th>
                <th  width="40" align="left">Indent Required</th>
               </tr>
               <?php
               $i=1;
               $sql1="select * from bill_material where part_code='".$query['ItemCode']."'" ;
			   $row=Db_query($sql1,$db);
			   $billdata=DB_fetch_array($row);
			   $itemdata=DB_query("select * from item_master where StockID='".$billdata['part_code']."'",$db);
			   while($rowdata=DB_fetch_array($itemdata))
			  {
			  $sqldata=DB_query("select * from bill_raw_material where bill_material_id='".$billdata['id']."'",$db);
              while($bill_raw=DB_fetch_array($sqldata))
			  {
			   $stockqty=$unit=$s->getField('stock_maintenance','itemcode',$bill_raw['part_code'],'stockbalance'); 
			   $qty_total=$bill_raw['bom_qty']*1000;               
              ?>  
              <tr>
                 <td align="left" valign="top"><?php echo $i; ?></td>
                 <td align="left" valign="top"><?php echo $bill_raw['part_code'] ?><input type="hidden" name="item_code[]" value="<?php echo $bill_raw['part_code']; ?> " /></td>
                 <td align="left" valign="top"><?php echo $itemname=$s->getField('item_master','StockID',$bill_raw['part_code'],'item_name');?>
                 <input type="hidden" name="item_desc[]" value="<?php echo $itemname; ?>" />	
                 	
                 </td>
                 <td align="left" valign="top"><?php  echo $qty_total;  ?></td>
                 <td align="left" valign="top"><input type="hidden" name="qty2[]" value="<?php echo $stockqty; ?>"/><?php echo $stockqty; ?></td>
                 <td align="left" valign="top"><input type="text" name="indent_req2[]" value="<?php echo $qty_total;?>" />
              
                 	
                 	
                 </td>                
              </tr>
              <?php $i++; } } ?>
                 <input type="hidden" name="branch"  value="<?php echo $billdata['brach_code']; ?>"/>
              </table></td>
          </tr>       
      <tr>
        <td>&nbsp;</td>
      </tr>
    </table>    
    </fieldset>
    </td>
  </tr>
  </table>
<p align="center"><input type="submit" value="Generate Indent" name="Generat"  /></p>
</form>	
<?php
if(isset($_POST['Generat']))
{
$PageName = 'Purchase Requisition';
$year=date('y');
$year_after=$year+1;
$prefix_sql=DB_query("Select * from prefix_tbl where prefix_name='".$PageName."'",$db);
$prefix_row=DB_fetch_array($prefix_sql);
$prefix=$prefix_row['prefix_code'];	
$sql="Select Reqno from materialrequisitions where sitecode='".$_REQUEST['branch']."' order by Reqno desc limit 1";
$result=DB_query($sql,$db);
$myrow=DB_fetch_array($result);
if(DB_num_rows($result)>0)
{
	$Reqno=$myrow['Reqno'];
	$req_part1=substr($Reqno,-4);
	//echo $req_part1;
	$req_final=$req_part1+1;
	//echo "<br>".$req_final;
}
else
{
	$req_final="1";
}

$string=$prefix."/".$_REQUEST['branch']."/".$year."-".$year_after."/";
$final_string=$string.str_pad($req_final,4,"0",STR_PAD_LEFT);	
$requisition_sql="Insert into materialrequisitions set
									requisitionno='',
									initiator='".$_SESSION['UserID']."',
									sitecode='".$_REQUEST['branch']."',
									Reqno='".$final_string."',prdate=now()";
	//echo "<pre>";
	//print_r($requisition_sql);
	$requisition_res=DB_query($requisition_sql,$db);
	$last_id=DB_Last_Insert_ID($db,'materialrequisitions','id');
	
	if($last_id!='')
	{
		$qty=$_REQUEST['item_code'];
		
		for($cr=0;$cr<count($qty);$cr++)
		{
		$make=$s->getField('item_master','StockID',$_REQUEST['item_code'][$cr],'make'); 	
			 $material_details_sql="insert into requisitionorderdetails set
											id='',
											requsitionno='".$last_id."',
											product_id='".$_REQUEST['item_code'][$cr]."',
											product_description='".$_REQUEST['item_desc'][$cr]."',
											make='".$s->getField('item_master','StockID',$_REQUEST['item_code'][$cr],'make')."',    
											color='".$s->getField('item_master','StockID',$_REQUEST['item_code'][$cr],'color')."',   
											grade='".$s->getField('item_master','StockID',$_REQUEST['item_code'][$cr],'grade')."',
											size='".$s->getField('item_master','StockID',$_REQUEST['item_code'][$cr],'size')."',
											gsm='".$s->getField('item_master','StockID',$_REQUEST['item_code'][$cr],'gsm')."',                  
											totalreceivetilldate = '".$_REQUEST['qty2'][$cr]."', 
											req_quantity='".$_REQUEST['indent_req2'][$cr]."'
											"; 
			//echo "<pre>";
			//print_r($material_details_sql);
			$details_res=DB_query($material_details_sql,$db);
		}
	}

$update=DB_query("update salesorder set indent_status='1'where id='".$_REQUEST['id']."'",$db);
$s->pageLocation('index.php?route=mrp_sop&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].$rurl);

}

?>



