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

📄 configurationservices.java

📁 Sequoia ERP是一个真正的企业级开源ERP解决方案。它提供的模块包括:电子商务应用(e-commerce), POS系统(point of sales),知识管理,存货与仓库管理
💻 JAVA
字号:
/*
 * 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
 */

package com.opensourcestrategies.financials.configuration;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.sql.Timestamp;
import java.math.BigDecimal;

import javolution.util.FastMap;

import org.ofbiz.accounting.util.UtilAccounting;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilNumber;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityConditionList;
import org.ofbiz.entity.condition.EntityExpr;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ModelService;
import org.ofbiz.service.ServiceUtil;

import com.opensourcestrategies.financials.util.UtilFinancial;

/**
 * ConfigurationServices - Services for configuring GL Accounts
 *
 * @author     <a href="mailto:ali@opensourcestrategies.com">Ali Afzal Malik</a> 
 * @version    $Rev: 150 $
 * @since      2.2
 */

public class ConfigurationServices {
    
    public static String module = ConfigurationServices.class.getName();
    
    /**
     * Removes a GL Account from an organization if it is not associated with any other entity
     */
    public static Map removeGlAccountFromOrganization(DispatchContext dctx, Map context) {
    	GenericDelegator delegator = dctx.getDelegator();
        LocalDispatcher dispatcher = dctx.getDispatcher();
    	GenericValue userLogin = (GenericValue) context.get("userLogin");    	
        String glAccountId = (String) context.get("glAccountId");
        String organizationPartyId = (String) context.get("organizationPartyId");
        Map result = new HashMap();
        List value = null;
        GenericValue glAccount = null;

        Map fields = UtilMisc.toMap("glAccountId", glAccountId, "organizationPartyId", organizationPartyId);
        
        try {
        	//check for relation with GlAccountTypeDefault
            value = delegator.findByAnd("GlAccountTypeDefault", fields);              
            if (!value.isEmpty()) {      
            	return ServiceUtil.returnError("Could not remove Gl Account from organization because it is associated with an account type through GlAccountTypeDefault.");
            }
            
            //check for relation with InvoiceItemTypeGlAccount
            value = delegator.findByAnd("InvoiceItemTypeGlAccount", fields);            
            if (!value.isEmpty()) {    
            	return ServiceUtil.returnError("Could not remove Gl Account from organization because it is associated with an invoice item type through InvoiceItemTypeGlAccount.");
            }
            
            //check for relation with PaymentMethod
            fields = UtilMisc.toMap("glAccountId", glAccountId, "partyId", organizationPartyId);
            value = delegator.findByAnd("PaymentMethod", fields);            
            if (!value.isEmpty()) { 
            	return ServiceUtil.returnError("Could not remove Gl Account from organization because it is associated with a payment method through PaymentMethod.");
            }
            
            //reset fields
            fields = UtilMisc.toMap("glAccountId", glAccountId, "organizationPartyId", organizationPartyId);
            
            //check for relation with PaymentMethodTypeGlAccount
            value = delegator.findByAnd("PaymentMethodTypeGlAccount", fields);            
            if (!value.isEmpty()) {  
            	return ServiceUtil.returnError("Could not remove Gl Account from organization because it is associated with a payment method type through PaymentMethodTypeGlAccount.");
            }
            
            //check for relation with ProductGlAccount
            value = delegator.findByAnd("ProductGlAccount", fields);            
            if (!value.isEmpty()) {   
            	return ServiceUtil.returnError("Could not remove Gl Account from organization because it is associated with a product through ProductGlAccount.");
            }
            
            //check for relation with VarianceReasonGlAccount
            value = delegator.findByAnd("VarianceReasonGlAccount", fields);            
            if (!value.isEmpty()) {  
            	return ServiceUtil.returnError("Could not remove Gl Account from organization because it is associated with an inventory variance reason through VarianceReasonGlAccount.");
            }
            
            //remove the GL Account by setting the thru date to now date
            Map updateGlAccountOrganizationContext = UtilMisc.toMap("glAccountId", glAccountId, "organizationPartyId", organizationPartyId, "thruDate", UtilDateTime.nowTimestamp(), "userLogin", userLogin);
            Map updateGlAccountOrganizationResult = dispatcher.runSync("updateGlAccountOrganization", updateGlAccountOrganizationContext);
            
            if(ServiceUtil.isError(updateGlAccountOrganizationResult)) {
            	return updateGlAccountOrganizationResult;
            }
            
        	return ServiceUtil.returnSuccess();
        } catch (GenericEntityException e) {
            return ServiceUtil.returnError("Could not remove Gl Account from organization (" + e.getMessage() + ").");
        } catch (GenericServiceException e) {
        	return ServiceUtil.returnError("Could not remove Gl Account from organization (" + e.getMessage() + ").");
        }
    }
    
    /**
     * Adds a new GL Account and associates it to an Organization if the specified account
     * code is unique i.e. no existing GL Account has the same account code
     */
    public static Map addNewGlAccount(DispatchContext dctx, Map context) {
    	GenericDelegator delegator = dctx.getDelegator();
        LocalDispatcher dispatcher = dctx.getDispatcher();
    	GenericValue userLogin = (GenericValue) context.get("userLogin");
    	String glAccountId = null;
        String accountCode = (String) context.get("accountCode");
        String accountName = (String) context.get("accountName");
        String description = (String) context.get("description");
        String glAccountTypeId = (String) context.get("glAccountTypeId");
        String glAccountClassId = (String) context.get("glAccountClassId");
        String glResourceTypeId = (String) context.get("glResourceTypeId");
        String parentGlAccountId = (String) context.get("parentGlAccountId");
        Double postedBalance = (Double) context.get("postedBalance");
        String organizationPartyId = (String) context.get("organizationPartyId");
        Map result = new HashMap();
        List value = null;

        Map fields = UtilMisc.toMap("accountCode", accountCode);
        
        try {
        	//check whether the account code is already present
            value = delegator.findByAnd("GlAccount", fields);              
            if (!value.isEmpty()) {      
            	return ServiceUtil.returnError("The account code specified [" + accountCode + "] is already associated with an existing Gl Account.");
            }
            else {
            	glAccountId = accountCode;
            }
            
            //TODO: Confirm parameters
            //Add a new Gl Account and a new Gl Account Organization
            Map addNewGlAccountContext = UtilMisc.toMap("glAccountId", glAccountId, "accountCode", accountCode, "accountName", accountName, "description", description, "glAccountTypeId", glAccountTypeId, "glAccountClassId", glAccountClassId);
            addNewGlAccountContext.put("glResourceTypeId", glResourceTypeId);
            addNewGlAccountContext.put("parentGlAccountId", parentGlAccountId);
            addNewGlAccountContext.put("postedBalance", postedBalance);
            addNewGlAccountContext.put("userLogin", userLogin);
            Map addNewGlAccountResult = dispatcher.runSync("createGlAccount", addNewGlAccountContext);
            
            if(ServiceUtil.isError(addNewGlAccountResult)) {
            	return addNewGlAccountResult;
            }
            
            Map addNewGlAccountOrganizationContext = UtilMisc.toMap("glAccountId", glAccountId, "organizationPartyId", organizationPartyId, "postedBalance", postedBalance, "userLogin", userLogin);
            Map addNewGlAccountOrganizationResult = dispatcher.runSync("createGlAccountOrganization", addNewGlAccountOrganizationContext);
            if(ServiceUtil.isError(addNewGlAccountOrganizationResult)) {
            	return addNewGlAccountOrganizationResult;
            }
            
            return ServiceUtil.returnSuccess();
        } catch (GenericEntityException e) {
            return ServiceUtil.returnError("Could not add the Gl Account (" + e.getMessage() + ").");
        } catch (GenericServiceException e) {
        	return ServiceUtil.returnError("Could not add the Gl Account (" + e.getMessage() + ").");
        }
	}
}

⌨️ 快捷键说明

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