KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > grid > VPayment


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.grid;
15
16 import java.awt.*;
17 import java.awt.event.*;
18 import java.text.*;
19 import javax.swing.*;
20 import java.util.*;
21 import java.sql.*;
22 import java.math.*;
23
24 import org.compiere.apps.*;
25 import org.compiere.grid.ed.*;
26 import org.compiere.util.*;
27 import org.compiere.model.*;
28 import org.compiere.plaf.*;
29 import org.compiere.swing.*;
30
31 /**
32  * Display (and process) Payment Options.
33  * <pre>
34  * Payment Rule
35  * -B- Cash (Date) -> Cash Entry
36  * -P- Payment Term (Term)
37  * -S- Check (Routing, ..) -> Payment Entry
38  * -K- CreditCard (No) -> Payment Entry
39  * -U- ACH Transfer (Routing) -> Payment Entry
40  *
41  * When processing:
42  * - If an invoice is a S/K/U, but has no Payment Entry, it is changed to P
43  * - If an invoive is B and has no Cash Entry, it is created
44  * - An invoice is "Open" if it is "P" and no Payment
45  *
46  * Entry:
47  * - If not processed, an invoice has no Cash or Payment entry
48  * - The entry is created, during "Online" and when Saving
49  *
50  * Changes/Reversals:
51  * - existing Cash Entries are reversed and newly created
52  * - existing Payment Entries are not changed and then "hang there" and need to be allocated
53  * </pre>
54  *
55  * @author Jorg Janke
56  * @version $Id: VPayment.java,v 1.26 2003/10/31 05:29:05 jjanke Exp $
57  */

58 public class VPayment extends JDialog implements ActionListener
59 {
60     /**
61      * Constructor
62      *
63      * @param WindowNo owning window
64      * @param mTab owning tab
65      * @param button button with access information
66      */

67     public VPayment (int WindowNo, MTab mTab, VButton button)
68     {
69         super(Env.getWindow(WindowNo), Msg.getMsg(Env.getCtx(), "Payment"), true);
70         m_WindowNo = WindowNo;
71         m_isSOTrx = "Y".equals(Env.getContext(Env.getCtx(), WindowNo, "IsSOTrx"));
72         m_mTab = mTab;
73         try
74         {
75             bDateField = new VDate("DateAcct", false, false, true, DisplayType.Date, "DateAcct");
76             jbInit();
77             m_initOK = dynInit(button); // Null Pointer if order/invoice not saved yet
78
}
79         catch(Exception JavaDoc ex)
80         {
81             Log.error("VPayment", ex);
82             m_initOK = false;
83         }
84         //
85
AEnv.positionCenterWindow(Env.getWindow(WindowNo), this);
86     } // VPayment
87

88     /** Window */
89     private int m_WindowNo = 0;
90     /** Tab */
91     private MTab m_mTab;
92
93     // Data from Order/Invoice
94
private String JavaDoc m_DocStatus = null;
95     /** Start Payment Rule */
96     private String JavaDoc m_PaymentRule = "";
97     /** Start Payment Term */
98     private int m_C_PaymentTerm_ID = 0;
99     /** Start Acct Date */
100     private Timestamp m_DateAcct = null;
101     /** Start Payment */
102     private int m_C_Payment_ID = 0;
103     private MPayment m_mPayment = null;
104     private MPayment m_mPaymentOriginal = null;
105     /** Start CashBook Line */
106     private int m_C_CashLine_ID = 0;
107     private MCashBook m_mCashBook = null;
108     /** Start CreditCard */
109     private String JavaDoc m_CCType = "";
110     /** Start Bank Account */
111     private int m_C_BankAccount_ID = 0;
112     /** Start CashBook */
113     private int m_C_CashBook_ID = 0;
114
115     /** Is SOTrx */
116     private boolean m_isSOTrx = true;
117
118     /** Invoice Currency */
119     private int m_C_Currency_ID = 0;
120     private int m_AD_Client_ID = 0;
121     private int m_AD_Org_ID = 0;
122     private int m_C_BPartner_ID = 0;
123     private BigDecimal m_Amount = Env.ZERO; // Payment Amount
124
//
125
private boolean m_initOK = false;
126     /** Only allow changing Rule */
127     private boolean m_onlyRule = false;
128     private DecimalFormat m_Format = DisplayType.getNumberFormat(DisplayType.Amount);
129     private static Hashtable s_Currencies = null; // EMU Currencies
130

131     //
132
private CPanel mainPanel = new CPanel();
133     private BorderLayout mainLayout = new BorderLayout();
134     private CPanel northPanel = new CPanel();
135     private CPanel centerPanel = new CPanel();
136     private FlowLayout northLayout = new FlowLayout();
137     private CComboBox paymentCombo = new CComboBox();
138     private CLabel paymentLabel = new CLabel();
139     private CardLayout centerLayout = new CardLayout();
140     private CPanel bPanel = new CPanel();
141     private CPanel kPanel = new CPanel();
142     private GridBagLayout kLayout = new GridBagLayout();
143     private CLabel kTypeLabel = new CLabel();
144     private CComboBox kTypeCombo = new CComboBox();
145     private CLabel kNumnerLabel = new CLabel();
146     private CTextField kNumberField = new CTextField();
147     private CLabel kExpLabel = new CLabel();
148     private CTextField kExpField = new CTextField();
149     private CLabel kApprovalLabel = new CLabel();
150     private CTextField kApprovalField = new CTextField();
151     private CPanel tPanel = new CPanel();
152     private CLabel tAccountLabel = new CLabel();
153     private CComboBox tAccountCombo = new CComboBox();
154     private CPanel sPanel = new CPanel();
155     private GridBagLayout sPanelLayout = new GridBagLayout();
156     private CLabel sNumberLabel = new CLabel();
157     private CTextField sNumberField = new CTextField();
158     private CLabel sRoutingLabel = new CLabel();
159     private CTextField sRoutingField = new CTextField();
160     private CLabel sCurrencyLabel = new CLabel();
161     private CComboBox sCurrencyCombo = new CComboBox();
162     private CLabel bCurrencyLabel = new CLabel();
163     private CComboBox bCurrencyCombo = new CComboBox();
164     private CPanel pPanel = new CPanel();
165     private CLabel pTermLabel = new CLabel();
166     private CComboBox pTermCombo = new CComboBox();
167     private GridBagLayout bPanelLayout = new GridBagLayout();
168     private CLabel bAmountLabel = new CLabel();
169     private CLabel bAmountField = new CLabel();
170     private CLabel sAmountLabel = new CLabel();
171     private CLabel sAmountField = new CLabel();
172     private VDate bDateField;
173     private CLabel bDateLabel = new CLabel();
174     private ConfirmPanel confirmPanel = new ConfirmPanel(true);
175     private CTextField sCheckField = new CTextField();
176     private CLabel sCheckLabel = new CLabel();
177     private CButton kOnline = new CButton();
178     private CButton sOnline = new CButton();
179     private CComboBox sBankAccountCombo = new CComboBox();
180     private CLabel sBankAccountLabel = new CLabel();
181     private GridBagLayout pPanelLayout = new GridBagLayout();
182     private CLabel bCashBookLabel = new CLabel();
183     private CComboBox bCashBookCombo = new CComboBox();
184     private GridBagLayout tPanelLayout = new GridBagLayout();
185     private CButton tOnline = new CButton();
186     private CLabel kStatus = new CLabel();
187     private CTextField tRoutingField = new CTextField();
188     private CTextField tNumberField = new CTextField();
189     private CLabel tStatus = new CLabel();
190     private CLabel tRoutingText = new CLabel();
191     private CLabel tNumberText = new CLabel();
192     private CLabel sStatus = new CLabel();
193
194     /**
195      * Static Init
196      * @throws Exception
197      */

198     private void jbInit() throws Exception JavaDoc
199     {
200         CompiereColor.setBackground(this);
201         centerPanel.setBorder(BorderFactory.createRaisedBevelBorder());
202         getContentPane().add(mainPanel);
203         mainPanel.setLayout(mainLayout);
204         mainPanel.add(centerPanel, BorderLayout.CENTER);
205         //
206
northPanel.setLayout(northLayout);
207         paymentLabel.setText(Msg.translate(Env.getCtx(), "PaymentRule"));
208         mainPanel.add(northPanel, BorderLayout.NORTH);
209         northPanel.add(paymentLabel, null);
210         northPanel.add(paymentCombo, null);
211         //
212
centerPanel.setLayout(centerLayout);
213         //
214
kPanel.setLayout(kLayout);
215         kNumberField.setPreferredSize(new Dimension(120, 21));
216         kExpField.setPreferredSize(new Dimension(40, 21));
217         kApprovalField.setPreferredSize(new Dimension(120, 21));
218         kTypeLabel.setText(Msg.translate(Env.getCtx(), "CreditCardType"));
219         kNumnerLabel.setText(Msg.translate(Env.getCtx(), "CreditCardNumber"));
220         kExpLabel.setText(Msg.getMsg(Env.getCtx(), "Expires"));
221         kApprovalLabel.setText(Msg.translate(Env.getCtx(), "VoiceAuthCode"));
222         kOnline.setText(Msg.getMsg(Env.getCtx(), "Online"));
223         kOnline.addActionListener(this);
224         kStatus.setText(" ");
225         centerPanel.add(kPanel, "kPanel");
226         centerLayout.addLayoutComponent(kPanel, "kPanel");
227         kPanel.add(kTypeLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
228             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2, 0, 2, 0), 0, 0));
229         kPanel.add(kTypeCombo, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
230             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
231         kPanel.add(kNumnerLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
232             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2, 0, 2, 0), 0, 0));
233         kPanel.add(kNumberField, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
234             ,GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new Insets(2, 5, 2, 5), 0, 0));
235         kPanel.add(kExpLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
236             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2, 0, 2, 0), 0, 0));
237         kPanel.add(kExpField, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
238             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
239         kPanel.add(kApprovalLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
240             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 0), 0, 0));
241         kPanel.add(kApprovalField, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0
242             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
243         kPanel.add(kStatus, new GridBagConstraints(0, 4, 2, 1, 0.0, 0.0
244             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
245         kPanel.add(kOnline, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0
246             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
247         //
248
tPanel.setLayout(tPanelLayout);
249         tAccountLabel.setText(Msg.translate(Env.getCtx(), "C_BP_BankAccount_ID"));
250         tRoutingField.setColumns(8);
251         tNumberField.setColumns(10);
252         tRoutingText.setText(Msg.translate(Env.getCtx(), "RoutingNo"));
253         tNumberText.setText(Msg.translate(Env.getCtx(), "AccountNo"));
254         tOnline.setText(Msg.getMsg(Env.getCtx(), "Online"));
255         tStatus.setText(" ");
256         centerPanel.add(tPanel, "tPanel");
257         centerLayout.addLayoutComponent(tPanel, "tPanel");
258         tPanel.add(tAccountLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
259             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 0), 0, 0));
260         tPanel.add(tAccountCombo, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
261             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
262         tPanel.add(tRoutingField, new GridBagConstraints(1, 1, 2, 1, 0.0, 0.0
263             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 0, 5), 0, 0));
264         tPanel.add(tNumberField, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
265             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 0, 5), 0, 0));
266         tPanel.add(tStatus, new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0
267             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
268         tPanel.add(tRoutingText, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
269             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 0, 0), 0, 0));
270         tPanel.add(tNumberText, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
271             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 0, 0), 0, 0));
272         tPanel.add(tOnline, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0
273             ,GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
274         //
275
sPanel.setLayout(sPanelLayout);
276         sBankAccountLabel.setText(Msg.translate(Env.getCtx(), "C_BankAccount_ID"));
277         sAmountLabel.setText(Msg.getMsg(Env.getCtx(), "Amount"));
278         sAmountField.setText("");
279         sRoutingLabel.setText(Msg.translate(Env.getCtx(), "RoutingNo"));
280         sNumberLabel.setText(Msg.translate(Env.getCtx(), "AccountNo"));
281         sCheckLabel.setText(Msg.translate(Env.getCtx(), "CheckNo"));
282         sCheckField.setColumns(8);
283         sCurrencyLabel.setText(Msg.translate(Env.getCtx(), "C_Currency_ID"));
284         sNumberField.setPreferredSize(new Dimension(100, 21));
285         sRoutingField.setPreferredSize(new Dimension(70, 21));
286         sStatus.setText(" ");
287         sOnline.setText(Msg.getMsg(Env.getCtx(), "Online"));
288         centerPanel.add(sPanel, "sPanel");
289         centerLayout.addLayoutComponent(sPanel, "sPanel");
290         sPanel.add(sCurrencyLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
291             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2, 0, 2, 0), 0, 0));
292         sPanel.add(sCurrencyCombo, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
293             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
294         sPanel.add(sRoutingField, new GridBagConstraints(1, 4, 2, 1, 0.0, 0.0
295             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 2, 0), 0, 0));
296         sPanel.add(sNumberField, new GridBagConstraints(1, 5, 2, 1, 0.0, 0.0
297             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 2, 0), 0, 0));
298         sPanel.add(sCheckField, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0
299             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 5, 2, 0), 0, 0));
300         sPanel.add(sRoutingLabel, new GridBagConstraints(0, 3, 1, 2, 0.0, 0.0
301             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 2, 0), 0, 0));
302         sPanel.add(sNumberLabel, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0
303             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2, 0, 2, 0), 0, 0));
304         sPanel.add(sCheckLabel, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0
305             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2, 0, 2, 0), 0, 0));
306         sPanel.add(sBankAccountCombo, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
307             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 2, 5), 0, 0));
308         sPanel.add(sBankAccountLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
309             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 0, 2, 0), 0, 0));
310         sPanel.add(sStatus, new GridBagConstraints(0, 7, 3, 1, 0.0, 0.0
311             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
312         sPanel.add(sOnline, new GridBagConstraints(3, 6, 1, 1, 0.0, 0.0
313             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
314         sPanel.add(sAmountField, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
315             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 5, 5, 5), 0, 0));
316         sPanel.add(sAmountLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
317             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2, 0, 5, 0), 0, 0));
318         //
319
pPanel.setLayout(pPanelLayout);
320         pTermLabel.setText(Msg.translate(Env.getCtx(), "C_PaymentTerm_ID"));
321         centerPanel.add(pPanel, "pPanel");
322         centerLayout.addLayoutComponent(pPanel, "pPanel");
323         pPanel.add(pTermLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
324             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2, 5, 2, 0), 0, 0));
325         pPanel.add(pTermCombo, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
326             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
327         //
328
bCashBookLabel.setText(Msg.translate(Env.getCtx(), "C_CashBook_ID"));
329         bCurrencyLabel.setText(Msg.translate(Env.getCtx(), "C_Currency_ID"));
330         bPanel.setLayout(bPanelLayout);
331         bAmountLabel.setText(Msg.getMsg(Env.getCtx(), "Amount"));
332         bAmountField.setText("");
333         bDateLabel.setText(Msg.translate(Env.getCtx(), "DateAcct"));
334         centerLayout.addLayoutComponent(bPanel, "bPanel");
335         centerPanel.add(bPanel, "bPanel");
336         bPanel.add(bCurrencyLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
337             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2, 0, 2, 0), 0, 0));
338         bPanel.add(bCurrencyCombo, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
339             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
340         bPanel.add(bDateField, new GridBagConstraints(1, 2, 2, 1, 0.0, 0.0
341             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 2, 5), 0, 0));
342         bPanel.add(bDateLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
343             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 0, 2, 0), 0, 0));
344         bPanel.add(bCashBookLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
345             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2, 0, 2, 0), 0, 0));
346         bPanel.add(bCashBookCombo, new GridBagConstraints(1, 0, 2, 1, 0.0, 0.0
347             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
348         bPanel.add(bAmountLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
349             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 0, 2, 0), 0, 0));
350         bPanel.add(bAmountField, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0
351             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 2, 5), 0, 0));
352         //
353
mainPanel.add(confirmPanel, BorderLayout.SOUTH);
354         confirmPanel.addActionListener(this);
355     } // jbInit
356

