<?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 `receipts` WHERE  `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();

?>

<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 reportHeader">Receipts 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>
<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];
$pVat=$pVat+$t[1];
?>
<tr>
<td class="c1"><?= date('d-m-Y',$d['date'])?></td>
<td class="c2"><?= $d['details']?></td>
<td class="c6">&pound;<?= number_format($t[0],2)?></td>
<td class="c7">&pound;<?= number_format($t[1],2)?></td>
</tr>
<?php }?>
</tbody>
<tfoot>
<tr class="footCell">
<th class="c1 footCell">Totals</th>
<td class="c2"></td>
<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_rcp_data.php?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:'receipts_'+start+'_'+end,content:data,format:'A4-L'},function(ret){

//debug(ret)
$('#genRow').html('<a href="output/reps/000_receipts_'+start+'_'+end+'.pdf" target="blank" class="btn btn-info"><i class="fad fa-file-pdf"></i> VIEW REPORT</a>')
genFiles()
})

})

</script>