<?
session_start();
$brcode = $_SESSION['brcode'];
$user = $_SESSION['user'];
if(strlen($brcode)<=0)
{
 	print "<script> alert('Invalid Login'); parent.document.location='ind.php' </script>";
 	return;
}
include("../opendb.php");
include("../test.php");
$d = $_REQUEST['d'];
$m = $_REQUEST['m'];
$y = $_REQUEST['y'];
$d1 = $_REQUEST['d1'];
$m1 = $_REQUEST['m1'];
$y1 = $_REQUEST['y1'];
$df=$y.'-'.$m.'-'.$d;
//$dt=date('Y-m-d');
$dt=$y1."-".$m1."-".$d1;
$ltbl="ledger".$brcode.$user;
$jtbl="journal2".$brcode.$user;
mysqli_query($db,"drop TABLE $ltbl");
mysqli_query($db,"drop TABLE $jtbl");
mysqli_query($db,"CREATE TABLE $ltbl select * from gen4psnauto_psn_ampleicher2223.ledger  where brcode='$brcode'");
mysqli_query($db,"insert into $ltbl (select * from ledger  where brcode='$brcode' and led not in(select led from $ltbl where brcode='$brcode'))") or die(mysqli_error($db));
mysqli_query($db,"CREATE TABLE $jtbl select * from gen4psnauto_psn_ampleicher2223.journal2 where brcode='$brcode'");
mysqli_query($db,"insert into $jtbl select * from journal2 where brcode='$brcode' and date <= '$dt'");
mysqli_query($db,"CREATE INDEX partic ON $jtbl (partic, date)");
?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<script>
   function checkAll(ele) {
     var checkboxes = document.getElementsByTagName('input');
     if (ele.checked) {
         for (var i = 0; i < checkboxes.length; i++) {
             if (checkboxes[i].type == 'checkbox') {
                 checkboxes[i].checked = true;
             }
         }
     } else {
         for (var i = 0; i < checkboxes.length; i++) {
             console.log(i)
             if (checkboxes[i].type == 'checkbox') {
                 checkboxes[i].checked = false;
             }
         }
     }
 }
</script>
<style>
body,td{font-family:arial narrow;font-size:14;background-color:white}
th{background-color:silver};
</style>
<center>
<? 
print "<b>".$branch."</b>";
print "<br> Debtors aging list as on ".$dt."<br>[printed by $user]";
?>
 <form id="category" name="category" method="get" action="sendledgermail.php">
     <?
print "<table border=0 cellspacing=1 cellpadding=3 bgcolor='gray'>";
print "<th>No. <th>Debtor<th>Phone<th>Total";
?>

<th><INPUT type="checkbox" onchange="checkAll(this)" name="chk[]" /> </th>

<?
$i=1;
$j=1;
$t_bal=0;
$t_30=0;
$t_60=0;
$t_90=0;
$t_120=0;
$t_120more=0;
$re=mysqli_query($db,"select distinct led,grp,op,drcr from $ltbl where grp='Sundry Debtors' and brcode='$brcode' order by led")or die(mysqli_error($db));
while($srow=mysqli_fetch_array($re))
{
    $sendsql=mysqli_query($db,"select asondate,datetime,lastaccessed from accledgermail where acname='$srow[led]' and asondate='$dt' and brcode = '$brcode' order by id desc");
    $sendnum=mysqli_num_rows($sendsql);
    $sendrw=mysqli_fetch_array($sendsql);
    if($srow['drcr']=='Cr')
 	    $opbal=$srow['op']*-1;
    else
        $opbal=$srow['op'];
    
 	$sql="select sum(dramt)-sum(cramt) from $jtbl where partic='$srow[led]' and brcode='$brcode'";
 	$balres=mysqli_query($db,$sql)or die('err1'.mysqli_error($db));
  	$balrow=mysqli_fetch_row($balres)or die('err2'.mysqli_error($db));
 	$balance=$opbal+$balrow[0];
 	
	if($balance>0)
	{
		$t_bal+=$balance;
		$sql11="select corp,acname,email from customer where acname='$srow[led]'"; 
		$res111=mysqli_query($db,$sql11);
  		$row11=mysqli_fetch_array($res111);
   		$mcorp=$row11[2];
   		if(strlen($row11[1])<=1)
   		{
   		    $code = substr($brcode,0,3).rand();
   		    //$sql111="insert into customer(brcode,code,cname,acname) values ('$brcode','$code','$srow[led]','$srow[led]')"; 
   		    //print $sql111;
		    //mysqli_query($db,$sql111);
   		}
   		print "<tr><td>".$i++."</td><td>$srow[led]</td><td>$mcorp</td><td align=right>".putComa(round($balance,2))."</td></td><td align=right>";
   		if($sendnum>0)
  		{
  		    $msendt = implode('-', array_reverse(explode('-', substr($sendrw['datetime'],0,10))));
  		    echo $msendt." ".substr($sendrw['datetime'],11,10);
  		    
  		}else {
  		print "<input type='checkbox' name='chkled[]' class='checkBoxClass' id='Checkbox$j' value='$row11[1]'  />";
  		}
  		$mlastacc = implode('-', array_reverse(explode('-', substr($sendrw['lastaccessed'],0,10))));
  		$mlastacc.=" ".substr($sendrw['lastaccessed'],11,10);
  		print "</td><td>$mlastacc</td></tr>";
  		$j++;
	}
}
print "<tr><td colspan=3 align=center>Total</td><td align=right>".putComa($t_bal)."</td><td align=right>".putComa($t_15)."<input type='hidden' name='asondt' id='asondt' value='$dt'></td></tr>";
if(strtolower($user)=='joy'||strtolower($user)=='amal'||strtolower($user)=='midhun'){
?>
    <tr><td colspan=5 align="right"><input type="submit" value="submit"></td></tr>
<?}?>
</table>
