<?php
include('../../../configuracion.php');
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=contacto_".date('d-m-Y').".xls");

$lafecha = (isset($_GET["datestart"])) ? $_GET["datestart"] : "";
$end = (isset($_GET["dateend"])) ? $_GET["dateend"] : "";
if(!empty($lafecha)){
	$sf = explode("/",$lafecha);
	$dfs = $sf[2]."-".$sf[1]."-".$sf[0];
	$sfend = explode("/",$end);
	$dfsend = $sfend[2]."-".$sfend[1]."-".$sfend[0];
	
	$buscarfecha = " and fecha >= '{$dfs}' and fecha <= '{$dfsend}'";
}else{
	$buscarfecha = "";
}
$resultados = $db->results("comprasbig",'*',"id!='0' ".$filtro_busqueda."  $buscarfecha",'fecha desc');



?>
<table>
	<tr>
    <th>Order ID</th>
		<th>fecha</th>
		<th>nombre</th>
        <th>rut</th>
		<th>email</th>
		<th>telefono</th>
		<th>region</th>
        <th>comuna</th>
        <th>direccion</th>
        <th>Tipo</th>
        <th>Cambio Logistico</th>
       <!--<th>Cod producto</th>-->
        <th>SKU</th>
        <th>despacho</th>
        <th>subtotal</th>
        <th>total</th>
        <th>forma de pago</th>
        <th>COD Autorizacion</th>
        <th>4 Digitos tarjeta</th>
        <th>estado</th>

	</tr>
<?php
foreach($resultados as $dato){

	$json_order = json_decode(Utf8_ansi($dato->big_order));
	$json_sku = json_decode(Utf8_ansi($dato->big_sku));
	$json_shipping = json_decode(Utf8_ansi($dato->big_shipit));

    $fecha = funcion::nombre_dia($dato->fecha).', '.funcion::cambiar_fecha($dato->fecha, 'D de F Y').' a las '.funcion::cambiar_fecha($dato->fecha, 'H:i');
    $region = $db->getvar("regiones","nombre","id='{$dato->region}'");
    $comuna = $db->getvar("comunas","nombre","id='{$dato->comuna}'");

    $loscodigos = "";
    $productos = explode(",",$dato->id_productos);
    foreach($productos as $k => $v){
        $loscodigos .= $db->getVar("productos","codigo","id='{$productos[$k]}'").", ";
    }
    $loscodigos = substr($loscodigos,0,-2);

    $lossku = "";
    $skus = explode(",",$dato->id_precios);
    foreach($skus as $k => $v){
        $lossku .= $db->getVar("productos_precios","codigo","id='{$skus[$k]}'").", ";
    }
    $lossku = substr($lossku,0,-2);



    if(isset($json_shipping[0]->shipping_method) AND ($json_shipping[0]->shipping_method=="Free Shipping" AND $dato->recibelo==1 )){ 
        $datos = array(
            'tipocambio_logistico' => 1
        );
        $db->update("comprasbig",$datos,"id='{$dato->id}'");  
     }
?>
<tr>
    <td><?php echo $dato->orderId?></td>
	<td><?php echo $fecha?></td>
	<td><?php echo $json_order->billing_address->first_name?> <?php echo $json_order->billing_address->last_name?></td>
	<td><?php echo $json_order->billing_address->form_fields[0]->value;?></td>
    <td><?php echo $json_order->billing_address->email?></td>
	<td><?php echo $json_order->billing_address->phone?></td>
<td><?php echo $json_order->billing_address->state?></td>
<td><?php echo $json_order->billing_address->form_fields[1]->value;?>, <?php echo $json_order->billing_address->city?></td>
<td><?php echo $json_order->billing_address->street_1;?> <?php echo $json_order->billing_address->street_2?></td>
<td><?php echo $json_shipping[0]->shipping_method;?></td>
<td><?php if(isset($json_shipping[0]->shipping_method) AND ($json_shipping[0]->shipping_method=="Free Shipping" AND $dato->recibelo==1 )){ echo "SEND RECIBELO"; }?></td>
<!--<td><?php echo $loscodigos?></td>-->
<td>
<?php
		echo "SKU: ";

		for($x=0;$x<count($json_sku);$x++){
			echo $json_sku[$x]->sku.", ";

		}
		?>
</td>
<td><?php echo funcion::formatoPrecio($json_order->shipping_cost_inc_tax) ?></td>
<td><?php echo funcion::formatoPrecio($json_order->subtotal_inc_tax) ?></td>
<td><?php echo funcion::formatoPrecio($json_order->total_inc_tax) ?>.-</td>
<td><?php echo $json_order->payment_method ?></td>
<td><?php echo $dato->wp_authorizationCode?></td>
<td><?php echo $dato->wp_cardNumber?></td>
<td>
<?php echo $json_order->status?>										
</td>


</tr>

<?php }?>
</table>
<?php

function Utf8_ansi($valor) {

	$valor = str_replace("u00c0","À",$valor);
	$valor = str_replace("u00c1","Á",$valor);
    $valor = str_replace("u00c2","Â",$valor);
    $valor = str_replace("u00c3","Ã",$valor);
    $valor = str_replace("u00c4","Ä",$valor);
    $valor = str_replace("u00c5","Å",$valor);
    $valor = str_replace("u00c6","Æ",$valor);
    $valor = str_replace("u00c7","Ç",$valor);
    $valor = str_replace("u00c8","È",$valor);
    $valor = str_replace("u00c9","É",$valor);
    $valor = str_replace("u00ca","Ê",$valor);
    $valor = str_replace("u00cb","Ë",$valor);
    $valor = str_replace("u00cc","Ì",$valor);
    $valor = str_replace("u00cd","Í",$valor);
    $valor = str_replace("u00ce","Î",$valor);
	$valor = str_replace("u00cf","Ï",$valor);
    $valor = str_replace("u00d1","Ñ",$valor);
    $valor = str_replace("u00d2","Ò",$valor);
    $valor = str_replace("u00d3","Ó",$valor);
    $valor = str_replace("u00d4","Ô",$valor);
    $valor = str_replace("u00d5","Õ",$valor);
    $valor = str_replace("u00d6","Ö",$valor);
	$valor = str_replace("u00d8","Ø",$valor);
    $valor = str_replace("u00d9","Ù",$valor);
    $valor = str_replace("u00da","Ú",$valor);
    $valor = str_replace("u00db","Û",$valor);
    $valor = str_replace("u00dc","Ü",$valor);
    $valor = str_replace("u00dd","Ý",$valor);
    $valor = str_replace("u00df","ß",$valor);
    $valor = str_replace("u00e0","à",$valor);
	$valor = str_replace("u00e1","á",$valor);
    $valor = str_replace("u00e2","â",$valor);
    $valor = str_replace("u00e3","ã",$valor);
    $valor = str_replace("u00e4","ä",$valor);
    $valor = str_replace("u00e5","å",$valor);
    $valor = str_replace("u00e6","æ",$valor);
    $valor = str_replace("u00e7","ç",$valor);
    $valor = str_replace("u00e8","è",$valor);
	$valor = str_replace("u00e9","é",$valor);
    $valor = str_replace("u00ea","ê",$valor);
    $valor = str_replace("u00eb","ë",$valor);
    $valor = str_replace("u00ec","ì",$valor);
    $valor = str_replace("u00ed","í",$valor);
    $valor = str_replace("u00ee","î",$valor);
    $valor = str_replace("u00ef","ï",$valor);
    $valor = str_replace("u00f0","ð",$valor);
    $valor = str_replace("u00f1","ñ",$valor);
	$valor = str_replace("u00f2","ò",$valor);
    $valor = str_replace("u00f3","ó",$valor);
    $valor = str_replace("u00f4","ô",$valor);
    $valor = str_replace("u00f5","õ",$valor);
	$valor = str_replace("u00f6","ö",$valor);
    $valor = str_replace("u00f8","ø",$valor);
    $valor = str_replace("u00f9","ù",$valor);
    $valor = str_replace("u00fa","ú",$valor);
    $valor = str_replace("u00fb","û",$valor);
    $valor = str_replace("u00fc","ü",$valor);
    $valor = str_replace("u00fd","ý",$valor);
    $valor = str_replace("u00ff","ÿ",$valor);

    return $valor;      

}
?>