<?php require('../../includes/connection.php');if(!empty($_REQUEST['FUNCTION'])){$_REQUEST['src']='ext2';
require('../../functions/global.php');
}

//////////////////////////
/// PAYMENT STATUS     ///
/// 1 - Unpaid         ///
/// 2 - Reminder       ///
/// 3 - Paid           ///
/// 4 - Refund         ///
/// 5 - Archive        ///
//////////////////////////


$GLOBALS['type']=$_REQUEST['type'];
$pt=str_replace(' by Category','',$_REQUEST['type']);
$GLOBALS['start']=strtotime($_REQUEST['start']);
$GLOBALS['end']=strtotime($_REQUEST['end']);
$GLOBALS['payment_type']=$pt;
function getExpenses(){
$stmt = $GLOBALS['master']->prepare("SELECT * FROM `dd_expenses` ORDER BY `desc`")or die(mysqli_error("GET_EXPENSES: ".$GLOBALS['master']));
$stmt->execute();$iv=$stmt->get_result();$stmt->close();
$ret.='<option value="">Expense Type</option>';
while($pd=mysqli_fetch_array($iv)){
$ret.='<option value="'.$pd['desc'].'">'.$pd['desc'].'</option>';
}
return $ret;
}

function explodeInvoice($string){
$items = unserialize($string);
if(!empty($items)){
foreach($items as $item){
$ret.=$item['service'].', ';
}
return $ret;
}}

function explodeAddress($string){
$items = unserialize($string);

$ret=getClientName($item['client_name']);

print_R($items);
}


function getClientName($clientnumber){
$stmt = $GLOBALS['con']->prepare("SELECT `clientname` FROM `clientdetails` WHERE `clientnumber`=?")or die(mysqli_error("getItemName: ".$GLOBALS['con']));
$stmt->bind_param('i',$clientnumber);$stmt->execute();$rc=$stmt->get_result();$stmt->close();	
$cd=mysqli_fetch_array($rc);	
return $cd['clientname'];
}

function getClientContact($clientnumber,$type){
    $stmt = $GLOBALS['con']->prepare("SELECT `billing_name`,`phone1`,`email` FROM `clientdetails` WHERE `clientnumber`=?")or die(mysqli_error("getItemName: ".$GLOBALS['con']));
    $stmt->bind_param('i',$clientnumber);$stmt->execute();$rc=$stmt->get_result();$stmt->close();	
    $cd=mysqli_fetch_array($rc);
    switch($type){
    case "phone":return $cd['phone1'];
    case "email":return $cd['email'];
    case "billing":return $cd['billing_name'].' - '.$cd['phone1'];
    }
    
    }
    
?>

<div id="expenseData">
    <p id="exportTitle" class="lead reportHeader"><?php echo $type?> for the period <b><?php echo date('d-m-Y',$start)?></b> to <b><?php echo date('d-m-Y',$end)?></b></p>
    <?php
switch($_REQUEST['type']){
case "Invoice Paid":
$rowAlignments="
doc.content[1].table.body[i][4].alignment = 'right';
doc.content[1].table.body[i][5].alignment = 'right';
doc.content[1].table.body[i][6].alignment = 'right';
doc.content[1].table.body[i][7].alignment = 'right';
";
require('finance_invoices_paid_data.php');
$dateTarget='0';
require('../finance_table.engine');
break;

case "Invoice Outstanding":
$rowAlignments="
doc.content[1].table.body[i][4].alignment = 'right';
doc.content[1].table.body[i][5].alignment = 'right';
doc.content[1].table.body[i][6].alignment = 'right';
";
require('finance_invoices_outstanding_data.php');
$dateTarget='0';
require('../finance_table.engine');
break;

case "Invoice All":
$rowAlignments="
doc.content[1].table.body[i][4].alignment = 'right';
doc.content[1].table.body[i][5].alignment = 'right';
doc.content[1].table.body[i][6].alignment = 'right';
doc.content[1].table.body[i][7].alignment = 'right';
";
require('finance_invoices_all_data.php');
$dateTarget='0';
require('../finance_table.engine');
break;

case "Invoice Outstanding":
$rowAlignments="
doc.content[1].table.body[i][4].alignment = 'right';
doc.content[1].table.body[i][5].alignment = 'right';
doc.content[1].table.body[i][6].alignment = 'right';
";
require('finance_invoices_outstanding_data.php');
$dateTarget='0';
require('../finance_table.engine');
break;


case "Outgoings":
$rowAlignments="
doc.content[1].table.body[i][3].alignment = 'right';
doc.content[1].table.body[i][4].alignment = 'right';
doc.content[1].table.body[i][5].alignment = 'right';
doc.content[1].table.body[i][6].alignment = 'right';
doc.content[1].table.body[i][7].alignment = 'right';
";
require('finance_invoices_outgoings_data.php');
$dateTarget='0';
require('../finance_table.engine');
break;

case "Outgoings by Category":
$rowAlignments="
doc.content[1].table.body[i][3].alignment = 'right';
doc.content[1].table.body[i][4].alignment = 'right';
doc.content[1].table.body[i][5].alignment = 'right';
doc.content[1].table.body[i][6].alignment = 'right';
doc.content[1].table.body[i][7].alignment = 'right';
";
require('finance_categoryBreakdown.php');
$dateTarget='0';
require('../finance_table.engine');
break;

case "Receipts":
$rowAlignments="
doc.content[1].table.body[i][4].alignment = 'right';
doc.content[1].table.body[i][5].alignment = 'right';
doc.content[1].table.body[i][6].alignment = 'right';
doc.content[1].table.body[i][7].alignment = 'right';
";
require('finance_invoices_receipts_data.php');
$dateTarget='0';
require('../finance_table.engine');
break;

case "Receipts by Category":
$rowAlignments="
doc.content[1].table.body[i][3].alignment = 'right';
doc.content[1].table.body[i][4].alignment = 'right';
doc.content[1].table.body[i][5].alignment = 'right';
doc.content[1].table.body[i][6].alignment = 'right';
doc.content[1].table.body[i][7].alignment = 'right';
";
require('finance_invoiceBreakdown.php');
$dateTarget='0';
require('../finance_table.engine');
break;

case "VAT In":
$rowAlignments="
doc.content[1].table.body[i][4].alignment = 'right';
doc.content[1].table.body[i][5].alignment = 'right';
doc.content[1].table.body[i][6].alignment = 'right';
";
require('finance_invoices_vatIn_data.php');
$dateTarget='0';
require('../finance_table.engine');
break;
case "VAT Out":
$rowAlignments="
doc.content[1].table.body[i][3].alignment = 'right';
doc.content[1].table.body[i][4].alignment = 'right';
doc.content[1].table.body[i][5].alignment = 'right';
";
require('finance_invoices_vatOut_data.php');
$dateTarget='0';
require('../finance_table.engine');
break;
}
?>
    <div class="clearfix"></div>

    <?php //	SUMMARY		///////////////////////////////////////////////////////////////////////////////

