Ejemplo n.º 1
0
  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == ventaGui.getClienteALaFactura()) { // Boton cliente a la factura
      int row = ventaGui.getTablaClientes().getSelectedRow();
      if (row > -1) {
        String id = (String) tablaClientes.getValueAt(row, 0);
        String nom = (String) tablaClientes.getValueAt(row, 1);
        String ap = (String) tablaClientes.getValueAt(row, 2);
        ventaGui.getClienteFactura().setText(id + " " + nom + " " + ap);
      }
    }

    if (e.getSource() == ventaGui.getArticulosALaFactura()) { // Boton articulos a la factura
      int[] rows = ventaGui.getTablaArticulos().getSelectedRows();
      if (rows.length > 0) {
        for (int i = 0; i < rows.length; i++) {
          abrirBase();
          if (!existeProdFacc(Integer.valueOf((String) tablap.getValueAt(rows[i], 0)))) {
            Producto p = Producto.findFirst("numero_producto = ?", (tablap.getValueAt(rows[i], 0)));
            Object cols[] = new Object[5];
            cols[0] = p.get("numero_producto");
            cols[1] = 1;
            cols[2] = p.get("nombre") + " " + p.get("marca");
            cols[3] =
                BigDecimal.valueOf(p.getFloat("precio_venta")).setScale(2, RoundingMode.CEILING);
            cols[4] =
                BigDecimal.valueOf(p.getFloat("precio_venta")).setScale(2, RoundingMode.CEILING);
            if (Base.hasConnection()) {
              Base.close();
            }
            ventaGui.getTablaFacturaDefault().addRow(cols);
            setCellEditor();
            actualizarPrecio();
          }
        }
      }
    }

    if (e.getSource()
        == ventaGui.getBorrarArticulosSeleccionados()) { // boton borrar articulos seleccionados
      int[] rows = ventaGui.getTablaFactura().getSelectedRows();
      if (rows.length > 0) {
        Integer[] idABorrar = new Integer[rows.length];
        for (int i = 0; i < rows.length; i++) {
          idABorrar[i] = (Integer) tablafac.getValueAt(rows[i], 0);
        }
        int i = 0;
        int cantABorrar = 0;
        while (cantABorrar < rows.length) {
          while (i < ventaGui.getTablaFactura().getRowCount()) {
            if ((Integer) ventaGui.getTablaFactura().getValueAt(i, 0) == idABorrar[cantABorrar]) {
              ventaGui.getTablaFacturaDefault().removeRow(i);
              cantABorrar++;
            }
            i++;
          }
          i = 0;
        }
        actualizarPrecio();
      }
    }

    if (e.getSource() == ventaGui.getRealizarVenta()) { // Boton realizar venta
      if (ventaGui.getClienteFactura().getText().equals("")
          || ventaGui.getCalenFacturaText().getText().equals("")) {
        JOptionPane.showMessageDialog(
            ventaGui, "Fecha o cliente vacios", "Error!", JOptionPane.ERROR_MESSAGE);
      } else {
        Venta v = new Venta();
        LinkedList<Pair> parDeProductos = new LinkedList();
        String laFecha = ventaGui.getCalenFacturaText().getText(); // saco la fecha
        String cliente = ventaGui.getClienteFactura().getText();
        Integer idCliente = Integer.valueOf(cliente.split(" ")[0]); // saco el id cliente
        for (int i = 0; i < ventaGui.getTablaFactura().getRowCount(); i++) {
          abrirBase();
          Producto producto = Producto.findFirst("numero_producto = ?", tablafac.getValueAt(i, 0));
          if (Base.hasConnection()) {
            Base.close();
          }
          Integer cantidad = (Integer) tablafac.getValueAt(i, 1); // saco la cantidad
          BigDecimal precioFinal = (BigDecimal) tablafac.getValueAt(i, 3);
          Pair parCantYPrecioFinal = new Pair(cantidad, precioFinal.doubleValue());
          Pair par = new Pair(producto, parCantYPrecioFinal); // creo el par
          parDeProductos.add(par); // meto el par a la lista
        }
        v.set("fecha", laFecha);
        v.set("cliente_id", idCliente);
        v.setProductos(parDeProductos);
        abrirBase();
        if (abmVenta.alta(v)) {
          if (JOptionPane.showConfirmDialog(
                  ventaGui,
                  "¿Desea abrir el dialogo de impresión?",
                  "¡Venta exitosa!",
                  JOptionPane.YES_NO_OPTION)
              == 0) {
            try {
              reporteFactura.mostrarFactura(abmVenta.getUltimoIdVenta());
            } catch (ClassNotFoundException ex) {
              Logger.getLogger(VentaControlador.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
              Logger.getLogger(VentaControlador.class.getName()).log(Level.SEVERE, null, ex);
            } catch (JRException ex) {
              Logger.getLogger(VentaControlador.class.getName()).log(Level.SEVERE, null, ex);
            }
          }
          ventaGui.limpiarVentana();
        } else {
          JOptionPane.showMessageDialog(
              ventaGui, "Ocurrió un error inesperado, venta no realizada");
        }
      }
      if (Base.hasConnection()) {
        Base.close();
      }
    }

    if (e.getSource() == ventaGui.getFacturaNueva()) {
      ventaGui.limpiarVentana();
      ventaGui.getModificar().setEnabled(false);
      ventaGui.getRealizarVenta().setEnabled(true);
    }

    if (e.getSource() == ventaGui.getModificar()) {
      if (ventaGui.getClienteFactura().getText().equals("")
          || ventaGui.getCalenFacturaText().getText().equals("")) {
        JOptionPane.showMessageDialog(
            ventaGui, "Fecha o cliente vacios", "Error!", JOptionPane.ERROR_MESSAGE);
      } else {
        Venta v = new Venta();
        LinkedList<Pair> parDeProductos = new LinkedList();
        String laFecha = ventaGui.getCalenFacturaText().getText(); // saco la fecha
        String cliente = ventaGui.getClienteFactura().getText();
        Integer idCliente = Integer.valueOf(cliente.split(" ")[0]); // saco el id cliente
        for (int i = 0; i < ventaGui.getTablaFactura().getRowCount(); i++) {
          abrirBase();
          Producto producto = Producto.findFirst("numero_producto = ?", tablafac.getValueAt(i, 0));
          if (Base.hasConnection()) {
            Base.close();
          }
          Integer cantidad = (Integer) tablafac.getValueAt(i, 1); // saco la cantidad
          BigDecimal precioFinal = (BigDecimal) tablafac.getValueAt(i, 3);
          Pair parCantYPrecioFinal = new Pair(cantidad, precioFinal.doubleValue());
          Pair par = new Pair(producto, parCantYPrecioFinal); // creo el par
          parDeProductos.add(par); // meto el par a la lista
        }
        v.set("fecha", laFecha);
        v.set("cliente_id", idCliente);
        v.setProductos(parDeProductos);
        System.out.print(idFacturaAModificar);
        v.set("id", idFacturaAModificar);
        abrirBase();
        if (abmVenta.modificar(v)) {
          if (JOptionPane.showConfirmDialog(
                  ventaGui,
                  "¿Desea abrir el dialogo de impresión?",
                  "¡Venta modificada!",
                  JOptionPane.YES_NO_OPTION)
              == 0) {
            try {
              reporteFactura.mostrarFactura(idFacturaAModificar);
            } catch (ClassNotFoundException ex) {
              Logger.getLogger(VentaControlador.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
              Logger.getLogger(VentaControlador.class.getName()).log(Level.SEVERE, null, ex);
            } catch (JRException ex) {
              Logger.getLogger(VentaControlador.class.getName()).log(Level.SEVERE, null, ex);
            }
          }

          ventaGui.limpiarVentana();
          ventaGui.getModificar().setEnabled(false);
          ventaGui.getRealizarVenta().setEnabled(true);

        } else {
          JOptionPane.showMessageDialog(
              ventaGui, "Ocurrió un error inesperado, venta no realizada");
        }
      }
      if (Base.hasConnection()) {
        Base.close();
      }
    }
    ventasControlador.actualizarListaFacturas();
  }