📄 importproductinventory.bsh
字号:
/* * Copyright (C) 2006 Open Source Strategies, Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *//** * Script to generate data from FinancialsImportProduct. Running this script is the * final step in the data import process (aside from vetting the results). * It will create InventoryItems, InventoryItemDetails, ProductAverageCosts, and a series of * AcctgTrans and AcctgTransEntries. * * Only non-serialized inventory supported. Make sure that all costs are normalized * to a single currencyUomId. * * If bad data is imported and you wish to re-import, delete the affected rows from * InventoryItems, InventoryItemDetails, ProductAverageCosts, AcctgTrans, and AcctgTransEntries. Then set the * processedTimestamp of the corresponding FinancialsImportProduct rows to null. * * @author Leon Torres (leon@opensourcestrategies.com) * @author Si Chen (sichen@opensourcestrategies.com) */import java.util.*;import org.ofbiz.base.util.*;import org.ofbiz.entity.*;import org.ofbiz.entity.condition.*;import org.ofbiz.service.*;// dctx is available in beanshell service context just like in a Java servicedelegator = dctx.getDelegator();dispatcher = dctx.getDispatcher();module = "ImportProduct.bsh"; // this doesn't do anything right now, but maybe one day we'll fix the Debug class// this is also pretty similar to Java servicesorganizationPartyId = context.get("organizationPartyId"); // organization of the import datafacilityId = context.get("facilityId"); // destination facility for the import inventory datainventoryGlAccountId = context.get("inventoryGlAccountId"); // inventory and offsetting GL accountsoffsettingGlAccountId = context.get("offsettingGlAccountId");fromDate = UtilDateTime.nowTimestamp(); // from date for the ProductAverageCostuserLogin = context.get("userLogin");partyAcctgPref = delegator.findByPrimaryKeyCache("PartyAcctgPreference", UtilMisc.toMap("partyId", organizationPartyId));currencyUomId = partyAcctgPref.getString("baseCurrencyUomId");// A comment to add to every InventoryItem generated by this scriptcomments = "Auto-generated from Product Inventory Import.";// some constants that shouldn't be changedinventoryItemTypeId = "NON_SERIAL_INV_ITEM";now = UtilDateTime.nowTimestamp();// get the FinancialsImportProducts that have not been processed yetimports = delegator.findByAnd("FinancialsImportProductInventory", UtilMisc.toMap("processedTimestamp", null));// build the static arguments for creating inventory iteminventoryItemInput = UtilMisc.toMap("inventoryItemTypeId", inventoryItemTypeId, "currencyUomId", currencyUomId, "ownerPartyId", organizationPartyId);inventoryItemInput.put("facilityId", facilityId);inventoryItemInput.put("datetimeReceived", now);inventoryItemInput.put("comments", comments);inventoryItemInput.put("userLogin", userLogin);// build the static arguments for creating product average costavgCostInput = UtilMisc.toMap("organizationPartyId", organizationPartyId, "fromDate", fromDate);// the static arguments for creating acctg trans entriesacctgTransEntryInput = UtilMisc.toMap("acctgTransEntryTypeId", "_NA_", "organizationPartyId", organizationPartyId, "currencyUomId", currencyUomId, "reconcileStatusId", "AES_NOT_RECONCILED");// import one by oneimported = 0;skipped = 0;acctgTransSeqNum = 1;try { // create the header of an AcctgTrans record for all the subsequent transaction entries results = dispatcher.runSync("createAcctgTrans", UtilMisc.toMap("acctgTransTypeId", "INTERNAL_ACCTG_TRANS", "glFiscalTypeId", "ACTUAL", "transactionDate", now, "userLogin", userLogin)); if (ServiceUtil.isError(results)) { return results; } acctgTransId = results.get("acctgTransId"); for (iter = imports.iterator(); iter.hasNext(); ) { importProduct = iter.next(); productId = importProduct.get("productId"); onHand = importProduct.get("onHand"); availableToPromise = importProduct.get("availableToPromise"); inventoryValue = importProduct.get("inventoryValue"); averageCost = 0.0; if (onHand > 0.0) { averageCost = inventoryValue/onHand; } // verify that productId exists product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId)); if (product == null) { message = "Could not find product ["+productId+"], not importing."; Debug.logInfo(message, module); skipped += 1; continue; } // create the inventory item inventoryItemInput.put("unitCost", averageCost); inventoryItemInput.put("productId", productId); results = dispatcher.runSync("createInventoryItem", inventoryItemInput); if (ServiceUtil.isError(results)) { message = "Failed to import product ["+productId+"]: " + results.getMessage(); message += "\nImported: "+imported+", Skipped: "+skipped+"."; Debug.logWarning(message, module); return ServiceUtil.returnError(message); } inventoryItemId = results.get("inventoryItemId"); // create the inventory item detail inventoryDetailInput = UtilMisc.toMap("inventoryItemId", inventoryItemId, "quantityOnHandDiff", onHand, "userLogin", userLogin); if (availableToPromise != null) { inventoryDetailInput.put("availableToPromiseDiff", availableToPromise); } else { inventoryDetailInput.put("availableToPromiseDiff", onHand); } results = dispatcher.runSync("createInventoryItemDetail", inventoryDetailInput); if (ServiceUtil.isError(results)) { message = "Failed to import product ["+productId+"]: " + results.getMessage(); message += "\nImported: "+imported+", Skipped: "+skipped+"."; Debug.logWarning(message, module); return ServiceUtil.returnError(message); } // create the product average cost avgCostInput.put("averageCost", averageCost); avgCostInput.put("productId", productId); delegator.create("ProductAverageCost", avgCostInput); // create the two AcctgTransEntries for this item. Note that using the createAcctgTransEntry service was very slow--2.5 seconds per entry, or 5 seconds // per product, so we switched to direct call on the delegator, which became very very fast String acctgTransSeqId = UtilFormatOut.formatPaddedNumber(acctgTransSeqNum, 6); acctgTransEntryInput.put("acctgTransId", acctgTransId); acctgTransEntryInput.put("acctgTransEntrySeqId", acctgTransSeqId); acctgTransEntryInput.put("productId", productId); acctgTransEntryInput.put("glAccountId", inventoryGlAccountId); acctgTransEntryInput.put("debitCreditFlag", "D"); acctgTransEntryInput.put("amount", inventoryValue); delegator.create("AcctgTransEntry", acctgTransEntryInput); acctgTransSeqNum++; String acctgTransSeqId = UtilFormatOut.formatPaddedNumber(acctgTransSeqNum, 6); acctgTransEntryInput.put("acctgTransEntrySeqId", acctgTransSeqId); acctgTransEntryInput.put("glAccountId", offsettingGlAccountId); acctgTransEntryInput.put("debitCreditFlag", "C"); delegator.create("AcctgTransEntry", acctgTransEntryInput); acctgTransSeqNum++; // mark this row as processed importProduct.set("processedTimestamp", now); importProduct.store(); // inform user and log about success Debug.logInfo("Imported product ["+productId+"] (onHand = "+onHand+", averageCost = "+averageCost+" "+currencyUomId+") into InventoryItem ["+inventoryItemId+"]", module); imported += 1; }} catch (GenericEntityException e) { message = "Failed to import due to error: " + e.getMessage(); return ServiceUtil.returnError(message);} catch (GenericServiceException e) { message = "Failed to import due to error: " + e.getMessage(); return ServiceUtil.returnError(message);}Debug.logInfo("Imported: " + imported + " inventory item records, Skipped: "+skipped+".", module);// this is how you pass results after a service is done context.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);context.put("importedRecords", new Long(imported));context.put("skippedRecords", new Long(skipped));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -