1 18 package org.ofbiz.accounting.invoice; 19 20 import java.math.BigDecimal ; 21 import java.sql.Timestamp ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import org.ofbiz.base.util.Debug; 26 import org.ofbiz.base.util.UtilDateTime; 27 import org.ofbiz.base.util.UtilMisc; 28 import org.ofbiz.base.util.UtilNumber; 29 import org.ofbiz.entity.condition.EntityConditionList; 30 import org.ofbiz.entity.condition.EntityExpr; 31 import org.ofbiz.entity.condition.EntityOperator; 32 import org.ofbiz.entity.GenericDelegator; 33 import org.ofbiz.entity.GenericEntityException; 34 import org.ofbiz.entity.GenericValue; 35 import org.ofbiz.entity.util.EntityUtil; 36 37 44 public class InvoiceWorker { 45 46 public static String module = InvoiceWorker.class.getName(); 47 private static int decimals = UtilNumber.getBigDecimalScale("invoice.decimals"); 48 private static int rounding = UtilNumber.getBigDecimalRoundingMode("invoice.rounding"); 49 50 55 public static double getInvoiceTotal(GenericDelegator delegator, String invoiceId) { 56 return getInvoiceTotalBd(delegator, invoiceId).doubleValue(); 57 } 58 59 public static BigDecimal getInvoiceTotalBd(GenericDelegator delegator, String invoiceId) { 60 if (delegator == null) { 61 throw new IllegalArgumentException ("Null delegator is not allowed in this method"); 62 } 63 64 GenericValue invoice = null; 65 try { 66 invoice = delegator.findByPrimaryKey("Invoice", UtilMisc.toMap("invoiceId", invoiceId)); 67 } catch (GenericEntityException e) { 68 Debug.logError(e, "Problem getting Invoice", module); 69 } 70 71 if (invoice == null) { 72 throw new IllegalArgumentException ("The invoiceId passed does not match an existing invoice"); 73 } 74 75 return getInvoiceTotalBd(invoice); 76 } 77 78 83 public static double getInvoiceTotal(GenericValue invoice) { 84 return getInvoiceTotalBd(invoice).doubleValue(); 85 } 86 87 public static BigDecimal getInvoiceTotalBd(GenericValue invoice) { 88 BigDecimal invoiceTotal = new BigDecimal ("0"); 89 List invoiceItems = null; 90 try { 91 invoiceItems = invoice.getRelated("InvoiceItem"); 92 } catch (GenericEntityException e) { 93 Debug.logError(e, "Trouble getting InvoiceItem list", module); 94 } 95 if (invoiceItems != null && invoiceItems.size() > 0) { 96 Iterator invoiceItemsIter = invoiceItems.iterator(); 97 while (invoiceItemsIter.hasNext()) { 98 GenericValue invoiceItem = (GenericValue) invoiceItemsIter.next(); 99 BigDecimal amount = invoiceItem.getBigDecimal("amount"); 100 BigDecimal quantity = invoiceItem.getBigDecimal("quantity"); 101 if (amount == null) 102 amount = new BigDecimal ("0"); 103 if (quantity == null) 104 quantity = new BigDecimal ("1"); 105 invoiceTotal = invoiceTotal.add( amount.multiply(quantity)).setScale(decimals,rounding); 106 } 107 } 108 return invoiceTotal; 109 } 110 111 116 public static GenericValue getBillToParty(GenericValue invoice) { 117 try { 118 GenericValue billToParty = invoice.getRelatedOne("Party"); 119 if (billToParty != null) { 120 return billToParty; 121 } 122 } catch (GenericEntityException e) { 123 Debug.logError(e, "Trouble getting Party from Invoice", module); 124 } 125 126 List billToRoles = null; 128 try { 129 billToRoles = invoice.getRelated("InvoiceRole", UtilMisc.toMap("roleTypeId", "BILL_TO_CUSTOMER"), 130 UtilMisc.toList("-datetimePerformed")); 131 } catch (GenericEntityException e) { 132 Debug.logError(e, "Trouble getting InvoiceRole list", module); 133 } 134 135 if (billToRoles != null) { 136 GenericValue role = EntityUtil.getFirst(billToRoles); 137 GenericValue party = null; 138 try { 139 party = role.getRelatedOne("Party"); 140 } catch (GenericEntityException e) { 141 Debug.logError(e, "Trouble getting Party from InvoiceRole", module); 142 } 143 if (party != null) 144 return party; 145 } 146 return null; 147 } 148 149 150 public static GenericValue getBillFromParty(GenericValue invoice) { 151 try { 152 return invoice.getRelatedOne("FromParty"); 153 } catch (GenericEntityException e) { 154 Debug.logError(e, "Trouble getting FromParty from Invoice", module); 155 } 156 return null; 157 } 158 159 164 public static GenericValue getSendFromParty(GenericValue invoice) { 165 GenericValue billFromParty = getBillFromParty(invoice); 166 if (billFromParty != null) { 167 return billFromParty; 168 } 169 170 List sendFromRoles = null; 172 try { 173 sendFromRoles = invoice.getRelated("InvoiceRole", UtilMisc.toMap("roleTypeId", "BILL_FROM_VENDOR"), 174 UtilMisc.toList("-datetimePerformed")); 175 } catch (GenericEntityException e) { 176 Debug.logError(e, "Trouble getting InvoiceRole list", module); 177 } 178 179 if (sendFromRoles != null) { 180 GenericValue role = EntityUtil.getFirst(sendFromRoles); 181 GenericValue party = null; 182 try { 183 party = role.getRelatedOne("Party"); 184 } catch (GenericEntityException e) { 185 Debug.logError(e, "Trouble getting Party from InvoiceRole", module); 186 } 187 if (party != null) 188 return party; 189 } 190 return null; 191 } 192 193 198 public static GenericValue getBillToAddress(GenericValue invoice) { 199 return getInvoiceAddressByType(invoice, "BILLING_LOCATION"); 200 } 201 202 207 public static GenericValue getSendFromAddress(GenericValue invoice) { 208 return getInvoiceAddressByType(invoice, "PAYMENT_LOCATION"); 209 } 210 211 public static GenericValue getInvoiceAddressByType(GenericValue invoice, String contactMechPurposeTypeId) { 212 GenericDelegator delegator = invoice.getDelegator(); 213 List locations = null; 214 try { 216 locations = invoice.getRelated("InvoiceContactMech", UtilMisc.toMap("contactMechPurposeTypeId", contactMechPurposeTypeId), null); 217 } catch (GenericEntityException e) { 218 Debug.logError("Touble getting InvoiceContactMech entity list", module); 219 } 220 221 if (locations == null || locations.size() == 0) { 222 String destinationPartyId = null; 224 if (invoice.getString("invoiceTypeId").equals("SALES_INVOICE")) 225 destinationPartyId = invoice.getString("partyId"); 226 if (invoice.getString("invoiceTypeId").equals("PURCHASE_INVOICE")) 227 destinationPartyId = new String ("partyFrom"); 228 try { 229 locations = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMechPurpose", 230 UtilMisc.toMap("partyId", destinationPartyId, "contactMechPurposeTypeId", contactMechPurposeTypeId))); 231 } catch (GenericEntityException e) { 232 Debug.logError("Trouble getting contact party purpose list", module); 233 } 234 if (locations == null || locations.size() == 0) { 236 try { 237 locations = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMechPurpose", 238 UtilMisc.toMap("partyId", destinationPartyId, "contactMechPurposeTypeId", "GENERAL_LOCATION"))); 239 } catch (GenericEntityException e) { 240 Debug.logError("Trouble getting contact party purpose list", module); 241 } 242 } 243 } 244 245 GenericValue postalAddress = null; 247 GenericValue contactMech = null; 248 if (locations != null && locations.size() > 0) { 249 try { 250 contactMech = ((GenericValue) locations.get(0)).getRelatedOne("ContactMech"); 251 } catch (GenericEntityException e) { 252 Debug.logError(e, "Trouble getting Contact for contactMechId: " + contactMech.getString("contactMechId"), module); 253 } 254 255 if (contactMech.getString("contactMechTypeId").equals("POSTAL_ADDRESS")) { 256 try { 257 postalAddress = contactMech.getRelatedOne("PostalAddress"); 258 return postalAddress; 259 } catch (GenericEntityException e) { 260 Debug.logError(e, "Trouble getting PostalAddress for contactMechId: " + contactMech.getString("contactMechId"), module); 261 } 262 } 263 } 264 return contactMech; 265 } 266 267 private static GenericValue getAddressFromParty(GenericValue party, String purposeTypeId) { 268 if (party == null) return null; 269 270 GenericValue contactMech = null; 271 GenericValue postalAddress = null; 272 try { 273 List mecs = party.getRelated("PartyContactMechPurpose", 274 UtilMisc.toMap("contactMechPurposeTypeId", purposeTypeId), null); 275 if (mecs != null) { 276 List filteredMecs = EntityUtil.filterByDate(mecs); 277 GenericValue mecPurpose = EntityUtil.getFirst(filteredMecs); 278 if (mecPurpose != null) 279 contactMech = mecPurpose.getRelatedOne("ContactMech"); 280 } 281 } catch (GenericEntityException e) { 282 Debug.logError(e, "Trouble getting current ContactMech for Party/Purpose", module); 283 } 284 285 if (contactMech != null) { 286 if (contactMech.getString("contactMechTypeId").equals("POSTAL_ADDRESS")) { 287 try { 288 postalAddress = contactMech.getRelatedOne("PostalAddress"); 289 } catch (GenericEntityException e) { 290 Debug.logError(e, "Trouble getting PostalAddress from ContactMech", module); 291 } 292 } 293 } 294 295 if (postalAddress != null) 296 return postalAddress; 297 return null; 298 } 299 300 305 public static BigDecimal getInvoiceNotApplied(GenericDelegator delegator, String invoiceId) { 306 return InvoiceWorker.getInvoiceTotalBd(delegator, invoiceId).subtract(getInvoiceAppliedBd(delegator, invoiceId)); 307 } 308 public static BigDecimal getInvoiceNotApplied(GenericValue invoice) { 309 return InvoiceWorker.getInvoiceTotalBd(invoice).subtract(getInvoiceAppliedBd(invoice)); 310 } 311 318 public static BigDecimal getInvoiceNotApplied(GenericValue invoice, Timestamp asOfDateTime) { 319 return InvoiceWorker.getInvoiceTotalBd(invoice).subtract(getInvoiceAppliedBd(invoice, asOfDateTime)); 320 } 321 322 323 328 public static double getInvoiceApplied(GenericDelegator delegator, String invoiceId) { 329 return getInvoiceAppliedBd(delegator, invoiceId).doubleValue(); 330 } 331 332 public static BigDecimal getInvoiceAppliedBd(GenericDelegator delegator, String invoiceId) { 333 return getInvoiceAppliedBd(delegator, invoiceId, UtilDateTime.nowTimestamp()); 334 } 335 336 344 public static BigDecimal getInvoiceAppliedBd(GenericDelegator delegator, String invoiceId, Timestamp asOfDateTime) { 345 if (delegator == null) { 346 throw new IllegalArgumentException ("Null delegator is not allowed in this method"); 347 } 348 349 BigDecimal invoiceApplied = new BigDecimal ("0"); 350 List paymentApplications = null; 351 352 EntityConditionList dateCondition = new EntityConditionList(UtilMisc.toList( 354 new EntityExpr("effectiveDate", EntityOperator.EQUALS, null), 355 new EntityExpr("effectiveDate", EntityOperator.LESS_THAN_EQUAL_TO, asOfDateTime)), EntityOperator.OR); 356 EntityConditionList conditions = new EntityConditionList(UtilMisc.toList( 357 dateCondition, 358 new EntityExpr("invoiceId", EntityOperator.EQUALS, invoiceId)), 359 EntityOperator.AND); 360 361 try { 362 paymentApplications = delegator.findByCondition("PaymentAndApplication", conditions, null, UtilMisc.toList("effectiveDate")); 363 } catch (GenericEntityException e) { 364 Debug.logError(e, "Trouble getting paymentApplicationlist", module); 365 } 366 if (paymentApplications != null && paymentApplications.size() > 0) { 367 Iterator p = paymentApplications.iterator(); 368 while (p.hasNext()) { 369 GenericValue paymentApplication = (GenericValue) p.next(); 370 invoiceApplied = invoiceApplied.add(paymentApplication.getBigDecimal("amountApplied")).setScale(decimals,rounding); 371 } 372 } 373 return invoiceApplied; 374 } 375 380 public static double getInvoiceApplied(GenericValue invoice) { 381 return getInvoiceAppliedBd(invoice).doubleValue(); 382 } 383 391 public static BigDecimal getInvoiceAppliedBd(GenericValue invoice, Timestamp asOfDateTime) { 392 return getInvoiceAppliedBd(invoice.getDelegator(), invoice.getString("invoiceId"), asOfDateTime); 393 } 394 public static BigDecimal getInvoiceAppliedBd(GenericValue invoice) { 395 return getInvoiceAppliedBd(invoice, UtilDateTime.nowTimestamp()); 396 } 397 398 403 public static double getInvoiceItemApplied(GenericDelegator delegator, String invoiceId, String invoiceItemSeqId) { 404 return getInvoiceItemAppliedBd(delegator, invoiceId, invoiceItemSeqId).doubleValue(); 405 } 406 407 415 public static BigDecimal getInvoiceItemAppliedBd(GenericDelegator delegator, String invoiceId, String invoiceItemSeqId) { 416 if (delegator == null) { 417 throw new IllegalArgumentException ("Null delegator is not allowed in this method"); 418 } 419 420 GenericValue invoiceItem = null; 421 try { 422 invoiceItem = delegator.findByPrimaryKey("Invoice", UtilMisc.toMap("invoiceId", invoiceId,"invoiceItemSeqId", invoiceItemSeqId)); 423 } catch (GenericEntityException e) { 424 Debug.logError(e, "Problem getting InvoiceItem", module); 425 } 426 427 if (invoiceItem == null) { 428 throw new IllegalArgumentException ("The invoiceId/itemSeqId passed does not match an existing invoiceItem"); 429 } 430 431 return getInvoiceItemAppliedBd(invoiceItem); 432 } 433 434 439 public static double getInvoiceItemApplied(GenericValue invoiceItem) { 440 return getInvoiceItemAppliedBd(invoiceItem).doubleValue(); 441 } 442 public static BigDecimal getInvoiceItemAppliedBd(GenericValue invoiceItem) { 443 BigDecimal invoiceItemApplied = new BigDecimal ("0"); 444 List paymentApplications = null; 445 try { 446 paymentApplications = invoiceItem.getRelated("PaymentApplication"); 447 } catch (GenericEntityException e) { 448 Debug.logError(e, "Trouble getting paymentApplicationlist", module); 449 } 450 if (paymentApplications != null && paymentApplications.size() > 0) { 451 Iterator p = paymentApplications.iterator(); 452 while (p.hasNext()) { 453 GenericValue paymentApplication = (GenericValue) p.next(); 454 invoiceItemApplied = invoiceItemApplied.add(paymentApplication.getBigDecimal("amountApplied")).setScale(decimals,rounding); 455 } 456 } 457 return invoiceItemApplied; 458 } 459 460 461 } 462 | Popular Tags |