<?
ob_start();
session_start();
if(isset($_SESSION['ses_name']))
{
$user=$_SESSION['ses_name'];
}else{
header("location:index.php");
}


$veh_num='';
$sdate_12 ='';
$page='1';
include("include/config.php");
require_once "include/Database_functions.php";
$sqlObj=new sqlclass();
require_once "Numbers/Words.php";

$numberToWords = new Numbers_Words();
$msg = '';

if(isset($_REQUEST['del']) && $_REQUEST['del']=='delete')
{
	$id = $_REQUEST['id'];
	$sql=mysql_query("delete from cf_account_hire where trip_id='$id'");
	header("location:view-accountpay.php?msg=You Have Successfully Deleted");
}



if(isset($_POST['generatebill']))
{
	$dis =  $_POST['selecteds'];
	
	$genquery = mysql_query("select gen_dyn_no from cf_genbill");
			if(mysql_num_rows($genquery) == 0)
			{
				$genautono = 1001; 
			}
			else
			{
				$genmax = mysql_query("select max(gen_dyn_no) as gendynno from cf_genbill");
				$maxgen = mysql_fetch_array($genmax);
				$genautono = $maxgen['gendynno']+1;
			}
			
	
	
		
	foreach($dis as $dev_id)
		{
			$query1 = mysql_query("select * from cf_tripsheet where trip_id = '$dev_id'");	
			$rows = mysql_fetch_array($query1);
			
			$clientids = $rows['trip_sub_client_id'];
			$vechno = $rows['trip_vechno'];
			$addrate = '';
			$freightwgh = '';
			$sq_1="select * from cf_tripsheetsub where trip_id = '$dev_id' ";
			$array3=mysql_query($sq_1);
			$arrycnt = mysql_num_rows($array3);
			while($res3=mysql_fetch_array($array3))
			{
				$locationid = $res3['trip_location_id'];
				//echo "select * from cf_rate_act where  client_code = '$clientids' and rate_district_id = '$locationid'";
				$sq_w=mysql_query("select * from cf_rate_act where  client_code = '$clientids' and rate_district_id = '$locationid'");
				$rw4 = mysql_fetch_array($sq_w);
				$rates = $rw4['rate'];
				$addrate+= $rates;
				$res3['grossweight'];
				//echo "<br/>";
				$freightwgh+= $res3['grossweight'] * $rates;
				
			}
			
			$avrrate = $addrate/$arrycnt;
			$roundavgrate = round($avrrate,2);
			$roundfrewgh = round($freightwgh,2);
			
			/*$kms = $rows['kms'];			
			$vechqry = mysql_query("select * from cf_vehicle where vehiclenum = '$vechno'");	
			$vechrw = mysql_fetch_array($vechqry);			
			$ratekm = $vechrw['ratekm'];
			$ratechrg = $kms * $ratekm;*/
			$getinv= explode("-01",$rows['trip_newsrno']);		
			$clientno = $getinv[0].'-03'.$getinv[1];			
			$clientcode = $getinv[0];
			
			$accamt = $rows['client_inv_amt'];
			
			$todays = date('Y-m-d');
			
			$query = mysql_query("select * from cf_receipt where client_type='invoice'");
			if(mysql_num_rows($query) == 0)
			{
				$tripnewautono = 10001; 
			}
			else
			{			
				$cilentmax = mysql_query("select max(dyn_clientcnt) as clientinvoicenum from cf_receipt where client_type='invoice'");
				$maxclient = mysql_fetch_array($cilentmax);
				$tripnewautono = $maxclient['clientinvoicenum']+1;
			}	
			
			
			
			$mnt = date('m');			
			$genbillno = $getinv[0].'-04-'.$genautono;
			
			
			//echo "insert into cf_genbill (gen_dyn_no,gen_billno,gen_billdate,trip_id,tripsheet_no,trip_date,gross_wg,net_wg,rate_chg,freight_amt) values ('$genautono','$genbillno','$todays','$dev_id','".$rows['trip_newsrno']."','".$rows['trip_date']."','".$rows['gross_weight']."','".$rows['net_weight']."','$ratechrg','".$freightwgh."') ";	
			
			/*echo "insert into cf_genbill (gen_dyn_no,gen_billno,gen_billdate,trip_id,tripsheet_no,trip_date,gross_wg,net_wg,rate_chg,freight_amt) values ('$genautono','$genbillno','$todays','$dev_id','".$rows['trip_newsrno']."','".$rows['trip_date']."','".$rows['gross_weight']."','".$rows['net_weight']."','12','".$rows['bal_amt']."') ";*/
			
	//echo "insert into cf_genbill (gen_dyn_no,gen_billno,gen_billdate,trip_id,tripsheet_no,trip_date,gross_wg,net_wg,freight_amt) values ('$genautono','$genbillno','$todays','$dev_id','".$rows['trip_newsrno']."','".$rows['trip_date']."','".$rows['gross_weight']."','".$rows['net_weight']."','".$roundfrewgh."') ";
	//exit;		
	
	$sqls = mysql_query("insert into cf_genbill (gen_dyn_no,gen_billno,gen_billdate,trip_id,tripsheet_no,trip_date,gross_wg,net_wg,rate_chg,freight_amt) values ('$genautono','$genbillno','$todays','$dev_id','".$rows['trip_newsrno']."','".$rows['trip_date']."','".$rows['gross_weight']."','".$rows['net_weight']."','".$roundavgrate."','".$roundfrewgh."') ");
			
			
			$result = mysql_query("update cf_tripsheet set client_inv_status  = '1' where trip_id ='$dev_id'");
			
			$newballetter=$numberToWords->toWords($rows['client_inv_amt']);
			$sqls = mysql_query("insert into cf_receipt (trip_id,client_code,acc_date,acc_amt,amt_letter,acc_status,client_type,receipt_id,dyn_clientcnt) values ('$dev_id','$clientcode','$todays','$accamt','$newballetter','0','invoice','$clientno','$tripnewautono') ");
		
		}
}

