<?php 
session_start();

/*if(!isset($_SESSION['tipo']) or $_SESSION['tipo'] != 2){ header("Location:index.php"); }*/
	
include("../conexion_bd.php");
include("../funciones.php"); 
set_time_limit(2000);
?> 
<table width="100%" border="0" cellspacing="0" cellpadding="1" class="stdtable" id="dyntable">

<thead>
                        <tr>
                            <th class="head1">N</th>
                            <th class="head1">Fecha</th>
                            <th class="head1">Folio</th>
                            <th class="head1">Articulo</th>
                            <th class="head1">Piezas</th>
                            <th class="head1">Importe</th>
                            <th class="head1">Descuento</th>
                            <th class="head1">Impuesto</th>
                            
                            <th class="head1">Total</th>
                            <th class="head1">Costo</th>
                            <th class="head1">% Utilidad</th>
                           
                          
                        </tr>
                    </thead>
       <tbody>
	
 
 <?php 
 
  $fecha_inicial = substr($_GET['fecha_inicial'],3,2)."/".substr($_GET['fecha_inicial'],0,2)."/".substr($_GET['fecha_inicial'],6,4); 
  $fecha_final = substr($_GET['fecha_final'],3,2)."/".substr($_GET['fecha_final'],0,2)."/".substr($_GET['fecha_final'],6,4); 
  $id_producto = get_id_producto($_GET['codigo']);
  
  
  
    $x=1;
	$conn = new COM("ADODB.Connection") or die ("Cannot start"); 
    $rs = New COM("ADODB.Recordset");
    $conn->Open(PATH); 
	$rs = $conn->Execute('select * from MGW10010 WHERE CIDDOCUM02 = 4 AND CFECHA >= ctod("'.$fecha_inicial.'") AND CFECHA <= ctod("'.$fecha_final.'") AND CIDPRODU01 = '.$id_producto.' ' );  

    while (!$rs->EOF)
     {
  
  ?>
  <tr>
    <td class="con0" align="center"><?php echo $x++; ?></td>
    <td class="con1"><?php echo $rs->fields['CFECHA']->value(); ?></td>  
    <td class="con1" align="center"><?php echo get_folio($rs->fields['CIDDOCUM01']->value()); ?></td>  
    <td class="con0"><?php echo utf8_encode(nombre_producto($rs->fields['CIDPRODU01']->value())); ?></td>
    <td class="con0" align="right"><?php echo number_format($rs->fields['CUNIDADES']->value());  @$piezas += $rs->fields['CUNIDADES']->value(); ?></td>
    <td class="con0" align="right"><?php echo formato($rs->fields['CNETO']->value());  @$total += $rs->fields['CNETO']->value(); ?></td>
     <td class="con0" align="right"><?php echo formato($rs->fields['CDESCUEN01']->value());  @$total2 += $rs->fields['CDESCUEN01']->value(); ?></td>
      <td class="con0" align="right"><?php echo formato($rs->fields['CIMPUESTO1']->value());  @$total3 += $rs->fields['CIMPUESTO1']->value(); ?></td>
      <td class="con0" align="right"><?php echo formato($rs->fields['CTOTAL']->value());  @$total4 += $rs->fields['CTOTAL']->value(); ?></td>
      
    <td class="con0" align="right"><?php	
$costo =  ultimo_costo_producto($rs->fields['CIDPRODU01']->value()); echo @formato($suma_costo = ($costo*$rs->fields['CUNIDADES']->value()));  @$total5 += $suma_costo; ?></td>
 <td class="con0" align="right">
 <?php 
 echo @number_format((($rs->fields['CTOTAL']->value()-$suma_costo)/$suma_costo)*100,2)." %";
 
 ?>
 
 </td>

    
  </tr>
  <?php 
  $rs->MoveNext();
  }
  
  $rs->Close();
  $conn->Close();
  
   ?>
  <thead>
  <tr>
    <td class="head1" colspan="4" align="center">Total</td>
    <td class="head1" style="text-align:right;"><?php echo @number_format($piezas); ?></td>
    <td class="head1" style="text-align:right;"><?php echo @formato($total); ?></td>
    <td class="head1" style="text-align:right;"><?php echo @formato($total2); ?></td>
     <td class="head1" style="text-align:right;"><?php echo @formato($total3); ?></td>
      <td class="head1" style="text-align:right;"><?php echo @formato($total4); ?></td>
      <td class="head1" style="text-align:right;"><?php echo @formato($total5); ?></td>
      <td class="head1" style="text-align:right;"><?php echo @number_format((($total4-$total5)/$total5)*100,2); ?></td>
    
  </tr> 
  </thead>
  </tbody>
  
 </table>