Below is a quick and dirty 997 (Acknowledgment) analyzer. The purpose of the analyzer is to detect failures and send an email alert. I'm currently writing a PHP EDI parser class which will extend into a 997 and 824 analyzer but I needed something quick to send alerts in production now. It may take a week to finish my PHP EDI Parser so here is what I came up with (in Perl):
[highlight="perl"]
#!/usr/bin/perl
# Copyright: Jordan DeLozier
# Purpose: EDI
#
# Description: Send an error email about
# rejected 997s
#
# Author: Jordan DeLozier
# Date: 9-10-2008
################################################
# Variables
$input = $ARGV[0]; # File
$error = 0;
$type = "";
$lineSep = "\n"; # Value will change below
$segSep = "*"; # Value will change below
$email = "some\@email.com"; # Email or distribution list to receive
# rejected 997 notices.
$from = "EDI Translator"; # Email From
##################################################
# Email Subject and Message set lower
# Open the File
open(INFO, $input) || die ("Could not open file - $input\n");
$edi_lines = <INFO>; # Read into array
close(INFO); # close the file
# Read the 106th character to determine the
# line seperator
$lineSep = substr($edi_lines, 105, 1);
$segSep = "\\" . substr($edi_lines, 3, 1);
# Split the lines
@edi = split(/$lineSep/, $edi_lines);
# Read Each Line
foreach $n(@edi) {
# Split the EDI Lines
@elements = split(/$segSep/, $n);
# Determine each segment
$segment = $elements[0];
# Grab the type for email Subject
if ($segment eq "AK2") {
$type = $elements[1];
}
# Check for the Error Codes
if ($segment eq "AK5") {
if ($elements[1] eq "R" || $elements[1] eq "E") {
# Found Error, we only want one email so
# we will set a flag here instead of sending
# the email here.
$error = 1;
# CLI Output (for debugging)
print "$n\n";
}
}
}
#kick off the email
if ($error == 1)
{
# email Variables
$subject = "$type Rejected!";
$message = "Hello,\nAn EDI message has been rejected via 997. This problem needs correcting ASAP. Below are the contents of the EDI document.\n\n $edi_lines";
sendEmail($email, $from, $subject, $message);
# CLI Output
print "Email Sent!\n";
}
# Simple Email Function
# ($to, $from, $subject, $message)
sub sendEmail
{
my ($to, $from, $subject, $message) = @_;
my $sendmail = '/usr/lib/sendmail';
open(MAIL, "|$sendmail -oi -t");
print MAIL "From: $from\n";
print MAIL "To: $to\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$message\n";
close(MAIL);
}
[/highlight]
Usage:
# ./analyze997.pl /path/to/997/file
Sample Rejection 997:
Feel free to change, use, distribute or anything you like. I've added it as an attachment as well.Code:ISA*00* *00* *01*01 *01*02 *080909*1730*U*00200*000001789*0*P*: GS*FA*05*02*080909*1730*1789*X*002001 ST*997*1789 AK1*SH*17 AK2*856*0017 AK5*R*3 AK9*R*1*1*0 SE*6*1789 GE*1*1789 IEA*1*000001789


LinkBack URL
About LinkBacks









Reply With Quote





Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum