Generate raised bills functionality completed
[habeas.git] / src / habeas / GenerateRaisedBillsDialog.java
diff --git a/src/habeas/GenerateRaisedBillsDialog.java b/src/habeas/GenerateRaisedBillsDialog.java
new file mode 100644 (file)
index 0000000..2273353
--- /dev/null
@@ -0,0 +1,205 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package habeas;
+
+import java.util.ArrayList;
+import javax.swing.JFileChooser;
+import javax.swing.JOptionPane;
+import javax.swing.filechooser.FileFilter;
+import javax.swing.filechooser.FileNameExtensionFilter;
+
+/**
+ *
+ * @author hari
+ */
+public class GenerateRaisedBillsDialog extends javax.swing.JDialog {
+
+    /**
+     * Creates new form GenerateRaisedBillsDialog
+     */
+    public GenerateRaisedBillsDialog(java.awt.Frame parent, boolean modal) {
+        super(parent, modal);
+        initComponents();
+        populateFields ();
+    }
+
+    /**
+     * This method is called from within the constructor to initialize the form.
+     * WARNING: Do NOT modify this code. The content of this method is always
+     * regenerated by the Form Editor.
+     */
+    @SuppressWarnings("unchecked")
+    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
+    private void initComponents() {
+
+        jLabel1 = new javax.swing.JLabel();
+        comboClients = new javax.swing.JComboBox<>();
+        buttonGenerate = new javax.swing.JButton();
+        buttonClose = new javax.swing.JButton();
+
+        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
+        setTitle("Generate Raised Bills");
+        setLocationByPlatform(true);
+
+        jLabel1.setText("For client");
+
+        buttonGenerate.setMnemonic('g');
+        buttonGenerate.setText("Generate");
+        buttonGenerate.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                buttonGenerateActionPerformed(evt);
+            }
+        });
+
+        buttonClose.setMnemonic('C');
+        buttonClose.setText("Close");
+        buttonClose.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                buttonCloseActionPerformed(evt);
+            }
+        });
+
+        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
+        getContentPane().setLayout(layout);
+        layout.setHorizontalGroup(
+            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+            .addGroup(layout.createSequentialGroup()
+                .addContainerGap()
+                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
+                .addGap(18, 18, 18)
+                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+                    .addComponent(comboClients, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+                    .addGroup(layout.createSequentialGroup()
+                        .addComponent(buttonGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
+                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
+                        .addComponent(buttonClose, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
+                .addContainerGap())
+        );
+        layout.setVerticalGroup(
+            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+            .addGroup(layout.createSequentialGroup()
+                .addGap(13, 13, 13)
+                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+                    .addComponent(jLabel1)
+                    .addComponent(comboClients, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
+                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+                    .addComponent(buttonGenerate)
+                    .addComponent(buttonClose))
+                .addContainerGap())
+        );
+
+        pack();
+    }// </editor-fold>//GEN-END:initComponents
+
+    private void buttonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCloseActionPerformed
+        // TODO add your handling code here:
+        dispose ();
+    }//GEN-LAST:event_buttonCloseActionPerformed
+
+    private void buttonGenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonGenerateActionPerformed
+        // TODO add your handling code here:
+        DBItem selitem = (DBItem)comboClients.getSelectedItem();
+        if (selitem == null) return;
+        ArrayList<Object> client = Utility.getClientDetails(selitem.getKey());
+        if (client == null) {
+            JOptionPane.showMessageDialog(this, ERROR_CLIENT_DETAILS);
+            return;
+        } else if (client.isEmpty()) {
+            JOptionPane.showMessageDialog(this, ERROR_CLIENT_DETAILS);
+            return;            
+        }
+        ArrayList<Object> bills = Utility.getRaisedBills(selitem.getKey());
+        if (bills == null) {
+            JOptionPane.showMessageDialog(this, ERROR_RAISED_BILLS);
+            return;
+        } else if (bills.isEmpty()) {
+            JOptionPane.showMessageDialog(this, ERROR_RAISED_BILLS);
+            return;            
+        }
+        
+        JFileChooser chooser = new JFileChooser ();
+        FileFilter filter = new FileNameExtensionFilter("Flat OpenDocument text", "fodt");
+        chooser.setFileFilter(filter);
+        int r = chooser.showSaveDialog(this);
+        if (r == JFileChooser.APPROVE_OPTION) {
+            String filename = chooser.getSelectedFile().getAbsolutePath();
+            if (!filename.endsWith(".fodt"))
+                filename = filename.concat(".fodt");
+        
+            boolean rt = Utility.generateRaisedNoticesBill(filename, (String)client.get(0), 
+                (String)client.get(1), (String)client.get(2), bills);
+            if (rt == false) 
+                JOptionPane.showMessageDialog(this, ERROR_WRITING_FILE);
+            else
+                JOptionPane.showMessageDialog(this, SUCCESSFULLY_GENERATED_BILL);
+        }
+    }//GEN-LAST:event_buttonGenerateActionPerformed
+    private static final String ERROR_WRITING_FILE = "Error writing file";
+    private static final String SUCCESSFULLY_GENERATED_BILL = "Successfully generated bill";
+    private static final String ERROR_RAISED_BILLS = "Error getting raised "
+            + "bills or no raised bills found for this client";
+    private static final String ERROR_CLIENT_DETAILS = "Error getting client details";
+
+    /**
+     * @param args the command line arguments
+     */
+    public static void main(String args[]) {
+        /* Set the Nimbus look and feel */
+        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
+        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
+         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
+         */
+        try {
+            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
+                if ("Nimbus".equals(info.getName())) {
+                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
+                    break;
+                }
+            }
+        } catch (ClassNotFoundException ex) {
+            java.util.logging.Logger.getLogger(GenerateRaisedBillsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+        } catch (InstantiationException ex) {
+            java.util.logging.Logger.getLogger(GenerateRaisedBillsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+        } catch (IllegalAccessException ex) {
+            java.util.logging.Logger.getLogger(GenerateRaisedBillsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
+            java.util.logging.Logger.getLogger(GenerateRaisedBillsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+        }
+        //</editor-fold>
+
+        /* Create and display the dialog */
+        java.awt.EventQueue.invokeLater(new Runnable() {
+            public void run() {
+                GenerateRaisedBillsDialog dialog = new GenerateRaisedBillsDialog(new javax.swing.JFrame(), true);
+                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
+                    @Override
+                    public void windowClosing(java.awt.event.WindowEvent e) {
+                        System.exit(0);
+                    }
+                });
+                dialog.setVisible(true);
+            }
+        });
+    }
+
+    // Variables declaration - do not modify//GEN-BEGIN:variables
+    private javax.swing.JButton buttonClose;
+    private javax.swing.JButton buttonGenerate;
+    private javax.swing.JComboBox<DBItem> comboClients;
+    private javax.swing.JLabel jLabel1;
+    // End of variables declaration//GEN-END:variables
+
+    private void populateFields() {
+        comboClients.removeAllItems();;
+        ArrayList<Object> clients = Utility.getClientsNameAndId();
+        if (clients == null) return;
+        for (int i = 0; i < clients.size(); i +=2) {
+            comboClients.addItem(new DBItem ((int)clients.get(i), (String)clients.get(i+1)));
+        }
+        
+    }
+}