View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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.businessobject.*;
29  import org.kuali.ole.select.document.OleInvoiceDocument;
30  import org.kuali.ole.select.document.OleLineItemReceivingDocument;
31  import org.kuali.ole.select.document.OlePurchaseOrderDocument;
32  import org.kuali.ole.select.document.service.OleDocstoreHelperService;
33  import org.kuali.ole.select.document.service.OleInvoiceService;
34  import org.kuali.ole.select.document.service.OleLineItemReceivingService;
35  import org.kuali.ole.select.document.service.impl.OleLineItemReceivingServiceImpl;
36  import org.kuali.ole.sys.OLEConstants;
37  import org.kuali.ole.sys.OLEKeyConstants;
38  import org.kuali.ole.sys.businessobject.Bank;
39  import org.kuali.ole.sys.context.SpringContext;
40  import org.kuali.ole.sys.document.validation.event.AttributedBlanketApproveDocumentEvent;
41  import org.kuali.ole.sys.service.BankService;
42  import org.kuali.ole.vnd.VendorConstants;
43  import org.kuali.ole.vnd.businessobject.VendorAddress;
44  import org.kuali.ole.vnd.document.service.VendorService;
45  import org.kuali.rice.core.api.datetime.DateTimeService;
46  import org.kuali.rice.core.api.util.type.AbstractKualiDecimal;
47  import org.kuali.rice.core.api.util.type.KualiDecimal;
48  import org.kuali.rice.core.api.util.type.KualiInteger;
49  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
50  import org.kuali.rice.kew.api.document.DocumentStatus;
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.sql.Date;
66  import java.util.ArrayList;
67  import java.util.HashMap;
68  import java.util.List;
69  import java.util.Map;
70  public class OLEAddTitlesToInvoiceService {
71      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OLEAddTitlesToInvoiceService.class);
72      DocumentService documentService;
73      List<String> receivingDocumentsList = new ArrayList<String>();
74      boolean isAllowedPoForReceiving=false;
75  
76      public List<String> getReceivingDocumentsList() {
77          return receivingDocumentsList;
78      }
79  
80      OleInvoiceDocument oleInvoiceDocument;
81  
82      public OleInvoiceDocument getOleInvoiceDocument() {
83          return oleInvoiceDocument;
84      }
85  
86      public void setOleInvoiceDocument(OleInvoiceDocument oleInvoiceDocument) {
87          this.oleInvoiceDocument = oleInvoiceDocument;
88      }
89  
90      public boolean isAllowedPoForReceiving() {
91          return isAllowedPoForReceiving;
92      }
93  
94      public void setAllowedPoForReceiving(boolean allowedPoForReceiving) {
95          isAllowedPoForReceiving = allowedPoForReceiving;
96      }
97  
98      public List<OlePurchaseOrderItem> populateOlePurchaseOrderItem() {
99          List<OlePurchaseOrderItem> result = new ArrayList<OlePurchaseOrderItem>();
100         List<OlePurchaseOrderDocument> olePurchaseOrderDocuments = (List<OlePurchaseOrderDocument>) KRADServiceLocator.getBusinessObjectService().findAll(OlePurchaseOrderDocument.class);
101         for (OlePurchaseOrderDocument olePurchaseOrderDocument : olePurchaseOrderDocuments) {
102             String olePurchaseOrderDocumentNumber = olePurchaseOrderDocument.getDocumentNumber();
103             List<OlePurchaseOrderItem> olePurchaseOrderItems = olePurchaseOrderDocument.getItems();
104             for (OlePurchaseOrderItem olePurchaseOrderItem : olePurchaseOrderItems) {
105                 if (olePurchaseOrderItem.getItemTypeCode().toString().equals("ITEM") && olePurchaseOrderItem.getDocumentNumber().equals(olePurchaseOrderDocumentNumber)) {
106                     Map<String, String> map = new HashMap<String, String>();
107                     map.put("poItemIdentifier", olePurchaseOrderItem.getItemIdentifier().toString());
108                             /*List<OleInvoiceItem> oleInvoiceItems=(List<OleInvoiceItem>) KRADServiceLocator.getBusinessObjectService().findMatching(OleInvoiceItem.class,map);*/
109                             /* olePurchaseOrderItem.setSelected(true);*/
110                            /* if(!(oleInvoiceItems.size()>0)){*/
111                     if (validateRecords(olePurchaseOrderDocument.getPurapDocumentIdentifier(), olePurchaseOrderDocument.getDocumentNumber())) {
112                         olePurchaseOrderItem.setOlePurchaseOrderDocument(olePurchaseOrderDocument);
113                         result.add(olePurchaseOrderItem);
114                     }
115                             /*}*/
116                 }
117             }
118         }
119         return result;
120     }
121 
122     public List<OlePurchaseOrderItem> populateOlePurchaseOrderItemByPoItemList(List<String> purchaseOrderItemIds,List<String> invoicePrice) {
123         List<OlePurchaseOrderItem> result = new ArrayList<OlePurchaseOrderItem>();
124         List<OlePurchaseOrderDocument> olePurchaseOrderDocuments = (List<OlePurchaseOrderDocument>) KRADServiceLocator.getBusinessObjectService().findAll(OlePurchaseOrderDocument.class);
125         int index=0;
126         for (String poitmId : purchaseOrderItemIds) {
127             for (OlePurchaseOrderDocument olePurchaseOrderDocument : olePurchaseOrderDocuments) {
128                 List<OlePurchaseOrderItem> olePurchaseOrderItems = olePurchaseOrderDocument.getItems();
129                 for (OlePurchaseOrderItem olePurchaseOrderItem : olePurchaseOrderItems) {
130                     if (olePurchaseOrderItem.getItemIdentifier().toString().equalsIgnoreCase(poitmId.toString())) {
131                         if (validateRecords(olePurchaseOrderDocument.getPurapDocumentIdentifier(), olePurchaseOrderDocument.getDocumentNumber())) {
132                             olePurchaseOrderItem.setSelected(true);
133                             olePurchaseOrderItem.setOlePurchaseOrderDocument(olePurchaseOrderDocument);
134                             if(invoicePrice.get(index)!=null && (!invoicePrice.get(index).trim().equalsIgnoreCase(""))){
135                              olePurchaseOrderItem.setInvoiceItemListPrice(invoicePrice.get(index));
136                             }else {
137                                 olePurchaseOrderItem.setInvoiceItemListPrice(olePurchaseOrderItem.getExtendedPrice().toString());
138                             }
139                             result.add(olePurchaseOrderItem);
140 
141                         }
142                     }
143                 }
144 
145             }
146            index++;
147         }
148 
149 
150         return result;
151     }
152 
153     public boolean validateSelectOlePurchaseOrderItem(List<OlePurchaseOrderItem> olePurchaseOrderItems) {
154         boolean isSuccess = false;
155 
156         for (OlePurchaseOrderItem olePurchaseOrderItem : olePurchaseOrderItems) {
157             if (olePurchaseOrderItem.isSelected()) {
158                 isSuccess = true;
159             }
160         }
161 
162         return isSuccess;
163     }
164 
165     public boolean validateOlePurchaseOrderItemByPoId(List<OlePurchaseOrderItem> olePurchaseOrderItems) {
166         List<OlePurchaseOrderItem> result = new ArrayList<OlePurchaseOrderItem>();
167         boolean isSuccess = true;
168         String poId = "";
169         for (OlePurchaseOrderItem olePurchaseOrderItem : olePurchaseOrderItems) {
170             if (poId.equalsIgnoreCase("")) {
171                 poId = olePurchaseOrderItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier().toString();
172             } else {
173                 if (!poId.equalsIgnoreCase(olePurchaseOrderItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier().toString())) {
174                     isSuccess = false;
175                 }
176             }
177 
178         }
179         return isSuccess;
180     }
181 
182     public boolean validateSelectedPurchaseOrderByVendor(List<OlePurchaseOrderItem> olePurchaseOrderItems, String vendorId) {
183         boolean isSuccess = true;
184         for (OlePurchaseOrderItem olePurchaseOrderItem : olePurchaseOrderItems) {
185             if (!olePurchaseOrderItem.getOlePurchaseOrderDocument().getVendorHeaderGeneratedIdentifier().toString().equalsIgnoreCase(vendorId.toString())) {
186                 isSuccess = false;
187             }
188         }
189         return isSuccess;
190     }
191 
192     public boolean validateInvoiceDocumentVendor(String invoiceDocumentNumber, List<OlePurchaseOrderItem> purchaseOrderItems) {
193         boolean isValid = true;
194         Map<String, String> map = new HashMap<String, String>();
195         map.put("purapDocumentIdentifier", invoiceDocumentNumber);
196         List<OleInvoiceDocument> oleInvoiceDocuments = (List<OleInvoiceDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OleInvoiceDocument.class, map);
197         OleInvoiceDocument oleInvoiceDocument = oleInvoiceDocuments.get(0);
198         for (OlePurchaseOrderItem olePurchaseOrderItem : purchaseOrderItems) {
199             if (olePurchaseOrderItem.isSelected()) {
200                 if (oleInvoiceDocument.getVendorAddressGeneratedIdentifier() != null) {
201                     if (!(olePurchaseOrderItem.getOlePurchaseOrderDocument().getVendorHeaderGeneratedIdentifier().toString().equalsIgnoreCase(oleInvoiceDocument.getVendorHeaderGeneratedIdentifier().toString()))) {
202                         if (oleInvoiceDocument.getVendorAddressGeneratedIdentifier().toString().equalsIgnoreCase(olePurchaseOrderItem.getOlePurchaseOrderDocument().getVendorAddressGeneratedIdentifier().toString())) {
203                             isValid = false;
204                         }
205                     }
206                 } else {
207 
208                     if (!(olePurchaseOrderItem.getOlePurchaseOrderDocument().getVendorHeaderGeneratedIdentifier().toString().equalsIgnoreCase(oleInvoiceDocument.getVendorHeaderGeneratedIdentifier().toString()))) {
209                         isValid = false;
210                     }
211 
212                 }
213             }
214         }
215         return isValid;
216     }
217 
218     public boolean validateSelectedPurchaseOrderByVendor(List<OlePurchaseOrderItem> purchaseOrderItems) {
219         boolean isSuccess = true;
220         List<String> list = new ArrayList<String>();
221         for (OlePurchaseOrderItem olePurchaseOrderItem : purchaseOrderItems) {
222             if (olePurchaseOrderItem.isSelected()) {
223                 if (!list.contains(olePurchaseOrderItem.getOlePurchaseOrderDocument().getVendorHeaderGeneratedIdentifier().toString() + "-" + olePurchaseOrderItem.getOlePurchaseOrderDocument().getVendorDetailAssignedIdentifier())) {
224                     list.add(olePurchaseOrderItem.getOlePurchaseOrderDocument().getVendorHeaderGeneratedIdentifier().toString() + "-" + olePurchaseOrderItem.getOlePurchaseOrderDocument().getVendorDetailAssignedIdentifier());
225                 }
226             }
227         }
228         if (list.size() > 1) {
229             isSuccess = false;
230         }
231         return isSuccess;
232     }
233     public boolean isSelectedAtleatOneItem(List<OlePurchaseOrderItem> purchaseOrderItems) {
234         boolean isValid=false;
235         List<OlePurchaseOrderItem> olePurchaseOrderItemSelected = new ArrayList<OlePurchaseOrderItem>();
236         for (OlePurchaseOrderItem olePurchaseOrderItem : purchaseOrderItems) {
237             if (olePurchaseOrderItem.isSelected()) {
238                 isValid=true;
239             }
240         }
241         return isValid;
242     }
243     public List<OlePurchaseOrderItem> getSelectedItems(List<OlePurchaseOrderItem> purchaseOrderItems) {
244         List<OlePurchaseOrderItem> olePurchaseOrderItemSelected = new ArrayList<OlePurchaseOrderItem>();
245         for (OlePurchaseOrderItem olePurchaseOrderItem : purchaseOrderItems) {
246             if (olePurchaseOrderItem.isSelected()) {
247                 olePurchaseOrderItemSelected.add(olePurchaseOrderItem);
248             }
249         }
250         return olePurchaseOrderItemSelected;
251     }
252 
253     public boolean createNewInvoiceDocument(List<OlePurchaseOrderDocument> olePurchaseOrderDocuments, List<OlePurchaseOrderItem> olePurchaseOrderItems, String paymentMethodId, Date invoiceDate, String invoiceNumber, String vendorInvoiceAmt, String principalId) throws Exception {
254         boolean isSuccess = false;
255         try {
256 
257             OleInvoiceDocument oleInvoiceDocument = (OleInvoiceDocument) SpringContext.getBean(DocumentService.class).getNewDocument("OLE_PRQS");
258             java.util.Date date1 = new java.util.Date();
259             oleInvoiceDocument.setInvoiceDate(new Date(SpringContext.getBean(DateTimeService.class).getCurrentDate().getTime()));
260             oleInvoiceDocument.setInvoiceNumber(invoiceNumber);
261             oleInvoiceDocument.setPaymentMethodId(Integer.parseInt(paymentMethodId));
262             oleInvoiceDocument.setVendorInvoiceAmount(new KualiDecimal(vendorInvoiceAmt));
263             /*oleInvoiceDocument.setInvoiceNumber("11112");*/
264 
265             /*for(OlePurchaseOrderDocument purchaseOrderDocument:olePurchaseOrderDocuments){
266                 List<OlePurchaseOrderItem> olePurchaseOrderItems1=purchaseOrderDocument.getItems();
267                 for(OlePurchaseOrderItem olePurchaseOrderItem1:olePurchaseOrderItems){
268                     for(OlePurchaseOrderItem olePurchaseOrderItem2:olePurchaseOrderItems1){
269                         if(olePurchaseOrderItem1.getItemIdentifier().toString().equalsIgnoreCase(olePurchaseOrderItem2.getItemIdentifier().toString())){
270                             olePurchaseOrderItem1.setItemForInvoice(true);
271                         }
272                     }
273                     purchaseOrderDocument.setPendingActionIndicator(false);
274 
275                 }
276 
277             }*/
278             Map<String, ExpiredOrClosedAccount> map1 = new HashMap<String, ExpiredOrClosedAccount>();
279             for (OlePurchaseOrderDocument olePurchaseOrderDocument : olePurchaseOrderDocuments) {
280                 if (olePurchaseOrderDocument != null) {
281                     this.populateInvoiceFromPurchaseOrder(olePurchaseOrderDocument, oleInvoiceDocument, null, true);
282                     oleInvoiceDocument.getPurchaseOrderDocuments().add(olePurchaseOrderDocument);
283                 }
284             }
285 
286             oleInvoiceDocument.getDocumentHeader().setDocumentDescription("New Invoice");
287             boolean isRulePassed = (KRADServiceLocatorWeb.getKualiRuleService()).applyRules(new AttributedBlanketApproveDocumentEvent("", oleInvoiceDocument));
288             if (isRulePassed) {
289                 try {
290                     getOleInvoiceServiceImpl().routeInvoiceDocument(oleInvoiceDocument);
291                 } catch (Exception e) {
292                     e.printStackTrace();
293                 }
294             } else {
295                 return false;
296             }
297             isSuccess = true;
298             if (isSuccess) {
299                 this.setOleInvoiceDocument(oleInvoiceDocument);
300             }
301 
302         } catch (WorkflowException e) {
303             LOG.info("Exception Occurred during creating Invoice Document" + e);
304             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
305         }
306 
307         return isSuccess;
308     }
309 
310     public List<OlePurchaseOrderDocument> populatePurchaseOrderDocuments(List<OlePurchaseOrderItem> purchaseOrderItems) {
311         List<OlePurchaseOrderDocument> result = new ArrayList<OlePurchaseOrderDocument>();
312         List<String> poId = new ArrayList<String>();
313         for (OlePurchaseOrderItem olePurchaseOrderItem : purchaseOrderItems) {
314             if (!poId.contains(olePurchaseOrderItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier().toString())) {
315                 if (olePurchaseOrderItem.isSelected()) {
316                     poId.add(olePurchaseOrderItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier().toString());
317                 }
318             }
319 
320         }
321         for (String id : poId) {
322             Map<String, String> map = new HashMap<String, String>();
323             map.put("purapDocumentIdentifier", id);
324             List<OlePurchaseOrderDocument> olePurchaseOrderDocuments = (List<OlePurchaseOrderDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePurchaseOrderDocument.class, map);
325             result.add(olePurchaseOrderDocuments.get(0));
326         }
327 
328         for (OlePurchaseOrderItem purchaseOrderItem : purchaseOrderItems) {
329             if (purchaseOrderItem.isSelected()) {
330                 purchaseOrderItem.setItemForInvoice(false);
331                 for (OlePurchaseOrderDocument olePurchaseOrderDocument : result) {
332                     List<OlePurchaseOrderItem> olePurchaseOrderItems = olePurchaseOrderDocument.getItems();
333                     for (OlePurchaseOrderItem olePurchaseOrderItem : olePurchaseOrderItems) {
334                         if (purchaseOrderItem.getItemIdentifier().toString().equalsIgnoreCase(olePurchaseOrderItem.getItemIdentifier().toString()) && olePurchaseOrderItem.getItemTypeCode().equals("ITEM")) {
335                             olePurchaseOrderItem.setSelected(true);
336                             olePurchaseOrderItem.setItemForInvoice(true);
337                             olePurchaseOrderItem.setInvoiceItemListPrice(purchaseOrderItem.getInvoiceItemListPrice());
338 
339                         }
340                     }
341                 }
342             }
343         }
344         return result;
345     }
346 
347 
348     public boolean addOlePurchaseOrderItemsToInvoiceDocument(List<OlePurchaseOrderDocument> olePurchaseOrderDocuments, List<OlePurchaseOrderItem> olePurchaseOrderItems, String invoiceNbr, String principalId,String invoiceAmt) throws Exception {
349         boolean isSuccess = false;
350         //getting invoice document
351         Map<String, String> map = new HashMap<String, String>();
352         map.put("purapDocumentIdentifier", invoiceNbr);
353         List<OleInvoiceDocument> oleInvoiceDocuments = (List<OleInvoiceDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OleInvoiceDocument.class, map);
354         OleInvoiceDocument oleInvoiceDocument = oleInvoiceDocuments.get(0);
355         /*for(OlePurchaseOrderDocument purchaseOrderDocument:olePurchaseOrderDocuments){
356             List<OlePurchaseOrderItem> olePurchaseOrderItems1=purchaseOrderDocument.getItems();
357             for(OlePurchaseOrderItem olePurchaseOrderItem1:olePurchaseOrderItems){
358                 for(OlePurchaseOrderItem olePurchaseOrderItem2:olePurchaseOrderItems1){
359                     if(olePurchaseOrderItem1.getItemIdentifier().toString().equalsIgnoreCase(olePurchaseOrderItem2.getItemIdentifier().toString())){
360                         olePurchaseOrderItem1.setItemForInvoice(true);
361                     }
362                 }
363                 purchaseOrderDocument.setPendingActionIndicator(false);
364 
365             }
366 
367         }*/
368         if(invoiceAmt!=null){
369             //oleInvoiceDocument.setInvoiceAmount(invoiceAmt);
370             oleInvoiceDocument.setVendorInvoiceAmount(new KualiDecimal(invoiceAmt));
371         }
372         for (OlePurchaseOrderDocument olePurchaseOrderDocument : olePurchaseOrderDocuments) {
373             if (olePurchaseOrderDocument != null) {
374                 this.populateInvoiceFromPurchaseOrder(olePurchaseOrderDocument, oleInvoiceDocument, null, false);
375                 oleInvoiceDocument.getPurchaseOrderDocuments().add(olePurchaseOrderDocument);
376             }
377         }
378 
379         oleInvoiceDocument.getDocumentHeader().setDocumentDescription("New Invoice");
380         boolean isRulePassed = (KRADServiceLocatorWeb.getKualiRuleService()).applyRules(new AttributedBlanketApproveDocumentEvent("", oleInvoiceDocument));
381         if (isRulePassed) {
382             try {
383                 getOleInvoiceServiceImpl().saveInvoiceDocument(oleInvoiceDocument);
384             } catch (Exception e) {
385                 LOG.info("Exception Occurred during updating Invoice Document" + e);
386                 e.printStackTrace();
387             }
388         } else {
389             return false;
390         }
391         isSuccess = true;
392         if (isSuccess) {
393             this.setOleInvoiceDocument(oleInvoiceDocument);
394         }
395         return isSuccess;
396 
397 
398     }
399 
400     public boolean validateReceivingForProcess(List<OlePurchaseOrderItem> purchaseOrderItems) {
401         boolean isValid = false;
402         int selectedItem=0;
403         int receivingExistForItem=0;
404         for (OlePurchaseOrderItem olePurchaseOrderItem : purchaseOrderItems) {
405             if (olePurchaseOrderItem.isSelected()) {
406                 selectedItem++;
407                 Map<String, String> map = new HashMap<String, String>();
408                 map.put("purchaseOrderIdentifier", olePurchaseOrderItem
409                         .getItemIdentifier().toString());
410                 List<OleLineItemReceivingItem> oleLineItemReceivingItems = (List<OleLineItemReceivingItem>) KRADServiceLocator
411                         .getBusinessObjectService().findMatching(
412                                 OleLineItemReceivingItem.class, map);
413                 if (oleLineItemReceivingItems.size() > 0) {
414                     OleLineItemReceivingItem oleLineItemReceivingItem = oleLineItemReceivingItems.get(0);
415                     /*if (oleLineItemReceivingItem.getReceiptStatusId().toString().equalsIgnoreCase(
416                             getReceiptStatusDetails(OLEConstants.PO_RECEIPT_STATUS_FULLY_RECEIVED) + "")) {
417                         */
418                         receivingExistForItem++;
419                         isValid = true;
420                         olePurchaseOrderItem.setSelected(false);
421                     /*}*/
422                 }
423             }
424         }
425         if(selectedItem!=receivingExistForItem){
426             this.isAllowedPoForReceiving=true;
427         }
428         return isValid;
429     }
430 
431     public int getReceiptStatusDetails(String receiptStatusCd) {
432         int receiptStatusId = 0;
433         Map<String, String> receiptStatusCdMap = new HashMap<String, String>();
434         receiptStatusCdMap.put(OLEConstants.RCPT_STATUS_CD, receiptStatusCd);
435         List<OleReceiptStatus> oleReceiptStatusList = (List) SpringContext.getBean(BusinessObjectService.class)
436                 .findMatching(OleReceiptStatus.class, receiptStatusCdMap);
437         for (OleReceiptStatus oleReceiptStatus : oleReceiptStatusList) {
438             receiptStatusId = oleReceiptStatus.getReceiptStatusId().intValue();
439         }
440         return receiptStatusId;
441     }
442 
443     public OleInvoiceService getOleInvoiceServiceImpl() {
444 
445         OleInvoiceService oleInvoiceService = (OleInvoiceService) SpringContext.getBean(OleInvoiceService.class);
446         return oleInvoiceService;
447     }
448 
449     public boolean validateForReceiving(List<OlePurchaseOrderItem> purchaseOrderItems) {
450         boolean isValid = true;
451         String poid = "";
452         for (OlePurchaseOrderItem olePurchaseOrderItem : purchaseOrderItems) {
453             if (olePurchaseOrderItem.isSelected()) {
454                 if (!poid.equalsIgnoreCase("")) {
455                     if (!olePurchaseOrderItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier().toString().equalsIgnoreCase(poid)) {
456                         isValid = false;
457                         break;
458                     }
459                 } else {
460                     poid = olePurchaseOrderItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier().toString();
461                 }
462 
463             }
464         }
465         return isValid;
466     }
467 
468 
469     public void populateInvoiceFromPurchaseOrder(PurchaseOrderDocument po, OleInvoiceDocument oleInvoiceDocument, HashMap<String, ExpiredOrClosedAccountEntry> expiredOrClosedAccountList, boolean isNew) {
470         AccountsPayableDocumentSpecificService accountsPayableDocumentSpecificService = SpringContext.getBean(InvoiceService.class);
471         if (isNew) {
472             VendorService vendorService = SpringContext.getBean(VendorService.class);
473             PurapService purapService = SpringContext.getBean(PurapService.class);
474             //oleInvoiceDocument.setPaymentMethodId(po.getVendorDetail().getPaymentMethodId());
475             oleInvoiceDocument.setPurchaseOrderIdentifier(po.getPurapDocumentIdentifier());
476             oleInvoiceDocument.getDocumentHeader().setOrganizationDocumentNumber(po.getDocumentHeader().getOrganizationDocumentNumber());
477             oleInvoiceDocument.setPostingYear(po.getPostingYear());
478             oleInvoiceDocument.setReceivingDocumentRequiredIndicator(po.isReceivingDocumentRequiredIndicator());
479             oleInvoiceDocument.setUseTaxIndicator(po.isUseTaxIndicator());
480             oleInvoiceDocument.setInvoicePositiveApprovalIndicator(po.isPaymentRequestPositiveApprovalIndicator());
481             oleInvoiceDocument.setVendorCustomerNumber(po.getVendorCustomerNumber());
482             oleInvoiceDocument.setReceivingDocumentRequiredIndicator(po.isReceivingDocumentRequiredIndicator());
483 
484             if (po.getPurchaseOrderCostSource() != null) {
485                 oleInvoiceDocument.setInvoiceCostSource(po.getPurchaseOrderCostSource());
486                 oleInvoiceDocument.setInvoiceCostSourceCode(po.getPurchaseOrderCostSourceCode());
487             }
488 
489             if (po.getVendorShippingPaymentTerms() != null) {
490                 oleInvoiceDocument.setVendorShippingPaymentTerms(po.getVendorShippingPaymentTerms());
491                 oleInvoiceDocument.setVendorShippingPaymentTermsCode(po.getVendorShippingPaymentTermsCode());
492             }
493 
494             if (po.getVendorPaymentTerms() != null) {
495                 oleInvoiceDocument.setVendorPaymentTermsCode(po.getVendorPaymentTermsCode());
496                 oleInvoiceDocument.setVendorPaymentTerms(po.getVendorPaymentTerms());
497             }
498 
499             if (po.getRecurringPaymentType() != null) {
500                 oleInvoiceDocument.setRecurringPaymentType(po.getRecurringPaymentType());
501                 oleInvoiceDocument.setRecurringPaymentTypeCode(po.getRecurringPaymentTypeCode());
502             }
503 
504             Bank defaultBank = SpringContext.getBean(BankService.class).getDefaultBankByDocType(oleInvoiceDocument.getClass());
505             if (defaultBank != null) {
506                 oleInvoiceDocument.setBankCode(defaultBank.getBankCode());
507                 oleInvoiceDocument.setBank(defaultBank);
508             }
509             oleInvoiceDocument.setVendorHeaderGeneratedIdentifier(po.getVendorHeaderGeneratedIdentifier());
510             oleInvoiceDocument.setVendorDetailAssignedIdentifier(po.getVendorDetailAssignedIdentifier());
511             oleInvoiceDocument.setVendorCustomerNumber(po.getVendorCustomerNumber());
512             oleInvoiceDocument.setVendorName(po.getVendorName());
513 
514             // set original vendor
515             oleInvoiceDocument.setOriginalVendorHeaderGeneratedIdentifier(po.getVendorHeaderGeneratedIdentifier());
516             oleInvoiceDocument.setOriginalVendorDetailAssignedIdentifier(po.getVendorDetailAssignedIdentifier());
517 
518             // set alternate vendor info as well
519             oleInvoiceDocument.setAlternateVendorHeaderGeneratedIdentifier(po.getAlternateVendorHeaderGeneratedIdentifier());
520             oleInvoiceDocument.setAlternateVendorDetailAssignedIdentifier(po.getAlternateVendorDetailAssignedIdentifier());
521 
522             // populate preq vendor address with the default remit address type for the vendor if found
523             String userCampus = GlobalVariables.getUserSession().getPerson().getCampusCode();
524             VendorAddress vendorAddress = vendorService.getVendorDefaultAddress(po.getVendorHeaderGeneratedIdentifier(), po.getVendorDetailAssignedIdentifier(), VendorConstants.AddressTypes.REMIT, userCampus);
525             if (vendorAddress != null) {
526                 oleInvoiceDocument.templateVendorAddress(vendorAddress);
527                 oleInvoiceDocument.setVendorAddressGeneratedIdentifier(vendorAddress.getVendorAddressGeneratedIdentifier());
528                 oleInvoiceDocument.setVendorAttentionName(StringUtils.defaultString(vendorAddress.getVendorAttentionName()));
529             } else {
530                 // set address from PO
531                 oleInvoiceDocument.setVendorAddressGeneratedIdentifier(po.getVendorAddressGeneratedIdentifier());
532                 oleInvoiceDocument.setVendorLine1Address(po.getVendorLine1Address());
533                 oleInvoiceDocument.setVendorLine2Address(po.getVendorLine2Address());
534                 oleInvoiceDocument.setVendorCityName(po.getVendorCityName());
535                 oleInvoiceDocument.setVendorAddressInternationalProvinceName(po.getVendorAddressInternationalProvinceName());
536                 oleInvoiceDocument.setVendorStateCode(po.getVendorStateCode());
537                 oleInvoiceDocument.setVendorPostalCode(po.getVendorPostalCode());
538                 oleInvoiceDocument.setVendorCountryCode(po.getVendorCountryCode());
539 
540                 boolean blankAttentionLine = StringUtils.equalsIgnoreCase(
541                         "Y",
542                         SpringContext.getBean(ParameterService.class).getParameterValueAsString(
543                                 PurapConstants.PURAP_NAMESPACE, "Document",
544                                 PurapParameterConstants.BLANK_ATTENTION_LINE_FOR_PO_TYPE_ADDRESS));
545 
546                 if (blankAttentionLine) {
547                     oleInvoiceDocument.setVendorAttentionName(StringUtils.EMPTY);
548                 } else {
549                     oleInvoiceDocument.setVendorAttentionName(StringUtils.defaultString(po.getVendorAttentionName()));
550                 }
551             }
552         }
553         oleInvoiceDocument.setInvoicePayDate(getOleInvoiceServiceImpl().calculatePayDate(oleInvoiceDocument.getInvoiceDate(), oleInvoiceDocument.getVendorPaymentTerms()));
554 
555      //   if (getOleInvoiceServiceImpl().encumberedItemExistsForInvoicing(po)) {
556             for (OlePurchaseOrderItem poi : (List<OlePurchaseOrderItem>) po.getItems()) {
557                 // check to make sure it's eligible for payment (i.e. active and has encumbrance available
558                 if (poi.isSelected()) {
559                     poi.setNoOfCopiesInvoiced(poi.getOleItemQuantity());
560                     OleInvoiceItem invoiceItem = new OleInvoiceItem(poi, oleInvoiceDocument);
561                     invoiceItem.setPurchaseOrderIdentifier(po.getPurapDocumentIdentifier());
562                     invoiceItem.setItemQuantity(poi.getItemQuantity());
563                     invoiceItem.setOlePoOutstandingQuantity(new KualiInteger(poi.getOutstandingQuantity().bigDecimalValue()));
564                     /*invoiceItem.setExtendedPrice(poi.getExtendedPrice());*/
565                     invoiceItem.setAccountsPayablePurchasingDocumentLinkIdentifier(po.getAccountsPayablePurchasingDocumentLinkIdentifier());
566                     invoiceItem.setReceivingDocumentRequiredIndicator(po.isReceivingDocumentRequiredIndicator());
567                     /*if (new KualiDecimal(poi.getInvoiceItemListPrice()).isLessThan(AbstractKualiDecimal.ZERO)) {
568                         invoiceItem.setInvoiceListPrice(poi.getInvoiceItemListPrice());
569                     }
570                     else {
571                         invoiceItem.setItemListPrice(new KualiDecimal(poi.getInvoiceItemListPrice()));
572                     }*/
573                     /*List<OleInvoiceItem> oleInvoiceItems=(List<OleInvoiceItem>)oleInvoiceDocument.getItems();
574                     int pos=1;
575                     if(oleInvoiceItems.size()>0){
576                         for(OleInvoiceItem oleInvoiceItem:oleInvoiceItems){
577                             if(oleInvoiceItem.getItemTypeCode().equalsIgnoreCase("ITEM")){
578                                 pos=oleInvoiceItem.getItemLineNumber();
579                             }
580                         }
581                         invoiceItem.setItemLineNumber(new Integer(pos+1));
582                     }  else{
583                         invoiceItem.setItemLineNumber(new Integer(pos));
584                     }*/
585                     oleInvoiceDocument.getItems().add(invoiceItem);
586                     PurchasingCapitalAssetItem purchasingCAMSItem = po.getPurchasingCapitalAssetItemByItemIdentifier(poi.getItemIdentifier());
587                     if (purchasingCAMSItem != null) {
588                         invoiceItem.setCapitalAssetTransactionTypeCode(purchasingCAMSItem.getCapitalAssetTransactionTypeCode());
589                     }
590 
591 
592                     // copy use tax items over
593                     invoiceItem.getUseTaxItems().clear();
594                     for (PurApItemUseTax useTax : poi.getUseTaxItems()) {
595                         invoiceItem.getUseTaxItems().add(useTax);
596                     }
597 
598                 }
599             }
600        // }
601 
602         // add missing below the line
603         /*purapService.addBelowLineItems(oleInvoiceDocument);*/
604         /*oleInvoiceDocument.setAccountsPayablePurchasingDocumentLinkIdentifier(po.getAccountsPayablePurchasingDocumentLinkIdentifier());*/
605 
606         //fix up below the line items
607         getOleInvoiceServiceImpl().removeIneligibleAdditionalCharges(oleInvoiceDocument);
608 
609         oleInvoiceDocument.fixItemReferences();
610         oleInvoiceDocument.refreshNonUpdateableReferences();
611     }
612 
613     public boolean receivePO(OleLineItemReceivingDocument oleLineItemReceivingDocument, Integer purchaseOrderIdentifier, boolean isCreateRCV, List<OlePurchaseOrderItem> purchaseOrderItems) {
614         boolean receivePOSuccess = false;
615 
616         // Setting defaults
617         oleLineItemReceivingDocument.setPurchaseOrderIdentifier(purchaseOrderIdentifier);
618         DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
619         oleLineItemReceivingDocument.setShipmentReceivedDate(dateTimeService.getCurrentSqlDate());
620         // Validations Start
621         boolean isValid = true;
622 
623         PurchaseOrderDocument po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(purchaseOrderIdentifier);
624 
625         if (ObjectUtils.isNotNull(po)) {
626             oleLineItemReceivingDocument.setAccountsPayablePurchasingDocumentLinkIdentifier(po.getAccountsPayablePurchasingDocumentLinkIdentifier());
627             if (!SpringContext.getBean(DocumentHelperService.class).getDocumentAuthorizer(oleLineItemReceivingDocument).isAuthorizedByTemplate(oleLineItemReceivingDocument, KRADConstants.KNS_NAMESPACE, KimConstants.PermissionTemplateNames.OPEN_DOCUMENT, GlobalVariables.getUserSession().getPrincipalId())) {
628                 throw new DocumentAuthorizationException(GlobalVariables.getUserSession().getPerson().getPrincipalName(), "initiate document", oleLineItemReceivingDocument.getDocumentNumber());
629             }
630         } else {
631             GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, OLEKeyConstants.ERROR_RECEIVING_LINE_PO_NOT_EXIST, oleLineItemReceivingDocument.getPurchaseOrderIdentifier().toString());
632         }
633 
634 
635         if (!SpringContext.getBean(ReceivingService.class).isPurchaseOrderActiveForLineItemReceivingDocumentCreation(oleLineItemReceivingDocument.getPurchaseOrderIdentifier())) {
636             GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, OLEKeyConstants.ERROR_RECEIVING_LINE_PONOTACTIVE, oleLineItemReceivingDocument.getPurchaseOrderIdentifier().toString());
637 
638             isValid = false;
639             return isValid;
640         }
641 
642         if (SpringContext.getBean(ReceivingService.class).canCreateLineItemReceivingDocument(oleLineItemReceivingDocument.getPurchaseOrderIdentifier(), oleLineItemReceivingDocument.getDocumentNumber()) == false) {
643             String inProcessDocNum = "";
644             List<String> inProcessDocNumbers = SpringContext.getBean(ReceivingService.class).getLineItemReceivingDocumentNumbersInProcessForPurchaseOrder(oleLineItemReceivingDocument.getPurchaseOrderIdentifier(), oleLineItemReceivingDocument.getDocumentNumber());
645             if (!inProcessDocNumbers.isEmpty()) {    // should not be empty if we reach this point
646                 inProcessDocNum = inProcessDocNumbers.get(0);
647             }
648             GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, OLEKeyConstants.ERROR_RECEIVING_LINE_DOCUMENT_ACTIVE_FOR_PO, oleLineItemReceivingDocument.getPurchaseOrderIdentifier().toString(), inProcessDocNum);
649             isValid = false;
650             return isValid;
651         }
652         try {
653             if (isValid) {
654 
655                 SpringContext.getBean(ReceivingService.class).populateAndSaveLineItemReceivingDocument(oleLineItemReceivingDocument);
656 
657                 List<OleLineItemReceivingItem> itemList = new ArrayList<OleLineItemReceivingItem>();
658                 for (Object item : oleLineItemReceivingDocument.getItems()) {
659                     OleLineItemReceivingItem rlItem = (OleLineItemReceivingItem) item;
660                     // Receiving 100pc
661                     boolean isPOItemPresent = false;
662                     for (OlePurchaseOrderItem poItem : purchaseOrderItems) {
663                         if (poItem.isSelected()) {
664                             if (!isPOItemPresent
665                                     && poItem.getItemIdentifier().equals(rlItem.getPurchaseOrderIdentifier())) {
666                                 rlItem.setOleItemReceivedTotalQuantity(rlItem.getOleItemReceivedToBeQuantity());
667 
668                                 rlItem.setOleItemReceivedTotalParts(rlItem.getOleItemReceivedToBeParts());
669                                 rlItem.setPoSelected(true);
670                                 /*
671                                  * rlItem.setItemReceivedTotalQuantity(rlItem.getItemOrderedQuantity());
672                                  * rlItem.setItemReceivedTotalParts(rlItem.getItemOrderedParts());
673                                  */
674                                 isPOItemPresent = true;
675                             } else if (!isPOItemPresent) {
676                                 rlItem.setItemReceivedTotalQuantity(KualiDecimal.ZERO);
677                                 rlItem.setItemReceivedTotalParts(KualiDecimal.ZERO);
678                             }
679                         }
680                     }
681 
682                     itemList.add(rlItem);
683                 }
684                 if (ObjectUtils.isNotNull(itemList) && itemList.size() > 0) {
685                     oleLineItemReceivingDocument.setItems(itemList);
686 
687                     Person principalPerson = SpringContext.getBean(PersonService.class).getPerson(GlobalVariables.getUserSession().getPerson().getPrincipalId());
688                     //oleLineItemReceivingDocument.getDocumentHeader().setWorkflowDocument(KRADServiceLocatorWeb.getWorkflowDocumentService().createWorkflowDocument(oleLineItemReceivingDocument.getDocumentHeader().getWorkflowDocument().getDocumentTypeName(), principalPerson));
689                     oleLineItemReceivingDocument.getDocumentHeader().setWorkflowDocument(KRADServiceLocatorWeb.getWorkflowDocumentService().loadWorkflowDocument(oleLineItemReceivingDocument.getDocumentNumber(), principalPerson));
690                     //oleLineItemReceivingDocument.setAdHocRoutePersons(buildFyiRecipient());
691                     if (isCreateRCV) {
692                         SpringContext.getBean(DocumentService.class).saveDocument(oleLineItemReceivingDocument);
693                     } else {
694                         List<OleLineItemReceivingItem> items = oleLineItemReceivingDocument.getItems();
695                         for (OleLineItemReceivingItem item : items) {
696                             OleLineItemReceivingService oleLineItemReceivingService = SpringContext.getBean(OleLineItemReceivingServiceImpl.class);
697                             OlePurchaseOrderItem olePurchaseOrderItem = oleLineItemReceivingService.getOlePurchaseOrderItem(item.getPurchaseOrderIdentifier());
698                             if (olePurchaseOrderItem != null) {
699                                 if (item.isPoSelected()) {
700                                     for (OleCopy oleCopy : item.getCopyList()) {
701                                         oleCopy.setReceiptStatus(OLEConstants.OleLineItemReceiving.RECEIVED_STATUS);
702                                     }
703                                     OleDocstoreHelperService oleDocstoreHelperService = SpringContext.getBean(OleDocstoreHelperService.class);
704                                     oleDocstoreHelperService.updateRequisitionAndPOItems(olePurchaseOrderItem, item, null, oleLineItemReceivingDocument.getIsATypeOfRCVGDoc());
705                                 }
706                             }
707                         }
708                         try {
709                             SpringContext.getBean(DocumentService.class).routeDocument(oleLineItemReceivingDocument, "Line Item Receiving from Receiving Queue Search page", null);
710                         } catch (Exception e) {
711                             e.printStackTrace();
712                         }
713 
714                     }
715 
716                     receivePOSuccess = true;
717                 }
718             }
719         } catch (WorkflowException wfe) {
720             String rcvDocNum = oleLineItemReceivingDocument.getDocumentNumber();
721             String poId = oleLineItemReceivingDocument.getPurchaseOrderIdentifier().toString();
722             GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, OLEKeyConstants.ERROR_RECEIVING_LINE_SAVE_OR_SUBMIT, new String[]{poId, rcvDocNum, wfe.getMessage()});
723 
724           /*  wfe.printStackTrace();*/
725             isValid = false;
726             return isValid;
727         }
728 
729         return receivePOSuccess;
730     }
731 
732 
733     public boolean receiveAndPay(List<OlePurchaseOrderItem> purchaseOrderItems,OlePurchaseOrderDocument olePurchaseOrderDocument) throws Exception {
734         boolean isSuccess = false;
735 
736         OleLineItemReceivingDocument oleLineItemReceivingDocument = (OleLineItemReceivingDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("OLE_RCVL");
737         // Elimiate duplicate POs from the list
738         HashMap<Integer, OlePurchaseOrderItem> selectedPOs = getSelectedPurchaseOrders(purchaseOrderItems);
739         // Map containing PO ID and receive PO status
740         HashMap<Integer, Boolean> receivePOStatus = new HashMap<Integer, Boolean>();
741         boolean isInfoMsg = false;
742         for (Map.Entry<Integer, OlePurchaseOrderItem> entry : selectedPOs.entrySet()) {
743             boolean receivePOSuccess = this.receivePO(oleLineItemReceivingDocument, entry.getKey(), false, purchaseOrderItems);
744             if (receivePOSuccess) {
745                 receivingDocumentsList.add(oleLineItemReceivingDocument.getDocumentNumber());
746                 isInfoMsg = true;
747                 isSuccess = true;
748             }
749             receivePOStatus.put(entry.getKey(), receivePOSuccess);
750 
751         }
752         List<OlePurchaseOrderItem> refreshedPOList = new ArrayList<OlePurchaseOrderItem>();
753         for (OlePurchaseOrderItem poItem : purchaseOrderItems) {
754             Integer poId = poItem.getPurchaseOrder().getPurapDocumentIdentifier();
755             if (ObjectUtils.isNull(receivePOStatus.get(poId))) {
756                 refreshedPOList.add(poItem);
757             } else {
758                 if (receivePOStatus.get(poId) == false) {
759                     refreshedPOList.add(poItem);
760                 }
761             }
762         }
763         return isSuccess;
764     }
765 
766 
767     private HashMap<Integer, OlePurchaseOrderItem> getSelectedPurchaseOrders(List<OlePurchaseOrderItem> purchaseOrders) {
768         HashMap<Integer, OlePurchaseOrderItem> poItemMap = new HashMap<Integer, OlePurchaseOrderItem>();
769         for (OlePurchaseOrderItem poItem : purchaseOrders) {
770             if (poItem.isSelected()) {
771                 if (ObjectUtils.isNull(poItemMap.get(poItem.getPurchaseOrder().getPurapDocumentIdentifier()))) {
772                     poItemMap.put(poItem.getPurchaseOrder().getPurapDocumentIdentifier(), poItem);
773                 }
774             }
775         }
776 
777 
778         return poItemMap;
779     }
780 
781     public boolean validateRecords(Integer purAppNum, String docNumber) {
782         boolean valid = true;
783 
784         PurchaseOrderDocument po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(purAppNum);
785         valid &= SpringContext.getBean(ReceivingService.class).canCreateLineItemReceivingDocument(purAppNum, null);
786         return valid;
787     }
788 
789     public boolean validateStatusOfPurchaseOrderDocument(OlePurchaseOrderDocument olePurchaseOrderDocument) {
790         boolean isValid = true;
791         PurchaseOrderDocument po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(olePurchaseOrderDocument.getPurapDocumentIdentifier());
792         if (po.getApplicationDocumentStatus().equalsIgnoreCase("Closed")) {
793             isValid = false;
794         }
795         return isValid;
796     }
797 
798 
799 }