1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.select.document.service.impl;
17
18 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.lang.ArrayUtils;
20 import org.apache.commons.lang.StringUtils;
21 import org.kuali.ole.DataCarrierService;
22 import org.kuali.ole.batch.bo.OLEBatchProcessProfileBo;
23 import org.kuali.ole.batch.bo.OLEBatchProcessProfileConstantsBo;
24 import org.kuali.ole.batch.bo.OLEBatchProcessProfileDataMappingOptionsBo;
25 import org.kuali.ole.batch.bo.OLEBatchProcessProfileMappingOptionsBo;
26 import org.kuali.ole.coa.businessobject.Account;
27 import org.kuali.ole.coa.businessobject.ObjectCode;
28 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
29 import org.kuali.ole.module.purap.PurapConstants;
30 import org.kuali.ole.module.purap.PurapConstants.PurchaseOrderDocTypes;
31 import org.kuali.ole.module.purap.PurapConstants.PurchaseOrderStatuses;
32 import org.kuali.ole.module.purap.PurapKeyConstants;
33 import org.kuali.ole.module.purap.PurapParameterConstants;
34 import org.kuali.ole.module.purap.PurapPropertyConstants;
35 import org.kuali.ole.module.purap.businessobject.*;
36 import org.kuali.ole.module.purap.document.InvoiceDocument;
37 import org.kuali.ole.module.purap.document.PurchaseOrderAmendmentDocument;
38 import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
39 import org.kuali.ole.module.purap.document.service.*;
40 import org.kuali.ole.module.purap.document.service.impl.InvoiceServiceImpl;
41 import org.kuali.ole.module.purap.document.validation.event.AttributedCalculateAccountsPayableEvent;
42 import org.kuali.ole.module.purap.service.PurapAccountingService;
43 import org.kuali.ole.module.purap.util.ExpiredOrClosedAccountEntry;
44 import org.kuali.ole.pojo.OleInvoiceRecord;
45 import org.kuali.ole.select.OleSelectConstant;
46 import org.kuali.ole.select.OleSelectNotificationConstant;
47 import org.kuali.ole.select.bo.OleInvoiceEncumbranceNotification;
48 import org.kuali.ole.select.businessobject.*;
49 import org.kuali.ole.select.document.OleInvoiceDocument;
50 import org.kuali.ole.select.document.OlePaymentRequestDocument;
51 import org.kuali.ole.select.document.OlePurchaseOrderDocument;
52 import org.kuali.ole.select.document.OleVendorCreditMemoDocument;
53 import org.kuali.ole.select.document.service.OleCreditMemoService;
54 import org.kuali.ole.select.document.service.OleInvoiceService;
55 import org.kuali.ole.select.document.service.OlePurapAccountingService;
56 import org.kuali.ole.select.document.service.OleSelectDocumentService;
57 import org.kuali.ole.select.form.OLEInvoiceForm;
58 import org.kuali.ole.service.OleOrderRecordService;
59 import org.kuali.ole.sys.OLEConstants;
60 import org.kuali.ole.sys.OLEKeyConstants;
61 import org.kuali.ole.sys.OLEPropertyConstants;
62 import org.kuali.ole.sys.businessobject.Bank;
63 import org.kuali.ole.sys.businessobject.SourceAccountingLine;
64 import org.kuali.ole.sys.context.SpringContext;
65 import org.kuali.ole.sys.service.BankService;
66 import org.kuali.ole.sys.service.impl.OleParameterConstants;
67 import org.kuali.ole.vnd.businessobject.OleCurrencyType;
68 import org.kuali.ole.vnd.businessobject.OleExchangeRate;
69 import org.kuali.ole.vnd.businessobject.VendorAddress;
70 import org.kuali.ole.vnd.businessobject.VendorDetail;
71 import org.kuali.rice.core.api.config.property.ConfigurationService;
72 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
73 import org.kuali.rice.core.api.util.type.KualiDecimal;
74 import org.kuali.rice.core.web.format.CurrencyFormatter;
75 import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
76 import org.kuali.rice.coreservice.api.parameter.Parameter;
77 import org.kuali.rice.coreservice.api.parameter.ParameterKey;
78 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
79 import org.kuali.rice.kew.api.exception.WorkflowException;
80 import org.kuali.rice.kim.api.identity.Person;
81 import org.kuali.rice.kns.util.KNSGlobalVariables;
82 import org.kuali.rice.krad.UserSession;
83 import org.kuali.rice.krad.bo.Note;
84 import org.kuali.rice.krad.exception.ValidationException;
85 import org.kuali.rice.krad.service.*;
86 import org.kuali.rice.krad.util.ErrorMessage;
87 import org.kuali.rice.krad.util.GlobalVariables;
88 import org.kuali.rice.krad.util.ObjectUtils;
89 import org.springframework.util.AutoPopulatingList;
90
91 import java.math.BigDecimal;
92 import java.math.RoundingMode;
93 import java.text.ParseException;
94 import java.text.SimpleDateFormat;
95 import java.util.*;
96
97
98
99
100 public class OleInvoiceServiceImpl extends InvoiceServiceImpl implements OleInvoiceService {
101
102 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleInvoiceServiceImpl.class);
103
104 protected PurchaseOrderService purchaseOrderService;
105 protected PurapService purapService;
106 protected DocumentService documentService;
107 protected NoteService noteService;
108 private OlePurapAccountingService olePurapAccountingService;
109 private PurapAccountingService purapAccountingService;
110 protected ConfigurationService kualiConfigurationService;
111 List<OleInvoiceEncumbranceNotification> invoiceEncumbranceNotificationList;
112 private OLEBatchProcessProfileBo oleBatchProcessProfileBo;
113 private OleOrderRecordService oleOrderRecordService;
114 DataCarrierService dataCarrierService = GlobalResourceLoader.getService(org.kuali.ole.OLEConstants.DATA_CARRIER_SERVICE);
115 private OleSelectDocumentService oleSelectDocumentService;
116
117 public void setOleBatchProcessProfileBo(OLEBatchProcessProfileBo oleBatchProcessProfileBo) {
118 this.oleBatchProcessProfileBo = oleBatchProcessProfileBo;
119 }
120
121 protected BusinessObjectService getBusinessObjectService() {
122 if (businessObjectService == null) {
123 businessObjectService = SpringContext.getBean(BusinessObjectService.class);
124 }
125 return businessObjectService;
126 }
127
128 public OleOrderRecordService getOleOrderRecordService() {
129 if (oleOrderRecordService == null) {
130 oleOrderRecordService = SpringContext.getBean(OleOrderRecordService.class);
131 }
132 return oleOrderRecordService;
133 }
134
135
136 @Override
137 public void setPurapService(PurapService purapService) {
138 this.purapService = purapService;
139 }
140
141 @Override
142 public void setPurchaseOrderService(PurchaseOrderService purchaseOrderService) {
143 this.purchaseOrderService = purchaseOrderService;
144 }
145
146 @Override
147 public void setDocumentService(DocumentService documentService) {
148 this.documentService = documentService;
149 }
150
151 @Override
152 public void setNoteService(NoteService noteService) {
153 this.noteService = noteService;
154 }
155
156
157 @Override
158 public void setPurapAccountingService(PurapAccountingService purapAccountingService) {
159 this.purapAccountingService = purapAccountingService;
160 }
161
162 public void setOlePurapAccountingService(OlePurapAccountingService olePurapAccountingService) {
163 this.olePurapAccountingService = olePurapAccountingService;
164 }
165
166
167
168
169
170
171 @Override
172 public void completePaymentDocument(OleInvoiceDocument invoiceDocument) {
173 LOG.debug("Inside CompletePaymentDocument");
174
175 PurchaseOrderDocument poDoc = null;
176
177 if (invoiceDocument != null && invoiceDocument instanceof InvoiceDocument) {
178
179 purapService.deleteUnenteredItems(invoiceDocument);
180
181
182 }
183 updatePaymentTotalsOnPurchaseOrder(invoiceDocument);
184
185
186
187
188
189
190 purapService.saveDocumentNoValidation(invoiceDocument);
191
192 LOG.debug("Leaving CompletePaymentDocument");
193 }
194
195 protected void updatePaymentTotalsOnPurchaseOrder(OleInvoiceDocument invoiceDocument) {
196 LOG.debug("Inside updatePaymentTotalsOnPurchaseOrder");
197 List<Integer> poList = new ArrayList();
198 Integer invPoId = 0;
199 for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>) invoiceDocument.getItems()) {
200 if (!(poList.contains(invoiceItem.getPurchaseOrderIdentifier()))) {
201 poList.add(invoiceItem.getPurchaseOrderIdentifier());
202 }
203 }
204 for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>) invoiceDocument.getItems()) {
205 for (Integer purchaseOrderId : poList) {
206 if (invoiceItem.getItemType() != null && invoiceItem.getItemType().getItemTypeCode() != null && StringUtils.equalsIgnoreCase(invoiceItem.getItemType().getItemTypeCode(), PurapConstants.ItemTypeCodes.ITEM_TYPE_ITEM_CODE) &&
207 purchaseOrderId != null && invPoId != null && invPoId.compareTo(purchaseOrderId) != 0 && invoiceItem.getPurchaseOrderIdentifier().compareTo(purchaseOrderId) == '0') {
208 PurchaseOrderDocument poDoc = invoiceDocument.getPurchaseOrderDocument(purchaseOrderId);
209 OlePurchaseOrderItem poItem = (OlePurchaseOrderItem) poDoc.getItemByLineNumber(invoiceItem.getItemLineNumber());
210
211 if (ObjectUtils.isNotNull(poItem)) {
212
213 KualiDecimal poItemReceivedTotal = poItem.getOutstandingQuantity();
214
215 KualiDecimal itemQuantity = invoiceItem.getItemQuantity();
216
217 if (ObjectUtils.isNull(itemQuantity)) {
218 itemQuantity = KualiDecimal.ZERO;
219 }
220 if (ObjectUtils.isNull(poItemReceivedTotal)) {
221 poItemReceivedTotal = KualiDecimal.ZERO;
222 }
223
224
225
226
227 }
228 purapService.saveDocumentNoValidation(poDoc);
229 }
230 invPoId = purchaseOrderId;
231 }
232
233 }
234 LOG.debug("Leaving updatePaymentTotalsOnPurchaseOrder");
235 }
236
237
238
239
240
241
242
243
244 protected void spawnPoAmendmentForUnorderedItems(OleInvoiceDocument invoice, PurchaseOrderDocument po) {
245
246 LOG.debug("Inside spawnPoAmendmentForUnorderedItems");
247 if (invoice != null && invoice instanceof OleInvoiceDocument) {
248 OleInvoiceDocument rlDoc = invoice;
249
250
251 if (hasNewUnorderedItem(invoice)) {
252 String newSessionUserId = getOleSelectDocumentService().getSelectParameterValue(OLEConstants.SYSTEM_USER);
253 try {
254
255 LogicContainer logicToRun = new LogicContainer() {
256 @Override
257 public Object runLogic(Object[] objects) throws Exception {
258 OleInvoiceDocument rlDoc = (OleInvoiceDocument) objects[0];
259 String poDocNumber = (String) objects[1];
260
261
262 PurchaseOrderAmendmentDocument amendmentPo = (PurchaseOrderAmendmentDocument) purchaseOrderService.createAndSavePotentialChangeDocument(poDocNumber, PurchaseOrderDocTypes.PURCHASE_ORDER_AMENDMENT_DOCUMENT, PurchaseOrderStatuses.APPDOC_AMENDMENT);
263
264
265 addUnorderedItemsToAmendment(amendmentPo, rlDoc);
266
267
268 documentService.routeDocument(amendmentPo, null, null);
269
270
271 String note = "Purchase Order Amendment " + amendmentPo.getPurapDocumentIdentifier() + " (document id " + amendmentPo.getDocumentNumber() + ") created for new unordered line items (document id " + rlDoc.getDocumentNumber() + ")";
272
273 Note noteObj = documentService.createNoteFromDocument(amendmentPo, note);
274 amendmentPo.addNote(noteObj);
275 documentService.saveDocumentNotes(amendmentPo);
276 noteService.save(noteObj);
277
278 return null;
279 }
280 };
281
282 purapService.performLogicWithFakedUserSession(newSessionUserId, logicToRun, rlDoc, po.getDocumentNumber());
283 } catch (WorkflowException e) {
284 String errorMsg = "Workflow Exception caught: " + e.getLocalizedMessage();
285 throw new RuntimeException(errorMsg, e);
286 } catch (Exception e) {
287 throw new RuntimeException(e);
288 }
289 }
290 }
291 LOG.debug("Leaving spawnPoAmendmentForUnorderedItems");
292 }
293
294
295
296
297
298
299
300 protected boolean hasNewUnorderedItem(OleInvoiceDocument paymentDoc) {
301 LOG.debug("Inside hasNewUnorderedItem");
302 boolean itemAdded = false;
303
304 for (OleInvoiceItem prItem : (List<OleInvoiceItem>) paymentDoc.getItems()) {
305 if (PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE.equals(prItem.getItemTypeCode())) {
306 itemAdded = true;
307 break;
308 }
309 }
310 LOG.debug("Leaving hasNewUnorderedItem");
311 return itemAdded;
312 }
313
314
315
316
317
318
319
320 protected void addUnorderedItemsToAmendment(PurchaseOrderAmendmentDocument amendment, OleInvoiceDocument rlDoc) {
321
322 LOG.debug("Inside addUnorderedItemsToAmendment");
323 OlePurchaseOrderItem poi = null;
324
325 for (OleInvoiceItem rlItem : (List<OleInvoiceItem>) rlDoc.getItems()) {
326 if (PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE.equals(rlItem.getItemTypeCode())) {
327 poi = createPoItemFromPaymentLine(rlItem);
328 poi.setDocumentNumber(amendment.getDocumentNumber());
329 poi.refreshNonUpdateableReferences();
330 amendment.addItem(poi);
331 }
332 }
333 LOG.debug("Leaving addUnorderedItemsToAmendment");
334
335 }
336
337
338
339
340
341
342
343 protected OlePurchaseOrderItem createPoItemFromPaymentLine(OleInvoiceItem rlItem) {
344 LOG.debug("Inside createPoItemFromPaymentLine");
345 OlePurchaseOrderItem poi = new OlePurchaseOrderItem();
346 poi.setItemActiveIndicator(true);
347 poi.setItemTypeCode(rlItem.getItemTypeCode());
348 poi.setItemLineNumber(rlItem.getItemLineNumber());
349 poi.setItemCatalogNumber(rlItem.getItemCatalogNumber());
350 poi.setItemDescription(rlItem.getItemDescription());
351 poi.setItemQuantity(rlItem.getItemQuantity());
352 poi.setItemUnitOfMeasureCode(rlItem.getItemUnitOfMeasureCode());
353 poi.setItemUnitPrice(rlItem.getItemUnitPrice());
354
355 poi.setItemNoOfParts(rlItem.getItemNoOfParts());
356 poi.setItemListPrice(rlItem.getItemListPrice());
357 poi.setItemDiscount(rlItem.getItemDiscount());
358 poi.setItemDiscountType(rlItem.getItemDiscountType());
359 poi.setFormatTypeId(rlItem.getFormatTypeId());
360
361
362 poi.setItemCurrencyType(rlItem.getItemCurrencyType());
363 poi.setItemForeignListPrice((rlItem.getItemForeignListPrice()));
364 poi.setItemForeignDiscount((rlItem.getItemForeignDiscount()));
365 poi.setItemForeignDiscountAmt((rlItem.getItemForeignDiscountAmt()));
366 poi.setItemForeignDiscountType(rlItem.getItemForeignDiscountType());
367 poi.setItemForeignUnitCost((rlItem.getItemForeignUnitCost()));
368 poi.setItemExchangeRate((rlItem.getItemExchangeRate()));
369 poi.setItemUnitCostUSD((rlItem.getItemUnitCostUSD()));
370 poi.setItemInvoicedTotalAmount(rlItem.getTotalAmount());
371 poi.setBibInfoBean(rlItem.getBibInfoBean());
372 poi.setItemTitleId(rlItem.getItemTitleId());
373 setAccountingLinesFromPayment(rlItem, poi);
374
375 LOG.debug("Leaving createPoItemFromPaymentLine");
376 return poi;
377 }
378
379
380
381
382
383
384
385
386 public void setAccountingLinesFromPayment(OleInvoiceItem payItem, OlePurchaseOrderItem purItem) {
387 LOG.debug("Inside setAccountingLinesFromPayment");
388 for (int i = 0; i < payItem.getSourceAccountingLines().size(); i++) {
389
390
391 while (purItem.getSourceAccountingLines().size() < i + 1) {
392 PurchaseOrderAccount poAccount = new PurchaseOrderAccount();
393 poAccount.setPurchaseOrderItem(purItem);
394 purItem.getSourceAccountingLines().add(poAccount);
395 }
396 purItem.getSourceAccountingLine(i).copyFrom(payItem.getSourceAccountingLine(i));
397 purItem.getSourceAccountingLine(i).setAccountLinePercent(payItem.getSourceAccountingLine(i).getAccountLinePercent());
398 }
399 LOG.debug("Leaving setAccountingLinesFromPayment");
400 }
401
402
403
404
405 @Override
406 public void calculateProrateItemSurcharge(OleInvoiceDocument invoiceDocument) {
407 LOG.debug("Inside Calculation for ProrateItemSurcharge");
408
409 BigDecimal addChargeItem = BigDecimal.ZERO;
410 List<OleInvoiceItem> item = invoiceDocument.getItems();
411 for (OleInvoiceItem items : item) {
412 if (items.getItemType().isAdditionalChargeIndicator() && items.getExtendedPrice() != null) {
413 addChargeItem = addChargeItem.add(items.getExtendedPrice().bigDecimalValue());
414 }
415 }
416 List<PurApItem> items = new ArrayList<>();
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433 for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>) invoiceDocument.getItems()) {
434
435
436
437 items.add(invoiceItem);
438 }
439
440
441
442
443
444
445
446
447 List<BigDecimal> newUnitPriceList = new ArrayList<>();
448 BigDecimal totalExtPrice = new BigDecimal(0);
449 BigDecimal newUnitPrice = new BigDecimal(0);
450 BigDecimal extPrice = new BigDecimal(0);
451 BigDecimal unitPricePercent = new BigDecimal(0);
452 BigDecimal hundred = new BigDecimal(100);
453 BigDecimal one = new BigDecimal(1);
454 BigDecimal totalSurCharge = new BigDecimal(0);
455 BigDecimal totalItemQuantity = new BigDecimal(0);
456 BigDecimal itemSurchargeCons = new BigDecimal(0);
457 for (PurApItem purItem : items) {
458 if(purItem instanceof OlePurchaseOrderItem) {
459 OlePurchaseOrderItem poItem = (OlePurchaseOrderItem) purItem;
460
461 if ((poItem.getItemType().isQuantityBasedGeneralLedgerIndicator()) && !ObjectUtils.isNull(poItem.getNoOfCopiesInvoiced()) && poItem.getItemListPrice().compareTo(KualiDecimal.ZERO) >= 0) {
462 if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY)) {
463 totalItemQuantity = totalItemQuantity.add(poItem.getNoOfCopiesInvoiced().bigDecimalValue());
464 }
465 if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_DOLLAR) || invoiceDocument.getProrateBy().equals(OLEConstants.MANUAL_PRORATE) || invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY)) {
466 if (poItem.getItemDiscount() == null) {
467 poItem.setItemDiscount(KualiDecimal.ZERO);
468 }
469 if (poItem.getItemDiscountType() != null && poItem.getItemDiscountType().equalsIgnoreCase(OleSelectConstant.DISCOUNT_TYPE_PERCENTAGE)) {
470 newUnitPrice = (hundred.subtract(poItem.getItemDiscount().bigDecimalValue())).divide(hundred).multiply(poItem.getItemListPrice().bigDecimalValue());
471 } else {
472 newUnitPrice = poItem.getItemListPrice().bigDecimalValue().subtract(poItem.getItemDiscount().bigDecimalValue());
473 }
474 newUnitPriceList.add(newUnitPrice);
475 extPrice = newUnitPrice.multiply(poItem.getNoOfCopiesInvoiced().bigDecimalValue());
476 totalExtPrice = totalExtPrice.add(extPrice);
477 }
478 if (invoiceDocument.getProrateBy().equals(OLEConstants.MANUAL_PRORATE)) {
479 if (poItem.getItemSurcharge() == null) {
480 poItem.setItemSurcharge(BigDecimal.ZERO);
481 }
482 totalSurCharge = totalSurCharge.add(poItem.getNoOfCopiesInvoiced().bigDecimalValue().multiply(poItem.getItemSurcharge()));
483 }
484 }
485 }
486
487 else if(purItem instanceof OleInvoiceItem) {
488 OleInvoiceItem invItem = (OleInvoiceItem) purItem;
489 if ((invItem.getItemType().isQuantityBasedGeneralLedgerIndicator()) && !ObjectUtils.isNull(invItem.getItemQuantity()) && invItem.getItemListPrice().compareTo(KualiDecimal.ZERO) >= 0) {
490 if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY)) {
491 totalItemQuantity = totalItemQuantity.add(invItem.getItemQuantity().bigDecimalValue());
492 }
493 if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_DOLLAR) || invoiceDocument.getProrateBy().equals(OLEConstants.MANUAL_PRORATE) || invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY)) {
494 if (invItem.getItemDiscount() == null) {
495 invItem.setItemDiscount(KualiDecimal.ZERO);
496 }
497 if (invItem.getItemDiscountType() != null && invItem.getItemDiscountType().equalsIgnoreCase(OleSelectConstant.DISCOUNT_TYPE_PERCENTAGE)) {
498 newUnitPrice = (hundred.subtract(invItem.getItemDiscount().bigDecimalValue())).divide(hundred).multiply(invItem.getItemListPrice().bigDecimalValue());
499 } else {
500 newUnitPrice = invItem.getItemListPrice().bigDecimalValue().subtract(invItem.getItemDiscount().bigDecimalValue());
501 }
502 newUnitPriceList.add(newUnitPrice);
503 extPrice = newUnitPrice.multiply(invItem.getItemQuantity().bigDecimalValue());
504 totalExtPrice = totalExtPrice.add(extPrice);
505 }
506 if (invoiceDocument.getProrateBy().equals(OLEConstants.MANUAL_PRORATE)) {
507 if (invItem.getItemSurcharge() == null) {
508 invItem.setItemSurcharge(BigDecimal.ZERO);
509 }
510 totalSurCharge = totalSurCharge.add(invItem.getItemQuantity().bigDecimalValue().multiply(invItem.getItemSurcharge()));
511 }
512 }
513 }
514 }
515
516 if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY)) {
517 if (totalItemQuantity.compareTo(BigDecimal.ZERO) != 0) {
518 itemSurchargeCons = one.divide(totalItemQuantity, 8, RoundingMode.HALF_UP);
519 }
520 }
521 for (int i = 0, j = 0; items.size() > i; i++) {
522 PurApItem purItem = items.get(i);
523 if(purItem instanceof OlePurchaseOrderItem) {
524 OlePurchaseOrderItem poItem = (OlePurchaseOrderItem) purItem;
525
526 if (poItem.getItemType().isQuantityBasedGeneralLedgerIndicator() && newUnitPriceList.size() > j && !ObjectUtils.isNull(poItem.getNoOfCopiesInvoiced())) {
527 if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_DOLLAR)) {
528 if (totalExtPrice.compareTo(BigDecimal.ZERO) != 0) {
529 unitPricePercent = newUnitPriceList.get(j).divide(totalExtPrice, 8, RoundingMode.HALF_UP);
530 }
531 poItem.setItemSurcharge(unitPricePercent.multiply(addChargeItem).setScale(4, RoundingMode.HALF_UP));
532 poItem.setItemUnitPrice(newUnitPriceList.get(j).add(poItem.getItemSurcharge()));
533 }
534 if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY)) {
535 poItem.setItemSurcharge(itemSurchargeCons.multiply(addChargeItem).setScale(4, RoundingMode.HALF_UP));
536 poItem.setItemUnitPrice(newUnitPriceList.get(j).add(poItem.getItemSurcharge()));
537 }
538 if (invoiceDocument.getProrateBy().equals(OLEConstants.MANUAL_PRORATE) && poItem.getItemSurcharge() != null) {
539 poItem.setItemUnitPrice(newUnitPriceList.get(j).add(poItem.getItemSurcharge()));
540 }
541 j++;
542 }
543 }
544 else if(purItem instanceof OleInvoiceItem) {
545 OleInvoiceItem invItem = (OleInvoiceItem) purItem;
546 if (invItem.getItemType().isQuantityBasedGeneralLedgerIndicator() && newUnitPriceList.size() > j && !ObjectUtils.isNull(invItem.getItemQuantity())) {
547 if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_DOLLAR)) {
548 if (totalExtPrice.compareTo(BigDecimal.ZERO) != 0) {
549 unitPricePercent = newUnitPriceList.get(j).divide(totalExtPrice, 8, RoundingMode.HALF_UP);
550 }
551 invItem.setItemSurcharge(unitPricePercent.multiply(addChargeItem).setScale(4, RoundingMode.HALF_UP));
552 invItem.setItemUnitPrice(newUnitPriceList.get(j).add(invItem.getItemSurcharge()));
553 }
554 if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY)) {
555 invItem.setItemSurcharge(itemSurchargeCons.multiply(addChargeItem).setScale(4, RoundingMode.HALF_UP));
556 invItem.setItemUnitPrice(newUnitPriceList.get(j).add(invItem.getItemSurcharge()));
557 }
558 if (invoiceDocument.getProrateBy().equals(OLEConstants.MANUAL_PRORATE) && invItem.getItemSurcharge() != null) {
559 invItem.setItemUnitPrice(newUnitPriceList.get(j).add(invItem.getItemSurcharge()));
560 }
561 j++;
562 }
563
564 }
565 }
566 if (invoiceDocument.getProrateBy().equals(OLEConstants.MANUAL_PRORATE)) {
567 if (totalSurCharge.compareTo(addChargeItem) != 0) {
568 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, OLEKeyConstants.ERROR_PAYMENT_REQUEST_TOTAL_MISMATCH);
569 }
570 }
571 LOG.debug("Leaving Calculation for ProrateItemSurcharge");
572 }
573
574 @Override
575 public void calculateInvoice(InvoiceDocument invoice, boolean updateDiscount) {
576 LOG.debug("calculateInvoice() started");
577
578
579 if (ObjectUtils.isNull(invoice.getInvoicePayDate()) || invoice.getInvoicePayDate().before(new Date())) {
580 invoice.setInvoicePayDate(calculatePayDate(invoice.getInvoiceDate(), invoice.getVendorPaymentTerms()));
581 }
582
583 distributeAccounting(invoice);
584
585
586
587
588 purapService.prorateForTradeInAndFullOrderDiscount(invoice);
589
590
591 if (updateDiscount) {
592
593 }
594
595
596 }
597
598 @Override
599 protected void distributeAccounting(InvoiceDocument invoiceDocument) {
600
601 purapAccountingService.updateAccountAmounts(invoiceDocument);
602 OleInvoiceDocument oleInvoiceDocument = (OleInvoiceDocument) invoiceDocument;
603 for (InvoiceItem item : (List<InvoiceItem>) invoiceDocument.getItems()) {
604 KualiDecimal totalAmount = KualiDecimal.ZERO;
605 KualiDecimal totalQty = KualiDecimal.ZERO;
606 KualiDecimal totalSurcharge = KualiDecimal.ZERO;
607 List<PurApAccountingLine> distributedAccounts = null;
608 List<SourceAccountingLine> summaryAccounts = null;
609 Set excludedItemTypeCodes = new HashSet();
610 boolean canProrate = false;
611 excludedItemTypeCodes.add(PurapConstants.ItemTypeCodes.ITEM_TYPE_PMT_TERMS_DISCOUNT_CODE);
612
613
614 if (item.getItemType().isLineItemIndicator()) {
615 continue;
616 }
617
618
619
620 if ((ObjectUtils.isNotNull(item.getExtendedPrice())) && (KualiDecimal.ZERO.compareTo(item.getExtendedPrice()) != 0) && !"DISC".equals(item.getItemTypeCode())) {
621 if ((StringUtils.equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_PMT_TERMS_DISCOUNT_CODE, item.getItemType().getItemTypeCode())) && (invoiceDocument.getGrandTotal() != null) && ((KualiDecimal.ZERO.compareTo(invoiceDocument.getGrandTotal()) != 0))) {
622
623
624
625
626
627 totalAmount = invoiceDocument.getLineItemTotal();
628
629
630 Set includedItemTypeCodes = new HashSet();
631 includedItemTypeCodes.add(PurapConstants.ItemTypeCodes.ITEM_TYPE_ITEM_CODE);
632 summaryAccounts = olePurapAccountingService.generateSummaryIncludeItemTypesAndNoZeroTotals(invoiceDocument.getItems(), includedItemTypeCodes);
633 distributedAccounts = olePurapAccountingService.generateAccountDistributionForProration(summaryAccounts, totalAmount, PurapConstants.PRORATION_SCALE, InvoiceAccount.class);
634
635
636 purapAccountingService.updateAccountAmountsWithTotal(distributedAccounts, item.getTotalAmount());
637 } else {
638 OleInvoiceItem poi = null;
639 if (item.getItemType().isLineItemIndicator()) {
640 List<OleInvoiceItem> items = invoiceDocument.getItems();
641 poi = items.get(item.getItemLineNumber() - 1);
642
643
644
645
646
647
648
649 } else {
650 poi = (OleInvoiceItem) SpringContext.getBean(PurapService.class).getBelowTheLineByType(invoiceDocument, item.getItemType());
651 }
652
653 List<PurApItem> items = new ArrayList<>();
654
655
656
657
658
659
660
661
662
663
664
665 if (items.size() == 0) {
666 for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>) invoiceDocument.getItems()) {
667 if(invoiceItem.getItemType().isQuantityBasedGeneralLedgerIndicator() ) {
668
669 if (invoiceItem.getItemQuantity() != null) {
670 totalQty = totalQty.add(invoiceItem.getItemQuantity());
671 }
672 if (invoiceItem.getItemListPrice().isNonZero()) {
673 canProrate = true;
674 }
675 }
676
677 if (!(invoiceItem.getItemType().isQuantityBasedGeneralLedgerIndicator()) && invoiceItem.getExtendedPrice()!=null) {
678 totalSurcharge = totalSurcharge.add(invoiceItem.getExtendedPrice());
679 }
680 if (invoiceItem.getItemUnitPrice() != null ) {
681 if (invoiceItem.getItemUnitPrice().compareTo(BigDecimal.ZERO) != 0 || oleInvoiceDocument.isProrateQty()) {
682 items.add(invoiceItem);
683 }
684
685 }
686 }
687 }
688 else {
689 for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>) invoiceDocument.getItems()) {
690 if (!(invoiceItem.getItemType().isQuantityBasedGeneralLedgerIndicator())) {
691 if (invoiceItem.getItemUnitPrice() != null && invoiceItem.getItemUnitPrice().compareTo(BigDecimal.ZERO) != 0) {
692 items.add(invoiceItem);
693 }
694 if(invoiceItem.getExtendedPrice()!=null){
695 totalSurcharge = totalSurcharge.add(invoiceItem.getExtendedPrice());
696 }
697 }
698 }
699 }
700
701 totalAmount = getTotalDollarAmountWithExclusionsSubsetItems(null, true, items);
702 totalAmount = totalAmount.subtract(totalSurcharge);
703 purapAccountingService.updateAccountAmounts(invoiceDocument);
704 if(oleInvoiceDocument.isProrateManual()){
705 summaryAccounts = olePurapAccountingService.generateSummaryForManual(items);
706 distributedAccounts = olePurapAccountingService.generateAccountDistributionForProrationByManual(summaryAccounts,InvoiceAccount.class);
707 }
708
709 if(oleInvoiceDocument.isProrateDollar() || oleInvoiceDocument.isProrateQty()){
710 summaryAccounts = olePurapAccountingService.generateSummary(items);
711 }
712 if(oleInvoiceDocument.isProrateDollar() && totalAmount.isNonZero()){
713 distributedAccounts = olePurapAccountingService.generateAccountDistributionForProration(summaryAccounts, totalAmount, PurapConstants.PRORATION_SCALE, InvoiceAccount.class);
714 }else if(oleInvoiceDocument.isProrateQty() && totalQty.isNonZero()) {
715 distributedAccounts = olePurapAccountingService.generateAccountDistributionForProrationByQty(summaryAccounts, totalQty, PurapConstants.PRORATION_SCALE, InvoiceAccount.class);
716 }
717 }
718 if (CollectionUtils.isNotEmpty(distributedAccounts) && !oleInvoiceDocument.isProrateManual()) {
719 item.setSourceAccountingLines(distributedAccounts);
720 }
721 else if (!oleInvoiceDocument.isNoProrate() && !oleInvoiceDocument.isProrateManual()) {
722 item.setSourceAccountingLines(new ArrayList<PurApAccountingLine>());
723 }
724 }
725
726 purapAccountingService.updateItemAccountAmounts(item);
727
728 }
729
730 purapAccountingService.updateAccountAmounts(invoiceDocument);
731
732 }
733
734
735
736
737
738
739
740
741
742
743 public KualiDecimal getTotalDollarAmountWithExclusionsSubsetItems(String[] excludedTypes, boolean includeBelowTheLine, List<PurApItem> itemsForTotal) {
744 if (excludedTypes == null) {
745 excludedTypes = new String[]{};
746 }
747
748 KualiDecimal total = new KualiDecimal(BigDecimal.ZERO);
749 for (PurApItem item : itemsForTotal) {
750 item.refreshReferenceObject(PurapPropertyConstants.ITEM_TYPE);
751 ItemType it = item.getItemType();
752 if ((includeBelowTheLine || it.isLineItemIndicator()) && !ArrayUtils.contains(excludedTypes, it.getItemTypeCode())) {
753 KualiDecimal totalAmount = item.getTotalAmount();
754 KualiDecimal itemTotal = (totalAmount != null) ? totalAmount : KualiDecimal.ZERO;
755 total = total.add(itemTotal);
756 }
757 }
758 return total;
759 }
760
761
762
763
764 @Override
765 public OleInvoiceDocument getInvoiceByDocumentNumber(String documentNumber) {
766 LOG.debug("getInvoiceByDocumentNumber() started");
767
768 if (ObjectUtils.isNotNull(documentNumber)) {
769 try {
770 OleInvoiceDocument doc = (OleInvoiceDocument) documentService.getByDocumentHeaderId(documentNumber);
771 return doc;
772 } catch (WorkflowException e) {
773 String errorMessage = "Error getting invoice document from document service";
774 LOG.error("Exception While getting invoice document based on document number " + errorMessage, e);
775 throw new RuntimeException(errorMessage, e);
776 }
777 }
778 return null;
779 }
780
781
782
783
784
785
786 @Override
787 public boolean validateProratedSurcharge(OleInvoiceDocument invoiceDocument) {
788
789 List<OleInvoiceItem> items = invoiceDocument.getItems();
790 boolean manuvalProrateValidFlag = false;
791 BigDecimal proratedSurchargeAmount = new BigDecimal(0);
792 for (int i = 0; items.size() > i; i++) {
793 OleInvoiceItem item = (OleInvoiceItem) invoiceDocument.getItem(i);
794 if ("".equals(item.getItemTitleId()) || item.getItemTitleId() == null) {
795 if (item.getItemUnitPrice() != null && !"".equals(item.getItemUnitPrice())) {
796 manuvalProrateValidFlag = true;
797 }
798 }
799 }
800 if (manuvalProrateValidFlag) {
801 for (int i = 0; items.size() > i; i++) {
802 OleInvoiceItem item = (OleInvoiceItem) invoiceDocument.getItem(i);
803 if (!"".equals(item.getItemTitleId()) && item.getItemTitleId() != null) {
804 if (item.getItemSurcharge() != null && item.getItemSurcharge().compareTo(new BigDecimal(0)) != 0) {
805 proratedSurchargeAmount = proratedSurchargeAmount.add(item.getItemSurcharge());
806 }
807 }
808 }
809 if (proratedSurchargeAmount.compareTo(new BigDecimal(0)) == 0) {
810 manuvalProrateValidFlag = false;
811 invoiceDocument.setProrateBy(null);
812 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, OLEKeyConstants.ERROR_PAYMENT_REQUEST_TOTAL_MISMATCH);
813 }
814 }
815 return manuvalProrateValidFlag;
816 }
817
818
819
820
821
822
823 @Override
824 public void calculateWithoutProrates(OleInvoiceDocument invoiceDocument) {
825 LOG.debug("Inside Calculation for with out prorate");
826 BigDecimal addChargeItem = BigDecimal.ZERO;
827 List<OleInvoiceItem> items = invoiceDocument.getItems();
828
829 for (OleInvoiceItem item : items) {
830 if (item.getItemTitleId() != null && !"".equals(item.getItemTitleId())) {
831 if (!item.getItemListPrice().equals(item.getExtendedPrice())) {
832 item.setItemUnitPrice(item.getItemListPrice().bigDecimalValue());
833 item.setExtendedPrice(item.getItemListPrice());
834 item.setItemSurcharge(BigDecimal.ZERO);
835 }
836 }
837 }
838
839 for (OleInvoiceItem item : items) {
840 if (!item.getItemType().isQuantityBasedGeneralLedgerIndicator()
841 && !item.getItemTypeCode().equalsIgnoreCase(
842 PurapConstants.ItemTypeCodes.ITEM_TYPE_PMT_TERMS_DISCOUNT_CODE)
843 && item.getItemUnitPrice() != null) {
844 addChargeItem = addChargeItem.add(item.getItemUnitPrice());
845 }
846 }
847 List<BigDecimal> newUnitPriceList = new ArrayList<>();
848 BigDecimal totalExtPrice = new BigDecimal(0);
849 BigDecimal newUnitPrice = new BigDecimal(0);
850 BigDecimal extPrice = new BigDecimal(0);
851 BigDecimal unitPricePercent = new BigDecimal(0);
852 BigDecimal hundred = new BigDecimal(100);
853 BigDecimal one = new BigDecimal(1);
854 BigDecimal totalSurCharge = new BigDecimal(0);
855 BigDecimal totalItemQuantity = new BigDecimal(0);
856 BigDecimal itemSurchargeCons = new BigDecimal(0);
857 for (int i = 0; items.size() > i; i++) {
858 OleInvoiceItem item = (OleInvoiceItem) invoiceDocument.getItem(i);
859 if ((item.getItemType().isQuantityBasedGeneralLedgerIndicator())
860 && !ObjectUtils.isNull(item.getItemQuantity())) {
861 if (item.getItemSurcharge() == null) {
862 item.setItemSurcharge(BigDecimal.ZERO);
863 }
864 if (invoiceDocument.getProrateBy() == null) {
865 if (item.getItemDiscount() == null) {
866 item.setItemDiscount(KualiDecimal.ZERO);
867 }
868 if (item.getItemDiscountType() != null
869 && item.getItemDiscountType().equalsIgnoreCase(OleSelectConstant.DISCOUNT_TYPE_PERCENTAGE)) {
870 newUnitPrice =SpringContext.getBean(OlePurapService.class).calculateDiscount(item).setScale(2, BigDecimal.ROUND_HALF_UP);
871 } else {
872 newUnitPrice = SpringContext.getBean(OlePurapService.class).calculateDiscount(item).setScale(2, BigDecimal.ROUND_HALF_UP);
873 }
874 newUnitPriceList.add(newUnitPrice);
875 extPrice = newUnitPrice.multiply(item.getItemQuantity().bigDecimalValue());
876 totalExtPrice = totalExtPrice.add(extPrice);
877 }
878 totalSurCharge = totalSurCharge.add(item.getItemQuantity().bigDecimalValue()
879 .multiply(item.getItemSurcharge()));
880 }
881 }
882 for (int i = 0, j = 0; items.size() > i; i++) {
883 OleInvoiceItem item = (OleInvoiceItem) invoiceDocument.getItem(i);
884 if (item.getItemType().isQuantityBasedGeneralLedgerIndicator() && newUnitPriceList.size() > j
885 && !ObjectUtils.isNull(item.getItemQuantity())) {
886 if (item.getItemSurcharge() != null) {
887 item.setItemUnitPrice(newUnitPriceList.get(j).add(item.getItemSurcharge()));
888 }
889 j++;
890 }
891 }
892 LOG.debug("Leaving Calculation for with out prorate");
893 }
894
895 public void createPaymentRequestOrCreditMemoDocument(OleInvoiceDocument inv) {
896 List<OleInvoiceItem> negativeItems = new ArrayList<>();
897 List<OleInvoiceItem> positiveItems = new ArrayList<>();
898 List<OleInvoiceItem> lineItems = new ArrayList<>();
899 List<OleInvoiceItem> positiveLineItems = new ArrayList<>();
900 List<OleInvoiceItem> negativeLineItems = new ArrayList<>();
901 Boolean isItemLevelDebit = null;
902 Boolean isAdditionalChargeLevelDebit = null;
903 Boolean isItemLevelCredit = null;
904 Boolean isAdditionalChargeLevelCredit = null;
905 BigDecimal firstPOTotalUnitPrice=BigDecimal.ZERO;
906 for (OleInvoiceItem item : (List<OleInvoiceItem>) inv.getItems()) {
907 if (item.isDebitItem() &&
908 (item.getItemListPrice().isNonZero() ||
909 (item.getItemUnitPrice()!=null && item.getItemUnitPrice().compareTo(BigDecimal.ZERO)!=0))){
910
911 if(lineItems.size()==0 && item.getPurchaseOrderIdentifier()!=null){
912 lineItems.add(item);
913 firstPOTotalUnitPrice = firstPOTotalUnitPrice.add(item.getItemUnitPrice());
914 }else if(lineItems.size()>0 && item.getPurchaseOrderIdentifier()!=null &&
915 lineItems.get(0).getPurchaseOrderIdentifier().compareTo(item.getPurchaseOrderIdentifier())==0){
916 lineItems.add(item);
917 firstPOTotalUnitPrice = firstPOTotalUnitPrice.add(item.getItemUnitPrice());
918 }
919
920 if(item.getItemType().isQuantityBasedGeneralLedgerIndicator()){
921 if(isItemLevelDebit==null){
922 isItemLevelDebit = true;
923 }
924 isItemLevelDebit &= item.isDebitItem();
925
926 if(lineItems.size()>0 && item.getItemListPrice().isNonZero()){
927 positiveLineItems.add(item);
928 }
929 }
930 if(item.getItemType().isAdditionalChargeIndicator()){
931 if(isAdditionalChargeLevelDebit==null){
932 isAdditionalChargeLevelDebit = true;
933 }
934 isAdditionalChargeLevelDebit &= item.isDebitItem();
935 firstPOTotalUnitPrice = firstPOTotalUnitPrice.add(item.getItemUnitPrice());
936
937 }
938 if(item.getItemListPrice().isNonZero()){
939 positiveItems.add(item);
940 }
941
942 }
943 else if(!item.isDebitItem() &&
944 (item.getItemListPrice().isNonZero() ||
945 (item.getItemUnitPrice()!=null && item.getItemUnitPrice().compareTo(BigDecimal.ZERO)!=0))){
946
947 if(lineItems.size()==0 && item.getPurchaseOrderIdentifier()!=null){
948 lineItems.add(item);
949 firstPOTotalUnitPrice = firstPOTotalUnitPrice.subtract(item.getItemUnitPrice());
950 }else if(lineItems.size()>0 && item.getPurchaseOrderIdentifier()!=null &&
951 lineItems.get(0).getPurchaseOrderIdentifier().compareTo(item.getPurchaseOrderIdentifier())==0){
952 lineItems.add(item);
953 firstPOTotalUnitPrice = firstPOTotalUnitPrice.subtract(item.getItemUnitPrice());
954 }
955
956 if(item.getItemType().isQuantityBasedGeneralLedgerIndicator()){
957 if(isItemLevelCredit==null){
958 isItemLevelCredit = true;
959 }
960 isItemLevelCredit &= !item.isDebitItem();
961
962 if(lineItems.size()>0 && item.getItemListPrice().isNonZero()){
963 negativeLineItems.add(item);
964 }
965 }
966 if(item.getItemType().isAdditionalChargeIndicator()){
967 if(isAdditionalChargeLevelCredit==null){
968 isAdditionalChargeLevelCredit = true;
969 }
970 isAdditionalChargeLevelCredit &= !item.isDebitItem();
971 firstPOTotalUnitPrice = firstPOTotalUnitPrice.subtract(item.getItemUnitPrice());
972
973 }
974 if(item.getItemListPrice().isNonZero()){
975 negativeItems.add(item);
976 }
977 }
978 }
979 positiveLineItems.removeAll(lineItems);
980 negativeLineItems.removeAll(lineItems);
981 if((isItemLevelDebit == null && isAdditionalChargeLevelDebit!=null && isAdditionalChargeLevelDebit) ||
982 (isAdditionalChargeLevelDebit == null && isItemLevelDebit!=null && isItemLevelDebit) ||
983 (isItemLevelCredit == null && isAdditionalChargeLevelCredit!=null && isAdditionalChargeLevelCredit) ||
984 (isAdditionalChargeLevelCredit == null && isItemLevelCredit!=null && isItemLevelCredit)
985 && !(isItemLevelCredit!=null && isItemLevelCredit && isItemLevelDebit!=null && isItemLevelDebit)){
986 if(new KualiDecimal(firstPOTotalUnitPrice).isNegative()){
987 createCreditMemoDocument(inv, lineItems,true);
988 }else{
989 createPaymentRequestDocument(inv, lineItems,true);
990 }
991 if (positiveLineItems.size() > 0) {
992 createPaymentRequestDocument(inv, positiveLineItems,false);
993 }
994 if (negativeLineItems.size() > 0) {
995 createCreditMemoDocument(inv, negativeLineItems,false);
996 }
997 }else{
998 if (positiveItems.size() > 0) {
999 createPaymentRequestDocument(inv, positiveItems,false);
1000 }
1001 if (negativeItems.size() > 0) {
1002 createCreditMemoDocument(inv, negativeItems,false);
1003 }
1004 }
1005 }
1006
1007 public void createPaymentRequestDocument(OleInvoiceDocument inv, List<OleInvoiceItem> items,boolean flag) {
1008
1009 if (LOG.isDebugEnabled()) {
1010 LOG.debug("Creating Payment Request document");
1011 }
1012
1013 KNSGlobalVariables.getMessageList().clear();
1014
1015
1016
1017 if (LOG.isDebugEnabled()) {
1018 if (inv.isInvoiceCancelIndicator()) {
1019 LOG.debug("Not possible to convert cancelled Invoice details into payment request");
1020 } else {
1021 LOG.debug("Payment request document creation validation succeeded");
1022 }
1023 }
1024
1025 if (inv.isInvoiceCancelIndicator()) {
1026 LOG.debug("Invoice Cancelled, Payment Request not created");
1027 }
1028 if (LOG.isDebugEnabled()) {
1029 LOG.debug("Total Dollar Amount above line items >>>>" + inv.getTotalDollarAmountAboveLineItems());
1030 }
1031 Integer invId = inv.getPurapDocumentIdentifier();
1032 Integer poId = 0;
1033 List<Integer> poList = new ArrayList();
1034
1035 List<OleInvoiceItem> oleInvoiceItemList = items;
1036 KualiDecimal invItemCount = new KualiDecimal(0);
1037 for (OleInvoiceItem oleInvoiceItem : oleInvoiceItemList) {
1038 if (oleInvoiceItem.getItemTypeCode().equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_ITEM_CODE)) {
1039 invItemCount = invItemCount.add(oleInvoiceItem.getItemQuantity());
1040 }
1041 if ((!(poList.contains(oleInvoiceItem.getPurchaseOrderIdentifier()))) && oleInvoiceItem.getExtendedPrice().isNonZero()) {
1042 poList.add(oleInvoiceItem.getPurchaseOrderIdentifier());
1043 }
1044 }
1045
1046 String prorateBy = inv.getProrateBy();
1047
1048 OlePaymentRequestDocument preqDoc = null;
1049 Integer invPoId = 0;
1050 for (Integer purchaseOrderId : poList) {
1051 if (purchaseOrderId != null && invPoId.compareTo(purchaseOrderId) != 0) {
1052 try {
1053 preqDoc = (OlePaymentRequestDocument) SpringContext.getBean(DocumentService.class).getNewDocument("OLE_PREQ");
1054 preqDoc.setImmediatePaymentIndicator(inv.getImmediatePaymentIndicator());
1055 preqDoc.setInvoiceDate(inv.getInvoiceDate());
1056 preqDoc.setInvoiceNumber(inv.getInvoiceNumber());
1057 preqDoc.setVendorInvoiceAmount(inv.getVendorInvoiceAmount().abs());
1058 preqDoc.setVendorDetail(inv.getVendorDetail());
1059 preqDoc.setVendorName(inv.getVendorName());
1060 preqDoc.setVendorHeaderGeneratedIdentifier(inv.getVendorHeaderGeneratedIdentifier());
1061 preqDoc.setVendorDetailAssignedIdentifier(inv.getVendorDetailAssignedIdentifier());
1062 preqDoc.setVendorNumber(inv.getVendorNumber());
1063 preqDoc.setVendorHeaderGeneratedIdentifier(inv.getVendorHeaderGeneratedIdentifier());
1064 preqDoc.setVendorDetailAssignedIdentifier(inv.getVendorDetailAssignedIdentifier());
1065 preqDoc.setVendorPaymentTerms(inv.getVendorPaymentTerms());
1066 if (inv.getVendorPaymentTerms() != null) {
1067 preqDoc.setVendorPaymentTermsCode(inv.getVendorPaymentTerms().getVendorPaymentTermsCode());
1068 }
1069 preqDoc.setVendorShippingTitleCode(inv.getVendorShippingTitleCode());
1070 preqDoc.setVendorShippingPaymentTerms(inv.getVendorShippingPaymentTerms());
1071 preqDoc.setVendorCityName(inv.getVendorCityName());
1072 preqDoc.setVendorLine1Address(inv.getVendorLine1Address());
1073 preqDoc.setVendorLine2Address(inv.getVendorLine2Address());
1074 preqDoc.setVendorAttentionName(inv.getVendorAttentionName());
1075 preqDoc.setVendorPostalCode(inv.getVendorPostalCode());
1076 preqDoc.setVendorStateCode(inv.getVendorStateCode());
1077 preqDoc.setVendorAttentionName(inv.getVendorAttentionName());
1078 preqDoc.setVendorAddressInternationalProvinceName(inv.getVendorAddressInternationalProvinceName());
1079 preqDoc.setVendorCountryCode(inv.getVendorCountryCode());
1080 preqDoc.setVendorCountry(inv.getVendorCountry());
1081 preqDoc.setVendorCustomerNumber(inv.getVendorCustomerNumber());
1082 preqDoc.setAccountsPayableProcessorIdentifier(inv.getAccountsPayableProcessorIdentifier());
1083 preqDoc.setProcessingCampusCode(inv.getProcessingCampusCode());
1084 preqDoc.setPurchaseOrderIdentifier(purchaseOrderId);
1085
1086
1087 preqDoc.setPaymentRequestPayDate(inv.getInvoicePayDate());
1088 preqDoc.setImmediatePaymentIndicator(inv.getImmediatePaymentIndicator());
1089 preqDoc.setPaymentRequestCostSource(inv.getInvoiceCostSource());
1090 preqDoc.setProrateBy(inv.getProrateBy());
1091 preqDoc.setProrateDollar(inv.isProrateDollar());
1092 preqDoc.setProrateQty(inv.isProrateQty());
1093 preqDoc.setProrateManual(inv.isProrateManual());
1094 preqDoc.setNoProrate(inv.isNoProrate());
1095 preqDoc.setForeignVendorInvoiceAmount(inv.getForeignVendorInvoiceAmount());
1096
1097
1098 if (inv.getPaymentMethodId() != null) {
1099 OlePaymentMethod olePaymentMethod = SpringContext.getBean(BusinessObjectService.class)
1100 .findBySinglePrimaryKey(OlePaymentMethod.class, inv.getPaymentMethodId());
1101 preqDoc.setPaymentMethod(olePaymentMethod);
1102 preqDoc.getPaymentMethod().setPaymentMethodId(olePaymentMethod.getPaymentMethodId());
1103 preqDoc.setPaymentMethodId(olePaymentMethod.getPaymentMethodId());
1104 }
1105
1106 preqDoc.setInvoiceIdentifier(inv.getPurapDocumentIdentifier());
1107 preqDoc.setBankCode(inv.getBankCode());
1108 preqDoc.setBank(inv.getBank());
1109 } catch (WorkflowException e) {
1110 String extraDescription = "Error=" + e.getMessage();
1111 LOG.error("Exception creating Payment request document - " + e.getMessage());
1112 }
1113
1114 Map invItemMap = new HashMap();
1115 invItemMap.put(PurapConstants.PRQSDocumentsStrings.PUR_ID, inv.getPurapDocumentIdentifier());
1116 invItemMap.put(PurapConstants.PRQSDocumentsStrings.PO_ID, purchaseOrderId);
1117 List<OleInvoiceItem> invoiceItems = (List<OleInvoiceItem>) businessObjectService.findMatchingOrderBy(OleInvoiceItem.class, invItemMap, PurapConstants.PRQSDocumentsStrings.PO_ID, true);
1118
1119 KualiDecimal itemCount = new KualiDecimal(0);
1120 KualiDecimal itemPrice = new KualiDecimal(0);
1121 PurchaseOrderDocument poDoc = inv.getPurchaseOrderDocument(purchaseOrderId);
1122 if (poDoc == null) {
1123 throw new RuntimeException("Purchase Order document (invPoId=" + invPoId + ") does not exist in the system");
1124 }
1125
1126 preqDoc.getDocumentHeader().setDocumentDescription(createPreqDocumentDescription(poDoc.getPurapDocumentIdentifier(), inv.getVendorName()));
1127
1128 try {
1129 preqDoc.updateAndSaveAppDocStatus(PurapConstants.PaymentRequestStatuses.APPDOC_IN_PROCESS);
1130 } catch (WorkflowException we) {
1131 throw new RuntimeException("Unable to save route status data for document: " + preqDoc.getDocumentNumber(), we);
1132 }
1133
1134 SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedCalculateAccountsPayableEvent(preqDoc));
1135
1136 SpringContext.getBean(PaymentRequestService.class).calculatePaymentRequest(preqDoc, false);
1137 HashMap<String, ExpiredOrClosedAccountEntry> expiredOrClosedAccountList = SpringContext.getBean(AccountsPayableService.class).expiredOrClosedAccountsList(poDoc);
1138 if (expiredOrClosedAccountList == null) {
1139 expiredOrClosedAccountList = new HashMap();
1140 }
1141
1142 if (LOG.isDebugEnabled()) {
1143 LOG.debug(expiredOrClosedAccountList.size() + " accounts has been found as Expired or Closed");
1144 }
1145 List<OlePaymentRequestItem> olePaymentRequestItems = new ArrayList<>();
1146
1147 for (OleInvoiceItem invoiceItem : invoiceItems) {
1148 if ((flag || invoiceItem.isDebitItem()) && invoiceItem.getExtendedPrice().isNonZero()) {
1149 OlePaymentRequestItem olePaymentRequestItem = new OlePaymentRequestItem(invoiceItem, preqDoc, expiredOrClosedAccountList);
1150 if(flag && !invoiceItem.isDebitItem()){
1151 olePaymentRequestItem.setItemListPrice(olePaymentRequestItem.getItemListPrice().negated());
1152 olePaymentRequestItem.setItemUnitPrice(olePaymentRequestItem.getItemUnitPrice().negate());
1153 olePaymentRequestItem.setExtendedPrice(olePaymentRequestItem.getExtendedPrice().negated());
1154 for(PurApAccountingLine purApAccountingLine : olePaymentRequestItem.getSourceAccountingLines()){
1155 purApAccountingLine.setAmount(purApAccountingLine.getAmount().negated());
1156 }
1157 }
1158 olePaymentRequestItems.add(olePaymentRequestItem);
1159 if (invoiceItem.isReopenPurchaseOrderIndicator()) {
1160 preqDoc.setReopenPurchaseOrderIndicator(invoiceItem.isReopenPurchaseOrderIndicator());
1161 }
1162 if (invoiceItem.isClosePurchaseOrderIndicator()) {
1163 preqDoc.setClosePurchaseOrderIndicator(invoiceItem.isClosePurchaseOrderIndicator());
1164 }
1165 if (preqDoc.getAccountsPayablePurchasingDocumentLinkIdentifier() == null) {
1166 preqDoc.setAccountsPayablePurchasingDocumentLinkIdentifier(invoiceItem.getAccountsPayablePurchasingDocumentLinkIdentifier());
1167 }
1168 preqDoc.setReceivingDocumentRequiredIndicator(invoiceItem.isReceivingDocumentRequiredIndicator());
1169 }
1170 }
1171
1172 invPoId = purchaseOrderId;
1173 preqDoc.setItems(olePaymentRequestItems);
1174
1175 try {
1176 SpringContext.getBean(PaymentRequestService.class).populateAndSavePaymentRequest(preqDoc);
1177
1178 SpringContext.getBean(PaymentRequestService.class).autoApprovePaymentRequest(preqDoc);
1179 } catch (WorkflowException e) {
1180 e.printStackTrace();
1181 } catch (ValidationException e) {
1182 String extraDescription = GlobalVariables.getMessageMap().toString();
1183 }
1184 }
1185
1186
1187 if (GlobalVariables.getMessageMap().hasErrors()) {
1188
1189 LOG.error("***************Error in rules processing - " + GlobalVariables.getMessageMap());
1190 Map<String, AutoPopulatingList<ErrorMessage>> errorMessages = GlobalVariables.getMessageMap().getErrorMessages();
1191
1192 String errors = errorMessages.toString();
1193 }
1194
1195 if (KNSGlobalVariables.getMessageList().size() > 0) {
1196 if (LOG.isDebugEnabled()) {
1197 LOG.debug("Payment request contains " + KNSGlobalVariables.getMessageList().size() + " warning message(s)");
1198 for (int i = 0; i < KNSGlobalVariables.getMessageList().size(); i++) {
1199 LOG.debug("Warning " + i + " - " + KNSGlobalVariables.getMessageList().get(i));
1200 }
1201 }
1202 }
1203
1204
1205 String routingAnnotation = null;
1206 if (!inv.isInvoiceCancelIndicator()) {
1207 routingAnnotation = "Routed by New Invoice Creation";
1208 }
1209 }
1210 }
1211
1212
1213 public void populateInvoice(OleInvoiceDocument invoiceDocument) {
1214
1215 PurchaseOrderDocument purchaseOrderDocument = invoiceDocument.getPurchaseOrderDocument();
1216
1217
1218 HashMap<String, ExpiredOrClosedAccountEntry> expiredOrClosedAccountList = getAccountsPayableService().getExpiredOrClosedAccountList(invoiceDocument);
1219
1220
1221
1222
1223
1224
1225
1226 getAccountsPayableService().generateExpiredOrClosedAccountNote(invoiceDocument, expiredOrClosedAccountList);
1227
1228
1229 if (!expiredOrClosedAccountList.isEmpty()) {
1230 invoiceDocument.setContinuationAccountIndicator(true);
1231 }
1232
1233
1234 calculateDiscount(invoiceDocument);
1235
1236 distributeAccounting(invoiceDocument);
1237
1238
1239 Bank defaultBank = bankService.getDefaultBankByDocType(invoiceDocument.getClass());
1240 if (defaultBank != null) {
1241 invoiceDocument.setBankCode(defaultBank.getBankCode());
1242 invoiceDocument.setBank(defaultBank);
1243 }
1244 }
1245
1246
1247
1248
1249
1250 public OleInvoiceDocument populateInvoiceFromPurchaseOrders(OleInvoiceDocument invoiceDocument,
1251 HashMap<String, ExpiredOrClosedAccountEntry> expiredOrClosedAccountList) {
1252 LOG.debug("Inside populateInvoiceFromPurchaseOrders method ");
1253 List<OleInvoiceItem> items = new ArrayList<>();
1254 if (expiredOrClosedAccountList == null) {
1255 expiredOrClosedAccountList = new HashMap<>();
1256 }
1257
1258 List<OleInvoiceItem> invoiceItemList = new ArrayList<>();
1259 OleInvoiceItem oleInvoiceItem;
1260
1261 for (int count = 0; count < invoiceDocument.getItems().size(); count++) {
1262 OleInvoiceItem invoiceditem = (OleInvoiceItem) invoiceDocument.getItems().get(count);
1263 if (!(invoiceditem.getItemType().getItemTypeCode().equals("ITEM"))) {
1264 invoiceItemList.add(invoiceditem);
1265 }
1266 }
1267 invoiceDocument.setItems(invoiceItemList);
1268 if (invoiceDocument.getPurchaseOrderDocuments() != null && invoiceDocument.getPurchaseOrderDocuments().size() > 0) {
1269 for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>) invoiceDocument.getItems()) {
1270 if (invoiceItem.getItemType().isAdditionalChargeIndicator() && invoiceItem.getItemUnitPrice() != null)
1271 invoiceItem.setPurchaseOrderIdentifier(invoiceDocument.getPurchaseOrderDocuments().get(0).getPurapDocumentIdentifier());
1272 }
1273 }
1274
1275 for (OlePurchaseOrderDocument po : invoiceDocument.getPurchaseOrderDocuments()) {
1276
1277
1278 for (OlePurchaseOrderItem poi : (List<OlePurchaseOrderItem>) po.getItems()) {
1279
1280
1281 if (poi.isItemForInvoice()) {
1282 OleInvoiceItem invoiceItem = new OleInvoiceItem(poi, invoiceDocument, expiredOrClosedAccountList);
1283
1284 invoiceItem.setClosePurchaseOrderIndicator(po.isClosePO());
1285 invoiceItem.setReopenPurchaseOrderIndicator(po.getIsReOpenPO());
1286 PurchasingCapitalAssetItem purchasingCAMSItem = po.getPurchasingCapitalAssetItemByItemIdentifier(poi.getItemIdentifier());
1287 if (purchasingCAMSItem != null) {
1288 invoiceItem.setCapitalAssetTransactionTypeCode(purchasingCAMSItem.getCapitalAssetTransactionTypeCode());
1289 }
1290 invoiceItem.setUseTaxIndicator(po.isUseTaxIndicator());
1291 invoiceItem.setPurchaseOrderIdentifier(po.getPurapDocumentIdentifier());
1292 invoiceItem.setPostingYear(po.getPostingYear());
1293 invoiceItem.setAccountsPayablePurchasingDocumentLinkIdentifier(po.getAccountsPayablePurchasingDocumentLinkIdentifier());
1294
1295 invoiceItem.getUseTaxItems().clear();
1296 for (PurApItemUseTax useTax : poi.getUseTaxItems()) {
1297 invoiceItem.getUseTaxItems().add(useTax);
1298 }
1299 invoiceDocument.getItems().add(invoiceItem);
1300 if (LOG.isDebugEnabled()) {
1301 LOG.debug("Size**********************" + invoiceDocument.getItems().size());
1302 }
1303 }
1304
1305
1306
1307 }
1308
1309 invoiceDocument.setInvoicePayDate(calculatePayDate(invoiceDocument.getInvoiceDate(), invoiceDocument.getVendorPaymentTerms()));
1310 invoiceDocument.setTotalDollarAmount(invoiceDocument.getTotalDollarAmount().add(po.getTotalDollarAmount()));
1311
1312 if (invoiceDocument.getInvoiceTypeHdnId() != null && !invoiceDocument.getInvoiceTypeHdnId().isEmpty()) {
1313 invoiceDocument.setInvoiceTypeId(Integer.valueOf(invoiceDocument.getInvoiceTypeHdnId()));
1314 }
1315 if (invoiceDocument.getPaymentMethodIdentifier() != null && !invoiceDocument.getPaymentMethodIdentifier().isEmpty()) {
1316 invoiceDocument.setPaymentMethodId(Integer.valueOf(invoiceDocument.getPaymentMethodIdentifier()));
1317 }
1318 if (invoiceDocument.getInvoiceSubTypeHdnId() != null && !invoiceDocument.getInvoiceSubTypeHdnId().isEmpty()) {
1319 invoiceDocument.setInvoiceSubTypeId(Integer.valueOf(invoiceDocument.getInvoiceSubTypeHdnId()));
1320 }
1321
1322 if (invoiceDocument.getInvoiceAmount() != null && !invoiceDocument.getInvoiceAmount().isEmpty()) {
1323 invoiceDocument.setVendorInvoiceAmount(new KualiDecimal(invoiceDocument.getInvoiceAmount()));
1324 }
1325
1326 if (invoiceDocument.getForeignInvoiceAmount() != null && !invoiceDocument.getForeignInvoiceAmount().isEmpty()) {
1327 invoiceDocument.setForeignVendorInvoiceAmount(new KualiDecimal(invoiceDocument.getForeignInvoiceAmount()).bigDecimalValue());
1328 if (StringUtils.isNotBlank(invoiceDocument.getInvoiceCurrencyType())) {
1329 if (StringUtils.isNotBlank(invoiceDocument.getInvoiceCurrencyExchangeRate())) {
1330 try {
1331 Double.parseDouble(invoiceDocument.getInvoiceCurrencyExchangeRate());
1332 invoiceDocument.setVendorInvoiceAmount(new KualiDecimal(invoiceDocument.getForeignVendorInvoiceAmount().divide(new BigDecimal(invoiceDocument.getInvoiceCurrencyExchangeRate()), 4, RoundingMode.HALF_UP)));
1333 }
1334 catch (NumberFormatException nfe) {
1335 throw new RuntimeException("Invalid Exchange Rate", nfe);
1336 }
1337 } else {
1338 BigDecimal exchangeRate = getExchangeRate(invoiceDocument.getInvoiceCurrencyType()).getExchangeRate();
1339 invoiceDocument.setVendorInvoiceAmount(new KualiDecimal(invoiceDocument.getForeignVendorInvoiceAmount().divide(exchangeRate, 4, RoundingMode.HALF_UP)));
1340 }
1341 }
1342 }
1343
1344 Bank defaultBank = SpringContext.getBean(BankService.class).getDefaultBankByDocType(invoiceDocument.getClass());
1345 if (defaultBank != null) {
1346 invoiceDocument.setBankCode(defaultBank.getBankCode());
1347 invoiceDocument.setBank(defaultBank);
1348 }
1349 }
1350
1351
1352 invoiceDocument.setInvoicePayDate(this.calculatePayDate(invoiceDocument.getInvoiceDate(), invoiceDocument.getVendorPaymentTerms()));
1353
1354
1355
1356
1357 BigDecimal addChargeItem=BigDecimal.ZERO;
1358 List<OleInvoiceItem> item = invoiceDocument.getItems();
1359 for(OleInvoiceItem invoiceditem : item){
1360 if(invoiceditem.getItemType().isAdditionalChargeIndicator() && invoiceditem.getExtendedPrice()!=null){
1361 addChargeItem =addChargeItem.add(invoiceditem.getExtendedPrice().bigDecimalValue());
1362 }
1363 }
1364 if (invoiceDocument.getTotalDollarAmount() != null ) {
1365 invoiceDocument.setInvoiceItemTotal(invoiceDocument.getTotalDollarAmount().subtract(new KualiDecimal(addChargeItem)).toString());
1366 invoiceDocument.setDocumentTotalAmount(invoiceDocument.getTotalDollarAmount().toString());
1367 }
1368
1369
1370
1371
1372
1373
1374
1375
1376 LOG.debug("Leaving populateInvoiceFromPurchaseOrders method ");
1377 return invoiceDocument;
1378 }
1379
1380 public String saveInvoiceDocument(OleInvoiceDocument invoiceDocument) throws WorkflowException {
1381
1382 Long nextLinkIdentifier = SpringContext.getBean(SequenceAccessorService.class).getNextAvailableSequenceNumber("AP_PUR_DOC_LNK_ID");
1383 invoiceDocument.setAccountsPayablePurchasingDocumentLinkIdentifier(nextLinkIdentifier.intValue());
1384
1385 Bank defaultBank = SpringContext.getBean(BankService.class).getDefaultBankByDocType(invoiceDocument.getClass());
1386 if (defaultBank != null) {
1387 invoiceDocument.setBankCode(defaultBank.getBankCode());
1388 invoiceDocument.setBank(defaultBank);
1389 }
1390 Person currentUser = GlobalVariables.getUserSession().getPerson();
1391 invoiceDocument.setAccountsPayableProcessorIdentifier(currentUser.getPrincipalId());
1392 invoiceDocument.setProcessingCampusCode(currentUser.getCampusCode());
1393 DocumentService documentService = GlobalResourceLoader.getService(org.kuali.ole.OLEConstants.DOCUMENT_HEADER_SERVICE);
1394 invoiceDocument = (OleInvoiceDocument) documentService.saveDocument(invoiceDocument);
1395
1396
1397 return invoiceDocument.getDocumentNumber();
1398
1399 }
1400
1401 public String routeInvoiceDocument(OleInvoiceDocument invoiceDocument) throws WorkflowException {
1402
1403 Long nextLinkIdentifier = SpringContext.getBean(SequenceAccessorService.class).getNextAvailableSequenceNumber("AP_PUR_DOC_LNK_ID");
1404 invoiceDocument.setAccountsPayablePurchasingDocumentLinkIdentifier(nextLinkIdentifier.intValue());
1405
1406 Bank defaultBank = SpringContext.getBean(BankService.class).getDefaultBankByDocType(invoiceDocument.getClass());
1407 if (defaultBank != null) {
1408 invoiceDocument.setBankCode(defaultBank.getBankCode());
1409 invoiceDocument.setBank(defaultBank);
1410 }
1411 Person currentUser = GlobalVariables.getUserSession().getPerson();
1412 invoiceDocument.setAccountsPayableProcessorIdentifier(currentUser.getPrincipalId());
1413 invoiceDocument.setProcessingCampusCode(currentUser.getCampusCode());
1414
1415 DocumentService documentService = GlobalResourceLoader.getService(org.kuali.ole.OLEConstants.DOCUMENT_HEADER_SERVICE);
1416 invoiceDocument = (OleInvoiceDocument) documentService.routeDocument(invoiceDocument, null, null);
1417
1418
1419 return invoiceDocument.getDocumentNumber();
1420
1421 }
1422
1423
1424 public OleInvoiceDocument populateVendorDetail(String vendorNumber, OleInvoiceDocument oleInvoiceDocument) {
1425 Map<String, String> criteria = new HashMap<>();
1426 String[] vendorIds = vendorNumber != null ? vendorNumber.split("-") : new String[0];
1427 criteria.put(OLEConstants.InvoiceDocument.VENDOR_HEADER_IDENTIFIER, vendorIds.length > 0 ? vendorIds[0] : "");
1428 criteria.put(OLEConstants.InvoiceDocument.VENDOR_DETAIL_IDENTIFIER, vendorIds.length > 1 ? vendorIds[1] : "");
1429 VendorDetail vendorDetail = getBusinessObjectService().findByPrimaryKey(VendorDetail.class, criteria);
1430 if (vendorDetail != null) {
1431 oleInvoiceDocument.setVendorDetail(vendorDetail);
1432 oleInvoiceDocument.setVendorName(vendorDetail.getVendorName());
1433 oleInvoiceDocument.setVendorHeaderGeneratedIdentifier(vendorDetail.getVendorHeaderGeneratedIdentifier());
1434 oleInvoiceDocument.setVendorDetailAssignedIdentifier(vendorDetail.getVendorDetailAssignedIdentifier());
1435 oleInvoiceDocument.setVendorNumber(vendorDetail.getVendorNumber());
1436 oleInvoiceDocument.setVendorHeaderGeneratedIdentifier(vendorDetail.getVendorHeaderGeneratedIdentifier());
1437 oleInvoiceDocument.setVendorDetailAssignedIdentifier(vendorDetail.getVendorDetailAssignedIdentifier());
1438 oleInvoiceDocument.setVendorFaxNumber(vendorDetail.getDefaultFaxNumber());
1439
1440 if (vendorDetail.getPaymentMethodId() != null) {
1441 oleInvoiceDocument.setPaymentMethodIdentifier(vendorDetail.getPaymentMethodId().toString());
1442 oleInvoiceDocument.setPaymentMethodId(vendorDetail.getPaymentMethodId());
1443 }
1444
1445 if (vendorDetail.getVendorPaymentTerms() != null) {
1446 oleInvoiceDocument.setVendorPaymentTerms(vendorDetail.getVendorPaymentTerms());
1447 oleInvoiceDocument.setVendorPaymentTermsCode(vendorDetail.getVendorPaymentTerms().getVendorPaymentTermsCode());
1448
1449 }
1450 if (vendorDetail.getVendorShippingTitle() != null) {
1451 oleInvoiceDocument.setVendorShippingTitleCode(vendorDetail.getVendorShippingTitle().getVendorShippingTitleCode());
1452 }
1453 if (vendorDetail.getVendorShippingPaymentTerms() != null) {
1454 oleInvoiceDocument.setVendorShippingPaymentTerms(vendorDetail.getVendorShippingPaymentTerms());
1455 }
1456
1457 for (VendorAddress vendorAddress : vendorDetail.getVendorAddresses()) {
1458 if (vendorAddress.isVendorDefaultAddressIndicator()) {
1459 oleInvoiceDocument.setVendorCityName(vendorAddress.getVendorCityName());
1460 oleInvoiceDocument.setVendorLine1Address(vendorAddress.getVendorLine1Address());
1461 oleInvoiceDocument.setVendorLine2Address(vendorAddress.getVendorLine2Address());
1462 oleInvoiceDocument.setVendorAttentionName(vendorAddress.getVendorAttentionName());
1463 oleInvoiceDocument.setVendorPostalCode(vendorAddress.getVendorZipCode());
1464 oleInvoiceDocument.setVendorStateCode(vendorAddress.getVendorStateCode());
1465 oleInvoiceDocument.setVendorAttentionName(vendorAddress.getVendorAttentionName());
1466 oleInvoiceDocument.setVendorAddressInternationalProvinceName(vendorAddress.getVendorAddressInternationalProvinceName());
1467 oleInvoiceDocument.setVendorCountryCode(vendorAddress.getVendorCountryCode());
1468 oleInvoiceDocument.setVendorCountry(vendorAddress.getVendorCountry());
1469
1470 }
1471 }
1472 }
1473
1474 return oleInvoiceDocument;
1475 }
1476
1477
1478 public void createCreditMemoDocument(OleInvoiceDocument invoiceDocument, List<OleInvoiceItem> items,boolean flag) {
1479 if (LOG.isDebugEnabled()) {
1480 LOG.debug("Creating Payment Request document");
1481 }
1482
1483 KNSGlobalVariables.getMessageList().clear();
1484
1485
1486
1487 if (LOG.isDebugEnabled()) {
1488 if (invoiceDocument.isInvoiceCancelIndicator()) {
1489 LOG.debug("Not possible to convert cancelled Invoice details into payment request");
1490 } else {
1491 LOG.debug("Payment request document creation validation succeeded");
1492 }
1493 }
1494
1495 if (invoiceDocument.isInvoiceCancelIndicator()) {
1496 LOG.debug("Invoice Cancelled, Payment Request not created");
1497 }
1498 if (LOG.isDebugEnabled()) {
1499 LOG.debug("Total Dollar Amount above line items >>>>" + invoiceDocument.getTotalDollarAmountAboveLineItems());
1500 }
1501 Integer invId = invoiceDocument.getPurapDocumentIdentifier();
1502 Integer poId = 0;
1503 List<Integer> poList = new ArrayList();
1504
1505 List<OleInvoiceItem> oleInvoiceItemList = items;
1506 KualiDecimal invTotalAmount = new KualiDecimal(0);
1507 KualiDecimal invItemCount = new KualiDecimal(0);
1508 invTotalAmount = invoiceDocument.getTotalDollarAmountAboveLineItems();
1509 for (OleInvoiceItem oleInvoiceItem : oleInvoiceItemList) {
1510 if (oleInvoiceItem.getItemTypeCode().equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_ITEM_CODE)) {
1511 invItemCount = invItemCount.add(oleInvoiceItem.getItemQuantity());
1512 }
1513 if (!(poList.contains(oleInvoiceItem.getPurchaseOrderIdentifier()))) {
1514 poList.add(oleInvoiceItem.getPurchaseOrderIdentifier());
1515 }
1516 }
1517
1518 String prorateBy = invoiceDocument.getProrateBy();
1519 OleVendorCreditMemoDocument vendorCreditMemoDocument = null;
1520
1521 Integer invoicePoId = 0;
1522 for (Integer purchaseOrderId : poList) {
1523 if (purchaseOrderId!=null && invoicePoId.compareTo(purchaseOrderId) != 0) {
1524
1525 try {
1526
1527 String user = null;
1528 if (GlobalVariables.getUserSession() == null) {
1529 kualiConfigurationService = SpringContext.getBean(ConfigurationService.class);
1530 user = kualiConfigurationService.getPropertyValueAsString(getOleSelectDocumentService().getSelectParameterValue(OleSelectNotificationConstant.ACCOUNT_DOCUMENT_INTIATOR));
1531 GlobalVariables.setUserSession(new UserSession(user));
1532 }
1533
1534 vendorCreditMemoDocument = (OleVendorCreditMemoDocument) SpringContext.getBean(DocumentService.class).getNewDocument("OLE_CM");
1535
1536
1537 vendorCreditMemoDocument.setCreditMemoNumber(invoiceDocument.getInvoiceNumber());
1538
1539 vendorCreditMemoDocument.setVendorDetail(invoiceDocument.getVendorDetail());
1540 vendorCreditMemoDocument.setVendorName(invoiceDocument.getVendorName());
1541 vendorCreditMemoDocument.setVendorHeaderGeneratedIdentifier(invoiceDocument.getVendorHeaderGeneratedIdentifier());
1542 vendorCreditMemoDocument.setVendorDetailAssignedIdentifier(invoiceDocument.getVendorDetailAssignedIdentifier());
1543 vendorCreditMemoDocument.setVendorNumber(invoiceDocument.getVendorNumber());
1544 vendorCreditMemoDocument.setVendorHeaderGeneratedIdentifier(invoiceDocument.getVendorHeaderGeneratedIdentifier());
1545 vendorCreditMemoDocument.setVendorDetailAssignedIdentifier(invoiceDocument.getVendorDetailAssignedIdentifier());
1546
1547
1548
1549
1550
1551
1552 vendorCreditMemoDocument.setVendorCityName(invoiceDocument.getVendorCityName());
1553 vendorCreditMemoDocument.setVendorLine1Address(invoiceDocument.getVendorLine1Address());
1554 vendorCreditMemoDocument.setVendorLine2Address(invoiceDocument.getVendorLine2Address());
1555 vendorCreditMemoDocument.setVendorAttentionName(invoiceDocument.getVendorAttentionName());
1556 vendorCreditMemoDocument.setVendorPostalCode(invoiceDocument.getVendorPostalCode());
1557 vendorCreditMemoDocument.setVendorStateCode(invoiceDocument.getVendorStateCode());
1558 vendorCreditMemoDocument.setVendorAttentionName(invoiceDocument.getVendorAttentionName());
1559 vendorCreditMemoDocument.setVendorAddressInternationalProvinceName(invoiceDocument.getVendorAddressInternationalProvinceName());
1560 vendorCreditMemoDocument.setVendorCountryCode(invoiceDocument.getVendorCountryCode());
1561 vendorCreditMemoDocument.setVendorCountry(invoiceDocument.getVendorCountry());
1562 vendorCreditMemoDocument.setVendorCustomerNumber(invoiceDocument.getVendorCustomerNumber());
1563 vendorCreditMemoDocument.setAccountsPayableProcessorIdentifier(invoiceDocument.getAccountsPayableProcessorIdentifier());
1564 vendorCreditMemoDocument.setProcessingCampusCode(invoiceDocument.getProcessingCampusCode());
1565 vendorCreditMemoDocument.setPurchaseOrderIdentifier(purchaseOrderId);
1566
1567
1568 vendorCreditMemoDocument.setCreditMemoDate(invoiceDocument.getInvoiceDate());
1569
1570
1571
1572
1573
1574 vendorCreditMemoDocument.setVendorCustomerNumber(invoiceDocument.getVendorCustomerNumber());
1575 vendorCreditMemoDocument.setAccountsPayableProcessorIdentifier(invoiceDocument.getAccountsPayableProcessorIdentifier());
1576 vendorCreditMemoDocument.setProcessingCampusCode(invoiceDocument.getProcessingCampusCode());
1577 vendorCreditMemoDocument.setPurchaseOrderIdentifier(purchaseOrderId);
1578 vendorCreditMemoDocument.setProrateBy(invoiceDocument.getProrateBy());
1579 vendorCreditMemoDocument.setProrateDollar(invoiceDocument.isProrateDollar());
1580 vendorCreditMemoDocument.setProrateQty(invoiceDocument.isProrateQty());
1581 vendorCreditMemoDocument.setProrateManual(invoiceDocument.isProrateManual());
1582 vendorCreditMemoDocument.setNoProrate(invoiceDocument.isNoProrate());
1583
1584
1585 if (invoiceDocument.getPaymentMethodId() != null) {
1586 OlePaymentMethod olePaymentMethod = SpringContext.getBean(BusinessObjectService.class).findBySinglePrimaryKey(OlePaymentMethod.class, invoiceDocument.getPaymentMethodId());
1587 vendorCreditMemoDocument.setOlePaymentMethod(olePaymentMethod);
1588
1589 vendorCreditMemoDocument.setPaymentMethodId(olePaymentMethod.getPaymentMethodId());
1590 }
1591
1592 vendorCreditMemoDocument.setInvoiceIdentifier(invoiceDocument.getPurapDocumentIdentifier());
1593
1594
1595
1596 vendorCreditMemoDocument.setBankCode(invoiceDocument.getBankCode());
1597 vendorCreditMemoDocument.setBank(invoiceDocument.getBank());
1598
1599 } catch (WorkflowException e) {
1600 String extraDescription = "Error=" + e.getMessage();
1601 LOG.error("Exception creating Payment request document - " + e.getMessage());
1602 }
1603
1604 Map invItemMap = new HashMap();
1605 invItemMap.put(PurapConstants.PRQSDocumentsStrings.PUR_ID, invoiceDocument.getPurapDocumentIdentifier());
1606 invItemMap.put(PurapConstants.PRQSDocumentsStrings.PO_ID, purchaseOrderId);
1607 List<OleInvoiceItem> invoiceItems = (List<OleInvoiceItem>) businessObjectService.findMatchingOrderBy(OleInvoiceItem.class, invItemMap, PurapConstants.PRQSDocumentsStrings.PO_ID, true);
1608 KualiDecimal itemCount = new KualiDecimal(0);
1609 KualiDecimal itemPrice = new KualiDecimal(0);
1610 PurchaseOrderDocument poDoc = invoiceDocument.getPurchaseOrderDocument(purchaseOrderId);
1611 if (poDoc == null) {
1612 throw new RuntimeException("Purchase Order document " + purchaseOrderId + " does not exist in the system");
1613 }
1614 if (vendorCreditMemoDocument.getDocumentHeader() != null) {
1615 vendorCreditMemoDocument.getDocumentHeader().setDocumentDescription(createPreqDocumentDescription(poDoc.getPurapDocumentIdentifier(), invoiceDocument.getVendorName()));
1616 }
1617
1618 try {
1619 vendorCreditMemoDocument.updateAndSaveAppDocStatus(PurapConstants.PaymentRequestStatuses.APPDOC_IN_PROCESS);
1620 } catch (WorkflowException we) {
1621 throw new RuntimeException("Unable to save route status data for document: " + vendorCreditMemoDocument.getDocumentNumber(), we);
1622 }
1623 List<OleCreditMemoItem> creditMemoItems = new ArrayList<>();
1624
1625 HashMap<String, ExpiredOrClosedAccountEntry> expiredOrClosedAccountList = SpringContext.getBean(AccountsPayableService.class).expiredOrClosedAccountsList(poDoc);
1626
1627 for (OleInvoiceItem invoiceItem : invoiceItems) {
1628 if ((flag || !invoiceItem.isDebitItem()) && invoiceItem.getExtendedPrice().isNonZero()) {
1629
1630 OleCreditMemoItem creditMemoItem = new OleCreditMemoItem(invoiceItem, vendorCreditMemoDocument, expiredOrClosedAccountList);
1631 if(flag && invoiceItem.isDebitItem()){
1632 creditMemoItem.setItemUnitPrice(creditMemoItem.getItemUnitPrice().negate());
1633 creditMemoItem.setExtendedPrice(creditMemoItem.getExtendedPrice().negated());
1634 for(PurApAccountingLine purApAccountingLine : creditMemoItem.getSourceAccountingLines()){
1635 purApAccountingLine.setAmount(purApAccountingLine.getAmount().negated());
1636 }
1637 }
1638 creditMemoItems.add(creditMemoItem);
1639 if (vendorCreditMemoDocument.getAccountsPayablePurchasingDocumentLinkIdentifier() == null) {
1640 vendorCreditMemoDocument.setAccountsPayablePurchasingDocumentLinkIdentifier(invoiceItem.getAccountsPayablePurchasingDocumentLinkIdentifier());
1641 }
1642 }
1643
1644 }
1645 vendorCreditMemoDocument.setItems(creditMemoItems);
1646 vendorCreditMemoDocument.setCreditMemoAmount(vendorCreditMemoDocument.getTotalDollarAmount());
1647 SpringContext.getBean(OleCreditMemoService.class).calculateCreditMemo(vendorCreditMemoDocument);
1648
1649 SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedCalculateAccountsPayableEvent(vendorCreditMemoDocument));
1650
1651 if (expiredOrClosedAccountList == null) {
1652 expiredOrClosedAccountList = new HashMap();
1653 }
1654
1655 if (LOG.isDebugEnabled()) {
1656 LOG.debug(expiredOrClosedAccountList.size() + " accounts has been found as Expired or Closed");
1657 }
1658
1659 invoicePoId = purchaseOrderId;
1660
1661
1662 SpringContext.getBean(CreditMemoService.class).populateAndSaveCreditMemo(vendorCreditMemoDocument);
1663
1664 SpringContext.getBean(OleCreditMemoService.class).autoApproveCreditMemo(vendorCreditMemoDocument);
1665 }
1666 }
1667 }
1668
1669 @Override
1670 public OleInvoiceDocument getInvoiceDocumentById(Integer invoiceIdentifier) {
1671 OleInvoiceDocument invoiceDocument = getInvoiceByDocumentNumber(invoiceDao.getDocumentNumberByInvoiceId(invoiceIdentifier));
1672 return invoiceDocument;
1673 }
1674
1675
1676 public boolean autoApprovePaymentRequest(OleInvoiceDocument doc) {
1677 try {
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688 try {
1689 ObjectUtils.materializeUpdateableCollections(doc);
1690 for (OleInvoiceItem item : (List<OleInvoiceItem>) doc.getItems()) {
1691 ObjectUtils.materializeUpdateableCollections(item);
1692 }
1693 } catch (Exception ex) {
1694 throw new RuntimeException(ex);
1695 }
1696 doc = (OleInvoiceDocument) ObjectUtils.deepCopy(doc);
1697
1698
1699
1700
1701 saveInvoiceDocument(doc);
1702 } catch (WorkflowException we) {
1703 LOG.error("Exception encountered when approving document number " + doc.getDocumentNumber() + ".", we);
1704
1705 throw new RuntimeException("Exception encountered when approving document number " + doc.getDocumentNumber() + ".", we);
1706 }
1707 return true;
1708 }
1709
1710 public OleInvoiceDocument populateInvoiceDocument (OleInvoiceDocument invoiceDocument) {
1711 Boolean isItemLevelDebit = null;
1712 Boolean isAdditionalChargeLevelDebit = null;
1713 Boolean isItemLevelCredit = null;
1714 Boolean isAdditionalChargeLevelCredit = null;
1715 if (invoiceDocument.getPurchaseOrderDocuments() != null && invoiceDocument.getPurchaseOrderDocuments().size() > 0) {
1716 for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>) invoiceDocument.getItems()) {
1717 if (invoiceItem.getItemType().isAdditionalChargeIndicator() && invoiceItem.getItemUnitPrice() != null)
1718 invoiceItem.setPurchaseOrderIdentifier(invoiceDocument.getPurchaseOrderDocuments().get(0).getPurapDocumentIdentifier());
1719 }
1720 }
1721 Integer poIdentifier=null;
1722 for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>) invoiceDocument.getItems()) {
1723 if (invoiceItem.isDebitItem() &&
1724 (invoiceItem.getItemListPrice().isNonZero() ||
1725 (invoiceItem.getItemUnitPrice()!=null && invoiceItem.getItemUnitPrice().compareTo(BigDecimal.ZERO)!=0))){
1726 if(invoiceItem.getItemType().isQuantityBasedGeneralLedgerIndicator()){
1727 if(isItemLevelDebit==null){
1728 isItemLevelDebit = true;
1729 }
1730 isItemLevelDebit &= invoiceItem.isDebitItem();
1731 }
1732 if(invoiceItem.getItemType().isAdditionalChargeIndicator()){
1733 if(isAdditionalChargeLevelDebit==null){
1734 isAdditionalChargeLevelDebit = true;
1735 }
1736 isAdditionalChargeLevelDebit &= invoiceItem.isDebitItem();
1737 }
1738
1739 }
1740 else if(!invoiceItem.isDebitItem() &&
1741 (invoiceItem.getItemListPrice().isNonZero() ||
1742 (invoiceItem.getItemUnitPrice()!=null && invoiceItem.getItemUnitPrice().compareTo(BigDecimal.ZERO)!=0))){
1743 if(invoiceItem.getItemType().isQuantityBasedGeneralLedgerIndicator()){
1744 if(isItemLevelCredit==null){
1745 isItemLevelCredit = true;
1746 }
1747 isItemLevelCredit &= !invoiceItem.isDebitItem();
1748 }
1749 if(invoiceItem.getItemType().isAdditionalChargeIndicator()){
1750 if(isAdditionalChargeLevelCredit==null){
1751 isAdditionalChargeLevelCredit = true;
1752 }
1753 isAdditionalChargeLevelCredit &= !invoiceItem.isDebitItem();
1754 }
1755 }
1756 if (invoiceItem.getItemType().isQuantityBasedGeneralLedgerIndicator() &&
1757 invoiceItem.getItemListPrice().isNonZero() && poIdentifier==null) {
1758 poIdentifier = invoiceItem.getPurchaseOrderIdentifier();
1759 }
1760 }
1761 boolean flag = (isItemLevelDebit == null && isAdditionalChargeLevelDebit!=null && isAdditionalChargeLevelDebit) ||
1762 (isAdditionalChargeLevelDebit == null && isItemLevelDebit!=null && isItemLevelDebit) ||
1763 (isItemLevelCredit == null && isAdditionalChargeLevelCredit!=null && isAdditionalChargeLevelCredit) ||
1764 (isAdditionalChargeLevelCredit == null && isItemLevelCredit!=null && isItemLevelCredit) &&
1765 !(isItemLevelCredit!=null && isItemLevelCredit && isItemLevelDebit!=null && isItemLevelDebit);
1766 if(!flag){
1767 Integer poIdentifierForCredit = null;
1768 Integer poIdentifierForDebit = null;
1769 if (invoiceDocument.getItems() != null && invoiceDocument.getItems().size() > 0) {
1770 for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>)invoiceDocument.getItems()) {
1771 if (invoiceItem.getItemType().isQuantityBasedGeneralLedgerIndicator() && invoiceItem.isDebitItem() &&
1772 invoiceItem.getItemListPrice().isNonZero()) {
1773 poIdentifierForDebit = invoiceItem.getPurchaseOrderIdentifier();
1774 break;
1775 }
1776 }
1777 }
1778 if (invoiceDocument.getItems() != null && invoiceDocument.getItems().size() > 0) {
1779 for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>)invoiceDocument.getItems()) {
1780 if (invoiceItem.getItemType().isQuantityBasedGeneralLedgerIndicator() && !invoiceItem.isDebitItem() &&
1781 invoiceItem.getItemListPrice().isNonZero()) {
1782 poIdentifierForCredit = invoiceItem.getPurchaseOrderIdentifier();
1783 break;
1784 }
1785 }
1786 }
1787
1788
1789 for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>) invoiceDocument.getItems()) {
1790 if (invoiceItem.getItemType().isAdditionalChargeIndicator() && invoiceItem.getItemUnitPrice() != null && invoiceItem.isDebitItem()) {
1791 invoiceItem.setPurchaseOrderIdentifier(poIdentifierForDebit);
1792 }
1793 if (invoiceItem.getItemType().isAdditionalChargeIndicator() && invoiceItem.getItemUnitPrice() != null && !invoiceItem.isDebitItem()) {
1794 invoiceItem.setPurchaseOrderIdentifier(poIdentifierForCredit);
1795 }
1796 }
1797 }else{
1798 for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>) invoiceDocument.getItems()) {
1799 if (invoiceItem.getItemType().isAdditionalChargeIndicator() && invoiceItem.getItemUnitPrice() != null) {
1800 invoiceItem.setPurchaseOrderIdentifier(poIdentifier);
1801 }
1802 }
1803 }
1804
1805 if (ObjectUtils.isNull(invoiceDocument.getInvoicePayDate()) || invoiceDocument.getInvoicePayDate().before(new Date())) {
1806 invoiceDocument.setInvoicePayDate(calculatePayDate(invoiceDocument.getInvoiceDate(), invoiceDocument.getVendorPaymentTerms()));
1807 }
1808
1809
1810
1811 if (invoiceDocument.getInvoiceTypeHdnId() != null && !invoiceDocument.getInvoiceTypeHdnId().isEmpty()) {
1812 invoiceDocument.setInvoiceTypeId(Integer.valueOf(invoiceDocument.getInvoiceTypeHdnId()));
1813 }
1814 if (invoiceDocument.getPaymentMethodIdentifier() != null && !invoiceDocument.getPaymentMethodIdentifier().isEmpty()) {
1815 invoiceDocument.setPaymentMethodId(Integer.valueOf(invoiceDocument.getPaymentMethodIdentifier()));
1816 }
1817 if (invoiceDocument.getInvoiceSubTypeHdnId() != null && !invoiceDocument.getInvoiceSubTypeHdnId().isEmpty()) {
1818 invoiceDocument.setInvoiceSubTypeId(Integer.valueOf(invoiceDocument.getInvoiceSubTypeHdnId()));
1819 }
1820
1821 if (invoiceDocument.getInvoiceAmount() != null && !invoiceDocument.getInvoiceAmount().isEmpty()) {
1822 invoiceDocument.setVendorInvoiceAmount(new KualiDecimal(invoiceDocument.getInvoiceAmount()));
1823 }
1824
1825 if (invoiceDocument.getForeignInvoiceAmount() != null && !invoiceDocument.getForeignInvoiceAmount().isEmpty()) {
1826 invoiceDocument.setForeignVendorInvoiceAmount(new KualiDecimal(invoiceDocument.getForeignInvoiceAmount()).bigDecimalValue());
1827 if (StringUtils.isNotBlank(invoiceDocument.getInvoiceCurrencyType())) {
1828 if (StringUtils.isNotBlank(invoiceDocument.getInvoiceCurrencyExchangeRate())) {
1829 try {
1830 Double.parseDouble(invoiceDocument.getInvoiceCurrencyExchangeRate());
1831 }
1832 catch (NumberFormatException nfe) {
1833 throw new RuntimeException("Invalid Exchange Rate", nfe);
1834 }
1835 invoiceDocument.setVendorInvoiceAmount(new KualiDecimal(invoiceDocument.getForeignVendorInvoiceAmount().divide(new BigDecimal(invoiceDocument.getInvoiceCurrencyExchangeRate()), 4, RoundingMode.HALF_UP)));
1836 invoiceDocument.setInvoiceAmount(invoiceDocument.getVendorInvoiceAmount().toString());
1837 } else {
1838 BigDecimal exchangeRate = getExchangeRate(invoiceDocument.getInvoiceCurrencyType()).getExchangeRate();
1839 invoiceDocument.setVendorInvoiceAmount(new KualiDecimal(invoiceDocument.getForeignVendorInvoiceAmount().divide(exchangeRate, 4, RoundingMode.HALF_UP)));
1840 invoiceDocument.setInvoiceAmount(invoiceDocument.getVendorInvoiceAmount().toString());
1841 }
1842
1843 }
1844 }
1845
1846 Bank defaultBank = SpringContext.getBean(BankService.class).getDefaultBankByDocType(invoiceDocument.getClass());
1847 if (defaultBank != null) {
1848 invoiceDocument.setBankCode(defaultBank.getBankCode());
1849 invoiceDocument.setBank(defaultBank);
1850 }
1851
1852 if (ObjectUtils.isNull(invoiceDocument.getInvoicePayDate())) {
1853 invoiceDocument.setInvoicePayDate(this.calculatePayDate(invoiceDocument.getInvoiceDate(), invoiceDocument.getVendorPaymentTerms()));
1854 }
1855
1856
1857
1858
1859
1860 BigDecimal addChargeItem=BigDecimal.ZERO;
1861 List<OleInvoiceItem> item = invoiceDocument.getItems();
1862 for(OleInvoiceItem invoiceditem : item){
1863 if(invoiceditem.getItemType().isAdditionalChargeIndicator() && invoiceditem.getExtendedPrice()!=null){
1864 addChargeItem =addChargeItem.add(invoiceditem.getExtendedPrice().bigDecimalValue());
1865 }
1866 }
1867 if (invoiceDocument.getTotalDollarAmount() != null ) {
1868 invoiceDocument.setInvoiceItemTotal(invoiceDocument.getTotalDollarAmount().subtract(new KualiDecimal(addChargeItem)).toString());
1869 invoiceDocument.setDocumentTotalAmount(invoiceDocument.getTotalDollarAmount().toString());
1870 }
1871 return invoiceDocument;
1872 }
1873
1874 public OleInvoiceDocument populateInvoiceItems (OleInvoiceDocument invoiceDocument) {
1875
1876
1877 LOG.debug("Inside populateInvoiceItems method ");
1878 List<OleInvoiceItem> items = new ArrayList<>();
1879 Boolean receiveRequired = false;
1880 HashMap<String, ExpiredOrClosedAccountEntry> expiredOrClosedAccountList = new HashMap<>();
1881 BigDecimal addChargeItem=BigDecimal.ZERO;
1882 for (OlePurchaseOrderDocument po : invoiceDocument.getPurchaseOrderDocuments()) {
1883 if(po.isReceivingDocumentRequiredIndicator()) {
1884 receiveRequired=true;
1885 }
1886
1887
1888 for (OlePurchaseOrderItem poi : (List<OlePurchaseOrderItem>) po.getItems()) {
1889
1890
1891 if (poi.isItemForInvoice()) {
1892 OleInvoiceItem invoiceItem = new OleInvoiceItem(poi, invoiceDocument, expiredOrClosedAccountList);
1893
1894 invoiceItem.setClosePurchaseOrderIndicator(po.isClosePO());
1895 invoiceItem.setReopenPurchaseOrderIndicator(po.getIsReOpenPO());
1896 PurchasingCapitalAssetItem purchasingCAMSItem = po.getPurchasingCapitalAssetItemByItemIdentifier(poi.getItemIdentifier());
1897 if (purchasingCAMSItem != null) {
1898 invoiceItem.setCapitalAssetTransactionTypeCode(purchasingCAMSItem.getCapitalAssetTransactionTypeCode());
1899 }
1900 invoiceItem.setUseTaxIndicator(po.isUseTaxIndicator());
1901 invoiceItem.setPurchaseOrderIdentifier(po.getPurapDocumentIdentifier());
1902 invoiceItem.setPostingYear(po.getPostingYear());
1903 invoiceItem.setAccountsPayablePurchasingDocumentLinkIdentifier(po.getAccountsPayablePurchasingDocumentLinkIdentifier());
1904 invoiceItem.setReceivingDocumentRequiredIndicator(po.isReceivingDocumentRequiredIndicator());
1905 if(invoiceItem.getItemType().isAdditionalChargeIndicator() && invoiceItem.getExtendedPrice()!=null){
1906 addChargeItem =addChargeItem.add(invoiceItem.getExtendedPrice().bigDecimalValue());
1907 }
1908
1909 invoiceItem.getUseTaxItems().clear();
1910 for (PurApItemUseTax useTax : poi.getUseTaxItems()) {
1911 invoiceItem.getUseTaxItems().add(useTax);
1912 }
1913 invoiceItem.setPurchaseOrderEndDate(invoiceDocument.getPurchaseOrderDocuments().get(0).getPoEndDate());
1914
1915
1916 invoiceDocument.getItems().add(invoiceItem);
1917 if (LOG.isDebugEnabled()) {
1918 LOG.debug("Size**********************" + invoiceDocument.getItems().size());
1919 }
1920 }
1921 }
1922 invoiceDocument.setTotalDollarAmount(invoiceDocument.getTotalDollarAmount().add(po.getTotalDollarAmount()));
1923 }
1924
1925
1926
1927
1928
1929
1930
1931 if (invoiceDocument.getTotalDollarAmount() != null ) {
1932 invoiceDocument.setInvoiceItemTotal(invoiceDocument.getTotalDollarAmount().subtract(new KualiDecimal(addChargeItem)).toString());
1933 invoiceDocument.setDocumentTotalAmount(invoiceDocument.getTotalDollarAmount().toString());
1934 }
1935 invoiceDocument.setDocumentTotalAmount(invoiceDocument.getInvoicedItemTotal());
1936
1937 invoiceDocument.getPurchaseOrderDocuments().clear();
1938 invoiceDocument.setReceivingDocumentRequiredIndicator(receiveRequired);
1939 return invoiceDocument;
1940 }
1941
1942
1943
1944
1945 public void calculateAccount(PurApItem purapItem) {
1946 purapItem.setExtendedPrice(purapItem.calculateExtendedPrice());
1947 List<PurApAccountingLine> purApAccountingLines = purapItem.getSourceAccountingLines();
1948 BigDecimal totalPercent = BigDecimal.ZERO;
1949 BigDecimal totalAmt = BigDecimal.ZERO;
1950 for (PurApAccountingLine account : purApAccountingLines) {
1951 if (purapItem.getTotalAmount() != null && !purapItem.getTotalAmount().equals(KualiDecimal.ZERO)) {
1952 if (account.getAccountLinePercent() != null && (account.getAmount() == null || account.getAmount().equals(KualiDecimal.ZERO))) {
1953 BigDecimal percent = account.getAccountLinePercent().divide(new BigDecimal(100));
1954 account.setAmount((purapItem.getTotalAmount().multiply(new KualiDecimal(percent))));
1955 } else if (account.getAmount() != null && account.getAmount().isNonZero() && account.getAccountLinePercent() == null) {
1956 KualiDecimal dollar = account.getAmount().multiply(new KualiDecimal(100));
1957 BigDecimal dollarToPercent = dollar.bigDecimalValue().divide((purapItem.getTotalAmount().bigDecimalValue()), 0, RoundingMode.FLOOR);
1958 account.setAccountLinePercent(dollarToPercent);
1959 } else if (account.getAmount() != null && account.getAmount().isZero() && account.getAccountLinePercent() == null) {
1960 account.setAccountLinePercent(new BigDecimal(0));
1961 } else if((account.getAmount()!=null&&account.getAccountLinePercent() != null) ||
1962 (account.getAmount()!=null&& account.getAccountLinePercent().intValue()== 100)){
1963 BigDecimal percent = account.getAccountLinePercent().divide(new BigDecimal(100));
1964 account.setAmount((purapItem.getTotalAmount().multiply(new KualiDecimal(percent))));
1965 }
1966 totalPercent = totalPercent.add(account.getAccountLinePercent());
1967 totalAmt = totalAmt.add(account.getAmount().bigDecimalValue());
1968 } else {
1969 account.setAmount(KualiDecimal.ZERO);
1970 }
1971 }
1972
1973 if(totalPercent.intValue() != 100 ||
1974 (purapItem.getTotalAmount()!=null && totalAmt.compareTo(purapItem.getTotalAmount().bigDecimalValue())!=0)){
1975 for (PurApAccountingLine account : purApAccountingLines) {
1976 if (purapItem.getTotalAmount() != null && !purapItem.getTotalAmount().equals(KualiDecimal.ZERO)) {
1977 BigDecimal percent = BigDecimal.ONE.divide(new BigDecimal(purApAccountingLines.size()), BigDecimal.ROUND_CEILING, BigDecimal.ROUND_HALF_UP);
1978 account.setAmount((purapItem.getTotalAmount().multiply(new KualiDecimal(percent))));
1979 } else {
1980 account.setAmount(KualiDecimal.ZERO);
1981 }
1982 }
1983 }
1984
1985 }
1986
1987 @Override
1988 public void convertPOItemToInvoiceItem (OleInvoiceDocument oleInvoiceDocument) {
1989 boolean poItemsSelected = false;
1990 for (OlePurchaseOrderDocument po : oleInvoiceDocument.getPurchaseOrderDocuments()) {
1991 for (OlePurchaseOrderItem poi : (List<OlePurchaseOrderItem>) po.getItems()) {
1992 if (poi.isItemForInvoice()) {
1993 poItemsSelected = true;
1994 break;
1995 }
1996 }
1997 }
1998 if (poItemsSelected) {
1999 oleInvoiceDocument = this.populateInvoiceItems(oleInvoiceDocument);
2000
2001
2002 }
2003 else {
2004 oleInvoiceDocument.setPurchaseOrderDocuments(new ArrayList<OlePurchaseOrderDocument>());
2005 GlobalVariables.getMessageMap().putError(OleSelectConstant.PROCESS_ITEM_SECTION_ID, OLEKeyConstants.ERROR_NO_PO_SELECTED);
2006 }
2007 }
2008
2009
2010
2011
2012
2013 public String createInvoiceNoMatchQuestionText(OleInvoiceDocument invoiceDocument) {
2014
2015 String questionText = null;
2016 StringBuffer questionTextBuffer = new StringBuffer("");
2017 if (invoiceDocument.getInvoiceAmount() != null && invoiceDocument.getInvoicedGrandTotal() != null ) {
2018 KualiDecimal invoiceAmount = new KualiDecimal(invoiceDocument.getInvoiceAmount());
2019 KualiDecimal invoiceGrandTotal = new KualiDecimal(invoiceDocument.getInvoicedGrandTotal());
2020 if(!invoiceAmount.equals(invoiceGrandTotal)) {
2021 questionText = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(PurapKeyConstants.AP_QUESTION_CONFIRM_INVOICE_MISMATCH);
2022 questionText = StringUtils.replace(questionText, "{0}", "");
2023
2024
2025 CurrencyFormatter cf = new CurrencyFormatter();
2026
2027
2028
2029 questionTextBuffer.append(questionText);
2030 if (StringUtils.isNotBlank(invoiceDocument.getInvoiceCurrencyType())) {
2031 String currencyType = getCurrencyType(invoiceDocument.getInvoiceCurrencyType());
2032 if (StringUtils.isNotBlank(currencyType)) {
2033 if (!currencyType.equalsIgnoreCase(OleSelectConstant.CURRENCY_TYPE_NAME) && invoiceDocument.getForeignVendorInvoiceAmount() != null) {
2034 if (StringUtils.isNotBlank(invoiceDocument.getInvoiceCurrencyExchangeRate())) {
2035 try {
2036 Double.parseDouble(invoiceDocument.getInvoiceCurrencyExchangeRate());
2037 }
2038 catch (NumberFormatException nfe) {
2039 throw new RuntimeException("Invalid Exchange Rate", nfe);
2040 }
2041 invoiceDocument.setVendorInvoiceAmount(new KualiDecimal(invoiceDocument.getForeignVendorInvoiceAmount().divide(new BigDecimal(invoiceDocument.getInvoiceCurrencyExchangeRate()), 4, RoundingMode.HALF_UP)));
2042 } else {
2043 BigDecimal exchangeRate = getExchangeRate(invoiceDocument.getInvoiceCurrencyType()).getExchangeRate();
2044 invoiceDocument.setVendorInvoiceAmount(new KualiDecimal(invoiceDocument.getForeignVendorInvoiceAmount().divide(exchangeRate, 4, RoundingMode.HALF_UP)));
2045 }
2046 }
2047 }
2048
2049 }
2050 questionTextBuffer.append("[br][br][b]Summary Detail Below:[b][br][br][table questionTable]");
2051 questionTextBuffer.append("[tr][td leftTd]Vendor Invoice Amount :[/td][td rightTd]" + (String) cf.format(invoiceDocument.getVendorInvoiceAmount()) + "[/td][/tr]");
2052 questionTextBuffer.append("[tr][td leftTd]Invoice Total Prior to Additional Charges:[/td][td rightTd]" + (String) cf.format(new KualiDecimal(invoiceDocument.getInvoicedItemTotal())) + "[/td][/tr]");
2053
2054
2055
2056 if (invoiceDocument.isDiscount()) {
2057 questionTextBuffer.append("[tr][td leftTd]Total Before Discount:[/td][td rightTd]" + (String) cf.format(invoiceDocument.getGrandPreTaxTotalExcludingDiscount()) + "[/td][/tr]");
2058 }
2059
2060
2061 boolean salesTaxInd = SpringContext.getBean(ParameterService.class).getParameterValueAsBoolean(OleParameterConstants.PURCHASING_DOCUMENT.class, PurapParameterConstants.ENABLE_SALES_TAX_IND);
2062 if (salesTaxInd) {
2063 questionTextBuffer.append("[tr][td leftTd]Grand Total Prior to Tax:[/td][td rightTd]" + (String) cf.format(invoiceDocument.getGrandPreTaxTotal()) + "[/td][/tr]");
2064 questionTextBuffer.append("[tr][td leftTd]Grand Total Tax:[/td][td rightTd]" + (String) cf.format(invoiceDocument.getGrandTaxAmount()) + "[/td][/tr]");
2065 }
2066
2067 questionTextBuffer.append("[tr][td leftTd]Grand Total:[/td][td rightTd]" + (String) cf.format(new KualiDecimal(invoiceDocument.getInvoicedGrandTotal())) + "[/td][/tr]");
2068
2069 if (StringUtils.isNotBlank(invoiceDocument.getInvoiceCurrencyType())) {
2070 String currencyType = getCurrencyType(invoiceDocument.getInvoiceCurrencyType());
2071 if (StringUtils.isNotBlank(currencyType)) {
2072 if (!currencyType.equalsIgnoreCase(OleSelectConstant.CURRENCY_TYPE_NAME)) {
2073 questionTextBuffer.append("[tr][td] [/td][/tr]");
2074 questionTextBuffer.append("[tr][td leftTd]Foreign Vendor Invoice Amount :[/td][td rightTd]" + (String) cf.format(new KualiDecimal(invoiceDocument.getForeignVendorInvoiceAmount())) + "[/td][/tr]");
2075 questionTextBuffer.append("[tr][td leftTd]Foreign Invoice Total Prior to Additional Charges:[/td][td rightTd]" + (String) cf.format(new KualiDecimal(invoiceDocument.getInvoicedForeignItemTotal())) + "[/td][/tr]");
2076 questionTextBuffer.append("[tr][td leftTd]Foreign Grand Total:[/td][td rightTd]" + (String) cf.format(new KualiDecimal(invoiceDocument.getInvoicedForeignGrandTotal())) + "[/td][/tr]");
2077 }
2078 }
2079 }
2080 questionTextBuffer.append("[/table]");
2081 }
2082 }
2083
2084 return questionTextBuffer.toString();
2085
2086 }
2087
2088
2089
2090
2091
2092
2093 public String createSubscriptionDateOverlapQuestionText(OleInvoiceDocument invoiceDocument) {
2094 boolean isOverlap = false;
2095 List<OleInvoiceDocument> overlapInvDocumentList = new ArrayList<OleInvoiceDocument>();
2096 List<OleInvoiceItem> invItems = (List<OleInvoiceItem>) invoiceDocument.getItems();
2097 List<OleInvoiceItem> subscriptionInvItems = new ArrayList<OleInvoiceItem>();
2098 List<OleInvoiceItem> subscriptionInvoicedItems = new ArrayList<OleInvoiceItem>();
2099
2100 for (OleInvoiceItem invoiceItem : invItems) {
2101 if (invoiceItem.isSubscriptionOverlap()) {
2102 subscriptionInvItems.add(invoiceItem);
2103 }
2104 }
2105 for (OleInvoiceItem subscriptionInvItem : subscriptionInvItems) {
2106
2107 if (subscriptionInvItem.getPoItemIdentifier() != null) {
2108 Map matchPOItem = new HashMap();
2109 matchPOItem.put("poItemIdentifier", subscriptionInvItem.getPoItemIdentifier());
2110 List<OleInvoiceItem> itemList = (List<OleInvoiceItem>) getBusinessObjectService().findMatching(OleInvoiceItem.class, matchPOItem);
2111
2112 for (OleInvoiceItem invoicedItem : itemList) {
2113 if (invoicedItem.getSubscriptionFromDate() != null && invoicedItem.getSubscriptionToDate() != null) {
2114 subscriptionInvoicedItems.add(invoicedItem);
2115 }
2116 }
2117
2118 for (OleInvoiceItem oleInvoiceItem : subscriptionInvoicedItems) {
2119
2120 if (oleInvoiceItem.getPoItemIdentifier().equals(subscriptionInvItem.getPoItemIdentifier())) {
2121
2122 if (subscriptionInvItem.getSubscriptionFromDate().compareTo(oleInvoiceItem.getSubscriptionFromDate()) >= 0 && subscriptionInvItem.getSubscriptionFromDate().compareTo(oleInvoiceItem.getSubscriptionToDate()) <= 0) {
2123 isOverlap = true;
2124 overlapInvDocumentList.add((OleInvoiceDocument) oleInvoiceItem.getInvoiceDocument());
2125 } else if (subscriptionInvItem.getSubscriptionToDate().compareTo(oleInvoiceItem.getSubscriptionFromDate()) >= 0 && subscriptionInvItem.getSubscriptionToDate().compareTo(oleInvoiceItem.getSubscriptionToDate()) <= 0) {
2126 isOverlap = true;
2127 overlapInvDocumentList.add((OleInvoiceDocument) oleInvoiceItem.getInvoiceDocument());
2128 } else if ((subscriptionInvItem.getSubscriptionFromDate().compareTo(oleInvoiceItem.getSubscriptionFromDate()) < 0) && (subscriptionInvItem.getSubscriptionToDate().compareTo(oleInvoiceItem.getSubscriptionToDate()) > 0)) {
2129 isOverlap = true;
2130 overlapInvDocumentList.add((OleInvoiceDocument) oleInvoiceItem.getInvoiceDocument());
2131 }
2132 }
2133 }
2134 }
2135 }
2136 String questionText = null;
2137 StringBuffer questionTextBuffer = new StringBuffer("");
2138 if (isOverlap) {
2139 questionText = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(PurapKeyConstants.AP_QUESTION_CONFIRM_INVOICE_SUBSCRIPTION_DATE_OVERLAP);
2140 questionText = StringUtils.replace(questionText, "{0}", "");
2141
2142
2143
2144 questionTextBuffer.append(questionText);
2145
2146 questionTextBuffer.append("[br][br][b]Summary Detail Below:[b][br][br][table questionTable]");
2147 questionTextBuffer.append("[tr][td leftTd]Following Invoices Subscription Date Overlap with current Invoice :[/td][td rightTd] [/td][/tr]");
2148 questionTextBuffer.append("[tr][td leftTd][/td][td rightTd]");
2149
2150 for (OleInvoiceDocument overlapInvDocument : overlapInvDocumentList) {
2151 questionTextBuffer.append(overlapInvDocument.getDocumentNumber() + " ");
2152 }
2153 questionTextBuffer.append("[/td][/tr][/table]");
2154 }
2155
2156 overlapInvDocumentList.clear();
2157 subscriptionInvItems.clear();
2158 return questionTextBuffer.toString();
2159
2160 }
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175 public String getParameter(String name){
2176 ParameterKey parameterKey = ParameterKey.create(org.kuali.ole.OLEConstants.APPL_ID, org.kuali.ole.OLEConstants.SELECT_NMSPC, org.kuali.ole.OLEConstants.SELECT_CMPNT,name);
2177 Parameter parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
2178 return parameter!=null?parameter.getValue():null;
2179 }
2180
2181 public String[] getCollapseSections() {
2182 LOG.debug("Inside getCollapseSections()");
2183 String[] collapseSections = new String[]{};
2184 try {
2185 collapseSections = parameterService.getParameterValuesAsString(Class.forName(PurapConstants.PURAP_DETAIL_TYPE_CODE_MAP.get(PurapConstants.PurapDocTypeCodes.INVOICE_DOCUMENT)),
2186 OLEConstants.INVOICE_COLLAPSE_SECTIONS_ON_PO_ADD).toArray(new String[]{});
2187 }
2188 catch (Exception e) {
2189 LOG.error("Exception while getting the default Collapse section on Invoice Document"+e);
2190 throw new RuntimeException(e);
2191 }
2192 LOG.debug("Leaving getCollapseSections()");
2193 return collapseSections;
2194 }
2195
2196 public String[] getDefaultCollapseSections() {
2197 LOG.debug("Inside getDefaultCollapseSections()");
2198 String[] collapseSections = new String[]{};
2199 try {
2200 collapseSections = parameterService.getParameterValuesAsString(Class.forName(PurapConstants.PURAP_DETAIL_TYPE_CODE_MAP.get(PurapConstants.PurapDocTypeCodes.INVOICE_DOCUMENT)),
2201 OLEConstants.INITIAL_COLLAPSE_SECTIONS).toArray(new String[]{});
2202 }
2203 catch (Exception e) {
2204 LOG.error("Exception while getting the default Collapse section on Invoice Document"+e);
2205 throw new RuntimeException(e);
2206 }
2207 LOG.debug("Leaving getDefaultCollapseSections()");
2208 return collapseSections;
2209 }
2210
2211 public boolean canCollapse(String sectionName, String[] collapseSections) {
2212 LOG.debug("Inside method canCollapse()");
2213 List<String> sectionLists = Arrays.asList(collapseSections);
2214 if (sectionLists.contains(sectionName)) {
2215 return false;
2216 }
2217 return true;
2218 }
2219
2220
2221
2222
2223
2224
2225
2226 public boolean isDuplicationExists(OleInvoiceDocument invoiceDocument, OLEInvoiceForm invoiceForm, boolean isBlanketApprove) {
2227 LOG.debug("Inside method isDuplicationExists()");
2228 boolean isDuplicationExists = false;
2229 if(invoiceDocument.getInvoiceNumber()!=null && !invoiceDocument.getInvoiceNumber().equalsIgnoreCase("")){
2230 Map<String, Object> map = new HashMap<String, Object>();
2231 map.put(OLEConstants.InvoiceDocument.INVOICE_NUMBER, invoiceDocument.getInvoiceNumber().toString());
2232
2233 map.put(OLEConstants.InvoiceDocument.VENDOR_GENERATED_IDENTIFIER, invoiceDocument.getVendorHeaderGeneratedIdentifier().toString());
2234 map.put(OLEConstants.InvoiceDocument.VENDOR_DETAIL_ASSIGNED_GENERATED_IDENTIFIER, invoiceDocument.getVendorDetailAssignedIdentifier().toString());
2235 List<OleInvoiceDocument> documents = (List<OleInvoiceDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OleInvoiceDocument.class, map);
2236 StringBuffer duplicationMessage = new StringBuffer();
2237
2238 duplicationMessage.append("\n");
2239 duplicationMessage.append(OleSelectConstant.DUPLICATE_INVOICE + " ");
2240 if (documents.size() > 0) {
2241 for (OleInvoiceDocument invDoc : documents) {
2242 if (invDoc.getDocumentNumber() != null &&
2243 invoiceDocument.getDocumentNumber() != null &&
2244 !invDoc.getDocumentNumber().equalsIgnoreCase(invoiceDocument.getDocumentNumber())) {
2245 String docNum = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEKeyConstants.LOAD_DUPLICATE_INVOICE);
2246 docNum = StringUtils.replace(docNum, "{0}", invDoc.getDocumentNumber());
2247 duplicationMessage.append(docNum + " ");
2248 isDuplicationExists = true;
2249 }
2250 }
2251 }
2252 if (isDuplicationExists && !isBlanketApprove) {
2253 duplicationMessage.append(OleSelectConstant.QUES_FOR_DUPLICATE_INVOICE);
2254 invoiceDocument.setDuplicateFlag(true);
2255 invoiceForm.setDuplicationMessage(duplicationMessage.toString());
2256 }
2257 else if (isDuplicationExists && isBlanketApprove) {
2258 duplicationMessage.append(OleSelectConstant.QUES_FOR_DUPLICATE_INVOICE);
2259 invoiceDocument.setDuplicateApproveFlag(true);
2260 invoiceForm.setDuplicationApproveMessage(duplicationMessage.toString());
2261 }
2262 }
2263 LOG.debug("Leaving method isDuplicationExists()");
2264 return isDuplicationExists;
2265 }
2266
2267
2268 public String getPaymentMethodType(String paymentId){
2269 if(paymentId != null && !(paymentId.equals(""))){
2270 Map payMap = new HashMap();
2271 payMap.put("paymentMethodId", paymentId);
2272 OlePaymentMethod olePaymentMethod = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePaymentMethod.class,payMap);
2273 if(olePaymentMethod != null){
2274 return olePaymentMethod.getPaymentMethod();
2275 }
2276 }
2277 return "";
2278 }
2279
2280 public boolean validateDepositAccount(OleInvoiceDocument oleInvoiceDocument) {
2281 boolean valid = true;
2282 if (getPaymentMethodType(oleInvoiceDocument.getPaymentMethodIdentifier()).equals(OLEConstants.DEPOSIT)) {
2283 for (OleInvoiceItem item : (List<OleInvoiceItem>) oleInvoiceDocument.getItems()) {
2284 KualiDecimal invoiceAmount = new KualiDecimal(item.getInvoiceListPrice());
2285 if (invoiceAmount.isLessEqual(KualiDecimal.ZERO) && item.getItemTypeCode().equals("ITEM")) {
2286 return false;
2287 }
2288 }
2289 }
2290 return valid;
2291 }
2292
2293
2294 public boolean isNotificationRequired(OleInvoiceDocument oleInvoiceDocument) {
2295 boolean isAmountExceeds = false;
2296 List<OleInvoiceEncumbranceNotification> invoiceEncumbranceNotificationList = new ArrayList<>();
2297 List<OleInvoiceItem> oleInvoiceItems = oleInvoiceDocument.getItems();
2298 for (OleInvoiceItem oleInvoiceItem : oleInvoiceItems) {
2299 if (oleInvoiceItem.getExtendedPrice() != null && oleInvoiceItem.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
2300 KualiDecimal extendedCost = oleInvoiceItem.getExtendedPrice();
2301 List<PurApAccountingLine> sourceAccountingLines = oleInvoiceItem.getSourceAccountingLines();
2302 for (PurApAccountingLine accLine : sourceAccountingLines) {
2303 Map<String, Object> key = new HashMap<String, Object>();
2304 String chartCode = accLine.getChartOfAccountsCode();
2305 String accNo = accLine.getAccountNumber();
2306 key.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
2307 key.put(OLEPropertyConstants.ACCOUNT_NUMBER, accNo);
2308 Account account = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(
2309 Account.class, key);
2310 if (account != null) {
2311 KualiDecimal thresholdAmount = null;
2312 KualiDecimal thresholdPercentLimit = null;
2313 KualiDecimal thresholdPercentage = null;
2314 KualiDecimal purchaseOrderAmount = KualiDecimal.ZERO;
2315 if (oleInvoiceItem.getPurchaseOrderItemUnitPrice() != null) {
2316 purchaseOrderAmount = new KualiDecimal(oleInvoiceItem.getPurchaseOrderItemUnitPrice());
2317 }
2318 if (account.getThresholdAmount() != null && oleInvoiceItem.getExtendedPrice() != null) {
2319 thresholdAmount = account.getThresholdAmount();
2320 thresholdAmount = thresholdAmount.add(purchaseOrderAmount);
2321 }
2322 if (account.getThresholdPercentage() != null && oleInvoiceItem.getExtendedPrice() != null) {
2323 thresholdPercentLimit = calculateThresholdAmount(account.getThresholdPercentage(), purchaseOrderAmount);
2324 thresholdPercentLimit = thresholdPercentLimit.add(purchaseOrderAmount);
2325 }
2326 if (thresholdAmount != null && thresholdPercentLimit != null) {
2327 if (extendedCost.isGreaterThan(thresholdAmount) && extendedCost.isGreaterThan(thresholdPercentLimit)) {
2328 OleInvoiceEncumbranceNotification invoiceEncumbranceNotification = new OleInvoiceEncumbranceNotification();
2329 invoiceEncumbranceNotification.setItemTitle(oleInvoiceItem.getItemDescription());
2330 invoiceEncumbranceNotification.setPurchaseOrderIdentifier(oleInvoiceItem.getPurchaseOrderIdentifier().toString());
2331 invoiceEncumbranceNotification.setPurchaseOrderAmount(purchaseOrderAmount.toString());
2332 invoiceEncumbranceNotification.setInvoiceAmount(oleInvoiceItem.getExtendedPrice().toString());
2333 invoiceEncumbranceNotification.setDifferenceByThresholdAmount((extendedCost.subtract(purchaseOrderAmount)).toString());
2334 invoiceEncumbranceNotificationList.add(invoiceEncumbranceNotification);
2335 }
2336 } else if (thresholdAmount != null) {
2337 if (extendedCost.isGreaterThan(thresholdAmount)) {
2338 OleInvoiceEncumbranceNotification invoiceEncumbranceNotification = new OleInvoiceEncumbranceNotification();
2339 invoiceEncumbranceNotification.setItemTitle(oleInvoiceItem.getItemDescription());
2340 invoiceEncumbranceNotification.setPurchaseOrderIdentifier(oleInvoiceItem.getPurchaseOrderIdentifier().toString());
2341 invoiceEncumbranceNotification.setPurchaseOrderAmount(purchaseOrderAmount.toString());
2342 invoiceEncumbranceNotification.setInvoiceAmount(oleInvoiceItem.getExtendedPrice().toString());
2343 invoiceEncumbranceNotification.setDifferenceByThresholdAmount((extendedCost.subtract(purchaseOrderAmount)).toString());
2344 invoiceEncumbranceNotificationList.add(invoiceEncumbranceNotification);
2345 }
2346 } else if (thresholdPercentLimit != null) {
2347 if (extendedCost.isGreaterThan(thresholdPercentLimit)) {
2348 OleInvoiceEncumbranceNotification invoiceEncumbranceNotification = new OleInvoiceEncumbranceNotification();
2349 invoiceEncumbranceNotification.setItemTitle(oleInvoiceItem.getItemDescription());
2350 invoiceEncumbranceNotification.setPurchaseOrderIdentifier(oleInvoiceItem.getPurchaseOrderIdentifier().toString());
2351 invoiceEncumbranceNotification.setPurchaseOrderAmount(purchaseOrderAmount.toString());
2352 invoiceEncumbranceNotification.setInvoiceAmount(oleInvoiceItem.getExtendedPrice().toString());
2353 invoiceEncumbranceNotification.setDifferenceByThresholdAmount((extendedCost.subtract(purchaseOrderAmount)).toString());
2354 invoiceEncumbranceNotificationList.add(invoiceEncumbranceNotification);
2355 }
2356 }
2357 }
2358 }
2359 }
2360 }
2361 if (invoiceEncumbranceNotificationList != null && invoiceEncumbranceNotificationList.size() > 0) {
2362 this.invoiceEncumbranceNotificationList = invoiceEncumbranceNotificationList;
2363 return true;
2364 }
2365 oleInvoiceDocument.setAmountExceeds(isAmountExceeds);
2366 return isAmountExceeds;
2367 }
2368
2369
2370 public KualiDecimal calculateThresholdAmount(KualiDecimal thresholdPercentage, KualiDecimal purchaseOrderAmount) {
2371 KualiDecimal thresholdLimt = (purchaseOrderAmount.multiply(thresholdPercentage)).divide(new KualiDecimal(100));
2372 return thresholdLimt;
2373 }
2374
2375
2376
2377 public String createInvoiceAmountExceedsThresholdText(OleInvoiceDocument oleinvoiceDocument) {
2378 StringBuffer warningMessageBuffer = new StringBuffer("");
2379 warningMessageBuffer.append("Warning: Some Titles on this Invoice are billed at a significantly higher rate than the PO line indicated:");
2380 warningMessageBuffer.append("<br/><br/><b>Summary Detail Below :</b><br/><br/><table border=\"1\">");
2381 warningMessageBuffer.append("<tr><th>Title</th>").
2382 append("<th>PO #</th>").
2383 append("<th>PO Price</th>").append("<th>Invoiced Price</th>").
2384 append("<th>Difference</th>");
2385 List<OleInvoiceEncumbranceNotification> oleInvoiceEncumbranceNotificationList = this.invoiceEncumbranceNotificationList;
2386 for (int accCount = 0; accCount < oleInvoiceEncumbranceNotificationList.size(); accCount++) {
2387 warningMessageBuffer.append("<tr><td>").append(oleInvoiceEncumbranceNotificationList.get(accCount).getItemTitle()).append("</td><td>").
2388 append(oleInvoiceEncumbranceNotificationList.get(accCount).getPurchaseOrderIdentifier()).append("</td><td>").
2389 append(oleInvoiceEncumbranceNotificationList.get(accCount).getPurchaseOrderAmount()).append("</td><td>").
2390 append(oleInvoiceEncumbranceNotificationList.get(accCount).getInvoiceAmount()).append("</td><td>").
2391 append(oleInvoiceEncumbranceNotificationList.get(accCount).getDifferenceByThresholdAmount());
2392
2393 }
2394 warningMessageBuffer.append("</td></tr></table>");
2395 warningMessageBuffer.append("<br/>Do you want to approve the invoice anyway?");
2396 return warningMessageBuffer.toString();
2397 }
2398
2399 public OleInvoiceRecord populateValuesFromProfile(BibMarcRecord bibMarcRecord){
2400 OleInvoiceRecord oleInvoiceRecord = new OleInvoiceRecord();
2401 mapDataFieldsToInvoiceRecord(oleInvoiceRecord,bibMarcRecord);
2402 setDefaultAndConstantValuesToInvoiceRecord(oleInvoiceRecord);
2403 checkForForeignCurrency(oleInvoiceRecord);
2404 oleInvoiceRecord.setUnitPrice(oleInvoiceRecord.getListPrice());
2405 return oleInvoiceRecord;
2406 }
2407
2408 private void mapDataFieldsToInvoiceRecord(OleInvoiceRecord oleInvoiceRecord,BibMarcRecord bibMarcRecord) {
2409 List<OLEBatchProcessProfileMappingOptionsBo> oleBatchProcessProfileMappingOptionsBoList = oleBatchProcessProfileBo.getOleBatchProcessProfileMappingOptionsList();
2410 for (OLEBatchProcessProfileMappingOptionsBo oleBatchProcessProfileMappingOptionsBo : oleBatchProcessProfileMappingOptionsBoList) {
2411 List<OLEBatchProcessProfileDataMappingOptionsBo> oleBatchProcessProfileDataMappingOptionsBoList = oleBatchProcessProfileMappingOptionsBo.getOleBatchProcessProfileDataMappingOptionsBoList();
2412 Collections.sort(oleBatchProcessProfileDataMappingOptionsBoList, new Comparator<OLEBatchProcessProfileDataMappingOptionsBo>() {
2413 @Override
2414 public int compare(OLEBatchProcessProfileDataMappingOptionsBo obj1, OLEBatchProcessProfileDataMappingOptionsBo obj2) {
2415 int result = obj1.getDestinationField().compareTo(obj2.getDestinationField());
2416 if(result != 0){
2417 return result;
2418 }
2419 return obj1.getPriority() < obj2.getPriority() ? -1 : obj1.getPriority() > obj2.getPriority() ? 1 : 0;
2420 }
2421 });
2422 List<String> failureRecords = new ArrayList<>();
2423 for (int dataMapCount = 0;dataMapCount<oleBatchProcessProfileDataMappingOptionsBoList.size();dataMapCount++) {
2424 if (StringUtils.isNotBlank(oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getDataTypeDestinationField()) &&
2425 StringUtils.isNotBlank(oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getSourceField()) && StringUtils.isNotBlank(oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getDestinationField())) {
2426 String sourceField = oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getSourceField();
2427 String sourceFields[] = sourceField.split("\\$");
2428 if (sourceFields.length == 2) {
2429 String dataField = sourceFields[0].trim();
2430 String tagField = sourceFields[1].trim();
2431 if (org.kuali.ole.OLEConstants.OLEBatchProcess.VENDOR_ITEM_IDENTIFIER.equals(oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getDestinationField())) {
2432 String vendorItemIdentifier = setDataMappingValues(oleBatchProcessProfileDataMappingOptionsBoList,dataMapCount,bibMarcRecord,dataField,tagField);
2433 if(!StringUtils.isBlank(vendorItemIdentifier)){
2434 oleInvoiceRecord.setVendorItemIdentifier(vendorItemIdentifier);
2435 }
2436 else {
2437 failureRecords.add(org.kuali.ole.OLEConstants.REQUIRED_VENDOR_ITEM_IDENTIFIER + " " + dataField + org.kuali.ole.OLEConstants.DELIMITER_DOLLAR + tagField + " " + org.kuali.ole.OLEConstants.NULL_VALUE_MESSAGE);
2438 }
2439 }
2440 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.VENDOR_NUMBER.equals(oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getDestinationField())) {
2441 String vendorNumber = setDataMappingValues(oleBatchProcessProfileDataMappingOptionsBoList,dataMapCount,bibMarcRecord,dataField,tagField);
2442 if(!StringUtils.isBlank(vendorNumber)) {
2443 boolean validVendorNumber = getOleOrderRecordService().validateVendorNumber(vendorNumber);
2444 if(!validVendorNumber){
2445 failureRecords.add(org.kuali.ole.OLEConstants.INVALID_VENDOR_NUMBER + " "+ dataField + " " + org.kuali.ole.OLEConstants.DELIMITER_DOLLAR + tagField + " " + vendorNumber);
2446 vendorNumber = null;
2447 }
2448 oleInvoiceRecord.setVendorNumber(vendorNumber);
2449 }
2450 else {
2451 failureRecords.add(org.kuali.ole.OLEConstants.REQUIRED_VENDOR_NUMBER + " " + dataField + org.kuali.ole.OLEConstants.DELIMITER_DOLLAR + tagField + " " + org.kuali.ole.OLEConstants.NULL_VALUE_MESSAGE);
2452 }
2453 }
2454 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.LIST_PRICE.equals(oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getDestinationField())) {
2455 String listPrice = setDataMappingValues(oleBatchProcessProfileDataMappingOptionsBoList,dataMapCount,bibMarcRecord,dataField,tagField);
2456 if(!StringUtils.isBlank(listPrice)){
2457 if(!StringUtils.isBlank(listPrice)){
2458 boolean validListPrice = getOleOrderRecordService().validateDestinationFieldValues(listPrice);
2459 if(!validListPrice){
2460 failureRecords.add(org.kuali.ole.OLEConstants.INVALID_INVOICED_PRICE + " "+ dataField + " " + org.kuali.ole.OLEConstants.DELIMITER_DOLLAR + tagField + " " + listPrice);
2461 listPrice = null;
2462 }
2463 else {
2464 listPrice = Float.parseFloat(listPrice) + "";
2465 }
2466 oleInvoiceRecord.setListPrice(listPrice);
2467 }
2468 }
2469 else{
2470 failureRecords.add(org.kuali.ole.OLEConstants.REQUIRED_INVOICED_PRICE + " " + dataField + org.kuali.ole.OLEConstants.DELIMITER_DOLLAR + tagField + " " + org.kuali.ole.OLEConstants.NULL_VALUE_MESSAGE);
2471 }
2472 }
2473 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.FOREIGN_LIST_PRICE.equals(oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getDestinationField())) {
2474 String foreignListPrice = setDataMappingValues(oleBatchProcessProfileDataMappingOptionsBoList,dataMapCount,bibMarcRecord,dataField,tagField);
2475 if(!StringUtils.isBlank(foreignListPrice)){
2476 if(!StringUtils.isBlank(foreignListPrice)){
2477 boolean validForeignListPrice = getOleOrderRecordService().validateDestinationFieldValues(foreignListPrice);
2478 if(!validForeignListPrice){
2479 failureRecords.add(org.kuali.ole.OLEConstants.INVALID_FOREIGN_INVOICED_PRICE + " "+ dataField + " " + org.kuali.ole.OLEConstants.DELIMITER_DOLLAR + tagField + " " + foreignListPrice);
2480 foreignListPrice = null;
2481 }
2482 else {
2483 foreignListPrice = Float.parseFloat(foreignListPrice) + "";
2484 }
2485 oleInvoiceRecord.setForeignListPrice(foreignListPrice);
2486 }
2487 }
2488 else{
2489 failureRecords.add(org.kuali.ole.OLEConstants.REQUIRED_INVOICED_FOREIGN_PRICE + " " + dataField + org.kuali.ole.OLEConstants.DELIMITER_DOLLAR + tagField + " " + org.kuali.ole.OLEConstants.NULL_VALUE_MESSAGE);
2490 }
2491 }
2492 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.QUANTITY.equals(oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getDestinationField())) {
2493 String quantity = setDataMappingValues(oleBatchProcessProfileDataMappingOptionsBoList,dataMapCount,bibMarcRecord,dataField,tagField);
2494 if(!StringUtils.isBlank(quantity)) {
2495 boolean validQuantity = getOleOrderRecordService().validateForNumber(quantity);
2496 if(!validQuantity){
2497 failureRecords.add(org.kuali.ole.OLEConstants.INVALID_QTY + " "+ dataField + " " + org.kuali.ole.OLEConstants.DELIMITER_DOLLAR + tagField + " " + quantity);
2498 quantity = null;
2499 }
2500 oleInvoiceRecord.setQuantity(quantity);
2501 }
2502 else {
2503 failureRecords.add(org.kuali.ole.OLEConstants.REQUIRED_QTY + " " + dataField + org.kuali.ole.OLEConstants.DELIMITER_DOLLAR + tagField + " " + org.kuali.ole.OLEConstants.NULL_VALUE_MESSAGE);
2504 }
2505 }
2506 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.ITEM_TYPES.equals(oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getDestinationField())) {
2507 String itemType = setDataMappingValues(oleBatchProcessProfileDataMappingOptionsBoList,dataMapCount,bibMarcRecord,dataField,tagField);
2508 if(!StringUtils.isBlank(itemType)){
2509 Map<String,String> itemTypeMap = new HashMap<>();
2510 itemTypeMap.put(org.kuali.ole.OLEConstants.OLEBatchProcess.ITEM_TYPE_CODE, itemType);
2511 List<ItemType> itemTypeList = (List) getBusinessObjectService().findMatching(ItemType.class, itemTypeMap);
2512 if(itemTypeList != null && itemTypeList.size() == 0){
2513 failureRecords.add(org.kuali.ole.OLEConstants.INVALID_ITEM_TYPE_CD + " "+ dataField + " " + org.kuali.ole.OLEConstants.DELIMITER_DOLLAR + tagField + " " + itemType);
2514 }
2515 oleInvoiceRecord.setItemType(org.kuali.ole.OLEConstants.ITEM_TYP);
2516 }
2517 else {
2518 failureRecords.add(org.kuali.ole.OLEConstants.REQUIRED_ITEM_TYPE + " " + dataField + org.kuali.ole.OLEConstants.DELIMITER_DOLLAR + tagField + " " + org.kuali.ole.OLEConstants.NULL_VALUE_MESSAGE);
2519 }
2520 }
2521 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.INVOICE_NUMBER.equals(oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getDestinationField())) {
2522 String invoiceNumber = setDataMappingValues(oleBatchProcessProfileDataMappingOptionsBoList,dataMapCount,bibMarcRecord,dataField,tagField);
2523 if(!StringUtils.isBlank(invoiceNumber)) {
2524
2525
2526
2527
2528
2529 oleInvoiceRecord.setInvoiceNumber(invoiceNumber);
2530 }
2531 else {
2532
2533 }
2534 }
2535 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.INVOICE_DATE.equals(oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getDestinationField())) {
2536 String invoiceDate = setDataMappingValues(oleBatchProcessProfileDataMappingOptionsBoList,dataMapCount,bibMarcRecord,dataField,tagField);
2537 if(!StringUtils.isBlank(invoiceDate)){
2538 boolean validInvoiceDate = validateInvoiceDate(invoiceDate);
2539 if(!validInvoiceDate){
2540 failureRecords.add(org.kuali.ole.OLEConstants.INVALID_INV_DT + " " + dataField + " " + org.kuali.ole.OLEConstants.DELIMITER_DOLLAR + tagField + " " + invoiceDate + " " + "Allowed format is yyyymmdd");
2541 }
2542 oleInvoiceRecord.setInvoiceDate(invoiceDate);
2543 }
2544 else {
2545 failureRecords.add(org.kuali.ole.OLEConstants.REQUIRED_INVOICE_DATE + " " + dataField + org.kuali.ole.OLEConstants.DELIMITER_DOLLAR + tagField + " " + org.kuali.ole.OLEConstants.NULL_VALUE_MESSAGE);
2546 }
2547 }
2548 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.ITEM_DESCRIPTION.equals(oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getDestinationField())) {
2549 String itemDescription = setDataMappingValues(oleBatchProcessProfileDataMappingOptionsBoList,dataMapCount,bibMarcRecord,dataField,tagField);
2550 if(!StringUtils.isBlank(itemDescription)){
2551 oleInvoiceRecord.setItemDescription(itemDescription);
2552 }
2553 else {
2554
2555 }
2556 }
2557 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.ACCOUNT_NUMBER.equals(oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getDestinationField())) {
2558 String accountNumber = setDataMappingValues(oleBatchProcessProfileDataMappingOptionsBoList,dataMapCount,bibMarcRecord,dataField,tagField);
2559 if(!StringUtils.isBlank(accountNumber)){
2560 Map<String,String> accountNumberMap = new HashMap<>();
2561 accountNumberMap.put(org.kuali.ole.OLEConstants.OLEBatchProcess.ACCOUNT_NUMBER, accountNumber);
2562 List<Account> accountNumberList = (List) getBusinessObjectService().findMatching(Account.class, accountNumberMap);
2563 if(accountNumberList.size() == 0){
2564 failureRecords.add(org.kuali.ole.OLEConstants.INVALID_ACCOUNT_NUMBER + " " +dataField + " " + org.kuali.ole.OLEConstants.DELIMITER_DOLLAR + tagField +" " + accountNumber);
2565 accountNumber = null;
2566 }
2567 oleInvoiceRecord.setAccountNumber(accountNumber);
2568 }
2569 else {
2570 failureRecords.add(org.kuali.ole.OLEConstants.REQUIRED_ACCOUNT_NUMBER + " " + dataField + org.kuali.ole.OLEConstants.DELIMITER_DOLLAR + tagField + " " + org.kuali.ole.OLEConstants.NULL_VALUE_MESSAGE);
2571 }
2572 }
2573 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.OBJECT_CODE.equals(oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getDestinationField())) {
2574 String objectCode = setDataMappingValues(oleBatchProcessProfileDataMappingOptionsBoList,dataMapCount,bibMarcRecord,dataField,tagField);
2575 if(!StringUtils.isBlank(objectCode)){
2576 Map<String,String> objectCodeMap = new HashMap<>();
2577 objectCodeMap.put(org.kuali.ole.OLEConstants.OLEBatchProcess.OBJECT_CODE, objectCode);
2578 List<ObjectCode> objectCodeList = (List) getBusinessObjectService().findMatching(ObjectCode.class, objectCodeMap);
2579 if(objectCodeList.size() == 0){
2580 failureRecords.add(org.kuali.ole.OLEConstants.INVALID_OBJECT_CODE + " " +dataField + " " + org.kuali.ole.OLEConstants.DELIMITER_DOLLAR + tagField +" " + objectCode);
2581 objectCode = null;
2582 }
2583 oleInvoiceRecord.setObjectCode(objectCode);
2584 }
2585 else {
2586 failureRecords.add(org.kuali.ole.OLEConstants.REQUIRED_OBJECT_CODE + " " + dataField + org.kuali.ole.OLEConstants.DELIMITER_DOLLAR + tagField + " " + org.kuali.ole.OLEConstants.NULL_VALUE_MESSAGE);
2587 }
2588 }
2589 }
2590 }
2591 }
2592 if(failureRecords != null && failureRecords.size() > 0){
2593 List reasonForFailure = (List) dataCarrierService.getData("invoiceIngestFailureReason");
2594 if(reasonForFailure != null){
2595 reasonForFailure.addAll(failureRecords);
2596 reasonForFailure.add("==================================================================================");
2597 dataCarrierService.addData("invoiceIngestFailureReason",reasonForFailure);
2598 }
2599 }
2600 }
2601 }
2602
2603 public void setDefaultAndConstantValuesToInvoiceRecord(OleInvoiceRecord oleInvoiceRecord) {
2604 List<OLEBatchProcessProfileConstantsBo> oleBatchProcessProfileConstantsBoList = oleBatchProcessProfileBo.getOleBatchProcessProfileConstantsList();
2605 for (OLEBatchProcessProfileConstantsBo oleBatchProcessProfileConstantsBo : oleBatchProcessProfileConstantsBoList) {
2606 if (StringUtils.isNotBlank(oleBatchProcessProfileConstantsBo.getDataType()) && org.kuali.ole.OLEConstants.OLEBatchProcess.INVOICE_IMPORT.equalsIgnoreCase(oleBatchProcessProfileConstantsBo.getDataType())
2607 && StringUtils.isNotBlank(oleBatchProcessProfileConstantsBo.getAttributeValue()) && StringUtils.isNotBlank(oleBatchProcessProfileConstantsBo.getAttributeName())) {
2608 if (org.kuali.ole.OLEConstants.OLEBatchProcess.CONSTANT.equals(oleBatchProcessProfileConstantsBo.getDefaultValue())) {
2609
2610 if (org.kuali.ole.OLEConstants.OLEBatchProcess.VENDOR_ITEM_IDENTIFIER.equals(oleBatchProcessProfileConstantsBo.getAttributeName())) {
2611 oleInvoiceRecord.setVendorItemIdentifier(oleBatchProcessProfileConstantsBo.getAttributeValue());
2612 }
2613 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.VENDOR_NUMBER.equals(oleBatchProcessProfileConstantsBo.getAttributeName())) {
2614 oleInvoiceRecord.setVendorNumber(oleBatchProcessProfileConstantsBo.getAttributeValue());
2615 }
2616 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.LIST_PRICE.equals(oleBatchProcessProfileConstantsBo.getAttributeName())) {
2617 oleInvoiceRecord.setListPrice(oleBatchProcessProfileConstantsBo.getAttributeValue());
2618 }
2619 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.FOREIGN_LIST_PRICE.equals(oleBatchProcessProfileConstantsBo.getAttributeName())) {
2620 oleInvoiceRecord.setForeignListPrice(oleBatchProcessProfileConstantsBo.getAttributeValue());
2621 }
2622 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.QUANTITY.equals(oleBatchProcessProfileConstantsBo.getAttributeName())) {
2623 oleInvoiceRecord.setQuantity(oleBatchProcessProfileConstantsBo.getAttributeValue());
2624 }
2625 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.ITEM_TYPES.equals(oleBatchProcessProfileConstantsBo.getAttributeName())) {
2626 oleInvoiceRecord.setItemType(org.kuali.ole.OLEConstants.ITEM_TYP);
2627 }
2628 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.INVOICE_NUMBER.equals(oleBatchProcessProfileConstantsBo.getAttributeName())) {
2629 oleInvoiceRecord.setInvoiceNumber(oleBatchProcessProfileConstantsBo.getAttributeValue());
2630 }
2631 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.INVOICE_DATE.equals(oleBatchProcessProfileConstantsBo.getAttributeName())) {
2632 oleInvoiceRecord.setInvoiceDate(oleBatchProcessProfileConstantsBo.getAttributeValue());
2633 }
2634 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.ITEM_DESCRIPTION.equals(oleBatchProcessProfileConstantsBo.getAttributeName())) {
2635 oleInvoiceRecord.setItemDescription(oleBatchProcessProfileConstantsBo.getAttributeValue());
2636 }
2637 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.ACCOUNT_NUMBER.equals(oleBatchProcessProfileConstantsBo.getAttributeName())) {
2638 oleInvoiceRecord.setAccountNumber(oleBatchProcessProfileConstantsBo.getAttributeValue());
2639 }
2640 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.OBJECT_CODE.equals(oleBatchProcessProfileConstantsBo.getAttributeName())) {
2641 oleInvoiceRecord.setObjectCode(oleBatchProcessProfileConstantsBo.getAttributeValue());
2642 }
2643 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.CURRENCY_TYPE.equals(oleBatchProcessProfileConstantsBo.getAttributeName())) {
2644 oleInvoiceRecord.setCurrencyType(oleBatchProcessProfileConstantsBo.getAttributeValue());
2645 oleInvoiceRecord.setCurrencyTypeId(getCurrencyTypeIdFromCurrencyType(oleInvoiceRecord.getCurrencyType()));
2646 }
2647 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.EXCHANGE_RATE.equals(oleBatchProcessProfileConstantsBo.getAttributeName())) {
2648 oleInvoiceRecord.setInvoiceCurrencyExchangeRate(oleBatchProcessProfileConstantsBo.getAttributeValue());
2649 }
2650 }
2651 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.DEFAULT.equals(oleBatchProcessProfileConstantsBo.getDefaultValue())) {
2652
2653 if (org.kuali.ole.OLEConstants.OLEBatchProcess.VENDOR_ITEM_IDENTIFIER.equals(oleBatchProcessProfileConstantsBo.getAttributeName()) && StringUtils.isBlank(oleInvoiceRecord.getVendorItemIdentifier())) {
2654 oleInvoiceRecord.setVendorItemIdentifier(oleBatchProcessProfileConstantsBo.getAttributeValue());
2655 }
2656 if (org.kuali.ole.OLEConstants.OLEBatchProcess.VENDOR_NUMBER.equals(oleBatchProcessProfileConstantsBo.getAttributeName()) && StringUtils.isBlank(oleInvoiceRecord.getVendorNumber())) {
2657 oleInvoiceRecord.setVendorNumber(oleBatchProcessProfileConstantsBo.getAttributeValue());
2658 }
2659 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.LIST_PRICE.equals(oleBatchProcessProfileConstantsBo.getAttributeName()) && StringUtils.isBlank(oleInvoiceRecord.getListPrice())) {
2660 oleInvoiceRecord.setListPrice(oleBatchProcessProfileConstantsBo.getAttributeValue());
2661 }
2662 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.FOREIGN_LIST_PRICE.equals(oleBatchProcessProfileConstantsBo.getAttributeName()) && StringUtils.isBlank(oleInvoiceRecord.getForeignListPrice())) {
2663 oleInvoiceRecord.setForeignListPrice(oleBatchProcessProfileConstantsBo.getAttributeValue());
2664 }
2665 else if (org.kuali.ole.OLEConstants.OLEBatchProcess.QUANTITY.equals(oleBatchProcessProfileConstantsBo.getAttributeName()) && StringUtils.isBlank(oleInvoiceRecord.getQuantity())) {
2666 oleInvoiceRecord.setQuantity(oleBatchProcessProfileConstantsBo.getAttributeValue());
2667 }
2668 else if(org.kuali.ole.OLEConstants.OLEBatchProcess.ITEM_TYPES.equals(oleBatchProcessProfileConstantsBo.getAttributeName()) && StringUtils.isBlank(oleInvoiceRecord.getItemType())) {
2669 oleInvoiceRecord.setItemType(org.kuali.ole.OLEConstants.ITEM_TYP);
2670 }
2671 else if(org.kuali.ole.OLEConstants.OLEBatchProcess.INVOICE_NUMBER.equals(oleBatchProcessProfileConstantsBo.getAttributeName()) && StringUtils.isBlank(oleInvoiceRecord.getInvoiceNumber())) {
2672 oleInvoiceRecord.setInvoiceNumber(oleBatchProcessProfileConstantsBo.getAttributeValue());
2673 }
2674 else if(org.kuali.ole.OLEConstants.OLEBatchProcess.INVOICE_DATE.equals(oleBatchProcessProfileConstantsBo.getAttributeName()) && StringUtils.isBlank(oleInvoiceRecord.getInvoiceDate())) {
2675 oleInvoiceRecord.setInvoiceDate(oleBatchProcessProfileConstantsBo.getAttributeValue());
2676 }
2677 else if(org.kuali.ole.OLEConstants.OLEBatchProcess.ITEM_DESCRIPTION.equals(oleBatchProcessProfileConstantsBo.getAttributeName()) && StringUtils.isBlank(oleInvoiceRecord.getItemDescription())) {
2678 oleInvoiceRecord.setItemDescription(oleBatchProcessProfileConstantsBo.getAttributeValue());
2679 }
2680 else if(org.kuali.ole.OLEConstants.OLEBatchProcess.ACCOUNT_NUMBER.equals(oleBatchProcessProfileConstantsBo.getAttributeName()) && StringUtils.isBlank(oleInvoiceRecord.getAccountNumber())) {
2681 oleInvoiceRecord.setAccountNumber(oleBatchProcessProfileConstantsBo.getAttributeValue());
2682 }
2683 else if(org.kuali.ole.OLEConstants.OLEBatchProcess.OBJECT_CODE.equals(oleBatchProcessProfileConstantsBo.getAttributeName()) && StringUtils.isBlank(oleInvoiceRecord.getObjectCode())) {
2684 oleInvoiceRecord.setObjectCode(oleBatchProcessProfileConstantsBo.getAttributeValue());
2685 }
2686 else if(org.kuali.ole.OLEConstants.OLEBatchProcess.CURRENCY_TYPE.equals(oleBatchProcessProfileConstantsBo.getAttributeName()) && StringUtils.isBlank(oleInvoiceRecord.getCurrencyType())) {
2687 oleInvoiceRecord.setCurrencyType(oleBatchProcessProfileConstantsBo.getAttributeValue());
2688 oleInvoiceRecord.setCurrencyTypeId(getCurrencyTypeIdFromCurrencyType(oleInvoiceRecord.getCurrencyType()));
2689 }
2690 else if(org.kuali.ole.OLEConstants.OLEBatchProcess.EXCHANGE_RATE.equals(oleBatchProcessProfileConstantsBo.getAttributeName()) && StringUtils.isBlank(oleInvoiceRecord.getInvoiceCurrencyExchangeRate())) {
2691 oleInvoiceRecord.setInvoiceCurrencyExchangeRate(oleBatchProcessProfileConstantsBo.getAttributeValue());
2692 }
2693 }
2694 }
2695 }
2696 }
2697
2698 public String getCurrencyTypeIdFromCurrencyType(String currencyType){
2699 Map<String,String> currencyTypeMap = new HashMap<>();
2700 currencyTypeMap.put(org.kuali.ole.OLEConstants.OLEBatchProcess.CURRENCY_TYPE,currencyType);
2701 List<OleCurrencyType> currencyTypeList = (List) getBusinessObjectService().findMatching(OleCurrencyType.class, currencyTypeMap);
2702 return currencyTypeList.get(0).getCurrencyTypeId().toString();
2703 }
2704
2705 private String getSubFieldValueFor(BibMarcRecord bibMarcRecord, String dataField, String tag) {
2706 String subFieldValue = null;
2707 org.kuali.ole.docstore.common.document.content.bib.marc.DataField dataFieldForTag = getDataFieldForTag(bibMarcRecord, dataField);
2708 if (null != dataFieldForTag) {
2709 List<org.kuali.ole.docstore.common.document.content.bib.marc.SubField> subfields = dataFieldForTag.getSubFields();
2710 for (Iterator<org.kuali.ole.docstore.common.document.content.bib.marc.SubField> iterator = subfields.iterator(); iterator.hasNext(); ) {
2711 org.kuali.ole.docstore.common.document.content.bib.marc.SubField marcSubField = iterator.next();
2712 if (marcSubField.getCode().equals(tag)) {
2713 return marcSubField.getValue();
2714 }
2715 }
2716 }
2717 return subFieldValue;
2718 }
2719
2720 public org.kuali.ole.docstore.common.document.content.bib.marc.DataField getDataFieldForTag(BibMarcRecord bibMarcRecord, String tag) {
2721 for (Iterator<org.kuali.ole.docstore.common.document.content.bib.marc.DataField> iterator = bibMarcRecord.getDataFields().iterator(); iterator.hasNext(); ) {
2722 org.kuali.ole.docstore.common.document.content.bib.marc.DataField marcDataField = iterator.next();
2723 if (marcDataField.getTag().equalsIgnoreCase(tag)) {
2724 return marcDataField;
2725 }
2726 }
2727 return null;
2728 }
2729
2730
2731 private String setDataMappingValues(List<OLEBatchProcessProfileDataMappingOptionsBo> oleBatchProcessProfileDataMappingOptionsBoList,int dataMapCount,BibMarcRecord bibMarcRecord,String dataField,String tagField){
2732 String subFieldValue = getSubFieldValueFor(bibMarcRecord, dataField, tagField);
2733 if (StringUtils.isBlank(subFieldValue)) {
2734 OLEBatchProcessProfileDataMappingOptionsBo oleBatchProcessProfileDataMappingOptionsBo = oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount);
2735 if (dataMapCount+1 <= oleBatchProcessProfileDataMappingOptionsBoList.size()) {
2736 if(dataMapCount+1 == oleBatchProcessProfileDataMappingOptionsBoList.size()) {
2737 subFieldValue = oleBatchProcessProfileDataMappingOptionsBo.getDestinationFieldValue();
2738 }
2739 else if(!oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount+1).getDestinationField().equalsIgnoreCase(oleBatchProcessProfileDataMappingOptionsBo.getDestinationField())){
2740 subFieldValue = oleBatchProcessProfileDataMappingOptionsBo.getDestinationFieldValue();
2741 }
2742 }
2743 }
2744 return subFieldValue;
2745 }
2746
2747 private boolean validateInvoiceDate(String invoiceDate){
2748 SimpleDateFormat dateFromRawFile = new SimpleDateFormat(org.kuali.ole.OLEConstants.DATE_FORMAT);
2749 try {
2750 dateFromRawFile.parse(invoiceDate);
2751 return true;
2752 }
2753 catch (ParseException e) {
2754 return false;
2755 }
2756 }
2757
2758 private void checkForForeignCurrency(OleInvoiceRecord oleInvoiceRecord){
2759 if(!StringUtils.isBlank(oleInvoiceRecord.getCurrencyType())){
2760 if(!oleInvoiceRecord.getCurrencyType().equalsIgnoreCase(OleSelectConstant.CURRENCY_TYPE_NAME)){
2761 if (oleInvoiceRecord.getForeignListPrice() != null && !oleInvoiceRecord.getForeignListPrice().isEmpty() &&
2762 oleInvoiceRecord.getInvoiceCurrencyExchangeRate()!= null && !oleInvoiceRecord.getInvoiceCurrencyExchangeRate().isEmpty()) {
2763 oleInvoiceRecord.setListPrice((new BigDecimal(oleInvoiceRecord.getForeignListPrice()).
2764 divide(new BigDecimal(oleInvoiceRecord.getInvoiceCurrencyExchangeRate()), 4, RoundingMode.HALF_UP)).toString());
2765 }
2766 }
2767 }
2768 }
2769
2770
2771 @Override
2772 public String getCurrencyType(String currencyTypeId) {
2773 if(StringUtils.isNotBlank(currencyTypeId)){
2774 Map currencyMap = new HashMap();
2775 currencyMap.put(OleSelectConstant.CURRENCY_TYPE_ID, new Long(currencyTypeId));
2776 OleCurrencyType oleCurrencyType = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleCurrencyType.class,currencyMap);
2777 if(oleCurrencyType != null){
2778 return oleCurrencyType.getCurrencyType();
2779 }
2780 }
2781 return "";
2782 }
2783
2784
2785 @Override
2786 public OleExchangeRate getExchangeRate(String currencyTypeId) {
2787 Map documentNumberMap = new HashMap();
2788 documentNumberMap.put(OleSelectConstant.CURRENCY_TYPE_ID, new Long(currencyTypeId));
2789 List<OleExchangeRate> exchangeRateList = (List) getBusinessObjectService().findMatchingOrderBy(OleExchangeRate.class, documentNumberMap, OleSelectConstant.EXCHANGE_RATE_DATE, false);
2790 Iterator iterator = exchangeRateList.iterator();
2791 if (iterator.hasNext()) {
2792 OleExchangeRate tempOleExchangeRate = (OleExchangeRate) iterator.next();
2793 return tempOleExchangeRate;
2794 }
2795 return null;
2796 }
2797
2798 @Override
2799 public void deleteInvoiceItem(OleInvoiceDocument oleInvoiceDocument) {
2800 List<OleInvoiceItem> oleDeletedInvoiceItemList =oleInvoiceDocument.getDeletedInvoiceItems();
2801 for(OleInvoiceItem oleInvoiceItem:oleDeletedInvoiceItemList){
2802 List<OLEPaidCopy> olePaidCopies = oleInvoiceItem.getPaidCopies();
2803 for(OLEPaidCopy olePaidCopy : olePaidCopies){
2804 Map<String,Integer> olePaidCopyMap=new HashMap<String,Integer>();
2805 olePaidCopyMap.put("olePaidCopyId",olePaidCopy.getOlePaidCopyId());
2806 KRADServiceLocator.getBusinessObjectService().deleteMatching(OLEPaidCopy.class,olePaidCopyMap);
2807 }
2808 Map<String,Integer> invoiceAccountMap=new HashMap<String,Integer>();
2809 invoiceAccountMap.put("itemIdentifier",oleInvoiceItem.getItemIdentifier());
2810 KRADServiceLocator.getBusinessObjectService().deleteMatching(InvoiceAccount.class,invoiceAccountMap);
2811 List<OleInvoiceNote> oleDeletedNotesList = oleInvoiceItem.getNotes();
2812 for(OleInvoiceNote oleInvoiceNote:oleDeletedNotesList) {
2813 Map<String,Integer> invoiceNoteMap=new HashMap<String,Integer>();
2814 invoiceNoteMap.put("itemNoteIdentifier",oleInvoiceNote.getItemNoteIdentifier());
2815 KRADServiceLocator.getBusinessObjectService().deleteMatching(OleInvoiceNote.class,invoiceNoteMap);
2816 }
2817 Map<String,Integer> invoiceItemMap=new HashMap<String,Integer>();
2818 invoiceItemMap.put("itemIdentifier",oleInvoiceItem.getItemIdentifier());
2819 KRADServiceLocator.getBusinessObjectService().deleteMatching(OleInvoiceItem.class,invoiceItemMap);
2820 }
2821 }
2822
2823 public OleSelectDocumentService getOleSelectDocumentService() {
2824 if(oleSelectDocumentService == null){
2825 oleSelectDocumentService = SpringContext.getBean(OleSelectDocumentService.class);
2826 }
2827 return oleSelectDocumentService;
2828 }
2829
2830 public void setOleSelectDocumentService(OleSelectDocumentService oleSelectDocumentService) {
2831 this.oleSelectDocumentService = oleSelectDocumentService;
2832 }
2833
2834 public String getPurchaseOrderVendor(String poId) {
2835 Map poMap = new HashMap();
2836 poMap.put(OLEConstants.PUR_DOC_IDENTIFIER,poId);
2837 OlePurchaseOrderDocument document = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePurchaseOrderDocument.class,poMap);
2838 if(document != null) {
2839 return document.getVendorName();
2840 }
2841 return null;
2842
2843 }
2844 }