public List<UploadLog> uploadInvoice(String fileName) { // upload products to database String line; List<String> dataHolder = pdfExtractorUtility.extractOrder(fileName); // extract data from given fileName int lineNum = 1; List<VendorInventory> inventory = new ArrayList<VendorInventory>(); List<VendorInventory> insertionErrors = new ArrayList<VendorInventory>(); List<UploadLog> errorLogs = new ArrayList<UploadLog>(); VendorOrder vendorOrder; Product product; String[] splitLine; String vendorId; int quantity; double weight = 0; double price; double credit = 0; double deliveryFee = 0; double totalPrice = 0; Date orderDate = null; Date deliveryDate = null; Date currentDate = null; String vendorOrderId = null; String weightString; SimpleDateFormat dateFormat; boolean found = false; boolean firstDate = true; for (int i = 0; i < dataHolder.size(); i++) { // for each row in sheet line = dataHolder.get(i).trim(); // get current row log.debug(line); splitLine = line.split("^" + lineNum + "\\s{4}"); if (splitLine.length > 1) { // this is an item line lineNum++; if (line.contains(" 8338 ")) { // ignore line for Oma's info packet continue; } if (line.contains(" SRVCRG ")) { // add this charge to order charges table splitLine = line.split("^.+?(?=\\d+\\.\\d{2}$)"); deliveryFee = Double.parseDouble(splitLine[1]); continue; } line = splitLine[1]; // set line equal to everything after line number splitLine = line.split("(?<=\\d{2,4}\\w{0,2})\\s+", 2); vendorId = splitLine[0]; // set vendor id to first piece of split line = splitLine[1]; // set line equal to rest of split splitLine = line.split("\\d+\\.\\d{2}\\s+", 2); line = splitLine[1]; // remove quantity ordered splitLine = line.split("(?<=\\d+\\.\\d{2})\\s", 2); quantity = (int) Double.parseDouble(splitLine[0]); // set quantity received to first piece of split line = splitLine[1]; // set line equal to rest of split splitLine = line.split("^.+?(?=\\d+\\.?\\d*\\s+-?\\d+\\.\\d{2})", 2); line = splitLine[1]; // remove bill by label and product name along with trailing spaces splitLine = line.split("\\s+(?=-?\\d+\\.\\d{2}\\s+-?\\d+\\.\\d{2}$)", 2); weightString = splitLine[0]; // number representing either quantity or exact weight if (weightString.contains(".")) { // this is an exact weight weight = Double.parseDouble(weightString); } else { weight = 0; } line = splitLine[1]; splitLine = line.split("(?<=\\d+\\.\\d{2})\\s+", 2); price = Double.parseDouble(splitLine[0]); // set price each if (price < 0) { // this is a credit, add to credits if (weight != 0) { credit = credit + (new BigDecimal(price * weight) .setScale(2, BigDecimal.ROUND_HALF_UP) .doubleValue()); } else { credit = credit + (new BigDecimal(price * quantity) .setScale(2, BigDecimal.ROUND_HALF_UP) .doubleValue()); } continue; } else { // this is a billed item, add to total bill if (weight != 0) { totalPrice = totalPrice + (new BigDecimal(price * weight) .setScale(2, BigDecimal.ROUND_HALF_UP) .doubleValue()); log.debug( "item price so far: " + (new BigDecimal(price * weight).setScale(2, BigDecimal.ROUND_HALF_UP))); } else { totalPrice = totalPrice + (new BigDecimal(price * quantity) .setScale(2, BigDecimal.ROUND_HALF_UP) .doubleValue()); log.debug( "item price so far: " + (new BigDecimal(price * quantity).setScale(2, BigDecimal.ROUND_HALF_UP))); } } log.debug("total price so far: " + totalPrice); product = new Product(); product.setVendorId(vendorId); inventory.add(new VendorInventory(product, quantity, weight, price, false)); } else { if (line.contains("JPEELE") && !found) { // this is line than contains order and delivery dates and order id dateFormat = new SimpleDateFormat("M/d/yy"); found = true; splitLine = line.trim().split("\\s+"); for (String split : splitLine) { log.debug(split); if (split.contains("/")) { try { currentDate = dateFormat.parse(split); } catch (ParseException pe) { log.error("Unable to parse date", pe); } if (firstDate) { firstDate = false; orderDate = currentDate; } else { deliveryDate = currentDate; } } else if (!firstDate) { // this is order id vendorOrderId = split; } } log.debug( "Order Date= " + orderDate + "Delivery Date= " + deliveryDate + "Vendor Order Id= " + vendorOrderId); } } } String logDescription; List<String> headers; List<List<String>> logData; String orderId; log.debug("credit: " + credit); log.debug("total price: " + (totalPrice + deliveryFee + credit)); vendorOrder = new VendorOrder( credit * -1, deliveryFee, totalPrice + deliveryFee + credit, ProductGroup.OMAS, "Received", orderDate, deliveryDate, vendorOrderId); if ((vendorOrder = orderUtility.updateVendorOrder(vendorOrder)) == null) { logDescription = "Unable to upload vendor order information."; headers = Arrays.asList("Credit", "Delivery Fee", "Total Cost", "Status"); logData = new ArrayList<List<String>>(); logData.add( Arrays.asList( String.format("$%.2f", credit), String.format("$%.2f", deliveryFee), String.format("$%.2f", totalPrice), "Received")); errorLogs.add(new UploadLog(logDescription, headers, logData)); } else if (vendorOrder.getId() == null) { logDescription = "You are attempting to upload a duplicate order."; headers = Arrays.asList("Vendor Order Id"); logData = new ArrayList<List<String>>(); logData.add(Arrays.asList(vendorOrderId)); errorLogs.add(new UploadLog(logDescription, headers, logData)); } else { orderId = vendorOrder.getId(); for (VendorInventory vendorInventory : inventory) { if (productUtility.updateVendorInventoryItems( Arrays.asList(vendorInventory), Integer.parseInt(orderId), null, VendorOrder.STATUS.get("Received")) == null) { insertionErrors.add(vendorInventory); } } if (insertionErrors.size() > 0) { logDescription = "Vendor order items that were not uploaded properly."; headers = Arrays.asList("Vendor Id", "Quantity", "Weight", "Cost"); logData = new ArrayList<List<String>>(); for (VendorInventory vendorInventory : insertionErrors) { logData.add( Arrays.asList( vendorInventory.getProduct().getVendorId(), Double.toString(vendorInventory.getQuantity()), vendorInventory.getCostFormatted())); } errorLogs.add(new UploadLog(logDescription, headers, logData)); } log.debug(credit + ", " + deliveryFee); } return errorLogs; // this should be report of orphaned products/inventory }