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

$sqlsop="SELECT * FROM salesorder where id='".$_REQUEST['id']."'";
$sqlsop=DB_query($sqlsop,$db);
$qrysop=DB_fetch_array($sqlsop);

$sql="SELECT * FROM salesorder_details where masterId='".$_REQUEST['id']."'";
$sql2=DB_query($sql,$db);



function materialshow($PcateID, $dashVal)
		{
			global $db;
			$spacebar = $dashVal ;
			    $s=new db_query();
   		 

		  	 $sqlCate = "select * from bill_material  where  id  = '".$PcateID."'";
		 
			$rsCate  = DB_query($sqlCate,$db);	
 			if(DB_num_rows($rsCate)>0)
			{
				$rowCate = DB_fetch_array($rsCate);
				//echo "select * from bill_raw_material where  bill_material_id='".$rowCate['id']."'";
				 $query_comprole=DB_query("select * from bill_raw_material where  bill_material_id='".$rowCate['id']."'",$db);
 			  while($row_comprole=DB_fetch_array($query_comprole))
			  {
			  $bom=$s->getField('bill_material','part_id',$row_comprole['mst_itemid'],'id');
			  $unitid=$s->getField('item_master','id',$row_comprole['mst_itemid'],'mst_unitid');	
			  
			  ?>
			  <tr>
<td class="jjrs_tablehead"><?php echo $i; ?></td>
<td class="jjrs_tablehead"><?php echo $spacebar; ?>

<?php if($bom!='') {  ?>
<a href="">
<img src="css/professional/images/menu_root.gif" border="0" align="absmiddle" style="cursor:pointer;">
<?php }  else {   echo "|__"; ?>
<img src="css/professional/images/customer.png" border="0" align="absmiddle" style="cursor:pointer;">
<?php
 } ?>
<b><font face="Arial, Helvetica, sans-serif">
<?php echo $itmname=$s->getField('item_master','id',$row_comprole['mst_itemid'],'item_name'); ?></font></b> 
<?php if($bom!='') { ?></a><?php } ?></td>
<td class="jjrs_tablehead">
<?php echo $qty=$s->getField('bill_material','id',$bom,'total_qty'); ?></td>
<td colspan="10"><?php echo $qty=$s->getField('unitsofmeasure','unitid',$unitid,'unitname'); ?></td>              
</tr>
			  
				
  			<?php
			$spacebar =$spacebar. "&nbsp;&nbsp;&nbsp;";
			if($bom!='')								
materialshow($bom, $spacebar);
else
materialshow1($row_comprole['mst_itemid'], $spacebar);	
							
								
					
				}
			}
		} 
		
	 
		
		$spacebar = "&nbsp;&nbsp;&nbsp;";	





function materialshow1($PcateID, $dashVal)
		{
			global $db;
			$spacebar = $dashVal;
			    $s=new db_query();
   		 

		  	  $sqlCate = "select * from bill_material  where  part_id  = '".$PcateID."'";
		 
			$rsCate  = DB_query($sqlCate,$db);	
 			if(DB_num_rows($rsCate)>0)
			{
				$rowCate = DB_fetch_array($rsCate);
				//echo "select * from bill_raw_material where  bill_material_id='".$rowCate['id']."'";
				 $query_comprole=DB_query("select * from bill_raw_material where  bill_material_id='".$rowCate['id']."'",$db);
 			  while($row_comprole=DB_fetch_array($query_comprole))
			  {
			  $bom=$s->getField('bill_material','part_id',$row_comprole['mst_itemid'],'id');
			  $unitid=$s->getField('item_master','id',$row_comprole['mst_itemid'],'mst_unitid');	
			 
			  ?>
			  <tr>
<td class="jjrs_tablehead"><?php echo $i; ?></td>
<td class="jjrs_tablehead"><?php echo  "|__"; $spacebar; ?>
<?php if($bom!='') { ?>
<a href="">
<img src="css/professional/images/menu_root.gif" border="0" align="absmiddle" style="cursor:pointer;">
<?php }  else { ?>
<img src="css/professional/images/customer.png" border="0" align="absmiddle" style="cursor:pointer;">
<?php
 } ?>
<b><font face="Arial, Helvetica, sans-serif">
<?php echo $itmname=$s->getField('item_master','id',$row_comprole['mst_itemid'],'item_name'); ?></font></b> <?php if($bom!='') { ?></a><?php } ?></td>
<td class="jjrs_tablehead">
<?php echo $qty=$s->getField('bill_material','id',$bom,'total_qty'); ?></td>
<td colspan="10"><?php echo $qty=$s->getField('unitsofmeasure','unitid',$unitid,'unitname'); ?></td>              
</tr>
			  
				
  			<?php
			$spacebar = "&nbsp;&nbsp;&nbsp;";
			if($bom!='')								
materialshow($bom, $spacebar);
else
materialshow1($row_comprole['mst_itemid'], $spacebar);	
							
								
								
					
				}
			}
		} 
		
	 
		
			