switch($_REQUEST['type']){
case "Invoice Paid":
$summaryData='
<table class="vatBox table table-bordered table-sm" style="width:50%;">
<tr><th colspan="2">VAT Analysis</th></tr>
<tr><th>TOTAL VAT INPUT</th><th>&pound;'.number_format($vTotal,2).'</th></tr>
<tr><td>CLAIM EXCLUDING VAT</td><td>&pound;'.number_format($nTotal,2).'</td></tr>
</table>
';break;

case "Invoice Outstanding":
$summaryData='
<table class="vatBox table table-bordered table-sm" style="width:50%;">
<tr><th colspan="2">VAT Analysis</th></tr>
<tr><th>TOTAL VAT INPUT</th><th>&pound;'.number_format($vTotal,2).'</th></tr>
<tr><td>OUTSTANDING EXCLUDING VAT</td><td>&pound;'.number_format($nTotal,2).'</td></tr>
</table>
';break;

case "Outgoings":
$summaryData='
<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($nTotal,2).'</td></tr>
</table>
';
break;

case "Outgoings by Category":

break;

case "Receipts":
$summaryData='
<table class="vatBox table table-bordered table-sm" style="width:50%;">
<tr><th colspan="2">VAT Analysis</th></tr>
<tr><th>TOTAL VAT INPUT</th><th>&pound;'.number_format($vTotal,2).'</th></tr>
<tr><td>CLAIM EXCLUDING VAT</td><td>&pound;'.number_format($nTotal,2).'</td></tr>
</table>
';
break;

case "Receipts by Categorys":
$summaryData='
<table class="vatBox table table-bordered table-sm" style="width:50%;">
<tr><th colspan="2">VAT Analysis</th></tr>
<tr><th>TOTAL VAT INPUT</th><th>&pound;'.number_format($pVat,2).'</th></tr>
<tr><td>CLAIM EXCLUDING VAT</td><td>&pound;'.number_format($iTotal,2).'</td></tr>
</table>
';
break;


case "VAT In":
$summaryData='
<table class="vatBox table table-bordered table-sm" style="width:50%;">
<tr><th colspan="2">VAT Analysis</th></tr>
<tr><th>TOTAL VAT PAYABLE</th><th>&pound;'.number_format($vTotal,2).'</th></tr>
<tr><td>INCOME EXCLUDING VAT</td><td>&pound;'.number_format($nTotal,2).'</td></tr>
</table>
';
break;

case "VAT Out":
$summaryData='
<table class="vatBox table table-bordered table-sm" style="width:50%;">
<tr><th colspan="2">VAT Analysis</th></tr>
<tr><th>TOTAL VAT CLAIM</th><th>&pound;'.number_format($vTotal,2).'</th></tr>
<tr><td>OUTGOINGS EXCLUDING VAT</td><td>&pound;'.number_format($nTotal,2).'</td></tr>
</table>
';
break;
}
////////////////////////////////////////////////////////////////////////////////////////////////?>

    <div class="clearfix"></div>
    <?php echo $summaryData;?>

</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>

<div class="clearfix col-md-12"></div>



<?php


?>


<script>
$('#catDrop').select2()
$('#filterDate').click(function() {
    var start = $('#date1').val()
    var end = $('#date2').val()
    $('#expenseList').load('includes/forms/elements/finance_exp_data.php?type=<?php echo $_REQUEST['type']?>&start=' + start + '&end=' + end)
})

$(window).resize(function() {
    if ($(window).width() < 760) {
        $('#toIcon').removeClass('fa-arrow-alt-circle-right')
        $('#toIcon').addClass('fa-arrow-alt-circle-down')
    } else {
        $('#toIcon').removeClass('fa-arrow-alt-circle-down')
        $('#toIcon').addClass('fa-arrow-alt-circle-right')
    }
})

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

})
</script>