<link rel="stylesheet" href="css5/jquery.treeview.css" />
<link rel="stylesheet" href="css5/screen.css" />
<script src="js5/jquery.js" type="text/javascript"></script>
<script src="js5/jquery.cookie.js" type="text/javascript"></script>
<script src="js5/jquery.treeview.js" type="text/javascript"></script>
<script type="text/javascript" src="js5/demo.js"></script>
	
<?php 
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
include("includes/stockcalculation.php");
$rurl="&modid=".$modid."&frmid=".$frmid;

unset($_SESSION['stockval']);
$sqlsop="SELECT * FROM manufacturing_advice where id='".$_REQUEST['id']."'";
$sqlsop=DB_query($sqlsop,$db);
$qrysop=DB_fetch_array($sqlsop);
$function_filter[]=$qrysop['branchid'];
$function_filter[]=$qrysop['store'];
$data_date=date('Y-m-d');
					    
$function_filter[]=$data_date;


$sql="SELECT * FROM manufacturing_advice_items where advice_id='".$_REQUEST['id']."'";
$sql2=DB_query($sql,$db);
$query=DB_fetch_array($sql2); 

$spacebar = "&nbsp;&nbsp;&nbsp;";
$mm=1;
//global $stock_val;
$stock_val=array();

if(isset($_REQUEST['Generat']))
{
    //echo "<pre>";print_r($_REQUEST);die;
	$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);	
	//echo "<pre>";print_r($_SESSION);
	$empid=$s->getField('empmaster','mst_www_userid',$_SESSION['UserID'],'id');
	$requisition_sql="Insert into materialrequisitions set	requisitionno='', initiator='".$_SESSION['UserID']."', sitecode='".$_REQUEST['branch']."',saleorderid='".$qrysop['id']."',category='PPC',requition_by='".$empid."',CompanyId='".$_SESSION['ACGCompanyId']."',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['chk'];
		//echo count($qty);
		//print_r($_REQUEST['item_id']);
        $cntarray=array();
        $itemarray=array();
        $itemnamearray=array();
        $reqarray=array();
        $requiredarray=array();
        $requisitionarray=array();
        $material=array();
		$advice_details=array();
		$bill_material=array();
        for($cr=0;$cr<count($qty);$cr++)
		{
            $cr1=$_REQUEST['chk'][$cr];
			if(!in_array($_REQUEST['item_id'][$cr1],$itemarray))
            {
                $cntarray[$cr]=$_REQUEST['chk'][$cr];
                $itemarray[$cr]=$_REQUEST['item_id'][$cr1];
                $itemnamearray[$cr]=$_REQUEST['item_name'][$cr1];
                $reqarray[$cr]=$_REQUEST['req_qty'][$cr1];
                $requiredarray[$cr]=$_REQUEST['indent_req'][$cr1];
                $requisitionarray[$cr]=$_REQUEST['totalqty'][$cr1];
                $material[$cr]=$_REQUEST['material'][$cr1];
				$advice_details[$cr]=$_REQUEST['advice_details_id'][$cr1];
				$bill_material[$cr]=$_REQUEST['bill_material_id'][$cr1];
            }else{
                $cc=array_search($_REQUEST['item_id'][$cr1], $itemarray);
                $reqarray[$cc]=$_REQUEST['req_qty'][$cr1]+$reqarray[$cc];
                $requiredarray[$cc]=$_REQUEST['indent_req'][$cr1]+$requiredarray[$cc];
                $requisitionarray[$cc]=$_REQUEST['totalqty'][$cr1]+$requisitionarray[$cc];
                $material[$cc]=$_REQUEST['material'][$cr1].",".$material[$cc];
				$advice_details[$cc]=$_REQUEST['advice_details_id'][$cr1].",".$advice_details[$cc];
				$bill_material[$cc]=$_REQUEST['bill_material_id'][$cr1].",".$bill_material[$cc];
            }    
        }
		for($cr2=0;$cr2<count($cntarray);$cr2++)
		{
			if($requisitionarray[$cr2]>0)
            {
		    $material_details_sql="insert into requisitionorderdetails set
			id='',
			requsitionno='".$last_id."',
			material_required_by='".date('Y-m-d')."',
			mst_itemid='".$itemarray[$cr2]."',
			product_id='".$itemarray[$cr2]."',
			product_description='".$itemnamearray[$cr2]."',			             
			req_quantity='".$reqarray[$cr2]."',
			required_qty='".$requiredarray[$cr2]."',
			requisition_qty='".$requisitionarray[$cr2]."',
			indent_item_status=1,
			salesorder='".$qrysop['id']."'"; 
			$details_res=DB_query($material_details_sql,$db);						
			$indent_details_sql="insert into manufacturing_advice_indent set
			id='',
			requsitionno='".$last_id."',
			material_required_by='".date('Y-m-d')."',
			mst_itemid='".$itemarray[$cr2]."',
			product_description='".$itemnamearray[$cr2]."',			             
			req_quantity='".$reqarray[$cr2]."',
			required_qty='".$requiredarray[$cr2]."',
			requisition_qty='".$requisitionarray[$cr2]."',
			indent_item_status=1,
			salesorder='".$qrysop['id']."',
			advice_details_id='".$advice_details[$cr2]."',
			bill_material_id='".$bill_material[$cr2]."'"; 
			$ind_details_res=DB_query($indent_details_sql,$db);
            }else if($requisitionarray[$cr2]==0){
			$indent_details_sql="insert into manufacturing_advice_indent set
			id='',
			requsitionno='".$last_id."',
			material_required_by='".date('Y-m-d')."',
			mst_itemid='".$itemarray[$cr2]."',
			product_description='".$itemnamearray[$cr2]."',			             
			req_quantity='".$reqarray[$cr2]."',
			required_qty='".$requiredarray[$cr2]."',
			requisition_qty='".$requisitionarray[$cr2]."',
			indent_item_status=2,
			salesorder='".$qrysop['id']."',
			advice_details_id='".$advice_details[$cr2]."',
			bill_material_id='".$bill_material[$cr2]."'"; 
			$ind_details_res=DB_query($indent_details_sql,$db);
	    }
		}
	}
	
	$update=DB_query("update manufacturing_advice set indent_status='0' and bom_status='1' where id='".$_REQUEST['id']."'",$db);
	$s->pageLocation('index.php?route=PMS/Transcations/mrp_boq&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].$rurl);
}
?>
<link rel="stylesheet" href="css/css/purchase_requision_print.css">
<link rel="stylesheet" href="css/new_mxerp.css">
<form action="" method="post"> 
  
    
    <?php 
    if(isset($_REQUEST['mainid'])){
    $sql="SELECT * FROM manufacturing_advice_items where advice_id='".$_REQUEST['id']."' AND id='".$_REQUEST['mainid']."'";
    }else{
    $sql="SELECT * FROM manufacturing_advice_items where advice_id='".$_REQUEST['id']."'";   
    }
    $sql2=DB_query($sql,$db);
    while($query=DB_fetch_array($sql2)){ 
    $spacebar1="<img src='images/pms-arrow.jpg'/>";    
    ?>
 
	<table align="center" border="0" cellpadding="3" cellspacing="0" width="100%" style="border-collapse:collapse; border:#666666 solid 2px;">
		<tr class="row1">
          <td colspan="10"><h3><strong>MRP Generate</strong></h3></td>
        </tr>
        <tr class="row1">
            <td><strong>Advice Date</strong></td>
			<td width="13%"><?php echo $qrysop['advice_date'];?></td>
			<td><strong>Advice No.</strong></td>
			<td><?php echo $qrysop['advice_no'];?></td>
			<td><strong>Project</strong></td>
			<td><?php echo $qrysop['project_name']; ?></td>
			<td><strong>Finished Material</strong></td>
			<td><?php echo $itmname=$s->getField('item_master','id',$query['item_code'],'item_name'); ?></td>
			<td><strong>Order Qty</strong></td>
			<td>
                <?php echo $totalqty1= $query['Qty'];?>
                <input type="hidden" name="branch"  value="<?php echo $qrysop['branchid']; ?>"/>
				<?php $sopstatus=$query['bom_status']; ?>			</td>		
		</tr>
  </table>
    </center>
    <div style="clear:both;"></div>
	<div style="width:98%; height:auto; padding-top:10px; float:right;">
	<div style="width:250px; float:left; margin-top:15px;">
        <ul id="browser" class="filetree" style="margin:0px; padding:0px;">
        <?php
        $billid=$s->getField('bill_material','part_id',$query['item_code'],'id');
		$billqty=$s->getField('bill_material','part_id',$query['item_code'],'total_qty');
		$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');?>  
            
            <?php if($bom!='')
            {?>
                <li style="text-align:left;">
                <span class="folder">
                    <a href="index.php?route=PMS/Transcations/mrp_boq_generatedata&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>&id=<?php echo $_REQUEST['id']; ?>&subid=<?php echo $query1['mst_itemid']; ?>&mainid=<?php echo $query['id']; ?>"> 
                    <?php if($_REQUEST['subid']==$query1['mst_itemid']) {?>
                        <font color="#CC33CC"><?php echo  $itmname=$s->getField('item_master','id',$query1['mst_itemid'],'item_name'); ?></font>
                    <?php } else { ?>
                        <?php echo  $itmname=$s->getField('item_master','id',$query1['mst_itemid'],'item_name'); ?>
                    <?php } ?>
                    </a>
                </span>
            <?php } ?>
                        
            <?php 
            if($bom!='')								
            {?>
                <ul>
            <?php   
                materialshow2($bom,$spacebar,$req_qty,$j,$i,$query['id']);
            }else{
                ?>
                <li style="text-align:left;"><span class="folder"><?php echo $itmname=$s->getField('item_master','id',$query1['mst_itemid'],'item_name'); ?></span></li>
            
                <?php 
                //materialshow3($query1['mst_itemid'], $spacebar,$req_qty,$j,$i);
            }?>
        <?php }?>
        </ul>
        </li>
        </ul>
    </div>
	<div style="width:900px; float:right;">
        <table  border="1" cellpadding="3" cellspacing="0" width="100%" style="border-collapse:collapse;" >
            <tr bgcolor="#CCCCCC" height="25">
                <td><strong>Action</strong></td>								
				<td colspan="2"><strong>Part Code</strong></td>
				<td width="100px;"><strong>Part Name</strong></td>
				<td><strong>Unit</strong></td>
				<td><strong>Qty</strong></td>
				<td><strong>Stock</strong></td>
				<td><strong>Indent Qty</strong></td>
				<td><strong>Extra Qty</strong></td>
				<td><strong>Total Qty</strong></td>
                <td><strong>Indent Date</strong></td>
				<td><strong>Status</strong></td>
			</tr>
			<?php
            $billid=$s->getField('bill_material','part_id',$query['item_code'],'id');
            $billqty=$s->getField('bill_material','part_id',$query['item_code'],'total_qty');
            $i=1;
            $spacebar = "&nbsp&nbsp;&nbsp;&nbsp;";
            if($_REQUEST['subid']!='')
            {
                $billid=$s->getField('bill_material','part_id',$_REQUEST['subid'],'id');
            }
            $sql4="SELECT * FROM bill_raw_material where bill_material_id='".$billid."' ";
            $sql5=DB_query($sql4,$db);
            while($query1=DB_fetch_array($sql5)) 
            {
                $status=$s->GetFieldValue('manufacturing_advice_indent',"salesorder='".$_REQUEST['id']."' and mst_itemid='".$query1['mst_itemid']."' and FIND_IN_SET(".$query['id'].",advice_details_id) and FIND_IN_SET(".$query1['id'].",bill_material_id)",'indent_item_status');
		
                $bom=$s->getField('bill_material','part_id',$query1['mst_itemid'],'id');
                $unitid=$s->getField('item_master','id',$query1['mst_itemid'],'mst_unitid');
                $unitid1=$s->getField('item_master','id',$query1['mst_itemid'],'conv_unit');
                $confac=$s->getField('item_master','id',$query1['mst_itemid'],'conv_factor');	
                if($confac=='')
                    $confac=0;						

                $material_id=$query1['id'];
                $sqldata=DB_query("select * from bill_raw_material where id='".$query1['id']."'",$db);
                $j=0;
                $bill_raw=DB_fetch_array($sqldata);?>
                <tr>
                    <td>
                    <?php 
                    if($status==1) { echo "Indent Generated"; } else if($status==2) { echo "Closed W/O PR"; } else {
                    if($bom=='')								
                    {    ?>
                    <input type="checkbox" name="chk[]" value="<?php echo  $mm.$billid.$bill_raw['id']; ?>" id="chk_<?php echo $j; ?>" />
                    <?php }} ?>
                    </td>
                    <td class="jjrs_tablehead" colspan="2"><?php if($bom!=''){echo "<b>";}  echo "<img src='images/pms-folder.jpg'/>".$spacebar1."&nbsp;".$bill_raw['part_code'];if($bom!=''){echo "</b>";}?>
                    <input type="hidden" name="part_code[<?php echo  $mm.$billid.$bill_raw['id']; ?>]" value="<?php echo $bill_raw['part_code'];?>" />
                    <?php $itemid=$s->getField('item_master','id',$bill_raw['mst_itemid'],'id');?>
                    <input type="hidden" name="item_id[<?php echo  $mm.$billid.$bill_raw['id']; ?>]" value="<?php echo $bill_raw['mst_itemid'];?>" />
					<input type="hidden" name="advice_details_id[<?php echo  $mm.$billid.$bill_raw['id']; ?>]" value="<?php echo $query['id'];?>" />
					<input type="hidden" name="bill_material_id[<?php echo  $mm.$billid.$bill_raw['id']; ?>]" value="<?php echo $query1['id'];?>" />
                    </td>
                    <td class="jjrs_tablehead">
                    <?php if($bom!=''){echo "<b>";}echo $itemname=$s->getField('item_master','id',$bill_raw['mst_itemid'],'item_name');if($bom!=''){echo "</b>";}?>
                    <input type="hidden" name="item_name[<?php echo  $mm.$billid.$bill_raw['id']; ?>]" value="<?php echo $itemname;?>" />
                    </td>
                    <td><?php if($bom!=''){echo "<b>";}echo $qty=$s->getField('unitsofmeasure','unitid',$unitid,'unitname');if($bom!=''){echo "</b>";} ?></td>
                    <td class="jjrs_tablehead">
                    <?php
                    // echo  $query['Quantity']."qty".$bill_raw['bom_qty']."bomqty";
                    if($bom!=''){echo "<b>";}echo $req_qty=$totalqty1*$bill_raw['bom_qty'];if($bom!=''){echo "</b>";}
                    ?>
                    <input type="hidden" name="req_qty[<?php echo  $mm.$billid.$bill_raw['id']; ?>]" id="req_qty_<?php echo $i;?><?php echo $j;?>" value="<?php echo $req_qty;?>" readonly="readonly" size="10" />
                    </td>
                    <td class="jjrs_tablehead">
                    <?php 
                    $op=openingBalance($function_filter,$bill_raw['mst_itemid']);
                    $in_1=receivedMRN($function_filter,$bill_raw['mst_itemid']);
                    $out_1=issueSlip($function_filter,$bill_raw['mst_itemid']);
                    $out_2=issueSiteToSite($function_filter,$bill_raw['mst_itemid']);		
                    $in_2=receivedSiteToSite($function_filter,$bill_raw['mst_itemid']);
                    $out_3=issueMatarialReturn($function_filter,$bill_raw['mst_itemid']);
                    $out_4=Rgpchallan($function_filter,$bill_raw['mst_itemid']);
                    $out_5=Nrgp($function_filter,$bill_raw['mst_itemid']);
                    $in_3=receivedReturn($function_filter,$bill_raw['mst_itemid']);		 
                    $in_4=Rgpreturn($function_filter,$bill_raw['mst_itemid']);
                    $in_5=FinishedMR($function_filter,$bill_raw['mst_itemid']);
                    $invalue=($op+$in_1[0]+$in_2+$in_3+$in_4+$in_5);
                    $outvalue=($out_1+$out_2+$out_3+$out_4+$out_5);
                    $tot=trim($invalue)-trim($outvalue);
                    $closing_stock=$tot;
                    if($closing_stock!='0' and $confac!='0')
                    {
                        $closing_stock2=round($closing_stock/$confac,2);
                    }else
                    $closing_stock2=0;

                    //echo $closing_stock;?>
                        
                    <?php 
                    if($bom==''){
                    if(!empty($stock_val))
                    {    
                        if(array_key_exists($bill_raw['mst_itemid'],$stock_val))
                        {
                            if($stock_val[$bill_raw['mst_itemid']]==0)
                            { 
                                echo $closing_stock=0;
                                $stock_val[$bill_raw['mst_itemid']]=0;
                            }else{
                                if($req_qty>$stock_val[$bill_raw['mst_itemid']])
                                { 
                                    echo $closing_stock=$stock_val[$bill_raw['mst_itemid']];
                                    $stock_val[$bill_raw['mst_itemid']]=0;
                                }else{
                                    echo $closing_stock=$stock_val[$bill_raw['mst_itemid']];
                                    $sval=$stock_val[$bill_raw['mst_itemid']]-$req_qty;
                                    $stock_val[$bill_raw['mst_itemid']]=$sval;
                                }
                            }
                        }else{
                            if($req_qty>$closing_stock)
                            { 
                                echo $closing_stock;
                                $stock_val[$bill_raw['mst_itemid']]=0;
                            }else{
                                echo $closing_stock;
                                $sval=$closing_stock-$req_qty;
                                $stock_val[$bill_raw['mst_itemid']]=$sval;
                            }
                        }
                    }else{
                        if($req_qty>$closing_stock)
                        { 
                            echo $closing_stock;
                            $stock_val[$bill_raw['mst_itemid']]=0;
                        }else{
                            echo $closing_stock;
                            $sval=$closing_stock-$req_qty;
                            $stock_val[$bill_raw['mst_itemid']]=$sval;
                        }
                    }  
                    }   
                        
                    ?>    
                    </td>
                    <?php
                    if($bom=='')								
                    { ?>
                    <td class="jjrs_tablehead">
                    <?php
                    if($closing_stock>=$req_qty)
                    $req_qty3=0;
                    else if($closing_stock<$req_qty)
                    $req_qty3=($req_qty-$closing_stock);

                    echo $req_qty3; ?>
                    <input type="hidden" name="indent_req[<?php echo  $mm.$billid.$bill_raw['id']; ?>]" id="indent_req_<?php echo  $mm.$billid.$bill_raw['id']; ?>" value="<?php echo $req_qty3;?>" <?php if($bill_raw['status']==1){echo "readonly";}?> size="10" />
                    </td>
                    <td class="jjrs_tablehead">
                    <input type="text" name="extra[<?php echo $mm.$billid.$bill_raw['id']; ?>]" id="extra_<?php echo $mm.$billid.$bill_raw['id']; ?>" onkeyup="calculatetotal(this.value,<?php echo  $billid.$bill_raw['id']; ?>)" value="" size="10" />
                    </td> 
                    <td class="jjrs_tablehead">
                    <input type="text" name="totalqty[<?php echo  $mm.$billid.$bill_raw['id']; ?>]" id="totalqty_<?php echo  $mm.$billid.$bill_raw['id']; ?>" value="<?php echo $req_qty3;?>" size="10" readonly='true' />
                    </td> 
                    <td class="jjrs_tablehead">
                    <input type="text" name="indentdate[<?php echo  $mm.$billid.$bill_raw['id']; ?>]" id="indentdate_<?php echo  $mm.$billid.$bill_raw['id']; ?>" value="<?php echo date("d/m/Y");?>" readonly="true" class="datepicker" />
                    </td>  
                    <td class="jjrs_tablehead">
                    <?php if($status==1) { echo "PR Generated"; } else if($status==2) { echo "Closed W/O PR"; } else {echo "Pending";}?>
                    <input type="hidden" name="material[<?php echo  $mm.$billid.$bill_raw['id']; ?>]"  value="<?php echo $material_id; ?>" size="10"/>
                    </td>  
                    <?php }else{ ?>
                    <td class="jjrs_tablehead" colspan="5"></td>
                    <?php } ?>
                </tr>
                <?php $j++;  ?>
                <?php
                if($bom!='')								
                {
                    materialshow($bom,$spacebar1,$req_qty,$j,$_REQUEST['id'],$function_filter,$mm,$stock_val,$query['id']);
                }
                else
                    materialshow1($query1['mst_itemid'], $spacebar1,$req_qty,$j,$_REQUEST['id'],$function_filter,$mm,$stock_val,$query['id']);

            $i++; }   ?>
        </table>
    </div>
    </div> 
    <div style="clear:both;"></div>
    <?php $mm++;} ?>    
	<div style="width:750px; float:right;"><p align="center"><input type="submit" value="Generate Indent" name="Generat"  /></p></div>
    <div style="clear:both;"></div>

    <div style="clear:both;"></div>
    
