<?php
session_start();
$ruta='../';
require_once("clases/covensol_cxc_c_cuentasxcobrar.php");
$objcxc = new covensol_cxc_cuentasxcobrar();
$objcxc->io_conexiones->decodificar_post();
$opciones = $objcxc->io_conexiones->asignar_post();
$resp = $objcxc->MonedaPorDefecto();
switch($opciones['criterio']){                
	case 'mostrar_cxc_cliente': 
	        $movcob=false;
			$diasabled="";
			$retornar="";
			$textocob="A Cobrar";
			$opciones['criterio']='por_listado_cxc';
			$proc='mostrar_cxc_cliente';
			$filas = $objcxc->consulta_facturas($opciones); 
			break;	
	case 'mostrar_cxc_ingresos': 
	        $movcob=true;			
			$diasabled="disabled";
			$retornar="return false;";
			$opciones['criterio']='por_listado';
			$opciones['tipdoc']='FACT';
			$textocob="Cob Aplic";
			$opciones['no_anulados'] = true;
			$proc='mostrar_cxc_ingresos';			
			$filas = $objcxc->ConsultaMovCobros($opciones); 
			break;		
}		

$ancho[0] = 10;
$ancho[1] = 40;
$ancho[2] = 40;
$ancho[3] = 50;
$ancho[4] = 60;
$ancho[5] = 180;
$ancho[6] = 70;
$ancho[7] = 70;
$ancho[8] = 80;
$ancho[9] = 80;
$ancho[10] = 50;
$ancho[11] = 50;
$ancho[12] = 100;
$ancho[13] = 100;
$ancho[15] = 100;
$ancho[16] = 100;
$ancho[17] = 100;
$ancho[18] = 100;		
$ancho_total = array_sum($ancho);
			
