📄 invoiceservices.java
字号:
try {
//Debug.log("Storing : " + toStore, module);
delegator.storeAll(toStore);
} catch (GenericEntityException e) {
Debug.logError(e, "Problems storing invoice items", module);
return ServiceUtil.returnError("Cannot create invoice; problem storing items");
}
// check to see if we are all paid up
Map checkResp = null;
try {
checkResp = dispatcher.runSync("checkInvoicePaymentApplications", UtilMisc.toMap("invoiceId", invoice.get("invoiceId"), "userLogin", userLogin));
} catch (GenericServiceException e) {
Debug.logError(e, "Problem checking payment applications", module);
return ServiceUtil.returnError("Problem checking payment applications");
}
Map resp = ServiceUtil.returnSuccess();
resp.put("invoiceId", invoiceId);
return resp;
}
public static Map createInvoicesFromShipment(DispatchContext dctx, Map context) {
GenericDelegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
String shipmentId = (String) context.get("shipmentId");
List invoicesCreated = new ArrayList();
GenericValue shipment = null;
try {
shipment = delegator.findByPrimaryKey("Shipment", UtilMisc.toMap("shipmentId", shipmentId));
} catch (GenericEntityException e) {
Debug.logError(e, "Trouble getting Shipment entity", module);
return ServiceUtil.returnError("Trouble getting Shipment entity");
}
// check the status of the shipment
// get the issued items
List itemsIssued = null;
try {
itemsIssued = delegator.findByAnd("ItemIssuance", UtilMisc.toMap("shipmentId", shipmentId));
} catch (GenericEntityException e) {
Debug.logError(e, "Problem getting issued items from Shipment", module);
return ServiceUtil.returnError("Problem getting issued items from Shipment");
}
if (itemsIssued == null) {
Debug.logInfo("No items issued for shipment", module);
return ServiceUtil.returnSuccess();
}
// group items by order
Map shippedOrderItems = new HashMap();
Iterator itemsIter = itemsIssued.iterator();
while (itemsIter.hasNext()) {
GenericValue itemIssuance = (GenericValue) itemsIter.next();
String itemIssuanceId = itemIssuance.getString("itemIssuanceId");
String orderId = itemIssuance.getString("orderId");
String orderItemSeqId = itemIssuance.getString("orderItemSeqId");
List itemsByOrder = (List) shippedOrderItems.get(orderId);
if (itemsByOrder == null) {
itemsByOrder = new ArrayList();
}
// check and make sure we haven't already billed for this issuance
Map billFields = UtilMisc.toMap("orderId", orderId, "orderItemSeqId", orderItemSeqId, "itemIssuanceId", itemIssuanceId);
List itemBillings = null;
try {
itemBillings = delegator.findByAnd("OrderItemBilling", billFields);
} catch (GenericEntityException e) {
Debug.logError(e, "Problem looking up OrderItemBilling records for : " + billFields, module);
return ServiceUtil.returnError("Problem getting OrderItemBilling records");
}
// if none found, then okay to bill
if (itemBillings == null || itemBillings.size() == 0) {
itemsByOrder.add(itemIssuance);
}
// update the map with modified list
shippedOrderItems.put(orderId, itemsByOrder);
}
// make sure we aren't billing items already invoiced i.e. items billed as digital (FINDIG)
Set orders = shippedOrderItems.keySet();
Iterator ordersIter = orders.iterator();
while (ordersIter.hasNext()) {
String orderId = (String) ordersIter.next();
// we'll only use this list to figure out which ones to send
List billItems = (List) shippedOrderItems.get(orderId);
// a new list to be used to pass to the create invoice service
List toBillItems = new ArrayList();
// map of available quantities so we only have to calc once
Map itemQtyAvail = new HashMap();
// now we will check each issuance and make sure it hasn't already been billed
Iterator billIt = billItems.iterator();
while (billIt.hasNext()) {
GenericValue issue = (GenericValue) billIt.next();
Double issueQty = issue.getDouble("quantity");
Double billAvail = (Double) itemQtyAvail.get(issue.getString("orderItemSeqId"));
if (billAvail == null) {
Map lookup = UtilMisc.toMap("orderId", orderId, "orderItemSeqId", issue.get("orderItemSeqId"));
GenericValue orderItem = null;
List billed = null;
try {
orderItem = issue.getRelatedOne("OrderItem");
billed = delegator.findByAnd("OrderItemBilling", lookup);
} catch (GenericEntityException e) {
Debug.logError(e, "Problem looking up OrderItem/OrderItemBilling records for : " + lookup, module);
return ServiceUtil.returnError("Problem getting OrderItem/OrderItemBilling records");
}
// total ordered
double orderedQty = orderItem.getDouble("quantity").doubleValue();
// add up the already billed total
if (billed != null && billed.size() > 0) {
double billedQuantity = 0.00;
Iterator bi = billed.iterator();
while (bi.hasNext()) {
GenericValue oib = (GenericValue) bi.next();
Double qty = oib.getDouble("quantity");
if (qty != null) {
billedQuantity += qty.doubleValue();
}
}
double leftToBill = orderedQty - billedQuantity;
billAvail = new Double(leftToBill);
} else {
billAvail = new Double(orderedQty);
}
}
// no available means we cannot bill anymore
if (billAvail != null && billAvail.doubleValue() > 0) {
if (issueQty != null && issueQty.doubleValue() > billAvail.doubleValue()) {
// can only bill some of the issuance; others have been billed already
issue.set("quantity", billAvail);
billAvail = new Double(0);
} else {
// now have been billed
billAvail = new Double(billAvail.doubleValue() - issueQty.doubleValue());
}
// okay to bill these items; but none else
toBillItems.add(issue);
}
// update the available to bill quantity for the next pass
itemQtyAvail.put(issue.getString("orderItemSeqId"), billAvail);
}
// call the createInvoiceForOrder service for each order
Map serviceContext = UtilMisc.toMap("orderId", orderId, "billItems", toBillItems, "userLogin", context.get("userLogin"));
try {
Map result = dispatcher.runSync("createInvoiceForOrder", serviceContext);
invoicesCreated.add(result.get("invoiceId"));
} catch (GenericServiceException e) {
Debug.logError(e, "Trouble calling createInvoiceForOrder service; invoice not created for shipment", module);
return ServiceUtil.returnError("Trouble calling createInvoiceForOrder service; invoice not created for shipment");
}
}
Map response = ServiceUtil.returnSuccess();
response.put("invoicesCreated", invoicesCreated);
return response;
}
private static String getInvoiceItemType(GenericDelegator delegator, String key, String defaultValue) {
GenericValue itemMap = null;
try {
itemMap = delegator.findByPrimaryKey("InvoiceItemTypeMap", UtilMisc.toMap("invoiceItemMapKey", key));
} catch (GenericEntityException e) {
Debug.logError(e, "Trouble getting InvoiceItemTypeMap entity", module);
return defaultValue;
}
if (itemMap != null) {
return itemMap.getString("invoiceItemTypeId");
} else {
return defaultValue;
}
}
public static Map checkInvoicePaymentApplications(DispatchContext ctx, Map context) {
GenericDelegator delegator = ctx.getDelegator();
LocalDispatcher dispatcher = ctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
String invoiceId = (String) context.get("invoiceId");
List paymentAppl = null;
try {
paymentAppl = delegator.findByAnd("PaymentApplication", UtilMisc.toMap("invoiceId", invoiceId));
} catch (GenericEntityException e) {
Debug.logError(e, "Problem getting PaymentApplication(s) for Invoice #" + invoiceId, module);
return ServiceUtil.returnError("Problem getting PaymentApplication(s) for Invoice #" + invoiceId);
}
Map payments = new HashMap();
if (paymentAppl != null) {
Iterator pai = paymentAppl.iterator();
while (pai.hasNext()) {
GenericValue payAppl = (GenericValue) pai.next();
payments.put(payAppl.getString("paymentId"), payAppl.getDouble("amountApplied"));
}
}
double totalPayments = 0.00;
Iterator pi = payments.keySet().iterator();
while (pi.hasNext()) {
String paymentId = (String) pi.next();
Double amount = (Double) payments.get(paymentId);
if (amount == null) amount = new Double(0.00);
totalPayments += amount.doubleValue();
}
if (totalPayments > 0.00) {
double invoiceTotal = InvoiceWorker.getInvoiceTotal(delegator, invoiceId);
//Debug.log("Invoice #" + invoiceId + " total: " + invoiceTotal, module);
//Debug.log("Total payments : " + totalPayments, module);
if (totalPayments >= invoiceTotal) {
// this invoice is paid
Map svcCtx = UtilMisc.toMap("statusId", "INVOICE_PAID", "invoiceId", invoiceId, "userLogin", userLogin);
try {
Map stRes = dispatcher.runSync("setInvoiceStatus", svcCtx);
} catch (GenericServiceException e) {
Debug.logError(e, "Problem changing invoice status : " + svcCtx, module);
return ServiceUtil.returnError("Problem changing invoice status");
}
}
} else {
Debug.log("No payments found for Invoice #" + invoiceId, module);
}
return ServiceUtil.returnSuccess();
}
private static double calcHeaderAdj(GenericDelegator delegator, GenericValue adj, String invoiceId, int itemSeqId, List toStore, double divisor, double multiplier, double invoiceQuantity) {
//Debug.log("Divisor : " + divisor + " / Multiplier: " + multiplier, module);
double adjAmount = 0.00;
if (adj.get("amount") != null) {
// pro-rate the amount
double amount = ((adj.getDouble("amount").doubleValue() / divisor) * multiplier);
if (amount != 0) {
GenericValue invoiceItem = delegator.makeValue("InvoiceItem", UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemSeqId", new Integer(itemSeqId).toString()));
invoiceItem.set("invoiceItemTypeId", getInvoiceItemType(delegator, adj.getString("orderAdjustmentTypeId"), "INVOICE_ADJ"));
//invoiceItem.set("productId", orderItem.get("productId"));
//invoiceItem.set("productFeatureId", orderItem.get("productFeatureId"));
//invoiceItem.set("uomId", "");
//invoiceItem.set("taxableFlag", product.get("taxable"));
invoiceItem.set("quantity", new Double(1));
invoiceItem.set("amount", new Double(amount));
invoiceItem.set("description", adj.get("description"));
toStore.add(invoiceItem);
}
adjAmount = amount;
} else if (adj.get("percentage") != null || adj.get("amountPerQuantity") != null) {
Double amountPerQty = adj.getDouble("amount");
Double percent = adj.getDouble("percentage");
double totalAmount = 0.00;
if (percent != null)
totalAmount += percent.doubleValue() * multiplier;
if (amountPerQty != null)
totalAmount += amountPerQty.doubleValue() * invoiceQuantity;
if (totalAmount != 0) {
GenericValue adjInvItem = delegator.makeValue("InvoiceItem", UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemSeqId", new Integer(itemSeqId).toString()));
adjInvItem.set("invoiceItemTypeId", getInvoiceItemType(delegator, adj.getString("orderAdjustmentTypeId"), "INVOICE_ITM_ADJ"));
//adjInvItem.set("productId", orderItem.get("productId"));
//adjInvItem.set("productFeatureId", orderItem.get("productFeatureId"));
//adjInvItem.set("uomId", "");
//adjInvItem.set("taxableFlag", product.get("taxable"));
adjInvItem.set("quantity", new Double(1));
adjInvItem.set("amount", new Double(totalAmount));
adjInvItem.set("description", adj.get("description"));
toStore.add(adjInvItem);
}
adjAmount = totalAmount;
}
return adjAmount;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -