Reporting Functionality - Filter by Client
[habeas.git] / src / habeas / GenerateRaisedBillsDialog.java
1 /*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package habeas;
7
8 import java.util.ArrayList;
9 import javax.swing.JFileChooser;
10 import javax.swing.JOptionPane;
11 import javax.swing.filechooser.FileFilter;
12 import javax.swing.filechooser.FileNameExtensionFilter;
13
14 /**
15 *
16 * @author hari
17 */
18 public class GenerateRaisedBillsDialog extends javax.swing.JDialog {
19
20 /**
21 * Creates new form GenerateRaisedBillsDialog
22 */
23 public GenerateRaisedBillsDialog(java.awt.Frame parent, boolean modal) {
24 super(parent, modal);
25 initComponents();
26 populateFields ();
27 }
28
29 /**
30 * This method is called from within the constructor to initialize the form.
31 * WARNING: Do NOT modify this code. The content of this method is always
32 * regenerated by the Form Editor.
33 */
34 @SuppressWarnings("unchecked")
35 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
36 private void initComponents() {
37
38 jLabel1 = new javax.swing.JLabel();
39 comboClients = new javax.swing.JComboBox<>();
40 buttonGenerate = new javax.swing.JButton();
41 buttonClose = new javax.swing.JButton();
42 buttonMarkAsAwaiting = new javax.swing.JButton();
43
44 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
45 setTitle("Generate Raised Bills");
46 setLocationByPlatform(true);
47
48 jLabel1.setText("For client");
49
50 buttonGenerate.setMnemonic('g');
51 buttonGenerate.setText("Generate");
52 buttonGenerate.addActionListener(new java.awt.event.ActionListener() {
53 public void actionPerformed(java.awt.event.ActionEvent evt) {
54 buttonGenerateActionPerformed(evt);
55 }
56 });
57
58 buttonClose.setMnemonic('C');
59 buttonClose.setText("Close");
60 buttonClose.addActionListener(new java.awt.event.ActionListener() {
61 public void actionPerformed(java.awt.event.ActionEvent evt) {
62 buttonCloseActionPerformed(evt);
63 }
64 });
65
66 buttonMarkAsAwaiting.setMnemonic('M');
67 buttonMarkAsAwaiting.setText("Mark Raised");
68 buttonMarkAsAwaiting.addActionListener(new java.awt.event.ActionListener() {
69 public void actionPerformed(java.awt.event.ActionEvent evt) {
70 buttonMarkAsAwaitingActionPerformed(evt);
71 }
72 });
73
74 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
75 getContentPane().setLayout(layout);
76 layout.setHorizontalGroup(
77 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
78 .addGroup(layout.createSequentialGroup()
79 .addContainerGap()
80 .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
81 .addGap(18, 18, 18)
82 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
83 .addComponent(comboClients, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
84 .addGroup(layout.createSequentialGroup()
85 .addGap(0, 0, Short.MAX_VALUE)
86 .addComponent(buttonGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
87 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
88 .addComponent(buttonMarkAsAwaiting, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
89 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
90 .addComponent(buttonClose, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
91 .addContainerGap())
92 );
93 layout.setVerticalGroup(
94 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
95 .addGroup(layout.createSequentialGroup()
96 .addGap(13, 13, 13)
97 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
98 .addComponent(jLabel1)
99 .addComponent(comboClients, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
100 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
101 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
102 .addComponent(buttonGenerate)
103 .addComponent(buttonClose)
104 .addComponent(buttonMarkAsAwaiting))
105 .addContainerGap())
106 );
107
108 pack();
109 }// </editor-fold>//GEN-END:initComponents
110
111 private void buttonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCloseActionPerformed
112 // TODO add your handling code here:
113 dispose ();
114 }//GEN-LAST:event_buttonCloseActionPerformed
115
116 private void buttonGenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonGenerateActionPerformed
117 // TODO add your handling code here:
118 DBItem selitem = (DBItem)comboClients.getSelectedItem();
119 if (selitem == null) return;
120 ArrayList<Object> client = Utility.getClientDetails(selitem.getKey());
121 if (client == null) {
122 JOptionPane.showMessageDialog(this, ERROR_CLIENT_DETAILS);
123 return;
124 } else if (client.isEmpty()) {
125 JOptionPane.showMessageDialog(this, ERROR_CLIENT_DETAILS);
126 return;
127 }
128 ArrayList<Object> bills = Utility.getRaisedBills(selitem.getKey());
129 if (bills == null) {
130 JOptionPane.showMessageDialog(this, ERROR_RAISED_BILLS);
131 return;
132 } else if (bills.isEmpty()) {
133 JOptionPane.showMessageDialog(this, ERROR_RAISED_BILLS);
134 return;
135 }
136
137 JFileChooser chooser = new JFileChooser ();
138 FileFilter filter = new FileNameExtensionFilter("Flat OpenDocument text (.fodt)", "fodt");
139 chooser.setFileFilter(filter);
140 int r = chooser.showSaveDialog(this);
141 if (r == JFileChooser.APPROVE_OPTION) {
142 String filename = chooser.getSelectedFile().getAbsolutePath();
143 if (!filename.endsWith(".fodt"))
144 filename = filename.concat(".fodt");
145
146 boolean rt = Utility.generateRaisedNoticesBill(filename, (String)client.get(0),
147 (String)client.get(1), (String)client.get(2), bills);
148 if (rt == false)
149 JOptionPane.showMessageDialog(this, ERROR_WRITING_FILE);
150 else
151 JOptionPane.showMessageDialog(this, SUCCESSFULLY_GENERATED_BILL);
152 }
153 }//GEN-LAST:event_buttonGenerateActionPerformed
154
155 private void buttonMarkAsAwaitingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonMarkAsAwaitingActionPerformed
156 // TODO add your handling code here:
157 DBItem selitem = (DBItem)comboClients.getSelectedItem();
158 if (selitem == null) return;
159 ArrayList<Object> client = Utility.getClientDetails(selitem.getKey());
160 if (client == null) {
161 JOptionPane.showMessageDialog(this, ERROR_CLIENT_DETAILS);
162 return;
163 } else if (client.isEmpty()) {
164 JOptionPane.showMessageDialog(this, ERROR_CLIENT_DETAILS);
165 return;
166 }
167
168 int conf = JOptionPane.showConfirmDialog(this, MARK_RAISED_CONFIRM);
169 if (conf == JOptionPane.YES_OPTION) {
170 boolean rt = Utility.updateNoticeBillStatus (selitem.getKey(), "RAISED",
171 "AWAITING PAYMENT");
172 if (rt == false)
173 JOptionPane.showMessageDialog(this, ERROR_UPDATING);
174 else
175 JOptionPane.showMessageDialog(this, UPDATED_SUCCESSFULLY);
176
177 }
178
179
180 }//GEN-LAST:event_buttonMarkAsAwaitingActionPerformed
181 private static final String UPDATED_SUCCESSFULLY = "Updated bill status successfully";
182 private static final String ERROR_UPDATING = "Error in updating";
183 private static final String MARK_RAISED_CONFIRM = "This will change the status of "
184 + "all raised bills for this client to 'awaiting payment' - are you sure?";
185 private static final String ERROR_WRITING_FILE = "Error writing file";
186 private static final String SUCCESSFULLY_GENERATED_BILL = "Successfully generated bill";
187 private static final String ERROR_RAISED_BILLS = "Error getting raised "
188 + "bills or no raised bills found for this client";
189 private static final String ERROR_CLIENT_DETAILS = "Error getting client details";
190
191 /**
192 * @param args the command line arguments
193 */
194 public static void main(String args[]) {
195 /* Set the Nimbus look and feel */
196 //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
197 /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
198 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
199 */
200 try {
201 for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
202 if ("Nimbus".equals(info.getName())) {
203 javax.swing.UIManager.setLookAndFeel(info.getClassName());
204 break;
205 }
206 }
207 } catch (ClassNotFoundException ex) {
208 java.util.logging.Logger.getLogger(GenerateRaisedBillsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
209 } catch (InstantiationException ex) {
210 java.util.logging.Logger.getLogger(GenerateRaisedBillsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
211 } catch (IllegalAccessException ex) {
212 java.util.logging.Logger.getLogger(GenerateRaisedBillsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
213 } catch (javax.swing.UnsupportedLookAndFeelException ex) {
214 java.util.logging.Logger.getLogger(GenerateRaisedBillsDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
215 }
216 //</editor-fold>
217
218 /* Create and display the dialog */
219 java.awt.EventQueue.invokeLater(new Runnable() {
220 public void run() {
221 GenerateRaisedBillsDialog dialog = new GenerateRaisedBillsDialog(new javax.swing.JFrame(), true);
222 dialog.addWindowListener(new java.awt.event.WindowAdapter() {
223 @Override
224 public void windowClosing(java.awt.event.WindowEvent e) {
225 System.exit(0);
226 }
227 });
228 dialog.setVisible(true);
229 }
230 });
231 }
232
233 // Variables declaration - do not modify//GEN-BEGIN:variables
234 private javax.swing.JButton buttonClose;
235 private javax.swing.JButton buttonGenerate;
236 private javax.swing.JButton buttonMarkAsAwaiting;
237 private javax.swing.JComboBox<DBItem> comboClients;
238 private javax.swing.JLabel jLabel1;
239 // End of variables declaration//GEN-END:variables
240
241 private void populateFields() {
242 comboClients.removeAllItems();;
243 ArrayList<Object> clients = Utility.getClientsNameAndId();
244 if (clients == null) return;
245 for (int i = 0; i < clients.size(); i +=2) {
246 comboClients.addItem(new DBItem ((int)clients.get(i), (String)clients.get(i+1)));
247 }
248
249 }
250 }