<?php $GLOBALS['payment_type']="Outgoings";

//echo $filter."-".$payment_type."-".$start."-".$end;

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

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

$table="expenses"; $type=""; $datetype="date"; $order="type ASC, abs(date) DESC";
$tableHeader='<thead><tr><th class="c20">DATE</th><th class="c20">TYPE</th><th class="c20">PAYEE</th><th class="c30">DETAILS</th><th class="c10">MILES</th><th class="c10">RATE</th><th class="c10">CLAIM</th><th class="c10">NET</th><th class="c10">VAT</th></tr></thead>';
$catDrop='
<div class="form-inline row noprint">
<label for="catDrop" class="col-md-1 control-label">Filter Categories:</label><select id="catDrop" name="catDrop" class="form-control autocomplete col-md-2" data-placeholder="Expense Type"><option value="">ALL</option>'.getExpenses().'</select>
</div>
<div class="row clearfix"></div>
';


$typeArray=array();
$typeArray2=array();
$stmt = $GLOBALS['master']->prepare("SELECT `desc` FROM `dd_expenses` ORDER BY `desc`")or die(mysqli_error($GLOBALS['master']));
$stmt->execute();$iv=$stmt->get_result();$stmt->close();
while($pd=mysqli_fetch_array($iv)){array_push($typeArray,$pd['desc']);}

$stmt2 = $GLOBALS['con']->prepare("SELECT `desc` FROM `dd_expenses` ORDER BY `desc`")or die(mysqli_error($GLOBALS['con']));
$stmt2->execute();$ix=$stmt2->get_result();$stmt2->close();
while($pd2=mysqli_fetch_array($ix)){array_push($typeArray2,$pd2['desc']);}

$mergedArray = array_merge($typeArray, $typeArray2);

// Remove duplicates
$uniqueMergedArray = array_unique($mergedArray, SORT_REGULAR);

// Reindex the array to ensure continuous numeric keys
$uniqueMergedArray = array_values($uniqueMergedArray);
asort($uniqueMergedArray);

$typeArray=$uniqueMergedArray;



function checkData($eType,$filter,$payment_type,$fstart,$fend){
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `expenses` WHERE `payment_type`='".$payment_type."' AND `code`='".$eType."' AND `date`>='".$fstart."' AND `date`<='".$fend."' ORDER BY `date` DESC")or die(mysqli_error($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){

$stmt = $GLOBALS['con']->prepare("SELECT * FROM `expenses` WHERE `payment_type`='".$payment_type."' AND `code`='".$eType."' AND `date`>='".$fstart."' AND `date`<='".$fend."' ORDER BY `date` DESC")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
while($data=mysqli_fetch_array($cc)){
$t=explode("|",$data['totals']);
$t[0]=str_replace(',','',$t[0]);
$t[1]=str_replace(',','',$t[1]);
$rnet=$t[0];
$rvat=$t[1];
$data['amount']=str_replace(',','',$data['amount']);
$dataset.='
<tr>
<td class="c10">'.date('d-m-Y',$data['date']).'</td>
<td class="c70">'.$data['code'].'</td>
<td class="c70">'.$data['payee'].'</td>
<td class="c70">'.$data['desc'].'</td>
<td class="c10" style="text-align:center">'.$data['vMileage'].'</td>
<td class="c10">'.$data['vRate'].'</td>
<td class="c10" style="text-align:center">'.number_format($data['amount'],2).'</td>
<td class="c10" style="text-align:center">&pound;'.number_format($rnet,2).'</td>
<td class="c10" style="text-align:center">&pound;'.number_format($rvat,2).'</td>
</tr>
';
$nTotal=$nTotal+$rnet;
$vTotal=$vTotal+$rvat;
}
$dataset.='
<tr style="border-botton:2px solid #000000">
<th colspan="7"></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 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>
</tr>

<tr>
<th colspan="9"><hr /></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>
<th class="remove" style="display: none;"></th>
</tr>
';
return $dataset;
}

