<?php
session_start();
if (!isset($_SESSION['reusername'])) { echo 'Sorry You Dont Have permissions to View this Page!'; exit();}
require_once 'includes/database.php';
require_once 'includes/display.php';
require_once 'includes/funcs.php';
echo $htmlheaders;  echo '<head>';
echo $js;
echo $recss;
echo '<style>body{overflow-y:scroll;}</style>';
echo '</head><body>';
$user = $_SESSION['reusername'];

$supp = $_REQUEST['supp'];		$cus = $_REQUEST['cus'];	
if($cus === ""){ $cust = "All Customers";}	
else {$cust = $_REQUEST['cus'];}	 
$ed = substr($_REQUEST['e'],6,4)."-".substr($_REQUEST['e'],3,2)."-".substr($_REQUEST['e'],0,2);
$sd = substr($_REQUEST['s'],6,4)."-".substr($_REQUEST['s'],3,2)."-".substr($_REQUEST['s'],0,2);

    echo '<p><a $href="#" id="printnow" class="printnow" onclick="myprint();" style="float:left;">Print</a>';
    echo '<a href="#" id="closenow" class="closenow" onclick="window.close();">Close</a></p>';
	echo '<div id="printblk">';	
    echo '<table id="treport" style="width:90%;margin-left:30px;">
    <tr><td colspan="7"><center><span style="font-size:18px;font-weight:normal;float:right;"> <b>'.getsupplier($supp).' ACCOUNT</b></span><span style="font-size:22px;float:left;font-weight:bold"> QOOLPOINT LTD</span></center></td></tr>
    <tr><td colspan="7"><span style="font-size:16px;font-weight:bold;"><center>'. $cust.'</center></span><br/>
	<br/><span style="font-size:16px;font-weight:bold;"><center>'. str_repeat('&nbsp;',15).'Details From: &nbsp;'.$_REQUEST['s'].' &nbsp; To: &nbsp;'.$_REQUEST['e'].'</center></span></td></tr>';
    echo'<tr><u><th>S/no</th> <th>Date</th><th>Doc. No.</th><th>Naration</th> <th>Debit</th> <th>Credit</th> <th>Balance</th></u></tr>'; 
    echo '<tr><td colspan="7"><hr/></td></tr>'; 
	$tot = 0;
	$db=new MySQLDatabase();
	$db->open_connection();
																	// get BF balance
	if($cus === ""){ $query = "SELECT 'B/F Balance' as nar,  SUM(tt.ram) as bfam FROM ( SELECT SUM(amount) as ram FROM paytrans WHERE supp = '$supp' AND pdate < '$sd' UNION SELECT IF(invoicecredit=1, SUM(-1*amount), SUM(amount)) as ram FROM insbills WHERE supp = '$supp' AND idate < '$sd') as tt"; }
	else { $query = "SELECT 'B/F Balance' as nar,  SUM(tt.ram) as bfam FROM ( SELECT SUM(insbills.paidamount) as ram FROM paytrans, insbills WHERE insbills.payno = paytrans.payno AND insbills.cname = '$cus' AND paytrans.pdate < '$sd' UNION SELECT IF(invoicecredit=1, SUM(-1*amount), SUM(amount)) as ram FROM insbills WHERE insbills.cname = '$cus' AND idate < '$sd') as tt";}
	
	$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){		  
		$tot = $tot + floatval($row['bfam']);
        echo '<tr><td colspan="3"></td> <td colspan="3">'.$row['nar'].'</td><td align="right">'.number_format($tot,0).'</td></tr>'; 
	 }	
																	// get invoices & payments
	if($cus === ""){ $query = "SELECT payno, pdate, CONCAT(pmode, '-', chqno, ' - ', chqdet, ' PAYMENT -', rmks) as nar, IF(amount>0,amount,0) as dam, IF(amount<0,-1*amount,0) as cam FROM paytrans WHERE supp = '$supp' AND pdate >= '$sd' AND pdate <= '$ed' 
              UNION SELECT CONCAT(insbills.billno, '- BK.No.',insbills.insno) as payno, insbills.idate as pdate, CONCAT(IF(invoicecredit=1,'INVOICE - ','CREDIT NOTE - ' ),insrecs.regno,' - ',insrecs.vehmod,' - ',insrecs.insurer), IF(insbills.invoicecredit=0,insbills.amount,0), IF(insbills.invoicecredit=1,insbills.amount,0) FROM insbills, insrecs WHERE insbills.supp = '$supp' AND insbills.insno = insrecs.insno AND insbills.idate >= '$sd' AND insbills.idate <= '$ed' ORDER BY pdate, payno"; }
	else { $query = "SELECT paytrans.payno, paytrans.pdate, CONCAT(paytrans.pmode, '-', paytrans.chqno, ' - ', paytrans.chqdet, ' PAYMENT -', paytrans.rmks) as nar, IF(insbills.paidamount>0,insbills.paidamount,0) as dam, IF(insbills.paidamount<0,-1*insbills.paidamount,0) as cam FROM paytrans, insbills WHERE paytrans.supp = '$supp' AND paytrans.payno = insbills.payno AND insbills.cname = '$cus' AND paytrans.pdate >= '$sd' AND paytrans.pdate <= '$ed' 
              UNION SELECT CONCAT(insbills.billno, '- BK.No.',insbills.insno) as payno, insbills.idate as pdate, CONCAT(IF(invoicecredit=1,'INVOICE - ','CREDIT NOTE - ' ),insrecs.regno,' - ',insrecs.vehmod,' - ',insrecs.insurer), IF(insbills.invoicecredit=0,insbills.amount,0), IF(insbills.invoicecredit=1,insbills.amount,0) FROM insbills, insrecs WHERE insbills.supp = '$supp' AND insbills.insno = insrecs.insno AND insbills.cname = '$cus' AND insbills.idate >= '$sd' AND insbills.idate <= '$ed' ORDER BY pdate, payno";
			}
	$dtot = 0;  $ctot = 0;  $i = 1;
    $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
        $dtot = $dtot + floatval($row['dam']);
        $ctot = $ctot + floatval($row['cam']);
        $tot =  $tot + floatval($row['dam']) - floatval($row['cam']);
        $pdate = substr($row['pdate'],8,2)."-".substr($row['pdate'],5,2)."-".substr($row['pdate'] ,0,4);
        echo '<tr><td>'.$i.'</td><td>'.$pdate.'</td><td style="width:110px;">'.$row['payno'].'</td><td>'.$row['nar'].'</td><td align="right">'.number_format($row['dam'],0).'</td><td align="right">'.number_format($row['cam'],0).'</td><td align="right">'.number_format($tot,0).'</td></tr>';      
        $i++;
    }
    echo '<tr><td colspan="7"><hr/></td></tr>';   
    echo '<tr><td colspan="4"></td><td align="right">'.number_format($dtot,0).'</td><td align="right">'.number_format($ctot,0).'</td><td align="right">'.number_format($tot,0).'</td></tr>';  
    echo '<tr><td colspan="7"><hr/></td></tr>';  
    echo '</table>'; 
    echo '<div id="report">Please check your balance and bring any discrepancies within 15 days</div> <br /><br />';
    echo '<span align="left" style="font-size:x-small">Printed by: '.$user.' &nbsp;&nbsp; '.date("d-m-Y H:i:s"). '</span> </div> <br />';
    echo '<div style="page-break-after:always"> </div>';

$db->close_connection();
echo '</body></html>';
?>
<script type="text/javascript">
function myprint(){
	var w = window.open('', '', 'width=800,height=600,resizeable,scrollbars');
	 w.document.write($("#printblk").html());
	 w.document.close(); 										// needed for chrome and safari
	 javascript:w.print();
	 w.close();
};	
</script>