📄 invoiceservices.java
字号:
} if (items == null) { Debug.logInfo("No items issued for shipments", module); return ServiceUtil.returnSuccess(); } // group items by order Map shippedOrderItems = new HashMap(); Iterator itemsIter = items.iterator(); while (itemsIter.hasNext()) { GenericValue item = (GenericValue) itemsIter.next(); String orderId = item.getString("orderId"); String orderItemSeqId = item.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 or shipment receipt Map billFields = UtilMisc.toMap("orderId", orderId, "orderItemSeqId", orderItemSeqId); if (item.getEntityName().equals("ItemIssuance")) { billFields.put("itemIssuanceId", item.get("itemIssuanceId")); } else if (item.getEntityName().equals("ShipmentReceipt")) { billFields.put("shipmentReceiptId", item.getString("receiptId")); } 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(item); } // 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(); BigDecimal issueQty = ZERO; if (issue.getEntityName().equals("ShipmentReceipt")) { issueQty = issue.getBigDecimal("quantityAccepted"); } else { issueQty = issue.getBigDecimal("quantity"); } BigDecimal billAvail = (BigDecimal) 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 BigDecimal orderedQty = orderItem.getBigDecimal("quantity"); // add up the already billed total if (billed != null && billed.size() > 0) { BigDecimal billedQuantity = ZERO; Iterator bi = billed.iterator(); while (bi.hasNext()) { GenericValue oib = (GenericValue) bi.next(); BigDecimal qty = oib.getBigDecimal("quantity"); if (qty != null) { billedQuantity = billedQuantity.add(qty).setScale(decimals, rounding); } } BigDecimal leftToBill = orderedQty.subtract(billedQuantity).setScale(decimals, rounding); billAvail = leftToBill; } else { billAvail = orderedQty; } } // no available means we cannot bill anymore if (billAvail != null && billAvail.signum() == 1) { // this checks if billAvail is a positive non-zero number if (issueQty != null && issueQty.doubleValue() > billAvail.doubleValue()) { // can only bill some of the issuance; others have been billed already issue.set("quantity", new Double(billAvail.doubleValue())); billAvail = ZERO; } else { // now have been billed billAvail = billAvail.subtract(issueQty).setScale(decimals, rounding); } // 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 invoiceTypeId, String defaultValue) { GenericValue itemMap = null; try { itemMap = delegator.findByPrimaryKey("InvoiceItemTypeMap", UtilMisc.toMap("invoiceItemMapKey", key, "invoiceTypeId", invoiceTypeId)); } 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 createInvoicesFromReturnShipment(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); String shipmentId = (String) context.get("shipmentId"); String errorMsg = "Error creating invoice for shipment [" + shipmentId + "]. "; List invoicesCreated = new ArrayList(); try { // get the shipment and validate that it is a sales return GenericValue shipment = delegator.findByPrimaryKey("Shipment", UtilMisc.toMap("shipmentId", shipmentId)); if (shipment == null) { return ServiceUtil.returnError(errorMsg + "Shipment not found."); } if (!shipment.getString("shipmentTypeId").equals("SALES_RETURN")) { return ServiceUtil.returnError(errorMsg + "Shipment is not of type SALES_RETURN."); } // get the list of ShipmentReceipt for this shipment List shipmentReceipts = shipment.getRelated("ShipmentReceipt"); // group the shipments by returnId (because we want a seperate itemized invoice for each return) Map receiptsGroupedByReturn = new HashMap(); for (Iterator iter = shipmentReceipts.iterator(); iter.hasNext(); ) { GenericValue receipt = (GenericValue) iter.next(); String returnId = receipt.getString("returnId"); // see if there are ReturnItemBillings for this item List billings = delegator.findByAnd("ReturnItemBilling", UtilMisc.toMap("shipmentReceiptId", receipt.getString("receiptId"), "returnId", returnId, "returnItemSeqId", receipt.get("returnItemSeqId"))); // if there are billings, we have already billed the item, so skip it if (billings.size() > 0) continue; // get the List of receipts keyed to this returnId or create a new one List receipts = (List) receiptsGroupedByReturn.get(returnId); if (receipts == null) { receipts = new ArrayList(); } // add our item to the group and put it back in the map receipts.add(receipt); receiptsGroupedByReturn.put(returnId, receipts); } // loop through the returnId keys in the map and invoke the createInvoiceFromReturn service for each for (Iterator iter = receiptsGroupedByReturn.keySet().iterator(); iter.hasNext(); ) { String returnId = (String) iter.next(); List receipts = (List) receiptsGroupedByReturn.get(returnId); if (Debug.verboseOn()) { Debug.logVerbose("Creating invoice for return [" + returnId + "] with receipts: " + receipts.toString(), module); } Map input = UtilMisc.toMap("returnId", returnId, "shipmentReceiptsToBill", receipts, "userLogin", context.get("userLogin")); Map serviceResults = dispatcher.runSync("createInvoiceFromReturn", input); if (ServiceUtil.isError(serviceResults)) { return ServiceUtil.returnError(errorMsg, null, null, serviceResults); } // put the resulting invoiceId in the return list invoicesCreated.add(serviceResults.get("invoiceId")); } } catch (GenericServiceException e) { Debug.logError(e, errorMsg + e.getMessage(), module); return ServiceUtil.returnError(errorMsg + e.getMessage()); } catch (GenericEntityException e) { Debug.logError(e, errorMsg + e.getMessage(), module); return ServiceUtil.returnError(errorMsg + e.getMessage()); } Map result = ServiceUtil.returnSuccess(); result.put("invoicesCreated", invoicesCreated); return result; } public static Map createInvoiceFromReturn(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -