1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.ole.select.document;
18
19 import org.apache.commons.lang.StringEscapeUtils;
20 import org.apache.commons.lang.StringUtils;
21 import org.kuali.ole.DocumentUniqueIDPrefix;
22 import org.kuali.ole.docstore.common.client.*;
23 import org.kuali.ole.docstore.common.document.Bib;
24 import org.kuali.ole.docstore.common.document.BibMarc;
25 import org.kuali.ole.module.purap.PurapConstants;
26 import org.kuali.ole.module.purap.PurapPropertyConstants;
27 import org.kuali.ole.module.purap.PurapWorkflowConstants;
28 import org.kuali.ole.module.purap.businessobject.CreditMemoItem;
29 import org.kuali.ole.module.purap.businessobject.PurchaseOrderType;
30 import org.kuali.ole.module.purap.document.VendorCreditMemoDocument;
31 import org.kuali.ole.module.purap.document.service.PaymentRequestService;
32 import org.kuali.ole.module.purap.document.validation.event.AttributedContinuePurapEvent;
33 import org.kuali.ole.select.OleSelectConstant;
34 import org.kuali.ole.select.businessobject.OleCreditMemoItem;
35 import org.kuali.ole.select.businessobject.OleInvoiceType;
36 import org.kuali.ole.select.document.service.OleCreditMemoService;
37 import org.kuali.ole.select.document.service.OlePurchaseOrderDocumentHelperService;
38 import org.kuali.ole.select.service.BibInfoService;
39 import org.kuali.ole.select.service.BibInfoWrapperService;
40 import org.kuali.ole.select.service.FileProcessingService;
41 import org.kuali.ole.select.service.impl.BibInfoServiceImpl;
42 import org.kuali.ole.sys.OLEConstants;
43 import org.kuali.ole.sys.context.SpringContext;
44 import org.kuali.ole.vnd.businessobject.OleExchangeRate;
45 import org.kuali.ole.vnd.businessobject.VendorAlias;
46 import org.kuali.rice.core.api.config.property.ConfigurationService;
47 import org.kuali.rice.core.api.util.type.KualiDecimal;
48 import org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent;
49 import org.kuali.rice.krad.service.BusinessObjectService;
50 import org.kuali.rice.krad.util.ObjectUtils;
51
52 import java.math.BigDecimal;
53 import java.math.RoundingMode;
54 import java.util.*;
55
56
57
58
59 public class OleVendorCreditMemoDocument extends VendorCreditMemoDocument {
60 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleVendorCreditMemoDocument.class);
61
62
63 private static transient BusinessObjectService businessObjectService;
64 private static transient ConfigurationService kualiConfigurationService;
65 private static transient FileProcessingService fileProcessingService;
66 private static transient BibInfoWrapperService bibInfoWrapperService;
67 private static transient BibInfoService bibInfoService;
68
69 private boolean prorateQty;
70 private boolean prorateDollar;
71 private boolean prorateManual;
72 private boolean noProrate;
73
74 private String prorateBy;
75 private boolean currencyTypeIndicator = true;
76
77 private String vendorAliasName;
78 public String getVendorAliasName() {
79 return vendorAliasName;
80 }
81
82 public void setVendorAliasName(String vendorAliasName) {
83 this.vendorAliasName = vendorAliasName;
84 }
85
86 private Integer invoiceIdentifier;
87
88 public OleVendorCreditMemoDocument() {
89 super();
90 }
91
92 @Override
93 public Class getItemClass() {
94 return OleCreditMemoItem.class;
95 }
96
97 public static ConfigurationService getConfigurationService() {
98 if (kualiConfigurationService == null) {
99 kualiConfigurationService = SpringContext.getBean(ConfigurationService.class);
100 }
101 return kualiConfigurationService;
102 }
103
104 public static void setConfigurationService(ConfigurationService kualiConfigurationService) {
105 OleVendorCreditMemoDocument.kualiConfigurationService = kualiConfigurationService;
106 }
107
108 public String getBibeditorCreateURL() {
109 String bibeditorCreateURL = getConfigurationService().getPropertyValueAsString(
110 OLEConstants.BIBEDITOR_CREATE_URL_KEY);
111 return bibeditorCreateURL;
112 }
113
114 public String getBibeditorEditURL() {
115 String bibeditorEditURL = getConfigurationService().getPropertyValueAsString(OLEConstants.BIBEDITOR_URL_KEY);
116 return bibeditorEditURL;
117 }
118
119 public String getBibeditorViewURL() {
120 String bibeditorViewURL = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(
121 OLEConstants.DOCSTORE_APP_URL_KEY);
122 return bibeditorViewURL;
123 }
124 public String getDublinEditorEditURL() {
125 return SpringContext.getBean(OlePurchaseOrderDocumentHelperService.class).getDublinEditorEditURL();
126
127 }
128 public String getDublinEditorViewURL() {
129 return SpringContext.getBean(OlePurchaseOrderDocumentHelperService.class).getDublinEditorViewURL();
130 }
131 public static FileProcessingService getFileProcessingService() {
132 if (fileProcessingService == null) {
133 fileProcessingService = SpringContext.getBean(FileProcessingService.class);
134 }
135 return fileProcessingService;
136 }
137
138 public static void setFileProcessingService(FileProcessingService fileProcessingService) {
139 OleVendorCreditMemoDocument.fileProcessingService = fileProcessingService;
140 }
141
142 public static BibInfoService getBibInfoService() {
143 if (bibInfoService == null) {
144 bibInfoService = SpringContext.getBean(BibInfoServiceImpl.class);
145 }
146 return bibInfoService;
147 }
148
149 public String getMarcXMLFileDirLocation() throws Exception {
150 String externaleDirectory = getFileProcessingService().getMarcXMLFileDirLocation();
151 return externaleDirectory;
152 }
153
154 public static BibInfoWrapperService getBibInfoWrapperService() {
155 if (bibInfoWrapperService == null) {
156 bibInfoWrapperService = SpringContext.getBean(BibInfoWrapperService.class);
157 }
158 return bibInfoWrapperService;
159 }
160
161 public static void setBibInfoWrapperService(BibInfoWrapperService bibInfoWrapperService) {
162 OleVendorCreditMemoDocument.bibInfoWrapperService = bibInfoWrapperService;
163 }
164
165 public boolean isProrateQty() {
166 return prorateQty;
167 }
168
169 public void setProrateQty(boolean prorateQty) {
170 this.prorateQty = prorateQty;
171 }
172
173 public boolean isProrateDollar() {
174 return prorateDollar;
175 }
176
177 public void setProrateDollar(boolean prorateDollar) {
178 this.prorateDollar = prorateDollar;
179 }
180
181 public boolean isProrateManual() {
182 return prorateManual;
183 }
184
185 public void setProrateManual(boolean prorateManual) {
186 this.prorateManual = prorateManual;
187 }
188
189 public boolean isNoProrate() {
190 return noProrate;
191 }
192
193 public void setNoProrate(boolean noProrate) {
194 this.noProrate = noProrate;
195 }
196
197 public String getProrateBy() {
198 return prorateBy;
199 }
200
201 public void setProrateBy(String prorateBy) {
202 this.prorateBy = prorateBy;
203 }
204
205
206
207
208
209
210 public boolean getIsSaved() {
211 if (this.getDocumentHeader().getWorkflowDocument().isSaved()
212 || this.getDocumentHeader().getWorkflowDocument().isInitiated()) {
213 return true;
214 }
215 return false;
216 }
217
218 @Override
219 public void prepareForSave(KualiDocumentEvent event) {
220
221 if (event instanceof AttributedContinuePurapEvent) {
222 SpringContext.getBean(OleCreditMemoService.class).populateDocumentAfterInit(this);
223 }
224 customPrepareForSave(event);
225 }
226
227 @Override
228 public void processAfterRetrieve() {
229 super.processAfterRetrieve();
230
231 try {
232 if (this.getVendorAliasName() == null) {
233 populateVendorAliasName();
234 }
235 Map purchaseOrderTypeIdMap = new HashMap();
236 if (this.getPurchaseOrderTypeId() != null) {
237 purchaseOrderTypeIdMap.put("purchaseOrderTypeId", this.getPurchaseOrderTypeId());
238 List<PurchaseOrderType> purchaseOrderTypeDocumentList = (List) getBusinessObjectService().findMatching(PurchaseOrderType.class, purchaseOrderTypeIdMap);
239 if (purchaseOrderTypeDocumentList != null && purchaseOrderTypeDocumentList.size() > 0) {
240 PurchaseOrderType purchaseOrderTypeDoc = purchaseOrderTypeDocumentList.get(0);
241 this.setOrderType(purchaseOrderTypeDoc);
242 }
243 }
244 List<BigDecimal> newUnitPriceList = new ArrayList<BigDecimal>();
245 BigDecimal newUnitPrice = new BigDecimal(0);
246 BigDecimal hundred = new BigDecimal(100);
247 List<OleCreditMemoItem> item = this.getItems();
248
249 for (int i = 0; item.size() > i; i++) {
250 OleCreditMemoItem items = (OleCreditMemoItem) this.getItem(i);
251 if ((items.getItemType().isQuantityBasedGeneralLedgerIndicator())) {
252 newUnitPrice = items.getItemUnitPrice().abs();
253
254 if (items.getItemUnitPrice() != null && items.getExtendedPrice() != null) {
255 items.setItemSurcharge(items.getExtendedPrice().bigDecimalValue()
256 .subtract(newUnitPrice.multiply(items.getItemQuantity().bigDecimalValue()))
257 .setScale(4, RoundingMode.HALF_UP));
258 } else {
259 items.setItemSurcharge(BigDecimal.ZERO);
260 }
261 }
262 }
263 if (this.getVendorDetail().getCurrencyType()!=null){
264 if(this.getVendorDetail().getCurrencyType().getCurrencyType().equalsIgnoreCase(OleSelectConstant.CURRENCY_TYPE_NAME)){
265 currencyTypeIndicator=true;
266 }
267 else{
268 currencyTypeIndicator=false;
269 }
270 }
271
272 if (this.getVendorDetail() != null && (!currencyTypeIndicator)) {
273 Long currencyTypeId = this.getVendorDetail().getCurrencyType().getCurrencyTypeId();
274 Map documentNumberMap = new HashMap();
275 documentNumberMap.put(OleSelectConstant.CURRENCY_TYPE_ID, currencyTypeId);
276 List<OleExchangeRate> exchangeRateList = (List) getBusinessObjectService().findMatchingOrderBy(OleExchangeRate.class, documentNumberMap, OleSelectConstant.EXCHANGE_RATE_DATE, false);
277 Iterator iterator = exchangeRateList.iterator();
278 for (OleCreditMemoItem items : item) {
279 iterator = exchangeRateList.iterator();
280 if (iterator.hasNext()) {
281 OleExchangeRate tempOleExchangeRate = (OleExchangeRate) iterator.next();
282 items.setItemExchangeRate(new KualiDecimal(tempOleExchangeRate.getExchangeRate()));
283 }
284 }
285
286 }
287
288 List<OleCreditMemoItem> items = this.getItems();
289
290
291 String itemDescription = "";
292
293 for (OleCreditMemoItem singleItem : items) {
294 if (LOG.isDebugEnabled()) {
295 LOG.debug("Title id while retriving ------>" + singleItem.getItemTitleId());
296 }
297
298
299 if (singleItem.getItemTitleId() != null) {
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315 Bib bib = new BibMarc();
316 DocstoreClientLocator docstoreClientLocator=new DocstoreClientLocator();
317 if(singleItem.getItemTitleId()!=null && singleItem.getItemTitleId()!=""){
318 bib= docstoreClientLocator.getDocstoreClient().retrieveBib(singleItem.getItemTitleId());
319 singleItem.setBibUUID(bib.getId());
320 singleItem.setDocFormat(DocumentUniqueIDPrefix.getBibFormatType(singleItem.getItemTitleId()));
321 }
322
323 itemDescription = ((bib.getTitle() != null && !bib.getTitle().isEmpty()) ? bib.getTitle() + "," : "") +
324 ((bib.getAuthor() != null && !bib.getAuthor().isEmpty()) ? bib.getAuthor() + "," : "") +
325 ((bib.getPublisher() != null && !bib.getPublisher().isEmpty()) ? bib.getPublisher() + "," : "") +
326 ((bib.getIsbn() != null && !bib.getIsbn().isEmpty()) ? bib.getIsbn() + "," : "");
327 if (!itemDescription.isEmpty()) {
328 itemDescription = itemDescription.substring(0, itemDescription.lastIndexOf(","));
329 }
330 StringEscapeUtils stringEscapeUtils = new StringEscapeUtils();
331 itemDescription = stringEscapeUtils.unescapeXml(itemDescription);
332 singleItem.setItemDescription(itemDescription);
333 }
334 }
335 if (this.getProrateBy() != null) {
336 this.setProrateQty(this.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY));
337 this.setProrateManual(this.getProrateBy().equals(OLEConstants.MANUAL_PRORATE));
338 this.setProrateDollar(this.getProrateBy().equals(OLEConstants.PRORATE_BY_DOLLAR));
339 this.setNoProrate(this.getProrateBy().equals(OLEConstants.NO_PRORATE));
340 }
341
342 } catch (Exception e) {
343 LOG.error("Exception in OleVendorCreditMemoDocument:processAfterRetrieve " + e);
344 throw new RuntimeException(e);
345 }
346 }
347
348 public void creditMemoCalculation(OleCreditMemoItem item) {
349 item.setItemUnitPrice(item.getPoUnitPrice());
350 BigDecimal calcExtendedPrice = item.getItemUnitPrice().multiply(item.getItemQuantity().bigDecimalValue());
351 KualiDecimal extendedPrice = new KualiDecimal(calcExtendedPrice.setScale(KualiDecimal.SCALE, KualiDecimal.ROUND_BEHAVIOR));
352 item.setPoTotalAmount(extendedPrice);
353 }
354
355 private void setItemDescription(OleCreditMemoItem singleItem) throws Exception {
356 if (singleItem.getOleOrderRecord() != null) {
357 Map<String, ?> bibAssociatedFieldValuesMap = singleItem.getOleOrderRecord().getOleBibRecord().getBibAssociatedFieldsValueMap();
358 List titleList = (List) bibAssociatedFieldValuesMap.get("Title_search");
359 String title = titleList != null && !titleList.isEmpty() ? (String) (titleList).get(0) : null;
360 List authorList = (List) bibAssociatedFieldValuesMap.get("Author_search");
361 String author = authorList != null && !authorList.isEmpty() ? (String) (authorList).get(0) : null;
362 List publisherList = (List) bibAssociatedFieldValuesMap.get("Publisher_search");
363 String publisher = publisherList != null && !publisherList.isEmpty() ? (String) (publisherList).get(0) : null;
364 List isbnList = (List) bibAssociatedFieldValuesMap.get("020a");
365 String isbn = isbnList != null && !isbnList.isEmpty() ? (String) (isbnList).get(0) : null;
366
367 singleItem.setBibUUID(singleItem.getOleOrderRecord().getOleBibRecord().getBibUUID());
368 singleItem.setItemDescription(title + "," + author + "," + publisher + "," + isbn);
369 }
370 }
371
372 @Override
373 public OlePaymentRequestDocument getPaymentRequestDocument() {
374 if ((ObjectUtils.isNull(paymentRequestDocument)) && (ObjectUtils.isNotNull(getPaymentRequestIdentifier()))) {
375 setPaymentRequestDocument(SpringContext.getBean(PaymentRequestService.class).getPaymentRequestById(getPaymentRequestIdentifier()));
376 }
377 return (OlePaymentRequestDocument) this.paymentRequestDocument;
378 }
379
380
381
382
383
384
385
386
387 @Override
388 public boolean answerSplitNodeQuestion(String nodeName) throws UnsupportedOperationException {
389 if (nodeName.equals(OLEConstants.OlePaymentRequest.HAS_INVOICE_TYPE)) {
390 return hasInvoiceType();
391 }
392 if (nodeName.equals(OLEConstants.OlePaymentRequest.HAS_PREPAID_INVOICE_TYPE)) {
393 return hasPrepaidInvoiceType();
394 }
395 if (nodeName.equals(PurapWorkflowConstants.REQUIRES_IMAGE_ATTACHMENT)) {
396 return requiresAccountsPayableReviewRouting();
397 }
398 throw new UnsupportedOperationException("Cannot answer split question for this node you call \"" + nodeName + "\"");
399 }
400
401
402 private boolean hasInvoiceType() {
403 if (this.getInvoiceTypeId() != null) {
404 return true;
405 }
406 return false;
407 }
408
409 private boolean hasPrepaidInvoiceType() {
410 if (this.getInvoiceTypeId() != null) {
411 Map<String, String> invoiceMap = new HashMap<String, String>();
412 invoiceMap.put("invoiceTypeId", this.getInvoiceTypeId().toString());
413 OleInvoiceType invoiceType = this.getBusinessObjectService().findByPrimaryKey(OleInvoiceType.class, invoiceMap);
414 if (invoiceType != null &&
415 invoiceType.getInvoiceType().equals("Prepay") ||
416 invoiceType.getInvoiceType().equals("Deposit")) {
417 return true;
418 }
419 }
420 return false;
421 }
422
423 @Override
424 public KualiDecimal getGrandTotal() {
425 KualiDecimal grandTotal = KualiDecimal.ZERO;
426
427 if ((this.prorateBy != null) && (this.prorateBy.equals(OLEConstants.PRORATE_BY_QTY) || this.prorateBy.equals(OLEConstants.PRORATE_BY_DOLLAR) || this.prorateBy.equals(OLEConstants.MANUAL_PRORATE))) {
428
429
430 for (CreditMemoItem item : (List<CreditMemoItem>) getItems()) {
431 item.refreshReferenceObject(PurapPropertyConstants.ITEM_TYPE);
432
433 if (item.getTotalAmount() != null) {
434
435 if (!StringUtils.equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_RESTCK_FEE_CODE, item.getItemTypeCode()) && !StringUtils.equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_MISC_CRDT_CODE, item.getItemTypeCode())) {
436 if (item.getExtendedPrice() != null) {
437 grandTotal = grandTotal.add(item.getExtendedPrice());
438 }
439 }
440 }
441 }
442 } else {
443
444 for (CreditMemoItem item : (List<CreditMemoItem>) getItems()) {
445 if (!StringUtils.equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_RESTCK_FEE_CODE, item.getItemTypeCode()) && !StringUtils.equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_MISC_CRDT_CODE, item.getItemTypeCode())) {
446 if (item.getExtendedPrice() != null) {
447 grandTotal = grandTotal.add(item.getExtendedPrice());
448 }
449 }
450 }
451 }
452
453 return grandTotal;
454 }
455
456 @Override
457 public KualiDecimal getTotalDollarAmountAllItems(String[] excludedTypes) {
458 if ((this.prorateBy != null) && (this.prorateBy.equals(OLEConstants.PRORATE_BY_QTY) || this.prorateBy.equals(OLEConstants.PRORATE_BY_DOLLAR) || this.prorateBy.equals(OLEConstants.MANUAL_PRORATE))) {
459 return getTotalDollarAmountWithExclusions(excludedTypes, false);
460 } else {
461 return getTotalDollarAmountWithExclusions(excludedTypes, true);
462 }
463 }
464
465 @Override
466 public KualiDecimal getTotalPreTaxDollarAmountAllItems(String[] excludedTypes) {
467 if ((this.prorateBy != null) && (this.prorateBy.equals(OLEConstants.PRORATE_BY_QTY) || this.prorateBy.equals(OLEConstants.PRORATE_BY_DOLLAR) || this.prorateBy.equals(OLEConstants.MANUAL_PRORATE))) {
468 return getTotalPreTaxDollarAmountWithExclusions(excludedTypes, false);
469 } else {
470 return getTotalPreTaxDollarAmountWithExclusions(excludedTypes, true);
471 }
472 }
473
474 @Override
475 public KualiDecimal getTotalPreTaxDollarAmountAboveLineItems() {
476 if ((this.prorateBy != null) && (this.prorateBy.equals(OLEConstants.PRORATE_BY_QTY) || this.prorateBy.equals(OLEConstants.PRORATE_BY_DOLLAR) || this.prorateBy.equals(OLEConstants.MANUAL_PRORATE))) {
477 KualiDecimal addChargeItem = KualiDecimal.ZERO;
478 KualiDecimal lineItemPreTaxTotal = KualiDecimal.ZERO;
479 KualiDecimal prorateSurcharge = KualiDecimal.ZERO;
480 List<OleCreditMemoItem> item = this.getItems();
481 for (OleCreditMemoItem items : item) {
482 if (items.getItemType().isQuantityBasedGeneralLedgerIndicator() && items.getExtendedPrice() != null && items.getExtendedPrice().compareTo(KualiDecimal.ZERO) != 0) {
483 if (items.getItemSurcharge() != null) {
484 prorateSurcharge = new KualiDecimal(items.getItemSurcharge());
485 }
486 addChargeItem = addChargeItem.add(items.getExtendedPrice().subtract(prorateSurcharge.multiply(items.getItemQuantity())));
487 }
488 }
489 lineItemPreTaxTotal = addChargeItem;
490 return lineItemPreTaxTotal;
491 } else {
492 return super.getTotalPreTaxDollarAmountAboveLineItems();
493 }
494 }
495
496 public Integer getInvoiceIdentifier() {
497 return invoiceIdentifier;
498 }
499
500 public void setInvoiceIdentifier(Integer invoiceIdentifier) {
501 this.invoiceIdentifier = invoiceIdentifier;
502 }
503
504 private void populateVendorAliasName() {
505 Map vendorDetailMap = new HashMap();
506 vendorDetailMap.put(OLEConstants.VENDOR_HEADER_IDENTIFIER, this.getVendorHeaderGeneratedIdentifier());
507 vendorDetailMap.put(OLEConstants.VENDOR_DETAIL_IDENTIFIER, this.getVendorDetailAssignedIdentifier());
508 List<VendorAlias> vendorDetailList = (List) getBusinessObjectService().findMatching(VendorAlias.class, vendorDetailMap);
509 if (vendorDetailList != null && vendorDetailList.size() > 0) {
510 this.setVendorAliasName(vendorDetailList.get(0).getVendorAliasName());
511 }
512 }
513 }