<?php require('../../../includes/connection.php');

if(empty($_REQUEST['start']) && empty($_REQUEST['end'])){
$d=date('t');
$m=date('m');
$y=date('Y');

$monthStart="01-".$m."-".$y;
$monthEnd=$d."-".$m."-".$y;
//WHERE `date`>='".$_REQUEST['start']."' AND `date`<='".$_REQUEST['end']."'
$_REQUEST['start']=strtotime($monthStart);
$_REQUEST['end']=strtotime($monthEnd);
}
else{
$_REQUEST['start']=strtotime($_REQUEST['start']);
$_REQUEST['end']=strtotime($_REQUEST['end']);

}

$stmt = $GLOBALS['con']->prepare("SELECT * FROM `expenses` WHERE `payment_type`='".$_REQUEST['payment_type']."' AND `date`>='".$_REQUEST['start']."' AND `date`<='".$_REQUEST['end']."' ORDER BY abs(date) DESC")or die(mysqli_error("listClient B: ".$GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();

?>
<style>
#expenseTable {width:100%; table-layout: fixed; border-collapse: collapse; background-color:#ffffff}
#expenseTable th {text-decoration: underline;}
#expenseTable th{width:100%}
#expenseTable .c1{width:20%!important}
#expenseTable .c2{width:40%!important}
#expenseTable .c3{width:10%!important}
#expenseTable .c4{width:10%!important}
#expenseTable .c5{width:10%!important}
#expenseTable .c6{width:10%!important}
#expenseTable .c7{width:10%!important}
#expenseTable th,#expenseTable td {padding: 5px; text-align: left; }
#expenseTable thead tr,#expenseTable tbody tr ,#expenseTable tfoot tr {display: block; position: relative;width: 100%;}
#expenseTable tbody {display: block; overflow: auto; width: 100%; height: 400px; overflow-y: scroll; overflow-x: hidden;}
#expenseTable tfoot tr {background-color:#2a3f54; color:#fff}
</style>

<div class="col-md-4 input-group"><input class="form-control datepicker" type="text" name="date" id="date1" value="<?= date('d-m-Y',$_REQUEST['start'])?>" /></div>
<div class="col-md-1 text-center"><p style="font-size: 30px"><i id="toIcon" class="fad fa-arrow-alt-circle-right"></i></p></div>
<div class="col-md-4 input-group"><input class="form-control datepicker" type="text" name="date" id="date2" value="<?= date('d-m-Y',$_REQUEST['end'])?>"  /></div>
<div class="col-md-1 text-center"><button type="button" class="btn btn-secondary" id="filterDate"><i class="fad fa-filter"></i></button></div>
<div class="clearfix"></div>
<div id="expenseData">
<p class="lead"><?= $_REQUEST['payment_type']?> for the period <b><?= date('d-m-Y',$_REQUEST['start'])?></b> to <b><?= date('d-m-Y',$_REQUEST['end'])?></b></p>
<table id="expenseTable" class="table table-striped table-bordered">
<thead>
<tr>
<th class="c1">DATE</th>
<th class="c2">DETAILS</th>
<?php if($_REQUEST['payment_type']=="Expenses"){?>
<th class="c3">MILES</th>
<th class="c4">RATE</th>
<?php }?>
<th class="c5">CLAIM</th>
<th class="c6">NET</th>
<th class="c7">VAT</th>
</tr>
</thead>
<tbody>
<?php 
while($d=mysqli_fetch_array($cc)){
$t=explode("|",$d['totals']);
$nTotal=$nTotal+$t[0];
$vTotal=$vTotal+$t[1];
$mTotal=$mTotal+$d['vMileage'];

if(!empty($d['vMileage'])){
$mileage=$d['vMileage'];
$fVat=$fVat+$t[1];
}
else{
$mileage="";
$pVat=$pVat+$t[1];
}
$gTotal=$gTotal+$d['amount'];
if(!empty($d['vRate'])){$rate="&pound;".number_format($d['vRate'],2);}else{$rate="";}
if(!empty($d['amount'])){$amount=number_format($d['amount'],2);}else{$amount="0.00";}
if(!empty($t[0])){$net=number_format($t[0],2);}else{$net="0.00";}
if(!empty($t[1])){$vat=number_format($t[1],2);}else{$vat="0.00";}

?>
<tr>
<td class="c1"><?= date('d-m-Y',$d['date'])?></td>
<td class="c2"><?= $d['desc']?></td>
<?php if($_REQUEST['payment_type']=="Expenses"){?>
<td class="c3"><?= $mileage?></td>
<td class="c4"><?= $rate?></td>
<?php }?>
<td class="c5">&pound;<?= $amount?></td>
<td class="c6">&pound;<?= $net?></td>
<td class="c7">&pound;<?= $vat?></td>
</tr>
<?php }?>
</tbody>
<tfoot>
<tr class="footCell">
<th class="c1 footCell">Totals</th>
<td class="c2"></td>
<?php if($_REQUEST['payment_type']=="Expenses"){?>
<td class="c3"><?= $mTotal?></td>
<td class="c4"></td>
<?php }?>
<th class="c5">&pound;<?= number_format($gTotal,2)?></th>
<th class="c6">&pound;<?= number_format($nTotal,2)?></th>
<th class="c7">&pound;<?= number_format($vTotal,2)?></th>
</tr>
</tfoot>
</table>
<div class="clearfix"></div>
<table class="vatBox table table-bordered table-sm" style="width:50%; display: none">
<tr><th colspan="2">VAT Analysis</th></tr>
<tr><td>VAT FROM PURCHASES</td><td>&pound;<?= number_format($pVat,2)?></td></tr>
<?php if($_REQUEST['payment_type']=="Expenses"){?>
<tr><td>VAT ELEMENT FROM FUEL</td><td>&pound;<?= number_format($fVat,2)?></td></tr>
<?php }?>
<tr><th>TOTAL VAT INPUT</th><th>&pound;<?= number_format($fVat+$pVat,2)?></th></tr>
<tr><td>CLAIM EXCLUDING VAT</td><td>&pound;<?= number_format($gTotal,2)?></td></tr>
</table>
<div class="clearfix"></div>
</div>
<div id="genRow" class="col-md-12 text-center">
<button id="expenseReport" type="button" class="btn btn-secondary"><i class="fad fa-file-chart-line"></i> GENERATE REPORT</button>
</div>



<script>
$('#filterDate').click(function(){
var start=$('#date1').val()
var end=$('#date2').val()
$('#expenseList').load('includes/forms/elements/finance_exp_data.php?payment_type=<?= $_REQUEST['payment_type']?>&start='+start+'&end='+end)
})

$( ".datepicker" ).datepicker({changeMonth: true,showOn: 'button',buttonText: '<i class="fad fa-calendar-alt"></i>',yearRange: "1920:+10", dateFormat: "dd-mm-yy", changeYear:true,beforeShow: function(){$(".ui-datepicker").css({'font-size':35}),setBox()},onClose: function(){$('#overlay').css('display','none')}});

 $(window).resize(function() {
if ($(window).width() < 760) {
$('#toIcon').removeClass('fa-arrow-alt-circle-right')
$('#toIcon').addClass('fa-arrow-alt-circle-down')
}
else {
$('#toIcon').removeClass('fa-arrow-alt-circle-down')
$('#toIcon').addClass('fa-arrow-alt-circle-right')
}
})
$('#expenseReport').click(function(){
var start=$('#date1').val()
var end=$('#date2').val()
data=$('#expenseData').html()
$.post('includes/engines/report.php',{'clientnumber':'000',type:'<?= $_REQUEST['payment_type']?>_'+start+'_'+end,content:data,format:'A4-L'},function(ret){//debug(ret)
$('#genRow').html('<a href="output/reps/000_<?= $_REQUEST['payment_type']?>_'+start+'_'+end+'.pdf" target="blank" class="btn btn-info"><i class="fad fa-file-pdf"></i> VIEW REPORT</a>')
genFiles()
})

})

</script>