<link rel="stylesheet" href="css/new_mxerp.css">
<script src="js1/jquery.ui.widget.js"></script>
<script src="js1/jquery.ui.tabs.js"></script>
<script>
	$(function() {
		$( "#tabs" ).tabs();
	});
	
	function getdate(i)
	{
	
 	 a=document.getElementById('qty_'+i).value;
 	 b=document.getElementById('rate_'+i).value;
 	 c=a*b;
 	 document.getElementById('amount_'+i).value=c;
	}
	</script>
	<script>
	$( document ).ready(function() {
$("#fade").fadeOut(4000);
});

function validates()
	{
	var r=confirm("R u want to save?");
	if (r==true)
	{
	  return true;
	}
	else
	{
	  return false;
	} 
 	if(document.project_entry.handeled_by.value=='')
	{
		alert("Please Enter Handled By");
		(document.project_entry.handeled_by).focus();
		return false;
	}
}
</script>
<?php
$sqlsop="SELECT * FROM manufacturing_advice where id='".$_REQUEST['pid']."'";
$sqlsop=DB_query($sqlsop,$db);
$qrysop=DB_fetch_array($sqlsop);

$function_filter[]=$qrysop['branch'];
$function_filter[]=$qrysop['store'];
$data_date=date('Y-m-d');
					    
$function_filter[]=$data_date;
$m=0;


if($_REQUEST['savedata'])
{ 
        //echo "<pre>";print_r($_REQUEST);die;
        // fetch Prefix
	$prefixCode = $s->GetFieldValue('prefix_tbl',"prefix_id='16'",'prefix_code');
	$retrieve = "SELECT * FROM boq_entry";
	$resultret = DB_query($retrieve,$db);
	if(DB_num_rows($resultret)>0)
	{
		$re = "SELECT MAX(id) as id FROM boq_entry";
		$exe = DB_query($re,$db);
		$maximumID=DB_fetch_array($exe);
		$pad =$maximumID['id']+1;
		$autoBoqNo = $prefixCode.'-00'.$pad;
	}else{
		$autoBoqNo = $prefixCode.'-001';
	}
     
	$data1['salesorder_id'] = $_REQUEST['so_id'];
	$data1['boq_no'] = $autoBoqNo;
	$data1['project_date'] = date('Y-m-d');
	$data1['project_handeled_by'] = $_REQUEST['handeled_by'];
	$data1['project_status'] = '1';
	$data1['companyId'] = $_SESSION['ACGCompanyId'];
	//$data1['total_amount'] = $_REQUEST['total_amount'];
	$data1['created_by'] = $_SESSION['UserID'];
	$r= $s->RecordInsert('boq_entry', $data1);
	$last_id=DB_Last_Insert_Id($db,"boq_entry","id");
	
	$data7['project_status'] = '1';	
	$condition7="id='".$_REQUEST['so_id']."'";
 	$s->RecordEdit('manufacturing_advice',$data7,$condition7);	

	if(DB_affected_rows($db)>0)
	{	
	for($i=0;$i<count($_REQUEST['ActQuantity']);$i++)
	{ 
		if($_REQUEST['ActQuantity'][$i]!=''){
			$flag = 1;
			$data6['project_status'] = '1';	
			$condition6="id='".$_REQUEST['so_det_id'][$i]."'";
		 	$s->RecordEdit('manufacturing_advice_items',$data6,$condition6);

			$data['salesorder_id'] = $_REQUEST['so_id'];
			$data['work_description'] = $_REQUEST['description'][$i];
			$data['quantity'] = $_REQUEST['ActQuantity'][$i];
			//$data['amount'] = $_REQUEST['ActAmount'][$i];
			$data['startdate'] =FormatDateFORSQL($_REQUEST['fromdate1'][$i]);
			$data['enddate'] =FormatDateFORSQL($_REQUEST['enddate1'][$i]);
			$data['remarks'] = $_REQUEST['text'][$i];
			$data['master_id'] = $last_id;
			$data['itemCode'] = $_REQUEST['itemCode'][$i];
			$data['activity_group'] = $_REQUEST['act_group'][$i];
			$data['activity_name'] = $_REQUEST['act_name'][$i];
			$data['unit'] = $_REQUEST['act_unit'][$i];
			//$data['rate'] = $_REQUEST['act_rate'][$i];
			$data['level'] = $_REQUEST['so_det_id'][$i];
			$data['bom_main_id'] = $_REQUEST['bom_main_id'][$i];
			$r= $s->RecordInsert('boq_entry_details', $data);
			if(DB_affected_rows($db)>0)
			{
				$flag1 = 1;
			}
		}
	}
	}
	$s->pageLocation("index.php?route=PMS/Transcations/ViewBOQ".$rurl);
		
	if($flag == 1 && $flag1 == 1)
	{
		echo "<div id = 'fade' style='color:green;'>Successfully Insearted</div>";
	}
}
?>

