1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.module.purap.service.impl;
17
18 import org.kuali.ole.module.purap.businessobject.*;
19 import org.kuali.ole.module.purap.service.PurapAccountRevisionService;
20 import org.kuali.rice.core.api.datetime.DateTimeService;
21 import org.kuali.rice.krad.service.BusinessObjectService;
22
23 import java.util.*;
24
25 public class PurapAccountRevisionServiceImpl implements PurapAccountRevisionService {
26 private BusinessObjectService businessObjectService;
27 private DateTimeService dateTimeService;
28
29
30
31
32
33 public void savePaymentRequestAccountRevisions(List<PaymentRequestItem> paymentRequestItems, Integer postingYear, String postingPeriodCode) {
34 List<PaymentRequestAccountRevision> accountHistories = new ArrayList<PaymentRequestAccountRevision>();
35 for (PaymentRequestItem item : paymentRequestItems) {
36 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> currentAcctLineGroups = buildAccountLineGroups(item, postingYear, postingPeriodCode);
37 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> historyAcctLineGroups = buildAccountHistoryGroups(item, postingYear, postingPeriodCode, PaymentRequestAccountRevision.class);
38 HashSet<PurapAccountRevisionGroup> existList = new HashSet<PurapAccountRevisionGroup>();
39
40 for (PurapAccountRevisionGroup histGroup : historyAcctLineGroups.keySet()) {
41 PurapAccountRevisionGroup currGroup = currentAcctLineGroups.get(histGroup);
42 if (currGroup != null) {
43
44 histGroup.setChangeAmount(currGroup.getAmount().subtract(histGroup.getAmount()));
45 } else {
46
47 histGroup.setChangeAmount(histGroup.getAmount().negated());
48 }
49
50 PaymentRequestAccountRevision history = (PaymentRequestAccountRevision) histGroup.buildRevisionRecord(PaymentRequestAccountRevision.class);
51 history.setAccountRevisionTimestamp(dateTimeService.getCurrentTimestamp());
52 accountHistories.add(history);
53 existList.add(histGroup);
54 }
55
56 for (PurapAccountRevisionGroup group : currentAcctLineGroups.keySet()) {
57 if (!existList.contains(group)) {
58
59 group.setChangeAmount(group.getAmount());
60 PaymentRequestAccountRevision history = (PaymentRequestAccountRevision) group.buildRevisionRecord(PaymentRequestAccountRevision.class);
61 history.setAccountRevisionTimestamp(dateTimeService.getCurrentTimestamp());
62 accountHistories.add(history);
63 }
64 }
65 }
66 businessObjectService.save(accountHistories);
67 }
68
69 public void cancelPaymentRequestAccountRevisions(List<PaymentRequestItem> paymentRequestItems, Integer postingYear, String postingPeriodCode) {
70 List<PaymentRequestAccountRevision> accountHistories = new ArrayList<PaymentRequestAccountRevision>();
71 for (PaymentRequestItem item : paymentRequestItems) {
72 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> historyAcctLineGroups = buildAccountHistoryGroups(item, postingYear, postingPeriodCode, PaymentRequestAccountRevision.class);
73
74 for (PurapAccountRevisionGroup histGroup : historyAcctLineGroups.keySet()) {
75
76 histGroup.setChangeAmount(histGroup.getAmount().negated());
77
78 PaymentRequestAccountRevision history = (PaymentRequestAccountRevision) histGroup.buildRevisionRecord(PaymentRequestAccountRevision.class);
79 history.setAccountRevisionTimestamp(dateTimeService.getCurrentTimestamp());
80 accountHistories.add(history);
81 }
82 }
83 businessObjectService.save(accountHistories);
84 }
85
86
87
88
89
90 public void saveInvoiceAccountRevisions(List<InvoiceItem> invoiceItems, Integer postingYear, String postingPeriodCode) {
91 List<InvoiceAccountRevision> accountHistories = new ArrayList<InvoiceAccountRevision>();
92 for (InvoiceItem item : invoiceItems) {
93 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> currentAcctLineGroups = buildAccountLineGroups(item, postingYear, postingPeriodCode);
94 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> historyAcctLineGroups = buildAccountHistoryGroups(item, postingYear, postingPeriodCode, PaymentRequestAccountRevision.class);
95 HashSet<PurapAccountRevisionGroup> existList = new HashSet<PurapAccountRevisionGroup>();
96
97 for (PurapAccountRevisionGroup histGroup : historyAcctLineGroups.keySet()) {
98 PurapAccountRevisionGroup currGroup = currentAcctLineGroups.get(histGroup);
99 if (currGroup != null) {
100
101 histGroup.setChangeAmount(currGroup.getAmount().subtract(histGroup.getAmount()));
102 } else {
103
104 histGroup.setChangeAmount(histGroup.getAmount().negated());
105 }
106
107 InvoiceAccountRevision history = (InvoiceAccountRevision) histGroup.buildRevisionRecord(InvoiceAccountRevision.class);
108 history.setAccountRevisionTimestamp(dateTimeService.getCurrentTimestamp());
109 accountHistories.add(history);
110 existList.add(histGroup);
111 }
112
113 for (PurapAccountRevisionGroup group : currentAcctLineGroups.keySet()) {
114 if (!existList.contains(group)) {
115
116 group.setChangeAmount(group.getAmount());
117 InvoiceAccountRevision history = (InvoiceAccountRevision) group.buildRevisionRecord(InvoiceAccountRevision.class);
118 history.setAccountRevisionTimestamp(dateTimeService.getCurrentTimestamp());
119 accountHistories.add(history);
120 }
121 }
122 }
123 businessObjectService.save(accountHistories);
124 }
125
126 public void cancelInvoiceAccountRevisions(List<InvoiceItem> invoiceItems, Integer postingYear, String postingPeriodCode) {
127 List<InvoiceAccountRevision> accountHistories = new ArrayList<InvoiceAccountRevision>();
128 for (InvoiceItem item : invoiceItems) {
129 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> historyAcctLineGroups = buildAccountHistoryGroups(item, postingYear, postingPeriodCode, PaymentRequestAccountRevision.class);
130
131 for (PurapAccountRevisionGroup histGroup : historyAcctLineGroups.keySet()) {
132
133 histGroup.setChangeAmount(histGroup.getAmount().negated());
134
135 InvoiceAccountRevision history = (InvoiceAccountRevision) histGroup.buildRevisionRecord(InvoiceAccountRevision.class);
136 history.setAccountRevisionTimestamp(dateTimeService.getCurrentTimestamp());
137 accountHistories.add(history);
138 }
139 }
140 businessObjectService.save(accountHistories);
141 }
142
143
144
145
146
147 public void saveCreditMemoAccountRevisions(List<CreditMemoItem> creditMemoItems, Integer postingYear, String postingPeriodCode) {
148 List<CreditMemoAccountRevision> accountHistories = new ArrayList<CreditMemoAccountRevision>();
149 for (CreditMemoItem item : creditMemoItems) {
150 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> currentAcctLineGroups = buildAccountLineGroups(item, postingYear, postingPeriodCode);
151 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> historyAcctLineGroups = buildAccountHistoryGroups(item, postingYear, postingPeriodCode, CreditMemoAccountRevision.class);
152 HashSet<PurapAccountRevisionGroup> existList = new HashSet<PurapAccountRevisionGroup>();
153
154 for (PurapAccountRevisionGroup histGroup : historyAcctLineGroups.keySet()) {
155 PurapAccountRevisionGroup currGroup = currentAcctLineGroups.get(histGroup);
156 if (currGroup != null) {
157
158 histGroup.setChangeAmount(currGroup.getAmount().subtract(histGroup.getAmount()));
159 } else {
160
161 histGroup.setChangeAmount(histGroup.getAmount().negated());
162 }
163
164 CreditMemoAccountRevision history = (CreditMemoAccountRevision) histGroup.buildRevisionRecord(CreditMemoAccountRevision.class);
165 history.setAccountRevisionTimestamp(dateTimeService.getCurrentTimestamp());
166 accountHistories.add(history);
167 existList.add(histGroup);
168 }
169
170 for (PurapAccountRevisionGroup group : currentAcctLineGroups.keySet()) {
171 if (!existList.contains(group)) {
172
173 group.setChangeAmount(group.getAmount());
174 CreditMemoAccountRevision history = (CreditMemoAccountRevision) group.buildRevisionRecord(CreditMemoAccountRevision.class);
175 history.setAccountRevisionTimestamp(dateTimeService.getCurrentTimestamp());
176 accountHistories.add(history);
177 }
178 }
179 }
180 businessObjectService.save(accountHistories);
181 }
182
183 public void cancelCreditMemoAccountRevisions(List<CreditMemoItem> creditMemoItems, Integer postingYear, String postingPeriodCode) {
184 List<CreditMemoAccountRevision> accountHistories = new ArrayList<CreditMemoAccountRevision>();
185 for (CreditMemoItem item : creditMemoItems) {
186 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> historyAcctLineGroups = buildAccountHistoryGroups(item, postingYear, postingPeriodCode, CreditMemoAccountRevision.class);
187
188 for (PurapAccountRevisionGroup histGroup : historyAcctLineGroups.keySet()) {
189
190 histGroup.setChangeAmount(histGroup.getAmount().negated());
191
192 CreditMemoAccountRevision history = (CreditMemoAccountRevision) histGroup.buildRevisionRecord(CreditMemoAccountRevision.class);
193 history.setAccountRevisionTimestamp(dateTimeService.getCurrentTimestamp());
194 accountHistories.add(history);
195 }
196 }
197 businessObjectService.save(accountHistories);
198 }
199
200
201
202
203
204
205
206
207
208 protected Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> buildAccountLineGroups(AccountsPayableItemBase item, Integer postingYear, String postingPeriodCode) {
209 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> accountLineGroups = new HashMap<PurapAccountRevisionGroup, PurapAccountRevisionGroup>();
210 for (PurApAccountingLine account : item.getSourceAccountingLines()) {
211 PurapAccountRevisionGroup lineGroup = new PurapAccountRevisionGroup((PurApAccountingLineBase) account);
212 lineGroup.setPostingYear(postingYear);
213 lineGroup.setPostingPeriodCode(postingPeriodCode);
214 if ((accountLineGroups.get(lineGroup)) == null) {
215 accountLineGroups.put(lineGroup, lineGroup);
216 } else {
217 accountLineGroups.get(lineGroup).combineEntry((PurApAccountingLineBase) account);
218 }
219 }
220 return accountLineGroups;
221 }
222
223
224
225
226
227
228
229
230
231
232 protected Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> buildAccountHistoryGroups(AccountsPayableItemBase item, Integer postingYear, String postingPeriodCode, Class<? extends PurApAccountingLineBase> clazz) {
233 Map<PurapAccountRevisionGroup, PurapAccountRevisionGroup> historyGroups = new HashMap<PurapAccountRevisionGroup, PurapAccountRevisionGroup>();
234
235 Map<String, Object> fieldValues = new HashMap<String, Object>();
236 fieldValues.put("itemIdentifier", item.getItemIdentifier());
237 Collection<PurApAccountingLineBase> existingAccounts = (Collection<PurApAccountingLineBase>) businessObjectService.findMatching(clazz, fieldValues);
238 if (existingAccounts != null && !existingAccounts.isEmpty()) {
239 for (PurApAccountingLineBase existAcct : existingAccounts) {
240 PurapAccountRevisionGroup historyGroup = new PurapAccountRevisionGroup(existAcct);
241 historyGroup.setPostingYear(postingYear);
242 historyGroup.setPostingPeriodCode(postingPeriodCode);
243 if ((historyGroups.get(historyGroup)) == null) {
244 historyGroups.put(historyGroup, historyGroup);
245 } else {
246 historyGroups.get(historyGroup).combineEntry((PaymentRequestAccount) existAcct);
247 }
248 }
249 }
250 return historyGroups;
251 }
252
253
254
255
256
257
258 public BusinessObjectService getBusinessObjectService() {
259 return businessObjectService;
260 }
261
262
263
264
265
266
267 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
268 this.businessObjectService = businessObjectService;
269 }
270
271
272
273
274
275
276 public DateTimeService getDateTimeService() {
277 return dateTimeService;
278 }
279
280
281
282
283
284
285 public void setDateTimeService(DateTimeService dateTimeService) {
286 this.dateTimeService = dateTimeService;
287 }
288
289 }