<?php



include("config.inc.php");

include("Function.inc.php");

session_start();

include('GetModulesServices.php');



	


$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");

mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");


$service='delivery';


$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2,ifnull(cashreg_colortheme,'default') as cashreg_colortheme from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}


print <<<HERE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>



<head>
HERE;

if($cashreg_colortheme=='red'){
	echo'<link rel="stylesheet" type="text/css" media="all" href="css-v2/style-red.css">';
	echo'<link rel="stylesheet" type="text/css" href="js/menu-v2/menu-red.css" />';
}	
else if($cashreg_colortheme=='' || $cashreg_colortheme=='default')	{
	echo'<link rel="stylesheet" type="text/css" media="all" href="css-v2/style-default.css">';
	echo'<link rel="stylesheet" type="text/css" href="js/menu-v2/menu-default.css" />';
}


print <<<HERE
<style type="text/css">
::-webkit-scrollbar {width: 20px; height: 4px; background: #4682B4; }
::-webkit-scrollbar-thumb { background-color: #FFFFFF; -webkit-border-radius: 1ex; }
</style>
<link rel="stylesheet" href="Style.css" type="text/css" />

<script type="text/javascript">



function setamount(vobj,vvalue){


var bfr = document.getElementById('topay').innerHTML;
if (vobj.checked == true)
{
	
if (bfr=='')
	bfr = 0;
	

var tot = parseFloat(bfr) + parseFloat(vvalue);
document.getElementById('topay').innerHTML= tot;
}
else
{
if (bfr=='')
	bfr = 0;
	
var tot = parseFloat(bfr) - parseFloat(vvalue);
document.getElementById('topay').innerHTML= tot;
}
}

function closewin(){
self.close();
}

function refresh(){
document.driveraccount.submit();
}

function setallpaid(){
var delname = document.driveraccount.deliveryname.value;

if (confirm ( "Pay All by Driver "+delname))
{
document.driveraccount.sale_id.value=-23;
document.driveraccount.status.value=1;
document.driveraccount.submit();
}
else
document.driveraccount.setall.checked = false;
}

function showaccount(Vdeldriverid){
document.driveraccount.sale_id.value = '';
document.driveraccount.ledger_number.value = Vdeldriverid;
document.driveraccount.action ='Form_DriverAccount.php';
document.driveraccount.submit();
}

function setstatus(vsaleid,vobj){

document.driveraccount.sale_id.value=vsaleid;

if (vobj.checked)
	document.driveraccount.status.value=1;
else	
	document.driveraccount.status.value=0;
	
document.driveraccount.submit();	
	
}

</script>


<style type="text/css">



.hilghlightimg { background: #fff; }
img:hover {
    opacity: 0.5;
    filter: alpha(opacity=50);
	cursor: hand; 
	cursor: pointer;
}




</style>


</head>
<body leftmargin="0px" topmargin="0px" marginwidth="0px" marginheight="0px"   > 
<form method="post" name='driveraccount' action=''>
<input type='hidden' name='ledger_number' value='$ledger_number'>
<input type='hidden' name='sale_id' value='$sale_id'>
<input type='hidden' name='status' value='$status'>



HERE;

echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' ><br>";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px; width:650px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>Drivers Accounts
			</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr><tr height = 400 valign = top><td id='search-content1'></td><td>
	
";

echo '  <table width="950" border="0" cellspacing="1" align="center">';


 
print <<<HERE

<tr><td ></td><td ></td></tr>
<tr><td >
<div style='overflow-x:scroll;width:940px' >
<table width='940px' border=0>
HERE;

	if ($sale_id!=''){
	if ($sale_id !='-23') {
	if ($ledger_number=='')
		$ledger_number='retail.driver_code';
			
			
			$query="update retail set sale_closed = '".$status."' where sale_id = '".$sale_id."' and  driver_code = ".$ledger_number;
			$result=mysql_query($query,$connection) or die(mysql_error());
	
	
	}
	else if ($sale_id =='-23') {
		
	fbegin();
	if ($ledger_number!='')		
		$query="update retail set sale_closed = '".$status."' where driver_code = '".$ledger_number."'";
	
	$result=mysql_query($query,$connection) or die(mysql_error());
		
	 
				if ($jj != 1){
				fcommit();
				} 
								
	}
	}
	if 	($ledger_number == '')
		$query="select sum(ifnull(SALE_NETTOTAL,0)) from retail where driver_code <> '' and sale_type = 'delivery' and ifnull(sale_closed,0) = 0 order by Sale_Date";
	else
		$query="select sum(ifnull(SALE_NETTOTAL,0)) from retail where driver_code = ".$ledger_number." and sale_type = 'delivery' and ifnull(sale_closed,0) = 0 order by Sale_Date";
	$result=mysql_query($query,$connection) or die(mysql_error());
	
	$row=mysql_fetch_array($result);
	if ($row[0]=='')
		$sumamount=0;
	else
		$sumamount =  $row[0];

	$query="select ledger_number,ledger_name,ledger_name from clients where  client_type = 'driver'  ";
	$result=mysql_query($query,$connection) or die(mysql_error());
	echo"<tr   >";
	
	while($row=mysql_fetch_array($result)){
	
	if ($ledger_number==$row[0])
		$drivercolor='#FF0000';
		else
		$drivercolor='#FFFFFF';
	
	echo"<td  width='15'>
	
	<button type='button' id='btnSubmit' name='btnSubmit' onclick='showaccount($row[0])' title=''><span><span>".$row[2]."</span></span></button>
	
	
	</td>";
	}
	
	echo"<td></td>";
	
	echo"</tr>";
	
	echo"<tr><td colspan=10 height='20'></td></tr>";
echo"</table></div>";	
echo"</td>	";

echo"<td valign=top width='15' align=right></td>";
echo"</tr><tr><td colspan=2><div style='width:820px;height:425' ><table width='950' border=0 style='face:Arial;font-size:14px;color:#186194'>";
	
	if  ($group1 == 0 )
		{
			$chk='checked';
			$chk1='';
		}
	else {
			$chk='';
			$chk1='checked';
		}
		
	echo"<tr><td colspan=9 align=right>
			<font color=white><b><span class='style5'>Unpaid</span></b></font><input type='radio' onclick='refresh()' name='group1' value='0' $chk>
			&nbsp;&nbsp;&nbsp;<font color=white><b><span class='style5'>Paid</span></b></font><input type='radio'  onclick='refresh()'  name='group1' value='1' $chk1>
			</td></tr>";

		if  ($group1 == 0 )			
			$inqry=" and ifnull(retail.Sale_closed,0) = 0 ";
		else
			$inqry=" and ifnull(retail.Sale_closed,0) = 1 ";			
	
	if 	($ledger_number == '')
		$query="select sale_id,Sale_Reference,CashRegister_Name,SALE_NETTOTAL,retail.currency_code,ifnull(sale_closed,0),date_format(sale_date,'%h:%i:%s')
					,customer_name ,substr(clients.Ledger_name,1,9)
					from retail left join customer on customer.customer_code=retail.customer_code
					left join clients on clients.ledger_number=retail.driver_code
					where 1 = 1
					and driver_code <> '' 
					and sale_type = 'delivery'
					$inqry 
					order by retail.driver_code,Sale_reference desc";
	else
		$query="select 				  			sale_id,Sale_Reference,CashRegister_Name,SALE_NETTOTAL,retail.currency_code,ifnull(sale_closed,0),date_format(sale_date,'%h:%i:%s'),customer_name 
			from retail left join customer on customer.customer_code=retail.customer_code  
			left join clients on clients.ledger_number = retail.driver_code
			where retail.driver_code = ".$ledger_number."
			and sale_type = 'delivery'
			$inqry order by retail.driver_code,Sale_reference desc";

	$result=mysql_query($query,$connection) or die(mysql_error());

	$i=1;
	$cum=0;
	while($row=mysql_fetch_array($result)){
		
		$ct+=1;

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";		
		
		$curr = $row[4];
		if ($i==1) {
		
			$querydeldriver="select substr(ledger_name,1,9) from clients where ledger_number = '".$ledger_number."'  ";
			$resultdeldriver=mysql_query($querydeldriver,$connection) or die(mysql_error());
			$rowdeldriver=mysql_fetch_array($resultdeldriver);
			
			echo"<input type='hidden' name='deliveryname' value='$rowdeldriver[0]'>";
			
			echo"<tr><td><font color=red><b></b></font></td><td align=center colspan=3></td><td></td>
			<td></td><td>";
			
			//if 	($driver_code != '')
			if ($group1==0)
			echo"<input type='checkbox' name='setall' value='1' onclick='setallpaid()'><span class='style5'><b>All</b></span></font>";
			
			if ($topayvalue=='')
				$topayvalue = '0';
			echo"</td>
			<td align=right><font color=red size=3><b><div id='topay' style='width:40px'>
			$topayvalue
			</div></font></b></td>
			</tr>";
			
			
		
			echo"<tr  bgcolor='#E5E5E5' >
										<td width='50px'   style=' border-bottom: thin solid white;font-size:11px'><b><font color=#000000></tD>
										<td width='50px'   style=' border-bottom: thin solid white;font-size:11px'><b><font color=#000000>Order#</tD>
									   <td width='180px'   style=' border-bottom: thin solid white;font-size:11px'><b><font color=#000000>Customer</td>
									   <td width='80px'   style=' border-bottom: thin solid white;font-size:11px'><b><font color=#000000>Time</td>
									   <td width='60px'   style=' border-bottom: thin solid white;font-size:11px'><b><font color=#000000>CashResgister</td>
										<td width='80px'   style=' border-bottom: thin solid white;font-size:11px'><b><font color=#000000>Total</td>
										<td width='50px'  style=' border-bottom: thin solid white;font-size:11px'><b><font color=#000000>Paid</td>	
										<td width='50px'   style=' border-bottom: thin solid white;font-size:11px'><b><font color=#000000>Calculate Amount</td>	
										<td width='90px'   style=' border-bottom: thin solid white;font-size:11px'><b><font color=#000000>Cumulative Amount</td>	
										
										</tr>";
	
			}
			
		 
		
		$cum = $cum + $row[3];
		echo"<tr  height='20' $Color >";
		if 	($ledger_number == '')
			echo"<td>$row[8]</td>";
		else	
			echo"<td>$rowdeldriver[0]</td>";
		echo"<td ><font   size=3 color=#FAFAD2 ><span class='style5'>".$row[1]."</span></font></tD><td ><font   size=3 color=#FAFAD2><span class='style5'><span class='style5'>".$row[7]."</span></font></td><td ><font color=#FAFAD2><span class='style5'>".$row[6]."</span></font></td><td><font  size=3 color=#FAFAD2><span class='style5'>$row[2]</span></font></td><td align=right ><font   size=3 color=#FAFAD2><b><span class='style5'>".number_format($row[3], $DecimalBase)."</b></font></td><td>
		<input type='checkbox'  onMouseOver=style.cursor='pointer' name='chk$row[0]' ";
			if ($row[5]=='1')
				echo " checked ";
		echo" onclick='setstatus($row[0],this)' ></td>";
		
		echo"<td  ><input type='checkbox' onMouseOver=style.cursor='pointer'  name='calc$row[0]' style='width:70px'  value='$row[3]' onclick='setamount(this,this.value)'   ></td>";
		echo"<td align=right>".number_format($cum, $DecimalBase)."</td>";	
		echo"</tr>";
		//echo"<tr><td colspan=12><hr style='border-style:solid;border-color:#FFFFFF;height:0px'></tD></tr>";
		$i+=1;
	}
	if  ($group1 == 0 )
	echo"<tr><td><font color=red><b></b></font></td><td></td><td></td><td></td><td align=right><span class='style5'>Unpaid $curr</span></td><td align=right><font color=white><b>
			<font color=red>".number_format($sumamount, $DecimalBase)."&nbsp;$row[4]</span></b></font>
			</td></tr>";
echo"</td></tr></table></div>";	
echo"</tr>";
 



print <<<HERE
  </table></td><td id='search-content3' ></td></tr>
  <tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:600px;' valign='top'>
HERE;
	 
		echo"</td>
		<td id='search-bottom3' style='height:34px; width:31px;'>";

 
	
print <<<HERE
&nbsp;</td></tr>
		
   </table>
</form>
</body>
</html>

HERE;




?>