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_CREDIT_CODE;
019:
020: import org.apache.commons.lang.StringUtils;
021: import org.kuali.core.bo.user.UniversalUser;
022: import org.kuali.core.document.Document;
023: import org.kuali.core.exceptions.UserNotFoundException;
024: import org.kuali.core.exceptions.ValidationException;
025: import org.kuali.core.rule.event.ApproveDocumentEvent;
026: import org.kuali.core.service.UniversalUserService;
027: import org.kuali.core.util.GlobalVariables;
028: import org.kuali.core.util.ObjectUtils;
029: import org.kuali.kfs.KFSConstants;
030: import org.kuali.kfs.bo.AccountingLine;
031: import org.kuali.kfs.bo.GeneralLedgerPendingEntry;
032: import org.kuali.kfs.context.SpringContext;
033: import org.kuali.kfs.document.AccountingDocument;
034: import org.kuali.kfs.service.ParameterService;
035: import org.kuali.kfs.service.impl.ParameterConstants;
036: import org.kuali.module.purap.PurapKeyConstants;
037: import org.kuali.module.purap.PurapParameterConstants;
038: import org.kuali.module.purap.PurapPropertyConstants;
039: import org.kuali.module.purap.PurapConstants.PurapDocTypeCodes;
040: import org.kuali.module.purap.PurapConstants.PurchaseOrderStatuses;
041: import org.kuali.module.purap.document.PurchaseOrderDocument;
042: import org.kuali.module.purap.service.PurapGeneralLedgerService;
043: import org.kuali.module.purap.service.PurchaseOrderService;
044:
045: /**
046: * Rules for Purchase Order Void document creation.
047: */
048: public class PurchaseOrderVoidDocumentRule extends
049: PurchasingDocumentRuleBase {
050:
051: /**
052: * @see org.kuali.module.financial.rules.TransactionalDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.core.document.Document)
053: */
054: @Override
055: protected boolean processCustomRouteDocumentBusinessRules(
056: Document document) {
057: boolean isValid = true;
058: PurchaseOrderDocument porDocument = (PurchaseOrderDocument) document;
059: return isValid &= processValidation(porDocument);
060: }
061:
062: /**
063: * @see org.kuali.core.rules.DocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.core.document.Document)
064: */
065: @Override
066: protected boolean processCustomSaveDocumentBusinessRules(
067: Document document) {
068: boolean isValid = true;
069: PurchaseOrderDocument porDocument = (PurchaseOrderDocument) document;
070: return isValid &= processValidation(porDocument);
071: }
072:
073: /**
074: * @see org.kuali.core.rules.DocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.core.rule.event.ApproveDocumentEvent)
075: */
076: @Override
077: protected boolean processCustomApproveDocumentBusinessRules(
078: ApproveDocumentEvent approveEvent) {
079: boolean isValid = true;
080: PurchaseOrderDocument porDocument = (PurchaseOrderDocument) approveEvent
081: .getDocument();
082: return isValid;
083: }
084:
085: /**
086: * Central method to control the processing of rule checks. Checks that the purchase order document is not null, that it is in
087: * the correct status, and that the user is in the purchasing workgroup.
088: *
089: * @param document A PurchaseOrderDocument.
090: * @return True if the document passes all the validations.
091: */
092: public boolean processValidation(PurchaseOrderDocument document) {
093: boolean valid = true;
094:
095: // Check that the PO is not null
096: if (ObjectUtils.isNull(document)) {
097: throw new ValidationException(
098: "Purchase Order Void document was null on validation.");
099: } else {
100: PurchaseOrderDocument currentPO = SpringContext.getBean(
101: PurchaseOrderService.class)
102: .getCurrentPurchaseOrder(
103: document.getPurapDocumentIdentifier());
104: // The PO must be in OPEN status.
105: if (!StringUtils.equalsIgnoreCase(
106: currentPO.getStatusCode(),
107: PurchaseOrderStatuses.OPEN)
108: && !StringUtils.equalsIgnoreCase(currentPO
109: .getStatusCode(),
110: PurchaseOrderStatuses.PENDING_VOID)
111: && !StringUtils.equalsIgnoreCase(currentPO
112: .getStatusCode(),
113: PurchaseOrderStatuses.PENDING_PRINT)) {
114: valid = false;
115: GlobalVariables
116: .getErrorMap()
117: .putError(
118: PurapPropertyConstants.STATUS_CODE,
119: PurapKeyConstants.ERROR_PURCHASE_ORDER_STATUS_NOT_REQUIRED_STATUS,
120: PurchaseOrderStatuses.OPEN);
121: }
122:
123: // Check that the user is in purchasing workgroup.
124: String initiatorNetworkId = document.getDocumentHeader()
125: .getWorkflowDocument().getInitiatorNetworkId();
126: UniversalUserService uus = SpringContext
127: .getBean(UniversalUserService.class);
128: UniversalUser user = null;
129: try {
130: user = uus
131: .getUniversalUserByAuthenticationUserId(initiatorNetworkId);
132: String purchasingGroup = SpringContext
133: .getBean(ParameterService.class)
134: .getParameterValue(
135: ParameterConstants.PURCHASING_DOCUMENT.class,
136: PurapParameterConstants.Workgroups.WORKGROUP_PURCHASING);
137: if (!uus.isMember(user, purchasingGroup)) {
138: valid = false;
139: }
140: } catch (UserNotFoundException ue) {
141: valid = false;
142: }
143: }
144: return valid;
145: }
146:
147: /**
148: * @see org.kuali.module.purap.rules.PurapAccountingDocumentRuleBase#customizeExplicitGeneralLedgerPendingEntry(org.kuali.kfs.document.AccountingDocument,
149: * org.kuali.kfs.bo.AccountingLine, org.kuali.kfs.bo.GeneralLedgerPendingEntry)
150: */
151: @Override
152: protected void customizeExplicitGeneralLedgerPendingEntry(
153: AccountingDocument accountingDocument,
154: AccountingLine accountingLine,
155: GeneralLedgerPendingEntry explicitEntry) {
156: super .customizeExplicitGeneralLedgerPendingEntry(
157: accountingDocument, accountingLine, explicitEntry);
158: PurchaseOrderDocument po = (PurchaseOrderDocument) accountingDocument;
159:
160: SpringContext.getBean(PurapGeneralLedgerService.class)
161: .customizeGeneralLedgerPendingEntry(po, accountingLine,
162: explicitEntry, po.getPurapDocumentIdentifier(),
163: GL_CREDIT_CODE, PurapDocTypeCodes.PO_DOCUMENT,
164: true);
165:
166: // don't think i should have to override this, but default isn't getting the right PO doc
167: explicitEntry
168: .setFinancialDocumentTypeCode(PurapDocTypeCodes.PO_VOID_DOCUMENT);
169: explicitEntry
170: .setFinancialDocumentApprovedCode(KFSConstants.PENDING_ENTRY_APPROVED_STATUS_CODE.APPROVED);
171: }
172:
173: }
|