?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Supply Chain Management</title>
<link href="css/styles.css" rel="stylesheet"  />
<link href="css/menu.css" rel="stylesheet" type="text/css" />
<link type="text/css" href="css_date/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
<script type='text/javascript' src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<script src="js/popup1.js" type="text/javascript"></script>


<script type='text/javascript' src="js/jquery.autocomplete.js"></script>
<link rel="stylesheet" type="text/css" href="js/jquery.autocomplete.css" />

<script type="text/javascript" src="scripts_date/jquery-calendar-1.7.2.js"></script>
<script>$(document).ready(function(){
$('#tripdate').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});

$(document).ready(function(){
$('#fromdate').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});

$(document).ready(function(){
$('#todate').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});


/*function drivername()
{
	alert(document.frmname.clientid.value);
}*/



</script>
<script language="javascript">
function getXMLHTTP()
	 { //fuction to return the xml http object
	var xmlhttp=false;	
		try
		{
		xmlhttp=new XMLHttpRequest();
		}
		catch(e)	
		{		
			try{			
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
				}
			catch(e)
			{
				try
				{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e1)
				{
				xmlhttp=false;
				}
			}
		}
	return xmlhttp;
	}   

function Searchdate(val)
{ 
 	 var page=val;	
	 var routeid='';
	 var tripdate='';
	 var clientcode='';
	 
	 if(document.trip_frm.client_code){
	 clientcode= document.trip_frm.client_code.value;
	 } 
	 
	 if(document.trip_frm.tripdate){
	 tripdate= document.trip_frm.tripdate.value;
	 }
	 
	 if(document.trip_frm.fromdate){
	 fromdate= document.trip_frm.fromdate.value;
	 }
	 
	 if(document.trip_frm.todate){
	 todate= document.trip_frm.todate.value;
	 }
	 
	 var strURL="ajax/billlist_reportdata.php?&page="+page;
			
		if(clientcode!=''){
		strURL+="&clientcode="+clientcode;
		}
		
		if(tripdate!=''){
		strURL+="&tripdate="+tripdate;
		}
		
		if(fromdate!=''){
		strURL+="&fromdate="+fromdate;
		}
		
		if(todate!=''){
		strURL+="&todate="+todate;
		}
	
	
	//alert(strURL);
	
	var req = getXMLHTTP();
   if (req)
   {
     req.onreadystatechange = function()
     {
		 
			if ((req.readyState == 0)||(req.readyState == 1)||(req.readyState == 2)||(req.readyState == 3))
      		{
			//document.getElementById('order').innerHTML='<div align="center"><img src="'+path+'images/ajax_loader.gif"/></div>';
			//document.getElementById('Cakeorder').innerHTML='<div align="center"><img src="'+path+'images/ajax_loader.gif"/></div>';
			}
		 
		 
      if (req.readyState == 4)
      {
	 // only if "OK"
	 	if (req.status == 200)
	 	{ 
	   document.getElementById('list').innerHTML=req.responseText;
	//	document.getElementById('Cakeorder').innerHTML=req.responseText;
	 	}
	  else 
	 	{
   	   alert("There was a problem while using XMLHTTP:\n" + req.statusText);

	 }
     }
     }
   req.open("GET", strURL, true);
   req.send(null);
   }
 
   
}
</script>

