<?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>

                <!-- Main content -->
                <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" "Starting Date" required>
                 
                 		</div>
                        <div class="col-md-5">
                 			<input type="text" class="form-control datepicker" autocomplete="off" name="date2" "Ending Date" required>
                 
                 		</div>
                        <div class="col-md-2">
                        	<button type="submit" name="submit" class="btn btn-primary">Submit</button>
                        </div>
                    </div>
                 
                 </form>
				
				  <?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));
														
														
					echo "<h3 style='text-align:center'>" ;echo " Income Statement From ";	echo ($date1);echo " To "; echo ($date2);   echo "</h3>";
														?>
				 
                 	 <div class="row">
                        <div class="col-md-6" style="border:2px solid black">
                 <!---Main Content--->
                <div class="col-md-12" style="border:1px solid black; text-align:center;">
                 			<h4>Income</h4>
							</div>
						<?php 
						
						$query = "SELECT distinct ledger FROM transaction WHERE `date` >= '$date1_con' AND `date` <= '$date2_con' AND debit <> 0";
						 $result = mysqli_query($connection,$query);
						 
						 $income_total = 0 ;
						 
						echo "<table class='table'>
						<tr> <th>Perticular</th>
						<th>Amount</th>
						</tr>";

						
						while($row=mysqli_fetch_array($result))
                                                {
												$ledger = $row['ledger'];
												
						$result_for_income = mysqli_query($connection,"SELECT SUM(debit) AS income
FROM transaction WHERE `date` >= '$date1_con' AND `date` <= '$date2_con' AND ledger ='$ledger'");
		$test_for_income = mysqli_fetch_array($result_for_income);
				
				$income=$test_for_income['income'];
			$income_total = $income_total +	$income;															
			$result_for_ledger = mysqli_query($connection,"SELECT *
FROM particulars WHERE particulars_id ='$ledger'");
		$test_for_ledger = mysqli_fetch_array($result_for_ledger);
				
				$ledger_txt=$test_for_ledger['particulars'];
										echo "<tr>
										<td>$ledger_txt</td>
										<td>$income</td>
										";		
									
												}
						?>
											
                 </table>
                 		</div>
						<div class="col-md-6" style="border:2px solid black">
							<div class="col-md-12" style="border:1px solid black; text-align:center;">
                 			<h4>Expense</h4>
							</div>
						<?php 
						
						$query2 = "SELECT distinct ledger FROM transaction WHERE `date` >= '$date1_con' AND `date` <= '$date2_con' AND credit <> 0";
						 $result2 = mysqli_query($connection,$query2);
						 $exp_total = 0 ;
						 
						echo "<table class='table'>
						<tr> <th>Perticular</th>
						<th>Amount</th>
						</tr>";
						
						while($row2=mysqli_fetch_array($result2))
                                                {
												$ledger_exp = $row2['ledger'];
												
						$result_for_income = mysqli_query($connection,"SELECT SUM(credit) AS exp
FROM transaction WHERE `date` >= '$date1_con' AND `date` <= '$date2_con' AND ledger ='$ledger_exp'");
		$test_for_income = mysqli_fetch_array($result_for_income);
				
				$exp=$test_for_income['exp'];
					$exp_total = $exp_total + $exp;																
			$result_for_ledger = mysqli_query($connection,"SELECT *
FROM particulars WHERE particulars_id ='$ledger_exp'");
		$test_for_ledger = mysqli_fetch_array($result_for_ledger);
				
				$ledger_txt=$test_for_ledger['particulars'];
										echo "<tr>
										<td>$ledger_txt</td>
										<td>$exp</td>
										";		
									
												}
						?>
											
                 </table>
							
                 
                 		</div>
					</div>	
			<div class="row">			
			<div class="col-md-6" style="border:2px solid black">
			<strong>Total Income: <?php echo $income_total ; ?></strong>
			</div>
			<div class="col-md-6" style="border:2px solid black">
			<strong>Total Expense: <?php echo $exp_total ; ?></strong>
			</div>
              <div class="col-md-12" style="border:2px solid black; text-align:center;">
			  <strong>Net Income/Loss: <?php echo $income_total - $exp_total ; ?></strong>
			  </div>
				<br> <br>
										
		<script type="text/javascript">

            $(function() {

                var bar_data = {
                    data: [
					
					
					<?php 
					$query = "SELECT distinct ledger FROM transaction WHERE `date` >= '$date1_con' AND `date` <= '$date2_con' AND debit <> 0";
						 $result = mysqli_query($connection,$query);
						
						while($row=mysqli_fetch_array($result))
                                                {
												$ledger = $row['ledger'];
												
						$result_for_income = mysqli_query($connection,"SELECT SUM(debit) AS income
FROM transaction WHERE `date` >= '$date1_con' AND `date` <= '$date2_con' AND ledger ='$ledger'");
		$test_for_income = mysqli_fetch_array($result_for_income);
				
				$income=$test_for_income['income'];														
			$result_for_ledger = mysqli_query($connection,"SELECT *
FROM particulars WHERE particulars_id ='$ledger'");
		$test_for_ledger = mysqli_fetch_array($result_for_ledger);
				
				$ledger_txt=$test_for_ledger['particulars'];
										
									echo '["' . $ledger_txt . '", ' . $income . '],';
												}
					
					
					
					 ?>
					
					
					
					],
                    color: "#3c8dbc"
                };
                $.plot("#bar-chart", [bar_data], {
                    grid: {
                        borderWidth: 1,
                        borderColor: "#f3f3f3",
                        tickColor: "#f3f3f3"
                    },
                    series: {
                        bars: {
                            show: true,
                            barWidth: 0.5,
                            align: "center"
                        }
                    },
                    xaxis: {
                        mode: "categories",
                        tickLength: 0
                    }
                });
            });

     
        </script>

                    <div class="row">
               

                        <div class="col-md-12">
                            <!-- Bar chart -->
                            <div class="box box-primary">
                                <div class="box-header">
                                    <i class="fa fa-bar-chart-o"></i>
                                    <h3 class="box-title">Income Chart</h3>
                                </div>
                                <div class="box-body">
                                    <div id="bar-chart" style="height: 300px;"></div>
                                </div><!-- /.box-body-->
                            </div><!-- /.box -->

                        </div><!-- /.col -->
                    </div><!-- /.row -->
					
					
					
					
					
					      
				<br> <br>
										
		<script type="text/javascript">

            $(function() {

                var bar_data = {
                    data: [
					
					
					<?php 
					
					
					
					$query2 = "SELECT distinct ledger FROM transaction WHERE `date` >= '$date1_con' AND `date` <= '$date2_con' AND credit <> 0";
						 $result2 = mysqli_query($connection,$query2);
						
						while($row2=mysqli_fetch_array($result2))
                                                {
												$ledger_exp = $row2['ledger'];
												
						$result_for_income = mysqli_query($connection,"SELECT SUM(credit) AS exp
FROM transaction WHERE `date` >= '$date1_con' AND `date` <= '$date2_con' AND ledger ='$ledger_exp'");
		$test_for_income = mysqli_fetch_array($result_for_income);
				
				$exp=$test_for_income['exp'];															
			$result_for_ledger = mysqli_query($connection,"SELECT *
FROM particulars WHERE particulars_id ='$ledger_exp'");
		$test_for_ledger = mysqli_fetch_array($result_for_ledger);
				
				$ledger_txt=$test_for_ledger['particulars'];
										echo '["' . $ledger_txt . '", ' . $exp . '],';
									
												}
					
					
					
					
					
					 ?>
					
					
					
					],
                    color: "#3c8dbc"
                };
                $.plot("#bar-chart2", [bar_data], {
                    grid: {
                        borderWidth: 1,
                        borderColor: "#f3f3f3",
                        tickColor: "#f3f3f3"
                    },
                    series: {
                        bars: {
                            show: true,
                            barWidth: 0.5,
                            align: "center"
                        }
                    },
                    xaxis: {
                        mode: "categories",
                        tickLength: 0
                    }
                });
            });

     
        </script>

                    <div class="row">
               

                        <div class="col-md-12">
                            <!-- Bar chart -->
                            <div class="box box-primary">
                                <div class="box-header">
                                    <i class="fa fa-bar-chart-o"></i>
                                    <h3 class="box-title">Expense Chart</h3>
                                </div>
                                <div class="box-body">
                                    <div id="bar-chart2" style="height: 300px;"></div>
                                </div><!-- /.box-body-->
                            </div><!-- /.box -->

                        </div><!-- /.col -->
                    </div>
					
					
			   <?php 		


		

				 }						
							?>
                </section><!-- /.content -->

            </aside><!-- /.right-side -->
        </div><!-- ./wrapper -->


        <?php include("sass/footer_for_datepicker_and_datatable.php");?>
		  <!-- FLOT CHARTS -->
        <script src="js/plugins/flot/jquery.flot.min.js" type="text/javascript"></script>
        <!-- FLOT RESIZE PLUGIN - allows the chart to redraw when the window is resized -->
        <script src="js/plugins/flot/jquery.flot.resize.min.js" type="text/javascript"></script>
        <!-- FLOT PIE PLUGIN - also used to draw donut charts -->
        <script src="js/plugins/flot/jquery.flot.pie.min.js" type="text/javascript"></script>
        <!-- FLOT CATEGORIES PLUGIN - Used to draw bar charts -->
        <script src="js/plugins/flot/jquery.flot.categories.min.js" type="text/javascript"></script>

     
    </body>
</html>
