<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);
}
</script>

<script>
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;
	
	
	
	
}
</script>
<?php
// fetch Prefix
	//$prefixCode = $s->GetFieldValue('prefix_tbl',"prefix_id='18'",'prefix_code');
$prefixCode="VWE/RA";
		$retrieve = "SELECT * FROM ra_bill";
		$resultret = DB_query($retrieve,$db);
		if(DB_num_rows($resultret)>0)
		{
		$re = "SELECT MAX(id) as id FROM ra_bill";
		$exe = DB_query($re,$db);
		$maximumID=DB_fetch_array($exe);
		$pad =$maximumID['id']+1;
		$autojobNo = $prefixCode.'-00'.$pad;
		}
		else
		{
		$autojobNo = $prefixCode.'-001';
		}

	  if($_REQUEST['savedata'])
	  { 
		$data1['branch'] =$_REQUEST['branchs'];
		$data1['bill_code'] = $autojobNo;
		$data1['bill_no'] =$_REQUEST['bill_no'];
		$data1['bill_date'] =FormatDateFORSQL($_REQUEST['bill_date']);
		$data1['ra_type'] = $_REQUEST['ra_type'];
		$data1['assign_to'] = $_REQUEST['contr_id'];
		$data1['pa_id'] = $_REQUEST['pa_id'];
		$data1['partyid'] = $_REQUEST['partyid'];
		$data1['pcode'] = $_REQUEST['pcode'];
		$data1['pname'] = $_REQUEST['pname'];
		$data1['total_sum'] = $_REQUEST['total_sum'];
		$data1['EDRate'] = $_REQUEST['EDRate'];
		$data1['service_tax_value'] = $_REQUEST['service_tax_value'];
		$data1['edu_cess'] = $_REQUEST['edu_cess'];
		$data1['educess_value'] = $_REQUEST['educess_value'];
		$data1['sh_edu'] = $_REQUEST['sh_edu'];
		$data1['shecess_value'] = $_REQUEST['shecess_value'];
		$data1['sign'] = $_REQUEST['sign'];
		$data1['round_off_value'] = $_REQUEST['round_off_value'];
		$data1['round_ledger'] = $_REQUEST['round_ledger'];
		$data1['gross_amt'] = $_REQUEST['gross_amt'];
		$data1['h_gross_amt'] = $_REQUEST['h_gross_amt'];
		$data1['companyId'] = $_SESSION['ACGCompanyId'];
		$data1['created_by'] = $_SESSION['UserID'];
		$data1['Remarks'] = $_REQUEST['Remarks'];
		$r= $s->RecordInsert('ra_bill', $data1);
		$last_id=DB_Last_Insert_Id($db,"ra_bill","id");
		
		if(DB_affected_rows($db)>0)
		{
		$flag = 1;
		
	  	for($i=0;$i<count($_REQUEST['act_per']);$i++)
		{ 
		$data['master_id'] = $last_id;
		$data['boq_id'] = $_REQUEST['boq_id'][$i];
		$data['act_group'] = $_REQUEST['act_group'][$i];
		$data['act_name'] = $_REQUEST['act_name'][$i];
		$data['act_sum'] = $_REQUEST['act_sum'][$i];
		$data['act_per'] = $_REQUEST['act_per'][$i];
		$data['level'] = $_REQUEST['level'][$i];
		$data['bom_main_id'] = $_REQUEST['bom_main_id'][$i];
			$r= $s->RecordInsert('ra_bill_details', $data);
			if(DB_affected_rows($db)>0)
			{
				$flag1 = 1;
			}
		}
		if($flag == 1 && $flag1 == 1)
			{
				$pcs=explode(",",$_REQUEST['pcs']);
				for($a=0;$a<count($pcs);$a++){
					$data6['ra_status'] = 1;	
					$condition6="id=".$pcs[$a]."";
					$s->RecordEdit('project_completion',$data6,$condition6);
				}
				echo "<div id = 'fade' style='color:green;'>Successfully Insearted</div>";
			}
		$s->pageLocation("index.php?route=PMS/Transcations/rabill_entry_view".$rurl);
		}	
			
			
}	  
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
	<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()
	{
	
 	if(document.bill_creation.bill_no.value=='')
	{
		alert("Please Enter Bill No.");
		(document.bill_creation.bill_no).focus();
		return false;
	}
	else if(document.bill_creation.bill_date.value=='')
	{
		alert("Please Enter Bill Date");
		(document.bill_creation.bill_date).focus();
		return false;
	}
	}
