001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.purap.rules;
017:
018: import static org.kuali.kfs.KFSConstants.GL_DEBIT_CODE;
019:
020: import java.util.List;
021:
022: import org.kuali.core.bo.user.UniversalUser;
023: import org.kuali.core.exceptions.UserNotFoundException;
024: import org.kuali.core.service.DataDictionaryService;
025: import org.kuali.core.service.UniversalUserService;
026: import org.kuali.core.util.GlobalVariables;
027: import org.kuali.kfs.KFSKeyConstants;
028: import org.kuali.kfs.bo.AccountingLine;
029: import org.kuali.kfs.bo.GeneralLedgerPendingEntry;
030: import org.kuali.kfs.context.SpringContext;
031: import org.kuali.kfs.document.AccountingDocument;
032: import org.kuali.kfs.service.ParameterService;
033: import org.kuali.kfs.service.impl.ParameterConstants;
034: import org.kuali.module.purap.PurapConstants;
035: import org.kuali.module.purap.PurapKeyConstants;
036: import org.kuali.module.purap.PurapParameterConstants;
037: import org.kuali.module.purap.PurapPropertyConstants;
038: import org.kuali.module.purap.PurapConstants.PurapDocTypeCodes;
039: import org.kuali.module.purap.bo.PurApItem;
040: import org.kuali.module.purap.bo.PurchaseOrderItem;
041: import org.kuali.module.purap.document.PurchaseOrderDocument;
042: import org.kuali.module.purap.document.PurchasingAccountsPayableDocument;
043: import org.kuali.module.purap.service.PurapGeneralLedgerService;
044:
045: /**
046: * Rules for Purchase Order Amendment documents creation.
047: */
048: public class PurchaseOrderAmendmentDocumentRule extends
049: PurchaseOrderDocumentRule {
050:
051: /**
052: * @see org.kuali.module.purap.rules.PurchasingAccountsPayableDocumentRuleBase#processValidation(org.kuali.module.purap.document.PurchasingAccountsPayableDocument)
053: */
054: @Override
055: public boolean processValidation(
056: PurchasingAccountsPayableDocument purapDocument) {
057: boolean valid = super .processValidation(purapDocument);
058: // Check that the user is in purchasing workgroup.
059: String initiatorNetworkId = purapDocument.getDocumentHeader()
060: .getWorkflowDocument().getInitiatorNetworkId();
061: UniversalUserService uus = SpringContext
062: .getBean(UniversalUserService.class);
063: UniversalUser user = null;
064: try {
065: user = uus
066: .getUniversalUserByAuthenticationUserId(initiatorNetworkId);
067: String purchasingGroup = SpringContext
068: .getBean(ParameterService.class)
069: .getParameterValue(
070: ParameterConstants.PURCHASING_DOCUMENT.class,
071: PurapParameterConstants.Workgroups.WORKGROUP_PURCHASING);
072: if (!uus.isMember(user, purchasingGroup)) {
073: valid = false;
074: GlobalVariables
075: .getErrorMap()
076: .putError(
077: PurapPropertyConstants.PURAP_DOC_ID,
078: KFSKeyConstants.AUTHORIZATION_ERROR_DOCUMENT,
079: initiatorNetworkId,
080: "amend",
081: PurapConstants.PurchaseOrderDocTypes.PURCHASE_ORDER_DOCUMENT);
082: }
083: } catch (UserNotFoundException ue) {
084: valid = false;
085: }
086: valid &= validateContainsAtLeastOneActiveItem(purapDocument);
087: return valid;
088: }
089:
090: /**
091: * Validates that the given document contains at least one active item.
092: *
093: * @param purapDocument A PurchasingAccountsPayableDocument. (Should contain PurchaseOrderItems.)
094: * @return True if the document contains at least one active item
095: */
096: private boolean validateContainsAtLeastOneActiveItem(
097: PurchasingAccountsPayableDocument purapDocument) {
098: List<PurApItem> items = purapDocument.getItems();
099: for (PurApItem item : items) {
100: if (((PurchaseOrderItem) item).isItemActiveIndicator()
101: && (!((PurchaseOrderItem) item).isEmpty() && item
102: .getItemType()
103: .isItemTypeAboveTheLineIndicator())) {
104: return true;
105: }
106: }
107: String documentType = SpringContext.getBean(
108: DataDictionaryService.class).getDataDictionary()
109: .getDocumentEntry(
110: purapDocument.getDocumentHeader()
111: .getWorkflowDocument()
112: .getDocumentType()).getLabel();
113:
114: GlobalVariables.getErrorMap().putError(
115: PurapConstants.ITEM_TAB_ERROR_PROPERTY,
116: PurapKeyConstants.ERROR_ITEM_REQUIRED, documentType);
117: return false;
118: }
119:
120: /**
121: * @see org.kuali.module.purap.rules.PurapAccountingDocumentRuleBase#customizeExplicitGeneralLedgerPendingEntry(org.kuali.kfs.document.AccountingDocument,
122: * org.kuali.kfs.bo.AccountingLine, org.kuali.kfs.bo.GeneralLedgerPendingEntry)
123: */
124: @Override
125: protected void customizeExplicitGeneralLedgerPendingEntry(
126: AccountingDocument accountingDocument,
127: AccountingLine accountingLine,
128: GeneralLedgerPendingEntry explicitEntry) {
129: super .customizeExplicitGeneralLedgerPendingEntry(
130: accountingDocument, accountingLine, explicitEntry);
131: PurchaseOrderDocument po = (PurchaseOrderDocument) accountingDocument;
132:
133: SpringContext.getBean(PurapGeneralLedgerService.class)
134: .customizeGeneralLedgerPendingEntry(po, accountingLine,
135: explicitEntry, po.getPurapDocumentIdentifier(),
136: GL_DEBIT_CODE, PurapDocTypeCodes.PO_DOCUMENT,
137: true);
138:
139: // don't think i should have to override this, but default isn't getting the right PO doc
140: explicitEntry
141: .setFinancialDocumentTypeCode(PurapDocTypeCodes.PO_AMENDMENT_DOCUMENT);
142: }
143:
144: }
|