1 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 28 public class CalloutInvoice extends CalloutEngine 29 { 30 31 46 public String docType (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 47 { 48 Integer C_DocType_ID = (Integer )value; 49 if (C_DocType_ID == null || C_DocType_ID.intValue() == 0) 50 return ""; 51 52 try 53 { 54 String 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=?" + " 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 Env.setContext(ctx, WindowNo, "HasCharges", rs.getString(1)); 67 String YN = "Y"; 69 if (rs.getString(2).equals("Y")) 70 YN = "N"; 71 mTab.setValue("IsApproved", YN); 72 Env.setContext(ctx, WindowNo, "IsApproved", YN); if (rs.getString(3).equals("Y")) 75 mTab.setValue("DocumentNo", "<" + rs.getString(4) + ">"); 76 String s = rs.getString(5); 78 Env.setContext(ctx, WindowNo, "DocBaseType", s); 79 if (s.startsWith("AP")) 81 mTab.setValue("PaymentRule", "S"); else if (s.endsWith("C")) 83 mTab.setValue("PaymentRule", "P"); } 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 } 96 97 113 public String bPartner (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 114 { 115 Integer C_BPartner_ID = (Integer )value; 116 if (C_BPartner_ID == null || C_BPartner_ID.intValue() == 0) 117 return ""; 118 119 String 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'"; 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 if (rs.next()) 138 { 139 Integer ii = new Integer (rs.getInt(IsSOTrx ? "M_PriceList_ID" : "PO_PriceList_ID")); 141 if (!rs.wasNull()) 142 mTab.setValue("M_PriceList_ID", ii); 143 else 144 { int i = Env.getContextAsInt(ctx, "#M_PriceList_ID"); 146 if (i != 0) 147 mTab.setValue("M_PriceList_ID", new Integer (i)); 148 } 149 150 String s = rs.getString(IsSOTrx ? "PaymentRule" : "PaymentRulePO"); 152 if (s != null && s.length() != 0) 153 { 154 if (Env.getContext(ctx, WindowNo, "DocBaseType").endsWith("C")) s = "P"; 156 else if (IsSOTrx && (s.equals("S") || s.equals("U"))) s = "P"; mTab.setValue("PaymentRule", s); 159 } 160 ii = new Integer (rs.getInt(IsSOTrx ? "C_PaymentTerm_ID" : "PO_PaymentTerm_ID")); 162 if (!rs.wasNull()) 163 mTab.setValue("C_PaymentTerm_ID", ii); 164 165 int locID = rs.getInt("C_BPartner_Location_ID"); 167 if (C_BPartner_ID.toString().equals(Env.getContext(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "C_BPartner_ID"))) 170 { 171 String 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 (locID)); 179 180 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 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 (contID)); 192 193 double CreditAvailable = rs.getDouble("CreditAvailable"); 195 if (!rs.wasNull() && CreditAvailable < 0) 196 mTab.fireDataStatusEEvent("CreditLimitOver", 197 DisplayType.getNumberFormat(DisplayType.Amount).format(CreditAvailable)); 198 199 s = rs.getString("POReference"); 201 if (s != null && s.length() != 0) 202 mTab.setValue("POReference", s); 203 else 204 mTab.setValue("POReference", null); 205 s = rs.getString("SO_Description"); 207 if (s != null && s.trim().length() != 0) 208 mTab.setValue("Description", s); 209 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 } 228 229 230 231 243 public String product (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 244 { 245 Integer M_Product_ID = (Integer )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 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 (Env.getContextAsInt(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "M_AttributeSetInstance_ID"))); 254 else 255 mTab.setValue("M_AttributeSetInstance_ID", null); 256 257 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 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 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 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 (pp.getC_Currency_ID())); 284 Env.setContext(ctx, WindowNo, "EnforcePriceLimit", pp.isEnforcePriceLimit() ? "Y" : "N"); 285 } 286 287 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 (C_UOM_ID)); 299 setCalloutActive(false); 301 return tax (ctx, WindowNo, mTab, mField, value); 302 } 304 315 public String charge (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 316 { 317 Integer C_Charge_ID = (Integer )value; 318 if (C_Charge_ID == null || C_Charge_ID.intValue() == 0) 319 return ""; 320 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 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 return tax (ctx, WindowNo, mTab, mField, value); 350 } 352 353 364 public String tax (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 365 { 366 String column = mField.getColumnName(); 367 if (value == null) 368 return ""; 369 370 int M_Product_ID = 0; 372 if (column.equals("M_Product_ID")) 373 M_Product_ID = ((Integer )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 )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 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 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 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 if (C_Tax_ID == 0) 412 mTab.fireDataStatusEEvent(Log.retrieveError()); 413 else 414 mTab.setValue("C_Tax_ID", new Integer (C_Tax_ID)); 415 416 return amt (ctx, WindowNo, mTab, mField, value); 417 } 419 420 430 public String amt (Properties ctx, int WindowNo, MTab mTab, MField mField, Object value) 431 { 432 if (isCalloutActive() || value == null) 433 return ""; 434 setCalloutActive(true); 435 436 int StdPrecision = Env.getContextAsInt(ctx, WindowNo, "StdPrecision"); 437 BigDecimal QtyInvoiced = (BigDecimal)mTab.getValue("QtyInvoiced"); 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 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 int C_Tax_ID = ((Integer )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 mTab.setValue("LineTotalAmt", LineNetAmt.add(TaxAmt)); 472 } 473 474 setCalloutActive(false); 475 return ""; 476 } 478 }
| Popular Tags
|