<?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-5">
                 			<input type="text" class="form-control datepicker" autocomplete="off" name="date1" placeholder="Starting Date">
                 
                 		</div>
                        <div class="col-md-5">
                 			<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-12">
                 
                 <?php
												
												
												if(isset($_POST['submit'])) // To check 'submmited or not' by if(isset($_POST['submit'])) 
														{
														$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 {
														
													echo "<h3 style='text-align:center'>" ;echo " Cash Ledger Book 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>Ref.</th>
                                                <th>Supplier Name</th>
                                                <th>Customer/Retailler Name</th>
                                                <th>Ledger</th>
                                                <th>Debit</th>
                                                <th>Credit</th>
												 <th>balance</th>
												 <th>Details</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 payment_type = 'cash' AND branch_id  = '$branch_id_for_header_user'");
			
		$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='6' style='text-align:right'>".($balance_cd)."</td>";
echo "<td></td>";
$debit_total = $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='6' style='text-align:right'>".($balance_bd)."</td>";
$credit_total = $balance_bd;
}
echo "<td></td>";
echo "</tr>";

/*if (empty($credit_total)){
$balance_bd = 0 ;
$credit_total = $prev_debit;
//$credit_total = 0 ;
}
if (empty($debit_total)){
$balance_cd = 0 ;
$debit_total = $prev_credit;
//$debit_total = 0 ;
}*/ //change if disturb

if (empty($credit_total)){
$balance_bd = 0 ;
$credit_total = 0;
//$credit_total = 0 ;
}
if (empty($debit_total)){
$balance_cd = 0 ;
$debit_total = 0;
//$debit_total = 0 ;
}
$lba = $balance_bd + $balance_cd ;


													
														
														 $query = "SELECT * FROM transaction WHERE date >= '$date1_con' AND date <= '$date2_con' AND payment_type = 'cash' AND branch_id  = '$branch_id_for_header_user' order by `date`";
														
                                                $result = mysqli_query($connection,$query);
                                                 
                                                $prbat = 0 ;
												
												if($balance_cd==0){
												
												$prba = $balance_bd ;

												} elseif($balance_bd==0){
												
												$prba = $balance_cd ;
												}  
												  $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'];
												$customer_id = $row['customer_id'];
												$supplier_id = $row['supplier_id'];
												$ledger = $row['ledger'];
												$debit = $row['debit'];
												$debit_total = $debit_total + $debit ;
												$debit_bn = ($debit) ;
												$credit = $row['credit'];
												
												
			$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'];
												
												if($debit==0){
												if($balance_cd==0){
												
												$prba = $prba + $credit ;
												
											

												} elseif($balance_bd==0){
												
												$prba = $prba - $credit ;
												
												}
												
												} elseif($credit==0){
												if($balance_cd==0){
												
												$prba = $prba - $debit ;

												} elseif($balance_bd==0){
												
												$prba = $prba + $debit ;
												
												}
												
												}  
												
												$hs = $prba - $prbat ;
												
												$prbat = $prbat + $hs ;
												
												$hint = $row['hint'];
												
												$details = $row['details'];
												$credit_total = $credit_total + $credit ;
												$credit_bn = ($credit) ;
												
												
												if($hint=="Card Sale"){
												$result_for_customer = mysqli_query($connection,"SELECT retailer_name
FROM retailer_setup WHERE retailer_setup_id ='$customer_id'");
		$test_for_customer = mysqli_fetch_array($result_for_customer);
				
				$customer_name=$test_for_customer['retailer_name'];
												
												}
												else{
												$result_for_customer = mysqli_query($connection,"SELECT customer_name
FROM customer_setup WHERE customer_id ='$customer_id'");
		$test_for_customer = mysqli_fetch_array($result_for_customer);
				
				$customer_name=$test_for_customer['customer_name'];
										}		
												$result_for_supplier = mysqli_query($connection,"SELECT supplier_name
FROM supplier_setup WHERE supplier_id ='$supplier_id'");
		$test_for_supplier = mysqli_fetch_array($result_for_supplier);
				
				$supplier_name=$test_for_supplier['supplier_name'];
												
												
												echo "
												<tr>
												<td>"; echo ($i) ; echo "</td>
												<td>$date</td>
												<td>$ref_id </td>
												<td>$supplier_name</td>
												<td>$customer_name</td>
												<td>$ledger</td>
												<td>$debit_bn</td>
												<td>$credit_bn</td>
												<td>$prbat</td>
												<td>$details</td>
												</tr>
												";
												
												
												}
												
												
												echo "<tr>";
												if($debit_total>$credit_total)
{
echo "<td>Balance C/D</td>";
echo "<td></td>";
//$balance = ($debit_total-$credit_total)+ $balance_cd;
$balance = $debit_total-$credit_total ;
echo "<td colspan='6' 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) + $balance_bd;
$balance = $credit_total-$debit_total ;
echo "<td colspan='6' style='text-align:right'>".($balance)."</td>";
echo "<td></td>";
$debit_total = $debit_total + $balance;
} echo "<td></td>";
												echo "</tr>";
												
												echo "<tr>";
												echo "<th>Total</th>";
												echo "<th  colspan='6' style='text-align:right'>"; echo ($debit_total) ; echo "</th>";
												echo "<th>"; echo ($credit_total) ; echo "</th>";
												echo "<td></td>";
												echo "<td></td>";
												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>