<?php


$userData = $user->cdp_getUserData();

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

$month = date('m');
$year = date('Y');

$sql = "SELECT * FROM cdb_add_order where status_courier!=21 and order_payment_method >1  and month(order_date)='$month' AND year(order_date)='$year' order by order_id desc";



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

//$db->cdp_query("SELECT currency FROM cdb_add_order WHERE order_id='" . $row->order_id . "'");
//$curr = $db->cdp_registro();

$curr = '$';

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

foreach ($data as $row) {



    $db->cdp_query('SELECT  IFNULL(sum(total), 0)  as total  FROM cdb_charges_order WHERE order_id=:order_id');

    $db->bind(':order_id', $row->order_id);

    $db->cdp_execute();

    $sum_payment = $db->cdp_registro();

    $pendiente = $row->total_order - $sum_payment->total;

    $sumador_pendiente += $pendiente;
    $sumador_total += $row->total_order;
    $sumador_pagado += $sum_payment->total;


    $count++;
}

?>
<!DOCTYPE html>
<html dir="ltr" lang="en">

<head>
    <meta charset="utf-8">
    <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="keywords" content="Galeyr Cargo Web System" />
    <meta name="author" content="Myne">
    <title>Dashboard Income/Expense | <?php echo $core->site_name ?></title>

    <!-- Favicon icon -->
    <link rel="icon" type="image/png" sizes="16x16" href="assets/<?php echo $core->favicon ?>">

    <title></title>
    <!-- Custom CSS -->
    <link href="assets/css/style.min.css" rel="stylesheet">
    <link href="assets/css/front.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="assets/js/jquery.js"></script>
    <script type="text/javascript" src="assets/js/jquery-ui.js"></script>
    <script src="assets/js/jquery.ui.touch-punch.js"></script>
    <script src="assets/js/jquery.wysiwyg.js"></script>
    <script src="assets/js/global.js"></script>
    <script src="assets/js/custom.js"></script>
    <link href="assets/customClassPagination.css" rel="stylesheet">
    <script src="assets/libs/chart.js-2.8/Chart.min.js"></script>

</head>

