View Javadoc

1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.fp.document.validation.impl;
17  
18  import org.kuali.ole.fp.businessobject.CashDrawer;
19  import org.kuali.ole.fp.businessobject.CoinDetail;
20  import org.kuali.ole.fp.businessobject.CurrencyDetail;
21  import org.kuali.ole.fp.document.CashReceiptDocument;
22  import org.kuali.ole.fp.document.CashReceiptFamilyBase;
23  import org.kuali.ole.fp.document.service.CashReceiptService;
24  import org.kuali.ole.fp.service.CashDrawerService;
25  import org.kuali.ole.sys.OLEConstants;
26  import org.kuali.ole.sys.OLEKeyConstants;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.ole.sys.document.validation.GenericValidation;
29  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
30  import org.kuali.ole.sys.service.FinancialSystemWorkflowHelperService;
31  import org.kuali.rice.kew.api.WorkflowDocument;
32  import org.kuali.rice.krad.util.GlobalVariables;
33  
34  /**
35   * Validation for the cash receipt document that verifies that the cash drawer is open at approval.
36   */
37  public class CashReceiptCashDrawerOpenValidation extends GenericValidation {
38      private CashReceiptFamilyBase cashReceiptDocumentForValidation;
39      private CashReceiptService cashReceiptService;
40      private CashDrawerService cashDrawerService;
41  
42      /**
43       * Makes sure that the cash drawer for the verification unit associated with this CR doc is
44       * open. If it's not, the the rule fails.
45       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
46       */
47      @Override
48      public boolean validate(AttributedDocumentEvent event) {
49  
50          CashDrawer cd = getCashDrawerService().getByCampusCode(getCashReceiptDocumentForValidation().getCampusLocationCode());
51          if (cd == null) {
52              throw new IllegalStateException("There is no cash drawer associated with unitName '" + getCashReceiptDocumentForValidation().getCampusLocationCode() + "' from cash receipt " + getCashReceiptDocumentForValidation().getDocumentNumber());
53          }
54          WorkflowDocument workflowDocument = getCashReceiptDocumentForValidation().getDocumentHeader().getWorkflowDocument();
55          boolean isAdhocApproval = SpringContext.getBean(FinancialSystemWorkflowHelperService.class).isAdhocApprovalRequestedForPrincipal(workflowDocument, GlobalVariables.getUserSession().getPrincipalId());
56  
57          if (cd.isClosed() && !isAdhocApproval) {
58              GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.CashReceipt.MSG_CASH_DRAWER_CLOSED_VERIFICATION_NOT_ALLOWED, cd.getCampusCode());
59              return false;
60          }
61  
62          //check whether cash manager confirmed amount equals to the old amount
63          CashReceiptDocument crDoc = (CashReceiptDocument)cashReceiptDocumentForValidation; 
64          //check whether the change request is valid
65          if (crDoc.getChangeCurrencyDetail() != null && crDoc.getChangeCoinDetail() != null) {
66              return checkChangeRequestIsValid();
67          }
68          return true;
69      }
70      
71      /**
72       * 
73       * This method checks whether the change request is valid by comparing the currency/coin counts in the request with the 
74       * counts in the cash drawer + currency/coin counts sent with the cash receipt itself. 
75       * @return Returns true if the request is valid. 
76       */
77      public boolean checkChangeRequestIsValid() {
78          CashDrawer cd = getCashDrawerService().getByCampusCode(getCashReceiptDocumentForValidation().getCampusLocationCode());
79          CurrencyDetail changeCurrency = ((CashReceiptDocument) getCashReceiptDocumentForValidation()).getChangeCurrencyDetail();
80          CoinDetail changeCoin = ((CashReceiptDocument) getCashReceiptDocumentForValidation()).getChangeCoinDetail();
81          CurrencyDetail confirmedCurrency = ((CashReceiptDocument) getCashReceiptDocumentForValidation()).getConfirmedCurrencyDetail();
82          CoinDetail confirmedCoin = ((CashReceiptDocument) getCashReceiptDocumentForValidation()).getConfirmedCoinDetail();
83          
84          if(changeCurrency.getHundredDollarCount() > cd.getHundredDollarCount() + confirmedCurrency.getHundredDollarCount() ||
85                  changeCurrency.getFiftyDollarCount() > cd.getFiftyDollarCount() + confirmedCurrency.getFiftyDollarCount() ||
86                  changeCurrency.getTwentyDollarCount() > cd.getTwentyDollarCount() + confirmedCurrency.getTwentyDollarCount() ||
87                  changeCurrency.getTenDollarCount() > cd.getTenDollarCount() + confirmedCurrency.getTenDollarCount() ||
88                  changeCurrency.getFiveDollarCount() > cd.getFiveDollarCount() + confirmedCurrency.getFiveDollarCount() ||
89                  changeCurrency.getTwoDollarCount() > cd.getTwoDollarCount() + confirmedCurrency.getTwoDollarCount() ||
90                  changeCurrency.getOneDollarCount() > cd.getOneDollarCount() + confirmedCurrency.getOneDollarCount() ||
91                  changeCoin.getHundredCentCount() > cd.getHundredCentCount() + confirmedCoin.getHundredCentCount() ||
92                  changeCoin.getFiftyCentCount() > cd.getFiftyCentCount() + confirmedCoin.getFiftyCentCount() ||
93                  changeCoin.getTwentyFiveCentCount() > cd.getTwentyFiveCentCount() + confirmedCoin.getTwentyFiveCentCount() ||
94                  changeCoin.getTenCentCount() > cd.getTenCentCount() + confirmedCoin.getTenCentCount() ||
95                  changeCoin.getFiveCentCount() > cd.getFiveCentCount() + confirmedCoin.getFiveCentCount() ||
96                  changeCoin.getOneCentCount() > cd.getOneCentCount() + confirmedCoin.getOneCentCount()) {
97                   
98              GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.CashReceipt.ERROR_CHANGE_REQUEST, "");
99              return false;
100         }
101         
102         return true;
103     }
104 
105     /**
106      * Gets the cashReceiptDocumentForValidation attribute. 
107      * @return Returns the cashReceiptDocumentForValidation.
108      */
109     public CashReceiptFamilyBase getCashReceiptDocumentForValidation() {
110         return cashReceiptDocumentForValidation;
111     }
112 
113     /**
114      * Sets the cashReceiptDocumentForValidation attribute value.
115      * @param cashReceiptDocumentForValidation The cashReceiptDocumentForValidation to set.
116      */
117     public void setCashReceiptDocumentForValidation(CashReceiptFamilyBase cashReceiptFamilyDocumentForValidation) {
118         this.cashReceiptDocumentForValidation = cashReceiptFamilyDocumentForValidation;
119     }
120 
121     /**
122      * Gets the cashDrawerService attribute. 
123      * @return Returns the cashDrawerService.
124      */
125     public CashDrawerService getCashDrawerService() {
126         return cashDrawerService;
127     }
128 
129     /**
130      * Sets the cashDrawerService attribute value.
131      * @param cashDrawerService The cashDrawerService to set.
132      */
133     public void setCashDrawerService(CashDrawerService cashDrawerService) {
134         this.cashDrawerService = cashDrawerService;
135     }
136 
137     /**
138      * Gets the cashReceiptService attribute. 
139      * @return Returns the cashReceiptService.
140      */
141     public CashReceiptService getCashReceiptService() {
142         return cashReceiptService;
143     }
144 
145     /**
146      * Sets the cashReceiptService attribute value.
147      * @param cashReceiptService The cashReceiptService to set.
148      */
149     public void setCashReceiptService(CashReceiptService cashReceiptService) {
150         this.cashReceiptService = cashReceiptService;
151     }
152 }