001/*
002 * Copyright 2007 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 */
016package org.kuali.ole.sys.businessobject.options;
017
018import java.sql.Date;
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.commons.lang.StringUtils;
023import org.kuali.ole.coa.businessobject.AccountingPeriod;
024import org.kuali.ole.coa.service.AccountingPeriodService;
025import org.kuali.ole.sys.OLEConstants;
026import org.kuali.ole.sys.context.SpringContext;
027import org.kuali.ole.sys.service.impl.OleParameterConstants;
028import org.kuali.rice.core.api.datetime.DateTimeService;
029import org.kuali.rice.core.api.util.ConcreteKeyValue;
030import org.kuali.rice.coreservice.framework.parameter.ParameterService;
031import org.kuali.rice.krad.keyvalues.KeyValuesBase;
032
033/**
034 * returns list of accounting period value pairs valid for year end posting
035 */
036public class YearEndAccountingPeriodValuesFinder extends KeyValuesBase {
037
038    /**
039     * @see org.kuali.rice.kns.lookup.keyvalues.KeyValuesFinder#getKeyValues()
040     */
041    @Override
042    public List getKeyValues() {
043        List<ConcreteKeyValue> accountingPeriodCodes = new ArrayList<ConcreteKeyValue>();
044
045        Date date = SpringContext.getBean(DateTimeService.class).getCurrentSqlDate();
046        AccountingPeriod currentAccountingPeriod = SpringContext.getBean(AccountingPeriodService.class).getByDate(date);
047
048        if (currentAccountingPeriod.isOpen()) {
049            // add the current period with blank value, so scrubber will set entries when posting
050            accountingPeriodCodes.add(new ConcreteKeyValue("", currentAccountingPeriod.getUniversityFiscalPeriodName()));
051        }
052
053        String numberOfPostbackPeriodsParmVal = SpringContext.getBean(ParameterService.class).getParameterValueAsString(OLEConstants.CoreModuleNamespaces.OLE, OleParameterConstants.YEAR_END_ACCOUNTING_PERIOD_PARAMETER_NAMES.DETAIL_PARAMETER_TYPE, OleParameterConstants.YEAR_END_ACCOUNTING_PERIOD_PARAMETER_NAMES.NUMBER_OF_POSTBACK_PERIODS);
054        if (StringUtils.isNotBlank(numberOfPostbackPeriodsParmVal) && Integer.valueOf(numberOfPostbackPeriodsParmVal) > 0) {
055            for (int i = 1; i <= Integer.valueOf(numberOfPostbackPeriodsParmVal); i++) {
056                int currentFiscalYear = currentAccountingPeriod.getUniversityFiscalYear();
057                int currentFiscalPeriod = Integer.valueOf(currentAccountingPeriod.getUniversityFiscalPeriodCode());
058
059                if (currentFiscalPeriod == 1) {
060                    currentFiscalYear = currentFiscalYear - 1;
061                    currentFiscalPeriod = 13;
062                } else {
063                    currentFiscalPeriod = currentFiscalPeriod - 1;
064                }
065
066                currentAccountingPeriod = SpringContext.getBean(AccountingPeriodService.class).getByPeriod(StringUtils.leftPad(Integer.toString(currentFiscalPeriod), 2, "0"), currentFiscalYear);
067                if (currentAccountingPeriod.isOpen()) {
068                    accountingPeriodCodes.add(new ConcreteKeyValue(currentAccountingPeriod.getUniversityFiscalPeriodCode() + currentAccountingPeriod.getUniversityFiscalYear(), currentAccountingPeriod.getUniversityFiscalPeriodName()));
069                }
070            }
071        }
072
073        return accountingPeriodCodes;
074    }
075}