001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.module.ar.document.authorization;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    import java.util.Set;
021    
022    import org.kuali.kfs.module.ar.ArConstants;
023    import org.kuali.kfs.module.ar.ArPropertyConstants;
024    import org.kuali.kfs.module.ar.businessobject.OrganizationOptions;
025    import org.kuali.kfs.sys.KFSConstants;
026    import org.kuali.kfs.sys.context.SpringContext;
027    import org.kuali.kfs.sys.document.authorization.FinancialSystemMaintenanceDocumentPresentationControllerBase;
028    import org.kuali.kfs.sys.service.impl.KfsParameterConstants;
029    import org.kuali.rice.kim.bo.Person;
030    import org.kuali.rice.kim.service.KIMServiceLocator;
031    import org.kuali.rice.kim.service.RoleManagementService;
032    import org.kuali.rice.kns.document.MaintenanceDocument;
033    import org.kuali.rice.kns.service.ParameterService;
034    import org.kuali.rice.kns.util.GlobalVariables;
035    
036    public class OrganizationOptionsPresentationController extends FinancialSystemMaintenanceDocumentPresentationControllerBase {
037    
038        protected static final String ACCOUNTS_RECEIVABLE_MANAGER_ROLE_NAME = "Accounts Receivable Manager";
039        
040        @Override
041        public Set<String> getConditionallyReadOnlySectionIds(MaintenanceDocument document) {
042            Set<String> readOnlySectionIds = super.getConditionallyReadOnlySectionIds(document);
043            setRemitToAddressSectionEditable(readOnlySectionIds);
044            return readOnlySectionIds;
045        }
046    
047        @Override
048        public Set<String> getConditionallyReadOnlyPropertyNames(MaintenanceDocument document) {
049            Set<String> readOnlyPropertyNames = super.getConditionallyReadOnlyPropertyNames(document);
050            setRemitToNameEditable(readOnlyPropertyNames);
051            setOrgPostalZipCodeEditable(readOnlyPropertyNames);
052            setBillingOrgFieldsEditable(readOnlyPropertyNames, document);
053            setProcessingOrgFieldsEditable(readOnlyPropertyNames, document);
054            return readOnlyPropertyNames;
055        }
056    
057        /**
058         * 
059         * Billing Chart/Org are always read-only on an edit.  Always.
060         * 
061         * They are editable on an Add, but only if KIM lets you in on an Add, 
062         * but thats handled elsewhere.
063         * 
064         * @param readOnlyPropertyNames
065         * @param document
066         */
067        protected void setBillingOrgFieldsEditable(Set<String> readOnlyPropertyNames, MaintenanceDocument document) {
068            if (document.isEdit()) {
069                readOnlyPropertyNames.add(ArPropertyConstants.OrganizationOptionsFields.CHART_OF_ACCOUNTS_CODE);
070                readOnlyPropertyNames.add(ArPropertyConstants.OrganizationOptionsFields.ORGANIZATION_CODE);
071            }
072        }
073        
074        /**
075         * Sets the processing Chart/Org code editable
076         * 
077         * @param readOnlyPropertyNames
078         * @param document
079         */
080        protected void setProcessingOrgFieldsEditable(Set<String> readOnlyPropertyNames, MaintenanceDocument document) {
081            
082            if (document.isEdit()) {
083                
084                RoleManagementService rms = KIMServiceLocator.getRoleManagementService();
085                
086                Person user = GlobalVariables.getUserSession().getPerson();
087                String principalId = user.getPrincipalId();
088                
089                List<String> roleIds = new ArrayList<String>();
090                roleIds.add(rms.getRoleIdByName(KFSConstants.ParameterNamespaces.KFS, ACCOUNTS_RECEIVABLE_MANAGER_ROLE_NAME));
091                
092                // editable only for the AR Manager role
093                if (!rms.principalHasRole(principalId, roleIds, null)) {
094                    readOnlyPropertyNames.add(ArPropertyConstants.OrganizationOptionsFields.PROCESSING_CHART_OF_ACCOUNTS_CODE);
095                    readOnlyPropertyNames.add(ArPropertyConstants.OrganizationOptionsFields.PROCESSING_ORGANIZATION_CODE);
096                } 
097            }
098        }
099        
100        /**
101         * 
102         * Sets the Remit-To Name (ie, OrgCheckPayableToName) to read only if thats how the system parameters are 
103         * configured, otherwise leave it read/write.
104         * 
105         * @param readOnlyPropertyNames
106         */
107        protected void setRemitToNameEditable(Set<String> readOnlyPropertyNames) {
108            ParameterService service = SpringContext.getBean(ParameterService.class);
109            String nameEditable = service.getParameterValue(OrganizationOptions.class, ArConstants.REMIT_TO_NAME_EDITABLE_IND);
110            if ("N".equalsIgnoreCase(nameEditable)) {
111                readOnlyPropertyNames.add(ArPropertyConstants.OrganizationOptionsFields.ORGANIZATION_CHECK_PAYABLE_TO_NAME);
112            }
113        }
114        
115        /**
116         * 
117         * Sets the OrgPostalZipCode to readonly if thats what the system parameters say, otherwise leave it 
118         * read/write.
119         * 
120         * @param readOnlyPropertyNames
121         */
122        protected void setOrgPostalZipCodeEditable(Set<String> readOnlyPropertyNames) {
123            ParameterService service = SpringContext.getBean(ParameterService.class);
124            if (!service.getIndicatorParameter(KfsParameterConstants.ACCOUNTS_RECEIVABLE_DOCUMENT.class, ArConstants.ENABLE_SALES_TAX_IND) ){
125                readOnlyPropertyNames.add(ArPropertyConstants.OrganizationOptionsFields.ORGANIZATION_POSTAL_ZIP_CODE);
126            }
127        }
128        
129        /**
130         * 
131         * Sets the whole Remit-To Address section to read-only if thats what the system parameter says, otherwise leave
132         * it read/wrtie.
133         * 
134         * @param readOnlySectionIds
135         */
136        protected void setRemitToAddressSectionEditable(Set<String> readOnlySectionIds) {
137            ParameterService service = SpringContext.getBean(ParameterService.class);
138            String addressEditable = service.getParameterValue(OrganizationOptions.class, ArConstants.REMIT_TO_ADDRESS_EDITABLE_IND);
139            if ("N".equalsIgnoreCase(addressEditable)) {
140                readOnlySectionIds.add(ArConstants.OrganizationOptionsSections.EDIT_ORGANIZATION_REMIT_TO_ADDRESS);
141            }
142        }
143        
144    }