<?php


require_once('helpers/querys.php');

$db = new Conexion;
$user = new User;
$core = new Core;
$userData = $user->cdp_getUserData();

$range = $_REQUEST['range'];

$sWhere = "";
$name_off=$userData->name_off;

if ($customer_id > 0) {

    $sWhere .= " and b.sender_id = '" . $customer_id . "'";
}


if ($pay_mode > 0) {

    $sWhere .= " and a.payment_type = '" . $pay_mode . "'";
}


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 .= " and  created_date between '" . $fecha_inicio . "'  and '" . $fecha_fin . "'";
}

if($name_off == 'undefined'){
    
  $sql = "SELECT * FROM expenses_master 
        Where created_date between '" . $fecha_inicio . "'  and '" . $fecha_fin . "'
        and id > 1
        and currency = '$'
        order by created_date desc";  
    
} else{
    
   $sql = "SELECT * FROM expenses_master
        Where created_date between '" . $fecha_inicio . "'  and '" . $fecha_fin . "'
        and currency = '$'
        and name_off='$name_off'
        order by created_date desc"; 
}

if($name_off == 'undefined'){

$db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as total FROM expenses_master where created_date between '" . $fecha_inicio . "'  and '" . $fecha_fin . "' and currency='$'");
$db->cdp_execute();
$row = $db->cdp_registro();
$dexp = $row->total;

$db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as total FROM expenses_master where created_date between '" . $fecha_inicio . "'  and '" . $fecha_fin . "' and currency='Ksh.'");
$db->cdp_execute();
$row = $db->cdp_registro();
$kexp = $row->total;    
    
}else{
    
$db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as total FROM expenses_master where created_date between '" . $fecha_inicio . "'  and '" . $fecha_fin . "' and currency='$' and name_off='$name_off' ");
$db->cdp_execute();
$row = $db->cdp_registro();
$dexp = $row->total;

$db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as total FROM expenses_master where created_date between '" . $fecha_inicio . "'  and '" . $fecha_fin . "' and currency='Ksh.' and name_off='$name_off' ");
$db->cdp_execute();
$row = $db->cdp_registro();
$kexp = $row->total;

}

$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);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="theme-color" content="#ffffff">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- Tell the browser to be responsive to screen width -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <!-- Favicon icon -->
    <link rel="icon" type="image/png" sizes="16x16" href="assets/<?php echo $core->favicon ?>">

    <title>Expense Report - USD</title>
    <link href="assets/custom_dependencies/print_report.css" rel="stylesheet">


</head>

<body>
    <div id="page-wrap">

        <h2 style="border: 0;  text-align: center">
            <?php echo ($core->logo) ? '<img src="assets/uploads/galeyr.png"/>' : $core->site_name; ?>
            <br/>
            <?php echo $core->site_name; ?><br>
            Expense Report - USD<br>

            [<?php echo $fecha[0] . ' - ' . $fecha[1]; ?>] <br>


        </h2>


        <table>
            <tr>
                <th><b>Expense Title</b></th>
				<th><b>Expense Type</b></th>
				<th><b>User</b></th>
				<th><b>Expense Amount</b></th>
				<th><b>Expense Agency</b></th>
				<th><b>Created Date</b></th>
            </tr>

            <?php

            if ($numrows > 0) {

                $count = 1;
                $sumador_total = 0;

                foreach ($data as $row) {

                    $db->cdp_query("SELECT * FROM cdb_users where id= '" . $row->sender_id . "'");
						$sender_data = $db->cdp_registro();
						
						$db->cdp_query("SELECT * FROM cdb_users where id= '" . $row->receiver_id . "'");
						$receiver_data = $db->cdp_registro();
						
						$db->cdp_query("SELECT currency FROM cdb_add_order WHERE order_id='" . $row->order_id . "'");
                        $curr = $db->cdp_registro();

						$db->cdp_query("SELECT * FROM cdb_address_shipments where order_track='" . $row->order_prefix . $row->order_no . "'");
						$address_order = $db->cdp_registro();

						$db->cdp_query("SELECT * FROM cdb_courier_com where id= '" . $row->order_courier . "'");
						$courier_com = $db->cdp_registro();

						$db->cdp_query("SELECT * FROM cdb_styles where id= '14'");
						$status_style_pickup = $db->cdp_registro();

						$db->cdp_query("SELECT * FROM cdb_styles where id= '13'");
						$status_style_consolidate = $db->cdp_registro();
						
						$db->cdp_query('SELECT IFNULL(SUM(expense_amount),0) as total FROM expenses_master where currency="Ksh."');
                        $db->cdp_execute();
                        $ksum = $db->cdp_registro();
                        $sum1 = $ksum->total;
                        
                        $db->cdp_query('SELECT IFNULL(SUM(expense_amount),0) as total FROM expenses_master where currency="$"');
                        $db->cdp_execute();
                        $usum = $db->cdp_registro();
                        $sum2 = $usum->total;
						
						if ($row->status_invoice == 1) {
							$text_status = $lang['invoice_paid'];
							$label_class = "label-success";
						} else if ($row->status_invoice == 2) {
							$text_status = $lang['invoice_pending'];
							$label_class = "label-warning";
						} else if ($row->status_invoice == 3) {
							$text_status = $lang['invoice_due'];
							$label_class = "label-danger";
						}

            ?>
                    <tr class="card-hover">

							<td>
							    <b><?php echo $row->expense_title; ?></b>
							</td>
							<td class="text-center">
								<?php echo $row->expense_type; ?>
							</td>

							<td class="text-center">
								<?php echo $row->pre_user; ?>
							</td>

							<td class="text-center">
								<?php echo $row->currency; ?><?php echo number_format($row->expense_amount); ?>

							</td>
							
							<td class="text-center">
								<?php echo $row->name_off; ?>
							</td>

							<td class="text-center">
								<?php echo $row->created_date; ?>
							</td>

						</tr>
                <?php

                    $count++;
                }
                ?>

                <tr>
                    <td class="text-left"><b>TOTAL</b></td>


                    <td colspan="2"></td>
                    <td class="text-left">
                        <b>USD.<?php echo number_format($dexp); ?> </b>
                    </td>
                </tr>
            <?php
            }
            ?>


        </table>

        <button class='button -dark center no-print' onClick="window.print();" style="font-size:16px; margin-top: 20px;">Print &nbsp;&nbsp; <i class="fa fa-print"></i></button>
    </div>

</body>

</html>