<?php echo $filter;

if(empty($_REQUEST['filter'])){$filter=""; $label="";}

else{
$filter=$_REQUEST['filter']; echo "<hr><h4><strong>Showing results for ".$_REQUEST['filter']."</strong></h4>";}
?>

<table id="dataExpenseTable" class="table table-striped table-bordered table-fixed">
<thead><tr><th class="c20">DATE PAID</th><th class="c10">INVOICE REF</th><th class="c30">PAYEE</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 TYPE</th></tr></thead>
<tbody>

<?php
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `invoices` WHERE `status`='3' AND `date_paid`>='".$start."' AND `date_paid`<='".$end."' ORDER BY abs(date_paid) DESC")or die(mysqli_error("listClient B: ".$GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
$num=mysqli_num_rows($cc);

function serviceOptions(){
$services=array(
array("code"=>"CONS","desc"=>"Consultancy Work"),
array("code"=>"INSP","desc"=>"In Service Inspection"),
array("code"=>"PP","desc"=>"Prior to purchase Inspection"),
array("code"=>"SS","desc"=>"Special Service"),
array("code"=>"TECH","desc"=>"Technical Service")
);
foreach($services as $arrays=>$values){
$ret.='<option value="'.$values['desc'].'">'.$values['desc'].'</option>';
}
return $ret;
}


$catDrop='
<div class="form-inline row">
<label for="catDrop" class="col-md-1 control-label">Filter Categories:</label><select id="catDrop" name="catDrop" class="form-control autocomplete col-md-2">'.serviceOptions().'</select>
</div>
<div class="row clearfix"></div>
';


$services=array(
array("code"=>"CONS","desc"=>"Consultancy Work"),
array("code"=>"INSP","desc"=>"In Service Inspection"),
array("code"=>"PP","desc"=>"Prior to purchase Inspection"),
array("code"=>"SS","desc"=>"Special Service"),
array("code"=>"TECH","desc"=>"Technical Service")
);
foreach($services as $arrays=>$values){
$typeArray[]=$values['desc'];
}


function checkData($eType,$filter,$payment_type,$fstart,$fend){
$stmt = $GLOBALS['con']->prepare("SELECT `invNo` FROM `invoices` WHERE `filter`='".$eType."' AND `date_paid`>='".$fstart."' AND `date_paid`<='".$fend."'")or die(mysqli_error("listClient B: ".$GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
if(mysqli_num_rows($cc)==0){return "0";}
else{return "1";}
}

function genData($eType,$filter,$payment_type,$fstart,$fend){
if(empty($filter)){}
else{$sf="`filter`='".$eType."' AND ";}
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `invoices` WHERE $sf `date_paid`>='".$fstart."' AND `date_paid`<='".$fend."' ORDER BY `date_paid` ASC")or die(mysqli_error("listClient B: ".$GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
while($data=mysqli_fetch_array($cc)){
$t=explode("|",$data['totals']);
$rnet=$t[0];
$rvat=$t[1];
$details="";
$items=unserialize($data['items']);
if(!empty($items)){
foreach($items as $item){
$details.=$item['service'].",";
}
}
$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);
$dataset.='
<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="c10">'.$details.'</td>

<td class="c10">&pound;'.number_format($totals[0],2).'</td>
<td class="c10">&pound;'.number_format($totals[1],2).'</td>
<td class="c10">&pound;'.$total.'</td>
<td class="c10">'.$data['payment_type'].'</td>
</tr>
';
$nTotal=$nTotal+$rnet;
$vTotal=$vTotal+$rvat;
}
$dataset.='
<tr style="border-botton:2px solid #000000">
<th colspan="4"></th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
<th style="border-top:1px solid #000000; border-bottom:1px solid #000000; text-align:center">'.number_format($nTotal,2).'</th>
<th style="border-top:1px solid #000000; border-bottom:1px solid #000000; text-align:center">'.number_format($vTotal,2).'</th>
<th style="border-top:1px solid #000000; border-bottom:1px solid #000000; text-align:center">'.number_format($nTotal+$vTotal,2).'</th>
<th></th>
</tr>

<tr>
<th colspan="8"><hr class="rule"/></th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
</tr>
';
return $dataset;
}

function genTotal($eType,$filter,$payment_type,$fstart,$fend){
if(empty($filter)){}
else{$sf="`filter`='".$eType."' AND ";}
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `invoices` WHERE $sf  `date_paid`>='".$fstart."' AND `date_paid`<='".$fend."' ORDER BY `date_paid` ASC")or die(mysqli_error("listClient B: ".$GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
while($data=mysqli_fetch_array($cc)){
$t=explode("|",$data['totals']);
$rnet=$t[0];
$rvat=$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'];
}
return number_format($gTotal,2);
}

function genFoot($eType,$filter,$payment_type,$fstart,$fend){
if(empty($filter)){}
else{$sf="`filter`='".$eType."' AND ";}
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `invoices` WHERE $sf `date_paid`>='".$fstart."' AND `date_paid`<='".$fend."' ORDER BY `date_paid` ASC")or die(mysqli_error("listClient B: ".$GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
while($data=mysqli_fetch_array($cc)){
$t=explode("|",$data['totals']);
$rnet=$t[0];
$rvat=$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'];
}
return '
<tr>
<th class="c20">Totals</th>
<th></th>
<th></th>
<th></th>
<th class="c10">&pound;'.number_format($nTotal,2).'</th>
<th class="c10">&pound;'.number_format($vTotal,2).'</th>
<th class="c10">&pound;'.number_format($nTotal+$vTotal,2).'</th>
<th class="c10"></th>
</tr>
';
}

if($filter==""){

foreach($typeArray as $ta){
if(checkData($ta,$filter,$payment_type,$start,$end)==0){}
else{
$tdata.='<tr>
<th colspan="7">'.$ta.'</th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
<th>'.genTotal($ta,$filter,$payment_type,$start,$end).'</th>
</tr>

';
$tdata.=genData($ta,$filter,$payment_type,$start,$end);
}
}
}
else{
if(checkData($filter,$filter,$payment_type,$start,$end)==0){}
else{
$tdata.='<tr>
<th colspan="7">'.$filter.'</th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
<th class="remove" style="display: none;"></th>
<th>'.genTotal($filter,$filter,$payment_type,$start,$end).'</th>
</tr>

';
$tdata.=genData($filter,$filter,$payment_type,$start,$end);

}
}


echo $catDrop;
?>

<?php echo $tdata?>
</tbody>
<tfoot>
<?php echo genFoot($filter,$filter,$payment_type,$start,$end)?>
</tfoot>

</table>
</div>
<script>
$('#catDrop').change(function(){
var s='<?php echo $_REQUEST['start']?>'
var e='<?php echo $_REQUEST['end']?>'
var t='<?php echo $_REQUEST['type']?>'
var ftr=$('#catDrop').val()
$.post('includes/finance/reports_data.php',{type:t,start:s,end:e,filter:ftr},function(r){
if($.trim(r)!=="error"){$('#reportData').html(r)}

})


})

</script>
<?php $table="dataExpenseTable";?>