View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.tem.util;
20  
21  import java.math.BigDecimal;
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.kuali.kfs.module.tem.TemPropertyConstants;
29  import org.kuali.kfs.module.tem.businessobject.ActualExpense;
30  import org.kuali.kfs.module.tem.businessobject.ExpenseTypeObjectCode;
31  import org.kuali.kfs.module.tem.businessobject.HistoricalTravelExpense;
32  import org.kuali.kfs.module.tem.businessobject.ImportedExpense;
33  import org.kuali.kfs.module.tem.businessobject.TemExpense;
34  import org.kuali.kfs.module.tem.businessobject.TemProfile;
35  import org.kuali.kfs.module.tem.document.TravelDocument;
36  import org.kuali.kfs.module.tem.service.TravelExpenseService;
37  import org.kuali.kfs.sys.context.SpringContext;
38  import org.kuali.rice.core.api.util.type.KualiDecimal;
39  import org.kuali.rice.krad.service.BusinessObjectService;
40  import org.kuali.rice.krad.util.ObjectUtils;
41  
42  public class ExpenseUtils {
43      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ExpenseUtils.class);
44  
45      public static List<ImportedExpense> convertHistoricalToImportedExpense(List<HistoricalTravelExpense> historicalTravelExpenses, TravelDocument travelDocument){
46          List<ImportedExpense> expenses = new ArrayList<ImportedExpense>();
47          BusinessObjectService service = SpringContext.getBean(BusinessObjectService.class);
48          for (HistoricalTravelExpense historicalTravelExpense : historicalTravelExpenses){
49              int i = 0;
50              historicalTravelExpense.refreshReferenceObject(TemPropertyConstants.CREDIT_CARD_AGENCY);
51              historicalTravelExpense.refreshReferenceObject(TemPropertyConstants.AGENCY_STAGING_DATA);
52              historicalTravelExpense.refreshReferenceObject(TemPropertyConstants.CREDIT_CARD_STAGING_DATA);
53              historicalTravelExpense.getCreditCardAgency().refreshReferenceObject(TemPropertyConstants.TRAVEL_CARD_TYPE);
54  
55              historicalTravelExpense.setDocumentNumber(travelDocument.getDocumentNumber());
56  
57              ImportedExpense importedExpense = new ImportedExpense();
58              importedExpense.setCardType(historicalTravelExpense.getCreditCardAgency().getTravelCardTypeCode());
59  
60              importedExpense.setNonReimbursable(!historicalTravelExpense.getReimbursable());
61              importedExpense.setMissingReceipt(historicalTravelExpense.getMissingReceipt());
62              importedExpense.setExpenseDate(historicalTravelExpense.getTransactionPostingDate());
63              importedExpense.setCurrencyRate(historicalTravelExpense.getCurrencyRate());
64              importedExpense.setConvertedAmount(historicalTravelExpense.getConvertedAmount());
65              importedExpense.setExpenseAmount(historicalTravelExpense.getAmount());
66              importedExpense.setTravelCompanyCodeName(historicalTravelExpense.getTravelCompany());
67              importedExpense.setExpenseTypeCode(historicalTravelExpense.getTravelExpenseTypeCode());
68  
69              final String travelerTypeCode = ObjectUtils.isNull(travelDocument.getTraveler()) ? null : travelDocument.getTraveler().getTravelerTypeCode(); // we shouldn't get here if traveler type is null, but just in case
70              final ExpenseTypeObjectCode travelExpenseTypeCode = SpringContext.getBean(TravelExpenseService.class).getExpenseType(importedExpense.getExpenseTypeCode(), travelDocument.getDocumentTypeName(), travelDocument.getTripTypeCode(), travelerTypeCode);
71  
72              if (travelExpenseTypeCode != null) {
73                  historicalTravelExpense.setDescription(travelExpenseTypeCode.getExpenseType().getName());
74                  importedExpense.setDescription(historicalTravelExpense.getDescription());
75                  importedExpense.setTravelExpenseTypeCode(travelExpenseTypeCode);
76                  importedExpense.setExpenseTypeObjectCodeId(travelExpenseTypeCode.getExpenseTypeObjectCodeId());
77                  importedExpense.setTaxable(travelExpenseTypeCode.isTaxable());
78              }
79  
80              importedExpense.setHistoricalTravelExpenseId(historicalTravelExpense.getId());
81  
82              expenses.add(importedExpense);
83              historicalTravelExpense.setAssigned(true);
84              historicalTravelExpense.setDocumentType(travelDocument.getFinancialDocumentTypeCode());
85          }
86          service.save(historicalTravelExpenses);
87          return expenses;
88      }
89  
90      public static String getDefaultChartCode(TravelDocument document){
91          String defaultChartCode = null;
92          if (document.getTemProfile() == null && document.getTemProfileId() != null) {
93              Map<String, Object> primaryKeys = new HashMap<String, Object>();
94              primaryKeys.put(TemPropertyConstants.TemProfileProperties.PROFILE_ID, document.getTemProfileId().toString());
95              TemProfile profile = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(TemProfile.class, primaryKeys);
96              defaultChartCode = profile.getDefaultChartCode();
97          }
98          else if (document.getTemProfile() != null) {
99              defaultChartCode = document.getTemProfile().getDefaultChartCode();
100         }
101 
102         return defaultChartCode;
103     }
104 
105     public static void assignExpense(Long historicalTravelExpenseId, String tripId, String documentNumber, String documentType, boolean isAssigned){
106         BusinessObjectService service = SpringContext.getBean(BusinessObjectService.class);
107         HistoricalTravelExpense historicalTravelExpense = service.findBySinglePrimaryKey(HistoricalTravelExpense.class, historicalTravelExpenseId);
108         historicalTravelExpense.setAssigned(isAssigned);
109         historicalTravelExpense.setTripId(tripId);
110         historicalTravelExpense.setDocumentNumber(documentNumber);
111         historicalTravelExpense.setDocumentType(documentType);
112         service.save(historicalTravelExpense);
113     }
114 
115     public static void calculateMileage(TravelDocument travelDocument, List<ActualExpense> actualExpenses){
116         for (ActualExpense actualExpense : actualExpenses){
117             if (!StringUtils.isBlank(actualExpense.getExpenseTypeCode()) && actualExpense.isMileage()){
118                 KualiDecimal total = KualiDecimal.ZERO;
119                 for (TemExpense detail : actualExpense.getExpenseDetails()){
120                     ActualExpense detailExpense = (ActualExpense) detail;
121                     if (detailExpense.getMileageRate(travelDocument.getEffectiveDateForMileageRate(detailExpense)) != null) {
122                         KualiDecimal mileage = new KualiDecimal(new BigDecimal(detailExpense.getMiles()).multiply(detailExpense.getMileageRate(travelDocument.getEffectiveDateForMileageRate(detailExpense)).getRate()));
123                         detailExpense.setExpenseAmount(mileage);
124                         detailExpense.setConvertedAmount(mileage);
125                         total = total.add(detailExpense.getExpenseAmount());
126                         detailExpense.setCurrencyRate(actualExpense.getCurrencyRate());
127                         detailExpense.setExpenseTypeObjectCodeId(actualExpense.getExpenseTypeObjectCodeId());
128                     }
129                 }
130                 actualExpense.setExpenseAmount(total);
131             }
132         }
133     }
134 
135 }