001package org.kuali.ole.module.purap.service.impl;
002
003import org.apache.commons.lang.StringUtils;
004import org.kuali.ole.coa.businessobject.*;
005import org.kuali.ole.module.purap.businessobject.InvoiceAccount;
006import org.kuali.ole.select.OleSelectConstant;
007import org.kuali.ole.select.businessobject.OlePurchaseOrderAccount;
008import org.kuali.ole.sys.OLEConstants;
009import org.kuali.ole.sys.OLEKeyConstants;
010import org.kuali.ole.sys.businessobject.AccountingLine;
011import org.kuali.ole.sys.businessobject.OriginationCode;
012import org.kuali.ole.sys.businessobject.SourceAccountingLine;
013import org.kuali.ole.sys.context.SpringContext;
014import org.kuali.rice.core.api.config.property.ConfigurationService;
015import org.kuali.rice.kew.doctype.bo.DocumentTypeEBO;
016import org.kuali.rice.krad.datadictionary.DataDictionary;
017import org.kuali.rice.krad.util.GlobalVariables;
018import org.kuali.rice.krad.util.ObjectUtils;
019
020/**
021 * Created with IntelliJ IDEA.
022 * User: chenchulakshmig
023 * Date: 11/25/13
024 * Time: 12:32 PM
025 * To change this template use File | Settings | File Templates.
026 */
027public class OLEPaymentRequestAccountingLineRuleHelperServiceImpl extends PaymentRequestAccountingLineRuleHelperServiceImpl {
028    @Override
029    public boolean validateAccountingLine(AccountingLine accountingLine) {
030        boolean isInvoice = false;
031        if (accountingLine instanceof InvoiceAccount || accountingLine instanceof OlePurchaseOrderAccount) {
032            isInvoice = true;
033        }
034        if (accountingLine == null) {
035            throw new IllegalStateException(SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEKeyConstants.ERROR_DOCUMENT_NULL_ACCOUNTING_LINE));
036        }
037        DataDictionary dd = getDataDictionaryService().getDataDictionary();
038        org.kuali.rice.krad.datadictionary.BusinessObjectEntry accountingLineEntry = dd.getBusinessObjectEntry(SourceAccountingLine.class.getName());
039        String accountIdentifyingPropertyName = getAccountIdentifyingPropertyName(accountingLine);
040        accountingLine.refreshReferenceObject(OLEConstants.CHART_PROPERTY_NAME);
041        Chart chart = accountingLine.getChart();
042        accountingLine.refreshReferenceObject(OLEConstants.ACCOUNT_PROPERTY_NAME);
043        String accountNumber = accountingLine.getAccountNumber();
044        Account account = accountingLine.getAccount();
045        accountingLine.refreshReferenceObject(OLEConstants.OrderQueue.ORDQ_OBJ_CODE);
046        ObjectCode objectCode = accountingLine.getObjectCode();
047        boolean valid = true;
048        valid &= isValidChart(accountIdentifyingPropertyName, chart, dd, isInvoice);
049        valid &= isValidAccount(accountIdentifyingPropertyName, account, accountNumber, dd, isInvoice);
050        if (StringUtils.isNotBlank(accountingLine.getSubAccountNumber())) {
051            accountingLine.refreshReferenceObject(OLEConstants.SUB_ACCOUNT_PROPERTY_NAME);
052            SubAccount subAccount = accountingLine.getSubAccount();
053            valid &= isValidSubAccount(accountIdentifyingPropertyName, subAccount, dd);
054        }
055        valid &= isValidObjectCode(accountIdentifyingPropertyName, objectCode, dd);
056        if (StringUtils.isNotBlank(accountingLine.getFinancialSubObjectCode())) {
057            accountingLine.refreshReferenceObject(OLEConstants.SUB_OBJECT_CODE_PROPERTY_NAME);
058            SubObjectCode subObjectCode = accountingLine.getSubObjectCode();
059            valid &= isValidSubObjectCode(accountIdentifyingPropertyName, subObjectCode, dd);
060        }
061        if (StringUtils.isNotBlank(accountingLine.getProjectCode())) {
062            accountingLine.refreshReferenceObject(OLEConstants.PROJECT_PROPERTY_NAME);
063            ProjectCode projectCode = accountingLine.getProject();
064            valid &= isValidProjectCode(accountIdentifyingPropertyName, projectCode, dd);
065        }
066        if (StringUtils.isNotBlank(accountingLine.getReferenceOriginCode())) {
067            accountingLine.refreshReferenceObject(OLEConstants.REF_ORIGIN_PROPERTY_NAME);
068            OriginationCode referenceOrigin = accountingLine.getReferenceOrigin();
069            valid &= isValidReferenceOriginCode(accountIdentifyingPropertyName, referenceOrigin, accountingLineEntry);
070        }
071        if (StringUtils.isNotBlank(accountingLine.getReferenceTypeCode())) {
072            DocumentTypeEBO referenceType = accountingLine.getReferenceFinancialSystemDocumentTypeCode();
073            valid &= isValidReferenceTypeCode(accountingLine.getReferenceTypeCode(), referenceType, accountingLineEntry, accountIdentifyingPropertyName);
074        }
075        valid &= hasRequiredOverrides(accountingLine, accountingLine.getOverrideCode());
076        return valid;
077    }
078
079    private boolean isValidAccount(String accountIdentifyingPropertyName, Account account, String accountNumber, DataDictionary dataDictionary, boolean isInvoice) {
080        return isValidAccount(account, accountNumber, dataDictionary, OLEConstants.ACCOUNT_NUMBER_PROPERTY_NAME, accountIdentifyingPropertyName, isInvoice);
081    }
082
083    private boolean isValidAccount(Account account, String accountNumber, DataDictionary dataDictionary, String errorPropertyName, String accountIdentifyingPropertyName, boolean isInvoice) {
084        String label = getAccountLabel();
085        if (ObjectUtils.isNull(account)) {
086            if (isInvoice) {
087                GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLEInvoiceView_ProcessItems_AccountingLines, OLEKeyConstants.ERROR_EXISTING_WITH_IDENTIFYING_ACCOUNTING_LINE, label, accountNumber);
088            } else {
089                GlobalVariables.getMessageMap().putError(errorPropertyName, OLEKeyConstants.ERROR_EXISTING_WITH_IDENTIFYING_ACCOUNTING_LINE, label, accountNumber);
090            }
091            return false;
092        }
093        if (!account.isActive()) {
094            if (isInvoice) {
095                GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLEInvoiceView_ProcessItems_AccountingLines, OLEKeyConstants.ERROR_DOCUMENT_ACCOUNT_CLOSED_WITH_IDENTIFYING_ACCOUNTING_LINE, label);
096            } else {
097                GlobalVariables.getMessageMap().putError(errorPropertyName, OLEKeyConstants.ERROR_DOCUMENT_ACCOUNT_CLOSED_WITH_IDENTIFYING_ACCOUNTING_LINE, label);
098            }
099            return false;
100        }
101        if (ObjectUtils.isNotNull(account.getAccountRestrictedStatusCode())) {
102            if (account.getAccountRestrictedStatusCode().equalsIgnoreCase(OleSelectConstant.ACCOUNT_TEMPORARY_RESTRICTED_CODE)) {
103                if (isInvoice) {
104                    GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLEInvoiceView_ProcessItems_AccountingLines, OLEKeyConstants.ERROR_DOCUMENT_ACCOUNT_IS_TEMPORARY_RESTRICTED, label);
105                } else {
106                    GlobalVariables.getMessageMap().putError(errorPropertyName, OLEKeyConstants.ERROR_DOCUMENT_ACCOUNT_IS_TEMPORARY_RESTRICTED, label);
107                }
108                return false;
109            } else if (account.getAccountRestrictedStatusCode().equalsIgnoreCase(OleSelectConstant.ACCOUNT_RESTRICTED_CODE)) {
110                if (isInvoice) {
111                    GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLEInvoiceView_ProcessItems_AccountingLines, OLEKeyConstants.ERROR_DOCUMENT_ACCOUNT_IS_RESTRICTED, accountNumber);
112                } else {
113                    GlobalVariables.getMessageMap().putError(errorPropertyName, OLEKeyConstants.ERROR_DOCUMENT_ACCOUNT_IS_RESTRICTED, accountNumber);
114                }
115                return false;
116            }
117        }
118        return true;
119    }
120
121    public boolean isValidChart(String accountIdentifyingPropertyName, Chart chart, DataDictionary dataDictionary, boolean isInvoice) {
122        return isValidChart(chart, dataDictionary, OLEConstants.CHART_OF_ACCOUNTS_CODE_PROPERTY_NAME, accountIdentifyingPropertyName, isInvoice);
123    }
124
125    public boolean isValidChart(Chart chart, DataDictionary dataDictionary, String errorPropertyName, String accountIdentifyingPropertyName, boolean isInvoice) {
126        String label = getChartLabel();
127        if (ObjectUtils.isNull(chart)) {
128            if (isInvoice) {
129                GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLEInvoiceView_ProcessItems_AccountingLines, OLEKeyConstants.ERROR_EXISTING_WITH_IDENTIFYING_ACCOUNTING_LINE, label);
130            } else {
131                GlobalVariables.getMessageMap().putError(errorPropertyName, OLEKeyConstants.ERROR_EXISTING_WITH_IDENTIFYING_ACCOUNTING_LINE, label);
132            }
133            return false;
134        }
135        if (!chart.isActive()) {
136            if (isInvoice) {
137                GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OLEInvoiceView_ProcessItems_AccountingLines, OLEKeyConstants.ERROR_INACTIVE_WITH_IDENTIFYING_ACCOUNTING_LINE, label);
138            } else {
139                GlobalVariables.getMessageMap().putError(errorPropertyName, OLEKeyConstants.ERROR_INACTIVE_WITH_IDENTIFYING_ACCOUNTING_LINE, label);
140            }
141            return false;
142        }
143        return true;
144    }
145
146}