📄 invoiceservices.java
字号:
/*
* $Id: InvoiceServices.java,v 1.8 2004/01/22 17:47:24 ajzeneski Exp $
*
* Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.ofbiz.accounting.invoice;
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 org.ofbiz.accounting.payment.PaymentWorker;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.order.order.OrderReadHelper;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.product.product.ProductWorker;
/**
* InvoiceServices - Services for creating invoices
*
* @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
* @version $Revision: 1.8 $
* @since 2.2
*/
public class InvoiceServices {
public static String module = InvoiceServices.class.getName();
/* 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");
String orderId = (String) context.get("orderId");
List billItems = (List) context.get("billItems");
boolean previousInvoiceFound = false;
if (billItems == null || billItems.size() == 0) {
Debug.logVerbose("No items to invoice; not creating; returning success", module);
return ServiceUtil.returnSuccess();
}
List toStore = new LinkedList();
GenericValue orderHeader = null;
try {
orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId));
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot get order header", module);
}
if (orderHeader == null) {
return ServiceUtil.returnError("No OrderHeader, cannot create invoice");
}
// get list of previous invoices for the order
List billedItems = null;
try {
billedItems = delegator.findByAnd("OrderItemBilling", UtilMisc.toMap("orderId", orderId));
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot looking billed items", module);
return ServiceUtil.returnError("Unable to looked previous billed items");
}
if (billedItems != null && billedItems.size() > 0) {
boolean nonDigitalInvoice = false;
Iterator bii = billedItems.iterator();
while (bii.hasNext() && !nonDigitalInvoice) {
GenericValue orderItemBilling = (GenericValue) bii.next();
GenericValue invoiceItem = null;
try {
invoiceItem = orderItemBilling.getRelatedOne("InvoiceItem");
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot get InvoiceItem from billing record; assuming non-digital.", module);
nonDigitalInvoice = true;
}
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";
}
OrderReadHelper orh = new OrderReadHelper(orderHeader);
// get the product store
GenericValue productStore = null;
try {
productStore = delegator.findByPrimaryKey("ProductStore", UtilMisc.toMap("productStoreId", orh.getProductStoreId()));
} catch (GenericEntityException e) {
Debug.logError(e, "Unable to get ProductStore", module);
return ServiceUtil.returnError("Unable to get Product Store from order");
}
// get the payToParty
String payToPartyId = productStore.getString("payToPartyId");
if (payToPartyId == null) {
return ServiceUtil.returnError("Unable to create invoice; no payToPartyId set for ProductStore Id : " + orh.getProductStoreId());
}
// get some quantity totals
double totalItemsInOrder = orh.getTotalOrderItemsQuantity();
// get some price totals
double shippableAmount = orh.getShippableTotal();
double orderSubTotal = orh.getOrderItemsSubTotal();
double invoiceShipProRateAmount = 0.00;
double invoiceSubTotal = 0.00;
double invoiceQuantity = 0.00;
// create the invoice record
String invoiceId = delegator.getNextSeqId("Invoice").toString();
GenericValue invoice = delegator.makeValue("Invoice", UtilMisc.toMap("invoiceId", invoiceId));
invoice.set("invoiceDate", UtilDateTime.nowTimestamp());
invoice.set("invoiceTypeId", invoiceType);
invoice.set("statusId", "INVOICE_READY");
GenericValue billingAccount = null;
List billingAccountTerms = null;
try {
billingAccount = orderHeader.getRelatedOne("BillingAccount");
} catch (GenericEntityException e) {
Debug.logError(e, "Trouble getting BillingAccount entity from OrderHeader", module);
ServiceUtil.returnError("Trouble getting BillingAccount entity from OrderHeader");
}
// for billing accounts we will use related information
if (billingAccount != null) {
// set the billing account
invoice.set("billingAccountId", billingAccount.getString("billingAccountId"));
// get the billing account terms
try {
billingAccountTerms = billingAccount.getRelated("BillingAccountTerm");
} catch (GenericEntityException e) {
Debug.logError(e, "Trouble getting BillingAccountTerm entity list", module);
ServiceUtil.returnError("Trouble getting BillingAccountTerm entity list");
}
// set the invoice terms as defined for the billing account
if (billingAccountTerms != null) {
Iterator billingAcctTermsIter = billingAccountTerms.iterator();
while (billingAcctTermsIter.hasNext()) {
GenericValue term = (GenericValue) billingAcctTermsIter.next();
GenericValue invoiceTerm = delegator.makeValue("InvoiceTerm",
UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemSeqId", "_NA_"));
invoiceTerm.set("termType", term.get("termType"));
invoiceTerm.set("termValue", term.get("termValue"));
invoiceTerm.set("uomId", term.get("uomId"));
toStore.add(invoiceTerm);
}
}
// set the invoice bill_to_customer from the billing account
List billToRoles = null;
try {
billToRoles = billingAccount.getRelated("BillingAccountRole", UtilMisc.toMap("roleTypeId", "BILL_TO_CUSTOMER"), null);
} catch (GenericEntityException e) {
Debug.logError(e, "Trouble getting BillingAccountRole entity list", module);
ServiceUtil.returnError("Trouble getting BillingAccountRole entity list");
}
Iterator billToIter = billToRoles.iterator();
while (billToIter.hasNext()) {
GenericValue billToRole = (GenericValue) billToIter.next();
GenericValue invoiceRole = delegator.makeValue("InvoiceRole", UtilMisc.toMap("invoiceId", invoiceId));
invoiceRole.set("partyId", billToRole.get("partyId"));
invoiceRole.set("roleTypeId", "BILL_TO_CUSTOMER");
toStore.add(invoiceRole);
}
// set the bill-to contact mech as the contact mech of the billing account
GenericValue billToContactMech = delegator.makeValue("InvoiceContactMech", UtilMisc.toMap("invoiceId", invoiceId));
billToContactMech.set("contactMechId", billingAccount.getString("contactMechId"));
billToContactMech.set("contactMechPurposeTypeId", "BILLING_LOCATION");
toStore.add(billToContactMech);
} else {
// no billing account use the info off the order header
GenericValue billToPerson = orh.getBillToPerson();
if (billToPerson != null) {
GenericValue invoiceRole = delegator.makeValue("InvoiceRole", UtilMisc.toMap("invoiceId", invoiceId));
invoiceRole.set("partyId", billToPerson.getString("partyId"));
invoiceRole.set("roleTypeId", "BILL_TO_CUSTOMER");
toStore.add(invoiceRole);
}
GenericValue billingAddress = orh.getBillingAddress();
if (billingAddress != null) {
GenericValue billToContactMech = delegator.makeValue("InvoiceContactMech", UtilMisc.toMap("invoiceId", invoiceId));
billToContactMech.set("contactMechId", billingAddress.getString("contactMechId"));
billToContactMech.set("contactMechPurposeTypeId", "BILLING_LOCATION");
toStore.add(billToContactMech);
}
}
// store the invoice first
try {
delegator.create(invoice);
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot create invoice record", module);
ServiceUtil.returnError("Problems storing Invoice record");
}
// get a list of the payment method types
List paymentPreferences = null;
try {
paymentPreferences = orderHeader.getRelated("OrderPaymentPreference");
} catch (GenericEntityException e) {
Debug.logError(e, "Trouble getting OrderPaymentPreference entity list", module);
}
// create the bill-from role BILL_FROM_VENDOR as the partyId for the store
GenericValue payToRole = delegator.makeValue("InvoiceRole", UtilMisc.toMap("invoiceId", invoiceId));
payToRole.set("partyId", payToPartyId);
payToRole.set("roleTypeId", "BILL_FROM_VENDOR");
toStore.add(payToRole);
// create the bill-from (or pay-to) contact mech as the primary PAYMENT_LOCATION of the party from the store
GenericValue payToAddress = PaymentWorker.getPaymentAddress(delegator, payToPartyId);
if (payToAddress != null) {
GenericValue payToCm = delegator.makeValue("InvoiceContactMech", UtilMisc.toMap("invoiceId", invoiceId));
payToCm.set("contactMechId", payToAddress.getString("contactMechId"));
payToCm.set("contactMechPurposeTypeId", "PAYMENT_LOCATION");
toStore.add(payToCm);
}
// sequence for items - all OrderItems or InventoryReservations + all Adjustments
int itemSeqId = 1;
// create the item records
if (billItems != null) {
Iterator itemIter = billItems.iterator();
while (itemIter.hasNext()) {
GenericValue itemIssuance = null;
GenericValue orderItem = null;
GenericValue currentValue = (GenericValue) itemIter.next();
if ("ItemIssuance".equals(currentValue.getEntityName())) {
itemIssuance = currentValue;
} else if ("OrderItem".equals(currentValue.getEntityName())) {
orderItem = currentValue;
}
if (orderItem == null && itemIssuance != null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -