⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 invoiceservices.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                    try {
                        orderItem = itemIssuance.getRelatedOne("OrderItem");
                    } catch (GenericEntityException e) {                   
                        Debug.logError(e, "Trouble getting related OrderItem from ItemIssuance", module);
                        return ServiceUtil.returnError("Trouble getting OrderItem from ItemIssuance");
                    }
                } else if (orderItem == null && itemIssuance == null) {
                    Debug.logError("Cannot create invoice when both orderItem and itemIssuance is null", module);
                    return ServiceUtil.returnError("Illegal values passed to create invoice service");
                }
                GenericValue product = null;
                if (orderItem.get("productId") != null) {                
                    try {
                        product = orderItem.getRelatedOne("Product");                  
                    } catch (GenericEntityException e) {
                        Debug.logError(e, "Trouble getting Product from OrderItem", module);
                        ServiceUtil.returnError("Trouble getting Product from OrderItem");
                    }
                }
                
                // get some quantities
                Double orderedQuantity = orderItem.getDouble("quantity");
                Double billingQuantity = null;
                if (itemIssuance != null) {
                    billingQuantity = itemIssuance.getDouble("quantity");
                } else {
                    billingQuantity = orderedQuantity;
                }
                if (orderedQuantity == null) orderedQuantity = new Double(0.00);
                if (billingQuantity == null) billingQuantity = new Double(0.00);

                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
                boolean shippingApplies = false;
                if (product != null && ProductWorker.shippingApplies(product)) {
                    shippingApplies = true;
                }

                GenericValue invoiceItem = delegator.makeValue("InvoiceItem", UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemSeqId", new Integer(itemSeqId).toString()));
                invoiceItem.set("invoiceItemTypeId", getInvoiceItemType(delegator, lookupType, "INV_FPROD_ITEM"));
                invoiceItem.set("description", orderItem.get("itemDescription"));
                invoiceItem.set("quantity", billingQuantity);
                invoiceItem.set("amount", orderItem.get("unitPrice"));
                invoiceItem.set("productId", orderItem.get("productId"));
                invoiceItem.set("productFeatureId", orderItem.get("productFeatureId"));
                //invoiceItem.set("uomId", "");
                
                String itemIssuanceId = null;           
                if (itemIssuance != null && itemIssuance.get("inventoryItemId") != null) {
                    itemIssuanceId = itemIssuance.getString("itemIssuanceId");                
                    invoiceItem.set("inventoryItemId", itemIssuance.get("inventoryItemId"));
                }                                                   
                if (product != null) {
                    invoiceItem.set("taxableFlag", product.get("taxable"));
                }                                
                toStore.add(invoiceItem);

                // this item total
                double thisAmount = invoiceItem.getDouble("amount").doubleValue() * invoiceItem.getDouble("quantity").doubleValue();

                // add to the ship amount only if it applies to this item
                if (shippingApplies) {
                    invoiceShipProRateAmount += thisAmount;
                }

                // increment the invoice subtotal
                invoiceSubTotal += thisAmount;

                // increment the invoice quantity
                invoiceQuantity += billingQuantity.doubleValue();

                // create the OrderItemBilling record
                GenericValue orderItemBill = delegator.makeValue("OrderItemBilling", UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemSeqId", new Integer(itemSeqId).toString()));
                orderItemBill.set("orderId", orderItem.get("orderId"));
                orderItemBill.set("orderItemSeqId", orderItem.get("orderItemSeqId"));
                orderItemBill.set("itemIssuanceId", itemIssuanceId);
                orderItemBill.set("quantity", invoiceItem.get("quantity"));
                orderItemBill.set("amount", invoiceItem.get("amount"));
                toStore.add(orderItemBill);
                        
                // increment the counter
                itemSeqId++;                
                
                // 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
                        double amount = ((adj.getDouble("amount").doubleValue() / orderItem.getDouble("quantity").doubleValue()) * invoiceItem.getDouble("quantity").doubleValue());
                        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(amount));
                        adjInvItem.set("description", adj.get("description"));
                        toStore.add(adjInvItem);
                        
                        // this adjustment amount
                        double thisAdjAmount = adjInvItem.getDouble("amount").doubleValue() * adjInvItem.getDouble("quantity").doubleValue();

                        // 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 += thisAdjAmount;

                            // add to the ship amount only if it applies to this item
                            if (shippingApplies) {
                                invoiceShipProRateAmount += thisAdjAmount;
                            }
                        }

                        // increment the counter
                        itemSeqId++;
                    }
                    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() * (invoiceItem.getDouble("amount").doubleValue() * invoiceItem.getDouble("quantity").doubleValue());
                        if (amountPerQty != null)
                            totalAmount += amountPerQty.doubleValue() * invoiceItem.getDouble("quantity").doubleValue();

                        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", orderItem.getDouble("quantity"));
                        adjInvItem.set("amount", new Double(totalAmount));
                        adjInvItem.set("description", adj.get("description"));
                        toStore.add(adjInvItem);
                        
                        // this adjustment amount
                        double thisAdjAmount = adjInvItem.getDouble("amount").doubleValue() * adjInvItem.getDouble("quantity").doubleValue();

                        // 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 += thisAdjAmount;

                            // add to the ship amount only if it applies to this item
                            if (shippingApplies) {
                                invoiceShipProRateAmount += thisAdjAmount;
                            }
                        }

                        // increment the counter
                        itemSeqId++;
                    }
                }
            }
        }

        // get the shipping adjustment mode (Y = Pro-Rate; N = First-Invoice)
        String prorateShipping = productStore.getString("prorateShipping");
        if (prorateShipping == null) {
            prorateShipping = "Y"; 
        }
        
        // 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 {
                // other adjustment type
                double adjAmount = calcHeaderAdj(delegator, adj, invoiceId, itemSeqId, toStore, orderSubTotal, invoiceSubTotal, invoiceQuantity);
                // these will effect the shipping pro-rate (unless commented)
                // invoiceShipProRateAmount += adjAmount;
                // do adjustments compound or are they based off subtotal? Here we will (unless commented)
                // invoiceSubTotal += adjAmount;

                // increment the counter
                itemSeqId++;
            }
        }

        // 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
                    double adjAmount = calcHeaderAdj(delegator, adj, invoiceId, itemSeqId, toStore, 1, 1, totalItemsInOrder);
                    // should shipping effect the tax pro-rate?
                    invoiceSubTotal += adjAmount; // here we do

                    // increment the counter
                    itemSeqId++;
                }
            } else {
                // pro-rate the shipping amount based on shippable information
                double adjAmount = calcHeaderAdj(delegator, adj, invoiceId, itemSeqId, toStore, shippableAmount, invoiceShipProRateAmount, invoiceQuantity);
                // should shipping effect the tax pro-rate?
                invoiceSubTotal += adjAmount; // here we do

                // increment the counter
                itemSeqId++;
            }
        }

        // last do the tax adjustments
        Iterator taxAdjIter = taxAdjustments.iterator();
        while (taxAdjIter.hasNext()) {
            GenericValue adj = (GenericValue) taxAdjIter.next();
            double adjAmount = calcHeaderAdj(delegator, adj, invoiceId, itemSeqId, toStore, orderSubTotal, invoiceSubTotal, invoiceQuantity);
            // this doesn't really effect anything; but just for our totals
            invoiceSubTotal += adjAmount;
        }

        // invoice status object
        GenericValue invStatus = delegator.makeValue("InvoiceStatus",
            UtilMisc.toMap("invoiceId", invoiceId, "statusId", "INVOICE_IN_PROCESS", "statusDate", UtilDateTime.nowTimestamp()));
        toStore.add(invStatus);

        // check for previous order payments
        List orderPaymentPrefs = null;
        try {
            orderPaymentPrefs = delegator.findByAnd("OrderPaymentPreference", UtilMisc.toMap("orderId", orderId));
        } catch (GenericEntityException e) {
            Debug.logError(e, "Problem getting order payment preference records", module);
            return ServiceUtil.returnError("Problem getting order payment preference records");
        }
        if (orderPaymentPrefs != null) {
            List currentPayments = new ArrayList();
            Iterator opi = orderPaymentPrefs.iterator();
            while (opi.hasNext()) {
                GenericValue paymentPref = (GenericValue) opi.next();
                try {
                    List payments = paymentPref.getRelated("Payment");
                    currentPayments.addAll(payments);
                } catch (GenericEntityException e) {
                    Debug.logError(e, "Problem getting payments from preference", module);
                    return ServiceUtil.returnError("Problem getting payments from preference");
                }
            }
            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;
                    try {
                        currentApplications = payment.getRelated("PaymentApplication");
                    } catch (GenericEntityException e) {
                        Debug.logError(e, "Problem getting application(s) for payment", module);
                        return ServiceUtil.returnError("Problem getting application(s) for payment");
                    }
                    if (currentApplications == null || currentApplications.size() == 0) {
                        // no applications; okay to apply
                        String applId = delegator.getNextSeqId("PaymentApplication").toString();
                        GenericValue appl = delegator.makeValue("PaymentApplication", UtilMisc.toMap("paymentApplicationId", applId));
                        appl.set("paymentId", payment.get("paymentId"));
                        appl.set("invoiceId", invoice.get("invoiceId"));
                        appl.set("billingAccountId", invoice.get("billingAccountId"));
                        appl.set("amountApplied", payment.get("amount"));
                        toStore.add(appl);
                    }
                }
            }
        }

        // store value objects

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -