/******************************************************************************* * 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. *******************************************************************************/ package org.apache.ofbiz.accounting.ledger; import java.math.BigDecimal; import java.util.Map; import org.apache.ofbiz.base.util.Debug; import org.apache.ofbiz.base.util.UtilGenerics; import org.apache.ofbiz.base.util.UtilMisc; import org.apache.ofbiz.base.util.UtilValidate; import org.apache.ofbiz.entity.GenericValue; import org.apache.ofbiz.service.DispatchContext; import org.apache.ofbiz.service.GenericServiceException; import org.apache.ofbiz.service.LocalDispatcher; import org.apache.ofbiz.service.ServiceUtil; public class GeneralLedgerServices { public static final String module = GeneralLedgerServices.class.getName(); private static BigDecimal ZERO = BigDecimal.ZERO; public static Map<String, Object> createUpdateCostCenter(DispatchContext dctx, Map<String, ? extends Object> context) { LocalDispatcher dispatcher = dctx.getDispatcher(); BigDecimal totalAmountPercentage = ZERO; Map<String, Object> createGlAcctCatMemFromCostCentersMap = null; String glAccountId = (String) context.get("glAccountId"); GenericValue userLogin = (GenericValue) context.get("userLogin"); Map<String, String> amountPercentageMap = UtilGenerics.checkMap(context.get("amountPercentageMap")); totalAmountPercentage = GeneralLedgerServices.calculateCostCenterTotal(amountPercentageMap); Map<String, Object> result = ServiceUtil.returnSuccess(); for (String rowKey : amountPercentageMap.keySet()) { String rowValue = amountPercentageMap.get(rowKey); if (UtilValidate.isNotEmpty(rowValue)) { createGlAcctCatMemFromCostCentersMap = UtilMisc.toMap("glAccountId", glAccountId, "glAccountCategoryId", rowKey, "amountPercentage", new BigDecimal(rowValue), "userLogin", userLogin, "totalAmountPercentage", totalAmountPercentage); } else { createGlAcctCatMemFromCostCentersMap = UtilMisc.toMap("glAccountId", glAccountId, "glAccountCategoryId", rowKey, "amountPercentage", new BigDecimal(0), "userLogin", userLogin, "totalAmountPercentage", totalAmountPercentage); } try { result = dispatcher.runSync("createGlAcctCatMemFromCostCenters", createGlAcctCatMemFromCostCentersMap); } catch (GenericServiceException e) { Debug.logError(e, module); return ServiceUtil.returnError(e.getMessage()); } } return result; } public static BigDecimal calculateCostCenterTotal(Map<String, String> amountPercentageMap) { BigDecimal totalAmountPercentage = ZERO; for (String rowKey : amountPercentageMap.keySet()) { if (UtilValidate.isNotEmpty(amountPercentageMap.get(rowKey))) { BigDecimal rowValue = new BigDecimal(amountPercentageMap.get(rowKey)); if (rowValue != null) totalAmountPercentage = totalAmountPercentage.add(rowValue); } } return totalAmountPercentage; } }