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