1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.fp.document.web.struts;
17  
18  import java.sql.Timestamp;
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Properties;
25  import java.util.Set;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.commons.lang.StringUtils;
31  import org.apache.struts.action.ActionForm;
32  import org.apache.struts.action.ActionForward;
33  import org.apache.struts.action.ActionMapping;
34  import org.kuali.ole.fp.businessobject.CashDrawer;
35  import org.kuali.ole.fp.businessobject.CashieringTransaction;
36  import org.kuali.ole.fp.businessobject.Check;
37  import org.kuali.ole.fp.businessobject.CoinDetail;
38  import org.kuali.ole.fp.businessobject.CurrencyDetail;
39  import org.kuali.ole.fp.businessobject.Deposit;
40  import org.kuali.ole.fp.businessobject.DepositWizardCashieringCheckHelper;
41  import org.kuali.ole.fp.businessobject.DepositWizardHelper;
42  import org.kuali.ole.fp.businessobject.format.CashDrawerStatusCodeFormatter;
43  import org.kuali.ole.fp.document.CashManagementDocument;
44  import org.kuali.ole.fp.document.CashReceiptDocument;
45  import org.kuali.ole.fp.document.service.CashManagementService;
46  import org.kuali.ole.fp.document.service.CashReceiptService;
47  import org.kuali.ole.fp.exception.CashDrawerStateException;
48  import org.kuali.ole.fp.service.CashDrawerService;
49  import org.kuali.ole.sys.OLEConstants;
50  import org.kuali.ole.sys.OLEConstants.CashDrawerConstants;
51  import org.kuali.ole.sys.OLEConstants.DocumentStatusCodes.CashReceipt;
52  import org.kuali.ole.sys.OLEKeyConstants;
53  import org.kuali.ole.sys.OLEPropertyConstants;
54  import org.kuali.ole.sys.businessobject.Bank;
55  import org.kuali.ole.sys.context.SpringContext;
56  import org.kuali.ole.sys.document.datadictionary.FinancialSystemTransactionalDocumentEntry;
57  import org.kuali.rice.core.api.datetime.DateTimeService;
58  import org.kuali.rice.core.api.util.type.KualiDecimal;
59  import org.kuali.rice.core.web.format.CurrencyFormatter;
60  import org.kuali.rice.kew.api.exception.WorkflowException;
61  import org.kuali.rice.kim.api.identity.Person;
62  import org.kuali.rice.kns.document.authorization.DocumentAuthorizer;
63  import org.kuali.rice.kns.document.authorization.TransactionalDocumentAuthorizer;
64  import org.kuali.rice.kns.document.authorization.TransactionalDocumentPresentationController;
65  import org.kuali.rice.kns.service.DataDictionaryService;
66  import org.kuali.rice.kns.service.DocumentHelperService;
67  import org.kuali.rice.kns.web.struts.action.KualiAction;
68  import org.kuali.rice.krad.service.BusinessObjectService;
69  import org.kuali.rice.krad.service.DocumentService;
70  import org.kuali.rice.krad.util.GlobalVariables;
71  import org.kuali.rice.krad.util.KRADConstants;
72  import org.kuali.rice.krad.util.UrlFactory;
73  
74  
75  
76  
77  
78  public class DepositWizardAction extends KualiAction {
79      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DepositWizardAction.class);
80      private static final String CASH_MANAGEMENT_STATUS_PAGE = "/cashManagementStatus.do";
81  
82      
83  
84  
85  
86  
87  
88  
89      @Override
90      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
91          DepositWizardForm dwForm = (DepositWizardForm) form;
92  
93          ActionForward dest = super.execute(mapping, form, request, response);
94  
95          
96          DocumentAuthorizer cmDocAuthorizer = SpringContext.getBean(DocumentHelperService.class).getDocumentAuthorizer(OLEConstants.FinancialDocumentTypeCodes.CASH_MANAGEMENT);
97          Person luser = GlobalVariables.getUserSession().getPerson();
98          cmDocAuthorizer.canInitiate(OLEConstants.FinancialDocumentTypeCodes.CASH_MANAGEMENT, luser);
99  
100         
101         String cmDocId = dwForm.getCashManagementDocId();
102         if (StringUtils.isBlank(cmDocId)) {
103             cmDocId = request.getParameter("cmDocId");
104             String depositTypeCode = request.getParameter("depositTypeCode");
105 
106             CashManagementDocument cmDoc = (CashManagementDocument) SpringContext.getBean(DocumentService.class).getByDocumentHeaderId(cmDocId);
107 
108             try {
109                 initializeForm(dwForm, cmDoc, depositTypeCode);
110             }
111             catch (CashDrawerStateException cdse) {
112                 dest = new ActionForward(UrlFactory.parameterizeUrl(CASH_MANAGEMENT_STATUS_PAGE, cdse.toProperties()), true);
113             }
114         } else { 
115             loadCashReceipts(dwForm);
116             loadUndepositedCashieringChecks(dwForm);
117             if (dwForm.getTargetDepositAmount() == null) {
118                 calculateTargetFinalDepositAmount(dwForm);
119             }
120             
121         }
122 
123         return dest;
124     }
125 
126     
127 
128 
129 
130 
131 
132 
133     private void initializeForm(DepositWizardForm dform, CashManagementDocument cmDoc, String depositTypeCode) {
134         CashDrawer cd = SpringContext.getBean(CashDrawerService.class).getByCampusCode(cmDoc.getCampusCode());
135         if (cd == null) {
136             throw new RuntimeException("No cash drawer exists for campus code " + cmDoc.getCampusCode() + "; please create on via the Cash Drawer Maintenance Document before attemping to create a CashManagementDocument for campus " + cmDoc.getCampusCode());
137         }
138         if (!cd.isOpen()) {
139             CashDrawerStatusCodeFormatter f = new CashDrawerStatusCodeFormatter();
140 
141             String cmDocId = cmDoc.getDocumentNumber();
142             String currentState = cd.getStatusCode();
143 
144             throw new CashDrawerStateException(cmDoc.getCampusCode(), cmDocId, (String) f.format(CashDrawerConstants.STATUS_OPEN), (String) f.format(cd.getStatusCode()));
145         }
146 
147         dform.setCashManagementDocId(cmDoc.getDocumentNumber());
148         dform.setCashDrawerCampusCode(cmDoc.getCampusCode());
149 
150         dform.setDepositTypeCode(depositTypeCode);
151 
152         if (depositTypeCode.equals(OLEConstants.DocumentStatusCodes.CashReceipt.FINAL)) {
153             
154             CurrencyDetail currencyDetail = new CurrencyDetail();
155             currencyDetail.setDocumentNumber(cmDoc.getDocumentNumber());
156             currencyDetail.setCashieringStatus(OLEConstants.CurrencyCoinSources.DEPOSITS);
157             currencyDetail.setFinancialDocumentTypeCode(CashieringTransaction.DETAIL_DOCUMENT_TYPE);
158             dform.setCurrencyDetail(currencyDetail);
159 
160             CoinDetail coinDetail = new CoinDetail();
161             coinDetail.setDocumentNumber(cmDoc.getDocumentNumber());
162             coinDetail.setCashieringStatus(OLEConstants.CurrencyCoinSources.DEPOSITS);
163             coinDetail.setFinancialDocumentTypeCode(CashieringTransaction.DETAIL_DOCUMENT_TYPE);
164             dform.setCoinDetail(coinDetail);
165         }
166 
167         loadCashReceipts(dform);
168         loadUndepositedCashieringChecks(dform);
169         if (dform.isDepositFinal() && dform.getTargetDepositAmount() == null) {
170             calculateTargetFinalDepositAmount(dform);
171         }
172         loadEditModesAndDocumentActions(dform);
173     }
174 
175     
176 
177 
178 
179 
180     private void loadCashReceipts(DepositWizardForm dform) {
181         List<CashReceiptDocument> verifiedReceipts = SpringContext.getBean(CashReceiptService.class).getCashReceipts(dform.getCashDrawerCampusCode(), new String[] { CashReceipt.VERIFIED, CashReceipt.INTERIM });
182         dform.setDepositableCashReceipts(new ArrayList());
183         dform.setCheckFreeCashReceipts(new ArrayList<CashReceiptDocument>());
184 
185         
186         int index = 0;
187         for (Iterator i = verifiedReceipts.iterator(); i.hasNext();) {
188             CashReceiptDocument receipt = (CashReceiptDocument) i.next();
189             receipt.processAfterRetrieve(); 
190             String docStatus = receipt.getFinancialSystemDocumentHeader().getFinancialDocumentStatusCode();
191             if (docStatus.equalsIgnoreCase(CashReceipt.VERIFIED)) { 
192                 if (receipt.getCheckCount() == 0 && receipt.getTotalConfirmedCheckAmount().equals(KualiDecimal.ZERO)) {
193                     dform.getCheckFreeCashReceipts().add(receipt);
194                 }
195                 else {
196                     dform.getDepositableCashReceipts().add(receipt);
197                     DepositWizardHelper d = dform.getDepositWizardHelper(index++);
198                     
199                     Timestamp ts = new Timestamp(receipt.getDocumentHeader().getWorkflowDocument().getDateCreated().getMillis());
200                     try {
201                         d.setCashReceiptCreateDate(SpringContext.getBean(DateTimeService.class).convertToSqlDate(ts));
202                     }
203                     catch (Exception e) {
204 
205                     }
206                 }
207             }
208             else if (docStatus.equalsIgnoreCase(CashReceipt.INTERIM)) { 
209                 
210                 if (receipt.getTotalConfirmedCashAmount().isGreaterThan(KualiDecimal.ZERO)) {
211                     dform.getCheckFreeCashReceipts().add(receipt);
212                 }
213             }
214             dform.addCashReceiptToChecks(receipt);
215         }
216     }
217     
218     
219 
220 
221 
222     private void calculateTargetFinalDepositAmount(DepositWizardForm dwform) {
223         final CashReceiptService cashReceiptService = SpringContext.getBean(CashReceiptService.class);
224         final List<CashReceiptDocument> interestingReceipts = 
225                 cashReceiptService.getCashReceipts(dwform.getCashDrawerCampusCode(), new String[] { CashReceipt.VERIFIED, CashReceipt.INTERIM, CashReceipt.FINAL });
226         for (CashReceiptDocument crDoc : interestingReceipts) {
227             crDoc.refreshCashDetails();
228             dwform.addCashReceiptToTargetTotal(crDoc);
229         }
230 
231         final CashManagementService cashManagementService = SpringContext.getBean(CashManagementService.class);
232         KualiDecimal toBeDepositedChecksTotal = KualiDecimal.ZERO;
233         for (Check check : cashManagementService.selectUndepositedCashieringChecks(dwform.getCashManagementDocId())) {
234             toBeDepositedChecksTotal = toBeDepositedChecksTotal.add(check.getAmount());
235         }
236         
237         for (Check check : cashManagementService.selectDepositedCashieringChecks(dwform.getCashManagementDocId())) {
238             toBeDepositedChecksTotal = toBeDepositedChecksTotal.add(check.getAmount());
239         }
240         dwform.addCashieringTransactionToTargetTotal(toBeDepositedChecksTotal);
241     }
242 
243     
244 
245 
246 
247 
248     private void loadUndepositedCashieringChecks(DepositWizardForm dform) {
249         List<Check> cashieringChecks = SpringContext.getBean(CashManagementService.class).selectUndepositedCashieringChecks(dform.getCashManagementDocId());
250         dform.setDepositableCashieringChecks(cashieringChecks);
251     }
252 
253     private void loadEditModesAndDocumentActions(DepositWizardForm dform) {
254         final FinancialSystemTransactionalDocumentEntry ddEntry = getCashManagementDataDictionaryEntry();
255         final TransactionalDocumentPresentationController presentationController = getCashManagementPresentationController(ddEntry);
256         final TransactionalDocumentAuthorizer docAuthorizer = getCashManagementDocumentAuthorizer(ddEntry);
257 
258         dform.setEditingMode(retrieveEditingModes(dform.getCashManagementDocId(), presentationController, docAuthorizer));
259         dform.setDocumentActions(retrieveDocumentActions(dform.getCashManagementDocId(), presentationController, docAuthorizer));
260     }
261 
262     
263 
264 
265     protected String getCashManagementDocumentTypeName() {
266         return "CMD";
267     }
268 
269     
270 
271 
272     private FinancialSystemTransactionalDocumentEntry getCashManagementDataDictionaryEntry() {
273         final DataDictionaryService ddService = SpringContext.getBean(DataDictionaryService.class);
274         return (FinancialSystemTransactionalDocumentEntry)ddService.getDataDictionary().getDocumentEntry(getCashManagementDocumentTypeName());
275     }
276 
277     
278 
279 
280 
281 
282     private TransactionalDocumentPresentationController getCashManagementPresentationController(FinancialSystemTransactionalDocumentEntry cashManagementEntry) {
283         final Class presentationControllerClass = cashManagementEntry.getDocumentPresentationControllerClass();
284         TransactionalDocumentPresentationController presentationController = null;
285         try {
286             presentationController = (TransactionalDocumentPresentationController)presentationControllerClass.newInstance();
287         }
288         catch (InstantiationException ie) {
289             throw new RuntimeException("Could not instantiate cash management presentation controller of class " + presentationControllerClass.getName(), ie);
290         }
291         catch (IllegalAccessException iae) {
292             throw new RuntimeException("Could not instantiate cash management presentation controller of class " + presentationControllerClass.getName(), iae);
293         }
294         return presentationController;
295     }
296 
297     
298 
299 
300 
301 
302     private TransactionalDocumentAuthorizer getCashManagementDocumentAuthorizer(FinancialSystemTransactionalDocumentEntry cashManagementEntry) {
303         final Class docAuthorizerClass = cashManagementEntry.getDocumentAuthorizerClass();
304         TransactionalDocumentAuthorizer docAuthorizer = null;
305         try {
306             docAuthorizer = (TransactionalDocumentAuthorizer)docAuthorizerClass.newInstance();
307         }
308         catch (InstantiationException ie) {
309             throw new RuntimeException("Could not instantiate cash management document authorizer of class " + docAuthorizerClass.getName(), ie);
310         }
311         catch (IllegalAccessException iae) {
312             throw new RuntimeException("Could not instantiate cash management document authorizer of class " + docAuthorizerClass.getName(), iae);
313         }
314         return docAuthorizer;
315     }
316 
317     
318 
319 
320 
321 
322 
323 
324     private Map retrieveEditingModes(String cashManagementDocId, TransactionalDocumentPresentationController presentationController, TransactionalDocumentAuthorizer docAuthorizer) {
325         Map editModeMap = null;
326         try {
327             final CashManagementDocument cmDoc = (CashManagementDocument)SpringContext.getBean(DocumentService.class).getByDocumentHeaderId(cashManagementDocId);
328             Set<String> editModes = presentationController.getEditModes(cmDoc);
329             editModes = docAuthorizer.getEditModes(cmDoc, GlobalVariables.getUserSession().getPerson(), editModes);
330             editModeMap = convertSetToMap(editModes);
331         }
332         catch (WorkflowException we) {
333             throw new RuntimeException("Workflow exception while retrieving document " + cashManagementDocId, we);
334         }
335         return editModeMap;
336     }
337 
338     
339 
340 
341 
342 
343 
344 
345     private Map retrieveDocumentActions(String cashManagementDocId, TransactionalDocumentPresentationController presentationController, TransactionalDocumentAuthorizer docAuthorizer) {
346         Map documentActionsMap = null;
347         try {
348             final CashManagementDocument cmDoc = (CashManagementDocument)SpringContext.getBean(DocumentService.class).getByDocumentHeaderId(cashManagementDocId);
349             Set<String> documentActions = presentationController.getDocumentActions(cmDoc);
350             documentActions = docAuthorizer.getEditModes(cmDoc, GlobalVariables.getUserSession().getPerson(), documentActions);
351             documentActionsMap = convertSetToMap(documentActions);
352         }
353         catch (WorkflowException we) {
354             throw new RuntimeException("Workflow exception while retrieving document " + cashManagementDocId, we);
355         }
356         return documentActionsMap;
357     }
358 
359     
360 
361 
362 
363 
364     protected Map convertSetToMap(Set s){
365         Map map = new HashMap();
366         Iterator i = s.iterator();
367         while(i.hasNext()) {
368             Object key = i.next();
369             map.put(key, KRADConstants.KUALI_DEFAULT_TRUE_VALUE);
370         }
371         return map;
372     }
373 
374     
375 
376 
377 
378 
379 
380     @Override
381     public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
382         loadCashReceipts((DepositWizardForm) form);
383         loadUndepositedCashieringChecks((DepositWizardForm) form);
384         if (((DepositWizardForm) form).isDepositFinal() && ((DepositWizardForm)form).getTargetDepositAmount() == null) {
385             calculateTargetFinalDepositAmount((DepositWizardForm) form);
386         }
387         loadEditModesAndDocumentActions((DepositWizardForm) form);
388 
389         return super.refresh(mapping, form, request, response);
390     }
391 
392     
393 
394 
395 
396 
397 
398 
399 
400 
401 
402     public ActionForward startWizard(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
403         return mapping.findForward(OLEConstants.MAPPING_BASIC);
404     }
405 
406     
407 
408 
409 
410 
411 
412 
413 
414 
415     public ActionForward createDeposit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
416         ActionForward dest = mapping.findForward(OLEConstants.MAPPING_BASIC);
417 
418         DepositWizardForm dform = (DepositWizardForm) form;
419         final BusinessObjectService boService = SpringContext.getBean(BusinessObjectService.class);
420         final CashReceiptService cashReceiptService = SpringContext.getBean(CashReceiptService.class);
421         final DocumentService documentService = SpringContext.getBean(DocumentService.class);
422         final CashManagementService cashManagementService = SpringContext.getBean(CashManagementService.class);
423 
424         CurrencyFormatter formatter = new CurrencyFormatter();
425 
426         
427         loadCashReceipts((DepositWizardForm) form);
428         if (((DepositWizardForm) form).isDepositFinal() && ((DepositWizardForm)form).getTargetDepositAmount() == null) {
429             calculateTargetFinalDepositAmount((DepositWizardForm) form);
430         }
431         loadEditModesAndDocumentActions(dform);
432 
433         
434         String bankCode = dform.getBankCode();
435         if (StringUtils.isBlank(bankCode)) {
436             GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_MISSING_BANK);
437         }
438         else {
439             Map keyMap = new HashMap();
440             keyMap.put(OLEPropertyConstants.BANK_CODE, bankCode);
441 
442             Bank bank = boService.findByPrimaryKey(Bank.class, keyMap);
443             if (bank == null) {
444                 GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_UNKNOWN_BANK, bankCode);
445             }
446             else {
447                 dform.setBank(bank);
448             }
449         }
450 
451         boolean depositIsFinal = (StringUtils.equals(dform.getDepositTypeCode(), OLEConstants.DepositConstants.DEPOSIT_TYPE_FINAL));
452 
453         
454         List selectedIds = new ArrayList();
455         for (Iterator i = dform.getDepositWizardHelpers().iterator(); i.hasNext();) {
456             String checkValue = ((DepositWizardHelper) i.next()).getSelectedValue();
457 
458             if (StringUtils.isNotBlank(checkValue) && !checkValue.equals(OLEConstants.ParameterValues.NO)) {
459                 
460                 selectedIds.add(checkValue);
461             }
462         }
463 
464         if (depositIsFinal) {
465             
466             dform.setCheckFreeCashReceipts(new ArrayList<CashReceiptDocument>());
467             List<CashReceiptDocument> cashReceipts = cashReceiptService.getCashReceipts(dform.getCashDrawerCampusCode(), new String[] {CashReceipt.VERIFIED, CashReceipt.INTERIM});
468             for (Object crDocObj : cashReceipts) {
469                 CashReceiptDocument crDoc = (CashReceiptDocument) crDocObj;
470                 crDoc.refreshCashDetails();
471                 if (crDoc.getFinancialSystemDocumentHeader().getFinancialDocumentStatusCode().equals(CashReceipt.VERIFIED) && crDoc.getCheckCount() == 0) {
472                     
473                     selectedIds.add(crDoc.getDocumentNumber());
474                     dform.getCheckFreeCashReceipts().add(crDoc);
475                 }
476                 else if (crDoc.getFinancialSystemDocumentHeader().getFinancialDocumentStatusCode().equals(CashReceipt.INTERIM) &&
477                         crDoc.getGrandTotalConfirmedCashAmount().isGreaterThan(KualiDecimal.ZERO)) {
478 
479 
480 
481                         selectedIds.add(crDoc.getDocumentNumber());
482                         dform.getCheckFreeCashReceipts().add(crDoc);
483                 }
484             }
485         }
486 
487         
488         List<Integer> selectedCashieringChecks = new ArrayList<Integer>();
489         for (DepositWizardCashieringCheckHelper helper : dform.getDepositWizardCashieringCheckHelpers()) {
490             if (helper.getSequenceId() != null && !helper.getSequenceId().equals(new Integer(-1))) {
491                 selectedCashieringChecks.add(helper.getSequenceId());
492             }
493         }
494 
495         if (selectedIds.isEmpty() && selectedCashieringChecks.isEmpty()) {
496             GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_CASHRECEIPT_ERROR, OLEKeyConstants.Deposit.ERROR_NO_CASH_RECEIPTS_SELECTED);
497         }
498 
499         
500         
501         if (GlobalVariables.getMessageMap().hasNoErrors()) {
502             try {
503                 
504                 List selectedReceipts = new ArrayList();
505                 if (selectedIds != null && !selectedIds.isEmpty()) {
506                     selectedReceipts = documentService.getDocumentsByListOfDocumentHeaderIds(CashReceiptDocument.class, selectedIds);
507                 }
508 
509                 if (depositIsFinal) {
510                     
511 
512 
513 
514 
515 
516 
517 
518                     KualiDecimal toBeDepositedChecksTotal = KualiDecimal.ZERO;
519                     
520                     for (Check check : cashManagementService.selectUndepositedCashieringChecks(dform.getCashManagementDocId())) {
521                         if (!selectedCashieringChecks.contains(check.getSequenceId())) {
522                             GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_CASHIERING_CHECK_MUST_BE_DEPOSITED, new String[] { check.getCheckNumber() });
523                         }
524                         else {
525                             toBeDepositedChecksTotal = toBeDepositedChecksTotal.add(check.getAmount());
526                         }
527                     }
528                     
529                     for (Check check : cashManagementService.selectDepositedCashieringChecks(dform.getCashManagementDocId())) {
530                         toBeDepositedChecksTotal = toBeDepositedChecksTotal.add(check.getAmount());
531                     }
532 
533                     
534                     checkEnoughCurrencyForDeposit(dform);
535                     checkEnoughCoinForDeposit(dform);
536 
537                     
538                     List<CashReceiptDocument> interestingReceipts = cashReceiptService.getCashReceipts(dform.getCashDrawerCampusCode(), new String[] { CashReceipt.VERIFIED, CashReceipt.INTERIM, CashReceipt.FINAL });
539                     CurrencyDetail currencyTotal = new CurrencyDetail();
540                     CoinDetail coinTotal = new CoinDetail();
541                     for (CashReceiptDocument receipt : interestingReceipts) {
542                         receipt.refreshCashDetails();
543                         if (receipt.getCurrencyDetail() != null) {
544                             currencyTotal.add(receipt.getConfirmedCurrencyDetail());
545                             currencyTotal.subtract(receipt.getChangeCurrencyDetail());
546                         }
547                         if (receipt.getCoinDetail() != null) {
548                             coinTotal.add(receipt.getConfirmedCoinDetail());
549                             coinTotal.subtract(receipt.getChangeCoinDetail());
550                         }
551                     }
552 
553                     KualiDecimal cashReceiptCashTotal = currencyTotal.getTotalAmount().add(coinTotal.getTotalAmount());
554                     
555                     
556                     cashReceiptCashTotal = cashReceiptCashTotal.subtract(toBeDepositedChecksTotal);
557                     KualiDecimal depositedCashTotal = dform.getCurrencyDetail().getTotalAmount().add(dform.getCoinDetail().getTotalAmount());
558                     if (!cashReceiptCashTotal.equals(depositedCashTotal)) {
559                         GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_CASH_DEPOSIT_DID_NOT_BALANCE, new String[] { formatter.format(depositedCashTotal).toString(), formatter.format(cashReceiptCashTotal).toString() });
560                     }
561                 }
562 
563                 
564                 if (GlobalVariables.getMessageMap().hasNoErrors()) {
565                     
566                     String cashManagementDocId = dform.getCashManagementDocId();
567                     CashManagementDocument cashManagementDoc = null;
568                     try {
569                         cashManagementDoc = (CashManagementDocument) documentService.getByDocumentHeaderId(cashManagementDocId);
570                         if (cashManagementDoc == null) {
571                             throw new IllegalStateException("unable to find cashManagementDocument with id " + cashManagementDocId);
572                         }
573                     }
574                     catch (WorkflowException e) {
575                         throw new IllegalStateException("unable to retrieve cashManagementDocument with id " + cashManagementDocId, e);
576                     }
577 
578                     
579                     String cmDocId = dform.getCashManagementDocId();
580 
581                     cashManagementService.addDeposit(cashManagementDoc, dform.getDepositTicketNumber(), dform.getBank(), selectedReceipts, selectedCashieringChecks, depositIsFinal);
582 
583                     if (depositIsFinal) {
584                         
585                         Deposit finalDeposit = findFinalDeposit(cashManagementDoc);
586                         
587                         if (dform.getCurrencyDetail() != null) {
588                             
589                             boService.save(dform.getCurrencyDetail());
590                             cashManagementDoc.getCashDrawer().removeCurrency(dform.getCurrencyDetail());
591                             finalDeposit.setDepositAmount(finalDeposit.getDepositAmount().add(dform.getCurrencyDetail().getTotalAmount()));
592                         }
593                         if (dform.getCoinDetail() != null) {
594                             
595                             boService.save(dform.getCoinDetail());
596                             cashManagementDoc.getCashDrawer().removeCoin(dform.getCoinDetail());
597                             finalDeposit.setDepositAmount(finalDeposit.getDepositAmount().add(dform.getCoinDetail().getTotalAmount()));
598                         }
599                         boService.save(cashManagementDoc.getCashDrawer());
600                         boService.save(finalDeposit);
601                     }
602 
603                     
604                     dest = returnToSender(cashManagementDocId);
605                 }
606             }
607             catch (WorkflowException e) {
608                 throw new IllegalArgumentException("unable to retrieve cashReceipts by documentId", e);
609             }
610         }
611 
612         return dest;
613     }
614 
615     private Deposit findFinalDeposit(CashManagementDocument cmDoc) {
616         Deposit finalDeposit = null;
617         for (Deposit deposit : cmDoc.getDeposits()) {
618             if (deposit.getDepositTypeCode().equals(OLEConstants.DepositConstants.DEPOSIT_TYPE_FINAL)) {
619                 finalDeposit = deposit;
620                 break;
621             }
622         }
623         return finalDeposit;
624     }
625 
626     
627 
628 
629 
630 
631 
632 
633 
634     private boolean checkEnoughCurrencyForDeposit(DepositWizardForm depositForm) {
635         boolean success = true;
636         CurrencyDetail detail = depositForm.getCurrencyDetail();
637         if (detail != null) {
638             
639             CashDrawer drawer = SpringContext.getBean(CashDrawerService.class).getByCampusCode(depositForm.getCashDrawerCampusCode());
640             
641             
642             
643             
644             CurrencyFormatter formatter = new CurrencyFormatter();
645             if (detail.getFinancialDocumentHundredDollarAmount() != null && detail.getFinancialDocumentHundredDollarAmount().isGreaterThan(KualiDecimal.ZERO)) {
646                 if (drawer.getFinancialDocumentHundredDollarAmount() == null || drawer.getFinancialDocumentHundredDollarAmount().isLessThan(detail.getFinancialDocumentHundredDollarAmount())) {
647                     GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_NOT_ENOUGH_CASH_TO_COMPLETE_DEPOSIT, new String[] { "hundred dollar amount", formatter.format(detail.getFinancialDocumentHundredDollarAmount()).toString(), formatter.format(drawer.getFinancialDocumentHundredDollarAmount()).toString() });
648                     success = false;
649                 }
650             }
651             if (detail.getFinancialDocumentFiftyDollarAmount() != null && detail.getFinancialDocumentFiftyDollarAmount().isGreaterThan(KualiDecimal.ZERO)) {
652                 if (drawer.getFinancialDocumentFiftyDollarAmount() == null || drawer.getFinancialDocumentFiftyDollarAmount().isLessThan(detail.getFinancialDocumentFiftyDollarAmount())) {
653                     GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_NOT_ENOUGH_CASH_TO_COMPLETE_DEPOSIT, new String[] { "fifty dollar amount", formatter.format(detail.getFinancialDocumentFiftyDollarAmount()).toString(), formatter.format(drawer.getFinancialDocumentFiftyDollarAmount()).toString() });
654                     success = false;
655                 }
656             }
657             if (detail.getFinancialDocumentTwentyDollarAmount() != null && detail.getFinancialDocumentTwentyDollarAmount().isGreaterThan(KualiDecimal.ZERO)) {
658                 if (drawer.getFinancialDocumentTwentyDollarAmount() == null || drawer.getFinancialDocumentTwentyDollarAmount().isLessThan(detail.getFinancialDocumentTwentyDollarAmount())) {
659                     GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_NOT_ENOUGH_CASH_TO_COMPLETE_DEPOSIT, new String[] { "twenty dollar amount", formatter.format(detail.getFinancialDocumentTwentyDollarAmount()).toString(), formatter.format(drawer.getFinancialDocumentTwentyDollarAmount()).toString() });
660                     success = false;
661                 }
662             }
663             if (detail.getFinancialDocumentTenDollarAmount() != null && detail.getFinancialDocumentTenDollarAmount().isGreaterThan(KualiDecimal.ZERO)) {
664                 if (drawer.getFinancialDocumentTenDollarAmount() == null || drawer.getFinancialDocumentTenDollarAmount().isLessThan(detail.getFinancialDocumentTenDollarAmount())) {
665                     GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_NOT_ENOUGH_CASH_TO_COMPLETE_DEPOSIT, new String[] { "ten dollar amount", formatter.format(detail.getFinancialDocumentTenDollarAmount()).toString(), formatter.format(drawer.getFinancialDocumentTenDollarAmount()).toString() });
666                     success = false;
667                 }
668             }
669             if (detail.getFinancialDocumentFiveDollarAmount() != null && detail.getFinancialDocumentFiveDollarAmount().isGreaterThan(KualiDecimal.ZERO)) {
670                 if (drawer.getFinancialDocumentFiveDollarAmount() == null || drawer.getFinancialDocumentFiveDollarAmount().isLessThan(detail.getFinancialDocumentFiveDollarAmount())) {
671                     GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_NOT_ENOUGH_CASH_TO_COMPLETE_DEPOSIT, new String[] { "five dollar amount", formatter.format(detail.getFinancialDocumentFiveDollarAmount()).toString(), formatter.format(drawer.getFinancialDocumentFiveDollarAmount()).toString() });
672                     success = false;
673                 }
674             }
675             if (detail.getFinancialDocumentTwoDollarAmount() != null && detail.getFinancialDocumentTwoDollarAmount().isGreaterThan(KualiDecimal.ZERO)) {
676                 if (drawer.getFinancialDocumentTwoDollarAmount() == null || drawer.getFinancialDocumentTwoDollarAmount().isLessThan(detail.getFinancialDocumentTwoDollarAmount())) {
677                     GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_NOT_ENOUGH_CASH_TO_COMPLETE_DEPOSIT, new String[] { "two dollar amount", formatter.format(detail.getFinancialDocumentTwoDollarAmount()).toString(), formatter.format(drawer.getFinancialDocumentTwoDollarAmount()).toString() });
678                     success = false;
679                 }
680             }
681             if (detail.getFinancialDocumentOneDollarAmount() != null && detail.getFinancialDocumentOneDollarAmount().isGreaterThan(KualiDecimal.ZERO)) {
682                 if (drawer.getFinancialDocumentOneDollarAmount() == null || drawer.getFinancialDocumentOneDollarAmount().isLessThan(detail.getFinancialDocumentOneDollarAmount())) {
683                     GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_NOT_ENOUGH_CASH_TO_COMPLETE_DEPOSIT, new String[] { "one dollar amount", formatter.format(detail.getFinancialDocumentOneDollarAmount()).toString(), formatter.format(drawer.getFinancialDocumentOneDollarAmount()).toString() });
684                     success = false;
685                 }
686             }
687             if (detail.getFinancialDocumentOtherDollarAmount() != null && detail.getFinancialDocumentOtherDollarAmount().isGreaterThan(KualiDecimal.ZERO)) {
688                 if (drawer.getFinancialDocumentOtherDollarAmount() == null || drawer.getFinancialDocumentOtherDollarAmount().isLessThan(detail.getFinancialDocumentOtherDollarAmount())) {
689                     GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_NOT_ENOUGH_CASH_TO_COMPLETE_DEPOSIT, new String[] { "other dollar amount", formatter.format(detail.getFinancialDocumentOtherDollarAmount()).toString(), formatter.format(drawer.getFinancialDocumentOtherDollarAmount()).toString() });
690                     success = false;
691                 }
692             }
693         }
694         return success;
695     }
696 
697     
698 
699 
700 
701 
702 
703 
704     public boolean checkEnoughCoinForDeposit(DepositWizardForm depositForm) {
705         boolean success = true;
706         CoinDetail detail = depositForm.getCoinDetail();
707         if (detail != null) {
708             
709             CashDrawer drawer = SpringContext.getBean(CashDrawerService.class).getByCampusCode(depositForm.getCashDrawerCampusCode());
710             
711             
712             
713             
714             CurrencyFormatter formatter = new CurrencyFormatter();
715             if (detail.getFinancialDocumentHundredCentAmount() != null && detail.getFinancialDocumentHundredCentAmount().isGreaterThan(KualiDecimal.ZERO)) {
716                 if (drawer.getFinancialDocumentHundredCentAmount() == null || drawer.getFinancialDocumentHundredCentAmount().isLessThan(detail.getFinancialDocumentHundredCentAmount())) {
717                     GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_NOT_ENOUGH_CASH_TO_COMPLETE_DEPOSIT, new String[] { "hundred cent amount", formatter.format(detail.getFinancialDocumentHundredCentAmount()).toString(), formatter.format(drawer.getFinancialDocumentHundredCentAmount()).toString() });
718                     success = false;
719                 }
720             }
721             if (detail.getFinancialDocumentFiftyCentAmount() != null && detail.getFinancialDocumentFiftyCentAmount().isGreaterThan(KualiDecimal.ZERO)) {
722                 if (drawer.getFinancialDocumentFiftyCentAmount() == null || drawer.getFinancialDocumentFiftyCentAmount().isLessThan(detail.getFinancialDocumentFiftyCentAmount())) {
723                     GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_NOT_ENOUGH_CASH_TO_COMPLETE_DEPOSIT, new String[] { "fifty cent amount", formatter.format(detail.getFinancialDocumentFiftyCentAmount()).toString(), formatter.format(drawer.getFinancialDocumentFiftyCentAmount()).toString() });
724                     success = false;
725                 }
726             }
727             if (detail.getFinancialDocumentTwentyFiveCentAmount() != null && detail.getFinancialDocumentTwentyFiveCentAmount().isGreaterThan(KualiDecimal.ZERO)) {
728                 if (drawer.getFinancialDocumentTwentyFiveCentAmount() == null || drawer.getFinancialDocumentTwentyFiveCentAmount().isLessThan(detail.getFinancialDocumentTwentyFiveCentAmount())) {
729                     GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_NOT_ENOUGH_CASH_TO_COMPLETE_DEPOSIT, new String[] { "twenty five cent amount", formatter.format(detail.getFinancialDocumentTwentyFiveCentAmount()).toString(), formatter.format(drawer.getFinancialDocumentTwentyFiveCentAmount()).toString() });
730                     success = false;
731                 }
732             }
733             if (detail.getFinancialDocumentTenCentAmount() != null && detail.getFinancialDocumentTenCentAmount().isGreaterThan(KualiDecimal.ZERO)) {
734                 if (drawer.getFinancialDocumentTenCentAmount() == null || drawer.getFinancialDocumentTenCentAmount().isLessThan(detail.getFinancialDocumentTenCentAmount())) {
735                     GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_NOT_ENOUGH_CASH_TO_COMPLETE_DEPOSIT, new String[] { "ten cent amount", formatter.format(detail.getFinancialDocumentTenCentAmount()).toString(), formatter.format(drawer.getFinancialDocumentTenCentAmount()).toString() });
736                     success = false;
737                 }
738             }
739             if (detail.getFinancialDocumentFiveCentAmount() != null && detail.getFinancialDocumentFiveCentAmount().isGreaterThan(KualiDecimal.ZERO)) {
740                 if (drawer.getFinancialDocumentFiveCentAmount() == null || drawer.getFinancialDocumentFiveCentAmount().isLessThan(detail.getFinancialDocumentFiveCentAmount())) {
741                     GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_NOT_ENOUGH_CASH_TO_COMPLETE_DEPOSIT, new String[] { "five cent amount", formatter.format(detail.getFinancialDocumentFiveCentAmount()).toString(), formatter.format(drawer.getFinancialDocumentFiveCentAmount()).toString() });
742                     success = false;
743                 }
744             }
745             if (detail.getFinancialDocumentOneCentAmount() != null && detail.getFinancialDocumentOneCentAmount().isGreaterThan(KualiDecimal.ZERO)) {
746                 if (drawer.getFinancialDocumentOneCentAmount() == null || drawer.getFinancialDocumentOneCentAmount().isLessThan(detail.getFinancialDocumentOneCentAmount())) {
747                     GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_NOT_ENOUGH_CASH_TO_COMPLETE_DEPOSIT, new String[] { "one cent amount", formatter.format(detail.getFinancialDocumentOneCentAmount()).toString(), formatter.format(drawer.getFinancialDocumentOneCentAmount()).toString() });
748                     success = false;
749                 }
750             }
751             if (detail.getFinancialDocumentOtherCentAmount() != null && detail.getFinancialDocumentOtherCentAmount().isGreaterThan(KualiDecimal.ZERO)) {
752                 if (drawer.getFinancialDocumentOtherCentAmount() == null || drawer.getFinancialDocumentOtherCentAmount().isLessThan(detail.getFinancialDocumentOtherCentAmount())) {
753                     GlobalVariables.getMessageMap().putError(OLEConstants.DepositConstants.DEPOSIT_WIZARD_DEPOSITHEADER_ERROR, OLEKeyConstants.Deposit.ERROR_NOT_ENOUGH_CASH_TO_COMPLETE_DEPOSIT, new String[] { "other cent amount", formatter.format(detail.getFinancialDocumentOtherCentAmount()).toString(), formatter.format(drawer.getFinancialDocumentOtherCentAmount()).toString() });
754                     success = false;
755                 }
756             }
757         }
758         return success;
759     }
760 
761 
762     
763 
764 
765 
766 
767 
768 
769 
770 
771 
772     public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
773         DepositWizardForm dform = (DepositWizardForm) form;
774 
775         ActionForward dest = returnToSender(dform.getCashManagementDocId());
776         return dest;
777     }
778 
779 
780     
781 
782 
783 
784 
785     private ActionForward returnToSender(String cmDocId) {
786         Properties params = new Properties();
787         params.setProperty("methodToCall", "docHandler");
788         params.setProperty("command", "displayDocSearchView");
789         params.setProperty("docId", cmDocId);
790 
791         String cmActionUrl = UrlFactory.parameterizeUrl(OLEConstants.CASH_MANAGEMENT_DOCUMENT_ACTION, params);
792 
793         return new ActionForward(cmActionUrl, true);
794     }
795 }