📄 invoiceservices.java
字号:
/* * $Id: InvoiceServices.java 7251 2006-04-10 05:39:15Z jacopo $ * * 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.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Set;import javolution.util.FastMap;import org.ofbiz.accounting.payment.BillingAccountWorker;import org.ofbiz.accounting.payment.PaymentWorker;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.base.util.UtilFormatOut;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilNumber;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.condition.EntityCondition;import org.ofbiz.entity.condition.EntityOperator;import org.ofbiz.entity.condition.EntityExpr;import org.ofbiz.order.order.OrderReadHelper;import org.ofbiz.product.product.ProductWorker;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ServiceUtil;/** * InvoiceServices - Services for creating invoices * * Note that throughout this file we use BigDecimal to do arithmetic. It is * critical to understand the way BigDecimal works if you wish to modify the * computations in this file. The most important things to keep in mind: * * Critically important: BigDecimal arithmetic methods like add(), * multiply(), divide() do not modify the BigDecimal itself. Instead, they * return a new BigDecimal. For example, to keep a running total of an * amount, make sure you do this: * * amount = amount.add(subAmount); * * and not this, * * amount.add(subAmount); * * Use .setScale(scale, roundingMode) after every computation to scale and * round off the decimals. Check the code to see how the scale and * roundingMode are obtained and how the function is used. * * use .compareTo() to compare big decimals * * ex. (amountOne.compareTo(amountTwo) == 1) * checks if amountOne is greater than amountTwo * * Use .signum() to test if value is negative, zero, or positive * * ex. (amountOne.signum() == 1) * checks if the amount is a positive non-zero number * * Never use the .equals() function becaues it considers 2.0 not equal to 2.00 (the scale is different) * Instead, use .compareTo() or .signum(), which handles scale correctly. * * For reference, check the official Sun Javadoc on java.math.BigDecimal. * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @author <a href="mailto:sichen@opensourcestrategies.com">Si Chen</a> * @author <a href="mailto:leon@opensourcestrategies.com">Leon Torres</a> * @version $Rev: 7251 $ * @since 2.2 */public class InvoiceServices { public static String module = InvoiceServices.class.getName(); // set some BigDecimal properties private static BigDecimal ZERO = new BigDecimal("0"); private static int decimals = UtilNumber.getBigDecimalScale("invoice.decimals"); private static int rounding = UtilNumber.getBigDecimalRoundingMode("invoice.rounding"); private static int taxDecimals = UtilNumber.getBigDecimalScale("salestax.calc.decimals"); private static int taxRounding = UtilNumber.getBigDecimalScale("salestax.rounding"); /* Service to create an invoice for an order */ public static Map createInvoiceForOrder(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue userLogin = (GenericValue) context.get("userLogin"); if (decimals == -1 || rounding == -1) { return ServiceUtil.returnError("Arithmetic properties for Invoice services not configured properly. Cannot proceed."); } String orderId = (String) context.get("orderId"); List billItems = (List) context.get("billItems"); boolean previousInvoiceFound = false; if (billItems == null || billItems.size() == 0) { Debug.logVerbose("No order items to invoice; not creating invoice; returning success", module); return ServiceUtil.returnSuccess("No order items to invoice, not creating invoice."); } try { List toStore = new LinkedList(); GenericValue orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId)); if (orderHeader == null) { return ServiceUtil.returnError("No OrderHeader, cannot create invoice"); } // get list of previous invoices for the order List billedItems = delegator.findByAnd("OrderItemBilling", UtilMisc.toMap("orderId", orderId)); if (billedItems != null && billedItems.size() > 0) { boolean nonDigitalInvoice = false; Iterator bii = billedItems.iterator(); while (bii.hasNext() && !nonDigitalInvoice) { GenericValue orderItemBilling = (GenericValue) bii.next(); GenericValue invoiceItem = orderItemBilling.getRelatedOne("InvoiceItem"); if (invoiceItem != null) { String invoiceItemType = invoiceItem.getString("invoiceItemTypeId"); if (invoiceItemType != null) { if ("INV_FPROD_ITEM".equals(invoiceItemType) || "INV_PROD_FEATR_ITEM".equals(invoiceItemType)) { nonDigitalInvoice = true; } } } } if (nonDigitalInvoice) { previousInvoiceFound = true; } } // figure out the invoice type String invoiceType = null; String orderType = orderHeader.getString("orderTypeId"); if (orderType.equals("SALES_ORDER")) { invoiceType = "SALES_INVOICE"; } else if (orderType.equals("PURCHASE_ORDER")) { invoiceType = "PURCHASE_INVOICE"; } // Make an order read helper from the order OrderReadHelper orh = new OrderReadHelper(orderHeader); // get the product store GenericValue productStore = delegator.findByPrimaryKey("ProductStore", UtilMisc.toMap("productStoreId", orh.getProductStoreId())); // get the shipping adjustment mode (Y = Pro-Rate; N = First-Invoice) String prorateShipping = productStore.getString("prorateShipping"); if (prorateShipping == null) { prorateShipping = "Y"; } // get the billing parties String billToCustomerPartyId = orh.getBillToParty().getString("partyId"); String billFromVendorPartyId = orh.getBillFromParty().getString("partyId"); // get some quantity totals BigDecimal totalItemsInOrder = orh.getTotalOrderItemsQuantityBd(); // get some price totals BigDecimal shippableAmount = orh.getShippableTotalBd(null); BigDecimal orderSubTotal = orh.getOrderItemsSubTotalBd(); BigDecimal invoiceShipProRateAmount = ZERO; BigDecimal invoiceSubTotal = ZERO; BigDecimal invoiceQuantity = ZERO; GenericValue billingAccount = orderHeader.getRelatedOne("BillingAccount"); String billingAccountId = billingAccount != null ? billingAccount.getString("billingAccountId") : null; // TODO: ideally this should be the same time as when a shipment is sent and be passed in as a parameter Timestamp invoiceDate = UtilDateTime.nowTimestamp(); // TODO: perhaps consider billing account net days term as well? Long orderTermNetDays = orh.getOrderTermNetDays(); Timestamp dueDate = null; if (orderTermNetDays != null) { dueDate = UtilDateTime.getDayEnd(invoiceDate, orderTermNetDays.intValue()); } // create the invoice record Map createInvoiceContext = FastMap.newInstance(); createInvoiceContext.put("partyId", billToCustomerPartyId); createInvoiceContext.put("partyIdFrom", billFromVendorPartyId); createInvoiceContext.put("billingAccountId", billingAccountId); createInvoiceContext.put("invoiceDate", invoiceDate); createInvoiceContext.put("dueDate", dueDate); createInvoiceContext.put("invoiceTypeId", invoiceType); // start with INVOICE_IN_PROCESS, in the INVOICE_READY we can't change the invoice (or shouldn't be able to...) createInvoiceContext.put("statusId", "INVOICE_IN_PROCESS"); createInvoiceContext.put("currencyUomId", orderHeader.getString("currencyUom")); createInvoiceContext.put("userLogin", userLogin); // store the invoice first Map createInvoiceResult = dispatcher.runSync("createInvoice", createInvoiceContext); if (ServiceUtil.isError(createInvoiceResult)) { return ServiceUtil.returnError("Error creating invoice from order", null, null, createInvoiceResult); } // call service, not direct entity op: delegator.create(invoice); String invoiceId = (String) createInvoiceResult.get("invoiceId"); // order roles to invoice roles List orderRoles = orderHeader.getRelated("OrderRole"); if (orderRoles != null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -