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

$disp = '';
$pagetitle = 'View/Search Expenses';
date_default_timezone_set('Asia/Kolkata');

$date = $_REQUEST['date'];
$date2 = $_REQUEST['date2'];
$project = 1;
$work_type= $_REQUEST['work_type'];

if(!empty($date))
@$dateref = date("d-m-Y", strtotime($date));
else
$dateref = '';

if(!empty($date2))
@$dateref2 = date("d-m-Y", strtotime($date2));
else
$dateref2 = '';

if(empty($work_type))
{
echo '<meta http-equiv="refresh" content="0;URL=dailyexpense.php?sm=Please select dates">';
exit;
}

if($work_type!='all')
{
	if($project=='all')
	{
		if(empty($date) and empty($date2))
		{
		$q = "select * from expenses where work_type='$work_type' order by id";
		$qsum = "select sum(amount) as total_sum from expenses where work_type='$work_type'";
		}
		else
		{
		$q = "select * from expenses where work_type='$work_type' and date between '$date' and '$date2' order by id ";
		$qsum = "select sum(amount) as total_sum from expenses where work_type='$work_type' and date between '$date' and '$date2'";
		}
	}
	else
	{
		if(empty($date) and empty($date2))
		{
		$q = "select * from expenses where project='$project' and work_type='$work_type' order by id";
		$qsum = "select sum(amount) as total_sum from expenses where project='$project' and work_type='$work_type'";
		}
		else
		{
		$q = "select * from expenses where project='$project' and work_type='$work_type' and date between '$date' and '$date2' order by id";
		$qsum = "select sum(amount) as total_sum from expenses where project='$project' and work_type='$work_type' and date between '$date' and '$date2'";
		}
	}
}//end of if for checking if worktype is not all

elseif($work_type='all')
{
	if($project=='all')
	{
		if(empty($date) and empty($date2))
		{
		$q = "select * from expenses order by id";
		$qsum = "select sum(amount) as total_sum from expenses";
		}
		else
		{
		$q = "select * from expenses where date between '$date' and '$date2' order by id";
		$qsum = "select sum(amount) as total_sum from expenses where date between '$date' and '$date2'";
		}
	}
	else
	{
		if(empty($date) and empty($date2))
		{
		$q = "select * from expenses where project='$project' order by id";
		$qsum = "select sum(amount) as total_sum from expenses where project='$project'";
		}
		else
		{
		$q = "select * from expenses where project='$project' and date between '$date' and '$date2' order by id";
		$qsum = "select sum(amount) as total_sum from expenses where project='$project' and date between '$date' and '$date2'";
		}
	}
}//end of elseif for checking if worktype is all

$res = mysqli_query($dbcon,$q) or die(mysqli_error($dbcon));
if(mysqli_num_rows($res)<1)
{
$disp .= 'No results found';
include('../template.php');
exit;
}

$rsum = mysqli_query($dbcon,$qsum) or die(mysqli_error($dbcon));
$rwsum = mysqli_fetch_array($rsum);

$qwork = "select * from work_type where id='$work_type'";
$rwork = mysqli_query($dbcon,$qwork) or die(mysqli_error($dbcon));
$rwwork = mysqli_fetch_array($rwork);


$disp .= '
<div class="row">
                <div class="col-md-8"><button class="btn btn-primary" style="margin-bottom:15px">Total Expenses: '.$rwsum['total_sum'].'</button>
				</div></div>
<div class="row">
                <div class="col-md-12">
                    <!-- Advanced Tables -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            Expense Details for <b>'.$rwwork['work_type'].'</b> for Period Between: '.$dateref.' and '.$dateref2.': <br>
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                    <thead>
                                        <tr>
											<th>SNO</th>
											<th style="width:14%">Date</th>
											<th>Category</th>
											<th>Name</th>
											<th>Particulars</th>
                                            <th>Amount </th>
                                            <th>Receipt</th>
                                            
											
                                            <th>Operation</th>
											
                                        </tr>
                                    </thead>
                                    <tbody>';

while($row = mysqli_fetch_array($res))
{
$qproj = "select project_name from project where id='".$row['project']."'";
$rproj = mysqli_query($dbcon,$qproj) or die(mysqli_error($dbcon));
$rwproj = mysqli_fetch_array($rproj);

$qcat = "select * from work_type where id='".$row['work_type']."'";
$rcat = mysqli_query($dbcon,$qcat) or die(mysqli_error($dbcon));
$rwcat = mysqli_fetch_array($rcat);

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

if($row['payment_method'] == 'cheque')
$payment_details = 'Cheque No: '.$row['payment_details'];
else
$payment_details = $row['payment_details'];

$disp .= '<tr class="odd gradeX"><td>'.$row['id'].'</td><td>'.$datedisp.'</td><td>'.$rwcat['work_type'].'</td><td>'.$row['name'].'</td><td><a href="viewexpense.php?id='.$row['id'].'" target=_blank>'.substr($row['particulars'],0,20).' ...</a></td><td>&#8377;'.$row['amount'].' paid by '.$row['payment_method'].',<Br>'.$payment_details.'</td>

<td><a href="../'.$row['rcptphoto'].'" target=_blank><img src="../'.$row['rcptphoto'].'" style="width:100px;height:100px;"></a></td>

<td><a href="editexpense.php?id='.$row['id'].'"><img src="../assets/img/edit.png"></a> &nbsp; <a href="deleteexpense.php?id='.$row['id'].'&date='.$date.'&date2='.$date2.'&project='.$project.'&work_type='.$work_type.'" onclick="return confirm(\'Are you sure you want to delete this entry?\')"><img src="../assets/img/delete.png"></a>&nbsp;&nbsp;<a href="printexpensercpt.php?id='.$row['id'].'" target=_blank><img src="../assets/img/printericon.jpg" style="height:20px;width:20px;"></a></td>
</tr>';
}

$disp .='            </tbody>
                                </table>
                            </div>
                            
                        </div>
                    </div>
                    <!--End Advanced Tables -->
                </div>
            </div>';
			
			//echo '<a onclick="return confirm(\'delete?\')" href="abc.html" )>del</a>';

include('../header.php');
?>

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

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