<?php


header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=Report-Expenses_" . date('d-m-Y') . ".xls");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);



$db = new Conexion;

$created_date = cdp_sanitize($_REQUEST['created_date']);
$expense_title = intval($_REQUEST['expense_title']);
$expense_type = intval($_REQUEST['expense_type']);
$pre_user = intval($_REQUEST['pre_user']);
$expense_amount = intval($_REQUEST['expense_amount']);
$id = intval($_REQUEST['id']);

$range = $_REQUEST['range'];

$sWhere = "";

if (!empty($range)) {

    $fecha =  explode(" - ", $range);
    $fecha = str_replace('/', '-', $fecha);

    $fecha_inicio = date('Y-m-d', strtotime($fecha[0]));
    $fecha_fin = date('Y-m-d', strtotime($fecha[1]));


    $sWhere .= " created_date between '" . $fecha_inicio . "'  and '" . $fecha_fin . "'";
}


$db->cdp_execute();


$sql = "SELECT * FROM expenses_master
        $sWhere
		order by id asc
        ";


$query_count = $db->cdp_query($sql);
$db->cdp_execute();
$numrows = $db->cdp_rowCount();


$db->cdp_query($sql);
$data = $db->cdp_registros();

$fecha = str_replace('-', '/', $fecha);

$html = '
	<html>
		<body>
		
		<h2>' . $core->site_name . '<br>
		Expenses Report <br>

		</h2>


		<table border=1>
		<tbody>
			<tr style="background-color: #3e5569; color: white">	
			
				<th><b>ID</b></th>
                <th class="text-center"><b>User</b></th>
				<th><b>Expense Title</b></th>
                <th class="text-center"><b>Created Date</b></th>
                <th class="text-center"><b>Expense Type</b></th>                
                <th class="text-center"><b>Expense Amount</b></th>                
                <th class="text-center"><b>Currency</b></th>              
			</tr>';

if ($numrows > 0) {

    $count = 1;
    $sumador_pendiente = 0;
    $sumador_total = 0;
    $sumador_pagado = 0;

    foreach ($data as $row) {

        $db->cdp_query('SELECT * FROM expenses_master');
		$expense_data = $db->cdp_registro();
						
		$db->cdp_query("SELECT currency FROM expenses_master WHERE id='" . $row->id . "'");
        $curr = $db->cdp_registro();

        $count++;



        $html .= '<tr>';
        $html .= '<td >' . $expense_data->id . '</td>';
        $html .= '<td >' . $expense_data->pre_user . '</td>';
        $html .= '<td>' . $expense_data->expense_title . '</td>';
        $html .= '<td >' . $expense_data->created_date . '</td>';
        $html .= '<td >' . $expense_data->expense_type . '</td>';
        $html .= '<td >' . $expense_data->expense_amount . '</td>';
        $html .= '<td>' . $expense_data->currency . '</td>';
        $html .= '</tr>';
    }
}

$html .= '</table></html>';
echo utf8_decode($html);