</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;
}

function calculatetotal(str,sval)
{
    if(document.getElementById("indent_req_"+sval)){
        document.getElementById("totalqty_"+sval).value=parseFloat(str)+parseFloat(document.getElementById("indent_req_"+sval).value);
    }else{
        document.getElementById("totalqty_1"+sval).value=parseFloat(str)+parseFloat(document.getElementById("indent_req_1"+sval).value);    
    }
}
</script>

<?php
function materialshow($PcateID, $dashVal1,$totalqty1,$j,$sopstatus,$function_filter,$mm,$stock_val,$adv_id)
{	
    global $db;
	$spacebar2="<img src='images/pms-arrow.jpg'/>";  
	$spacebar1 = $spacebar2.$dashVal1;
	$totalqty1=$totalqty1;
	$s=new db_query(); 		
	$sopstatus=$sopstatus;
    //if(!isset($_REQUEST['mainid'])){
    if(isset($_SESSION['stockval'])){$stock_val=$_SESSION['stockval'];unset($_SESSION['stockval']);}
    //}
    $sqlCate = "select * from bill_material  where  id  = '".$PcateID."'";
	$rsCate  = DB_query($sqlCate,$db);	
 	if(DB_num_rows($rsCate)>0)
	{
		$rowCate = DB_fetch_array($rsCate);
        $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');
			$unitid1=$s->getField('item_master','id',$row_comprole['mst_itemid'],'conv_unit');
			$confac=$s->getField('item_master','id',$row_comprole['mst_itemid'],'conv_factor');
			if($confac=='')
			$confac=0;		  	
			
            $j=$j++;
            $itmcode=$s->getField('item_master','id',$row_comprole['mst_itemid'],'StockID');
            //echo "select * from bill_raw_material where bill_material_id='".$row_comprole['id']."'";
            $material_id=$s->getField('bill_material','part_id',$row_comprole['mst_itemid'],'id');
            $sqldata=DB_query("select * from bill_raw_material where id='".$row_comprole['id']."'",$db);
            while($bill_raw=DB_fetch_array($sqldata))
            {
                $status=$s->GetFieldValue('requisitionorderdetails',"salesorder='".$sopstatus."' and mst_itemid='".$bill_raw['mst_itemid']."'",'indent_item_status');
		if($status==''){if($query1['status']==2){$status=$query1['status'];}}	
				$status=$s->GetFieldValue('manufacturing_advice_indent',"salesorder='".$sopstatus."' and mst_itemid='".$bill_raw['mst_itemid']."' and FIND_IN_SET(".$adv_id.",advice_details_id) and FIND_IN_SET(".$row_comprole['id'].",bill_material_id)",'indent_item_status');	
            ?>
            <tr>
                <td>
                <?php if($status==1) { echo "Indent Generated"; } else if($status==2) { echo "Closed W/O PR"; } else {
                if($bom=='')								
                {  ?>
                <input type="checkbox" name="chk[]" value="<?php echo $mm.$rowCate['id'].$bill_raw['id'];?>" id="chk_<?php echo $j; ?>" />
                <?php }} ?>
                </td>
                <td class="jjrs_tablehead" colspan="2"><?php if($bom!=''){echo "<b>";}  echo "<img src='images/pms-folder.jpg'/>".$spacebar1."&nbsp;".$bill_raw['part_code'];if($bom!=''){echo "</b>";}?>
                <input type="hidden" name="part_code[<?php echo $mm.$rowCate['id'].$bill_raw['id'];?>]" value="<?php echo $bill_raw['part_code'];?>" />
                <?php $itemid=$s->getField('item_master','id',$bill_raw['mst_itemid'],'id');?>
                <input type="hidden" name="item_id[<?php echo $mm.$rowCate['id'].$bill_raw['id'];?>]" value="<?php echo $bill_raw['mst_itemid'];?>" />
				<input type="hidden" name="advice_details_id[<?php echo  $mm.$rowCate['id'].$bill_raw['id']; ?>]" value="<?php echo $adv_id;?>" />
				<input type="hidden" name="bill_material_id[<?php echo  $mm.$rowCate['id'].$bill_raw['id']; ?>]" value="<?php echo $row_comprole['id'];?>" />
                </td>
                <td class="jjrs_tablehead">
                <?php if($bom!=''){echo "<b>";}echo $itemname=$s->getField('item_master','id',$bill_raw['mst_itemid'],'item_name');if($bom!=''){echo "</b>";}?>
                <input type="hidden" name="item_name[<?php echo $mm.$rowCate['id'].$bill_raw['id'];?>]" value="<?php echo $itemname;?>" />
                </td>
                <td><?php if($bom!=''){echo "<b>";}echo $qty=$s->getField('unitsofmeasure','unitid',$unitid,'unitname');if($bom!=''){echo "</b>";} ?></td>
                <td class="jjrs_tablehead">
                <?php
                // echo  $query['Quantity']."qty".$bill_raw['bom_qty']."bomqty";
                if($bom!=''){echo "<b>";}echo $req_qty=$totalqty1*$bill_raw['bom_qty'];if($bom!=''){echo "</b>";}?>
                <input type="hidden" name="req_qty[<?php echo $mm.$rowCate['id'].$bill_raw['id'];?>]" id="req_qty" value="<?php echo $req_qty;?>"  size="10" readonly />
                </td>
                <td class="jjrs_tablehead">
                <?php 
                $op=openingBalance($function_filter,$bill_raw['mst_itemid']);
                $in_1=receivedMRN($function_filter,$bill_raw['mst_itemid']);
                $out_1=issueSlip($function_filter,$bill_raw['mst_itemid']);
                $out_2=issueSiteToSite($function_filter,$bill_raw['mst_itemid']);		
                $in_2=receivedSiteToSite($function_filter,$bill_raw['mst_itemid']);
                $out_3=issueMatarialReturn($function_filter,$bill_raw['mst_itemid']);
                $out_4=Rgpchallan($function_filter,$bill_raw['mst_itemid']);
                $out_5=Nrgp($function_filter,$bill_raw['mst_itemid']);
                $in_3=receivedReturn($function_filter,$bill_raw['mst_itemid']);		 
                $in_4=Rgpreturn($function_filter,$bill_raw['mst_itemid']);
                $in_5=FinishedMR($function_filter,$bill_raw['mst_itemid']);
                $invalue=($op+$in_1[0]+$in_2+$in_3+$in_4+$in_5);
                $outvalue=($out_1+$out_2+$out_3+$out_4+$out_5);
                $tot=trim($invalue)-trim($outvalue);
                $closing_stock=$tot;
                if($closing_stock!='0' and $confac!='0')
                {
                    $closing_stock2=round($closing_stock/$confac,2);
                }else{
                    $closing_stock2=0;
                }?>
                <?php //echo $closing_stock;
                if($bom==''){
                if(!empty($stock_val))
                {  
                    if(array_key_exists($bill_raw['mst_itemid'],$stock_val))
                    {
                        if($stock_val[$bill_raw['mst_itemid']]==0)
                        { 
                            echo $closing_stock=0;
                            $stock_val[$bill_raw['mst_itemid']]=0;
                        }else{
                            if($req_qty>$stock_val[$bill_raw['mst_itemid']])
                            { 
                                echo $closing_stock=$stock_val[$bill_raw['mst_itemid']];
                                $stock_val[$bill_raw['mst_itemid']]=0;
                            }else{
                                echo $closing_stock=$stock_val[$bill_raw['mst_itemid']];
                                $sval=$stock_val[$bill_raw['mst_itemid']]-$req_qty;
                                $stock_val[$bill_raw['mst_itemid']]=$sval;
                            }
                        }
                    }else{
                        if($req_qty>$closing_stock)
                        { 
                            echo $closing_stock;
                            $stock_val[$bill_raw['mst_itemid']]=0;
                        }else{
                            echo $closing_stock;
                            $sval=$closing_stock-$req_qty;
                            $stock_val[$bill_raw['mst_itemid']]=$sval;
                        }
                    }
                }else{
                    if($req_qty>$closing_stock)
                    { 
                        echo $closing_stock;
                        $stock_val[$bill_raw['mst_itemid']]=0;
                    }else{
                        echo $closing_stock;
                        $sval=$closing_stock-$req_qty;
                        $stock_val[$bill_raw['mst_itemid']]=$sval;
                    }
                }
                }
                ?>
                </td>
                <?php
                if($bom=='')								
                { ?>
                <td class="jjrs_tablehead">
                <?php
                if($closing_stock>=$req_qty)
                    $req_qty3=0;
                else if($closing_stock<$req_qty)
                    $req_qty3=($req_qty-$closing_stock);
                
                echo $req_qty3; ?>
                <input type="hidden" name="indent_req[<?php echo $mm.$rowCate['id'].$bill_raw['id'];?>]" id="indent_req_<?php echo $mm.$rowCate['id']; ?><?php echo $bill_raw['id']; ?>" value="<?php echo $req_qty3;?>" <?php if($bill_raw['status']==1){echo "readonly";}?> size="10" />
                </td>
                <td class="jjrs_tablehead">
                <input type="text" name="extra[<?php echo $mm.$rowCate['id']; ?><?php echo $bill_raw['id']; ?>]" id="extra_<?php echo $mm.$rowCate['id']; ?><?php echo $bill_raw['id']; ?>" onkeyup="calculatetotal(this.value,<?php echo $mm.$rowCate['id'].$bill_raw['id']; ?>)" value="" size="10" />
                </td> 
                <td class="jjrs_tablehead">
                <input type="text" name="totalqty[<?php echo $mm.$rowCate['id']; ?><?php echo $bill_raw['id']; ?>]" id="totalqty_<?php echo $mm.$rowCate['id']; ?><?php echo $bill_raw['id']; ?>" value="<?php echo $req_qty3;?>" size="10" readonly='true' />
                </td> 
                <td class="jjrs_tablehead">
                <input type="text" name="indentdate[<?php echo $mm.$rowCate['id']; ?><?php echo $bill_raw['id']; ?>]" id="indentdate_1<?php echo $rowCate['id']; ?><?php echo $bill_raw['id']; ?>" value="<?php echo date("d/m/Y");?>" readonly="true" class="datepicker" />
                </td>  
                <td width="91" class="jjrs_tablehead">
                <?php if($status==1) { echo "PR Generated"; } else if($status==2) { echo "Closed W/O PR"; } else {echo "Pending";}?>
                <input type="hidden" name="material[<?php echo $mm.$rowCate['id'].$bill_raw['id'];?>]"  value="<?php echo $rowCate['id']; ?>" size="10"/>
                </td>
                 <?php }else{ ?>
                 <td class="jjrs_tablehead" colspan="5"></td>
                 <?php } ?>
            </tr>
            <?php $j++; 
        } ?>

        <?php
        if($bom!=''){
            materialshow($bom,$spacebar1,$req_qty,$j,$sopstatus,$function_filter,$mm,$stock_val,$adv_id);
        }else{
            materialshow1($row_comprole['mst_itemid'], $spacebar1,$req_qty,$j,$sopstatus,$function_filter,$mm,$stock_val,$adv_id);	
        }    
							
		}
	}
} 
		