?>
<link rel="stylesheet" href="css/css/purchase_requision_print.css">
<link rel="stylesheet" href="css/new_mxerp.css">

<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" ><h3><strong>MRP Generate</strong></h3></td>
		</tr>
		<tr>
	<td>
		<table align="center" border="0" cellpadding="0" cellspacing="0" width="70%" style="border-collapse:collapse; border:#666666 solid 2px;">
					<tr>
						<td class="tabledata"><strong>SOP Date</strong></td>
						<td class="jjrs_tablehead" ><?php echo $qrysop['orderDate'];?></td>
						<td class="tabledata"><strong>SOP No.</strong></td>
						<td  class="jjrs_tablehead"><?php echo $qrysop['orderNo'];?></td>
						<td class="tabledata"><strong>Party</strong></td>
						<td class="jjrs_tablehead"><?php echo $clientname=$s->getField('clientmaster','id',$qrysop['client'],'c_name'); ?></td>
						<?php $query=DB_fetch_array($sql2); 
						
						?>
					   <td class="tabledata"><strong>Finished Material</strong></td>
						<td class="jjrs_tablehead" ><?php echo $itmname=$s->getField('item_master','id',$query['ItemCode'],'item_name'); ?></td>
											</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td align="left" valign="top">
				<table border="0" align="center" cellpadding="0" cellspacing="0" width="100%">
					<tr>
						<td>&nbsp;</td>
					</tr>
					<tr>
						<td>
	<table  border="1" cellpadding="0" cellspacing="0" width="100%" style="border-collapse:collapse; border:#666666 solid 2px;">
								<tr>
									<td width="2%" class="tabledata" >S. No.</td>
									<td width="55%" class="tabledata">Item Name</td>
									<td width="4%" class="tabledata">Order Qty</td>
								  <td colspan="10" class="tabledata">UOM</td>
								  <td width="39%"  colspan="10">
									<table width="100%" ><tr>									
									<td  width="50" class="tabledata">Part Code</td>
									<td  width="102" class="tabledata">Part Name</td>
									<td width="82" class="tabledata">Qty Required</td>
									<td  width="82"class="tabledata">Stock In Hand</td>
									<td width="82" class="tabledata" >Indent Qty</td>
									<td  width="82" class="tabledata">Rate</td>
									<td width="82" class="tabledata">Amount</td>
									<td width="82" class="tabledata">Status</td>
									</tr>
									</table>	
								</tr>
								<?php
								 $billid=$s->getField('bill_material','part_id',$query['ItemCode'],'id');
								$i=1;
			$spacebar = "&nbsp&nbsp;&nbsp;&nbsp;";
			
							    $sql4="SELECT * FROM bill_raw_material where bill_material_id='".$billid."'";
								$sql5=DB_query($sql4,$db);
								while($query1=DB_fetch_array($sql5)) 
								{	
								$bom=$s->getField('bill_material','part_id',$query1['mst_itemid'],'id');
								$unitid=$s->getField('item_master','id',$query1['mst_itemid'],'mst_unitid');							
								 ?>  
<tr bgcolor="#CC3333">
<td class="jjrs_tablehead" ><?php echo $i; ?></td>
<td class="jjrs_tablehead"><b>&nbsp;&nbsp;&nbsp;<img src="css/professional/images/allocation.gif" border="0" align="absmiddle" style="cursor:pointer;">&nbsp;
<?php echo $itmname=$s->getField('item_master','id',$query1['mst_itemid'],'item_name'); ?></b><?php echo $spacebar; ?></td>
<td class="jjrs_tablehead">
<?php echo $qty=$s->getField('bill_material','id',$bom,'total_qty'); ?></td>
<td colspan="10"><span class="jjrs_tablehead"><?php echo $qty=$s->getField('unitsofmeasure','unitid',$unitid,'unitname'); ?></span></td>  
             
</tr>

<?php
if($bom!='')								
{
materialshow($bom, $spacebar);
}
else
materialshow1($query1['mst_itemid'], $spacebar);

 $i++; }   ?>
