<?php // INVOICES PAID

function getStatus($s){
if($s==1){return '<span class="badge badge-warning" style="display:block">Unpaid</span>';}
if($s==2){return '<span class="badge badge-danger" style="display:block">Overdue</span>';}
if($s==3){return '<span class="badge badge-success" style="display:block">Paid</span>';}
if($s==4){return '<span class="badge badge-secondary" style="display:block">Refund</span>';}
if($s==5){return '<span class="badge badge-info" style="display:block">Archived</span>';}
}

function listAgent($c){

$stmt = $GLOBALS['con']->prepare("SELECT `agent_name` FROM `agents` WHERE `agent_number`='".$c."'")or die(mysqli_error("listAgent A: ".$GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
$pd=mysqli_fetch_array($cc);
return ($pd['agent_name']);
}

if($_REQUEST['client']!=""){
$f=" AND `client`='".$_REQUEST['client']."'";
}

if($_REQUEST['agent']!=""){
$f=" AND `agent`='".$_REQUEST['agent']."'";

}

$stmt = $GLOBALS['con']->prepare("SELECT * FROM `invoices` WHERE `date`>='".$start."' AND `date`<='".$end."' AND `status`!='5' $f ORDER BY abs(date_paid) DESC");
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
$num=mysqli_num_rows($cc);
?>

<table id="expenseTable" class="table table-striped table-bordered table-responsive">
<thead><tr><th class="c20">DATE PAID</th><th class="c10">INVOICE REF</th><th class="c30">PAYEE</th><th class="c20">AGENT</th><th class="c10">DETAILS</th><th class="c10">NET</th><th class="c10">VAT</th><th class="c10">TOTAL</th><th class="c10">PAYMENT STATUS</th></tr></thead>
<tbody>

<?php while($data=mysqli_fetch_array($cc)){
if($_REQUEST['agent']!=""){$td='<td class="c20">'.listAgent($data['agent']).'</td>';$ts="<th></th>";}
$t=explode("|",$data['totals']);
$t[0]=str_replace(',','',$t[0]);
$t[1]=str_replace(',','',$t[1]);
$rnet=$t[0];
$rvat=$t[1];
$rtotal=($t[0]+$t[1]);
$nTotal=$nTotal+$rnet;
$vTotal=$vTotal+$rvat;
$mTotal=$mTotal+$data['vMileage'];

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

<?php
$totals=explode('|',$data['totals']);
$totals[0]=str_replace(',','',$totals[0]);
$totals[1]=str_replace(',','',$totals[1]);
$total=0;
$total=number_format($totals[0]+$totals[1],2);
if($data['items']!=""){$desc=explodeInvoice($data['items']);}
$rnet=number_format($t[0],2);
$rvat=number_format($t[1],2);
$rtotal=number_format($rtotal,2);
echo '
<tr>
<td class="c20">'.date('d-m-Y',$data['date_paid']).'</td>
<td class="c10">'.sprintf("%06d", $data['invNo']).'</td>
<td class="c30">'.getClientName($data['client']).'</td>
<td class="c20">'.listAgent($data['agent']).'</td>
<td class="c10">'.$desc.'</td>
<td class="c10">'.$rnet.'</td>
<td class="c10">'.$rvat.'</td>
<td class="c10">&pound;'.$rtotal.'</td>
<td class="c10 text-center">'.getStatus($data['status']).'</td>
</tr>
';
}
?>
</tbody>
<tfoot>
<tr class="footCell">
<?php
$nt=number_format($nTotal,2);
$vt=number_format($vTotal,2);

$nx=str_replace(',','',$nt);
$vx=str_replace(',','',$vt);
echo'
<th class="c20">Totals</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th class="c10">&pound;'.$nt.'</th>
<th class="c10">&pound;'.$vt.'</th>
<th class="c10">&pound;'.number_format($nx+$vx,2).'</th>
<th class="c10"></th>
';
?>
</tr>
</tfoot>
</table>
<?php $table="expenseTable";?>