<?php $_REQUEST['src']="ext2";require('../../functions/global.php');require('finance_functions.php');
$start=strtotime($_REQUEST['start']);
$end=strtotime($_REQUEST['end']);
?>
<div id="invoiceData">
<?php 
if($_REQUEST['type']=="receipts"){
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `receipts` WHERE  `date`>='".$start."' AND `date`<='".$end."' ORDER BY abs(date) DESC")or die(mysqli_error("listClient B: ".$GLOBALS['con']));
$stmt->execute();$i=$stmt->get_result();$stmt->close();

while($d=mysqli_fetch_array($i)){
    
$itemRow="";
$address=unserialize($d['address']);
$items=unserialize($d['items']);
$totals=explode("|",$d['totals']);
$totals[0]=str_replace(',','',$totals[0]);
$totals[1]=str_replace(',','',$totals[1]);
if($totals[1]=="" || $totals[1]=="0" || $totals[1]=="0.00"){}
else{

$exVat=$exVat+$totals[0];
$vat=$vat+$totals[1];


$tableData.='<tr><td>'.date('d-m-Y',$d['date']).'</td><td>'.$d['payee'].'</td><td>'.$d['details'].'</td><td>'.$cn.'</td><td>'.number_format($totals[0],2).'</td><td>'.number_format($totals[1],2).'</td></tr>';
}}
?>
<div class="col-md-12 reportHeader"><p class="lead exportTitle">VAT results for period <strong><?php echo $_REQUEST['start']?></strong> to <strong><?php echo $_REQUEST['end']?></strong></div>
<div class="col-md-12">
<table id="expenseTable" class="table table-sm table-striped">
<thead>
<tr><th class="noExport">DATE</th><th>PAYEE</th><th>DETAILS</th><th>CHQ NO</th><th>EX-VAT</th><th>VAT</th></tr>
</thead>
<tbody>
<?php echo $tableData?>
</tbody>
<tfoot>
    <tr>
h></th>
        <th>Total Ex-VAT for period:</th>
        <th>&pound;<?php echo number_format($exVat,2)?></th>
        <th></th>
        <th></th>
        <th>Total VAT <?php echo $_REQUEST['type']?> for period:</th>
        <th>&pound;<?php echo number_format($vat,2)?></th>
    </tr>
   
</tfoot>
</table>
</div>

<div class="col-md-12">
<div class="col-md-11"><p class="float-right">Total Ex-VAT for period:</p></div><div class="col-md-1"><p class="float-right">&pound;<?php echo number_format($exVat,2)?></p></div>
</div>
<div class="col-md-12">
<div class="col-md-11"><p class="float-right">Total VAT <?php echo $_REQUEST['type']?> for period:</p></div><div class="col-md-1"><p class="float-right">&pound;<?php echo number_format($vat,2)?></p></div>
</div>

<?php }

if($_REQUEST['type']=="expenditure"){
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `expenses` WHERE (`payment_type`='Expenses' OR `payment_type`='Outgoings') AND `date`>='".$start."' AND `date`<='".$end."' ORDER BY abs(id) ASC")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$i=$stmt->get_result();$stmt->close();
$exVat=0;
$vat=0;
while($d=mysqli_fetch_array($i)){
    
$itemRow="";
$totals=explode("|",$d['totals']);
$totals[0]=str_replace(',','',$totals[0]);
$totals[1]=str_replace(',','',$totals[1]);
if($totals[1]=="" || $totals[1]=="0" || $totals[1]=="0.00"){}
else{
if($totals[1]>0){
$exVat=$exVat+$totals[0];
}
$vat=$vat+$totals[1];

if($totals[1]>0){
$tableData.='<tr><td>'.date('d-m-Y',$d['date']).'</td><td>'.$d['payment_type'].'</td><td>'.$d['desc'].'</td><td>'.$cn.'</td><td>'.number_format($totals[0],2).'</td><td>'.number_format($totals[1],2).'</td></tr>';
}
}}
?>
<style>.remove{display: none!important;}</style>
<div class="col-md-12 reportHeader"><p class="lead exportTitle">VAT results for period <strong><?php echo $_REQUEST['start']?></strong> to <strong><?php echo $_REQUEST['end']?></strong></div>
<div class="col-md-12">
<table id="expenseTable" class="table table-sm table-striped">
<thead>
<tr><th class="noExport">DATE</th><th>TYPE</th><th>DETAILS</th><th>CHQ NO</th><th>EX-VAT</th><th>VAT</th></tr>
</thead>
<tbody>
<?php echo $tableData?>
</tbody>
<tfoot>
    <tr>
h></th>
        <th>Total Ex-VAT for period:</th>
        <th>&pound;<?php echo number_format($exVat,2)?></th>
        <th></th>
        <th></th>
        <th>Total VAT <?php echo $_REQUEST['type']?> for period:</th>
        <th>&pound;<?php echo number_format($vat,2)?></th>
    </tr>
   
</tfoot>
</table>
</div>

<div class="col-md-12">
<div class="col-md-11"><p class="float-right">Total Ex-VAT for period:</p></div><div class="col-md-1"><p class="float-right">&pound;<?php echo number_format($exVat,2)?></p></div>
</div>
<div class="col-md-12">
<div class="col-md-11"><p class="float-right">Total VAT <?php echo $_REQUEST['type']?> for period:</p></div><div class="col-md-1"><p class="float-right">&pound;<?php echo number_format($vat,2)?></p></div>
</div>

<?php }?>
</div>
<div id="genRow" class="col-md-12 text-center remove">
<button id="expenseReport" type="button" class="btn btn-secondary"><i class="fal fa-file-chart-line"></i> GENERATE REPORT</button>
</div>

<?php
$dateTarget='0';
$table="expenseTable";
require('../../includes/finance_table.engine');
?>

<script>
$('#expenseReport').click(function(){
var start=$('#sdate').val()
var end=$('#edate').val()
data=$('#invoiceData').html()
$.post('includes/engines/report.php',{'clientnumber':'000',type:'vat<?php echo $_REQUEST['type']?>_generated_<?php echo date('d-m-Y')?>',content:data, format:'A4-L'},function(ret){//debug(ret)
$('#genRow').html('<a href="output/reps/000_vat<?php echo $_REQUEST['type']?>_generated_<?php echo date('d-m-Y')?>.pdf" target="blank" class="btn btn-info"><i class="fal fa-file-pdf"></i> VIEW REPORT</a>')
})

})
</script>




