1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.select.service;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.ole.module.purap.PurapConstants;
20 import org.kuali.ole.module.purap.PurapParameterConstants;
21 import org.kuali.ole.module.purap.PurapPropertyConstants;
22 import org.kuali.ole.module.purap.businessobject.PurApItemUseTax;
23 import org.kuali.ole.module.purap.businessobject.PurchasingCapitalAssetItem;
24 import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
25 import org.kuali.ole.module.purap.document.service.*;
26 import org.kuali.ole.module.purap.util.ExpiredOrClosedAccount;
27 import org.kuali.ole.module.purap.util.ExpiredOrClosedAccountEntry;
28 import org.kuali.ole.select.OleSelectConstant;
29 import org.kuali.ole.select.businessobject.*;
30 import org.kuali.ole.select.document.OleInvoiceDocument;
31 import org.kuali.ole.select.document.OleLineItemReceivingDocument;
32 import org.kuali.ole.select.document.OlePurchaseOrderDocument;
33 import org.kuali.ole.select.document.service.OleCopyHelperService;
34 import org.kuali.ole.select.document.service.OleInvoiceService;
35 import org.kuali.ole.select.document.service.OleLineItemReceivingService;
36 import org.kuali.ole.select.document.service.impl.OleLineItemReceivingServiceImpl;
37 import org.kuali.ole.sys.OLEConstants;
38 import org.kuali.ole.sys.OLEKeyConstants;
39 import org.kuali.ole.sys.businessobject.Bank;
40 import org.kuali.ole.sys.context.SpringContext;
41 import org.kuali.ole.sys.document.validation.event.AttributedBlanketApproveDocumentEvent;
42 import org.kuali.ole.sys.service.BankService;
43 import org.kuali.ole.vnd.VendorConstants;
44 import org.kuali.ole.vnd.businessobject.VendorAddress;
45 import org.kuali.ole.vnd.document.service.VendorService;
46 import org.kuali.rice.core.api.datetime.DateTimeService;
47 import org.kuali.rice.core.api.util.type.AbstractKualiDecimal;
48 import org.kuali.rice.core.api.util.type.KualiDecimal;
49 import org.kuali.rice.core.api.util.type.KualiInteger;
50 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
51 import org.kuali.rice.kew.api.exception.WorkflowException;
52 import org.kuali.rice.kim.api.KimConstants;
53 import org.kuali.rice.kim.api.identity.Person;
54 import org.kuali.rice.kim.api.identity.PersonService;
55 import org.kuali.rice.kns.service.DocumentHelperService;
56 import org.kuali.rice.krad.exception.DocumentAuthorizationException;
57 import org.kuali.rice.krad.service.BusinessObjectService;
58 import org.kuali.rice.krad.service.DocumentService;
59 import org.kuali.rice.krad.service.KRADServiceLocator;
60 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
61 import org.kuali.rice.krad.util.GlobalVariables;
62 import org.kuali.rice.krad.util.KRADConstants;
63 import org.kuali.rice.krad.util.ObjectUtils;
64
65 import java.math.BigDecimal;
66 import java.math.RoundingMode;
67 import java.sql.Date;
68 import java.util.ArrayList;
69 import java.util.HashMap;
70 import java.util.List;
71 import java.util.Map;
72 import java.util.regex.Matcher;
73 import java.util.regex.Pattern;
74
75 public class OLEAddTitlesToInvoiceService {
76 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OLEAddTitlesToInvoiceService.class);
77 DocumentService documentService;
78 List<String> receivingDocumentsList = new ArrayList<String>();
79 boolean isAllowedPoForReceiving = false;
80 private static transient OlePurapService olePurapService;
81 private static transient OleInvoiceService invoiceService;
82
83 public static OlePurapService getOlePurapService() {
84 if (olePurapService == null) {
85 olePurapService = SpringContext.getBean(OlePurapService.class);
86 }
87 return olePurapService;
88 }
89
90 public List<String> getReceivingDocumentsList() {
91 return receivingDocumentsList;
92 }
93
94 OleInvoiceDocument oleInvoiceDocument;
95
96 public OleInvoiceDocument getOleInvoiceDocument() {
97 return oleInvoiceDocument;
98 }
99
100 public void setOleInvoiceDocument(OleInvoiceDocument oleInvoiceDocument) {
101 this.oleInvoiceDocument = oleInvoiceDocument;
102 }
103
104 public boolean isAllowedPoForReceiving() {
105 return isAllowedPoForReceiving;
106 }
107
108 public void setAllowedPoForReceiving(boolean allowedPoForReceiving) {
109 isAllowedPoForReceiving = allowedPoForReceiving;
110 }
111
112 public static OleInvoiceService getInvoiceService() {
113 if (invoiceService == null) {
114 invoiceService = SpringContext.getBean(OleInvoiceService.class);
115 }
116 return invoiceService;
117 }
118
119 public List<OlePurchaseOrderItem> populateOlePurchaseOrderItem() {
120 List<OlePurchaseOrderItem> result = new ArrayList<OlePurchaseOrderItem>();
121 List<OlePurchaseOrderDocument> olePurchaseOrderDocuments = (List<OlePurchaseOrderDocument>) KRADServiceLocator.getBusinessObjectService().findAll(OlePurchaseOrderDocument.class);
122 for (OlePurchaseOrderDocument olePurchaseOrderDocument : olePurchaseOrderDocuments) {
123 String olePurchaseOrderDocumentNumber = olePurchaseOrderDocument.getDocumentNumber();
124 List<OlePurchaseOrderItem> olePurchaseOrderItems = olePurchaseOrderDocument.getItems();
125 for (OlePurchaseOrderItem olePurchaseOrderItem : olePurchaseOrderItems) {
126 if (olePurchaseOrderItem.getItemTypeCode().toString().equals("ITEM") && olePurchaseOrderItem.getDocumentNumber().equals(olePurchaseOrderDocumentNumber)) {
127 Map<String, String> map = new HashMap<String, String>();
128 map.put("poItemIdentifier", olePurchaseOrderItem.getItemIdentifier().toString());
129
130
131
132 if (validateRecords(olePurchaseOrderDocument.getPurapDocumentIdentifier(), olePurchaseOrderDocument.getDocumentNumber())) {
133 olePurchaseOrderItem.setOlePurchaseOrderDocument(olePurchaseOrderDocument);
134 result.add(olePurchaseOrderItem);
135 }
136
137 }
138 }
139 }
140 return result;
141 }
142
143 public List<OlePurchaseOrderItem> populateOlePurchaseOrderItemByPoItemList(List<String> purchaseOrderItemIds, List<String> invoicePrice) {
144 List<OlePurchaseOrderItem> result = new ArrayList<OlePurchaseOrderItem>();
145 List<OlePurchaseOrderDocument> olePurchaseOrderDocuments = (List<OlePurchaseOrderDocument>) KRADServiceLocator.getBusinessObjectService().findAll(OlePurchaseOrderDocument.class);
146 int index = 0;
147 for (String poitmId : purchaseOrderItemIds) {
148 for (OlePurchaseOrderDocument olePurchaseOrderDocument : olePurchaseOrderDocuments) {
149 List<OlePurchaseOrderItem> olePurchaseOrderItems = olePurchaseOrderDocument.getItems();
150 for (OlePurchaseOrderItem olePurchaseOrderItem : olePurchaseOrderItems) {
151 if (olePurchaseOrderItem.getItemIdentifier().toString().equalsIgnoreCase(poitmId.toString())) {
152 if (validateRecords(olePurchaseOrderDocument.getPurapDocumentIdentifier(), olePurchaseOrderDocument.getDocumentNumber())) {
153 olePurchaseOrderItem.setSelected(true);
154 olePurchaseOrderItem.setOlePurchaseOrderDocument(olePurchaseOrderDocument);
155 if (invoicePrice.get(index) != null && (!invoicePrice.get(index).trim().equalsIgnoreCase(""))) {
156 olePurchaseOrderItem.setInvoiceItemListPrice(invoicePrice.get(index));
157 } else {
158 olePurchaseOrderItem.setInvoiceItemListPrice(olePurchaseOrderItem.getExtendedPrice().toString());
159 }
160 result.add(olePurchaseOrderItem);
161
162 }
163 }
164 }
165
166 }
167 index++;
168 }
169
170
171 return result;
172 }
173
174 public boolean validateSelectOlePurchaseOrderItem(List<OlePurchaseOrderItem> olePurchaseOrderItems) {
175 boolean isSuccess = false;
176
177 for (OlePurchaseOrderItem olePurchaseOrderItem : olePurchaseOrderItems) {
178 if (olePurchaseOrderItem.isSelected()) {
179 isSuccess = true;
180 }
181 }
182
183 return isSuccess;
184 }
185
186 public boolean validateOlePurchaseOrderItemByPoId(List<OlePurchaseOrderItem> olePurchaseOrderItems) {
187 List<OlePurchaseOrderItem> result = new ArrayList<OlePurchaseOrderItem>();
188 boolean isSuccess = true;
189 String poId = "";
190 for (OlePurchaseOrderItem olePurchaseOrderItem : olePurchaseOrderItems) {
191 if (poId.equalsIgnoreCase("")) {
192 poId = olePurchaseOrderItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier().toString();
193 } else {
194 if (!poId.equalsIgnoreCase(olePurchaseOrderItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier().toString())) {
195 isSuccess = false;
196 }
197 }
198
199 }
200 return isSuccess;
201 }
202
203 public boolean validateSelectedPurchaseOrderByVendor(List<OlePurchaseOrderItem> olePurchaseOrderItems, String vendorId) {
204 boolean isSuccess = true;
205 for (OlePurchaseOrderItem olePurchaseOrderItem : olePurchaseOrderItems) {
206 if (!olePurchaseOrderItem.getOlePurchaseOrderDocument().getVendorHeaderGeneratedIdentifier().toString().equalsIgnoreCase(vendorId.toString())) {
207 isSuccess = false;
208 }
209 }
210 return isSuccess;
211 }
212
213 public boolean validateInvoiceDocumentVendor(String invoiceDocumentNumber, List<OlePurchaseOrderItem> purchaseOrderItems) {
214 boolean isValid = true;
215 Map<String, String> map = new HashMap<String, String>();
216 map.put("documentNumber", invoiceDocumentNumber);
217 List<OleInvoiceDocument> oleInvoiceDocuments = (List<OleInvoiceDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OleInvoiceDocument.class, map);
218 OleInvoiceDocument oleInvoiceDocument = oleInvoiceDocuments.get(0);
219 for (OlePurchaseOrderItem olePurchaseOrderItem : purchaseOrderItems) {
220 if (olePurchaseOrderItem.isSelected()) {
221 if (oleInvoiceDocument.getVendorAddressGeneratedIdentifier() != null) {
222 if (!(olePurchaseOrderItem.getOlePurchaseOrderDocument().getVendorHeaderGeneratedIdentifier().toString().equalsIgnoreCase(oleInvoiceDocument.getVendorHeaderGeneratedIdentifier().toString()))) {
223 if (oleInvoiceDocument.getVendorAddressGeneratedIdentifier().toString().equalsIgnoreCase(olePurchaseOrderItem.getOlePurchaseOrderDocument().getVendorAddressGeneratedIdentifier().toString())) {
224 isValid = false;
225 }
226 }
227 } else {
228
229 if (!(olePurchaseOrderItem.getOlePurchaseOrderDocument().getVendorHeaderGeneratedIdentifier().toString().equalsIgnoreCase(oleInvoiceDocument.getVendorHeaderGeneratedIdentifier().toString()))) {
230 isValid = false;
231 }
232
233 }
234 }
235 }
236 return isValid;
237 }
238
239 public boolean validateSelectedPurchaseOrderByVendor(List<OlePurchaseOrderItem> purchaseOrderItems) {
240 boolean isSuccess = true;
241 List<String> list = new ArrayList<String>();
242 for (OlePurchaseOrderItem olePurchaseOrderItem : purchaseOrderItems) {
243 if (olePurchaseOrderItem.isSelected()) {
244 if (!list.contains(olePurchaseOrderItem.getOlePurchaseOrderDocument().getVendorHeaderGeneratedIdentifier().toString() + "-" + olePurchaseOrderItem.getOlePurchaseOrderDocument().getVendorDetailAssignedIdentifier())) {
245 list.add(olePurchaseOrderItem.getOlePurchaseOrderDocument().getVendorHeaderGeneratedIdentifier().toString() + "-" + olePurchaseOrderItem.getOlePurchaseOrderDocument().getVendorDetailAssignedIdentifier());
246 }
247 }
248 }
249 if (list.size() > 1) {
250 isSuccess = false;
251 }
252 return isSuccess;
253 }
254
255 public boolean isSelectedAtleatOneItem(List<OlePurchaseOrderItem> purchaseOrderItems) {
256 boolean isValid = false;
257 List<OlePurchaseOrderItem> olePurchaseOrderItemSelected = new ArrayList<OlePurchaseOrderItem>();
258 for (OlePurchaseOrderItem olePurchaseOrderItem : purchaseOrderItems) {
259 if (olePurchaseOrderItem.isSelected()) {
260 isValid = true;
261 }
262 }
263 return isValid;
264 }
265
266 public List<OlePurchaseOrderItem> getSelectedItems(List<OlePurchaseOrderItem> purchaseOrderItems) {
267 List<OlePurchaseOrderItem> olePurchaseOrderItemSelected = new ArrayList<OlePurchaseOrderItem>();
268 for (OlePurchaseOrderItem olePurchaseOrderItem : purchaseOrderItems) {
269 if (olePurchaseOrderItem.isSelected()) {
270 olePurchaseOrderItemSelected.add(olePurchaseOrderItem);
271 }
272 }
273 return olePurchaseOrderItemSelected;
274 }
275
276 public boolean createNewInvoiceDocument(List<OlePurchaseOrderDocument> olePurchaseOrderDocuments, List<OlePurchaseOrderItem> olePurchaseOrderItems, String paymentMethodId, Date invoiceDate, String invoiceNumber, String vendorInvoiceAmt, String principalId) throws Exception {
277 boolean isSuccess = false;
278 try {
279
280 OleInvoiceDocument oleInvoiceDocument = (OleInvoiceDocument) SpringContext.getBean(DocumentService.class).getNewDocument("OLE_PRQS");
281 java.util.Date date1 = new java.util.Date();
282 oleInvoiceDocument.setInvoiceDate(new Date(SpringContext.getBean(DateTimeService.class).getCurrentDate().getTime()));
283 oleInvoiceDocument.setInvoiceNumber(invoiceNumber);
284 oleInvoiceDocument.setPaymentMethodId(Integer.parseInt(paymentMethodId));
285 oleInvoiceDocument.setVendorInvoiceAmount(new KualiDecimal(vendorInvoiceAmt));
286 oleInvoiceDocument.setInvoiceAmount(vendorInvoiceAmt);
287 oleInvoiceDocument.initiateDocument();
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303 Map<String, ExpiredOrClosedAccount> map1 = new HashMap<String, ExpiredOrClosedAccount>();
304 for (OlePurchaseOrderDocument olePurchaseOrderDocument : olePurchaseOrderDocuments) {
305 if (olePurchaseOrderDocument != null) {
306 this.populateInvoiceFromPurchaseOrder(olePurchaseOrderDocument, oleInvoiceDocument, null, true);
307 oleInvoiceDocument.getPurchaseOrderDocuments().add(olePurchaseOrderDocument);
308 }
309 }
310 String description = getOlePurapService().getParameter(OLEConstants.INV_DESC);
311 description = getOlePurapService().setDocumentDescription(description,null);
312 oleInvoiceDocument.getDocumentHeader().setDocumentDescription(description);
313 updateForeignCurrencyDetails(oleInvoiceDocument);
314 List<OleInvoiceItem> oleInvItems = oleInvoiceDocument.getItems();
315 for (OleInvoiceItem oleInvoiceItem : oleInvItems) {
316 getInvoiceService().calculateAccount(oleInvoiceItem);
317 }
318
319 boolean isRulePassed = (KRADServiceLocatorWeb.getKualiRuleService()).applyRules(new AttributedBlanketApproveDocumentEvent("", oleInvoiceDocument));
320 if (isRulePassed) {
321 try {
322 getOleInvoiceServiceImpl().saveInvoiceDocument(oleInvoiceDocument);
323 } catch (Exception e) {
324 e.printStackTrace();
325 }
326 } else {
327 return false;
328 }
329 isSuccess = true;
330 if (isSuccess) {
331 this.setOleInvoiceDocument(oleInvoiceDocument);
332 }
333
334 } catch (WorkflowException e) {
335 LOG.error("Exception Occurred during creating Invoice Document" + e);
336 e.printStackTrace();
337 }
338
339 return isSuccess;
340 }
341
342 public List<OlePurchaseOrderDocument> populatePurchaseOrderDocuments(List<OlePurchaseOrderItem> purchaseOrderItems) {
343 List<OlePurchaseOrderDocument> result = new ArrayList<OlePurchaseOrderDocument>();
344 List<String> poId = new ArrayList<String>();
345 for (OlePurchaseOrderItem olePurchaseOrderItem : purchaseOrderItems) {
346 if (!poId.contains(olePurchaseOrderItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier().toString())) {
347 if (olePurchaseOrderItem.isSelected()) {
348 poId.add(olePurchaseOrderItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier().toString());
349 }
350 }
351
352 }
353 for (String id : poId) {
354 Map<String, String> map = new HashMap<String, String>();
355 map.put("purapDocumentIdentifier", id);
356 List<OlePurchaseOrderDocument> olePurchaseOrderDocuments = (List<OlePurchaseOrderDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePurchaseOrderDocument.class, map);
357 if (olePurchaseOrderDocuments.size() > 0) {
358 for (OlePurchaseOrderDocument olePurchaseOrderDocument : olePurchaseOrderDocuments) {
359 if (olePurchaseOrderDocument.getPurchaseOrderCurrentIndicatorForSearching()) {
360 result.add(olePurchaseOrderDocument);
361 }
362 }
363 }
364
365 }
366
367 for (OlePurchaseOrderItem purchaseOrderItem : purchaseOrderItems) {
368 if (purchaseOrderItem.isSelected()) {
369 purchaseOrderItem.setItemForInvoice(false);
370 for (OlePurchaseOrderDocument olePurchaseOrderDocument : result) {
371 List<OlePurchaseOrderItem> olePurchaseOrderItems = olePurchaseOrderDocument.getItems();
372 for (OlePurchaseOrderItem olePurchaseOrderItem : olePurchaseOrderItems) {
373 if (purchaseOrderItem.getItemIdentifier().toString().equalsIgnoreCase(olePurchaseOrderItem.getItemIdentifier().toString()) && olePurchaseOrderItem.getItemTypeCode().equals("ITEM")) {
374 olePurchaseOrderItem.setSelected(true);
375 olePurchaseOrderItem.setItemForInvoice(true);
376 olePurchaseOrderItem.setInvoiceItemListPrice(purchaseOrderItem.getInvoiceItemListPrice());
377
378 }
379 }
380 }
381 }
382 }
383 return result;
384 }
385
386
387 public boolean addOlePurchaseOrderItemsToInvoiceDocument(List<OlePurchaseOrderDocument> olePurchaseOrderDocuments, List<OlePurchaseOrderItem> olePurchaseOrderItems, String documentNumber, String principalId, String invoiceAmt) throws Exception {
388 boolean isSuccess = false;
389
390 Map<String, String> map = new HashMap<String, String>();
391 map.put("documentNumber", documentNumber);
392 List<OleInvoiceDocument> oleInvoiceDocuments = (List<OleInvoiceDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OleInvoiceDocument.class, map);
393 OleInvoiceDocument oleInvoiceDocument = oleInvoiceDocuments.get(0);
394
395
396
397
398
399
400
401
402
403
404
405
406
407 if (invoiceAmt != null) {
408
409 oleInvoiceDocument.setVendorInvoiceAmount(new KualiDecimal(invoiceAmt));
410 }
411 for (OlePurchaseOrderDocument olePurchaseOrderDocument : olePurchaseOrderDocuments) {
412 if (olePurchaseOrderDocument != null) {
413 this.populateInvoiceFromPurchaseOrder(olePurchaseOrderDocument, oleInvoiceDocument, null, false);
414 oleInvoiceDocument.getPurchaseOrderDocuments().add(olePurchaseOrderDocument);
415 }
416 }
417 String description = getOlePurapService().getParameter(OLEConstants.INV_DESC);
418 description = getOlePurapService().setDocumentDescription(description,null);
419 oleInvoiceDocument.getDocumentHeader().setDocumentDescription(description);
420 boolean isRulePassed = (KRADServiceLocatorWeb.getKualiRuleService()).applyRules(new AttributedBlanketApproveDocumentEvent("", oleInvoiceDocument));
421 if (isRulePassed) {
422 try {
423 getOleInvoiceServiceImpl().saveInvoiceDocument(oleInvoiceDocument);
424 } catch (Exception e) {
425 LOG.error("Exception Occurred during updating Invoice Document" + e);
426 e.printStackTrace();
427 }
428 } else {
429 return false;
430 }
431 isSuccess = true;
432 if (isSuccess) {
433 this.setOleInvoiceDocument(oleInvoiceDocument);
434 }
435 return isSuccess;
436
437
438 }
439
440 public boolean validateReceivingForProcess(List<OlePurchaseOrderItem> purchaseOrderItems) {
441 boolean isValid = false;
442 int selectedItem = 0;
443 int receivingExistForItem = 0;
444 for (OlePurchaseOrderItem olePurchaseOrderItem : purchaseOrderItems) {
445 if (olePurchaseOrderItem.isSelected()) {
446 selectedItem++;
447 Map<String, String> map = new HashMap<String, String>();
448 map.put("purchaseOrderIdentifier", olePurchaseOrderItem
449 .getItemIdentifier().toString());
450 List<OleLineItemReceivingItem> oleLineItemReceivingItems = (List<OleLineItemReceivingItem>) KRADServiceLocator
451 .getBusinessObjectService().findMatching(
452 OleLineItemReceivingItem.class, map);
453 if (oleLineItemReceivingItems.size() > 0) {
454 OleLineItemReceivingItem oleLineItemReceivingItem = oleLineItemReceivingItems.get(0);
455
456
457
458 receivingExistForItem++;
459 isValid = true;
460 olePurchaseOrderItem.setSelected(false);
461
462 }
463 }
464 }
465 if (selectedItem != receivingExistForItem) {
466 this.isAllowedPoForReceiving = true;
467 }
468 return isValid;
469 }
470
471 public int getReceiptStatusDetails(String receiptStatusCd) {
472 int receiptStatusId = 0;
473 Map<String, String> receiptStatusCdMap = new HashMap<String, String>();
474 receiptStatusCdMap.put(OLEConstants.RCPT_STATUS_CD, receiptStatusCd);
475 List<OleReceiptStatus> oleReceiptStatusList = (List) SpringContext.getBean(BusinessObjectService.class)
476 .findMatching(OleReceiptStatus.class, receiptStatusCdMap);
477 for (OleReceiptStatus oleReceiptStatus : oleReceiptStatusList) {
478 receiptStatusId = oleReceiptStatus.getReceiptStatusId().intValue();
479 }
480 return receiptStatusId;
481 }
482
483 public OleInvoiceService getOleInvoiceServiceImpl() {
484
485 OleInvoiceService oleInvoiceService = (OleInvoiceService) SpringContext.getBean(OleInvoiceService.class);
486 return oleInvoiceService;
487 }
488
489 public boolean validateForReceiving(List<OlePurchaseOrderItem> purchaseOrderItems) {
490 boolean isValid = true;
491 String poid = "";
492 for (OlePurchaseOrderItem olePurchaseOrderItem : purchaseOrderItems) {
493 if (olePurchaseOrderItem.isSelected()) {
494 if (!poid.equalsIgnoreCase("")) {
495 if (!olePurchaseOrderItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier().toString().equalsIgnoreCase(poid)) {
496 isValid = false;
497 break;
498 }
499 } else {
500 poid = olePurchaseOrderItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier().toString();
501 }
502
503 }
504 }
505 return isValid;
506 }
507
508
509 public void populateInvoiceFromPurchaseOrder(PurchaseOrderDocument po, OleInvoiceDocument oleInvoiceDocument, HashMap<String, ExpiredOrClosedAccountEntry> expiredOrClosedAccountList, boolean isNew) {
510 AccountsPayableDocumentSpecificService accountsPayableDocumentSpecificService = SpringContext.getBean(InvoiceService.class);
511 if (isNew) {
512 VendorService vendorService = SpringContext.getBean(VendorService.class);
513 PurapService purapService = SpringContext.getBean(PurapService.class);
514
515 oleInvoiceDocument.setPurchaseOrderIdentifier(po.getPurapDocumentIdentifier());
516 oleInvoiceDocument.getDocumentHeader().setOrganizationDocumentNumber(po.getDocumentHeader().getOrganizationDocumentNumber());
517 oleInvoiceDocument.setPostingYear(po.getPostingYear());
518 oleInvoiceDocument.setReceivingDocumentRequiredIndicator(po.isReceivingDocumentRequiredIndicator());
519 oleInvoiceDocument.setUseTaxIndicator(po.isUseTaxIndicator());
520 oleInvoiceDocument.setInvoicePositiveApprovalIndicator(po.isPaymentRequestPositiveApprovalIndicator());
521 oleInvoiceDocument.setVendorCustomerNumber(po.getVendorCustomerNumber());
522 oleInvoiceDocument.setReceivingDocumentRequiredIndicator(po.isReceivingDocumentRequiredIndicator());
523
524 if (po.getPurchaseOrderCostSource() != null) {
525 oleInvoiceDocument.setInvoiceCostSource(po.getPurchaseOrderCostSource());
526 oleInvoiceDocument.setInvoiceCostSourceCode(po.getPurchaseOrderCostSourceCode());
527 }
528
529 if (po.getVendorShippingPaymentTerms() != null) {
530 oleInvoiceDocument.setVendorShippingPaymentTerms(po.getVendorShippingPaymentTerms());
531 oleInvoiceDocument.setVendorShippingPaymentTermsCode(po.getVendorShippingPaymentTermsCode());
532 }
533
534 if (po.getVendorPaymentTerms() != null) {
535 oleInvoiceDocument.setVendorPaymentTermsCode(po.getVendorPaymentTermsCode());
536 oleInvoiceDocument.setVendorPaymentTerms(po.getVendorPaymentTerms());
537 }
538
539 if (po.getRecurringPaymentType() != null) {
540 oleInvoiceDocument.setRecurringPaymentType(po.getRecurringPaymentType());
541 oleInvoiceDocument.setRecurringPaymentTypeCode(po.getRecurringPaymentTypeCode());
542 }
543
544 Bank defaultBank = SpringContext.getBean(BankService.class).getDefaultBankByDocType(oleInvoiceDocument.getClass());
545 if (defaultBank != null) {
546 oleInvoiceDocument.setBankCode(defaultBank.getBankCode());
547 oleInvoiceDocument.setBank(defaultBank);
548 }
549 oleInvoiceDocument.setVendorHeaderGeneratedIdentifier(po.getVendorHeaderGeneratedIdentifier());
550 oleInvoiceDocument.setVendorDetailAssignedIdentifier(po.getVendorDetailAssignedIdentifier());
551 oleInvoiceDocument.setVendorCustomerNumber(po.getVendorCustomerNumber());
552 oleInvoiceDocument.setVendorName(po.getVendorName());
553
554
555 oleInvoiceDocument.setOriginalVendorHeaderGeneratedIdentifier(po.getVendorHeaderGeneratedIdentifier());
556 oleInvoiceDocument.setOriginalVendorDetailAssignedIdentifier(po.getVendorDetailAssignedIdentifier());
557
558
559 oleInvoiceDocument.setAlternateVendorHeaderGeneratedIdentifier(po.getAlternateVendorHeaderGeneratedIdentifier());
560 oleInvoiceDocument.setAlternateVendorDetailAssignedIdentifier(po.getAlternateVendorDetailAssignedIdentifier());
561
562
563 String userCampus = GlobalVariables.getUserSession().getPerson().getCampusCode();
564 VendorAddress vendorAddress = vendorService.getVendorDefaultAddress(po.getVendorHeaderGeneratedIdentifier(), po.getVendorDetailAssignedIdentifier(), VendorConstants.AddressTypes.REMIT, userCampus);
565 if (vendorAddress != null) {
566 oleInvoiceDocument.templateVendorAddress(vendorAddress);
567 oleInvoiceDocument.setVendorAddressGeneratedIdentifier(vendorAddress.getVendorAddressGeneratedIdentifier());
568 oleInvoiceDocument.setVendorAttentionName(StringUtils.defaultString(vendorAddress.getVendorAttentionName()));
569 } else {
570
571 oleInvoiceDocument.setVendorAddressGeneratedIdentifier(po.getVendorAddressGeneratedIdentifier());
572 oleInvoiceDocument.setVendorLine1Address(po.getVendorLine1Address());
573 oleInvoiceDocument.setVendorLine2Address(po.getVendorLine2Address());
574 oleInvoiceDocument.setVendorCityName(po.getVendorCityName());
575 oleInvoiceDocument.setVendorAddressInternationalProvinceName(po.getVendorAddressInternationalProvinceName());
576 oleInvoiceDocument.setVendorStateCode(po.getVendorStateCode());
577 oleInvoiceDocument.setVendorPostalCode(po.getVendorPostalCode());
578 oleInvoiceDocument.setVendorCountryCode(po.getVendorCountryCode());
579
580 boolean blankAttentionLine = StringUtils.equalsIgnoreCase(
581 "Y",
582 SpringContext.getBean(ParameterService.class).getParameterValueAsString(
583 PurapConstants.PURAP_NAMESPACE, "Document",
584 PurapParameterConstants.BLANK_ATTENTION_LINE_FOR_PO_TYPE_ADDRESS));
585
586 if (blankAttentionLine) {
587 oleInvoiceDocument.setVendorAttentionName(StringUtils.EMPTY);
588 } else {
589 oleInvoiceDocument.setVendorAttentionName(StringUtils.defaultString(po.getVendorAttentionName()));
590 }
591 }
592 }
593 oleInvoiceDocument.setInvoicePayDate(getOleInvoiceServiceImpl().calculatePayDate(oleInvoiceDocument.getInvoiceDate(), oleInvoiceDocument.getVendorPaymentTerms()));
594
595
596 for (OlePurchaseOrderItem poi : (List<OlePurchaseOrderItem>) po.getItems()) {
597
598 if (poi.isSelected()) {
599 poi.setNoOfCopiesInvoiced(poi.getOleItemQuantity());
600 OleInvoiceItem invoiceItem = new OleInvoiceItem(poi, oleInvoiceDocument);
601 invoiceItem.setPurchaseOrderIdentifier(po.getPurapDocumentIdentifier());
602 invoiceItem.setItemQuantity(poi.getItemQuantity());
603 invoiceItem.setOlePoOutstandingQuantity(new KualiInteger(poi.getOutstandingQuantity().bigDecimalValue()));
604
605 invoiceItem.setAccountsPayablePurchasingDocumentLinkIdentifier(po.getAccountsPayablePurchasingDocumentLinkIdentifier());
606 invoiceItem.setReceivingDocumentRequiredIndicator(po.isReceivingDocumentRequiredIndicator());
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625 oleInvoiceDocument.getItems().add(invoiceItem);
626 PurchasingCapitalAssetItem purchasingCAMSItem = po.getPurchasingCapitalAssetItemByItemIdentifier(poi.getItemIdentifier());
627 if (purchasingCAMSItem != null) {
628 invoiceItem.setCapitalAssetTransactionTypeCode(purchasingCAMSItem.getCapitalAssetTransactionTypeCode());
629 }
630
631
632
633 invoiceItem.getUseTaxItems().clear();
634 for (PurApItemUseTax useTax : poi.getUseTaxItems()) {
635 invoiceItem.getUseTaxItems().add(useTax);
636 }
637
638 }
639 }
640
641
642
643
644
645
646
647 getOleInvoiceServiceImpl().removeIneligibleAdditionalCharges(oleInvoiceDocument);
648
649 oleInvoiceDocument.fixItemReferences();
650 oleInvoiceDocument.refreshNonUpdateableReferences();
651 }
652
653 public boolean receivePO(OleLineItemReceivingDocument oleLineItemReceivingDocument, Integer purchaseOrderIdentifier, boolean isCreateRCV, List<OlePurchaseOrderItem> purchaseOrderItems) {
654 boolean receivePOSuccess = false;
655
656
657 oleLineItemReceivingDocument.setPurchaseOrderIdentifier(purchaseOrderIdentifier);
658 DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
659 oleLineItemReceivingDocument.setShipmentReceivedDate(dateTimeService.getCurrentSqlDate());
660
661 boolean isValid = true;
662
663 PurchaseOrderDocument po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(purchaseOrderIdentifier);
664
665 if (ObjectUtils.isNotNull(po)) {
666 oleLineItemReceivingDocument.setAccountsPayablePurchasingDocumentLinkIdentifier(po.getAccountsPayablePurchasingDocumentLinkIdentifier());
667 if (!SpringContext.getBean(DocumentHelperService.class).getDocumentAuthorizer(oleLineItemReceivingDocument).isAuthorizedByTemplate(oleLineItemReceivingDocument, KRADConstants.KNS_NAMESPACE, KimConstants.PermissionTemplateNames.OPEN_DOCUMENT, GlobalVariables.getUserSession().getPrincipalId())) {
668 throw new DocumentAuthorizationException(GlobalVariables.getUserSession().getPerson().getPrincipalName(), "initiate document", oleLineItemReceivingDocument.getDocumentNumber());
669 }
670 } else {
671 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, OLEKeyConstants.ERROR_RECEIVING_LINE_PO_NOT_EXIST, oleLineItemReceivingDocument.getPurchaseOrderIdentifier().toString());
672 }
673
674
675 if (!SpringContext.getBean(ReceivingService.class).isPurchaseOrderActiveForLineItemReceivingDocumentCreation(oleLineItemReceivingDocument.getPurchaseOrderIdentifier())) {
676 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, OLEKeyConstants.ERROR_RECEIVING_LINE_PONOTACTIVE, oleLineItemReceivingDocument.getPurchaseOrderIdentifier().toString());
677
678 isValid = false;
679 return isValid;
680 }
681
682 if (SpringContext.getBean(ReceivingService.class).canCreateLineItemReceivingDocument(oleLineItemReceivingDocument.getPurchaseOrderIdentifier(), oleLineItemReceivingDocument.getDocumentNumber()) == false) {
683 String inProcessDocNum = "";
684 List<String> inProcessDocNumbers = SpringContext.getBean(ReceivingService.class).getLineItemReceivingDocumentNumbersInProcessForPurchaseOrder(oleLineItemReceivingDocument.getPurchaseOrderIdentifier(), oleLineItemReceivingDocument.getDocumentNumber());
685 if (!inProcessDocNumbers.isEmpty()) {
686 inProcessDocNum = inProcessDocNumbers.get(0);
687 }
688 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, OLEKeyConstants.ERROR_RECEIVING_LINE_DOCUMENT_ACTIVE_FOR_PO, oleLineItemReceivingDocument.getPurchaseOrderIdentifier().toString(), inProcessDocNum);
689 isValid = false;
690 return isValid;
691 }
692 try {
693 if (isValid) {
694
695 SpringContext.getBean(ReceivingService.class).populateAndSaveLineItemReceivingDocument(oleLineItemReceivingDocument);
696
697 List<OleLineItemReceivingItem> itemList = new ArrayList<OleLineItemReceivingItem>();
698 for (Object item : oleLineItemReceivingDocument.getItems()) {
699 OleLineItemReceivingItem rlItem = (OleLineItemReceivingItem) item;
700
701 boolean isPOItemPresent = false;
702 for (OlePurchaseOrderItem poItem : purchaseOrderItems) {
703 if (poItem.isSelected()) {
704 if (!isPOItemPresent
705 && poItem.getItemIdentifier().equals(rlItem.getPurchaseOrderIdentifier())) {
706 rlItem.setOleItemReceivedTotalQuantity(rlItem.getOleItemReceivedToBeQuantity());
707
708 rlItem.setOleItemReceivedTotalParts(rlItem.getOleItemReceivedToBeParts());
709 rlItem.setPoSelected(true);
710
711
712
713
714 isPOItemPresent = true;
715 } else if (!isPOItemPresent) {
716 rlItem.setItemReceivedTotalQuantity(KualiDecimal.ZERO);
717 rlItem.setItemReceivedTotalParts(KualiDecimal.ZERO);
718 }
719 }
720 }
721
722 itemList.add(rlItem);
723 }
724 if (ObjectUtils.isNotNull(itemList) && itemList.size() > 0) {
725 oleLineItemReceivingDocument.setItems(itemList);
726
727 Person principalPerson = SpringContext.getBean(PersonService.class).getPerson(GlobalVariables.getUserSession().getPerson().getPrincipalId());
728
729 oleLineItemReceivingDocument.getDocumentHeader().setWorkflowDocument(KRADServiceLocatorWeb.getWorkflowDocumentService().loadWorkflowDocument(oleLineItemReceivingDocument.getDocumentNumber(), principalPerson));
730
731 if (isCreateRCV) {
732 SpringContext.getBean(DocumentService.class).saveDocument(oleLineItemReceivingDocument);
733 } else {
734 List<OleLineItemReceivingItem> items = oleLineItemReceivingDocument.getItems();
735 for (OleLineItemReceivingItem item : items) {
736 OleLineItemReceivingService oleLineItemReceivingService = SpringContext.getBean(OleLineItemReceivingServiceImpl.class);
737 OlePurchaseOrderItem olePurchaseOrderItem = oleLineItemReceivingService.getOlePurchaseOrderItem(item.getPurchaseOrderIdentifier());
738 if (olePurchaseOrderItem != null) {
739 if (item.isPoSelected()) {
740 for (OleCopy oleCopy : item.getCopyList()) {
741 oleCopy.setReceiptStatus(OLEConstants.OleLineItemReceiving.RECEIVED_STATUS);
742 }
743 OleCopyHelperService oleCopyHelperService = SpringContext.getBean(OleCopyHelperService.class);
744 oleCopyHelperService.updateRequisitionAndPOItems(olePurchaseOrderItem, item, null, oleLineItemReceivingDocument.getIsATypeOfRCVGDoc());
745 }
746 }
747 }
748 try {
749 SpringContext.getBean(DocumentService.class).routeDocument(oleLineItemReceivingDocument, "Line Item Receiving from Receiving Queue Search page", null);
750 } catch (Exception e) {
751 e.printStackTrace();
752 }
753
754 }
755
756 receivePOSuccess = true;
757 }
758 }
759 } catch (WorkflowException wfe) {
760 String rcvDocNum = oleLineItemReceivingDocument.getDocumentNumber();
761 String poId = oleLineItemReceivingDocument.getPurchaseOrderIdentifier().toString();
762 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, OLEKeyConstants.ERROR_RECEIVING_LINE_SAVE_OR_SUBMIT, new String[]{poId, rcvDocNum, wfe.getMessage()});
763
764
765 isValid = false;
766 return isValid;
767 }
768
769 return receivePOSuccess;
770 }
771
772
773 public boolean receiveAndPay(List<OlePurchaseOrderItem> purchaseOrderItems, OlePurchaseOrderDocument olePurchaseOrderDocument) throws Exception {
774 boolean isSuccess = false;
775
776 OleLineItemReceivingDocument oleLineItemReceivingDocument = (OleLineItemReceivingDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("OLE_RCVL");
777
778 HashMap<Integer, OlePurchaseOrderItem> selectedPOs = getSelectedPurchaseOrders(purchaseOrderItems);
779
780 HashMap<Integer, Boolean> receivePOStatus = new HashMap<Integer, Boolean>();
781 boolean isInfoMsg = false;
782 for (Map.Entry<Integer, OlePurchaseOrderItem> entry : selectedPOs.entrySet()) {
783 boolean receivePOSuccess = this.receivePO(oleLineItemReceivingDocument, entry.getKey(), false, purchaseOrderItems);
784 if (receivePOSuccess) {
785 receivingDocumentsList.add(oleLineItemReceivingDocument.getDocumentNumber());
786 isInfoMsg = true;
787 isSuccess = true;
788 }
789 receivePOStatus.put(entry.getKey(), receivePOSuccess);
790
791 }
792 List<OlePurchaseOrderItem> refreshedPOList = new ArrayList<OlePurchaseOrderItem>();
793 for (OlePurchaseOrderItem poItem : purchaseOrderItems) {
794 Integer poId = poItem.getPurchaseOrder().getPurapDocumentIdentifier();
795 if (ObjectUtils.isNull(receivePOStatus.get(poId))) {
796 refreshedPOList.add(poItem);
797 } else {
798 if (receivePOStatus.get(poId) == false) {
799 refreshedPOList.add(poItem);
800 }
801 }
802 }
803 return isSuccess;
804 }
805
806
807 private HashMap<Integer, OlePurchaseOrderItem> getSelectedPurchaseOrders(List<OlePurchaseOrderItem> purchaseOrders) {
808 HashMap<Integer, OlePurchaseOrderItem> poItemMap = new HashMap<Integer, OlePurchaseOrderItem>();
809 for (OlePurchaseOrderItem poItem : purchaseOrders) {
810 if (poItem.isSelected()) {
811 if (ObjectUtils.isNull(poItemMap.get(poItem.getPurchaseOrder().getPurapDocumentIdentifier()))) {
812 poItemMap.put(poItem.getPurchaseOrder().getPurapDocumentIdentifier(), poItem);
813 }
814 }
815 }
816
817
818 return poItemMap;
819 }
820
821 public boolean validateRecords(Integer purAppNum, String docNumber) {
822 boolean valid = true;
823
824 PurchaseOrderDocument po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(purAppNum);
825 valid &= SpringContext.getBean(ReceivingService.class).canCreateLineItemReceivingDocument(purAppNum, null);
826 return valid;
827 }
828
829 public boolean validateStatusOfPurchaseOrderDocument(OlePurchaseOrderDocument olePurchaseOrderDocument) {
830 boolean isValid = true;
831 PurchaseOrderDocument po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(olePurchaseOrderDocument.getPurapDocumentIdentifier());
832 if (po.getApplicationDocumentStatus().equalsIgnoreCase("Closed")) {
833 isValid = false;
834 }
835 return isValid;
836 }
837
838 public boolean validateInvoiceDocumentNumber(String documentNumber) {
839 Map<String, String> map = new HashMap<String, String>();
840 map.put("documentNumber", documentNumber);
841 List<OleInvoiceDocument> oleInvoiceDocuments = (List<OleInvoiceDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OleInvoiceDocument.class, map);
842 if (oleInvoiceDocuments.size() > 0) {
843 return true;
844 } else {
845 return false;
846 }
847
848 }
849
850 public boolean validateNumber(final String hex) {
851 Pattern pattern = Pattern.compile("[0-9]+");
852 Matcher matcher = pattern.matcher(hex);
853 return matcher.matches();
854
855 }
856
857 public boolean isPoAlreadyPaid(List<OlePurchaseOrderItem> olePurchaseOrderItems) {
858 boolean isPaid = false;
859 for (OlePurchaseOrderItem olePurchaseOrderItem : olePurchaseOrderItems) {
860 if (olePurchaseOrderItem.isSelected()) {
861 if (olePurchaseOrderItem.getCopyList() != null && olePurchaseOrderItem.getCopyList().size() > 0) {
862 for (OleCopy oleCopy : olePurchaseOrderItem.getCopyList()) {
863 if (oleCopy != null && oleCopy.getOlePaidCopies() != null && oleCopy.getOlePaidCopies().size() > 0) {
864 for (OLEPaidCopy olePaidCopy : oleCopy.getOlePaidCopies()) {
865 if (olePaidCopy != null && olePaidCopy.getPaymentRequestIdentifier() != null) {
866 isPaid = true;
867 break;
868 }
869 }
870 }
871 }
872 }
873 }
874 }
875 return isPaid;
876 }
877
878
879
880
881 private void updateForeignCurrencyDetails(OleInvoiceDocument oleInvoiceDocument) {
882 String currencyType = null;
883 BigDecimal exchangeRate = null;
884 oleInvoiceDocument.setInvoiceCurrencyType(oleInvoiceDocument.getVendorDetail().getCurrencyType().getCurrencyTypeId().toString());
885 if (StringUtils.isNotBlank(oleInvoiceDocument.getInvoiceCurrencyType())) {
886 currencyType = getInvoiceService().getCurrencyType(oleInvoiceDocument.getInvoiceCurrencyType());
887 if (StringUtils.isNotBlank(currencyType)) {
888
889 if (!currencyType.equalsIgnoreCase(OleSelectConstant.CURRENCY_TYPE_NAME)) {
890 oleInvoiceDocument.setForeignCurrencyFlag(true);
891 oleInvoiceDocument.setInvoiceCurrencyTypeId(new Long(oleInvoiceDocument.getInvoiceCurrencyType()));
892 exchangeRate = getInvoiceService().getExchangeRate(oleInvoiceDocument.getInvoiceCurrencyType()).getExchangeRate();
893 oleInvoiceDocument.setInvoiceCurrencyExchangeRate(exchangeRate.toString());
894 if (StringUtils.isNotBlank(oleInvoiceDocument.getInvoiceAmount())) {
895 oleInvoiceDocument.setForeignVendorInvoiceAmount(new BigDecimal(oleInvoiceDocument.getInvoiceAmount()).multiply(exchangeRate));
896 oleInvoiceDocument.setForeignInvoiceAmount(new KualiDecimal(oleInvoiceDocument.getForeignVendorInvoiceAmount()).toString());
897 oleInvoiceDocument.setForeignVendorAmount(new KualiDecimal(oleInvoiceDocument.getForeignVendorInvoiceAmount()).toString());
898 }
899 }
900 }
901 }
902
903 }
904
905
906
907 }