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
Last edited by Jordan; 09-10-2008 at 05:51 PM.
Can you either attach the files or disable the GeSHi line numbers (or toggle the line numbers) - its a pain in the arse to copy / paste the code.
I thought I did attach it but I will once I get back on my laptop.
Posted via CodeCall Mobile
There you go, attached.
Jordan,
Why would you write the emailer in perl or php for errors in another programming language? Wouldn't an EDI error be best found in EDI itself? Is EDI capable of emailing errors to someone? I'm kind of perplexed.
EDI is like xml, it is just information. The EDI mapper software is code but it is probably less useful than basic in DOS 3.1. It doesn't have the ability to email or do complex algos.
Posted via CodeCall Mobile
Thanks for the explanation. If EDI is basically xml, why don't you and your company port over to more modern languages that are more powerful?
It is not dictated by us. It is an automotive standard(Ford, GM, Chrysler, etc) who tell us we have to use it in order to do business with them. Other industries also use it like Walmart, for instance.
I believe EDI is about 40 years old but in all that time it is still a very a very rare and unknown skill. For instance, before you met me, did you ever hear of EDI?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks