Skip to content

Instantly share code, notes, and snippets.

@neomadara
Created February 10, 2017 14:40
Show Gist options
  • Save neomadara/3031aeb73b24940af9fae7361fbd75e8 to your computer and use it in GitHub Desktop.
Save neomadara/3031aeb73b24940af9fae7361fbd75e8 to your computer and use it in GitHub Desktop.
php to excel
<?
session_start();
include("sg_funciones.php");
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename=Utilidad-'.date('U').'.xls');
header('Pragma: no-cache');
header('Expires: 0');
?>
<meta charset="UTF-8">
<table border="1">
<thead>
<tr>
<th>Tsn</th>
<th>Tracto</th>
<th>Productor</th>
<th>Predio</th>
<th>Contrato</th>
<th>Kms</th>
<th>Kilos</th>
<th>Tarifa Ts</th>
<th>Neto Ts</th>
<th>Con Iva Ts</th>
<th>Tarifa Cyt</th>
<th>Neto Cyt</th>
<th>Neto Con Iva Cyt</th>
<th>Utilidad Neto</th>
<th>Utilidad Con Iva</th>
</tr>
</thead>
<tbody>
<?
$select = "select * from viajes";
$result = mysqli_query($link,$select);
$total=0;
while($row = mysqli_fetch_array($result)){
//idtracto
$idtracto=$row['id_tractos'] ;
//buscar ncamion
$query_ncamion = "select * from temporadas where id_tractos = '".$idtracto."'";
$result_ncamion = mysqli_query($link,$query_ncamion);
$data_ncamion = mysqli_fetch_assoc($result_ncamion);
$ncamion = $data_ncamion['numerotracto_temporadas'];
$kilos = $row['kilos'];
//buscar nombre productor
$idproductor = $row['id_productores'];
$query_nombre_productor = "select * from productores where id_productores = '".$idproductor."' ";
$result_nombre_productor = mysqli_query($link,$query_nombre_productor);
$data_nombre_productor = mysqli_fetch_assoc($result_nombre_productor);
$nombre_productor = $data_nombre_productor['nombre_productores'];
//buscar nombre predio
$idpredio = $row['id_predios'];
$query_nombre_predio = "select * from predios where id_predios = '".$idpredio."' ";
$result_nombre_predio = mysqli_query($link,$query_nombre_predio);
$data_nombre_predio = mysqli_fetch_assoc($result_nombre_predio);
$nombre_predio = $data_nombre_predio['nombre_predios'];
//buscar numero contrato
$idcontrato = $row['id_contrato'];
$query_numero_contrato = "select * from contratos where id_contratos = '".$idcontrato."' ";
$result_numero_contrato = mysqli_query($link,$query_numero_contrato);
$data_numero_contrato = mysqli_fetch_assoc($result_numero_contrato);
$numero_contrato = $data_numero_contrato['numero_contratos'];
//buscar tipo
$query_tipocamion = "select concarro from tractos where id = '".$idtracto."' ";
$result_tipocamion = mysqli_query($link,$query_tipocamion);
$data_tipocamion = mysqli_fetch_assoc($result_tipocamion);
$tipocamion = $data_tipocamion['concarro'];
//kilometros
$km = $row['km'];
//obtener tarifa
$query_tarifa = "select * from tarifas where hastakm >= '".$km."' limit 1";
$result_tarifa = mysqli_query($link,$query_tarifa);
$data_tarifa = mysqli_fetch_assoc($result_tarifa);
if($tipocamion==0){
$tarifats = $data_tarifa['tscamion'];
$tarifacyt = $data_tarifa['cytcamion'];
}else{
$tarifats = $data_tarifa['tscarro'];
$tarifacyt = $data_tarifa['cytcarro'];
}
$tarifats = str_replace(",",".",$tarifats);
$tarifacyt = str_replace(",",".",$tarifacyt);
$resultado_ts = round($kilos*$tarifats); //neto ts
$resultado_ts_con_iva = round($resultado_ts + ($resultado_ts*0.19)); //neto ts con iva o total
$resultado_cyt = round($kilos*$tarifacyt); //neto cyt
$resultado_cyt_con_iva = round($resultado_cyt + ($resultado_cyt*0.19));
$resultado = $resultado_cyt-$resultado_ts ; //resultado sin iva
$resultado_con_iva = $resultado_cyt_con_iva-$resultado_ts_con_iva; //resultado con iva
$total = $total + $resultado;
echo '<tr>';
echo '<td align="right">'.$row['id_viajes'].'</td>';
echo '<td align="right">'.$ncamion.'</td>';
echo '<td>'.$nombre_productor.'</td>';
echo '<td>'.$nombre_predio.'</td>';
echo '<td align="right">'.$numero_contrato.'</td>';
echo '<td align="right">'.$km.'</td>';
echo '<td align="right">'.$kilos.'</td>';
echo '<td align="right">'.$tarifats.'</td>';
echo '<td align="right">$'.enpesos($resultado_ts).'</td>';
echo '<td align="right">$'.enpesos($resultado_ts_con_iva).'</td>';
echo '<td align="right">'.$tarifacyt.'</td>';
echo '<td align="right">$'.enpesos($resultado_cyt).'</td>';
echo '<td align="right">$'.enpesos($resultado_cyt_con_iva).'</td>';
echo '<td align="right">$'.enpesos($resultado).'</td>';
echo '<td align="right">$'.enpesos($resultado_con_iva).'</td>';
echo '</tr>';
}
?>
</tbody>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment