<?php


require_once('helpers/querys.php');

if (isset($_GET['id'])) {
    $data = cdp_getCourierPrint($_GET['id']);
}

if (!isset($_GET['id']) or $data['rowCount'] != 1) {
    cdp_redirect_to("courier_list.php");
}



$row = $data['data'];

$db->cdp_query("SELECT * FROM cdb_add_order_item WHERE order_id='" . $_GET['id'] . "'");
$order_items = $db->cdp_registros();

$db->cdp_query("SELECT * FROM cdb_met_payment where id= '" . $row->order_pay_mode . "'");
$met_payment = $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_category where id= '" . $row->order_item_category . "'");
$category = $db->cdp_registro();

$db->cdp_query("SELECT * FROM cdb_shipping_mode where id= '" . $row->order_service_options . "'");
$shipping_mode = $db->cdp_registro();

$fecha = date("Y-m-d :h:i A", strtotime($row->order_datetime));

$db->cdp_query("SELECT * FROM cdb_users where id= '" . $row->receiver_id . "'");
$receiver_data = $db->cdp_registro();

$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->user_id."'"); 
$sender_data2= $db->cdp_registro();


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

$db->cdp_query("SELECT currency FROM cdb_add_order WHERE order_id='" . $row->order_id . "'");
$curr = $db->cdp_registro();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

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

    <link rel="stylesheet" href="assets/css/input-css/intlTelInput.css">

    <link rel="stylesheet" type="text/css" href="assets/select2/dist/css/select2.min.css">
    <link href="assets/css/style.min.css" rel="stylesheet">

    <link rel="stylesheet" href="assets/jquery-ui.css" type="text/css" />
    <link href="assets/css/front.css" rel="stylesheet" type="text/css">


    <link rel="stylesheet" href="assets/custom_dependencies/bootstrap.min.css" rel="stylesheet">

</head>

