KickJava   Java API By Example, From Geeks To Geeks.

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


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.Properties JavaDoc;
18
19 import org.compiere.util.*;
20
21 /**
22  * Tax Handling
23  *
24  * @author Jorg Janke
25  * @version $Id: Tax.java,v 1.13 2003/10/04 03:51:50 jjanke Exp $
26  */

27 public class Tax
28 {
29     /** Logger */
30     static private Logger s_log = Logger.getCLogger (Tax.class);
31
32     /*************************************************************************/
33
34     /**
35      * Get Tax ID - converts parameters to call Get Tax.
36      * <pre>
37      * M_Product_ID/C_Charge_ID -> C_TaxCategory_ID
38      * billDate, shipDate -> billDate, shipDate
39      * AD_Org_ID -> billFromC_Location_ID
40      * M_Warehouse_ID -> shipFromC_Location_ID
41      * billC_BPartner_Location_ID -> billToC_Location_ID
42      * shipC_BPartner_Location_ID -> shipToC_Location_ID
43      *
44      * if IsSOTrx is false, bill and ship are reversed
45      * </pre>
46      * @param ctx context
47      * @param M_Product_ID product
48      * @param C_Charge_ID product
49      * @param billDate invoice date
50      * @param shipDate ship date
51      * @param AD_Org_ID org
52      * @param M_Warehouse_ID warehouse
53      * @param billC_BPartner_Location_ID invoice location
54      * @param shipC_BPartner_Location_ID ship location
55      * @param IsSOTrx is a sales trx
56      * @return C_Tax_ID
57      * If error it returns 0 and sets error log (TaxCriteriaNotFound)
58      */

59     public static int get (Properties JavaDoc ctx, int M_Product_ID, int C_Charge_ID,
60         Timestamp billDate, Timestamp shipDate,
61         int AD_Org_ID, int M_Warehouse_ID,
62         int billC_BPartner_Location_ID, int shipC_BPartner_Location_ID,
63         boolean IsSOTrx)
64     {
65         if (M_Product_ID != 0)
66             return getProduct (ctx, M_Product_ID, billDate, shipDate, AD_Org_ID, M_Warehouse_ID,
67                 billC_BPartner_Location_ID, shipC_BPartner_Location_ID, IsSOTrx);
68         else if (C_Charge_ID != 0)
69             return getCharge (ctx, C_Charge_ID, billDate, shipDate, AD_Org_ID, M_Warehouse_ID,
70                 billC_BPartner_Location_ID, shipC_BPartner_Location_ID, IsSOTrx);
71         else
72             return getExemptTax (ctx, AD_Org_ID);
73     } // get
74

75     /**
76      * Get Tax ID - converts parameters to call Get Tax.
77      * <pre>
78      * C_Charge_ID -> C_TaxCategory_ID
79      * billDate, shipDate -> billDate, shipDate
80      * AD_Org_ID -> billFromC_Location_ID
81      * M_Warehouse_ID -> shipFromC_Location_ID
82      * billC_BPartner_Location_ID -> billToC_Location_ID
83      * shipC_BPartner_Location_ID -> shipToC_Location_ID
84      *
85      * if IsSOTrx is false, bill and ship are reversed
86      * </pre>
87      * @param ctx context
88      * @param C_Charge_ID product
89      * @param billDate invoice date
90      * @param shipDate ship date
91      * @param AD_Org_ID org
92      * @param M_Warehouse_ID warehouse
93      * @param billC_BPartner_Location_ID invoice location
94      * @param shipC_BPartner_Location_ID ship location
95      * @param IsSOTrx is a sales trx
96      * @return C_Tax_ID
97      * If error it returns 0 and sets error log (TaxCriteriaNotFound)
98      */

99     public static int getCharge (Properties JavaDoc ctx, int C_Charge_ID,
100         Timestamp billDate, Timestamp shipDate,
101         int AD_Org_ID, int M_Warehouse_ID,
102         int billC_BPartner_Location_ID, int shipC_BPartner_Location_ID,
103         boolean IsSOTrx)
104     {
105         String JavaDoc variable = "";
106         int C_TaxCategory_ID = 0;
107         int shipFromC_Location_ID = 0;
108         int shipToC_Location_ID = 0;
109         int billFromC_Location_ID = 0;
110         int billToC_Location_ID = 0;
111         String JavaDoc IsTaxExempt = null;
112
113         try
114         {
115             // Get all at once
116
String JavaDoc sql = "SELECT c.C_TaxCategory_ID, o.C_Location_ID, il.C_Location_ID, b.IsTaxExempt,"
117                  + " w.C_Location_ID, sl.C_Location_ID "
118                  + "FROM C_Charge c, AD_OrgInfo o,"
119                  + " C_BPartner_Location il INNER JOIN C_BPartner b ON (il.C_BPartner_ID=b.C_BPartner_ID),"
120                  + " M_Warehouse w, C_BPartner_Location sl "
121                  + "WHERE c.C_Charge_ID=?"
122                  + " AND o.AD_Org_ID=?"
123                  + " AND il.C_BPartner_Location_ID=?"
124                  + " AND w.M_Warehouse_ID=?"
125                  + " AND sl.C_BPartner_Location_ID=?";
126             PreparedStatement pstmt = DB.prepareStatement (sql);
127             pstmt.setInt (1, C_Charge_ID);
128             pstmt.setInt (2, AD_Org_ID);
129             pstmt.setInt (3, billC_BPartner_Location_ID);
130             pstmt.setInt (4, M_Warehouse_ID);
131             pstmt.setInt (5, shipC_BPartner_Location_ID);
132             ResultSet rs = pstmt.executeQuery ();
133             boolean found = false;
134             if (rs.next ())
135             {
136                 C_TaxCategory_ID = rs.getInt (1);
137                 billFromC_Location_ID = rs.getInt (2);
138                 billToC_Location_ID = rs.getInt (3);
139                 IsTaxExempt = rs.getString (4);
140                 shipFromC_Location_ID = rs.getInt (5);
141                 shipToC_Location_ID = rs.getInt (6);
142                 found = true;
143             }
144             rs.close ();
145             pstmt.close ();
146             //
147
if (!found)
148             {
149                 s_log.error("getCharge - Not found - C_Charge_ID=" + C_Charge_ID);
150                 return 0;
151             }
152             else if ("Y".equals (IsTaxExempt))
153                 return getExemptTax (ctx, AD_Org_ID);
154         }
155         catch (Exception JavaDoc e)
156         {
157             s_log.error("getCharge", e);
158             return 0;
159         }
160
161         // Reverese for PO
162
if (!IsSOTrx)
163         {
164             int temp = billFromC_Location_ID;
165             billFromC_Location_ID = billToC_Location_ID;
166             billToC_Location_ID = temp;
167             temp = shipFromC_Location_ID;
168             shipFromC_Location_ID = shipToC_Location_ID;
169             shipToC_Location_ID = temp;
170         }
171         //
172
s_log.debug ("C_TaxCategory_ID=" + C_TaxCategory_ID
173           + ", billFromC_Location_ID=" + billFromC_Location_ID
174           + ", billToC_Location_ID=" + billToC_Location_ID
175           + ", shipFromC_Location_ID=" + shipFromC_Location_ID
176           + ", shipToC_Location_ID=" + shipToC_Location_ID);
177         return get (C_TaxCategory_ID, IsSOTrx,
178           shipDate, shipFromC_Location_ID, shipToC_Location_ID,
179           billDate, billFromC_Location_ID, billToC_Location_ID);
180     } // getCharge
181

182
183     /**
184      * Get Tax ID - converts parameters to call Get Tax.
185      * <pre>
186      * M_Product_ID -> C_TaxCategory_ID
187      * billDate, shipDate -> billDate, shipDate
188      * AD_Org_ID -> billFromC_Location_ID
189      * M_Warehouse_ID -> shipFromC_Location_ID
190      * billC_BPartner_Location_ID -> billToC_Location_ID
191      * shipC_BPartner_Location_ID -> shipToC_Location_ID
192      *
193      * if IsSOTrx is false, bill and ship are reversed
194      * </pre>
195      * @param ctx context
196      * @param M_Product_ID product
197      * @param billDate invoice date
198      * @param shipDate ship date
199      * @param AD_Org_ID org
200      * @param M_Warehouse_ID warehouse
201      * @param billC_BPartner_Location_ID invoice location
202      * @param shipC_BPartner_Location_ID ship location
203      * @param IsSOTrx is a sales trx
204      * @return C_Tax_ID
205      * If error it returns 0 and sets error log (TaxCriteriaNotFound)
206      */

207     public static int getProduct (Properties JavaDoc ctx, int M_Product_ID,
208         Timestamp billDate, Timestamp shipDate,
209         int AD_Org_ID, int M_Warehouse_ID,
210         int billC_BPartner_Location_ID, int shipC_BPartner_Location_ID,
211         boolean IsSOTrx)
212     {
213         String JavaDoc variable = "";
214         int C_TaxCategory_ID = 0;
215         int shipFromC_Location_ID = 0;
216         int shipToC_Location_ID = 0;
217         int billFromC_Location_ID = 0;
218         int billToC_Location_ID = 0;
219         String JavaDoc IsTaxExempt = null;
220
221         try
222         {
223             // Get all at once
224
String JavaDoc sql = "SELECT p.C_TaxCategory_ID, o.C_Location_ID, il.C_Location_ID, b.IsTaxExempt,"
225                 + " w.C_Location_ID, sl.C_Location_ID "
226                 + "FROM M_Product p, AD_OrgInfo o,"
227                 + " C_BPartner_Location il INNER JOIN C_BPartner b ON (il.C_BPartner_ID=b.C_BPartner_ID),"
228                 + " M_Warehouse w, C_BPartner_Location sl "
229                 + "WHERE p.M_Product_ID=?"
230                 + " AND o.AD_Org_ID=?"
231                 + " AND il.C_BPartner_Location_ID=?"
232                 + " AND w.M_Warehouse_ID=?"
233                 + " AND sl.C_BPartner_Location_ID=?";
234             PreparedStatement pstmt = DB.prepareStatement(sql);
235             pstmt.setInt(1, M_Product_ID);
236             pstmt.setInt(2, AD_Org_ID);
237             pstmt.setInt(3, billC_BPartner_Location_ID);
238             pstmt.setInt(4, M_Warehouse_ID);
239             pstmt.setInt(5, shipC_BPartner_Location_ID);
240             ResultSet rs = pstmt.executeQuery();
241             boolean found = false;
242             if (rs.next())
243             {
244                 C_TaxCategory_ID = rs.getInt(1);
245                 billFromC_Location_ID = rs.getInt(2);
246                 billToC_Location_ID = rs.getInt(3);
247                 IsTaxExempt = rs.getString(4);
248                 shipFromC_Location_ID = rs.getInt(5);
249                 shipToC_Location_ID = rs.getInt(6);
250                 found = true;
251             }
252             rs.close();
253             pstmt.close();
254             //
255
if (found && "Y".equals(IsTaxExempt))
256                 return getExemptTax(ctx, AD_Org_ID);
257             else if (found)
258             {
259                 if (!IsSOTrx)
260                 {
261                     int temp = billFromC_Location_ID;
262                     billFromC_Location_ID = billToC_Location_ID;
263                     billToC_Location_ID = temp;
264                     temp = shipFromC_Location_ID;
265                     shipFromC_Location_ID = shipToC_Location_ID;
266                     shipToC_Location_ID = temp;
267                 }
268                 s_log.debug("C_TaxCategory_ID=" + C_TaxCategory_ID
269                     + ", billFromC_Location_ID=" + billFromC_Location_ID
270                     + ", billToC_Location_ID=" + billToC_Location_ID
271                     + ", shipFromC_Location_ID=" + shipFromC_Location_ID
272                     + ", shipToC_Location_ID=" + shipToC_Location_ID);
273                 return get(C_TaxCategory_ID, IsSOTrx,
274                     shipDate, shipFromC_Location_ID, shipToC_Location_ID,
275                     billDate, billFromC_Location_ID, billToC_Location_ID);
276             }
277
278             // ----------------------------------------------------------------
279

280             // Detail for error isolation
281

282         // M_Product_ID -> C_TaxCategory_ID
283
sql = "SELECT C_TaxCategory_ID FROM M_Product "
284                 + "WHERE M_Product_ID=?";
285             variable = "M_Product_ID";
286             pstmt = DB.prepareStatement(sql);
287             pstmt.setInt(1, M_Product_ID);
288             rs = pstmt.executeQuery();
289             found = false;
290             if (rs.next())
291             {
292                 C_TaxCategory_ID = rs.getInt(1);
293                 found = true;
294             }
295             rs.close();
296             pstmt.close();
297             if (C_TaxCategory_ID == 0)
298             {
299                 Log.saveError("TaxCriteriaNotFound", Msg.translate(ctx, variable)
300                     + (found ? "" : " (Product=" + M_Product_ID + " not found)"));
301                 return 0;
302             }
303             s_log.debug("C_TaxCategory_ID=" + C_TaxCategory_ID);
304
305         // AD_Org_ID -> billFromC_Location_ID
306
sql = "SELECT C_Location_ID FROM AD_OrgInfo "
307                 + "WHERE AD_Org_ID=?";
308             variable = "AD_Org_ID";
309             pstmt = DB.prepareStatement(sql);
310             pstmt.setInt(1, AD_Org_ID);
311             rs = pstmt.executeQuery();
312             found = false;
313             if (rs.next())
314             {
315                 billFromC_Location_ID = rs.getInt (1);
316                 found = true;
317             }
318             rs.close();
319             pstmt.close();
320             if (billFromC_Location_ID == 0)
321             {
322                 Log.saveError("TaxCriteriaNotFound", Msg.translate(Env.getAD_Language(ctx), variable)
323                   + (found ? "" : " (Info/Org=" + AD_Org_ID + " not found)"));
324                 return 0;
325             }
326
327         // billC_BPartner_Location_ID -> billToC_Location_ID
328
sql = "SELECT l.C_Location_ID, b.IsTaxExempt "
329                 + "FROM C_BPartner_Location l INNER JOIN C_BPartner b ON (l.C_BPartner_ID=b.C_BPartner_ID) "
330                 + "WHERE C_BPartner_Location_ID=?";
331             variable = "BillTo_ID";
332             pstmt = DB.prepareStatement(sql);
333             pstmt.setInt(1, billC_BPartner_Location_ID);
334             rs = pstmt.executeQuery();
335             found = false;
336             if (rs.next())
337             {
338                 billToC_Location_ID = rs.getInt(1);
339                 IsTaxExempt = rs.getString(2);
340                 found = true;
341             }
342             rs.close();
343             pstmt.close();
344             if (billToC_Location_ID == 0)
345             {
346                 Log.saveError("TaxCriteriaNotFound", Msg.translate(Env.getAD_Language(ctx), variable)
347                     + (found ? "" : " (BPLocation=" + billC_BPartner_Location_ID + " not found)"));
348                 return 0;
349             }
350             if ("Y".equals(IsTaxExempt))
351                 return getExemptTax(ctx, AD_Org_ID);
352
353             // Reverse for PO
354
if (!IsSOTrx)
355             {
356                 int temp = billFromC_Location_ID;
357                 billFromC_Location_ID = billToC_Location_ID;
358                 billToC_Location_ID = temp;
359             }
360             s_log.debug("billFromC_Location_ID = " + billFromC_Location_ID);
361             s_log.debug("billToC_Location_ID = " + billToC_Location_ID);
362
363             //-----------------------------------------------------------------
364

365         // M_Warehouse_ID -> shipFromC_Location_ID
366
sql = "SELECT C_Location_ID FROM M_Warehouse "
367                 + "WHERE M_Warehouse_ID=?";
368             variable = "M_Warehouse_ID";
369             pstmt = DB.prepareStatement(sql);
370             pstmt.setInt(1, M_Warehouse_ID);
371             rs = pstmt.executeQuery();
372             found = false;
373             if (rs.next())
374             {
375                 shipFromC_Location_ID = rs.getInt (1);
376                 found = true;
377             }
378             rs.close();
379             pstmt.close();
380             if (shipFromC_Location_ID == 0)
381             {
382                 Log.saveError("TaxCriteriaNotFound", Msg.translate(Env.getAD_Language(ctx), variable)
383                     + (found ? "" : " (Warehouse=" + M_Warehouse_ID + " not found)"));
384                 return 0;
385             }
386
387         // shipC_BPartner_Location_ID -> shipToC_Location_ID
388
sql = "SELECT C_Location_ID FROM C_BPartner_Location "
389                 + "WHERE C_BPartner_Location_ID=?";
390             variable = "C_BPartner_Location_ID";
391             pstmt = DB.prepareStatement(sql);
392             pstmt.setInt(1, shipC_BPartner_Location_ID);
393             rs = pstmt.executeQuery();
394             found = false;
395             if (rs.next())
396             {
397                 shipToC_Location_ID = rs.getInt (1);
398                 found = true;
399             }
400             rs.close();
401             pstmt.close();
402             if (shipToC_Location_ID == 0)
403             {
404                 Log.saveError("TaxCriteriaNotFound", Msg.translate(Env.getAD_Language(ctx), variable)
405                     + (found ? "" : " (BPLocation=" + shipC_BPartner_Location_ID + " not found)"));
406                 return 0;
407             }
408
409             // Reverse for PO
410
if (!IsSOTrx)
411             {
412                 int temp = shipFromC_Location_ID;
413                 shipFromC_Location_ID = shipToC_Location_ID;
414                 shipToC_Location_ID = temp;
415             }
416             s_log.debug("shipFromC_Location_ID = " + shipFromC_Location_ID);
417             s_log.debug("shipToC_Location_ID = " + shipToC_Location_ID);
418         }
419         catch (SQLException e)
420         {
421             s_log.error("getProduct (" + variable + ")", e);
422         }
423
424         return get(C_TaxCategory_ID, IsSOTrx,
425             shipDate, shipFromC_Location_ID, shipToC_Location_ID,
426             billDate, billFromC_Location_ID, billToC_Location_ID);
427     } // getProduct
428

429     /**
430      * Get Exempt Tax Code
431      * @param ctx context
432      * @param AD_Org_ID org to find client
433      * @return C_Tax_ID
434      */

435     private static int getExemptTax (Properties JavaDoc ctx, int AD_Org_ID)
436     {
437         int C_Tax_ID = 0;
438         String JavaDoc sql = "SELECT t.C_Tax_ID "
439             + "FROM C_Tax t"
440             + " INNER JOIN AD_Org o ON (t.AD_Client_ID=o.AD_Client_ID) "
441             + "WHERE t.IsTaxExempt='Y' AND o.AD_Org_ID=? "
442             + "ORDER BY t.Rate DESC";
443         boolean found = false;
444         try
445         {
446             PreparedStatement pstmt = DB.prepareStatement(sql);
447             pstmt.setInt(1, AD_Org_ID);
448             ResultSet rs = pstmt.executeQuery();
449             if (rs.next())
450             {
451                 C_Tax_ID = rs.getInt (1);
452                 found = true;
453             }
454             rs.close();
455             pstmt.close();
456         }
457         catch (SQLException e)
458         {
459             s_log.error("Tax.getExemptTax", e);
460         }
461         s_log.debug("TaxExempt=Y - C_Tax_ID=" + C_Tax_ID);
462         if (C_Tax_ID == 0)
463             Log.saveError("TaxCriteriaNotFound", Msg.getMsg(ctx, "TaxNoExemptFound")
464                 + (found ? "" : " (Tax/Org=" + AD_Org_ID + " not found)"));
465         return C_Tax_ID;
466     } // getExemptTax
467

468     /*************************************************************************/
469
470     /**
471      * Get Tax ID (Detail).
472      * If error return 0 and set error log (TaxNotFound)
473      * @param C_TaxCategory_ID tax category
474      * @param IsSOTrx Sales Order Trx
475      * @param shipDate ship date
476      * @param shipFromC_Locction_ID ship from
477      * @param shipToC_Location_ID ship to
478      * @param billDate invoice date
479      * @param billFromC_Location_ID invoice from
480      * @param billToC_Location_ID invoice to
481      * @return C_Tax_ID
482      */

483     protected static int get (int C_TaxCategory_ID, boolean IsSOTrx,
484         Timestamp shipDate, int shipFromC_Locction_ID, int shipToC_Location_ID,
485         Timestamp billDate, int billFromC_Location_ID, int billToC_Location_ID)
486     {
487         s_log.info("get(Detail) - Category=" + C_TaxCategory_ID + ", SOTrx=" + IsSOTrx
488             + ", BillFrom=" + billFromC_Location_ID + ", ShipTo=" + shipToC_Location_ID + ", BillDate=" + billDate);
489         int C_Tax_ID = 0;
490
491         // C_TaxCategory contains CommodityCode
492
// API to Tax Vendor comes here
493

494
495         /**********************************************************************
496          * own table lookup
497          * - uses billFromC_Location, shipToC_Location, billDate
498          */

499         s_log.debug("DestinationTax");
500         String JavaDoc sql = "SELECT t.C_Tax_ID, t.ValidFrom "
501             + "FROM C_Tax t, C_Location lf, C_Location lt "
502             + "WHERE t.Parent_Tax_ID IS NULL" // No detail tax
503
+ " AND t.SOPOType<>?" // #1 Exclude
504
+ " AND t.C_TaxCategory_ID=?" // #2 C_TaxCategory_ID
505
+ " AND lf.C_Location_ID=?" // #3 billFromC_Location_ID
506
+ " AND t.C_Country_ID=lf.C_Country_ID"
507             + " AND (t.C_Region_ID=lf.C_Region_ID OR (t.C_Region_ID IS NULL AND lf.C_Region_ID IS NULL))"
508             + " AND lt.C_Location_ID=?" // #4 shipToC_Location_ID
509
+ " AND t.To_Country_ID=lt.C_Country_ID"
510             + " AND (t.To_Region_ID=lt.C_Region_ID OR (t.To_Region_ID IS NULL AND lt.C_Region_ID IS NULL)) "
511             + "ORDER BY t.ValidFrom";
512         try
513         {
514             PreparedStatement pstmt = DB.prepareStatement(sql);
515             pstmt.setString(1, IsSOTrx ? "P" : "S"); // exclude
516
pstmt.setInt(2, C_TaxCategory_ID);
517             pstmt.setInt(3, billFromC_Location_ID);
518             pstmt.setInt(4, shipToC_Location_ID);
519             ResultSet rs = pstmt.executeQuery();
520             // Take latest one -> wrong, needs to search
521
boolean noTax = true;
522             while (rs.next() && noTax)
523             {
524                 Timestamp taxDate = rs.getTimestamp(2);
525                 // we have the tax rate,
526
// if the tax valid from date is after or equal bill date
527
if (taxDate == null || taxDate.after(billDate))
528                     continue;
529                 noTax = false;
530                 C_Tax_ID = rs.getInt(1);
531             }
532             rs.close();
533             pstmt.close();
534
535             // Try with no destination country in tax table
536
if (noTax)
537             {
538                 s_log.debug("OriginTax");
539                 sql = "SELECT t.C_Tax_ID, t.ValidFrom "
540                     + "FROM C_Tax t, C_Location lf "
541                     + "WHERE t.Parent_Tax_ID IS NULL"
542                     + " AND t.SOPOType<>?" // #1 Exclude
543
+ " AND t.C_TaxCategory_ID=?" // #2 C_TaxCategory_ID
544
+ " AND lf.C_Location_ID=?" // #3 billFromC_Location_ID
545
+ " AND t.C_Country_ID=lf.C_Country_ID"
546                     + " AND (t.C_Region_ID=lf.C_Region_ID OR (t.C_Region_ID IS NULL AND lf.C_Region_ID IS NULL))"
547                     + " AND t.To_Country_ID IS NULL AND t.To_Region_ID IS NULL "
548                     + "ORDER BY t.ValidFrom";
549                 pstmt = DB.prepareStatement(sql);
550                 pstmt.setString(1, IsSOTrx ? "P" : "S"); // exclude
551
pstmt.setInt(2, C_TaxCategory_ID);
552                 pstmt.setInt(3, billFromC_Location_ID);
553                 rs = pstmt.executeQuery();
554                 while (rs.next() && noTax)
555                 {
556                     Timestamp taxDate = rs.getTimestamp(2);
557                     if (taxDate == null || taxDate.after(billDate))
558                         continue;
559                     noTax = false;
560                     C_Tax_ID = rs.getInt(1);
561                 }
562                 rs.close();
563                 pstmt.close();
564             } // no destination country
565

566             // Get Default Tax
567
if (noTax)
568             {
569                 s_log.debug("DefaultTax");
570                 sql = "SELECT t.C_Tax_ID, t.ValidFrom "
571                     + "FROM C_Tax t, C_Location lf "
572                     + "WHERE t.AD_Client_ID=lf.AD_Client_ID" // get Client from bill-from
573
+ " AND t.SOPOType<>?" // #1 Exclude
574
+ " AND lf.C_Location_ID=?" // #2 billFromC_Location_ID
575
+ " AND t.IsDefault='Y' "
576                     + "ORDER BY t.ValidFrom";
577                 pstmt = DB.prepareStatement(sql);
578                 pstmt.setString(1, IsSOTrx ? "P" : "S"); // exclude
579
pstmt.setInt(2, billFromC_Location_ID);
580                 rs = pstmt.executeQuery();
581                 while (rs.next() && noTax)
582                 {
583                     Timestamp taxDate = rs.getTimestamp(2);
584                     if (taxDate == null || taxDate.after(billDate))
585                         continue;
586                     noTax = false;
587                     C_Tax_ID = rs.getInt(1);
588                 }
589                 rs.close();
590                 pstmt.close();
591             }
592         }
593         catch (SQLException e)
594         {
595             s_log.error("get (C_Tax_ID) - " + sql, e);
596         }
597
598         if (C_Tax_ID == 0)
599             Log.saveError("TaxNotFound", "");
600
601         return C_Tax_ID;
602     } // get
603

604 } // Tax
605
Popular Tags