function genTotal($eType,$filter,$payment_type,$fstart,$fend){
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `expenses` WHERE `payment_type`='".$payment_type."' AND `code`='".$eType."' AND `date`>='".$fstart."' AND `date`<='".$fend."' ORDER BY `type` 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']);
$t[0]=str_replace(',','',$t[0]);
$t[1]=str_replace(',','',$t[1]);
$rnet=$t[0];
$rvat=$t[1];
$nTotal=$nTotal+$rnet;
$vTotal=$vTotal+$rvat;
$mTotal=$mTotal+$data['vMileage'];
$data['amount']=str_replace(',','',$data['amount']);
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="AND type='".$eType."'";}
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `expenses` WHERE `payment_type`='".$payment_type."' AND `date`>='".$fstart."' AND `date`<='".$fend."' $sf ORDER BY `type` 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']);
$t[0]=str_replace(',','',$t[0]);
$t[1]=str_replace(',','',$t[1]);
$rnet=$t[0];
$rvat=$t[1];
$nTotal=$nTotal+$rnet;
$vTotal=$vTotal+$rvat;
$mTotal=$mTotal+$data['vMileage'];
$data['amount']=str_replace(',','',$data['amount']);
if(!empty($data['vMileage'])){
$mileage=$data['vMileage'];
$fVat=$fVat+$t[1];
}
else{
$mileage="";
$pVat=$pVat+$t[1];
}
$gTotal=$gTotal+$data['amount'];
}
return '
<tr>
<th colspan="5">Totals</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="" style="text-align:center">'.$mTotal.'</th>
<th class="" style="text-align:center">'.number_format($data['amount'],2).'</th>
<th class="" style="text-align:center">&pound;'.number_format($nTotal,2).'</th>
<th class="" style="text-align:center">&pound;'.number_format($vTotal,2).'</th>
</tr>
';
}

function genSum($eType,$filter,$payment_type,$fstart,$fend){
if(empty($filter)){}
else{$sf="AND type='".$eType."'";}
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `expenses` WHERE `payment_type`='".$payment_type."' AND `date`>='".$fstart."' AND `date`<='".$fend."' $sf ORDER BY `type` 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']);
$t[0]=str_replace(',','',$t[0]);
$t[1]=str_replace(',','',$t[1]);
$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 '
<table class="vatBox table table-bordered table-sm" style="width:50%;">
<tr><th colspan="2">VAT Analysis</th></tr>
<tr><td>VAT FROM PURCHASES</td><td>&pound;'.number_format($pVat,2).'</td></tr>
<tr><td>VAT ELEMENT FROM FUEL</td><td>&pound;'.number_format($fVat,2).'</td></tr>
<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>
';
}

if($filter==""){

foreach($typeArray as $key=>$ta){
if(checkData($ta,$filter,$payment_type,$start,$end)==0){}
else{
$tdesc=str_replace(' ','_',$ta);
$tdata.='<tr>
<th colspan="8" class="bg-dark text-white"><strong>'.$ta.'</strong></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>
<th class="bg-dark text-white"><strong>'.genTotal($ta,$filter,$payment_type,$start,$end).'</strong></th>
</tr>

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

}
}
}
else{

if(checkData($filter,$filter,$payment_type,$start,$end)==0){}
else{
$tdesc=str_replace(' ','_',$ta);
$tdata.='
<tr>
<th colspan="8" class="bg-dark text-white"><strong>'.$filter.'</strong></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>
<th class="bg-dark text-white"><strong>'.genTotal($filter,$filter,$payment_type,$start,$end).'</strong></th>
</tr>

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

}
echo $catDrop;
?>

<table id="expenseTable" class="table table-striped table-bordered table-fixed display" data-order="">
    <?php echo $tableHeader?>
    <tbody>
        <?php echo $tdata?>
    </tbody>
    <tfoot>
        <?php echo genFoot($filter,$filter,$payment_type,$start,$end)?>
    </tfoot>

</table>


<?php //$summaryData=genSum($filter,$filter,$payment_type,$start,$end);?>

<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="expenseTable";?>