1 package org.kuali.ole.ingest;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.apache.log4j.Logger;
5 import org.kuali.ole.OLEConstants;
6 import org.kuali.ole.pojo.OleInvoiceRecord;
7 import org.kuali.ole.pojo.edi.*;
8 import org.kuali.ole.sys.context.SpringContext;
9 import org.kuali.ole.vnd.businessobject.OleCurrencyType;
10 import org.kuali.rice.krad.service.BusinessObjectService;
11
12 import java.text.NumberFormat;
13 import java.text.ParsePosition;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17
18
19
20
21
22
23
24
25 public class OleInvoiceRecordBuilder {
26 private static final Logger LOG = Logger.getLogger(OleInvoiceRecordBuilder.class);
27 private static OleInvoiceRecordBuilder oleInvoiceRecordBuilder;
28 protected BusinessObjectService businessObjectService;
29
30
31
32
33
34 private OleInvoiceRecordBuilder(){
35
36 }
37
38
39
40
41
42
43 public static OleInvoiceRecordBuilder getInstance() {
44 if (null == oleInvoiceRecordBuilder) {
45 oleInvoiceRecordBuilder = new OleInvoiceRecordBuilder();
46 }
47 return oleInvoiceRecordBuilder;
48 }
49
50 protected BusinessObjectService getBusinessObjectService() {
51 if (businessObjectService == null) {
52 businessObjectService = SpringContext.getBean(BusinessObjectService.class);
53 }
54 return businessObjectService;
55 }
56
57
58 public OleInvoiceRecord build(LineItemOrder lineItemOrder, INVOrder invOrder) throws Exception {
59
60 OleInvoiceRecord oleInvoiceRecord = new OleInvoiceRecord();
61
62 oleInvoiceRecord.setListPrice(getListPrice(lineItemOrder));
63 oleInvoiceRecord.setUnitPrice(populateUnitPrice(lineItemOrder));
64 oleInvoiceRecord.setQuantity(getQuantity(lineItemOrder));
65 oleInvoiceRecord.setVendorItemIdentifier(getVendorItemIdentifier(lineItemOrder)!=null?getVendorItemIdentifier(lineItemOrder):getVendorIdentifierByPIA(lineItemOrder));
66 oleInvoiceRecord.setBfnNumber(getBFNNumber(lineItemOrder));
67 oleInvoiceRecord.setVendorNumber(getVendorNumber(invOrder,oleInvoiceRecord));
68 oleInvoiceRecord.setItemChartCode(OLEConstants.OleInvoiceImport.ITM_CHART_CODE);
69 oleInvoiceRecord.setInvoiceNumber(getInvoiceNumber(invOrder));
70 oleInvoiceRecord.setInvoiceDate(getInvoiceDate(invOrder));
71 oleInvoiceRecord.setVendorInvoiceAmount(getVendorInvoiceAmount(invOrder));
72
73
74 oleInvoiceRecord.setISBN(getISBN(lineItemOrder) == null? getLineItemISBN(lineItemOrder):getISBN(lineItemOrder));
75 oleInvoiceRecord.setISSN(getISSN(lineItemOrder));
76
77 oleInvoiceRecord.setBillToCustomerID(populateBillToCustomerId(invOrder));
78 oleInvoiceRecord.setItemDescription(getItemDescription(lineItemOrder));
79 oleInvoiceRecord.setNumberOfCopiesOrdered(oleInvoiceRecord.getQuantity());
80
81 oleInvoiceRecord.setSubscriptionPeriodFrom(getSubscriptionDateFrom(lineItemOrder));
82 oleInvoiceRecord.setSubscriptionPeriodTo(getSubscriptionDateTo(lineItemOrder));
83 oleInvoiceRecord.setSummaryAmount(getSummaryCharge(invOrder));
84 oleInvoiceRecord.setAdditionalChargeCode(getAdditionChargeCode(invOrder));
85 oleInvoiceRecord.setAdditionalCharge(getAdditionCharge(invOrder));
86 oleInvoiceRecord.setLineItemAdditionalChargeCode(populateLineItemChargeCode(lineItemOrder));
87 oleInvoiceRecord.setLineItemAdditionalCharge(populateLineItemAdditionalCharge(lineItemOrder));
88 oleInvoiceRecord.setPurchaseOrderNumber(getPurchaseOrderNumber(lineItemOrder));
89 setCurrencyType(invOrder,oleInvoiceRecord);
90 return oleInvoiceRecord;
91 }
92
93 private void setCurrencyType(INVOrder invOrder,OleInvoiceRecord oleInvoiceRecord){
94 if(invOrder.getMessage() != null && invOrder.getMessage().getCurrencyDetails() != null && invOrder.getMessage().getCurrencyDetails().getCurrencyDetailsSupplierInformation() != null){
95 if(!StringUtils.isBlank(invOrder.getMessage().getCurrencyDetails().getCurrencyDetailsSupplierInformation().getCurrencyType())){
96 Map<String,String> currencyTypeMap = new HashMap<>();
97 currencyTypeMap.put(OLEConstants.CURR_ALPHA_CD, invOrder.getMessage().getCurrencyDetails().getCurrencyDetailsSupplierInformation().getCurrencyType());
98 List<OleCurrencyType> currencyTypeList = (List) getBusinessObjectService().findMatching(OleCurrencyType.class, currencyTypeMap);
99 if(currencyTypeList != null && currencyTypeList.size() >0){
100 oleInvoiceRecord.setCurrencyTypeId(currencyTypeList.get(0).getCurrencyTypeId().toString());
101 oleInvoiceRecord.setCurrencyType(currencyTypeList.get(0).getCurrencyType());
102
103 }
104 }
105 }
106 }
107
108 private String populateBillToCustomerId(INVOrder invOrder){
109 if(invOrder.getMessage() != null && invOrder.getMessage().getPartyQualifier() != null && invOrder.getMessage().getPartyQualifier().size() > 0){
110 for(int i=0;i<invOrder.getMessage().getPartyQualifier().size();i++){
111 if(invOrder.getMessage().getPartyQualifier().get(i).getPartyCode()!=null && invOrder.getMessage().getPartyQualifier().get(i).getPartyCode().equalsIgnoreCase("BY")){
112 return invOrder.getMessage().getPartyQualifier().get(i).getPartyInformation().getCodeIdentification();
113 }
114 }
115 }
116 return null;
117 }
118
119 private Integer getPurchaseOrderNumber(LineItemOrder lineItemOrder)throws Exception{
120 if(lineItemOrder.getSupplierReferenceInformation() != null && lineItemOrder.getSupplierReferenceInformation().size() > 0){
121 for(int i=0;i<lineItemOrder.getSupplierReferenceInformation().size();i++){
122 if(lineItemOrder.getSupplierReferenceInformation().get(i).getSupplierLineItemReference() != null &&
123 lineItemOrder.getSupplierReferenceInformation().get(i).getSupplierLineItemReference().size() >0){
124 for(int j=0;j<lineItemOrder.getSupplierReferenceInformation().get(i).getSupplierLineItemReference().size();j++){
125 if(lineItemOrder.getSupplierReferenceInformation().get(i).getSupplierLineItemReference().get(j).getSuppliersOrderLine() != null &&
126 lineItemOrder.getSupplierReferenceInformation().get(i).getSupplierLineItemReference().get(j).getSuppliersOrderLine().equals("ON")){
127 try{
128 if (lineItemOrder.getSupplierReferenceInformation().get(i).getSupplierLineItemReference().get(j).getVendorReferenceNumber() != null &&
129 isNumeric(lineItemOrder.getSupplierReferenceInformation().get(i).getSupplierLineItemReference().get(j).getVendorReferenceNumber())) {
130 return Integer.parseInt(lineItemOrder.getSupplierReferenceInformation().get(i).getSupplierLineItemReference().get(j).getVendorReferenceNumber());
131 }
132 else {
133 return 0;
134 }
135
136
137 }catch(Exception e){
138 LOG.error("Purchase Order Number should be a number but invoice file has :"+lineItemOrder.getSupplierReferenceInformation().get(i).getSupplierLineItemReference().get(j).getVendorReferenceNumber());
139 throw new Exception("Purchase Order Number should be a number but invoice file has :"+lineItemOrder.getSupplierReferenceInformation().get(i).getSupplierLineItemReference().get(j).getVendorReferenceNumber(),e);
140 }
141 }
142 }
143
144 }
145 }
146
147 }
148
149
150
151
152
153 return null;
154 }
155
156 private String populateUnitPrice(LineItemOrder lineItemOrder){
157 if(lineItemOrder.getMonetaryDetail() != null){
158 if(lineItemOrder.getMonetaryDetail().get(0).getMonetaryLineItemInformation() != null){
159 return lineItemOrder.getMonetaryDetail().get(0).getMonetaryLineItemInformation().get(0).getAmount();
160 }
161 }
162 return null;
163 }
164
165 private String populateLineItemChargeCode(LineItemOrder lineItemOrder){
166 if(lineItemOrder.getLineItemAllowanceOrCharge() != null){
167 if(lineItemOrder.getLineItemAllowanceOrCharge().get(0).getLineItemSpecialServiceIdentification()!= null){
168 return lineItemOrder.getLineItemAllowanceOrCharge().get(0).getLineItemSpecialServiceIdentification().get(0).getSpecialServiceCode();
169 }
170 }
171 return null;
172 }
173
174 private String populateLineItemAdditionalCharge(LineItemOrder lineItemOrder){
175 if(lineItemOrder.getAllowanceMonetaryDetail()!= null){
176 if(lineItemOrder.getAllowanceMonetaryDetail().get(0).getAllowanceMonetaryLineItemInformation() != null){
177 return lineItemOrder.getAllowanceMonetaryDetail().get(0).getAllowanceMonetaryLineItemInformation().get(0).getAmount();
178 }
179 }
180 return null;
181 }
182
183
184 private String getAdditionCharge(INVOrder invOrder) {
185 if(invOrder.getMessage().getMonetary() != null){
186 if(invOrder.getMessage().getMonetary().getMonetaryInformation() != null){
187 return invOrder.getMessage().getMonetary().getMonetaryInformation().getAmount();
188 }
189 }
190 return null;
191 }
192
193
194 private String getAdditionChargeCode(INVOrder invOrder) {
195 if(invOrder.getMessage().getAllowanceOrCharge() != null){
196 if(invOrder.getMessage().getAllowanceOrCharge().getSpecialServiceIdentification() != null){
197 return invOrder.getMessage().getAllowanceOrCharge().getSpecialServiceIdentification().getSpecialServiceCode();
198 }
199 }
200 return null;
201 }
202
203
204
205
206
207
208
209
210 private String getListPrice(LineItemOrder lineItemOrder) {
211 List<PriceInformation> priceInformation = lineItemOrder.getPriceInformation();
212 if (priceInformation !=null && priceInformation.size() > 0) {
213 List<ItemPrice> itemPrice = priceInformation.get(0).getItemPrice();
214
215 if (itemPrice != null && itemPrice.size() > 0) {
216 String priceCode = itemPrice.get(0).getGrossPrice();
217 if(priceCode != null && (priceCode.equalsIgnoreCase("AAB") || priceCode.equalsIgnoreCase("CAL"))){
218 return itemPrice.get(0).getPrice();
219 }
220 }
221 }
222 return null;
223 }
224
225
226
227
228
229
230
231
232 private String getQuantity(LineItemOrder lineItemOrder) {
233 List<QuantityInformation> quantityInformation = lineItemOrder.getQuantityInformation();
234 if (quantityInformation != null && quantityInformation.size() > 0) {
235 List<Qunatity> qunatity = quantityInformation.get(0).getQunatity();
236 if (qunatity.size() > 0) {
237 return qunatity.get(0).getQuantity();
238 }
239 }
240 return null;
241 }
242
243 private String getVendorIdentifierByPIA(LineItemOrder lineItemOrder) {
244 List<ProductFunction> productFunction = lineItemOrder.getProductFunction();
245 if(productFunction != null && productFunction.size()>0){
246 for(int i=0;i<productFunction.size();i++){
247 if(productFunction.get(i).getProductArticleNumber() != null && productFunction.get(i).getProductArticleNumber().size()>0){
248 if(productFunction.get(i).getProductArticleNumber().get(0).getProductItemNumberType().equalsIgnoreCase("SA")){
249 return productFunction.get(i).getProductArticleNumber().get(0).getProductIsbn();
250 }
251 }
252 }
253 }
254 return null;
255 }
256
257 public String getVendorItemIdentifier(LineItemOrder lineItemOrder) {
258 List<SupplierReferenceInformation> supplierReferenceInformationList = lineItemOrder.getSupplierReferenceInformation();
259 if (supplierReferenceInformationList != null && supplierReferenceInformationList.size() > 0) {
260 for(int i=0;i<supplierReferenceInformationList.size();i++){
261 SupplierReferenceInformation supplierReferenceInformation = supplierReferenceInformationList.get(i);
262 List<SupplierLineItemReference> supplierLineItemReferenceList = supplierReferenceInformation.getSupplierLineItemReference();
263 if (supplierLineItemReferenceList != null && supplierLineItemReferenceList.size() > 0) {
264 SupplierLineItemReference supplierLineItemReferenceRef = supplierLineItemReferenceList.get(0);
265 String supplierOrderLine = supplierLineItemReferenceRef.getSuppliersOrderLine();
266 String vendorItemReference = supplierLineItemReferenceRef.getVendorReferenceNumber();
267 if (supplierOrderLine.equals("SNA")) {
268 return vendorItemReference;
269 }
270 }
271 }
272 }
273 return null;
274 }
275
276 public String getBFNNumber(LineItemOrder lineItemOrder) {
277 List<SupplierReferenceInformation> supplierReferenceInformationList = lineItemOrder.getSupplierReferenceInformation();
278 if (supplierReferenceInformationList != null && supplierReferenceInformationList.size() > 0) {
279 for(int i=0;i<supplierReferenceInformationList.size();i++){
280 SupplierReferenceInformation supplierReferenceInformation = supplierReferenceInformationList.get(i);
281 List<SupplierLineItemReference> supplierLineItemReferenceList = supplierReferenceInformation.getSupplierLineItemReference();
282 if (supplierLineItemReferenceList != null && supplierLineItemReferenceList.size() > 0) {
283 SupplierLineItemReference supplierLineItemReferenceRef = supplierLineItemReferenceList.get(0);
284 String supplierOrderLine = supplierLineItemReferenceRef.getSuppliersOrderLine();
285 String vendorItemReference = supplierLineItemReferenceRef.getVendorReferenceNumber();
286 if (supplierOrderLine.equals("BFN")) {
287 return vendorItemReference;
288 }
289 }
290 }
291 }
292 return null;
293 }
294
295
296
297
298
299
300 private String getVendorNumber(INVOrder invOrder,OleInvoiceRecord oleInvoiceRecord) throws Exception{
301 try{
302 for(int i=0;i<invOrder.getMessage().getPartyQualifier().size();i++){
303 if(invOrder.getMessage().getPartyQualifier().get(i).getPartyCode()!=null && invOrder.getMessage().getPartyQualifier().get(i).getPartyCode().equalsIgnoreCase("SR")){
304 return invOrder.getMessage().getPartyQualifier().get(i).getPartyInformation().getCodeIdentification();
305 }
306 }
307 }catch (Exception e){
308 if(oleInvoiceRecord.getVendorItemIdentifier() == null)
309 throw new Exception("Edifact file has no vendor number for invoice number:: "+getInvoiceNumber(invOrder)+" and invoice date:: "+getInvoiceDate(invOrder),e);
310 }
311 return null;
312 }
313
314
315
316
317
318
319
320 private String getInvoiceNumber(INVOrder invOrder)throws Exception{
321 try{
322 return invOrder.getMessage().getMessageBeginning().getMessageBeginningInterchangeControlReference();
323 }catch (Exception e){
324 throw new Exception("Edifact file has no invoice number.",e);
325 }
326 }
327
328
329
330
331
332
333 private String getInvoiceDate(INVOrder invOrder) throws Exception{
334 try{
335 return invOrder.getMessage().getMessageCreationInformation().getMessageCreationInfoDetails().getMessageCreationInfoDate();
336 }catch (Exception e){
337 throw new Exception("Edifact file has no invoice date.",e);
338 }
339 }
340
341
342 private String getForeignInvoiceAmount(INVOrder invOrder){
343
344 return null;
345 }
346
347 private String getVendorInvoiceAmount(INVOrder invOrder){
348 if(invOrder.getSummary() != null && invOrder.getSummary().getMonetarySummary() != null && invOrder.getSummary().getMonetarySummary().size() > 0){
349 for(int i=0;i<invOrder.getSummary().getMonetarySummary().size();i++){
350 MonetarySummary monetarySummary = (MonetarySummary)invOrder.getSummary().getMonetarySummary().get(i);
351 if(monetarySummary.getMonetarySummaryInformation()!=null && monetarySummary.getMonetarySummaryInformation().size()>0){
352 for(int j=0;j<monetarySummary.getMonetarySummaryInformation().size();j++){
353 if(monetarySummary.getMonetarySummaryInformation().get(0).getAmountType().equalsIgnoreCase("86")){
354 return monetarySummary.getMonetarySummaryInformation().get(0).getAmount();
355 }
356 }
357 }
358 }
359 }
360 return null;
361 }
362
363 private String getLineItemISBN(LineItemOrder lineItemOrder){
364 if(lineItemOrder.getLineItem() != null && lineItemOrder.getLineItem().get(0)!= null){
365 if(lineItemOrder.getLineItem().get(0).getLineItemArticleNumber() != null && lineItemOrder.getLineItem().get(0).getLineItemArticleNumber().get(0)!=null){
366 return lineItemOrder.getLineItem().get(0).getLineItemArticleNumber().get(0).getLineItemIsbn();
367 }
368 }
369 return null;
370 }
371
372 private String getISBN(LineItemOrder lineItemOrder){
373 if(lineItemOrder.getProductFunction()!= null && lineItemOrder.getProductFunction().size() > 0){
374 if(lineItemOrder.getProductFunction().get(0).getProductArticleNumber() != null && lineItemOrder.getProductFunction().get(0).getProductArticleNumber().size() > 0){
375 if(lineItemOrder.getProductFunction().get(0).getProductArticleNumber().get(0).getProductItemNumberType() != null){
376 return lineItemOrder.getProductFunction().get(0).getProductArticleNumber().get(0).getProductIsbn();
377 }
378 if(lineItemOrder.getProductFunction().get(0).getProductArticleNumber().get(0).getProductItemNumberType() == null &&
379 lineItemOrder.getProductFunction().get(0).getProductArticleNumber().get(0).getProductIsbn() != null){
380 return lineItemOrder.getProductFunction().get(0).getProductArticleNumber().get(0).getProductIsbn();
381 }
382 }
383 }
384 return null;
385 }
386
387 private String getISSN(LineItemOrder lineItemOrder){
388 if (lineItemOrder.getProductFunction()!= null && lineItemOrder.getProductFunction().size() > 0){
389 if(lineItemOrder.getProductFunction().get(0).getSupplierArticleNumber() != null && lineItemOrder.getProductFunction().get(0).getSupplierArticleNumber().size() > 0){
390 return lineItemOrder.getProductFunction().get(0).getSupplierArticleNumber().get(0).getIsbn();
391 }
392 }
393 return null;
394 }
395
396
397 private String getItemDescription(LineItemOrder lineItemOrder){
398 if(lineItemOrder.getItemDescriptionList() != null && lineItemOrder.getItemDescriptionList().size() > 0){
399 return lineItemOrder.getItemDescriptionList().get(0).getData();
400 }
401 return null;
402 }
403
404 private String getSubscriptionDateFrom(LineItemOrder lineItemOrder){
405 if(lineItemOrder.getDateTimeDetail() != null && lineItemOrder.getDateTimeDetail().size() > 0){
406 if(lineItemOrder.getDateTimeDetail().get(0).getDateTimeInformationList() != null && lineItemOrder.getDateTimeDetail().get(0).getDateTimeInformationList().size() > 0){
407 return lineItemOrder.getDateTimeDetail().get(0).getDateTimeInformationList().get(0).getPeriod();
408 }
409 }
410 return null;
411 }
412
413 private String getSubscriptionDateTo(LineItemOrder lineItemOrder){
414 if(lineItemOrder.getDateTimeDetail()!= null && lineItemOrder.getDateTimeDetail().size() > 1){
415 if(lineItemOrder.getDateTimeDetail().get(1).getDateTimeInformationList() != null && lineItemOrder.getDateTimeDetail().get(1).getDateTimeInformationList().size() > 0){
416 return lineItemOrder.getDateTimeDetail().get(1).getDateTimeInformationList().get(0).getPeriod();
417 }
418 }
419 return null;
420 }
421
422 private String getSummaryCharge(INVOrder invOrder){
423 if(invOrder.getSummary()!= null && invOrder.getSummary().getMonetarySummary() != null &&
424 invOrder.getSummary().getMonetarySummary().size()> 0){
425 if(invOrder.getSummary().getMonetarySummary().get(invOrder.getSummary().getMonetarySummary().size()-1).getMonetarySummaryInformation() != null && invOrder.getSummary().getMonetarySummary().get(invOrder.getSummary().getMonetarySummary().size()-1).getMonetarySummaryInformation().size() > 0){
426 invOrder.getSummary().getMonetarySummary().get(invOrder.getSummary().getMonetarySummary().size()-1).getMonetarySummaryInformation().get(0).getAmount();
427 }
428 }
429 return null;
430 }
431
432 private static boolean isNumeric(String str) {
433 NumberFormat formatter = NumberFormat.getInstance();
434 ParsePosition pos = new ParsePosition(0);
435 formatter.parse(str, pos);
436 return str.length() == pos.getIndex();
437 }
438
439 }