KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > accounting > invoice > InvoiceWorker


1 /*
2  * $Id: InvoiceWorker.java 7051 2006-03-22 22:34:02Z sichen $
3  *
4  * Copyright 2003-2006 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7  * use this file except in compliance with the License. You may obtain a copy of
8  * the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */

18 package org.ofbiz.accounting.invoice;
19
20 import java.math.BigDecimal JavaDoc;
21 import java.sql.Timestamp JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
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 /**
38  * InvoiceWorker - Worker methods of invoices
39  *
40  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
41  * @version $Rev: 7051 $
42  * @since 2.1
43  */

44 public class InvoiceWorker {
45     
46     public static String JavaDoc module = InvoiceWorker.class.getName();
47     private static int decimals = UtilNumber.getBigDecimalScale("invoice.decimals");
48     private static int rounding = UtilNumber.getBigDecimalRoundingMode("invoice.rounding");
49
50     /**
51      * Method to return the total amount of an invoice
52      * @param invoice GenericValue object of the Invoice
53      * @return the invoice total as double
54      */

55     public static double getInvoiceTotal(GenericDelegator delegator, String JavaDoc invoiceId) {
56         return getInvoiceTotalBd(delegator, invoiceId).doubleValue();
57     }
58
59     public static BigDecimal JavaDoc getInvoiceTotalBd(GenericDelegator delegator, String JavaDoc invoiceId) {
60         if (delegator == null) {
61             throw new IllegalArgumentException JavaDoc("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 JavaDoc("The invoiceId passed does not match an existing invoice");
73         }
74         
75         return getInvoiceTotalBd(invoice);
76     }
77     
78     /**
79      * Method to return the total amount of an invoice
80      * @param invoice GenericValue object of the Invoice
81      * @return the invoice total as double
82      */

83         public static double getInvoiceTotal(GenericValue invoice) {
84             return getInvoiceTotalBd(invoice).doubleValue();
85         }
86         
87         public static BigDecimal JavaDoc getInvoiceTotalBd(GenericValue invoice) {
88         BigDecimal JavaDoc invoiceTotal = new BigDecimal JavaDoc("0");
89         List JavaDoc 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 JavaDoc invoiceItemsIter = invoiceItems.iterator();
97             while (invoiceItemsIter.hasNext()) {
98                 GenericValue invoiceItem = (GenericValue) invoiceItemsIter.next();
99                 BigDecimal JavaDoc amount = invoiceItem.getBigDecimal("amount");
100                 BigDecimal JavaDoc quantity = invoiceItem.getBigDecimal("quantity");
101                 if (amount == null)
102                     amount = new BigDecimal JavaDoc("0");
103                 if (quantity == null)
104                     quantity = new BigDecimal JavaDoc("1");
105                 invoiceTotal = invoiceTotal.add( amount.multiply(quantity)).setScale(decimals,rounding);
106             }
107         }
108         return invoiceTotal;
109     }
110
111     /**
112      * Method to obtain the bill to party for an invoice. Note that invoice.partyId is the bill to party.
113      * @param invoice GenericValue object of the Invoice
114      * @return GenericValue object of the Party
115      */

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         // remaining code is the old method, which we leave here for compatibility purposes
127
List JavaDoc 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     /** Convenience method to obtain the bill from party for an invoice. Note that invoice.partyIdFrom is the bill from party. */
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     /**
160       * Method to obtain the send from party for an invoice
161       * @param invoice GenericValue object of the Invoice
162       * @return GenericValue object of the Party
163       */

164     public static GenericValue getSendFromParty(GenericValue invoice) {
165         GenericValue billFromParty = getBillFromParty(invoice);
166         if (billFromParty != null) {
167             return billFromParty;
168         }
169
170         // remaining code is the old method, which we leave here for compatibility purposes
171
List JavaDoc 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     /**
194       * Method to obtain the billing address for an invoice
195       * @param invoice GenericValue object of the Invoice
196       * @return GenericValue object of the PostalAddress
197       */

198     public static GenericValue getBillToAddress(GenericValue invoice) {
199         return getInvoiceAddressByType(invoice, "BILLING_LOCATION");
200     }
201     
202     /**
203       * Method to obtain the sending address for an invoice
204       * @param invoice GenericValue object of the Invoice
205       * @return GenericValue object of the PostalAddress
206       */

207     public static GenericValue getSendFromAddress(GenericValue invoice) {
208         return getInvoiceAddressByType(invoice, "PAYMENT_LOCATION");
209     }
210     
211     public static GenericValue getInvoiceAddressByType(GenericValue invoice, String JavaDoc contactMechPurposeTypeId) {
212         GenericDelegator delegator = invoice.getDelegator();
213         List JavaDoc locations = null;
214         // first try InvoiceContactMech to see if we can find the address needed
215
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             // if no locations found get it from the PartyAndContactMech using the from and to party on the invoice
223
String JavaDoc 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 JavaDoc("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 still not found get it from the general location
235
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         // now return the first PostalAddress from the locations
246
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 JavaDoc purposeTypeId) {
268         if (party == null) return null;
269         
270         GenericValue contactMech = null;
271         GenericValue postalAddress = null;
272         try {
273             List JavaDoc mecs = party.getRelated("PartyContactMechPurpose",
274                 UtilMisc.toMap("contactMechPurposeTypeId", purposeTypeId), null);
275             if (mecs != null) {
276                 List JavaDoc 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     /**
301      * Method to return the total amount of an invoice which is not yet applied to a payment
302      * @param invoice GenericValue object of the Invoice
303      * @return the invoice total as double
304      */

305     public static BigDecimal JavaDoc getInvoiceNotApplied(GenericDelegator delegator, String JavaDoc invoiceId) {
306         return InvoiceWorker.getInvoiceTotalBd(delegator, invoiceId).subtract(getInvoiceAppliedBd(delegator, invoiceId));
307     }
308     public static BigDecimal JavaDoc getInvoiceNotApplied(GenericValue invoice) {
309         return InvoiceWorker.getInvoiceTotalBd(invoice).subtract(getInvoiceAppliedBd(invoice));
310     }
311     /**
312      * Returns amount not applied (ie, still outstanding) of an invoice at an asOfDate, based on Payment.effectiveDate <= asOfDateTime
313      *
314      * @param invoice
315      * @param asOfDateTime
316      * @return
317      */

318     public static BigDecimal JavaDoc getInvoiceNotApplied(GenericValue invoice, Timestamp JavaDoc asOfDateTime) {
319         return InvoiceWorker.getInvoiceTotalBd(invoice).subtract(getInvoiceAppliedBd(invoice, asOfDateTime));
320     }
321
322     
323     /**
324      * Method to return the total amount of an invoice which is applied to a payment
325      * @param invoice GenericValue object of the Invoice
326      * @return the invoice total as double
327      */

328     public static double getInvoiceApplied(GenericDelegator delegator, String JavaDoc invoiceId) {
329         return getInvoiceAppliedBd(delegator, invoiceId).doubleValue();
330     }
331         
332     public static BigDecimal JavaDoc getInvoiceAppliedBd(GenericDelegator delegator, String JavaDoc invoiceId) {
333         return getInvoiceAppliedBd(delegator, invoiceId, UtilDateTime.nowTimestamp());
334     }
335     
336     /**
337      * Returns amount applied to invoice before an asOfDateTime, based on Payment.effectiveDate <= asOfDateTime
338      *
339      * @param delegator
340      * @param invoiceId
341      * @param asOfDateTime - a Timestamp
342      * @return
343      */

344     public static BigDecimal JavaDoc getInvoiceAppliedBd(GenericDelegator delegator, String JavaDoc invoiceId, Timestamp JavaDoc asOfDateTime) {
345         if (delegator == null) {
346             throw new IllegalArgumentException JavaDoc("Null delegator is not allowed in this method");
347         }
348         
349         BigDecimal JavaDoc invoiceApplied = new BigDecimal JavaDoc("0");
350         List JavaDoc paymentApplications = null;
351         
352         // lookup payment applications which took place before the asOfDateTime for this invoice
353
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 JavaDoc 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     /**
376      * Method to return the total amount of an invoice which is applied to a payment
377      * @param invoice GenericValue object of the Invoice
378      * @return the applied total as double
379      */

380     public static double getInvoiceApplied(GenericValue invoice) {
381         return getInvoiceAppliedBd(invoice).doubleValue();
382     }
383     /**
384      * Big decimal version of getInvoiceApplied
385      *
386      * @param delegator
387      * @param invoiceId
388      * @param invoiceItemSeqId
389      * @return
390      */

391     public static BigDecimal JavaDoc getInvoiceAppliedBd(GenericValue invoice, Timestamp JavaDoc asOfDateTime) {
392         return getInvoiceAppliedBd(invoice.getDelegator(), invoice.getString("invoiceId"), asOfDateTime);
393     }
394     public static BigDecimal JavaDoc getInvoiceAppliedBd(GenericValue invoice) {
395         return getInvoiceAppliedBd(invoice, UtilDateTime.nowTimestamp());
396     }
397     
398     /**
399      * Method to return the amount of an invoiceItem which is applied to a payment
400      * @param invoice GenericValue object of the Invoice
401      * @return the invoice total as double
402      */

403     public static double getInvoiceItemApplied(GenericDelegator delegator, String JavaDoc invoiceId, String JavaDoc invoiceItemSeqId) {
404         return getInvoiceItemAppliedBd(delegator, invoiceId, invoiceItemSeqId).doubleValue();
405     }
406     
407     /**
408      * Big decimal version of getInvoiceApplied
409      *
410      * @param delegator
411      * @param invoiceId
412      * @param invoiceItemSeqId
413      * @return
414      */

415     public static BigDecimal JavaDoc getInvoiceItemAppliedBd(GenericDelegator delegator, String JavaDoc invoiceId, String JavaDoc invoiceItemSeqId) {
416         if (delegator == null) {
417             throw new IllegalArgumentException JavaDoc("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 JavaDoc("The invoiceId/itemSeqId passed does not match an existing invoiceItem");
429         }
430         
431         return getInvoiceItemAppliedBd(invoiceItem);
432     }
433     
434     /**
435      * Method to return the total amount of an invoiceItem which is applied to a payment
436      * @param invoice GenericValue object of the Invoice
437      * @return the applied total as double
438      */

439     public static double getInvoiceItemApplied(GenericValue invoiceItem) {
440         return getInvoiceItemAppliedBd(invoiceItem).doubleValue();
441     }
442     public static BigDecimal JavaDoc getInvoiceItemAppliedBd(GenericValue invoiceItem) {
443         BigDecimal JavaDoc invoiceItemApplied = new BigDecimal JavaDoc("0");
444         List JavaDoc 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 JavaDoc 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