$spacebar = "&nbsp;&nbsp;&nbsp;";	

function materialshow1($PcateID, $dashVal,$totalqty1,$j,$sopstatus,$function_filter,$mm,$stock_val,$adv_id)
{
    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);
   		$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 $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
            if($bom!='')								
                materialshow($bom, $spacebar,$totalqty1,$j,$sopstatus,$function_filter,$mm,$stock_val,$adv_id);
            else
                materialshow1($row_comprole['mst_itemid'], $spacebar,$totalqty1,$j,$sopstatus,$function_filter,$mm,$stock_val,$adv_id);	
							
		$j=$j++;	
        }
	}else{
        $_SESSION['stockval']=$stock_val;
    }
}

function materialshow2($PcateID, $dashVal,$totalqty1,$j,$i,$mainid)
{	
    global $db;
	$spacebar = $dashVal ;
	$totalqty1=$totalqty1;
	$s=new db_query(); 		
	   
	$sqlCate = "select * from bill_material  where  id  = '".$PcateID."'";
    $rsCate  = DB_query($sqlCate,$db);	
 	$rowCate = DB_fetch_array($rsCate);
	
    $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');
		if($bom!='')
		{?>
            <li style="text-align:left;">
            <span class="folder">
                <a href="index.php?route=PMS/Transcations/mrp_boq_generatedata&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>&id=<?php echo $_REQUEST['id']; ?>&subid=<?php echo $row_comprole['mst_itemid']; ?>&mainid=<?php echo $mainid; ?>"> 
                <?php if($_REQUEST['subid']==$row_comprole['mst_itemid']) {?>
                    <font color="#CC33CC"><?php echo  $itmname=$s->getField('item_master','id',$row_comprole['mst_itemid'],'item_name'); ?></font>
                <?php } else { ?>
                    <?php echo  $itmname=$s->getField('item_master','id',$row_comprole['mst_itemid'],'item_name'); ?>
                <?php } ?>
                </a>
            </span>
        <?php } else { ?>
                <li style="text-align:left;"><span class="file"> <?php echo $itmname=$s->getField('item_master','id',$row_comprole['mst_itemid'],'item_name'); ?></span></li>
		<?php } ?>
		
        <?php			
        if($bom!='')
        {?>
            <ul>	
            <?php materialshow2($bom,$spacebar,$req_qty,$j,$i,$mainid);
        }
        else{
            materialshow3($row_comprole['mst_itemid'], $spacebar,$req_qty,$j,$i,$mainid);
        }?>
        <?php	
		}
		?>
</li></ul>
<?php 
}
					
function materialshow3($PcateID, $dashVal,$totalqty1,$j,$i,$mainid)
{
	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');	
			if($bom!='')
			{?>
            <ul>
			<li style="text-align:left;"><span class="folder"><?php echo $itmname=$s->getField('item_master','id',$row_comprole['mst_itemid'],'item_name'); ?></span>
            <?php } else { ?>
			<li style="text-align:left;"><span class="file"> <?php echo $itmname=$s->getField('item_master','id',$row_comprole['mst_itemid'],'item_name'); ?></span></li>
			<?php } ?>
			<?php			
            if($bom!=''){?>
            <ul>								
            <?php materialshow2($bom,$spacebar,$req_qty,$j,$i,$mainid);
            }else{
            materialshow3($row_comprole['mst_itemid'], $spacebar,$req_qty,$j,$i,$mainid);
            }?>
            </ul>
            </li>   
            <?php	
		}
	}	
}?>
