<?php

ini_set('display_startup_errors',1); 
ini_set('display_errors',1);
error_reporting(-1);

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

$customer_id = intval($_REQUEST['customer']);
$fecha_inicio = cdp_sanitize($_REQUEST['fecha_inicio']);
$fecha_fin = cdp_sanitize($_REQUEST['fecha_fin']);

$sWhere = "";


if ($customer_id > 0) {

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



$sql = "SELECT * FROM cdb_add_order where order_payment_method !=1  
            and order_date between '$fecha_inicio'  and '$fecha_fin'
            $sWhere
            
             order by order_id desc 
             ";

$db->cdp_query('SELECT IFNULL(SUM(total_fixed_value),0) as total FROM cdb_add_order where status_courier!=22
                                        and order_payment_method = 8
                                        and currency="$"');
$db->cdp_execute();
$row = $db->cdp_registro();
$dexp = $row->total;

$db->cdp_query('SELECT IFNULL(SUM(total_order),0) as total FROM cdb_add_order where status_courier!=22
                                        and order_payment_method = 8
                                        and currency="$"');
$db->cdp_execute();
$row = $db->cdp_registro();
$exp = $row->total;

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


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


$db->cdp_query("SELECT * FROM cdb_users where id= '" . $customer_id . "'");
$sender_data = $db->cdp_registro();


$fecha_inicio = str_replace('-', '/', $fecha_inicio);
$fecha_fin = str_replace('-', '/', $fecha_fin);


?>
<!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="description" content="">
    <meta name="author" content="">
    <!-- Favicon icon -->
    <link rel="icon" type="image/png" sizes="16x16" href="assets/<?php echo $core->favicon ?>">
    <title>Customers balance report detail</title>
    <!-- This Page CSS -->
    <!-- Custom CSS -->
    <link href="assets/css/style.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="assets/select2/dist/css/select2.min.css">
    <link rel="stylesheet" href="assets/jquery-ui.css" type="text/css" />

    <script type="text/javascript" src="assets/js/jquery.js"></script>
    <script type="text/javascript" src="assets/js/jquery-ui.js"></script>

    <link href="assets/css/front.css" rel="stylesheet" type="text/css">
    <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">
    <!--    daterangerpicker-master 
 -->
    <link rel="stylesheet" href="assets/css/daterangepicker.css">
    <script src="assets/js/moment.min.js"></script>
    <script src="assets/js/daterangepicker.js"></script>


    <style type="text/css">
        .scrollable-menu {
            height: auto;
            max-height: 300px;
            overflow-x: hidden;
        }

        .card-outline {
            border-top: 3px solid #bbb;
        }
    </style>


    <script>
        $(function() {
            "use strict";
            $("#main-wrapper").AdminSettings({
                Theme: false, // this can be true or false ( true means dark and false means light ),
                Layout: 'vertical',
                LogoBg: 'skin6', // You can change the Value to be skin1/skin2/skin3/skin4/skin5/skin6 
                NavbarBg: 'skin1', // You can change the Value to be skin1/skin2/skin3/skin4/skin5/skin6
                SidebarType: 'mini-sidebar', // You can change it full / mini-sidebar / iconbar / overlay
                SidebarColor: 'skin6', // You can change the Value to be skin1/skin2/skin3/skin4/skin5/skin6
                SidebarPosition: true, // it can be true / false ( true means Fixed and false means absolute )
                HeaderPosition: true, // it can be true / false ( true means Fixed and false means absolute )
                BoxedLayout: false, // it can be true / false ( true means Boxed and false means Fluid ) 
            });
        });
    </script>

</head>

<body>
    <!-- ============================================================== -->
    <!-- Preloader - style you can find in spinners.css -->
    <!-- ============================================================== -->
    <?php $agencyrow = $core->cdp_getBranchoffices(); ?>


    <?php include 'views/inc/preloader.php'; ?>
    <!-- ============================================================== -->
    <!-- Main wrapper - style you can find in pages.scss -->
    <!-- ============================================================== -->
    <div id="main-wrapper">
        <!-- ============================================================== -->
        <!-- Topbar header - style you can find in pages.scss -->
        <!-- ============================================================== -->

        <!-- ============================================================== -->
        <!-- 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">

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

            <!-- Action part -->
            <!-- Button group part -->
            <div class="bg-light p-15">
                <div class="row justify-content-center">
                    <div class="col-md-12">
                        <div class="row">
                            <div class="col-12">

                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- Action part -->


            <div class="container-fluid">

                <div class="row">
                    <!-- Column -->

                    <div class="col-lg-12 col-xl-12 col-md-12">

                        <div class="card card-outline">
                            <h3 class="card-title  ml-4 mt-3"> Customers balance report detail
                                <br>
                                [<?php echo $fecha_inicio . ' - ' . $fecha_fin; ?>]

                            </h3>

                            <h4 class="card-title  ml-4 mt-3">

                                Customer: <?php echo $sender_data->fname . ' ' . $sender_data->lname; ?>
                                &nbsp; 
                            </h4>
                            
                            <!-- <div class="col-sm-12 col-md-4">
                                <div class="btn-group pull-right">
                                    <button class="btn waves-effect waves-light btn-secondary"><i class=' fas fa-file-excel'></i><a href="export_excel.php" target="_blank"> Export Excel</a></button>
                                </div>
                            </div> -->

                            <div class="card-body">

                                <table id="zero_config" class="table table-condensed table-hover table-striped">
                                    <thead>
                                        <tr>

                                            <th><b><?php echo $lang['ltracking'] ?></b></th>
                                            <th class="text-center"><b><?php echo $lang['ddate'] ?></b></th>
                                            <th class="text-center"><b>Receiver</b></th>
                                            <th class="text-center"><b>Receiver Phone</b></th>
                                            <th class="text-center"><b><?php echo $lang['lstatusinvoice'] ?></b></th>
                                            <th class="text-center"><b>Total amount</b></th>
                                            <th class="text-center"><b>Total paid</b></th>
                                            <th class="text-center"><b>balance</b></th>


                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        if ($numrows > 0) {

                                            $count = 0;
                                            $sumador_pendiente = 0;
                                            $sumador_total = 0;
                                            $sumador_pagado = 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  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();
                                                // var_dump($sum_payment->total);

                                                $pendiente = $row->total_order - $sum_payment->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";
                                                }



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




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

                                                    <td><b><a data-toggle="modal" data-target="#charges_list" data-id="<?php echo $row->order_id; ?>"><?php echo $row->order_prefix . $row->order_no; ?></a></b></td>

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

                                                    <td class="text-center">
                                                        <?php echo $receiver_data->fname; ?>&nbsp;<?php echo $receiver_data->lname; ?>
                                                    </td>
                                                    
                                                    <td class="text-center">
                                                        <?php echo $receiver_data->phone; ?>
                                                    </td>

                                                    <td class="text-center">
                                                        <span class="label label-large <?php echo $label_class; ?>"><?php echo $text_status; ?></span>

                                                    </td>

                                                    <td class="text-center">
                                                        <b><?php echo $curr->currency; ?></b> <?php echo $row->total_order; ?>
                                                    </td>

                                                    <td class="text-center">
                                                        <b><?php echo $curr->currency; ?></b> <?php echo $sum_payment->total; ?>
                                                    </td>

                                                    <td class="text-center">
                                                        <b><?php echo $curr->currency; ?></b> <?php echo $pendiente; ?>
                                                    </td>


                                    </tbody>

                                <?php $count++;
                                            } ?>

                            <?php } ?>
                            <tfoot>
                                <tr class="card-hover">
                                    <td class="text-left"><b>TOTAL</b></td>

                                    <td colspan="4"></td>
                                    <td class="text-center  ">
                                        <b><?php echo number_format($sumador_total); ?> </b>
                                    </td>

                                    <td class="text-center  ">
                                        <b><?php echo number_format($sumador_pagado); ?> </b>
                                    </td>

                                    <td class="text-center  ">
                                        <b><?php echo number_format($sumador_pendiente); ?> </b>
                                    </td>

                                </tr>

                            </tfoot>
                                </table>

                                <div class="pull-right">

                                    <a class="btn btn-primary" href="report_customers_balance_list.php">Back to Customer Balance List</a>
                                </div>

                            </div>
                        </div>
                    </div>
                    <!-- Column -->
                </div>
            </div>


        </div>
        <!-- ============================================================== -->
        <!-- End Page wrapper  -->
        <!-- ============================================================== -->
    </div>
    <!-- ============================================================== -->
    <!-- End Wrapper -->
    <!-- ============================================================== -->


    <!-- ============================================================== -->
    <!-- All Jquery -->
    <!-- ============================================================== -->
    <!-- Bootstrap tether Core JavaScript -->
    <script src="assets/libs/popper.js/dist/umd/popper.min.js"></script>
    <script src="assets/libs/bootstrap/dist/js/bootstrap.min.js"></script>
    <!-- apps -->
    <script src="assets/js/app.min.js"></script>
    <script src="assets/js/app-style-switcher.js"></script>
    <!-- slimscrollbar scrollbar JavaScript -->
    <script src="assets/libs/perfect-scrollbar/dist/perfect-scrollbar.jquery.min.js"></script>
    <script src="assets/js/sparkline/sparkline.js"></script>
    <!--Wave Effects -->
    <script src="assets/js/waves.js"></script>
    <!--Menu sidebar -->
    <script src="assets/js/sidebarmenu.js"></script>
    <!--Custom JavaScript -->
    <script src="assets/js/custom.min.js"></script>


</body>

</html>