<body>
    <div id="page-wrap">
        <table width="100%">
            <tr>
                <td style="border: 0;  text-align: center" width="56%">
                     <?php echo ($core->logo) ? '<img src="assets/uploads/galeyr.png"/>' : $core->site_name; ?>
               <br/>
               <br/>
                    NBO Eastleigh: +254-721215150<br>
                    MGQ KM4 H.Afrik: +252-619878788<br>
                    HGA: +252-634100266<br>
                </td>
                <td style="border: 0;  text-align: center" width="48%">
                    </br><img src='https://barcode.tec-it.com/barcode.ashx?data=<?php echo $row->order_prefix . $row->order_no; ?>&code=Code128&multiplebarcodes=false&translate-esc=false&unit=Fit&dpi=72&imagetype=Gif&rotation=0&color=%23000000&bgcolor=%23ffffff&qunit=Mm&quiet=0&modulewidth=50' alt='' />
                </td>
    </div>
    </tr>
    </table>
    <hr>
    </br>
    <div id="customer">

        <table id="meta">
            <tr>
                <td rowspan="5" style="border: 1px solid white; border-right: 1px solid black; text-align: left" width="55%">
                    <strong><?php echo $lang['inv-shipping5'] ?></strong> </br>
                        <table id="items">
					<b><?php echo $sender_data->fname." ".$sender_data->lname; ?></b></br> </br>
					<?php echo $address_order->sender_address; ?> </br>
					<?php echo $address_order->sender_country." | ".$address_order->sender_city; ?> </br>
					<?php echo $sender_data->phone; ?> </br>
					<?php echo $sender_data->email; ?> 
				</table>	
				</td>
				<td rowspan="5" style="border: 1px solid white; border-right: 1px solid black; text-align: left" width="35%">
                <strong>Receiver:</strong> </br>
				<table id="items">
					<b><?php echo $receiver_data->fname." ".$receiver_data->lname; ?></b></br> </br>
					<?php echo $address_order->recipient_address; ?> </br>
					<?php echo $address_order->recipient_country." | ".$address_order->recipient_city; ?> </br>
					<?php echo $receiver_data->phone; ?> </br>
					<?php echo $receiver_data->email; ?> 
				</table>
                </td>
                <td class="meta-head">
                    <p style="color:white;"><?php echo $lang['inv-shipping6'] ?></p>
                </td>
                <td>
                    <?php echo $met_payment->name_pay; ?>
                </td>
            </tr>
            <tr>
                <td class="meta-head" style="line-height: 7px" width="31%">
                    <p style="color:white;" width="25%">Freight</p>
                </td>
                <td><?php if ($category != null) {
                        echo $category->name_item;
                    } ?></td>
            </tr>
            <tr>
                 <td class="meta-head" style="line-height: 8px" width="31%">
                    <p style="color:white;">Courier</p>
                </td>
                <td>
                    <?php if ($courier_com != null) {
                        echo $courier_com->name_com;
                    } ?>
                </td>
            </tr>
            <tr>
                 <td class="meta-head" style="line-height: 7px" width="25%">
                    <p style="color:white;">Shipping</p>
                </td>
                <td><?php echo $fecha; ?></td>
            </tr>
            <tr>
                <td class="meta-head" style="line-height: 7px" width="25%">
                    <p style="color:white;">Invoice</p>
                </td>
                <td><b><?php echo $row->order_prefix . $row->order_no; ?></b></td>
            </tr>
        </table>
    </div>
    <table id="items">
        <tr>
            <th style="color:white;" width="5%"><b><?php echo $lang['left214'] ?></b></th>
            <th style="color:white;" width="30%"><b><?php echo $lang['left213'] ?></b></th>
            <th style="color:white;" width="25%"><b>Logistics</b></th>
            <th style="color:white;" width="15%"><b><?php echo $lang['left215'] ?> (kg)</b></th>
            <th style="color:white;" width="10%"><b>Destination</b></th>

        </tr>

        <?php

        $sumador_total = 0;
        $sumador_libras = 0;
        $sumador_volumetric = 0;
        $sumador_valor_declarado = 0;
        $max_fixed_charge = 0;
        $precio_total = 0;
        $total_impuesto = 0;
        $total_seguro = 0;
        $total_peso = 0;
        $total_descuento = 0;
        $total_impuesto_aduanero = 0;
        $total_valor_declarado = 0;

        foreach ($order_items as $row_item) {

            $description_item = $row_item->order_item_description;
            $category_item = $category->name_item;
            $weight_item = $row_item->order_item_weight;

            $total_metric = $row_item->order_item_length * $row_item->order_item_width * $row_item->order_item_height / $row->volumetric_percentage;

            // calculate weight x price
            if ($weight_item > $total_metric) {

                $calculate_weight = $weight_item;
                $sumador_libras += $weight_item; //Sumador

            } else {

                $calculate_weight = $total_metric;
                $sumador_volumetric += $total_metric; //Sumador
            }

            $precio_total = $calculate_weight * $row->value_weight;
            $precio_total = $precio_total; //Precio total formateado

            $sumador_total += $precio_total;
            $sumador_valor_declarado += $row_item->order_item_declared_value;
            $max_fixed_charge += $row_item->order_item_fixed_value;


            if ($sumador_total > $core->min_cost_tax) {

                $total_impuesto = $sumador_total * $row->tax_value / 100;
            }

            if ($sumador_valor_declarado > $core->min_cost_declared_tax) {

                $total_valor_declarado = $sumador_valor_declarado * $row->declared_value / 100;
            }

             $total_discount = $row->tax_discount;

            $total_descuento = $sumador_total - $total_discount;

            $total_peso = $sumador_libras + $sumador_volumetric;

            $total_seguro = $row->tax_insurance_value * $row->total_insured_value / 100;

            $total_impuesto_aduanero = $total_peso * $row->tax_custom_tariffis_value;

             $total_envio = $total_descuento + $total_impuesto + $total_seguro + $total_impuesto_aduanero + $total_valor_declarado + $max_fixed_charge + $row->total_reexp;

            $sumador_total = $sumador_total;
            $sumador_libras = $sumador_libras;
            $sumador_volumetric = $sumador_volumetric;
            $total_envio = $total_envio;
            $total_seguro = $total_seguro;
            $total_peso = $total_peso;
            $total_impuesto_aduanero = $total_impuesto_aduanero;
            $total_impuesto = $total_impuesto;
            $sumador_valor_declarado = $sumador_valor_declarado;
            $total_valor_declarado = $total_valor_declarado;
            $max_fixed_charge = $max_fixed_charge;

        ?>

             <tr class="item-row">
                <td><?php echo $row_item->order_item_quantity; ?></td>
                <td><?php echo $description_item; ?></td>
                <td><?php echo $category_item; ?></td>
                <td><?php echo $weight_item; ?></td>
                <td><?php echo $address_order->recipient_country; ?></td>

            </tr>
        <?php

        } ?>


        <tr class="">
            <td colspan="2"><b>Price &nbsp; <?php echo $core->weight_p; ?>:</b> <?php echo $row->value_weight; ?></td>
            <td></td>
            <td></td>

            <td colspan="3" class="text-right"><b><?php echo $lang['left240'] ?></b></td>
            <td class="text-center"><?php echo $curr->currency; ?><?php echo $sumador_total; ?></td>

        </tr>

        <tr class="">
            <td colspan="2"></td>
            <td></td>
            <td></td>

            <td colspan="3" class="text-right"><b><b>Discount </b></td>
            <td class="text-center"><?php echo $curr->currency; ?><?php echo $total_discount; ?></td>

        </tr>
         <tr>
            <td colspan="2"><b><?php echo $lang['left236'] ?> (Price * Weight)</b>: <span id="total_peso"><?php echo $total_peso; ?> * <?php echo $row->value_weight; ?></span></td>
            <td></td>
            <td></td>
            <td></td>
            <td colspan="3" class="text-right"><b>Tax <?php echo $row->tax_value; ?> % </b></td>
            <td class="text-center" id="impuesto"><?php echo $total_impuesto; ?></td>
        </tr>

        <tr>
            <td colspan="2"></td>
            <td></td>
            <td></td>
            <td></td>
            <td colspan="3" class="text-right"><b><?php echo $lang['add-title44'] ?> &nbsp;</b></td>
            <td class="text-center" id="total_envio"><?php echo $curr->currency; ?><?php echo $total_envio; ?></td>
        </tr>
    </table>


    <?php

    $dir = 'doc_signs/' . $_GET['id'] . '.png';

    if (file_exists($dir) || !empty($row->photo_delivered)) { ?>

        <table width="100%">
            <tr>
                <div id="logo">
                    <?php

                    if (file_exists($dir)) {

                    ?>
                        <td style="border: 0;  text-align: center;" width="50%">
                            <h3> &nbsp;<b>Receiver Signature</b></h3>


                            <img src="<?php echo $dir; ?>" width="300" height="150">


                        </td>
                    <?php
                    }
                    ?>


                </div>
            </tr>
        </table>

    <?php
    }
    ?>

    <!--    end related transactions -->

    <div id="terms">
        <h5><?php echo $lang['inv-shipping18'] ?></h5>
        <table id="related_transactions" style="width: 100%">
            <p align="justify"><?php echo cdp_cleanOut($core->interms); ?></p>
        </table>

        <?php



        $db->cdp_query("SELECT * FROM cdb_order_files where order_id='" . $_GET['id'] . "' ORDER BY date_file");
                $files_order = $db->cdp_registros();
                $numrows = $db->cdp_rowCount();


                if ($numrows > 0) {
                ?>
                <div class="col-lg-12">

                            
                    <h5 class=""> Attached images</h5>

                    <div  class="col-md-12 row">

                    <?php
                    $count = 0;
                    $count_hr=0;

                    foreach ($files_order as $file) {

                        $date_add = date("Y-m-d h:i A", strtotime($file->date_file));

                        $src='assets/images/no-preview.jpeg';

                        if($file->file_type=='jpg'||
                            $file->file_type=='jpeg'||
                            $file->file_type=='png'||
                            $file->file_type=='ico'
                        ){

                            $src=$file->url;

                        $count++;
                    ?>

                    <div class="col-md-3" id="file_delete_item_<?php echo $file->id; ?>">                 
                            
                       <div style="text-align: center; margin-bottom: 5px">  
                           <img style="width: 120px; height: 120px;" class="" src="<?php echo $src; ?>">
                       </div>
                    </div>
                     <?php
                        }

                    } ?>
                    </div>                  

                </div>
                         
                <?php
                } ?>
        </br></br></br></br>
        <table id="signing">
            <tr class="noBorder">
                <td align="center">
                    <h4><?php echo $sender_data2->fname.' '. $sender_data2->lname; ?></h4>
                </td>
                <td align="center">
                    <h4><?php echo $receiver_data->fname." ".$receiver_data->lname; ?></h4>
                </td>
      
            </tr>
            <tr class="noBorder">
					<td align="center">Prepared By</td>
					<td align="center">Package For</td>
					<td align="center">Receiver Sign</td>
				</tr>
        </table>
    </div>

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

</body>

</html>