String SFTPUSER = "svxtririga04";
String SFTPPASS = "tririga@123";
String SFTPWORKINGDIR = "/fssetl41";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try{
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel;
channelSftp.cd(SFTPWORKINGDIR);
File ctl = new File(ctlfile.getPath());
channelSftp.put(new FileInputStream(ctl), ctl.getName());
File hdr = new File(condtlpath);
channelSftp.put(new FileInputStream(hdr), hdr.getName());
File dtl = new File(condtlpath);
channelSftp.put(new FileInputStream(dtl), dtl.getName());
}catch(Exception ex){
ex.printStackTrace();
}
log.info("End of Contract");
}//end of Contracts and Change Orders
//TODO Payments
else if (type.equalsIgnoreCase("PAYMENT")){
System.out.println("Payment Called");
log.info("Payment Called");
//TODO Setting up file writer for Payment DIS file
String paydispath = "\\\\dtcnit" + File.separator + "FTP" + File.separator + "users" + File.separator + "tarftp" + File.separator + "Prod" + File.separator + "OUT"
+ File.separator + "FSSP.AP.TCPM.POINVOICE.DIS.UNSORTED";
File paydisfile = new File(paydispath);
if(paydisfile.exists()){
paydisfile.delete();
log.info("Payment File found and deleted!");
}
try {
paydisfile.createNewFile();
log.info("New Payment file created!");
} catch (IOException e2) {
log.info("Cannot create new ContPaymentract file!");
e2.printStackTrace();
}
FileWriter paydisfw = null;
try {
paydisfw = new FileWriter(paydisfile);
log.info("File Writer for Payment initialized");
} catch (IOException e1) {
log.info("ERROR! File Writer for Payment failed to initialized");
e1.printStackTrace();
}
//TODO PAYMENT DIS FILE QUERY
QueryMultiBoResult PAY_DIS = tririgaws.runNamedQueryMultiBo(null, "triCostItem", "triContractInvoiceLineItem", "tgt - Classloader - Integrations OUTBOUND - SAP Payment Request - DIS", datefilter, 1, 999999);
// int paytotaldis = PAY_DIS.getTotalResults();
//List<String[]> paydisDS = new ArrayList<String[]>();
for (QueryMultiBoResponseHelper paydishelper : PAY_DIS.getQueryMultiBoResponseHelpers()) {
int columnCount = paydishelper.getQueryMultiBoResponseColumns().length;
String[] row = new String[columnCount];
for (QueryMultiBoResponseColumn paydiscolumn : paydishelper.getQueryMultiBoResponseColumns()) {
int columnIndex = paydiscolumn.getIndex();
row[columnIndex] = paydiscolumn.getValue();
//Formatting Vendor Number
if(columnIndex == 0){
String vendorNumber = paydiscolumn.getValue();
if (vendorNumber == null) {
vendorNumber = "000000000";
} else if (vendorNumber.length() < 9){
vendorNumber = ("000000000" + vendorNumber).substring(vendorNumber.length());
} else if (vendorNumber.length() >= 9){
vendorNumber = vendorNumber.substring(vendorNumber.length() - 9);
}
paydisfw.write(vendorNumber);
}
//Formatting Invoice
else if(columnIndex == 1 ){
String invoice = paydiscolumn.getValue();
if(invoice == null){
invoice = String.format("%25s", "");
}
else if (invoice.length() < 25){
invoice = invoice.substring(0) + String.format("%25s","").substring(invoice.length());
}
else if (invoice.length() >= 25){
invoice = invoice.substring(invoice.length()-3);
}
paydisfw.write(invoice);
}
//Formatting Invoice Type
else if(columnIndex == 2 ){
String invoiceType = paydiscolumn.getValue();
if(invoiceType == null){
invoiceType = String.format("%2s", "");
}
else if (invoiceType.length() < 2){
invoiceType = invoiceType.substring(0) + String.format("%2s","").substring(invoiceType.length());
}
else if (invoiceType.length() >= 2){
invoiceType = invoiceType.substring(invoiceType.length()-2);
}
paydisfw.write(invoiceType);
}
//Formatting Line Number
else if(columnIndex == 3 ){
String lineNumber = paydiscolumn.getValue();
if (lineNumber.length() < 5) {
lineNumber = ("00000" + lineNumber).substring(lineNumber.length());
} else if (lineNumber.length() >= 5) {
lineNumber = lineNumber.substring(lineNumber.length() - 5);
}
paydisfw.write(lineNumber);
}
//formatting for Rate
else if(columnIndex == 4){
String rate = paydiscolumn.getValue();
rate = rate.replaceAll("[^\\d.]", "");
DecimalFormat df = new DecimalFormat("000000000000.00");
String formatrate = df.format(Double.parseDouble(rate));
paydisfw.write(formatrate);
}
}
//paydisDS.add(row);
//testing fw
paydisfw.write(System.getProperty("line.separator"));
}
//testing fw
paydisfw.flush();
paydisfw.close();
int totaldis = 0;
BigDecimal DISTotal = new BigDecimal("0.00");
// if( paytotaldis > 0){
stripDuplicatesFromFile(paydispath);
totaldis = count(paydispath);
DISTotal = sum(paydispath, type, false);
log.info("Payment DIS file completed");
// }else{
// File errorfile = new File(paydispath.substring(0, paydispath.length() - 9) + ".ERROR");
// paydisfile.renameTo(errorfile);
// paydisfile.delete();
// log.info("Payment DIS file errored out!");
// }
//Setting up file writer for Payment INV file
String payinvpath = "\\\\dtcnit" + File.separator + "FTP" + File.separator + "users" + File.separator + "tarftp" + File.separator + "Prod" + File.separator + "OUT"
+ File.separator + "FSSP.AP.TCPM.POINVOICE.INV.UNSORTED";
File payinvfile = new File(payinvpath);
if(payinvfile.exists()){
payinvfile.delete();
log.info("Payment File found and deleted!");
}
try {
payinvfile.createNewFile();
log.info("New Payment file created!");
} catch (IOException e2) {
log.info("Cannot create new Payment file!");
e2.printStackTrace();
}
FileWriter payinvfw = null;
try {
payinvfw = new FileWriter(payinvfile);
log.info("File Writer for Payment initialized");
} catch (IOException e1) {
log.info("ERROR! File Writer for Payment failed to initialized");
e1.printStackTrace();
}
//PAYMENT INV FILE QUERY
QueryMultiBoResult PAY_INV = tririgaws.runNamedQueryMultiBo(null, "triPayment", "triContractInvoice", "tgt - Classloader - Integrations OUTBOUND - SAP Payment Request - INV", datefilter, 1, 999999);
//List<String[]> payinvDS = new ArrayList<String[]>();
// int paytotalinv = PAY_INV.getTotalResults();
for (QueryMultiBoResponseHelper payinvhelper : PAY_INV.getQueryMultiBoResponseHelpers()) {
int columnCount = payinvhelper.getQueryMultiBoResponseColumns().length;
String[] row = new String[columnCount];
for (QueryMultiBoResponseColumn payinvcolumn : payinvhelper.getQueryMultiBoResponseColumns()) {
int columnIndex = payinvcolumn.getIndex();
row[columnIndex] = payinvcolumn.getValue();
//log.info("Column: [" + column.getIndex() + "] " + column.getLabel() + " Value: " + column.getValue());
//Formatting Vendor Number
if(columnIndex == 0){
String vendorNumber = payinvcolumn.getValue();
if (vendorNumber == null) {
vendorNumber = "000000000";
} else if (vendorNumber.length() < 9){
vendorNumber = ("000000000" + vendorNumber).substring(vendorNumber.length());
} else if (vendorNumber.length() >= 9){
vendorNumber = vendorNumber.substring(vendorNumber.length() - 9);
}
payinvfw.write(vendorNumber);
}
//Formatting Invoice
else if(columnIndex == 1 ){
String invoice = payinvcolumn.getValue();
if(invoice == null){
invoice = String.format("%25s", "");
}
else if (invoice.length() < 25){
invoice = invoice.substring(0) + String.format("%25s","").substring(invoice.length());
}
else if (invoice.length() >= 25){
invoice = invoice.substring(invoice.length()-3);
}
payinvfw.write(invoice);
}
//Formatting Invoice Type
else if(columnIndex == 2 ){
String invoiceType = payinvcolumn.getValue();
if(invoiceType == null){
invoiceType = String.format("%2s", "");
}
else if (invoiceType.length() < 2){
invoiceType = invoiceType.substring(0) + String.format("%2s","").substring(invoiceType.length());
}
else if (invoiceType.length() >= 2){
invoiceType = invoiceType.substring(invoiceType.length()-2);
}
payinvfw.write(invoiceType);
}
//Formatting PO Number
else if(columnIndex == 3 ){
String POnum = payinvcolumn.getValue();
if (POnum == null) {
POnum = "0000000000";
}
else if(POnum.length() < 10) {
POnum = ("0000000000" + POnum).substring(POnum.length());
}
else if (POnum.length() >= 10) {
POnum = POnum.substring(POnum.length() - 10);
}
payinvfw.write(POnum);
}
//formatting for Rate
else if(columnIndex == 4){
String rate = payinvcolumn.getValue();
rate = rate.replaceAll("[^\\d.]", "");
DecimalFormat df = new DecimalFormat("000000000000.00");
String formatrate = df.format(Double.parseDouble(rate));
payinvfw.write(formatrate);
//log.info("Rate: "+formatrate);
}
//formatting for date
else if(columnIndex == 5){
String docdate = payinvcolumn.getValue();
String month = docdate.substring(0, 2);
String day = docdate.substring(3, 5);
String century = docdate.substring(6, 8);
String year = docdate.substring(8, 10);
docdate = century + year + month + day;
payinvfw.write(docdate.toString());
}
}
//payinvDS.add(row);
//testing fw
payinvfw.write(System.getProperty("line.separator"));
}
//testing fw
payinvfw.flush();
payinvfw.close();
int totalinv = 0;
BigDecimal INVTotal = new BigDecimal ("0.00");
// File file = new File(paydispath.substring(0, paydispath.length() - 9) + ".ERROR");
// if( paytotalinv > 0 && !file.exists()){
stripDuplicatesFromFile(payinvpath);
totalinv = count(payinvpath);
INVTotal = sum(payinvpath, type, true);
log.info("Payment INV file completed");
// }else{
// File errorfile = new File(payinvpath.substring(0, payinvpath.length() - 9) + ".ERROR");
// payinvfile.renameTo(errorfile);
// payinvfile.delete();
// log.info("Payment INV file errored out!");
// }
//Creating Payment CTL file
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date reportdate = new Date();
String Sdate = dateFormat.format(reportdate).substring(0, 10);
String time = dateFormat.format(reportdate).substring(11, 16);
String discount = ("0000000" + Integer.toString(totaldis)).substring(Integer.toString(totaldis).length());
String invcount = ("0000000" + Integer.toString(totalinv)).substring(Integer.toString(totalinv).length());
String INVtotal1 = String.format("%.2f", INVTotal);
String invtotal = ("000000000000000" + INVtotal1).substring(INVtotal1.length());
String DIStotal1 = String.format("%.2f", DISTotal);
String distotal = ("000000000000000" + DIStotal1).substring(DIStotal1.length());
File ctlfile = null;
// File payerror = new File(payinvpath.substring(0, payinvpath.length() - 9) + ".ERROR");
// File payerror1 = new File(paydispath.substring(0, paydispath.length() - 9) + ".ERROR");
// if( (invtotal.equals(distotal)) && totaldis > 0 && totalinv > 0 && !payerror.exists() && !payerror1.exists())
if( (invtotal.equals(distotal))) //check if totals match
{
paydispath = sort(paydispath);
payinvpath = sort(payinvpath);
ctlfile = new File("\\\\dtcnit" + File.separator + "FTP" + File.separator + "users" + File.separator + "tarftp" + File.separator + "Prod" + File.separator + "OUT" + File.separator + "FSSP.AP.TCPM.POINVOICE.CTL");
}
else{
ctlfile = new File("\\\\dtcnit" + File.separator + "FTP" + File.separator + "users" + File.separator + "tarftp" + File.separator + "Prod" + File.separator + "OUT" + File.separator + "FSSP.AP.TCPM.POINVOICE.CTL.ERROR");
File errorfile = new File(payinvpath + ".ERROR");
boolean success = payinvfile.renameTo(errorfile);
log.info("inv error file renamed: " + payinvpath);
File errorfile1 = new File(paydispath + ".ERROR");
boolean success1 = paydisfile.renameTo(errorfile1);
log.info("inv error file renamed: " + paydispath);
if(success){
payinvfile.delete();
}
if(success1){
paydisfile.delete();
}
}
FileWriter ctlfw = new FileWriter(ctlfile);
ctlfw.write("TCPM - Date: " + Sdate + System.getProperty("line.separator"));
ctlfw.write("Time: " + time + System.getProperty("line.separator"));
ctlfw.write("Data type Rec Count Amount"+ System.getProperty("line.separator"));
ctlfw.write(" PO Invoice Lines " + invcount + " " + invtotal + System.getProperty("line.separator"));
ctlfw.write(" PO Distrib Lines " + discount + " " + distotal);
ctlfw.flush();
ctlfw.close();
if( (invtotal.equals(distotal))) //check if totals match
{
//send to sftp - CFI ftp
// String SFTPHOST = "ftp.gocfi.com";
// int SFTPPORT = 22;
// String SFTPUSER = "tarftp";
// String SFTPPASS = "Tar_00274";
// String SFTPWORKINGDIR = "/PROD/";
//Target FTP
String SFTPHOST = "emft.target.com";
int SFTPPORT = 22;
String SFTPUSER = "svxtririga04";
String SFTPPASS = "tririga@123";
String SFTPWORKINGDIR = "/fssetl41";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try{
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel;
channelSftp.cd(SFTPWORKINGDIR);
File ctl = new File(ctlfile.getPath());
channelSftp.put(new FileInputStream(ctl), ctl.getName());
File hdr = new File(payinvpath);
channelSftp.put(new FileInputStream(hdr), hdr.getName());
File dtl = new File(paydispath);
channelSftp.put(new FileInputStream(dtl), dtl.getName());
}catch(Exception ex){
ex.printStackTrace();
}
}
log.info("End of Payment");
}//end of Payments
//Retainage
else if (type.equalsIgnoreCase("RETAINAGE")){
System.out.println("Retainage Called");
log.info("Retainage Called");
//TODO Setting up file writer for Retainage DIS file
String retdispath = "\\\\dtcnit" + File.separator + "FTP" + File.separator + "users" + File.separator + "tarftp" + File.separator + "Prod" + File.separator + "OUT"
+ File.separator + "FSSP.AP.TCPM.FIINVOICE.DIS.UNSORTED";
File retdisfile = new File(retdispath);
if(retdisfile.exists()){
retdisfile.delete();
log.info("Retainage File found and deleted!");
}
try {
retdisfile.createNewFile();
log.info("New Retainage file created!");
} catch (IOException e2) {
log.info("Cannot create new Retainage file!");
e2.printStackTrace();
}
FileWriter retdisfw = null;
try {
retdisfw = new FileWriter(retdisfile);
log.info("File Writer for Retainage initialized");
} catch (IOException e1) {
log.info("ERROR! File Writer for Retainage failed to initialized");
e1.printStackTrace();
}
QueryMultiBoResult RET_DIS = tririgaws.runNamedQueryMultiBo(null, "triCostItem", "triContractInvoiceLineItem", "tgt - Classloader - Integrations OUTBOUND - SAP Retainage - DIS", datefilter, 1, 999999);
// int rettotaldis = RET_DIS.getTotalResults();
int rowcount = 0;
for (QueryMultiBoResponseHelper retdishelper : RET_DIS.getQueryMultiBoResponseHelpers()) {
int columnCount = retdishelper.getQueryMultiBoResponseColumns().length;
String[] row = new String[columnCount];
boolean sign = false;
String vendorNumber = ""; //9
String loc = "";
String invoice = ""; //25
String invoiceType = ""; //1
String invoiceDate = ""; //8
rowcount = rowcount + 1;
String seqnum = Integer.toString(rowcount); //3
String description = ""; //50
String amount = ""; //16
String companycode = ""; //4
String wbs = ""; //21
String costcode = "063100"; //6
String profitcenter = ""; //4
String costcenter = String.format("%8s",""); //8
String internalorder = String.format("%8s","");//8
for (QueryMultiBoResponseColumn retdiscolumn : retdishelper.getQueryMultiBoResponseColumns()) {
int columnIndex = retdiscolumn.getIndex();
row[columnIndex] = retdiscolumn.getValue();
//log.info("Column: [" + column.getIndex() + "] " + column.getLabel() + " Value: " + column.getValue());
//Formatting Vendor Number
if(columnIndex == 0){
vendorNumber = retdiscolumn.getValue();
if (vendorNumber == null) {
vendorNumber = "000000000";
} else if (vendorNumber.length() < 9){
vendorNumber = ("000000000" + vendorNumber).substring(vendorNumber.length());
} else if (vendorNumber.length() >= 9){
vendorNumber = vendorNumber.substring(vendorNumber.length() - 9);
}
}
//Formatting Location
else if(columnIndex == 1 ){
loc = retdiscolumn.getValue();
if (loc.length() < 4) {
loc = ("0000" + loc).substring(loc.length());
} else if (loc.length() >= 4) {
loc = loc.substring(loc.length() - 4);
}
}
//Formatting Invoice
else if(columnIndex == 2 ){
invoice = retdiscolumn.getValue();
if (invoice == null) {
invoice = "";
} else if (invoice.length() <= 18) {
invoice = invoice + "-R-" + loc;
} else if (invoice.length() > 18) {
invoice = invoice.substring(0, 18) + "-R-" + loc;
}
if(invoice.length() < 25){
invoice = invoice + String.format("%25s", "").substring(invoice.length());
}
else{
invoice = invoice.substring(0,25);
}
}
//formatting for Amount
else if(columnIndex == 3){
amount = retdiscolumn.getValue();
if(amount.contains("-")){
sign = true;
}
amount = amount.replaceAll("[^\\d.]", "");
DecimalFormat df = null;
if(sign){
df = new DecimalFormat("-000000000000.00");
}else{
df = new DecimalFormat("+000000000000.00");
}
amount = df.format(Double.parseDouble(amount));
}
//Formatting Invoice Type
else if(columnIndex == 4 ){
invoiceType = retdiscolumn.getValue();
if(invoiceType == null){
invoiceType = String.format("%1s", "");
}
if(sign){
invoiceType = "C";
}
else{
invoiceType = " ";
}
}
//Formatting Invoice Date
else if(columnIndex == 5 ){
invoiceDate = retdiscolumn.getValue();
if (invoiceDate == null) {
invoiceDate = "";
} else {
String month = invoiceDate.substring(0, 2);
String day = invoiceDate.substring(3, 5);
String century = invoiceDate.substring(6, 8);
String year = invoiceDate.substring(8, 10);
invoiceDate = century + year + month + day;
}
if(seqnum.length() < 3){
seqnum = ("000" + seqnum).substring(seqnum.length());
}
}
//Formatting Description
else if(columnIndex == 6 ){
description = retdiscolumn.getValue();
if (description == null) {
description = String.format("%50s","");
}
else if (description.length() < 50) {
description = description + String.format("%50s", "").substring(description.length());
} else if (description.length() >= 50) {
description = description.substring(0,50);
}
}
//Formatting Company Code
else if(columnIndex == 7 ){
companycode = retdiscolumn.getValue();
if (companycode.length() < 4) {
companycode = ("0000" + companycode).substring(companycode.length());
} else if (companycode.length() >= 4) {
companycode = companycode.substring(companycode.length() - 4);
}
}
//Formatting WBS
else if(columnIndex == 8){
wbs = retdiscolumn.getValue();
if (wbs == null) {
wbs = String.format("%21s", "");
} else if (wbs.length() >= 12) {
wbs = wbs.substring(0, wbs.length() - 6) + "063100";
wbs = wbs + String.format("%21s", "").substring(wbs.length());
} else {
wbs = wbs + "063100" + String.format("%21s","").substring(wbs.length() + 6);
}
}
//Writing Cost Code -- did not need since static value
// else if(columnIndex == 9){
//
// }
//
//Formatting Profit Center
else if(columnIndex == 10){
profitcenter = retdiscolumn.getValue();
if(profitcenter == null){
profitcenter = String.format("%4s","");
}else if (profitcenter.length() >= 4){
profitcenter = profitcenter.substring(profitcenter.length()-4);
}else if (profitcenter.length() < 4){
profitcenter = ("0000" + profitcenter).substring(profitcenter.length());
}
}
}
//payinvDS.add(row);
//testing fw
retdisfw.write(vendorNumber);
retdisfw.write(invoice);
retdisfw.write(invoiceType);
retdisfw.write(invoiceDate);
retdisfw.write(seqnum);
retdisfw.write(description);
retdisfw.write(amount);
retdisfw.write(companycode);
retdisfw.write(wbs);
retdisfw.write(costcode);
retdisfw.write(profitcenter);
retdisfw.write(costcenter);
retdisfw.write(internalorder);
retdisfw.write(System.getProperty("line.separator"));
}
//testing fw
retdisfw.flush();
retdisfw.close();
int totaldis = 0;
BigDecimal DISTotal = new BigDecimal ("0.00");
// if(rettotaldis > 0 ){
stripDuplicatesFromFile(retdispath);
totaldis = count(retdispath);
DISTotal = sum(retdispath, type, false);
log.info("Retainage file completed");
// }else{
// File errorfile = new File(retdispath.substring(0, retdispath.length() - 9) + ".ERROR");
// retdisfile.renameTo(errorfile);
// retdisfile.delete();
// log.info("Retainage file errored out!");
// }
//Setting up file writer for RETAINAGE INV file
String retinvpath = "\\\\dtcnit" + File.separator + "FTP" + File.separator + "users" + File.separator + "tarftp" + File.separator + "Prod" + File.separator + "OUT"
+ File.separator + "FSSP.AP.TCPM.FIINVOICE.INV.UNSORTED";
File retinvfile = new File(retinvpath);
if(retinvfile.exists()){
retinvfile.delete();
log.info("Retainage File found and deleted!");
}
try {
retinvfile.createNewFile();
log.info("New Retainage file created!");
} catch (IOException e2) {
log.info("Cannot create new Retainage file!");
e2.printStackTrace();
}
FileWriter retinvfw = null;
try {
retinvfw = new FileWriter(retinvfile);
log.info("File Writer for Retainage initialized");
} catch (IOException e1) {
log.info("ERROR! File Writer for Retainage failed to initialized");
e1.printStackTrace();
}
//RETAINAGE INV FILE QUERY
QueryMultiBoResult RET_INV = tririgaws.runNamedQueryMultiBo(null, "triPayment", "triContractInvoice", "tgt - Classloader - Integrations OUTBOUND - SAP Retainage - INV", datefilter, 1, 999999);
// int rettotalinv = RET_INV.getTotalResults();
// log.info("Total INV Records: "+ rettotalinv);
//List<String[]> payinvDS = new ArrayList<String[]>();
for (QueryMultiBoResponseHelper retinvhelper : RET_INV.getQueryMultiBoResponseHelpers()) {
int columnCount = retinvhelper.getQueryMultiBoResponseColumns().length;
String[] row = new String[columnCount];
boolean sign = false;
String loc = "";
String vendorNumber = "";
String invoice = "";
String invoiceType = "";
String invoiceDate = "";
String amount = "";
String currency = "";
String source = "TCPM ";
String retainageFL = "Y";
String lastExtractDate = "";
for (QueryMultiBoResponseColumn retinvcolumn : retinvhelper.getQueryMultiBoResponseColumns()) {
int columnIndex = retinvcolumn.getIndex();
row[columnIndex] = retinvcolumn.getValue();
//log.info("Column: [" + retinvcolumn.getIndex() + "] " + retinvcolumn.getLabel() + " Value: " + retinvcolumn.getValue());
//Formatting Vendor Number
if(columnIndex == 0){
vendorNumber = retinvcolumn.getValue();
if (vendorNumber == null) {
vendorNumber = "000000000";
} else if (vendorNumber.length() < 9){
vendorNumber = ("000000000" + vendorNumber).substring(vendorNumber.length());
} else if (vendorNumber.length() >= 9){
vendorNumber = vendorNumber.substring(vendorNumber.length() - 9);
}
}
//Formatting Location
else if(columnIndex == 1 ){
loc = retinvcolumn.getValue();
if (loc.length() < 4) {
loc = ("0000" + loc).substring(loc.length());
} else if (loc.length() >= 4) {
loc = loc.substring(loc.length() - 4);
}
}
//Formatting Invoice
else if(columnIndex == 2 ){
invoice = retinvcolumn.getValue();
if (invoice == null) {
invoice = "";
} else if (invoice.length() <= 18) {
invoice = invoice + "-R-" + loc;
} else if (invoice.length() > 18) {
invoice = invoice.substring(0, 18) + "-R-" + loc;
}
if(invoice.length() < 25){
invoice = invoice + String.format("%25s", "").substring(invoice.length());
}
else{
invoice = invoice.substring(0,25);
}
}
//formatting for Amount
else if(columnIndex == 3){
amount = retinvcolumn.getValue();
if(amount.contains("-")){
sign = true;
}
amount = amount.replaceAll("[^\\d.]", "");
DecimalFormat df = null;
if(sign){
df = new DecimalFormat("-000000000000.00");
}else{
df = new DecimalFormat("+000000000000.00");
}
amount = df.format(Double.parseDouble(amount));
}
//Formatting Invoice Type
else if(columnIndex == 4 ){
invoiceType = retinvcolumn.getValue();
if(invoiceType == null){
invoiceType = String.format("%1s", "");
}
if(sign){
invoiceType = "C";
}
else{
invoiceType = " ";
}
}
//Formatting Invoice Date
else if(columnIndex == 5 ){
invoiceDate = retinvcolumn.getValue();
if (invoiceDate == null) {
invoiceDate = "";
} else {
String month = invoiceDate.substring(0, 2);
String day = invoiceDate.substring(3, 5);
String century = invoiceDate.substring(6, 8);
String year = invoiceDate.substring(8, 10);
invoiceDate = century + year + month + day;
}
}
//Formatting Currency
else if(columnIndex == 6 ){
currency = retinvcolumn.getValue();
if (currency == null) {
currency = " ";
}
else if (currency.toUpperCase().contains("US")) {
currency = "USD";
} else if (currency.toUpperCase().contains("CAN")) {
currency = "CAD";
}
}
//Formatting last extract date
else if(columnIndex == 7 ){
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
Date reportdate = new Date();
lastExtractDate = dateFormat.format(reportdate);
}
}
//payinvDS.add(row);
retinvfw.write(vendorNumber);
retinvfw.write(invoice);
retinvfw.write(invoiceType);
retinvfw.write(invoiceDate);
retinvfw.write(amount);
retinvfw.write(currency);
retinvfw.write(source);
retinvfw.write(retainageFL);
retinvfw.write(lastExtractDate);
//testing fw
retinvfw.write(System.getProperty("line.separator"));
}
//testing fw
retinvfw.flush();
retinvfw.close();
int totalinv = 0;
BigDecimal INVTotal = new BigDecimal("0.00");
// File file = new File (retdispath.substring(0, retdispath.length() - 9) + ".ERROR");
// if(rettotalinv > 0 && !file.exists()){
stripDuplicatesFromFile(retinvpath);
totalinv = count(retinvpath);
INVTotal= sum(retinvpath, type, true);
// }else{
// File errorfile = new File(retinvpath.substring(0, retinvpath.length() - 9) + ".ERROR");
// retinvfile.renameTo(errorfile);
// retinvfile.delete();
// log.info("Retainage INV error!");
// }
//Creating Retainage CTL file
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date reportdate = new Date();
String Sdate = dateFormat.format(reportdate).substring(0, 10);
String time = dateFormat.format(reportdate).substring(11, 16);
String discount = ("0000000" + Integer.toString(totaldis)).substring(Integer.toString(totaldis).length());
String invcount = ("0000000" + Integer.toString(totalinv)).substring(Integer.toString(totalinv).length());
String INVtotal1 = String.format("%.2f", INVTotal);
String invtotal = ("000000000000000" + INVtotal1).substring(INVtotal1.length());
String DIStotal1 = String.format("%.2f", DISTotal);
String distotal = ("000000000000000" + DIStotal1).substring(DIStotal1.length());
File ctlfile = null;
// File reterror = new File(retdispath.substring(0, retdispath.length() - 9) + ".ERROR");
// File reterror1 = new File(retinvpath.substring(0, retinvpath.length() - 9) + ".ERROR");
// if( invtotal.equals(distotal) && totalinv > 0 && totaldis > 0 && !reterror.exists() && !reterror1.exists())
if(invtotal.equals(distotal)) //check if totals match
{
retdispath = sort(retdispath);
retinvpath = sort(retinvpath);
ctlfile = new File("\\\\dtcnit" + File.separator + "FTP" + File.separator + "users" + File.separator + "tarftp" + File.separator + "Prod" + File.separator + "OUT" + File.separator + "FSSP.AP.TCPM.FIINVOICE.CTL");
}
else{
ctlfile = new File("\\\\dtcnit" + File.separator + "FTP" + File.separator + "users" + File.separator + "tarftp" + File.separator + "Prod" + File.separator + "OUT" + File.separator + "FSSP.AP.TCPM.FIINVOICE.CTL.ERROR");
File errorfile = new File(retinvpath + ".ERROR");
boolean success = retinvfile.renameTo(errorfile);
log.info("inv error file renamed: " + retinvfile);
File errorfile1 = new File(retdispath + ".ERROR");
boolean success1 = retdisfile.renameTo(errorfile1);
log.info("inv error file renamed: " + retdisfile);
if(success){
retinvfile.delete();
}
if(success1){
retdisfile.delete();
}
}
FileWriter ctlfw = new FileWriter(ctlfile);
ctlfw.write("TCPM - Date: " + Sdate + System.getProperty("line.separator"));
ctlfw.write("Time: " + time + System.getProperty("line.separator"));
ctlfw.write("Data type Rec Count Amount"+ System.getProperty("line.separator"));
ctlfw.write(" FI Invoice Lines " + invcount + " " + invtotal + System.getProperty("line.separator"));
ctlfw.write(" FI Distrib Lines " + discount + " " + distotal);
ctlfw.flush();
ctlfw.close();
if(invtotal.equals(distotal)) //check if totals match
{
//send to sftp - CFI ftp
// String SFTPHOST = "ftp.gocfi.com";
// int SFTPPORT = 22;
// String SFTPUSER = "tarftp";
// String SFTPPASS = "Tar_00274";
// String SFTPWORKINGDIR = "/PROD/";
// Target FTP
String SFTPHOST = "emft.target.com";
int SFTPPORT = 22;
String SFTPUSER = "svxtririga04";
String SFTPPASS = "tririga@123";
String SFTPWORKINGDIR = "/fssetl41";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try{
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel;
channelSftp.cd(SFTPWORKINGDIR);
File ctl = new File(ctlfile.getPath());
channelSftp.put(new FileInputStream(ctl), ctl.getName());
File hdr = new File(retinvpath);