Search in sources :

Example 1 with AssetDepreciationTransaction

use of org.kuali.kfs.module.cam.businessobject.AssetDepreciationTransaction in project cu-kfs by CU-CommunityApps.

the class CuAssetDepreciationServiceImpl method runDepreciation.

@Override
public void runDepreciation() {
    LOG.debug("runDepreciation() started");
    Integer fiscalYear = -1;
    Integer fiscalMonth = -1;
    String errorMsg = "";
    List<String> documentNos = new ArrayList<>();
    List<String[]> reportLog = new ArrayList<>();
    Collection<AssetObjectCode> assetObjectCodes = new ArrayList<>();
    boolean hasErrors = false;
    Calendar depreciationDate = dateTimeService.getCurrentCalendar();
    String depreciationDateParameter = null;
    DateFormat dateFormat = new SimpleDateFormat(CamsConstants.DateFormats.YEAR_MONTH_DAY, Locale.US);
    boolean executeJob = false;
    String errorMessage = kualiConfigurationService.getPropertyValueAsString(CamsKeyConstants.Depreciation.DEPRECIATION_ALREADY_RAN_MSG);
    try {
        executeJob = runAssetDepreciation();
        if (executeJob) {
            LOG.info("*******" + CamsConstants.Depreciation.DEPRECIATION_BATCH + " HAS BEGUN *******");
            if (parameterService.parameterExists(AssetDepreciationStep.class, CamsParameterConstants.DEPRECIATION_DATE_PARAMETER)) {
                depreciationDateParameter = parameterService.getParameterValueAsString(AssetDepreciationStep.class, CamsParameterConstants.DEPRECIATION_DATE_PARAMETER);
            }
            if (StringUtils.isBlank(depreciationDateParameter)) {
                depreciationDateParameter = dateFormat.format(dateTimeService.getCurrentDate());
            }
            // This validates the system parameter depreciation_date has a valid format of YYYY-MM-DD.
            if (!StringUtils.isBlank(depreciationDateParameter)) {
                try {
                    depreciationDate.setTime(dateFormat.parse(depreciationDateParameter.trim()));
                } catch (ParseException e) {
                    throw new IllegalArgumentException(kualiConfigurationService.getPropertyValueAsString(CamsKeyConstants.Depreciation.INVALID_DEPRECIATION_DATE_FORMAT));
                }
            }
            LOG.info(CamsConstants.Depreciation.DEPRECIATION_BATCH + "Depreciation run date: " + depreciationDateParameter);
            /**
             * CU Customization to use java.sql.Date
             */
            UniversityDate universityDate = businessObjectService.findBySinglePrimaryKey(UniversityDate.class, new java.sql.Date(depreciationDate.getTimeInMillis()));
            if (universityDate == null) {
                throw new IllegalStateException(kualiConfigurationService.getPropertyValueAsString(KFSKeyConstants.ERROR_UNIV_DATE_NOT_FOUND));
            }
            fiscalYear = universityDate.getUniversityFiscalYear();
            fiscalMonth = new Integer(universityDate.getUniversityFiscalAccountingPeriod());
            assetObjectCodes = getAssetObjectCodes(fiscalYear);
            // If the depreciation date is not = to the system date then, the depreciation process cannot run.
            LOG.info(CamsConstants.Depreciation.DEPRECIATION_BATCH + "Fiscal Year = " + fiscalYear + " & Fiscal Period=" + fiscalMonth);
            int fiscalStartMonth = Integer.parseInt(optionsService.getCurrentYearOptions().getUniversityFiscalYearStartMo());
            reportLog.addAll(depreciableAssetsDao.generateStatistics(true, null, fiscalYear, fiscalMonth, depreciationDate, dateTimeService.toDateString(depreciationDate.getTime()), assetObjectCodes, fiscalStartMonth, errorMessage));
            // update if fiscal period is 12
            // depreciationBatchDao.updateAssetsCreatedInLastFiscalPeriod(fiscalMonth, fiscalYear);
            updateAssetsDatesForLastFiscalPeriod(fiscalMonth, fiscalYear);
            // Retrieving eligible asset payment details
            LOG.info(CamsConstants.Depreciation.DEPRECIATION_BATCH + "Getting list of asset payments " + "eligible for depreciation.");
            Collection<AssetPaymentInfo> depreciableAssetsCollection = depreciationBatchDao.getListOfDepreciableAssetPaymentInfo(fiscalYear, fiscalMonth, depreciationDate);
            // transactions
            if (depreciableAssetsCollection != null && !depreciableAssetsCollection.isEmpty()) {
                SortedMap<String, AssetDepreciationTransaction> depreciationTransactions = this.calculateDepreciation(fiscalYear, fiscalMonth, depreciableAssetsCollection, depreciationDate, assetObjectCodes);
                processGeneralLedgerPendingEntry(fiscalYear, fiscalMonth, documentNos, depreciationTransactions);
            } else {
                throw new IllegalStateException(kualiConfigurationService.getPropertyValueAsString(CamsKeyConstants.Depreciation.NO_ELIGIBLE_FOR_DEPRECIATION_ASSETS_FOUND));
            }
        }
    } catch (Exception e) {
        LOG.error("Error occurred");
        LOG.error(CamsConstants.Depreciation.DEPRECIATION_BATCH + "**************************************************************************");
        LOG.error(CamsConstants.Depreciation.DEPRECIATION_BATCH + "AN ERROR HAS OCCURRED! - ERROR: " + e.getMessage(), e);
        LOG.error(CamsConstants.Depreciation.DEPRECIATION_BATCH + "**************************************************************************");
        hasErrors = true;
        errorMsg = "Depreciation process ran unsuccessfully.\nReason:" + e.getMessage();
    } finally {
        if (!hasErrors && executeJob) {
            int fiscalStartMonth = Integer.parseInt(optionsService.getCurrentYearOptions().getUniversityFiscalYearStartMo());
            reportLog.addAll(depreciableAssetsDao.generateStatistics(false, documentNos, fiscalYear, fiscalMonth, depreciationDate, dateTimeService.toDateString(depreciationDate.getTime()), assetObjectCodes, fiscalStartMonth, errorMessage));
        }
        // the report will be generated only when there is an error or when the log has something.
        if (!reportLog.isEmpty() || !errorMsg.trim().equals("")) {
            reportService.generateDepreciationReport(reportLog, errorMsg, depreciationDateParameter);
        }
        LOG.debug("*******" + CamsConstants.Depreciation.DEPRECIATION_BATCH + " HAS ENDED *******");
    }
}
Also used : UniversityDate(org.kuali.kfs.sys.businessobject.UniversityDate) AssetPaymentInfo(org.kuali.kfs.module.cam.batch.AssetPaymentInfo) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) AssetDepreciationTransaction(org.kuali.kfs.module.cam.businessobject.AssetDepreciationTransaction) AssetObjectCode(org.kuali.kfs.module.cam.businessobject.AssetObjectCode) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) AssetDepreciationStep(org.kuali.kfs.module.cam.batch.AssetDepreciationStep)

Aggregations

DateFormat (java.text.DateFormat)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 AssetDepreciationStep (org.kuali.kfs.module.cam.batch.AssetDepreciationStep)1 AssetPaymentInfo (org.kuali.kfs.module.cam.batch.AssetPaymentInfo)1 AssetDepreciationTransaction (org.kuali.kfs.module.cam.businessobject.AssetDepreciationTransaction)1 AssetObjectCode (org.kuali.kfs.module.cam.businessobject.AssetObjectCode)1 UniversityDate (org.kuali.kfs.sys.businessobject.UniversityDate)1