if(!$filas['rs']->RecordCount()){
	//$mensaje = 'No hay facturas encontradas para este criterio de busqueda.<br>';
	//$objcxc->io_conexiones->mensajes_ajax($mensaje);
	exit();
}
$clave = 1;
?>
<link href="../shared/css/tablas.css" rel="stylesheet" type="text/css">
<link href="../shared/css/ventanas.css" rel="stylesheet" type="text/css">
<link href="../shared/css/cabecera.css" rel="stylesheet" type="text/css">
<link href="../shared/css/general.css" rel="stylesheet" type="text/css">
<table width="786" border="0" align="center" cellpadding="0" cellspacing="20" class="contorno">
      <tr>
        <td >
            <table id="GridCxcCliente" width="<?php echo $ancho_total; ?>" border="1" cellpadding="0" cellspacing="0"  bordercolor="#CCCCCC" bgcolor="#FFFFFF" style="border-collapse:collapse">
              <tr class="titulo-celda">
                <td colspan="19" align="center" class="formato-azul">FACTURAS</td>
              </tr>
              <tr class="titulo-celda">
                <td width="<?php echo $ancho[0] ?>" align="center">Apli</td>
                <td width="<?php echo $ancho[1] ?>" align="center">Sucursal</td>
                <td width="<?php echo $ancho[2] ?>" align="center">Caja</td>
                <td width="<?php echo $ancho[3] ?>" align="center">Número</td>
                <td width="<?php echo $ancho[4] ?>" align="center">Control</td>
                <td width="<?php echo $ancho[5] ?>" align="center">Cliente</td>
                <td width="<?php echo $ancho[6] ?>" align="center">Est. Fact.</td>
                <td width="<?php echo $ancho[7] ?>" align="center">Est. CXC.</td>
                <td width="<?php echo $ancho[8] ?>" align="center">Cond. Pag</td>
                <td width="<?php echo $ancho[9] ?>" align="center">Fecha</td>
                <td width="<?php echo $ancho[10] ?>" align="center">Mon</td>
                <td width="<?php echo $ancho[11] ?>" align="center">Tasa</td>
                <td width="<?php echo $ancho[12] ?>" align="center">Monto </td>
                <td width="<?php echo $ancho[13] ?>" align="center"><?php echo $textocob; ?> </td>                
                <td width="<?php echo $ancho[15] ?>" align="center">Mon. Cob.</td>
                <td width="<?php echo $ancho[16] ?>" align="center">Reten.</td>
                <td width="<?php echo $ancho[17] ?>" align="center">Cred.</td>
                <td width="<?php echo $ancho[18] ?>" align="center">Saldo</td>                
              </tr>
              <?php foreach($filas['rs'] as $datos) {
                    //SEGURIDAD DE LA SUCURSAL
                    $objcxc->codintper = $datos['codsuc'];		
                    if(!$objcxc->obtSegCxc()){continue;}
                    
                    //SEGURIDAD DE LA CAJA
                    $objcxc->codintper = $datos['codsuc'].' - '.$datos['codcaj'];		
                    if(!$objcxc->obtSegCxc()){continue;}
                    
										
					$datos['id_fact'] = $datos['id_fact']?$datos['id_fact']:$datos['id_doc'];
					
                    $aceptar = 'onClick="aceptar('.$datos['id_fact'].",'".
                                                  $objcxc->formato_numero_fact($datos['numfact'])."','".
                                                  $datos['numcont']."','".
                                                  $datos['codfact']."','".
                                                  $datos['codsuc']."','".
                                                  $datos['codcaj']."','".
                                                  $clave."')".'"'; 
                                                                                        
                    if($datos['abrmon']=='E'){$datos['abrmon']='&#x20AC';}								
                    switch($datos['id_estfact']){                
                            case '1': $color_fila='style="background-color:#FFFFFF"'; break;
                            case '4': $color_fila='style="background-color:#DAE9F3"'; break;
                            case '3': $color_fila='style="background-color:#E1ECEC"'; break;
                            case '7': $color_fila='style="background-color:#FF7777"'; break;
                    }
                                                                                                            
                    $datos['totmonext'] = ($datos['codmon']!=$objcxc->DatosMonedaDef['codmon'])?number_format($datos['total']/$datos['tascam'],2,',','.'):"";
                            
                    $cobrado = $objcxc->BuscarMontoCobradoFact($datos);
                    if($cobrado===false){exit();}
                    
					$ncapli = $objcxc->BuscarCreditosFact($datos);
                    if($ncapli===false){exit();}
					
					$MonAnt = $objcxc->BuscarMontoAntApliFact($datos);
					if($MonAnt===false){exit();}
					
					$creditos = $ncapli + $MonAnt;
					
                    $datos['moncob'] = $cobrado;
                    $Saldo = $objcxc->SaldoFact($datos);
                    
                    $objcxc->CalcularVencimientoCxc($datos);
                    
                    if($objcxc->DiasVenCxc){$color_fila='style="background-color:'.$objcxc->ColorEstVenCxc.'"';}
                    $color_fila='style="background-color:#FFFFFF"';		
                    $datos['totmonext'] = $datos['totmonext']==''?"0,00":$datos['totmonext'];				
					
					if($proc=='mostrar_cxc_ingresos'){
						$datos['codmon'] = $datos['codmonfact'];
						$datos['tascam'] = $datos['tascamfact'];
						$datos['abrmon'] = $datos['abrmonfact'];
						$datos['denmon'] = $datos['denmonfact'];	
					}
									
					$datos = $objcxc->FormatDatosFactPresent($datos);
					$acobrar = ($movcob)?number_format($datos['monmovcob']/$objcxc->TasaCambio,2,',','.'):'0,00';
					$objcxc->MonTotRet = $objcxc->MonTotRet/$objcxc->TasaCambio;
					$creditos = $creditos/$objcxc->TasaCambio;
					$Saldo = $Saldo/$objcxc->TasaCambio;
					
					/*************************** NO MUESTRA LAS ANULADAS ***********************************/
					if($datos['id_estfact']==7){continue;}
					/***************************************************************************************/
					
                ?>
                   <tr id="consulta" <?php echo $color_fila; ?> >
                      <td width="<?php echo $ancho[0] ?>" align="right"><img src="../shared/imagebank/tools20/aprobado.gif" alt="aplicar" width="20" height="20" style="cursor:pointer"  onclick=" <?php echo $retornar; ?> ObjForm.AplicarCobroFila(<?php echo $clave; ?>);" /></td>
                     <td width="<?php echo $ancho[1] ?>" align="right"><div style="color:#189FBC;padding-right:10px;"><?php echo $datos['sigsuc']; ?></div></td> 
                      <td width="<?php echo $ancho[2] ?>" align="right"><div style="color:#189FBC;padding-right:10px;"><?php echo $datos['codcaj']; ?></div></td>
                      <td width="<?php echo $ancho[3] ?>" align="right"><div style="color:#189FBC;padding-right:10px;"><?php echo $datos['numfact']; ?></div></td>
                      <td width="<?php echo $ancho[4] ?>" align="right"><?php echo $datos['numcont']; ?></td>
                      <td width="<?php echo $ancho[5] ?>" align="left"><?php echo $datos['nombre_cliente']; ?></td>
                      <td width="<?php echo $ancho[6] ?>" align="left"><?php echo $datos['descestfact']; ?></td>
                      <td width="<?php echo $ancho[7] ?>" align="left"><?php echo $objcxc->DescEstVenCxc; ?></td>
                      <td width="<?php echo $ancho[8] ?>" align="left"><?php echo $datos['desccondpago']; ?></td>
                      <td width="<?php echo $ancho[9] ?>" align="center"><?php echo $datos['fecfact']; ?></td>
                      <td width="<?php echo $ancho[10] ?>" align="center"><?php echo $datos['abrmon']; ?></td>
                      <td width="<?php echo $ancho[11] ?>" align="right"><?php echo $datos['tascam']; ?></td>
                      <td width="<?php echo $ancho[12] ?>" align="right"><input name="txttotal<?php echo $clave;?>" type="text" class="sin-borde" id="txttotal<?php echo $clave;?>" style="text-align:right; color:#000000;" value="<?php echo $datos['total']; ?>" readonly <?php echo $diasabled ?> /></td>
                      <td width="<?php echo $ancho[13] ?>" align="left"><input name="txtmonmovcob<?php echo $clave;?>" type="text" class="sin-borde" id="txtmonmovcob<?php echo $clave;?>"  onblur="ObjForm.ActualizarTotales(); ObjForm.ValidarEdicionFila(<?php echo $clave; ?>);" onKeyPress="resp = ObjForm.ValidarTotales(); if(resp===false){return false;} return(cj_formatonumero(this,'.',',',event));" style="text-align:right; color:#000000; " value="<?php echo $acobrar; ?>"  <?php echo $diasabled ?>/></td>
                      <td width="<?php echo $ancho[15] ?>" align="right"><input name="txtmoncob<?php echo $clave;?>" type="text" class="sin-borde" id="txtmoncob<?php echo $clave;?>" style="text-align:right; color:#000000;" value="<?php echo $datos['moncob']; ?>"  <?php echo $diasabled ?> readonly/></td>
                      <td width="<?php echo $ancho[16] ?>" align="right"><input name="txtmonret<?php echo $clave;?>" type="text" class="sin-borde" id="txtmonret<?php echo $clave;?>" style="text-align:right; color:#000000;" value="<?php echo number_format($objcxc->MonTotRet,2,',','.'); ?>" size="10" readonly  <?php echo $diasabled ?>/>
                      <img src="../shared/imagebank/tools20/buscar.gif" alt="buscar" style="cursor:pointer" width="15" height="15" onclick=" ObjForm.AplicarRetenciones('<?php echo $clave; ?>');" />                      </td>
                      <td width="<?php echo $ancho[17] ?>" align="right"><input name="txtotros<?php echo $clave;?>" type="text" class="sin-borde" id="txtotros<?php echo $clave;?>" style="text-align:right; color:#000000;" value="<?php echo number_format($creditos,2,',','.'); ?>" size="10" readonly  <?php echo $diasabled ?>/>
                      <img src="../shared/imagebank/tools20/buscar.gif" alt="buscar" style="cursor:pointer" width="15" height="15" onclick=" ObjForm.AplicarOtros(<?php echo $clave; ?>);" />                      </td>                      
                      <td width="<?php echo $ancho[18] ?>" align="right"><input name="txtsaldo<?php echo $clave;?>" type="text" class="sin-borde" id="txtsaldo<?php echo $clave;?>" style="text-align:right; color:#000000;" value="<?php echo number_format($Saldo,2,',','.'); ?>"  <?php echo $diasabled ?> readonly/>
                        <input name="hnombre_cliente<?php echo $clave;?>" type="hidden" id="hnombre_cliente<?php echo $clave;?>"  value="<?php echo $datos['nombre_cliente']; ?>"/>                       
                        <input name="hidestfact<?php echo $clave;?>" type="hidden" id="hidestfact<?php echo $clave;?>"  value="<?php echo $datos['id_estfact']; ?>"/>
                        <input name="hestfact<?php echo $clave;?>" type="hidden" id="hestfact<?php echo $clave;?>"  value="<?php echo $datos['descestfact']; ?>"/>
                        <input name="hid_fact<?php echo $clave;?>" type="hidden" id="hid_fact<?php echo $clave;?>"  value="<?php echo $datos['id_fact']; ?>"/>
                        <input name="hnumfact<?php echo $clave;?>" type="hidden" id="hnumfact<?php echo $clave;?>"  value="<?php echo $datos['numfact']; ?>"/>
                        <input name="hnomsuc<?php echo $clave;?>" type="hidden" id="hnomsuc<?php echo $clave;?>"  value="<?php echo $datos['nomsuc']; ?>"/>
                        <input name="hnomcaj<?php echo $clave;?>" type="hidden" id="hnomcaj<?php echo $clave;?>"  value="<?php echo $datos['nomcaj']; ?>"/>                     
                        <input name="htotcredapli<?php echo $clave;?>" type="hidden" id="htotcredapli<?php echo $clave;?>"  value="0"/>
                       	<input name="htotantapli<?php echo $clave;?>" type="hidden" id="htotantapli<?php echo $clave;?>"  value="0"/>
                        <input name="hcredaplifact<?php echo $clave;?>" type="hidden" id="hcredaplifact<?php echo $clave;?>"  value="<?php echo number_format($creditos,2,',','.'); ?>"/>
                        <input name="txtcodmon<?php echo $clave;?>" type="hidden" id="txtcodmon<?php echo $clave;?>"  value="<?php echo $datos['codmon'];?>"/>
                        <input name="txttascam<?php echo $clave;?>" type="hidden" id="txttascam<?php echo $clave;?>"  value="<?php echo $datos['tascam'];?>"/>
                        <input name="txtabrmon<?php echo $clave;?>" type="hidden" id="txtabrmon<?php echo $clave;?>"  value="<?php echo $datos['abrmon'];?>"/>
                        <input name="totantapl<?php echo $clave;?>" type="hidden" id="totantapl<?php echo $clave;?>"  value="<?php echo number_format($MonAnt/$objcxc->TasaCambio,2,',','.'); ?>"/>
                        <input name="totncapl<?php echo $clave;?>" type="hidden" id="totncapl<?php echo $clave;?>"  value="<?php echo number_format($ncapli/$objcxc->TasaCambio,2,',','.'); ?>"/>
                       </td>   
                  </tr>
                  
            
              <?php $clave++;  } ?>
            </table>	         
    </td>
   </tr>
 </table>