<link rel="stylesheet" href="css/new_mxerp.css">
<?php session_name('CAKEPHP'); session_start();
$PageSecurity = 2;
include('includes/SQL_CommonFunctions.inc');
include('includes/DefinePOClass.php');
$title = _('Job Work Bill Book');
$PageName ='Job Work Bill Book';
if($_REQUEST['Save']!='')
{
		$branch=$_REQUEST['site'];
	 	$prefSql="select prefix_code from prefix_tbl where prefix_name = 'Job Work Bill Book'";
		$resultPrfx = DB_query($prefSql,$db);
		$Prefxrow=DB_Fetch_array($resultPrfx);
		$prefixcode=$Prefxrow['prefix_code'];
		
		 $sql="select billNo from job_billbook where site = '".$_REQUEST['site']."' and CompanyId='".$_SESSION['ACGCompanyId']."' and INSTR(`billNo`, '$financial') > 0 order by billNo DESC LIMIT 0,1";
		$result = DB_query($sql,$db);
		$numrows = DB_num_rows($result);
		if($numrows>0)
		{
			$myrow=DB_Fetch_array($result);
			$ExplBill=explode("/",$myrow['billNo']);
			 $maxval = $ExplBill[3]; 
		}
		else
		{$maxval=0;}
		
		$strChar=$branch."/".$prefixcode."/".$financial."/";
		$strFinal = $maxval+1;
		$strBothFinal = $strChar.str_pad($strFinal,5,"0",STR_PAD_LEFT);
		
		$partyChallan=implode(', ', $_REQUEST['partyChallanNo']);
		$insert1="Insert Into job_billbook set
						site='".$_REQUEST['site']."',
						store='".$_REQUEST['store']."',     
						vendor='".$_REQUEST['vendor']."',  
						billNo='".$strBothFinal."',  
						billDate='".FormatDateForSQL($_REQUEST['billDate'])."',  
						partyChallanNo='".$partyChallan."',  
						billType='".$_REQUEST['billType']."',  
						basicAmount='".$_REQUEST['basicAmount']."',  
						STaxPerc='".$_REQUEST['STaxPerc']."',  
						STaxValue='".$_REQUEST['STaxValue']."',  
						SEduPerc='".$_REQUEST['SEduPerc']."',  
						SEduValue='".$_REQUEST['SEduValue']."',  
						SShePerc='".$_REQUEST['SShePerc']."',  
						SSheValue='".$_REQUEST['SSheValue']."',  
						TDSPerc='".$_REQUEST['TDSPerc']."',  
						TDSValue='".$_REQUEST['TDSValue']."',  
						freight='".$_REQUEST['freight']."',  
						grandTotal='".$_REQUEST['grandTotal']."',  
						remarks='".$_REQUEST['remarks']."',  
						ledger='".$_REQUEST['ledger']."', 
						
						
						tds_ECess_rate='".$_REQUEST['tds_ECess_rate']."', 
						tds_ECess_value='".$_REQUEST['tds_ECess_value']."', 
						tds_SHECess_rate='".$_REQUEST['tds_SHECess_rate']."', 
						tds_SHECess_value='".$_REQUEST['tds_SHECess_value']."', 
						 
						createdBy='".$_REQUEST['UserID']."',  
						createdDate='".FormatDateForSQL(date('d/m/Y'))."',
						CompanyId='".$_SESSION['ACGCompanyId']."' ";
						
	$result1=DB_query($insert1,$db);
	$lastId=DB_Last_Insert_ID($db,'job_billbook','id');	
	if($lastId!='')
	{
		for($c=0;$c<$_REQUEST['countItems'];$c++)
		{
			$insert2="Insert into job_billbook_details set 
									masterId='".$lastId."',  
									mst_itemid='".$_REQUEST['mst_itemid'][$c]."',    
									receivedQty='".$_REQUEST['receivedQty'][$c]."',    
									rate='".$_REQUEST['rate'][$c]."',
									process='".$_REQUEST['process'][$c]."',  
									totalAmount='".$_REQUEST['totalAmount'][$c]."'";
			$result2=DB_query($insert2,$db);
		}
	}
	
	if($lastId!='')
	{
		for($k=0;$k<count($_REQUEST['partyChallanNo']);$k++)
		{
			$updateSQL=DB_query("Update job_work_return set billStatus='closed' where id='".$_REQUEST['partyChallanNo'][$k]."'",$db);
		}
	}
echo "<script>location.href='index.php?route=JobWorkBillBookList&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
unset($_REQUEST);
}
?>
<script language="javascript" type="text/javascript">
function partyChallans(str)
{
	var site=document.getElementById("site").value;
	if (str.length==0)
  	{
  	document.getElementById("ChallanList").innerHTML="";
  	return;
 	 }
	if (window.XMLHttpRequest)
 	 {// code for IE7+, Firefox, Chrome, Opera, Safari
 	 xmlhttp=new XMLHttpRequest();
 	 }
	else
  	{// code for IE6, IE5
  	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	xmlhttp.onreadystatechange=function()
  	{
  	if (xmlhttp.readyState==4 && xmlhttp.status==200)
   	 {
	 var data=xmlhttp.responseText.split("__");	

    document.getElementById("ChallanList").innerHTML=data[0];
	document.getElementById("TDSPerc").value=data[1];
	document.getElementById("tds_ECess_rate").value=data[2];
	document.getElementById("tds_SHECess_rate").value=data[3];
   	 }
  	}
xmlhttp.open("GET","JobPartyChallan.php?vendor="+str+"&branch="+site,true);
xmlhttp.send();
}
function generateBillNumber(str)
{
	if (str.length==0)
  	{
  	document.getElementById("store").innerHTML="";
	document.getElementById("vendor").innerHTML="";
  	return;
 	 }
	if (window.XMLHttpRequest)
 	 {// code for IE7+, Firefox, Chrome, Opera, Safari
 	 xmlhttp=new XMLHttpRequest();
 	 }
	else
  	{// code for IE6, IE5
  	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	xmlhttp.onreadystatechange=function()
  	{
  	if (xmlhttp.readyState==4 && xmlhttp.status==200)
   	 {
    	var data=xmlhttp.responseText.split("__");	
	
		document.getElementById("store").innerHTML=data[0];
		document.getElementById("vendor").innerHTML=data[1];
   	 }
  	}
xmlhttp.open("GET","JobBookNo.php?branch="+str,true);
xmlhttp.send();
}
function returnType(str)
{
	if(str=='Service')
	{
		document.getElementById("TDSPerc").disabled=true;
		document.getElementById("TDSValue").disabled=true;
		document.getElementById("STaxPerc").disabled=false;
		document.getElementById("STaxValue").disabled=false;
		document.getElementById("SEduPerc").disabled=false;
		document.getElementById("SEduValue").disabled=false;
		document.getElementById("SShePerc").disabled=false;
		document.getElementById("SSheValue").disabled=false;
	}
	else if(str=='JW')
	{
		document.getElementById("STaxPerc").disabled=true;
		document.getElementById("STaxValue").disabled=true;
		document.getElementById("SEduPerc").disabled=true;
		document.getElementById("SEduValue").disabled=true;
		document.getElementById("SShePerc").disabled=true;
		document.getElementById("SSheValue").disabled=true;
		document.getElementById("TDSPerc").disabled=false;
		document.getElementById("TDSValue").disabled=false;
	}
}
function calculateTotalAmt(a)
{
	var rate=document.getElementById("rate_"+a).value;
	var receivedQty=document.getElementById("receivedQty_"+a).value;
	var TOT=parseFloat(receivedQty)*parseFloat(rate);
	document.getElementById("totalAmount_"+a).value=TOT.toFixed(2);
}
function calculateBasic()
{
	var basicAmts=document.getElementById("totBasicAmt").value;
	if(basicAmts=='') {basicAmts=0;}
	document.getElementById("basicAmount").value=basicAmts;

	//var cnt=document.getElementById("countItems").value;
//	var q=0;
//	for(var str=0;str<cnt;str++)
//	{
//		var amount=document.getElementById("totalAmount_"+str).value;
//		q=parseFloat(q)+parseFloat(amount);
//	}
//	document.getElementById("basicAmount").value=q.toFixed(2);
}

function calculateServiceTax()
{
	var spercentage=document.getElementById("STaxPerc").value;
	var BasicAmt=document.getElementById("basicAmount").value;
	var totval=BasicAmt*(spercentage/100);
	document.getElementById("STaxValue").value=totval.toFixed(2);
}
function calculateSEdu()
{
	var STaxValue=document.getElementById("STaxValue").value;
	var SEduPerc=document.getElementById("SEduPerc").value;
	var totalval2=STaxValue*(SEduPerc/100);
	document.getElementById("SEduValue").value=totalval2.toFixed(2);
}
function calculateSShe()
{
	var STaxValue=document.getElementById("STaxValue").value;
	var SShePerc=document.getElementById("SShePerc").value;
	var totalval3=STaxValue*(SShePerc/100);
	document.getElementById("SSheValue").value=totalval3.toFixed(2);
}
function calculateTDS()
{
	var TDSPerc=document.getElementById("TDSPerc").value;
	var basicamt=document.getElementById("basicAmount").value;
	var totalval4=basicamt*(TDSPerc/100);
	document.getElementById("TDSValue").value=totalval4.toFixed(2);
}	
function calculateGrandTotal()
{
	var basicAmount=document.getElementById("basicAmount").value;
	var STax=document.getElementById("STaxValue").value;
	var SEdu=document.getElementById("SEduValue").value;
	var SShe=document.getElementById("SSheValue").value;
	var TDS=document.getElementById("TDSValue").value;
	var freight=document.getElementById("freight").value;
	if(basicAmount=='')basicAmount=0;
	if(STax=='')STax=0;
	if(SEdu=='')SEdu=0;
	if(SShe=='')SShe=0;
	if(TDS=='')TDS=0;
	if(freight=='')freight=0;
	var TOTAL=parseFloat(basicAmount)+parseFloat(STax)+parseFloat(SEdu)+parseFloat(SShe)-parseFloat(TDS)+parseFloat(freight);
	document.getElementById("grandTotal").value=TOTAL.toFixed(2);
}
function getURL(str)
{
	location.replace("index.php?route=JobWorkBillBook&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&type="+str);
}
function open_items()
{
	window.open("JobBillItems.php?action=add&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&type=<?php echo $_REQUEST['type'];?>","item_popup_window","height=500,width=900,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}
</script>

<div class="body_contain">
<div class="tab_div">

<h1>Job Work/Service Bill</h1>

<form action="index.php?route=JobWorkBillBook&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>" method="post">
<table width="99%" border="1" align="center" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <tr>
    <td class="tabledata3">Branch</td>
    <td style="width:250px;">
	<?php
	if($_SESSION['AssignedBranch']!='')
	  {	  	
  $ss="Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in ('".$_SESSION['AssignedBranch']."')";
	  	 $site_sql=DB_query($ss,$db);
	  }
	  else
	  {
		$ss="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$site_sql=DB_query($ss,$db);
	  }
	?>
	
	<select name="site" id="site" style="width:100%" onchange="generateBillNumber(this.value)">
      <option value="0">Select</option>
      <?php 
					while($Row=DB_fetch_array($site_sql)){?>
             <option value="<?php echo $Row['loccode']?>"<?php echo($_SESSION['Site']==$Row['loccode'] ? 'selected':'');?> ><?php echo $Row['locationname']?></option>
                                <?php  } ?>
    </select></td>
    <td rowspan="4" colspan="2" id="partychallanId"><?php 
	$query="Select * from job_work_return where site='".$_REQUEST['site']."' and vendor='".$_REQUEST['vendor']."'";
$result=DB_query($query,$db);
?>
<table width="100%">
	<tr>
	<td width="25%" class="tabledata3">Party Challan No. </td>
	<td>
	<div style="overflow:auto; height:100px; width:auto; border: ridge;" id="ChallanList" ><!--1px solid; -->
	
	</div>
	</td>
	</tr>
	</table>
	</td>
  </tr>
  <tr><td class="tabledata3">Godown</td>
    <td><?php
	     $sql_store ='select distinct StoreCode, StoreName from storetype';
		 $resultStkLocs = DB_query($sql_store,$db);
		?>
      <select name="store" id="store" style="width:100%">
        <option value="0">Select</option>
      </select></td></tr>
  <tr>
    <td class="tabledata3">Vendor</td>
    <td><select name="vendor" id="vendor" style="width:100%" onChange="partyChallans(this.value)">
        <option name="0">Select</option>
    </select></td>
  </tr>
  <tr>
    <td class="tabledata3">Bill Book No. </td>
    <td colspan="0" id="booknoId"><input name="billPrefix" type="text" id="billPrefix" value="Auto Generated" style="width:80%" > 
   </td></tr>
  <tr>
    <td class="tabledata3">Bill Date</td>
    <?php if(isset($_REQUEST['billDate'])){$billDate=$_REQUEST['billDate'];} else {$billDate=date('d/m/Y');}?>
    <td ><input type="text" name="billDate" id="billDate" style="width:100%" value="<?php echo $billDate;?>" readonly class="datepicker"></td>
	
    <td class="tabledata3">Bill Type </td>
    <td><input type="radio" name="billType" value="Service" id="billType" onclick="returnType(this.value)" />
    <strong>Service </strong>
    <input type="radio" name="billType" value="JW" id="billType" onclick="returnType(this.value)"/>
    <strong> Job Work </strong></td>
	</tr>
</table>
<p align="center">



<div style="overflow:auto; width:810px; height:auto;" id="showItems"> 
</div>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="50%" valign="top"><table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
      <tr>
        <td class="tabledata3" scope="row">Remarks</td>
        <td><textarea name="remarks" style="width:100%" ></textarea></td>
      </tr>
      <tr>
        <td class="tabledata3" scope="row">Ledger</td>
        <td>
		<select name="ledger" id="ledger" style="width:100%">
		<?php 
		$ledger_sql=DB_query("Select AccountsID,AccountName from accountledger",$db);
		while($ledger_row=DB_fetch_array($ledger_sql))
		{
		?>
		<option value="<?php echo $ledger_row['AccountsID']; ?>"><?php echo $ledger_row['AccountName']; ?></option>
		<?php } ?>
		</select>
		</td>
      </tr>
    </table></td>
    <td width="27%">
	<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <tr>
    <td class="tabledata3">Basic Amount</td>
	
    <td><input type="text" name="basicAmount" id="basicAmount" value="<?php echo $totalBasic; ?>" readonly="true" onfocus="calculateBasic()" /></td>
  </tr>
  <tr>
    <td class="tabledata3">Service Tax </td>
    <td>
	<select name="STaxPerc" id="STaxPerc" onchange="cal_tax(this.value)">
	<?php 
	$sql_tax_service=DB_query("select id,tax_code from service_tax_master where type='ST'",$db);
	while($row1=DB_fetch_array($sql_tax_service))
	{
	?>
	<option value="<?php echo $row1['id']; ?>"><?php echo $row1['tax_code']; ?></option>
	<?php } ?>
	
	</select>
<input name="STaxValue" type="text" id="STaxValue" value="<?php echo $_REQUEST['STaxValue'];?>" size="14" readonly/></td>
  </tr>
  <tr>
    <td class="tabledata3">S. Edu Tax </td>
    <td><select name="SEduPerc" id="SEduPerc" onchange="cal_cess(this.value)">
	<?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 name="SEduValue" type="text" id="SEduValue" value="<?php echo $_REQUEST['SEduValue'];?>" size="14" onfocus="calculateSEdu()" readonly/></td>
  </tr>
  <tr>
    <td class="tabledata3">S. She Tax </td>
    <td><select name="SShePerc" id="SShePerc" onchange="cal_she(this.value)">
	<?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 name="SSheValue" type="text" id="SSheValue" value="<?php echo $_REQUEST['SSheValue'];?>" size="14" onfocus="calculateSShe()" readonly/></td>
  </tr>
  <tr>
    <td class="tabledata3">Tds</td>
    <td><input name="TDSPerc" type="text" id="TDSPerc" value="<?php echo $_REQUEST['TDSPerc'];?>" size="3" readonly/><input name="TDSValue" type="text" id="TDSValue" value="<?php echo $_REQUEST['TDSValue'];?>" size="14" onfocus="calculateTDS()" readonly/></td>
  </tr>
  
  <tr>
    <td class="tabledata3">E. Cess Rate</td>
    <td><input name="tds_ECess_rate" type="text" id="tds_ECess_rate" value="<?php echo $_REQUEST['tds_ECess_rate'];?>" size="3" readonly/><input name="tds_ECess_value" type="text" id="tds_ECess_value" value="<?php echo $_REQUEST['TDSValue'];?>" size="14" onfocus="calculateTDS()" readonly/></td>
  </tr>
  
  <tr>
    <td class="tabledata3">SHE. Cess Rate</td>
    <td><input name="tds_SHECess_rate" type="text" id="tds_SHECess_rate" value="<?php echo $_REQUEST['TDSPerc'];?>" size="3" readonly/><input name="tds_SHECess_value" type="text" id="tds_SHECess_value" value="<?php echo $_REQUEST['TDSValue'];?>" size="14" onfocus="calculateTDS()" readonly/></td>
  </tr>
  
  <tr>
    <td class="tabledata3">Freight</td>
    <td><input type="text" name="freight" id="freight" value="<?php echo $_REQUEST['freight'];?>" autocomplete="off" onkeyup="calculateGrandTotal()"/></td>
  </tr>
  <tr>
    <td class="tabledata3">Grand Total </td>
    <td><input type="text" name="grandTotal" id="grandTotal" value="<?php echo $_REQUEST['grandTotal'];?>" onfocus="calculateGrandTotal()" readonly/></td>
  </tr>
</table>
	</td>
  </tr>
</table>
<p align="center"><input type="submit" name="Save" value="Save" /></p>
</form>

</div></div>
<script language="javascript" type="text/javascript">
function cal_tax(str){

var ba=document.getElementById('basicAmount').value;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	var data=xmlhttp.responseText.split('__');
    document.getElementById("STaxValue").value=data[0];
	calculateGrandTotal();
    }

  }
xmlhttp.open("GET","check_data_bsm.php?section=cal_servicetax&tax_id="+str+"&ba="+ba,true);
xmlhttp.send();
}

function getReturn()
{
	var tdspercentage=document.getElementById("TDSPerc").value;
	var ecesPercentage=document.getElementById("tds_ECess_rate").value;
	var secerssPercentage=document.getElementById("tds_SHECess_rate").value;
	var count=document.getElementById("countGroup").value;
	var sub_cat_array=Array();
	for(var i=0;i<count;i++)
	{
		var item_sub_category=document.getElementById("partyChallanNo_"+i).value;
		//alert(item_sub_category);
		if(document.getElementById("partyChallanNo_"+i).checked==true)
		{
			sub_cat_array[i]=item_sub_category;
		}
	}
	
	if (window.XMLHttpRequest)
  	{// code for IE7+, Firefox, Chrome, Opera, Safari
  		xmlhttp=new XMLHttpRequest();
  	}
	else
  	{// code for IE6, IE5
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
		xmlhttp.onreadystatechange=function()
  	{
  	if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
		var data=xmlhttp.responseText.split('__');
    	document.getElementById("showItems").innerHTML=data[0];
		document.getElementById("basicAmount").value=data[1];
		document.getElementById("STaxValue").value=data[2];
		document.getElementById("SEduValue").value=data[3];
		document.getElementById("SSheValue").value=data[4];
		document.getElementById("grandTotal").value=data[5];
		document.getElementById("TDSValue").value=data[6];
		document.getElementById("tds_ECess_value").value=data[7];
		document.getElementById("tds_SHECess_value").value=data[8];
    }
  }
xmlhttp.open("GET","ajax_get_items.php?q="+sub_cat_array+"&tdsPerc="+tdspercentage+"&ecesPerc="+ecesPercentage+"&secesPerc="+secerssPercentage,true);
xmlhttp.send();
}

function cal_cess(str){
var ba=document.getElementById('basicAmount').value;
var stv=document.getElementById('STaxValue').value;

if (str.length==0)
  {
  document.getElementById("SEduValue").value="";
  return;
  }

if (window.XMLHttpRequest)

  {// code for IE7+, Firefox, Chrome, Opera, Safari

  xmlhttp=new XMLHttpRequest();

  }

else

  {// code for IE6, IE5

  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

  }

xmlhttp.onreadystatechange=function()

  {

  if (xmlhttp.readyState==4 && xmlhttp.status==200)

    {
	var data=xmlhttp.responseText.split('__');
    document.getElementById("SEduValue").value=data[0];
	calculateGrandTotal();
    }

  }

xmlhttp.open("GET","check_data_bsm.php?section=cal_educess&tax_id="+str+"&stv="+stv+"&ba="+ba,true);

xmlhttp.send();
}

function cal_she(str){
var ba=document.getElementById('basicAmount').value;
var stv=document.getElementById('STaxValue').value;
var ecess=document.getElementById('SEduValue').value;
if (str.length==0)
  {
  document.getElementById("SSheValue").value="";
  return;
  }

if (window.XMLHttpRequest)

  {// code for IE7+, Firefox, Chrome, Opera, Safari

  xmlhttp=new XMLHttpRequest();

  }

else

  {// code for IE6, IE5

  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

  }

xmlhttp.onreadystatechange=function()

  {

  if (xmlhttp.readyState==4 && xmlhttp.status==200)

    {
	var data=xmlhttp.responseText.split('__');
    document.getElementById("SSheValue").value=data[0];
	calculateGrandTotal();
    }

  }

xmlhttp.open("GET","check_data_bsm.php?section=cal_shecess&tax_id="+str+"&stv="+stv+"&ecess="+ecess+"&ba="+ba,true);

xmlhttp.send();


}
</script>