001    /**
002     * Copyright 2005-2012 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    package edu.sampleu.travel.approval.dataobject;
017    
018    import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
019    import java.sql.Timestamp;
020    import java.util.LinkedHashMap;
021    import javax.persistence.Column;
022    import javax.persistence.Entity;
023    import javax.persistence.GeneratedValue;
024    import javax.persistence.Id;
025    import javax.persistence.JoinColumn;
026    import javax.persistence.ManyToOne;
027    import javax.persistence.SequenceGenerator;
028    import javax.persistence.Table;
029    import org.kuali.rice.krad.util.KRADPropertyConstants;
030    //import org.kuali.kfs.module.tem.TemConstants.TravelParameters;
031    //import org.kuali.kfs.sys.KFSConstants;
032    //import org.kuali.kfs.sys.context.SpringContext;
033    //import org.kuali.rice.kns.service.ParameterService;
034    import org.kuali.rice.core.api.util.type.KualiDecimal;
035    import org.kuali.rice.krad.util.ObjectUtils;
036    
037    @Entity
038    @Table(name="TRVL_PER_DIEM_EXP_T")
039    public class PerDiemExpense extends PersistableBusinessObjectBase {
040    
041        @Id
042        @GeneratedValue(generator = "TEM_PER_DIEM_EXP_ID_SEQ")
043        @SequenceGenerator(name = "TEM_PER_DIEM_EXP_ID_SEQ", sequenceName = "TEM_PER_DIEM_EXP_ID_SEQ", allocationSize = 5)
044        @Column(name = "ID", nullable = false)
045        private Integer id;
046        private String documentNumber;
047        private String countryState;
048        private String county;
049        private String primaryDestination;
050        //boolean flag if it gets disabled due to duplicate
051        private Boolean breakfast = Boolean.TRUE;
052        private Boolean lunch = Boolean.TRUE;
053        private Boolean dinner = Boolean.TRUE;
054        private Boolean personal = Boolean.FALSE;
055        private Boolean incidentalsWithMealsOnly;
056    
057        private KualiDecimal breakfastValue = KualiDecimal.ZERO;
058        private KualiDecimal lunchValue = KualiDecimal.ZERO;
059        private KualiDecimal dinnerValue = KualiDecimal.ZERO;
060        private KualiDecimal incidentalsValue = KualiDecimal.ZERO;
061    
062        @Column(name = "per_diem_id")
063        private Integer perDiemId;
064    
065        @ManyToOne
066        @JoinColumn(name = "per_diem_id")
067        //private PerDiem perDiem;
068    
069        private Integer miles = new Integer(0);
070        private Timestamp mileageDate;
071        //private MileageRate mileageRate;
072        private Integer mileageRateId;
073    
074        private String accommodationTypeCode;
075        private String accommodationName;
076        private String accommodationPhoneNum;
077        private String accommodationAddress;
078        //private AccommodationType accommodationType;
079    
080        private KualiDecimal lodging = KualiDecimal.ZERO;
081        private boolean prorated = false;
082    
083        public Integer getId() {
084            return id;
085        }
086    
087        public void setId(Integer id) {
088            this.id = id;
089        }
090    
091        @Column(name = "FDOC_NBR", length = 14, nullable = false)
092        public String getDocumentNumber() {
093            return documentNumber;
094        }
095    
096        public void setDocumentNumber(String documentNumber) {
097            this.documentNumber = documentNumber;
098        }
099    
100        @Column(name = "COUNTRY", length = 100, nullable = false)
101        public String getCountryState() {
102            return countryState;
103        }
104    
105        public void setCountryState(String countryState) {
106            this.countryState = countryState;
107        }
108    
109        @Column(name = "COUNTY_CD", length = 100, nullable = false)
110        public String getCounty() {
111            return county;
112        }
113    
114        public void setCounty(String county) {
115            this.county = county;
116        }
117    
118        @Column(name = "PRI_DEST", length = 100, nullable = false)
119        public String getPrimaryDestination() {
120            return primaryDestination;
121        }
122    
123        public void setPrimaryDestination(String primaryDestination) {
124            this.primaryDestination = primaryDestination;
125        }
126    
127        @Column(name = "BREAKFAST_IND", nullable = false, length = 1)
128        public Boolean getBreakfast() {
129            return breakfast;
130        }
131    
132        public void setBreakfast(Boolean breakfast) {
133            this.breakfast = breakfast;
134        }
135    
136        @Column(name = "LUNCH_IND", nullable = false, length = 1)
137        public Boolean getLunch() {
138            return lunch;
139        }
140    
141        public void setLunch(Boolean lunch) {
142            this.lunch = lunch;
143        }
144    
145        @Column(name = "DINNER_IND", nullable = false, length = 1)
146        public Boolean getDinner() {
147            return dinner;
148        }
149    
150        public void setDinner(Boolean dinner) {
151            this.dinner = dinner;
152        }
153    
154        /**
155         * Gets the perDiemId attribute.
156         *
157         * @return Returns the perDiemId.
158         */
159        public Integer getPerDiemId() {
160            return perDiemId;
161        }
162    
163        /**
164         * Sets the perDiemId attribute value.
165         *
166         * @param perDiemId The perDiemId to set.
167         */
168        public void setPerDiemId(Integer perDiemId) {
169            this.perDiemId = perDiemId;
170        }
171    
172        /**
173         * Gets the perDiem attribute.
174         *
175         * @return Returns the perDiem.
176         */
177        /**
178        public PerDiem getPerDiem() {
179            return perDiem;
180        }
181          */
182        /**
183         * Sets the perDiem attribute value.
184         *
185         * @param perDiem The perDiem to set.
186         */
187        /**
188        public void setPerDiem(PerDiem perDiem) {
189            this.perDiem = perDiem;
190    
191            setupCustomPerDiem();
192        }
193           */
194        /**
195         * This method gets the accommodation type code associated with this day
196         *
197         * @return accoomodation type code
198         */
199        @Column(name = "ACCOM_TYP_CD", length = 4)
200        public String getAccommodationTypeCode() {
201            return accommodationTypeCode;
202        }
203    
204        /**
205         * This method sets the accommodation type code for this day
206         *
207         * @param accommodationTypeCode
208         */
209        public void setAccommodationTypeCode(String accommodationTypeCode) {
210            this.accommodationTypeCode = accommodationTypeCode;
211        }
212    
213        /**
214         * This method returns the current accommodation Type for this TripDetailEstimate
215         *
216         * @return accommodation Type
217         */
218        /**
219        @ManyToOne
220        @JoinColumn(name = "ACCOM_TYP_CD")
221        public AccommodationType getAccommodationType() {
222            return accommodationType;
223        }
224            /*
225        /**
226         * This method is only used for when the object is initially populated from OJB
227         *
228         * @param accommodationType
229         */
230        /**
231        public void setAccommodationType(AccommodationType accommodationType) {
232            this.accommodationType = accommodationType;
233        }
234            */
235        /**
236         * This method returns the name of the accommodation for this day
237         *
238         * @return accommodation name
239         */
240        @Column(name = "ACCOM_NM")
241        public String getAccommodationName() {
242            return accommodationName;
243        }
244    
245        /**
246         * This method sets the accommodation name for this day
247         *
248         * @param accommodationName
249         */
250        public void setAccommodationName(String accommodationName) {
251            this.accommodationName = accommodationName;
252        }
253    
254        /**
255         * This method returns the accommodation's phone
256         *
257         * @return the phone number for the accommodation
258         */
259        @Column(name = "ACCOM_PH")
260        public String getAccommodationPhoneNum() {
261            return accommodationPhoneNum;
262        }
263    
264        /**
265         * This method sets the accommodation phone number
266         *
267         * @param accommodationPhoneNum
268         */
269        public void setAccommodationPhoneNum(String accommodationPhoneNum) {
270            this.accommodationPhoneNum = accommodationPhoneNum;
271        }
272    
273        /**
274         * This method returns the accommodation's address
275         *
276         * @return accommodation's address
277         */
278        @Column(name = "ACCOM_ADDRESS")
279        public String getAccommodationAddress() {
280            return accommodationAddress;
281        }
282    
283        /**
284         * This method sets the accommodation's address
285         *
286         * @param accommodationAddress
287         */
288        public void setAccommodationAddress(String accommodationAddress) {
289            this.accommodationAddress = accommodationAddress;
290        }
291    
292        /**
293         * This method gets the lodging cost for this day. This method does not take into account personal expenses. Use getLodgingTotal() for that.
294         *
295         * @return lodging cost
296         */
297        @Column(name = "LODGING", precision = 19, scale = 2)
298        public KualiDecimal getLodging() {
299            if (ObjectUtils.isNotNull(lodging) && this.lodging.isGreaterThan(KualiDecimal.ZERO)) {
300                return lodging;
301            }
302    
303            return KualiDecimal.ZERO;
304        }
305    
306        public KualiDecimal getLodging(boolean defaultZero) {
307            return defaultZero? getLodging() : lodging;
308        }
309    
310        /**
311         * This method sets the lodging cost for this day
312         *
313         * @param lodging
314         */
315        public void setLodging(KualiDecimal lodging) {
316            this.lodging = lodging;
317        }
318    
319        @Column(name = "MILES", nullable = false)
320        public Integer getMiles() {
321            if (ObjectUtils.isNotNull(miles) && miles > 0) {
322                return miles;
323            }
324    
325            return 0;
326        }
327    
328        public Integer getMiles(boolean defaultZero) {
329            return defaultZero? getMiles() : miles;
330        }
331    
332        public void setMiles(Integer miles) {
333            this.miles = miles;
334        }
335    
336        /**
337         * Gets the breakfastValue attribute.
338         *
339         * @return Returns the breakfastValue.
340         */
341        public KualiDecimal getBreakfastValue() {
342            if (ObjectUtils.isNotNull(breakfastValue) && this.breakfastValue.isGreaterThan(KualiDecimal.ZERO)) {
343                return breakfastValue;
344            }
345    
346            return KualiDecimal.ZERO;
347        }
348    
349        public KualiDecimal getBreakfastValue(boolean defaultZero) {
350            return defaultZero? getBreakfastValue() : breakfastValue;
351        }
352    
353        /**
354         * Sets the breakfastValue attribute value.
355         *
356         * @param breakfastValue The breakfastValue to set.
357         */
358        public void setBreakfastValue(KualiDecimal breakfastValue) {
359            this.breakfastValue = breakfastValue;
360        }
361    
362        /**
363         * Gets the lunchValue attribute.
364         *
365         * @return Returns the lunchValue.
366         */
367        public KualiDecimal getLunchValue() {
368            if (ObjectUtils.isNotNull(lunchValue) && this.lunchValue.isGreaterThan(KualiDecimal.ZERO)) {
369                return lunchValue;
370            }
371    
372            return KualiDecimal.ZERO;
373        }
374    
375        public KualiDecimal getLunchValue(boolean defaultZero) {
376            return defaultZero? getLunchValue() : lunchValue;
377        }
378    
379        /**
380         * Sets the lunchValue attribute value.
381         *
382         * @param lunchValue The lunchValue to set.
383         */
384        public void setLunchValue(KualiDecimal lunchValue) {
385            this.lunchValue = lunchValue;
386        }
387    
388        /**
389         * Gets the dinnerValue attribute.
390         *
391         * @return Returns the dinnerValue.
392         */
393        public KualiDecimal getDinnerValue() {
394            if (ObjectUtils.isNotNull(dinnerValue) && this.dinnerValue.isGreaterThan(KualiDecimal.ZERO)) {
395                return dinnerValue;
396            }
397    
398            return KualiDecimal.ZERO;
399        }
400    
401        public KualiDecimal getDinnerValue(boolean defaultZero) {
402            return defaultZero? getDinnerValue() : dinnerValue;
403        }
404    
405        /**
406         * Sets the dinnerValue attribute value.
407         *
408         * @param dinnerValue The dinnerValue to set.
409         */
410        public void setDinnerValue(KualiDecimal dinnerValue) {
411            this.dinnerValue = dinnerValue;
412        }
413        /**
414        @ManyToOne
415        @JoinColumn(name = "MILEAGE_RT_ID")
416        public MileageRate getMileageRate() {
417            return mileageRate;
418        }
419    
420        public void setMileageRate(MileageRate mileageRate) {
421            this.mileageRate = mileageRate;
422        }
423         */
424        @Column(name = "MILEAGE_RT_ID", precision = 19, scale = 2, nullable = false)
425        public Integer getMileageRateId() {
426            return mileageRateId;
427        }
428    
429        public void setMileageRateId(Integer mileageRateId) {
430            this.mileageRateId = mileageRateId;
431        }
432    
433        @Column(name = "MILEAGE_TOT", precision = 19, scale = 2, nullable = false)
434        public KualiDecimal getMileageTotal() {
435            KualiDecimal total = KualiDecimal.ZERO;
436            if (!personal) {
437                if (ObjectUtils.isNotNull(this.mileageRateId) && ObjectUtils.isNotNull(this.miles) && this.miles > 0) {
438                    this.refreshReferenceObject("mileageRate");
439                    //total = new KualiDecimal(miles).multiply(mileageRate.getRate());
440                }
441            }
442    
443            return total;
444        }
445    
446        public KualiDecimal getDailyTotal() {
447            KualiDecimal total = KualiDecimal.ZERO;
448            if (!personal) {
449                total = total.add(this.getMileageTotal());
450                total = total.add(this.getLodging());
451                total = total.add(getMealsAndIncidentals());
452            }
453    
454            return total;
455        }
456    
457        /**
458         *
459         * This method gets the mealsTotal with
460         * @return
461         */
462        public KualiDecimal getMealsTotal() {
463            KualiDecimal total = KualiDecimal.ZERO;
464            if (!personal) {
465                if (breakfast) {
466                    total = total.add(this.getBreakfastValue());
467                }
468                if (lunch) {
469                    total = total.add(this.getLunchValue());
470                }
471                if (dinner) {
472                    total = total.add(this.getDinnerValue());
473                }
474            }
475            return total;
476        }
477    
478        /**
479         *
480         * This method gets the Lodging Total if it is not a personal expense.
481         * @return
482         */
483        public KualiDecimal getLodgingTotal() {
484            if (!personal) {
485                return this.getLodging();
486            }
487            return KualiDecimal.ZERO;
488        }
489    
490        /**
491         * Retrieve the Mileage date of per diem
492         *
493         * CLEANUP: mileage date should be the per diem expense date
494         *
495         * @return mileage date
496         */
497        @Column(name = "MLG_DT")
498        public Timestamp getMileageDate() {
499            return mileageDate;
500        }
501    
502        public KualiDecimal getMealsAndIncidentals() {
503            KualiDecimal total = KualiDecimal.ZERO;
504            if (!personal) {
505                total = total.add(getMealsTotal());
506                total = total.add(getIncidentalsValue());
507            }
508            return total;
509        }
510    
511    
512        public static KualiDecimal calculateMealsAndIncidentalsProrated(KualiDecimal total, Integer perDiemPercent) {
513            KualiDecimal percent = new KualiDecimal(perDiemPercent).divide(new KualiDecimal(100));
514            total = total.multiply(percent);
515            return total;
516        }
517    
518        /**
519         *
520         * This method split mealsAndIncidentals into breakfast, lunch, dinner and incidentals (overriding the existing breakfast, lunch, dinner and incidentals values).
521         * @param mealsAndIncidentals
522         */
523        public void setMealsAndIncidentals(KualiDecimal mealsAndIncidentals) {
524            KualiDecimal meal = mealsAndIncidentals.divide(new KualiDecimal(4));
525            setBreakfastValue(meal);
526            setLunchValue(meal);
527            setDinnerValue(meal);
528            setIncidentalsValue(mealsAndIncidentals.subtract(getMealsTotal()));
529        }
530    
531        /**
532         * This method returns whether or not this is a personal expense or not (true for personal expense, false otherwise)
533         *
534         * @return true for personal expense, false otherwise
535         */
536        @Column(name = "PERSONAL", nullable = false, length = 1)
537        public Boolean getPersonal() {
538            return personal;
539        }
540    
541        /**
542         * This method sets true if this is a personal expense
543         *
544         * @param true for personal expense
545         */
546        public void setPersonal(Boolean personal) {
547            this.personal = personal;
548        }
549    
550        /**
551         * Assign the Mileage date of per diem
552         *
553         * @param mileageDate as Date
554         */
555        public void setMileageDate(final Timestamp mileageDate) {
556            this.mileageDate = mileageDate;
557        }
558    
559        protected LinkedHashMap toStringMapper() {
560            LinkedHashMap map = new LinkedHashMap();
561            map.put("id", id);
562            map.put("countryState", this.countryState);
563            map.put("county", this.county);
564            map.put("primaryDestination", this.primaryDestination);
565    
566            return map;
567        }
568    
569        /**
570        public KualiDecimal getDefaultMealsAndIncidentals() {
571            KualiDecimal total = KualiDecimal.ZERO;
572            refreshPerDiem();
573            if (!personal && this.perDiem != null) {
574                if (breakfast) {
575                    total = total.add(new KualiDecimal(this.perDiem.getBreakfast()));
576                }
577                if (lunch) {
578                    total = total.add(new KualiDecimal(this.perDiem.getLunch()));
579                }
580                if (dinner) {
581                    total = total.add(new KualiDecimal(this.perDiem.getDinner()));
582                }
583    
584                // finally add in the incidentals
585                if (getIncludeIncidentals()) {
586                    total = total.add(this.perDiem.getIncidentals());
587                }
588            }
589            return total;
590        }
591    
592    
593        public void refreshPerDiem() {
594            if (!isCustomPerDiem()) {
595                this.refreshReferenceObject("perDiem");
596            }
597    
598            setupCustomPerDiem();
599        }
600         */
601        /**
602         * This method generates a custom perDiem if perDiem is null
603    
604        private void setupCustomPerDiem() {
605            if (this.perDiem == null) {
606                this.perDiem = new PerDiem();
607                this.perDiem.setId(TemConstants.CUSTOM_PER_DIEM_ID);
608            }
609        }
610    
611        public boolean isCustomPerDiem() {
612            return this.perDiem == null || this.perDiem.getId() == null ? true : this.perDiem.getId() == TemConstants.CUSTOM_PER_DIEM_ID;
613        }
614         */
615        /**
616         * This method checks for incidentals inclusion to the mealsAndIncidentals calculation.
617         *
618         * @return
619         */
620        /**
621        public Boolean getIncludeIncidentals() {
622            return !(!breakfast && !lunch && !dinner && getIncidentalsWithMealsOnly());
623        }
624         /*
625       /**
626        public Boolean getIncidentalsWithMealsOnly() {
627            if (incidentalsWithMealsOnly == null) {
628                String incidentalsWithMealsOnlyParam = SpringContext.getBean(ParameterService.class).getParameterValue(PARAM_NAMESPACE, DOCUMENT_DTL_TYPE, TravelParameters.INCIDENTALS_WITH_MEALS_ONLY_IND);
629    
630                //incidentalsWithMealsOnly = incidentalsWithMealsOnlyParam != null && incidentalsWithMealsOnlyParam.equals(KFSConstants.DocumentTypeAttributes.INDICATOR_ATTRIBUTE_TRUE_VALUE);
631                incidentalsWithMealsOnly = incidentalsWithMealsOnlyParam != null && incidentalsWithMealsOnlyParam.equals(KFSConstants.ParameterValues.YES);
632            }
633    
634            return incidentalsWithMealsOnly;
635        }
636             */
637        public void setIncidentalsWithMealsOnly(Boolean incidentalsWithMealsOnly) {
638            this.incidentalsWithMealsOnly = incidentalsWithMealsOnly;
639        }
640    
641        /**
642         * This method returns incidentalsValue affected by INCIDENTALS_WITH_MEALS_ONLY_IND parameter
643         *
644         * @return
645         */
646        public KualiDecimal getIncidentalsValue() {
647            //if (getIncludeIncidentals()) {
648                if (ObjectUtils.isNotNull(incidentalsValue) && this.incidentalsValue.isGreaterThan(KualiDecimal.ZERO)) {
649                    return incidentalsValue;
650                }
651            //}
652    
653            return KualiDecimal.ZERO;
654        }
655    
656        public KualiDecimal getIncidentalsValue(boolean defaultZero) {
657            return defaultZero? getIncidentalsValue() : incidentalsValue;
658        }
659    
660        public void setIncidentalsValue(KualiDecimal incidentalsValue) {
661            this.incidentalsValue = incidentalsValue;
662        }
663    
664        public boolean isProrated() {
665            //if(isCustomPerDiem()){
666            //    return false;
667           // }
668    
669            return prorated;
670        }
671    
672        public void setProrated(boolean prorated) {
673            this.prorated = prorated;
674        }
675    }