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 package org.kuali.kfs.module.purap.service.impl; 017 018 import java.util.ArrayList; 019 import java.util.Collection; 020 import java.util.HashMap; 021 import java.util.HashSet; 022 import java.util.List; 023 import java.util.Map; 024 025 import org.kuali.kfs.module.purap.businessobject.AccountsPayableItemBase; 026 import org.kuali.kfs.module.purap.businessobject.CreditMemoAccountRevision; 027 import org.kuali.kfs.module.purap.businessobject.CreditMemoItem; 028 import org.kuali.kfs.module.purap.businessobject.PaymentRequestAccount; 029 import org.kuali.kfs.module.purap.businessobject.PaymentRequestAccountRevision; 030 import org.kuali.kfs.module.purap.businessobject.PaymentRequestItem; 031 import org.kuali.kfs.module.purap.businessobject.PurApAccountingLine; 032 import org.kuali.kfs.module.purap.businessobject.PurApAccountingLineBase; 033 import org.kuali.kfs.module.purap.service.PurapAccountRevisionService; 034 import org.kuali.rice.kns.service.BusinessObjectService; 035 import org.kuali.rice.kns.service.DateTimeService; 036 037 public class PurapAccountRevisionServiceImpl implements PurapAccountRevisionService { 038 private BusinessObjectService businessObjectService; 039 private DateTimeService dateTimeService; 040 041 /** 042 * @see org.kuali.kfs.module.purap.service.PurapAccountHistoryService#savePaymentRequestAccountHistories(java.util.List, 043 * java.lang.Integer, java.lang.String) 044 */ 045 public void savePaymentRequestAccountRevisions(List<PaymentRequestItem> paymentRequestItems, Integer postingYear, String postingPeriodCode) { 046 List<PaymentRequestAccountRevision> accountHistories = new ArrayList<PaymentRequestAccountRevision>(); 047 for (PaymentRequestItem item : paymentRequestItems) { 048 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> currentAcctLineGroups = buildAccountLineGroups(item, postingYear, postingPeriodCode); 049 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> historyAcctLineGroups = buildAccountHistoryGroups(item, postingYear, postingPeriodCode, PaymentRequestAccountRevision.class); 050 HashSet<PurapAccountRevisionGroup> existList = new HashSet<PurapAccountRevisionGroup>(); 051 // handle existing account line changes 052 for (PurapAccountRevisionGroup histGroup : historyAcctLineGroups.keySet()) { 053 PurapAccountRevisionGroup currGroup = currentAcctLineGroups.get(histGroup); 054 if (currGroup != null) { 055 // adjust the amount value 056 histGroup.setChangeAmount(currGroup.getAmount().subtract(histGroup.getAmount())); 057 } 058 else { 059 // negate the amount if acct line is deleted 060 histGroup.setChangeAmount(histGroup.getAmount().negated()); 061 } 062 // build history record and save 063 PaymentRequestAccountRevision history = (PaymentRequestAccountRevision) histGroup.buildRevisionRecord(PaymentRequestAccountRevision.class); 064 history.setAccountRevisionTimestamp(dateTimeService.getCurrentTimestamp()); 065 accountHistories.add(history); 066 existList.add(histGroup); 067 } 068 // handle new accounting lines 069 for (PurapAccountRevisionGroup group : currentAcctLineGroups.keySet()) { 070 if (!existList.contains(group)) { 071 // set change amount same as new amount 072 group.setChangeAmount(group.getAmount()); 073 PaymentRequestAccountRevision history = (PaymentRequestAccountRevision) group.buildRevisionRecord(PaymentRequestAccountRevision.class); 074 history.setAccountRevisionTimestamp(dateTimeService.getCurrentTimestamp()); 075 accountHistories.add(history); 076 } 077 } 078 } 079 businessObjectService.save(accountHistories); 080 } 081 082 public void cancelPaymentRequestAccountRevisions(List<PaymentRequestItem> paymentRequestItems, Integer postingYear, String postingPeriodCode) { 083 List<PaymentRequestAccountRevision> accountHistories = new ArrayList<PaymentRequestAccountRevision>(); 084 for (PaymentRequestItem item : paymentRequestItems) { 085 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> historyAcctLineGroups = buildAccountHistoryGroups(item, postingYear, postingPeriodCode, PaymentRequestAccountRevision.class); 086 // handle existing account line changes 087 for (PurapAccountRevisionGroup histGroup : historyAcctLineGroups.keySet()) { 088 // negate the amount 089 histGroup.setChangeAmount(histGroup.getAmount().negated()); 090 // build history record and save 091 PaymentRequestAccountRevision history = (PaymentRequestAccountRevision) histGroup.buildRevisionRecord(PaymentRequestAccountRevision.class); 092 history.setAccountRevisionTimestamp(dateTimeService.getCurrentTimestamp()); 093 accountHistories.add(history); 094 } 095 } 096 businessObjectService.save(accountHistories); 097 } 098 099 /** 100 * @see org.kuali.kfs.module.purap.service.PurapAccountHistoryService#saveCreditMemoAccountHistories(java.util.List, 101 * java.lang.Integer, java.lang.String) 102 */ 103 public void saveCreditMemoAccountRevisions(List<CreditMemoItem> creditMemoItems, Integer postingYear, String postingPeriodCode) { 104 List<CreditMemoAccountRevision> accountHistories = new ArrayList<CreditMemoAccountRevision>(); 105 for (CreditMemoItem item : creditMemoItems) { 106 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> currentAcctLineGroups = buildAccountLineGroups(item, postingYear, postingPeriodCode); 107 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> historyAcctLineGroups = buildAccountHistoryGroups(item, postingYear, postingPeriodCode, CreditMemoAccountRevision.class); 108 HashSet<PurapAccountRevisionGroup> existList = new HashSet<PurapAccountRevisionGroup>(); 109 // first handle existing account line changes 110 for (PurapAccountRevisionGroup histGroup : historyAcctLineGroups.keySet()) { 111 PurapAccountRevisionGroup currGroup = currentAcctLineGroups.get(histGroup); 112 if (currGroup != null) { 113 // adjust the amount 114 histGroup.setChangeAmount(currGroup.getAmount().subtract(histGroup.getAmount())); 115 } 116 else { 117 // negate the amount if line is deleted 118 histGroup.setChangeAmount(histGroup.getAmount().negated()); 119 } 120 // build history record and save 121 CreditMemoAccountRevision history = (CreditMemoAccountRevision) histGroup.buildRevisionRecord(CreditMemoAccountRevision.class); 122 history.setAccountRevisionTimestamp(dateTimeService.getCurrentTimestamp()); 123 accountHistories.add(history); 124 existList.add(histGroup); 125 } 126 // handle new account lines added 127 for (PurapAccountRevisionGroup group : currentAcctLineGroups.keySet()) { 128 if (!existList.contains(group)) { 129 // set same change amount same as new amount 130 group.setChangeAmount(group.getAmount()); 131 CreditMemoAccountRevision history = (CreditMemoAccountRevision) group.buildRevisionRecord(CreditMemoAccountRevision.class); 132 history.setAccountRevisionTimestamp(dateTimeService.getCurrentTimestamp()); 133 accountHistories.add(history); 134 } 135 } 136 } 137 businessObjectService.save(accountHistories); 138 } 139 140 public void cancelCreditMemoAccountRevisions(List<CreditMemoItem> creditMemoItems, Integer postingYear, String postingPeriodCode) { 141 List<CreditMemoAccountRevision> accountHistories = new ArrayList<CreditMemoAccountRevision>(); 142 for (CreditMemoItem item : creditMemoItems) { 143 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> historyAcctLineGroups = buildAccountHistoryGroups(item, postingYear, postingPeriodCode, CreditMemoAccountRevision.class); 144 // first handle existing account line changes 145 for (PurapAccountRevisionGroup histGroup : historyAcctLineGroups.keySet()) { 146 // negate the amount 147 histGroup.setChangeAmount(histGroup.getAmount().negated()); 148 // build history record and save 149 CreditMemoAccountRevision history = (CreditMemoAccountRevision) histGroup.buildRevisionRecord(CreditMemoAccountRevision.class); 150 history.setAccountRevisionTimestamp(dateTimeService.getCurrentTimestamp()); 151 accountHistories.add(history); 152 } 153 } 154 businessObjectService.save(accountHistories); 155 } 156 157 /** 158 * Builds account history grouping data based on given list of purap account lines 159 * 160 * @param item PurAp Item 161 * @param postingYear Posting year 162 * @param postingPeriodCode Posting period 163 * @return 164 */ 165 protected Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> buildAccountLineGroups(AccountsPayableItemBase item, Integer postingYear, String postingPeriodCode) { 166 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> accountLineGroups = new HashMap<PurapAccountRevisionGroup, PurapAccountRevisionGroup>(); 167 for (PurApAccountingLine account : item.getSourceAccountingLines()) { 168 PurapAccountRevisionGroup lineGroup = new PurapAccountRevisionGroup((PurApAccountingLineBase) account); 169 lineGroup.setPostingYear(postingYear); 170 lineGroup.setPostingPeriodCode(postingPeriodCode); 171 if ((accountLineGroups.get(lineGroup)) == null) { 172 accountLineGroups.put(lineGroup, lineGroup); 173 } 174 else { 175 accountLineGroups.get(lineGroup).combineEntry((PurApAccountingLineBase) account); 176 } 177 } 178 return accountLineGroups; 179 } 180 181 /** 182 * Builds account history group based on existing account history lines 183 * 184 * @param item PurAp item 185 * @param postingYear Posting year 186 * @param postingPeriodCode Posting period code 187 * @param clazz History class 188 * @return Map of account history groups 189 */ 190 protected Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> buildAccountHistoryGroups(AccountsPayableItemBase item, Integer postingYear, String postingPeriodCode, Class<? extends PurApAccountingLineBase> clazz) { 191 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> historyGroups = new HashMap<PurapAccountRevisionGroup, PurapAccountRevisionGroup>(); 192 // find the current sum value from history table and adjusts the amount 193 Map<String, Object> fieldValues = new HashMap<String, Object>(); 194 fieldValues.put("itemIdentifier", item.getItemIdentifier()); 195 Collection<PurApAccountingLineBase> existingAccounts = businessObjectService.findMatching(clazz, fieldValues); 196 if (existingAccounts != null && !existingAccounts.isEmpty()) { 197 for (PurApAccountingLineBase existAcct : existingAccounts) { 198 PurapAccountRevisionGroup historyGroup = new PurapAccountRevisionGroup(existAcct); 199 historyGroup.setPostingYear(postingYear); 200 historyGroup.setPostingPeriodCode(postingPeriodCode); 201 if ((historyGroups.get(historyGroup)) == null) { 202 historyGroups.put(historyGroup, historyGroup); 203 } 204 else { 205 historyGroups.get(historyGroup).combineEntry((PaymentRequestAccount) existAcct); 206 } 207 } 208 } 209 return historyGroups; 210 } 211 212 /** 213 * Gets the businessObjectService attribute. 214 * 215 * @return Returns the businessObjectService. 216 */ 217 public BusinessObjectService getBusinessObjectService() { 218 return businessObjectService; 219 } 220 221 /** 222 * Sets the businessObjectService attribute value. 223 * 224 * @param businessObjectService The businessObjectService to set. 225 */ 226 public void setBusinessObjectService(BusinessObjectService businessObjectService) { 227 this.businessObjectService = businessObjectService; 228 } 229 230 /** 231 * Gets the dateTimeService attribute. 232 * 233 * @return Returns the dateTimeService. 234 */ 235 public DateTimeService getDateTimeService() { 236 return dateTimeService; 237 } 238 239 /** 240 * Sets the dateTimeService attribute value. 241 * 242 * @param dateTimeService The dateTimeService to set. 243 */ 244 public void setDateTimeService(DateTimeService dateTimeService) { 245 this.dateTimeService = dateTimeService; 246 } 247 248 }