001: /*
002: * $Id: BillingAccountWorker.java,v 1.3 2003/09/04 19:23:52 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.payment;
025:
026: import java.util.Iterator;
027: import java.util.LinkedList;
028: import java.util.List;
029: import java.util.Map;
030:
031: import org.ofbiz.accounting.invoice.InvoiceWorker;
032: import org.ofbiz.base.util.Debug;
033: import org.ofbiz.base.util.UtilMisc;
034: import org.ofbiz.entity.GenericDelegator;
035: import org.ofbiz.entity.GenericEntityException;
036: import org.ofbiz.entity.GenericValue;
037: import org.ofbiz.entity.condition.EntityExpr;
038: import org.ofbiz.entity.condition.EntityOperator;
039: import org.ofbiz.order.order.OrderReadHelper;
040: import org.ofbiz.service.DispatchContext;
041: import org.ofbiz.service.ServiceUtil;
042:
043: /**
044: * Worker methods for BillingAccounts
045: *
046: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
047: * @version $Revision: 1.3 $
048: * @since 2.1
049: */
050: public class BillingAccountWorker {
051:
052: public static final String module = BillingAccountWorker.class
053: .getName();
054:
055: public static double getBillingAccountBalance(
056: GenericValue billingAccount) {
057: return getBillingAccountBalance(billingAccount.getDelegator(),
058: billingAccount.getString("billingAccountId"));
059: }
060:
061: public static double getBillingAccountBalance(
062: GenericDelegator delegator, String billingAccountId) {
063: double balance = 0.00;
064:
065: // first get all the pending orders (not cancelled, rejected or completed)
066: List orderHeaders = null;
067: List exprs1 = new LinkedList();
068: exprs1.add(new EntityExpr("billingAccountId",
069: EntityOperator.EQUALS, billingAccountId));
070: exprs1.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL,
071: "ORDER_REJECTED"));
072: exprs1.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL,
073: "ORDER_CANCELLED"));
074: exprs1.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL,
075: "ORDER_COMPLETED"));
076: try {
077: orderHeaders = delegator.findByAnd("OrderHeader", exprs1);
078: } catch (GenericEntityException e) {
079: Debug.logError(e, "Trouble getting OrderHeader list",
080: module);
081: return 0.01;
082: }
083: if (orderHeaders != null) {
084: Iterator ohi = orderHeaders.iterator();
085: while (ohi.hasNext()) {
086: GenericValue orderHeader = (GenericValue) ohi.next();
087: OrderReadHelper orh = new OrderReadHelper(orderHeader);
088: balance += orh.getOrderGrandTotal();
089: }
090: }
091:
092: // next get all the un-paid invoices (this will include all completed orders)
093: List invoices = null;
094: List exprs2 = new LinkedList();
095: exprs2.add(new EntityExpr("billingAccountId",
096: EntityOperator.EQUALS, billingAccountId));
097: exprs2.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL,
098: "INVOICE_CANCELLED"));
099: try {
100: invoices = delegator.findByAnd("Invoice", exprs2);
101: } catch (GenericEntityException e) {
102: Debug.logError(e, "Trouble getting Invoice list", module);
103: return 0.01;
104: }
105: if (invoices != null) {
106: Iterator ii = invoices.iterator();
107: while (ii.hasNext()) {
108: GenericValue invoice = (GenericValue) ii.next();
109: balance += InvoiceWorker.getInvoiceTotal(invoice);
110: }
111: }
112:
113: // finally apply any payments to the balance
114: List credits = null;
115: List exprs3 = new LinkedList();
116: exprs3.add(new EntityExpr("billingAccountId",
117: EntityOperator.EQUALS, billingAccountId));
118: try {
119: credits = delegator.findByAnd("PaymentApplication", exprs3);
120: } catch (GenericEntityException e) {
121: Debug.logError(e,
122: "Trouble getting PaymentApplication list", module);
123: return 0.01;
124: }
125: if (credits != null) {
126: Iterator ci = credits.iterator();
127: while (ci.hasNext()) {
128: GenericValue credit = (GenericValue) ci.next();
129: Double amount = credit.getDouble("amountApplied");
130: if (amount != null) {
131: balance -= amount.doubleValue();
132: }
133: }
134: }
135:
136: return balance;
137: }
138:
139: public static Map calcBillingAccountBalance(DispatchContext dctx,
140: Map context) {
141: GenericDelegator delegator = dctx.getDelegator();
142: String billingAccountId = (String) context
143: .get("billingAccountId");
144: GenericValue billingAccount = null;
145: try {
146: billingAccount = delegator.findByPrimaryKey(
147: "BillingAccount", UtilMisc.toMap(
148: "billingAccountId", billingAccountId));
149: } catch (GenericEntityException e) {
150: Debug.logError(e, module);
151: return ServiceUtil
152: .returnError("Unable to locate billing account #"
153: + billingAccountId);
154: }
155:
156: if (billingAccount == null) {
157: return ServiceUtil
158: .returnError("Unable to locate billing account #"
159: + billingAccountId);
160: }
161:
162: Map result = ServiceUtil.returnSuccess();
163: result.put("accountBalance", new Double(
164: getBillingAccountBalance(delegator, billingAccountId)));
165: result.put("billingAccount", billingAccount);
166: return result;
167: }
168:
169: }
|