Search in sources :

Example 1 with LaborLedgerExpenseTransferAccountingLine

use of org.kuali.kfs.integration.ld.LaborLedgerExpenseTransferAccountingLine in project cu-kfs by CU-CommunityApps.

the class CuContractsAndGrantsResponsibilityPlusPayPeriodRoleTypeServiceImpl method documentIsWithinPayPeriodLimit.

/*
     * Helper method for determining if the difference between the document's create date period and
     * the earliest account pay period is within the given limit. Assumed to be within limit unless
     * proven otherwise.
     */
private boolean documentIsWithinPayPeriodLimit(String documentNumber, int limit) {
    boolean withinLimit = true;
    AccountingDocument document;
    // Get the document, which is expected to be an accounting one.
    try {
        document = (AccountingDocument) SpringContext.getBean(DocumentService.class).getByDocumentHeaderId(documentNumber);
    } catch (WorkflowException e) {
        document = null;
    } catch (ClassCastException e) {
        document = null;
    }
    // Skip non-existent or non-retrievable documents, and have them default to within-limit routing.
    if (document == null) {
        return true;
    }
    // Make sure source/target lines containing the extra labor-ledger-related data actually exist on the document.
    boolean hasLLSourceLine = document.getSourceAccountingLineClass() != null && CollectionUtils.isNotEmpty(document.getSourceAccountingLines()) && LaborLedgerExpenseTransferAccountingLine.class.isAssignableFrom(document.getSourceAccountingLineClass());
    boolean hasLLTargetLine = document.getTargetAccountingLineClass() != null && CollectionUtils.isNotEmpty(document.getTargetAccountingLines()) && LaborLedgerExpenseTransferAccountingLine.class.isAssignableFrom(document.getTargetAccountingLineClass());
    if (hasLLSourceLine || hasLLTargetLine) {
        // Locate the earliest source/target line.
        LaborLedgerExpenseTransferAccountingLine earliestLine = null;
        if (hasLLSourceLine) {
            earliestLine = findEarliestPayPeriodAccountingLine(document.getSourceAccountingLines());
        }
        if (hasLLTargetLine) {
            if (earliestLine != null) {
                LaborLedgerExpenseTransferAccountingLine earliestTargetLine = findEarliestPayPeriodAccountingLine(document.getTargetAccountingLines());
                earliestLine = (earliestTargetLine == null) ? earliestLine : findEarliestPayPeriodAccountingLine(Arrays.asList(earliestLine, earliestTargetLine));
            } else {
                earliestLine = findEarliestPayPeriodAccountingLine(document.getTargetAccountingLines());
            }
        }
        // If an earliest line was found, then proceed with the within-limit determination.
        if (earliestLine != null) {
            // Prepare helper constants.
            final int NUM_MONTHS = 12;
            final int FY_OFFSET = 6;
            // Get the creation date, and calculate its corresponding fiscal year and pay period.
            DateTime dateCreated = document.getDocumentHeader().getWorkflowDocument().getDateCreated();
            int dateCreatedFiscalYear;
            int dateCreatedPayPeriod = dateCreated.getMonthOfYear() + FY_OFFSET;
            if (dateCreatedPayPeriod > NUM_MONTHS) {
                dateCreatedPayPeriod -= NUM_MONTHS;
            }
            dateCreatedFiscalYear = dateCreated.getYear() + ((dateCreatedPayPeriod <= FY_OFFSET) ? 1 : 0);
            // Determine difference between the pay period the doc was created in and the earliest impacted source/target account's pay period.
            int payPeriodDifference = ((dateCreatedFiscalYear - earliestLine.getPayrollEndDateFiscalYear().intValue()) * NUM_MONTHS) + dateCreatedPayPeriod - Integer.parseInt(earliestLine.getPayrollEndDateFiscalPeriodCode());
            // Set flag based on whether the difference is within the limit for standard Award node routing.
            withinLimit = payPeriodDifference <= limit;
        }
    }
    return withinLimit;
}
Also used : LaborLedgerExpenseTransferAccountingLine(org.kuali.kfs.integration.ld.LaborLedgerExpenseTransferAccountingLine) WorkflowException(org.kuali.kfs.kew.api.exception.WorkflowException) AccountingDocument(org.kuali.kfs.sys.document.AccountingDocument) DocumentService(org.kuali.kfs.krad.service.DocumentService) DateTime(org.joda.time.DateTime)

Aggregations

DateTime (org.joda.time.DateTime)1 LaborLedgerExpenseTransferAccountingLine (org.kuali.kfs.integration.ld.LaborLedgerExpenseTransferAccountingLine)1 WorkflowException (org.kuali.kfs.kew.api.exception.WorkflowException)1 DocumentService (org.kuali.kfs.krad.service.DocumentService)1 AccountingDocument (org.kuali.kfs.sys.document.AccountingDocument)1