#!/usr/bin/perl
$data = 'ISA^00^ ^00^ ^01^043645501 ^01^077900207 ^090810^1019^U^00401^973397655^0^P^|~GS^IN^108878828^077900207^20090810^1019^791^X^004010~ST^810^7910004~BIG^20090807^45315897^20090807^1550510^^^DI~PER^BI^CREDIT DEPT^TE^800-426-5772~N1^BT^OCHSNER CLINIC FOUNDATION^91^00007164~N3^ACCOUNTS PAYABLE~N4^NEW ORLEANS^LA^70123~N1^BY^OCHSNER CLINIC FOUNDATION^92^7164~N1^ST^OCHSNER CLINIC NORTHSHORE^92^08978219~ITD^05^3^^20090906^^20090906^30^^^^^NET 30 DAYS~DTM^011^20090807^000000~IT1^1^2^EA^64.76^^VC^7470961~PID^F^^^^NO 9 BLACK TONER CARTRIDGE~TDS^12952~CTT^1~SE^15^7910004~GE^1^791~IEA^1^973397655~';
#### program variables
$vPath1="D:\\Invoice\\";
$vName1="GHX810.out";
#open(X12, "$vPath1$vName1");
#while (<X12>) {
@segin = split(/\~/, $data);
# segin[3] is BIG
@line = split(/\^/, $segin[3]);
$company = substr($line[4], 0, 1);
if($company == 1) {
$company = 120;
} elsif($company == 7) {
$company = 710;
} elsif($company == 8) {
$company = 800;
}
$invoice_dte = $line[1];
$invoice = $line[2];
$po_date = $line[3];
$po_number = $line[4];
$inv_typ = $line[7];
# segin[5] is N1 - Bill to Name
@line = split(/\^/, $segin[5]);
$bt_name = $line[2];
# segin[6] is N3 - Bill to Address
@line = split(/\^/, $segin[6]);
$bt_add1 = $line[1];
if ($line[2]) {
$bt_add2 = " ";
}else {
$bt_add2 = $line[2];
}
# segin[7] is N4 - Bill to City, ST Zip
@line = split(/\^/, $segin[7]);
$bt_city = $line[1];
$bt_stat = $line[2];
$bt_zip = $line[3];
# segin[8] is N1 - SU (supplier) or BY (Purchaser)
@line = split(/\^/, $segin[8]);
if ($line[1] eq "BY") {
$vendor_vname = " ";
}else {
$vendor_vname = $line[2];
}
# segin[9] is N1 - Ship to name
@line = split(/\^/, $segin[9]);
$st_name = $line[2];
#Changed your loop to just go to the end of the array..
for $L1 (10..$#segin) {
@temp = split(/\^/, $segin[$L1]);
if ($temp[0] eq "N3") {
$st_add1 = $temp[1];
if ($temp[2]) {
$st_add2 = " ";
}else {
$st_add2 = $temp[2];
}
}
elsif ($temp[0] eq "N4") {
$st_city = $temp[1];
$st_stat = $temp[2];
$st_zip = $temp[3];
}
elsif ($temp[0] eq "ITD") {
$terms = $temp[1];
$dte_code = $temp[2];
$net_days = $temp[7];
}
elsif ($temp[0] eq "DTM") {
$dtm = $segin[1];
}
elsif ($temp[0] eq "IT1") {
$line_nbr = $temp[1];
$qty = $temp[2];
$uom = $temp[3];
$price = $temp[4];
$price_cd = $temp[5];
$ven_item = $temp[7];
$item = $temp[9];
}
elsif ($temp[0] eq "PID") {
$description = $temp[5];
}
elsif ($temp[0] eq "TDS") {
$tot_inv_amt = $temp[1];
}
}
#}
#close(X12);