<?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_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();

$db->cdp_query("SELECT cdb_met_payment.name_pay as PayType FROM cdb_met_payment 
                INNER JOIN cdb_charges_order on (cdb_met_payment.id = cdb_charges_order.payment_type) 
                WHERE order_id='" . $row->order_id . "'");
$pay_name = $db->cdp_registro();

?>

<html>

	<head>
	<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 href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet">
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

		</head>
		<body>
<div class="container bootstrap snippets bootdeys">
<div class="row">
  <div class="col-sm-12">
	  	<div class="panel panel-default invoice" id="invoice">
		  <div class="panel-body">
			<!--<div class="invoice-ribbon"><div class="ribbon-inner">PAID</div></div>-->
		    <div class="row">

				<div class="col-sm-6 top-left">
					<img src="assets/uploads/shiine01.png"/>
				</div>

				<div class="col-sm-6 top-right">
						<h3 class="marginright">INVOICE</h3>
			    </div>
                
			</div>
			<hr>
			<div class="row">

				<div class="col-xs-4 from">
					<p class="lead marginbottom">Bill From : </p>
					<table id="items">
					<b><?php echo $sender_data->fname." ".$sender_data->lname; ?></b></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>
				</div>

				<div class="col-xs-4 to">
					<p class="lead marginbottom">Ship To : </p>
					<table id="items">
					<b><?php echo $receiver_data->fname." ".$receiver_data->lname; ?></b></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>

			    </div>

			    <div class="col-xs-4 text-right payment-details">
					<p class="lead marginbottom payment-info text-left">Invoice Details</p>
					<p class="text-left">Invoice Date: &nbsp;&nbsp;<?php echo $fecha; ?></p>
					<p class="text-left">Invoice No:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b><?php echo $row->order_prefix . $row->order_no; ?></b></p>
					<p class="text-left">Payment Type:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b><?php print_r($pay_name->PayType); ?></b></p>
			    </div>

			</div>

			<div class="row table-row">
				<table class="table table-striped">
			      <thead>
			        <tr>
			          
			          <th style="width:15%">Quantity</th>
			          <th style="width:40%">Description</th>
			          <th style="width:15%">Destination</th>
			          <th style="width:15%">Weight (kgs)</th>
			          <th style="width:15%">Unit Price</th>
			          <th style="width:15%">Amount</th>
			        </tr>
			      </thead>
			      <tbody>
			            <?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;

        ?>

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

        } ?>

			       </tbody>
			    </table>

			</div>

			<div class="row">
			<div class="col-xs-6 text-right pull-right invoice-total">
					  <p class="align-left">Fixed Fee : &nbsp;&nbsp;&nbsp;&nbsp;<?php echo $curr->currency; ?><?php echo $max_fixed_charge; ?></p>
			          <p class="align-left">Discount : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $curr->currency; ?><?php echo $total_discount; ?></p>
			          <p class="text-right"><b>Total : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<td class="text-center" id="total_envio"><?php echo $curr->currency; ?><?php echo $total_envio; ?></td></b> </p>
			</div>
			</div>
			
			<div class="row">
			    <div class="col-xs-6 margintop">
			   <p class="lead marginbottom"><b>Terms & Conditions</b></p>
			   <p>
			       Payment is due within 1 day after delivery of shipment to the following account:<br/>
			       <strong>Gulf African Bank<br/>
			       Account Name: Shiine Travel Agency<br/>
			       Account No: </strong>
			   </p>
			   
			   </div>
			</div>
			<hr style="border-top: dotted 1px black;" />
			<div class="col-xs-4 from">
					<p class="lead marginbottom">Contact Kenya </p>
					<p>Nairobi</p>
					<table id="items">
					+254 798886456</br>    
				    </table>
				</div>

				<div class="col-xs-4 to">
					<p class="lead marginbottom">Contact Somalia </p>
					<p>Mogadishu</p>
					<table id="items">
					+252 619442255</br>    
				    </table>

			    </div>

			    <!--<div class="col-xs-4 text-right payment-details">
					<p class="lead marginbottom text-left">Contact Somaliland</p>
					<p class="text-left">Hargeysa</p>
					<table id="items">
					    <p class="text-left">+252 634100266</p></br>    
				    </table>
			    </div>-->

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

<style>
body{margin-top:5px;
background:#eee;
}
hr{
    display: block;
        height: 5px;
        border: 0;
        border-top: 30px solid #0287cc;
        margin: 1em 0;
        padding: 0;
}

/*Invoice*/
.invoice .top-left {
    font-size:65px;
	color:#3ba0ff;
}

.invoice .top-right {
	text-align:right;
	padding-right:20px;
}

.invoice .table-row {
	margin-left:-15px;
	margin-right:-15px;
	margin-top:25px;
}

.invoice .payment-info {
	font-weight:500;
}

.invoice .table-row .table>thead {
	border-top:1px solid #ddd;
}

.invoice .table-row .table>thead>tr>th {
	border-bottom:none;
}

.invoice .table>tbody>tr>td {
	padding:8px 20px;
}

.invoice .invoice-total {
	margin-right:-10px;
	font-size:16px;
}

.invoice .last-row {
	border-bottom:1px solid #ddd;
}

.invoice-ribbon {
	width:85px;
	height:88px;
	overflow:hidden;
	position:absolute;
	top:-1px;
	right:14px;
}

.ribbon-inner {
	text-align:center;
	-webkit-transform:rotate(45deg);
	-moz-transform:rotate(45deg);
	-ms-transform:rotate(45deg);
	-o-transform:rotate(45deg);
	position:relative;
	padding:7px 0;
	left:-5px;
	top:11px;
	width:120px;
	background-color:#66c591;
	font-size:15px;
	color:#fff;
}

.ribbon-inner:before,.ribbon-inner:after {
	content:"";
	position:absolute;
}

.ribbon-inner:before {
	left:0;
}

.ribbon-inner:after {
	right:0;
}

@media(max-width:575px) {
    
	.invoice .top-left,.invoice .top-right,.invoice .payment-details {
		text-align:center;
	}

	.invoice .from,.invoice .to,.invoice .payment-details {
		float:none;
		width:100%;
		text-align:center;
		margin-bottom:25px;
	}

	.invoice p.lead,.invoice .from p.lead,.invoice .to p.lead,.invoice .payment-details p.lead {
		font-size:22px;
	}

	.invoice .btn {
		margin-top:10px;
	}
}

@media print {
    #printPageButton {
    display: none;
    }
	.invoice {
		width:900px;
		height:800px;
	}
}

</style>
	
	</body>

</html>