<SCRIPT language="javascript">

$(function(){
 
    // add multiple select / deselect functionality
    $("#selectall").click(function () {
	   $('.case').attr('checked', this.checked);
    });
 
    // if all checkbox are selected, check the selectall checkbox
    // and viceversa
    $(".case").click(function(){
 
        if($(".case").length == $(".case:checked").length) {
            $("#selectall").attr("checked", "checked");
        } else {
            $("#selectall").removeAttr("checked");
        }
 
    });
});
</SCRIPT>


</head>

<body>
<!------------------popup area (view, edit)--------------->
<div class="viewEmployeePopup login-popup"  id="login-box">
<a href="#" class="close"><img src="images/close.png" class="btn_close" title="Close Window" alt="Close" /></a>
   <div id="popupid"></div>
   
     
    </div>


<!---------------popup area ends------------------>
<div class="wrapper">
   <div>
<?php 
	include("header.php");
?>
 
</div>
 
 <div class="tabbedArea">
  <?php if(isset($_REQUEST['msg'])){ ?>
  <div class="warningMessage">
     <p> <?=$_REQUEST['msg']?> </p>
    </div>
    <?php } ?>
  
  <div class="tabBody">
   <h1>Bill List</h1>
   <form name="trip_frm" id="trip_frm" method="post"  >
   <div class="searchBox" style="width:93%; margin-right:0px;  padding:0px; ">
   <div style="padding:20px;">
   
    <div class="searchField">
     <p class="input1">Client Code</p>
       
   <select class="input1 input3" name="client_code">
     <option value="">Client Code</option>
     	 <?php 
        	$sq_clientcd="select * from cf_client";
			$arr2=mysql_query($sq_clientcd);
			while($resclient=mysql_fetch_array($arr2))
			{
		?>
			<option value="<?=$resclient['client_sub_id']?>" ><?=$resclient['client_newcode']?></option>
        <?php
				}
	 ?>
     </select>
     <!--<input type="text" name="client_name" id="client_name" class="input1" />-->
    </div>
   
    <div class="searchField" style="padding-left:20px;">
     <p class="input1">Trip Date</p>
     <input type="text" name="tripsdate" id="tripdate" size="20" maxlength="100" class="input1" />
    </div>
    
    <div class="searchField" style="padding-left:20px;">
     <p class="input1">From Date</p>
     <input type="text" name="fromdate" id="fromdate" size="20" maxlength="100" class="input1" />
    </div>
    
    <div class="searchField" style="padding-left:20px;">
     <p class="input1">To Date</p>
     <input type="text" name="todate" id="todate" size="20" maxlength="100" class="input1" />
    </div>
       
    <div class="searchField">
     <input name="button" type="button" class="input1" onClick="Searchdate(1);" value="search" />
    </div>
    
    
    
    
    
  </div>
   </div></form>
   <form name="payval" id="payval" method="post" action="">   
    
    <div class="searchField" style="padding-left:20px;padding-top:0px; margin-top:0px;">
     <div class="input1" style="float:left;margin-top:25px;margin-left:48px;"><input type="checkbox" id="selectall" /></div>
     <div style="float:right;"><input name="generatebill" type="submit" class="input1" value="Generate Bill"  /></div>
    </div>
    
    
   <div id="list" >
   
  
  
  
  
 
   <table  border="0" cellspacing="0" cellpadding="0" class="tableList" style=" clear:both;">
   <tr> <td colspan="8" style="padding-top:0px;"><div style=" clear:both;">
   
   
   
   
   </div></td></tr>
  <tr class="heading">
  	<td>No</td>
    <td>Tripsheet No</td>
  	<td>Date</td>
    <td>Route Name</td>
    <td>Km</td> 
    <td>Gross Weight</td>
    <td>Net Amount</td>
   </tr>
  
  <?php


