1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.fp.batch.service.impl;
17
18 import static org.kuali.ole.fp.document.validation.impl.ProcurementCardDocumentRuleConstants.AUTO_APPROVE_DOCUMENTS_IND;
19 import static org.kuali.ole.fp.document.validation.impl.ProcurementCardDocumentRuleConstants.AUTO_APPROVE_NUMBER_OF_DAYS;
20 import static org.kuali.ole.fp.document.validation.impl.ProcurementCardDocumentRuleConstants.DEFAULT_TRANS_ACCOUNT_PARM_NM;
21 import static org.kuali.ole.fp.document.validation.impl.ProcurementCardDocumentRuleConstants.DEFAULT_TRANS_CHART_CODE_PARM_NM;
22 import static org.kuali.ole.fp.document.validation.impl.ProcurementCardDocumentRuleConstants.DEFAULT_TRANS_OBJECT_CODE_PARM_NM;
23 import static org.kuali.ole.fp.document.validation.impl.ProcurementCardDocumentRuleConstants.ERROR_TRANS_ACCOUNT_PARM_NM;
24 import static org.kuali.ole.fp.document.validation.impl.ProcurementCardDocumentRuleConstants.SINGLE_TRANSACTION_IND_PARM_NM;
25 import static org.kuali.ole.sys.OLEConstants.GL_CREDIT_CODE;
26 import static org.kuali.ole.sys.OLEConstants.FinancialDocumentTypeCodes.PROCUREMENT_CARD;
27
28 import java.sql.Timestamp;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.Map;
35
36 import org.apache.commons.lang.StringUtils;
37 import org.kuali.ole.coa.businessobject.ProjectCode;
38 import org.kuali.ole.coa.businessobject.SubAccount;
39 import org.kuali.ole.coa.businessobject.SubObjectCode;
40 import org.kuali.ole.coa.service.AccountService;
41 import org.kuali.ole.coa.service.ChartService;
42 import org.kuali.ole.coa.service.ObjectCodeService;
43 import org.kuali.ole.coa.service.ProjectCodeService;
44 import org.kuali.ole.coa.service.SubAccountService;
45 import org.kuali.ole.coa.service.SubObjectCodeService;
46 import org.kuali.ole.fp.batch.ProcurementCardAutoApproveDocumentsStep;
47 import org.kuali.ole.fp.batch.ProcurementCardCreateDocumentsStep;
48 import org.kuali.ole.fp.batch.ProcurementCardLoadStep;
49 import org.kuali.ole.fp.batch.service.ProcurementCardCreateDocumentService;
50 import org.kuali.ole.fp.businessobject.CapitalAssetInformation;
51 import org.kuali.ole.fp.businessobject.ProcurementCardDefault;
52 import org.kuali.ole.fp.businessobject.ProcurementCardHolder;
53 import org.kuali.ole.fp.businessobject.ProcurementCardSourceAccountingLine;
54 import org.kuali.ole.fp.businessobject.ProcurementCardTargetAccountingLine;
55 import org.kuali.ole.fp.businessobject.ProcurementCardTransaction;
56 import org.kuali.ole.fp.businessobject.ProcurementCardTransactionDetail;
57 import org.kuali.ole.fp.businessobject.ProcurementCardVendor;
58 import org.kuali.ole.fp.document.ProcurementCardDocument;
59 import org.kuali.ole.fp.document.validation.impl.ProcurementCardDocumentRuleConstants;
60 import org.kuali.ole.integration.cab.CapitalAssetBuilderModuleService;
61 import org.kuali.ole.sys.OLEConstants;
62 import org.kuali.ole.sys.OLEPropertyConstants;
63 import org.kuali.ole.sys.context.SpringContext;
64 import org.kuali.ole.sys.document.service.AccountingLineRuleHelperService;
65 import org.kuali.ole.sys.document.validation.event.DocumentSystemSaveEvent;
66 import org.kuali.ole.sys.service.UniversityDateService;
67 import org.kuali.ole.sys.util.KfsDateUtils;
68 import org.kuali.rice.core.api.datetime.DateTimeService;
69 import org.kuali.rice.core.api.util.type.KualiDecimal;
70 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
71 import org.kuali.rice.kew.api.KewApiConstants;
72 import org.kuali.rice.kew.api.KewApiServiceLocator;
73 import org.kuali.rice.kew.api.document.DocumentStatus;
74 import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
75 import org.kuali.rice.kew.api.document.search.DocumentSearchResult;
76 import org.kuali.rice.kew.api.document.search.DocumentSearchResults;
77 import org.kuali.rice.kew.api.exception.WorkflowException;
78 import org.kuali.rice.kns.service.DataDictionaryService;
79 import org.kuali.rice.krad.bo.DocumentHeader;
80 import org.kuali.rice.krad.service.BusinessObjectService;
81 import org.kuali.rice.krad.service.DocumentService;
82 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
83 import org.kuali.rice.krad.util.GlobalVariables;
84 import org.kuali.rice.krad.util.MessageMap;
85 import org.kuali.rice.krad.util.ObjectUtils;
86 import org.kuali.rice.krad.workflow.service.WorkflowDocumentService;
87 import org.springframework.transaction.annotation.Transactional;
88
89
90
91
92
93
94
95 public class ProcurementCardCreateDocumentServiceImpl implements ProcurementCardCreateDocumentService {
96 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProcurementCardCreateDocumentServiceImpl.class);
97
98 protected static final String WORKFLOW_SEARCH_RESULT_KEY = "routeHeaderId";
99
100 protected ParameterService parameterService;
101 protected BusinessObjectService businessObjectService;
102 protected DocumentService documentService;
103 protected WorkflowDocumentService workflowDocumentService;
104 protected DataDictionaryService dataDictionaryService;
105 protected DateTimeService dateTimeService;
106 protected AccountingLineRuleHelperService accountingLineRuleUtil;
107 protected CapitalAssetBuilderModuleService capitalAssetBuilderModuleService;
108
109
110
111
112
113
114
115
116
117
118
119 @Override
120 @SuppressWarnings("rawtypes")
121 @Transactional
122 public boolean createProcurementCardDocuments() {
123 List documents = new ArrayList();
124 List cardTransactions = retrieveTransactions();
125
126
127 for (Iterator iter = cardTransactions.iterator(); iter.hasNext();) {
128 documents.add(createProcurementCardDocument((List) iter.next()));
129 }
130
131
132 for (Iterator iter = documents.iterator(); iter.hasNext();) {
133 ProcurementCardDocument pcardDocument = (ProcurementCardDocument) iter.next();
134 try {
135 documentService.saveDocument(pcardDocument, DocumentSystemSaveEvent.class);
136 if ( LOG.isInfoEnabled() ) {
137 LOG.info("Saved Procurement Card document: "+pcardDocument.getDocumentNumber());
138 }
139 }
140 catch (Exception e) {
141 LOG.error("Error persisting document # " + pcardDocument.getDocumentHeader().getDocumentNumber() + " " + e.getMessage(), e);
142 throw new RuntimeException("Error persisting document # " + pcardDocument.getDocumentHeader().getDocumentNumber() + " " + e.getMessage(), e);
143 }
144 }
145
146 return true;
147 }
148
149
150
151
152
153
154
155
156
157 @Override
158 public boolean routeProcurementCardDocuments() {
159
160 List<String> documentIdList = retrieveProcurementCardDocumentsToRoute(KewApiConstants.ROUTE_HEADER_SAVED_CD);
161
162
163 if ( LOG.isInfoEnabled() ) {
164 LOG.info("PCards to Route: "+documentIdList);
165 }
166
167 for (String pcardDocumentId: documentIdList) {
168 try {
169 ProcurementCardDocument pcardDocument = (ProcurementCardDocument)documentService.getByDocumentHeaderId(pcardDocumentId);
170 if ( LOG.isInfoEnabled() ) {
171 LOG.info("Routing PCDO document # " + pcardDocumentId + ".");
172 }
173 documentService.prepareWorkflowDocument(pcardDocument);
174
175
176
177
178
179
180 workflowDocumentService.route( pcardDocument.getDocumentHeader().getWorkflowDocument(), "", null);
181 }
182 catch (WorkflowException e) {
183 LOG.error("Error routing document # " + pcardDocumentId + " " + e.getMessage());
184 throw new RuntimeException(e.getMessage(),e);
185 }
186 }
187
188 return true;
189 }
190
191
192
193
194
195 protected List<String> retrieveProcurementCardDocumentsToRoute(String statusCode){
196 List<String> documentIds = new ArrayList<String>();
197
198 DocumentSearchCriteria.Builder criteria = DocumentSearchCriteria.Builder.create();
199 criteria.setDocumentTypeName(OLEConstants.FinancialDocumentTypeCodes.PROCUREMENT_CARD);
200 criteria.setDocumentStatuses(Collections.singletonList(DocumentStatus.fromCode(statusCode)));
201 DocumentSearchResults results = KewApiServiceLocator.getWorkflowDocumentService().documentSearch(
202 GlobalVariables.getUserSession().getPrincipalId(), criteria.build());
203
204 for (DocumentSearchResult resultRow: results.getSearchResults()) {
205 documentIds.add(resultRow.getDocument().getDocumentId());
206 }
207
208 return documentIds;
209 }
210
211
212
213
214
215
216
217
218
219
220
221 @Override
222 public boolean autoApproveProcurementCardDocuments() {
223
224 boolean autoApproveOn = parameterService.getParameterValueAsBoolean(ProcurementCardAutoApproveDocumentsStep.class, AUTO_APPROVE_DOCUMENTS_IND);
225
226 if (!autoApproveOn) {
227 return true;
228 }
229
230 List<String> documentIdList = retrieveProcurementCardDocumentsToRoute(KewApiConstants.ROUTE_HEADER_ENROUTE_CD);
231
232
233 int autoApproveNumberDays = Integer.parseInt(parameterService.getParameterValueAsString(ProcurementCardAutoApproveDocumentsStep.class, AUTO_APPROVE_NUMBER_OF_DAYS));
234
235 Timestamp currentDate = dateTimeService.getCurrentTimestamp();
236 for (String pcardDocumentId: documentIdList) {
237 try {
238 ProcurementCardDocument pcardDocument = (ProcurementCardDocument)documentService.getByDocumentHeaderId(pcardDocumentId);
239
240
241 if(capitalAssetBuilderModuleService.hasCapitalAssetObjectSubType(pcardDocument)) {
242 continue;
243 }
244
245
246 Timestamp docCreateDate = new Timestamp( pcardDocument.getDocumentHeader().getWorkflowDocument().getDateCreated().getMillis() );
247 if (KfsDateUtils.getDifferenceInDays(docCreateDate, currentDate) > autoApproveNumberDays) {
248
249 pcardDocument.getDocumentHeader().setDocumentDescription("Auto Approved On " + dateTimeService.toDateTimeString(currentDate) + ".");
250
251 if ( LOG.isInfoEnabled() ) {
252 LOG.info("Auto approving document # " + pcardDocument.getDocumentHeader().getDocumentNumber());
253 }
254 documentService.superUserApproveDocument(pcardDocument, "");
255 }
256 } catch (WorkflowException e) {
257 LOG.error("Error auto approving document # " + pcardDocumentId + " " + e.getMessage(),e);
258 throw new RuntimeException(e.getMessage(),e);
259 }
260 }
261
262 return true;
263 }
264
265
266
267
268
269
270
271
272 @SuppressWarnings("rawtypes")
273 protected List retrieveTransactions() {
274 List groupedTransactions = new ArrayList();
275
276
277 List transactions = (List) KRADServiceLocatorWeb.getLegacyDataAdapter().findMatchingOrderBy(ProcurementCardTransaction.class, new HashMap(), OLEPropertyConstants.TRANSACTION_CREDIT_CARD_NUMBER, true);
278
279
280 boolean singleTransaction = parameterService.getParameterValueAsBoolean(ProcurementCardCreateDocumentsStep.class, SINGLE_TRANSACTION_IND_PARM_NM);
281
282 List documentTransactions = new ArrayList();
283 if (singleTransaction) {
284 for (Iterator iter = transactions.iterator(); iter.hasNext();) {
285 documentTransactions.add(iter.next());
286 groupedTransactions.add(documentTransactions);
287 documentTransactions = new ArrayList();
288 }
289 }
290 else {
291 Map cardTransactionsMap = new HashMap();
292 for (Iterator iter = transactions.iterator(); iter.hasNext();) {
293 ProcurementCardTransaction transaction = (ProcurementCardTransaction) iter.next();
294 if (!cardTransactionsMap.containsKey(transaction.getTransactionCreditCardNumber())) {
295 cardTransactionsMap.put(transaction.getTransactionCreditCardNumber(), new ArrayList());
296 }
297 ((List) cardTransactionsMap.get(transaction.getTransactionCreditCardNumber())).add(transaction);
298 }
299
300 for (Iterator iter = cardTransactionsMap.values().iterator(); iter.hasNext();) {
301 groupedTransactions.add(iter.next());
302
303 }
304 }
305
306 return groupedTransactions;
307 }
308
309
310
311
312
313
314
315
316 protected ProcurementCardDocument createProcurementCardDocument(List transactions) {
317 ProcurementCardDocument pcardDocument = null;
318
319 try {
320
321 pcardDocument = (ProcurementCardDocument) SpringContext.getBean(DocumentService.class).getNewDocument(PROCUREMENT_CARD);
322
323 List<CapitalAssetInformation> capitalAssets = pcardDocument.getCapitalAssetInformation();
324 for (CapitalAssetInformation capitalAsset : capitalAssets) {
325 if (ObjectUtils.isNotNull(capitalAsset) && ObjectUtils.isNotNull(capitalAsset.getCapitalAssetInformationDetails())) {
326 capitalAsset.setDocumentNumber(pcardDocument.getDocumentNumber());
327 }
328 }
329
330 ProcurementCardTransaction trans = (ProcurementCardTransaction) transactions.get(0);
331 String errors = validateTransaction(trans);
332 createCardHolderRecord(pcardDocument, trans);
333
334
335 int transactionLineNumber = 1;
336 KualiDecimal documentTotalAmount = KualiDecimal.ZERO;
337 String errorText = "";
338 for (Iterator iter = transactions.iterator(); iter.hasNext();) {
339 ProcurementCardTransaction transaction = (ProcurementCardTransaction) iter.next();
340
341
342 errorText += createTransactionDetailRecord(pcardDocument, transaction, transactionLineNumber);
343
344
345 documentTotalAmount = documentTotalAmount.add(transaction.getFinancialDocumentTotalAmount());
346
347 transactionLineNumber++;
348 }
349
350 pcardDocument.getFinancialSystemDocumentHeader().setFinancialDocumentTotalAmount(documentTotalAmount);
351 pcardDocument.getDocumentHeader().setDocumentDescription("SYSTEM Generated");
352
353
354 String messages[] = StringUtils.split(errorText, ".");
355 for (int i = 0; i < messages.length; i++) {
356 int countMatches = StringUtils.countMatches(errorText, messages[i]) - 1;
357 errorText = StringUtils.replace(errorText, messages[i] + ".", "", countMatches);
358 }
359
360 Integer documentExplanationMaxLength = dataDictionaryService.getAttributeMaxLength(DocumentHeader.class.getName(), OLEPropertyConstants.EXPLANATION);
361 if (documentExplanationMaxLength != null && errorText.length() > documentExplanationMaxLength.intValue()) {
362 String truncatedMessage = " ... TRUNCATED.";
363 errorText = errorText.substring(0, documentExplanationMaxLength - truncatedMessage.length()) + truncatedMessage;
364 }
365 pcardDocument.getDocumentHeader().setExplanation(errorText);
366 }
367 catch (WorkflowException e) {
368 LOG.error("Error creating pcdo documents: " + e.getMessage(),e);
369 throw new RuntimeException("Error creating pcdo documents: " + e.getMessage(),e);
370 }
371
372 return pcardDocument;
373 }
374
375
376
377
378
379
380
381 protected void createCardHolderRecord(ProcurementCardDocument pcardDocument, ProcurementCardTransaction transaction) {
382 ProcurementCardHolder cardHolder = new ProcurementCardHolder();
383
384 cardHolder.setDocumentNumber(pcardDocument.getDocumentNumber());
385 cardHolder.setTransactionCreditCardNumber(transaction.getTransactionCreditCardNumber());
386 cardHolder.setChartOfAccountsCode(transaction.getChartOfAccountsCode());
387 cardHolder.setAccountNumber(transaction.getAccountNumber());
388 cardHolder.setSubAccountNumber(transaction.getSubAccountNumber());
389
390 if (getParameterService().getParameterValueAsBoolean(ProcurementCardCreateDocumentsStep.class, ProcurementCardCreateDocumentsStep.USE_CARD_HOLDER_DEFAULT_PARAMETER_NAME)) {
391 final ProcurementCardDefault procurementCardDefault = retrieveProcurementCardDefault(transaction.getTransactionCreditCardNumber());
392 if (procurementCardDefault != null) {
393 cardHolder.setCardCycleAmountLimit(procurementCardDefault.getCardCycleAmountLimit());
394 cardHolder.setCardCycleVolumeLimit(procurementCardDefault.getCardCycleVolumeLimit());
395 cardHolder.setCardHolderAlternateName(procurementCardDefault.getCardHolderAlternateName());
396 cardHolder.setCardHolderCityName(procurementCardDefault.getCardHolderCityName());
397 cardHolder.setCardHolderLine1Address(procurementCardDefault.getCardHolderLine1Address());
398 cardHolder.setCardHolderLine2Address(procurementCardDefault.getCardHolderLine2Address());
399 cardHolder.setCardHolderName(procurementCardDefault.getCardHolderName());
400 cardHolder.setCardHolderStateCode(procurementCardDefault.getCardHolderStateCode());
401 cardHolder.setCardHolderWorkPhoneNumber(procurementCardDefault.getCardHolderWorkPhoneNumber());
402 cardHolder.setCardHolderZipCode(procurementCardDefault.getCardHolderZipCode());
403 cardHolder.setCardLimit(procurementCardDefault.getCardLimit());
404 cardHolder.setCardNoteText(procurementCardDefault.getCardNoteText());
405 cardHolder.setCardStatusCode(procurementCardDefault.getCardStatusCode());
406
407 if (getParameterService().getParameterValueAsBoolean(ProcurementCardCreateDocumentsStep.class, ProcurementCardCreateDocumentsStep.USE_ACCOUNTING_DEFAULT_PARAMETER_NAME)) {
408 cardHolder.setChartOfAccountsCode(procurementCardDefault.getChartOfAccountsCode());
409 cardHolder.setAccountNumber(procurementCardDefault.getAccountNumber());
410 cardHolder.setSubAccountNumber(procurementCardDefault.getSubAccountNumber());
411 }
412 }
413 }
414 if (StringUtils.isEmpty(cardHolder.getCardHolderName())) {
415 cardHolder.setCardCycleAmountLimit(transaction.getCardCycleAmountLimit());
416 cardHolder.setCardCycleVolumeLimit(transaction.getCardCycleVolumeLimit());
417 cardHolder.setCardHolderAlternateName(transaction.getCardHolderAlternateName());
418 cardHolder.setCardHolderCityName(transaction.getCardHolderCityName());
419 cardHolder.setCardHolderLine1Address(transaction.getCardHolderLine1Address());
420 cardHolder.setCardHolderLine2Address(transaction.getCardHolderLine2Address());
421 cardHolder.setCardHolderName(transaction.getCardHolderName());
422 cardHolder.setCardHolderStateCode(transaction.getCardHolderStateCode());
423 cardHolder.setCardHolderWorkPhoneNumber(transaction.getCardHolderWorkPhoneNumber());
424 cardHolder.setCardHolderZipCode(transaction.getCardHolderZipCode());
425 cardHolder.setCardLimit(transaction.getCardLimit());
426 cardHolder.setCardNoteText(transaction.getCardNoteText());
427 cardHolder.setCardStatusCode(transaction.getCardStatusCode());
428 }
429
430 pcardDocument.setProcurementCardHolder(cardHolder);
431 }
432
433
434
435
436
437
438
439
440
441 protected String createTransactionDetailRecord(ProcurementCardDocument pcardDocument, ProcurementCardTransaction transaction, Integer transactionLineNumber) {
442 ProcurementCardTransactionDetail transactionDetail = new ProcurementCardTransactionDetail();
443
444
445 transactionDetail.setDocumentNumber(pcardDocument.getDocumentNumber());
446 transactionDetail.setFinancialDocumentTransactionLineNumber(transactionLineNumber);
447 transactionDetail.setTransactionDate(transaction.getTransactionDate());
448 transactionDetail.setTransactionReferenceNumber(transaction.getTransactionReferenceNumber());
449 transactionDetail.setTransactionBillingCurrencyCode(transaction.getTransactionBillingCurrencyCode());
450 transactionDetail.setTransactionCurrencyExchangeRate(transaction.getTransactionCurrencyExchangeRate());
451 transactionDetail.setTransactionDate(transaction.getTransactionDate());
452 transactionDetail.setTransactionOriginalCurrencyAmount(transaction.getTransactionOriginalCurrencyAmount());
453 transactionDetail.setTransactionOriginalCurrencyCode(transaction.getTransactionOriginalCurrencyCode());
454 transactionDetail.setTransactionPointOfSaleCode(transaction.getTransactionPointOfSaleCode());
455 transactionDetail.setTransactionPostingDate(transaction.getTransactionPostingDate());
456 transactionDetail.setTransactionPurchaseIdentifierDescription(transaction.getTransactionPurchaseIdentifierDescription());
457 transactionDetail.setTransactionPurchaseIdentifierIndicator(transaction.getTransactionPurchaseIdentifierIndicator());
458 transactionDetail.setTransactionSalesTaxAmount(transaction.getTransactionSalesTaxAmount());
459 transactionDetail.setTransactionSettlementAmount(transaction.getTransactionSettlementAmount());
460 transactionDetail.setTransactionTaxExemptIndicator(transaction.getTransactionTaxExemptIndicator());
461 transactionDetail.setTransactionTravelAuthorizationCode(transaction.getTransactionTravelAuthorizationCode());
462 transactionDetail.setTransactionUnitContactName(transaction.getTransactionUnitContactName());
463
464 if (GL_CREDIT_CODE.equals(transaction.getTransactionDebitCreditCode())) {
465 transactionDetail.setTransactionTotalAmount(transaction.getFinancialDocumentTotalAmount().negated());
466 }
467 else {
468 transactionDetail.setTransactionTotalAmount(transaction.getFinancialDocumentTotalAmount());
469 }
470
471
472 createTransactionVendorRecord(pcardDocument, transaction, transactionDetail);
473
474
475 pcardDocument.getTransactionEntries().add(transactionDetail);
476
477
478 return createAndValidateAccountingLines(pcardDocument, transaction, transactionDetail);
479 }
480
481
482
483
484
485
486
487
488
489 protected void createTransactionVendorRecord(ProcurementCardDocument pcardDocument, ProcurementCardTransaction transaction, ProcurementCardTransactionDetail transactionDetail) {
490 ProcurementCardVendor transactionVendor = new ProcurementCardVendor();
491
492 transactionVendor.setDocumentNumber(pcardDocument.getDocumentNumber());
493 transactionVendor.setFinancialDocumentTransactionLineNumber(transactionDetail.getFinancialDocumentTransactionLineNumber());
494 transactionVendor.setTransactionMerchantCategoryCode(transaction.getTransactionMerchantCategoryCode());
495 transactionVendor.setVendorCityName(transaction.getVendorCityName());
496 transactionVendor.setVendorLine1Address(transaction.getVendorLine1Address());
497 transactionVendor.setVendorLine2Address(transaction.getVendorLine2Address());
498 transactionVendor.setVendorName(transaction.getVendorName());
499 transactionVendor.setVendorOrderNumber(transaction.getVendorOrderNumber());
500 transactionVendor.setVendorStateCode(transaction.getVendorStateCode());
501 transactionVendor.setVendorZipCode(transaction.getVendorZipCode());
502 transactionVendor.setVisaVendorIdentifier(transaction.getVisaVendorIdentifier());
503
504 transactionDetail.setProcurementCardVendor(transactionVendor);
505 }
506
507
508
509
510
511
512
513
514
515
516 protected String createAndValidateAccountingLines(ProcurementCardDocument pcardDocument, ProcurementCardTransaction transaction, ProcurementCardTransactionDetail docTransactionDetail) {
517
518 ProcurementCardSourceAccountingLine sourceLine = createSourceAccountingLine(transaction, docTransactionDetail);
519 sourceLine.setPostingYear(pcardDocument.getPostingYear());
520
521
522 pcardDocument.addSourceAccountingLine(sourceLine);
523
524
525 ProcurementCardTargetAccountingLine targetLine = createTargetAccountingLine(transaction, docTransactionDetail);
526 targetLine.setPostingYear(pcardDocument.getPostingYear());
527
528
529 pcardDocument.addTargetAccountingLine(targetLine);
530
531 return validateTargetAccountingLine(targetLine);
532 }
533
534
535
536
537
538
539
540
541
542 protected ProcurementCardTargetAccountingLine createTargetAccountingLine(ProcurementCardTransaction transaction, ProcurementCardTransactionDetail docTransactionDetail) {
543 ProcurementCardTargetAccountingLine targetLine = new ProcurementCardTargetAccountingLine();
544 targetLine.setDocumentNumber(docTransactionDetail.getDocumentNumber());
545 targetLine.setFinancialDocumentTransactionLineNumber(docTransactionDetail.getFinancialDocumentTransactionLineNumber());
546 targetLine.setChartOfAccountsCode(transaction.getChartOfAccountsCode());
547 targetLine.setAccountNumber(transaction.getAccountNumber());
548 targetLine.setFinancialObjectCode(transaction.getFinancialObjectCode());
549 targetLine.setSubAccountNumber(transaction.getSubAccountNumber());
550 targetLine.setFinancialSubObjectCode(transaction.getFinancialSubObjectCode());
551 targetLine.setProjectCode(transaction.getProjectCode());
552
553 if (getParameterService().getParameterValueAsBoolean(ProcurementCardCreateDocumentsStep.class, ProcurementCardCreateDocumentsStep.USE_ACCOUNTING_DEFAULT_PARAMETER_NAME)) {
554 final ProcurementCardDefault procurementCardDefault = retrieveProcurementCardDefault(transaction.getTransactionCreditCardNumber());
555 if (procurementCardDefault != null) {
556 targetLine.setChartOfAccountsCode(procurementCardDefault.getChartOfAccountsCode());
557 targetLine.setAccountNumber(procurementCardDefault.getAccountNumber());
558 targetLine.setFinancialObjectCode(procurementCardDefault.getFinancialObjectCode());
559 targetLine.setSubAccountNumber(procurementCardDefault.getSubAccountNumber());
560 targetLine.setFinancialSubObjectCode(procurementCardDefault.getFinancialSubObjectCode());
561 targetLine.setProjectCode(procurementCardDefault.getProjectCode());
562 }
563 }
564
565 if (GL_CREDIT_CODE.equals(transaction.getTransactionDebitCreditCode())) {
566 targetLine.setAmount(transaction.getFinancialDocumentTotalAmount().negated());
567 }
568 else {
569 targetLine.setAmount(transaction.getFinancialDocumentTotalAmount());
570 }
571
572 return targetLine;
573 }
574
575
576
577
578
579
580
581
582 protected ProcurementCardSourceAccountingLine createSourceAccountingLine(ProcurementCardTransaction transaction, ProcurementCardTransactionDetail docTransactionDetail) {
583 ProcurementCardSourceAccountingLine sourceLine = new ProcurementCardSourceAccountingLine();
584
585 sourceLine.setDocumentNumber(docTransactionDetail.getDocumentNumber());
586 sourceLine.setFinancialDocumentTransactionLineNumber(docTransactionDetail.getFinancialDocumentTransactionLineNumber());
587 sourceLine.setChartOfAccountsCode(getDefaultChartCode());
588 sourceLine.setAccountNumber(getDefaultAccountNumber());
589 sourceLine.setFinancialObjectCode(getDefaultObjectCode());
590
591 if (GL_CREDIT_CODE.equals(transaction.getTransactionDebitCreditCode())) {
592 sourceLine.setAmount(transaction.getFinancialDocumentTotalAmount().negated());
593 }
594 else {
595 sourceLine.setAmount(transaction.getFinancialDocumentTotalAmount());
596 }
597
598 return sourceLine;
599 }
600
601
602
603
604
605
606
607
608 protected String validateTargetAccountingLine(ProcurementCardTargetAccountingLine targetLine) {
609 String errorText = "";
610
611 targetLine.refresh();
612
613 if (!accountingLineRuleUtil.isValidChart("", targetLine.getChart(), dataDictionaryService.getDataDictionary())) {
614 String tempErrorText = "Chart " + targetLine.getChartOfAccountsCode() + " is invalid; using error Chart Code.";
615 if ( LOG.isInfoEnabled() ) {
616 LOG.info(tempErrorText);
617 }
618 errorText += " " + tempErrorText;
619
620 targetLine.setChartOfAccountsCode(getErrorChartCode());
621 targetLine.refresh();
622 }
623
624 if (!accountingLineRuleUtil.isValidAccount("", targetLine.getAccount(), dataDictionaryService.getDataDictionary()) || targetLine.getAccount().isExpired()) {
625 String tempErrorText = "Chart " + targetLine.getChartOfAccountsCode() + " Account " + targetLine.getAccountNumber() + " is invalid; using error account.";
626 if ( LOG.isInfoEnabled() ) {
627 LOG.info(tempErrorText);
628 }
629 errorText += " " + tempErrorText;
630
631 targetLine.setChartOfAccountsCode(getErrorChartCode());
632 targetLine.setAccountNumber(getErrorAccountNumber());
633 targetLine.refresh();
634 }
635
636 if (!accountingLineRuleUtil.isValidObjectCode("", targetLine.getObjectCode(), dataDictionaryService.getDataDictionary())) {
637 String tempErrorText = "Chart " + targetLine.getChartOfAccountsCode() + " Object Code " + targetLine.getFinancialObjectCode() + " is invalid; using default Object Code.";
638 if ( LOG.isInfoEnabled() ) {
639 LOG.info(tempErrorText);
640 }
641 errorText += " " + tempErrorText;
642
643 targetLine.setFinancialObjectCode(getDefaultObjectCode());
644 targetLine.refresh();
645 }
646
647 if (StringUtils.isNotBlank(targetLine.getSubAccountNumber()) && !accountingLineRuleUtil.isValidSubAccount("", targetLine.getSubAccount(), dataDictionaryService.getDataDictionary())) {
648 String tempErrorText = "Chart " + targetLine.getChartOfAccountsCode() + " Account " + targetLine.getAccountNumber() + " Sub Account " + targetLine.getSubAccountNumber() + " is invalid; Setting Sub Account to blank.";
649 if ( LOG.isInfoEnabled() ) {
650 LOG.info(tempErrorText);
651 }
652 errorText += " " + tempErrorText;
653
654 targetLine.setSubAccountNumber("");
655 }
656
657 if (StringUtils.isNotBlank(targetLine.getFinancialSubObjectCode()) && !accountingLineRuleUtil.isValidSubObjectCode("", targetLine.getSubObjectCode(), dataDictionaryService.getDataDictionary())) {
658 String tempErrorText = "Chart " + targetLine.getChartOfAccountsCode() + " Account " + targetLine.getAccountNumber() + " Object Code " + targetLine.getFinancialObjectCode() + " Sub Object Code " + targetLine.getFinancialSubObjectCode() + " is invalid; setting Sub Object to blank.";
659 if ( LOG.isInfoEnabled() ) {
660 LOG.info(tempErrorText);
661 }
662 errorText += " " + tempErrorText;
663
664 targetLine.setFinancialSubObjectCode("");
665 }
666
667 if (StringUtils.isNotBlank(targetLine.getProjectCode()) && !accountingLineRuleUtil.isValidProjectCode("", targetLine.getProject(), dataDictionaryService.getDataDictionary())) {
668 if ( LOG.isInfoEnabled() ) {
669 LOG.info("Project Code " + targetLine.getProjectCode() + " is invalid; setting to blank.");
670 }
671 errorText += " Project Code " + targetLine.getProjectCode() + " is invalid; setting to blank.";
672
673 targetLine.setProjectCode("");
674 }
675
676
677 GlobalVariables.setMessageMap(new MessageMap());
678
679 return errorText;
680 }
681
682
683
684
685
686
687
688
689
690 protected String validateTransaction(ProcurementCardTransaction transaction) {
691 String errorText = "";
692
693 if (getParameterService().getParameterValueAsBoolean(ProcurementCardCreateDocumentsStep.class, ProcurementCardCreateDocumentsStep.USE_ACCOUNTING_DEFAULT_PARAMETER_NAME)) {
694 final ProcurementCardDefault procurementCardDefault = retrieveProcurementCardDefault(transaction.getTransactionCreditCardNumber());
695 if (ObjectUtils.isNull(procurementCardDefault)) {
696 final String tempErrorText = "Procurement Card Accounting Line Defaults are turned on but no Procurement Card Default record could be retrieved for transaction: "+transaction.getTransactionReferenceNumber() + " by card number.";
697 if ( LOG.isInfoEnabled() ) {
698 LOG.info(tempErrorText);
699 }
700 errorText += " " + tempErrorText;
701 }
702 }
703 else {
704 transaction.refresh();
705
706 final ChartService chartService = SpringContext.getBean(ChartService.class);
707 if (transaction.getChartOfAccountsCode() == null || ObjectUtils.isNull(chartService.getByPrimaryId(transaction.getChartOfAccountsCode()))) {
708 String tempErrorText = "Chart " + transaction.getChartOfAccountsCode() + " is invalid; using error Chart Code.";
709 if ( LOG.isInfoEnabled() ) {
710 LOG.info(tempErrorText);
711 }
712 errorText += " " + tempErrorText;
713 transaction.setChartOfAccountsCode(getErrorChartCode());
714 transaction.refresh();
715 }
716
717 final AccountService accountService = SpringContext.getBean(AccountService.class);
718 if (transaction.getAccountNumber() == null || ObjectUtils.isNull(accountService.getByPrimaryIdWithCaching(transaction.getChartOfAccountsCode(), transaction.getAccountNumber())) || accountService.getByPrimaryIdWithCaching(transaction.getChartOfAccountsCode(), transaction.getAccountNumber()).isExpired()) {
719 String tempErrorText = "Chart " + transaction.getChartOfAccountsCode() + " Account " + transaction.getAccountNumber() + " is invalid; using error account.";
720 if ( LOG.isInfoEnabled() ) {
721 LOG.info(tempErrorText);
722 }
723 errorText += " " + tempErrorText;
724 transaction.setChartOfAccountsCode(getErrorChartCode());
725 transaction.setAccountNumber(getErrorAccountNumber());
726 transaction.refresh();
727 }
728
729 final UniversityDateService uds = SpringContext.getBean(UniversityDateService.class);
730 final ObjectCodeService ocs = SpringContext.getBean(ObjectCodeService.class);
731 if (transaction.getFinancialObjectCode() == null || ObjectUtils.isNull(ocs.getByPrimaryIdWithCaching(uds.getCurrentFiscalYear(), transaction.getChartOfAccountsCode(), transaction.getFinancialObjectCode()))) {
732 String tempErrorText = "Chart " + transaction.getChartOfAccountsCode() + " Object Code " + transaction.getFinancialObjectCode() + " is invalid; using default Object Code.";
733 if ( LOG.isInfoEnabled() ) {
734 LOG.info(tempErrorText);
735 }
736 errorText += " " + tempErrorText;
737
738 transaction.setFinancialObjectCode(getDefaultObjectCode());
739 transaction.refresh();
740 }
741
742 if (StringUtils.isNotBlank(transaction.getSubAccountNumber())) {
743 SubAccountService sas = SpringContext.getBean(SubAccountService.class);
744 SubAccount subAccount = sas.getByPrimaryIdWithCaching(transaction.getChartOfAccountsCode(), transaction.getAccountNumber(), transaction.getSubAccountNumber());
745
746 if (ObjectUtils.isNull(subAccount)) {
747 String tempErrorText = "Chart " + transaction.getChartOfAccountsCode() + " Account " + transaction.getAccountNumber() + " Sub Account " + transaction.getSubAccountNumber() + " is invalid; Setting Sub Account to blank.";
748 if ( LOG.isInfoEnabled() ) {
749 LOG.info(tempErrorText);
750 }
751 errorText += " " + tempErrorText;
752
753 transaction.setSubAccountNumber("");
754 }
755 }
756
757 if (StringUtils.isNotBlank(transaction.getFinancialSubObjectCode())) {
758
759 SubObjectCodeService socs = SpringContext.getBean(SubObjectCodeService.class);
760 SubObjectCode soc = socs.getByPrimaryIdForCurrentYear(transaction.getChartOfAccountsCode(), transaction.getAccountNumber(), transaction.getFinancialObjectCode(), transaction.getFinancialSubObjectCode());
761
762 if (ObjectUtils.isNull(soc)) {
763 String tempErrorText = "Chart " + transaction.getChartOfAccountsCode() + " Account " + transaction.getAccountNumber() + " Object Code " + transaction.getFinancialObjectCode() + " Sub Object Code " + transaction.getFinancialSubObjectCode() + " is invalid; setting Sub Object to blank.";
764 if ( LOG.isInfoEnabled() ) {
765 LOG.info(tempErrorText);
766 }
767 errorText += " " + tempErrorText;
768
769 transaction.setFinancialSubObjectCode("");
770 }
771 }
772
773 if (StringUtils.isNotBlank(transaction.getProjectCode())) {
774
775 ProjectCodeService pcs = SpringContext.getBean(ProjectCodeService.class);
776 ProjectCode pc = pcs.getByPrimaryId(transaction.getProjectCode());
777
778 if (ObjectUtils.isNull(pc)) {
779 if ( LOG.isInfoEnabled() ) {
780 LOG.info("Project Code " + transaction.getProjectCode() + " is invalid; setting to blank.");
781 }
782 errorText += " Project Code " + transaction.getProjectCode() + " is invalid; setting to blank.";
783
784 transaction.setProjectCode("");
785 }
786 }
787 }
788
789
790 GlobalVariables.setMessageMap(new MessageMap());
791
792 return errorText;
793 }
794
795
796
797
798
799 protected String getErrorChartCode() {
800 return parameterService.getParameterValueAsString(ProcurementCardCreateDocumentsStep.class, ProcurementCardDocumentRuleConstants.ERROR_TRANS_CHART_CODE_PARM_NM);
801 }
802
803
804
805
806
807 protected String getErrorAccountNumber() {
808 return parameterService.getParameterValueAsString(ProcurementCardCreateDocumentsStep.class, ERROR_TRANS_ACCOUNT_PARM_NM);
809 }
810
811
812
813
814
815 protected ProcurementCardDefault retrieveProcurementCardDefault(String creditCardNumber) {
816 Map<String, String> pkMap = new HashMap<String, String>();
817 pkMap.put(OLEPropertyConstants.CREDIT_CARD_NUMBER, creditCardNumber);
818 ProcurementCardDefault procurementCardDefault = KRADServiceLocatorWeb.getLegacyDataAdapter().findByPrimaryKey(ProcurementCardDefault.class, pkMap);
819
820 return procurementCardDefault;
821 }
822
823
824
825
826
827 protected String getDefaultChartCode() {
828 return parameterService.getParameterValueAsString(ProcurementCardLoadStep.class, DEFAULT_TRANS_CHART_CODE_PARM_NM);
829 }
830
831
832
833
834
835 protected String getDefaultAccountNumber() {
836 return parameterService.getParameterValueAsString(ProcurementCardLoadStep.class, DEFAULT_TRANS_ACCOUNT_PARM_NM);
837 }
838
839
840
841
842
843 protected String getDefaultObjectCode() {
844 return parameterService.getParameterValueAsString(ProcurementCardLoadStep.class, DEFAULT_TRANS_OBJECT_CODE_PARM_NM);
845 }
846
847
848
849
850 protected void cleanTransactionsTable() {
851 KRADServiceLocatorWeb.getLegacyDataAdapter().deleteMatching(ProcurementCardTransaction.class, new HashMap());
852 }
853
854
855
856
857
858
859 protected void loadTransactions(List transactions) {
860 KRADServiceLocatorWeb.getLegacyDataAdapter().save(transactions);
861 }
862
863
864
865
866 public ParameterService getParameterService() {
867 return parameterService;
868 }
869
870
871
872
873
874 public void setParameterService(ParameterService parameterService) {
875 this.parameterService = parameterService;
876 }
877
878
879
880
881
882 public BusinessObjectService getBusinessObjectService() {
883 return businessObjectService;
884 }
885
886
887
888
889
890 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
891 this.businessObjectService = businessObjectService;
892 }
893
894
895
896
897
898 public DocumentService getDocumentService() {
899 return documentService;
900 }
901
902
903
904
905
906 public void setDocumentService(DocumentService documentService) {
907 this.documentService = documentService;
908 }
909
910 public WorkflowDocumentService getWorkflowDocumentService() {
911 return workflowDocumentService;
912 }
913
914 public void setWorkflowDocumentService(WorkflowDocumentService workflowDocumentService) {
915 this.workflowDocumentService = workflowDocumentService;
916 }
917
918
919
920
921
922 public DataDictionaryService getDataDictionaryService() {
923 return dataDictionaryService;
924 }
925
926
927
928
929
930 public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
931 this.dataDictionaryService = dataDictionaryService;
932 }
933
934
935
936
937
938
939 public DateTimeService getDateTimeService() {
940 return dateTimeService;
941 }
942
943
944
945
946
947 public void setDateTimeService(DateTimeService dateTimeService) {
948 this.dateTimeService = dateTimeService;
949 }
950
951
952
953
954
955 public void setAccountingLineRuleUtil(AccountingLineRuleHelperService accountingLineRuleUtil) {
956 this.accountingLineRuleUtil = accountingLineRuleUtil;
957 }
958
959
960
961
962
963 public void setCapitalAssetBuilderModuleService(CapitalAssetBuilderModuleService capitalAssetBuilderModuleService) {
964 this.capitalAssetBuilderModuleService = capitalAssetBuilderModuleService;
965 }
966
967 }