public void cargarFactura() {
   factura = facturaService.obtenerPorCodigo(cobroReporte.getCodigoDocumento());
 }
  public void onRowSelect(SelectEvent event) {
    if (listaFacturasSeleccionados.size() > 1
        && listaFacturasSeleccionados
                .get(0)
                .getNombre()
                .compareTo(
                    listaFacturasSeleccionados
                        .get(listaFacturasSeleccionados.size() - 1)
                        .getNombre())
            != 0) {
      listaFacturasSeleccionados.remove(listaFacturasSeleccionados.size() - 1);
      presentaMensaje(
          FacesMessage.SEVERITY_WARN, "SOLO PUEDE ESCOJER FACTURAS DE UN MISMO CLIENTE");
    }

    totalTotal = newBigDecimal();
    totalAbonos = newBigDecimal();
    totalSaldo = newBigDecimal();
    for (CobroReporte cr : listaFacturasSeleccionados) {
      totalTotal = totalTotal.add(cr.getTotal());
      totalAbonos = totalAbonos.add(cr.getAbono());
      totalSaldo = totalSaldo.add(cr.getSaldo());
    }

    for (CobroReporte cr1 : listaFacturasPendientes) {
      cr1.setEscogido(false);
      cr1.setOrden(0);
    }

    int orden = 1;
    for (CobroReporte cr1 : listaFacturasSeleccionados) {
      cr1.setOrden(orden++);
    }

    for (CobroReporte cr1 : listaFacturasPendientes) {
      for (CobroReporte cr2 : listaFacturasSeleccionados) {
        if (cr1.getId() == cr2.getId()) {
          cr1.setEscogido(true);
          cr1.setOrden(cr2.getOrden());
          break;
        }
      }
    }
  }
  public void onRowUnselect(UnselectEvent event) {
    totalAbonos = newBigDecimal();
    totalSaldo = newBigDecimal();
    for (CobroReporte cr : listaFacturasSeleccionados) {
      totalTotal = totalTotal.add(cr.getTotal());
      totalAbonos = totalAbonos.add(cr.getAbono());
      totalSaldo = totalSaldo.add(cr.getSaldo());
    }

    for (CobroReporte cr1 : listaFacturasPendientes) {
      cr1.setEscogido(false);
      cr1.setOrden(0);
    }

    int orden = 1;
    for (CobroReporte cr1 : listaFacturasSeleccionados) {
      cr1.setOrden(orden++);
    }

    for (CobroReporte cr1 : listaFacturasPendientes) {
      for (CobroReporte cr2 : listaFacturasSeleccionados) {
        if (cr1.getId() == cr2.getId()) {
          cr1.setEscogido(true);
          cr1.setOrden(cr2.getOrden());
          break;
        }
      }
    }
  }
  public void obtenerFacturasPendientes() {
    List<Factura> lista =
        facturaService.obtenerLista(
            "select distinct f from Factura f "
                + "inner join f.cliente c "
                + "where p.numeroDocumento=?1 "
                + "and f.fechaCierre is null and f.activo=true order by f.fechaInicio",
            new Object[] {criterioBusqueda},
            true,
            new Object[] {});

    if (lista.isEmpty()) {
      limpiar();
    } else {
      listaFacturasPendientes = new ArrayList<CobroReporte>();
      criterioBusqueda = new String();

      totalTotal = newBigDecimal();
      totalAbonos = newBigDecimal();
      totalSaldo = newBigDecimal();

      for (Factura factura : lista) {
        CobroReporte cobroReporte = new CobroReporte();

        cobroReporte.setId(factura.getId());
        cobroReporte.setNombre(factura.getCliente().getRazonSocial());
        cobroReporte.setCodigoDocumento(
            factura
                .getEstablecimiento()
                .concat("-")
                .concat(factura.getPuntoEmision())
                .concat("-")
                .concat(factura.getSecuencia()));
        cobroReporte.setFechaEmision(factura.getFechaInicio());
        cobroReporte.setEscogido(false);
        cobroReporte.setOrden(0);
        cobroReporte.setTotal(facturaService.calcularTotal(factura));
        cobroReporte.setAbono(facturaService.calcularEntradas(factura));
        cobroReporte.setSaldo(cobroReporte.getTotal().subtract(cobroReporte.getAbono()));

        listaFacturasPendientes.add(cobroReporte);
      }
    }
  }