Author: apatel
Date: Thu Jul 30 05:23:03 2009
New Revision: 799171
URL: http://svn.apache.org/viewvc?rev=799171&view=rev
Log:
Adding Utility methods to figure out Invoice types. Patch from OFBIZ-2769.
Thanks Rishi, Chirag.
Modified:
ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/util/UtilAccounting.java
Modified:
ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/util/UtilAccounting.java
URL:
http://svn.apache.org/viewvc/ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/util/UtilAccounting.java?rev=799171&r1=799170&r2=799171&view=diff
==============================================================================
---
ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/util/UtilAccounting.java
(original)
+++
ofbiz/trunk/applications/accounting/src/org/ofbiz/accounting/util/UtilAccounting.java
Thu Jul 30 05:23:03 2009
@@ -261,4 +261,60 @@
public static boolean isExpenseAccount(GenericValue account) throws
GenericEntityException {
return isAccountClass(account, "EXPENSE");
}
+
+ /**
+ * Recurses up invoice type tree via parentTypeId to see if input invoice
type ID is in tree.
+ */
+ private static boolean isInvoiceTypeRecurse(GenericValue invoiceType,
String inputTypeId) throws GenericEntityException {
+
+ // first check the invoiceTypeId and parentTypeId against inputTypeId
+ String invoiceTypeId = invoiceType.getString("invoiceTypeId");
+ String parentTypeId = invoiceType.getString("parentTypeId");
+ if (parentTypeId == null || invoiceTypeId.equals(parentTypeId)) {
+ return false;
+ }
+ if (parentTypeId.equals(inputTypeId)) {
+ return true;
+ }
+
+ // otherwise, we have to go to the grandparent (recurse)
+ return
isInvoiceTypeRecurse(invoiceType.getRelatedOne("ParentInvoiceType"),
inputTypeId);
+ }
+
+ /**
+ * Checks if a invoice is of a specified InvoiceType.invoiceTypeId. Return
false if invoice is null. It's better to use
+ * more specific calls like isPurchaseInvoice().
+ */
+ public static boolean isInvoiceType(GenericValue invoice, String
inputTypeId) throws GenericEntityException {
+ if (invoice == null) {
+ return false;
+ }
+
+ GenericValue invoiceType = invoice.getRelatedOneCache("InvoiceType");
+ if (invoiceType == null) {
+ throw new GenericEntityException("Cannot find InvoiceType for
invoiceId " + invoice.getString("invoiceId"));
+ }
+
+ String invoiceTypeId = invoiceType.getString("invoiceTypeId");
+ if (inputTypeId.equals(invoiceTypeId)) {
+ return true;
+ }
+
+ // recurse up tree
+ return isInvoiceTypeRecurse(invoiceType, inputTypeId);
+ }
+
+
+ public static boolean isPurchaseInvoice(GenericValue invoice) throws
GenericEntityException {
+ return isInvoiceType(invoice, "PURCHASE_INVOICE");
+ }
+
+ public static boolean isSalesInvoice(GenericValue invoice) throws
GenericEntityException {
+ return isInvoiceType(invoice, "SALES_INVOICE");
+ }
+
+ public static boolean isTemplate(GenericValue invoice) throws
GenericEntityException {
+ return isInvoiceType(invoice, "TEMPLATE");
+ }
+
}