?? invoiceworker.java
字號:
/* * $Id: InvoiceWorker.java 7051 2006-03-22 22:34:02Z sichen $ * * Copyright 2003-2006 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */package org.ofbiz.accounting.invoice;import java.math.BigDecimal;import java.sql.Timestamp;import java.util.Iterator;import java.util.List;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilNumber;import org.ofbiz.entity.condition.EntityConditionList;import org.ofbiz.entity.condition.EntityExpr;import org.ofbiz.entity.condition.EntityOperator;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.util.EntityUtil;/** * InvoiceWorker - Worker methods of invoices * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 7051 $ * @since 2.1 */public class InvoiceWorker { public static String module = InvoiceWorker.class.getName(); private static int decimals = UtilNumber.getBigDecimalScale("invoice.decimals"); private static int rounding = UtilNumber.getBigDecimalRoundingMode("invoice.rounding"); /** * Method to return the total amount of an invoice * @param invoice GenericValue object of the Invoice * @return the invoice total as double */ public static double getInvoiceTotal(GenericDelegator delegator, String invoiceId) { return getInvoiceTotalBd(delegator, invoiceId).doubleValue(); } public static BigDecimal getInvoiceTotalBd(GenericDelegator delegator, String invoiceId) { if (delegator == null) { throw new IllegalArgumentException("Null delegator is not allowed in this method"); } GenericValue invoice = null; try { invoice = delegator.findByPrimaryKey("Invoice", UtilMisc.toMap("invoiceId", invoiceId)); } catch (GenericEntityException e) { Debug.logError(e, "Problem getting Invoice", module); } if (invoice == null) { throw new IllegalArgumentException("The invoiceId passed does not match an existing invoice"); } return getInvoiceTotalBd(invoice); } /** * Method to return the total amount of an invoice * @param invoice GenericValue object of the Invoice * @return the invoice total as double */ public static double getInvoiceTotal(GenericValue invoice) { return getInvoiceTotalBd(invoice).doubleValue(); } public static BigDecimal getInvoiceTotalBd(GenericValue invoice) { BigDecimal invoiceTotal = new BigDecimal("0"); List invoiceItems = null; try { invoiceItems = invoice.getRelated("InvoiceItem"); } catch (GenericEntityException e) { Debug.logError(e, "Trouble getting InvoiceItem list", module); } if (invoiceItems != null && invoiceItems.size() > 0) { Iterator invoiceItemsIter = invoiceItems.iterator(); while (invoiceItemsIter.hasNext()) { GenericValue invoiceItem = (GenericValue) invoiceItemsIter.next(); BigDecimal amount = invoiceItem.getBigDecimal("amount"); BigDecimal quantity = invoiceItem.getBigDecimal("quantity"); if (amount == null) amount = new BigDecimal("0"); if (quantity == null) quantity = new BigDecimal("1"); invoiceTotal = invoiceTotal.add( amount.multiply(quantity)).setScale(decimals,rounding); } } return invoiceTotal; } /** * Method to obtain the bill to party for an invoice. Note that invoice.partyId is the bill to party. * @param invoice GenericValue object of the Invoice * @return GenericValue object of the Party */ public static GenericValue getBillToParty(GenericValue invoice) { try { GenericValue billToParty = invoice.getRelatedOne("Party"); if (billToParty != null) { return billToParty; } } catch (GenericEntityException e) { Debug.logError(e, "Trouble getting Party from Invoice", module); } // remaining code is the old method, which we leave here for compatibility purposes List billToRoles = null; try { billToRoles = invoice.getRelated("InvoiceRole", UtilMisc.toMap("roleTypeId", "BILL_TO_CUSTOMER"), UtilMisc.toList("-datetimePerformed")); } catch (GenericEntityException e) { Debug.logError(e, "Trouble getting InvoiceRole list", module); } if (billToRoles != null) { GenericValue role = EntityUtil.getFirst(billToRoles); GenericValue party = null; try { party = role.getRelatedOne("Party"); } catch (GenericEntityException e) { Debug.logError(e, "Trouble getting Party from InvoiceRole", module); } if (party != null) return party; } return null; } /** Convenience method to obtain the bill from party for an invoice. Note that invoice.partyIdFrom is the bill from party. */ public static GenericValue getBillFromParty(GenericValue invoice) { try { return invoice.getRelatedOne("FromParty"); } catch (GenericEntityException e) { Debug.logError(e, "Trouble getting FromParty from Invoice", module); } return null; } /** * Method to obtain the send from party for an invoice * @param invoice GenericValue object of the Invoice * @return GenericValue object of the Party */ public static GenericValue getSendFromParty(GenericValue invoice) { GenericValue billFromParty = getBillFromParty(invoice); if (billFromParty != null) { return billFromParty; } // remaining code is the old method, which we leave here for compatibility purposes List sendFromRoles = null; try { sendFromRoles = invoice.getRelated("InvoiceRole", UtilMisc.toMap("roleTypeId", "BILL_FROM_VENDOR"), UtilMisc.toList("-datetimePerformed")); } catch (GenericEntityException e) { Debug.logError(e, "Trouble getting InvoiceRole list", module); } if (sendFromRoles != null) { GenericValue role = EntityUtil.getFirst(sendFromRoles); GenericValue party = null; try { party = role.getRelatedOne("Party"); } catch (GenericEntityException e) { Debug.logError(e, "Trouble getting Party from InvoiceRole", module); } if (party != null) return party; } return null; } /** * Method to obtain the billing address for an invoice * @param invoice GenericValue object of the Invoice * @return GenericValue object of the PostalAddress */ public static GenericValue getBillToAddress(GenericValue invoice) { return getInvoiceAddressByType(invoice, "BILLING_LOCATION"); } /** * Method to obtain the sending address for an invoice * @param invoice GenericValue object of the Invoice * @return GenericValue object of the PostalAddress */ public static GenericValue getSendFromAddress(GenericValue invoice) { return getInvoiceAddressByType(invoice, "PAYMENT_LOCATION"); } public static GenericValue getInvoiceAddressByType(GenericValue invoice, String contactMechPurposeTypeId) { GenericDelegator delegator = invoice.getDelegator(); List locations = null; // first try InvoiceContactMech to see if we can find the address needed try { locations = invoice.getRelated("InvoiceContactMech", UtilMisc.toMap("contactMechPurposeTypeId", contactMechPurposeTypeId), null); } catch (GenericEntityException e) { Debug.logError("Touble getting InvoiceContactMech entity list", module); } if (locations == null || locations.size() == 0) { // if no locations found get it from the PartyAndContactMech using the from and to party on the invoice String destinationPartyId = null; if (invoice.getString("invoiceTypeId").equals("SALES_INVOICE")) destinationPartyId = invoice.getString("partyId"); if (invoice.getString("invoiceTypeId").equals("PURCHASE_INVOICE")) destinationPartyId = new String("partyFrom"); try { locations = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMechPurpose", UtilMisc.toMap("partyId", destinationPartyId, "contactMechPurposeTypeId", contactMechPurposeTypeId))); } catch (GenericEntityException e) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -