📄 invoiceservices.java
字号:
adjInvItem.set("overrideGlAccountId", adj.get("overrideGlAccountId")); adjInvItem.set("taxAuthGeoId", adj.get("taxAuthGeoId")); adjInvItem.set("taxAuthorityRateSeqId", adj.get("taxAuthorityRateSeqId")); toStore.add(adjInvItem); // this adjustment amount BigDecimal thisAdjAmount = adjInvItem.getBigDecimal("amount").multiply(adjInvItem.getBigDecimal("quantity")).setScale(decimals, rounding); // adjustments only apply to totals when they are not tax or shipping adjustments if (!"SALES_TAX".equals(adj.getString("orderAdjustmentTypeId")) && !"SHIPPING_ADJUSTMENT".equals(adj.getString("orderAdjustmentTypeId"))) { // increment the invoice subtotal invoiceSubTotal = invoiceSubTotal.add(thisAdjAmount).setScale(decimals, rounding); // add to the ship amount only if it applies to this item if (shippingApplies) { invoiceShipProRateAmount = invoiceShipProRateAmount.add(thisAdjAmount).setScale(decimals, rounding); } } // increment the counter invoiceItemSeqNum++; invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum, 2); } } } } // create header adjustments as line items -- always to tax/shipping last List shipAdjustments = new ArrayList(); List taxAdjustments = new ArrayList(); List headerAdjustments = orh.getOrderHeaderAdjustments(); Iterator headerAdjIter = headerAdjustments.iterator(); while (headerAdjIter.hasNext()) { GenericValue adj = (GenericValue) headerAdjIter.next(); if ("SHIPPING_CHARGES".equals(adj.getString("orderAdjustmentTypeId"))) { shipAdjustments.add(adj); } else if ("SALES_TAX".equals(adj.getString("orderAdjustmentTypeId"))) { taxAdjustments.add(adj); } else { // these will effect the shipping pro-rate (unless commented) // other adjustment type BigDecimal adjAmount = calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, toStore, orderSubTotal, invoiceSubTotal, invoiceQuantity, decimals, rounding); // invoiceShipProRateAmount += adjAmount; // do adjustments compound or are they based off subtotal? Here we will (unless commented) // invoiceSubTotal += adjAmount; // increment the counter invoiceItemSeqNum++; invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum, 2); } } // next do the shipping adjustments Iterator shipAdjIter = shipAdjustments.iterator(); while (shipAdjIter.hasNext()) { GenericValue adj = (GenericValue) shipAdjIter.next(); if ("N".equalsIgnoreCase(prorateShipping)) { if (previousInvoiceFound) { Debug.logInfo("Previous invoice found for this order [" + orderId + "]; shipping already billed", module); continue; } else { // this is the first invoice; bill it all now BigDecimal adjAmount = calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, toStore, new BigDecimal("1"), new BigDecimal("1"), totalItemsInOrder, decimals, rounding); // should shipping effect the tax pro-rate? here we do, and we also update order sub total for this adjustment's value invoiceSubTotal = invoiceSubTotal.add(adjAmount).setScale(decimals, rounding); orderSubTotal = orderSubTotal.add(adj.getBigDecimal("amount")).setScale(decimals, rounding); // increment the counter invoiceItemSeqNum++; invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum, 2); } } else { // pro-rate the shipping amount based on shippable information BigDecimal adjAmount = calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, toStore, shippableAmount, invoiceShipProRateAmount, invoiceQuantity, decimals, rounding); // should shipping effect the tax pro-rate? here we do, and we also update order sub total for this adjustment's value invoiceSubTotal = invoiceSubTotal.add(adjAmount).setScale(decimals, rounding); orderSubTotal = orderSubTotal.add(adj.getBigDecimal("amount")).setScale(decimals, rounding); // increment the counter invoiceItemSeqNum++; invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum, 2); } } // last do the tax adjustments Iterator taxAdjIter = taxAdjustments.iterator(); while (taxAdjIter.hasNext()) { GenericValue adj = (GenericValue) taxAdjIter.next(); BigDecimal adjAmount = calcHeaderAdj(delegator, adj, invoiceType, invoiceId, invoiceItemSeqId, toStore, orderSubTotal, invoiceSubTotal, invoiceQuantity, taxDecimals, taxRounding); // this doesn't really effect anything; but just for our totals invoiceSubTotal = invoiceSubTotal.add(adjAmount).setScale(decimals, rounding); } // check for previous order payments List orderPaymentPrefs = delegator.findByAnd("OrderPaymentPreference", UtilMisc.toMap("orderId", orderId)); if (orderPaymentPrefs != null) { List currentPayments = new ArrayList(); Iterator opi = orderPaymentPrefs.iterator(); while (opi.hasNext()) { GenericValue paymentPref = (GenericValue) opi.next(); List payments = paymentPref.getRelated("Payment"); currentPayments.addAll(payments); } if (currentPayments.size() > 0) { // apply these payments to the invoice; only if they haven't already been applied Iterator cpi = currentPayments.iterator(); while (cpi.hasNext()) { GenericValue payment = (GenericValue) cpi.next(); List currentApplications = null; currentApplications = payment.getRelated("PaymentApplication"); if (currentApplications == null || currentApplications.size() == 0) { // no applications; okay to apply Map appl = new HashMap(); appl.put("paymentId", payment.get("paymentId")); appl.put("invoiceId", invoiceId); appl.put("billingAccountId", billingAccountId); appl.put("amountApplied", payment.get("amount")); appl.put("userLogin", userLogin); Map createPayApplResult = dispatcher.runSync("createPaymentApplication", appl); if (ServiceUtil.isError(createPayApplResult)) { return ServiceUtil.returnError("Error creating invoice from order", null, null, createPayApplResult); } } } } } // store value objects //Debug.log("Storing : " + toStore, module); // TODO BIG TIME: need to get rid of the storeAll/toStore stuff and call all services for these things rather than direct entity ops delegator.storeAll(toStore); // should all be in place now, so set status to INVOICE_READY (unless it's a purchase invoice, which we sets to INVOICE_IN_PROCESS) String nextStatusId = "INVOICE_READY"; if (invoiceType.equals("PURCHASE_INVOICE")) { nextStatusId = "INVOICE_IN_PROCESS"; } Map setInvoiceStatusResult = dispatcher.runSync("setInvoiceStatus", UtilMisc.toMap("invoiceId", invoiceId, "statusId", nextStatusId, "userLogin", userLogin)); if (ServiceUtil.isError(setInvoiceStatusResult)) { return ServiceUtil.returnError("Error creating invoice from order", null, null, setInvoiceStatusResult); } // check to see if we are all paid up Map checkResp = dispatcher.runSync("checkInvoicePaymentApplications", UtilMisc.toMap("invoiceId", invoiceId, "userLogin", userLogin)); if (ServiceUtil.isError(checkResp)) { return ServiceUtil.returnError("Error creating invoice from order while checking payment applications", null, null, checkResp); } Map resp = ServiceUtil.returnSuccess(); resp.put("invoiceId", invoiceId); return resp; } catch (GenericEntityException e) { String errMsg = "Entity/data problem creating invoice from order items: " + e.toString(); Debug.logError(e, errMsg, module); return ServiceUtil.returnError(errMsg); } catch (GenericServiceException e) { String errMsg = "Service/other problem creating invoice from order items: " + e.toString(); Debug.logError(e, errMsg, module); return ServiceUtil.returnError(errMsg); } } 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(); Map serviceContext = UtilMisc.toMap("shipmentIds", UtilMisc.toList(shipmentId), "userLogin", context.get("userLogin")); try { Map result = dispatcher.runSync("createInvoicesFromShipments", serviceContext); invoicesCreated = (List)result.get("invoicesCreated"); } catch (GenericServiceException e) { Debug.logError(e, "Trouble calling createInvoicesFromShipments service; invoice not created for shipment [" + shipmentId + "]", module); return ServiceUtil.returnError("Trouble calling createInvoicesFromShipments service; invoice not created for shipment [" + shipmentId + "]"); } Map response = ServiceUtil.returnSuccess(); response.put("invoicesCreated", invoicesCreated); return response; } public static Map createInvoicesFromShipments(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); List shipmentIds = (List) context.get("shipmentIds"); boolean salesShipmentFound = false; boolean purchaseShipmentFound = false; List invoicesCreated = new ArrayList(); List shipmentIdList = new LinkedList(); for (int i = 0; i < shipmentIds.size(); i++) { String tmpShipmentId = (String)shipmentIds.get(i); try { GenericValue shipment = delegator.findByPrimaryKey("Shipment", UtilMisc.toMap("shipmentId", tmpShipmentId)); if ((shipment.getString("shipmentTypeId") != null) && (shipment.getString("shipmentTypeId").equals("PURCHASE_SHIPMENT"))) { purchaseShipmentFound = true; } else { salesShipmentFound = true; } if (purchaseShipmentFound && salesShipmentFound) { return ServiceUtil.returnError("Shipments of different types found; shipment [" + tmpShipmentId + "] of type [" + shipment.getString("shipmentTypeId") + "] is of different type from the previous ones."); } } catch (GenericEntityException e) { Debug.logError(e, "Trouble getting Shipment entity for shipment [" + tmpShipmentId + "]", module); return ServiceUtil.returnError("Trouble getting Shipment entity for shipment [" + tmpShipmentId + "]"); } } EntityCondition shipmentIdsCond = new EntityExpr("shipmentId", EntityOperator.IN, shipmentIds); // check the status of the shipment // get the items of the shipment. They can come from ItemIssuance if the shipment were from a sales order or ShipmentReceipt // if it were a purchase order List items = null; try { if (purchaseShipmentFound) { items = delegator.findByCondition("ShipmentReceipt", shipmentIdsCond, null, UtilMisc.toList("shipmentId")); } else { items = delegator.findByCondition("ItemIssuance", shipmentIdsCond, null, UtilMisc.toList("shipmentId")); } } catch (GenericEntityException e) { Debug.logError(e, "Problem getting issued items from shipments", module); return ServiceUtil.returnError("Problem getting issued items from shipments");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -