<?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);

?>
<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" ><h2>MRP Generate</h2></td>
		</tr>
		<tr>
			<td>
				<table border="0" cellpadding="0" cellspacing="0" width="70%">
					<tr>
						<td><strong>SOP Date</strong></td>
						<td><?php echo $qrysop['orderDate'];?></td>
						<td><strong>SOP No.</strong></td>
						<td><?php echo $qrysop['orderNo'];?></td>
						<td><strong>Party</strong></td>
						<td><?php echo $clientname=$s->getField('clientmaster','id',$qrysop['client'],'c_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="0" cellpadding="0" cellspacing="0" width="100%">
								<tr>
									<th align="left">S. No.</th>
									<th align="left">Item Name</th>
									<th align="left">Order Qty</th>
									<th align="left">Part Code</th>
									<th align="left">Part Name</th>
									<th align="left">Qty Required</th>
									<th align="left">Stock In Hand</th>
									<th align="left" style="width:100px;">Indent Qty</th>
									<th align="left" style="width:100px;">Rate</th>
									<th align="left" style="width:80px;">Amount</th>
									<th align="left">Status</th>
								</tr>
								<?php
								$i=1;
								while($query=DB_fetch_array($sql2)) 
								{
								?>  
								<tr>
									<td align="left" valign="top"><?php echo $i; ?></td>
									<td align="left" valign="top">
										<?php echo $itmname=$s->getField('item_master','id',$query['ItemCode'],'item_name'); ?>
									</td>
									<td valign="top" style="text-align:center;">
										<?php echo $query['Quantity']; ?>
									</td>
									<td colspan="8">
										<table border="1" cellpadding="0" cellspacing="0" width="100%">
									<?php
										$itmcode=$s->getField('item_master','id',$query['ItemCode'],'StockID');
										$material_id=$s->getField('bill_material','part_code',$itmcode,'id');
								$sqldata=DB_query("select * from bill_raw_material where bill_material_id='".$material_id."'",$db);
										$j=0;
										while($bill_raw=DB_fetch_array($sqldata))
										{
										?>
											<tr>
												<td align="left" valign="top" style="width:80px;">
													<?php echo $bill_raw['part_code'];?>
<input type="hidden" name="part_code[<?php echo $i;?><?php echo $j;?>]" value="<?php echo $bill_raw['part_code'];?>" />
						<?php $itemid=$s->getField('item_master','StockID',$bill_raw['part_code'],'id');?>
						<input type="hidden" name="item_id[<?php echo $i;?><?php echo $j;?>]" value="<?php echo $itemid;?>" />
												</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_name[<?php echo $i;?><?php echo $j;?>]" value="<?php echo $itemname;?>" />
												</td>
												<td align="center" valign="top">
												<?php  $req_qty=$query['Quantity']*$bill_raw['bom_qty'];  ?>
												<input type="text" name="req_qty[<?php echo $i;?><?php echo $j;?>]" id="req_qty_<?php echo $i;?><?php echo $j;?>" value="<?php echo $req_qty;?>" readonly="readonly" /></td>
												<td align="left" valign="top">
													<?php $closing_stock=$s->getField('stock_summary','item',$bill_raw['part_code'],'closing_qty'); 
													if($closing_stock==''){$closing_stock=0;}
													?>
													<input type="text" name="closing_stock[<?php echo $i;?><?php echo $j;?>]" id="closing_stock_<?php echo $i;?><?php echo $j;?>" value="<?php echo $closing_stock;?>" readonly="readonly" />
												</td>
												<td align="left" valign="top"><input type="text" name="indent_req[<?php echo $i;?><?php echo $j;?>]" id="indent_req_<?php echo $i;?><?php echo $j;?>" value="<?php echo $qty_total=$req_qty;?>" <?php if($bill_raw['status']==1){echo "readonly";}?> />
												</td>
												<td align="left" valign="top">
												<?php $itemrate=$s->getField('item_master','StockID',$bill_raw['part_code'],'net_rate');?>
												<input type="text" name="rate[<?php echo $j;?>]" id="rate_<?php echo $i;?><?php echo $j;?>" value="<?php echo $itemrate;?>" readonly="readonly" /></td> 
												<td align="left" valign="top">
												<?php //$total_amt=$itemrate*$qty_total;?>
												<input type="text" name="total_amt[<?php echo $i;?><?php echo $j;?>]" id="total_amt_<?php echo $i;?><?php echo $j;?>" value=""  <?php if($bill_raw['status']==0){?>onfocus="calculate(<?php echo $i;?><?php echo $j;?>)"<?php } ?> readonly="readonly" /></td>  
												<td align="left" valign="top">
												<?php if($bill_raw['status']==0){echo "Pending";}else{echo "PR Generated";}?>
												<input type="hidden" name="$material[<?php echo $i;?><?php echo $j;?>]"  value="<?php echo $material_id; ?>"/>
												</td>  
											</tr>
										<?php $j++;} ?>
										</table>
									</td>               
								</tr>
								<?php $i++;  } ?>
								<tr>
								<td colspan="8">&nbsp;</td>
								<td>Grand Total</td>
								<td>
								<input type="text" name="grand_tot" id="grand_tot"  value="" readonly="readonly"/>
								<input type="hidden" name="branch"  value="<?php echo $qrysop['branch']; ?>"/>
								<input type="hidden" name="listid"  value="<?php echo $i; ?><?php echo $j; ?>"/>
								</td>
								<td>&nbsp;</td>
								</tr>
								
							</table>
						</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']))
{
	$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;}
		//	$prod = array();
		//	$prod = $_REQUEST['item_id'][$cr];
			
		//}
		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']."'
			"; 
			//echo "<pre>";
			//print_r($material_details_sql);
			$details_res=DB_query($material_details_sql,$db);
			$update=DB_query("update bill_raw_material set status='1' where part_code='".$_REQUEST['part_code'][$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=POModule/bom_sop&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].$rurl);
	
}

?>



