001/*
002 * Copyright 2007 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 */
016/*
017 * Created on Jul 12, 2004
018 *
019 */
020package org.kuali.ole.pdp.businessobject;
021
022import java.sql.Date;
023import java.sql.Timestamp;
024import java.text.ParseException;
025import java.util.ArrayList;
026import java.util.Calendar;
027import java.util.LinkedHashMap;
028import java.util.List;
029
030import org.apache.commons.lang.StringUtils;
031import org.kuali.ole.pdp.PdpConstants;
032import org.kuali.ole.pdp.PdpParameterConstants;
033import org.kuali.ole.pdp.service.PaymentGroupService;
034import org.kuali.ole.sys.OLEConstants;
035import org.kuali.ole.sys.OLEPropertyConstants;
036import org.kuali.ole.sys.businessobject.TimestampedBusinessObjectBase;
037import org.kuali.ole.sys.context.SpringContext;
038import org.kuali.rice.core.api.datetime.DateTimeService;
039import org.kuali.rice.core.api.util.type.KualiDecimal;
040import org.kuali.rice.core.api.util.type.KualiInteger;
041import org.kuali.rice.coreservice.framework.parameter.ParameterService;
042import org.kuali.rice.krad.util.ObjectUtils;
043
044public class PaymentDetail extends TimestampedBusinessObjectBase {
045    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PaymentDetail.class);
046    private static KualiDecimal zero = KualiDecimal.ZERO;
047
048    private KualiInteger id;
049    private String invoiceNbr;
050    private Date invoiceDate;
051    private String purchaseOrderNbr;
052    private String custPaymentDocNbr;
053    private String financialSystemOriginCode;
054    private String financialDocumentTypeCode;
055    private String requisitionNbr;
056    private String organizationDocNbr;
057    private String customerInstitutionNumber;
058    private KualiDecimal origInvoiceAmount;
059    private KualiDecimal netPaymentAmount;
060    private KualiDecimal invTotDiscountAmount;
061    private KualiDecimal invTotShipAmount;
062    private KualiDecimal invTotOtherDebitAmount;
063    private KualiDecimal invTotOtherCreditAmount;
064    private Boolean primaryCancelledPayment;
065    private String paymentMethodCode;
066
067    private List<PaymentAccountDetail> accountDetail = new ArrayList<PaymentAccountDetail>();
068    private List<PaymentNoteText> notes = new ArrayList<PaymentNoteText>();
069
070    private KualiInteger paymentGroupId;
071    private PaymentGroup paymentGroup;
072
073    public PaymentDetail() {
074        super();
075    }
076
077    public boolean isDetailAmountProvided() {
078        return (origInvoiceAmount != null) || (invTotDiscountAmount != null) || (invTotShipAmount != null) || (invTotOtherDebitAmount != null) || (invTotOtherCreditAmount != null);
079    }
080
081    public KualiDecimal getCalculatedPaymentAmount() {
082        KualiDecimal orig_invoice_amt = origInvoiceAmount == null ? zero : origInvoiceAmount;
083        KualiDecimal invoice_tot_discount_amt = invTotDiscountAmount == null ? zero : invTotDiscountAmount;
084        KualiDecimal invoice_tot_ship_amt = invTotShipAmount == null ? zero : invTotShipAmount;
085        KualiDecimal invoice_tot_other_debits = invTotOtherDebitAmount == null ? zero : invTotOtherDebitAmount;
086        KualiDecimal invoice_tot_other_credits = invTotOtherCreditAmount == null ? zero : invTotOtherCreditAmount;
087
088        KualiDecimal t = orig_invoice_amt.subtract(invoice_tot_discount_amt);
089        t = t.add(invoice_tot_ship_amt);
090        t = t.add(invoice_tot_other_debits);
091        t = t.subtract(invoice_tot_other_credits);
092
093        return t;
094    }
095
096    /**
097     * Determines if the disbursement date is past the number of days old (configured in system parameter) in which actions can take
098     * place
099     * 
100     * @return true if actions are allowed on disbursement, false otherwise
101     */
102    public boolean isDisbursementActionAllowed() {
103        if (paymentGroup.getDisbursementDate() == null) {
104            if (PdpConstants.PaymentStatusCodes.EXTRACTED.equals(paymentGroup.getPaymentStatus().getCode())) {
105                return false;
106            }
107        return true;
108    }
109
110        String daysStr = SpringContext.getBean(ParameterService.class).getParameterValueAsString(PaymentDetail.class, PdpParameterConstants.DISBURSEMENT_CANCELLATION_DAYS);
111        int days = Integer.valueOf(daysStr);
112
113        Calendar c = Calendar.getInstance();
114        c.add(Calendar.DATE, (days * -1));
115        c.set(Calendar.HOUR, 12);
116        c.set(Calendar.MINUTE, 0);
117        c.set(Calendar.SECOND, 0);
118        c.set(Calendar.MILLISECOND, 0);
119        c.set(Calendar.AM_PM, Calendar.AM);
120        Timestamp lastDisbursementActionDate = new Timestamp(c.getTimeInMillis());
121
122        Calendar c2 = Calendar.getInstance();
123        c2.setTime(paymentGroup.getDisbursementDate());
124        c2.set(Calendar.HOUR, 11);
125        c2.set(Calendar.MINUTE, 59);
126        c2.set(Calendar.SECOND, 59);
127        c2.set(Calendar.MILLISECOND, 59);
128        c2.set(Calendar.AM_PM, Calendar.PM);
129        Timestamp disbursementDate = new Timestamp(c2.getTimeInMillis());
130
131        // date is equal to or after lastActionDate Allowed
132        return ((disbursementDate.compareTo(lastDisbursementActionDate)) >= 0);
133    }
134
135    /**
136     * @return total of all account detail amounts
137     */
138    public KualiDecimal getAccountTotal() {
139        KualiDecimal acctTotal = new KualiDecimal(0.00);
140
141        for (PaymentAccountDetail paymentAccountDetail : accountDetail) {
142            if (paymentAccountDetail.getAccountNetAmount() != null) {
143                acctTotal = acctTotal.add(paymentAccountDetail.getAccountNetAmount());
144            }
145        }
146
147        return acctTotal;
148    }
149
150    public Date getInvoiceDate() {
151        return invoiceDate;
152    }
153
154    public void setInvoiceDate(Date invoiceDate) {
155        this.invoiceDate = invoiceDate;
156    }
157
158    /**
159     * Takes a <code>String</code> and attempt to format as <code>Timestamp</code for setting the
160     * invoiceDate field
161     * 
162     * @param invoiceDate Timestamp as string
163     */
164    public void setInvoiceDate(String invoiceDate) throws ParseException {
165        this.invoiceDate = SpringContext.getBean(DateTimeService.class).convertToSqlDate(invoiceDate);
166    }
167
168    /**
169     * @hibernate.set name="accountDetail"
170     * @hibernate.collection-key column="pmt_dtl_id"
171     * @hibernate.collection-one-to-many class="edu.iu.uis.pdp.bo.PaymentAccountDetail"
172     */
173    public List<PaymentAccountDetail> getAccountDetail() {
174        return accountDetail;
175    }
176
177    public void setAccountDetail(List<PaymentAccountDetail> ad) {
178        accountDetail = ad;
179    }
180
181    public void addAccountDetail(PaymentAccountDetail pad) {
182        pad.setPaymentDetail(this);
183        accountDetail.add(pad);
184    }
185
186    public void deleteAccountDetail(PaymentAccountDetail pad) {
187        accountDetail.remove(pad);
188    }
189
190    public List<PaymentNoteText> getNotes() {
191        return notes;
192    }
193
194    public void setNotes(List<PaymentNoteText> n) {
195        notes = n;
196    }
197
198    public void addNote(PaymentNoteText pnt) {
199        if (!StringUtils.isBlank(pnt.getCustomerNoteText())) {
200            pnt.setPaymentDetail(this);
201            notes.add(pnt);
202        } else {
203            LOG.warn("Did not add note to payment detail build from Document #: "+(!StringUtils.isBlank(custPaymentDocNbr) ? custPaymentDocNbr : "")+" because note was empty");
204        }
205    }
206
207    /**
208     * Constructs a new <code>PaymentNoteText</code> for the given payment text and adds to the detail <code>List</code>
209     * 
210     * @param paymentText note text
211     */
212    public void addPaymentText(String paymentText) {
213        PaymentNoteText paymentNoteText = new PaymentNoteText();
214
215        paymentNoteText.setCustomerNoteText(paymentText);
216        paymentNoteText.setCustomerNoteLineNbr(new KualiInteger(this.notes.size() + 1));
217
218        addNote(paymentNoteText);
219    }
220
221    public void deleteNote(PaymentNoteText pnt) {
222        notes.remove(pnt);
223    }
224
225    /**
226     * @hibernate.id column="PMT_DTL_ID" generator-class="sequence"
227     * @hibernate.generator-param name="sequence" value="PDP.PDP_PMT_DTL_ID_SEQ"
228     * @return
229     */
230    public KualiInteger getId() {
231        return id;
232    }
233
234    /**
235     * @return
236     * @hibernate.property column="CUST_PMT_DOC_NBR" length="9"
237     */
238    public String getCustPaymentDocNbr() {
239        return custPaymentDocNbr;
240    }
241
242    /**
243     * @return
244     * @hibernate.property column="INV_NBR" length="14"
245     */
246    public String getInvoiceNbr() {
247        return invoiceNbr;
248    }
249
250    /**
251     * @return
252     * @hibernate.property column="INV_TOT_DSCT_AMT" length="14"
253     */
254    public KualiDecimal getInvTotDiscountAmount() {
255        return invTotDiscountAmount;
256    }
257
258    /**
259     * @return
260     * @hibernate.property column="INV_TOT_OTHR_CRDT_AMT" length="14"
261     */
262    public KualiDecimal getInvTotOtherCreditAmount() {
263        return invTotOtherCreditAmount;
264    }
265
266    /**
267     * @return
268     * @hibernate.property column="INV_TOT_OTHR_DEBIT_AMT" length="14"
269     */
270    public KualiDecimal getInvTotOtherDebitAmount() {
271        return invTotOtherDebitAmount;
272    }
273
274    /**
275     * @return
276     * @hibernate.property column="INV_TOT_SHP_AMT" length="14"
277     */
278    public KualiDecimal getInvTotShipAmount() {
279        return invTotShipAmount;
280    }
281
282    /**
283     * @return
284     * @hibernate.property column="NET_PMT_AMT" length="14"
285     */
286    public KualiDecimal getNetPaymentAmount() {
287        return netPaymentAmount;
288    }
289
290    /**
291     * @return
292     * @hibernate.property column="ORG_DOC_NBR" length="10"
293     */
294    public String getOrganizationDocNbr() {
295        return organizationDocNbr;
296    }
297
298    /**
299     * @return
300     * @hibernate.property column="ORIG_INV_AMT" length="14"
301     */
302    public KualiDecimal getOrigInvoiceAmount() {
303        return origInvoiceAmount;
304    }
305
306    /**
307     * @return
308     * @hibernate.property column="PO_NBR" length="9"
309     */
310    public String getPurchaseOrderNbr() {
311        return purchaseOrderNbr;
312    }
313
314    /**
315     * @return
316     * @hibernate.property column="REQS_NBR" length=8"
317     */
318    public String getRequisitionNbr() {
319        return requisitionNbr;
320    }
321
322    /**
323     * @return Returns the paymentGroup.
324     */
325    public PaymentGroup getPaymentGroup() {
326        return paymentGroup;
327    }
328
329    /**
330     * @param string
331     */
332    public void setCustPaymentDocNbr(String string) {
333        custPaymentDocNbr = string;
334    }
335
336    /**
337     * @param integer
338     */
339    public void setId(KualiInteger integer) {
340        id = integer;
341    }
342
343    /**
344     * @param string
345     */
346    public void setInvoiceNbr(String string) {
347        invoiceNbr = string;
348    }
349
350    /**
351     * @param decimal
352     */
353    public void setInvTotDiscountAmount(KualiDecimal decimal) {
354        invTotDiscountAmount = decimal;
355    }
356
357    public void setInvTotDiscountAmount(String decimal) {
358        invTotDiscountAmount = new KualiDecimal(decimal);
359    }
360
361    /**
362     * @param decimal
363     */
364    public void setInvTotOtherCreditAmount(KualiDecimal decimal) {
365        invTotOtherCreditAmount = decimal;
366    }
367
368    public void setInvTotOtherCreditAmount(String decimal) {
369        invTotOtherCreditAmount = new KualiDecimal(decimal);
370    }
371
372    /**
373     * @param decimal
374     */
375    public void setInvTotOtherDebitAmount(KualiDecimal decimal) {
376        invTotOtherDebitAmount = decimal;
377    }
378
379    public void setInvTotOtherDebitAmount(String decimal) {
380        invTotOtherDebitAmount = new KualiDecimal(decimal);
381    }
382
383    /**
384     * @param decimal
385     */
386    public void setInvTotShipAmount(KualiDecimal decimal) {
387        invTotShipAmount = decimal;
388    }
389
390    public void setInvTotShipAmount(String decimal) {
391        invTotShipAmount = new KualiDecimal(decimal);
392    }
393
394    /**
395     * @param decimal
396     */
397    public void setNetPaymentAmount(KualiDecimal decimal) {
398        netPaymentAmount = decimal;
399    }
400
401    public void setNetPaymentAmount(String decimal) {
402        netPaymentAmount = new KualiDecimal(decimal);
403    }
404
405    /**
406     * @param string
407     */
408    public void setOrganizationDocNbr(String string) {
409        organizationDocNbr = string;
410    }
411
412    /**
413     * @param decimal
414     */
415    public void setOrigInvoiceAmount(KualiDecimal decimal) {
416        origInvoiceAmount = decimal;
417    }
418
419    public void setOrigInvoiceAmount(String decimal) {
420        origInvoiceAmount = new KualiDecimal(decimal);
421    }
422
423    /**
424     * @param string
425     */
426    public void setPurchaseOrderNbr(String string) {
427        purchaseOrderNbr = string;
428    }
429
430    /**
431     * @param string
432     */
433    public void setRequisitionNbr(String string) {
434        requisitionNbr = string;
435    }
436
437    /**
438     * @return Returns the financialDocumentTypeCode.
439     */
440    public String getFinancialDocumentTypeCode() {
441        return financialDocumentTypeCode;
442    }
443
444    /**
445     * @param financialDocumentTypeCode The financialDocumentTypeCode to set.
446     */
447    public void setFinancialDocumentTypeCode(String financialDocumentTypeCode) {
448        this.financialDocumentTypeCode = financialDocumentTypeCode;
449    }
450
451    /**
452     * @return Returns the primaryCancelledPayment.
453     */
454    public Boolean getPrimaryCancelledPayment() {
455        return primaryCancelledPayment;
456    }
457
458    /**
459     * @param primaryCancelledPayment The primaryCancelledPayment to set.
460     */
461    public void setPrimaryCancelledPayment(Boolean primaryCancelledPayment) {
462        this.primaryCancelledPayment = primaryCancelledPayment;
463    }
464
465    /**
466     * @param paymentGroup The paymentGroup to set.
467     */
468    public void setPaymentGroup(PaymentGroup paymentGroup) {
469        this.paymentGroup = paymentGroup;
470    }
471
472    /**
473     * Gets the paymentGroupId attribute.
474     * 
475     * @return Returns the paymentGroupId.
476     */
477    public KualiInteger getPaymentGroupId() {
478        return paymentGroupId;
479    }
480
481    /**
482     * Sets the paymentGroupId attribute value.
483     * 
484     * @param paymentGroupId The paymentGroupId to set.
485     */
486    public void setPaymentGroupId(KualiInteger paymentGroupId) {
487        this.paymentGroupId = paymentGroupId;
488    }
489
490    /**
491     * Gets the financialSystemOriginCode attribute.
492     * 
493     * @return Returns the financialSystemOriginCode.
494     */
495    public String getFinancialSystemOriginCode() {
496        return financialSystemOriginCode;
497    }
498
499    /**
500     * Sets the financialSystemOriginCode attribute value.
501     * 
502     * @param financialSystemOriginCode The financialSystemOriginCode to set.
503     */
504    public void setFinancialSystemOriginCode(String financialSystemOriginCode) {
505        this.financialSystemOriginCode = financialSystemOriginCode;
506    }
507
508    /**
509     * @return the customerInstitutionNumber
510     */
511    public String getCustomerInstitutionNumber() {
512        return customerInstitutionNumber;
513    }
514
515    /**
516     * @param customerInstitutionNumber the customerInstitutionNumber to set
517     */
518    public void setCustomerInstitutionNumber(String customerInstitutionNumber) {
519        this.customerInstitutionNumber = customerInstitutionNumber;
520    }
521
522    /**
523     * This method returns a String representation of the payment detail notes
524     * 
525     * @return the String representation of the payment detail notes
526     */
527    public String getNotesText() {
528        StringBuffer notes = new StringBuffer();
529        List<PaymentNoteText> notesList = getNotes();
530        for (PaymentNoteText note : notesList) {
531            notes.append(note.getCustomerNoteText());
532            notes.append(OLEConstants.NEWLINE);
533        }
534        return notes.toString();
535    }
536
537    /**
538     * @see org.kuali.rice.krad.bo.BusinessObjectBase#toStringMapper()
539     */
540    
541    protected LinkedHashMap toStringMapper_RICE20_REFACTORME() {
542        LinkedHashMap m = new LinkedHashMap();
543
544        m.put(OLEPropertyConstants.ID, this.id);
545
546        return m;
547    }
548
549    /**
550     * This method returns the number of payments in the payment group associated with this payment detail.
551     * 
552     * @return the number of payments in the payment group
553     */
554    public int getNbrOfPaymentsInPaymentGroup() {
555        return paymentGroup.getPaymentDetails().size();
556    }
557
558    /**
559     * This method returns the number of payments in the disbursement associated with this payment detail.
560     * 
561     * @return the number of payments in the disbursement
562     */
563    public int getNbrOfPaymentsInDisbursement() {
564        
565        int nbrOfPaymentsInDisbursement = 0;
566        if (ObjectUtils.isNotNull((paymentGroup.getDisbursementNbr()))) {
567            List<PaymentGroup> paymentGroupList = SpringContext.getBean(PaymentGroupService.class).getByDisbursementNumber(paymentGroup.getDisbursementNbr().intValue());
568            for (PaymentGroup paymentGroup : paymentGroupList) {
569                nbrOfPaymentsInDisbursement += paymentGroup.getPaymentDetails().size();
570            }
571        }
572        return nbrOfPaymentsInDisbursement;
573    }
574
575    public String getPaymentMethodCode() {
576        return paymentMethodCode;
577    }
578
579    public void setPaymentMethodCode(String paymentMethodCode) {
580        this.paymentMethodCode = paymentMethodCode;
581    }
582}