<?php

$userData = $user->cdp_getUserData();
$statusrow = $core->cdp_getStatus();
$paymethodrow = $core->cdp_getPaymentMethod();
$agencyrow = $core->cdp_getBranchoffices();
$payrow = $core->cdp_getPayment();
$paymethodrow = $core->cdp_getPaymentMethod();

$db = new Conexion;
$name_off=$userData->name_off;

$curr = '$';

?>
<!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="Myne Cargo Web System" />
    <meta name="author" content="Myne Interactive">
    <title>Dashboard | <?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/extra-libs/css-chart/css-chart.css" rel="stylesheet">
    <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">
    
    <link rel="stylesheet" type="text/css" href="assets/select2/dist/css/select2.min.css">
     <link rel="stylesheet" href="assets/css/daterangepicker.css">
    <script src="assets/js/moment.min.js"></script>
    <script src="assets/js/daterangepicker.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, Sale Locations -->
                <!-- ============================================================== -->
                <div class="row">

                    <div class="col-sm-6 col-md-7 col-lg-8">
                        <div class="card">
                            <div class="card-body">
                                <div class="row">
                                    <!-- column -->
                                    <div class="col-sm-12 col-md-6 col-lg-8">
                                        <div class="card-body border-bottom">
                                            <h5 class="card-title"><?php echo $lang['dashnew06'] ?></h5>
                                        </div>

                                        <div class="row">
                                            <!-- col -->
                                            <div class="col-lg-6 col-md-6">
                                                <div class="d-flex align-items-center">
                                                    <div class="m-r-10"><a href="dashboard_admin_shipments.php"><span class="text-orange display-6"><i class="mdi mdi-package-variant-closed"></i></span></a></div>
                                                    <div><span><?php echo $lang['dashnew07'] ?></span>
                                                        <h3 class="font-medium m-b-0">
                                                            <?php
                                                            
                                                            if($name_off == 'undefined'){
                                                                
                                                               $db->cdp_query('SELECT COUNT(*) as total FROM cdb_add_order as 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.is_pickup=0 and MONTH(order_date)=MONTH(now())'); 
                                                                
                                                            }else{
                                                                
                                                                $db->cdp_query("SELECT COUNT(*) as total FROM cdb_add_order as 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.is_pickup=0
                                                                             and c.name_off ='$name_off' and MONTH(a.order_date)=MONTH(now())");
                                                                
                                                            }

                                                            

                                                            $db->cdp_execute();

                                                            $count = $db->cdp_registro();

                                                            echo $count->total;
                                                            ?>
                                                            
                                                        </h3>
                                                    </div>
                                                </div>
                                            </div>
                                            <!-- col -->

                                            <!-- col -->
                                            <div class="col-lg-6 col-md-6">
                                                <div class="d-flex align-items-center">
                                                    <div class="m-r-10"><a href="accounts_receivable.php"><span class="text-primary display-6"><i class="mdi mdi-package-down"></i></span></a></div>
                                                    <div><span><?php echo $lang['dashnew10'] ?></span>
                                                        <h3 class="font-medium m-b-0">
                                                            <?php
                                                            
                                                            if ($name_off == 'undefined'){
                                                                
                                                                $db->cdp_query('SELECT COUNT(*) as total FROM cdb_add_order as 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.order_payment_method>1 and MONTH(a.order_date)=MONTH(now()) ');
                                                                
                                                                
                                                            }else{
                                                                
                                                                $db->cdp_query("SELECT COUNT(*) as total FROM cdb_add_order as 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.order_payment_method>1
                                                                             and c.name_off ='$name_off' and MONTH(a.order_date)=MONTH(now())");
                                                                
                                                            }
                                                    

                                                            

                                                            $db->cdp_execute();

                                                            $count = $db->cdp_registro();

                                                            echo $count->total;
                                                            ?>
                                                        </h3>
                                                    </div>
                                                </div>
                                            </div>

                                        </div>

                                        <div class="row">

                                            <div class="col-md-6 col-sm-12 col-lg-6">
                                                
                                                <div class="d-flex align-items-center">
                                                    <div class="m-r-10"><span class="text-primary display-6"><i class="mdi mdi-wallet"></i></span></div>
                                                    <div><span class="text-muted">Total Income (Ksh.)</span>
                                                        <h4 class="font-medium m-b-0">
                                                  
                                                            <?php
                                                            
                                                            if($name_off == 'undefined'){
                                                                
                                                                $db->cdp_query("SELECT IFNULL(SUM(a.total_order),0) as keinc FROM cdb_add_order as 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!=21 and  a.status_invoice = 1 and a.order_payment_method>1 and a.currency='Ksh.'
                                                                            and MONTH(a.order_date)=MONTH(now())");
                                                                
                                                            }else{
                                                                
                                                                $db->cdp_query("SELECT IFNULL(SUM(a.total_order),0) as keinc FROM cdb_add_order as 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!=21 and  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->keinc);
                                                            ?>
                                                        </h4>
                                                        
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="col-md-6 col-sm-12 col-lg-6">
                                                
                                                <div class="d-flex align-items-center">
                                                    <div class="m-r-10"><span class="text-orange display-6"><i class="mdi mdi-wallet"></i></span></div>
                                                    <div><span class="text-muted">Total Income (USD)</span>
                                                        <h4 class="font-medium m-b-0">
                                                            <?php
                                                            
                                                            if($name_off == 'undefined'){
                                                                
                                                                $db->cdp_query("SELECT IFNULL(SUM(a.total_order),0) as usinc FROM cdb_add_order as 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!=21 and  a.status_invoice = 1 and a.order_payment_method>1 and a.currency='$'
                                                                            and MONTH(a.order_date)=MONTH(now())");
                                                                
                                                            }else{
                                                                
                                                                $db->cdp_query("SELECT IFNULL(SUM(a.total_order),0) as usinc FROM cdb_add_order as 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!=21 and  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->usinc);
                                                            ?>
                                                        </h4>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row">

                                            <div class="col-md-6 col-sm-12 col-lg-6">
                                                <div class="d-flex align-items-center">
                                                    <div class="m-r-10"><span class="text-primary display-6"><i class="mdi mdi-wallet"></i></span></div>
                                                    <div><span class="text-muted">Total Expense (Ksh.)</span>
                                                        <h4 class="font-medium m-b-0">
                                                            
                                                            <?php
                                                                
                                                                if ($name_off == 'undefined'){
                                                                    
                                                                $db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as keexp FROM expenses_master
                                                                                WHERE currency='Ksh.' and MONTH(created_date)=MONTH(now())");    
                                                                    
                                                                }else{
                                                                    
                                                                $db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as keexp 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->keexp;

                                                                echo number_format($sum1);

                                                                ?>
                                                        </h4>
                                                        
                                                    </div>
                                                </div>
                                                  
                                            </div>

                                            <div class="col-md-6 col-sm-12 col-lg-6">
                                                
                                                <div class="d-flex align-items-center">
                                                    <div class="m-r-10"><span class="text-orange display-6"><i class="mdi mdi-wallet"></i></span></div>
                                                    <div><span class="text-muted">Total Expense (USD)</span>
                                                        <h4 class="font-medium m-b-0">
                                                             
                                                            <?php
                                                                
                                                                if ($name_off == 'undefined'){
                                                                    
                                                                $db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as usexp FROM expenses_master
                                                                                WHERE currency='$' and MONTH(created_date)=MONTH(now())");    
                                                                    
                                                                }else{
                                                                    
                                                                $db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as usexp 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->usexp;

                                                                echo number_format($sum1);

                                                                ?>
                                                        </h4>
                                                    </div>
                                                </div>
                                            </div>
                                            
                                            <div class="card-body border-top">
                                                    <span class="text-muted">Total Income (Ksh.)</span>
                                                    <h4 class="font-medium m-b-0">
                                                    <?php
                                                    
                                                    if($name_off == 'undefined'){
                                                                
                                                        $db->cdp_query("SELECT IFNULL(SUM(a.total_order),0) as usinc FROM cdb_add_order as 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!=21 and  a.status_invoice!=0 and a.order_payment_method>1 and a.currency='Ksh.'
                                                        and MONTH(a.order_date)=MONTH(now())");
                                                                
                                                    }else{
                                                                
                                                        $db->cdp_query("SELECT IFNULL(SUM(a.total_order),0) as usinc FROM cdb_add_order as 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!=21 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();
                                                        $row = $db->cdp_registro();
                                                        $sdfin = $row->usinc;
                                                        
                                                    if ($name_off == 'undefined'){
                                                                    
                                                        $db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as usexp FROM expenses_master
                                                        WHERE currency='Ksh.' and MONTH(created_date)=MONTH(now())");    
                                                                    
                                                    }else{
                                                                    
                                                        $db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as usexp 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->usexp;
                                                        
                                                        $inc = $sdfin - $sum1;
                                                        
                                                        echo number_format($inc);
                                                    ?>
                                                    </h4>
                                            </div>
                                            <div class="card-body border-top">
                                                    <span class="text-muted">Total Income (USD)</span>
                                                    <h4 class="font-medium m-b-0">
                                                    <?php
                                                    
                                                    if($name_off == 'undefined'){
                                                                
                                                        $db->cdp_query("SELECT IFNULL(SUM(a.total_order),0) as usinc FROM cdb_add_order as 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!=21 and  a.status_invoice!=0 and a.order_payment_method>1 and a.currency='$'
                                                        and MONTH(a.order_date)=MONTH(now())");
                                                                
                                                    }else{
                                                                
                                                        $db->cdp_query("SELECT IFNULL(SUM(a.total_order),0) as usinc FROM cdb_add_order as 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!=21 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();
                                                        $row = $db->cdp_registro();
                                                        $sdfin = $row->usinc;
                                                        
                                                    if ($name_off == 'undefined'){
                                                                    
                                                        $db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as usexp FROM expenses_master
                                                        WHERE currency='$' and MONTH(created_date)=MONTH(now())");    
                                                                    
                                                    }else{
                                                                    
                                                        $db->cdp_query("SELECT IFNULL(SUM(expense_amount),0) as usexp 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->usexp;
                                                        
                                                        $inc = $sdfin - $sum1;
                                                        
                                                        echo number_format($inc);
                                                    ?>
                                                    </h4>
                                            </div>
                                            
                                            
                                            
                                        </div>
                                        
                                        
                                    </div>
                                    <!-- column -->

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


                    <div class="col-sm-12 col-md-5 col-lg-4">
                        <div class="card">
                            <div class="card-body">
                                <div class="card-body border-bottom">
                                    <h5 class="card-title"><?php echo $lang['dashnew01'] ?></h5>
                                    <!-- <h5 class="card-subtitle">Indicadores de usuarios</h5> -->
                                </div>

                                <div class="col-md-6 col-sm-12 col-lg-6 m-t-30 m-b-20">
                                    <div class="d-flex align-items-center">
                                        <div class="m-r-10">
                                            <?php if ($userData->userlevel == 9) { ?>
                                            <a href="users_list.php"><span class="display-6"><i class="mdi mdi-account-settings-variant" style="color: #36bea6;"></i></span></a>
                                            <?php } ?>
                                            <?php if ($userData->userlevel == 2) { ?>
                                            <a href="#"><span class="display-6"><i class="mdi mdi-account-settings-variant" style="color: #36bea6;"></i></span></a>
                                            <?php } ?>
                                        </div>
                                        <div>
                                            <span class="text-muted"><?php echo $lang['dashnew02'] ?></span>
                                            <h3 class="font-medium m-b-0">
                                                <?php

                                                $db->cdp_query('SELECT COUNT(*) as total FROM cdb_users WHERE userlevel=9');

                                                $db->cdp_execute();

                                                $count = $db->cdp_registro();

                                                echo $count->total;
                                                ?>
                                            </h3>
                                        </div>
                                    </div>
                                </div>

                                <div class="col-md-6 col-sm-12 col-lg-6">
                                    <div class="d-flex align-items-center">
                                        <div class="m-r-10">
                                            <?php if ($userData->userlevel == 9) { ?>
                                            <a href="users_list.php"><span class="display-6"><i class="mdi mdi-account-settings" style="color: #fb8c00;"></i></span></a>
                                            <?php } ?>
                                            <?php if ($userData->userlevel == 2) { ?>
                                            <a href="#"><span class="display-6"><i class="mdi mdi-account-settings" style="color: #fb8c00;"></i></span></a>
                                            <?php } ?>
                                        </div>
                                        <div><span class="text-muted"><?php echo $lang['dashnew03'] ?></span>
                                            <h3 class="font-medium m-b-0">
                                                <?php

                                                $db->cdp_query('SELECT COUNT(*) as total FROM cdb_users WHERE userlevel=2');

                                                $db->cdp_execute();

                                                $count = $db->cdp_registro();

                                                echo $count->total;
                                                ?>
                                            </h3>
                                        </div>
                                    </div>
                                </div>


                                <div class="col-md-6 col-sm-12 col-lg-6">
                                    <div class="d-flex align-items-center">
                                        <div class="m-r-10"><a href="drivers_list.php"><span class="display-6"><i class="mdi mdi-account-star-variant" style="color: #7460ee;"></i></span></a></div>
                                        <div><span class="text-muted">Normal Users</span>
                                            <h3 class="font-medium m-b-0">
                                                <?php

                                                $db->cdp_query('SELECT COUNT(*) as total FROM cdb_users WHERE userlevel=3');

                                                $db->cdp_execute();

                                                $count = $db->cdp_registro();

                                                echo $count->total;
                                                ?>
                                            </h3>
                                        </div>
                                    </div>
                                </div>

                                <div class="col-md-6 col-sm-12 col-lg-6">
                                    <div class="d-flex align-items-center">
                                        <div class="m-r-10"><a href="customers_list.php"><span class="display-6"><i class="mdi mdi-account-check" style="color: #1f95ff;"></i></span></a></div>
                                        <div><span class="text-muted"><?php echo $lang['dashnew05'] ?></span>
                                            <h3 class="font-medium m-b-0">
                                                <?php

                                                $db->cdp_query('SELECT COUNT(*) as total FROM cdb_users WHERE userlevel=1');

                                                $db->cdp_execute();

                                                $count = $db->cdp_registro();

                                                echo $count->total;
                                                ?>
                                            </h3>
                                        </div>
                                    </div>
                                </div>
                                
                            </div>
                        </div>
                    </div>
                </div>


                <div class="row">
                    <div class="col-12 col-sm-12 col-md-12">
                        <div class="card">
                            <div class="card-body">
                                <ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
                                    <li class="nav-item">
                                        <a class="nav-link active" id="pills-home-tab" data-toggle="pill" href="#pills-shipment" role="tab" aria-controls="pills-shipment" aria-selected="true"><?php echo $lang['dashnew19'] ?></a>
                                    </li>

                                </ul>
                                <div class="tab-content" id="pills-tabContent">
                                    <div class="tab-pane fade show active" id="pills-shipment" role="tabpanel" aria-labelledby="pills-home-tab">

                                        <div class="col-md-4 mt-4 mb-4">
                                            <div class="input-group">
                                                
                                                <input type="text" name="search_shipment" id="search_shipment" class="form-control input-sm float-right" placeholder="search tracking number" onkeyup="cdp_load(1);">
                                                <div class="input-group-append input-sm">
                                                    <button type="submit" class="btn btn-info"><i class="fa fa-search"></i></button>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="results_shipments"></div>
                                    </div>
                                </div>
                            </div> 
                        </div>
                    </div>
                </div>
                
                
                
            </div>
        </div>
    </div>



    <script src="dataJs/dashboard_index.js"></script> 
   <!-- <script src="dataJs/dashboard_summary.js"></script>
    <script src="assets/select2/dist/js/select2.full.min.js"></script>
    <script src="assets/select2/dist/js/select2.min.js"></script> -->

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