Reporting Functionality - Filter by Client
authorHarishankar <v.harishankar@gmail.com>
Thu, 2 Apr 2020 06:19:52 +0000 (11:49 +0530)
committerHarishankar <v.harishankar@gmail.com>
Thu, 2 Apr 2020 06:19:52 +0000 (11:49 +0530)
Enhanced the report functionality by adding filter by client to the
report view.

src/habeas/GenerateRaisedBillsDialog.java
src/habeas/MainFrame.java
src/habeas/NoticesReportViewDialog.form
src/habeas/NoticesReportViewDialog.java
src/habeas/Utility.java

index 0e1541d..e47de3a 100644 (file)
@@ -135,7 +135,7 @@ public class GenerateRaisedBillsDialog extends javax.swing.JDialog {
         }
         
         JFileChooser chooser = new JFileChooser ();
-        FileFilter filter = new FileNameExtensionFilter("Flat OpenDocument text", "fodt");
+        FileFilter filter = new FileNameExtensionFilter("Flat OpenDocument text (.fodt)", "fodt");
         chooser.setFileFilter(filter);
         int r = chooser.showSaveDialog(this);
         if (r == JFileChooser.APPROVE_OPTION) {
index 4c7d97b..ec3ce33 100644 (file)
@@ -192,7 +192,7 @@ public class MainFrame extends javax.swing.JFrame {
         // TODO add your handling code here:
         NoticesReportViewDialog frm = new NoticesReportViewDialog(this, false);
         String [] cols = {"Reference Number", "Description", "Bill Date", "Bill Amount", "Client"};
-        ArrayList<Object> data = Utility.getPendingPaymentBills ();
+        ArrayList<Object> data = Utility.getPendingPaymentBills (-1);
         if (data == null) {
             JOptionPane.showMessageDialog(this, ERROR_DISPLAYING_REPORT);
             return;
index 7578904..2a58dd0 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8" ?>
 
-<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JDialogFormInfo">
+<Form version="1.5" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JDialogFormInfo">
   <NonVisualComponents>
     <Container class="javax.swing.JPopupMenu" name="popMenu">
       <Properties>
@@ -26,6 +26,7 @@
   <Properties>
     <Property name="defaultCloseOperation" type="int" value="2"/>
     <Property name="title" type="java.lang.String" value="View Report"/>
+    <Property name="locationByPlatform" type="boolean" value="true"/>
   </Properties>
   <SyntheticProperties>
     <SyntheticProperty name="formSizePolicy" type="int" value="1"/>
   <Layout>
     <DimensionLayout dim="0">
       <Group type="103" groupAlignment="0" attributes="0">
+          <Component id="jScrollPane2" pref="679" max="32767" attributes="0"/>
           <Group type="102" attributes="0">
               <Component id="labelReportTitle" max="32767" attributes="0"/>
               <EmptySpace max="-2" attributes="0"/>
           </Group>
-          <Component id="jScrollPane2" pref="682" max="32767" attributes="0"/>
+          <Group type="102" attributes="0">
+              <EmptySpace max="-2" attributes="0"/>
+              <Component id="checkFilterByClient" min="-2" pref="196" max="-2" attributes="0"/>
+              <EmptySpace max="-2" attributes="0"/>
+              <Component id="comboClients" min="-2" pref="322" max="-2" attributes="0"/>
+              <EmptySpace max="-2" attributes="0"/>
+              <Component id="buttonApply" max="32767" attributes="0"/>
+          </Group>
       </Group>
     </DimensionLayout>
     <DimensionLayout dim="1">
           <Group type="102" alignment="0" attributes="0">
               <Component id="labelReportTitle" min="-2" pref="21" max="-2" attributes="0"/>
               <EmptySpace max="-2" attributes="0"/>
-              <Component id="jScrollPane2" pref="354" max="32767" attributes="0"/>
+              <Group type="103" groupAlignment="3" attributes="0">
+                  <Component id="checkFilterByClient" alignment="3" min="-2" max="-2" attributes="0"/>
+                  <Component id="comboClients" alignment="3" min="-2" max="-2" attributes="0"/>
+                  <Component id="buttonApply" alignment="3" min="-2" max="-2" attributes="0"/>
+              </Group>
+              <EmptySpace max="-2" attributes="0"/>
+              <Component id="jScrollPane2" pref="326" max="32767" attributes="0"/>
           </Group>
       </Group>
     </DimensionLayout>
         </Component>
       </SubComponents>
     </Container>
+    <Component class="javax.swing.JCheckBox" name="checkFilterByClient">
+      <Properties>
+        <Property name="text" type="java.lang.String" value="Filter by Client"/>
+      </Properties>
+    </Component>
+    <Component class="javax.swing.JComboBox" name="comboClients">
+      <Properties>
+        <Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
+          <StringArray count="0"/>
+        </Property>
+      </Properties>
+      <AuxValues>
+        <AuxValue name="JavaCodeGenerator_TypeParameters" type="java.lang.String" value="&lt;DBItem&gt;"/>
+      </AuxValues>
+    </Component>
+    <Component class="javax.swing.JButton" name="buttonApply">
+      <Properties>
+        <Property name="mnemonic" type="int" value="97"/>
+        <Property name="text" type="java.lang.String" value="Apply"/>
+        <Property name="toolTipText" type="java.lang.String" value=""/>
+      </Properties>
+      <Events>
+        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="buttonApplyActionPerformed"/>
+      </Events>
+    </Component>
   </SubComponents>
 </Form>
index ed0932d..64f5536 100644 (file)
@@ -9,6 +9,8 @@ import java.util.ArrayList;
 import javax.swing.JFileChooser;
 import javax.swing.JOptionPane;
 import javax.swing.JTable;
+import javax.swing.filechooser.FileFilter;
+import javax.swing.filechooser.FileNameExtensionFilter;
 import javax.swing.table.DefaultTableModel;
 import javax.swing.table.TableModel;
 
@@ -28,6 +30,7 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
     public NoticesReportViewDialog(java.awt.Frame parent, boolean modal) {
         super(parent, modal);
         initComponents();
+        populateClients ();
     }
 
     /**
@@ -44,6 +47,9 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
         labelReportTitle = new javax.swing.JLabel();
         jScrollPane2 = new javax.swing.JScrollPane();
         tableReport = new javax.swing.JTable();
+        checkFilterByClient = new javax.swing.JCheckBox();
+        comboClients = new javax.swing.JComboBox<>();
+        buttonApply = new javax.swing.JButton();
 
         popMenu.setLabel("Popup");
 
@@ -58,6 +64,7 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
 
         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
         setTitle("View Report");
+        setLocationByPlatform(true);
 
         labelReportTitle.setFont(new java.awt.Font("Noto Sans", 1, 15)); // NOI18N
         labelReportTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
@@ -73,21 +80,44 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
         ));
         jScrollPane2.setViewportView(tableReport);
 
+        checkFilterByClient.setText("Filter by Client");
+
+        buttonApply.setMnemonic('a');
+        buttonApply.setText("Apply");
+        buttonApply.setToolTipText("");
+        buttonApply.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                buttonApplyActionPerformed(evt);
+            }
+        });
+
         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
         getContentPane().setLayout(layout);
         layout.setHorizontalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 679, Short.MAX_VALUE)
             .addGroup(layout.createSequentialGroup()
                 .addComponent(labelReportTitle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                 .addContainerGap())
-            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE)
+            .addGroup(layout.createSequentialGroup()
+                .addContainerGap()
+                .addComponent(checkFilterByClient, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+                .addComponent(comboClients, javax.swing.GroupLayout.PREFERRED_SIZE, 322, javax.swing.GroupLayout.PREFERRED_SIZE)
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+                .addComponent(buttonApply, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
         );
         layout.setVerticalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(layout.createSequentialGroup()
                 .addComponent(labelReportTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
-                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE))
+                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+                    .addComponent(checkFilterByClient)
+                    .addComponent(comboClients, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+                    .addComponent(buttonApply))
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE))
         );
 
         pack();
@@ -96,9 +126,13 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
     private void menuExportCSVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuExportCSVActionPerformed
         // TODO add your handling code here:
         JFileChooser chooser = new JFileChooser ();
+        FileFilter filter = new FileNameExtensionFilter("Comma Separated Values (.csv)", "csv");
+        chooser.setFileFilter(filter);
         int rt = chooser.showSaveDialog(this);
         if (rt == JFileChooser.APPROVE_OPTION) {
             String filePath = chooser.getSelectedFile().getAbsolutePath();
+            if (!filePath.endsWith(".csv"))
+                filePath = filePath.concat(".csv");
             boolean ret = Utility.saveReportCSV (filePath, reportCols, reportData);
             if (ret == false) 
                 JOptionPane.showMessageDialog(this, ERROR_SAVING_REPORT);
@@ -106,6 +140,24 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
                 JOptionPane.showMessageDialog(this, REPORT_SAVED_SUCCESSFULLY);
         }
     }//GEN-LAST:event_menuExportCSVActionPerformed
+
+    private void buttonApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonApplyActionPerformed
+        // TODO add your handling code here:
+        if (! checkFilterByClient.isSelected()) {
+            reportData = Utility.getPendingPaymentBills(-1);
+            if (reportData != null)
+                populateReport();
+        } else {
+            DBItem db = (DBItem)comboClients.getSelectedItem();
+            if (db == null)
+                return;
+            int selid = db.getKey();
+            reportData = Utility.getPendingPaymentBills(selid);
+            if (reportData != null)
+                populateReport();
+        }
+        
+    }//GEN-LAST:event_buttonApplyActionPerformed
     private static final String REPORT_SAVED_SUCCESSFULLY = "Report saved successfully";
     private static final String ERROR_SAVING_REPORT = "Error saving report";
 
@@ -152,6 +204,9 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
     }
 
     // Variables declaration - do not modify//GEN-BEGIN:variables
+    private javax.swing.JButton buttonApply;
+    private javax.swing.JCheckBox checkFilterByClient;
+    private javax.swing.JComboBox<DBItem> comboClients;
     private javax.swing.JScrollPane jScrollPane2;
     private javax.swing.JLabel labelReportTitle;
     private javax.swing.JMenuItem menuExportCSV;
@@ -174,11 +229,27 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
         tableReport.setComponentPopupMenu(popMenu);
         
         labelReportTitle.setText(title);
-        for (int i = 0; i < data.size(); i += columns.length) {
-            Object[] row = new Object[columns.length];
-            for (int j = 0; j < columns.length; j ++)
-                row[j] = data.get(i+j);
+        populateReport ();
+    }
+    
+    private void populateReport () {
+        DefaultTableModel mdl = (DefaultTableModel)tableReport.getModel();
+        mdl.setRowCount(0);
+        for (int i = 0; i < reportData.size(); i += reportCols.length) {
+            Object[] row = new Object[reportCols.length];
+            for (int j = 0; j < reportCols.length; j ++)
+                row[j] = reportData.get(i+j);
             mdl.addRow (row);
+        }
+    }
+
+    private void populateClients() {
+        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)));
         }
     }
 }
index c0379a0..89d06cf 100644 (file)
@@ -503,16 +503,28 @@ public class Utility {
         
     }
 
-    static ArrayList<Object> getPendingPaymentBills() {
+    static ArrayList<Object> getPendingPaymentBills(int clientid) {
         ArrayList<Object> data = new ArrayList<>();
         try {
             Connection conn = DriverManager.getConnection(JDBC + connectionURL);
-            Statement st = conn.createStatement();
-            ResultSet rs = st.executeQuery("SELECT ReferenceNumber, Description, "
+
+            // if no client ID is specified i.e. -1 get ll
+            ResultSet rs;
+            if (clientid == -1) {
+                Statement st = conn.createStatement();                
+                rs = st.executeQuery("SELECT ReferenceNumber, Description, "
                     + "BillDate, BillAmount, ClientName"
                     + " FROM legalnotices INNER JOIN clients ON ClientId=clients.id "
                     + "WHERE BillStatus='AWAITING PAYMENT';");
-            
+            }
+            else {
+                PreparedStatement st = conn.prepareStatement("SELECT ReferenceNumber, "
+                        + "Description, BillDate, BillAmount, ClientName"
+                    + " FROM legalnotices INNER JOIN clients ON ClientId=clients.id "
+                    + "WHERE BillStatus='AWAITING PAYMENT' AND ClientId=?;");
+                st.setInt (1, clientid);
+                rs = st.executeQuery();
+            }
             DateFormat fmt = new SimpleDateFormat ("dd/MM/yyyy");
             while (rs.next()) {
                 data.add (rs.getString("ReferenceNumber"));
@@ -535,13 +547,13 @@ public class Utility {
             FileWriter f = new FileWriter (filePath);
             for (int i = 0; i < reportCols.length; i ++) {
                 f.append (escapeQuote(reportCols[i]));
-                f.append (", ");
+                f.append (",");
             }
             f.append("\n");
             for (int i = 0; i < reportData.size(); i += reportCols.length) {
                 for (int j = 0; j < reportCols.length; j ++) {
                     f.append (escapeQuote(reportData.get(i+j)));
-                    f.append (", ");
+                    f.append (",");
                 }
                 f.append ("\n");
             }