<?php 
require_once "../modelos/Estadocuentacliente.php";
if (strlen(session_id())<1) 
	session_start();

$cliente=new Estadocuentacliente();

$idcliente		=isset($_POST["idcliente"])? limpiarCadena($_POST["idcliente"]):"";
$tipo_cliente	=isset($_POST["tipo_cliente"])? limpiarCadena($_POST["tipo_cliente"]):"";
$nombre				=isset($_POST["nombre"])? limpiarCadena($_POST["nombre"]):"";
$idusuario		=$_SESSION["idusuario"];

switch ($_GET["op"]) {
		
	case 'mostrar':
		$rspta=$cliente->mostrar($idcliente);
		echo json_encode($rspta);
	break;

   case 'listar':
		$rspta=$cliente->listar();
		$data=Array();

		while ($reg=$rspta->fetch_object()) {
			if ($reg->saldod>0){
				$estado = '<span class="badge bg-red">Pendiente</span>';
				if ($reg->notacredito=='si'){
					$estado = '<span class="badge bg-red">Pendiente-NotaCrédito</span>';
				}
				if ($reg->pagadod>0) {
					$estado = '<span class="badge bg-orange">Parcial</span>';
					if ($reg->notacredito=='si'){
						$estado = '<span class="badge bg-orange">Parcial-NotaCrédito</span>';
					}
				}
				
				
			}else{
				$estado = '<span class="badge bg-green">Sin Deuda</span>';
			}
			$fecha		= $reg->fecha;
			$ranking 	= $reg->rank11;
			if($reg->fecha=='01-01-2121'){
				$fecha='-';
				$ranking 	= '';
			} 
			$fechan=$reg->fechanumero;
			if($reg->fechanumero=='80721'){
				$fechan='-';
				$fechan='-';
			}

			if($reg->nrodias==0){
				$diac = '<span class="badge bg-blue">'.$reg->nrodias.'</span>';
			}else{
				$diac = '<span class="badge bg-orange">'.$reg->nrodias.'</span>';
			}

            if($reg->promediodiascalle==0){
                $promediod = '<div align="center">-</div>';
            }else{
                $promediod = '<div align="center">'.number_format("$reg->promediodiascalle",0,",",".").'</div>';
            }
            
			$data[]=array(
			"0"=>'<button class="btn btn-warning btn-xs" onclick="mostrar('.$reg->idcliente.')"><i class="fa fa-eye"></i></button>',
			"1"=>'<div align="left">'.$reg->nombre.'</div>',
			"2"=>'<div align="left">'.$reg->vendedor.'</div>',
			"3"=>'<div align="center">'.$fecha.'</div>',
			/* "3"=>'<div align="center">'.$ranking.'</div>', */
			"4"=>'<div align="center">'.$reg->nrodocumento.'</div>',
			"5"=>'<div align="right">'.number_format("$reg->deudad",2,",",".").'</div>',
			"6"=>'<div align="right">'.number_format("$reg->pagadod",2,",",".").'</div>',
			"7"=>'<div align="right">'.number_format("$reg->saldod",2,",",".").'</div>',
			"8"=>'<div align="right">'.number_format("$reg->promediov90dias",2,",",".").'</div>',
			"9"=>'<div align="center">'.number_format("$reg->promedios_cajas",0,",",".").'</div>',
			"10"=>$promediod,
			"11"=>'<div align="center">'.$estado.'</div>',
			"12"=>$reg->fechanumero
            );
		}
		$results=array(
						"sEcho"=>1,//info para datatables
						"iTotalRecords"=>count($data),//enviamos el total de registros al datatable
						"iTotalDisplayRecords"=>count($data),//enviamos el total de registros a visualizar
						"aaData"=>$data); 
		echo json_encode($results);
	break;
	
	// Para el boton de exportacion
	case 'listarestadodecuentageneral':
		$rspta=$cliente->listarestadodecuentageneral();
		$data=Array();

		while ($reg=$rspta->fetch_object()) {
			if ($reg->saldod>0){
				$estado = '<span class="badge bg-red">Pendiente</span>';
				if ($reg->notacredito=='si'){
					$estado = '<span class="badge bg-red">Pendiente-NotaCrédito</span>';
				}
				if ($reg->pagadod>0) {
					$estado = '<span class="badge bg-orange">Parcial</span>';
					if ($reg->notacredito=='si'){
						$estado = '<span class="badge bg-orange">Parcial-NotaCrédito</span>';
					}
				}
				
				
			}else{
				$estado = '<span class="badge bg-green">Sin Deuda</span>';
			}
			$fecha		= $reg->fecha;
			$ranking 	= $reg->rank11;
			if($reg->fecha=='01-01-2121'){
				$fecha='-';
				$ranking 	= '';
			} 
			$fechan=$reg->fechanumero;
			if($reg->fechanumero=='80721'){
				$fechan='-';
				$fechan='-';
			}

			$data[]=array(
			
			"0"=>'<div align="left">'.$reg->nombre.'</div>',
			"1"=>'<div align="right">'.$reg->deudad.'</div>',
			"2"=>'<div align="right">'.$reg->pagadod.'</div>',
			"3"=>'<div align="right">'.$reg->saldod.'</div>',
			"4"=>'<div align="center">'.$estado.'</div>',
			 );
		}
		$results=array(
						"sEcho"=>1,//info para datatables
						"iTotalRecords"=>count($data),//enviamos el total de registros al datatable
						"iTotalDisplayRecords"=>count($data),//enviamos el total de registros a visualizar
						"aaData"=>$data); 
		echo json_encode($results);
	break;

	case 'listarvendedor':
		$rspta=$cliente->listarlistarvendedor($idusuario);
		$data=Array();

		while ($reg=$rspta->fetch_object()) {
			if ($reg->saldod>0){
				$estado = '<span class="badge bg-red">Pendiente</span>';
				if ($reg->notacredito=='si'){
					$estado = '<span class="badge bg-red">Pendiente-NotaCrédito</span>';
				}
				
			}else{
				$estado = '<span class="badge bg-blue">Sin Deuda</span>';
			}
			$fecha		= $reg->fecha;
			$ranking 	= $reg->rank11;
			if($reg->fecha=='01-01-2121'){
				$fecha='-';
				$ranking 	= '';
			}
			$fechan=$reg->fechanumero;
			if($reg->fechanumero=='80721'){
				$fechan='-';
			}

			$data[]=array(
			"0"=>'<button class="btn btn-warning btn-xs" onclick="mostrar('.$reg->idcliente.')"><i class="fa fa-eye"></i></button>',
			"1"=>$reg->nombre,
			"2"=>	$fecha,
			"3"=>	$ranking,
			"4"=>number_format("$reg->deudad",2,",","."),
			"5"=>number_format("$reg->pagadod",2,",","."),
			"6"=>number_format("$reg->saldod",2,",","."),
			"7"=>$estado
            );
		}
		$results=array(
						"sEcho"=>1,//info para datatables
						"iTotalRecords"=>count($data),//enviamos el total de registros al datatable
						"iTotalDisplayRecords"=>count($data),//enviamos el total de registros a visualizar
						"aaData"=>$data); 
		echo json_encode($results);
	break;

	case 'listardeuda':
		$rspta=$cliente->listardeuda();
		$data=Array();

		while ($reg=$rspta->fetch_object()) {
		    //No se toman en cuenta los cliente con saldo negativo
		    if ($reg->saldo>0){
    			if ((number_format("$reg->saldo",2,",","."))>0){
    				if ($reg->notacredito=='si'){
    					$deuda	= '<span class="badge bg-red">Pendiente-NotaCrédito</span>';
    				}else{
    					$deuda	='<span class="badge bg-red">Pendiente</span>';
    				}
    			
    			}else{
    				$deuda='<span class="badge bg-orange">Credito</span>';
    			}
    			$data[]=array(
    			"0"=>'<div align="center"><button class="btn btn-warning btn-xs" onclick="mostrar('.$reg->idcliente.')"><i class="fa fa-eye"></i></button></div>',
    			"1"=>'<div align="left">'.$reg->nombre.'</div>',
    			"2"=>'<div align="right">'.number_format("$reg->saldod",2,",",".").'</div>',
    			"3"=>'<div align="center">'.number_format("$reg->nrodocumento",0,",",".").'</div>',
    			"4"=>'<div align="center">'.$reg->rank11.'</div>',
    			"5"=>'<div align="center">'.$reg->fecha.'</div>',
    			"6"=>'<div align="center">'.$reg->num_comprobante1.'</div>',
    			"7"=>'<div align="center">'.$deuda.'</div>',
    			"8"=>$reg->saldod,
    			"9"=>$reg->fechanumero
    			);
		    }
		}
		$results=array(
				"sEcho"=>1,//info para datatables
				"iTotalRecords"=>count($data),//enviamos el total de registros al datatable
				"iTotalDisplayRecords"=>count($data),//enviamos el total de registros a visualizar
				"aaData"=>$data); 
		echo json_encode($results);
	break;

    /*
	case 'listarDetalle':
		//recibimos el idventa
		$id				=$_GET['id'];

		$rspta		=$cliente->listarDetalle($id);
		$saldo		=0;
		$saldod		=0;
		$deudad		=0;
		$cobradod	=0;
		echo '<thead style="background-color:#A9D0F5">
			<th style="text-align:center" valign="middle">Nota de Entrega</th>
			<th style="text-align:center" valign="middle">Nro. Factura</th>
			<th style="text-align:center" valign="middle">Fecha Emisión</th>
			<th style="text-align:center" valign="middle">Fecha Despacho</th>
			<th style="text-align:center" valign="middle">Fecha Vencimiento</th>
			<th style="text-align:center" valign="middle">Deuda</th>
			<th style="text-align:center" valign="middle">Cobrado</th>
			<th style="text-align:center" valign="middle">Por Cobrar</th>
			<th style="text-align:center" valign="middle">Días Crédito</th>
			<th style="text-align:center" valign="middle">Días Cobro</th>
			<th style="text-align:center" valign="middle">Comisión</th>
			<th style="text-align:center" valign="middle">Estado</th>
		</thead>';
		while ($reg=$rspta->fetch_object()) {
		    
		    $dias       =$reg->nrodias-$reg->diascalle;
            $primera    =$reg->nrodias*0.5;
            $segunda    =$reg->nrodias*0.25;
            
            if($dias>$primera){
                if($reg->estado=='Pagada'){
                         $diacalle   ='<span class="badge bg-green">'.number_format("$reg->diascalle",0,",",".").'</span>'; 
                }else{
                        $diacalle   ='<span class="badge bg-green">'.number_format("$reg->diascalle",0,",",".").'</span>';
                }
            }else{
                if($dias<=$primera&&$dias>$segunda){
                    if($reg->estado=='Pagada'){
                         $diacalle   ='<span class="badge bg-green">'.number_format("$reg->diascalle",0,",",".").'</span>'; 
                    }else{
                         $diacalle   ='<span class="badge bg-yellow">'.number_format("$reg->diascalle",0,",",".").'</span>';
                    }
                }else{
                   if($dias<=$segunda&&$dias>0){
                       if($reg->estado=='Pagada'){
                            $diacalle   ='<span class="badge bg-green">'.number_format("$reg->diascalle",0,",",".").'</span>'; 
                        }else{
                            $diacalle   ='<span class="badge bg-red">'.number_format("$reg->diascalle",0,",",".").'</span>';
                        }
                   }else{
                      if($reg->estado=='Pagada'){
                          if($reg->diascalle<=365){
                             $diacalle   ='<span class="badge bg-green">'.number_format("$reg->diascalle",0,",",".").'</span>'; 
                          }else{
                             $diacalle   ='<span class="badge bg-green">'.number_format(0,0,",",".").'</span>'; 
                          }
                          
                      }else{
                            if($reg->estado=='Pendiente'||$reg->estado=='Parcial'){
                                $diac = $reg->diascalle*-1;
                                $diacalle   ='<span class="badge bg-red">'.number_format("$diac",0,",",".").'</span>'; 
                            }else{
                                $diacalle   ='<span class="badge bg-default">'.number_format("$diac",0,",",".").'</span>';
                            }  
                      }  
                   } 
                }
            }
            
            if($reg->estado=='Anulado'||$reg->estado=='Regalo'){
                $diacalle   ='<span class="badge bg-default">'.number_format(0,0,",",".").'</span>'; 
            }
		    
		    
		    
			if ($reg->estado!='Anulado'){
				$saldo		=	$saldo + $reg->porcobrar;
				$saldod		=	$saldod + $reg->porcobrard;
				$deudad		=	$deudad + $reg->deudad;
				$cobradod	=	$cobradod + $reg->cobradod;
			}
			$nrodocumento = $reg->nrodocumento;
			if ($reg->nrodocumento==0){
				$nrodocumento ='Sin Factura';
				if ($reg->documento=='Notadebito'){
					$nrodocumento ='Nota Débito';
				};
			}
			//	$montod	=	number_format("$montod",2,",",".");
			// \''.$reg->nombre.'\'
			$estado = $reg->estado;
			$mostraro='<button class="btn btn-success btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\')"><i class="fa fa-eye"></i></button>';
			$mostrara='<button class="btn btn-primary btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\')"><i class="fa fa-eye"></i></button>';
			$mostrarv='<button class="btn btn-success btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\')"><i class="fa fa-eye"></i></button>';
			if ($reg->agenteretencion=='si'){
				$estado = $estado.'-Retencion';
			}	

			if ($reg->comision=='si'){
			    if ($reg->fechacomision!='N/A'){
			       $comision='<span class="badge bg-green">Si-('.$reg->fechacomision.')</span></td>'; 
			    }else{
			       $comision='<span class="badge bg-green">Si-N/A</span></td>'; 
			    }
				
			}else{
				$comision='<span class="badge bg-red">No</span></td>';
			}
			
			if ($reg->documento=='Regalo'||$reg->deudad==0){
				if ($reg->notacredito=='si'){
				    $comision='<span class="badge bg-blue">No</span></td>';
				}else{
				    $comision='<span class="badge bg-blue">Regalo</span></td>';
				}
				$diacalle   ='<span class="badge bg-default">'.number_format(0,0,",",".").'</span>';
			};
            
            $linea = '<a target="_blank" href="reportes/NotadeEntregaStandar.php?id='.$reg->idventa.'"><button class="btn btn-primary btn-xs"><i class="fa fa-file"></i></button></a>';    
			if($reg->documento=='Factura'){
			   $masfactura='<a target="_blank" href="reportes/FacturaStandar.php?id='.$reg->idventa.'"><button class="btn btn-info btn-xs"><i class="fa fa-file"></i></button></a>';  
			   $linea =$linea.' '.$masfactura;
			}
			
			$linea2='';
            if ($reg->notacredito=='si'){
                //Buscamos los id de la notas de credito
                $cadena = $reg->idnotasdecreditos;
                $partes = explode("-", $cadena);
                // Recorrer el array resultante
                $url='reportes/NotacreditoNewT.php?id=';
                $linea22 = "";
                foreach ($partes as $valor) {
                    $linea22 = '<a target="_blank" href="'.$url.$valor.'"> <button class="btn btn-warning btn-xs"><i class="fa fa-file"></i></button></a>';
                    $linea2 = $linea2.' '.$linea22;
                }  
                
            }
            

			if ($reg->estado=='Pagada'){
				echo '<tr class="filas">
				<td style="text-align:center"><a href="#" onclick="mostrar1('.$reg->idventa.')" >'.$reg->nrodocumento1.'</a>'.' '.$linea.' '.$linea2.'</td>
				<td style="text-align:center">'.$nrodocumento.'</td>
				<td style="text-align:center">'.$reg->fecha.'</td>
				<td style="text-align:center">'.$reg->fechadespacho.'</td>
				<td style="text-align:center">'.$reg->fecha_vencimiento.'</td>
				<td style="text-align:right">'.number_format("$reg->deudad",2,",",".").'</td>
				<td style="text-align:right">'.number_format("$reg->cobradod",2,",",".").'</td>
				<td style="text-align:right">'.number_format("$reg->porcobrard",2,",",".").'</td>
				<td style="text-align:center">'.number_format("$reg->nrodias",0,",",".").'</td>
				<td style="text-align:center">'.$diacalle.'</td>
				<td style="text-align:center">'.$comision.'</td>';
				if($reg->documento=='Regalo'||$reg->deudad==0){
				    if ($reg->notacredito=='si'){
				        echo '<td style="text-align:center"><span class="badge bg-green">Pagada-NotaCredito</span></td>';
				    }else{
				        echo '<td style="text-align:center"><span class="badge bg-blue">Regalo</span></td>';
				    }
				}else{
				    if ($reg->notacredito=='si'){
					$estado = $estado.'-NotaCredito';
					    echo '<td style="text-align:center">'.$mostraro.' '.'<span class="badge bg-green">'.$estado.'</span></td>';
    				}else{
    					echo '<td style="text-align:center">'.$mostrarv.' '.'<span class="badge bg-green">'.$estado.'</span></td>';
    				}
				}
				
					
				
			}elseif($reg->estado=='Parcial'){
				echo '<tr class="filas">
				
				<td style="text-align:center">	<a href="#" onclick="mostrar1('.$reg->idventa.')" >'.$reg->nrodocumento1.'</a>'.' '.$linea.' '.$linea2.'</td>
				<td style="text-align:center">'.$nrodocumento.'</td>
				<td style="text-align:center">'.$reg->fecha.'</td>
				<td style="text-align:center">'.$reg->fechadespacho.'</td>
				<td style="text-align:center">'.$reg->fecha_vencimiento.'</td>
				<td style="text-align:right">'.number_format("$reg->deudad",2,",",".").'</td>
				<td style="text-align:right">'.number_format("$reg->cobradod",2,",",".").'</td>
				<td style="text-align:right">'.number_format("$reg->porcobrard",2,",",".").'</td>
				<td style="text-align:center">'.number_format("$reg->nrodias",0,",",".").'</td>
				<td style="text-align:center">'.$diacalle.'</td>
				<td style="text-align:center">'.$comision.'</td>';
				$mostraro='<button class="btn btn-warning btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\')"><i class="fa fa-eye"></i></button>';
				$mostrara='<button class="btn btn-warning btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\')"><i class="fa fa-eye"></i></button>';
				if ($reg->notacredito=='si'){
					$estado = $estado.'-NotaCredito';
					echo '<td style="text-align:center">'.$mostraro.' '.'<span class="badge bg-yellow">'.$estado.'</span></td>';
				}else{
					echo '<td style="text-align:center">'.$mostrara.' '.'<span class="badge bg-yellow">'.$estado.'</span></td>';
				}
				
			}elseif($reg->estado=='Anulado'||$reg->documento=='Regalo'||$reg->deudad==0){
				if($reg->documento=='Regalo'||$reg->deudad==0){
				    $estado22='<td style="text-align:center"><span class="badge bg-blue">Regalo</span></td>';
				}else{
				    $estado22='<td style="text-align:center"><span class="badge bg-navy">'.$reg->estado.'</span></td>';
				}
				$comision='<span class="badge bg-navy">Anulado</span></td>';
				echo '<tr class="filas">
				<td style="text-align:center">	<a href="#" onclick="mostrar1('.$reg->idventa.')" >'.$reg->nrodocumento1.'</a>'.' '.$linea.' '.$linea2.'</td>
				<td style="text-align:center">'.$nrodocumento.'</td>
				<td style="text-align:center">'.$reg->fecha.'</td>
				<td style="text-align:center">'.$reg->fechadespacho.'</td>
				<td style="text-align:center">'.$reg->fecha_vencimiento.'</td>
				<td style="text-align:right">'.number_format("$reg->deudad",2,",",".").'</td>
				<td style="text-align:right">'.number_format("0.0",2,",",".").'</td>
				<td style="text-align:right">'.number_format("0.0",2,",",".").'</td>
				<td style="text-align:center">'.number_format("$reg->nrodias",0,",",".").'</td>
				<td style="text-align:center">'.$diacalle.'</td>
				<td style="text-align:center">'.$comision.'</td>'.
				$estado22;
				
			}else{
				echo '<tr class="filas">
				<td style="text-align:center">	<a href="#" onclick="mostrar1('.$reg->idventa.')" >'.$reg->nrodocumento1.'</a>'.' '.$linea.' '.$linea2.'</td>
				<td style="text-align:center">'.$nrodocumento.'</td>
				<td style="text-align:center">'.$reg->fecha.'</td>
				<td style="text-align:center">'.$reg->fechadespacho.'</td>
				<td style="text-align:center">'.$reg->fecha_vencimiento.'</td>
				<td style="text-align:right">'.number_format("$reg->deudad",2,",",".").'</td>
				<td style="text-align:right">'.number_format("$reg->cobradod",2,",",".").'</td>
				<td style="text-align:right">'.number_format("$reg->porcobrard",2,",",".").'</td>
				<td style="text-align:center">'.number_format("$reg->nrodias",0,",",".").'</td>
				<td style="text-align:center">'.$diacalle.'</td>
				<td style="text-align:center">'.$comision.'</td>';
				if ($reg->notacredito=='si'){
					$estado = $estado.'-NotaCredito';
					echo '<td style="text-align:center"><span class="badge bg-red">'.$estado.'</span></td>';
				}else{
					echo '<td style="text-align:center"><span class="badge bg-red">'.$estado.'</span></td>';
				}

			
			}
			echo '</tr>';
		}
		$saldo		=	number_format("$saldo",2,",",".");
		$saldod		=	number_format("$saldod",2,",",".");
		$deudad		=	number_format("$deudad",2,",",".");
		$cobradod	=	number_format("$cobradod",2,",",".");
		echo '<tfoot>
		<th><h4>Saldo</h4></th>
		<th></th>
		<th></th>
		<th></th>
		<th></th>
		<th style="text-align:right"><h4 id="deuda"> '.$deudad.'</h4></th>
		<th style="text-align:right"><h4 id="cobrado"> '.$cobradod.'</h4></th>
		<th style="text-align:right"><h4 id="total"> '.$saldod.'</h4> </th>
		</tfoot>';
	break;
    */
    
    case 'listarDetalle':
		//recibimos el idventa
		$id				=$_GET['id'];

		$rspta		=$cliente->listarDetalle($id);
		$saldo		=0;
		$saldod		=0;
		$deudad		=0;
		$cobradod	=0;
		echo '<thead style="background-color:#A9D0F5">
			<th style="text-align:center" valign="middle">Nota de Entrega</th>
			<th style="text-align:center" valign="middle">Nro. Factura</th>
			<th style="text-align:center" valign="middle">Fecha Emisión</th>
			<th style="text-align:center" valign="middle">Fecha Despacho</th>
			<th style="text-align:center" valign="middle">Fecha Vencimiento</th>
			<th style="text-align:center" valign="middle">Fecha Pago</th>
			<th style="text-align:center" valign="middle">Deuda</th>
			<th style="text-align:center" valign="middle">Cobrado</th>
			<th style="text-align:center" valign="middle">Por Cobrar</th>
			<th style="text-align:center" valign="middle">Días Crédito</th>
			<th style="text-align:center" valign="middle">Días Calle</th>
			<th style="text-align:center" valign="middle">Comisión</th>
			<th style="text-align:center" valign="middle">Estado</th>
		</thead>';
		while ($reg=$rspta->fetch_object()) {
		    /*
		    $dias       =$reg->nrodias-$reg->diascalle;
            $primera    =$reg->nrodias*0.5;
            $segunda    =$reg->nrodias*0.25;
            
            if($dias>$primera){
                if($reg->estado=='Pagada'){
                         $diacalle   ='<span class="badge bg-green">'.number_format("$reg->diascalle",0,",",".").'</span>'; 
                }else{
                        $diacalle   ='<span class="badge bg-green">'.number_format("$reg->diascalle",0,",",".").'</span>';
                }
            }else{
                if($dias<=$primera&&$dias>$segunda){
                    if($reg->estado=='Pagada'){
                         $diacalle   ='<span class="badge bg-green">'.number_format("$reg->diascalle",0,",",".").'</span>'; 
                    }else{
                         $diacalle   ='<span class="badge bg-yellow">'.number_format("$reg->diascalle",0,",",".").'</span>';
                    }
                }else{
                   if($dias<=$segunda&&$dias>0){
                       if($reg->estado=='Pagada'){
                            $diacalle   ='<span class="badge bg-green">'.number_format("$reg->diascalle",0,",",".").'</span>'; 
                        }else{
                            $diacalle   ='<span class="badge bg-red">'.number_format("$reg->diascalle",0,",",".").'</span>';
                        }
                   }else{
                      if($reg->estado=='Pagada'){
                          if($reg->diascalle<=365){
                             $diacalle   ='<span class="badge bg-green">'.number_format("$reg->diascalle",0,",",".").'</span>'; 
                          }else{
                             $diacalle   ='<span class="badge bg-green">'.number_format(0,0,",",".").'</span>'; 
                          }
                          
                      }else{
                            if($reg->estado=='Pendiente'||$reg->estado=='Parcial'){
                                $diac = $reg->diascalle*-1;
                                $diacalle   ='<span class="badge bg-red">'.number_format("$diac",0,",",".").'</span>'; 
                            }else{
                                $diacalle   ='<span class="badge bg-default">'.number_format("$diac",0,",",".").'</span>';
                            }  
                      }  
                   } 
                }
            }
            */
            
            
            if($reg->diascalle2<0){
                $diacalle   ='<span class="badge bg-green">'.number_format("$reg->diascalle2",0,",",".").'</span>';
            }else{
                $diacalle   ='<span class="badge bg-red">'.number_format("$reg->diascalle2",0,",",".").'</span>';
            }
            
            
            if($reg->estado=='Anulado'||$reg->estado=='Regalo'){
                $diacalle   ='<span class="badge bg-default">'.number_format(0,0,",",".").'</span>'; 
            }
		    
		    
		    
			if ($reg->estado!='Anulado'){
				$saldo		=	$saldo + $reg->porcobrar;
				$saldod		=	$saldod + $reg->porcobrard;
				$deudad		=	$deudad + $reg->deudad;
				$cobradod	=	$cobradod + $reg->cobradod;
			}
			$nrodocumento = $reg->nrodocumento;
			if ($reg->nrodocumento==0){
				$nrodocumento ='Sin Factura';
				if ($reg->documento=='Notadebito'){
					$nrodocumento ='Nota Débito';
				};
			}
			//	$montod	=	number_format("$montod",2,",",".");
			$estado = $reg->estado;
			$mostraro='<button class="btn btn-success btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\',\''.$reg->documento.'\')"><i class="fa fa-eye"></i></button>';
			$mostrarof='<button class="btn btn-warning btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\',\''.$reg->documento.'\')"><i class="fa fa-eye"></i></button>';
			$mostrara='<button class="btn btn-primary btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\',\''.$reg->documento.'\')"><i class="fa fa-eye"></i></button>';
			$mostrarv='<button class="btn btn-success btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\',\''.$reg->documento.'\')"><i class="fa fa-eye"></i></button>';
			$mostrarvf='<button class="btn btn-warning btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\',\''.$reg->documento.'\')"><i class="fa fa-eye"></i></button>';
			if ($reg->agenteretencion=='si'){
				$estado = $estado.'-Retencion';
			}	

			if ($reg->comision=='si'){
			    if ($reg->fechacomision!='N/A'){
			       $comision='<span class="badge bg-green">Si-('.$reg->fechacomision.')</span></td>'; 
			    }else{
			       $comision='<span class="badge bg-green">Si-N/A</span></td>'; 
			    }
				
			}else{
				$comision='<span class="badge bg-red">No</span></td>';
			}
			
			if ($reg->documento=='Regalo'||$reg->deudad==0){
				if ($reg->notacredito=='si'){
				    $comision='<span class="badge bg-blue">No</span></td>';
				}else{
				    $comision='<span class="badge bg-blue">Regalo</span></td>';
				}
				$diacalle   ='<span class="badge bg-default">'.number_format(0,0,",",".").'</span>';
			};
            
            
            $linea = '<a target="_blank" href="reportes/NotadeEntregaStandar.php?id='.$reg->idventa.'"><button class="btn btn-primary btn-xs"><i class="fa fa-file"></i></button></a>';    
			if ($reg->documento=='Notadebito'){
				$linea = '<a target="_blank" href="reportes/notadebitoNewT.php?id='.$reg->idventa.'"><button class="btn btn-success btn-xs"><i class="fa fa-file"></i></button></a>';
			};
			if($reg->documento=='Factura'){
			   $masfactura='<a target="_blank" href="reportes/FacturaStandar.php?id='.$reg->idventa.'"><button class="btn btn-info btn-xs"><i class="fa fa-file"></i></button></a>';  
			   $linea =$linea.' '.$masfactura;
			}
			
			$linea2='';
            if ($reg->notacredito=='si'){
                //Buscamos los id de la notas de credito
                $cadena = $reg->idnotasdecreditos;
                $partes = explode("-", $cadena);
                // Recorrer el array resultante
                $url='reportes/NotacreditoNewT.php?id=';
                $linea22 = "";
                foreach ($partes as $valor) {
                    $linea22 = '<a target="_blank" href="'.$url.$valor.'"> <button class="btn btn-warning btn-xs"><i class="fa fa-file"></i></button></a>';
                    $linea2 = $linea2.' '.$linea22;
                }  
                
            }
            
            //<td style="text-align:center">'.$diacalle.'</td>
			if ($reg->estado=='Pagada'){
				echo '<tr class="filas">
				<td style="text-align:center"><a href="#" onclick="mostrar1('.$reg->idventa.')" >'.$reg->nrodocumento1.'</a>'.' '.$linea.' '.$linea2.'</td>
				<td style="text-align:center">'.$nrodocumento.'</td>
				<td style="text-align:center">'.$reg->fecha.'</td>
				<td style="text-align:center">'.$reg->fechadespacho.'</td>
				<td style="text-align:center">'.$reg->fecha_vencimiento.'</td>
				<td style="text-align:center">'.$reg->fechapago.'</td>
				<td style="text-align:right">'.number_format("$reg->deudad",2,",",".").'</td>
				<td style="text-align:right">'.number_format("$reg->cobradod",2,",",".").'</td>
				<td style="text-align:right">'.number_format("$reg->porcobrard",2,",",".").'</td>
				<td style="text-align:center">'.number_format("$reg->nrodias",0,",",".").'</td>
				<td style="text-align:center">'.$diacalle.'</td>
				<td style="text-align:center">'.$comision.'</td>';
				if($reg->documento=='Regalo'||$reg->deudad==0){
				    if ($reg->notacredito=='si'){
				        echo '<td style="text-align:center"><span class="badge bg-green">Pagada-NotaCredito</span></td>';
				    }else{
				        echo '<td style="text-align:center"><span class="badge bg-blue">Regalo</span></td>';
				    }
				}else{
				    if ($reg->notacredito=='si'){
					$estado = $estado.'-NotaCredito';
					    echo '<td style="text-align:center">'.$mostraro.' '.'<span class="badge bg-green">'.$estado.'</span></td>';
    				}else{
    					echo '<td style="text-align:center">'.$mostrarv.' '.'<span class="badge bg-green">'.$estado.'</span></td>';
    				}
				}
			
			}elseif ($reg->estado=='Fiscalia'){
				echo '<tr class="filas">
				<td style="text-align:center"><a href="#" onclick="mostrar1('.$reg->idventa.')" >'.$reg->nrodocumento1.'</a>'.' '.$linea.' '.$linea2.'</td>
				<td style="text-align:center">'.$nrodocumento.'</td>
				<td style="text-align:center">'.$reg->fecha.'</td>
				<td style="text-align:center">'.$reg->fechadespacho.'</td>
				<td style="text-align:center">'.$reg->fecha_vencimiento.'</td>
				<td style="text-align:center">'.$reg->fechapago.'</td>
				<td style="text-align:right">'.number_format("$reg->deudad",2,",",".").'</td>
				<td style="text-align:right">'.number_format("$reg->cobradod",2,",",".").'</td>
				<td style="text-align:right">'.number_format("$reg->porcobrard",2,",",".").'</td>
				<td style="text-align:center">'.number_format("$reg->nrodias",0,",",".").'</td>
				<td style="text-align:center">'.$diacalle.'</td>
				<td style="text-align:center">'.$comision.'</td>';
				if($reg->documento=='Regalo'||$reg->deudad==0){
				    if ($reg->notacredito=='si'){
				        echo '<td style="text-align:center"><span class="badge bg-green">Pagada-NotaCredito</span></td>';
				    }else{
				        echo '<td style="text-align:center"><span class="badge bg-blue">Regalo</span></td>';
				    }
				}else{
				    if ($reg->notacredito=='si'){
					$estado = $estado.'-NotaCredito';
					    echo '<td style="text-align:center">'.$mostrarof.' '.'<span class="badge bg-orange">'.$estado.'</span></td>';
    				}else{
    					echo '<td style="text-align:center">'.$mostrarvf.' '.'<span class="badge bg-orange">'.$estado.'</span></td>';
    				}
				}
				
					
				
			}elseif($reg->estado=='Parcial'){
				echo '<tr class="filas">
				
				<td style="text-align:center">	<a href="#" onclick="mostrar1('.$reg->idventa.')" >'.$reg->nrodocumento1.'</a>'.' '.$linea.' '.$linea2.'</td>
				<td style="text-align:center">'.$nrodocumento.'</td>
				<td style="text-align:center">'.$reg->fecha.'</td>
				<td style="text-align:center">'.$reg->fechadespacho.'</td>
				<td style="text-align:center">'.$reg->fecha_vencimiento.'</td>
				<td style="text-align:center">'.$reg->fechapago.'</td>
				<td style="text-align:right">'.number_format("$reg->deudad",2,",",".").'</td>
				<td style="text-align:right">'.number_format("$reg->cobradod",2,",",".").'</td>
				<td style="text-align:right">'.number_format("$reg->porcobrard",2,",",".").'</td>
				<td style="text-align:center">'.number_format("$reg->nrodias",0,",",".").'</td>
				<td style="text-align:center">'.$diacalle.'</td>
				<td style="text-align:center">'.$comision.'</td>';
				$mostraro='<button class="btn btn-warning btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\',\''.$reg->documento.'\')"><i class="fa fa-eye"></i></button>';
				$mostrara='<button class="btn btn-warning btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\',\''.$reg->documento.'\')"><i class="fa fa-eye"></i></button>';
				if ($reg->notacredito=='si'){
					$estado = $estado.'-NotaCredito';
					echo '<td style="text-align:center">'.$mostraro.' '.'<span class="badge bg-yellow">'.$estado.'</span></td>';
				}else{
					echo '<td style="text-align:center">'.$mostrara.' '.'<span class="badge bg-yellow">'.$estado.'</span></td>';
				}
				
			}elseif($reg->estado=='Anulado'||$reg->documento=='Regalo'||$reg->deudad==0){
				if($reg->documento=='Regalo'||$reg->deudad==0){
				    $estado22='<td style="text-align:center"><span class="badge bg-blue">Regalo</span></td>';
				}else{
				    $estado22='<td style="text-align:center"><span class="badge bg-navy">'.$reg->estado.'</span></td>';
				}
				$comision='<span class="badge bg-navy">Anulado</span></td>';
				echo '<tr class="filas">
				<td style="text-align:center">	<a href="#" onclick="mostrar1('.$reg->idventa.')" >'.$reg->nrodocumento1.'</a>'.' '.$linea.' '.$linea2.'</td>
				<td style="text-align:center">'.$nrodocumento.'</td>
				<td style="text-align:center">'.$reg->fecha.'</td>
				<td style="text-align:center">'.$reg->fechadespacho.'</td>
				<td style="text-align:center">'.$reg->fecha_vencimiento.'</td>
				<td style="text-align:center">'.$reg->fechapago.'</td>
				<td style="text-align:right">'.number_format("$reg->deudad",2,",",".").'</td>
				<td style="text-align:right">'.number_format("0.0",2,",",".").'</td>
				<td style="text-align:right">'.number_format("0.0",2,",",".").'</td>
				<td style="text-align:center">'.number_format("$reg->nrodias",0,",",".").'</td>
				<td style="text-align:center">'.$diacalle.'</td>
				<td style="text-align:center">'.$comision.'</td>'.
				$estado22;
				
			}else{
				echo '<tr class="filas">
				<td style="text-align:center">	<a href="#" onclick="mostrar1('.$reg->idventa.')" >'.$reg->nrodocumento1.'</a>'.' '.$linea.' '.$linea2.'</td>
				<td style="text-align:center">'.$nrodocumento.'</td>
				<td style="text-align:center">'.$reg->fecha.'</td>
				<td style="text-align:center">'.$reg->fechadespacho.'</td>
				<td style="text-align:center">'.$reg->fecha_vencimiento.'</td>
				<td style="text-align:center">'.$reg->fechapago.'</td>
				<td style="text-align:right">'.number_format("$reg->deudad",2,",",".").'</td>
				<td style="text-align:right">'.number_format("$reg->cobradod",2,",",".").'</td>
				<td style="text-align:right">'.number_format("$reg->porcobrard",2,",",".").'</td>
				<td style="text-align:center">'.number_format("$reg->nrodias",0,",",".").'</td>
				<td style="text-align:center">'.$diacalle.'</td>
				<td style="text-align:center">'.$comision.'</td>';
				if($reg->fechadespacho1=='si'){
				    if ($reg->notacredito=='si'){
    					$estado = $estado.'-NotaCredito';
    					echo '<td style="text-align:center"><span class="badge bg-red">'.$estado.'</span></td>';
    				}else{
    					echo '<td style="text-align:center"><span class="badge bg-red">'.$estado.'</span></td>';
    				}  
				}else{
				    $estado = 'Por Despachar';
    				if ($reg->notacredito=='si'){
    					$estado = $estado.'-NotaCredito';
    					echo '<td style="text-align:center"><span class="badge bg-maroon">'.$estado.'</span></td>';
    				}else{
    					echo '<td style="text-align:center"><span class="badge bg-maroon">'.$estado.'</span></td>';
    				}  
				}
				

			
			}
			echo '</tr>';
		}
		$saldo		=	number_format("$saldo",2,",",".");
		$saldod		=	number_format("$saldod",2,",",".");
		$deudad		=	number_format("$deudad",2,",",".");
		$cobradod	=	number_format("$cobradod",2,",",".");
		echo '<tfoot>
		<th><h4>Saldo</h4></th>
		<th></th>
		<th></th>
		<th></th>
		<th></th>
		<th></th>
		<th style="text-align:right"><h4 id="deuda"> '.$deudad.'</h4></th>
		<th style="text-align:right"><h4 id="cobrado"> '.$cobradod.'</h4></th>
		<th style="text-align:right"><h4 id="total"> '.$saldod.'</h4> </th>
		</tfoot>';
	break;
    
	case 'listarDetallecobro':
		//recibimos el idventa
		$id=$_GET['id'];

		$rspta=$cliente->listarDetallecobro($id);
		$total=0;

		/* echo '<thead style="background-color:#A9D0F5">
				<th style="text-align:right;width:60px">Opción</th>
				<th style="text-align:right;width:120px">Orden</th>
				<th style="text-align:right;width:120px">Factura</th>
				<th style="text-align:right;width:120px">Total Documento</th>
				<th style="text-align:right;width:120px">Saldo</th>
				<th style="text-align:right;width:120px">Abono</th>
				<th style="text-align:right;width:120px">Pendiente</th>
				<th style="text-align:right;width:120px">Estado</th>
			</thead>
			<tbody>'; */
			echo '<thead style="background-color:#A9D0F5">
			<th style="text-align:center;width:60px">Seleccionar</th>
			<th style="text-align:center;width:120px">Nro Orden</th>
			<th style="text-align:center;width:120px">Nro Factura</th>
			<th style="text-align:center;width:180px"">Total Documento</th>
			<th style="text-align:center;width:180px"">Saldo</th>
			<th style="text-align:center;width:180px">Abono</th>
			<th style="text-align:center;width:180px">Pendiente</th>
			<th style="text-align:center">Estado</th>
		</thead>
		<tbody>';
		$cont		=0; 
		$this1	='this';
		$deuda	=0;
		$totald	=0;
		while ($reg=$rspta->fetch_object()) {
		    //Solo vemos los documento que tenga deuda
		    if($reg->estado=='Parcial'||($reg->estado=='Pendiente'&&$reg->total_ventaimpuesto>0)){
    			$montod		=	$reg->total_ventaimpuesto;
    			$montodd	=	$reg->total_ventaimpuestod;
    			$montodt	=	$reg->total_ventaimpuesto;
    			$montoddt	=	$reg->total_ventaimpuestod;
    			$montop		=	$reg->montopendiente;
    			$montopd	=	$reg->montopendiented;
    			if ($montop>0){
    				$montod		= 	$montop;
    				$montodd	= 	$montopd;
    			}
    			$num_comprobante = $reg->num_comprobante;
    			if ($reg->num_comprobante==0){
    				$num_comprobante = '';
    				if ($reg->tipo_comprobante=='Notadebito'){
    					$num_comprobante ='Nota Débito';
    				};
    			}
    			
    			$etiqueta 	= '';
    			$notacredito='';
    			$color='red';
    			if ($reg->notacredito=='si'){
    				$notacredito='-Notacredito';
    				//$color			='purple';
    			}
    			$retencion = '';
    			if ($reg->agenteretencion=='si'){
    				if($reg->tipo_comprobante=='Factura'){
    				   $retencion = '-Retencion'; 
    				}
    			}
    
    			
    			$mostrar='<button class="btn btn-default btn-xs"><i></i></button>';
    			if ($reg->estado=='Parcial'){
    				$color		='yellow';
    				$mostrar	='<button class="btn btn-warning btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\')"><i class="fa fa-eye"></i></button>';
    				$etiqueta ='<span class="badge bg-yellow">'.$reg->estado.$notacredito.$retencion.'</span>';
    			}else{
    				if($reg->tipo_comprobante=='Regalo'){
    			        $etiqueta ='<span class="badge bg-blue">Regalo</span>';
    			    }else{
    			        if($montodd==0){
    			           $etiqueta ='<span class="badge bg-blue">Regalo</span>';
    			        }else{
    			           $etiqueta ='<span class="badge bg-red">'.$reg->estado.$notacredito.$retencion.'</span>'; 
    			        }
    			        
    			    }
    			}
    			
    			$deuda 		= $deuda + $montodd;
    			$totald		= $totald + $montoddt;
    			$montod		=	number_format("$montod",2,",",".");
    			$montodd	=	number_format("$montodd",2,",",".");
    			$montodt	=	number_format("$montodt",2,",",".");
    			$montoddt	=	number_format("$montoddt",2,",",".");
    			
    			/* echo '<tr class="filas">
    			<td><input type="checkbox" name="iddocumento[]" id="iddocumento[]" onchange="change_checkbox('.$cont.','.$this1.')" value="'.$cont.'"></td>
    			<td><input type="text" style="text-align:right;width:120px;border: inset 0pt" name="num_comprobante[]" value="'.$reg->num_comprobante1.'"  readonly></td>
    			<td><input type="text" style="text-align:right;width:120px;border: inset 0pt" name="num_comprobantef[]" value="'.$num_comprobante.'"  readonly></td>
    			<td><input type="text" style="text-align:right;width:120px;border: inset 0pt" name="montoddt[]" value="'.$montoddt.'"  readonly></td>
    			<td><input type="text" style="text-align:right;width:120px;border: inset 0pt" name="montodd[]" id="montodd[]" step="0.01" value="'.$montodd.'"  readonly></td>
    					<input type="hidden" style="text-align:right" name="montod[]" id="montod[]" step="0.01" value="'.$montod.'" readonly>
    			<td style="text-align:right"><input type="text" style="text-align:right;width:120px" name="abono[]" id="abono[]" step="0.01" value="0,00" ></td>
    			<td><input type="text" style="text-align:right;width:120px;border: inset 0pt" name="pendiente[]" id="pendiente[]" step="0.01" value="0,00" readonly></td>
    			<td style="color:'.$color.';text-align:center;width:160px;border: inset 0pt">'.$mostrar.'  '.$reg->estado.$notacredito.'</td>
    			<td style="visibility:hidden">
    			<input type="hidden" name="idventa[]" id="idventa[]" value="'.$reg->idventa.'">
    			<input type="hidden" name="tasa_dollar[]" id="tasa_dollar[]" value="'.$reg->tasa_dollar.'">
    			<input type="hidden" name="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'">
    			<input type="hidden" name="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'" >
    			<input type="hidden" name="estado[]" value="'.$reg->estado.$notacredito.'">
    			</td></tr>'; 
    			
    			<td style="text-align:right;background-color:#bdf59a"><input type="text" style="text-align:right;width:180px;background-color:#bdf59a" name="abono[]" id="abono[]" step="0.01" value="0,00" ></td>
    			*/
    			if(($reg->tipo_comprobante!='Regalo')){
        			echo '<tr class="filas">
        				<td style="text-align:center;"><input type="checkbox"  name="iddocumento[]" id="iddocumento[]" onchange="change_checkbox('.$cont.','.$this1.')" value="'.$cont.'"></td>
        				<td><input type="text" style="text-align:center;width:120px;border: inset 0pt;background-color:#ffffff" name="num_comprobante[]" value="'.$reg->num_comprobante1.'"  readonly></td>
        				<td><input type="text" style="text-align:center;width:120px;border: inset 0pt;background-color:#ffffff" name="num_comprobantef[]" value="'.$num_comprobante.'"  readonly></td>
        				<td style="text-align:right"><input type="text" style="text-align:right;border: inset 0pt;width:180px;background-color:#ffffff" name="montoddt[]" value="'.$montoddt.'"  readonly></td>
        				<td style="text-align:right;color:black"><input type="text" style="text-align:right;border: inset 0pt;width:180px;background-color:#ffffff" name="montodd[]" id="montodd[]" step="0.01" value="'.$montodd.'"  readonly></td>
        						<input type="hidden" style="text-align:right" name="montod[]" id="montod[]" step="0.01" value="'.$montod.'" readonly>
        				<td style="text-align:right;background-color:#cffedf;color:blue""><input type="text" style="text-align:right;width:180px;background-color:#cffedf;border:none" name="abono[]" id="abono[]" step="0.01" value="0,00" ></td>
        				<td style="text-align:right"><input type="text" style="text-align:right;width:180px;border: inset 0pt;background-color:#ffffff" name="pendiente[]" id="pendiente[]" step="0.01" value="0,00" readonly></td>
        				<td style="color:'.$color.';text-align:center;border: inset 0pt">
        					<input type="hidden" style="border: inset 0pt;background-color:#ffffff" name="estado[]" value="'.$reg->estado.$notacredito.$retencion.'">'.$mostrar.' '.'<span id="estado'.$cont.'">'.$etiqueta.'</span>
        					<input type="hidden" name="estado1[]" id="estado1[]" value="'.$reg->estado.'">
        					<input type="hidden" name="idventa[]" id="idventa[]" value="'.$reg->idventa.'">
        					<input type="hidden" name="tasa_dollar[]" id="tasa_dollar[]" value="'.$reg->tasa_dollar.'">
        					<input type="hidden" name="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'">
        					
        					
        				</td>
        				
        			</tr>';
        		    $cont+=1;
    		    }else{
    		        if(($reg->tipo_comprobante=='Regalo')&&($montodd>0)){
    		           echo '<tr class="filas">
            				<td style="text-align:center;"><input type="checkbox"  name="iddocumento[]" id="iddocumento[]" onchange="change_checkbox('.$cont.','.$this1.')" value="'.$cont.'"></td>
            				<td><input type="text" style="text-align:center;width:120px;border: inset 0pt;background-color:#ffffff" name="num_comprobante[]" value="'.$reg->num_comprobante1.'"  readonly></td>
            				<td><input type="text" style="text-align:center;width:120px;border: inset 0pt;background-color:#ffffff" name="num_comprobantef[]" value="'.$num_comprobante.'"  readonly></td>
            				<td style="text-align:right"><input type="text" style="text-align:right;border: inset 0pt;width:180px;background-color:#ffffff" name="montoddt[]" value="'.$montoddt.'"  readonly></td>
            				<td style="text-align:right;color:black"><input type="text" style="text-align:right;border: inset 0pt;width:180px;background-color:#ffffff" name="montodd[]" id="montodd[]" step="0.01" value="'.$montodd.'"  readonly></td>
            						<input type="hidden" style="text-align:right" name="montod[]" id="montod[]" step="0.01" value="'.$montod.'" readonly>
            				<td style="text-align:right;background-color:#cffedf;color:blue""><input type="text" style="text-align:right;width:180px;background-color:#cffedf;border:none" name="abono[]" id="abono[]" step="0.01" value="0,00" ></td>
            				<td style="text-align:right"><input type="text" style="text-align:right;width:180px;border: inset 0pt;background-color:#ffffff" name="pendiente[]" id="pendiente[]" step="0.01" value="0,00" readonly></td>
            				<td style="color:'.$color.';text-align:center;border: inset 0pt">
            					<input type="hidden" style="border: inset 0pt;background-color:#ffffff" name="estado[]" value="'.$reg->estado.$notacredito.$retencion.'">'.$mostrar.' '.'<span id="estado'.$cont.'">'.$etiqueta.'</span>
            					<input type="hidden" name="estado1[]" id="estado1[]" value="'.$reg->estado.'">
            					<input type="hidden" name="idventa[]" id="idventa[]" value="'.$reg->idventa.'">
            					<input type="hidden" name="tasa_dollar[]" id="tasa_dollar[]" value="'.$reg->tasa_dollar.'">
            					<input type="hidden" name="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'">
            					
            					
            				</td>
        				
            			</tr>';
            		    $cont+=1; 
    		        }
    		    }
		    }
		}
		$deuda	=	number_format("$deuda",2,",",".");
		$totald =	number_format("$totald",2,",",".");
		// Solo Para JVG No coloco fila de diferencia y coloco el input en pagocliente-view
		/*  <tr>
			<th></th>
			<th></th>
			
			<th>Diferencia Bs.</th>
			<th><input type="text" style="text-align:right" name="diferencia" id="diferencia" value="0.00" step="any"></th>
			<th></th>
			<th></th>
			<th></th>
			</tr> */
		echo '</tbody><tfoot>
			<tr>
			<th></th>
			<th></th>
			<th></th>
			<th><input type="hidden" style="text-align:right;width:120px" name="total_documento" id="total_documento" value="'.$totald.'" step="0.01"></th>
			<th><input type="hidden" style="text-align:right;width:120px" name="total_saldo" id="total_saldo" value="'.$deuda.'" step="0.01"></th>
			<th></th>
			<th></th>
			<th></th>
			</tr>
			
		</tfoot>';
	break;

	case 'listarDetallecobrofueraruta':
		//recibimos el idventa
		$id=$_GET['id'];

		$rspta=$cliente->listarDetallecobro($id);
		$total=0;

		/* echo '<thead style="background-color:#A9D0F5">
				<th style="text-align:right;width:60px">Opción</th>
				<th style="text-align:right;width:120px">Orden</th>
				<th style="text-align:right;width:120px">Factura</th>
				<th style="text-align:right;width:120px">Total Documento</th>
				<th style="text-align:right;width:120px">Saldo</th>
				<th style="text-align:right;width:120px">Abono</th>
				<th style="text-align:right;width:120px">Pendiente</th>
				<th style="text-align:right;width:120px">Estado</th>
			</thead>
			<tbody>'; */
			echo '<thead style="background-color:#A9D0F5">
			<th style="text-align:right;width:45px">Opción</th>
			<th style="text-align:right;width:45px">Orden</th>
			<th style="text-align:right;width:45px">Factura</th>
			<th style="text-align:right;width:60px">Total</th>
			<th style="text-align:right;width:60px">Saldo</th>
			<th style="text-align:right;width:60px">Abono</th>
			<th style="text-align:right;width:60px">Pendiente</th>
			<th style="text-align:right;width:50px">Estado</th>
		</thead>
		<tbody>';
		$cont		=0; 
		$this1	='this';
		$deuda	=0;
		$totald	=0;
		while ($reg=$rspta->fetch_object()) {
			$montod		=	$reg->total_ventaimpuesto;
			$montodd	=	$reg->total_ventaimpuestod;
			$montodt	=	$reg->total_ventaimpuesto;
			$montoddt	=	$reg->total_ventaimpuestod;
			$montop		=	$reg->montopendiente;
			$montopd	=	$reg->montopendiented;
			if ($montop>0){
				$montod		= 	$montop;
				$montodd	= 	$montopd;
			}
			$num_comprobante = $reg->num_comprobante;
			if ($reg->num_comprobante==0){
				$num_comprobante = 'Sin Factura';
			}
			$notacredito='';
			$color='red';
			if ($reg->notacredito=='si'){
				$notacredito='-Notacredito';
				$color			='purple';
			}
			$mostrar='';
			if ($reg->estado=='Parcial'){
				$color	='orange';
				$mostrar='<button class="btn btn-warning btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\')"><i class="fa fa-eye"></i></button>';
			}
			$deuda 		= $deuda + $montodd;
			$totald		= $totald + $montoddt;
			$montod		=	number_format("$montod",2,",",".");
			$montodd	=	number_format("$montodd",2,",",".");
			$montodt	=	number_format("$montodt",2,",",".");
			$montoddt	=	number_format("$montoddt",2,",",".");
			
			/* echo '<tr class="filas">
			<td><input type="checkbox" name="iddocumento[]" id="iddocumento[]" onchange="change_checkbox('.$cont.','.$this1.')" value="'.$cont.'"></td>
			<td><input type="text" style="text-align:right;width:120px;border: inset 0pt" name="num_comprobante[]" value="'.$reg->num_comprobante1.'"  readonly></td>
			<td><input type="text" style="text-align:right;width:120px;border: inset 0pt" name="num_comprobantef[]" value="'.$num_comprobante.'"  readonly></td>
			<td><input type="text" style="text-align:right;width:120px;border: inset 0pt" name="montoddt[]" value="'.$montoddt.'"  readonly></td>
			<td><input type="text" style="text-align:right;width:120px;border: inset 0pt" name="montodd[]" id="montodd[]" step="0.01" value="'.$montodd.'"  readonly></td>
					<input type="hidden" style="text-align:right" name="montod[]" id="montod[]" step="0.01" value="'.$montod.'" readonly>
			<td style="text-align:right"><input type="text" style="text-align:right;width:120px" name="abono[]" id="abono[]" step="0.01" value="0,00" ></td>
			<td><input type="text" style="text-align:right;width:120px;border: inset 0pt" name="pendiente[]" id="pendiente[]" step="0.01" value="0,00" readonly></td>
			<td style="color:'.$color.';text-align:center;width:160px;border: inset 0pt">'.$mostrar.'  '.$reg->estado.$notacredito.'</td>
			<td style="visibility:hidden">
			<input type="hidden" name="idventa[]" id="idventa[]" value="'.$reg->idventa.'">
			<input type="hidden" name="tasa_dollar[]" id="tasa_dollar[]" value="'.$reg->tasa_dollar.'">
			<input type="hidden" name="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'">
			<input type="hidden" name="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'" >
			<input type="hidden" name="estado[]" value="'.$reg->estado.$notacredito.'">
			</td></tr>'; */
			echo '<tr class="filas">
			<td><input type="checkbox"  style="text-align:right;width:45px;border: inset 0pt" name="iddocumento[]" id="iddocumento[]" onchange="change_checkbox('.$cont.','.$this1.')" value="'.$cont.'"></td>
			<td><input type="text" style="text-align:right;width:45px;border: inset 0pt" name="num_comprobante[]" value="'.$reg->num_comprobante1.'"  readonly></td>
			<td><input type="text" style="text-align:right;width:45px;border: inset 0pt" name="num_comprobantef[]" value="'.$num_comprobante.'"  readonly></td>
			<td><input type="text" style="text-align:right;width:60px;border: inset 0pt" name="montoddt[]" value="'.$montoddt.'"  readonly></td>
			<td><input type="text" style="text-align:right;width:60px;border: inset 0pt" name="montodd[]" id="montodd[]" step="0.01" value="'.$montodd.'"  readonly></td>
					<input type="hidden" style="text-align:right" name="montod[]" id="montod[]" step="0.01" value="'.$montod.'" readonly>
			<td style="text-align:right"><input type="text" style="text-align:right;width:60px"  name="abono[]" id="abono[]" step="0.01" value="0,00" ></td>
			<td><input type="text" style="text-align:right;width:60px;border: inset 0pt" name="pendiente[]" id="pendiente[]" step="0.01" value="0,00" readonly></td>
			<td style="color:'.$color.';text-align:centerborder: inset 0pt;width:50px">'.$mostrar.'  '.$reg->estado.$notacredito.'</td>
			<td style="visibility:hidden">
			<input type="hidden" name="idventa[]" id="idventa[]" value="'.$reg->idventa.'">
			<input type="hidden" name="tasa_dollar[]" id="tasa_dollar[]" value="'.$reg->tasa_dollar.'">
			<input type="hidden" name="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'">
			<input type="hidden" name="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'" >
			<input type="hidden" name="estado[]" value="'.$reg->estado.$notacredito.'">
			</td></tr>';
			
			$cont+=1;	
		}
		$deuda	=	number_format("$deuda",2,",",".");
		$totald =	number_format("$totald",2,",",".");
		// Solo Para JVG No coloco fila de diferencia y coloco el input en pagocliente-view
		/*  <tr>
			<th></th>
			<th></th>
			
			<th>Diferencia Bs.</th>
			<th><input type="text" style="text-align:right" name="diferencia" id="diferencia" value="0.00" step="any"></th>
			<th></th>
			<th></th>
			<th></th>
			</tr> */
		echo '</tbody><tfoot>
			<tr>
			<th>Totales</th>
			<th></th>
			<th></th>
			<th><input type="hidden" style="text-align:right;width:120px" name="total_documento" id="total_documento" value="'.$totald.'" step="0.01"></th>
			<th><input type="hidden" style="text-align:right;width:120px" name="total_saldo" id="total_saldo" value="'.$deuda.'" step="0.01"></th>
			<th></th>
			<th></th>
			<th></th>
			</tr>
			
		</tfoot>';
	break;

    /*
	case 'listarDetallecobroradar':
		//recibimos el idventa
		$id=$_GET['id'];

		$rspta=$cliente->listarDetallecobro($id);
		$total=0;


		/* echo '<thead style="background-color:#A9D0F5">
				<th style="text-align:right;width:60px">Opción</th>
				<th style="text-align:right;width:120px">Orden</th>
				<th style="text-align:right;width:120px">Factura</th>
				<th style="text-align:right;width:120px">Total Documento</th>
				<th style="text-align:right;width:120px">Saldo</th>
				<th style="text-align:right;width:120px">Abono</th>
				<th style="text-align:right;width:120px">Pendiente</th>
				<th style="text-align:right;width:120px">Estado</th>
			</thead>
			<tbody>'; */
			
	/*
			echo '<thead style="background-color:#A9D0F5">
			<th style="text-align:right;width:45px">Opción</th>
			<th style="text-align:right;width:45px">Orden</th>
			<th style="text-align:right;width:45px">Factura</th>
			<th style="text-align:right;width:60px">Total</th>
			<th style="text-align:right;width:60px">Saldo</th>
			<th style="text-align:right;width:60px">Abono</th>
			<th style="text-align:right;width:60px">Pendiente</th>
			<th style="text-align:right;width:50px">Estado</th>
		</thead>
		<tbody>';
		$cont		=0; 
		$this1	='this';
		$deuda	=0;
		$totald	=0;
		while ($reg=$rspta->fetch_object()) {
			$montod		=	$reg->total_ventaimpuesto;
			$montodd	=	$reg->total_ventaimpuestod;
			$montodt	=	$reg->total_ventaimpuesto;
			$montoddt	=	$reg->total_ventaimpuestod;
			$montop		=	$reg->montopendiente;
			$montopd	=	$reg->montopendiented;
			if ($montop>0){
				$montod		= 	$montop;
				$montodd	= 	$montopd;
			}
			$num_comprobante = $reg->num_comprobante;
			if ($reg->num_comprobante==0){
				$num_comprobante = 'Sin Factura';
			}
			$notacredito='';
			$color='red';
			if ($reg->notacredito=='si'){
				$notacredito='-Notacredito';
				$color			='purple';
			}
			$mostrar='';
			if ($reg->estado=='Parcial'){
				$color	='orange';
				$mostrar='<button class="btn btn-warning btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\')"><i class="fa fa-eye"></i></button>';
			}
			$deuda 		= $deuda + $montodd;
			$totald		= $totald + $montoddt;
			$montod		=	number_format("$montod",2,",",".");
			$montodd	=	number_format("$montodd",2,",",".");
			$montodt	=	number_format("$montodt",2,",",".");
			$montoddt	=	number_format("$montoddt",2,",",".");
			
			/* echo '<tr class="filas">
			<td><input type="checkbox" name="iddocumento[]" id="iddocumento[]" onchange="change_checkbox('.$cont.','.$this1.')" value="'.$cont.'"></td>
			<td><input type="text" style="text-align:right;width:120px;border: inset 0pt" name="num_comprobante[]" value="'.$reg->num_comprobante1.'"  readonly></td>
			<td><input type="text" style="text-align:right;width:120px;border: inset 0pt" name="num_comprobantef[]" value="'.$num_comprobante.'"  readonly></td>
			<td><input type="text" style="text-align:right;width:120px;border: inset 0pt" name="montoddt[]" value="'.$montoddt.'"  readonly></td>
			<td><input type="text" style="text-align:right;width:120px;border: inset 0pt" name="montodd[]" id="montodd[]" step="0.01" value="'.$montodd.'"  readonly></td>
					<input type="hidden" style="text-align:right" name="montod[]" id="montod[]" step="0.01" value="'.$montod.'" readonly>
			<td style="text-align:right"><input type="text" style="text-align:right;width:120px" name="abono[]" id="abono[]" step="0.01" value="0,00" ></td>
			<td><input type="text" style="text-align:right;width:120px;border: inset 0pt" name="pendiente[]" id="pendiente[]" step="0.01" value="0,00" readonly></td>
			<td style="color:'.$color.';text-align:center;width:160px;border: inset 0pt">'.$mostrar.'  '.$reg->estado.$notacredito.'</td>
			<td style="visibility:hidden">
			<input type="hidden" name="idventa[]" id="idventa[]" value="'.$reg->idventa.'">
			<input type="hidden" name="tasa_dollar[]" id="tasa_dollar[]" value="'.$reg->tasa_dollar.'">
			<input type="hidden" name="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'">
			<input type="hidden" name="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'" >
			<input type="hidden" name="estado[]" value="'.$reg->estado.$notacredito.'">
			</td></tr>'; */
			
/*
			echo '<tr class="filas">
			<td><input type="checkbox" style="text-align:right;width:45px;border: inset 0pt" name="iddocumento[]" id="iddocumento[]" onchange="change_checkbox('.$cont.','.$this1.')" value="'.$cont.'"></td>
			<td><input type="text" style="text-align:right;width:45px;border: inset 0pt" name="num_comprobante[]" value="'.$reg->num_comprobante1.'"  readonly></td>
			<td><input type="text" style="text-align:right;width:45px;border: inset 0pt" name="num_comprobantef[]" value="'.$num_comprobante.'"  readonly></td>
			<td><input type="text" style="text-align:right;width:60px;border: inset 0pt" name="montoddt[]" value="'.$montoddt.'"  readonly></td>
			<td><input type="text" style="text-align:right;width:60px;border: inset 0pt" name="montodd[]" id="montodd[]" step="0.01" value="'.$montodd.'"  readonly></td>
					<input type="hidden" style="text-align:right" name="montod[]" id="montod[]" step="0.01" value="'.$montod.'" readonly>
			<td style="text-align:right"><input type="text" style="text-align:right;width:60px" name="abono[]" id="abono[]" step="0.01" value="0,00" ></td>
			<td><input type="text" style="text-align:right;width:60px;border: inset 0pt" name="pendiente[]" id="pendiente[]" step="0.01" value="0,00" readonly></td>
			<td style="color:'.$color.';text-align:centerborder: inset 0pt;width:50px">'.$mostrar.'  '.$reg->estado.$notacredito.'</td>
			<td style="visibility:hidden">
			<input type="hidden" name="idventa[]" id="idventa[]" value="'.$reg->idventa.'">
			<input type="hidden" name="tasa_dollar[]" id="tasa_dollar[]" value="'.$reg->tasa_dollar.'">
			<input type="hidden" name="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'">
			<input type="hidden" name="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'" >
			<input type="hidden" name="estado[]" value="'.$reg->estado.$notacredito.'">
			</td></tr>';
			$cont+=1;	
		}
		$deuda	=	number_format("$deuda",2,",",".");
		$totald =	number_format("$totald",2,",",".");
		
		echo '</tbody><tfoot>
			<tr>
			<th>Totales</th>
			<th></th>
			<th></th>
			<th><input type="hidden" style="text-align:right;width:120px" name="total_documento" id="total_documento" value="'.$totald.'" step="0.01"></th>
			<th><input type="hidden" style="text-align:right;width:120px" name="total_saldo" id="total_saldo" value="'.$deuda.'" step="0.01"></th>
			<th></th>
			<th></th>
			<th></th>
			</tr>
			
		</tfoot>';
	break;
	
	
	
	
	case 'listarDetallecobroradar':
		//recibimos el idventa
		$id=$_GET['id'];

		$rspta=$cliente->listarDetallecobro($id);
		$total=0;

		echo '<thead style="background-color:#A9D0F5">
			<td style="text-align:center;width:45px">Seleccionar</td>
			<td style="text-align:center;width:45px">Orden</td>
			<td style="text-align:center;width:45px">Factura</td>
			<td style="text-align:center;width:60px">Total</td>
			<td style="text-align:center;width:60px">Saldo</td>
			<td style="text-align:center;width:60px">Abono</td>
			<td style="text-align:center;width:60px">Pendiente</td>
			<td style="text-align:center;width:50px">Estado</td>
		</thead>
		<tbody>';
		$cont		=0; 
		$this1	='this';
		$deuda	=0;
		$totald	=0;
		while ($reg=$rspta->fetch_object()) {
			$montod		=	$reg->total_ventaimpuesto;
			$montodd	=	$reg->total_ventaimpuestod;
			$montodt	=	$reg->total_ventaimpuesto;
			$montoddt	=	$reg->total_ventaimpuestod;
			$montop		=	$reg->montopendiente;
			$montopd	=	$reg->montopendiented;
			if ($montop>0){
				$montod		= 	$montop;
				$montodd	= 	$montopd;
			}
			$num_comprobante = $reg->num_comprobante;
			$num_comprobante = $reg->num_comprobante;
			if ($reg->num_comprobante==0){
				$num_comprobante = '';
				if ($reg->tipo_comprobante=='Notadebito'){
					$num_comprobante ='Nota Débito';
				};
			}
			$etiqueta 	= '';
			$notacredito='';
			$color='red';
			if ($reg->notacredito=='si'){
				$notacredito='-Notacredito';
				//$color			='purple';
			}

			$retencion = '';
			if ($reg->agenteretencion=='si'){
				$retencion = '-Retencion';
			}

			$mostrar='<button class="btn btn-default btn-xs"><i></i></button>';
			if ($reg->estado=='Parcial'){
				$color		='yellow';
				$mostrar	='<button class="btn btn-warning btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\')"><i class="fa fa-eye"></i></button>';
				$etiqueta ='<span class="badge bg-yellow">'.$reg->estado.$notacredito.$retencion.'</span>';
			}else{
				$etiqueta ='<span class="badge bg-red">'.$reg->estado.$notacredito.$retencion.'</span>';
			}
			$deuda 		= $deuda + $montodd;
			$totald		= $totald + $montoddt;
			$montod		=	number_format("$montod",2,",",".");
			$montodd	=	number_format("$montodd",2,",",".");
			$montodt	=	number_format("$montodt",2,",",".");
			$montoddt	=	number_format("$montoddt",2,",",".");
			
		
			echo '<tr class="filas">
			<td style="text-align:right;width:45px"><input type="checkbox" style="text-align:right;width:45px;border: inset 0pt" name="iddocumento[]" id="iddocumento[]" onchange="change_checkbox('.$cont.','.$this1.')" value="'.$cont.'"></td>
			<td style="text-align:right;width:45px"><input type="text" style="text-align:right;width:45px;border: inset 0pt;background-color:#ffffff" name="num_comprobante[]" value="'.$reg->num_comprobante1.'"  readonly></td>
			<td style="text-align:right;width:45px"><input type="text" style="text-align:right;width:45px;border: inset 0pt;background-color:#ffffff" name="num_comprobantef[]" value="'.$num_comprobante.'"  readonly></td>
			<td style="text-align:right;width:60px"><input type="text" style="text-align:right;border: inset 0pt;width:60px;background-color:#ffffff" name="montoddt[]" value="'.$montoddt.'"  readonly></td>
			<td style="text-align:right;color:black;width:60px"><input type="text" style="text-align:right;border: inset 0pt;width:60px;background-color:#ffffff" name="montodd[]" id="montodd[]" step="0.01" value="'.$montodd.'"  readonly></td>
					<input type="hidden" style="text-align:right" name="montod[]" id="montod[]" step="0.01" value="'.$montod.'" readonly>
			<td style="text-align:right;background-color:#cffedf;color:blue;width:60px;"><input type="text" style="text-align:right;width:60px;background-color:#cffedf;border:none" name="abono[]" id="abono[]" step="0.01" value="0,00" ></td>
			<td style="text-align:right;width:60px;"><input type="text" style="text-align:right;width:60px;border: inset 0pt;background-color:#ffffff" name="pendiente[]" id="pendiente[]" step="0.01" value="0,00" readonly></td>
			
			<td style="color:'.$color.';text-align:center;border: inset 0pt;width:50px;">
					<input type="hidden" style="border: inset 0pt;background-color:#ffffff;width:50px;" name="estado[]" value="'.$reg->estado.$notacredito.$retencion.'">'.$mostrar.' '.'<span id="estado'.$cont.'">'.$etiqueta.'</span>
					<input type="hidden" name="estado1[]" id="estado1[]" value="'.$reg->estado.'">
					<input type="hidden" name="idventa[]" id="idventa[]" value="'.$reg->idventa.'">
					<input type="hidden" name="tasa_dollar[]" id="tasa_dollar[]" value="'.$reg->tasa_dollar.'">
					<input type="hidden" name="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'">
					
					
			</td>
			</tr>';
			$cont+=1;	
		}
		$deuda	=	number_format("$deuda",2,",",".");
		$totald =	number_format("$totald",2,",",".");
		
		echo '</tbody><tfoot>
			<tr>
			<td style="text-align:center;width:45px"></td>
			<td style="text-align:center;width:45px"></td>
			<td style="text-align:center;width:45px"></td>
			<td style="text-align:center;width:60px"><input type="hidden" style="text-align:right;width:60px" name="total_documento" id="total_documento" value="'.$totald.'" step="0.01"></td>
			<td style="text-align:center;width:60px"><input type="hidden" style="text-align:right;width:60px" name="total_saldo" id="total_saldo" value="'.$deuda.'" step="0.01"></td>
			<td style="text-align:center;width:60px"></td>
			<td style="text-align:center;width:60px"></td>
			<td style="text-align:center;width:50px"></td>

			</tr>
			
		</tfoot>';
	break;
	
	
	
	case 'listarDetallecobroradar':
		//recibimos el idventa
		$id=$_GET['id'];

		$rspta=$cliente->listarDetallecobro($id);
		$total=0;

		echo '<thead style="background-color:#A9D0F5">
			<th style="text-align:center;width:45px">Seleccionar</th>
			<th style="text-align:center;width:45px">Orden</th>
			<th style="text-align:center;width:45px">Factura</th>
			<th style="text-align:center;width:60px">Total</th>
			<th style="text-align:center;width:60px">Saldo</th>
			<th style="text-align:center;width:60px">Abono</th>
			<th style="text-align:center;width:60px">Pendiente</th>
			<th style="text-align:center;width:50px">Estado</th>
		</thead>
		<tbody>';
		$cont		=0; 
		$this1	='this';
		$deuda	=0;
		$totald	=0;
		while ($reg=$rspta->fetch_object()) {
			$montod		=	$reg->total_ventaimpuesto;
			$montodd	=	$reg->total_ventaimpuestod;
			$montodt	=	$reg->total_ventaimpuesto;
			$montoddt	=	$reg->total_ventaimpuestod;
			$montop		=	$reg->montopendiente;
			$montopd	=	$reg->montopendiented;
			if ($montop>0){
				$montod		= 	$montop;
				$montodd	= 	$montopd;
			}
			$num_comprobante = $reg->num_comprobante;
			$num_comprobante = $reg->num_comprobante;
			if ($reg->num_comprobante==0){
				$num_comprobante = '';
				if ($reg->tipo_comprobante=='Notadebito'){
					$num_comprobante ='Nota Débito';
				};
			}
			$etiqueta 	= '';
			$notacredito='';
			$color='red';
			if ($reg->notacredito=='si'){
				$notacredito='-Notacredito';
				//$color			='purple';
			}

			$retencion = '';
			if ($reg->agenteretencion=='si'){
				$retencion = '-Retencion';
			}

			$mostrar='<button class="btn btn-default btn-xs"><i></i></button>';
			if ($reg->estado=='Parcial'){
				$color		='yellow';
				$mostrar	='<button class="btn btn-warning btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\')"><i class="fa fa-eye"></i></button>';
				$etiqueta ='<span class="badge bg-yellow">'.$reg->estado.$notacredito.$retencion.'</span>';
			}else{
				$etiqueta ='<span class="badge bg-red">'.$reg->estado.$notacredito.$retencion.'</span>';
			}
			$deuda 		= $deuda + $montodd;
			$totald		= $totald + $montoddt;
			$montod		=	number_format("$montod",2,",",".");
			$montodd	=	number_format("$montodd",2,",",".");
			$montodt	=	number_format("$montodt",2,",",".");
			$montoddt	=	number_format("$montoddt",2,",",".");
			
		
			echo '<tr class="filas">
			<td><input type="checkbox" style="text-align:right;width:45px;border: inset 0pt" name="iddocumento[]" id="iddocumento[]" onchange="change_checkbox('.$cont.','.$this1.')" value="'.$cont.'"></td>
			<td><input type="text" style="text-align:right;width:45px;border: inset 0pt;background-color:#ffffff" name="num_comprobante[]" value="'.$reg->num_comprobante1.'"  readonly></td>
			<td><input type="text" style="text-align:right;width:45px;border: inset 0pt;background-color:#ffffff" name="num_comprobantef[]" value="'.$num_comprobante.'"  readonly></td>
			<td><input type="text" style="text-align:right;border: inset 0pt;width:60px;background-color:#ffffff" name="montoddt[]" value="'.$montoddt.'"  readonly></td>
			<td><input type="text" style="text-align:right;border: inset 0pt;width:60px;background-color:#ffffff" name="montodd[]" id="montodd[]" step="0.01" value="'.$montodd.'"  readonly></td>
					<input type="hidden" style="text-align:right" name="montod[]" id="montod[]" step="0.01" value="'.$montod.'" readonly>
			<td><input type="text" style="text-align:right;width:60px;background-color:#cffedf;border:none" name="abono[]" id="abono[]" step="0.01" value="0,00" ></td>
			<td><input type="text" style="text-align:right;width:60px;border: inset 0pt;background-color:#ffffff" name="pendiente[]" id="pendiente[]" step="0.01" value="0,00" readonly></td>
			
			<td style="color:'.$color.';text-align:center;">
					<input type="hidden" style="border: inset 0pt;background-color:#ffffff;width:50px;" name="estado[]" value="'.$reg->estado.$notacredito.$retencion.'">'.$mostrar.' '.'<span id="estado'.$cont.'">'.$etiqueta.'</span>
					
					
					
			</td>
				
			<td style="visibility:hidden">
				<input type="hidden" name="estado1[]" id="estado1[]" value="'.$reg->estado.'">
				<input type="hidden" name="idventa[]" id="idventa[]" value="'.$reg->idventa.'">
				<input type="hidden" name="tasa_dollar[]" id="tasa_dollar[]" value="'.$reg->tasa_dollar.'">
				<input type="hidden" name="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'">
			</td>
			</tr>';
			$cont+=1;	
		}
		$deuda	=	number_format("$deuda",2,",",".");
		$totald =	number_format("$totald",2,",",".");
		
		echo '</tbody><tfoot>
			<tr>
			<td></td>
			<td></td>
			<td></td>
			<td><input type="hidden" style="text-align:right;width:60px" name="total_documento" id="total_documento" value="'.$totald.'" step="0.01"></td>
			<td><input type="hidden" style="text-align:right;width:60px" name="total_saldo" id="total_saldo" value="'.$deuda.'" step="0.01"></td>
			<td></td>
			<td></td>
			<td></td>

			</tr>
			
		</tfoot>';
	break;
	*/
	
	case 'listarDetallecobroradar':
		//recibimos el idventa
		$id=$_GET['id'];

		$rspta=$cliente->listarDetallecobro($id);
		$total=0;

		//<th style="text-align:center;width:45px">NC</th>
		echo '<thead style="background-color:#A9D0F5">
			<th style="text-align:center;width:45px">Seleccionar</th>
			<th style="text-align:center;width:45px">NC</th>
			<th style="text-align:center;width:45px">MontoNC</th>
			<th style="text-align:center;width:45px">Orden</th>
			<th style="text-align:center;width:45px">Factura</th>
			<th style="text-align:center;width:60px">Total</th>
			<th style="text-align:center;width:60px">Saldo</th>
			<th style="text-align:center;width:60px">Abono</th>
			<th style="text-align:center;width:60px">Pendiente</th>
			<th style="text-align:center;width:50px">Estado</th>
		</thead>
		<tbody>';
		$cont		=0;
		$cont2		=0; 
		$this1	='this';
		$this2	='this';
		$deuda	=0;
		$totald	=0;
		while ($reg=$rspta->fetch_object()) {
			$montod		=	$reg->total_ventaimpuesto;
			$montodd	=	$reg->total_ventaimpuestod;
			$montodt	=	$reg->total_ventaimpuesto;
			$montoddt	=	$reg->total_ventaimpuestod;
			$montop		=	$reg->montopendiente;
			$montopd	=	$reg->montopendiented;
			if ($montop>0){
				$montod		= 	$montop;
				$montodd	= 	$montopd;
			}
			$num_comprobante = $reg->num_comprobante;
			$num_comprobante = $reg->num_comprobante;
			if ($reg->num_comprobante==0){
				$num_comprobante = '';
				if ($reg->tipo_comprobante=='Notadebito'){
					$num_comprobante ='Nota Débito';
				};
			}
			$etiqueta 	= '';
			$notacredito='';
			$color='red';
			if ($reg->notacredito=='si'){
				$notacredito='-Notacredito';
				//$color			='purple';
			}

			$retencion = '';
			if ($reg->agenteretencion=='si'){
				$retencion = '-Retencion';
			}

			$mostrar='<button class="btn btn-default btn-xs"><i></i></button>';
			if ($reg->estado=='Parcial'){
				$color		='yellow';
				$mostrar	='<button class="btn btn-warning btn-xs" onclick="mostrarcobros('.$reg->idventa.','.$reg->num_comprobante1.')"><i class="fa fa-eye"></i></button>';
				$etiqueta ='<span class="badge bg-yellow">'.$reg->estado.$notacredito.$retencion.'</span>';
			}else{
				$etiqueta ='<span class="badge bg-red">'.$reg->estado.$notacredito.$retencion.'</span>';
			}
			$deuda 		= $deuda + $montodd;
			$totald		= $totald + $montoddt;
			$montod		=	number_format("$montod",2,",",".");
			$montodd	=	number_format("$montodd",2,",",".");
			$montodt	=	number_format("$montodt",2,",",".");
			$montoddt	=	number_format("$montoddt",2,",",".");
			
			//<td><input type="checkbox" style="text-align:right;width:45px;border: inset 0pt" name="notacredito[]" id="notacredito[]" onchange="change_checkboxnc('.$cont.','.$this1.')" value="no"></td>
		
			echo '<tr class="filas">
			<td  style="text-align:center;">
				<label class="custom-checkbox">
				<input type="checkbox" style="width:30px;"  name="iddocumento[]" id="iddocumento[]" onchange="change_checkbox('.$cont.','.$this1.')" value="'.$cont.'">
				 <span class="checkmark"></span>
				</label>
			</td>
			<td  style="text-align:center;">
				<label class="custom-checkbox2">
					<input type="checkbox" style="width:30px" name="notacreditode[]" id="notacreditode[]" onchange="change_checkbox2('.$cont2.','.$this2.')" value="no" disabled>
					<span class="checkmark"></span>
				</label>
			</td>
			<td><input type="text" style="text-align:right;width:70px;border: inset 0pt;background-color:#ffffff" name="montonc[]"  id="montonc[]" step="0.01" value="0,00" readonly></td>
			<td><input type="text" style="text-align:right;width:45px;border: inset 0pt;background-color:#ffffff" name="num_comprobante[]" value="'.$reg->num_comprobante1.'"  readonly></td>
			<td><input type="text" style="text-align:right;width:45px;border: inset 0pt;background-color:#ffffff" name="num_comprobantef[]" value="'.$num_comprobante.'"  readonly></td>
			<td><input type="text" style="text-align:right;border: inset 0pt;width:60px;background-color:#ffffff" name="montoddt[]" value="'.$montoddt.'"  readonly></td>
			<td><input type="text" style="text-align:right;border: inset 0pt;width:60px;background-color:#ffffff" name="montodd[]" id="montodd[]" step="0.01" value="'.$montodd.'"  readonly></td>
					
			<td><input type="text" style="text-align:right;width:60px;background-color:#cffedf;border:none" name="abono[]" id="abono[]" step="0.01" value="0,00" ></td>
			<td><input type="text" style="text-align:right;width:60px;border: inset 0pt;background-color:#ffffff" name="pendiente[]" id="pendiente[]" step="0.01" value="0,00" readonly></td>
			
			<td style="color:'.$color.';text-align:center;">
					<input type="hidden" style="border: inset 0pt;background-color:#ffffff;width:50px;" name="estado[]" value="'.$reg->estado.$notacredito.$retencion.'">'.$mostrar.' '.'<span id="estado'.$cont.'">'.$etiqueta.'</span>
					
					
					
			</td>
				
			<td style="visibility:hidden">
				<input type="hidden" name="estado1[]" id="estado1[]" value="'.$reg->estado.'">
				<input type="hidden" name="idventa[]" id="idventa[]" value="'.$reg->idventa.'">
				<input type="hidden" name="tasa_dollar[]" id="tasa_dollar[]" value="'.$reg->tasa_dollar.'">
				<input type="hidden" name="tipo_comprobante[]" value="'.$reg->tipo_comprobante.'">
				<input type="hidden" name="montod[]" id="montod[]" step="0.01" value="'.$montod.'">
			</td>
			</tr>';
			$cont+=1;
			$cont2+=1;	
		}
		$deuda	=	number_format("$deuda",2,",",".");
		$totald =	number_format("$totald",2,",",".");
		//<td></td>
		echo '</tbody><tfoot>
			<tr>
			<td></td>
			<td></td>
			<td></td>
			<td><input type="hidden" style="text-align:right;width:60px" name="total_documento" id="total_documento" value="'.$totald.'" step="0.01"></td>
			<td><input type="hidden" style="text-align:right;width:60px" name="total_saldo" id="total_saldo" value="'.$deuda.'" step="0.01"></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>

			</tr>
			
		</tfoot>';
	break;

	/** Historico */
	case 'listarh':
		$rspta=$cliente->listarh();
		$data=Array();

		while ($reg=$rspta->fetch_object()) {
			if ($reg->saldod>0){
				$estado = '<span class="badge bg-red">Pendiente</span>';
				if ($reg->notacredito=='si'){
					$estado = '<span class="badge bg-red">Pendiente-NotaCrédito</span>';
				}
				if ($reg->pagadod>0) {
					$estado = '<span class="badge bg-orange">Parcial</span>';
					if ($reg->notacredito=='si'){
						$estado = '<span class="badge bg-orange">Parcial-NotaCrédito</span>';
					}
				}
				
				
			}else{
				$estado = '<span class="badge bg-green">Sin Deuda</span>';
			}
			$fecha		= $reg->fecha;
			$ranking 	= $reg->rank11;
			if($reg->fecha=='01-01-2121'){
				$fecha='-';
				$ranking 	= '';
			} 
			$fechan=$reg->fechanumero;
			if($reg->fechanumero=='80721'){
				$fechan='-';
				$fechan='-';
			}

			$data[]=array(
			"0"=>'<button class="btn btn-warning btn-xs" onclick="mostrar('.$reg->idcliente.')"><i class="fa fa-eye"></i></button>',
			"1"=>'<div align="left">'.$reg->nombre.'</div>',
			"2"=>'<div align="center">'.$fecha.'</div>',
			/* "3"=>'<div align="center">'.$ranking.'</div>', */
			"3"=>'<div align="center">'.$reg->nrodocumento.'</div>',
			"4"=>'<div align="right">'.number_format("$reg->deudad",2,",",".").'</div>',
			"5"=>'<div align="right">'.number_format("$reg->pagadod",2,",",".").'</div>',
			"6"=>'<div align="right">'.number_format("$reg->saldod",2,",",".").'</div>',
			"7"=>'<div align="center">'.$estado.'</div>',
			"8"=>$reg->fechanumero
            );
		}
		$results=array(
						"sEcho"=>1,//info para datatables
						"iTotalRecords"=>count($data),//enviamos el total de registros al datatable
						"iTotalDisplayRecords"=>count($data),//enviamos el total de registros a visualizar
						"aaData"=>$data); 
		echo json_encode($results);
	break;

	case 'mostrarh':
		$rspta=$cliente->mostrarh($idcliente);
		echo json_encode($rspta);
	break;

	case 'listarDetalleh':
		//recibimos el idventa
		$id				=$_GET['id'];

		$rspta		=$cliente->listarDetalleh($id);
		$saldo		=0;
		$saldod		=0;
		$deudad		=0;
		$cobradod	=0;
		echo '<thead style="background-color:#A9D0F5">
			<th>Nota de Entrega</th>
			<th>Nro. Factura</th>
			<th>Fecha Emisión</th>
			<th>Fecha Despacho</th>
			<th>Deuda</th>
			<th>Cobrado</th>
			<th>Por Cobrar</th>
			<th>Estado</th>
		</thead>';
		while ($reg=$rspta->fetch_object()) {
			if ($reg->estado!='Anulado'){
				$saldo		=	$saldo + $reg->porcobrar;
				$saldod		=	$saldod + $reg->porcobrard;
				$deudad		=	$deudad + $reg->deudad;
				$cobradod	=	$cobradod + $reg->cobradod;
			}
			$nrodocumento = $reg->nrodocumento;
			if ($reg->nrodocumento==0){
				$nrodocumento ='Sin Factura';
				if ($reg->documento=='Notadebito'){
					$nrodocumento ='Nota Débito';
				};
			}
			//	$montod	=	number_format("$montod",2,",",".");
			$estado = $reg->estado;
			$mostraro='<button class="btn btn-success btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\')"><i class="fa fa-eye"></i></button>';
			$mostrara='<button class="btn btn-primary btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\')"><i class="fa fa-eye"></i></button>';
			$mostrarv='<button class="btn btn-success btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\')"><i class="fa fa-eye"></i></button>';
			if ($reg->agenteretencion=='si'){
				$estado = $estado.'-Retencion';
			}	

			if ($reg->estado=='Pagada'){
				echo '<tr class="filas">
				<td>	<a href="#" onclick="mostrar1('.$reg->idventa.')" >'.$reg->nrodocumento1.'</a></td>
				<td>'.$nrodocumento.'</td>
				<td>'.$reg->fecha.'</td>
				<td>'.$reg->fechadespacho.'</td>
				<td>'.number_format("$reg->deudad",2,",",".").'</td>
				<td>'.number_format("$reg->cobradod",2,",",".").'</td>
				<td>'.number_format("$reg->porcobrard",2,",",".").'</td>';

				if ($reg->notacredito=='si'){
					$estado = $estado.'-NotaCredito';
					echo '<td>'.$mostraro.' '.'<span class="badge bg-green">'.$estado.'</span></td>';
				}else{
					echo '<td>'.$mostrarv.' '.'<span class="badge bg-green">'.$estado.'</span></td>';
				}	
				
			}elseif($reg->estado=='Parcial'){
				echo '<tr class="filas">
				
				<td>	<a href="#" onclick="mostrar1('.$reg->idventa.')" >'.$reg->nrodocumento1.'</a></td>
				<td>'.$nrodocumento.'</td>
				<td>'.$reg->fecha.'</td>
				<td>'.$reg->fechadespacho.'</td>
				<td>'.number_format("$reg->deudad",2,",",".").'</td>
				<td>'.number_format("$reg->cobradod",2,",",".").'</td>
				<td>'.number_format("$reg->porcobrard",2,",",".").'</td>';
				$mostraro='<button class="btn btn-warning btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\')"><i class="fa fa-eye"></i></button>';
				$mostrara='<button class="btn btn-warning btn-xs" onclick="mostrarcobros('.$reg->idventa.',\''.$reg->nrodocumento1.'\')"><i class="fa fa-eye"></i></button>';
				if ($reg->notacredito=='si'){
					$estado = $estado.'-NotaCredito';
					echo '<td>'.$mostraro.' '.'<span class="badge bg-yellow">'.$estado.'</span></td>';
				}else{
					echo '<td>'.$mostrara.' '.'<span class="badge bg-yellow">'.$estado.'</span></td>';
				}
				
			}elseif($reg->estado=='Anulado'){
				echo '<tr class="filas">
				<td>	<a href="#" onclick="mostrar1('.$reg->idventa.')" >'.$reg->nrodocumento1.'</a></td>
				<td>'.$nrodocumento.'</td>
				<td>'.$reg->fecha.'</td>
				<td>'.$reg->fechadespacho.'</td>
				<td>'.number_format("$reg->deudad",2,",",".").'</td>
				<td>'.number_format("0.0",2,",",".").'</td>
				<td>'.number_format("0.0",2,",",".").'</td>
				<td><span class="badge bg-navy">'.$reg->estado.'</span></td>';	
			}else{
				echo '<tr class="filas">
				<td>	<a href="#" onclick="mostrar1('.$reg->idventa.')" >'.$reg->nrodocumento1.'</a></td>
				<td>'.$nrodocumento.'</td>
				<td>'.$reg->fecha.'</td>
				<td>'.$reg->fechadespacho.'</td>
				<td>'.number_format("$reg->deudad",2,",",".").'</td>
				<td>'.number_format("$reg->cobradod",2,",",".").'</td>
				<td>'.number_format("$reg->porcobrard",2,",",".").'</td>';
				if ($reg->notacredito=='si'){
					$estado = $estado.'-NotaCredito';
					echo '<td><span class="badge bg-red">'.$estado.'</span></td>';
				}else{
					echo '<td><span class="badge bg-red">'.$estado.'</span></td>';
				}

			
			}
			echo '</tr>';
		}
		$saldo		=	number_format("$saldo",2,",",".");
		$saldod		=	number_format("$saldod",2,",",".");
		$deudad		=	number_format("$deudad",2,",",".");
		$cobradod	=	number_format("$cobradod",2,",",".");
		echo '<tfoot>
		<th><h4>Saldo</h4></th>
		<th></th>
		<th></th>
		<th></th>
		<th><h4 id="deuda"> '.$deudad.'</h4></th>
		<th><h4 id="cobrado"> '.$cobradod.'</h4></th>
		<th><h4 id="total"> '.$saldod.'</h4> </th>
		</tfoot>';
	break;

	/** fin de historico */
		
}
 ?>