View Javadoc
1   package org.kuali.ole.batch.ingest;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.DataCarrierService;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.OLETranscationalRecordGenerator;
8   import org.kuali.ole.batch.bo.OLEBatchBibImportDataObjects;
9   import org.kuali.ole.batch.bo.OLEBatchProcessProfileBo;
10  import org.kuali.ole.batch.controller.OLEBatchProcessJobDetailsController;
11  import org.kuali.ole.batch.impl.AbstractBatchProcess;
12  import org.kuali.ole.coa.businessobject.Account;
13  import org.kuali.ole.coa.businessobject.ObjectCode;
14  import org.kuali.ole.converter.MarcXMLConverter;
15  import org.kuali.ole.converter.OLEINVConverter;
16  import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
17  import org.kuali.ole.docstore.common.document.content.bib.marc.*;
18  import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
19  import org.kuali.ole.docstore.common.search.SearchResponse;
20  import org.kuali.ole.ingest.OleInvoiceRecordBuilder;
21  import org.kuali.ole.module.purap.PurapConstants;
22  import org.kuali.ole.module.purap.document.service.InvoiceService;
23  import org.kuali.ole.module.purap.document.service.OlePurapService;
24  import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
25  import org.kuali.ole.module.purap.businessobject.InvoiceAccount;
26  import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
27  import org.kuali.ole.module.purap.businessobject.PurchaseOrderAccount;
28  import org.kuali.ole.module.purap.document.validation.event.AttributedCalculateAccountsPayableEvent;
29  import org.kuali.ole.module.purap.service.PurapAccountingService;
30  import org.kuali.ole.pojo.OleInvoiceRecordHandler;
31  import org.kuali.ole.pojo.edi.INVOrders;
32  import org.kuali.ole.pojo.edi.INVOrder;
33  import org.kuali.ole.select.OleSelectConstant;
34  import org.kuali.ole.select.bo.OleVendorAccountInfo;
35  import org.kuali.ole.select.document.OLEInvoiceIngestLoadReport;
36  import org.kuali.ole.select.document.OleInvoiceDocument;
37  import org.kuali.ole.select.document.service.impl.OleInvoiceServiceImpl;
38  import org.kuali.ole.sys.document.validation.event.AttributedSaveDocumentEvent;
39  import org.kuali.ole.vnd.businessobject.OleCurrencyType;
40  import org.kuali.ole.vnd.businessobject.OleExchangeRate;
41  import org.kuali.ole.vnd.businessobject.VendorDetail;
42  import org.kuali.rice.core.api.config.property.ConfigurationService;
43  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
44  import org.kuali.rice.kew.api.exception.WorkflowException;
45  import org.kuali.rice.kim.api.identity.Person;
46  import org.kuali.ole.select.businessobject.OlePurchaseOrderItem;
47  import org.kuali.rice.krad.UserSession;
48  import org.kuali.rice.krad.service.*;
49  import org.kuali.rice.krad.util.ErrorMessage;
50  import org.kuali.rice.krad.util.GlobalVariables;
51  import org.kuali.ole.sys.context.SpringContext;
52  import org.kuali.ole.sys.service.BankService;
53  import org.kuali.ole.sys.businessobject.Bank;
54  import org.kuali.ole.sys.service.UniversityDateService;
55  import org.kuali.rice.core.api.util.type.KualiDecimal;
56  import org.kuali.rice.core.api.util.type.KualiInteger;
57  import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
58  import org.kuali.ole.pojo.OleInvoiceRecord;
59  import java.io.BufferedReader;
60  import java.io.File;
61  import java.io.FileReader;
62  import java.io.IOException;
63  import java.math.BigDecimal;
64  import java.text.MessageFormat;
65  import java.text.SimpleDateFormat;
66  import java.util.*;
67  
68  import org.kuali.ole.select.document.service.*;
69  import org.kuali.ole.select.businessobject.OleInvoiceItem;
70  import org.springframework.util.AutoPopulatingList;
71  
72  /**
73   * Created with IntelliJ IDEA.
74   * User: palanivel
75   * Date: 7/27/13
76   * Time: 3:37 PM
77   * To change this template use File | Settings | File Templates.
78   */
79  public class BatchProcessInvoiceIngest extends AbstractBatchProcess {
80  
81      private static final Logger LOG = Logger.getLogger(BatchProcessLocationIngest.class);
82  
83      private static transient BusinessObjectService businessObjectService;
84      private String xml;
85      protected DocumentService documentService;
86      public OleInvoiceService oleInvoiceService;
87      OLETranscationalRecordGenerator oleTranscationalRecordGenerator = new OLETranscationalRecordGenerator();
88      private StringBuffer failureRecords = new StringBuffer();
89      private DocstoreClientLocator docstoreClientLocator;
90      private String marcXMLContent;
91      DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
92      private static transient OlePurapService olePurapService;
93  
94      public static OlePurapService getOlePurapService() {
95          if (olePurapService == null) {
96              olePurapService = SpringContext.getBean(OlePurapService.class);
97          }
98          return olePurapService;
99      }
100 
101     public DocstoreClientLocator getDocstoreClientLocator() {
102 
103         if (docstoreClientLocator == null) {
104             docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
105 
106         }
107         return docstoreClientLocator;
108     }
109 
110     List<INVOrder> invOrder;
111     List<INVOrders> mismatchRecord = new ArrayList();
112 
113     protected BusinessObjectService getBusinessObjectService() {
114         if (businessObjectService == null) {
115             businessObjectService = SpringContext.getBean(BusinessObjectService.class);
116         }
117         return businessObjectService;
118     }
119 
120 
121     private OleInvoiceService getInvoiceService() {
122         if (oleInvoiceService == null) {
123             oleInvoiceService = SpringContext.getBean(OleInvoiceService.class);
124         }
125         return oleInvoiceService;
126     }
127 
128     public String readFile(File file) throws IOException {
129         BufferedReader reader = new BufferedReader(new FileReader(file));
130         String line = null;
131         StringBuilder stringBuilder = new StringBuilder();
132         String ls = System.getProperty("line.separator");
133         while ((line = reader.readLine()) != null) {
134             stringBuilder.append(line);
135             stringBuilder.append(ls);
136         }
137         return stringBuilder.toString();
138     }
139 
140 
141     @Override
142     public void prepareForRead() throws Exception {
143         try {
144             convertRawDataToXML();
145         } catch (Exception e) {
146             failureRecords.append("Please ensure the file is an Edifact file (OR) EDIFACT data has an extra EDIFACT segment.");
147             failureRecords.append("\n");
148             failureRecords.append("Unable to map the Edifact file.");
149             failureRecords.append("\n");
150             failureRecords.append("org.milyn.SmooksException:" + e.getMessage());
151             failureRecords.append("\n");
152             createBatchErrorAttachmentFile(failureRecords.append(new Throwable(e.getCause())).toString());
153             LOG.error(e, e);
154             job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
155             job.setStatusDesc(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
156             throw e;
157         }
158     }
159 
160     public void byPassLogicForPreProcess(String rawMarcContent, boolean preProcessingReq) {
161         if (preProcessingReq) {
162             preProcess(rawMarcContent);
163         } else {
164             marcXMLContent = rawMarcContent;
165         }
166     }
167 
168     private void preProcess(String rawMarcContent) {
169         marcXMLContent = preProcessMarc(rawMarcContent);
170     }
171 
172     private OLEBatchProcessProfileBo getBibImportProfile() {
173         String bibImportProfileForOrderRecord = this.processDef.getOleBatchProcessProfileBo().getBibImportProfileForOrderRecord();
174         org.kuali.rice.krad.service.BusinessObjectService
175                 businessObjectService = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
176         Map<String, String> bibImportProfileMap = new HashMap<>();
177         bibImportProfileMap.put("batchProcessProfileName", bibImportProfileForOrderRecord);
178         List<OLEBatchProcessProfileBo> oleBatchProcessProfileBoList = (List) businessObjectService.findMatching(OLEBatchProcessProfileBo.class, bibImportProfileMap);
179         if (oleBatchProcessProfileBoList != null && oleBatchProcessProfileBoList.size() > 0) {
180             return oleBatchProcessProfileBoList.get(0);
181         }
182         return null;
183     }
184 
185     public String preProcessMarc(String marcFileContent) {
186         String marcXMLContent = null;
187         MarcXMLConverter marcXMLConverter = new MarcXMLConverter();
188         marcXMLContent = marcXMLConverter.convert(marcFileContent);
189 
190         //TODO: hack to get rid of the extra xmlns entry. Not sure why the second entry gets generated when calling marc4J in ole-docstore-utility.
191         //TODO: the duplicate entry does not get genereated if its run directly in the ole-docstore-utilty project.
192         String modifiedXMLContent =
193                 marcXMLContent.
194                         replace("collection xmlns=\"http://www.loc.gov/MARC21/slim\" xmlns=\"http://www.loc.gov/MARC21/slim",
195                                 "collection xmlns=\"http://www.loc.gov/MARC21/slim");
196         return modifiedXMLContent;
197     }
198 
199     private void convertRawDataToXML() throws Exception {
200         String fileContent = getBatchProcessFileContent();
201         if (this.processDef.getUploadFileName().endsWith(".mrc")) {
202             byPassLogicForPreProcess(fileContent, true);
203             ;
204         } else {
205             OLEINVConverter OLEEDITranslator = new OLEINVConverter();
206             xml = OLEEDITranslator.convertToXML(fileContent);
207         }
208     }
209 
210     private void convertXMLToPojos(Map<String, List<OleInvoiceRecord>> map) throws Exception {
211         LOG.info("--------------xml----------" + xml);
212         INVOrders invOrders = null;
213         if (xml != null) {
214             invOrders = oleTranscationalRecordGenerator.fromInvoiceXml(xml);
215             invOrder = invOrders.getInvOrder();
216         }
217 
218         for (int i = 0; i < invOrder.size(); i++) {
219             OleInvoiceRecordBuilder oleInvoiceRecordBuilder = OleInvoiceRecordBuilder.getInstance();
220             OleInvoiceRecord oleInvoiceRecord = null;
221 
222             List<OleInvoiceRecord> oleInvoiceRecordList = new ArrayList<OleInvoiceRecord>();
223             for (int j = 0; j < invOrder.get(i).getLineItemOrder().size(); j++) {
224                 try {
225                     oleInvoiceRecord = oleInvoiceRecordBuilder.build(invOrder.get(i).getLineItemOrder().get(j), invOrder.get(i));
226                     oleInvoiceService = getInvoiceService();
227                     oleInvoiceService.setOleBatchProcessProfileBo(this.processDef.getOleBatchProcessProfileBo());
228                     oleInvoiceService.setDefaultAndConstantValuesToInvoiceRecord(oleInvoiceRecord);
229                     String invoiceNumber = (oleInvoiceRecord.getInvoiceNumber() != null && !oleInvoiceRecord.getInvoiceNumber().isEmpty())
230                             ? oleInvoiceRecord.getInvoiceNumber() : "0";
231                     oleInvoiceRecordList = map.get(invoiceNumber);
232                     if (oleInvoiceRecordList == null) {
233                         oleInvoiceRecordList = new ArrayList<OleInvoiceRecord>();
234                     }
235                     oleInvoiceRecordList.add(oleInvoiceRecord);
236                     map.put(invoiceNumber, oleInvoiceRecordList);
237                 } catch (Exception e) {
238                     failureRecords.append("Unable to create Invoice document");
239                     failureRecords.append("\n");
240                     failureRecords.append(e.getMessage());
241                     failureRecords.append("\n");
242                     job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
243                     job.setStatusDesc(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
244                     throw e;
245                 }
246             }
247         }
248     }
249 
250     private void convertXMLToPojos(Map<String, List<OleInvoiceRecord>> map, String marcXMLContent) throws Exception {
251         LOG.info("--------------xml----------" + marcXMLContent);
252         List<OleInvoiceRecord> oleInvoiceRecordList = new ArrayList<OleInvoiceRecord>();
253         try {
254             BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
255             BibMarcRecords bibMarcRecords = bibMarcRecordProcessor.fromXML(marcXMLContent);
256             oleInvoiceService = new OleInvoiceServiceImpl();
257             oleInvoiceService.setOleBatchProcessProfileBo(this.processDef.getOleBatchProcessProfileBo());
258             List<BibMarcRecord> records = bibMarcRecords.getRecords();
259             dataCarrierService.addData("invoiceIngestFailureReason",new ArrayList<>());
260             OLEBatchProcessProfileBo oleBatchProcessProfileBoForBibImport = getBibImportProfile();
261             BatchProcessBibImport batchProcessBibImport = new BatchProcessBibImport();
262             batchProcessBibImport.setOleBatchProcessProfileBo(oleBatchProcessProfileBoForBibImport);
263             OLEBatchBibImportDataObjects oleBatchBibImportDataObjects = new OLEBatchBibImportDataObjects();
264             List<OrderBibMarcRecord> orderBibMarcRecords = oleBatchBibImportDataObjects.processBibImport(records,batchProcessBibImport);
265             if(orderBibMarcRecords != null && orderBibMarcRecords.size() >0){
266                 for(int recCount = 0;recCount < orderBibMarcRecords.size();recCount ++){
267                     // Check for errors in the output of bib import
268                     if(!StringUtils.isBlank(orderBibMarcRecords.get(recCount).getFailureReason())){
269                         failureRecords.append(OLEConstants.OLEBatchProcess.REC_POSITION + (recCount+1) + "  " + OLEConstants.OLEBatchProcess.BIB_IMP_FAILED);
270                         getTitleInformationForFailureRecords(orderBibMarcRecords.get(recCount).getBibMarcRecord());
271                         failureRecords.append(OLEConstants.OLEBatchProcess.REASON + orderBibMarcRecords.get(recCount).getFailureReason() + "\n");
272                     }
273                 }
274             }
275             else {
276                 failureRecords.append(OLEConstants.OLEBatchProcess.BIB_IMP_FAILED);
277             }
278             for (int recordCount = 0; recordCount < records.size(); recordCount++) {
279                 OleInvoiceRecord oleInvoiceRecord = getInvoiceService().populateValuesFromProfile(records.get(recordCount));
280                 String invoiceNumber = (oleInvoiceRecord.getInvoiceNumber() != null && !oleInvoiceRecord.getInvoiceNumber().isEmpty())
281                         ? oleInvoiceRecord.getInvoiceNumber() : "0";
282                 oleInvoiceRecordList = map.get(invoiceNumber);
283                 if (oleInvoiceRecordList == null) {
284                     oleInvoiceRecordList = new ArrayList<OleInvoiceRecord>();
285                 }
286                 oleInvoiceRecordList.add(oleInvoiceRecord);
287                 map.put(invoiceNumber, oleInvoiceRecordList);
288             }
289 
290         } catch (Exception e) {
291             List invoiceIngestFailureReason = (List) dataCarrierService.getData("invoiceIngestFailureReason");
292             if(invoiceIngestFailureReason != null && invoiceIngestFailureReason.size()>0){
293                 for(int failCount = 0;failCount < invoiceIngestFailureReason.size();failCount++){
294                     failureRecords.append(invoiceIngestFailureReason.get(failCount) + "\n");
295                 }
296             }
297             failureRecords.append("Unable to create Invoice document");
298             failureRecords.append("\n");
299             failureRecords.append(e.getMessage());
300             failureRecords.append("\n");
301             job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
302             job.setStatusDesc(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
303             throw e;
304         }
305     }
306 
307     private void getTitleInformationForFailureRecords( BibMarcRecord bibMarcRecord){
308         String title = null;
309         for (DataField dataField : bibMarcRecord.getDataFields()){
310             if (dataField.getTag().equals("245")){
311                 for (SubField subfield : dataField.getSubFields()){
312                     if (subfield.getCode().equals("a")){
313                         title = subfield.getValue();
314                     }
315                 }
316             }
317         }
318         if (title!=null){
319             failureRecords.append("For Title : "+title);
320         }
321     }
322 
323 
324     public void updatePrice(OleInvoiceDocument oleInvoiceDocument) {
325 
326 
327         for (OleInvoiceItem item : (List<OleInvoiceItem>) oleInvoiceDocument.getItems()) {
328             if (item.getItemDiscount() != null) {
329                 if (item.getItemDiscountType() != null && item.getItemDiscountType().equals("%")) {
330                     BigDecimal discount = ((item.getItemListPrice().bigDecimalValue().multiply(item.getItemDiscount().bigDecimalValue()))).divide(new BigDecimal(100));
331                     item.setItemUnitPrice(item.getItemListPrice().bigDecimalValue().subtract(discount));
332                 } else {
333                     item.setItemUnitPrice(((OleInvoiceItem) item).getItemListPrice().bigDecimalValue().subtract(item.getItemDiscount().bigDecimalValue()));
334                 }
335             } else {
336                 item.setItemUnitPrice(((OleInvoiceItem) item).getItemListPrice().bigDecimalValue());
337             }
338             getInvoiceService().calculateAccount(item);
339         }
340     }
341 
342 
343     private OleInvoiceDocument initiateInvoiceDocument(OleInvoiceDocument invoiceDocument, Person currentUser) throws Exception {
344         try {
345             invoiceDocument = (OleInvoiceDocument) SpringContext.getBean(DocumentService.class).getNewDocument("OLE_PRQS");
346         } catch (WorkflowException e) {
347             LOG.error(e, e);
348         }
349         invoiceDocument.initiateDocument();
350 
351         UniversityDateService universityDateService = SpringContext.getBean(UniversityDateService.class);
352         invoiceDocument.setPostingYear(universityDateService.getCurrentUniversityDate().getUniversityFiscalYear());
353         Bank defaultBank = SpringContext.getBean(BankService.class).getDefaultBankByDocType(invoiceDocument.getClass());
354         if (defaultBank != null) {
355             invoiceDocument.setBankCode(defaultBank.getBankCode());
356             invoiceDocument.setBank(defaultBank);
357 
358         }
359         String description = getOlePurapService().getParameter(OLEConstants.INVOICE_IMPORT_INV_DESC);
360         if (LOG.isDebugEnabled()){
361             LOG.debug("Description for invoice import ingest is "+description);
362         }
363         description = getOlePurapService().setDocumentDescription(description,null);
364         invoiceDocument.getDocumentHeader().setDocumentDescription(description);
365         invoiceDocument.setAccountsPayableProcessorIdentifier(currentUser.getPrincipalId());
366         invoiceDocument.setProcessingCampusCode(currentUser.getCampusCode());
367         return invoiceDocument;
368     }
369 
370     private OleVendorAccountInfo populateBFN(String code) {
371         Map matchBFN = new HashMap();
372         matchBFN.put("vendorRefNumber", code);
373         List<OleVendorAccountInfo> oleVendorAccountInfo = (List<OleVendorAccountInfo>) getBusinessObjectService().findMatching(OleVendorAccountInfo.class, matchBFN);
374         return oleVendorAccountInfo != null && oleVendorAccountInfo.size() > 0 ? oleVendorAccountInfo.get(0) : null;
375     }
376 
377     private String populateChartOfAccount(String accountNumber) {
378         Map matchChartCode = new HashMap();
379         matchChartCode.put("accountNumber", accountNumber);
380         List<Account> accountList = (List<Account>) getBusinessObjectService().findMatching(Account.class, matchChartCode);
381         return accountList != null && accountList.size() > 0 ? accountList.get(0).getChartOfAccountsCode() : null;
382     }
383 
384     private HashMap addInvoiceItem(List<OlePurchaseOrderItem> olePurchaseOrderItems, OleInvoiceRecord invoiceRecord, OleInvoiceDocument invoiceDocument, PurchaseOrderDocument purchaseOrderDocument, HashMap itemMap, Set<Integer> poNumbers, Set<Integer> lineNumbers) throws Exception {
385 
386         for (OlePurchaseOrderItem poItem : olePurchaseOrderItems) {
387             if (poItem.getItemTypeCode().equalsIgnoreCase("ITEM")) {
388                 OleInvoiceItem oleInvoiceItem = new OleInvoiceItem();
389                 oleInvoiceItem.setItemTypeCode(poItem.getItemTypeCode());
390                 oleInvoiceItem.setItemType(poItem.getItemType());
391                 oleInvoiceItem.setItemQuantity(new KualiDecimal(invoiceRecord.getQuantity()));
392                 oleInvoiceItem.setItemListPrice(new KualiDecimal(invoiceRecord.getListPrice()));
393                 oleInvoiceItem.setItemDescription(poItem.getItemDescription());      // invoiceRecord.getItemDescription()
394                 oleInvoiceItem.setItemDiscount(invoiceRecord.getLineItemAdditionalCharge() != null ? new KualiDecimal(invoiceRecord.getLineItemAdditionalCharge()) : null);
395                 oleInvoiceItem.setItemDiscountType(invoiceRecord.getDiscountType());
396                 oleInvoiceItem.setItemUnitPrice(new BigDecimal(invoiceRecord.getUnitPrice()));
397                 oleInvoiceItem.setItemTitleId(poItem.getItemTitleId());
398                 if(invoiceRecord.getItemNote()!=null && invoiceRecord.getItemNote().size()>0) {
399                     oleInvoiceItem.setNotes(invoiceRecord.getItemNote());
400                 }
401                 if (invoiceRecord.getLineItemAdditionalCharge() != null) {
402                     oleInvoiceItem.setVendorItemIdentifier(poItem.getVendorItemPoNumber());
403                 }
404                 PurchaseOrderService purchaseOrderService = (PurchaseOrderService) SpringContext.getBean("purchaseOrderService");
405                 purchaseOrderDocument = purchaseOrderService.getPurchaseOrderByDocumentNumber(poItem.getDocumentNumber());
406                 oleInvoiceItem.setPurchaseOrderIdentifier(purchaseOrderDocument.getPurapDocumentIdentifier());
407                 oleInvoiceItem.setItemLineNumber(poItem.getItemLineNumber());
408                 oleInvoiceItem.setItemNoOfParts(poItem.getItemNoOfParts());
409                 oleInvoiceItem.setPoItemIdentifier(poItem.getItemIdentifier());
410                 oleInvoiceItem.setAccountsPayablePurchasingDocumentLinkIdentifier(purchaseOrderDocument.getAccountsPayablePurchasingDocumentLinkIdentifier());
411                 oleInvoiceItem.setOlePoOutstandingQuantity(new KualiInteger(poItem.getOutstandingQuantity().bigDecimalValue()));
412                 //call populate VendorInfo
413                 OleVendorAccountInfo oleVendorAccountInfo = populateBFN(invoiceRecord.getBfnNumber());
414                 List accountingLine = new ArrayList();
415                 if (oleVendorAccountInfo != null && oleVendorAccountInfo.isActive()) {
416 
417                     InvoiceAccount invoiceAccount = new InvoiceAccount();
418                     invoiceAccount.setAccountNumber(oleVendorAccountInfo.getAccountNumber());
419                     invoiceAccount.setFinancialObjectCode(oleVendorAccountInfo.getObjectCode());
420                     invoiceAccount.setAmount(new KualiDecimal(invoiceRecord.getUnitPrice()));
421                     invoiceAccount.setAccountLinePercent(new BigDecimal("100"));  // TODO: Need to get from edifact.
422                     invoiceAccount.setPurapItem(oleInvoiceItem);
423                     invoiceAccount.setItemIdentifier(oleInvoiceItem.getItemIdentifier());
424                     invoiceAccount.setChartOfAccountsCode(populateChartOfAccount(oleVendorAccountInfo.getAccountNumber()) != null ? populateChartOfAccount(oleVendorAccountInfo.getAccountNumber()) : invoiceRecord.getItemChartCode());     // TODO: Need to get chart of Account based on account number and object code.
425                     accountingLine.add(invoiceAccount);
426 
427                 } else {
428                     for (PurApAccountingLine poa : poItem.getSourceAccountingLines()) {
429                         InvoiceAccount invoiceAccount = new InvoiceAccount(oleInvoiceItem, (PurchaseOrderAccount) poa);
430                         invoiceAccount.setAccountNumber(!StringUtils.isBlank(invoiceRecord.getAccountNumber()) ? invoiceRecord.getAccountNumber() : invoiceAccount.getAccountNumber());
431                         invoiceAccount.setFinancialObjectCode(!StringUtils.isBlank(invoiceRecord.getObjectCode()) ? invoiceRecord.getObjectCode()  : invoiceAccount.getFinancialObjectCode());
432                         accountingLine.add(invoiceAccount);
433                     }
434                 }
435                 oleInvoiceItem.setSourceAccountingLines(accountingLine);
436                 oleInvoiceItem.setPostingYear(poItem.getPurchaseOrder().getPostingYear());
437                 SimpleDateFormat dateFromRawFile = new SimpleDateFormat(org.kuali.ole.OLEConstants.DATE_FORMAT);
438                 oleInvoiceItem.setSubscriptionFromDate(invoiceRecord.getSubscriptionPeriodFrom()!= null ? new java.sql.Date(dateFromRawFile.parse(invoiceRecord.getSubscriptionPeriodFrom()).getTime()):null);
439                 oleInvoiceItem.setSubscriptionToDate(invoiceRecord.getSubscriptionPeriodTo()!= null ? new java.sql.Date(dateFromRawFile.parse(invoiceRecord.getSubscriptionPeriodTo()).getTime()):null);
440                 if(!StringUtils.isBlank(invoiceDocument.getInvoiceCurrencyType())){
441                     invoiceDocument.setInvoiceCurrencyTypeId(new Long(invoiceDocument.getInvoiceCurrencyType()));
442                     String currencyType = getInvoiceService().getCurrencyType(invoiceDocument.getInvoiceCurrencyType());
443                     if (StringUtils.isNotBlank(currencyType)) {
444                         if (!currencyType.equalsIgnoreCase(OleSelectConstant.CURRENCY_TYPE_NAME)) {
445                             setItemForeignDetails(oleInvoiceItem,invoiceDocument.getInvoiceCurrencyType(),invoiceRecord);
446                         }
447                     }
448                 }
449 
450                 if (lineNumbers.add(oleInvoiceItem.getItemLineNumber())) {
451                     invoiceDocument.getItems().add(oleInvoiceItem);
452                     poNumbers.add(oleInvoiceItem.getPurchaseOrderIdentifier());
453                 } else if (poNumbers.add(oleInvoiceItem.getPurchaseOrderIdentifier())) {
454                     invoiceDocument.getItems().add(oleInvoiceItem);
455                 }
456 
457             }
458         }
459 
460         itemMap.put("invoiceDocument", invoiceDocument);
461         itemMap.put("purchaseOrderDocument", purchaseOrderDocument);
462         return itemMap;
463     }
464 
465     private HashMap createInvoiceItem(int lineItemCount, OleInvoiceRecord invoiceRecord, OleInvoiceDocument invoiceDocument
466             , HashMap itemMap) throws Exception {
467 
468         OleInvoiceItem oleInvoiceItem = new OleInvoiceItem();
469         validateInvoiceRecordValues(invoiceRecord);
470         oleInvoiceItem.setItemQuantity(new KualiDecimal(invoiceRecord.getQuantity()));
471         oleInvoiceItem.setItemListPrice(new KualiDecimal(invoiceRecord.getListPrice()));
472         oleInvoiceItem.setItemDescription(invoiceRecord.getItemDescription()); // invoiceRecord.getItemDescription()
473         if(invoiceRecord.getItemNote()!=null && invoiceRecord.getItemNote().size()>0) {
474             oleInvoiceItem.setNotes(invoiceRecord.getItemNote());
475         }
476         /*if (invoiceRecord.getAdditionalChargeCode() != null && invoiceRecord.getAdditionalChargeCode().equalsIgnoreCase("SVC")) {
477             oleInvoiceItem.setItemUnitPrice(new BigDecimal(invoiceRecord.getAdditionalCharge() != null ? invoiceRecord.getAdditionalCharge() : invoiceRecord.getAdditionalCharge()));
478             oleInvoiceItem.setItemTypeCode("SPHD");
479         } *//*else if (invoiceRecord.getLineItemAdditionalChargeCode() != null && invoiceRecord.getLineItemAdditionalChargeCode().equalsIgnoreCase("LD") && item.getItemTypeCode().equalsIgnoreCase("ITEM")) {
480             oleInvoiceItem.
481             oleInvoiceItem.setItemDiscountType("#"); // % or #
482             oleInvoiceItem.setItemDiscount(new KualiDecimal(invoiceRecord.getLineItemAdditionalCharge() != null ? invoiceRecord.getLineItemAdditionalCharge() : invoiceRecord.getLineItemAdditionalCharge()));
483         }*/
484         //else {
485         if (invoiceRecord.getUnitPrice() != null) {
486             oleInvoiceItem.setItemUnitPrice(new BigDecimal(invoiceRecord.getUnitPrice()));
487         }
488         oleInvoiceItem.setItemTypeCode("ITEM");
489         oleInvoiceItem.setItemDiscountType("%"); // % or #
490         oleInvoiceItem.setItemDiscount(new KualiDecimal(invoiceRecord.getLineItemAdditionalCharge() != null ? invoiceRecord.getLineItemAdditionalCharge() : invoiceRecord.getLineItemAdditionalCharge()));
491         //}
492         oleInvoiceItem.setItemTitleId(null);
493         if (invoiceRecord.getLineItemAdditionalCharge() != null) {
494             oleInvoiceItem.setVendorItemIdentifier(null);
495         }
496 
497         //PurchaseOrderService purchaseOrderService = (PurchaseOrderService) SpringContext.getBean("purchaseOrderService");
498         // purchaseOrderDocument = purchaseOrderService.getPurchaseOrderByDocumentNumber(poItem.getDocumentNumber());
499         oleInvoiceItem.setPurchaseOrderIdentifier(null);
500         oleInvoiceItem.setItemLineNumber(lineItemCount);
501         oleInvoiceItem.setItemNoOfParts(new KualiInteger("1"));
502         oleInvoiceItem.setPoItemIdentifier(null);
503         oleInvoiceItem.setAccountsPayablePurchasingDocumentLinkIdentifier((SpringContext.getBean(SequenceAccessorService.class).
504                 getNextAvailableSequenceNumber("AP_PUR_DOC_LNK_ID")).intValue());
505         oleInvoiceItem.setOlePoOutstandingQuantity(KualiInteger.ZERO);
506         //call populate VendorInfo
507         OleVendorAccountInfo oleVendorAccountInfo = populateBFN(invoiceRecord.getBfnNumber());
508         List accountingLine = new ArrayList();
509         if (oleVendorAccountInfo != null && oleVendorAccountInfo.isActive()) {
510 
511             InvoiceAccount invoiceAccount = new InvoiceAccount();
512             invoiceAccount.setAccountNumber(oleVendorAccountInfo.getAccountNumber());
513             invoiceAccount.setFinancialObjectCode(oleVendorAccountInfo.getObjectCode());
514             invoiceAccount.setAmount(new KualiDecimal(invoiceRecord.getUnitPrice()));
515             invoiceAccount.setAccountLinePercent(new BigDecimal("100"));  // TODO: Need to get from edifact.
516             invoiceAccount.setPurapItem(oleInvoiceItem);
517             invoiceAccount.setItemIdentifier(oleInvoiceItem.getItemIdentifier());
518             invoiceAccount.setChartOfAccountsCode(populateChartOfAccount(oleVendorAccountInfo.getAccountNumber()) != null ? populateChartOfAccount(oleVendorAccountInfo.getAccountNumber()) : invoiceRecord.getItemChartCode());     // TODO: Need to get chart of Account based on account number and object code.
519             accountingLine.add(invoiceAccount);
520 
521         } else if (invoiceRecord.getAccountNumber() != null && invoiceRecord.getObjectCode() != null) {
522             InvoiceAccount invoiceAccount = new InvoiceAccount();
523             invoiceAccount.setAccountNumber(invoiceRecord.getAccountNumber());
524             invoiceAccount.setFinancialObjectCode(invoiceRecord.getObjectCode());
525             invoiceAccount.setAmount(new KualiDecimal(invoiceRecord.getUnitPrice()));
526             invoiceAccount.setAccountLinePercent(new BigDecimal("100"));  // TODO: Need to get from edifact.
527             invoiceAccount.setPurapItem(oleInvoiceItem);
528             invoiceAccount.setItemIdentifier(oleInvoiceItem.getItemIdentifier());
529             invoiceAccount.setChartOfAccountsCode(populateChartOfAccount(oleVendorAccountInfo.getAccountNumber()) != null ? populateChartOfAccount(oleVendorAccountInfo.getAccountNumber()) : invoiceRecord.getItemChartCode());     // TODO: Need to get chart of Account based on account number and object code.
530             accountingLine.add(invoiceAccount);
531 
532         }
533 
534         oleInvoiceItem.setSourceAccountingLines(accountingLine);
535         oleInvoiceItem.setPostingYear(SpringContext.getBean(UniversityDateService.class).getCurrentUniversityDate().getUniversityFiscalYear());
536         SimpleDateFormat dateFromRawFile = new SimpleDateFormat(org.kuali.ole.OLEConstants.DATE_FORMAT);
537         oleInvoiceItem.setSubscriptionFromDate(invoiceRecord.getSubscriptionPeriodFrom()!= null ? new java.sql.Date(dateFromRawFile.parse(invoiceRecord.getSubscriptionPeriodFrom()).getTime()):null);
538         oleInvoiceItem.setSubscriptionToDate(invoiceRecord.getSubscriptionPeriodTo()!= null ? new java.sql.Date(dateFromRawFile.parse(invoiceRecord.getSubscriptionPeriodTo()).getTime()):null);
539         if(!StringUtils.isBlank(invoiceDocument.getInvoiceCurrencyType())){
540             invoiceDocument.setInvoiceCurrencyTypeId(new Long(invoiceDocument.getInvoiceCurrencyType()));
541             String currencyType = getInvoiceService().getCurrencyType(invoiceDocument.getInvoiceCurrencyType());
542             if (StringUtils.isNotBlank(currencyType)) {
543                 if (!currencyType.equalsIgnoreCase(OleSelectConstant.CURRENCY_TYPE_NAME)) {
544                     setItemForeignDetails(oleInvoiceItem,invoiceDocument.getInvoiceCurrencyType(),invoiceRecord);
545                 }
546             }
547         }
548         invoiceDocument.getItems().add(oleInvoiceItem);
549 
550         /* if (invoiceRecord.getAdditionalChargeCode() != null && invoiceRecord.getAdditionalChargeCode().equalsIgnoreCase("SVC") && item.getItemTypeCode().equalsIgnoreCase("SPHD")) {
551                     item.setItemUnitPrice(new BigDecimal(invoiceRecord.getAdditionalCharge() != null ? invoiceRecord.getAdditionalCharge() : invoiceRecord.getAdditionalCharge()));
552                     item.setPurchaseOrderIdentifier(purchaseOrderDocument != null && purchaseOrderDocument.getPurapDocumentIdentifier() != null ?
553                             purchaseOrderDocument.getPurapDocumentIdentifier() : null);
554                 } else if (invoiceRecord.getLineItemAdditionalChargeCode() != null && invoiceRecord.getLineItemAdditionalChargeCode().equalsIgnoreCase("LD") && item.getItemTypeCode().equalsIgnoreCase("ITEM")) {
555 
556                     if ((item.getItemDescription().contains(invoiceRecord.getISBN())
557                             || (invoiceRecord.getVendorItemIdentifier() != null ? invoiceRecord.getVendorItemIdentifier().equalsIgnoreCase(olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 ? item.getVendorItemIdentifier() : "") : false)
558                             || (invoiceRecord.getPurchaseOrderNumber() != null ? invoiceRecord.getPurchaseOrderNumber().equals(item.getPurchaseOrderDocument() != null ? item.getPurchaseOrderDocument().getPurapDocumentIdentifier(): null):false))) {
559 
560 
561                         item.setItemDiscountType("#"); // % or #
562                         item.setItemDiscount(new KualiDecimal(invoiceRecord.getLineItemAdditionalCharge() != null ? invoiceRecord.getLineItemAdditionalCharge() : invoiceRecord.getLineItemAdditionalCharge()));
563                     }
564                 }
565         */
566 
567         itemMap.put("invoiceDocument", invoiceDocument);
568         return itemMap;
569     }
570 
571     private HashMap createInvoiceItem(OleInvoiceRecord invoiceRecord, OleInvoiceDocument invoiceDocument
572             , HashMap itemMap) throws Exception {
573         OleInvoiceItem oleInvoiceItem = new OleInvoiceItem();
574         oleInvoiceItem.setItemQuantity(new KualiDecimal(invoiceRecord.getQuantity()));
575         oleInvoiceItem.setItemListPrice(new KualiDecimal(invoiceRecord.getListPrice()));
576         oleInvoiceItem.setItemUnitPrice(new BigDecimal(invoiceRecord.getUnitPrice()));
577         oleInvoiceItem.setItemTypeCode("ITEM");
578         oleInvoiceItem.setItemDiscountType("%");
579         oleInvoiceItem.setItemDiscount(new KualiDecimal(invoiceRecord.getLineItemAdditionalCharge() != null ? invoiceRecord.getLineItemAdditionalCharge() : invoiceRecord.getLineItemAdditionalCharge()));
580         oleInvoiceItem.setItemTitleId(invoiceRecord.getItemTitleIdForMRC());
581         oleInvoiceItem.setItemDescription(invoiceRecord.getItemDescription());
582         if (invoiceRecord.getLineItemAdditionalCharge() != null) {
583             oleInvoiceItem.setVendorItemIdentifier(null);
584         }
585         oleInvoiceItem.setPurchaseOrderIdentifier(null);
586         oleInvoiceItem.setItemNoOfParts(new KualiInteger("1"));
587         oleInvoiceItem.setPoItemIdentifier(null);
588         oleInvoiceItem.setAccountsPayablePurchasingDocumentLinkIdentifier((SpringContext.getBean(SequenceAccessorService.class).
589                 getNextAvailableSequenceNumber("AP_PUR_DOC_LNK_ID")).intValue());
590         oleInvoiceItem.setOlePoOutstandingQuantity(KualiInteger.ZERO);
591         List accountingLine = new ArrayList();
592         if (invoiceRecord.getAccountNumber() != null && invoiceRecord.getObjectCode() != null) {
593             InvoiceAccount invoiceAccount = new InvoiceAccount();
594             invoiceAccount.setAccountNumber(invoiceRecord.getAccountNumber());
595             invoiceAccount.setFinancialObjectCode(invoiceRecord.getObjectCode());
596             invoiceAccount.setAmount(new KualiDecimal(invoiceRecord.getUnitPrice()));
597             invoiceAccount.setAccountLinePercent(new BigDecimal("100"));
598             invoiceAccount.setPurapItem(oleInvoiceItem);
599             invoiceAccount.setItemIdentifier(oleInvoiceItem.getItemIdentifier());
600             invoiceAccount.setChartOfAccountsCode(populateChartOfAccount(invoiceRecord.getAccountNumber()));
601             if(checkForValidObjectCode(invoiceRecord,invoiceAccount)){
602                 accountingLine.add(invoiceAccount);
603             }
604         }
605         oleInvoiceItem.setSourceAccountingLines(accountingLine);
606         oleInvoiceItem.setPostingYear(SpringContext.getBean(UniversityDateService.class).getCurrentUniversityDate().getUniversityFiscalYear());
607         SimpleDateFormat dateFromRawFile = new SimpleDateFormat(org.kuali.ole.OLEConstants.DATE_FORMAT);
608         oleInvoiceItem.setSubscriptionFromDate(invoiceRecord.getSubscriptionPeriodFrom()!= null ? new java.sql.Date(dateFromRawFile.parse(invoiceRecord.getSubscriptionPeriodFrom()).getTime()):null);
609         oleInvoiceItem.setSubscriptionToDate(invoiceRecord.getSubscriptionPeriodTo()!= null ? new java.sql.Date(dateFromRawFile.parse(invoiceRecord.getSubscriptionPeriodTo()).getTime()):null);
610         if(!StringUtils.isBlank(invoiceDocument.getInvoiceCurrencyType())){
611             invoiceDocument.setInvoiceCurrencyTypeId(new Long(invoiceDocument.getInvoiceCurrencyType()));
612             String currencyType = getInvoiceService().getCurrencyType(invoiceDocument.getInvoiceCurrencyType());
613             if (StringUtils.isNotBlank(currencyType)) {
614                 if (!currencyType.equalsIgnoreCase(OleSelectConstant.CURRENCY_TYPE_NAME)) {
615                     setItemForeignDetails(oleInvoiceItem,invoiceDocument.getInvoiceCurrencyType(),invoiceRecord);
616                 }
617             }
618         }
619         invoiceDocument.getItems().add(oleInvoiceItem);
620         itemMap.put("invoiceDocument", invoiceDocument);
621         return itemMap;
622     }
623 
624     private void setItemForeignDetails(OleInvoiceItem oleInvoiceItem,String invoiceCurrencyType, OleInvoiceRecord invoiceRecord){
625          KualiDecimal foreignListPrice = new KualiDecimal(invoiceRecord.getForeignListPrice());
626         oleInvoiceItem.setItemForeignListPrice(new KualiDecimal(invoiceRecord.getForeignListPrice()));
627         oleInvoiceItem.setItemForeignDiscount(oleInvoiceItem.getItemDiscount() == null ? new KualiDecimal(0.0) : oleInvoiceItem.getItemDiscount());
628         oleInvoiceItem.setItemForeignDiscountType(oleInvoiceItem.getItemDiscountType() != null ? oleInvoiceItem.getItemDiscountType() : "%");
629         if(oleInvoiceItem.getItemForeignDiscountType().equalsIgnoreCase("%")){
630             BigDecimal discount = ((new BigDecimal(String.valueOf(foreignListPrice)).multiply(new BigDecimal(oleInvoiceItem.getForeignDiscount())))).divide(new BigDecimal(100));
631             oleInvoiceItem.setItemForeignUnitCost(new KualiDecimal(new BigDecimal(String.valueOf(foreignListPrice)).subtract(discount)));
632         }
633         else {
634             oleInvoiceItem.setItemForeignUnitCost(new KualiDecimal(new BigDecimal(String.valueOf(foreignListPrice)).subtract(new BigDecimal(oleInvoiceItem.getForeignDiscount()))));
635         }
636         oleInvoiceItem.setItemExchangeRate(new KualiDecimal(getInvoiceService().getExchangeRate(invoiceCurrencyType).getExchangeRate()));
637         if(StringUtils.isNotBlank(invoiceRecord.getInvoiceCurrencyExchangeRate())){
638            oleInvoiceItem.setItemUnitCostUSD(new KualiDecimal(oleInvoiceItem.getItemForeignUnitCost().bigDecimalValue().divide(new BigDecimal(invoiceRecord.getInvoiceCurrencyExchangeRate()), 4, BigDecimal.ROUND_HALF_UP)));
639         }else{
640            oleInvoiceItem.setItemUnitCostUSD(new KualiDecimal(oleInvoiceItem.getItemForeignUnitCost().bigDecimalValue().divide(oleInvoiceItem.getItemExchangeRate().bigDecimalValue(), 4, BigDecimal.ROUND_HALF_UP)));
641         }
642         oleInvoiceItem.setItemUnitPrice(oleInvoiceItem.getItemUnitCostUSD().bigDecimalValue());
643         oleInvoiceItem.setItemListPrice(oleInvoiceItem.getItemUnitCostUSD());
644     }
645 
646     private boolean checkForValidObjectCode(OleInvoiceRecord oleInvoiceRecord,InvoiceAccount invoiceAccount){
647         Map<String,String> chartCodeMap = new HashMap<>();
648         chartCodeMap.put("chartOfAccountsCode",invoiceAccount.getChartOfAccountsCode());
649         List<ObjectCode> objectCodeList = (List<ObjectCode>) getBusinessObjectService().findMatching(ObjectCode.class, chartCodeMap);
650         if(objectCodeList != null && objectCodeList.size() > 0){
651             for(int recCount = 0;recCount < objectCodeList.size();recCount++){
652                 if(oleInvoiceRecord.getObjectCode().equalsIgnoreCase(objectCodeList.get(recCount).getFinancialObjectCode())){
653                     return true;
654                 }
655             }
656         }
657         return false;
658     }
659 
660     private void validateInvoiceRecordValues(OleInvoiceRecord invoiceRecord) throws Exception {
661         boolean validQuantity = validateForNumber(invoiceRecord.getQuantity());
662         if(!validQuantity){
663             failureRecords.append(OLEConstants.INVALID_QTY + "  "+ invoiceRecord.getQuantity()+"\n" );
664         }
665         boolean validListPrice = validateDestinationFieldValues(invoiceRecord.getListPrice());
666         if(!validListPrice){
667             failureRecords.append(OLEConstants.INVALID_LIST_PRICE + "  " + invoiceRecord.getListPrice()+"\n");
668         }
669         boolean validVendorNumber = validateVendorNumber(invoiceRecord.getVendorNumber());
670         if(!validVendorNumber){
671             failureRecords.append(org.kuali.ole.OLEConstants.INVALID_VENDOR_NUMBER + "  "+ invoiceRecord.getVendorNumber() + "\n");
672         }
673         if(!StringUtils.isBlank(failureRecords.toString())){
674             createBatchErrorAttachmentFile(failureRecords.toString());
675         }
676     }
677 
678     @Override
679     public void prepareForWrite() throws Exception {
680         ConfigurationService kualiConfiguration = SpringContext.getBean(ConfigurationService.class);
681         GlobalVariables.setUserSession(new UserSession(processDef.getUser()));
682         Set<Integer> lineNumbers = new TreeSet<>();
683         Set<Integer> poNumbers = new TreeSet<>();
684         OleInvoiceDocument invoiceDocument = null;
685         OleInvoiceRecord invoiceRecord = null;
686         List<OleInvoiceDocument> allInvoiceDocument = new ArrayList<>();
687         OleInvoiceRecordHandler oleInvoiceRecordHandler = new OleInvoiceRecordHandler();
688         Map<String, List<OleInvoiceRecord>> map = new HashMap();
689         try {
690             if (this.processDef.getUploadFileName().endsWith(".mrc")) {
691                 convertXMLToPojos(map, marcXMLContent);
692             } else {
693                 convertXMLToPojos(map);
694             }
695         } catch (Exception e) {
696             createBatchErrorAttachmentFile(failureRecords.append(e.getMessage()).toString());
697             LOG.error(e, e);
698             job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
699             job.setStatusDesc(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
700             throw e;
701         }
702         oleInvoiceRecordHandler.setOleInvoiceLineItemRecords(map);
703         int totalInvoiceFileCount = 0;
704         // int totalInvoiceProcessedCount = 0;
705         int successCount = 0;
706         for (Map.Entry invoiceRecordList : oleInvoiceRecordHandler.getOleInvoiceLineItemRecords().entrySet()) {
707              List<OleInvoiceRecord> oleInvoiceRecord = (List<OleInvoiceRecord>)invoiceRecordList.getValue();
708             totalInvoiceFileCount = oleInvoiceRecordHandler.getOleInvoiceLineItemRecords().size();
709 
710             Person currentUser = GlobalVariables.getUserSession().getPerson();
711 
712             try {
713                 invoiceDocument = initiateInvoiceDocument(invoiceDocument, currentUser);
714             } catch (Exception e) {
715                 LOG.error(e, e);
716                 failureRecords.append("Unable to create Invoice document");
717                 failureRecords.append("\n");
718                 createBatchErrorAttachmentFile(failureRecords.append(e.getMessage()).toString());
719                 //job.setStatusDesc(OLEConstants.OLEBatchProcess.JOB_STATUS_STOPPED);
720             }
721             //Map<Integer, List<OleInvoiceRecord>> oleInvoiceRecord = oleInvoiceRecordHandler.getOleInvoiceLineItemRecords();
722             //List<OleInvoiceRecord> oleInvoiceRecordList = oleInvoiceRecord.get(0);
723             SimpleDateFormat dateFromRawFile = new SimpleDateFormat("yyyyMMdd");
724             OleInvoiceService oleInvoiceService = getInvoiceService();
725            // if (oleInvoiceRecordList != null && !oleInvoiceRecordList.isEmpty()) {
726                 OleInvoiceRecord invoiceRecordHeader = oleInvoiceRecord.get(0);
727                 invoiceDocument.setInvoiceNumber(invoiceRecordHeader.getInvoiceNumber());
728                 //invoiceDocument.setVendorInvoiceAmount(new KualiDecimal(invoiceRecordHeader.getVendorInvoiceAmount()));
729                 invoiceDocument.setVendorInvoiceAmount(null);
730                 Date rawDate = null;
731                 try {
732                     rawDate = dateFromRawFile.parse(invoiceRecordHeader.getInvoiceDate());
733                 } catch (Exception e) {
734                     LOG.error(e, e);
735                     failureRecords.append("Invoice Date: " + invoiceRecordHeader.getInvoiceDate());
736                     failureRecords.append("\n");
737                     failureRecords.append("Cannot parse Invoice Date" + e);
738                     createBatchErrorAttachmentFile(failureRecords.toString());
739                     throw e;
740 
741                 }
742                 if (rawDate != null) {
743                     invoiceDocument.setInvoiceDate(new java.sql.Date(rawDate.getTime()));
744                 }
745             //}
746             int lineItemCount = 0;
747             for (int j = 0; j < oleInvoiceRecord.size(); j++) {
748                 invoiceRecord = oleInvoiceRecord.get(j);
749                     String[] vendorIds = invoiceRecord.getVendorNumber() != null ? invoiceRecord.getVendorNumber().split("-") : new String[0];
750                     if(!StringUtils.isBlank(invoiceRecord.getCurrencyTypeId())){
751                         setDocumentForeignDetails(invoiceDocument,invoiceRecord);
752                     }
753                     else {
754                         Map vendorDetailMap = new HashMap();
755                         vendorDetailMap.put("vendorHeaderGeneratedIdentifier", vendorIds.length > 0 ? vendorIds[0] : "");
756                         vendorDetailMap.put("vendorDetailAssignedIdentifier", vendorIds.length > 1 ? vendorIds[1] : "");
757                         VendorDetail vendorDetail = (VendorDetail) getBusinessObjectService().findByPrimaryKey(VendorDetail.class, vendorDetailMap);
758                         if(vendorDetail != null){
759                             invoiceRecord.setCurrencyTypeId(vendorDetail.getCurrencyTypeId().toString());
760                             setDocumentForeignDetails(invoiceDocument,invoiceRecord);
761                         }
762                     }
763                     List<OlePurchaseOrderItem> olePurchaseOrderItems = null;
764                     Map vendorItemIdentifier = new HashMap();
765                     vendorItemIdentifier.put("vendorItemPoNumber", invoiceRecord.getVendorItemIdentifier());
766                     vendorItemIdentifier.put("purchaseOrder.vendorHeaderGeneratedIdentifier", vendorIds.length > 0 ? vendorIds[0] : "");
767                     vendorItemIdentifier.put("purchaseOrder.vendorDetailAssignedIdentifier", vendorIds.length > 1 ? vendorIds[1] : "");
768                     List<OlePurchaseOrderItem> dummyPurchaseOrderItems = null;
769                     if (invoiceRecord.getVendorItemIdentifier() != null) {
770                         dummyPurchaseOrderItems = (List<OlePurchaseOrderItem>) getBusinessObjectService().findMatching(OlePurchaseOrderItem.class, vendorItemIdentifier);
771                         Collections.sort(dummyPurchaseOrderItems,new Comparator<OlePurchaseOrderItem>(){
772                             public int compare(OlePurchaseOrderItem dummyPurchaseOrderItems1,OlePurchaseOrderItem dummyPurchaseOrderItems2){
773                                 return dummyPurchaseOrderItems2.getDocumentNumber().compareTo(dummyPurchaseOrderItems1.getDocumentNumber());
774                             }
775                         });
776                         if (dummyPurchaseOrderItems != null && dummyPurchaseOrderItems.size() > 0) {
777                             String documentNumber = dummyPurchaseOrderItems.get(0).getDocumentNumber();
778                             olePurchaseOrderItems = new ArrayList<>();
779                             for(int itemCount = 0;itemCount < dummyPurchaseOrderItems.size();itemCount++){
780                                 if(documentNumber.equalsIgnoreCase(dummyPurchaseOrderItems.get(itemCount).getDocumentNumber())){
781                                     olePurchaseOrderItems.add(dummyPurchaseOrderItems.get(itemCount));
782                                 }
783                             }
784                         }
785                     }
786                     PurchaseOrderDocument purchaseOrderDocument = null;
787                     HashMap itemMap = new HashMap();
788                     if (olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0) {
789 
790                         invoiceRecord.setPurchaseOrderNumber((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0) ? olePurchaseOrderItems.get(0).getPurapDocumentIdentifier() : null);
791                         if ((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 && olePurchaseOrderItems.get(0) != null && olePurchaseOrderItems.get(0).getPurchaseOrder() != null && olePurchaseOrderItems.get(0).getPurchaseOrder().getPostingYear() != null)) {
792                             invoiceDocument.setPostingYear((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0) ? olePurchaseOrderItems.get(0).getPurchaseOrder().getPostingYear() : null);
793                         }
794                         //invoiceDocument.setProrateBy(org.kuali.ole.sys.OLEConstants.MANUAL_PRORATE);
795                         invoiceDocument.setInvoicePayDate(SpringContext.getBean(InvoiceService.class).calculatePayDate(invoiceDocument.getInvoiceDate(), invoiceDocument.getVendorPaymentTerms()));
796                         itemMap = addInvoiceItem(olePurchaseOrderItems, invoiceRecord, invoiceDocument, purchaseOrderDocument, itemMap,lineNumbers,poNumbers);
797                         if (itemMap != null) {
798                             invoiceDocument = (OleInvoiceDocument) itemMap.get("invoiceDocument");
799                             purchaseOrderDocument = (PurchaseOrderDocument) itemMap.get("purchaseOrderDocument");
800                         }
801                         //totalInvoiceProcessedCount = i+1;
802                     } else if (olePurchaseOrderItems == null || olePurchaseOrderItems.size() < 1) {
803                         if (invoiceRecord.getVendorNumber() != null && !invoiceRecord.getVendorNumber().isEmpty()) {
804                             if ((invoiceRecord.getISBN() != null && !invoiceRecord.getISBN().isEmpty() ||
805                                     (invoiceRecord.getISSN() != null && !invoiceRecord.getISSN().isEmpty()))) {
806                                 org.kuali.ole.docstore.common.search.SearchParams search_Params = new org.kuali.ole.docstore.common.search.SearchParams();
807                                 SearchResponse searchResponse = null;
808                                 // Retrieve bib id through solr query
809                                 //StringBuffer query = new StringBuffer("");
810                                 //List<HashMap<String, Object>> documentList1 = null;
811                                 //query.append("(ISBN_search:" + invoiceRecord.getISBN() + ")");
812                                 if  (invoiceRecord.getISBN() != null && !invoiceRecord.getISBN().isEmpty()) {
813                                     search_Params.getSearchConditions().add(search_Params.buildSearchCondition("", search_Params.buildSearchField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(),"common_identifier_search", invoiceRecord.getISBN()), ""));
814                                 }
815                                 if (invoiceRecord.getISSN() != null && !invoiceRecord.getISSN().isEmpty()) {
816                                     search_Params.getSearchConditions().add(search_Params.buildSearchCondition("", search_Params.buildSearchField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(),"common_identifier_search", invoiceRecord.getISSN()), ""));
817                                 }
818                                 search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), "id"));
819                                 String titleId = null;
820                                 searchResponse = getDocstoreClientLocator().getDocstoreClient().search(search_Params);
821                                 if (searchResponse.getSearchResults() != null && searchResponse.getSearchResults().size() > 0) {
822                                     titleId = searchResponse.getSearchResults().get(0).getSearchResultFields().get(0).getFieldValue() != null ? searchResponse.getSearchResults().get(0).getSearchResultFields().get(0).getFieldValue() : "";
823                                 }
824 
825 
826                                 //documentList1 = QueryServiceImpl.getInstance().retriveResults(query.toString());
827                                 //Map map1 = documentList1 != null && documentList1.size() > 0 ? documentList1.get(0) : null;
828 
829                                 //String titleId = map1 != null ? (String) map1.get("id") : null;
830 
831                                 Map itemTitleIdMap = new HashMap();
832                                 itemTitleIdMap.put("itemTitleId", titleId);
833                                 itemTitleIdMap.put("purchaseOrder.vendorHeaderGeneratedIdentifier", vendorIds.length > 0 ? vendorIds[0] : "");
834                                 itemTitleIdMap.put("purchaseOrder.vendorDetailAssignedIdentifier", vendorIds.length > 1 ? vendorIds[1] : "");
835                                 if (titleId != null) {
836                                     olePurchaseOrderItems = (List<OlePurchaseOrderItem>) getBusinessObjectService().findMatching(OlePurchaseOrderItem.class, itemTitleIdMap);
837 
838                                     invoiceRecord.setPurchaseOrderNumber((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 && olePurchaseOrderItems.get(0) != null) ? olePurchaseOrderItems.get(0).getPurapDocumentIdentifier() : null);
839                                     if ((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 && olePurchaseOrderItems.get(0) != null && olePurchaseOrderItems.get(0).getPurchaseOrder() != null && olePurchaseOrderItems.get(0).getPurchaseOrder().getPostingYear() != null)) {
840                                         invoiceDocument.setPostingYear(olePurchaseOrderItems.get(0).getPurchaseOrder().getPostingYear());
841                                     }
842                                     //invoiceDocument.setProrateBy(org.kuali.ole.sys.OLEConstants.MANUAL_PRORATE);
843                                     invoiceDocument.setInvoicePayDate(SpringContext.getBean(InvoiceService.class).calculatePayDate(invoiceDocument.getInvoiceDate(), invoiceDocument.getVendorPaymentTerms()));
844                                     itemMap = addInvoiceItem(olePurchaseOrderItems, invoiceRecord, invoiceDocument, purchaseOrderDocument, itemMap,lineNumbers,poNumbers);
845                                     if (itemMap != null) {
846                                         invoiceDocument = (OleInvoiceDocument) itemMap.get("invoiceDocument");
847                                         purchaseOrderDocument = (PurchaseOrderDocument) itemMap.get("purchaseOrderDocument");
848                                     }
849                                     // totalInvoiceProcessedCount = i+1;
850                                 } else if (olePurchaseOrderItems == null || olePurchaseOrderItems.size() < 1) {
851 
852                                     String[] vendorId = invoiceRecord.getVendorNumber() != null ? invoiceRecord.getVendorNumber().split("-") : new String[0];
853                                     Map itemTitleId = new HashMap();
854                                     itemTitleId.put("purchaseOrder.purapDocumentIdentifier", invoiceRecord.getPurchaseOrderNumber());
855                                     itemTitleId.put("purchaseOrder.vendorHeaderGeneratedIdentifier", vendorId.length > 0 ? vendorId[0] : "");
856                                     itemTitleId.put("purchaseOrder.vendorDetailAssignedIdentifier", vendorId.length > 1 ? vendorId[1] : "");
857                                     if (invoiceRecord.getPurchaseOrderNumber() != null) {
858                                         olePurchaseOrderItems = (List<OlePurchaseOrderItem>) getBusinessObjectService().findMatching(OlePurchaseOrderItem.class, itemTitleId);
859                                         if (olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0) {
860                                             invoiceRecord.setPurchaseOrderNumber((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 && olePurchaseOrderItems.get(0) != null) ? olePurchaseOrderItems.get(0).getPurapDocumentIdentifier() : null);
861                                             if ((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 && olePurchaseOrderItems.get(0) != null && olePurchaseOrderItems.get(0).getPurchaseOrder() != null && olePurchaseOrderItems.get(0).getPurchaseOrder().getPostingYear() != null)) {
862                                                 invoiceDocument.setPostingYear(olePurchaseOrderItems.get(0).getPurchaseOrder().getPostingYear());
863                                             }
864                                             //invoiceDocument.setProrateBy(org.kuali.ole.sys.OLEConstants.MANUAL_PRORATE);
865                                             invoiceDocument.setInvoicePayDate(SpringContext.getBean(InvoiceService.class).calculatePayDate(invoiceDocument.getInvoiceDate(), invoiceDocument.getVendorPaymentTerms()));
866 
867                                             itemMap = addInvoiceItem(olePurchaseOrderItems, invoiceRecord, invoiceDocument, purchaseOrderDocument, itemMap,lineNumbers,poNumbers);
868                                             if (itemMap != null) {
869                                                 invoiceDocument = (OleInvoiceDocument) itemMap.get("invoiceDocument");
870                                                 purchaseOrderDocument = (PurchaseOrderDocument) itemMap.get("purchaseOrderDocument");
871                                             }
872                                             // totalInvoiceProcessedCount = i+1;
873                                         }
874                                     }
875                                 }
876                             }
877                             else if (invoiceRecord.getPurchaseOrderNumber() != null) {
878                                 String[] vendorId = invoiceRecord.getVendorNumber() != null ? invoiceRecord.getVendorNumber().split("-") : new String[0];
879                                 Map itemTitleId = new HashMap();
880                                 itemTitleId.put("purchaseOrder.purapDocumentIdentifier", invoiceRecord.getPurchaseOrderNumber());
881                                 itemTitleId.put("purchaseOrder.vendorHeaderGeneratedIdentifier", vendorId.length > 0 ? vendorId[0] : "");
882                                 itemTitleId.put("purchaseOrder.vendorDetailAssignedIdentifier", vendorId.length > 1 ? vendorId[1] : "");
883                                 if (invoiceRecord.getPurchaseOrderNumber() != null) {
884                                     olePurchaseOrderItems = (List<OlePurchaseOrderItem>) getBusinessObjectService().findMatching(OlePurchaseOrderItem.class, itemTitleId);
885                                     if (olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0) {
886                                         invoiceRecord.setPurchaseOrderNumber((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 && olePurchaseOrderItems.get(0) != null) ? olePurchaseOrderItems.get(0).getPurapDocumentIdentifier() : null);
887                                         if ((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 && olePurchaseOrderItems.get(0) != null && olePurchaseOrderItems.get(0).getPurchaseOrder() != null && olePurchaseOrderItems.get(0).getPurchaseOrder().getPostingYear() != null)) {
888                                             invoiceDocument.setPostingYear(olePurchaseOrderItems.get(0).getPurchaseOrder().getPostingYear());
889                                         }
890                                         //invoiceDocument.setProrateBy(org.kuali.ole.sys.OLEConstants.MANUAL_PRORATE);
891                                         invoiceDocument.setInvoicePayDate(SpringContext.getBean(InvoiceService.class).calculatePayDate(invoiceDocument.getInvoiceDate(), invoiceDocument.getVendorPaymentTerms()));
892 
893                                         itemMap = addInvoiceItem(olePurchaseOrderItems, invoiceRecord, invoiceDocument, purchaseOrderDocument, itemMap,lineNumbers,poNumbers);
894                                         if (itemMap != null) {
895                                             invoiceDocument = (OleInvoiceDocument) itemMap.get("invoiceDocument");
896                                             purchaseOrderDocument = (PurchaseOrderDocument) itemMap.get("purchaseOrderDocument");
897                                         }
898                                         // totalInvoiceProcessedCount = i+1;
899                                     }
900                                 }
901                             }
902                         }
903 
904 
905                     }
906                     if (purchaseOrderDocument != null) {
907                         for (OleInvoiceItem item : (List<OleInvoiceItem>) invoiceDocument.getItems()) {
908                             if (invoiceRecord.getAdditionalChargeCode() != null && invoiceRecord.getAdditionalChargeCode().equalsIgnoreCase("SVC") && item.getItemTypeCode().equalsIgnoreCase("SPHD")) {
909                                 item.setItemUnitPrice(new BigDecimal(invoiceRecord.getAdditionalCharge() != null ? invoiceRecord.getAdditionalCharge() : invoiceRecord.getAdditionalCharge()));
910                                 item.setPurchaseOrderIdentifier(purchaseOrderDocument.getPurapDocumentIdentifier());
911                             } else if (invoiceRecord.getLineItemAdditionalChargeCode() != null && invoiceRecord.getLineItemAdditionalChargeCode().equalsIgnoreCase("LD") && item.getItemTypeCode().equalsIgnoreCase("ITEM")) {
912 
913                                 if ((item.getItemDescription().contains(invoiceRecord.getISBN())
914                                         || (invoiceRecord.getVendorItemIdentifier() != null ? invoiceRecord.getVendorItemIdentifier().equalsIgnoreCase(olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 ? item.getVendorItemIdentifier() : "") : false)
915                                         || (invoiceRecord.getPurchaseOrderNumber() != null ? invoiceRecord.getPurchaseOrderNumber().equals(item.getPurchaseOrderDocument() != null ? item.getPurchaseOrderDocument().getPurapDocumentIdentifier() : null) : false))) {
916 
917 
918                                     item.setItemDiscountType("%"); // % or #
919                                     item.setItemDiscount(new KualiDecimal(invoiceRecord.getLineItemAdditionalCharge() != null ? invoiceRecord.getLineItemAdditionalCharge() : invoiceRecord.getLineItemAdditionalCharge()));
920                                 }
921                             }
922                         }
923 
924                         String vendorNumber = purchaseOrderDocument.getVendorHeaderGeneratedIdentifier() + "-" + purchaseOrderDocument.getVendorDetailAssignedIdentifier();
925                         oleInvoiceService.populateVendorDetail(vendorNumber, invoiceDocument);
926                         invoiceDocument.setVendorCustomerNumber(invoiceRecord.getBillToCustomerID());
927                         if (invoiceDocument.getPaymentMethodId() != null) {
928                             invoiceDocument.setPaymentMethodIdentifier(String.valueOf(invoiceDocument.getPaymentMethodId()));
929                         } else {
930                             invoiceDocument.setPaymentMethodId(Integer.parseInt(OLEConstants.OleInvoiceImport.PAY_METHOD));
931                             invoiceDocument.setPaymentMethodIdentifier(String.valueOf(invoiceDocument.getPaymentMethodId()));
932                         }
933                         //   SpringContext.getBean(OleInvoiceService.class).calculateProrateItemSurcharge(invoiceDocument);
934                         SpringContext.getBean(PurapAccountingService.class).updateAccountAmounts(invoiceDocument);
935                         Long nextLinkIdentifier = SpringContext.getBean(SequenceAccessorService.class).getNextAvailableSequenceNumber("AP_PUR_DOC_LNK_ID");
936                         invoiceDocument.setAccountsPayablePurchasingDocumentLinkIdentifier(nextLinkIdentifier.intValue());
937 
938 
939                         if ((invoiceDocument.getProrateBy() != null) && (invoiceDocument.getProrateBy().equals(org.kuali.ole.sys.OLEConstants.PRORATE_BY_QTY) || invoiceDocument.getProrateBy().equals(org.kuali.ole.sys.OLEConstants.PRORATE_BY_DOLLAR) || invoiceDocument.getProrateBy().equals(org.kuali.ole.sys.OLEConstants.MANUAL_PRORATE))) {
940                             // set amounts on any empty
941                             invoiceDocument.updateExtendedPriceOnItems();
942 
943                             // calculation just for the tax area, only at tax review stage
944                             // by now, the general calculation shall have been done.
945                             if (invoiceDocument.getApplicationDocumentStatus().equals(PurapConstants.PaymentRequestStatuses.APPDOC_AWAITING_TAX_REVIEW)) {
946                                 SpringContext.getBean(OleInvoiceService.class).calculateTaxArea(invoiceDocument);
947                             }
948 
949                             updatePrice(invoiceDocument);
950 
951                             // notice we're ignoring whether the boolean, because these are just warnings they shouldn't halt anything
952                             // Calculate Payment request before rules since the rule check totalAmount.
953                             SpringContext.getBean(OleInvoiceService.class).calculateInvoice(invoiceDocument, true);
954                             SpringContext.getBean(KualiRuleService.class).applyRules(
955                                     new AttributedCalculateAccountsPayableEvent(invoiceDocument));
956                         } else {
957                             // set amounts on any empty
958                             invoiceDocument.updateExtendedPriceOnItems();
959 
960                             // calculation just for the tax area, only at tax review stage
961                             // by now, the general calculation shall have been done.
962                             if (StringUtils.equals(invoiceDocument.getApplicationDocumentStatus(), PurapConstants.PaymentRequestStatuses.APPDOC_AWAITING_TAX_REVIEW)) {
963                                 SpringContext.getBean(OleInvoiceService.class).calculateTaxArea(invoiceDocument);
964 
965                             }
966 
967                             updatePrice(invoiceDocument);
968 
969                             // notice we're ignoring whether the boolean, because these are just warnings they shouldn't halt anything
970                             //Calculate Payment request before rules since the rule check totalAmount.
971                             SpringContext.getBean(OleInvoiceService.class).calculateInvoice(invoiceDocument, true);
972                             SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedCalculateAccountsPayableEvent(invoiceDocument));
973                         }
974                         if (!SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedSaveDocumentEvent(invoiceDocument)) ||
975                                 !SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedCalculateAccountsPayableEvent(invoiceDocument))) {
976 
977 
978                             //}else {
979                             List invoiceIngestFailureReason = (List) dataCarrierService.getData("invoiceIngestFailureReason");
980                             if(invoiceIngestFailureReason != null && invoiceIngestFailureReason.size()>0){
981                                 for(int failCount = 0;failCount < invoiceIngestFailureReason.size();failCount++){
982                                     failureRecords.append(invoiceIngestFailureReason.get(failCount) + "\n");
983                                 }
984                             }
985                             LOG.info("Invoice Error Message------------------->");
986                             failureRecords.append("Unable to create Invoice document");
987                             failureRecords.append("\n");
988                             failureRecords.append("Invoice Number :" + invoiceRecord.getInvoiceNumber());
989                             failureRecords.append("\n");
990                             failureRecords.append("Invoice Date :" + invoiceDocument.getInvoiceDate());
991                             failureRecords.append("\n");
992                             if ((GlobalVariables.getMessageMap().getErrorMessages().get("GLOBAL_ERRORS")) != null) {
993                                 for (int error = 0; error < (GlobalVariables.getMessageMap().getErrorMessages().get("GLOBAL_ERRORS")).size(); error++) {
994                                     failureRecords.append("Error Message:" + kualiConfiguration.getPropertyValueAsString((GlobalVariables.getMessageMap().getErrorMessages().get("GLOBAL_ERRORS")).get(error).getErrorKey()));
995                                     failureRecords.append("\n");
996                                 }
997                             }
998                             failureRecords.append("\n");
999                         }
1000                     } else {
1001                         if(marcXMLContent != null && StringUtils.isNotBlank(marcXMLContent.toString())){
1002                             createInvoiceItem(invoiceRecord, invoiceDocument, itemMap);
1003                         }
1004                         else {
1005                             createInvoiceItem(lineItemCount, invoiceRecord, invoiceDocument, itemMap);
1006                         }
1007                         invoiceDocument.setAccountsPayablePurchasingDocumentLinkIdentifier((SpringContext.getBean(SequenceAccessorService.class).
1008                                 getNextAvailableSequenceNumber("AP_PUR_DOC_LNK_ID")).intValue());
1009                         if (itemMap != null) {
1010                             invoiceDocument = (OleInvoiceDocument) itemMap.get("invoiceDocument");
1011                             purchaseOrderDocument = (PurchaseOrderDocument) itemMap.get("purchaseOrderDocument");
1012                         }
1013                         for (OleInvoiceItem item : (List<OleInvoiceItem>) invoiceDocument.getItems()) {
1014                             if (invoiceRecord.getAdditionalChargeCode() != null && invoiceRecord.getAdditionalChargeCode().equalsIgnoreCase("SVC") && item.getItemTypeCode().equalsIgnoreCase("SPHD")) {
1015                                 item.setItemUnitPrice(new BigDecimal(invoiceRecord.getAdditionalCharge() != null ? invoiceRecord.getAdditionalCharge() : invoiceRecord.getAdditionalCharge()));
1016                                 item.setPurchaseOrderIdentifier(purchaseOrderDocument != null && purchaseOrderDocument.getPurapDocumentIdentifier() != null ?
1017                                         purchaseOrderDocument.getPurapDocumentIdentifier() : null);
1018                             } else if (invoiceRecord.getLineItemAdditionalChargeCode() != null && invoiceRecord.getLineItemAdditionalChargeCode().equalsIgnoreCase("LD") && item.getItemTypeCode().equalsIgnoreCase("ITEM")) {
1019 
1020                                 if ((item.getItemDescription().contains(invoiceRecord.getISBN())
1021                                         || (invoiceRecord.getVendorItemIdentifier() != null ? invoiceRecord.getVendorItemIdentifier().equalsIgnoreCase(olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 ? item.getVendorItemIdentifier() : "") : false)
1022                                         || (invoiceRecord.getPurchaseOrderNumber() != null ? invoiceRecord.getPurchaseOrderNumber().equals(item.getPurchaseOrderDocument() != null ? item.getPurchaseOrderDocument().getPurapDocumentIdentifier(): null):false))) {
1023 
1024 
1025                                     item.setItemDiscountType("#"); // % or #
1026                                     item.setItemDiscount(new KualiDecimal(invoiceRecord.getLineItemAdditionalCharge() != null ? invoiceRecord.getLineItemAdditionalCharge() : invoiceRecord.getLineItemAdditionalCharge()));
1027                                 }
1028                             }
1029                         }
1030 
1031                         String vendorNumber = invoiceRecord.getVendorNumber();
1032                         oleInvoiceService.populateVendorDetail(vendorNumber, invoiceDocument);
1033                         invoiceDocument.setVendorCustomerNumber(invoiceRecord.getBillToCustomerID());
1034                         if (invoiceDocument.getPaymentMethodId() != null) {
1035                             invoiceDocument.setPaymentMethodIdentifier(String.valueOf(invoiceDocument.getPaymentMethodId()));
1036                         } else {
1037                             invoiceDocument.setPaymentMethodId(Integer.parseInt(OLEConstants.OleInvoiceImport.PAY_METHOD));
1038                             invoiceDocument.setPaymentMethodIdentifier(String.valueOf(invoiceDocument.getPaymentMethodId()));
1039                         }
1040                         //   SpringContext.getBean(OleInvoiceService.class).calculateProrateItemSurcharge(invoiceDocument);
1041                         SpringContext.getBean(PurapAccountingService.class).updateAccountAmounts(invoiceDocument);
1042                         Long nextLinkIdentifier = SpringContext.getBean(SequenceAccessorService.class).getNextAvailableSequenceNumber("AP_PUR_DOC_LNK_ID");
1043                         // invoiceDocument.setAccountsPayablePurchasingDocumentLinkIdentifier(nextLinkIdentifier.intValue());
1044 
1045 
1046                         if ((invoiceDocument.getProrateBy() != null) && (invoiceDocument.getProrateBy().equals(org.kuali.ole.sys.OLEConstants.PRORATE_BY_QTY) || invoiceDocument.getProrateBy().equals(org.kuali.ole.sys.OLEConstants.PRORATE_BY_DOLLAR) || invoiceDocument.getProrateBy().equals(org.kuali.ole.sys.OLEConstants.MANUAL_PRORATE))) {
1047                             // set amounts on any empty
1048                             invoiceDocument.updateExtendedPriceOnItems();
1049 
1050                             // calculation just for the tax area, only at tax review stage
1051                             // by now, the general calculation shall have been done.
1052                             if (invoiceDocument.getApplicationDocumentStatus().equals(PurapConstants.PaymentRequestStatuses.APPDOC_AWAITING_TAX_REVIEW)) {
1053                                 SpringContext.getBean(OleInvoiceService.class).calculateTaxArea(invoiceDocument);
1054                             }
1055 
1056                             updatePrice(invoiceDocument);
1057 
1058                             // notice we're ignoring whether the boolean, because these are just warnings they shouldn't halt anything
1059                             // Calculate Payment request before rules since the rule check totalAmount.
1060                             SpringContext.getBean(OleInvoiceService.class).calculateInvoice(invoiceDocument, true);
1061                             SpringContext.getBean(KualiRuleService.class).applyRules(
1062                                     new AttributedCalculateAccountsPayableEvent(invoiceDocument));
1063                         } else {
1064                             // set amounts on any empty
1065                             invoiceDocument.updateExtendedPriceOnItems();
1066 
1067                             // calculation just for the tax area, only at tax review stage
1068                             // by now, the general calculation shall have been done.
1069                             if (StringUtils.equals(invoiceDocument.getApplicationDocumentStatus(), PurapConstants.PaymentRequestStatuses.APPDOC_AWAITING_TAX_REVIEW)) {
1070                                 SpringContext.getBean(OleInvoiceService.class).calculateTaxArea(invoiceDocument);
1071 
1072                             }
1073 
1074                             updatePrice(invoiceDocument);
1075 
1076                             // notice we're ignoring whether the boolean, because these are just warnings they shouldn't halt anything
1077                             //Calculate Payment request before rules since the rule check totalAmount.
1078                             SpringContext.getBean(OleInvoiceService.class).calculateInvoice(invoiceDocument, true);
1079                             SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedCalculateAccountsPayableEvent(invoiceDocument));
1080                         }
1081 
1082 
1083                     }
1084                 }
1085             if (!"".equals(failureRecords.toString())) {
1086                 createBatchErrorAttachmentFile(failureRecords.toString());
1087             }
1088             allInvoiceDocument.add(invoiceDocument);
1089         }
1090         deleteBatchFile();
1091         if (allInvoiceDocument != null && !allInvoiceDocument.isEmpty()) {
1092             for (int process = 0; process < allInvoiceDocument.size(); process++) {
1093                 invoiceDocument = allInvoiceDocument.get(process);
1094                 if (invoiceDocument.getAccountsPayablePurchasingDocumentLinkIdentifier() != null) {
1095                     if (SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedSaveDocumentEvent(invoiceDocument)) &&
1096                             SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedCalculateAccountsPayableEvent(invoiceDocument))) {
1097 
1098                         try {
1099                             OLEInvoiceIngestLoadReport oleInvoiceIngestLoadReport =new OLEInvoiceIngestLoadReport();
1100                             oleInvoiceIngestLoadReport.setFileName(job.getUploadFileName());
1101                             java.sql.Date sqlDate = new java.sql.Date(new Date(System.currentTimeMillis()).getTime());
1102                             oleInvoiceIngestLoadReport.setDateUploaded(sqlDate);
1103                             oleInvoiceIngestLoadReport.setVendor(invoiceDocument.getVendorNumber());
1104                             getBusinessObjectService().save(oleInvoiceIngestLoadReport);
1105                             oleInvoiceService.autoApprovePaymentRequest(invoiceDocument);
1106                             successCount = process + 1;
1107                         } catch (Exception e) {
1108                             LOG.info("Invoice Error Message------------------->");
1109                             failureRecords.append("Unable to create Invoice document");
1110                             failureRecords.append("\n");
1111                             failureRecords.append("Invoice Number :" + invoiceRecord.getInvoiceNumber());
1112                             failureRecords.append("\n");
1113                             failureRecords.append("Invoice Date :" + invoiceDocument.getInvoiceDate());
1114                             failureRecords.append("\n");
1115                             if ((GlobalVariables.getMessageMap() != null && GlobalVariables.getMessageMap().getErrorMessages() != null) &&
1116                                     ((GlobalVariables.getMessageMap().getErrorMessages().size() > 0 ) ||
1117                                     (GlobalVariables.getMessageMap().getErrorMessages().get("GLOBAL_ERRORS")) != null)) {
1118                                 for (int error = 0; error < (GlobalVariables.getMessageMap().getErrorMessages().get("GLOBAL_ERRORS")).size(); error++) {
1119                                     failureRecords.append("Error Message:" + kualiConfiguration.getPropertyValueAsString((GlobalVariables.getMessageMap().getErrorMessages().get("GLOBAL_ERRORS")).get(error).getErrorKey()));
1120                                     failureRecords.append("\n");
1121                                 }
1122                             } else {
1123                                 failureRecords.append("Error Message:" + e.initCause(new Throwable()));
1124                             }
1125                             failureRecords.append("\n");
1126                             failureRecords.append("\n");
1127 
1128                         }
1129 
1130                         job.setTotalNoOfRecords(totalInvoiceFileCount + "");
1131                         job.setNoOfRecordsProcessed(totalInvoiceFileCount + "");
1132                         job.setNoOfSuccessRecords(String.valueOf(successCount));
1133                         job.setNoOfFailureRecords(String.valueOf(totalInvoiceFileCount - successCount));
1134                         job.setStatusDesc(OLEConstants.INVOICE_UPLOAD_SUCCESS);
1135                         job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
1136                         deleteBatchFile();
1137                         if (!"".equals(failureRecords.toString())) {
1138                             createBatchErrorAttachmentFile(failureRecords.toString());
1139                         }
1140                     }
1141                     else {
1142                         if ((GlobalVariables.getMessageMap() != null &&
1143                                 GlobalVariables.getMessageMap().getErrorMessages() != null &&
1144                                 GlobalVariables.getMessageMap().hasErrors()) &&
1145                                 (GlobalVariables.getMessageMap().getErrorMessages().size() > 0 )) {
1146                                 Map<String, AutoPopulatingList<ErrorMessage>> errorMap = GlobalVariables.getMessageMap().getErrorMessages();
1147                                 for (Map.Entry<String, AutoPopulatingList<ErrorMessage>> entry : errorMap.entrySet()) {
1148                                     AutoPopulatingList<ErrorMessage> errors = entry.getValue();
1149                                     /*ErrorMessage error = errors.get(0);*/
1150                                     /*String[] params = error.getMessageParameters();
1151                                     String param = params[0];
1152                                     if (param.toUpperCase().contains("ITEM")) {
1153                                         param = error.getErrorKey();
1154                                     }*/
1155                                     for(ErrorMessage error : errors){
1156                                         failureRecords.append("Error Message:" + MessageFormat.format(kualiConfiguration.getPropertyValueAsString(error.getErrorKey()), error.getMessageParameters()));
1157                                         failureRecords.append("\n");
1158                                     }
1159 
1160                             }
1161                         }
1162                         job.setTotalNoOfRecords(totalInvoiceFileCount + "");
1163                         job.setNoOfRecordsProcessed(totalInvoiceFileCount + "");
1164                         job.setNoOfSuccessRecords(String.valueOf(successCount));
1165                         job.setNoOfFailureRecords(String.valueOf(totalInvoiceFileCount - successCount));
1166                         job.setStatusDesc(OLEConstants.INVOICE_UPLOAD_SUCCESS);
1167                         job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
1168                         deleteBatchFile();
1169                         if (!"".equals(failureRecords.toString())) {
1170                             createBatchErrorAttachmentFile(failureRecords.toString());
1171                         }
1172                     }
1173                     job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
1174                 }
1175                 job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
1176             }
1177             job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
1178         }
1179         if (successCount == 0) {
1180             OLEBatchProcessJobDetailsController.setBatchProcessJobStatusMap(job.getJobId(),OLEConstants.OLEBatchProcess.JOB_STATUS_STOPPED);
1181             job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_STOPPED);
1182         } else {
1183             OLEBatchProcessJobDetailsController.setBatchProcessJobStatusMap(job.getJobId(),OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
1184             job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
1185         }
1186     }
1187 
1188     private void setDocumentForeignDetails(OleInvoiceDocument invoiceDocument,OleInvoiceRecord invoiceRecord){
1189         OleCurrencyType oleCurrencyType = getBusinessObjectService().findBySinglePrimaryKey(OleCurrencyType.class,invoiceRecord.getCurrencyTypeId());
1190         invoiceDocument.setInvoiceCurrencyType(oleCurrencyType.getCurrencyTypeId().toString());
1191         if(!oleCurrencyType.getCurrencyType().equalsIgnoreCase(OleSelectConstant.CURRENCY_TYPE_NAME)){
1192             Map documentNumberMap = new HashMap();
1193             documentNumberMap.put(OleSelectConstant.CURRENCY_TYPE_ID, invoiceRecord.getCurrencyTypeId());
1194             List<OleExchangeRate> exchangeRateList = (List) getBusinessObjectService().findMatchingOrderBy(
1195                     OleExchangeRate.class, documentNumberMap, OleSelectConstant.EXCHANGE_RATE_DATE, false);
1196             Iterator iterator = exchangeRateList.iterator();
1197             if (iterator.hasNext()) {
1198                 invoiceDocument.setForeignVendorInvoiceAmount(new BigDecimal(0.00));
1199                 invoiceDocument.setInvoiceCurrencyTypeId(new Long(invoiceRecord.getCurrencyTypeId()));
1200                 invoiceDocument.setInvoiceCurrencyExchangeRate(invoiceRecord.getInvoiceCurrencyExchangeRate());
1201             }
1202         }
1203     }
1204 
1205     public boolean validateVendorNumber(String vendorNumber){
1206         LOG.info("----Inside validateVendorNumber()------------------------------");
1207         boolean isValidVendor = false;
1208         if(vendorNumber != null){
1209             String[] vendorDetail = vendorNumber.split("-");
1210             if(vendorDetail.length == 2){
1211                 String vendorHeaderGeneratedIdentifier = vendorDetail[0];
1212                 String vendorDetailAssignedIdentifier = vendorDetail[1];
1213                 try {
1214                     Map<String,Integer> vendorMap = new HashMap<>();
1215                     vendorMap.put(OLEConstants.VENDOR_HEADER_GENERATED_ID, Integer.parseInt(vendorHeaderGeneratedIdentifier));
1216                     vendorMap.put(OLEConstants.VENDOR_DETAILED_ASSIGNED_ID, Integer.parseInt(vendorDetailAssignedIdentifier));
1217                     List<VendorDetail> vendorDetailList = (List) getBusinessObjectService().findMatching(VendorDetail.class, vendorMap);
1218                     if(vendorDetail != null && vendorDetailList.size() > 0){
1219                         isValidVendor  = true;
1220                         return isValidVendor;
1221                     }
1222                 }
1223                 catch (NumberFormatException nfe) {
1224                     return isValidVendor;
1225                 }
1226             }
1227         }
1228         return isValidVendor;
1229     }
1230 
1231     public boolean validateForNumber(String fieldValue){
1232         try {
1233             Integer quantity = Integer.parseInt(fieldValue);
1234             if(quantity <= 0){
1235                 return false;
1236             }
1237         }
1238         catch(NumberFormatException nfe){
1239             return false;
1240         }
1241         return true;
1242     }
1243 
1244     public boolean validateDestinationFieldValues(String destinationFieldValue){
1245         try {
1246             Float fieldValue = Float.parseFloat(destinationFieldValue);
1247             if(fieldValue <= 0){
1248                 return false;
1249             }
1250         }
1251         catch(NumberFormatException nfe){
1252             return false;
1253         }
1254         return true;
1255     }
1256 
1257     @Override
1258     public void getNextBatch() {
1259         //To change body of implemented methods use File | Settings | File Templates.
1260     }
1261 
1262     @Override
1263     public void processBatch() {
1264         //To change body of implemented methods use File | Settings | File Templates.
1265     }
1266 
1267 }