/*
if(isset($_REQUEST['typename'])){
$typenames=$_REQUEST['typename'];
}*/

$i=1;

if(($_SESSION['typename'] == 'SuperAdmin')) {
	$sq_arr="select * from cf_tripsheet where trip_status = '1' and client_inv_status = '0' and delete_status = '0'";	
}

if(($_SESSION['typename'] == 'SubAdmin')) {
	$userids = $_SESSION['admin_id'];
	
	/*$sq_clientid=mysql_query("select * from cf_user_clientid where user_id = $userids ");	
	while($rw = mysql_fetch_array($sq_clientid))
	{
		$clientcodeval.= "'".$rw['client_code']."',";
	}
	
	$clientnames = substr($clientcodeval, 0, -1);*/	
	$sq_arr="select * from cf_tripsheet where trip_status = '1' and client_inv_status = '0' and delete_status = '0' and trip_userid = '$userids' "; 	
}

//$sq_arr="select * from cf_tripsheet where advt_amt != '' and trip_status = '1' and client_inv_status = '0'";
$sq_arr.=" order by trip_id desc ";
$query=$sq_arr;
//echo $query;
$function="Searchdate";
include("pagingserch_bill.php");
$sq_arr.=" LIMIT $start, $limit";
$i = 0;
if(isset($_REQUEST['page'])){
$page=$_REQUEST['page'];
}else{
$page=10;
}

$array=mysql_query($sq_arr);
if($array && mysql_num_rows($array)>0){
$pagecnt=$start;
while($res=mysql_fetch_array($array))
{
$pagecnt++;

  ?> 
  <tr>
  	<td><?php echo $pagecnt;?><input type="checkbox" name="selecteds[]" class="case" value="<?=$res['trip_id']?>"></td>
    <?php
		//$getinv= explode("-01",$res['trip_newsrno']);		
		//$clientno = $getinv[0].'-03'.$getinv[1];
	?>
    
    <td><?php echo $res['trip_newsrno'];?></td>
    <td><?php echo date('d-m-Y', strtotime($res['trip_date']));?></td>
  	<?php 
		$routeid = $res['trip_route_id'];
    	$sq_route1="select * from cf_route where route_id = '$routeid' ";
		$array2=mysql_query($sq_route1);
		$res2=mysql_fetch_array($array2);
	?>
  	<td><?php echo $res2['route_name'];?></td>
    <td><?php echo $res['kms'];?></td>
    <td><?php echo $res['gross_weight'];?></td>
    <?php
		
		
		$grosswgh = '';
	
		$tripids = $res['trip_id'];
		$clientids = $res['trip_sub_client_id'];
		$sq_1="select * from cf_tripsheetsub where trip_id = '$tripids' ";
		$array3=mysql_query($sq_1);
		while($res3=mysql_fetch_array($array3))
		{
			$locationid = $res3['trip_location_id'];
			//echo "select * from cf_rate_act where  client_code = '$clientids' and rate_district_id = '$locationid'";
			$sq_w=mysql_query("select * from cf_rate_act where  client_code = '$clientids' and rate_district_id = '$locationid'");
			$rw4 = mysql_fetch_array($sq_w);
			$rates = $rw4['rate'];
			$res3['grossweight'];
			//echo "<br/>";
			$grosswgh+= $res3['grossweight'] * $rates;
			//echo "<br/>";
		}
		//echo $grosswgh;
	?>
    <td><?php  echo round($grosswgh,2);?></td>   
 </tr>
  <?php 
    $i++;} ?>
	
	  <?php }
  ?>
 
 

   
</table>

  </form>
  <div align="right" style="padding-top:10px;"><?=$pagination?></div> 
 </div> 
  </div>
 </div>
 <div class="footer"><p>Powered by <a href="http://www.aadhocc.co.uk/" target="_blank">Aadhocc</a></p></div>
 

<div class="quicLInksCover">
 <?php 
	include("leftlink.php");
?>

</div>
</body>
</html>
