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) 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.getLineItemAdditionalCharge() != null) {
399                     oleInvoiceItem.setVendorItemIdentifier(poItem.getVendorItemPoNumber());
400                 }
401                 PurchaseOrderService purchaseOrderService = (PurchaseOrderService) SpringContext.getBean("purchaseOrderService");
402                 purchaseOrderDocument = purchaseOrderService.getPurchaseOrderByDocumentNumber(poItem.getDocumentNumber());
403                 oleInvoiceItem.setPurchaseOrderIdentifier(purchaseOrderDocument.getPurapDocumentIdentifier());
404                 oleInvoiceItem.setItemLineNumber(poItem.getItemLineNumber());
405                 oleInvoiceItem.setItemNoOfParts(poItem.getItemNoOfParts());
406                 oleInvoiceItem.setPoItemIdentifier(poItem.getItemIdentifier());
407                 oleInvoiceItem.setAccountsPayablePurchasingDocumentLinkIdentifier(purchaseOrderDocument.getAccountsPayablePurchasingDocumentLinkIdentifier());
408                 oleInvoiceItem.setOlePoOutstandingQuantity(new KualiInteger(poItem.getOutstandingQuantity().bigDecimalValue()));
409                 //call populate VendorInfo
410                 OleVendorAccountInfo oleVendorAccountInfo = populateBFN(invoiceRecord.getBfnNumber());
411                 List accountingLine = new ArrayList();
412                 if (oleVendorAccountInfo != null && oleVendorAccountInfo.isActive()) {
413 
414                     InvoiceAccount invoiceAccount = new InvoiceAccount();
415                     invoiceAccount.setAccountNumber(oleVendorAccountInfo.getAccountNumber());
416                     invoiceAccount.setFinancialObjectCode(oleVendorAccountInfo.getObjectCode());
417                     invoiceAccount.setAmount(new KualiDecimal(invoiceRecord.getUnitPrice()));
418                     invoiceAccount.setAccountLinePercent(new BigDecimal("100"));  // TODO: Need to get from edifact.
419                     invoiceAccount.setPurapItem(oleInvoiceItem);
420                     invoiceAccount.setItemIdentifier(oleInvoiceItem.getItemIdentifier());
421                     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.
422                     accountingLine.add(invoiceAccount);
423 
424                 } else {
425                     for (PurApAccountingLine poa : poItem.getSourceAccountingLines()) {
426                         InvoiceAccount invoiceAccount = new InvoiceAccount(oleInvoiceItem, (PurchaseOrderAccount) poa);
427                         invoiceAccount.setAccountNumber(!StringUtils.isBlank(invoiceRecord.getAccountNumber()) ? invoiceRecord.getAccountNumber() : invoiceAccount.getAccountNumber());
428                         invoiceAccount.setFinancialObjectCode(!StringUtils.isBlank(invoiceRecord.getObjectCode()) ? invoiceRecord.getObjectCode()  : invoiceAccount.getFinancialObjectCode());
429                         accountingLine.add(invoiceAccount);
430                     }
431                 }
432                 oleInvoiceItem.setSourceAccountingLines(accountingLine);
433                 oleInvoiceItem.setPostingYear(poItem.getPurchaseOrder().getPostingYear());
434                 SimpleDateFormat dateFromRawFile = new SimpleDateFormat(org.kuali.ole.OLEConstants.DATE_FORMAT);
435                 oleInvoiceItem.setSubscriptionFromDate(invoiceRecord.getSubscriptionPeriodFrom()!= null ? new java.sql.Date(dateFromRawFile.parse(invoiceRecord.getSubscriptionPeriodFrom()).getTime()):null);
436                 oleInvoiceItem.setSubscriptionToDate(invoiceRecord.getSubscriptionPeriodTo()!= null ? new java.sql.Date(dateFromRawFile.parse(invoiceRecord.getSubscriptionPeriodTo()).getTime()):null);
437                 if(!StringUtils.isBlank(invoiceDocument.getInvoiceCurrencyType())){
438                     invoiceDocument.setInvoiceCurrencyTypeId(new Long(invoiceDocument.getInvoiceCurrencyType()));
439                     String currencyType = getInvoiceService().getCurrencyType(invoiceDocument.getInvoiceCurrencyType());
440                     if (StringUtils.isNotBlank(currencyType)) {
441                         if (!currencyType.equalsIgnoreCase(OleSelectConstant.CURRENCY_TYPE_NAME)) {
442                             setItemForeignDetails(oleInvoiceItem,invoiceDocument.getInvoiceCurrencyType(),invoiceRecord);
443                         }
444                     }
445                 }
446                 invoiceDocument.getItems().add(oleInvoiceItem);
447 
448             }
449         }
450 
451         itemMap.put("invoiceDocument", invoiceDocument);
452         itemMap.put("purchaseOrderDocument", purchaseOrderDocument);
453         return itemMap;
454     }
455 
456     private HashMap createInvoiceItem(int lineItemCount, OleInvoiceRecord invoiceRecord, OleInvoiceDocument invoiceDocument
457             , HashMap itemMap) throws Exception {
458 
459         OleInvoiceItem oleInvoiceItem = new OleInvoiceItem();
460         validateInvoiceRecordValues(invoiceRecord);
461         oleInvoiceItem.setItemQuantity(new KualiDecimal(invoiceRecord.getQuantity()));
462         oleInvoiceItem.setItemListPrice(new KualiDecimal(invoiceRecord.getListPrice()));
463         oleInvoiceItem.setItemDescription(invoiceRecord.getItemDescription());      // invoiceRecord.getItemDescription()
464         /*if (invoiceRecord.getAdditionalChargeCode() != null && invoiceRecord.getAdditionalChargeCode().equalsIgnoreCase("SVC")) {
465             oleInvoiceItem.setItemUnitPrice(new BigDecimal(invoiceRecord.getAdditionalCharge() != null ? invoiceRecord.getAdditionalCharge() : invoiceRecord.getAdditionalCharge()));
466             oleInvoiceItem.setItemTypeCode("SPHD");
467         } *//*else if (invoiceRecord.getLineItemAdditionalChargeCode() != null && invoiceRecord.getLineItemAdditionalChargeCode().equalsIgnoreCase("LD") && item.getItemTypeCode().equalsIgnoreCase("ITEM")) {
468             oleInvoiceItem.
469             oleInvoiceItem.setItemDiscountType("#"); // % or #
470             oleInvoiceItem.setItemDiscount(new KualiDecimal(invoiceRecord.getLineItemAdditionalCharge() != null ? invoiceRecord.getLineItemAdditionalCharge() : invoiceRecord.getLineItemAdditionalCharge()));
471         }*/
472         //else {
473         if (invoiceRecord.getUnitPrice() != null) {
474             oleInvoiceItem.setItemUnitPrice(new BigDecimal(invoiceRecord.getUnitPrice()));
475         }
476         oleInvoiceItem.setItemTypeCode("ITEM");
477         oleInvoiceItem.setItemDiscountType("%"); // % or #
478         oleInvoiceItem.setItemDiscount(new KualiDecimal(invoiceRecord.getLineItemAdditionalCharge() != null ? invoiceRecord.getLineItemAdditionalCharge() : invoiceRecord.getLineItemAdditionalCharge()));
479         //}
480         oleInvoiceItem.setItemTitleId(null);
481         if (invoiceRecord.getLineItemAdditionalCharge() != null) {
482             oleInvoiceItem.setVendorItemIdentifier(null);
483         }
484 
485         //PurchaseOrderService purchaseOrderService = (PurchaseOrderService) SpringContext.getBean("purchaseOrderService");
486         // purchaseOrderDocument = purchaseOrderService.getPurchaseOrderByDocumentNumber(poItem.getDocumentNumber());
487         oleInvoiceItem.setPurchaseOrderIdentifier(null);
488         oleInvoiceItem.setItemLineNumber(lineItemCount);
489         oleInvoiceItem.setItemNoOfParts(new KualiInteger("1"));
490         oleInvoiceItem.setPoItemIdentifier(null);
491         oleInvoiceItem.setAccountsPayablePurchasingDocumentLinkIdentifier((SpringContext.getBean(SequenceAccessorService.class).
492                 getNextAvailableSequenceNumber("AP_PUR_DOC_LNK_ID")).intValue());
493         oleInvoiceItem.setOlePoOutstandingQuantity(KualiInteger.ZERO);
494         //call populate VendorInfo
495         OleVendorAccountInfo oleVendorAccountInfo = populateBFN(invoiceRecord.getBfnNumber());
496         List accountingLine = new ArrayList();
497         if (oleVendorAccountInfo != null && oleVendorAccountInfo.isActive()) {
498 
499             InvoiceAccount invoiceAccount = new InvoiceAccount();
500             invoiceAccount.setAccountNumber(oleVendorAccountInfo.getAccountNumber());
501             invoiceAccount.setFinancialObjectCode(oleVendorAccountInfo.getObjectCode());
502             invoiceAccount.setAmount(new KualiDecimal(invoiceRecord.getUnitPrice()));
503             invoiceAccount.setAccountLinePercent(new BigDecimal("100"));  // TODO: Need to get from edifact.
504             invoiceAccount.setPurapItem(oleInvoiceItem);
505             invoiceAccount.setItemIdentifier(oleInvoiceItem.getItemIdentifier());
506             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.
507             accountingLine.add(invoiceAccount);
508 
509         } else if (invoiceRecord.getAccountNumber() != null && invoiceRecord.getObjectCode() != null) {
510             InvoiceAccount invoiceAccount = new InvoiceAccount();
511             invoiceAccount.setAccountNumber(invoiceRecord.getAccountNumber());
512             invoiceAccount.setFinancialObjectCode(invoiceRecord.getObjectCode());
513             invoiceAccount.setAmount(new KualiDecimal(invoiceRecord.getUnitPrice()));
514             invoiceAccount.setAccountLinePercent(new BigDecimal("100"));  // TODO: Need to get from edifact.
515             invoiceAccount.setPurapItem(oleInvoiceItem);
516             invoiceAccount.setItemIdentifier(oleInvoiceItem.getItemIdentifier());
517             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.
518             accountingLine.add(invoiceAccount);
519 
520         }
521 
522         oleInvoiceItem.setSourceAccountingLines(accountingLine);
523         oleInvoiceItem.setPostingYear(SpringContext.getBean(UniversityDateService.class).getCurrentUniversityDate().getUniversityFiscalYear());
524         SimpleDateFormat dateFromRawFile = new SimpleDateFormat(org.kuali.ole.OLEConstants.DATE_FORMAT);
525         oleInvoiceItem.setSubscriptionFromDate(invoiceRecord.getSubscriptionPeriodFrom()!= null ? new java.sql.Date(dateFromRawFile.parse(invoiceRecord.getSubscriptionPeriodFrom()).getTime()):null);
526         oleInvoiceItem.setSubscriptionToDate(invoiceRecord.getSubscriptionPeriodTo()!= null ? new java.sql.Date(dateFromRawFile.parse(invoiceRecord.getSubscriptionPeriodTo()).getTime()):null);
527         if(!StringUtils.isBlank(invoiceDocument.getInvoiceCurrencyType())){
528             invoiceDocument.setInvoiceCurrencyTypeId(new Long(invoiceDocument.getInvoiceCurrencyType()));
529             String currencyType = getInvoiceService().getCurrencyType(invoiceDocument.getInvoiceCurrencyType());
530             if (StringUtils.isNotBlank(currencyType)) {
531                 if (!currencyType.equalsIgnoreCase(OleSelectConstant.CURRENCY_TYPE_NAME)) {
532                     setItemForeignDetails(oleInvoiceItem,invoiceDocument.getInvoiceCurrencyType(),invoiceRecord);
533                 }
534             }
535         }
536         invoiceDocument.getItems().add(oleInvoiceItem);
537 
538         /* if (invoiceRecord.getAdditionalChargeCode() != null && invoiceRecord.getAdditionalChargeCode().equalsIgnoreCase("SVC") && item.getItemTypeCode().equalsIgnoreCase("SPHD")) {
539                     item.setItemUnitPrice(new BigDecimal(invoiceRecord.getAdditionalCharge() != null ? invoiceRecord.getAdditionalCharge() : invoiceRecord.getAdditionalCharge()));
540                     item.setPurchaseOrderIdentifier(purchaseOrderDocument != null && purchaseOrderDocument.getPurapDocumentIdentifier() != null ?
541                             purchaseOrderDocument.getPurapDocumentIdentifier() : null);
542                 } else if (invoiceRecord.getLineItemAdditionalChargeCode() != null && invoiceRecord.getLineItemAdditionalChargeCode().equalsIgnoreCase("LD") && item.getItemTypeCode().equalsIgnoreCase("ITEM")) {
543 
544                     if ((item.getItemDescription().contains(invoiceRecord.getISBN())
545                             || (invoiceRecord.getVendorItemIdentifier() != null ? invoiceRecord.getVendorItemIdentifier().equalsIgnoreCase(olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 ? item.getVendorItemIdentifier() : "") : false)
546                             || (invoiceRecord.getPurchaseOrderNumber() != null ? invoiceRecord.getPurchaseOrderNumber().equals(item.getPurchaseOrderDocument() != null ? item.getPurchaseOrderDocument().getPurapDocumentIdentifier(): null):false))) {
547 
548 
549                         item.setItemDiscountType("#"); // % or #
550                         item.setItemDiscount(new KualiDecimal(invoiceRecord.getLineItemAdditionalCharge() != null ? invoiceRecord.getLineItemAdditionalCharge() : invoiceRecord.getLineItemAdditionalCharge()));
551                     }
552                 }
553         */
554 
555         itemMap.put("invoiceDocument", invoiceDocument);
556         return itemMap;
557     }
558 
559     private HashMap createInvoiceItem(OleInvoiceRecord invoiceRecord, OleInvoiceDocument invoiceDocument
560             , HashMap itemMap) throws Exception {
561         OleInvoiceItem oleInvoiceItem = new OleInvoiceItem();
562         oleInvoiceItem.setItemQuantity(new KualiDecimal(invoiceRecord.getQuantity()));
563         oleInvoiceItem.setItemListPrice(new KualiDecimal(invoiceRecord.getListPrice()));
564         oleInvoiceItem.setItemUnitPrice(new BigDecimal(invoiceRecord.getUnitPrice()));
565         oleInvoiceItem.setItemTypeCode("ITEM");
566         oleInvoiceItem.setItemDiscountType("%");
567         oleInvoiceItem.setItemDiscount(new KualiDecimal(invoiceRecord.getLineItemAdditionalCharge() != null ? invoiceRecord.getLineItemAdditionalCharge() : invoiceRecord.getLineItemAdditionalCharge()));
568         oleInvoiceItem.setItemTitleId(invoiceRecord.getItemTitleIdForMRC());
569         oleInvoiceItem.setItemDescription(invoiceRecord.getItemDescription());
570         if (invoiceRecord.getLineItemAdditionalCharge() != null) {
571             oleInvoiceItem.setVendorItemIdentifier(null);
572         }
573         oleInvoiceItem.setPurchaseOrderIdentifier(null);
574         oleInvoiceItem.setItemNoOfParts(new KualiInteger("1"));
575         oleInvoiceItem.setPoItemIdentifier(null);
576         oleInvoiceItem.setAccountsPayablePurchasingDocumentLinkIdentifier((SpringContext.getBean(SequenceAccessorService.class).
577                 getNextAvailableSequenceNumber("AP_PUR_DOC_LNK_ID")).intValue());
578         oleInvoiceItem.setOlePoOutstandingQuantity(KualiInteger.ZERO);
579         List accountingLine = new ArrayList();
580         if (invoiceRecord.getAccountNumber() != null && invoiceRecord.getObjectCode() != null) {
581             InvoiceAccount invoiceAccount = new InvoiceAccount();
582             invoiceAccount.setAccountNumber(invoiceRecord.getAccountNumber());
583             invoiceAccount.setFinancialObjectCode(invoiceRecord.getObjectCode());
584             invoiceAccount.setAmount(new KualiDecimal(invoiceRecord.getUnitPrice()));
585             invoiceAccount.setAccountLinePercent(new BigDecimal("100"));
586             invoiceAccount.setPurapItem(oleInvoiceItem);
587             invoiceAccount.setItemIdentifier(oleInvoiceItem.getItemIdentifier());
588             invoiceAccount.setChartOfAccountsCode(populateChartOfAccount(invoiceRecord.getAccountNumber()));
589             if(checkForValidObjectCode(invoiceRecord,invoiceAccount)){
590                 accountingLine.add(invoiceAccount);
591             }
592         }
593         oleInvoiceItem.setSourceAccountingLines(accountingLine);
594         oleInvoiceItem.setPostingYear(SpringContext.getBean(UniversityDateService.class).getCurrentUniversityDate().getUniversityFiscalYear());
595         SimpleDateFormat dateFromRawFile = new SimpleDateFormat(org.kuali.ole.OLEConstants.DATE_FORMAT);
596         oleInvoiceItem.setSubscriptionFromDate(invoiceRecord.getSubscriptionPeriodFrom()!= null ? new java.sql.Date(dateFromRawFile.parse(invoiceRecord.getSubscriptionPeriodFrom()).getTime()):null);
597         oleInvoiceItem.setSubscriptionToDate(invoiceRecord.getSubscriptionPeriodTo()!= null ? new java.sql.Date(dateFromRawFile.parse(invoiceRecord.getSubscriptionPeriodTo()).getTime()):null);
598         if(!StringUtils.isBlank(invoiceDocument.getInvoiceCurrencyType())){
599             invoiceDocument.setInvoiceCurrencyTypeId(new Long(invoiceDocument.getInvoiceCurrencyType()));
600             String currencyType = getInvoiceService().getCurrencyType(invoiceDocument.getInvoiceCurrencyType());
601             if (StringUtils.isNotBlank(currencyType)) {
602                 if (!currencyType.equalsIgnoreCase(OleSelectConstant.CURRENCY_TYPE_NAME)) {
603                     setItemForeignDetails(oleInvoiceItem,invoiceDocument.getInvoiceCurrencyType(),invoiceRecord);
604                 }
605             }
606         }
607         invoiceDocument.getItems().add(oleInvoiceItem);
608         itemMap.put("invoiceDocument", invoiceDocument);
609         return itemMap;
610     }
611 
612     private void setItemForeignDetails(OleInvoiceItem oleInvoiceItem,String invoiceCurrencyType, OleInvoiceRecord invoiceRecord){
613          KualiDecimal foreignListPrice = new KualiDecimal(invoiceRecord.getForeignListPrice());
614         oleInvoiceItem.setItemForeignListPrice(new KualiDecimal(invoiceRecord.getForeignListPrice()));
615         oleInvoiceItem.setItemForeignDiscount(oleInvoiceItem.getItemDiscount() == null ? new KualiDecimal(0.0) : oleInvoiceItem.getItemDiscount());
616         oleInvoiceItem.setItemForeignDiscountType(oleInvoiceItem.getItemDiscountType() != null ? oleInvoiceItem.getItemDiscountType() : "%");
617         if(oleInvoiceItem.getItemForeignDiscountType().equalsIgnoreCase("%")){
618             BigDecimal discount = ((new BigDecimal(String.valueOf(foreignListPrice)).multiply(new BigDecimal(oleInvoiceItem.getForeignDiscount())))).divide(new BigDecimal(100));
619             oleInvoiceItem.setItemForeignUnitCost(new KualiDecimal(new BigDecimal(String.valueOf(foreignListPrice)).subtract(discount)));
620         }
621         else {
622             oleInvoiceItem.setItemForeignUnitCost(new KualiDecimal(new BigDecimal(String.valueOf(foreignListPrice)).subtract(new BigDecimal(oleInvoiceItem.getForeignDiscount()))));
623         }
624         oleInvoiceItem.setItemExchangeRate(new KualiDecimal(getInvoiceService().getExchangeRate(invoiceCurrencyType).getExchangeRate()));
625         if(StringUtils.isNotBlank(invoiceRecord.getInvoiceCurrencyExchangeRate())){
626            oleInvoiceItem.setItemUnitCostUSD(new KualiDecimal(oleInvoiceItem.getItemForeignUnitCost().bigDecimalValue().divide(new BigDecimal(invoiceRecord.getInvoiceCurrencyExchangeRate()), 4, BigDecimal.ROUND_HALF_UP)));
627         }else{
628            oleInvoiceItem.setItemUnitCostUSD(new KualiDecimal(oleInvoiceItem.getItemForeignUnitCost().bigDecimalValue().divide(oleInvoiceItem.getItemExchangeRate().bigDecimalValue(), 4, BigDecimal.ROUND_HALF_UP)));
629         }
630         oleInvoiceItem.setItemUnitPrice(oleInvoiceItem.getItemUnitCostUSD().bigDecimalValue());
631         oleInvoiceItem.setItemListPrice(oleInvoiceItem.getItemUnitCostUSD());
632     }
633 
634     private boolean checkForValidObjectCode(OleInvoiceRecord oleInvoiceRecord,InvoiceAccount invoiceAccount){
635         Map<String,String> chartCodeMap = new HashMap<>();
636         chartCodeMap.put("chartOfAccountsCode",invoiceAccount.getChartOfAccountsCode());
637         List<ObjectCode> objectCodeList = (List<ObjectCode>) getBusinessObjectService().findMatching(ObjectCode.class, chartCodeMap);
638         if(objectCodeList != null && objectCodeList.size() > 0){
639             for(int recCount = 0;recCount < objectCodeList.size();recCount++){
640                 if(oleInvoiceRecord.getObjectCode().equalsIgnoreCase(objectCodeList.get(recCount).getFinancialObjectCode())){
641                     return true;
642                 }
643             }
644         }
645         return false;
646     }
647 
648     private void validateInvoiceRecordValues(OleInvoiceRecord invoiceRecord) throws Exception {
649         boolean validQuantity = validateForNumber(invoiceRecord.getQuantity());
650         if(!validQuantity){
651             failureRecords.append(OLEConstants.INVALID_QTY + "  "+ invoiceRecord.getQuantity()+"\n" );
652         }
653         boolean validListPrice = validateDestinationFieldValues(invoiceRecord.getListPrice());
654         if(!validListPrice){
655             failureRecords.append(OLEConstants.INVALID_LIST_PRICE + "  " + invoiceRecord.getListPrice()+"\n");
656         }
657         boolean validVendorNumber = validateVendorNumber(invoiceRecord.getVendorNumber());
658         if(!validVendorNumber){
659             failureRecords.append(org.kuali.ole.OLEConstants.INVALID_VENDOR_NUMBER + "  "+ invoiceRecord.getVendorNumber() + "\n");
660         }
661         if(!StringUtils.isBlank(failureRecords.toString())){
662             createBatchErrorAttachmentFile(failureRecords.toString());
663         }
664     }
665 
666     @Override
667     public void prepareForWrite() throws Exception {
668         ConfigurationService kualiConfiguration = SpringContext.getBean(ConfigurationService.class);
669         GlobalVariables.setUserSession(new UserSession(processDef.getUser()));
670         OleInvoiceDocument invoiceDocument = null;
671         OleInvoiceRecord invoiceRecord = null;
672         List<OleInvoiceDocument> allInvoiceDocument = new ArrayList<>();
673         OleInvoiceRecordHandler oleInvoiceRecordHandler = new OleInvoiceRecordHandler();
674         Map<String, List<OleInvoiceRecord>> map = new HashMap();
675         try {
676             if (this.processDef.getUploadFileName().endsWith(".mrc")) {
677                 convertXMLToPojos(map, marcXMLContent);
678             } else {
679                 convertXMLToPojos(map);
680             }
681         } catch (Exception e) {
682             createBatchErrorAttachmentFile(failureRecords.append(e.getMessage()).toString());
683             LOG.error(e, e);
684             job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
685             job.setStatusDesc(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
686             throw e;
687         }
688         oleInvoiceRecordHandler.setOleInvoiceLineItemRecords(map);
689         int totalInvoiceFileCount = 0;
690         // int totalInvoiceProcessedCount = 0;
691         int successCount = 0;
692         for (Map.Entry invoiceRecordList : oleInvoiceRecordHandler.getOleInvoiceLineItemRecords().entrySet()) {
693              List<OleInvoiceRecord> oleInvoiceRecord = (List<OleInvoiceRecord>)invoiceRecordList.getValue();
694             totalInvoiceFileCount = oleInvoiceRecordHandler.getOleInvoiceLineItemRecords().size();
695 
696             Person currentUser = GlobalVariables.getUserSession().getPerson();
697 
698             try {
699                 invoiceDocument = initiateInvoiceDocument(invoiceDocument, currentUser);
700             } catch (Exception e) {
701                 LOG.error(e, e);
702                 failureRecords.append("Unable to create Invoice document");
703                 failureRecords.append("\n");
704                 createBatchErrorAttachmentFile(failureRecords.append(e.getMessage()).toString());
705                 //job.setStatusDesc(OLEConstants.OLEBatchProcess.JOB_STATUS_STOPPED);
706             }
707             //Map<Integer, List<OleInvoiceRecord>> oleInvoiceRecord = oleInvoiceRecordHandler.getOleInvoiceLineItemRecords();
708             //List<OleInvoiceRecord> oleInvoiceRecordList = oleInvoiceRecord.get(0);
709             SimpleDateFormat dateFromRawFile = new SimpleDateFormat("yyyyMMdd");
710             OleInvoiceService oleInvoiceService = getInvoiceService();
711            // if (oleInvoiceRecordList != null && !oleInvoiceRecordList.isEmpty()) {
712                 OleInvoiceRecord invoiceRecordHeader = oleInvoiceRecord.get(0);
713                 invoiceDocument.setInvoiceNumber(invoiceRecordHeader.getInvoiceNumber());
714                 //invoiceDocument.setVendorInvoiceAmount(new KualiDecimal(invoiceRecordHeader.getVendorInvoiceAmount()));
715                 invoiceDocument.setVendorInvoiceAmount(null);
716                 Date rawDate = null;
717                 try {
718                     rawDate = dateFromRawFile.parse(invoiceRecordHeader.getInvoiceDate());
719                 } catch (Exception e) {
720                     LOG.error(e, e);
721                     failureRecords.append("Invoice Date: " + invoiceRecordHeader.getInvoiceDate());
722                     failureRecords.append("\n");
723                     failureRecords.append("Cannot parse Invoice Date" + e);
724                     createBatchErrorAttachmentFile(failureRecords.toString());
725                     throw e;
726 
727                 }
728                 if (rawDate != null) {
729                     invoiceDocument.setInvoiceDate(new java.sql.Date(rawDate.getTime()));
730                 }
731             //}
732             int lineItemCount = 0;
733             for (int j = 0; j < oleInvoiceRecord.size(); j++) {
734                 invoiceRecord = oleInvoiceRecord.get(j);
735                     String[] vendorIds = invoiceRecord.getVendorNumber() != null ? invoiceRecord.getVendorNumber().split("-") : new String[0];
736                     if(!StringUtils.isBlank(invoiceRecord.getCurrencyTypeId())){
737                         setDocumentForeignDetails(invoiceDocument,invoiceRecord);
738                     }
739                     else {
740                         Map vendorDetailMap = new HashMap();
741                         vendorDetailMap.put("vendorHeaderGeneratedIdentifier", vendorIds.length > 0 ? vendorIds[0] : "");
742                         vendorDetailMap.put("vendorDetailAssignedIdentifier", vendorIds.length > 1 ? vendorIds[1] : "");
743                         VendorDetail vendorDetail = (VendorDetail) getBusinessObjectService().findByPrimaryKey(VendorDetail.class, vendorDetailMap);
744                         if(vendorDetail != null){
745                             invoiceRecord.setCurrencyTypeId(vendorDetail.getCurrencyTypeId().toString());
746                             setDocumentForeignDetails(invoiceDocument,invoiceRecord);
747                         }
748                     }
749                     List<OlePurchaseOrderItem> olePurchaseOrderItems = null;
750                     Map vendorItemIdentifier = new HashMap();
751                     vendorItemIdentifier.put("vendorItemPoNumber", invoiceRecord.getVendorItemIdentifier());
752                     vendorItemIdentifier.put("purchaseOrder.vendorHeaderGeneratedIdentifier", vendorIds.length > 0 ? vendorIds[0] : "");
753                     vendorItemIdentifier.put("purchaseOrder.vendorDetailAssignedIdentifier", vendorIds.length > 1 ? vendorIds[1] : "");
754                     List<OlePurchaseOrderItem> dummyPurchaseOrderItems = null;
755                     if (invoiceRecord.getVendorItemIdentifier() != null) {
756                         dummyPurchaseOrderItems = (List<OlePurchaseOrderItem>) getBusinessObjectService().findMatching(OlePurchaseOrderItem.class, vendorItemIdentifier);
757                         Collections.sort(dummyPurchaseOrderItems,new Comparator<OlePurchaseOrderItem>(){
758                             public int compare(OlePurchaseOrderItem dummyPurchaseOrderItems1,OlePurchaseOrderItem dummyPurchaseOrderItems2){
759                                 return dummyPurchaseOrderItems2.getDocumentNumber().compareTo(dummyPurchaseOrderItems1.getDocumentNumber());
760                             }
761                         });
762                         if (dummyPurchaseOrderItems != null && dummyPurchaseOrderItems.size() > 0) {
763                             String documentNumber = dummyPurchaseOrderItems.get(0).getDocumentNumber();
764                             olePurchaseOrderItems = new ArrayList<>();
765                             for(int itemCount = 0;itemCount < dummyPurchaseOrderItems.size();itemCount++){
766                                 if(documentNumber.equalsIgnoreCase(dummyPurchaseOrderItems.get(itemCount).getDocumentNumber())){
767                                     olePurchaseOrderItems.add(dummyPurchaseOrderItems.get(itemCount));
768                                 }
769                             }
770                         }
771                     }
772                     PurchaseOrderDocument purchaseOrderDocument = null;
773                     HashMap itemMap = new HashMap();
774                     if (olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0) {
775 
776                         invoiceRecord.setPurchaseOrderNumber((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0) ? olePurchaseOrderItems.get(0).getPurapDocumentIdentifier() : null);
777                         if ((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 && olePurchaseOrderItems.get(0) != null && olePurchaseOrderItems.get(0).getPurchaseOrder() != null && olePurchaseOrderItems.get(0).getPurchaseOrder().getPostingYear() != null)) {
778                             invoiceDocument.setPostingYear((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0) ? olePurchaseOrderItems.get(0).getPurchaseOrder().getPostingYear() : null);
779                         }
780                         //invoiceDocument.setProrateBy(org.kuali.ole.sys.OLEConstants.MANUAL_PRORATE);
781                         invoiceDocument.setInvoicePayDate(SpringContext.getBean(InvoiceService.class).calculatePayDate(invoiceDocument.getInvoiceDate(), invoiceDocument.getVendorPaymentTerms()));
782                         itemMap = addInvoiceItem(olePurchaseOrderItems, invoiceRecord, invoiceDocument, purchaseOrderDocument, itemMap);
783                         if (itemMap != null) {
784                             invoiceDocument = (OleInvoiceDocument) itemMap.get("invoiceDocument");
785                             purchaseOrderDocument = (PurchaseOrderDocument) itemMap.get("purchaseOrderDocument");
786                         }
787                         //totalInvoiceProcessedCount = i+1;
788                     } else if (olePurchaseOrderItems == null || olePurchaseOrderItems.size() < 1) {
789                         if (invoiceRecord.getVendorNumber() != null && !invoiceRecord.getVendorNumber().isEmpty()) {
790                             if ((invoiceRecord.getISBN() != null && !invoiceRecord.getISBN().isEmpty() ||
791                                     (invoiceRecord.getISSN() != null && !invoiceRecord.getISSN().isEmpty()))) {
792                                 org.kuali.ole.docstore.common.search.SearchParams search_Params = new org.kuali.ole.docstore.common.search.SearchParams();
793                                 SearchResponse searchResponse = null;
794                                 // Retrieve bib id through solr query
795                                 //StringBuffer query = new StringBuffer("");
796                                 //List<HashMap<String, Object>> documentList1 = null;
797                                 //query.append("(ISBN_search:" + invoiceRecord.getISBN() + ")");
798                                 if  (invoiceRecord.getISBN() != null && !invoiceRecord.getISBN().isEmpty()) {
799                                     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()), ""));
800                                 }
801                                 if (invoiceRecord.getISSN() != null && !invoiceRecord.getISSN().isEmpty()) {
802                                     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()), ""));
803                                 }
804                                 search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), "id"));
805                                 String titleId = null;
806                                 searchResponse = getDocstoreClientLocator().getDocstoreClient().search(search_Params);
807                                 if (searchResponse.getSearchResults() != null && searchResponse.getSearchResults().size() > 0) {
808                                     titleId = searchResponse.getSearchResults().get(0).getSearchResultFields().get(0).getFieldValue() != null ? searchResponse.getSearchResults().get(0).getSearchResultFields().get(0).getFieldValue() : "";
809                                 }
810 
811 
812                                 //documentList1 = QueryServiceImpl.getInstance().retriveResults(query.toString());
813                                 //Map map1 = documentList1 != null && documentList1.size() > 0 ? documentList1.get(0) : null;
814 
815                                 //String titleId = map1 != null ? (String) map1.get("id") : null;
816 
817                                 Map itemTitleIdMap = new HashMap();
818                                 itemTitleIdMap.put("itemTitleId", titleId);
819                                 itemTitleIdMap.put("purchaseOrder.vendorHeaderGeneratedIdentifier", vendorIds.length > 0 ? vendorIds[0] : "");
820                                 itemTitleIdMap.put("purchaseOrder.vendorDetailAssignedIdentifier", vendorIds.length > 1 ? vendorIds[1] : "");
821                                 if (titleId != null) {
822                                     olePurchaseOrderItems = (List<OlePurchaseOrderItem>) getBusinessObjectService().findMatching(OlePurchaseOrderItem.class, itemTitleIdMap);
823 
824                                     invoiceRecord.setPurchaseOrderNumber((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 && olePurchaseOrderItems.get(0) != null) ? olePurchaseOrderItems.get(0).getPurapDocumentIdentifier() : null);
825                                     if ((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 && olePurchaseOrderItems.get(0) != null && olePurchaseOrderItems.get(0).getPurchaseOrder() != null && olePurchaseOrderItems.get(0).getPurchaseOrder().getPostingYear() != null)) {
826                                         invoiceDocument.setPostingYear(olePurchaseOrderItems.get(0).getPurchaseOrder().getPostingYear());
827                                     }
828                                     //invoiceDocument.setProrateBy(org.kuali.ole.sys.OLEConstants.MANUAL_PRORATE);
829                                     invoiceDocument.setInvoicePayDate(SpringContext.getBean(InvoiceService.class).calculatePayDate(invoiceDocument.getInvoiceDate(), invoiceDocument.getVendorPaymentTerms()));
830                                     itemMap = addInvoiceItem(olePurchaseOrderItems, invoiceRecord, invoiceDocument, purchaseOrderDocument, itemMap);
831                                     if (itemMap != null) {
832                                         invoiceDocument = (OleInvoiceDocument) itemMap.get("invoiceDocument");
833                                         purchaseOrderDocument = (PurchaseOrderDocument) itemMap.get("purchaseOrderDocument");
834                                     }
835                                     // totalInvoiceProcessedCount = i+1;
836                                 } else if (olePurchaseOrderItems == null || olePurchaseOrderItems.size() < 1) {
837 
838                                     String[] vendorId = invoiceRecord.getVendorNumber() != null ? invoiceRecord.getVendorNumber().split("-") : new String[0];
839                                     Map itemTitleId = new HashMap();
840                                     itemTitleId.put("purchaseOrder.purapDocumentIdentifier", invoiceRecord.getPurchaseOrderNumber());
841                                     itemTitleId.put("purchaseOrder.vendorHeaderGeneratedIdentifier", vendorId.length > 0 ? vendorId[0] : "");
842                                     itemTitleId.put("purchaseOrder.vendorDetailAssignedIdentifier", vendorId.length > 1 ? vendorId[1] : "");
843                                     if (invoiceRecord.getPurchaseOrderNumber() != null) {
844                                         olePurchaseOrderItems = (List<OlePurchaseOrderItem>) getBusinessObjectService().findMatching(OlePurchaseOrderItem.class, itemTitleId);
845                                         if (olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0) {
846                                             invoiceRecord.setPurchaseOrderNumber((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 && olePurchaseOrderItems.get(0) != null) ? olePurchaseOrderItems.get(0).getPurapDocumentIdentifier() : null);
847                                             if ((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 && olePurchaseOrderItems.get(0) != null && olePurchaseOrderItems.get(0).getPurchaseOrder() != null && olePurchaseOrderItems.get(0).getPurchaseOrder().getPostingYear() != null)) {
848                                                 invoiceDocument.setPostingYear(olePurchaseOrderItems.get(0).getPurchaseOrder().getPostingYear());
849                                             }
850                                             //invoiceDocument.setProrateBy(org.kuali.ole.sys.OLEConstants.MANUAL_PRORATE);
851                                             invoiceDocument.setInvoicePayDate(SpringContext.getBean(InvoiceService.class).calculatePayDate(invoiceDocument.getInvoiceDate(), invoiceDocument.getVendorPaymentTerms()));
852 
853                                             itemMap = addInvoiceItem(olePurchaseOrderItems, invoiceRecord, invoiceDocument, purchaseOrderDocument, itemMap);
854                                             if (itemMap != null) {
855                                                 invoiceDocument = (OleInvoiceDocument) itemMap.get("invoiceDocument");
856                                                 purchaseOrderDocument = (PurchaseOrderDocument) itemMap.get("purchaseOrderDocument");
857                                             }
858                                             // totalInvoiceProcessedCount = i+1;
859                                         }
860                                     }
861                                 }
862                             }
863                             else if (invoiceRecord.getPurchaseOrderNumber() != null) {
864                                 String[] vendorId = invoiceRecord.getVendorNumber() != null ? invoiceRecord.getVendorNumber().split("-") : new String[0];
865                                 Map itemTitleId = new HashMap();
866                                 itemTitleId.put("purchaseOrder.purapDocumentIdentifier", invoiceRecord.getPurchaseOrderNumber());
867                                 itemTitleId.put("purchaseOrder.vendorHeaderGeneratedIdentifier", vendorId.length > 0 ? vendorId[0] : "");
868                                 itemTitleId.put("purchaseOrder.vendorDetailAssignedIdentifier", vendorId.length > 1 ? vendorId[1] : "");
869                                 if (invoiceRecord.getPurchaseOrderNumber() != null) {
870                                     olePurchaseOrderItems = (List<OlePurchaseOrderItem>) getBusinessObjectService().findMatching(OlePurchaseOrderItem.class, itemTitleId);
871                                     if (olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0) {
872                                         invoiceRecord.setPurchaseOrderNumber((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 && olePurchaseOrderItems.get(0) != null) ? olePurchaseOrderItems.get(0).getPurapDocumentIdentifier() : null);
873                                         if ((olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 && olePurchaseOrderItems.get(0) != null && olePurchaseOrderItems.get(0).getPurchaseOrder() != null && olePurchaseOrderItems.get(0).getPurchaseOrder().getPostingYear() != null)) {
874                                             invoiceDocument.setPostingYear(olePurchaseOrderItems.get(0).getPurchaseOrder().getPostingYear());
875                                         }
876                                         //invoiceDocument.setProrateBy(org.kuali.ole.sys.OLEConstants.MANUAL_PRORATE);
877                                         invoiceDocument.setInvoicePayDate(SpringContext.getBean(InvoiceService.class).calculatePayDate(invoiceDocument.getInvoiceDate(), invoiceDocument.getVendorPaymentTerms()));
878 
879                                         itemMap = addInvoiceItem(olePurchaseOrderItems, invoiceRecord, invoiceDocument, purchaseOrderDocument, itemMap);
880                                         if (itemMap != null) {
881                                             invoiceDocument = (OleInvoiceDocument) itemMap.get("invoiceDocument");
882                                             purchaseOrderDocument = (PurchaseOrderDocument) itemMap.get("purchaseOrderDocument");
883                                         }
884                                         // totalInvoiceProcessedCount = i+1;
885                                     }
886                                 }
887                             }
888                         }
889 
890 
891                     }
892                     if (purchaseOrderDocument != null) {
893                         for (OleInvoiceItem item : (List<OleInvoiceItem>) invoiceDocument.getItems()) {
894                             if (invoiceRecord.getAdditionalChargeCode() != null && invoiceRecord.getAdditionalChargeCode().equalsIgnoreCase("SVC") && item.getItemTypeCode().equalsIgnoreCase("SPHD")) {
895                                 item.setItemUnitPrice(new BigDecimal(invoiceRecord.getAdditionalCharge() != null ? invoiceRecord.getAdditionalCharge() : invoiceRecord.getAdditionalCharge()));
896                                 item.setPurchaseOrderIdentifier(purchaseOrderDocument.getPurapDocumentIdentifier());
897                             } else if (invoiceRecord.getLineItemAdditionalChargeCode() != null && invoiceRecord.getLineItemAdditionalChargeCode().equalsIgnoreCase("LD") && item.getItemTypeCode().equalsIgnoreCase("ITEM")) {
898 
899                                 if ((item.getItemDescription().contains(invoiceRecord.getISBN())
900                                         || (invoiceRecord.getVendorItemIdentifier() != null ? invoiceRecord.getVendorItemIdentifier().equalsIgnoreCase(olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 ? item.getVendorItemIdentifier() : "") : false)
901                                         || (invoiceRecord.getPurchaseOrderNumber() != null ? invoiceRecord.getPurchaseOrderNumber().equals(item.getPurchaseOrderDocument() != null ? item.getPurchaseOrderDocument().getPurapDocumentIdentifier() : null) : false))) {
902 
903 
904                                     item.setItemDiscountType("%"); // % or #
905                                     item.setItemDiscount(new KualiDecimal(invoiceRecord.getLineItemAdditionalCharge() != null ? invoiceRecord.getLineItemAdditionalCharge() : invoiceRecord.getLineItemAdditionalCharge()));
906                                 }
907                             }
908                         }
909 
910                         String vendorNumber = purchaseOrderDocument.getVendorHeaderGeneratedIdentifier() + "-" + purchaseOrderDocument.getVendorDetailAssignedIdentifier();
911                         oleInvoiceService.populateVendorDetail(vendorNumber, invoiceDocument);
912                         invoiceDocument.setVendorCustomerNumber(invoiceRecord.getBillToCustomerID());
913                         if (invoiceDocument.getPaymentMethodId() != null) {
914                             invoiceDocument.setPaymentMethodIdentifier(String.valueOf(invoiceDocument.getPaymentMethodId()));
915                         } else {
916                             invoiceDocument.setPaymentMethodId(Integer.parseInt(OLEConstants.OleInvoiceImport.PAY_METHOD));
917                             invoiceDocument.setPaymentMethodIdentifier(String.valueOf(invoiceDocument.getPaymentMethodId()));
918                         }
919                         //   SpringContext.getBean(OleInvoiceService.class).calculateProrateItemSurcharge(invoiceDocument);
920                         SpringContext.getBean(PurapAccountingService.class).updateAccountAmounts(invoiceDocument);
921                         Long nextLinkIdentifier = SpringContext.getBean(SequenceAccessorService.class).getNextAvailableSequenceNumber("AP_PUR_DOC_LNK_ID");
922                         invoiceDocument.setAccountsPayablePurchasingDocumentLinkIdentifier(nextLinkIdentifier.intValue());
923 
924 
925                         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))) {
926                             // set amounts on any empty
927                             invoiceDocument.updateExtendedPriceOnItems();
928 
929                             // calculation just for the tax area, only at tax review stage
930                             // by now, the general calculation shall have been done.
931                             if (invoiceDocument.getApplicationDocumentStatus().equals(PurapConstants.PaymentRequestStatuses.APPDOC_AWAITING_TAX_REVIEW)) {
932                                 SpringContext.getBean(OleInvoiceService.class).calculateTaxArea(invoiceDocument);
933                             }
934 
935                             updatePrice(invoiceDocument);
936 
937                             // notice we're ignoring whether the boolean, because these are just warnings they shouldn't halt anything
938                             // Calculate Payment request before rules since the rule check totalAmount.
939                             SpringContext.getBean(OleInvoiceService.class).calculateInvoice(invoiceDocument, true);
940                             SpringContext.getBean(KualiRuleService.class).applyRules(
941                                     new AttributedCalculateAccountsPayableEvent(invoiceDocument));
942                         } else {
943                             // set amounts on any empty
944                             invoiceDocument.updateExtendedPriceOnItems();
945 
946                             // calculation just for the tax area, only at tax review stage
947                             // by now, the general calculation shall have been done.
948                             if (StringUtils.equals(invoiceDocument.getApplicationDocumentStatus(), PurapConstants.PaymentRequestStatuses.APPDOC_AWAITING_TAX_REVIEW)) {
949                                 SpringContext.getBean(OleInvoiceService.class).calculateTaxArea(invoiceDocument);
950 
951                             }
952 
953                             updatePrice(invoiceDocument);
954 
955                             // notice we're ignoring whether the boolean, because these are just warnings they shouldn't halt anything
956                             //Calculate Payment request before rules since the rule check totalAmount.
957                             SpringContext.getBean(OleInvoiceService.class).calculateInvoice(invoiceDocument, true);
958                             SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedCalculateAccountsPayableEvent(invoiceDocument));
959                         }
960                         if (!SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedSaveDocumentEvent(invoiceDocument)) ||
961                                 !SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedCalculateAccountsPayableEvent(invoiceDocument))) {
962 
963 
964                             //}else {
965                             List invoiceIngestFailureReason = (List) dataCarrierService.getData("invoiceIngestFailureReason");
966                             if(invoiceIngestFailureReason != null && invoiceIngestFailureReason.size()>0){
967                                 for(int failCount = 0;failCount < invoiceIngestFailureReason.size();failCount++){
968                                     failureRecords.append(invoiceIngestFailureReason.get(failCount) + "\n");
969                                 }
970                             }
971                             LOG.info("Invoice Error Message------------------->");
972                             failureRecords.append("Unable to create Invoice document");
973                             failureRecords.append("\n");
974                             failureRecords.append("Invoice Number :" + invoiceRecord.getInvoiceNumber());
975                             failureRecords.append("\n");
976                             failureRecords.append("Invoice Date :" + invoiceDocument.getInvoiceDate());
977                             failureRecords.append("\n");
978                             if ((GlobalVariables.getMessageMap().getErrorMessages().get("GLOBAL_ERRORS")) != null) {
979                                 for (int error = 0; error < (GlobalVariables.getMessageMap().getErrorMessages().get("GLOBAL_ERRORS")).size(); error++) {
980                                     failureRecords.append("Error Message:" + kualiConfiguration.getPropertyValueAsString((GlobalVariables.getMessageMap().getErrorMessages().get("GLOBAL_ERRORS")).get(error).getErrorKey()));
981                                     failureRecords.append("\n");
982                                 }
983                             }
984                             failureRecords.append("\n");
985                         }
986                     } else {
987                         if(marcXMLContent != null && StringUtils.isNotBlank(marcXMLContent.toString())){
988                             createInvoiceItem(invoiceRecord, invoiceDocument, itemMap);
989                         }
990                         else {
991                             createInvoiceItem(lineItemCount, invoiceRecord, invoiceDocument, itemMap);
992                         }
993                         invoiceDocument.setAccountsPayablePurchasingDocumentLinkIdentifier((SpringContext.getBean(SequenceAccessorService.class).
994                                 getNextAvailableSequenceNumber("AP_PUR_DOC_LNK_ID")).intValue());
995                         if (itemMap != null) {
996                             invoiceDocument = (OleInvoiceDocument) itemMap.get("invoiceDocument");
997                             purchaseOrderDocument = (PurchaseOrderDocument) itemMap.get("purchaseOrderDocument");
998                         }
999                         for (OleInvoiceItem item : (List<OleInvoiceItem>) invoiceDocument.getItems()) {
1000                             if (invoiceRecord.getAdditionalChargeCode() != null && invoiceRecord.getAdditionalChargeCode().equalsIgnoreCase("SVC") && item.getItemTypeCode().equalsIgnoreCase("SPHD")) {
1001                                 item.setItemUnitPrice(new BigDecimal(invoiceRecord.getAdditionalCharge() != null ? invoiceRecord.getAdditionalCharge() : invoiceRecord.getAdditionalCharge()));
1002                                 item.setPurchaseOrderIdentifier(purchaseOrderDocument != null && purchaseOrderDocument.getPurapDocumentIdentifier() != null ?
1003                                         purchaseOrderDocument.getPurapDocumentIdentifier() : null);
1004                             } else if (invoiceRecord.getLineItemAdditionalChargeCode() != null && invoiceRecord.getLineItemAdditionalChargeCode().equalsIgnoreCase("LD") && item.getItemTypeCode().equalsIgnoreCase("ITEM")) {
1005 
1006                                 if ((item.getItemDescription().contains(invoiceRecord.getISBN())
1007                                         || (invoiceRecord.getVendorItemIdentifier() != null ? invoiceRecord.getVendorItemIdentifier().equalsIgnoreCase(olePurchaseOrderItems != null && olePurchaseOrderItems.size() > 0 ? item.getVendorItemIdentifier() : "") : false)
1008                                         || (invoiceRecord.getPurchaseOrderNumber() != null ? invoiceRecord.getPurchaseOrderNumber().equals(item.getPurchaseOrderDocument() != null ? item.getPurchaseOrderDocument().getPurapDocumentIdentifier(): null):false))) {
1009 
1010 
1011                                     item.setItemDiscountType("#"); // % or #
1012                                     item.setItemDiscount(new KualiDecimal(invoiceRecord.getLineItemAdditionalCharge() != null ? invoiceRecord.getLineItemAdditionalCharge() : invoiceRecord.getLineItemAdditionalCharge()));
1013                                 }
1014                             }
1015                         }
1016 
1017                         String vendorNumber = invoiceRecord.getVendorNumber();
1018                         oleInvoiceService.populateVendorDetail(vendorNumber, invoiceDocument);
1019                         invoiceDocument.setVendorCustomerNumber(invoiceRecord.getBillToCustomerID());
1020                         if (invoiceDocument.getPaymentMethodId() != null) {
1021                             invoiceDocument.setPaymentMethodIdentifier(String.valueOf(invoiceDocument.getPaymentMethodId()));
1022                         } else {
1023                             invoiceDocument.setPaymentMethodId(Integer.parseInt(OLEConstants.OleInvoiceImport.PAY_METHOD));
1024                             invoiceDocument.setPaymentMethodIdentifier(String.valueOf(invoiceDocument.getPaymentMethodId()));
1025                         }
1026                         //   SpringContext.getBean(OleInvoiceService.class).calculateProrateItemSurcharge(invoiceDocument);
1027                         SpringContext.getBean(PurapAccountingService.class).updateAccountAmounts(invoiceDocument);
1028                         Long nextLinkIdentifier = SpringContext.getBean(SequenceAccessorService.class).getNextAvailableSequenceNumber("AP_PUR_DOC_LNK_ID");
1029                         // invoiceDocument.setAccountsPayablePurchasingDocumentLinkIdentifier(nextLinkIdentifier.intValue());
1030 
1031 
1032                         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))) {
1033                             // set amounts on any empty
1034                             invoiceDocument.updateExtendedPriceOnItems();
1035 
1036                             // calculation just for the tax area, only at tax review stage
1037                             // by now, the general calculation shall have been done.
1038                             if (invoiceDocument.getApplicationDocumentStatus().equals(PurapConstants.PaymentRequestStatuses.APPDOC_AWAITING_TAX_REVIEW)) {
1039                                 SpringContext.getBean(OleInvoiceService.class).calculateTaxArea(invoiceDocument);
1040                             }
1041 
1042                             updatePrice(invoiceDocument);
1043 
1044                             // notice we're ignoring whether the boolean, because these are just warnings they shouldn't halt anything
1045                             // Calculate Payment request before rules since the rule check totalAmount.
1046                             SpringContext.getBean(OleInvoiceService.class).calculateInvoice(invoiceDocument, true);
1047                             SpringContext.getBean(KualiRuleService.class).applyRules(
1048                                     new AttributedCalculateAccountsPayableEvent(invoiceDocument));
1049                         } else {
1050                             // set amounts on any empty
1051                             invoiceDocument.updateExtendedPriceOnItems();
1052 
1053                             // calculation just for the tax area, only at tax review stage
1054                             // by now, the general calculation shall have been done.
1055                             if (StringUtils.equals(invoiceDocument.getApplicationDocumentStatus(), PurapConstants.PaymentRequestStatuses.APPDOC_AWAITING_TAX_REVIEW)) {
1056                                 SpringContext.getBean(OleInvoiceService.class).calculateTaxArea(invoiceDocument);
1057 
1058                             }
1059 
1060                             updatePrice(invoiceDocument);
1061 
1062                             // notice we're ignoring whether the boolean, because these are just warnings they shouldn't halt anything
1063                             //Calculate Payment request before rules since the rule check totalAmount.
1064                             SpringContext.getBean(OleInvoiceService.class).calculateInvoice(invoiceDocument, true);
1065                             SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedCalculateAccountsPayableEvent(invoiceDocument));
1066                         }
1067 
1068 
1069                     }
1070                 }
1071             if (!"".equals(failureRecords.toString())) {
1072                 createBatchErrorAttachmentFile(failureRecords.toString());
1073             }
1074             allInvoiceDocument.add(invoiceDocument);
1075         }
1076         deleteBatchFile();
1077         if (allInvoiceDocument != null && !allInvoiceDocument.isEmpty()) {
1078             for (int process = 0; process < allInvoiceDocument.size(); process++) {
1079                 invoiceDocument = allInvoiceDocument.get(process);
1080                 if (invoiceDocument.getAccountsPayablePurchasingDocumentLinkIdentifier() != null) {
1081                     if (SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedSaveDocumentEvent(invoiceDocument)) &&
1082                             SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedCalculateAccountsPayableEvent(invoiceDocument))) {
1083 
1084                         try {
1085                             OLEInvoiceIngestLoadReport oleInvoiceIngestLoadReport =new OLEInvoiceIngestLoadReport();
1086                             oleInvoiceIngestLoadReport.setFileName(job.getUploadFileName());
1087                             java.sql.Date sqlDate = new java.sql.Date(new Date(System.currentTimeMillis()).getTime());
1088                             oleInvoiceIngestLoadReport.setDateUploaded(sqlDate);
1089                             oleInvoiceIngestLoadReport.setVendor(invoiceDocument.getVendorNumber());
1090                             getBusinessObjectService().save(oleInvoiceIngestLoadReport);
1091                             oleInvoiceService.autoApprovePaymentRequest(invoiceDocument);
1092                             successCount = process + 1;
1093                         } catch (Exception e) {
1094                             LOG.info("Invoice Error Message------------------->");
1095                             failureRecords.append("Unable to create Invoice document");
1096                             failureRecords.append("\n");
1097                             failureRecords.append("Invoice Number :" + invoiceRecord.getInvoiceNumber());
1098                             failureRecords.append("\n");
1099                             failureRecords.append("Invoice Date :" + invoiceDocument.getInvoiceDate());
1100                             failureRecords.append("\n");
1101                             if ((GlobalVariables.getMessageMap() != null && GlobalVariables.getMessageMap().getErrorMessages() != null) &&
1102                                     ((GlobalVariables.getMessageMap().getErrorMessages().size() > 0 ) ||
1103                                     (GlobalVariables.getMessageMap().getErrorMessages().get("GLOBAL_ERRORS")) != null)) {
1104                                 for (int error = 0; error < (GlobalVariables.getMessageMap().getErrorMessages().get("GLOBAL_ERRORS")).size(); error++) {
1105                                     failureRecords.append("Error Message:" + kualiConfiguration.getPropertyValueAsString((GlobalVariables.getMessageMap().getErrorMessages().get("GLOBAL_ERRORS")).get(error).getErrorKey()));
1106                                     failureRecords.append("\n");
1107                                 }
1108                             } else {
1109                                 failureRecords.append("Error Message:" + e.initCause(new Throwable()));
1110                             }
1111                             failureRecords.append("\n");
1112                             failureRecords.append("\n");
1113 
1114                         }
1115 
1116                         job.setTotalNoOfRecords(totalInvoiceFileCount + "");
1117                         job.setNoOfRecordsProcessed(totalInvoiceFileCount + "");
1118                         job.setNoOfSuccessRecords(String.valueOf(successCount));
1119                         job.setNoOfFailureRecords(String.valueOf(totalInvoiceFileCount - successCount));
1120                         job.setStatusDesc(OLEConstants.INVOICE_UPLOAD_SUCCESS);
1121                         job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
1122                         deleteBatchFile();
1123                         if (!"".equals(failureRecords.toString())) {
1124                             createBatchErrorAttachmentFile(failureRecords.toString());
1125                         }
1126                     }
1127                     else {
1128                         if ((GlobalVariables.getMessageMap() != null &&
1129                                 GlobalVariables.getMessageMap().getErrorMessages() != null &&
1130                                 GlobalVariables.getMessageMap().hasErrors()) &&
1131                                 (GlobalVariables.getMessageMap().getErrorMessages().size() > 0 )) {
1132                                 Map<String, AutoPopulatingList<ErrorMessage>> errorMap = GlobalVariables.getMessageMap().getErrorMessages();
1133                                 for (Map.Entry<String, AutoPopulatingList<ErrorMessage>> entry : errorMap.entrySet()) {
1134                                     AutoPopulatingList<ErrorMessage> errors = entry.getValue();
1135                                     /*ErrorMessage error = errors.get(0);*/
1136                                     /*String[] params = error.getMessageParameters();
1137                                     String param = params[0];
1138                                     if (param.toUpperCase().contains("ITEM")) {
1139                                         param = error.getErrorKey();
1140                                     }*/
1141                                     for(ErrorMessage error : errors){
1142                                         failureRecords.append("Error Message:" + MessageFormat.format(kualiConfiguration.getPropertyValueAsString(error.getErrorKey()), error.getMessageParameters()));
1143                                         failureRecords.append("\n");
1144                                     }
1145 
1146                             }
1147                         }
1148                         job.setTotalNoOfRecords(totalInvoiceFileCount + "");
1149                         job.setNoOfRecordsProcessed(totalInvoiceFileCount + "");
1150                         job.setNoOfSuccessRecords(String.valueOf(successCount));
1151                         job.setNoOfFailureRecords(String.valueOf(totalInvoiceFileCount - successCount));
1152                         job.setStatusDesc(OLEConstants.INVOICE_UPLOAD_SUCCESS);
1153                         job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
1154                         deleteBatchFile();
1155                         if (!"".equals(failureRecords.toString())) {
1156                             createBatchErrorAttachmentFile(failureRecords.toString());
1157                         }
1158                     }
1159                     job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
1160                 }
1161                 job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
1162             }
1163             job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
1164         }
1165         if (successCount == 0) {
1166             OLEBatchProcessJobDetailsController.setBatchProcessJobStatusMap(job.getJobId(),OLEConstants.OLEBatchProcess.JOB_STATUS_STOPPED);
1167             job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_STOPPED);
1168         } else {
1169             OLEBatchProcessJobDetailsController.setBatchProcessJobStatusMap(job.getJobId(),OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
1170             job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
1171         }
1172     }
1173 
1174     private void setDocumentForeignDetails(OleInvoiceDocument invoiceDocument,OleInvoiceRecord invoiceRecord){
1175         OleCurrencyType oleCurrencyType = getBusinessObjectService().findBySinglePrimaryKey(OleCurrencyType.class,invoiceRecord.getCurrencyTypeId());
1176         invoiceDocument.setInvoiceCurrencyType(oleCurrencyType.getCurrencyTypeId().toString());
1177         if(!oleCurrencyType.getCurrencyType().equalsIgnoreCase(OleSelectConstant.CURRENCY_TYPE_NAME)){
1178             Map documentNumberMap = new HashMap();
1179             documentNumberMap.put(OleSelectConstant.CURRENCY_TYPE_ID, invoiceRecord.getCurrencyTypeId());
1180             List<OleExchangeRate> exchangeRateList = (List) getBusinessObjectService().findMatchingOrderBy(
1181                     OleExchangeRate.class, documentNumberMap, OleSelectConstant.EXCHANGE_RATE_DATE, false);
1182             Iterator iterator = exchangeRateList.iterator();
1183             if (iterator.hasNext()) {
1184                 invoiceDocument.setForeignVendorInvoiceAmount(new BigDecimal(0.00));
1185                 invoiceDocument.setInvoiceCurrencyTypeId(new Long(invoiceRecord.getCurrencyTypeId()));
1186                 invoiceDocument.setInvoiceCurrencyExchangeRate(invoiceRecord.getInvoiceCurrencyExchangeRate());
1187             }
1188         }
1189     }
1190 
1191     public boolean validateVendorNumber(String vendorNumber){
1192         LOG.info("----Inside validateVendorNumber()------------------------------");
1193         boolean isValidVendor = false;
1194         if(vendorNumber != null){
1195             String[] vendorDetail = vendorNumber.split("-");
1196             if(vendorDetail.length == 2){
1197                 String vendorHeaderGeneratedIdentifier = vendorDetail[0];
1198                 String vendorDetailAssignedIdentifier = vendorDetail[1];
1199                 try {
1200                     Map<String,Integer> vendorMap = new HashMap<>();
1201                     vendorMap.put(OLEConstants.VENDOR_HEADER_GENERATED_ID, Integer.parseInt(vendorHeaderGeneratedIdentifier));
1202                     vendorMap.put(OLEConstants.VENDOR_DETAILED_ASSIGNED_ID, Integer.parseInt(vendorDetailAssignedIdentifier));
1203                     List<VendorDetail> vendorDetailList = (List) getBusinessObjectService().findMatching(VendorDetail.class, vendorMap);
1204                     if(vendorDetail != null && vendorDetailList.size() > 0){
1205                         isValidVendor  = true;
1206                         return isValidVendor;
1207                     }
1208                 }
1209                 catch (NumberFormatException nfe) {
1210                     return isValidVendor;
1211                 }
1212             }
1213         }
1214         return isValidVendor;
1215     }
1216 
1217     public boolean validateForNumber(String fieldValue){
1218         try {
1219             Integer quantity = Integer.parseInt(fieldValue);
1220             if(quantity <= 0){
1221                 return false;
1222             }
1223         }
1224         catch(NumberFormatException nfe){
1225             return false;
1226         }
1227         return true;
1228     }
1229 
1230     public boolean validateDestinationFieldValues(String destinationFieldValue){
1231         try {
1232             Float fieldValue = Float.parseFloat(destinationFieldValue);
1233             if(fieldValue <= 0){
1234                 return false;
1235             }
1236         }
1237         catch(NumberFormatException nfe){
1238             return false;
1239         }
1240         return true;
1241     }
1242 
1243     @Override
1244     public void getNextBatch() {
1245         //To change body of implemented methods use File | Settings | File Templates.
1246     }
1247 
1248     @Override
1249     public void processBatch() {
1250         //To change body of implemented methods use File | Settings | File Templates.
1251     }
1252 
1253 }