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.ld.service.impl;
017    
018    import java.util.Collection;
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import org.kuali.kfs.integration.ld.LaborLedgerObject;
023    import org.kuali.kfs.module.ld.LaborConstants;
024    import org.kuali.kfs.module.ld.LaborPropertyConstants;
025    import org.kuali.kfs.module.ld.businessobject.BenefitsCalculation;
026    import org.kuali.kfs.module.ld.businessobject.LaborObject;
027    import org.kuali.kfs.module.ld.businessobject.PositionObjectBenefit;
028    import org.kuali.kfs.module.ld.service.LaborBenefitsCalculationService;
029    import org.kuali.kfs.module.ld.service.LaborPositionObjectBenefitService;
030    import org.kuali.kfs.sys.KFSConstants;
031    import org.kuali.kfs.sys.KFSPropertyConstants;
032    import org.kuali.rice.kns.service.BusinessObjectService;
033    import org.kuali.rice.kns.util.KualiDecimal;
034    import org.kuali.rice.kns.util.ObjectUtils;
035    import org.springframework.transaction.annotation.Transactional;
036    
037    /**
038     * To provide its clients with access to the benefit calculation.
039     */
040    @Transactional
041    public class LaborBenefitsCalculationServiceImpl implements LaborBenefitsCalculationService {
042        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LaborBenefitsCalculationServiceImpl.class);
043    
044        private BusinessObjectService businessObjectService;
045        private LaborPositionObjectBenefitService laborPositionObjectBenefitService;
046    
047        /**
048         * @see org.kuali.kfs.module.ld.service.LaborBenefitsCalculationService#getBenefitsCalculation(java.lang.Integer,
049         *      java.lang.String, java.lang.String)
050         */
051        public BenefitsCalculation getBenefitsCalculation(Integer universityFiscalYear, String chartOfAccountsCode, String benefitTypeCode) {
052            Map<String, Object> fieldValues = new HashMap<String, Object>();
053            fieldValues.put(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, universityFiscalYear);
054            fieldValues.put(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
055            fieldValues.put(LaborPropertyConstants.POSITION_BENEFIT_TYPE_CODE, benefitTypeCode);
056    
057            return (BenefitsCalculation) businessObjectService.findByPrimaryKey(BenefitsCalculation.class, fieldValues);
058        }
059    
060        /**
061         * @see org.kuali.kfs.module.ld.service.LaborBenefitsCalculationService#calculateFringeBenefit(java.lang.Integer,
062         *      java.lang.String, java.lang.String, org.kuali.rice.kns.util.KualiDecimal)
063         */
064        public KualiDecimal calculateFringeBenefit(Integer fiscalYear, String chartCode, String objectCode, KualiDecimal salaryAmount) {
065            LaborObject laborObject = new LaborObject();
066            
067            laborObject.setUniversityFiscalYear(fiscalYear);
068            laborObject.setChartOfAccountsCode(chartCode);
069            laborObject.setFinancialObjectCode(objectCode);
070    
071            laborObject = (LaborObject) businessObjectService.retrieve(laborObject);
072    
073            return calculateFringeBenefit(laborObject, salaryAmount);
074        }
075    
076        /**
077         * @see org.kuali.kfs.module.ld.service.LaborBenefitsCalculationService#calculateFringeBenefit(org.kuali.kfs.module.ld.businessobject.LaborObject,
078         *      org.kuali.rice.kns.util.KualiDecimal)
079         */
080        public KualiDecimal calculateFringeBenefit(LaborLedgerObject laborLedgerObject, KualiDecimal salaryAmount) {
081            KualiDecimal fringeBenefit = KualiDecimal.ZERO;
082    
083            if (salaryAmount == null || salaryAmount.isZero() || ObjectUtils.isNull(laborLedgerObject)) {
084                return fringeBenefit;
085            }
086    
087            String FringeOrSalaryCode = laborLedgerObject.getFinancialObjectFringeOrSalaryCode();
088            if (!LaborConstants.SalaryExpenseTransfer.LABOR_LEDGER_SALARY_CODE.equals(FringeOrSalaryCode)) {
089                return fringeBenefit;
090            }
091    
092            Integer fiscalYear = laborLedgerObject.getUniversityFiscalYear();
093            String chartOfAccountsCode = laborLedgerObject.getChartOfAccountsCode();
094            String objectCode = laborLedgerObject.getFinancialObjectCode();
095    
096            Collection<PositionObjectBenefit> positionObjectBenefits = laborPositionObjectBenefitService.getPositionObjectBenefits(fiscalYear, chartOfAccountsCode, objectCode);
097            for (PositionObjectBenefit positionObjectBenefit : positionObjectBenefits) {
098                KualiDecimal benefitAmount = this.calculateFringeBenefit(positionObjectBenefit, salaryAmount);
099                fringeBenefit = fringeBenefit.add(benefitAmount);
100            }
101    
102            return fringeBenefit;
103        }
104    
105        /**
106         * @see org.kuali.kfs.module.ld.service.LaborBenefitsCalculationService#calculateFringeBenefit(org.kuali.kfs.module.ld.businessobject.PositionObjectBenefit,
107         *      org.kuali.rice.kns.util.KualiDecimal)
108         */
109        public KualiDecimal calculateFringeBenefit(PositionObjectBenefit positionObjectBenefit, KualiDecimal salaryAmount) {
110            if (salaryAmount == null || salaryAmount.isZero() || ObjectUtils.isNull(positionObjectBenefit)) {
111                return KualiDecimal.ZERO;
112            }
113    
114            // calculate the benefit amount (ledger amt * (benfit pct/100) )
115            KualiDecimal fringeBenefitPercent = positionObjectBenefit.getBenefitsCalculation().getPositionFringeBenefitPercent();
116            return fringeBenefitPercent.multiply(salaryAmount).divide(KFSConstants.ONE_HUNDRED.kualiDecimalValue());
117        }
118    
119        /**
120         * Sets the laborPositionObjectBenefitService attribute value.
121         * 
122         * @param laborPositionObjectBenefitService The laborPositionObjectBenefitService to set.
123         */
124        public void setLaborPositionObjectBenefitService(LaborPositionObjectBenefitService laborPositionObjectBenefitService) {
125            this.laborPositionObjectBenefitService = laborPositionObjectBenefitService;
126        }
127    
128        /**
129         * Sets the businessObjectService attribute value.
130         * 
131         * @param businessObjectService The businessObjectService to set.
132         */
133        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
134            this.businessObjectService = businessObjectService;
135        }
136    }