<?php
 // ini_set('display_errors',1);
session_start();
require_once ("../../config/Cado.php");
require_once ("../compra/cCompra.php");
$oCompra = new cCompra();
require_once ("../venta/cVenta.php");
$oVenta = new cVenta();
require_once("../tipocambio/cTipocambio.php");
$oTipocambio = new ctipocambio();
require_once ("../notacredito/cNotacredito.php");
$oNotacredito = new cNotacredito();
$fec = date('Y-m-d');
require_once ("../formatos/formato.php");
require_once ("../formatos/fechas.php");
?>
<style>
	div#div_tabla { margin: 0 0; }
	div#div_tabla table { margin: 0 0; border-collapse: collapse; width: 100%; }
	div#div_tabla table td, div#div_tabla table th { border: 1px solid #eee; padding: 2px 3px; font-size:15px; }
	div#div_tabla table th { height:18px }
	div#div_tabla table td { height:17px }
</style>
<br />
		<div id="div_tabla" class="table-responsive">
		<table>
		<tr>
		<th colspan="9" >DETALLES DE COMPRAS</th>
		</tr>
		
		 <tr>
                <th class="" >FECHA</th>
                <th class="" >PERIODO</th>
                <th>DOCUMENTO</th>                    
                <th>RUC</th>             
                <th>PROVEEDOR</th>             
                <th class="" >EMPRESA</th>      
                <th NOWRAP class="" align="right">VALOR</th>
                <th NOWRAP class="" align="right">IGV</th>
                <th NOWRAP class="" align="right">TOTAL</th>
        </tr>
		
		<?php 
		$dtc1 = $oCompra->mostrar_detalle_agru($_POST['mes'],$_POST['anio']);
		while($dtc = mysqli_fetch_array($dtc1)){
		
		$dtc1d = $oCompra->mostrar_detalle_des($_POST['mes'],$_POST['anio'],$dtc['empid']);
		while($dt = mysqli_fetch_array($dtc1d)){
		
		 $i = $dt['tb_compra_permes'];
    	                                                if($i=='01'){
															$lblmes = 'Enero';
														}else if($i=='02'){
															$lblmes = 'Febrero';
														}else if($i=='03'){
															$lblmes = 'Marzo';
														}else if($i=='04'){
															$lblmes = 'Abril';
														}else if($i=='05'){
															$lblmes = 'Mayo';
														}else if($i=='06'){
															$lblmes = 'Junio';
														}else if($i=='07'){
															$lblmes = 'Julio';
														}else if($i=='08'){
															$lblmes = 'Agosto';
														}else if($i=='09'){
															$lblmes = 'Septiembre';
														}else if($i=='10'){
															$lblmes = 'Octubre';
														}else if($i=='11'){
															$lblmes = 'Noviembre';
														}else if($i=='12'){
															$lblmes = 'Diciembre';
														}
														
		if($dt['tb_moneda_id']== 2){
				$rs = $oTipocambio->consultar(fecha_mysql($dt['tb_compra_fec']));  
				$r = mysqli_fetch_array($rs);	
				$tipcam = formato_money3($r['tb_tipocambio_dolsun']);
		}
		else{
				$tipcam = '1.00';	
		}
		
		$valvenc = 0;
		$igvc = 0;
		$totc = 0;
		
		
		$valvenc = $dt['tb_compra_valven'] * $tipcam;
		$igvc = $dt['tb_compra_igv'] * $tipcam;
		$totc = $dt['tb_compra_tot'] * $tipcam;
		
		//if($dt['tb_empresa_id'] == 2){
			//echo $totc;
		//}
		
		
		$totalc  += $totc; 
		$igvtotalc  += $igvc; 
		$valventotalc  += $valvenc; 
		
		?>
		<tr>
		<td class="" nowrap="nowrap"><?php echo mostrarFecha($dt['tb_compra_fec'])?></td>
        <td class="" nowrap="nowrap"><?php echo $dt['tb_compra_peranio'].' | '. $lblmes ?></td>
        <td><?php echo $dt['tb_documento_abr'].' - '.$dt['tb_compra_numdoc']?></td>
        <td NOWRAP ><?php echo $dt['tb_proveedor_doc']?></td>
        <td NOWRAP ><?php echo $dt['tb_proveedor_nom']?></td>
        <td NOWRAP class="" ><?php echo $dt['tb_empresa_nomcom']?></td>
		 <td NOWRAP align="right"><?php echo 'S/ '.formato_money($valvenc)?></td>
		 <td NOWRAP align="right"><?php echo 'S/ '.formato_money($igvc)?></td>
		 <td NOWRAP align="right"><?php echo 'S/ '.formato_money($totc)?></td>
		</tr>
		<?php
		} 
		?>
		<tr class="bg-blue" >
		<td colspan="6" align="right"> 
		<?php echo $dtc['emp'];?>
		</td>
		<td NOWRAP align="right" ><?php echo 'S/ '.formato_money($valventotalc)?></td>
		<td NOWRAP align="right" ><?php echo 'S/ '.formato_money($igvtotalc)?></td>
		<td NOWRAP align="right" ><?php echo 'S/ '.formato_money($totalc)?></td>
		</tr>
		<?php 		
		$totalc  = 0;
		$igvtotalc  = 0;
		$valventotalc  = 0;
		}
		
		
		mysqli_free_result($dtc1);?>
		</table>
		
		</div>
		
		<br />
		<div id="div_tabla" class="table-responsive">
		<table>
		<tr>
		<th colspan="8" >DETALLES DE VENTAS</th>
		</tr>
		
		 <tr>
                <th class="" >FECHA</th>
                <th>DOCUMENTO</th>                    
                <th>RUC</th>             
                <th>CLIENTE</th>             
                <th class="" >EMPRESA</th>      
                <th NOWRAP class="" align="right">VALOR</th>
                <th NOWRAP class="" align="right">IGV</th>
                <th NOWRAP class="" align="right">TOTAL</th>
        </tr>
		
		<?php 
		$dtc1 = $oVenta->mostrar_detalle_agru($_POST['mes'],$_POST['anio'],'');
		while($dtc = mysqli_fetch_array($dtc1)){
		
		$dtc1d = $oVenta->mostrar_detalle_des($_POST['mes'],$_POST['anio'],$dtc['empid']);
		while($dt = mysqli_fetch_array($dtc1d)){
		
		if($dt['tb_moneda_id']==2){
				$rs = $oTipocambio->consultar(fecha_mysql($dt['tb_venta_fec']));  
				$r = mysqli_fetch_array($rs);	
				$tipcam = formato_money3($r['tb_tipocambio_dolsun']);
		}
		else{
				$tipcam = '1.00';	
		}
		
		
		$valvenv = 0;
		$igvv = 0;
		$totv = 0;
		
		$valvenv = $dt['tb_venta_valven'] * $tipcam;
		$igvv = $dt['tb_venta_igv'] * $tipcam;
		$totv = $dt['tb_venta_tot'] * $tipcam;
		
		$totalv  += $totv; 
		$igvtotalv  += $igvv; 
		$valventotalv  += $valvenv; 
		
		?>
		<tr>
		<td class="" nowrap="nowrap"><?php echo mostrarFecha($dt['tb_venta_fec'])?></td>
        <td nowrap ><?php echo $dt['tb_documento_abr'].' - '.$dt['tb_venta_numdoc']?></td>
        <td NOWRAP ><?php echo $dt['tb_cliente_doc']?></td>
        <td NOWRAP ><?php echo $dt['tb_cliente_nom']?></td>
        <td NOWRAP class="" ><?php echo $dt['tb_empresa_nomcom']?></td>
		 <td NOWRAP align="right"><?php echo 'S/ '. formato_money($valvenv)?></td>
		 <td NOWRAP align="right"><?php echo 'S/ '.formato_money($igvv)?></td>
		 <td NOWRAP align="right"><?php echo 'S/ '.formato_money($totv)?></td>
		</tr>
		<?php
		} 
		?>
		<tr class="bg-blue" >
		<td  colspan="5" align="right"> 
		<?php echo $dtc['emp'];?>
		</td>
		<td NOWRAP align="right"><?php echo 'S/ '.formato_money($valventotalv)?></td>
		<td NOWRAP align="right" ><?php echo 'S/ '.formato_money($igvtotalv)?></td>
		<td NOWRAP align="right" ><?php echo 'S/ '.formato_money($totalv)?></td>
		</tr>
		<?php 
		
		$totalv = 0; 
		$igvtotalv = 0; 
		$valventotalv  = 0; 
		
		}  
		
		mysqli_free_result($dtc1);?>
		</table>
		
		</div>
		<br />
		<div id="div_tabla" class="table-responsive">		
		<table>
		<tr>
		<th colspan="8" >DETALLES DE NOTAS DE CREDITO</th>
		</tr>
		
		 <tr>
                <th class="" >FECHA</th>
                <th>DOCUMENTO</th>                    
                <th>RUC</th>             
                <th>CLIENTE</th>             
                <th class="" >EMPRESA</th>      
                <th NOWRAP class="" align="right">VALOR</th>
                <th NOWRAP class="" align="right">IGV</th>
                <th NOWRAP class="" align="right">TOTAL</th>
        </tr>
		
		<?php 
		$dtc1 = $oNotacredito->mostrar_detalle_agru($_POST['mes'],$_POST['anio'],'');
		while($dtc = mysqli_fetch_array($dtc1)){
		
		$dtc1d = $oNotacredito->mostrar_detalle_des($_POST['mes'],$_POST['anio'],$dtc['tb_empresa_id']);
		while($dt = mysqli_fetch_array($dtc1d)){
		
		if($dt['tb_moneda_id']==2){
				$rs = $oTipocambio->consultar(fecha_mysql($dt['tb_notacredito_fec']));  
				$r = mysqli_fetch_array($rs);	
				$tipcam = formato_money3($r['tb_tipocambio_dolsun']);
		}
		else{
				$tipcam = '1.00';	
		}
		
		
		$valvenv = 0;
		$igvv = 0;
		$totv = 0;
		
		$totv = $dt['tb_notacredito_tot'] * $tipcam;
		$valvenv = $totv/1.18;
		$igvv = $valvenv * 0.18;
		
		
		$totalv  += $totv; 
		$igvtotalv  += $igvv; 
		$valventotalv  += $valvenv; 
		
		?>
		<tr>
		<td class="" nowrap="nowrap"><?php echo mostrarFecha($dt['tb_notacredito_fec'])?></td>
        <td nowrap ><?php echo $dt['tb_documento_abr'].' - '.$dt['tb_notacredito_numdoc']?></td>
        <td NOWRAP ><?php echo $dt['tb_cliente_doc']?></td>
        <td NOWRAP ><?php echo $dt['tb_cliente_nom']?></td>
        <td NOWRAP class="" ><?php echo $dt['tb_empresa_nomcom']?></td>
		 <td NOWRAP align="right"><?php echo 'S/ '. formato_money($valvenv)?></td>
		 <td NOWRAP align="right"><?php echo 'S/ '.formato_money($igvv)?></td>
		 <td NOWRAP align="right"><?php echo 'S/ '.formato_money($totv)?></td>
		</tr>
		<?php
		} 
		?>
		<tr class="bg-blue" >
		<td  colspan="5" align="right"> 
		<?php echo $dtc['tb_empresa_razsoc'];?>
		</td>
		<td NOWRAP align="right"><?php echo 'S/ '.formato_money($valventotalv)?></td>
		<td NOWRAP align="right" ><?php echo 'S/ '.formato_money($igvtotalv)?></td>
		<td NOWRAP align="right" ><?php echo 'S/ '.formato_money($totalv)?></td>
		</tr>
		<?php 
		
		$totalv = 0; 
		$igvtotalv = 0; 
		$valventotalv  = 0; 
		
		}  
		
		mysqli_free_result($dtc1);?>
		</table>
		
		</div>
		
		