357     /*************************************************************************/
358
359     /**
360      * Dynamic Init.
361      * B (Cash) (Currency)
362      * K (CreditCard) Type, Number, Exp, Approval
363      * L (DirectDebit) BPartner_Bank
364      * P (PaymentTerm) PaymentTerm
365      * S (Check) (Currency) CheckNo, Routing
366      *
367      * Currencies are shown, if member of EMU
368      * @param button button
369      * @return true if init OK
370      * @throws Exception
371      */

372     private boolean dynInit (VButton button) throws Exception JavaDoc
373     {
374         Log.trace(Log.l3_Util, "VPayment.dynInit");
375
376         if (m_mTab.getValue("C_BPartner_ID") == null)
377         {
378             ADialog.error(0, this, "SaveErrorRowNotFound");
379             return false;
380         }
381
382         // Is the Trx posted?
383
// String Posted = (String)m_mTab.getValue("Posted");
384
// if (Posted != null && Posted.equals("Y"))
385
// return false;
386

387         // DocStatus
388
m_DocStatus = (String JavaDoc)m_mTab.getValue("DocStatus");
389         if (m_DocStatus == null)
390             m_DocStatus = "";
391         // Is the Trx closed? Reversed / Voided / Cloased
392
if (m_DocStatus.equals("RE") || m_DocStatus.equals("VO") || m_DocStatus.equals("CL"))
393             return false;
394         // Document is not complete - allow to change the Payment Rule only
395
if (m_DocStatus.equals("CO"))
396             m_onlyRule = false;
397         else
398         {
399             m_onlyRule = true;
400             centerPanel.setVisible(false);
401         // ADialog.error(m_WindowNo, this, "PaymentCompleteDocument");
402
// return false;
403
}
404
405         // Amount
406
m_Amount = (BigDecimal)m_mTab.getValue("GrandTotal");
407         if (!m_onlyRule && m_Amount.compareTo(Env.ZERO) == 0)
408         {
409             ADialog.error(m_WindowNo, this, "PaymentZero");
410             return false;
411         }
412         bAmountField.setText(m_Format.format(m_Amount));
413         sAmountField.setText(m_Format.format(m_Amount));
414
415         /**
416          * Get Data from Grid
417          */

418         m_AD_Client_ID = ((Integer JavaDoc)m_mTab.getValue("AD_Client_ID")).intValue();
419         m_AD_Org_ID = ((Integer JavaDoc)m_mTab.getValue("AD_Org_ID")).intValue();
420         m_C_BPartner_ID = ((Integer JavaDoc)m_mTab.getValue("C_BPartner_ID")).intValue();
421         m_PaymentRule = (String JavaDoc)m_mTab.getValue("PaymentRule");
422         m_C_Currency_ID = ((Integer JavaDoc)m_mTab.getValue("C_Currency_ID")).intValue();
423         m_DateAcct = (Timestamp)m_mTab.getValue("DateAcct");
424         if (m_mTab.getValue("C_PaymentTerm_ID") != null)
425             m_C_PaymentTerm_ID = ((Integer JavaDoc)m_mTab.getValue("C_PaymentTerm_ID")).intValue();
426         // Existing Payment
427
if (m_mTab.getValue("C_Payment_ID") != null)
428         {
429             m_C_Payment_ID = ((Integer JavaDoc)m_mTab.getValue("C_Payment_ID")).intValue();
430             if (m_C_Payment_ID != 0)
431             {
432                 m_mPayment = new MPayment(Env.getCtx(), m_C_Payment_ID);
433                 m_mPaymentOriginal = new MPayment(Env.getCtx(), m_C_Payment_ID); // full copy
434
// CreditCard
435
m_CCType = m_mPayment.getCreditCardType();
436                 kNumberField.setText(m_mPayment.getCreditCardNumber());
437                 kExpField.setText(m_mPayment.getCreditCardExp());
438                 kApprovalField.setText(m_mPayment.getVoiceAuthCode());
439                 kStatus.setText(m_mPayment.getR_PnRef());
440                 // if approved/paid, don't let it change
441
kTypeCombo.setReadWrite(!m_mPayment.isApproved());
442                 kNumberField.setReadWrite(!m_mPayment.isApproved());
443                 kExpField.setReadWrite(!m_mPayment.isApproved());
444                 kApprovalField.setReadWrite(!m_mPayment.isApproved());
445                 kOnline.setReadWrite(!m_mPayment.isApproved());
446                 // Check
447
m_C_BankAccount_ID = m_mPayment.getC_BankAccount_ID();
448                 sRoutingField.setText(m_mPayment.getRoutingNo());
449                 sNumberField.setText(m_mPayment.getAccountNo());
450                 sCheckField.setText(m_mPayment.getCheckNo());
451                 sStatus.setText(m_mPayment.getR_PnRef());
452                 // Transfer
453
tRoutingField.setText(m_mPayment.getRoutingNo());
454                 tNumberField.setText(m_mPayment.getAccountNo());
455                 tStatus.setText(m_mPayment.getR_PnRef());
456             }
457         }
458         if (m_mPayment == null)
459         {
460             m_mPayment = new MPayment (Env.getCtx (), 0);
461             m_mPayment.setAmount (m_C_Currency_ID, m_Amount);
462         }
463
464         // Existing Cahbook entry
465
if (m_mTab.getValue("C_CashLine_ID") != null)
466         {
467             m_C_CashLine_ID = ((Integer JavaDoc)m_mTab.getValue("C_CashLine_ID")).intValue();
468             if (m_C_CashLine_ID != 0)
469                 m_mCashBook = new MCashBook(m_C_CashLine_ID);
470             if (m_mCashBook != null && m_mCashBook.getC_CashLine_ID() != 0)
471             {
472                 m_DateAcct = m_mCashBook.getStatementDate();
473                 m_C_CashBook_ID = m_mCashBook.getC_CashBook_ID();
474             }
475         }
476         else
477             m_C_CashLine_ID = 0;
478
479         // Accounting Date
480
bDateField.setValue(m_DateAcct);
481
482         if (s_Currencies == null)
483             loadCurrencies();
484
485         // Is the currency an EMU currency?
486
Integer JavaDoc C_Currency_ID = new Integer JavaDoc(m_C_Currency_ID);
487         if (s_Currencies.containsKey(C_Currency_ID))
488         {
489             Enumeration en = s_Currencies.keys();
490             while (en.hasMoreElements())
491             {
492                 Object JavaDoc key = en.nextElement();
493                 bCurrencyCombo.addItem(s_Currencies.get(key));
494                 sCurrencyCombo.addItem(s_Currencies.get(key));
495             }
496             sCurrencyCombo.addActionListener(this);
497             sCurrencyCombo.setSelectedItem(s_Currencies.get(C_Currency_ID));
498             bCurrencyCombo.addActionListener(this);
499             bCurrencyCombo.setSelectedItem(s_Currencies.get(C_Currency_ID));
500         }
501         else // No EMU Currency
502
{
503             bCurrencyLabel.setVisible(false); // Cash
504
bCurrencyCombo.setVisible(false);
505             sCurrencyLabel.setVisible(false); // Check
506
sCurrencyCombo.setVisible(false);
507         }
508
509         /**
510          * Payment Combo
511          */

512         if (m_PaymentRule == null)
513             m_PaymentRule = "";
514         ValueNamePair vp = null;
515         HashMap values = button.getValues();
516         Object JavaDoc[] a = values.keySet().toArray();
517         for (int i = 0; i < a.length; i++)
518         {
519             String JavaDoc PaymentRule = (String JavaDoc)a[i];
520             ValueNamePair pp = new ValueNamePair(PaymentRule, (String JavaDoc)values.get(a[i]));
521             paymentCombo.addItem(pp);
522             if (PaymentRule.toString().equals(m_PaymentRule)) // to select
523
vp = pp;
524         }
525
526         // Set PaymentRule
527
paymentCombo.addActionListener(this);
528         if (vp != null)
529             paymentCombo.setSelectedItem(vp);
530
531         /**
532          * Load Payment Terms
533          */

534         String JavaDoc SQL = MRole.getDefault().addAccessSQL(
535             "SELECT C_PaymentTerm_ID, Name FROM C_PaymentTerm",
536             "C_PaymentTerm", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO);
537         KeyNamePair kp = null;
538         try
539         {
540             PreparedStatement pstmt = DB.prepareStatement(SQL);
541             ResultSet rs = pstmt.executeQuery();
542             while (rs.next())
543             {
544                 int key = rs.getInt(1);
545                 String JavaDoc name = rs.getString(2);
546                 KeyNamePair pp = new KeyNamePair(key, name);
547                 pTermCombo.addItem(pp);
548                 if (key == m_C_PaymentTerm_ID)
549                     kp = pp;
550             }
551             rs.close();
552             pstmt.close();
553         }
554         catch (SQLException ept)
555         {
556             Log.error ("VPayment.dynInit -PaymentTerm-", ept);
557         }
558         // Set Selection
559
if (kp != null)
560             pTermCombo.setSelectedItem(kp);
561
562         /**
563          * Load Accounts
564          */

565         SQL = "SELECT a.C_BP_BankAccount_ID, NVL(b.Name, ' ')||a.AccountNo AS Acct "
566             + "FROM C_BP_BankAccount a,C_Bank b "
567             + "WHERE C_BPartner_ID=?";
568         kp = null;
569         try
570         {
571             PreparedStatement pstmt = DB.prepareStatement(SQL);
572             pstmt.setInt(1, m_C_BPartner_ID);
573             ResultSet rs = pstmt.executeQuery();
574             while (rs.next())
575             {
576                 int key = rs.getInt(1);
577                 String JavaDoc name = rs.getString(2);
578                 KeyNamePair pp = new KeyNamePair(key, name);
579                 tAccountCombo.addItem(pp);
580         // kp = pp;
581
}
582             rs.close();
583             pstmt.close();
584         }
585         catch (SQLException eac)
586         {
587             Log.error ("VPayment.dynInit -BPAcct-", eac);
588         }
589         // Set Selection
590
if (kp != null)
591             tAccountCombo.setSelectedItem(kp);
592
593         /**
594          * Load Credit Cards
595          */

596         ValueNamePair[] ccs = m_mPayment.getCreditCards();
597         vp = null;
598         for (int i = 0; i < ccs.length; i++)
599         {
600             kTypeCombo.addItem(ccs[i]);
601             if (ccs[i].getValue().equals(m_CCType))
602                 vp = ccs[i];
603         }
604         // Set Selection
605
if (vp != null)
606             kTypeCombo.setSelectedItem(vp);
607
608         /**
609          * Load Bank Accounts
610          */

611         SQL = MRole.getDefault().addAccessSQL(
612             "SELECT C_BankAccount_ID, Name || ' ' || AccountNo, IsDefault "
613             + "FROM C_BankAccount, C_Bank WHERE C_BankAccount.C_Bank_ID=C_Bank.C_Bank_ID",
614             "C_BankAccount", MRole.SQL_FULLYQUALIFIED, MRole.SQL_RO);
615         kp = null;
616         try
617         {
618             PreparedStatement pstmt = DB.prepareStatement(SQL);
619             ResultSet rs = pstmt.executeQuery();
620             while (rs.next())
621             {
622                 int key = rs.getInt(1);
623                 String JavaDoc name = rs.getString(2);
624                 KeyNamePair pp = new KeyNamePair(key, name);
625                 sBankAccountCombo.addItem(pp);
626                 if (key == m_C_BankAccount_ID)
627                     kp = pp;
628                 if (kp == null && rs.getString(3).equals("Y")) // Default
629
kp = pp;
630             }
631             rs.close();
632             pstmt.close();
633         }
634         catch (SQLException ept)
635         {
636             Log.error ("VPayment.dynInit -PaymentTerm-", ept);
637         }
638         // Set Selection
639
if (kp != null)
640             sBankAccountCombo.setSelectedItem(kp);
641
642
643         /**
644          * Load Cash Books
645          */

646         SQL = MRole.getDefault().addAccessSQL(
647             "SELECT C_CashBook_ID, Name, AD_Org_ID FROM C_CashBook WHERE IsActive='Y'",
648             "C_CashBook", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO);
649         kp = null;
650         try
651         {
652             PreparedStatement pstmt = DB.prepareStatement(SQL);
653             ResultSet rs = pstmt.executeQuery();
654             while (rs.next())
655             {
656                 int key = rs.getInt(1);
657                 String JavaDoc name = rs.getString(2);
658                 KeyNamePair pp = new KeyNamePair(key, name);
659                 bCashBookCombo.addItem(pp);
660                 if (key == m_C_CashBook_ID)
661                     kp = pp;
662                 if (kp == null && key == m_AD_Org_ID) // Default Org
663
kp = pp;
664             }
665             rs.close();
666             pstmt.close();
667         }
668         catch (SQLException epc)
669         {
670             Log.error ("VPayment.dynInit -CashBook-", epc);
671         }
672         // Set Selection
673
if (kp != null)
674         {
675             bCashBookCombo.setSelectedItem(kp);
676             if (m_C_CashBook_ID == 0)
677                 m_C_CashBook_ID = kp.getKey(); // set to default to avoid 'cashbook changed' message
678
}
679
680         //
681
return true;
682     } // dynInit
683

684     /**
685      * Init OK to be able to make changes?
686      * @return true if init OK
687      */

688     public boolean isInitOK()
689     {
690         return m_initOK;
691     } // isInitOK
692

693
694     /**
695      * Fill s_Currencies with EMU currencies
696      */

697     private void loadCurrencies()
698     {
699         s_Currencies = new Hashtable(12); // Currenly only 10+1
700
String JavaDoc SQL = "SELECT C_Currency_ID, ISO_Code FROM C_Currency "
701             + "WHERE (IsEMUMember='Y' AND EMUEntryDate<SysDate) OR IsEuro='Y' "
702             + "ORDER BY 2";
703         try
704         {
705             PreparedStatement pstmt = DB.prepareStatement(SQL);
706             ResultSet rs = pstmt.executeQuery();
707             while (rs.next())
708             {
709                 int id = rs.getInt(1);
710                 String JavaDoc name = rs.getString(2);
711                 s_Currencies.put(new Integer JavaDoc(id), new KeyNamePair(id, name));
712             }
713             rs.close();
714             pstmt.close();
715         }
716         catch (SQLException e)
717         {
718             Log.error("VPayment.loadCurrencies", e);
719         }
720     } // loadCurrencies
721

722
723     /*************************************************************************/
724
725     /**
726      * Action Listener
727      * @param e event
728      */

729     public void actionPerformed(ActionEvent e)
730     {
731     // Log.trace(Log.l5_DData, "VPayment.actionPerformed - " + e.getActionCommand());
732

733         // Finish
734
if (e.getActionCommand().equals(ConfirmPanel.A_OK))
735         {
736             if (checkMandatory())
737             {
738                 saveChanges (); // cannot recover
739
dispose ();
740             }
741         }
742         else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL))
743             dispose();
744
745         // Payment Method Change
746
else if (e.getSource() == paymentCombo)
747         {
748             // get selection
749
ValueNamePair pp = (ValueNamePair)paymentCombo.getSelectedItem();
750             if (pp != null)
751             {
752                 String JavaDoc s = pp.getValue().toLowerCase() + "Panel";
753                 centerLayout.show(centerPanel, s); // switch to panel
754
}
755         }
756
757         // Check Currency change
758
else if (e.getSource() == sCurrencyCombo)
759         {
760             KeyNamePair pp = (KeyNamePair)sCurrencyCombo.getSelectedItem();
761             BigDecimal amt = DB.getConvertedAmt(m_Amount, m_C_Currency_ID, pp.getKey(), m_AD_Client_ID, m_AD_Org_ID);
762             sAmountField.setText(m_Format.format(amt));
763         }
764         // Cash Currency change
765
else if (e.getSource() == bCurrencyCombo)
766         {
767             KeyNamePair pp = (KeyNamePair)bCurrencyCombo.getSelectedItem();
768             BigDecimal amt = DB.getConvertedAmt(m_Amount, m_C_Currency_ID, pp.getKey(), m_AD_Client_ID, m_AD_Org_ID);
769             bAmountField.setText(m_Format.format(amt));
770         }
771
772         // Online
773
else if (e.getSource() == kOnline || e.getSource() == sOnline || e.getSource() == tOnline)
774             processOnline();
775     } // actionPerformed
776

777
778     /*************************************************************************/
779
780     /**
781      * Save Changes
782      * @returns true, if eindow can exit
783      */

784     private boolean saveChanges()
785     {
786         Log.trace(Log.l3_Util, "VPayment.saveChanges");
787
788         ValueNamePair vp = (ValueNamePair)paymentCombo.getSelectedItem();
789         String JavaDoc PaymentRule = vp.getValue();
790         // only Payment Rule
791
if (m_onlyRule)
792         {
793             if (!PaymentRule.equals(m_PaymentRule))
794                 m_mTab.setValue("PaymentRule", PaymentRule);
795             return true;
796         }
797
798         //
799
Timestamp DateAcct = m_DateAcct;
800         int C_PaymentTerm_ID = m_C_PaymentTerm_ID;
801         int C_CashLine_ID = m_C_CashLine_ID;
802         int C_CashBook_ID = m_C_CashBook_ID;
803         String JavaDoc CCType = m_CCType;
804         //
805
int C_BankAccount_ID = 0;
806
807         // B (Cash) (Currency)
808
if (PaymentRule.equals(X_C_Order.PAYMENTRULE_Cash))
809         {
810             KeyNamePair kp = (KeyNamePair)bCashBookCombo.getSelectedItem();
811             if (kp != null)
812                 C_CashBook_ID = kp.getKey();
813             DateAcct = (Timestamp)bDateField.getValue();
814         }
815
816         // K (CreditCard) Type, Number, Exp, Approval
817
else if (PaymentRule.equals(X_C_Order.PAYMENTRULE_CreditCard))
818         {
819             vp = (ValueNamePair)kTypeCombo.getSelectedItem();
820             if (vp != null)
821                 CCType = vp.getValue();
822         }
823
824         // T (Transfer) BPartner_Bank
825
else if (PaymentRule.equals(X_C_Order.PAYMENTRULE_TransferACH))
826         {
827             tAccountCombo.getSelectedItem();
828         }
829
830         // P (PaymentTerm) PaymentTerm
831
else if (PaymentRule.equals(X_C_Order.PAYMENTRULE_OnCredit))
832         {
833             KeyNamePair kp = (KeyNamePair)pTermCombo.getSelectedItem();
834             if (kp != null)
835                 C_PaymentTerm_ID = kp.getKey();
836         }
837
838         // S (Check) (Currency) CheckNo, Routing
839
else if (PaymentRule.equals(X_C_Order.PAYMENTRULE_Check))
840         {
841         // sCurrencyCombo.getSelectedItem();
842
KeyNamePair kp = (KeyNamePair)sBankAccountCombo.getSelectedItem();
843             if (kp != null)
844                 C_BankAccount_ID = kp.getKey();
845         }
846         else
847             return false;
848
849         // find Bank Account if not qualified yet
850
if ("KTS".indexOf(PaymentRule) != -1 && C_BankAccount_ID == 0)
851         {
852             String JavaDoc tender = MPayment.TENDERTYPE_CreditCard;
853             if (PaymentRule.equals("T"))
854                 tender = MPayment.TENDERTYPE_ACH;
855             else if (PaymentRule.equals("S"))
856                 tender = MPayment.TENDERTYPE_Check;
857         }
858
859         /***********************
860          * Changed PaymentRule
861          */

862         if (!PaymentRule.equals(m_PaymentRule))
863         {
864             // We had a CashBook Entry
865
if (m_PaymentRule.equals(X_C_Order.PAYMENTRULE_Cash))
866             {
867                 if (m_mCashBook != null)
868                 {
869                     if (m_mCashBook.reverse(Env.getCtx(), m_WindowNo)) // Reverse Cashbook Entry
870
Log.trace(Log.l4_Data, "CashCancelled");
871                     else
872                         ADialog.error(m_WindowNo, this, "PaymentError", "CashNotCancelled");
873                 }
874                 C_CashLine_ID = 0; // reset
875
}
876             // We had a change in Payment type (e.g. Check to CC)
877
else if ("KTS".indexOf(m_PaymentRule) != -1 && "KTS".indexOf(PaymentRule) != -1 && m_mPaymentOriginal != null)
878             {
879                 if (m_mPaymentOriginal.cancel())
880                     Log.trace(Log.l1_User, "Payment Canecelled - " + m_mPaymentOriginal);
881                 else
882                     ADialog.error(m_WindowNo, this, "PaymentError", "PaymentNotCancelled " + m_mPaymentOriginal.getDocumentNo());
883                 m_mPayment.resetNew();
884             }
885             // We had a Payment and something else (e.g. Check to Cash)
886
else if ("KTS".indexOf(m_PaymentRule) != -1 && "KTS".indexOf(PaymentRule) == -1)
887             {
888                 if (m_mPaymentOriginal != null)
889                 {
890                     if (m_mPaymentOriginal.cancel()) // Cancel Payment
891
{
892                         Log.trace (Log.l4_Data, "PaymentCancelled " + m_mPayment.getDocumentNo ());
893                         m_mTab.getTableModel().dataSave(true);
894                         m_mPayment.resetNew();
895                         m_mPayment.setAmount(m_C_Currency_ID, m_Amount);
896                     }
897                     else
898                         ADialog.error(m_WindowNo, this, "PaymentError", "PaymentNotCancelled " + m_mPayment.getDocumentNo());
899                 }
900             }
901         }
902
903         // Get Order and optionally Invoice
904
int C_Order_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "C_Order_ID");
905         int C_Invoice_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "C_Invoice_ID");
906         if (C_Invoice_ID == 0 && m_DocStatus.equals("CO"))
907             C_Invoice_ID = getInvoiceID (C_Order_ID);
908
909         // Amount sign negative, if ARC (Credit Memo) or API (AP Invoice)
910
boolean negateAmt = false;
911         String JavaDoc sql = "SELECT DECODE (dt.DocBaseType, 'ARC','Y', 'API','Y', 'N'), dt.DocBaseType "
912             + "FROM C_DocType dt, C_Invoice i "
913             + "WHERE dt.C_DocType_ID = i.C_DocType_ID"
914             + " AND i.C_Invoice_ID=?";
915         try
916         {
917             PreparedStatement pstmt = DB.prepareStatement(sql);
918             pstmt.setInt(1, C_Invoice_ID);
919             ResultSet rs = pstmt.executeQuery();
920             if (rs.next())
921                 negateAmt = "Y".equals(rs.getString(1));
922             else
923                 Log.error("VPayment.saveChanges - NegateAmount not found C_Invoice_ID=" + C_Invoice_ID);
924             rs.close();
925             pstmt.close();
926         }
927         catch (SQLException e)
928         {
929             Log.error("VPayment.saveChanges - NegateAmount", e);
930         }
931         BigDecimal payAmount = m_Amount;
932         if (negateAmt)
933             payAmount = m_Amount.negate();
934
935         // Info
936
Log.trace(Log.l4_Data, "VPayment.saveChanges",
937             "C_Order_ID=" + C_Order_ID + ", C_Invoice_ID=" + C_Invoice_ID + ", NegateAmt=" + negateAmt);
938
939         /***********************
940          * CashBook
941          */

942         if (PaymentRule.equals(X_C_Order.PAYMENTRULE_Cash))
943         {
944             String JavaDoc description = (String JavaDoc)m_mTab.getValue("DocumentNo");
945
946             // Create new
947
if (m_mCashBook == null)
948             {
949                 if (C_Invoice_ID == 0)
950                 {
951                     Log.trace(Log.l4_Data, "VPayment.saveChanges - No Invoice!");
952                     ADialog.error(m_WindowNo, this, "PaymentError", "CashNotCreated");
953                 }
954                 else
955                 {
956                     Log.trace(Log.l4_Data, "VPayment.saveChanges - New CashBook");
957                     C_CashLine_ID = MCashBook.createCashBookEntry(Env.getCtx(), m_WindowNo,
958                         m_AD_Client_ID, m_AD_Org_ID, DateAcct, C_Invoice_ID,
959                         payAmount, description, C_CashBook_ID);
960                     if (C_CashLine_ID != 0)
961                         Log.trace(Log.l4_Data, "VPayment.saveChanges - CashCreated");
962                     else
963                         ADialog.error(m_WindowNo, this, "PaymentError", "CashNotCreated");
964                 }
965             }
966             // Changed Cash Book or Date
967
else if (C_CashBook_ID != m_C_CashBook_ID || !m_mCashBook.isSameDate(DateAcct))
968             {
969                 Log.trace(Log.l4_Data, "VPayment.saveChanges - Changed CashBook/Date");
970                 boolean reversal = m_mCashBook.reverse(Env.getCtx(), m_WindowNo);
971                 C_CashLine_ID = MCashBook.createCashBookEntry(Env.getCtx(), m_WindowNo,
972                     m_AD_Client_ID, m_AD_Org_ID, DateAcct, C_Invoice_ID,
973                     payAmount, description, C_CashBook_ID);
974                 if (reversal && C_CashLine_ID != 0)
975                     Log.trace(Log.l4_Data, "VPayment.saveChanges - CashChanged");
976                 else
977                     ADialog.error(m_WindowNo, this, "PaymentError", "CashNotChanged");
978             }
979             // Changed Amount
980
else if (payAmount.compareTo(m_mCashBook.getAmount()) != 0)
981             {
982                 Log.trace(Log.l4_Data, "VPayment.saveChanges - Changed CashBook Amount");
983                 m_mCashBook.setAmount(payAmount);
984                 m_mCashBook.update();
985                 Log.trace(Log.l4_Data, "VPayment.saveChanges - CashChanged");
986             }
987             else
988                 Log.trace(Log.l4_Data, "VPayment.saveChanges - No Change");
989         }
990         /***********************
991          * Payments
992          */

993         if ("KTS".indexOf(PaymentRule) != -1)
994         {
995             // Set Amount
996
m_mPayment.setAmount(m_C_Currency_ID, payAmount);
997             if (PaymentRule.equals(MOrder.PAYMENTRULE_CreditCard))
998             {
999                 m_mPayment.setCreditCard(MPayment.TRXTYPE_Sales, CCType,
1000                    kNumberField.getText(), "", kExpField.getText());
1001                m_mPayment.setPaymentProcessor();
1002            }
1003            else if (PaymentRule.equals(MOrder.PAYMENTRULE_TransferACH))
1004            {
1005                m_mPayment.setBankACH(C_BankAccount_ID, m_isSOTrx, tRoutingField.getText(),
1006                    tNumberField.getText());
1007            }
1008            else if (PaymentRule.equals(MOrder.PAYMENTRULE_Check))
1009            {
1010                m_mPayment.setBankCheck(C_BankAccount_ID, m_isSOTrx, sRoutingField.getText(),
1011                    sNumberField.getText(), sCheckField.getText());
1012            }
1013            m_mPayment.setC_BPartner_ID(m_C_BPartner_ID);
1014            m_mPayment.setC_Invoice_ID(C_Invoice_ID);
1015            m_mPayment.setDateTrx(m_DateAcct);
1016            // Save/Post
1017
if (m_mPayment.save() && m_mPayment.post())
1018                ADialog.info(m_WindowNo, this, "PaymentCreated", m_mPayment.getDocumentNo());
1019            else
1020                ADialog.error(m_WindowNo, this, "PaymentError", "PaymentNotCreated");
1021        }
1022
1023
1024        /**********************
1025         * Save Values to mTab
1026         */

1027        Log.trace(Log.l4_Data, "VPayment.saveChanges", "Saving changes");
1028        if (!PaymentRule.equals(m_PaymentRule))
1029            m_mTab.setValue("PaymentRule", PaymentRule);
1030        //
1031
if (!DateAcct.equals(m_DateAcct))
1032            m_mTab.setValue("DateAcct", DateAcct);
1033        //
1034
if (C_PaymentTerm_ID != m_C_PaymentTerm_ID)
1035            m_mTab.setValue("C_PaymentTerm_ID", new Integer JavaDoc(C_PaymentTerm_ID));
1036        //
1037
if (m_mPayment.getC_Payment_ID() != m_C_Payment_ID)
1038        {
1039            if (m_mPayment.getC_Payment_ID() == 0)
1040                m_mTab.setValue("C_Payment_ID", null);
1041            else
1042                m_mTab.setValue("C_Payment_ID", new Integer JavaDoc(m_mPayment.getC_Payment_ID()));
1043        }
1044        //
1045
if (C_CashLine_ID != m_C_CashLine_ID)
1046        {
1047            if (C_CashLine_ID == 0)
1048                m_mTab.setValue("C_CashLine_ID", null);
1049            else
1050                m_mTab.setValue("C_CashLine_ID", new Integer JavaDoc(C_CashLine_ID));
1051        }
1052        //
1053
return true;
1054    } // saveChanges
1055

1056    /**
1057     * Check Mandatory
1058     * @return true if all mandatory items are OK
1059     */

