001: /*
002: * $Id: InvoiceWorker.java,v 1.1 2003/08/18 17:31:37 ajzeneski Exp $
003: *
004: * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.accounting.invoice;
025:
026: import java.text.DecimalFormat;
027: import java.text.ParseException;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: import org.ofbiz.base.util.Debug;
032: import org.ofbiz.base.util.UtilMisc;
033: import org.ofbiz.base.util.UtilProperties;
034: import org.ofbiz.entity.GenericDelegator;
035: import org.ofbiz.entity.GenericEntityException;
036: import org.ofbiz.entity.GenericValue;
037: import org.ofbiz.entity.util.EntityUtil;
038:
039: /**
040: * InvoiceWorker - Worker methods of invoices
041: *
042: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
043: * @version $Revision: 1.1 $
044: * @since 2.1
045: */
046: public class InvoiceWorker {
047:
048: public static String module = InvoiceWorker.class.getName();
049:
050: /**
051: * Method to return the total amount of an invoice
052: * @param invoice GenericValue object of the Invoice
053: * @return the invoice total as double
054: */
055: public static double getInvoiceTotal(GenericDelegator delegator,
056: String invoiceId) {
057: if (delegator == null) {
058: throw new IllegalArgumentException(
059: "Null delegator is not allowed in this method");
060: }
061:
062: GenericValue invoice = null;
063: try {
064: invoice = delegator.findByPrimaryKey("Invoice", UtilMisc
065: .toMap("invoiceId", invoiceId));
066: } catch (GenericEntityException e) {
067: Debug.logError(e, "Problem getting Invoice", module);
068: }
069:
070: if (invoice == null) {
071: throw new IllegalArgumentException(
072: "The invoiceId passed does not match an existing invoice");
073: }
074:
075: return getInvoiceTotal(invoice);
076: }
077:
078: /**
079: * Method to return the total amount of an invoice
080: * @param invoice GenericValue object of the Invoice
081: * @return the invoice total as double
082: */
083: public static double getInvoiceTotal(GenericValue invoice) {
084: double invoiceTotal = 0.00;
085: List invoiceItems = null;
086: try {
087: invoiceItems = invoice.getRelated("InvoiceItem");
088: } catch (GenericEntityException e) {
089: Debug.logError(e, "Trouble getting InvoiceItem list",
090: module);
091: }
092: if (invoiceItems != null && invoiceItems.size() > 0) {
093: Iterator invoiceItemsIter = invoiceItems.iterator();
094: while (invoiceItemsIter.hasNext()) {
095: GenericValue invoiceItem = (GenericValue) invoiceItemsIter
096: .next();
097: Double amount = invoiceItem.getDouble("amount");
098: Double quantity = invoiceItem.getDouble("quantity");
099: if (amount == null)
100: amount = new Double(0.00);
101: if (quantity == null)
102: quantity = new Double(1);
103: invoiceTotal += amount.doubleValue()
104: * quantity.doubleValue();
105: }
106: }
107:
108: String currencyFormat = UtilProperties.getPropertyValue(
109: "general.properties", "currency.decimal.format",
110: "##0.00");
111: DecimalFormat formatter = new DecimalFormat(currencyFormat);
112: String invoiceTotalString = formatter.format(invoiceTotal);
113: Double formattedTotal = null;
114: try {
115: formattedTotal = new Double(formatter.parse(
116: invoiceTotalString).doubleValue());
117: } catch (ParseException e) {
118: Debug
119: .logError(
120: e,
121: "Problem getting parsed tax amount; using the primitive value",
122: module);
123: formattedTotal = new Double(invoiceTotal);
124: }
125:
126: return formattedTotal.doubleValue();
127: }
128:
129: /**
130: * Method to obtain the bill to party for an invoice
131: * @param invoice GenericValue object of the Invoice
132: * @return GenericValue object of the Party
133: */
134: public static GenericValue getBillToParty(GenericValue invoice) {
135: List billToRoles = null;
136: try {
137: billToRoles = invoice.getRelated("InvoiceRole", UtilMisc
138: .toMap("roleTypeId", "BILL_TO_CUSTOMER"), UtilMisc
139: .toList("-datetimePerformed"));
140: } catch (GenericEntityException e) {
141: Debug.logError(e, "Trouble getting InvoiceRole list",
142: module);
143: }
144:
145: if (billToRoles != null) {
146: GenericValue role = EntityUtil.getFirst(billToRoles);
147: GenericValue party = null;
148: try {
149: party = role.getRelatedOne("Party");
150: } catch (GenericEntityException e) {
151: Debug.logError(e,
152: "Trouble getting Party from InvoiceRole",
153: module);
154: }
155: if (party != null)
156: return party;
157: }
158: return null;
159: }
160:
161: /**
162: * Method to obtain the send from party for an invoice
163: * @param invoice GenericValue object of the Invoice
164: * @return GenericValue object of the Party
165: */
166: public static GenericValue getSendFromParty(GenericValue invoice) {
167: List sendFromRoles = null;
168: try {
169: sendFromRoles = invoice.getRelated("InvoiceRole", UtilMisc
170: .toMap("roleTypeId", "BILL_FROM_VENDOR"), UtilMisc
171: .toList("-datetimePerformed"));
172: } catch (GenericEntityException e) {
173: Debug.logError(e, "Trouble getting InvoiceRole list",
174: module);
175: }
176:
177: if (sendFromRoles != null) {
178: GenericValue role = EntityUtil.getFirst(sendFromRoles);
179: GenericValue party = null;
180: try {
181: party = role.getRelatedOne("Party");
182: } catch (GenericEntityException e) {
183: Debug.logError(e,
184: "Trouble getting Party from InvoiceRole",
185: 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(
212: GenericValue invoice, String contactMechPurposeTypeId) {
213: GenericDelegator delegator = invoice.getDelegator();
214: List locations = null;
215: try {
216: locations = invoice.getRelated("InvoiceContactMech",
217: UtilMisc.toMap("contactMechPurposeTypeId",
218: contactMechPurposeTypeId), null);
219: } catch (GenericEntityException e) {
220: Debug.logError(
221: "Touble getting InvoiceContactMech entity list",
222: module);
223: }
224:
225: GenericValue postalAddress = null;
226: if (locations != null && locations.size() > 0) {
227: GenericValue purpose = EntityUtil.getFirst(locations);
228: try {
229: postalAddress = delegator.findByPrimaryKey(
230: "PostalAddress", UtilMisc.toMap(
231: "contactMechId", purpose
232: .getString("contactMechId")));
233: } catch (GenericEntityException e) {
234: Debug.logError(e,
235: "Trouble getting PostalAddress for contactMechId: "
236: + purpose.getString("contactMechId"),
237: module);
238: }
239: }
240:
241: return postalAddress;
242: }
243:
244: private static GenericValue getAddressFromParty(GenericValue party,
245: String purposeTypeId) {
246: if (party == null)
247: return null;
248:
249: GenericValue contactMech = null;
250: GenericValue postalAddress = null;
251: try {
252: List mecs = party.getRelated("PartyContactMechPurpose",
253: UtilMisc.toMap("contactMechPurposeTypeId",
254: purposeTypeId), null);
255: if (mecs != null) {
256: List filteredMecs = EntityUtil.filterByDate(mecs);
257: GenericValue mecPurpose = EntityUtil
258: .getFirst(filteredMecs);
259: if (mecPurpose != null)
260: contactMech = mecPurpose
261: .getRelatedOne("ContactMech");
262: }
263: } catch (GenericEntityException e) {
264: Debug
265: .logError(
266: e,
267: "Trouble getting current ContactMech for Party/Purpose",
268: module);
269: }
270:
271: if (contactMech != null) {
272: if (contactMech.getString("contactMechTypeId").equals(
273: "POSTAL_ADDRESS")) {
274: try {
275: postalAddress = contactMech
276: .getRelatedOne("PostalAddress");
277: } catch (GenericEntityException e) {
278: Debug
279: .logError(
280: e,
281: "Trouble getting PostalAddress from ContactMech",
282: module);
283: }
284: }
285: }
286:
287: if (postalAddress != null)
288: return postalAddress;
289: return null;
290: }
291:
292: }
|