KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > form > VPayPrint


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.apps.form;
15
16 import java.awt.*;
17 import java.awt.event.*;
18 import java.sql.*;
19 import javax.swing.*;
20 import java.text.*;
21 import java.math.*;
22
23 import org.compiere.util.*;
24 import org.compiere.apps.*;
25 import org.compiere.grid.ed.*;
26 import org.compiere.model.*;
27 import org.compiere.print.*;
28 import org.compiere.plaf.*;
29 import org.compiere.swing.*;
30
31 /**
32  * Payment Print & Export
33  *
34  * @author Jorg Janke
35  * @version $Id: VPayPrint.java,v 1.26 2003/06/16 14:39:30 jjanke Exp $
36  */

37 public class VPayPrint extends CPanel
38     implements FormPanel, ActionListener
39 {
40     /** @todo ability to enter underpayments */
41
42     /**
43      * Constructor
44      */

45     public VPayPrint()
46     {
47     } // VPayPrint
48

49     /**
50      * Initialize Panel
51      * @param WindowNo window
52      * @param frame frame
53      */

54     public void init (int WindowNo, FormFrame frame)
55     {
56         Log.trace(Log.l1_User, "VPayPrint.init");
57         m_WindowNo = WindowNo;
58         m_frame = frame;
59         try
60         {
61             jbInit();
62             dynInit();
63             frame.getContentPane().add(centerPanel, BorderLayout.CENTER);
64             frame.getContentPane().add(southPanel, BorderLayout.SOUTH);
65         }
66         catch(Exception JavaDoc e)
67         {
68             Log.error("VPayPrint.init", e);
69         }
70     } // init
71

72     /** Window No */
73     private int m_WindowNo = 0;
74     /** FormFrame */
75     private FormFrame m_frame;
76     /** Used Bank Account */
77     private int m_C_BankAccount_ID = -1;
78
79     /** Payment Information */
80     private MPaySelectionCheck[] m_checks = null;
81
82     // Static Variables
83
private CPanel centerPanel = new CPanel();
84     private CPanel southPanel = new CPanel();
85     private FlowLayout southLayout = new FlowLayout();
86     private GridBagLayout centerLayout = new GridBagLayout();
87     private JButton bPrint = ConfirmPanel.createPrintButton(true);
88     private JButton bExport = ConfirmPanel.createExportButton(true);
89     private JButton bCancel = ConfirmPanel.createCancelButton(true);
90     private JButton bProcess = ConfirmPanel.createProcessButton(Msg.getMsg(Env.getCtx(), "VPayPrintProcess"));
91     private CLabel lPaySelect = new CLabel();
92     private CComboBox fPaySelect = new CComboBox();
93     private CLabel lBank = new CLabel();
94     private CLabel fBank = new CLabel();
95     private CLabel lPaymentRule = new CLabel();
96     private CComboBox fPaymentRule = new CComboBox();
97     private CLabel lDocumentNo = new CLabel();
98     private VNumber fDocumentNo = new VNumber();
99     private CLabel lNoPayments = new CLabel();
100     private CLabel fNoPayments = new CLabel();
101     private CLabel lBalance = new CLabel();
102     private VNumber fBalance = new VNumber();
103     private CLabel lCurrency = new CLabel();
104     private CLabel fCurrency = new CLabel();
105
106     /**
107      * Static Init
108      * @throws Exception
109      */

110     private void jbInit() throws Exception JavaDoc
111     {
112         CompiereColor.setBackground(this);
113         //
114
southPanel.setLayout(southLayout);
115         southLayout.setAlignment(FlowLayout.RIGHT);
116         centerPanel.setLayout(centerLayout);
117         //
118
bPrint.addActionListener(this);
119         bExport.addActionListener(this);
120         bCancel.addActionListener(this);
121         //
122
bProcess.setText(Msg.getMsg(Env.getCtx(), "EFT"));
123         bProcess.setEnabled(false);
124         bProcess.addActionListener(this);
125         //
126
lPaySelect.setText(Msg.translate(Env.getCtx(), "C_PaySelection_ID"));
127         fPaySelect.addActionListener(this);
128         //
129
lBank.setText(Msg.translate(Env.getCtx(), "C_BankAccount_ID"));
130         //
131
lPaymentRule.setText(Msg.translate(Env.getCtx(), "PaymentRule"));
132         fPaymentRule.addActionListener(this);
133         //
134
lDocumentNo.setText(Msg.translate(Env.getCtx(), "DocumentNo"));
135         fDocumentNo.setDisplayType(DisplayType.Integer);
136         lNoPayments.setText(Msg.getMsg(Env.getCtx(), "NoOfPayments"));
137         fNoPayments.setText("0");
138         lBalance.setText(Msg.translate(Env.getCtx(), "CurrentBalance"));
139         fBalance.setReadWrite(false);
140         fBalance.setDisplayType(DisplayType.Amount);
141         lCurrency.setText(Msg.translate(Env.getCtx(), "C_Currency_ID"));
142         //
143
southPanel.add(bCancel, null);
144         southPanel.add(bExport, null);
145         southPanel.add(bPrint, null);
146         southPanel.add(bProcess, null);
147         //
148
centerPanel.add(lPaySelect, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
149             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(12, 12, 5, 5), 0, 0));
150         centerPanel.add(fPaySelect, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
151             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(12, 0, 5, 12), 0, 0));
152         centerPanel.add(lBank, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
153             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 12, 5, 5), 0, 0));
154         centerPanel.add(fBank, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
155             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 12), 0, 0));
156         centerPanel.add(lPaymentRule, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
157             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 12, 5, 5), 0, 0));
158         centerPanel.add(fPaymentRule, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
159             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 12), 0, 0));
160         centerPanel.add(lDocumentNo, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
161             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 12, 5, 5), 0, 0));
162         centerPanel.add(fDocumentNo, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0
163             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 12), 0, 0));
164         centerPanel.add(lNoPayments, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0
165             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 12, 5, 5), 0, 0));
166         centerPanel.add(fNoPayments, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0
167             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 12), 0, 0));
168         centerPanel.add(lBalance, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
169             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 12, 5, 5), 0, 0));
170         centerPanel.add(fBalance, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0
171             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 12), 0, 0));
172         centerPanel.add(lCurrency, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
173             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 12, 12, 5), 0, 0));
174         centerPanel.add(fCurrency, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0
175             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 12, 12), 0, 0));
176     } // VPayPrint
177

