<?php $_REQUEST['src']="ext2"; require('../../functions/global.php');
$start=strtotime($_REQUEST['start']);
$end=strtotime($_REQUEST['end']);
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `invoices` WHERE `status`='3' AND `commission`!='' AND `date_paid`>='".$start."' AND `date_paid`<='".$end."' ORDER BY abs(date_paid) DESC")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$i=$stmt->get_result();$stmt->close();
$num=mysqli_num_rows($i);

if($num==0){echo '<div class="alert alert-warning">No data within date range</div>';}
else{?>
<div id="repdata">
<p id="exportTitle" class="lead reportHeader">Commission report for the period <b><?php echo date('d-m-Y',$start)?></b> to <b><?php echo date('d-m-Y',$end)?></b></p>
<table id="commissionrep" class="table table-striped">
<thead>
<tr><th>INVOICE</th><th>DATE</th><th>CLIENT</th><th>AGENT</th><th align="right">COMMISSION</th><th class="noExport"></th></tr>
</thead>
<tbody>
<?php while($d=mysqli_fetch_array($i)){$ct=$ct+$d['commission']?>
<tr><td><?php echo sprintf("%06d", $d['invNo']);?></td><td><?php echo date('d-m-Y',$d['date_paid'])?></td><td><?php echo getClientName($d['client'])?></td><td><?php echo getAgentName($d['agent'])?></td><td align="right">&pound;<?php echo number_format($d['commission'],2)?></td><td></td></tr>
<?php }?>
</tbody>
<tfoot>
<tr><th></th><th></th><th></th><th></th><th align="right" style="text-align:right">Total: &pound;<?php echo number_format($ct,2)?></th><th></th></tr>
</tfoot>
</table>
</div>

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

<?php 
$rowAlignments="doc.content[1].table.body[i][4].alignment ='right';";
$table="commissionrep";
require('../single_table.engine');
}?>

<script>
$('#expenseReport').click(function(){
var start=$('#sdate').val()
var end=$('#edate').val()
data=$('#repdata').html()
$.post('includes/engines/report.php',{'clientnumber':'000',type:'commission_'+start+'_'+end,content:data, format:'A4-L'},function(ret){//debug(ret)
$('#genRow').html('<a href="output/reps/000_commission_'+start+'_'+end+'.pdf" target="blank" class="btn btn-info"><i class="fal fa-file-pdf"></i> VIEW REPORT</a>')
genFiles()
})

})
</script>