/* * 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 org.ofbiz.entity.*; import org.ofbiz.entity.util.*; import org.ofbiz.entity.condition.*; import org.ofbiz.base.util.*; import org.ofbiz.service.ServiceUtil; delegator = request.getAttribute("delegator"); dispatcher = request.getAttribute("dispatcher"); facilityId = request.getParameter("facilityId"); purchaseOrderId = request.getParameter("purchaseOrderId"); productId = request.getParameter("productId"); shipmentId = request.getParameter("shipmentId"); facility = null; if (facilityId != null) { facility = delegator.findByPrimaryKey("Facility", UtilMisc.toMap("facilityId", facilityId)); } ownerAcctgPref = null; if (facility != null) { owner = facility.getRelatedOne("OwnerParty"); if (owner != null) ownerAcctgPref = owner.getRelatedOne("PartyAcctgPreference"); } purchaseOrder = null; if (purchaseOrderId != null && purchaseOrderId.length() > 0) { purchaseOrder = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", purchaseOrderId)); if (purchaseOrder != null && !"PURCHASE_ORDER".equals(purchaseOrder.getString("orderTypeId"))) { purchaseOrder = null; } } product = null; if (productId != null) { product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId)); } shipments = null; if (purchaseOrder != null && shipmentId == null) { issuances = delegator.findByAnd("ItemIssuance", UtilMisc.toMap("orderId", purchaseOrderId)); if (issuances != null) { shipments = new TreeSet(); issueIter = issuances.iterator(); while (issueIter.hasNext()) { issuance = issueIter.next(); shipment = issuance.getRelatedOne("Shipment"); if (!"PURCH_SHIP_RECEIVED".equals(shipment.getString("statusId")) && !"SHIPMENT_CANCELLED".equals(shipment.getString("statusId")) && (shipment.getString("destinationFacilityId") == null || facilityId.equals(shipment.getString("destinationFacilityId")))) { shipments.add(shipment); } } } } shipment = null; if (shipmentId != null && !shipmentId.equals("_NA_")) { shipment = delegator.findByPrimaryKey("Shipment", UtilMisc.toMap("shipmentId", shipmentId)); } shippedQuantities = new HashMap(); purchaseOrderItems = null; if (purchaseOrder != null) { if (product != null) { purchaseOrderItems = purchaseOrder.getRelated("OrderItem", UtilMisc.toMap("productId", productId), null); } else if (shipment != null) { orderItems = purchaseOrder.getRelated("OrderItem"); issuances = shipment.getRelated("ItemIssuance", UtilMisc.toMap("orderId", purchaseOrderId), null); issueIter = issuances.iterator(); exprs = new ArrayList(); while (issueIter.hasNext()) { issuance = issueIter.next(); exprs.add(new EntityExpr("orderItemSeqId", EntityOperator.EQUALS, issuance.getString("orderItemSeqId"))); double issuanceQty = issuance.getDouble("quantity").doubleValue(); if (shippedQuantities.containsKey(issuance.getString("orderItemSeqId"))) { issuanceQty += ((Double)shippedQuantities.get(issuance.getString("orderItemSeqId"))).doubleValue(); } shippedQuantities.put(issuance.getString("orderItemSeqId"), issuanceQty); } purchaseOrderItems = EntityUtil.filterByOr(orderItems, exprs); } else { purchaseOrderItems = purchaseOrder.getRelated("OrderItem"); } } // convert the unit prices to that of the facility owner's currency if (purchaseOrder != null && facility != null) { if (ownerAcctgPref != null) { ownerCurrencyUomId = ownerAcctgPref.get("baseCurrencyUomId"); orderCurrencyUomId = purchaseOrder.get("currencyUom"); if (!orderCurrencyUomId.equals(ownerCurrencyUomId)) { for (iter = purchaseOrderItems.iterator(); iter.hasNext(); ) { item = iter.next(); serviceResults = dispatcher.runSync("convertUom", UtilMisc.toMap("uomId", orderCurrencyUomId, "uomIdTo", ownerCurrencyUomId, "originalValue", item.get("unitPrice"))); if (ServiceUtil.isError(serviceResults)) { request.setAttribute("_ERROR_MESSAGE_", ServiceUtil.getErrorMessage(serviceResults)); return; } else { convertedValue = serviceResults.get("convertedValue"); if (convertedValue != null) { item.put("unitPrice", convertedValue); } } } } // put the pref currency in the map for display and form use context.put("currencyUomId", ownerCurrencyUomId); } else { request.setAttribute("_ERROR_MESSAGE_", "Either no owner party was set for this facility, or no accounting preferences were set for this owner party."); } } receivedQuantities = new HashMap(); salesOrderItems = new HashMap(); if (purchaseOrderItems != null && purchaseOrderItems.size() > 0) { context.put("firstOrderItem", EntityUtil.getFirst(purchaseOrderItems)); context.put("purchaseOrderItemsSize", purchaseOrderItems.size()); itemsIter = purchaseOrderItems.iterator(); while (itemsIter.hasNext()) { totalReceived = 0.0; thisItem = itemsIter.next(); receipts = thisItem.getRelated("ShipmentReceipt"); if (receipts != null && receipts.size() > 0) { recIter = receipts.iterator(); while (recIter.hasNext()) { rec = recIter.next(); if (shipment != null) { if (rec.getString("shipmentId") == null || !rec.getString("shipmentId").equals(shipment.getString("shipmentId"))) { continue; } } accepted = rec.getDouble("quantityAccepted"); rejected = rec.getDouble("quantityRejected"); if (accepted != null) totalReceived += accepted.doubleValue(); if (rejected != null) totalReceived += rejected.doubleValue(); } } receivedQuantities.put(thisItem.getString("orderItemSeqId"), new Double(totalReceived)); //---------------------- salesOrderItemAssocs = delegator.findByAnd("OrderItemAssoc", UtilMisc.toMap("orderItemAssocTypeId", "PURCHASE_ORDER", "toOrderId", thisItem.getString("orderId"), "toOrderItemSeqId", thisItem.getString("orderItemSeqId"))); if (salesOrderItemAssocs != null && salesOrderItemAssocs.size() > 0) { salesOrderItem = EntityUtil.getFirst(salesOrderItemAssocs); salesOrderItems.put(thisItem.getString("orderItemSeqId"), salesOrderItem); } } } receivedItems = null; if (purchaseOrder != null) { receivedItems = delegator.findByAnd("ShipmentReceiptAndItem", UtilMisc.toMap("orderId", purchaseOrderId, "facilityId", facilityId)); context.put("receivedItems", receivedItems); } String invalidProductId = null; if (productId != null && productId.length() > 0 && product == null) { invalidProductId = "No product found with product ID: [" + productId + "]"; context.put("invalidProductId", invalidProductId); } // reject reasons rejectReasons = delegator.findAll("RejectionReason"); // inv item types inventoryItemTypes = delegator.findAll("InventoryItemType"); // facilities facilities = delegator.findAll("Facility"); // default per unit cost for both shipment or individual product standardCosts = new HashMap(); if (ownerAcctgPref != null) { // get the unit cost of the products in a shipment if (purchaseOrderItems != null) { for (iter = purchaseOrderItems.iterator(); iter.hasNext(); ) { orderItem = iter.next(); productId = orderItem.get("productId"); if (productId == null) continue; result = dispatcher.runSync("getProductCost", UtilMisc.toMap("productId", productId, "currencyUomId", ownerAcctgPref.get("baseCurrencyUomId"), "costComponentTypePrefix", "EST_STD", "userLogin", request.getAttribute("userLogin"))); if (!ServiceUtil.isError(result)) { standardCosts.put(productId, result.get("productCost")); } } } // get the unit cost of a single product if (productId != null) { result = dispatcher.runSync("getProductCost", UtilMisc.toMap("productId", productId, "currencyUomId", ownerAcctgPref.get("baseCurrencyUomId"), "costComponentTypePrefix", "EST_STD", "userLogin", request.getAttribute("userLogin"))); if (!ServiceUtil.isError(result)) { standardCosts.put(productId, result.get("productCost")); } } } context.put("facilityId", facilityId); context.put("facility", facility); context.put("purchaseOrder", purchaseOrder); context.put("product", product); context.put("shipments", shipments); context.put("shipment", shipment); context.put("shippedQuantities", shippedQuantities); context.put("purchaseOrderItems", purchaseOrderItems); context.put("receivedQuantities", receivedQuantities); context.put("salesOrderItems", salesOrderItems); context.put("rejectReasons", rejectReasons); context.put("inventoryItemTypes", inventoryItemTypes); context.put("facilities", facilities); context.put("standardCosts", standardCosts);