📄 invoiceservices.java
字号:
Iterator orderRolesIt = orderRoles.iterator(); Map createInvoiceRoleContext = FastMap.newInstance(); createInvoiceRoleContext.put("invoiceId", invoiceId); createInvoiceRoleContext.put("userLogin", userLogin); while (orderRolesIt.hasNext()) { GenericValue orderRole = (GenericValue)orderRolesIt.next(); createInvoiceRoleContext.put("partyId", orderRole.getString("partyId")); createInvoiceRoleContext.put("roleTypeId", orderRole.getString("roleTypeId")); Map createInvoiceRoleResult = dispatcher.runSync("createInvoiceRole", createInvoiceRoleContext); if (ServiceUtil.isError(createInvoiceRoleResult)) { return ServiceUtil.returnError("Error creating invoice role from order", null, null, createInvoiceRoleResult); } } } // order terms to invoice terms. Implemented for purchase orders, although it may be useful // for sales orders as well. Later it might be nice to filter OrderTerms to only copy over financial terms. List orderTerms = orh.getOrderTerms(); toStore.addAll(createInvoiceTerms(delegator, invoiceId, orderTerms)); // billing accounts List billingAccountTerms = null; // for billing accounts we will use related information if (billingAccount != null) { // get the billing account terms billingAccountTerms = billingAccount.getRelated("BillingAccountTerm"); // set the invoice terms as defined for the billing account toStore.addAll(createInvoiceTerms(delegator, invoiceId, billingAccountTerms)); // set the invoice bill_to_customer from the billing account List billToRoles = billingAccount.getRelated("BillingAccountRole", UtilMisc.toMap("roleTypeId", "BILL_TO_CUSTOMER"), null); Iterator billToIter = billToRoles.iterator(); while (billToIter.hasNext()) { GenericValue billToRole = (GenericValue) billToIter.next(); if (!(billToRole.getString("partyId").equals(billToCustomerPartyId))) { 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 if (UtilValidate.isNotEmpty(billingAccount.getString("contactMechId"))) { GenericValue billToContactMech = delegator.makeValue("InvoiceContactMech", UtilMisc.toMap("invoiceId", invoiceId)); billToContactMech.set("contactMechId", billingAccount.getString("contactMechId")); billToContactMech.set("contactMechPurposeTypeId", "BILLING_LOCATION"); toStore.add(billToContactMech); } } else { List billingLocations = orh.getBillingLocations(); if (billingLocations != null) { Iterator bli = billingLocations.iterator(); while (bli.hasNext()) { GenericValue ocm = (GenericValue) bli.next(); GenericValue billToContactMech = delegator.makeValue("InvoiceContactMech", UtilMisc.toMap("invoiceId", invoiceId)); billToContactMech.set("contactMechId", ocm.getString("contactMechId")); billToContactMech.set("contactMechPurposeTypeId", "BILLING_LOCATION"); toStore.add(billToContactMech); } } } // get a list of the payment method types //DEJ20050705 doesn't appear to be used: List paymentPreferences = orderHeader.getRelated("OrderPaymentPreference"); // create the bill-from (or pay-to) contact mech as the primary PAYMENT_LOCATION of the party from the store GenericValue payToAddress = null; if (invoiceType.equals("PURCHASE_INVOICE")) { // for purchase orders, the pay to address is the BILLING_LOCATION of the vendor GenericValue billFromVendor = orh.getPartyFromRole("BILL_FROM_VENDOR"); if (billFromVendor != null) { List billingContactMechs = billFromVendor.getRelatedOne("Party").getRelatedByAnd("PartyContactMechPurpose", UtilMisc.toMap("contactMechPurposeTypeId", "BILLING_LOCATION")); if ((billingContactMechs != null) && (billingContactMechs.size() > 0)) { payToAddress = (GenericValue) billingContactMechs.get(0); } } } else { // for sales orders, it is the payment address on file for the store payToAddress = PaymentWorker.getPaymentAddress(delegator, productStore.getString("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 invoiceItemSeqNum = 1; String invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum, 2); // create the item records if (billItems != null) { Iterator itemIter = billItems.iterator(); while (itemIter.hasNext()) { GenericValue itemIssuance = null; GenericValue orderItem = null; GenericValue shipmentReceipt = null; GenericValue currentValue = (GenericValue) itemIter.next(); if ("ItemIssuance".equals(currentValue.getEntityName())) { itemIssuance = currentValue; } else if ("OrderItem".equals(currentValue.getEntityName())) { orderItem = currentValue; } else if ("ShipmentReceipt".equals(currentValue.getEntityName())) { shipmentReceipt = currentValue; } else { Debug.logError("Unexpected entity " + currentValue + " of type " + currentValue.getEntityName(), module); } if (orderItem == null && itemIssuance != null) { orderItem = itemIssuance.getRelatedOne("OrderItem"); } else if ((orderItem == null) && (shipmentReceipt != null)) { orderItem = shipmentReceipt.getRelatedOne("OrderItem"); } else if ((orderItem == null) && (itemIssuance == null) && (shipmentReceipt == null)) { Debug.logError("Cannot create invoice when orderItem, itemIssuance, and shipmentReceipt are all null", module); return ServiceUtil.returnError("Illegal values passed to create invoice service"); } GenericValue product = null; if (orderItem.get("productId") != null) { product = orderItem.getRelatedOne("Product"); } // get some quantities BigDecimal orderedQuantity = orderItem.getBigDecimal("quantity"); BigDecimal billingQuantity = null; if (itemIssuance != null) { billingQuantity = itemIssuance.getBigDecimal("quantity"); } else if (shipmentReceipt != null) { billingQuantity = shipmentReceipt.getBigDecimal("quantityAccepted"); } else { billingQuantity = orderedQuantity; } if (orderedQuantity == null) orderedQuantity = ZERO; if (billingQuantity == null) billingQuantity = ZERO; String lookupType = "FINISHED_GOOD"; // the default product type if (product != null) { lookupType = product.getString("productTypeId"); } else if (orderItem != null) { lookupType = orderItem.getString("orderItemTypeId"); } // check if shipping applies to this item. Shipping is calculated for sales invoices, not purchase invoices. boolean shippingApplies = false; if ((product != null) && (ProductWorker.shippingApplies(product)) && (invoiceType.equals("SALES_INVOICE"))) { shippingApplies = true; } GenericValue invoiceItem = delegator.makeValue("InvoiceItem", UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemSeqId", invoiceItemSeqId)); invoiceItem.set("invoiceItemTypeId", getInvoiceItemType(delegator, lookupType, invoiceType, "INV_FPROD_ITEM")); invoiceItem.set("description", orderItem.get("itemDescription")); invoiceItem.set("quantity", new Double(billingQuantity.doubleValue())); invoiceItem.set("amount", orderItem.get("unitPrice")); invoiceItem.set("productId", orderItem.get("productId")); invoiceItem.set("productFeatureId", orderItem.get("productFeatureId")); invoiceItem.set("overrideGlAccountId", orderItem.get("overrideGlAccountId")); //invoiceItem.set("uomId", ""); String itemIssuanceId = null; if (itemIssuance != null && itemIssuance.get("inventoryItemId") != null) { itemIssuanceId = itemIssuance.getString("itemIssuanceId"); invoiceItem.set("inventoryItemId", itemIssuance.get("inventoryItemId")); } // similarly, tax only for purchase invoices if ((product != null) && (invoiceType.equals("SALES_INVOICE"))) { invoiceItem.set("taxableFlag", product.get("taxable")); } toStore.add(invoiceItem); // this item total BigDecimal thisAmount = invoiceItem.getBigDecimal("amount").multiply(invoiceItem.getBigDecimal("quantity")).setScale(decimals, rounding); // add to the ship amount only if it applies to this item if (shippingApplies) { invoiceShipProRateAmount = invoiceShipProRateAmount.add(thisAmount).setScale(decimals, rounding); } // increment the invoice subtotal invoiceSubTotal = invoiceSubTotal.add(thisAmount).setScale(decimals, rounding); // increment the invoice quantity invoiceQuantity = invoiceQuantity.add(billingQuantity).setScale(decimals, rounding); // create the OrderItemBilling record GenericValue orderItemBill = delegator.makeValue("OrderItemBilling", UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemSeqId", invoiceItemSeqId)); orderItemBill.set("orderId", orderItem.get("orderId")); orderItemBill.set("orderItemSeqId", orderItem.get("orderItemSeqId")); orderItemBill.set("itemIssuanceId", itemIssuanceId); if ((shipmentReceipt != null) && (shipmentReceipt.getString("receiptId") != null)) { orderItemBill.set("shipmentReceiptId", shipmentReceipt.getString("receiptId")); } orderItemBill.set("quantity", invoiceItem.get("quantity")); orderItemBill.set("amount", invoiceItem.get("amount")); toStore.add(orderItemBill); // increment the counter invoiceItemSeqNum++; invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum, 2); // create the item adjustment as line items List itemAdjustments = OrderReadHelper.getOrderItemAdjustmentList(orderItem, orh.getAdjustments()); Iterator itemAdjIter = itemAdjustments.iterator(); while (itemAdjIter.hasNext()) { GenericValue adj = (GenericValue) itemAdjIter.next(); if (adj.get("amount") != null) { // pro-rate the amount // set decimals = 100 means we don't round this intermediate value, which is very important BigDecimal amount = adj.getBigDecimal("amount").divide(orderItem.getBigDecimal("quantity"), 100, rounding); amount = amount.multiply(invoiceItem.getBigDecimal("quantity")); amount = amount.setScale(decimals, rounding); GenericValue adjInvItem = delegator.makeValue("InvoiceItem", UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemSeqId", invoiceItemSeqId)); adjInvItem.set("invoiceItemTypeId", getInvoiceItemType(delegator, adj.getString("orderAdjustmentTypeId"), invoiceType, "INVOICE_ITM_ADJ")); adjInvItem.set("productId", orderItem.get("productId")); adjInvItem.set("productFeatureId", orderItem.get("productFeatureId")); //adjInvItem.set("uomId", ""); // invoice items for sales tax are not taxable themselves // TODO: This is not an ideal solution. Instead, we need to use OrderAdjustment.includeInTax when it is implemented if (!(adj.getString("orderAdjustmentTypeId").equals("SALES_TAX"))) { adjInvItem.set("taxableFlag", product.get("taxable")); } adjInvItem.set("quantity", new Double(1)); adjInvItem.set("amount", new Double(amount.doubleValue())); adjInvItem.set("description", adj.get("description")); adjInvItem.set("taxAuthPartyId", adj.get("taxAuthPartyId"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -