/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import java.util.Date; import java.sql.Timestamp; import org.ofbiz.base.util.UtilDateTime; import org.ofbiz.base.util.UtilMisc; import org.ofbiz.entity.util.EntityUtil; quoteCoefficientsIt = quoteCoefficients.iterator(); costMult = 0.0; while (quoteCoefficientsIt.hasNext()) { quoteCoefficient = quoteCoefficientsIt.next(); value = quoteCoefficient.getDouble("coeffValue"); if (value != null) { costMult += value; } } double totalProfit = 0.0; double costToPriceMult = 1.0; if (costMult != 100) { costToPriceMult = 100 / (100 - costMult); } Timestamp issueDate = quote.getTimestamp("issueDate"); if (issueDate == null) { issueDate = UtilDateTime.nowTimestamp(); } double totalCost = 0.0; double totalPrice = 0.0; double totalCostMult = 0.0; currency = quote.getString("currencyUomId"); quoteItemAndCostInfos = new java.util.ArrayList(); quoteItemsIt = quoteItems.iterator(); while (quoteItemsIt.hasNext()) { quoteItem = quoteItemsIt.next(); double defaultQuoteUnitPrice = 0.0; double averageCost = 0.0; double unitPrice = 0.0; double quantity = 1.0; double profit = 0.0; double percProfit = 0.0; double selectedAmount = 1.0; if (quoteItem.get("quantity") != null) { quantity = (quoteItem.getDouble("quantity")).doubleValue(); } if (quoteItem.get("selectedAmount") != null) { selectedAmount = (quoteItem.getDouble("selectedAmount")).doubleValue(); } if (selectedAmount == 0.0) { selectedAmount = 1.0; } if (quoteItem.get("quoteUnitPrice") != null) { unitPrice = (quoteItem.getDouble("quoteUnitPrice")).doubleValue(); } try { if (currency != null && quoteItem.get("productId") != null) { productPrices = delegator.findByAnd("ProductPrice", UtilMisc.toMap("productId", quoteItem.getString("productId"), "currencyUomId", currency, "productPriceTypeId", "AVERAGE_COST")); productPrices = EntityUtil.filterByDate(productPrices, issueDate); productPrice = EntityUtil.getFirst(productPrices); if (productPrice != null && productPrice.get("price") != null) { averageCost = (productPrice.getDouble("price")).doubleValue(); averageCost = averageCost * selectedAmount; } } //defaultQuoteUnitPrice = averageCost * costToPriceMult * selectedAmount; totalCost += (averageCost * quantity); totalPrice += (unitPrice * quantity); } catch(Exception exc) { Debug.logError("Problems getting the averageCost for quoteItem: " + quoteItem); } profit = unitPrice - averageCost; percProfit = averageCost != 0? (unitPrice / averageCost) * 100.00: 0.00; quoteItemAndCostInfo = new java.util.HashMap(quoteItem); quoteItemAndCostInfo.put("averageCost", averageCost); quoteItemAndCostInfo.put("profit", profit); quoteItemAndCostInfo.put("percProfit", percProfit); quoteItemAndCostInfos.add(quoteItemAndCostInfo); } totalProfit = totalPrice - totalCost; context.put("costMult", costMult); context.put("costToPriceMult", costToPriceMult); context.put("quoteItemAndCostInfos", quoteItemAndCostInfos); context.put("totalCost", totalCost); context.put("totalPrice", totalPrice); context.put("totalProfit", totalProfit); context.put("totalPercProfit", totalCost != 0? (totalPrice / totalCost) * 100.00: 0.00);