1060    private boolean checkMandatory()
1061    {
1062        Log.trace(Log.l4_Data, "VPayment.checkMandatory");
1063
1064        ValueNamePair vp = (ValueNamePair)paymentCombo.getSelectedItem();
1065        String JavaDoc PaymentRule = vp.getValue();
1066        // only Payment Rule
1067
if (m_onlyRule)
1068            return true;
1069
1070        Timestamp DateAcct = m_DateAcct;
1071        int C_PaymentTerm_ID = m_C_PaymentTerm_ID;
1072        int C_CashLine_ID = m_C_CashLine_ID;
1073        int C_CashBook_ID = m_C_CashBook_ID;
1074        String JavaDoc CCType = m_CCType;
1075        //
1076
int C_BankAccount_ID = 0;
1077
1078        /***********************
1079         * Mandatory Data Check
1080         */

1081        boolean dataOK = true;
1082        // B (Cash) (Currency)
1083
if (PaymentRule.equals(MOrder.PAYMENTRULE_Cash))
1084        {
1085            KeyNamePair kp = (KeyNamePair)bCashBookCombo.getSelectedItem();
1086            if (kp != null)
1087                C_CashBook_ID = kp.getKey();
1088            DateAcct = (Timestamp)bDateField.getValue();
1089        }
1090
1091        // K (CreditCard) Type, Number, Exp, Approval
1092
else if (PaymentRule.equals(MOrder.PAYMENTRULE_CreditCard))
1093        {
1094            vp = (ValueNamePair)kTypeCombo.getSelectedItem();
1095            if (vp != null)
1096                CCType = vp.getValue();
1097            //
1098
String JavaDoc error = MPayment.validateCreditCardNumber(kNumberField.getText(), CCType);
1099            if (error.length() != 0)
1100            {
1101                kNumberField.setBackground(CompierePLAF.getFieldBackground_Error());
1102                if (error.indexOf("?") == -1)
1103                {
1104                    ADialog.error(m_WindowNo, this, error);
1105                    dataOK = false;
1106                }
1107                else // warning
1108
{
1109                    if (!ADialog.ask(m_WindowNo, this, error))
1110                        dataOK = false;
1111                }
1112            }
1113            error = MPayment.validateCreditCardExp(kExpField.getText());
1114            if(error.length() != 0)
1115            {
1116                kExpField.setBackground(CompierePLAF.getFieldBackground_Error());
1117                ADialog.error(m_WindowNo, this, error);
1118                dataOK = false;
1119            }
1120        }
1121
1122        // T (Transfer) BPartner_Bank
1123
else if (PaymentRule.equals(MOrder.PAYMENTRULE_TransferACH))
1124        {
1125            tAccountCombo.getSelectedItem();
1126            String JavaDoc error = MPayment.validateRoutingNo(tRoutingField.getText());
1127            if (error.length() != 0)
1128            {
1129                tRoutingField.setBackground(CompierePLAF.getFieldBackground_Error());
1130                ADialog.error(m_WindowNo, this, error);
1131                dataOK = false;
1132            }
1133            error = MPayment.validateAccountNo(tNumberField.getText());
1134            if (error.length() != 0)
1135            {
1136                tNumberField.setBackground(CompierePLAF.getFieldBackground_Error());
1137                ADialog.error(m_WindowNo, this, error);
1138                dataOK = false;
1139            }
1140        }
1141
1142        // P (PaymentTerm) PaymentTerm
1143
else if (PaymentRule.equals(MOrder.PAYMENTRULE_OnCredit))
1144        {
1145            KeyNamePair kp = (KeyNamePair)pTermCombo.getSelectedItem();
1146            if (kp != null)
1147                C_PaymentTerm_ID = kp.getKey();
1148        }
1149
1150        // S (Check) (Currency) CheckNo, Routing
1151
else if (PaymentRule.equals(MOrder.PAYMENTRULE_Check))
1152        {
1153        // sCurrencyCombo.getSelectedItem();
1154
KeyNamePair kp = (KeyNamePair)sBankAccountCombo.getSelectedItem();
1155            if (kp != null)
1156                C_BankAccount_ID = kp.getKey();
1157            String JavaDoc error = MPayment.validateRoutingNo(sRoutingField.getText());
1158            if (error.length() != 0)
1159            {
1160                sRoutingField.setBackground(CompierePLAF.getFieldBackground_Error());
1161                ADialog.error(m_WindowNo, this, error);
1162                dataOK = false;
1163            }
1164            error = MPayment.validateAccountNo(sNumberField.getText());
1165            if (error.length() != 0)
1166            {
1167                sNumberField.setBackground(CompierePLAF.getFieldBackground_Error());
1168                ADialog.error(m_WindowNo, this, error);
1169                dataOK = false;
1170            }
1171            error = MPayment.validateCheckNo(sCheckField.getText());
1172            if (error.length() != 0)
1173            {
1174                sCheckField.setBackground(CompierePLAF.getFieldBackground_Error());
1175                ADialog.error(m_WindowNo, this, error);
1176                dataOK = false;
1177            }
1178        }
1179        else
1180        {
1181            Log.error("VPayment.checkMandatory - Unknown PaymentRule " + PaymentRule);
1182            return false;
1183        }
1184
1185        // find Bank Account if not qualified yet
1186
if ("KTS".indexOf(PaymentRule) != -1 && C_BankAccount_ID == 0)
1187        {
1188            String JavaDoc tender = MPayment.TENDERTYPE_CreditCard;
1189            if (PaymentRule.equals("T"))
1190                tender = MPayment.TENDERTYPE_ACH;
1191            else if (PaymentRule.equals("S"))
1192                tender = MPayment.TENDERTYPE_Check;
1193            // ACH & Check must have a bank account
1194
if (C_BankAccount_ID == 0 && "TS".indexOf(PaymentRule) != -1)
1195            {
1196                ADialog.error(m_WindowNo, this, "PaymentNoProcessor");
1197                dataOK = false;
1198            }
1199        }
1200        //
1201
Log.trace(Log.l3_Util, "VPayment.checkMandatory - OK=" + dataOK);
1202        return dataOK;
1203    } // checkMandatory
1204

1205    /**
1206     * Get Invoice ID for Order
1207     * @param C_Order_ID order
1208     * @returns C_Invoice_ID or 0 if not found
1209     */

1210    private static int getInvoiceID (int C_Order_ID)
1211    {
1212        int retValue = 0;
1213        String JavaDoc sql = "SELECT C_Invoice_ID FROM C_Invoice WHERE C_Order_ID=? "
1214            + "ORDER BY C_Invoice_ID DESC"; // last invoice
1215
try
1216        {
1217            PreparedStatement pstmt = DB.prepareStatement(sql);
1218            pstmt.setInt(1, C_Order_ID);
1219            ResultSet rs = pstmt.executeQuery();
1220            if (rs.next())
1221                retValue = rs.getInt(1);
1222            rs.close();
1223            pstmt.close();
1224        }
1225        catch (SQLException e)
1226        {
1227            Log.error ("VPayment.getInvoiceID", e);
1228        }
1229        return retValue;
1230    } // getInvoiceID
1231

1232    /*************************************************************************/
1233
1234    /**
1235     * Process Online (sales only) - if approved - exit
1236     */

1237    private void processOnline()
1238    {
1239        Log.trace(Log.l3_Util, "VPayment.processOnline");
1240        if (!checkMandatory())
1241            return;
1242
1243        boolean approved = false;
1244        String JavaDoc info = "";
1245        //
1246
ValueNamePair vp = (ValueNamePair)paymentCombo.getSelectedItem();
1247        String JavaDoc PaymentRule = vp.getValue();
1248
1249        // -- CreditCard
1250
if (PaymentRule.equals(X_C_Order.PAYMENTRULE_CreditCard))
1251        {
1252            vp = (ValueNamePair)kTypeCombo.getSelectedItem();
1253            String JavaDoc CCType = vp.getValue();
1254
1255            m_mPayment.setCreditCard(MPayment.TRXTYPE_Sales, CCType,
1256                kNumberField.getText(), "", kExpField.getText());
1257            m_mPayment.setAmount(m_C_Currency_ID, m_Amount);
1258            m_mPayment.setPaymentProcessor();
1259            m_mPayment.setC_BPartner_ID(m_C_BPartner_ID);
1260            //
1261
int C_Invoice_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "C_Invoice_ID");
1262            if (C_Invoice_ID == 0 && m_DocStatus.equals("CO"))
1263            {
1264                int C_Order_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "C_Order_ID");
1265                C_Invoice_ID = getInvoiceID (C_Order_ID);
1266            }
1267            m_mPayment.setC_Invoice_ID(C_Invoice_ID);
1268            m_mPayment.setDateTrx(m_DateAcct);
1269            // Set Amount
1270
m_mPayment.setAmount(m_C_Currency_ID, m_Amount);
1271
1272            approved = m_mPayment.processOnline();
1273            info = m_mPayment.getR_RespMsg() + " (" + m_mPayment.getR_AuthCode()
1274                + ") ID=" + m_mPayment.getR_PnRef();
1275            boolean saved = m_mPayment.save();
1276
1277            if (approved)
1278            {
1279                if (m_mPayment.post())
1280                    ADialog.info(m_WindowNo, this, "PaymentProcessed", info + "\n" + m_mPayment.getDocumentNo());
1281                else
1282                    ADialog.error(m_WindowNo, this, "PaymentError", "PaymentNotCreated");
1283                saveChanges();
1284                dispose();
1285            }
1286            else
1287            {
1288                ADialog.error(m_WindowNo, this, "PaymentNotProcessed", info);
1289            }
1290        }
1291        else
1292            ADialog.error(m_WindowNo, this, "PaymentNoProcessor");
1293    } // online
1294

1295} // VPayment
1296
Popular Tags