</div>
<tr>
									<td  ></td>
									<td ></td>
									<td ></td>
				<td  colspan="10">&nbsp;</td>
</tr>       
<tr>
<td>&nbsp;</td>
</tr>
</table>    
</td>
</tr>
</table>
	<p align="center"><input type="submit" value="Generate Indent" name="Generat"  /></p>
</form>	
<script type="text/javascript">
function calculate(str1){
var str = str1;
var total;
var indent = document.getElementById("indent_req_"+str).value;
var rate = document.getElementById("rate_"+str).value;

total=parseFloat(indent)*parseFloat(rate);
document.getElementById("total_amt_"+str).value=total;
var grand = document.getElementById("grand_tot").value;
if(grand==''){grand=0;}
grand=parseFloat(grand)+total;
document.getElementById("grand_tot").value=grand;
}
</script>
<?php
if(isset($_REQUEST['Generat']))
{
$s="9:00 AM";
$val2=strpos($s,'AM');
echo $val3= substr($s,-2);
$valu=explode(" ",$s);
//print_r($valu);

exit;

	$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);
		$req_final=$req_part1+1;
	}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()";
	
	$requisition_res=DB_query($requisition_sql,$db);
	$last_id=DB_Last_Insert_ID($db,'materialrequisitions','id');
	
	if($last_id!='')
	{
		$qty=$_REQUEST['listid'];
		
		for($cr=10;$cr<$qty;$cr++)
		{
			if($_REQUEST['part_code'][$cr]==''){continue;}
			if($_REQUEST['total_amt'][$cr]==''){continue;}
			$material_details_sql="insert into requisitionorderdetails set
			id='',
			requsitionno='".$last_id."',
			material_required_by='".date('Y-m-d')."',
			product_id='".$_REQUEST['item_id'][$cr]."',
			product_description='".$_REQUEST['item_name'][$cr]."',
			make='".$s->getField('item_master','StockID',$_REQUEST['part_code'][$cr],'make')."',    
			color='".$s->getField('item_master','StockID',$_REQUEST['part_code'][$cr],'color')."',   
			grade='".$s->getField('item_master','StockID',$_REQUEST['part_code'][$cr],'grade')."',
			size='".$s->getField('item_master','StockID',$_REQUEST['part_code'][$cr],'size')."',
			gsm='".$s->getField('item_master','StockID',$_REQUEST['part_code'][$cr],'gsm')."',                  
			req_quantity='".$_REQUEST['indent_req'][$cr]."',
			required_qty='".$_REQUEST['indent_req'][$cr]."',
			requisition_qty='".$_REQUEST['indent_req'][$cr]."',
			salesorder='".$qrysop['id']."'"; 
			$details_res=DB_query($material_details_sql,$db);
			$update=DB_query("update bill_raw_material set status='1' where mst_itemid='".$_REQUEST['item_id'][$cr]."' and bill_material_id='".$_REQUEST['material'][$cr]."'",$db);
		}
	}
	$update=DB_query("update salesorder set indent_status='0' where id='".$_REQUEST['id']."'",$db);
	$s->pageLocation('index.php?route=Production/bom_sop&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].$rurl);
	
}

?>



