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.service.impl;
017    
018    import java.util.HashMap;
019    import java.util.Map;
020    
021    import org.kuali.kfs.module.ar.ArConstants;
022    import org.kuali.kfs.module.ar.businessobject.AccountsReceivableDocumentHeader;
023    import org.kuali.kfs.module.ar.businessobject.OrganizationOptions;
024    import org.kuali.kfs.module.ar.businessobject.SystemInformation;
025    import org.kuali.kfs.module.ar.document.service.AccountsReceivableDocumentHeaderService;
026    import org.kuali.kfs.module.ar.document.service.SystemInformationService;
027    import org.kuali.kfs.sys.businessobject.ChartOrgHolder;
028    import org.kuali.kfs.sys.context.SpringContext;
029    import org.kuali.kfs.sys.service.FinancialSystemUserService;
030    import org.kuali.kfs.sys.service.UniversityDateService;
031    import org.kuali.rice.kns.service.BusinessObjectService;
032    import org.kuali.rice.kns.util.GlobalVariables;
033    
034    public class AccountsReceivableDocumentHeaderServiceImpl implements AccountsReceivableDocumentHeaderService {
035    
036        private BusinessObjectService businessObjectService;
037        private UniversityDateService universityDateService;
038        private SystemInformationService sysInfoService;
039    
040        /**
041         * @see org.kuali.kfs.module.ar.document.service.AccountsReceivableDocumentHeaderService#getNewAccountsReceivableDocumentHeader(java.lang.String,
042         *      java.lang.String)
043         */
044        public AccountsReceivableDocumentHeader getNewAccountsReceivableDocumentHeader(String chartOfAccountsCode, String organizationCode) {
045            AccountsReceivableDocumentHeader accountsReceivableDocumentHeader = new AccountsReceivableDocumentHeader();
046    
047            //  we try to get the processing org directly, which we'll get if the initiating user is an AR Processor
048            SystemInformation processingOrg = getProcessingOrgIfExists(chartOfAccountsCode, organizationCode);
049            if (processingOrg != null) {
050                accountsReceivableDocumentHeader.setProcessingChartOfAccountCode(processingOrg.getProcessingChartOfAccountCode());
051                accountsReceivableDocumentHeader.setProcessingOrganizationCode(processingOrg.getProcessingOrganizationCode());
052                return accountsReceivableDocumentHeader;
053            }
054            
055            //  next we try to get the processing org through the initiating user's billing org, if that exists
056            OrganizationOptions orgOptions = getOrgOptionsIfExists(chartOfAccountsCode, organizationCode);
057            if (orgOptions != null) {
058                accountsReceivableDocumentHeader.setProcessingChartOfAccountCode(orgOptions.getProcessingChartOfAccountCode());
059                accountsReceivableDocumentHeader.setProcessingOrganizationCode(orgOptions.getProcessingOrganizationCode());
060                return accountsReceivableDocumentHeader;
061            }
062            
063            //  If we get here, then we didnt find a matching BillingOrg or ProcessingOrg, so we have 
064            // no way to retrieve the document processing org.  This should never happen if the authorization 
065            // is working right, so we're going to puke and die right here.
066            throw new UnsupportedOperationException("");
067        }
068    
069        protected OrganizationOptions getOrgOptionsIfExists(String chartOfAccountsCode, String organizationCode) {
070            Map<String, String> criteria = new HashMap<String, String>();
071            criteria.put("chartOfAccountsCode", chartOfAccountsCode);
072            criteria.put("organizationCode", organizationCode);
073            return (OrganizationOptions) SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(OrganizationOptions.class, criteria);
074        }
075        
076        protected SystemInformation getProcessingOrgIfExists(String chartOfAccountsCode, String organizationCode) {
077            return sysInfoService.getByProcessingChartOrgAndFiscalYear(chartOfAccountsCode, organizationCode, universityDateService.getCurrentFiscalYear());
078        }
079        /**
080         * @see org.kuali.kfs.module.ar.document.service.AccountsReceivableDocumentHeaderService#getNewAccountsReceivableDocumentHeaderForCurrentUser()
081         */
082        public AccountsReceivableDocumentHeader getNewAccountsReceivableDocumentHeaderForCurrentUser() {
083            ChartOrgHolder currentUser = SpringContext.getBean(FinancialSystemUserService.class).getPrimaryOrganization(GlobalVariables.getUserSession().getPerson(), ArConstants.AR_NAMESPACE_CODE);
084            return getNewAccountsReceivableDocumentHeader(currentUser.getChartOfAccountsCode(), currentUser.getOrganizationCode());
085        }
086    
087        public BusinessObjectService getBusinessObjectService() {
088            return businessObjectService;
089        }
090    
091        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
092            this.businessObjectService = businessObjectService;
093        }
094    
095        public void setUniversityDateService(UniversityDateService universityDateService) {
096            this.universityDateService = universityDateService;
097        }
098    
099        public void setSysInfoService(SystemInformationService sysInfoService) {
100            this.sysInfoService = sysInfoService;
101        }
102    
103    }