<?php include("sass/icon_for_datepicker_and_datatable.php");?>
              
        <div class="wrapper row-offcanvas row-offcanvas-left">
            <!-- Left side column. contains the logo and sidebar -->
            <?php include("sass/sidebar.php");?>

            <!-- Right side column. Contains the navbar and content of the page -->
               <aside class="right-side">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>
                       <?php echo $company_name ; ?>
                        <small>Control panel</small>
                    </h1>
                    <ol class="breadcrumb">
                        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
                        <li class="active">Dashboard</li>
                    </ol>
                </section>
                 <section class="content">
                 	 <form role="form"  action="<?php echo "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" method="post">
                 	 <div class="row">
                     <div class="col-md-3">
                     <select name="customer_id" class="form-control">
												<?php  
                                                $query_bank = "select * from customer_setup";
                                                $result_bank = mysqli_query($connection,$query_bank);
                                                
                                               
                                                while($row_bank=mysqli_fetch_array($result_bank))
                                                {
                                                
                                                echo "<option value='" . $row_bank['customer_id'] . "'>" . $row_bank['customer_name'] . "</option>";}?> 
                                            
                                            </select>
                     </div>
                        <div class="col-md-3">
                 			<input type="text" class="form-control datepicker" autocomplete="off" name="date1" placeholder="Starting Date">
                 
                 		</div>
                        <div class="col-md-3">
                 			<input type="text" class="form-control datepicker" autocomplete="off" name="date2" placeholder="Ending Date">
                 
                 		</div>
                        <div class="col-md-2">
                        	<button type="submit" name="submit" class="btn btn-primary">Submit</button>
                        </div>
                    </div>
                 
                 </form>
                 <button type="button" class="btn btn-info" onClick="printDiv('printableArea')"><i class="fa fa-print"></i>  Print</button>
                  <div class="row" id="printableArea"> 
                  	<div class="col-md-10 col-md-offset-1">
                 
                 <?php
												
												
												if(isset($_POST['submit'])) // To check 'submmited or not' by if(isset($_POST['submit'])) 
														{
														$customer_id = $_POST['customer_id'];
														$date1 = $_POST['date1'];
														$date1_con = date("Y-m-d", strtotime($date1));
														$date2 = $_POST['date2'];
														$date2_con = date("Y-m-d", strtotime($date2));
														
														if (empty($date1)) {
															 $message= "<div class='alert alert-danger alert-dismissable'><button type='button' class='close' data-dismiss='alert' 
																  aria-hidden='true'>
																  &times;
															   </button>
															  please write starting date
															</div>";
															 
															 }
															 elseif (empty($date2)) {
															 $message= "<div class='alert alert-danger alert-dismissable'><button type='button' class='close' data-dismiss='alert' 
																  aria-hidden='true'>
																  &times;
															   </button>
															   please write ending date
															</div>";
															 
															 }
														
														else {
														$result_for_bank_name1 = mysqli_query($connection,"SELECT customer_name
FROM customer_setup WHERE customer_id ='$customer_id'");
		$test_for_bank_name1 = mysqli_fetch_array($result_for_bank_name1);
				
				
				
				$customer_name1=$test_for_bank_name1['customer_name'];
													echo "<h3 style='text-align:center'>Ledger Record of Wholeseller $customer_name1  From" ;	echo ($date1); echo " To "; echo ($date2);echo "</h3>";
														
														echo "<table class='table table-bordered table-hover'>
                                        <thead>
                                        	<tr>
                                                <th>Serial No.</th>
                                                <th>Date</th>
                                                <th>ID</th>
												<th>Bank Name</th>
                                                <th>Ledger</th>
                                                <th>Debit</th>
                                                <th>Credit</th>
                                                <th>Cheque Number</th>
                                                <th>Cheque Date</th>
                                                <th>Transaction Type</th>
                                             </tr>
                                        </thead>";
													
													$previous_date = date('Y-m-d', strtotime($date1 . ' - 1 day'));
														
													$result_for_previous = mysqli_query($connection,"SELECT SUM(debit) as prev_debit , SUM(credit) as prev_credit FROM transaction WHERE date BETWEEN '2014-01-01' AND '$previous_date' AND customer_id = '$customer_id' AND payment_type = 'due'");
		$test_for_previous = mysqli_fetch_array($result_for_previous);
				
				$prev_debit=$test_for_previous['prev_debit'];
				$prev_credit=$test_for_previous['prev_credit'];
		echo "<tr>";		
				if($prev_debit>$prev_credit)
{

echo "<td>Balance B/D</td>";
$balance_cd = $prev_debit-$prev_credit;
echo "<td colspan='5' style='text-align:right'>".($balance_cd)."</td>";
echo "<td></td>";
$debit_total = $prev_credit + $balance_cd;
}
if($prev_debit<$prev_credit)
{
echo "<td>Balance B/D</td>";
$balance_bd = $prev_credit-$prev_debit;
echo "<td></td>";
echo "<td colspan='5' style='text-align:right'>".($balance_bd)."</td>";
$credit_total = $prev_debit + $balance_bd;
}
echo "</tr>";

if (empty($credit_total)){
$credit_total = 0 ;
}
if (empty($debit_total)){
$debit_total = 0 ;
}
													
														
														 $query = "SELECT * FROM transaction WHERE date >= '$date1_con' AND date <= '$date2_con' AND customer_id = '$customer_id' AND payment_type = 'due'";
														
                                                $result = mysqli_query($connection,$query);
												$i=0;
                                                while($row=mysqli_fetch_array($result))
                                                {
												$i++;
												$date = $row['date'];
												$date = date("d-m-Y", strtotime($date));
												$date = ($date) ;
												$ref_id = $row['ref_id'];
												$ledger = $row['ledger'];
												$debit = $row['debit'];
												$debit_total = $debit_total + $debit ;
												$debit_bn = ($debit) ;
												$credit = $row['credit'];
												$credit_total = $credit_total + $credit ;
												$credit_bn = ($credit) ;
												$cheque_no = $row['cheque_no'];
												$cheque_date = $row['cheque_date'];
												if(!empty($cheque_date)) {$cheque_date = date("d-m-Y", strtotime($cheque_date));}
												$cheque_date = ($cheque_date) ;
												$bank_id = $row['bank_id'];
												$payment_type = $row['payment_type'];
												
												
												
												$result_for_ledger = mysqli_query($connection,"SELECT *
FROM particulars WHERE particulars_id ='$ledger'");
		$test_for_ledger = mysqli_fetch_array($result_for_ledger);
				
				$ledger=$test_for_ledger['particulars'];
												
												$result_for_bank_name2 = mysqli_query($connection,"SELECT particulars
FROM particulars WHERE particulars_id ='$ledger'");
		$test_for_bank_name2 = mysqli_fetch_array($result_for_bank_name2);
				
				
				
				$ledger=$test_for_bank_name2['particulars'];
												
												$result_for_bank_name = mysqli_query($connection,"SELECT bank_name
FROM bank_name WHERE bank_id ='$bank_id'");
		$test_for_bank_name = mysqli_fetch_array($result_for_bank_name);
				
				
				
				$bank_name=$test_for_bank_name['bank_name'];
												
												echo "
												<tr>
												<td>"; echo ($i) ; echo "</td>
												<td>$date</td>
												<td>$ref_id</td>
												<td>$bank_name</td>
												<td>$ledger</td>
												<td>$debit_bn</td>
												<td>$credit_bn</td>
												<td>$cheque_no</td>
												<td>$cheque_date</td>
												<td>$payment_type</td>
												</tr>
												";
												}
												echo "<tr>";
												if($debit_total>$credit_total)
{
echo "<td>Balance C/D</td>";
echo "<td></td>";
$balance = $debit_total-$credit_total;
echo "<td colspan='5' style='text-align:right'>".($balance)."</td>";
$credit_total = $credit_total + $balance;
}
if($debit_total<$credit_total)
{
echo "<td>Balance C/D</td>";
$balance = $credit_total-$debit_total;
echo "<td colspan='5' style='text-align:right'>".($balance)."</td>";
echo "<td></td>";
$debit_total = $debit_total + $balance;
}
												echo "</tr>";
												
												echo "<tr>";
												echo "<th>Total</th>";
												echo "<th colspan='5' style='text-align:right'>"; echo ($debit_total) ; echo "</th>";
												echo "<th colspan='3'>"; echo ($credit_total) ; echo "</th>";
												echo "</tr>";
												}
												}		
														?>
                        </table>
                    </div>
                 </div>
                 
                 </section>
                </aside>
                <!-- /.right-side -->
        </div><!-- ./wrapper -->
        <!-- add new calendar event modal -->
        <!-- jQuery 2.0.2 -->
          <?php include("sass/footer_for_datepicker_and_datatable.php");?>

    </body>
</html>