</script>

<form action="" name="bill_creation" method="post" >
<?php 

 if($_REQUEST['addCont'])
	  { 
$mm=implode(",",$_REQUEST['arrayItem']);
$DOCsql=DB_query("select project_completion.id as main_id,project_completion.boq_id as bid,project_completion.*,project_assign_contractor.vendor_id as v_id,project_work_assignment.tech_id AS t_id from project_completion left join  project_assign_contractor on project_completion.boq_id=project_assign_contractor.boq_id LEFT JOIN project_work_assignment ON project_completion.boq_id=project_work_assignment.boq_id where  project_completion.approval_status='1' and project_completion.companyID='".$_SESSION['ACGCompanyId']."' and project_completion.id IN (".$mm.")",$db);
if(DB_num_rows($DOCsql)>0){
$i=0;
	  $Doc=DB_fetch_array($DOCsql);
	  extract($Doc);
$paid=$s->GetFieldValue('boq_entry','id="'.$Doc['bid'].'"','salesorder_id');
  ?>
<h1>RA Bill Creation Entry</h1>
<!-- Body Content Start -->
<input type="hidden" name="ra_type" value="<?php echo $_REQUEST['ra_type']; ?>"/>
<input type="hidden" name="contr_id" value="<?php echo $_REQUEST['cont_id']; ?>"/>
<div id="body_content">
	<!-- TAble 1 Start --><div style="overflow:auto; margin-left:auto; margin-right:auto;" width="100%">
	<table width="100%" border="1" cellspacing="0" cellpadding="3" class="">
   		<td class="label">Branch</td>
   		  <td><input type="text" name="branchs" value="<?php echo $branch; ?>" readonly/></td>
   		 <td class="label">Bill No <font style="color:#FF0000;">*</font></td>
		 <td><input type="text" name="bill_no" /></td>
		 <td class="label">Bill Date <font style="color:#FF0000;">*</font></td>
		 <td><input type="text" name="bill_date" class="datepicker"/></td>
	</tr>
	
	<tr>
		 <td class="label">PA No.</td>
		 <td><input type="hidden" name="pa_id" value="<?php echo $paid; ?>" readonly/><input type="text" name="pa_no" value="<?php echo $s->GetFieldValue('manufacturing_advice','id='.$paid.'','advice_no'); ?>" readonly/></td>
		 <td class="label">PA Date</td>
		 <td><input type="text" name="pa_date" value="<?php echo ConvertSQLDate($s->GetFieldValue('manufacturing_advice','id='.$paid.'','advice_date')); ?>" readonly/></td>
		<td class="label">Party</td>
		 <td><input type="hidden" name="partyid" value="<?php echo $partyid=$s->GetFieldValue('manufacturing_advice','id='.$paid.'','partyid'); ?>" readonly/><input type="text" name="party" value="<?php echo $s->GetFieldValue('clientmaster','id='.$partyid.'','c_name'); ?>" readonly/></td>
	</tr>
<tr>
		 <td class="label">Project Code</td>
		 <td><input type="text" name="pcode" value="<?php echo $s->GetFieldValue('manufacturing_advice','id='.$paid.'','project_code'); ?>" readonly/></td>
		 <td class="label">Project Name</td>
		 <td><input type="text" name="pname" value="<?php echo $s->GetFieldValue('manufacturing_advice','id='.$paid.'','project_name'); ?>" readonly/></td>
		<td class="label">&nbsp;</td>
		 <td>&nbsp;</td>
	</tr>
</table>
	</div>
	<br />
<!-- TAble 2 Start -->
<!-- TAble 2 Close -->
</div><!-- Body Content Close -->
<table border="1" width="100%" cellpadding="2" cellspacing="4">
  <!--<tr>
    <th height="29" class="label">BOQ No</th>
    <th class="label">BOQ Date</th>
    <th class="label">PCS No</th>
    <th class="label">PCS Date</th>
    <th class="label">Project</th>
    <th class="label">Amount</th>
  </tr>-->
  <?php 
$DOCsqlboq=DB_query("select project_completion.id as main_id,project_completion.*,boq_entry.* from project_completion left join  boq_entry on project_completion.boq_id=boq_entry.id where project_completion.approval_status='1' and project_completion.ra_status=0 and project_completion.companyID='".$_SESSION['ACGCompanyId']."' and project_completion.id IN (".$mm.") group by project_completion.boq_id",$db);
$b=0;
while($Docboq=DB_fetch_array($DOCsqlboq)){?>
<tr class="rowdata">
	<td><b>BOQ No.</b></td>
    <td><b><input type="hidden" name="boq_nos[]" value="<?php echo $Docboq['boq_id']; ?>"/>
        <?php echo $Docboq['boq_no']; ?></b></td>
	<td><b>BOQ Date</b></td>
    <td><b><input type="hidden" name="boq_dates[]" value="<?php echo $Docboq['boq_date']; ?>"/>
        <?php echo ConvertSQLDate($Docboq['boq_date']); ?></b></td>
    <td><input type="hidden" name="pcs" value="<?php echo $mm; ?>"/></td>
    <td>&nbsp;</td>
	<td>&nbsp;</td>
	</tr>
<tr class="rowdata">
		<td height="29">Under</td>
		<td height="29">Activity Group</td>
		<td>Activity Code</td>
		<td>Activity Name</td>
		<td>Total Quantity</td>
		<td>Quantity</td>
		<td>Percent (%)</td>
		
	</tr>

<?php //if(is_null($v_id)){
	//$query = "select project_completion.*,project_completion.id as idd from project_completion left join  project_work_assignment on project_completion.boq_id=project_work_assignment.boq_id where  project_completion.approval_status='1' and project_completion.ra_status is null and project_completion.companyID='".$_SESSION['ACGCompanyId']."' and project_work_assignment.tech_id='".$t_id."' and project_completion.id IN (".$mm.") order by project_completion.date ASC";
//}else{
//$query = "select project_completion.*,project_completion.id as idd from project_completion left join  project_assign_contractor on project_completion.boq_id=project_assign_contractor.boq_id where  project_completion.approval_status='1' and project_completion.ra_status is null and project_completion.companyID='".$_SESSION['ACGCompanyId']."' and project_assign_contractor.vendor_id='".$v_id."' and project_completion.id IN (".$mm.") order by project_completion.date ASC";
//}
	//$res = DB_query($query,$db) or die(DB_error());
	//$m=0;
	$sum=array();
	//while($result = DB_fetch_array($res)){
	//$m++;
	?>
  <?php 


$query1level = "select Distinct(level) AS mlevel from project_completion_details where master_id IN (".$mm.") and boq_id='".$Docboq['boq_id']."'";
	$res1level = DB_query($query1level,$db) or die(DB_error()); 
	$sum=array();
	while($result1level = DB_fetch_array($res1level)){
$querytot = "select count(id) AS mcount from boq_entry_details where master_id='".$Docboq['boq_id']."' and level='".$result1level['mlevel']."'";
$restot = DB_query($querytot,$db) or die(DB_error()); 
$resulttot = DB_fetch_array($restot);
//echo $resulttot['mcount'];  	


	$query1 = "select  *,SUM(complete_qty) AS act_sum from project_completion_details where master_id IN (".$mm.") and boq_id='".$Docboq['boq_id']."' and level='".$result1level['mlevel']."' group by bom_main_id,act_name";
	$res1 = DB_query($query1,$db) or die(DB_error()); 
	$mm1=1;
	$sum1=array();
	while($result1 = DB_fetch_array($res1)){
	?>
	<tr>
		<td><input type="hidden" name="level[]" value="<?php echo $result1['level']; ?>"/><?php echo $s->GetFieldValue('item_master','id="'.$s->GetFieldValue('manufacturing_advice_items','id="'.$result1['level'].'"','item_code').'"','item_name');?></td>
		<td><input type="hidden" name="act_group[]" value="<?php echo $result1['act_group']; ?>"/><?php echo $s->GetFieldValue('sub_activity_group','id="'.$result1['act_group'].'"','group_name');?></td>
		<td><input type="hidden" name="act_name[]" value="<?php echo $result1['act_name']; ?>"/><?php echo $s->GetFieldValue('sub_activity_defination','id="'.$result1['act_name'].'"','activity_code');?></td>
		<td><?php echo $s->GetFieldValue('sub_activity_defination','id="'.$result1['act_name'].'"','activity_name');?></td>
		<td><?php echo $result1['p_qty'];?></td>
		<td><input type="hidden" name="act_sum[]" value="<?php echo $result1['act_sum']; ?>"/><?php echo $result1['act_sum'];?></td>
		<?php 
		$act_per=($result1['act_sum']/$result1['p_qty'])*100;
		?>
		<td>
			<input type="hidden" name="salesorder_id[]" value="<?php echo $result1['salesorder_id']; ?>"/>
			<input type="hidden" name="boq_id[]" value="<?php echo $result1['boq_id']; ?>"/>
			<input type="hidden" name="bom_main_id[]" value="<?php echo $result1['bom_main_id']; ?>"/>
			<input type="text" name="act_per[]" value="<?php echo $act_per; $sum1[]=$act_per;?>" readonly /></td>
		
	</tr>
	<?php } ?>
<tr><td colspan="5">&nbsp;</td><td>Sub Total Percent</td><td><?php echo array_sum($sum1)/$resulttot['mcount']; $stot=array_sum($sum1)/$resulttot['mcount'];?></td></tr>
<?php 
//echo $result1level['mlevel'];
$itemid=$s->GetFieldValue('manufacturing_advice_items','id="'.$result1level['mlevel'].'"','item_code');
$qty=$s->GetFieldValue('manufacturing_advice_items','id="'.$result1level['mlevel'].'"','Qty');
$prate=$s->GetFieldValue('bill_material','part_id="'.$itemid.'"','total_production_rate');
$samt=(($prate*$qty)*$stot)/100;
?>
<tr><td colspan="5">&nbsp;</td><td>Sub Total Amount</td><td><?php echo $samt; $sum[]=$samt;?></td></tr>
<?php
}
 
// }
$b++;
} ?>
  <tr>
  <td colspan="5" height="29"></td>
    <td>Total Amount</td>
    <td><input type="text" name="total_sum" id="total_sum" value="<?php echo array_sum($sum); ?>" readonly/></td>
  </tr>
<tr>
  <td colspan="5" height="29"></td>
    <td>Service Tax</td>
    <td><select name="EDRate" id="exciserate" style="width:150px;" onchange="calculateExcise1(this.value,this.id)">
    		<option value="0">--Select--</option>
    		<?php
				$ServiceSql="select * from service_tax_master WHERE tax_code LIKE '%service%'" ;
				$ServiceRes=DB_query($ServiceSql,$db);
				while($ServiceRow=DB_fetch_array($ServiceRes))
				{
				
				?>
				<option value="<?php echo $ServiceRow['id']?>"<?php echo ($item_row1['EDRate']==$ServiceRow['id']?'selected':''); ?>><?php echo $ServiceRow['tax_code']?></option>
				<?php	
				}
    		
    		?>
    		</select><input type="text" name="service_tax_value" id="service_tax_value" value="" style="width:107px;" readonly="" ></td>
  </tr>
<tr>
  <td colspan="5" height="29"></td>
    <td>E.Cess on ST</td>
    <td><select name="edu_cess" id="edu_cess" onchange="cal_cess(this.value)" style="width:150px;">
	<option value="0">Select</option>
	<?php $sql_edu_cess=DB_query("select id,tax_code from service_tax_master where type='E'",$db);
	while($sql_ex=DB_fetch_array($sql_edu_cess)){ ?>
	<option value="<?php echo $sql_ex['id']; ?>"><?php echo $sql_ex['tax_code']; ?></option>
	<?php }
	?></select><input type="text" name="educess_value" id="educess_value" value="" readonly="" style="width:107px;" ></td>
  </tr>
<tr>
  <td colspan="5" height="29"></td>
    <td>SHE.Cess on ST</td>
    <td><select name="sh_edu" id="sh_edu" onchange="cal_she(this.value)" style="width:150px;">
	<option value="0">Select</option>
	<?php $sql_she_cess=DB_query("select id,tax_code from service_tax_master where type='S'",$db);
	while($sql_ex=DB_fetch_array($sql_she_cess)){ ?>
	<option value="<?php echo $sql_ex['id']; ?>"><?php echo $sql_ex['tax_code']; ?></option>
	<?php }
	?></select><input type="text" name="shecess_value" id="shecess_value" value="" style="width:107px;" readonly="" ></td>
  </tr>
<tr>
  <td colspan="5" height="29"></td>
    <td>Round Off</td>
    <td><input type="text" name="sign" id="sign" value="" style="width:40px;"><input type="text" name="round_off_value" id="round_off_value" value="" style="width:107px;" readonly=""  ><select name="round_ledger" id="" style="width:100px;">
					<?php 
					$roundLedger=DB_query("Select * from accountledger where ledgertype='round'",$db);
					while($roundLedgerRow=DB_fetch_array($roundLedger)){
					?>
					<option value="<?php echo $roundLedgerRow['AccountsID'];?>"><?php echo $roundLedgerRow['AccountName'];?></option><?php 	
				  }
				  ?></select></td>
  </tr>
<tr>
  <td colspan="5" height="29"></td>
    <td>Gross Amount</td>
    <td><input type="text" name="gross_amt" id="gross_amt" value="" style="width:150px;" readonly="readonly">
	<input type="hidden" name="h_gross_amt" id="h_gross_amt" value="" style="width:150px;" ></td>
  </tr>
  <tr>
  <td> Remarks </td>
  <td colspan="4"><textarea name="Remarks"></textarea></td>
   
  <td> Prepared By </td>
  <td><?php
   echo $s->GetFieldValue('www_users',"id='".$_SESSION['UserID']."'",'userid'); ?></td>
  </tr>
  <tr>
    <td colspan="7" style="text-align:center;"><input type="submit" name="savedata" value="save" class="search" onClick="return validates()" /></td>
  </tr>
</table>
<?php } 
 } ?>
