<?php
include "head.php";
include "dbconfig.php";
//include "css_header.php";
//Get the order data for this order.
$id=$_REQUEST['id'];
$sql="SELECT * from transactions WHERE source_id='$id'";
$res=mysqli_query($con,$sql);
$p_sql="
SELECT * from passbook";
$p_res=mysqli_query($con,$p_sql);
$passbook=mysqli_fetch_assoc($p_res);
$current_balance=$passbook['current_bal'];
$d_sql="
SELECT id,name,SUM(balance) as total from creditors WHERE source_id='$id'";
$d_res=mysqli_query($con,$d_sql);
$passbook=mysqli_fetch_assoc($d_res);
$debtor_balance=$passbook['total'];

$cd_sql="SELECT id,first_name from employees";
$cd_res=mysqli_query($con,$cd_sql);
$debt_name=mysqli_fetch_assoc($cd_res);
$debtor_name=$debt_name['first_name'];
$debtor_name=$passbook['name'];
?>

<!docpayment_type html>
<html>
<head>
<head>
    <meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
	<form method="post" action="">
					<div class="col-md-2">
                       <input type="button" class="btn btn-primary btn-sm" style="position:absolute; z-index:1221; margin-top:67%; margin-left:400%" onClick="printDiv('invoice_div')" value="Print" /> 
                    </div>
                    <!--div class="col-md-2">
                       <input type="submit" name="send_email" class="btn btn-danger btn-sm" value="Send Email" /> 
                    </div-->
                   </form>
    </head>

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="https://cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.11/js/dataTables.bootstrap.min.js"></script>
<script payment_type="text/javascript">
	$(document).ready(function() {
    $('#example').DataTable();
} );
</script>
<meta charset="utf-8">
<title>Debtor Statement</title>
</head>



  <?php $invoice_html=
					 '   
					 <!doctype html>
					  <html>
					  
					  <meta charset="utf-8">
					  <head><style>
h2 {
	
	 style="padding:10px; color:#000000; background-color:#68B0EC;"
	 }
*{
	font-size:14px;
	font-family:arial;
	color:#000000;}
table th {
	padding:2px;
    text-align: center;
    background-color: #4CAF50;
    color: white;
	font-size:13px;
}
table td
{
	align-content:center;
	margin-left:3px;

	padding:2px;
	font-size:12px;
	
	

	}
	table {
		width:100%;
		table-layout:auto;
		border:thin;
		border-color:#000000;}
		table tr:nth-child(even) {background-color:#E0DFDF;}

.navbar-inverse {
            background-color:#F3DB08;
            
			border-color: #f1f1f1;
         }
		 
</style></head>
					  
					
<body>
<div class="row">
    	<div class="col-sm-12" style="background-color:#F2F746">
        	<h1 style="" class="text-black text-center"> Statement of '.$debtor_name.'</h1>
            
        </div>
    </div>
<div class="container">
	<div class="row">
    	<div class="col-md-12">
        <br>
<p style="font-size:26px; font-family:serif; color:#12A2CD;"> ';
 if($debtor_balance<0){ 
  $invoice_html.=
					 
 '<h2>Total Amount Recievable &pound;'.number_format(-$debtor_balance,2).'</h2>';}
else{ 
 $invoice_html.=
					 '<h2>Total Amount Payable &pound;'.number_format($debtor_balance,2).'</h2>';}
 $invoice_html.=
					 '					 
</p>

<table id="example" class="table table-striped table-bordered warning" cellspacing="0" width="100%">
        <thead>
            <tr style="font-size:26px; font-family:serif;">
                   <th style="width:25%">Date</th>
                <th style="width:25%">Details</th>
                <th  style="width:25%">Money In</th>
               <th style="width:25%">Money Out</th>

                
            </tr>
        </thead>
        <tfoot>
            <tr style="font-size:26px; font-family:serif">
            	
                 <th style="width:25%">Date</th>
                <th style="width:25%">Details</th>
                <th style="width:25%">Money In</th>
               <th style="width:25%">Money Out</th>

                
            </tr>
        </tfoot>
        <tbody >';
        						
                                    while($statement=mysqli_fetch_assoc($res))
									{
														
			 $invoice_html.=
					 '						
            <tr style="font-size:26px; font-family:serif;"  class="warning">';
            
             $date=date("d/m/Y",strtotime($statement['transaction_date']));
             $details=$statement['details'];
              $invoice_html.=
					 '   <td    style="width:100px;">'.$date.'</td>
               
                 
 				<td    style="width:100px;">'.$details;
 					
					$money_in=0;
					$money_out=0;
					
 						if($statement['transaction_type'] == 'cr')
						{
 							//echo $clients[$statement['source_id']];
							$money_in='&pound;'.number_format($statement['amount'],2);
 						}
 						else
 						{
 							//echo $candidates[$statement['source_id']];
							$money_out='&pound;'.number_format($statement['amount'],2);
 						}
 			 $invoice_html.=
					 '
				</td>
                <td class="success"    style="width:100px;">'.$money_in.'</td>
                <td    style="width:100px;">'.$money_out.'</td>
                
            </tr>';
            
            
									
				}
								
		 $invoice_html.=
					 '		
            
            </tbody>
    </table>
    </div>
    </div>
    </div>
    
</body>
</html>';
//echo $invoice_html;
?>
 <?php 
				  echo '<div class="col-md-11" id="invoice_div" style="margin-left:20px;">'.$invoice_html.'</div>'
				  
				  ;?>
 <script type="text/javascript">
		 /*
function printDiv() {
     var divToPrint = document.getElementById('invoice');
    var htmlToPrint = ''+
    '<style type="text/css">'+'th{border: 1px solid #7A7575;'+
'table td{align-content:left;padding-top:1px;font-size:12px;font-family:Arial;}'+
        '</style>';
    htmlToPrint += divToPrint.outerHTML;
    newWin = window.open("");
    newWin.document.write(htmlToPrint);
    newWin.print();
    newWin.close();
}
*/
function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}

                   </script>