<?php
include('../headertop.php');
include('../db.php');
//include('../datepicker.php');

$disp = '';
$pagetitle = 'View/Search Investments';

if(!isset($_REQUEST['viewinvestmentssubmit']))
{
echo '<meta http-equiv="refresh" content="0;URL=viewinvestment.php">';
exit;
}

$date = $_REQUEST['date'];
$date2 = $_REQUEST['date2'];
$investor = $_REQUEST['investor'];

if($investor == 'all')
{
if(empty($date) and !empty($date2))
{
$q = "select * from investments where date <=  '$date2' order by date";
$q2 = "select sum(amount) as total_investment from investments where date <=  '$date2'";
}
elseif(!empty($date) and empty($date2))
{
$q = "select * from investments where date >= '$date' order by date";
$q2 = "select sum(amount) as total_investment from investments where date >=  '$date' ";
}
elseif(!empty($date) and !empty($date2))
{
$q = "select * from investments where date between '$date' and '$date2' order by date";
$q2 = "select sum(amount) as total_investment from investments where date between '$date' and  '$date2' order by date";
}
elseif(empty($date) and empty($date2))
{
$q = "select * from investments order by date";
$q2 = "select sum(amount) as total_investment from investments";
}
}

elseif($investor != 'all')
{
if(empty($date) and !empty($date2))
{
$q = "select * from investments where investor = '$investor' and date <=  '$date2' order by date";
$q2 = "select sum(amount) as total_investment from investments where investor = '$investor' and date <=  '$date2'";
}
elseif(!empty($date) and empty($date2))
{
$q = "select * from investments where investor = '$investor' and date >= '$date' order by date";
$q2 = "select sum(amount) as total_investment from investments where investor = '$investor' and date >=  '$date' ";
}
elseif(!empty($date) and !empty($date2))
{
$q = "select * from investments where (date between '$date' and '$date2') and  (investor='$investor') order by date";
$q2 = "select sum(amount) as total_investment from investments where (investor = '$investor') and (date between '$date' and '$date2') ";
}
elseif(empty($date) and empty($date2))
{
$q = "select * from investments where investor='$investor'  order by date";
$q2 = "select sum(amount) as total_investment from investments where investor='$investor'";
}
}
//echo $q;

$res = mysqli_query($dbcon,$q) or die(mysqli_error($dbcon));
$res2 = mysqli_query($dbcon,$q2) or die(mysqli_error($dbcon));

if(mysqli_num_rows($res)<1)
{
$disp .= 'No investments found';
include('../template.php');
exit;
}

$row2 = mysqli_fetch_array($res2);
//echo $q;
//echo '<br>q2: '.$q2;


//$disp .= '<h3>Viewing Expenses  Between '.$date.' to '.$date2.' :</h3> <br>';
$disp .= '<button class="btn btn-warning" style="margin-bottom:15px">Total Investments:'.$row2['total_investment'].'</button><table class="table table-striped" id="dataTables-example"><thead><th>Date</th><th>Investor Name</th><th>Payment Method<th>Details</th><th>Amount</th><th>Operation</th></thead><tbody>';

while($row = mysqli_fetch_array($res))
{
$q3 = "select * from investor where id='".$row['investor']."'";
$res3 = mysqli_query($dbcon,$q3);
$row3 = mysqli_fetch_array($res3);

$datedisp = date("d-m-Y", strtotime($row['date']));

$disp .='<tr><td>'.$datedisp.'</a></td><td>'.UCFirst($row3['name']).'</td><td>'.UCFirst($row['payment_method']).'<br>'.$row['payment_details'].'</td><td>'.$row['particulars'].'</td><td>'.$row['amount'].'</td><td><a href="deleteinvestments.php?id='.$row['id'].'&date='.$date.'&date2='.$date.'&investor='.$investor.'" onclick="return confirm(\'Are you sure you want to delete this entry?\')"><img src="../assets/img/delete.png"></a></td>';

}
$disp .= '</tbody></table>';


include('../header.php');
?>
<script>
            $(document).ready(function () {
                $('#dataTables-example').dataTable({
      dom: 'Bfrtip',
        buttons: [
             'excel', 'print'
        ]
    });
            });
			
			//$('#dataTables-example').dataTable({"pageLength":100});
			//$('#dataTables-example').dataTable({"paging":false);
    </script>
    
    <?php

include('../template.php');