001/*
002 * Copyright 2006 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.web.struts;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.kuali.ole.fp.businessobject.Check;
023import org.kuali.ole.fp.businessobject.CheckBase;
024import org.kuali.ole.fp.businessobject.CoinDetail;
025import org.kuali.ole.fp.businessobject.CurrencyDetail;
026import org.kuali.ole.fp.businessobject.DepositWizardCashieringCheckHelper;
027import org.kuali.ole.fp.businessobject.DepositWizardHelper;
028import org.kuali.ole.fp.businessobject.format.CashReceiptDepositTypeFormatter;
029import org.kuali.ole.fp.document.CashReceiptDocument;
030import org.kuali.ole.sys.OLEConstants;
031import org.kuali.ole.sys.OLEKeyConstants;
032import org.kuali.ole.sys.businessobject.Bank;
033import org.kuali.ole.sys.context.SpringContext;
034import org.kuali.ole.sys.service.BankService;
035import org.kuali.rice.core.api.util.type.KualiDecimal;
036import org.kuali.rice.core.web.format.CurrencyFormatter;
037import org.kuali.rice.kns.util.WebUtils;
038import org.kuali.rice.kns.web.struts.form.KualiForm;
039
040/**
041 * This class is the action form for the deposit document wizard.
042 */
043public class DepositWizardForm extends KualiForm {
044    private String cashDrawerCampusCode;
045    private String cashManagementDocId;
046
047    private List<CashReceiptDocument> depositableCashReceipts;
048    private List depositWizardHelpers;
049    private List<Check> depositableCashieringChecks;
050    private transient List<DepositWizardCashieringCheckHelper> depositWizardCashieringCheckHelpers;
051    private List<CashReceiptDocument> checkFreeCashReceipts;
052
053    // Deposit fields
054    private Bank bank;
055    private String bankCode;
056
057    private String depositTypeCode;
058    private String depositTicketNumber;
059
060    private CurrencyDetail currencyDetail;
061    private CoinDetail coinDetail;
062    
063    private KualiDecimal targetDepositAmount;
064    private KualiDecimal currentCheckTotal;
065    
066    // carried over editing modes and document actions to make the bank tags happy
067    protected Map editingMode;
068    protected Map documentActions;
069    
070    protected String noVerifiedCashErrorMessage = "";
071
072    /**
073     * Constructs a DepositWizardForm class instance.
074     */
075    public DepositWizardForm() {
076        depositableCashReceipts = new ArrayList();
077        depositableCashieringChecks = new ArrayList<Check>();
078        depositWizardHelpers = new ArrayList();
079        depositWizardCashieringCheckHelpers = new ArrayList<DepositWizardCashieringCheckHelper>();
080
081        setFormatterType("depositTypeCode", CashReceiptDepositTypeFormatter.class);
082        setDefaultBankCode();
083    }
084    
085    /**
086     * Sets the bank code for a new deposit to the setup default for the Cash Management document.
087     */
088    public void setDefaultBankCode() {
089        Bank defaultBank = SpringContext.getBean(BankService.class).getDefaultBankByDocType(OLEConstants.FinancialDocumentTypeCodes.CASH_MANAGEMENT);
090        if (defaultBank != null) {
091            this.bankCode = defaultBank.getBankCode();
092            this.bank = defaultBank;
093        }
094    }
095
096    /**
097     * @return current value of cashManagementDocId.
098     */
099    public String getCashManagementDocId() {
100        return cashManagementDocId;
101    }
102
103    /**
104     * Sets the cashManagementDocId attribute value.
105     * 
106     * @param cashManagementDocId The cashManagementDocId to set.
107     */
108    public void setCashManagementDocId(String cashManagementDocId) {
109        this.cashManagementDocId = cashManagementDocId;
110    }
111
112
113    /**
114     * @param depositTypeCode
115     */
116    public void setDepositTypeCode(String depositTypeCode) {
117        this.depositTypeCode = depositTypeCode;
118    }
119
120    /**
121     * @return depositTypeCode
122     */
123    public String getDepositTypeCode() {
124        return depositTypeCode;
125    }
126
127    /**
128     * Hack to make the translated depositTypeCode more readily available to the JSP
129     * 
130     * @return translated depositTypeCode
131     */
132    public String getDepositTypeString() {
133        CashReceiptDepositTypeFormatter f = new CashReceiptDepositTypeFormatter();
134        return (String) f.format(getDepositTypeCode());
135    }
136
137
138    /**
139     * @return List
140     */
141    public List<CashReceiptDocument> getDepositableCashReceipts() {
142        return depositableCashReceipts;
143    }
144
145    /**
146     * @param cashReceiptsReadyForDeposit
147     */
148    public void setDepositableCashReceipts(List cashReceiptsReadyForDeposit) {
149        this.depositableCashReceipts = cashReceiptsReadyForDeposit;
150    }
151
152    public CashReceiptDocument getDepositableCashReceipt(int i) {
153        while (depositableCashReceipts.size() <= i) {
154            depositableCashReceipts.add(new CashReceiptDocument());
155        }
156
157        return (CashReceiptDocument) depositableCashReceipts.get(i);
158    }
159
160    /**
161     * @return ArrayList
162     */
163    public List getDepositWizardHelpers() {
164        return depositWizardHelpers;
165    }
166
167    /**
168     * @param depositWizardHelpers
169     */
170    public void setDepositWizardHelpers(List depositWizardHelpers) {
171        this.depositWizardHelpers = depositWizardHelpers;
172    }
173
174    /**
175     * This method retrieves whether the cash receipt ID at the specified index will be selected or not.
176     * 
177     * @param index
178     * @return DepositWizarHelper
179     */
180    public DepositWizardHelper getDepositWizardHelper(int index) {
181        while (this.depositWizardHelpers.size() <= index) {
182            this.depositWizardHelpers.add(new DepositWizardHelper());
183            // default to no check
184        }
185        return (DepositWizardHelper) this.depositWizardHelpers.get(index);
186    }
187
188
189    /**
190     * @return current value of cashDrawerCampusCode.
191     */
192    public String getCashDrawerCampusCode() {
193        return cashDrawerCampusCode;
194    }
195
196    /**
197     * Sets the cashDrawerCampusCode attribute value.
198     * 
199     * @param cashDrawerCampusCode The cashDrawerCampusCode to set.
200     */
201    public void setCashDrawerCampusCode(String cashDrawerVerificationUnit) {
202        this.cashDrawerCampusCode = cashDrawerVerificationUnit;
203    }
204
205
206    /**
207     * @return current value of bankCode.
208     */
209    public String getBankCode() {
210        return bankCode;
211    }
212
213    /**
214     * Sets the bankCode attribute value.
215     * 
216     * @param bankCode The bankCode to set.
217     */
218    public void setBankCode(String bankCode) {
219        this.bankCode = bankCode;
220    }
221
222    /**
223     * @return current value of depositTicketNumber.
224     */
225    public String getDepositTicketNumber() {
226        return depositTicketNumber;
227    }
228
229    /**
230     * Sets the depositTicketNumber attribute value.
231     * 
232     * @param depositTicketNumber The depositTicketNumber to set.
233     */
234    public void setDepositTicketNumber(String depositTicketNumber) {
235        this.depositTicketNumber = depositTicketNumber;
236    }
237
238    /**
239     * @return current value of bank.
240     */
241    public Bank getBank() {
242        return bank;
243    }
244
245    /**
246     * Sets the bank attribute value.
247     * 
248     * @param bank The bank to set.
249     */
250    public void setBank(Bank bank) {
251        this.bank = bank;
252    }
253
254    /**
255     * Gets the coinDetail attribute.
256     * 
257     * @return Returns the coinDetail.
258     */
259    public CoinDetail getCoinDetail() {
260        return coinDetail;
261    }
262
263
264    /**
265     * Sets the coinDetail attribute value.
266     * 
267     * @param coinDetail The coinDetail to set.
268     */
269    public void setCoinDetail(CoinDetail coinDetail) {
270        this.coinDetail = coinDetail;
271    }
272
273
274    /**
275     * Gets the currencyDetail attribute.
276     * 
277     * @return Returns the currencyDetail.
278     */
279    public CurrencyDetail getCurrencyDetail() {
280        return currencyDetail;
281    }
282
283
284    /**
285     * Sets the currencyDetail attribute value.
286     * 
287     * @param currencyDetail The currencyDetail to set.
288     */
289    public void setCurrencyDetail(CurrencyDetail currencyDetail) {
290        this.currencyDetail = currencyDetail;
291    }
292    
293    /**
294     * Retrieves the summed total amount in a currency format with commas.
295     * 
296     * @return String
297     */
298    public String getCurrencyFormattedSumTotalAmount() {
299        return (String) new CurrencyFormatter().format(getTotalDollarAmount());
300    }
301    
302    /**
303     * This method returns the overall total of the document - coin plus check plus cash.
304     * 
305     * @return KualiDecimal
306     */
307    public KualiDecimal getTotalDollarAmount() {
308        KualiDecimal sumTotalAmount = getTotalCoinAmount().add(getTotalCashAmount());
309        return sumTotalAmount;
310    }
311    
312    /**
313     * Gets the totalCoinAmount attribute.
314     * 
315     * @return Returns the totalCoinAmount.
316     */
317    public KualiDecimal getTotalCoinAmount() {
318        return (coinDetail != null) ? coinDetail.getTotalAmount() : KualiDecimal.ZERO;
319    }
320    
321    /**
322     * Gets the totalCashAmount attribute.
323     * 
324     * @return Returns the totalCashAmount.
325     */
326    public KualiDecimal getTotalCashAmount() {
327        return (currencyDetail != null) ? currencyDetail.getTotalAmount() : KualiDecimal.ZERO;
328    }
329
330    /**
331     * Explains if this deposit form is for creating a final deposit or not
332     * 
333     * @return true if this deposit form will create a final deposit, false if it will create an interim
334     */
335    public boolean isDepositFinal() {
336        return (depositTypeCode.equals(OLEConstants.DepositConstants.DEPOSIT_TYPE_FINAL));
337    }
338
339    /**
340     * Gets the depositableCashieringChecks attribute.
341     * 
342     * @return Returns the depositableCashieringChecks.
343     */
344    public List<Check> getDepositableCashieringChecks() {
345        return depositableCashieringChecks;
346    }
347
348    /**
349     * Sets the depositableCashieringChecks attribute value.
350     * 
351     * @param depositableCashieringChecks The depositableCashieringChecks to set.
352     */
353    public void setDepositableCashieringChecks(List<Check> depositableCashieringChecks) {
354        this.depositableCashieringChecks = depositableCashieringChecks;
355    }
356
357    /**
358     * Return the deposit cashiering check at the given index
359     * 
360     * @param index index of check to retrieve
361     * @return a check
362     */
363    public Check getDepositableCashieringCheck(int index) {
364        while (this.depositableCashieringChecks.size() <= index) {
365            this.depositableCashieringChecks.add(new CheckBase());
366        }
367        return this.depositableCashieringChecks.get(index);
368    }
369
370    /**
371     * Gets the depositWizardCashieringCheckHelpers attribute.
372     * 
373     * @return Returns the depositWizardCashieringCheckHelpers.
374     */
375    public List<DepositWizardCashieringCheckHelper> getDepositWizardCashieringCheckHelpers() {
376        return depositWizardCashieringCheckHelpers;
377    }
378
379    /**
380     * Gets the checkFreeCashReceipts attribute.
381     * 
382     * @return Returns the checkFreeCashReceipts.
383     */
384    public List<CashReceiptDocument> getCheckFreeCashReceipts() {
385        return checkFreeCashReceipts;
386    }
387
388
389    /**
390     * Sets the checkFreeCashReceipts attribute value.
391     * 
392     * @param checkFreeCashReceipts The checkFreeCashReceipts to set.
393     */
394    public void setCheckFreeCashReceipts(List<CashReceiptDocument> checkFreeCashReceipts) {
395        this.checkFreeCashReceipts = checkFreeCashReceipts;
396    }
397
398    /**
399     * Retreive a single check free cash receipt
400     * 
401     * @param index the index of the cash receipt
402     * @return a cash receipt document
403     */
404    public CashReceiptDocument getCheckFreeCashReceipt(int index) {
405        while (this.checkFreeCashReceipts.size() <= index) {
406            this.checkFreeCashReceipts.add(new CashReceiptDocument());
407        }
408        return this.checkFreeCashReceipts.get(index);
409    }
410
411    /**
412     * Sets the depositWizardCashieringCheckHelpers attribute value.
413     * 
414     * @param depositWizardCashieringCheckHelpers The depositWizardCashieringCheckHelpers to set.
415     */
416    public void setDepositWizardCashieringCheckHelpers(List<DepositWizardCashieringCheckHelper> depositWizardCashieringCheckHelpers) {
417        this.depositWizardCashieringCheckHelpers = depositWizardCashieringCheckHelpers;
418    }
419
420    public DepositWizardCashieringCheckHelper getDepositWizardCashieringCheckHelper(int index) {
421        while (this.depositWizardCashieringCheckHelpers.size() <= index) {
422            this.depositWizardCashieringCheckHelpers.add(new DepositWizardCashieringCheckHelper());
423        }
424        return this.depositWizardCashieringCheckHelpers.get(index);
425    }
426
427    /**
428     * Gets the documentActions attribute. 
429     * @return Returns the documentActions.
430     */
431    public Map getDocumentActions() {
432        return documentActions; 
433    }
434
435    /**
436     * Sets the documentActions attribute value.
437     * @param documentActions The documentActions to set.
438     */
439    public void setDocumentActions(Map documentActions) {
440        this.documentActions = documentActions;
441    }
442
443    /**
444     * Gets the editingMode attribute. 
445     * @return Returns the editingMode.
446     */
447    public Map getEditingMode() {
448        return editingMode;
449    }
450
451    /**
452     * Sets the editingMode attribute value.
453     * @param editingMode The editingMode to set.
454     */
455    public void setEditingMode(Map editingMode) {
456        this.editingMode = editingMode;
457    }
458    
459    /**
460     * @return the total target amount which needs deposited
461     */
462    public KualiDecimal getTargetDepositAmount() {
463        return targetDepositAmount;
464    }
465
466    /**
467     * Adds a cash receipt to the target total amount for the given deposit (if the deposit was final)
468     * The amount to add is: confirmed check amount + confirmed currency amount + confirmed coin amount - change request
469     * currency amount - change request coin amount
470     * @param crDoc
471     */
472    public void addCashReceiptToTargetTotal(CashReceiptDocument crDoc) {
473        if (targetDepositAmount == null) {
474            targetDepositAmount = KualiDecimal.ZERO;
475        }
476        if (crDoc.getFinancialSystemDocumentHeader().getFinancialDocumentStatusCode().equals(OLEConstants.DocumentStatusCodes.CashReceipt.VERIFIED) || crDoc.getFinancialSystemDocumentHeader().getFinancialDocumentStatusCode().equals(OLEConstants.DocumentStatusCodes.CashReceipt.FINAL)) {
477            targetDepositAmount = targetDepositAmount.add(crDoc.getTotalConfirmedCheckAmount());
478        }
479        targetDepositAmount = targetDepositAmount.add(crDoc.getConfirmedCurrencyDetail().getTotalAmount()).subtract(crDoc.getChangeCurrencyDetail().getTotalAmount()).add(crDoc.getConfirmedCoinDetail().getTotalAmount()).subtract(crDoc.getChangeCoinDetail().getTotalAmount());
480    }
481
482    /**
483     * Adds total undeposited cashiering transaction amount to the target total
484     * @param transaction
485     */
486    public void addCashieringTransactionToTargetTotal(KualiDecimal undepositedCheckAmount) {
487        if (targetDepositAmount == null) {
488            targetDepositAmount = KualiDecimal.ZERO;
489        }
490        targetDepositAmount = targetDepositAmount.subtract(undepositedCheckAmount);
491    }
492
493    /**
494     * @return the current amount for checks needing depositing
495     */
496    public KualiDecimal getCurrentCheckTotal() {
497        return currentCheckTotal;
498    }
499
500    /**
501     * Adds the check total for a cash receipt to checks
502     * @param crDoc
503     */
504    public void addCashReceiptToChecks(CashReceiptDocument crDoc) {
505        if (currentCheckTotal == null) {
506            currentCheckTotal = KualiDecimal.ZERO;
507        }
508        currentCheckTotal = currentCheckTotal.add(crDoc.getTotalConfirmedCheckAmount());
509    }
510
511    /**
512     * @return the no verified cash error message
513     */
514    public String getNoVerifiedCashErrorMessage() {
515        String errorMessage = WebUtils.getKualiConfigurationService().getPropertyValueAsString(OLEKeyConstants.CashManagement.ERROR_NO_VERIFIED_CASH);
516        errorMessage = errorMessage.replace("{0}", getCashDrawerCampusCode());
517        errorMessage = errorMessage.replace("{1}", getCashManagementDocId());
518        return WebUtils.filterHtmlAndReplaceRiceMarkup(errorMessage);
519    }
520
521    /**
522     * @param noVerifiedCashErrorMessage the noVerifiedCashErrorMessage to set
523     */
524    public void setNoVerifiedCashErrorMessage(String noVerifiedCashErrorMessage) {
525        this.noVerifiedCashErrorMessage = noVerifiedCashErrorMessage;
526    }
527    
528    
529}