</form>
<script>
var doc=null;
function ajax(){
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  doc=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  doc=new ActiveXObject("Microsoft.XMLHTTP");
  }
}

function calculateExcise1(str, id)
    {
        var id_brk = id.split('_');
        var i = id_brk[1];
        if (id_brk.length > 1)
        {
            divid = "excisevalue_" + i;
            var amount = document.getElementById("total_sum_" + i).value;
        }
        else
        {
            divid = "service_tax_value";
            var amount = document.getElementById("total_sum").value;
        }
        if (str.length == 0)
        {
            document.getElementById(divid).value = "";
            return;
        }
        if (window.XMLHttpRequest)
        {
            xmlhttp = new XMLHttpRequest();
        }
        else
        {
            xmlhttp = new ActiveXobject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
            {
                document.getElementById(divid).value = xmlhttp.responseText;

            }

        }
        xmlhttp.open("GET", "calculateExcise.php?type=TELORD&amount=" + amount + "&q=" + str, true);
        xmlhttp.send();
    }
function cal_cess(str){
var ba=document.getElementById('total_sum').value;
var stv=document.getElementById('service_tax_value').value;
ajax();
if(doc){
	doc.onreadystatechange=function()
	{
	if (doc.readyState==4 && doc.status==200)
	{  
	  var data=doc.responseText.split("__");
	  document.getElementById("educess_value").value=data[0];
	  call_toround(data[1]);
	 
	  //document.getElementById("gross_amt").value=data[1];
	  //document.getElementById("h_gross_amt").value=data[1];
	}
	}
	doc.open("GET","check_data_bsm.php?section=cal_educess&tax_id="+str+"&stv="+stv+"&ba="+ba,true);
	doc.send();
	}

}
function call_toround(data){
 var h=data.split(".");
 if(h.length==2){
	    var first_digit=h[1]/10;
		  if(first_digit>=5){
		  var rov=100-parseInt(h[1]);
		  var tot=parseInt(h[0])+1;
		  document.getElementById("sign").value='+';
		  document.getElementById("round_off_value").value='.'+rov;
		  document.getElementById("gross_amt").value=tot;
		   document.getElementById("h_gross_amt").value=tot;
		  }else if(first_digit<5){
		  document.getElementById("sign").value='-';
		  document.getElementById("round_off_value").value='.'+h[1];
		  document.getElementById("gross_amt").value=h[0];
		  document.getElementById("h_gross_amt").value=h[0];
		  }
	  }else{
	  document.getElementById("sign").value='';
	  document.getElementById("round_off_value").value='';
	  document.getElementById("gross_amt").value=data;
	  document.getElementById("h_gross_amt").value=data;
	  }
   
}
function cal_she(str){
var ba=document.getElementById('total_sum').value;
var stv=document.getElementById('service_tax_value').value;
var ecess=document.getElementById('educess_value').value;
ajax();
if(doc){
	doc.onreadystatechange=function()
	{
	if (doc.readyState==4 && doc.status==200)
	{  
	  var data=doc.responseText.split("__");
	  document.getElementById("shecess_value").value=data[0];
	  call_toround(data[1]);
	  //document.getElementById("gross_amt").value=data[1];
	  //document.getElementById("h_gross_amt").value=data[1];
	}
	}
	doc.open("GET","check_data_bsm.php?section=cal_shecess&tax_id="+str+"&stv="+stv+"&ecess="+ecess+"&ba="+ba,true);
	doc.send();
	}

}
</script>
