1 14 package org.compiere.model; 15 16 import java.sql.*; 17 import java.util.Properties ; 18 19 import org.compiere.util.*; 20 21 27 public class Tax 28 { 29 30 static private Logger s_log = Logger.getCLogger (Tax.class); 31 32 33 34 59 public static int get (Properties 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 } 75 99 public static int getCharge (Properties 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 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 IsTaxExempt = null; 112 113 try 114 { 115 String 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 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 e) 156 { 157 s_log.error("getCharge", e); 158 return 0; 159 } 160 161 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 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 } 182 183 207 public static int getProduct (Properties 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 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 IsTaxExempt = null; 220 221 try 222 { 223 String 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 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 280 282 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 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 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 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 365 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 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 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 } 429 435 private static int getExemptTax (Properties ctx, int AD_Org_ID) 436 { 437 int C_Tax_ID = 0; 438 String 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 } 468 469 470 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 494 495 499 s_log.debug("DestinationTax"); 500 String 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" + " AND t.SOPOType<>?" + " AND t.C_TaxCategory_ID=?" + " AND lf.C_Location_ID=?" + " 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=?" + " 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"); 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 boolean noTax = true; 522 while (rs.next() && noTax) 523 { 524 Timestamp taxDate = rs.getTimestamp(2); 525 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 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<>?" + " AND t.C_TaxCategory_ID=?" + " AND lf.C_Location_ID=?" + " 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"); 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 } 566 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" + " AND t.SOPOType<>?" + " AND lf.C_Location_ID=?" + " AND t.IsDefault='Y' " 576 + "ORDER BY t.ValidFrom"; 577 pstmt = DB.prepareStatement(sql); 578 pstmt.setString(1, IsSOTrx ? "P" : "S"); 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 } 604 } | Popular Tags |