KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > Account


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.model;
15
16 import java.sql.*;
17 import java.util.*;
18
19 import org.compiere.util.*;
20 import java.io.*;
21
22 /**
23  * Account Object Entity to maintain all segment values
24  *
25  * @author Jorg Janke
26  * @version $Id: Account.java,v 1.10 2003/07/28 03:57:55 jjanke Exp $
27  */

28 public final class Account implements Serializable
29 {
30     /**
31      * Basic constructor
32      */

33     public Account()
34     {
35     } // Account
36

37     /**
38      * Default constructor
39      * @param C_ValidCombination_ID combination
40      */

41     public Account (int C_ValidCombination_ID)
42     {
43         if (C_ValidCombination_ID == 0)
44             return;
45         load(C_ValidCombination_ID);
46     } // Account
47

48
49     /** database field */
50     private int C_ValidCombination_ID = 0;
51     /** database field */
52     private int C_AcctSchema_ID = 0;
53     //
54
/** database field */
55     private int AD_Client_ID = 0;
56     /** database field */
57     private int AD_Org_ID = 0;
58     /** database field */
59     private int Account_ID = 0;
60     /** database field */
61     private int M_Product_ID = 0;
62     /** database field */
63     private int C_BPartner_ID = 0;
64     /** database field */
65     private int AD_OrgTrx_ID = 0;
66     /** database field */
67     private int C_LocFrom_ID = 0;
68     /** database field */
69     private int C_LocTo_ID = 0;
70     /** database field */
71     private int C_SalesRegion_ID = 0;
72     /** database field */
73     private int C_Project_ID = 0;
74     /** database field */
75     private int C_Channel_ID = 0;
76     /** database field */
77     private int C_Campaign_ID = 0;
78     /** database field */
79     private int C_Activity_ID = 0;
80     /** database field */
81     private int User1_ID = 0;
82     /** database field */
83     private int User2_ID = 0;
84     //
85
private String JavaDoc alias = "";
86     private String JavaDoc combination = "";
87     private String JavaDoc description = "";
88     private boolean active = true;
89     private int updatedBy = 0;
90     private boolean fullyQualified = false;
91
92
93     /** Account Type - Asset,.. */
94     private char m_AcctType = ' ';
95     /** Account definition valid? */
96     private Boolean JavaDoc m_IsValid = null;
97     /** Previous checked account */
98     private int m_OldAccount_ID = 0;
99
100     /** Has the current Account been changed */
101     private boolean m_changed = true;
102
103
104     /**
105      * Load Account with Combination_ID
106      * @param C_ValidCombination_ID combination
107      */

108     public void load (int C_ValidCombination_ID)
109     {
110         String JavaDoc sql = "SELECT AD_CLIENT_ID,AD_ORG_ID, ISACTIVE, UPDATED,UPDATEDBY," // 1..5
111
+ "ALIAS, COMBINATION, DESCRIPTION, ISFULLYQUALIFIED," // 1..9
112
+ "C_ACCTSCHEMA_ID, ACCOUNT_ID, M_PRODUCT_ID, C_BPARTNER_ID," // 10..13
113
+ "AD_ORGTRX_ID, C_LOCFROM_ID, C_LOCTO_ID, C_SALESREGION_ID," // 14..17
114
+ "C_PROJECT_ID, C_CAMPAIGN_ID, C_ACTIVITY_ID, USER1_ID,USER2_ID " // 18..22
115
+ "FROM C_ValidCombination "
116             + "WHERE C_ValidCombination_ID=?";
117         try
118         {
119             PreparedStatement pstmt = DB.prepareStatement(sql);
120             pstmt.setInt(1, C_ValidCombination_ID);
121             ResultSet rs = pstmt.executeQuery();
122             if (rs.next())
123             {
124                 // Minimum values
125
setAD_Client_ID(rs.getInt(1));
126                 setAD_Org_ID(rs.getInt(2));
127                 setActive(rs.getString(3).equals("Y"));
128                 // Updated
129
setUpdatedBy(rs.getInt(5));
130                 //
131
setAlias(rs.getString(6));
132                 setCombination(rs.getString(7));
133                 setDescription(rs.getString(8));
134                 setFullyQualified(rs.getString(9).equals("Y"));
135                 //
136
setC_AcctSchema_ID(rs.getInt(10));
137                 setAccount_ID(rs.getInt(11));
138                 setM_Product_ID(rs.getInt(12));
139                 setC_BPartner_ID(rs.getInt(13));
140                 //
141
setAD_OrgTrx_ID(rs.getInt(14));
142                 setC_LocFrom_ID(rs.getInt(15));
143                 setC_LocTo_ID(rs.getInt(16));
144                 setC_SalesRegion_ID(rs.getInt(17));
145                 //
146
setC_Project_ID(rs.getInt(18));
147                 setC_Campaign_ID(rs.getInt(19));
148                 setC_Activity_ID(rs.getInt(20));
149                 setUser1_ID(rs.getInt(21));
150                 setUser2_ID(rs.getInt(22));
151                 //
152
setC_ValidCombination_ID(C_ValidCombination_ID);
153                 m_changed = false;
154             }
155             else
156                 Log.error("Account.getAccount - " + C_ValidCombination_ID + " not found");
157             rs.close();
158             pstmt.close();
159         }
160         catch (SQLException e)
161         {
162             C_ValidCombination_ID = 0;
163             Log.error ("Account.load", e);
164         }
165     } // load
166

167     /**
168      * Factory: default combination
169      * @param C_AcctSchema_ID accounting schema
170      * @param optionalNull if true the optional values are null
171      * @return Account
172      */

173     public static Account getDefault (int C_AcctSchema_ID, boolean optionalNull)
174     {
175         AcctSchema acctSchema = new AcctSchema(C_AcctSchema_ID);
176         return getDefault(acctSchema, optionalNull);
177     } // getDefault
178

179     /**
180      * Factory: default combination
181      * @param acctSchema accounting schema
182      * @param optionalNull if true, the optional values are null
183      * @return Account
184      */

185     public static Account getDefault (AcctSchema acctSchema, boolean optionalNull)
186     {
187         Account vc = new Account();
188         vc.setC_AcctSchema_ID(acctSchema.getC_AcctSchema_ID());
189         // Active Elements
190
ArrayList list = acctSchema.getAcctSchemaElementList();
191         for (int i = 0; i < list.size(); i++)
192         {
193             AcctSchemaElement ase = (AcctSchemaElement)list.get(i);
194             String JavaDoc segmentType = ase.getSegmentType();
195             int defaultValue = ase.getDefaultValue();
196             boolean setValue = ase.isMandatory() || (!ase.isMandatory() && !optionalNull);
197             //
198
if (segmentType.equals(AcctSchemaElement.SEGMENT_Org))
199                 vc.setAD_Org_ID(defaultValue);
200             else if (segmentType.equals(AcctSchemaElement.SEGMENT_Account))
201                 vc.setAccount_ID(defaultValue);
202             else if (segmentType.equals(AcctSchemaElement.SEGMENT_BPartner) && setValue)
203                 vc.setC_BPartner_ID(defaultValue);
204             else if (segmentType.equals(AcctSchemaElement.SEGMENT_Product) && setValue)
205                 vc.setM_Product_ID(defaultValue);
206             else if (segmentType.equals(AcctSchemaElement.SEGMENT_Activity) && setValue)
207                 vc.setC_Activity_ID(defaultValue);
208             else if (segmentType.equals(AcctSchemaElement.SEGMENT_LocationFrom) && setValue)
209                 vc.setC_LocFrom_ID(defaultValue);
210             else if (segmentType.equals(AcctSchemaElement.SEGMENT_LocationTo) && setValue)
211                 vc.setC_LocTo_ID(defaultValue);
212             else if (segmentType.equals(AcctSchemaElement.SEGMENT_Campaign) && setValue)
213                 vc.setC_Campaign_ID(defaultValue);
214             else if (segmentType.equals(AcctSchemaElement.SEGMENT_OrgTrx) && setValue)
215                 vc.setAD_OrgTrx_ID(defaultValue);
216             else if (segmentType.equals(AcctSchemaElement.SEGMENT_Project) && setValue)
217                 vc.setC_Project_ID(defaultValue);
218             else if (segmentType.equals(AcctSchemaElement.SEGMENT_SalesRegion) && setValue)
219                 vc.setC_SalesRegion_ID(defaultValue);
220             else if (segmentType.equals(AcctSchemaElement.SEGMENT_User1) && setValue)
221                 vc.setUser1_ID(defaultValue);
222             else if (segmentType.equals(AcctSchemaElement.SEGMENT_User2) && setValue)
223                 vc.setUser2_ID(defaultValue);
224         }
225         Log.trace(Log.l3_Util, "Account.getDefault - Client_ID="
226             + vc.getAD_Client_ID() + ", Org_ID=" + vc.getAD_Org_ID(),
227             "AcctSchema_ID=" + vc.getC_AcctSchema_ID() + ", Account_ID=" + vc.getAccount_ID());
228         return vc;
229     } // getDefault
230

231     /**
232      * Factory: get Account
233      * @param C_ValidCombination_ID combination
234      * @return Account
235      */

236     public static Account getAccount (int C_ValidCombination_ID)
237     {
238         return new Account(C_ValidCombination_ID);
239     } // getAccount
240

241     /**
242      * Save
243      * @param AD_Client_ID new client
244      * @param UpdatedBy updated
245      * @return true if saved
246      */

247     public boolean save (int AD_Client_ID, int UpdatedBy)
248     {
249         setAD_Client_ID(AD_Client_ID);
250         setUpdatedBy(UpdatedBy);
251         return save();
252     } // save
253

254     /**
255      * Save and reload
256      * @return true if saved
257      */

258     public boolean save()
259     {
260         Log.trace(Log.l4_Data, "Account.save - Client_ID="
261             + AD_Client_ID + ", Org_ID=" + AD_Org_ID,
262             "AcctSchema_ID=" + C_AcctSchema_ID + ", Account_ID=" + Account_ID);
263
264         int C_ValidCombination_ID = 0;
265         boolean saved = false;
266         try
267         {
268             String JavaDoc SQL = "{CALL C_ValidCombination_Get(?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?)}";
269             CallableStatement cstmt = DB.prepareCall(SQL);
270             // 1 - ID
271
cstmt.registerOutParameter(1, Types.NUMERIC);
272
273             // -- Mandatory Accounting fields
274
// 2 - AD_Client_ID
275
cstmt.setInt(2, AD_Client_ID);
276             // 3 - AD_Org_ID
277
cstmt.setInt(3, AD_Org_ID);
278             // 4- C_AcctSchema_ID
279
cstmt.setInt(4, C_AcctSchema_ID);
280             // 5 - Account_ID
281
cstmt.setInt(5, Account_ID);
282
283             // -- Optional
284
// 6 - Base C_ValidCombination_ID
285
cstmt.setNull(6, Types.NUMERIC);
286             // 7 - MustBeFullyQualified
287
cstmt.setString(7, "Y");
288             // 8 - Alias
289
if (alias != null && alias.length() > 0)
290                 cstmt.setString(8, alias);
291             else
292                 cstmt.setNull(8, Types.VARCHAR);
293             // 9 - CreatedBy
294
cstmt.setInt(9, getUpdatedBy());
295
296             // -- Optional Accounting fields
297
// 10 - M_Product_ID
298
if (M_Product_ID != 0)
299                 cstmt.setInt(10, M_Product_ID);
300             else
301                 cstmt.setNull(10, Types.NUMERIC);
302             // 11 - C_BPartner_ID
303
if (C_BPartner_ID != 0)
304                 cstmt.setInt(11, C_BPartner_ID);
305             else
306                 cstmt.setNull(11, Types.NUMERIC);
307             // 12 - AD_OrgTrx_ID
308
if (AD_OrgTrx_ID != 0)
309                 cstmt.setInt(12, AD_OrgTrx_ID);
310             else
311                 cstmt.setNull(12, Types.NUMERIC);
312             // 13 - C_LocFrom_ID
313
if (C_LocFrom_ID != 0)
314                 cstmt.setInt(13, C_LocFrom_ID);
315             else
316                 cstmt.setNull(13, Types.NUMERIC);
317             // 14 - C_LocTo_ID
318
if (C_LocTo_ID != 0)
319                 cstmt.setInt(14, C_LocTo_ID);
320             else
321                 cstmt.setNull(14, Types.NUMERIC);
322             // 15 - C_SalesRegion_ID
323
if (C_SalesRegion_ID != 0)
324                 cstmt.setInt(15, C_SalesRegion_ID);
325             else
326                 cstmt.setNull(15, Types.NUMERIC);
327             // 16 - C_Project_ID
328
if (C_Project_ID != 0)
329                 cstmt.setInt(16, C_Project_ID);
330             else
331                 cstmt.setNull(16, Types.NUMERIC);
332             // 17 - C_Campaign_ID
333
if (C_Campaign_ID != 0)
334                 cstmt.setInt(17, C_Campaign_ID);
335             else
336                 cstmt.setNull(17, Types.NUMERIC);
337             // 18 - C_Activity_ID
338
if (C_Activity_ID != 0)
339                 cstmt.setInt(18, C_Activity_ID);
340             else
341                 cstmt.setNull(18, Types.NUMERIC);
342             // 19 - User1_ID
343
if (User1_ID != 0)
344                 cstmt.setInt(19, User1_ID);
345             else
346                 cstmt.setNull(19, Types.NUMERIC);
347             // 20 - User2_ID
348
if (User2_ID != 0)
349                 cstmt.setInt(20, User2_ID);
350             else
351                 cstmt.setNull(20, Types.NUMERIC);
352
353             //
354
cstmt.executeUpdate();
355             C_ValidCombination_ID = cstmt.getInt(1); // 1 - ID
356
cstmt.close();
357             m_changed = true;
358             saved = true;
359         }
360         catch(SQLException e)
361         {
362             Log.error("Account.save", e);
363             m_changed = false;
364         }
365
366         //
367
load(C_ValidCombination_ID);
368         return saved;
369     } // save
370

371     /*************************************************************************/
372
373     /**
374      * Return String representation
375      * @return String
376      */

377     public String JavaDoc toString()
378     {
379         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Account=[");
380         sb.append(C_ValidCombination_ID)
381             .append(",Client=").append(AD_Client_ID)
382             .append(",Org=").append(AD_Org_ID)
383             .append(",Acct=").append(Account_ID);
384     // sb.append(",U1=").append(User1_ID);
385
sb.append("]");
386         return sb.toString();
387     } // toString
388

389     /**
390      * Is this a Balance Sheet Account
391      * @return boolean
392      */

393     public boolean isBalanceSheet()
394     {
395         return (m_AcctType == 'A' || m_AcctType == 'L' || m_AcctType == 'O');
396     } // isBalanceSheet
397

398     /**
399      * Is this an Activa Account
400      * @return boolean
401      */

402     public boolean isActiva()
403     {
404         return m_AcctType == 'A';
405     } // isActive
406

407     /**
408      * Is this a Passiva Account
409      * @return boolean
410      */

411     public boolean isPassiva()
412     {
413         return m_AcctType == 'L' || m_AcctType == 'O';
414     } // isPassiva
415

416
417     /*************************************************************************/
418
419     /**
420      * Check Account Validity.
421      *
422      * Reads information from C_ElementValue
423      * - Set Account Type - A,L,O,E,R
424      * - Set Post...
425      * - Set Is ForeignCurrenvy
426      * @return boolean
427      */

428     public boolean isValid()
429     {
430         if (m_OldAccount_ID == Account_ID && m_IsValid != null)
431             return m_IsValid.booleanValue();
432         m_OldAccount_ID = Account_ID;
433
434         String JavaDoc sql = "SELECT AccountType,PostActual,PostBudget,PostEncumbrance,PostStatistical,IsForeignCurrency,C_Currency_ID "
435             + "FROM C_ElementValue "
436             + "WHERE C_ElementValue_ID=? AND IsActive='Y' AND IsSummary='N'";
437         String JavaDoc str = null;
438         try
439         {
440             PreparedStatement pstmt = DB.prepareStatement(sql);
441             pstmt.setInt(1, Account_ID);
442             ResultSet rs = pstmt.executeQuery();
443             if (rs.next())
444                 str = rs.getString(1);
445             rs.close();
446             pstmt.close();
447         }
448         catch (SQLException e)
449         {
450             Log.error ("Account.getAccountType", e);
451         }
452         catch (Exception JavaDoc e2)
453         {
454             Log.error("Account.getAccountType", e2);
455         }
456         if (str == null || str.length() == 0)
457         {
458             Log.error("Account.getAccountType - Type not found");
459             m_IsValid = new Boolean JavaDoc (false);
460             m_AcctType = '?';
461         }
462         else
463         {
464             m_IsValid = new Boolean JavaDoc (true);
465             m_AcctType = str.charAt(0);
466         }
467         return m_IsValid.booleanValue();
468     } // isValid
469

470     /*************************************************************************/
471
472     /**
473      * Get VC
474      * @param newC_ValidCombination_ID combination
475      */

476     public void setC_ValidCombination_ID(int newC_ValidCombination_ID)
477     {
478         C_ValidCombination_ID = newC_ValidCombination_ID;
479         m_changed = true;
480     }
481     public int getC_ValidCombination_ID()
482     {
483         return C_ValidCombination_ID;
484     }
485     public void setAD_Client_ID(int newAD_Client_ID)
486     {
487         AD_Client_ID = newAD_Client_ID;
488         m_changed = true;
489     }
490     public int getAD_Client_ID()
491     {
492         return AD_Client_ID;
493     }
494     public void setAD_Org_ID(int newAD_Org_ID)
495     {
496         AD_Org_ID = newAD_Org_ID;
497         m_changed = true;
498     }
499     public int getAD_Org_ID()
500     {
501         return AD_Org_ID;
502     }
503     public void setActive(boolean newActive)
504     {
505         active = newActive;
506         m_changed = true;
507     }
508     public boolean isActive()
509     {
510         return active;
511     }
512     public void setUpdatedBy(int newUpdatedBy)
513     {
514         updatedBy = newUpdatedBy;
515         m_changed = true;
516     }
517     public int getUpdatedBy()
518     {
519         return updatedBy;
520     }
521     public void setAlias(String JavaDoc newAlias)
522     {
523         alias = newAlias;
524         m_changed = true;
525     }
526     public String JavaDoc getAlias()
527     {
528         return alias;
529     }
530     public void setCombination(String JavaDoc newCombination)
531     {
532         combination = newCombination;
533         m_changed = true;
534     }
535     public String JavaDoc getCombination()
536     {
537         return combination;
538     }
539     public void setDescription(String JavaDoc newDescription)
540     {
541         description = newDescription;
542         m_changed = true;
543     }
544     public String JavaDoc getDescription()
545     {
546         return description;
547     }
548     public void setFullyQualified(boolean newFullyQualified)
549     {
550         fullyQualified = newFullyQualified;
551         m_changed = true;
552     }
553     public boolean isFullyQualified()
554     {
555         return fullyQualified;
556     }
557     public void setC_AcctSchema_ID(int newC_AcctSchema_ID)
558     {
559         C_AcctSchema_ID = newC_AcctSchema_ID;
560         m_changed = true;
561     }
562     public int getC_AcctSchema_ID()
563     {
564         return C_AcctSchema_ID;
565     }
566     public void setAccount_ID(int newAccount_ID)
567     {
568         Account_ID = newAccount_ID;
569         m_changed = true;
570     }
571     public int getAccount_ID()
572     {
573         return Account_ID;
574     }
575     public void setM_Product_ID(int newM_Product_ID)
576     {
577         M_Product_ID = newM_Product_ID;
578         m_changed = true;
579     }
580     public int getM_Product_ID()
581     {
582         return M_Product_ID;
583     }
584     public void setC_BPartner_ID(int newC_BPartner_ID)
585     {
586         C_BPartner_ID = newC_BPartner_ID;
587         m_changed = true;
588     }
589     public int getC_BPartner_ID()
590     {
591         return C_BPartner_ID;
592     }
593     public void setAD_OrgTrx_ID(int newAD_OrgTrx_ID)
594     {
595         AD_OrgTrx_ID = newAD_OrgTrx_ID;
596         m_changed = true;
597     }
598     public int getAD_OrgTrx_ID()
599     {
600         return AD_OrgTrx_ID;
601     }
602     public void setC_LocFrom_ID(int newC_LocFrom_ID)
603     {
604         C_LocFrom_ID = newC_LocFrom_ID;
605         m_changed = true;
606     }
607     public int getC_LocFrom_ID()
608     {
609         return C_LocFrom_ID;
610     }
611     public void setC_LocTo_ID(int newC_LocTo_ID)
612     {
613         C_LocTo_ID = newC_LocTo_ID;
614         m_changed = true;
615     }
616     public int getC_LocTo_ID()
617     {
618         return C_LocTo_ID;
619     }
620     public void setC_SalesRegion_ID(int newC_SalesRegion_ID)
621     {
622         C_SalesRegion_ID = newC_SalesRegion_ID;
623         m_changed = true;
624     }
625     public int getC_SalesRegion_ID()
626     {
627         return C_SalesRegion_ID;
628     }
629     public void setC_Project_ID(int newC_Project_ID)
630     {
631         C_Project_ID = newC_Project_ID;
632         m_changed = true;
633     }
634     public int getC_Project_ID()
635     {
636         return C_Project_ID;
637     }
638     public void setC_Campaign_ID(int newC_Campaign_ID)
639     {
640         C_Campaign_ID = newC_Campaign_ID;
641         m_changed = true;
642     }
643     public int getC_Campaign_ID()
644     {
645         return C_Campaign_ID;
646     }
647     public void setC_Activity_ID(int newC_Activity_ID)
648     {
649         C_Activity_ID = newC_Activity_ID;
650         m_changed = true;
651     }
652     public int getC_Activity_ID()
653     {
654         return C_Activity_ID;
655     }
656     public void setUser1_ID(int newUser1_ID)
657     {
658         User1_ID = newUser1_ID;
659         m_changed = true;
660     }
661     public int getUser1_ID()
662     {
663         return User1_ID;
664     }
665     public void setUser2_ID(int newUser2_ID)
666     {
667         User2_ID = newUser2_ID;
668         m_changed = true;
669     }
670     public int getUser2_ID()
671     {
672         return User2_ID;
673     }
674     public boolean isChanged()
675     {
676         return m_changed;
677     }
678     private void writeObject(ObjectOutputStream oos) throws IOException
679     {
680         oos.defaultWriteObject();
681     }
682     private void readObject(ObjectInputStream ois) throws ClassNotFoundException JavaDoc, IOException
683     {
684         ois.defaultReadObject();
685     }
686 } // Account
687

688
Popular Tags