<?php
// *************************************************************************
// *                                                                       *
// * DEPRIXA BASIC -  Freight Forwarding & Shipping Software Solutions     *
// * Copyright (c) JAOMWEB. All Rights Reserved                            *
// *                                                                       *
// *************************************************************************
// *                                                                       *
// * Email: support@jaom.info                                              *
// * Website: https://deprixa.link/documentation/                          *
// *                                                                       *
// *************************************************************************
// *                                                                       *
// * This software is furnished under a license and may be used and copied *
// * only  in  accordance  with  the  terms  of such  license and with the *
// * inclusion of the above copyright notice.                              *
// * If you Purchased from Codecanyon, Please read the full License from   *
// * here- http://codecanyon.net/licenses/standard                         *
// *                                                                       *
// *************************************************************************



require_once('helpers/querys.php');

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

$status_courier = intval($_REQUEST['status_courier']);
$agency = intval($_REQUEST['agency']);
$range = $_REQUEST['range'];
$customer_id = intval($_REQUEST['customer_id']);


$sWhere = "";


if ($status_courier > 0) {

    $sWhere .= " and  a.status_courier = '" . $status_courier . "'";
}

if ($customer_id > 0) {

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


if ($agency > 0) {

    $db->cdp_query("SELECT * FROM cdb_branchoffices where id= '" . $agency . "'");
    $agencys = $db->cdp_registro();

    $agencyFilter = $agencys->name_branch;

    $sWhere .= " and a.agency = '" . $agency . "'";
} else {

    $agencyFilter = 'All';
}

if ($userData->userlevel == 3) {

    $sWhere .= " and  a.driver_id = '" . $_SESSION['userid'] . "'";
}



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

$sql = "SELECT a.status_invoice, a.total_weight, a.total_tax_discount, a.sub_total, a.total_tax_insurance, a.total_tax_custom_tariffis, a.total_tax,   a.total_order, a.consolidate_id, a.c_prefix, a.c_no, a.c_date, a.sender_id, a.receiver_id, a.order_courier,a.status_courier,  b.mod_style, b.color FROM
             cdb_consolidate as a
             INNER JOIN cdb_styles as b ON a.status_courier = b.id
             $sWhere
              and a.status_courier!=14

             order by a.consolidate_id desc 
             ";


$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/uploads/favicon.png">
    <link href="assets/custom_dependencies/print_report.css" rel="stylesheet">

    <title><?php echo $lang['leftorder92con'] ?></title>

</head>

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

        <h2><?php echo $core->site_name; ?><br>
            <?php echo $lang['leftorder92con'] ?> <br>

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

            <?php echo $lang['left324'] ?>: <?php echo $agencyFilter; ?>
        </h2>
        <table>
            <tr>
                <th class="text-center"><b></b></th>
                <th><b><?php echo $lang['ltracking'] ?></b></th>
                <th class="text-center"><b><?php echo $lang['ddate'] ?></b></th>
                <th class="text-center"><b><?php echo $lang['left332'] ?></b></th>
                <th class="text-center"><b><?php echo $lang['left473'] ?></b></th>
                <th class="text-center"><b><?php echo $lang['left464'] ?></b></th>
                <th class="text-center"><b><?php echo $lang['left463'] ?></b></th>
                <th class="text-center"><b><?php echo $lang['left499'] ?></b></th>
                <th class="text-center"><b><?php echo $lang['left473'] ?></b></th>
                <th class="text-center"><b><?php echo $lang['left464'] ?></b></th>
                <th class="text-center"><b><?php echo $lang['left463'] ?></b></th>
                <th class="text-center"><b><?php echo $lang['left4800'] ?></b></th>
                <th class="text-center"><b><?php echo $lang['lstatusshipment'] ?></b></th>
            </tr> 

            <?php

            if ($numrows > 0) {

                $count = 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 * FROM cdb_address_shipments where order_track='" . $row->c_prefix . $row->c_no . "'");
                    $address_order = $db->cdp_registro();


                    $db->cdp_query("SELECT * FROM cdb_consolidate_detail where consolidate_id='" . $row->consolidate_id . "'");
                    $add_orderitem = $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();


                    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";
                    }


                    $count++;


            ?>

                    <tr>
                        <td><b><?php echo $count; ?> </b></td>
                        <td><?php echo $row->c_prefix . $row->c_no; ?></td>
                        <td><?php echo $row->c_date; ?></td>
                        <td><?php echo $sender_data->fname . ' ' . $sender_data->lname; ?></td>
                        <td><?php echo $address_order->sender_country . ' ' . $address_order->sender_city; ?></td>
                        <td><?php echo $sender_data->phone; ?></td>
                        <td><?php echo $sender_data->email; ?></td>
                        <td><?php echo $receiver_data->fname; ?> <?php echo $receiver_data->lname; ?></td>
                        <td><?php echo $address_order->recipient_address; ?>-<?php echo $address_order->recipient_city; ?></td>
                        <td><?php echo $receiver_data->phone; ?></td>
                        <td><?php echo $receiver_data->email; ?></td>
                        <td> <?php echo $row->total_weight; ?></td>
                        <td><span style="background: <?php echo $row->color; ?>;" class="badge  fw-normal"><?php echo $row->mod_style; ?></span>
                            <br>

                            <?php
                            if ($row->is_pickup == true) { ?>

                                <span style="background: <?php echo $status_style_pickup->color; ?>;" class="badge  fw-normal"><?php echo $status_style_pickup->mod_style; ?></span>
                            <?php
                            }
                            ?>

                            <?php
                            if ($row->is_consolidate == true) { ?>

                                <span style="background: <?php echo $status_style_consolidate->color; ?>;" class="badge  fw-normal"><?php echo $status_style_consolidate->mod_style; ?></span>
                            <?php
                            }
                            ?>
                        </td>

                    </tr>
                <?php
                }
                ?>

            <?php
            }
            ?>


        </table>

        <button class='button -dark center no-print' onClick="window.print();" style="font-size:16px; margin-top: 20px;"><?php echo $lang['left452'] ?> &nbsp;&nbsp; <i class="fa fa-print"></i></button>
    </div>

</body>

</html>