<body>

    <div id="main-wrapper">
        <!-- ============================================================== -->
        <!-- Preloader - style you can find in spinners.css -->
        <!-- ============================================================== -->

        <?php include 'views/inc/preloader.php'; ?>

        <!-- ============================================================== -->
        <!-- Preloader - style you can find in spinners.css -->
        <!-- ============================================================== -->

        <?php include 'views/inc/topbar.php'; ?>

        <!-- End Topbar header -->


        <!-- Left Sidebar - style you can find in sidebar.scss  -->

        <?php include 'views/inc/left_sidebar.php'; ?>


        <!-- End Left Sidebar - style you can find in sidebar.scss  -->

        <!-- Page wrapper  -->

        <div class="page-wrapper">

            <div class="page-breadcrumb">
                <div class="row">
                    <div class="col-5 align-self-center">
                        <h4 class="page-title">Dashboard</h4>
                    </div>
                </div>
            </div>
            <!-- ============================================================== -->
            <!-- End Bread crumb and right sidebar toggle -->
            <!-- ============================================================== -->
            <!-- ============================================================== -->
            <!-- Container fluid  -->
            <!-- ============================================================== -->
            
            <div class="container-fluid">


                <!-- ============================================================== -->
                <!-- Earnings -->
                <!-- ============================================================== -->
                <div class="row">
                    
                    <div class="col-sm-6 col-lg-6">
                        <div class="card">
                            <div class="card-body">
                                <div class="d-md-flex align-items-center">
                                    <div>
                                        <h4 class="card-title">Income Graph - KES</h4>

                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-lg-12 col-md-12">
                                        
                                        <canvas id="myChart" style="height: 220px;"></canvas>
                                       
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6 col-lg-6">
                        <div class="card">
                            <div class="card-body">
                                <div class="d-md-flex align-items-center">
                                    <div>
                                        <h4 class="card-title">Income Graph - USD</h4>

                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-lg-12 col-md-12">
                                        
                                        <canvas id="myChartUS" style="height: 220px;"></canvas>
                                       
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- col -->
                    <div class="col-sm-6 col-lg-6">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="card-title">Total Income - Ksh.</h4>
                                <h5 class="card-subtitle">Overview of Latest Month</h5>

                                <div class="row m-t-20 m-b-20">
                                    <div class="col-sm-12 col-md-12 col-lg-12">
                                        <ul class="list-style-none">

                                            <li class="m-t-10">
                                                <div class="d-flex align-items-center">
                                                    <div>
                                                        <span class="text-muted">Total Income - Ksh.</span>
                                                        <h4 class="m-b-0">
                                                            <span class="font-16">
                                                                Ksh. 
                                                                <?php

                                                                if($name_off == 'undefined'){
                                                                    
                                                                   $db->cdp_query('SELECT IFNULL(SUM(total_order),0) as total FROM cdb_add_order where status_courier!=22 and  status_invoice!=0 and order_payment_method>1 and currency="Ksh." and MONTH(order_date)=MONTH(now())');
                                                            
                                                                }else{
                                                                    
                                                                    $db->cdp_query("SELECT IFNULL(SUM(total_order),0) as total FROM cdb_add_order a
                                                                                    INNER JOIN cdb_users as b ON a.sender_id = b.id
                                                                                    INNER JOIN cdb_offices c on (a.origin_off=c.id)
                                                                                    WHERE a.status_courier!=22 and a.status_invoice!=0 and a.order_payment_method>1 and a.currency='Ksh.'
                                                                                    and c.name_off='$name_off' and MONTH(a.order_date)=MONTH(now())");            
                                                                    
                                                                }
                                                                $db->cdp_execute();
    
                                                                $count = $db->cdp_registro();
    
                                                                echo number_format($count->total);
                                                                ?>
                                                            </span>
                                                        </h4>
                                                    </div>
                                                </div>
                                                <div class="progress m-t-10">
                                                    <div class="progress-bar bg-info" role="progressbar" style="width: <?php echo cdb_money_format($sumador_total) / 100; ?>%" aria-valuenow="47" aria-valuemin="0" aria-valuemax="100"></div>
                                                </div>
                                            </li>

                                            <li class="m-t-10">
                                                <div class="d-flex align-items-center">
                                                    <div>
                                                        <span class="text-muted">Total accounts paid</span>
                                                        <h4 class="m-b-0">
                                                            <span class="font-16">

                                                                Ksh. 
                                                                <?php
                                                                
                                                                if($name_off == 'undefined'){
                                                                    
                                                                    $db->cdp_query('SELECT IFNULL(SUM(total_order),0) as total FROM cdb_add_order where status_invoice=1 and order_payment_method>1 and currency="Ksh." and MONTH(order_date)=MONTH(now())');
                                                                    
                                                                }else{
                                                                    
                                                                    $db->cdp_query("SELECT IFNULL(SUM(total_order),0) as total FROM cdb_add_order a
                                                                                    INNER JOIN cdb_users as b ON a.sender_id = b.id
                                                                                    INNER JOIN cdb_offices c on (a.origin_off=c.id)
                                                                                    WHERE a.status_invoice=1 and a.order_payment_method>1 and a.currency='Ksh.'
                                                                                    and c.name_off='$name_off' and MONTH(a.order_date)=MONTH(now())");

                                                                }
    
                                                                $db->cdp_execute();
    
                                                                $count = $db->cdp_registro();
    
                                                                echo number_format($count->total);
                                                                ?>
                                                            </span>
                                                        </h4>
                                                    </div>
                                                </div>
                                                <div class="progress m-t-10">
                                                    <div class="progress-bar bg-success" role="progressbar" style="width: <?php echo cdb_money_format($sumador_pagado) / 100; ?>%" aria-valuenow="47" aria-valuemin="0" aria-valuemax="100"></div>
                                                </div>
                                            </li>
                                            <li class="m-t-10">
                                                <div class="d-flex align-items-center">
                                                    <div>
                                                        <span class="text-muted">Total pending accounts</span>
                                                        <h4 class="m-b-0">
                                                            <span class="font-16">

                                                                Ksh. 
                                                                <?php
                                                                
                                                                if($name_off == 'undefined'){
                                                                    
                                                                    $db->cdp_query('SELECT IFNULL(SUM(total_order),0) as total FROM cdb_add_order where status_invoice!=1 and order_payment_method>1 and currency="Ksh." and MONTH(order_date)=MONTH(now())');
                                                                    
                                                                }else{
                                                                    
                                                                    $db->cdp_query("SELECT IFNULL(SUM(total_order),0) as total FROM cdb_add_order a
                                                                                    INNER JOIN cdb_users as b ON a.sender_id = b.id
                                                                                    INNER JOIN cdb_offices c on (a.origin_off=c.id)
                                                                                    WHERE a.status_invoice!=1 and a.order_payment_method>1 and a.currency='Ksh.'
                                                                                    and c.name_off='$name_off' and MONTH(a.order_date)=MONTH(now())");
                                                                }

                                                                $db->cdp_execute();
    
                                                                $count = $db->cdp_registro();
    
                                                                echo number_format($count->total);
                                                                ?>
                                                            </span>
                                                        </h4>
                                                    </div>
                                                </div>
                                                <div class="progress m-t-10">
                                                    <div class="progress-bar bg-orange" role="progressbar" style="width: <?php echo cdb_money_format($sumador_pendiente) / 100; ?>%" aria-valuenow="47" aria-valuemin="0" aria-valuemax="100"></div>
                                                </div>
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6 col-lg-6">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="card-title">Total Income USD</h4>
                                <h5 class="card-subtitle">Overview of Latest Month</h5>

                                <div class="row m-t-20 m-b-20">
                                    <div class="col-sm-12 col-md-12 col-lg-12">
                                        <ul class="list-style-none">

                                            <li class="m-t-10">
                                                <div class="d-flex align-items-center">
                                                    <div>
                                                        <span class="text-muted">Total Income - USD.</span>
                                                        <h4 class="m-b-0">
                                                            <span class="font-16">
                                                                $ 
                                                            <?php
                                                            
                                                            if($name_off == 'undefined'){
                                                                    
                                                                   $db->cdp_query("SELECT IFNULL(SUM(total_order),0) as total FROM cdb_add_order where status_courier!=22 and  status_invoice!=0 and order_payment_method>1 and currency='$' and MONTH(order_date)=MONTH(now())");
                                                            
                                                            }else{
                                                                    $db->cdp_query("SELECT IFNULL(SUM(total_order),0) as total FROM cdb_add_order a
                                                                                    INNER JOIN cdb_users as b ON a.sender_id = b.id
                                                                                    INNER JOIN cdb_offices c on (a.origin_off=c.id)
                                                                                    WHERE a.status_courier!=22 and a.status_invoice!=0 and a.order_payment_method>1 and a.currency='$'
                                                                                    and c.name_off='$name_off' and MONTH(a.order_date)=MONTH(now())");
                                                            }

                                                            $db->cdp_execute();

                                                            $count = $db->cdp_registro();

                                                            echo number_format($count->total);
                                                            ?>
                                                            </span>
                                                        </h4>
                                                    </div>
                                                </div>
                                                <div class="progress m-t-10">
                                                    <div class="progress-bar bg-info" role="progressbar" style="width: <?php echo cdb_money_format($sumador_total) / 100; ?>%" aria-valuenow="47" aria-valuemin="0" aria-valuemax="100"></div>
                                                </div>
                                            </li>

                                            <li class="m-t-10">
                                                <div class="d-flex align-items-center">
                                                    <div>
                                                        <span class="text-muted">Total accounts paid</span>
                                                        <h4 class="m-b-0">
                                                            <span class="font-16">

                                                                $ 
                                                                <?php
                                                                
                                                                if($name_off == 'undefined'){
                                                                    
                                                                    $db->cdp_query('SELECT IFNULL(SUM(total_order),0) as total FROM cdb_add_order where status_invoice=1 and order_payment_method>1 and currency="$" and MONTH(order_date)=MONTH(now())');
                                                                    
                                                                }else{
                                                                    
                                                                    $db->cdp_query("SELECT IFNULL(SUM(total_order),0) as total FROM cdb_add_order a
                                                                                    INNER JOIN cdb_users as b ON a.sender_id = b.id
                                                                                    INNER JOIN cdb_offices c on (a.origin_off=c.id)
                                                                                    WHERE a.status_invoice=1 and a.order_payment_method>1 and a.currency='$'
                                                                                    and c.name_off='$name_off' and MONTH(a.order_date)=MONTH(now())");

                                                                }

                                                                $db->cdp_execute();
    
                                                                $count = $db->cdp_registro();
    
                                                                echo number_format($count->total);
                                                                ?>
                                                            </span>
                                                        </h4>
                                                    </div>
                                                </div>
                                                <div class="progress m-t-10">
                                                    <div class="progress-bar bg-success" role="progressbar" style="width: <?php echo cdb_money_format($sumador_pagado) / 100; ?>%" aria-valuenow="47" aria-valuemin="0" aria-valuemax="100"></div>
                                                </div>
                                            </li>
                                            <li class="m-t-10">
                                                <div class="d-flex align-items-center">
                                                    <div>
                                                        <span class="text-muted">Total pending accounts</span>
                                                        <h4 class="m-b-0">
                                                            <span class="font-16">

                                                                $ 
                                                                <?php
                                                                
                                                                if($name_off == 'undefined'){
                                                                    
                                                                    $db->cdp_query('SELECT IFNULL(SUM(total_order),0) as total FROM cdb_add_order where status_invoice!=1 and order_payment_method>1 and currency="$" and MONTH(order_date)=MONTH(now())');
                                                                    
                                                                }else{
                                                                    
                                                                    $db->cdp_query("SELECT IFNULL(SUM(total_order),0) as total FROM cdb_add_order a
                                                                                    INNER JOIN cdb_users as b ON a.sender_id = b.id
                                                                                    INNER JOIN cdb_offices c on (a.origin_off=c.id)
                                                                                    WHERE a.status_invoice!=1 and a.order_payment_method>1 and a.currency='$'
                                                                                    and c.name_off='$name_off' and MONTH(a.order_date)=MONTH(now())");
                                                                }

                                                            $db->cdp_execute();

                                                            $count = $db->cdp_registro();

                                                            echo number_format($count->total);
                                                            ?>
                                                            </span>
                                                        </h4>
                                                    </div>
                                                </div>
                                                <div class="progress m-t-10">
                                                    <div class="progress-bar bg-orange" role="progressbar" style="width: <?php echo cdb_money_format($sumador_pendiente) / 100; ?>%" aria-valuenow="47" aria-valuemin="0" aria-valuemax="100"></div>
                                                </div>
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- ============================================================== -->

                <!--Expenses Shown here -->
                <div class="row">

                    <div class="col-sm-6 col-lg-6">
                        <div class="card">
                            <div class="card-body">
                                <div class="d-md-flex align-items-center">
                                    <div>
                                        <h4 class="card-title">Expense Graph - KES</h4>

                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-lg-12 col-md-12">
                                        
                                        <canvas id="myChartke" style="height: 220px;"></canvas>
                                       
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                    </div>
                    
                    <div class="col-sm-6 col-lg-6">
                        <div class="card">
                            <div class="card-body">
                                <div class="d-md-flex align-items-center">
                                    <div>
                                        <h4 class="card-title">Expense Graph - USD</h4>

                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-lg-12 col-md-12">
                                        
                                        <canvas id="myChartusd" style="height: 220px;"></canvas>
                                       
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- col -->
                    <div class="col-sm-6 col-lg-6">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="card-title">Total Expenses KES</h4>
                                <h5 class="card-subtitle">Overview of Latest Month</h5>

                                <div class="row m-t-20 m-b-20">
                                    <div class="col-sm-12 col-md-12 col-lg-12">
                                        <ul class="list-style-none">

                                            <li class="m-t-10">
                                                <div class="d-flex align-items-center">
                                                    <div>
                                                        <span class="text-muted">Total Expenses - KES.</span>
                                                        <h4 class="m-b-0">
                                                            <span class="font-16">
                                                                Ksh. 
                                                            <?php
                                                            
                                                                if ($name_off == 'undefined'){
                                                                    
                                                                $db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as total FROM expenses_master
                                                                                WHERE currency='Ksh.' and MONTH(created_date)=MONTH(now())");    
                                                                    
                                                                }else{
                                                                    
                                                                $db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as total FROM expenses_master
                                                                                WHERE currency='Ksh.' and name_off='$name_off' and MONTH(created_date)=MONTH(now())");    
                                                                }
                                                                
                                                                $db->cdp_execute();

                                                                $row = $db->cdp_registro();

                                                                $sum1 = $row->total;

                                                                echo number_format($sum1);

                                                                ?>
                                                            </span>
                                                        </h4>
                                                    </div>
                                                </div>
                                                <div class="progress m-t-10">
                                                    <div class="progress-bar bg-info" role="progressbar" style="width: <?php echo cdb_money_format($sumador_total) / 100; ?>%" aria-valuenow="47" aria-valuemin="0" aria-valuemax="100"></div>
                                                </div>
                                            </li>

                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6 col-lg-6">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="card-title">Total Expenses USD</h4>
                                <h5 class="card-subtitle">Overview of Latest Month</h5>

                                <div class="row m-t-20 m-b-20">
                                    <div class="col-sm-12 col-md-12 col-lg-12">
                                        <ul class="list-style-none">

                                            <li class="m-t-10">
                                                <div class="d-flex align-items-center">
                                                    <div>
                                                        <span class="text-muted">Total Expenses - USD.</span>
                                                        <h4 class="m-b-0">
                                                            <span class="font-16">
                                                                $ 
                                                            <?php
                                                            
                                                                if ($name_off == 'undefined'){
                                                                    
                                                                $db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as total FROM expenses_master
                                                                                WHERE currency='$' and MONTH(created_date)=MONTH(now())");    
                                                                    
                                                                }else{
                                                                    
                                                                $db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as total FROM expenses_master
                                                                                WHERE currency='$' and name_off='$name_off' and MONTH(created_date)=MONTH(now())");    
                                                                }
                                                                
                                                                $db->cdp_execute();

                                                                $row = $db->cdp_registro();

                                                                $sum1 = $row->total;

                                                                echo number_format($sum1);

                                                                ?>
                                                            </span>
                                                        </h4>
                                                    </div>
                                                </div>
                                                <div class="progress m-t-10">
                                                    <div class="progress-bar bg-info" role="progressbar" style="width: <?php echo cdb_money_format($sumador_total) / 100; ?>%" aria-valuenow="47" aria-valuemin="0" aria-valuemax="100"></div>
                                                </div>
                                            </li>

                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            
                <!-- End of Expenses -->


            </div>
        </div>
    </div>

    <script src="dataJs/dashboard_inc_exp_kes.js"> </script>
    <script src="dataJs/dashboard_incexp_kes.js"> </script>
    <script src="dataJs/dashboard_incexp_usd.js"> </script>
    <script src="dataJs/dashboard_inc_exp_usd.js"> </script>

    <?php include 'views/inc/footer.php'; ?>