KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.*;
17 import javax.swing.border.*;
18 import javax.swing.table.*;
19 import java.awt.*;
20 import java.awt.event.*;
21 import java.sql.*;
22 import java.util.*;
23
24 import org.compiere.util.*;
25 import org.compiere.apps.*;
26 import org.compiere.minigrid.*;
27 import org.compiere.model.*;
28 import org.compiere.plaf.*;
29 import org.compiere.swing.*;
30
31 /**
32  * Create Charge from Accounts
33  *
34  * @author Jorg Janke
35  * @version $Id: VCharge.java,v 1.13 2003/07/22 18:49:15 jjanke Exp $
36  */

37 public class VCharge extends CPanel
38     implements FormPanel, ActionListener
39 {
40     /**
41      * Constructor
42      */

43     public VCharge()
44     {
45     } // VCharge
46

47     /**
48      * Initialize Panel
49      * @param WindowNo window
50      * @param frame frame
51      */

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

70
71     /** Window No */
72     private int m_WindowNo = 0;
73     /** FormFrame */
74     private FormFrame m_frame;
75
76     /** Account Element */
77     private int m_C_Element_ID = 0;
78     /** AccountSchema */
79     private int m_C_AcctSchema_ID = 0;
80     /** Default Charge Tax Category */
81     private int m_C_TaxCategory_ID = 0;
82     private int m_AD_Client_ID = 0;
83     private int m_AD_Org_ID = 0;
84     private int m_CreatedBy = 0;
85     private AcctSchema m_acctSchema = null;
86     //
87
private CPanel mainPanel = new CPanel();
88     private BorderLayout mainLayout = new BorderLayout();
89     private CPanel newPanel = new CPanel();
90     private TitledBorder newBorder;
91     private GridBagLayout newLayout = new GridBagLayout();
92     private JLabel valueLabel = new JLabel();
93     private JTextField valueField = new JTextField();
94     private JCheckBox isExpense = new JCheckBox();
95     private JLabel nameLabel = new JLabel();
96     private JTextField nameField = new JTextField();
97     private JButton newButton = new JButton();
98     private CPanel accountPanel = new CPanel();
99     private TitledBorder accountBorder;
100     private BorderLayout accountLayout = new BorderLayout();
101     private CPanel accountOKPanel = new CPanel();
102     private JButton accountButton = new JButton();
103     private FlowLayout accountOKLayout = new FlowLayout();
104     private JScrollPane dataPane = new JScrollPane();
105     private MiniTable dataTable = new MiniTable();
106     private ConfirmPanel confirmPanel = new ConfirmPanel();
107
108     /**
109      * Static Init
110      * @throws Exception
111      */

112     private void jbInit() throws Exception JavaDoc
113     {
114         CompiereColor.setBackground(this);
115         newBorder = new TitledBorder("");
116         accountBorder = new TitledBorder("");
117         mainPanel.setLayout(mainLayout);
118         newPanel.setBorder(newBorder);
119         newPanel.setLayout(newLayout);
120         newBorder.setTitle(Msg.getMsg(Env.getCtx(), "ChargeNewAccount"));
121         valueLabel.setText(Msg.translate(Env.getCtx(), "Value"));
122         isExpense.setSelected(true);
123         isExpense.setText(Msg.getMsg(Env.getCtx(), "Expense"));
124         nameLabel.setText(Msg.translate(Env.getCtx(), "Name"));
125         nameField.setColumns(20);
126         valueField.setColumns(10);
127         newButton.setText(Msg.getMsg(Env.getCtx(), "Create"));
128         newButton.addActionListener(this);
129         accountPanel.setBorder(accountBorder);
130         accountPanel.setLayout(accountLayout);
131         accountBorder.setTitle(Msg.getMsg(Env.getCtx(), "ChargeFromAccount"));
132         accountButton.setText(Msg.getMsg(Env.getCtx(), "Create"));
133         accountButton.addActionListener(this);
134         accountOKPanel.setLayout(accountOKLayout);
135         accountOKLayout.setAlignment(FlowLayout.RIGHT);
136         confirmPanel.addActionListener(this);
137         //
138
mainPanel.add(newPanel, BorderLayout.NORTH);
139         newPanel.add(valueLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
140             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
141         newPanel.add(valueField, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
142             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 0, 5, 5), 0, 0));
143         newPanel.add(nameLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
144             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
145         newPanel.add(nameField, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
146             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 0, 5, 5), 0, 0));
147         newPanel.add(isExpense, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
148             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
149         newPanel.add(newButton, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
150             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
151         mainPanel.add(accountPanel, BorderLayout.CENTER);
152         accountPanel.add(accountOKPanel, BorderLayout.SOUTH);
153         accountOKPanel.add(accountButton, null);
154         accountPanel.add(dataPane, BorderLayout.CENTER);
155         dataPane.getViewport().add(dataTable, null);
156     } // jbInit
157

158     /**
159      * Dynamic Init
160      * - Get defaults for primary AcctSchema
161      * - Create Table with Accounts
162      */

163     private void dynInit()
164     {
165         m_C_AcctSchema_ID = Env.getContextAsInt(Env.getCtx(), "$C_AcctSchema_ID");
166         // get Element
167
String JavaDoc sql = "SELECT C_Element_ID FROM C_AcctSchema_Element "
168             + "WHERE ElementType='AC' AND C_AcctSchema_ID=?";
169         try
170         {
171             PreparedStatement pstmt = DB.prepareStatement(sql);
172             pstmt.setInt(1, m_C_AcctSchema_ID);
173             ResultSet rs = pstmt.executeQuery();
174             if (rs.next())
175                 m_C_Element_ID = rs.getInt(1);
176             rs.close();
177             pstmt.close();
178         }
179         catch (SQLException e)
180         {
181             Log.error ("VCharge.dynInit-ase", e);
182         }
183         if (m_C_Element_ID == 0)
184             return;
185
186
187         // Table
188
Vector data = new Vector();
189         sql = "SELECT C_ElementValue_ID,Value, Name, AccountType "
190             + "FROM C_ElementValue "
191             + "WHERE AccountType IN ('R','E')"
192             + " AND IsSummary='N'"
193             + " AND C_Element_ID=? "
194             + "ORDER BY 2";
195         try
196         {
197             PreparedStatement pstmt = DB.prepareStatement(sql);
198             pstmt.setInt(1, m_C_Element_ID);
199             ResultSet rs = pstmt.executeQuery();
200             while (rs.next())
201             {
202                 Vector line = new Vector(4);
203                 line.add(new Boolean JavaDoc(false)); // 0-Selection
204
KeyNamePair pp = new KeyNamePair(rs.getInt(1), rs.getString(2));
205                 line.add(pp); // 1-Value
206
line.add(rs.getString(3)); // 2-Name
207
boolean isExpense = rs.getString(4).equals("E");
208                 line.add(new Boolean JavaDoc(isExpense)); // 3-Expense
209
data.add(line);
210             }
211             rs.close();
212             pstmt.close();
213         }
214         catch (SQLException e)
215         {
216             Log.error ("VCharge.dynInit-2", e);
217         }
218         // Header Info
219
Vector columnNames = new Vector(4);
220         columnNames.add(Msg.getMsg(Env.getCtx(), "Select"));
221         columnNames.add(Msg.translate(Env.getCtx(), "Value"));
222         columnNames.add(Msg.translate(Env.getCtx(), "Name"));
223         columnNames.add(Msg.getMsg(Env.getCtx(), "Expense"));
224
225         // Set Model
226
DefaultTableModel model = new DefaultTableModel(data, columnNames);
227         dataTable.setModel(model);
228         //
229
dataTable.setColumnClass(0, Boolean JavaDoc.class, false); // 0-Selection
230
dataTable.setColumnClass(1, String JavaDoc.class, true); // 1-Value
231
dataTable.setColumnClass(2, String JavaDoc.class, true); // 2-Name
232
dataTable.setColumnClass(3, Boolean JavaDoc.class, true); // 3-Expense
233
// Table UI
234
dataTable.autoSize();
235
236         // Other Defaults
237
m_AD_Client_ID = Env.getContextAsInt(Env.getCtx(), "#AD_Client_ID");
238         m_AD_Org_ID = Env.getContextAsInt(Env.getCtx(), "#AD_Org_ID");
239         m_CreatedBy = Env.getContextAsInt(Env.getCtx(), "#AD_User_ID");
240
241         // TaxCategory
242
sql = "SELECT C_TaxCategory_ID FROM C_TaxCategory "
243             + "WHERE IsDefault='Y' AND AD_Client_ID=?";
244         m_C_TaxCategory_ID = 0;
245         try
246         {
247             PreparedStatement pstmt = DB.prepareStatement(sql);
248             pstmt.setInt(1, m_AD_Client_ID);
249             ResultSet rs = pstmt.executeQuery();
250             if (rs.next())
251                 m_C_TaxCategory_ID = rs.getInt(1);
252             rs.close();
253             pstmt.close();
254         }
255         catch (SQLException e)
256         {
257             Log.error ("VCharge.dynInit-tc", e);
258         }
259     } // dynInit
260

261     /**
262      * Dispose
263      */

264     public void dispose()
265     {
266         m_frame.dispose();
267     } // dispose
268

269     /**
270      * Action Listener
271      * @param e event
272      */

273     public void actionPerformed (ActionEvent e)
274     {
275         Log.trace(Log.l1_User, "VCharge.actionPerformed - " + e.getActionCommand());
276         //
277
if (e.getActionCommand().equals(ConfirmPanel.A_OK) || m_C_Element_ID == 0)
278             dispose();
279         // new Account
280
else if (e.getSource().equals(newButton))
281             createNew();
282         else if (e.getSource().equals(accountButton))
283             createAccount();
284     } // actionPerformed
285

286     /**
287      * Create new Account and Charge
288      */

289     private void createNew()
290     {
291         Log.trace(Log.l3_Util, "VCharge.createNew");
292         // Get Input
293
String JavaDoc value = valueField.getText();
294         if (value.length() == 0)
295         {
296             valueField.setBackground(CompierePLAF.getFieldBackground_Error());
297             return;
298         }
299         String JavaDoc name = nameField.getText();
300         if (name.length() == 0)
301         {
302             nameField.setBackground(CompierePLAF.getFieldBackground_Error());
303             return;
304         }
305         // Create Element
306
int C_ElementValue_ID = create_ElementValue (value, name, isExpense.isSelected());
307         if (C_ElementValue_ID == 0)
308         {
309             ADialog.error(m_WindowNo, this, "ChargeNotCreated", name);
310             return;
311         }
312         // Create Charge
313
int C_Charge_ID = create_Charge(name, C_ElementValue_ID);
314         if (C_Charge_ID == 0)
315         {
316             ADialog.error(m_WindowNo, this, "ChargeNotCreated", name);
317             return;
318         }
319         ADialog.info(m_WindowNo, this, "ChargeCreated", name);
320     } // createNew
321

322     /**
323      * Create Charges from Accounts
324      */

325     private void createAccount()
326     {
327         Log.trace(Log.l3_Util, "VCharge.createAccount");
328         //
329
StringBuffer JavaDoc listCreated = new StringBuffer JavaDoc();
330         StringBuffer JavaDoc listRejected = new StringBuffer JavaDoc();
331         //
332
TableModel model = dataTable.getModel();
333         int rows = model.getRowCount();
334         for (int i = 0; i < rows; i++)
335         {
336             if (((Boolean JavaDoc)model.getValueAt(i, 0)).booleanValue())
337             {
338                 KeyNamePair pp = (KeyNamePair)model.getValueAt(i, 1);
339                 int C_ElementValue_ID = pp.getKey();
340                 String JavaDoc name = (String JavaDoc)model.getValueAt(i, 2);
341                 //
342
int C_Charge_ID = create_Charge(name, C_ElementValue_ID);
343                 if (C_Charge_ID == 0)
344                 {
345                     if (listRejected.length() > 0)
346                         listRejected.append(", ");
347                     listRejected.append(name);
348                 }
349                 else
350                 {
351                     if (listCreated.length() > 0)
352                         listCreated.append(", ");
353                     listCreated.append(name);
354                 }
355                 // reset selection
356
model.setValueAt(new Boolean JavaDoc(false), i, 0);
357             }
358         }
359         if (listCreated.length() > 0)
360             ADialog.info(m_WindowNo, this, "ChargeCreated", listCreated.toString());
361         if (listRejected.length() > 0)
362             ADialog.error(m_WindowNo, this, "ChargeNotCreated", listRejected.toString());
363     } // createAccount
364

365     /*************************************************************************/
366
367     /**
368      * Create ElementValue for primary AcctSchema
369      * @param value value
370      * @param name name
371      * @param isExpense is expense
372      * @return element value
373      */

374     private int create_ElementValue (String JavaDoc value, String JavaDoc name, boolean isExpense)
375     {
376         Log.trace(Log.l4_Data, "VCharge.create_ElementValue - " + name);
377         //
378
int C_ElementValue_ID = DB.getKeyNextNo(Env.getCtx(), m_WindowNo, "C_ElementValue");
379         StringBuffer JavaDoc sql = new StringBuffer JavaDoc("INSERT INTO C_ElementValue "
380             + "(C_ElementValue_ID, C_Element_ID,"
381             + " AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,"
382             + " Value, Name, Description,"
383             + " AccountType, AccountSign,"
384             + " IsDocControlled, IsSummary,"
385             + " ValidFrom, ValidTo,"
386             + " PostActual, PostBudget, PostEncumbrance, PostStatistical,"
387             + " IsBankAccount, C_BankAccount_ID, IsForeignCurrency, C_Currency_ID"
388             + ") VALUES (");
389         // C_ElementValue_ID, C_Element_ID,
390
sql.append(C_ElementValue_ID).append(",").append(m_C_Element_ID).append(",");
391         // AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,
392
sql.append(m_AD_Client_ID).append(",").append(m_AD_Org_ID);
393         sql.append(",'Y',SysDate,").append(m_CreatedBy).append(",SysDate,").append(m_CreatedBy).append(",");
394         // Value, Name, Description,
395
sql.append("'").append(value).append("','").append(name).append("',NULL,");
396         // AccountType, AccountSign,
397
sql.append(isExpense ? "'E'" : "'R'").append(",'N',");
398         // IsDocControlled, IsSummary,
399
sql.append("'N','N',");
400         // ValidFrom, ValidTo,
401
sql.append("TO_DATE('01-JAN-1970'),NULL,");
402         // PostActual, PostBudget, PostEncumbrance, PostStatistical,
403
sql.append("'Y','Y','Y','Y',");
404         // IsBankAccount, C_BankAccount_ID, IsForeignCurrency, C_Currency_ID
405
sql.append("'N',NULL,'N',NULL)");
406         //
407
int no = DB.executeUpdate(sql.toString());
408         if (no != 1)
409         {
410             Log.error("VCharge.create_ElementValue #" + no + "\n" + sql.toString());
411             C_ElementValue_ID = 0;
412         }
413         else
414             Log.trace(Log.l4_Data, "VCharge.create_ElementValue - C_ElementValue_ID="
415                 + C_ElementValue_ID + " " + value + "=" + name);
416         return C_ElementValue_ID;
417     } // create_ElementValue
418

419     /**
420      * Create Charge and account entries for primary AcctSchema
421      *
422      * @param name name
423      * @param C_ElementValue_ID element value
424      * @return charge
425      */

426     private int create_Charge (String JavaDoc name, int C_ElementValue_ID)
427     {
428         Log.trace(Log.l4_Data, "VCharge.create_ElementCharge - " + name);
429         //
430
int C_Charge_ID = DB.getKeyNextNo(Env.getCtx(), m_WindowNo, "C_Charge");
431         StringBuffer JavaDoc sql = new StringBuffer JavaDoc("INSERT INTO C_Charge "
432             + "(C_Charge_ID,"
433             + " AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,"
434             + " Name, Description,"
435             + " ChargeAmt, IsSameTax, C_TaxCategory_ID, IsSameCurrency"
436             + ") VALUES (");
437         // C_Charge_ID,
438
sql.append(C_Charge_ID).append(",");
439         // AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,
440
sql.append(m_AD_Client_ID).append(",").append(m_AD_Org_ID);
441         sql.append(",'Y',SysDate,").append(m_CreatedBy).append(",SysDate,").append(m_CreatedBy).append(",");
442         // Name, Description,
443
sql.append("'").append(name).append("',NULL,");
444         // ChargeAmt, IsSameTax, C_TaxCategory_ID, IsSameCurrency
445
sql.append("0,'Y',").append(m_C_TaxCategory_ID).append(",'Y')");
446         //
447
int no = DB.executeUpdate(sql.toString());
448         if (no != 1)
449         {
450             Log.error("VCharge.create_Charge #" + no + "\n" + sql.toString());
451             return 0;
452         }
453
454         // Get Combination
455
int C_ValidCombination_ID = create_ValidCombination(C_ElementValue_ID);
456         if (C_ValidCombination_ID == 0)
457             return 0;
458
459         // Update Accounts
460
sql = new StringBuffer JavaDoc("UPDATE C_Charge_Acct ");
461         sql.append("SET CH_Expense_Acct=").append(C_ValidCombination_ID);
462         sql.append(", CH_Revenue_Acct=").append(C_ValidCombination_ID);
463         sql.append(" WHERE C_Charge_ID=").append(C_Charge_ID);
464         sql.append(" AND C_AcctSchema_ID=").append(m_C_AcctSchema_ID);
465         //
466
no = DB.executeUpdate(sql.toString());
467         if (no != 1)
468             Log.error("VCharge.create_Charge Acct Update #" + no + "\n" + sql.toString());
469         //
470
return C_Charge_ID;
471     } // create_Charge
472

473     /**
474      * Get ValidCombination
475      *
476      * @param C_ElementValue_ID element value
477      * @return valid combination
478      */

479     private int create_ValidCombination (int C_ElementValue_ID)
480     {
481         Log.trace(Log.l4_Data, "VCharge.create_ValidCombination - " + C_ElementValue_ID);
482
483         // Get AcctSchame
484
if (m_acctSchema == null)
485             m_acctSchema = new AcctSchema(m_C_AcctSchema_ID);
486         if (m_acctSchema == null || m_acctSchema.getC_AcctSchema_ID() == 0)
487             return 0;
488         //
489
Account vc = Account.getDefault(m_acctSchema, true); // optional null
490
vc.setAccount_ID(C_ElementValue_ID);
491         vc.save(m_AD_Client_ID, m_CreatedBy);
492         int C_ValidCombination_ID = vc.getC_ValidCombination_ID();
493         //
494
return C_ValidCombination_ID;
495     } // create_ValidCombination
496

497 } // VCharge
498
Popular Tags