001/*
002 * Copyright 2009 The Kuali Foundation
003 * 
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 */
016package org.kuali.ole.fp.document.validation.impl;
017
018import org.kuali.ole.fp.businessobject.CashDrawer;
019import org.kuali.ole.fp.service.CashDrawerService;
020import org.kuali.ole.sys.OLEKeyConstants;
021import org.kuali.ole.sys.context.SpringContext;
022import org.kuali.rice.core.api.util.type.KualiDecimal;
023import org.kuali.rice.kns.document.MaintenanceDocument;
024import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
025
026/**
027 * Validations for the Cash Drawer Maintenance Document
028 */
029public class CashDrawerMaintenanceDocumentRule extends MaintenanceDocumentRuleBase {
030
031    /**
032     * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
033     */
034    @Override
035    protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
036        boolean documentValid = super.processCustomRouteDocumentBusinessRules(document);
037        final CashDrawer cashDrawer = (CashDrawer)document.getNewMaintainableObject().getBusinessObject();
038        documentValid &= checkCashDrawerStillClosed(cashDrawer);
039        documentValid &= checkCurrencyAmountsPositive(cashDrawer);
040        documentValid &= checkCoinAmountsPositive(cashDrawer);
041        return documentValid;
042    }
043
044    /**
045     * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
046     */
047    @Override
048    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
049        boolean documentValid = super.processCustomSaveDocumentBusinessRules(document);
050        final CashDrawer cashDrawer = (CashDrawer)document.getNewMaintainableObject().getBusinessObject();
051        documentValid &= checkCashDrawerStillClosed(cashDrawer);
052        if (documentValid) {
053            checkCurrencyAmountsPositive(cashDrawer);
054            checkCoinAmountsPositive(cashDrawer);
055        }
056        return documentValid;
057    }
058
059    /**
060     * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
061     */
062    @Override
063    protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
064        boolean documentValid = super.processCustomApproveDocumentBusinessRules(document);
065        final CashDrawer cashDrawer = (CashDrawer)document.getNewMaintainableObject().getBusinessObject();
066        checkCurrencyAmountsPositive(cashDrawer);
067        checkCoinAmountsPositive(cashDrawer);
068        return documentValid;
069    }
070
071    /**
072     * Validates that all the currency counts are positive
073     * @param cashDrawer the cash drawer to check
074     * @return true if the cash drawer has valid positive currency amounts, false otherwise
075     */
076    protected boolean checkCurrencyAmountsPositive(CashDrawer cashDrawer) {
077        boolean valid = true;
078        if (cashDrawer.getFinancialDocumentHundredDollarAmount() != null && cashDrawer.getFinancialDocumentHundredDollarAmount().compareTo(KualiDecimal.ZERO) < 0) {
079            putFieldError("hundredDollarCount", OLEKeyConstants.CashDrawerMaintenance.HUNDRED_DOLLAR_AMOUNT_NEGATIVE, new String[] { cashDrawer.getHundredDollarCount().toString() });
080            valid = false;
081        }
082        if (cashDrawer.getFinancialDocumentFiftyDollarAmount() != null && cashDrawer.getFinancialDocumentFiftyDollarAmount().compareTo(KualiDecimal.ZERO) < 0) {
083            putFieldError("fiftyDollarCount", OLEKeyConstants.CashDrawerMaintenance.FIFTY_DOLLAR_AMOUNT_NEGATIVE, new String[] { cashDrawer.getFiftyDollarCount().toString() });
084            valid = false;
085        }
086        if (cashDrawer.getFinancialDocumentTwentyDollarAmount() != null && cashDrawer.getFinancialDocumentTwentyDollarAmount().compareTo(KualiDecimal.ZERO) < 0) {
087            putFieldError("twentyDollarCount", OLEKeyConstants.CashDrawerMaintenance.TWENTY_DOLLAR_AMOUNT_NEGATIVE, new String[] { cashDrawer.getTwentyDollarCount().toString() });
088            valid = false;
089        }
090        if (cashDrawer.getFinancialDocumentTenDollarAmount() != null && cashDrawer.getFinancialDocumentTenDollarAmount().compareTo(KualiDecimal.ZERO) < 0) {
091            putFieldError("tenDollarCount", OLEKeyConstants.CashDrawerMaintenance.TEN_DOLLAR_AMOUNT_NEGATIVE, new String[] { cashDrawer.getTenDollarCount().toString() });
092            valid = false;
093        }
094        if (cashDrawer.getFinancialDocumentFiveDollarAmount() != null && cashDrawer.getFinancialDocumentFiveDollarAmount().compareTo(KualiDecimal.ZERO) < 0) {
095            putFieldError("fiveDollarCount", OLEKeyConstants.CashDrawerMaintenance.FIVE_DOLLAR_AMOUNT_NEGATIVE, new String[] { cashDrawer.getFiveDollarCount().toString() });
096            valid = false;
097        }
098        if (cashDrawer.getFinancialDocumentTwoDollarAmount() != null && cashDrawer.getFinancialDocumentTwoDollarAmount().compareTo(KualiDecimal.ZERO) < 0) {
099            putFieldError("twoDollarCount", OLEKeyConstants.CashDrawerMaintenance.TWO_DOLLAR_AMOUNT_NEGATIVE, new String[] { cashDrawer.getTwoDollarCount().toString() });
100            valid = false;
101        }
102        if (cashDrawer.getFinancialDocumentOneDollarAmount() != null && cashDrawer.getFinancialDocumentOneDollarAmount().compareTo(KualiDecimal.ZERO) < 0) {
103            putFieldError("oneDollarCount", OLEKeyConstants.CashDrawerMaintenance.ONE_DOLLAR_AMOUNT_NEGATIVE, new String[] { cashDrawer.getOneDollarCount().toString() });
104            valid = false;
105        }
106        if (cashDrawer.getFinancialDocumentOtherDollarAmount() != null && cashDrawer.getFinancialDocumentOtherDollarAmount().compareTo(KualiDecimal.ZERO) < 0) {
107            putFieldError("financialDocumentOtherDollarAmount", OLEKeyConstants.CashDrawerMaintenance.OTHER_DOLLAR_AMOUNT_NEGATIVE, new String[] { cashDrawer.getFinancialDocumentOtherDollarAmount().toString() });
108            valid = false;
109        }
110        return valid;
111    }
112    
113    /**
114     * Validates that all the coin counts are positive
115     * @param cashDrawer the cash drawer to check
116     * @return true if the cash drawer has valid positive coin amounts, false otherwise
117     */
118    protected boolean checkCoinAmountsPositive(CashDrawer cashDrawer) {
119        boolean valid = true;
120        if (cashDrawer.getFinancialDocumentHundredCentAmount() != null && cashDrawer.getFinancialDocumentHundredCentAmount().compareTo(KualiDecimal.ZERO) < 0) {
121            putFieldError("hundredCentCount", OLEKeyConstants.CashDrawerMaintenance.HUNDRED_CENT_AMOUNT_NEGATIVE, new String[] { cashDrawer.getHundredCentCount().toString() });
122            valid = false;
123        }
124        if (cashDrawer.getFinancialDocumentFiftyCentAmount() != null && cashDrawer.getFinancialDocumentFiftyCentAmount().compareTo(KualiDecimal.ZERO) < 0) {
125            putFieldError("fiftyCentCount", OLEKeyConstants.CashDrawerMaintenance.FIFTY_CENT_AMOUNT_NEGATIVE, new String[] { cashDrawer.getFiftyCentCount().toString() });
126            valid = false;
127        }
128        if (cashDrawer.getFinancialDocumentTwentyFiveCentAmount() != null && cashDrawer.getFinancialDocumentTwentyFiveCentAmount().compareTo(KualiDecimal.ZERO) < 0) {
129            putFieldError("twentyFiveCentCount", OLEKeyConstants.CashDrawerMaintenance.TWENTY_FIVE_CENT_AMOUNT_NEGATIVE, new String[] { cashDrawer.getTwentyFiveCentCount().toString() });
130            valid = false;
131        }
132        if (cashDrawer.getFinancialDocumentTenCentAmount() != null && cashDrawer.getFinancialDocumentTenCentAmount().compareTo(KualiDecimal.ZERO) < 0) {
133            putFieldError("tenCentCount", OLEKeyConstants.CashDrawerMaintenance.TEN_CENT_AMOUNT_NEGATIVE, new String[] { cashDrawer.getTenCentCount().toString() });
134            valid = false;
135        }
136        if (cashDrawer.getFinancialDocumentFiveCentAmount() != null && cashDrawer.getFinancialDocumentFiveCentAmount().compareTo(KualiDecimal.ZERO) < 0) {
137            putFieldError("fiveCentCount", OLEKeyConstants.CashDrawerMaintenance.FIVE_CENT_AMOUNT_NEGATIVE, new String[] { cashDrawer.getFiveCentCount().toString() });
138            valid = false;
139        }
140        if (cashDrawer.getFinancialDocumentOneCentAmount() != null && cashDrawer.getFinancialDocumentOneCentAmount().compareTo(KualiDecimal.ZERO) < 0) {
141            putFieldError("oneCentCount", OLEKeyConstants.CashDrawerMaintenance.ONE_CENT_AMOUNT_NEGATIVE, new String[] { cashDrawer.getOneCentCount().toString() });
142            valid = false;
143        }
144        if (cashDrawer.getFinancialDocumentOtherCentAmount() != null && cashDrawer.getFinancialDocumentOtherCentAmount().compareTo(KualiDecimal.ZERO) < 0) {
145            putFieldError("financialDocumentOtherCentAmount", OLEKeyConstants.CashDrawerMaintenance.OTHER_CENT_AMOUNT_NEGATIVE, new String[] { cashDrawer.getFinancialDocumentOtherCentAmount().toString() });
146            valid = false;
147        }
148        return valid;
149    }
150    
151    /**
152     * Checks that the cash drawer is still closed at the time of the rule invocation.
153     * @param cashDrawer the cash drawer to check
154     */
155    public boolean checkCashDrawerStillClosed(CashDrawer cashDrawer) {
156       boolean valid = true;
157       final CashDrawerService cashDrawerService = SpringContext.getBean(CashDrawerService.class);
158       final CashDrawer cashDrawerFromDB = cashDrawerService.getByCampusCode(cashDrawer.getCampusCode());
159       if (cashDrawerFromDB != null && !cashDrawerFromDB.isClosed()) {
160           putFieldError("campusCode", OLEKeyConstants.CashDrawerMaintenance.CASH_DRAWER_NOT_CLOSED, new String[] { cashDrawer.getCampusCode() });
161           valid = false;
162       }
163       return valid;
164    }
165}