KickJava   Java API By Example, From Geeks To Geeks.

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


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-2003 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.math.*;
17 import java.util.*;
18 import java.sql.*;
19
20 import org.compiere.util.*;
21
22 /**
23  * Onvoice Callouts
24  *
25  * @author Jorg Janke
26  * @version $Id: CalloutInvoice.java,v 1.1 2003/10/17 06:15:20 jjanke Exp $
27  */

28 public class CalloutInvoice extends CalloutEngine
29 {
30
31     /**
32      * Invoice Header - DocType.
33      * - PaymentRule
34      * - IsApproved
35      * - temporary Document
36      * Context:
37      * - DocSubTypeSO
38      * - HasCharges
39      * - (re-sets Business Partner info of required)
40      * @param ctx Context
41      * @param WindowNo current Window No
42      * @param mTab Model Tab
43      * @param mField Model Field
44      * @param value The new value
45      */

46     public String JavaDoc docType (Properties ctx, int WindowNo, MTab mTab, MField mField, Object JavaDoc value)
47     {
48         Integer JavaDoc C_DocType_ID = (Integer JavaDoc)value;
49         if (C_DocType_ID == null || C_DocType_ID.intValue() == 0)
50             return "";
51
52         try
53         {
54             String JavaDoc SQL = "SELECT d.HasCharges,d.IsApproved,d.IsDocNoControlled,"
55                 + "s.CurrentNext, d.DocBaseType "
56                 + "FROM C_DocType d, AD_Sequence s "
57                 + "WHERE C_DocType_ID=?" // 1
58
+ " AND d.DocNoSequence_ID=s.AD_Sequence_ID(+)";
59
60             PreparedStatement pstmt = DB.prepareStatement(SQL);
61             pstmt.setInt(1, C_DocType_ID.intValue());
62             ResultSet rs = pstmt.executeQuery();
63             if (rs.next())
64             {
65                 // Charges - Set Context
66
Env.setContext(ctx, WindowNo, "HasCharges", rs.getString(1));
67                 // Approval required?
68
String JavaDoc YN = "Y";
69                 if (rs.getString(2).equals("Y"))
70                     YN = "N";
71                 mTab.setValue("IsApproved", YN);
72                 Env.setContext(ctx, WindowNo, "IsApproved", YN); // otherwise overwritten by default
73
// DocumentNo
74
if (rs.getString(3).equals("Y"))
75                     mTab.setValue("DocumentNo", "<" + rs.getString(4) + ">");
76                 // DocBaseType - Set Context
77
String JavaDoc s = rs.getString(5);
78                 Env.setContext(ctx, WindowNo, "DocBaseType", s);
79                 // AP Check & AR Credit Memo
80
if (s.startsWith("AP"))
81                     mTab.setValue("PaymentRule", "S"); // Check
82
else if (s.endsWith("C"))
83                     mTab.setValue("PaymentRule", "P"); // OnCredit
84
}
85             rs.close();
86             pstmt.close();
87         }
88         catch (SQLException e)
89         {
90             log.error("socType", e);
91             return e.getLocalizedMessage();
92         }
93         return "";
94     } // docType
95

96
97     /**
98      * Invoice Header- BPartner.
99      * - M_PriceList_ID (+ Context)
100      * - C_BPartner_Location_ID
101      * - AD_User_ID
102      * - POReference
103      * - SO_Description
104      * - IsDiscountPrinted
105      * - PaymentRule
106      * - C_PaymentTerm_ID
107      * @param ctx Context
108      * @param WindowNo current Window No
109      * @param mTab Model Tab
110      * @param mField Model Field
111      * @param value The new value
112      */

113     public String JavaDoc bPartner (Properties ctx, int WindowNo, MTab mTab, MField mField, Object JavaDoc value)
114     {
115         Integer JavaDoc C_BPartner_ID = (Integer JavaDoc)value;
116         if (C_BPartner_ID == null || C_BPartner_ID.intValue() == 0)
117             return "";
118
119         String JavaDoc SQL = "SELECT p.AD_Language,p.C_PaymentTerm_ID,"
120             + "p.M_PriceList_ID,p.PaymentRule,p.POReference,"
121             + "p.SO_Description,p.IsDiscountPrinted,"
122             + "p.SO_CreditLimit-p.SO_CreditUsed AS CreditAvailable,"
123             + "l.C_BPartner_Location_ID,c.AD_User_ID,"
124             + "p.PO_PriceList_ID, p.PaymentRulePO, p.PO_PaymentTerm_ID "
125             + "FROM C_BPartner p "
126             + " LEFT OUTER JOIN C_BPartner_Location l ON (p.C_BPartner_ID=l.C_BPartner_ID AND l.IsBillTo='Y' AND l.IsActive='Y')"
127             + " LEFT OUTER JOIN AD_User c ON (p.C_BPartner_ID=c.C_BPartner_ID) "
128             + "WHERE p.C_BPartner_ID=? AND p.IsActive='Y'"; // #1
129

130         boolean IsSOTrx = Env.getContext(ctx, WindowNo, "IsSOTrx").equals("Y");
131         try
132         {
133             PreparedStatement pstmt = DB.prepareStatement(SQL);
134             pstmt.setInt(1, C_BPartner_ID.intValue());
135             ResultSet rs = pstmt.executeQuery();
136             //
137
if (rs.next())
138             {
139                 // PriceList & IsTaxIncluded & Currency
140
Integer JavaDoc ii = new Integer JavaDoc(rs.getInt(IsSOTrx ? "M_PriceList_ID" : "PO_PriceList_ID"));
141                 if (!rs.wasNull())
142                     mTab.setValue("M_PriceList_ID", ii);
143                 else
144                 { // get default PriceList
145
int i = Env.getContextAsInt(ctx, "#M_PriceList_ID");
146                     if (i != 0)
147                         mTab.setValue("M_PriceList_ID", new Integer JavaDoc(i));
148                 }
149
150                 // PaymentRule
151
String JavaDoc s = rs.getString(IsSOTrx ? "PaymentRule" : "PaymentRulePO");
152                 if (s != null && s.length() != 0)
153                 {
154                     if (Env.getContext(ctx, WindowNo, "DocBaseType").endsWith("C")) // Credits are Payment Term
155
s = "P";
156                     else if (IsSOTrx && (s.equals("S") || s.equals("U"))) // No Check/Transfer for SO_Trx
157
s = "P"; // Payment Term
158
mTab.setValue("PaymentRule", s);
159                 }
160                 // Payment Term
161
ii = new Integer JavaDoc(rs.getInt(IsSOTrx ? "C_PaymentTerm_ID" : "PO_PaymentTerm_ID"));
162                 if (!rs.wasNull())
163                     mTab.setValue("C_PaymentTerm_ID", ii);
164
165                 // Location
166
int locID = rs.getInt("C_BPartner_Location_ID");
167                 // overwritten by InfoBP selection - works only if InfoWindow
168
// was used otherwise creates error (uses last value, may belong to differnt BP)
169
if (C_BPartner_ID.toString().equals(Env.getContext(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "C_BPartner_ID")))
170                 {
171                     String JavaDoc loc = Env.getContext(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "C_BPartner_Location_ID");
172                     if (loc.length() > 0)
173                         locID = Integer.parseInt(loc);
174                 }
175                 if (locID == 0)
176                     mTab.setValue("C_BPartner_Location_ID", null);
177                 else
178                     mTab.setValue("C_BPartner_Location_ID", new Integer JavaDoc(locID));
179
180                 // Contact - overwritten by InfoBP selection
181
int contID = rs.getInt("AD_User_ID");
182                 if (C_BPartner_ID.toString().equals(Env.getContext(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "C_BPartner_ID")))
183                 {
184                     String JavaDoc cont = Env.getContext(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "AD_User_ID");
185                     if (cont.length() > 0)
186                         contID = Integer.parseInt(cont);
187                 }
188                 if (contID == 0)
189                     mTab.setValue("AD_User_ID", null);
190                 else
191                     mTab.setValue("AD_User_ID", new Integer JavaDoc(contID));
192
193                 // CreditAvailable
194
double CreditAvailable = rs.getDouble("CreditAvailable");
195                 if (!rs.wasNull() && CreditAvailable < 0)
196                     mTab.fireDataStatusEEvent("CreditLimitOver",
197                         DisplayType.getNumberFormat(DisplayType.Amount).format(CreditAvailable));
198
199                 // PO Reference
200
s = rs.getString("POReference");
201                 if (s != null && s.length() != 0)
202                     mTab.setValue("POReference", s);
203                 else
204                     mTab.setValue("POReference", null);
205                 // SO Description
206
s = rs.getString("SO_Description");
207                 if (s != null && s.trim().length() != 0)
208                     mTab.setValue("Description", s);
209                 // IsDiscountPrinted
210
s = rs.getString("IsDiscountPrinted");
211                 if (s != null && s.length() != 0)
212                     mTab.setValue("IsDiscountPrinted", s);
213                 else
214                     mTab.setValue("IsDiscountPrinted", "N");
215             }
216             rs.close();
217             pstmt.close();
218         }
219         catch (SQLException e)
220         {
221             log.error("Invoice_BPartner", e);
222             return e.getLocalizedMessage();
223         }
224
225         return "";
226     } // bPartner
227

228
229     /*************************************************************************/
230     
231     /**
232      * Invoice Line - Product.
233      * - reset C_Charge_ID / M_AttributeSetInstance_ID
234      * - PriceList, PriceStd, PriceLimit, C_Currency_ID, EnforcePriceLimit
235      * - UOM
236      * Calls Tax
237      * @param ctx Context
238      * @param WindowNo current Window No
239      * @param mTab Model Tab
240      * @param mField Model Field
241      * @param value The new value
242      */

243     public String JavaDoc product (Properties ctx, int WindowNo, MTab mTab, MField mField, Object JavaDoc value)
244     {
245         Integer JavaDoc M_Product_ID = (Integer JavaDoc)value;
246         if (M_Product_ID == null || M_Product_ID.intValue() == 0)
247             return "";
248         setCalloutActive(true);
249         mTab.setValue("C_Charge_ID", null);
250         // Set Attribute
251
if (Env.getContextAsInt(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "M_Product_ID") == M_Product_ID.intValue()
252             && Env.getContextAsInt(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "M_AttributeSetInstance_ID") != 0)
253             mTab.setValue("M_AttributeSetInstance_ID", new Integer JavaDoc(Env.getContextAsInt(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "M_AttributeSetInstance_ID")));
254         else
255             mTab.setValue("M_AttributeSetInstance_ID", null);
256
257         // get invoice date - or default to today's date
258
Timestamp orderDate = Env.getContextAsDate(ctx, WindowNo, "DateInvoiced");
259         if (orderDate == null)
260             orderDate = new Timestamp(System.currentTimeMillis());
261
262         int M_PriceList_Version_ID = Env.getContextAsInt(ctx, WindowNo, "M_PriceList_Version_ID");
263         MProductPriceVO pp = MProductPriceVO.getBOMPricesPLV(M_Product_ID.intValue(), M_PriceList_Version_ID);
264         // Get via Price List
265
if (pp == null)
266         {
267             int M_PriceList_ID = Env.getContextAsInt(ctx, WindowNo, "M_PriceList_ID");
268             pp = MProductPriceVO.getBOMPricesPL(M_Product_ID.intValue(), M_PriceList_ID, orderDate);
269         }
270         // Get via Default PL
271
if (pp == null)
272         {
273             boolean isSOTrx = "Y".equals(Env.getContext(ctx, WindowNo, "IsSOTrx"));
274             pp = MProductPriceVO.getBOMPrices(M_Product_ID.intValue(), isSOTrx, orderDate);
275         }
276         
277         // Set Prices
278
if (pp != null)
279         {
280             mTab.setValue("PriceList", pp.PriceList);
281             mTab.setValue("PriceLimit", pp.PriceLimit);
282             mTab.setValue("PriceActual", pp.PriceStd);
283             mTab.setValue("C_Currency_ID", new Integer JavaDoc(pp.getC_Currency_ID()));
284             Env.setContext(ctx, WindowNo, "EnforcePriceLimit", pp.isEnforcePriceLimit() ? "Y" : "N");
285         }
286
287         // Check/Update Warehouse Setting
288
// int M_Warehouse_ID = Env.getContextAsInt(Env.WINDOW_INFO, "M_Warehouse_ID");
289
// Integer wh = (Integer)mTab.getValue("M_Warehouse_ID");
290
// if (wh.intValue() != M_Warehouse_ID)
291
// {
292
// mTab.setValue("M_Warehouse_ID", new Integer(M_Warehouse_ID));
293
// ADialog.warn(WindowNo, "WarehouseChanged");
294
// }
295

296         int C_UOM_ID = DB.getSQLValue("SELECT C_UOM_ID FROM M_Product WHERE M_Product_ID=?", M_Product_ID.intValue());
297         if (C_UOM_ID != 0)
298             mTab.setValue("C_UOM_ID", new Integer JavaDoc(C_UOM_ID));
299         //
300
setCalloutActive(false);
301         return tax (ctx, WindowNo, mTab, mField, value);
302     } // product
303

304     /**
305      * Order Line - Charge.
306      * - updates PriceActual from Charge
307      * - sets PriceLimit, PriceList to zero
308      * Calles tax
309      * @param ctx Context
310      * @param WindowNo current Window No
311      * @param mTab Model Tab
312      * @param mField Model Field
313      * @param value The new value
314      */

315     public String JavaDoc charge (Properties ctx, int WindowNo, MTab mTab, MField mField, Object JavaDoc value)
316     {
317         Integer JavaDoc C_Charge_ID = (Integer JavaDoc)value;
318         if (C_Charge_ID == null || C_Charge_ID.intValue() == 0)
319             return "";
320         // No Product defined
321
if (mTab.getValue("M_Product_ID") != null)
322         {
323             mTab.setValue("C_Charge_ID", null);
324             return "ChargeExclusively";
325         }
326
327         try
328         {
329             String JavaDoc SQL = "SELECT ChargeAmt FROM C_Charge WHERE C_Charge_ID=?";
330             PreparedStatement pstmt = DB.prepareStatement(SQL);
331             pstmt.setInt(1, C_Charge_ID.intValue());
332             ResultSet rs = pstmt.executeQuery();
333             if (rs.next())
334             {
335                 mTab.setValue ("PriceActual", rs.getBigDecimal (1));
336                 mTab.setValue ("PriceLimit", Env.ZERO);
337                 mTab.setValue ("PriceList", Env.ZERO);
338                 mTab.setValue ("Discount", Env.ZERO);
339             }
340             rs.close();
341             pstmt.close();
342         }
343         catch (SQLException e)
344         {
345             log.error("charge" + e);
346             return e.getLocalizedMessage();
347         }
348         //
349
return tax (ctx, WindowNo, mTab, mField, value);
350     } // charge
351

352
353     /**
354      * Invoice Line - Tax.
355      * - basis: Product, Charge, BPartner Location
356      * - sets C_Tax_ID
357      * Calles Amount
358      * @param ctx Context
359      * @param WindowNo current Window No
360      * @param mTab Model Tab
361      * @param mField Model Field
362      * @param value The new value
363      */

364     public String JavaDoc tax (Properties ctx, int WindowNo, MTab mTab, MField mField, Object JavaDoc value)
365     {
366         String JavaDoc column = mField.getColumnName();
367         if (value == null)
368             return "";
369
370         // Check Product
371
int M_Product_ID = 0;
372         if (column.equals("M_Product_ID"))
373             M_Product_ID = ((Integer JavaDoc)value).intValue();
374         else
375             M_Product_ID = Env.getContextAsInt(ctx, WindowNo, "M_Product_ID");
376         int C_Charge_ID = 0;
377         if (column.equals("C_Charge_ID"))
378             C_Charge_ID = ((Integer JavaDoc)value).intValue();
379         else
380             C_Charge_ID = Env.getContextAsInt(ctx, WindowNo, "C_Charge_ID");
381         log.debug("Product=" + M_Product_ID + ", C_Charge_ID=" + C_Charge_ID);
382         if (M_Product_ID == 0 && C_Charge_ID == 0)
383             return "";
384
385         // Check Partner Location
386
int shipC_BPartner_Location_ID = Env.getContextAsInt(ctx, WindowNo, "C_BPartner_Location_ID");
387         if (shipC_BPartner_Location_ID == 0)
388             return "";
389         log.debug("Ship BP_Location=" + shipC_BPartner_Location_ID);
390         int billC_BPartner_Location_ID = shipC_BPartner_Location_ID;
391         log.debug("Bill BP_Location=" + billC_BPartner_Location_ID);
392
393         // Dates
394
Timestamp billDate = Env.getContextAsDate(ctx, WindowNo, "DateInvoiced");
395         log.debug("Bill Date=" + billDate);
396         Timestamp shipDate = billDate;
397         log.debug("Ship Date=" + shipDate);
398
399         int AD_Org_ID = Env.getContextAsInt(ctx, WindowNo, "AD_Org_ID");
400         log.debug("Org=" + AD_Org_ID);
401
402         int M_Warehouse_ID = Env.getContextAsInt(ctx, "#M_Warehouse_ID");
403         log.debug("Warehouse=" + M_Warehouse_ID);
404
405         //
406
int C_Tax_ID = Tax.get(ctx, M_Product_ID, C_Charge_ID, billDate, shipDate,
407             AD_Org_ID, M_Warehouse_ID, billC_BPartner_Location_ID, shipC_BPartner_Location_ID,
408             Env.getContext(ctx, WindowNo, "IsSOTrx").equals("Y"));
409         log.debug("Tax ID=" + C_Tax_ID);
410         //
411
if (C_Tax_ID == 0)
412             mTab.fireDataStatusEEvent(Log.retrieveError());
413         else
414             mTab.setValue("C_Tax_ID", new Integer JavaDoc(C_Tax_ID));
415             
416         return amt (ctx, WindowNo, mTab, mField, value);
417     } // tax
418

419
420     /**
421      * Invoice - Amount.
422      * - called from QtyInvoiced, PriceActual
423      * - calculates LineNetAmt
424      * @param ctx Context
425      * @param WindowNo current Window No
426      * @param mTab Model Tab
427      * @param mField Model Field
428      * @param value The new value
429      */

430     public String JavaDoc amt (Properties ctx, int WindowNo, MTab mTab, MField mField, Object JavaDoc value)
431     {
432         if (isCalloutActive() || value == null)
433             return "";
434         setCalloutActive(true);
435
436         int StdPrecision = Env.getContextAsInt(ctx, WindowNo, "StdPrecision");
437         // get values
438
BigDecimal QtyInvoiced = (BigDecimal)mTab.getValue("QtyInvoiced"); //
439
if (QtyInvoiced == null)
440             QtyInvoiced = Env.ZERO;
441         BigDecimal PriceActual = (BigDecimal)mTab.getValue("PriceActual");
442         if (PriceActual == null)
443             PriceActual = Env.ZERO;
444         else
445             PriceActual = PriceActual.setScale(StdPrecision, BigDecimal.ROUND_HALF_UP);
446
447         // Multiply
448
BigDecimal LineNetAmt = QtyInvoiced.multiply(PriceActual);
449         if (LineNetAmt.scale() > StdPrecision)
450             LineNetAmt = LineNetAmt.setScale(StdPrecision, BigDecimal.ROUND_HALF_UP);
451         mTab.setValue("LineNetAmt", LineNetAmt);
452         log.debug("LineNetAmt=" + LineNetAmt);
453
454         // Calculate Tax Amount for PO
455
int C_Tax_ID = ((Integer JavaDoc)mTab.getValue("C_Tax_ID")).intValue();
456         boolean IsSOTrx = "Y".equals(Env.getContext(Env.getCtx(), WindowNo, "IsSOTrx"));
457         if (!IsSOTrx)
458         {
459             BigDecimal TaxAmt = null;
460             if (mField.getColumnName().equals("TaxAmt"))
461             {
462                 TaxAmt = (BigDecimal)mTab.getValue("TaxAmt");
463             }
464             else
465             {
466                 MTax tax = new MTax (ctx, C_Tax_ID);
467                 TaxAmt = tax.calculateTax(LineNetAmt, true, StdPrecision);
468                 mTab.setValue("TaxAmt", TaxAmt);
469             }
470             // Add it up
471
mTab.setValue("LineTotalAmt", LineNetAmt.add(TaxAmt));
472         }
473
474         setCalloutActive(false);
475         return "";
476     } // amt
477

478 } // CalloutInvoice
479
Popular Tags