<div class="body_contain">
    <div class="tab_div">
        <form action="" name="project_entry" method="post" >
	<input type="hidden" name="so_id" value="<?php echo $_REQUEST['pid']; ?>">
	<?php $DOCsql=extract(DB_fetch_array(DB_query("select * from manufacturing_advice where CompanyId='".$_SESSION['ACGCompanyId']."' and id='".$_REQUEST['pid']."' ",$db))); ?>
        <h1>Project Acivities Planning</h1>
        <!-- Body Content Start -->
        <div id="body_content">
        <!-- TAble 1 Start -->
            <div style="overflow:auto; margin-left:auto; margin-right:auto;" width="100%">
                <table width="100%" border="0" cellspacing="0" cellpadding="3" class="">
                    <tr class="row1">	
                        <td class="">Branch</td>
                        <td><input type="text" readonly name="branch" style="width:150px;" value="<?php echo $s->GetfieldValue('locations',"loccode='".$branchid."'",'locationname');?>"></td>
                        <td class="" >BOQ No</td>
                        <td ><input type="text" name="boq_no" style="width:150px;" value="will be auto generated" readonly /></td>
                        <td class="">Date</td>
                        <td><input name="boq_date" type="text" style="width:150px;" value="<?php echo ConvertSQLDate(date('Y-m-d')); ?>"  class="datepicker"/></td>
                    </tr>
                    <tr class="row1">
                        <td class="">Project No</td>
                        <td><input type="text" name="order_no" style="width:150px;" value="<?php echo $advice_no; ?>" readonly/>
                         </td>
                        <td class="">Project Date</td>
                        <td><input type="text" name="order_date" style="width:150px;" value="<?php echo ConvertSQLDate($advice_date); ?>" readonly/></td>
                        <td class="">Project Name</td>
                        <td><input type="text" name="boq_client" style="width:150px;" readonly value="<?php echo $project_name;?>"></td>
                    </tr>
                    <tr class="row1"> 
                        <td class="">Delivery Date</td>
                        <td><input type="text" name="po_no" style="width:150px;" value="<?php echo ConvertSQLDate($delivery_date); ?>" readonly /></td>
                        <td class="">Prepared By</td>
                        <td><input type="text" name="po_date" style="width:150px;" value="<?php echo $s->GetfieldValue('www_users',"id='".$prepared_by."'",'realname');?>" readonly/></td>
                        <td class="">Approved By <font style="color:#FF0000;">*</font></td>
                        <td><input type="text" style="width:150px;" value="<?php echo $approved_by; ?>" readonly name="handeled_by"></td>
                    </tr>
                </table>
            </div>
            <br />
            <!-- TAble 2 Start -->
            <table border="1" width="100%" cellpadding="3" cellspacing="0">
                <tr class="row1">
                    <th class="">Activity Group</th>
                    <th class="">Activity Name</th>
                    <th class="">Description</th>
                    <!--<th class="">Grade</th>-->
                    <th class="">Unit</th>
                    <th class="">Quantity</th>
                    <!--<th class="">Rate</th>
                    <th class="">Amount</th>-->
                    <th class="">Start Date</th>
                    <th class="">End Date</th>
                    <th class="">Remark</th>
                    <!--<th class="">Delete</th>-->
                </tr>
                <?php
                $qty=array(); 
                $query = "SELECT * from manufacturing_advice_items WHERE advice_id='".$id."'";
                $res = DB_query($query,$db) or die(DB_error());
                while($result = DB_fetch_array($res)){
                $x = $result['item_code'];
                $Subcat=$s->GetFieldVAlue('item_master',"id='".$result['item_code']."'",'SubCategoryID');
                $sqlactivityGroup=DB_query("select * from sub_activity_defination where inventory_group_id='".$Subcat."' and status='Active'",$db);
		if(DB_num_rows($sqlactivityGroup)>0){
		$m++;                
		?>
                <tr class="row1">
                    <td colspan="2"><?php echo "<b>".$s->GetFieldVAlue('item_master',"id='".$result['item_code']."'",'item_name')."</b>"; ?></td>
                    <td ></td>
                    <td><b><?php $unit=$s->GetFieldVAlue('item_master',"id='".$result['item_code']."'",'mst_unitid');  echo $s->GetFieldVAlue('unitsofmeasure',"unitid='".$unit."'",'unitname'); ?></b></td>
                    <td colspan="7"><b><?php  echo $tqty=$result['Qty'];?></b></td>
                </tr>
                <?php
	   	}
                while($AcGroup=DB_fetch_array($sqlactivityGroup)){
                ?>
                <input type="hidden" name="itemCode[]" value="<?php echo $result['item_code']; ?>"/>
		<input type="hidden" name="so_det_id[]" value="<?php echo $result['id']; ?>"/>                
		<input type="hidden" name="noOfRows[]" value="<?php echo $m; ?>"/>
		<input type="hidden" name="bom_main_id[]" value="<?php echo $id; ?>"/>
                <input type="hidden" name="act_group[]" value="<?php echo $AcGroup['group_name'];
                $s->GetFieldValue('sub_activity_group',"id='".$AcGroup['group_name']."'",'group_name'); ?>"/>
                <input type="hidden" name="act_name[]" value="<?php echo $AcGroup['id']; ?>"/>
                <tr class="row1">
                    <td><?php echo $s->GetFieldValue('sub_activity_group',"id='".$AcGroup['group_name']."'",'group_name'); ?></td>
                    <td><?php echo $AcGroup['activity_name']; ?></td>
                    <td><textarea id="description<?php echo $m;?>" name="description[]" style="height:20px; width:100px;"></textarea></td>
                    <!--<td>
                    <select style="width:100px; height:20px;" name="grade[]" id="grade<?php echo $m;?>">
                    <option value="0">Select</option>
                    <?php 
                    $sql_grade=DB_query("select * from product_grade where 1",$db);
                    while($sq=DB_fetch_array($sql_grade))
                    {
                    ?>
                    <option value="<?php echo $sq['id']; ?>"><?php echo $sq['grade']; ?></option>
                    <?php } ?>

                    </select>
                    </td>-->
                    <td ><input type="text" name="act_unit[]" value="<?php echo $s->GetFieldValue('unitsofmeasure',"unitid='".$AcGroup['unit']."'",'unitname'); ?>" readonly style="width:60px; height:20px;"/></td>
                    <td><input type="text" name="ActQuantity[]" onchange="get_amount('<?php echo $m;?>');" id="ActQuantity_<?php echo $m; ?>" style="width:50px;height:20px;" class="validate"/></td>
                    <!--<td><input type="text" name="act_rate[]" id="act_rate_<?php echo $m; ?>" value="<?php $rate= DB_fetch_array(DB_query("select Rate as total_rate from salesorder_details where mst_itemid='".$result['mst_itemid']."' and masterId='".$id."'",$db));echo $rate['total_rate']; //echo $AcGroup['rate']; ?>" readonly style="width:60px;height:20px;"/></td>
                    <td><input type="text" name="ActAmount[]" id="ActAmount_<?php echo $m; ?>" readonly style="width:80px;height:20px;" /></td>-->
                    <td><input name="fromdate1[]" value="<?php echo date('d/m/Y'); ?>" type="text"  class="datepicker" style="width:80px;height:20px;" /></td>
                    <td><input name="enddate1[]" value="<?php echo date('d/m/Y'); ?>" type="text"  class="datepicker" style="width:80px;height:20px;"/></td>
                    <td><textarea id="act_r_<?php echo $m; ?>" name="text[]" style="height:20px; width:130px;"></textarea></td>
                    <!--<td><a href="index.php?route=PMS/Transcations/boq_templates_entry<?php echo $rurl;?>&action=delete&opn=<?php echo $AcGroup['id']; ?>" onclick="return confirm('Are you sure you want to delete?')"> <img src="images/Icon_Delete.png" /></a></td>-->
                </tr>
                <?php }  ?>
                <?php
                
                    $bom=$s->getField('bill_material','part_id',$result['item_code'],'id');
                    if($bom!='')								
                    {
                        materialshow($bom,$spacebar,$tqty,$j,$result['item_code'],$function_filter,$m,$result['id']);
                    }
                ?>
                <?php } ?>
                <!--<tr class="row1">
                    <td colspan="6" style="text-align:right;">Total Project Amount</td>
                    <td colspan="6" style="text-align:left;"><input readonly type = "text" name= "total_amount" id="total_amount" style="width:100px; height:20px;" /></td>
                </tr>-->
                <tr class="row1">
                    <td colspan="11" style="text-align:center; font-weight:bold;">Prepared By <?php echo $s->GetFieldValue('www_users',"id='".$_SESSION['UserID']."'",'userid'); ?></td>
                </tr>
                <tr class="row1"> 
                    <td colspan="11" style="text-align:center;">
                        <input type="submit" name="savedata" value="save" class="search" onClick="return validates()"></td>
                </tr>
            </table>
            <!-- TAble 2 Close -->
        </div>
<!-- Body Content Close -->
        </form>
    </div>
</div>    
<?php
function materialshow($PcateID, $dashVal,$totalqty1,$j,$sopstatus,$function_filter,$m,$did)
{	
    global $db;
    $spacebar="<img src='images/pms-arrow.jpg'/>";
	$spacebar = $spacebar.$dashVal;
	$totalqty1=$totalqty1;
	$s=new db_query(); 		
	$sopstatus=$sopstatus;
    
        $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))
            {
	    $Subcat=$s->GetFieldVAlue('item_master',"id='".$row_comprole['mst_itemid']."'",'SubCategoryID');
            $sqlactivityGroup=DB_query("select * from sub_activity_defination where inventory_group_id='".$Subcat."' and status='Active'",$db);
	    if(DB_num_rows($sqlactivityGroup)>0){
            $m++;?>
            <tr class="row1">
                <td colspan="2"><?php echo "<b><img src='images/pms-folder.jpg'/>".$spacebar."&nbsp;".$s->GetFieldVAlue('item_master',"id='".$row_comprole['mst_itemid']."'",'item_name')."</b>"; ?></td>
                <td ></td>
                <td><?php $unit=$s->GetFieldVAlue('item_master',"id='".$row_comprole['mst_itemid']."'",'mst_unitid');  echo $s->GetFieldVAlue('unitsofmeasure',"unitid='".$unit."'",'unitname'); ?></td>
                <td colspan="7"><?php echo $totalqty1=$totalqty1*$row_comprole['bom_qty']; ?></td>
            </tr>
            <?php
	    }
            while($AcGroup=DB_fetch_array($sqlactivityGroup)){
            $m++;
            ?>
            <input type="hidden" name="itemCode[]" value="<?php echo $row_comprole['mst_itemid']; ?>"/>
            <input type="hidden" name="noOfRows[]" value="<?php echo $m; ?>"/>
            <input type="hidden" name="so_det_id[]" value="<?php echo $did; ?>"/>
			<input type="hidden" name="bom_main_id[]" value="<?php echo $PcateID; ?>"/>
            <input type="hidden" name="act_group[]" value="<?php echo $AcGroup['group_name'];
            $s->GetFieldValue('sub_activity_group',"id='".$AcGroup['group_name']."'",'group_name'); ?>"/>
            <input type="hidden" name="act_name[]" value="<?php echo $AcGroup['id']; ?>"/>
        	<tr class="row1">
            	<td><?php echo $s->GetFieldValue('sub_activity_group',"id='".$AcGroup['group_name']."'",'group_name'); ?></td>
                <td><?php echo $AcGroup['activity_name']; ?></td>
                <td><textarea id="description<?php echo $m;?>" name="description[]" style="height:20px; width:100px;"></textarea></td>
                <!--<td>
                <select style="width:100px; height:20px;" name="grade[]" id="grade<?php echo $m;?>">
	<option value="0">Select</option>
            	<?php 
                $sql_grade=DB_query("select * from product_grade where 1",$db);
                while($sq=DB_fetch_array($sql_grade))
                {
                ?>
                    <option value="<?php echo $sq['id']; ?>"><?php echo $sq['grade']; ?></option>
                <?php } ?>
                </select>
                </td>-->
                <td><input type="text" name="act_unit[]" value="<?php echo $s->GetFieldValue('unitsofmeasure',"unitid='".$AcGroup['unit']."'",'unitname'); ?>" readonly style="width:60px; height:20px;"/></td>
                <td><input type="text" name="ActQuantity[]" onchange="get_amount('<?php echo $m;?>');" id="ActQuantity_<?php echo $m; ?>" style="width:50px;height:20px;" class="validate"/></td>
                <!--<td><input type="text" name="act_rate[]" id="act_rate_<?php echo $m; ?>" value="<?php $rate= DB_fetch_array(DB_query("select Rate as total_rate from salesorder_details where mst_itemid='".$row_comprole['mst_itemid']."' and masterId='".$id."'",$db));echo $rate['total_rate']; //echo $AcGroup['rate']; ?>" readonly style="width:60px;height:20px;"/></td>
                <td><input type="text" name="ActAmount[]" id="ActAmount_<?php echo $m; ?>" readonly style="width:80px;height:20px;" /></td>-->
                <td><input name="fromdate1[]" value="<?php echo date('d/m/Y'); ?>" type="text"  class="datepicker" style="width:80px;height:20px;" /></td>
                <td><input name="enddate1[]" value="<?php echo date('d/m/Y'); ?>" type="text"  class="datepicker" style="width:80px;height:20px;"/></td>
                <td><textarea id="act_r_<?php echo $m; ?>" name="text[]" style="height:20px; width:130px;"></textarea></td>
                <!--<td><a href="index.php?route=PMS/Transcations/boq_templates_entry<?php echo $rurl;?>&action=delete&opn=<?php echo $AcGroup['id']; ?>" onclick="return confirm('Are you sure you want to delete?')"> <img src="images/Icon_Delete.png" /></a></td>-->
            </tr>
            <?php }  ?>
            <?php
            $bom=$s->getField('bill_material','part_id',$row_comprole['mst_itemid'],'id');
            if($bom!='')								
            {
                materialshow($bom,$spacebar,$totalqty1,$j,$row_comprole['mst_itemid'],$function_filter,$m,$did);
            }//else{
            //    materialshow1($row_comprole['mst_itemid'], $spacebar,$totalqty1,$j,$_REQUEST['pid'],$function_filter,$m);
            //}
       }
	}
} 
		