178     /**
179      * Dynamic Init
180      */

181     private void dynInit()
182     {
183         Log.trace(Log.l3_Util, "VPayPrint.dynInit");
184         int AD_Client_ID = Integer.parseInt(Env.getContext(Env.getCtx(), "#AD_Client_ID"));
185
186         // Load PaySelect
187
String JavaDoc sql = "SELECT C_PaySelection_ID, Name || ' - ' || TotalAmt FROM C_PaySelection "
188             + "WHERE AD_Client_ID=? AND Processed='Y' AND IsActive='Y'"
189             + "ORDER BY PayDate DESC";
190         try
191         {
192             PreparedStatement pstmt = DB.prepareStatement(sql);
193             pstmt.setInt(1, AD_Client_ID);
194             ResultSet rs = pstmt.executeQuery();
195             //
196
while (rs.next())
197             {
198                 KeyNamePair pp = new KeyNamePair(rs.getInt(1), rs.getString(2));
199                 fPaySelect.addItem(pp);
200             }
201             rs.close();
202             pstmt.close();
203         }
204         catch (SQLException e)
205         {
206             Log.error("VPayPrint.dynInit", e);
207         }
208         if (fPaySelect.getItemCount() == 0)
209             ADialog.info(m_WindowNo, this, "VPayPrintNoRecords");
210     } // dynInit
211

212     /**
213      * Dispose
214      */

215     public void dispose()
216     {
217         m_frame.dispose();
218     } // dispose
219

220     /*************************************************************************/
221
222     /**
223      * Action Listener
224      * @param e event
225      */

226     public void actionPerformed(ActionEvent e)
227     {
228     // Log.trace(Log.l3_Util, "VPayPrint.actionPerformed" + e.toString());
229
if (e.getSource() == fPaySelect)
230             loadPaySelectInfo();
231         else if (e.getSource() == fPaymentRule)
232             loadPaymentRuleInfo();
233         //
234
else if (e.getSource() == bCancel)
235             dispose();
236         else if (e.getSource() == bExport)
237             cmd_export();
238         else if (e.getSource() == bProcess)
239             cmd_EFT();
240         else if (e.getSource() == bPrint)
241             cmd_print();
242     } // actionPerformed
243

244     /**
245      * PaySelect changed - load Bank
246      */

247     private void loadPaySelectInfo()
248     {
249         Log.trace(Log.l1_User, "VPayPrint.loadPaySelectInfo");
250         if (fPaySelect.getSelectedIndex() == -1)
251             return;
252
253         // load Banks from PaySelectLine
254
int C_PaySelection_ID = ((KeyNamePair)fPaySelect.getSelectedItem()).getKey();
255         m_C_BankAccount_ID = -1;
256         String JavaDoc sql = "SELECT ps.C_BankAccount_ID, b.Name || ' ' || ba.AccountNo," // 1..2
257
+ " c.ISO_Code, CurrentBalance " // 3..4
258
+ "FROM C_PaySelection ps"
259             + " INNER JOIN C_BankAccount ba ON (ps.C_BankAccount_ID=ba.C_BankAccount_ID)"
260             + " INNER JOIN C_Bank b ON (ba.C_Bank_ID=b.C_Bank_ID)"
261             + " INNER JOIN C_Currency c ON (ba.C_Currency_ID=c.C_Currency_ID) "
262             + "WHERE ps.C_PaySelection_ID=? AND ps.Processed='Y' AND ba.IsActive='Y'";
263         try
264         {
265             PreparedStatement pstmt = DB.prepareStatement(sql);
266             pstmt.setInt(1, C_PaySelection_ID);
267             ResultSet rs = pstmt.executeQuery();
268             if (rs.next())
269             {
270                 m_C_BankAccount_ID = rs.getInt(1);
271                 fBank.setText(rs.getString(2));
272                 fCurrency.setText(rs.getString(3));
273                 fBalance.setValue(rs.getBigDecimal(4));
274             }
275             else
276             {
277                 m_C_BankAccount_ID = -1;
278                 fBank.setText("");
279                 fCurrency.setText("");
280                 fBalance.setValue(Env.ZERO);
281                 Log.error("VPayPrint.loadPaySelectInfo - No active BankAccount for C_PaySelection_ID=" + C_PaySelection_ID);
282             }
283             rs.close();
284             pstmt.close();
285         }
286         catch (SQLException e)
287         {
288             Log.error("VPayPrint.loadPaySelectInfo", e);
289         }
290         loadPaymentRule();
291     } // loadPaySelectInfo
292

293     /**
294      * Bank changed - load PaymentRule
295      */

296     private void loadPaymentRule()
297     {
298         Log.trace(Log.l1_User, "VPayPrint.loadPaymentRule");
299         if (m_C_BankAccount_ID == -1)
300             return;
301
302         // load PaymentRule for Bank
303
int C_PaySelection_ID = ((KeyNamePair)fPaySelect.getSelectedItem()).getKey();
304         fPaymentRule.removeAllItems();
305         int AD_Reference_ID = 195; // MLookupInfo.getAD_Reference_ID("All_Payment Rule");
306
Language language = Language.getLanguage(Env.getAD_Language(Env.getCtx()));
307         MLookupInfo info = MLookupFactory.getLookup_List(language, AD_Reference_ID);
308         String JavaDoc sql = info.Query.substring(0, info.Query.indexOf(" ORDER BY"))
309             + " AND " + info.KeyColumn
310             + " IN (SELECT PaymentRule FROM C_PaySelectionCheck WHERE C_PaySelection_ID=?) "
311             + info.Query.substring(info.Query.indexOf(" ORDER BY"));
312         try
313         {
314             PreparedStatement pstmt = DB.prepareStatement(sql);
315             pstmt.setInt(1, C_PaySelection_ID);
316             ResultSet rs = pstmt.executeQuery();
317             //
318
while (rs.next())
319             {
320                 ValueNamePair pp = new ValueNamePair(rs.getString(2), rs.getString(3));
321                 fPaymentRule.addItem(pp);
322             }
323             rs.close();
324             pstmt.close();
325         }
326         catch (SQLException e)
327         {
328             Log.error("VPayPrint.loadPayBankInfo - SQL=" + sql, e);
329         }
330         if (fPaymentRule.getItemCount() == 0)
331             Log.trace(Log.l3_Util, "PaySel=" + C_PaySelection_ID + ", BAcct=" + m_C_BankAccount_ID, sql);
332         loadPaymentRuleInfo();
333     } // loadPaymentRule
334

335     /**
336      * PaymentRule changed - load DocumentNo, NoPayments,
337      * enable/disable EFT, Print
338      */

339     private void loadPaymentRuleInfo()
340     {
341         ValueNamePair pp = (ValueNamePair)fPaymentRule.getSelectedItem();
342         if (pp == null)
343             return;
344         String JavaDoc PaymentRule = pp.getValue();
345
346         Log.trace(Log.l1_User, "VPayPrint.loadPaymentRuleInfo", "PaymentRule=" + PaymentRule);
347         fNoPayments.setText(" ");
348
349         int C_PaySelection_ID = ((KeyNamePair)fPaySelect.getSelectedItem()).getKey();
350         String JavaDoc sql = "SELECT COUNT(*) "
351             + "FROM C_PaySelectionCheck "
352             + "WHERE C_PaySelection_ID=?";
353         try
354         {
355             PreparedStatement pstmt = DB.prepareStatement(sql);
356             pstmt.setInt(1, C_PaySelection_ID);
357             ResultSet rs = pstmt.executeQuery();
358             //
359
if (rs.next())
360                 fNoPayments.setText(String.valueOf(rs.getInt(1)));
361             rs.close();
362             pstmt.close();
363         }
364         catch (SQLException e)
365         {
366             Log.error("VPayPrint.loadPaymentRuleInfo", e);
367         }
368         bProcess.setEnabled(PaymentRule.equals("T"));
369
370         // DocumentNo
371
sql = "SELECT CurrentNext "
372             + "FROM C_BankAccountDoc "
373             + "WHERE C_BankAccount_ID=? AND PaymentRule=? AND IsActive='Y'";
374         try
375         {
376             PreparedStatement pstmt = DB.prepareStatement(sql);
377             pstmt.setInt(1, m_C_BankAccount_ID);
378             pstmt.setString(2, PaymentRule);
379             ResultSet rs = pstmt.executeQuery();
380             //
381
if (rs.next())
382                 fDocumentNo.setValue(new Integer JavaDoc(rs.getInt(1)));
383             else
384             {
385                 Log.error ("VPayPrint.loadPaymentRuleInfo - No active BankAccountDoc for C_BankAccount_ID="
386                     + m_C_BankAccount_ID + " AND PaymentRule=" + PaymentRule);
387                 ADialog.error (m_WindowNo, this, "VPayPrintNoDoc");
388             }
389             rs.close();
390             pstmt.close();
391         }
392         catch (SQLException e)
393         {
394             Log.error("VPayPrint.loadPaymentRuleInfo ba", e);
395         }
396     } // loadPaymentRuleInfo
397

398
399     /*************************************************************************/
400
401     /**
402      * Export payments to file
403      */

404     private void cmd_export()
405     {
406         String JavaDoc PaymentRule = ((ValueNamePair)fPaymentRule.getSelectedItem()).getValue();
407         Log.trace(Log.l1_User, "VPayPrint.cmd_export", PaymentRule);
408         if (!getChecks(PaymentRule))
409             return;
410
411         // Get File Info
412
JFileChooser fc = new JFileChooser();
413         fc.setDialogTitle(Msg.getMsg(Env.getCtx(), "Export"));
414         fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
415         fc.setMultiSelectionEnabled(false);
416         fc.setSelectedFile(new java.io.File JavaDoc("paymentExport.txt"));
417         if (fc.showSaveDialog(this) != JFileChooser.APPROVE_OPTION)
418             return;
419
420         // Create File
421
int no = MPaySelectionCheck.exportToFile(m_checks, fc.getSelectedFile());
422         ADialog.info(m_WindowNo, this, "Saved",
423             fc.getSelectedFile().getAbsolutePath() + "\n"
424             + Msg.getMsg(Env.getCtx(), "NoOfLines") + "=" + no);
425
426         if (ADialog.ask(m_WindowNo, this, "VPayPrintSuccess?"))
427         {
428             int lastDocumentNo = MPaySelectionCheck.confirmPrint (m_checks);
429             // document No not updated
430
}
431         dispose();
432     } // cmd_export
433

434     /**
435      * Create EFT payment
436      */

437     private void cmd_EFT()
438     {
439         String JavaDoc PaymentRule = ((ValueNamePair)fPaymentRule.getSelectedItem()).getValue();
440         Log.trace(Log.l1_User, "VPayPrint.cmd_EFT", PaymentRule);
441         if (!getChecks(PaymentRule))
442             return;
443         dispose();
444     } // cmd_EFT
445

446     /**
447      * Print Checks and/or Remittance
448      */

449     private void cmd_print()
450     {
451         String JavaDoc PaymentRule = ((ValueNamePair)fPaymentRule.getSelectedItem()).getValue();
452         Log.trace(Log.l1_User, "VPayPrint.cmd_print", PaymentRule);
453         if (!getChecks(PaymentRule))
454             return;
455
456
457         this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
458
459         boolean somethingPrinted = false;
460         boolean directPrint = !Ini.getPropertyBool(Ini.P_PRINTPREVIEW);
461         // for all checks
462
for (int i = 0; i < m_checks.length; i++)
463         {
464             MPaySelectionCheck check = m_checks[i];
465             // ReportCtrl will check BankAccountDoc for PrintFormat
466
boolean ok = ReportCtl.startDocumentPrint(ReportCtl.CHECK, check.getID(), directPrint);
467             if (!somethingPrinted && ok)
468                 somethingPrinted = true;
469         }
470
471         // Confirm Print and Update BankAccountDoc
472
if (somethingPrinted && ADialog.ask(m_WindowNo, this, "VPayPrintSuccess?"))
473         {
474             int lastDocumentNo = MPaySelectionCheck.confirmPrint (m_checks);
475             if (lastDocumentNo != 0)
476             {
477                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
478                 sb.append("UPDATE C_BankAccountDoc SET CurrentNext=").append(++lastDocumentNo)
479                     .append(" WHERE C_BankAccount_ID=").append(m_C_BankAccount_ID)
480                     .append(" AND PaymentRule='").append(PaymentRule).append("'");
481                 DB.executeUpdate(sb.toString());
482             }
483         } // confirm
484

485         if (ADialog.ask(m_WindowNo, this, "VPayPrintPrintRemittance"))
486         {
487             for (int i = 0; i < m_checks.length; i++)
488             {
489                 MPaySelectionCheck check = m_checks[i];
490                 ReportCtl.startDocumentPrint(ReportCtl.REMITTANCE, check.getID(), directPrint);
491             }
492         } // remittance
493

494         this.setCursor(Cursor.getDefaultCursor());
495         dispose();
496     } // cmd_print
497

498     /*************************************************************************/
499
500     /**
501      * Get Checks
502      * @param PaymentRule Payment Rule
503      * @return true if payments were created
504      */

505     private boolean getChecks(String JavaDoc PaymentRule)
506     {
507         // do we have values
508
if (fPaySelect.getSelectedIndex() == -1 || m_C_BankAccount_ID == -1
509             || fPaymentRule.getSelectedIndex() == -1 || fDocumentNo.getValue() == null)
510         {
511             ADialog.error(m_WindowNo, this, "VPayPrintNoRecords",
512                 "(" + Msg.translate(Env.getCtx(), "C_PaySelectionLine_ID") + "=0)");
513             return false;
514         }
515
516         // get data
517
int C_PaySelection_ID = ((KeyNamePair)fPaySelect.getSelectedItem()).getKey();
518         int startDocumentNo = ((BigDecimal)fDocumentNo.getValue()).intValue();
519
520         Log.trace(Log.l3_Util, "VPayPrint.createPayments",
521             "C_PaySelection_ID=" + C_PaySelection_ID + ", PaymentRule=" + PaymentRule + ", DocumentNo=" + startDocumentNo);
522         //
523
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
524
525         m_checks = MPaySelectionCheck.get(C_PaySelection_ID, PaymentRule, startDocumentNo);
526
527         this.setCursor(Cursor.getDefaultCursor());
528         //
529
if (m_checks == null || m_checks.length == 0)
530         {
531             ADialog.error(m_WindowNo, this, "VPayPrintNoRecords",
532                 "(" + Msg.translate(Env.getCtx(), "C_PaySelectionLine_ID") + " #0");
533             return false;
534         }
535         return true;
536     } // getChecks
537

538 } // PayPrint
539
Popular Tags