1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys.service.impl;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Date;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.kuali.ole.fp.document.AdvanceDepositDocument;
26 import org.kuali.ole.sys.OLEConstants;
27 import org.kuali.ole.sys.businessobject.AccountingLine;
28 import org.kuali.ole.sys.businessobject.ElectronicPaymentClaim;
29 import org.kuali.ole.sys.context.SpringContext;
30 import org.kuali.ole.sys.service.ElectronicPaymentClaimingDocumentGenerationStrategy;
31 import org.kuali.ole.sys.service.ElectronicPaymentClaimingService;
32 import org.kuali.rice.core.api.datetime.DateTimeService;
33 import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
34 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
35 import org.kuali.rice.kim.api.KimConstants;
36 import org.kuali.rice.kim.api.identity.Person;
37 import org.kuali.rice.kim.api.services.IdentityManagementService;
38 import org.kuali.rice.krad.document.Document;
39 import org.kuali.rice.krad.service.BusinessObjectService;
40 import org.kuali.rice.krad.service.DocumentService;
41 import org.kuali.rice.krad.util.ObjectUtils;
42
43 public class ElectronicPaymentClaimingServiceImpl implements ElectronicPaymentClaimingService {
44 private org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ElectronicPaymentClaimingServiceImpl.class);
45 private DocumentService documentService;
46 private BusinessObjectService businessObjectService;
47 private ParameterService parameterService;
48 private DateTimeService dateTimeService;
49
50 protected static final String ELECTRONIC_FUNDS_CLAIMANT_GROUP_PARAMETER = "ELECTRONIC_FUNDS_CLAIMANT_GROUP";
51 protected final static String ELECTRONIC_PAYMENT_ADMINISTRATOR_GROUP_PARAM_NAME = "ELECTRONIC_FUNDS_ADMINISTRATOR_GROUP";
52 protected static final String ELECTRONIC_FUNDS_CLAIM_SUMMARIES_PER_NOTE_PARAMETER = "ELECTRONIC_FUNDS_CLAIM_SUMMARIES_PER_NOTE";
53 protected static final String CLAIMING_NOTE_PRELUDE = "Claiming CR Items: ";
54 protected static final String DI_CLAIMING_DOC_HELPER_BEAN_NAME = "distributionOfIncomeAndExpenseElectronicPaymentClaimingDocumentHelper";
55 protected static final String YEDI_CLAIMING_DOC_HELPER_BEAN_NAME = "yearEndDistributionOfIncomeAndExpenseElectronicPaymentClaimingDocumentHelper";
56 protected static final String CLAIMING_DOC_HELPER_BEAN_NAME = "expenseElectronicPaymentClaimingDocumentHelper";
57 protected static final String ELECTRONIC_PAYMENT_CLAIM_ACCOUNTS_PARAMETER = "ELECTRONIC_FUNDS_ACCOUNTS";
58
59
60
61
62 @Override
63 public List<String> constructNoteTextsForClaims(List<ElectronicPaymentClaim> claims) {
64 int summariesPerNote;
65 List<String> noteTexts = new ArrayList<String>();
66 try {
67 summariesPerNote = Integer.parseInt(parameterService.getParameterValueAsString(ElectronicPaymentClaim.class, ELECTRONIC_FUNDS_CLAIM_SUMMARIES_PER_NOTE_PARAMETER));
68 int i = 0;
69 while (i < claims.size()) {
70 String noteText = constructNoteText(claims, i, summariesPerNote);
71 noteTexts.add(noteText);
72 i += summariesPerNote;
73 }
74 }
75 catch (NumberFormatException nfe) {
76 throw new RuntimeException("The OLE-SYS / ElectronicPaymentClaim / " + ELECTRONIC_FUNDS_CLAIM_SUMMARIES_PER_NOTE_PARAMETER + " should have a value that can be parsed into an integer.", nfe);
77 }
78 return noteTexts;
79 }
80
81
82
83
84
85
86
87
88
89 protected String constructNoteText(List<ElectronicPaymentClaim> claims, int startPoint, int maxSummariesPerNote) {
90 StringBuilder sb = new StringBuilder();
91 sb.append(CLAIMING_NOTE_PRELUDE);
92 for (int i = startPoint; i < (startPoint + maxSummariesPerNote) && i < claims.size(); i++) {
93 ElectronicPaymentClaim claim = claims.get(i);
94 sb.append(createSummaryLineForClaim(claim));
95 }
96
97 String noteText = sb.substring(0, sb.length() - 3);
98 return noteText;
99 }
100
101
102
103
104
105
106
107 protected String createSummaryLineForClaim(ElectronicPaymentClaim claim) {
108 StringBuilder summary = new StringBuilder();
109 summary.append(claim.getDocumentNumber());
110 summary.append('-');
111 summary.append(claim.getFinancialDocumentLineNumber().toString());
112 summary.append(' ');
113 final Date advanceDepositDate = claim.getGeneratingAdvanceDepositDetail().getFinancialDocumentAdvanceDepositDate();
114 if (!ObjectUtils.isNull(advanceDepositDate)) {
115 summary.append(dateTimeService.toDateString(advanceDepositDate));
116 summary.append(' ');
117 }
118 summary.append('$');
119 summary.append(claim.getGeneratingAccountingLine().getAmount());
120 summary.append(' ');
121 summary.append(';');
122 summary.append(' ');
123 return summary.toString();
124 }
125
126
127
128
129
130 @Override
131 public String createPaymentClaimingDocument(List<ElectronicPaymentClaim> claims, ElectronicPaymentClaimingDocumentGenerationStrategy documentCreationHelper, Person user) {
132 return documentCreationHelper.createDocumentFromElectronicPayments(claims, user);
133 }
134
135
136
137
138 @Override
139 public List<ElectronicPaymentClaimingDocumentGenerationStrategy> getClaimingDocumentChoices(Person user) {
140 List<ElectronicPaymentClaimingDocumentGenerationStrategy> documentChoices = new ArrayList<ElectronicPaymentClaimingDocumentGenerationStrategy>();
141 Map<String, ElectronicPaymentClaimingDocumentGenerationStrategy> claimingDocHelpers = SpringContext.getBeansOfType(ElectronicPaymentClaimingDocumentGenerationStrategy.class);
142 ElectronicPaymentClaimingDocumentGenerationStrategy claimingDocHelper;
143
144
145 claimingDocHelper = claimingDocHelpers.get(CLAIMING_DOC_HELPER_BEAN_NAME);
146 if (claimingDocHelper.userMayUseToClaim(user)) {
147 documentChoices.add(claimingDocHelper);
148 }
149
150
151 claimingDocHelper = claimingDocHelpers.get(DI_CLAIMING_DOC_HELPER_BEAN_NAME);
152 if (claimingDocHelper.userMayUseToClaim(user)) {
153 documentChoices.add(claimingDocHelper);
154 }
155
156
157 claimingDocHelper = claimingDocHelpers.get(YEDI_CLAIMING_DOC_HELPER_BEAN_NAME);
158 if (claimingDocHelper.userMayUseToClaim(user)) {
159 documentChoices.add(claimingDocHelper);
160 }
161
162 return documentChoices;
163 }
164
165
166
167
168
169
170
171
172 @Override
173 public void claimElectronicPayments(List<ElectronicPaymentClaim> payments, String documentNumber) {
174 for (ElectronicPaymentClaim payment : payments) {
175 payment.setReferenceFinancialDocumentNumber(documentNumber);
176 payment.setPaymentClaimStatusCode(ElectronicPaymentClaim.ClaimStatusCodes.CLAIMED);
177 businessObjectService.save(payment);
178 }
179 }
180
181
182
183
184 @Override
185 public void declaimElectronicPaymentClaimsForDocument(Document document) {
186 Map<String, String> searchKeys = new HashMap<String, String>();
187 searchKeys.put("referenceFinancialDocumentNumber", document.getDocumentNumber());
188 Collection<ElectronicPaymentClaim> claimsAsObjects = businessObjectService.findMatching(ElectronicPaymentClaim.class, searchKeys);
189 for (Object claimAsObject : claimsAsObjects) {
190 ElectronicPaymentClaim claim = (ElectronicPaymentClaim) claimAsObject;
191 claim.setReferenceFinancialDocumentNumber(null);
192 claim.setPaymentClaimStatusCode(ElectronicPaymentClaim.ClaimStatusCodes.UNCLAIMED);
193 businessObjectService.save(claim);
194 }
195 }
196
197
198
199
200
201 @Override
202 public boolean isAuthorizedForClaimingElectronicPayment(Person user, String workflowDocumentTypeName) {
203 String principalId = user.getPrincipalId();
204 String namespaceCode = OLEConstants.ParameterNamespaces.OLE;
205 String permissionTemplateName = OLEConstants.PermissionTemplate.CLAIM_ELECTRONIC_PAYMENT.name;
206
207 Map<String,String> permissionDetails = new HashMap<String,String>();
208 permissionDetails.put(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME, workflowDocumentTypeName);
209
210 IdentityManagementService identityManagementService = SpringContext.getBean(IdentityManagementService.class);
211 boolean isAuthorized = identityManagementService.hasPermissionByTemplateName(principalId, namespaceCode, permissionTemplateName, permissionDetails);
212
213 return isAuthorized;
214 }
215
216
217
218
219 @Override
220 public List<ElectronicPaymentClaim> generateElectronicPaymentClaimRecords(AdvanceDepositDocument doc) {
221 List<ElectronicPaymentClaim> claimRecords = new ArrayList<ElectronicPaymentClaim>();
222 for (Object accountingLineAsObj : doc.getSourceAccountingLines()) {
223 final AccountingLine accountingLine = (AccountingLine) accountingLineAsObj;
224 if (this.representsElectronicFundAccount(accountingLine)) {
225 ElectronicPaymentClaim electronicPayment = createElectronicPayment(doc, accountingLine);
226 businessObjectService.save(electronicPayment);
227 claimRecords.add(electronicPayment);
228 }
229 }
230 return claimRecords;
231 }
232
233
234
235
236
237
238 @Override
239 public boolean representsElectronicFundAccount(AccountingLine accountingLine) {
240 return SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(AdvanceDepositDocument.class, ELECTRONIC_PAYMENT_CLAIM_ACCOUNTS_PARAMETER, accountingLine.getChartOfAccountsCode(), accountingLine.getAccountNumber()).evaluationSucceeds();
241 }
242
243
244
245
246
247
248
249 protected ElectronicPaymentClaim createElectronicPayment(AdvanceDepositDocument document, AccountingLine accountingLine) {
250 ElectronicPaymentClaim electronicPayment = new ElectronicPaymentClaim();
251 electronicPayment.setDocumentNumber(document.getDocumentNumber());
252 electronicPayment.setFinancialDocumentLineNumber(accountingLine.getSequenceNumber());
253 electronicPayment.setFinancialDocumentPostingPeriodCode(document.getPostingPeriodCode());
254 electronicPayment.setFinancialDocumentPostingYear(document.getPostingYear());
255 electronicPayment.setPaymentClaimStatusCode(ElectronicPaymentClaim.ClaimStatusCodes.UNCLAIMED);
256 return electronicPayment;
257 }
258
259
260
261
262
263
264 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
265 this.businessObjectService = businessObjectService;
266 }
267
268
269
270
271
272
273 public void setDocumentService(DocumentService documentService) {
274 this.documentService = documentService;
275 }
276
277
278
279
280
281
282 public void setParameterService(ParameterService parameterService) {
283 this.parameterService = parameterService;
284 }
285
286
287
288
289
290
291 public void setDateTimeService(DateTimeService dateTimeService) {
292 this.dateTimeService = dateTimeService;
293 }
294 }