/*function materialshow1($PcateID, $dashVal,$totalqty1,$j,$sopstatus,$function_filter,$m)
//{
//    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);
//            else
//                materialshow1($row_comprole['mst_itemid'], $spacebar,$totalqty1,$j,$sopstatus,$function_filter,$mm,$stock_val);	
//							
//		$j=$j++;	
//        }
//	}else{
//        $_SESSION['stockval']=$stock_val;
//    }
//}*/
?>
<script>
function calculate_amount(str)
{
	//alert(str);
	var edit_rate = $('#edit_rate'+str).val();
	var edit_quantity = $('#edit_quantity'+str).val();
	var edit_amount = edit_rate*edit_quantity;
	$('#edit_amount'+str).val(edit_amount);
	//alert(edit_amount);
}

function validate(str)
{
	var xyz = 0;
	var flag = 0;
	var flag1 = 0;
	
	for(i=0;i<str;i++)
	{
	if($('#description'+i).val() == "")
		{
			xyz = 1;
		}
	}
	
	$('.validate').each(function()
	{
		if($(this).val() == '') 
		{
			flag = 1 ;	
		}
	});
	
	
	$('.datepicker').each(function(){
		if($(this).val() == "")
		{
			flag1 = 1;
		}
	});
	
	if(flag1 == 1 || flag == 1 || xyz == 1)
	{
		alert('Fill Mandatery Fields');
		return false;
	}
}
function get_amount(i)
{
//alert(i);total_amount

	var rate=document.getElementById("act_rate_"+i).value;
	var quanty=document.getElementById("ActQuantity_"+i).value;
	
	var total=rate*quanty;
	var amount = document.getElementById("ActAmount_"+i).value=total.toFixed(2);
	
	
	var totalAm=document.getElementById("total_amount").value;
	
	
	if(totalAm!='')
	{
		//var amt1=document.getElementById("total_amount").value;
		totalAm=parseFloat(totalAm)+parseFloat(total);
		
	}
	else
	{
		totalAm=parseFloat(total);
	}
	
	document.getElementById("total_amount").value=totalAm;
	
	
	
	
}



// total amount

</script>
