private void btnAgregarActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_btnAgregarActionPerformed
    // TODO add your handling code here:
    try {
      ptos = "";
      pto = null;
      txtAPtos.setText("");
      conn.consulta("SELECT * FROM punto_emision");
      if (conn.getRowCount() == 0) {
        pto1 =
            JOptionPane.showInputDialog(
                this,
                "Ingrese Punto de Emisión para Facturación Electrónica en Playa:",
                "Facturacion Electrónica",
                JOptionPane.PLAIN_MESSAGE);
        if (conn.getRowCount() < 2) {
          pto2 =
              JOptionPane.showInputDialog(
                  this,
                  "Ingrese Punto de Emisión para Facturación Electrónica en Oficinas:",
                  "Facturacion Electrónica",
                  JOptionPane.PLAIN_MESSAGE);
          this.setEnabled(false);
          frameEmision.setVisible(true);
          txtAPtos.append(String.format("%10s   %-40s%12s\n", pto1, "", ""));
          txtAPtos.append(String.format("%10s   %-40s%12s\n", pto2, "", ""));
        }
      } else {
        this.setEnabled(false);
        frameEmision.setVisible(true);
        conn.consulta("SELECT s2 FROM punto_emision LIMIT 2");
        pto1 = (String) conn.getValueAt(0, 0);
        pto2 = (String) conn.getValueAt(1, 0);
        txtAPtos.append(String.format("%10s   %-40s%12s\n", pto1, "", ""));
        txtAPtos.append(String.format("%10s   %-40s%12s\n", pto2, "", ""));
        conn.consulta("SELECT * FROM punto_emision");
        int puntos = conn.getRowCount();
        conn.consulta(
            "SELECT s2, autorizacion, fecha_vencimiento FROM punto_emision LIMIT 3, " + puntos);
        for (int i = 0; i < conn.getRowCount(); i++) {
          txtAPtos.append(
              String.format(
                  "%10s   %-40s%12s\n",
                  (String) conn.getValueAt(i, 0),
                  (String) conn.getValueAt(i, 1),
                  (String) conn.getValueAt(i, 2)));
        }
      }

    } catch (SQLException | IllegalStateException ex) {
      System.err.println(ex.getMessage());
    }
  } // GEN-LAST:event_btnAgregarActionPerformed
 private void btnGuardarActionPerformed(
     java.awt.event.ActionEvent evt) { // GEN-FIRST:event_btnGuardarActionPerformed
   // TODO add your handling code here:
   if (txtRazonSocial.getText().length() != 0
       && txtRUC.getText().length() != 0
       && txtNombreComercial.getText().length() != 0
       && txtDireccion.getText().length() != 0
       && txtDireccion.getText().length() != 0
       && txtPropietario.getText().length() != 0
       && new String(passClaveEmail.getPassword()).length() != 0
       && new String(passClave.getPassword()).length() != 0
       && !txtCertificado.getText().equalsIgnoreCase("Sin Certificado")) {
     if (btnGuardar.getText().equalsIgnoreCase("Guardar")) {
       fileChanged = false;
     }
     try {
       conn.consulta("SELECT ruc FROM datos_gasolinera");
       if (conn.getRowCount() == 0) {
         conn.ejecutar(
             "INSERT INTO datos_gasolinera (razon_social, ruc, nombre_comercial, direccion, obligado_llevar_contabilidad, "
                 + "contribuyente_especial, email_estacion, contraseña_mail, despachadores_turno, tipo_ambiente, contraseña_certificado, tipo_cierre_turnos) "
                 + "VALUES ('"
                 + txtRazonSocial.getText()
                 + "', '"
                 + txtRUC.getText()
                 + "', '"
                 + txtNombreComercial.getText()
                 + "', '"
                 + txtDireccion.getText()
                 + "', '"
                 + (chkContabilidad.isSelected() ? "SI" : "NO")
                 + "', '"
                 + (txtContribuyente.getText().length() == 0 ? "0" : txtContribuyente.getText())
                 + "', '"
                 + txtPropietario.getText()
                 + "', "
                 + "AES_ENCRYPT('"
                 + new String(passClaveEmail.getPassword())
                 + "', 'thekey'), "
                 + Integer.parseInt(txtDespachadores.getText())
                 + ", "
                 + (cbxAmbiente.getSelectedIndex() + 1)
                 + "', '"
                 + "AES_ENCRYPT('"
                 + new String(passClave.getPassword())
                 + "', 'thekey'), "
                 + (cbxTurno.getSelectedIndex() + 1)
                 + ")");
         conn.psPrepararSentencias =
             conn.coneccion.prepareStatement(
                 "INSERT INTO datos_gasolinera (certificado_digital) VALUES (?)");
         conn.psPrepararSentencias.setBinaryStream(1, stream, (int) file.length());
         conn.psPrepararSentencias.executeUpdate();
         conn.psPrepararSentencias.close();
         for (int i = 0; i < pto.length; i++) {
           String[] row = pto[i].split(",");
           try {
             conn.consulta(
                 "SELECT idpunto_emision FROM punto_emision ORDER BY idpunto_emision DESC LIMIT 1");
             conn.ejecutar(
                 "INSERT INTO punto_emision "
                     + "VALUES ("
                     + (Integer) conn.getValueAt(0, 0)
                     + ", '"
                     + row[0]
                     + "', '"
                     + row[1]
                     + "', '"
                     + row[2]
                     + "', 1)");
           } catch (SQLException | IllegalStateException ex) {
             System.err.println(ex.getMessage());
           }
         }
         javax.swing.JOptionPane.showMessageDialog(this, "Datos agregados correctamente.");
       } else {
         conn.ejecutar(
             "UPDATE datos_gasolinera "
                 + "SET "
                 + "razon_social = '"
                 + txtRazonSocial.getText()
                 + "', "
                 + "ruc = '"
                 + txtRUC.getText()
                 + "', "
                 + "nombre_comercial = '"
                 + txtNombreComercial.getText()
                 + "', "
                 + "direccion = '"
                 + txtDireccion.getText()
                 + "', "
                 + "obligado_llevar_contabilidad = '"
                 + (chkContabilidad.isSelected() ? "SI" : "NO")
                 + "', "
                 + "contribuyente_especial = '"
                 + (txtContribuyente.getText().length() == 0 ? "0" : txtContribuyente.getText())
                 + "', "
                 + "email_estacion = '"
                 + txtPropietario.getText()
                 + "', "
                 + "contraseña_mail = AES_ENCRYPT('"
                 + new String(passClaveEmail.getPassword())
                 + "', 'thekey'), "
                 + "secuencia1_factura = '"
                 + txtEstablecimiento.getText()
                 + "', "
                 + "despachadores_turno = "
                 + Integer.parseInt(txtDespachadores.getText())
                 + ", "
                 + "tipo_ambiente = '"
                 + (cbxAmbiente.getSelectedIndex() + 1)
                 + "', "
                 + "contraseña_certificado = AES_ENCRYPT('"
                 + new String(passClave.getPassword())
                 + "', 'thekey'), "
                 + "tipo_cierre_turnos = "
                 + (cbxTurno.getSelectedIndex() + 1));
         if (fileChanged) {
           conn.psPrepararSentencias =
               conn.coneccion.prepareStatement(
                   "UPDATE datos_gasolinera SET certificado_digital = ?");
           conn.psPrepararSentencias.setBinaryStream(1, stream, (int) file.length());
           conn.psPrepararSentencias.executeUpdate();
           conn.psPrepararSentencias.close();
         }
         if (pto != null && puntosEmision != cbxEmision.getItemCount())
           for (int i = 0; i < pto.length; i++) {
             String[] row = pto[i].split(",");
             try {
               conn.consulta(
                   "SELECT idpunto_emision FROM punto_emision ORDER BY idpunto_emision DESC LIMIT 1");
               conn.ejecutar(
                   "INSERT INTO punto_emision "
                       + "VALUES ("
                       + (Integer) conn.getValueAt(0, 0)
                       + ", '"
                       + row[0]
                       + "', '"
                       + row[1]
                       + "', '"
                       + row[2]
                       + "', 1)");
             } catch (SQLException | IllegalStateException ex) {
               System.err.println(ex.getMessage());
             }
           }
         javax.swing.JOptionPane.showMessageDialog(this, "Datos editados correctamente.");
       }
       dispose();
     } catch (SQLException ex) {
       System.err.println(ex.getMessage());
     }
   } else {
     javax.swing.JOptionPane.showMessageDialog(this, "Debe llenar todos los campos obligatorios.");
     txtCertificado.setForeground(java.awt.Color.RED);
   }
 } // GEN-LAST:event_btnGuardarActionPerformed