unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, OleServer, FREDI_TLB,
  StdCtrls;

type
  TForm1 = class(TForm)
    cmdTranslate: TButton;
    cmdClose: TButton;
    Label1: TLabel;
    txtInvoiceNo: TEdit;
    Label2: TLabel;
    ListErrors: TListBox;
    Label3: TLabel;
    procedure cmdTranslateClick(Sender: TObject);
    procedure cmdCloseClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.cmdTranslateClick(Sender: TObject);
var
   oEdiDoc : IediDocument;
   oSegment : IediDataSegment;
   oDataElement : IediDataElement;
   oSchemas : IediSchemas;
   oWarning : IediWarning;
   oWarnings : IediWarnings;

   sPath : String;
   sQlfr : String;
   sSefFile : String;
   sEdiFile : String;
   sSegmentID : string;
   nArea : integer;
   sLoopSection : string;
   sValue : string;
   i : integer;
   nWarningCount : integer;


begin
   cmdTranslate.Enabled := False;

   sPath := Trim(GetCurrentDir) + '\';
   sSefFile := '810_X12-4010.SEF';
   sEdiFile := '810_4010.X12';

   instantiate EDI document object
   oEdiDoc := CoediDocument.Create();

   //Change cursor type from dynamic (default) to ForwardOnly to improve performance
   oEdiDoc.CursorType := Cursor_ForwardOnly;

   //Disable standard reference library to improve performance
   oSchemas := oEdiDoc.GetSchemas as IediSchemas;
   oSchemas.EnableStandardReference := false;

   //Load Sef file
   oEdidoc.LoadSchema(sPath + sSefFile,0);

   //Load EDI file
   oEdiDoc.LoadEdi(sPath + sEdiFile);

   //traverse through the entire EDI file a segment at a time

   //get first data segment
   oSegment := oEdiDoc.FirstDataSegment as IediDataSegment;

   while oSegment <> Nil do begin

      //segments are identified by their area, loop section they are in and their ID.
      //(segment properties of EDI file can be viewed with our eFileManager utility)
      
      nArea := oSegment.Area;
      sLoopSection := oSegment.LoopSection;
      sSegmentID := oSegment.ID;

      if nArea = 0 then begin
        if sSegmentID = 'ISA' then begin
           sValue := oSegment.DataElementValue[13,0];  //ISA INTERCHANGE CONTROL NO.
        end
        else if sSegmentID = 'GS' then begin
           sValue := oSegment.DataElementValue[6,0];  //GS CONTROL NO.
        end;

      end
      else if nArea = 1 then begin
         if sLoopSection = '' then begin
            if sSegmentID = 'ST' then begin
            end
            else if sSegmentID = 'BIG' then begin
               sValue := oSegment.DataElementValue[1,0];  //INVOICE DATE
               txtInvoiceNo.Text  := oSegment.DataElementValue[2,0];  //INVOICE NO.

               //Get description of datalement 1
               oDataElement := oSegment.DataElement[1] as IediDataElement;
               sValue := oDataElement.Description;
            end
            else if sSegmentID = 'ITD' then begin
               sValue := oSegment.DataElementValue[12,0];  //TERMS DESCRIPTION
            end
            else if sSegmentID = 'FOB' then begin
            end;
         end  //sLoopSection = ''
         else if sLoopSection = 'N1' then begin
            if sSegmentID = 'N1' then begin
               sQlfr := oSegment.DataElementValue[1,0];
            end;
            if sQlfr = 'ST' then begin  //SHIP TO
               if sSegmentID = 'N1' then begin
                  sValue := oSegment.DataElementValue[2,0];  //NAME
                  sValue := oSegment.DataElementValue[4,0];  //ID NO.
               end
               else if sSegmentID = 'N3' then begin
                  sValue := oSegment.DataElementValue[1,0];  //ADDRESS
               end
               else if sSegmentID = 'N4' then begin
                  sValue := oSegment.DataElementValue[1,0];  //CITY
                  sValue := oSegment.DataElementValue[2,0];  //STATE
                  sValue := oSegment.DataElementValue[3,0];  //ZIP
               end;
            end  //sQlfr = 'ST'
            else if sQlfr = 'BT' then begin  //BILL TO
               if sSegmentID = 'N1' then begin
                  sValue := oSegment.DataElementValue[2,0];  //NAME
                  sValue := oSegment.DataElementValue[4,0];  //ID NO.
               end
               else if sSegmentID = 'N3' then begin
                  sValue := oSegment.DataElementValue[1,0];  //ADDRESS
               end
               else if sSegmentID = 'N4' then begin
                  sValue := oSegment.DataElementValue[1,0];  //CITY
                  sValue := oSegment.DataElementValue[2,0];  //STATE
                  sValue := oSegment.DataElementValue[3,0];  //ZIP
               end;
            end  //sQlfr = 'BT'
            else if sQlfr = 'RE' then begin  //REMIT TO
               if sSegmentID = 'N1' then begin
                  sValue := oSegment.DataElementValue[2,0];  //NAME
                  sValue := oSegment.DataElementValue[4,0];  //ID NO.
               end
               else if sSegmentID = 'N3' then begin
                  sValue := oSegment.DataElementValue[1,0];  //ADDRESS
               end
               else if sSegmentID = 'N4' then begin
                  sValue := oSegment.DataElementValue[1,0];  //CITY
                  sValue := oSegment.DataElementValue[2,0];  //STATE
                  sValue := oSegment.DataElementValue[3,0];  //ZIP
               end;
            end;  //sQlfr = 'RE'
         end;  //sLoopSection = 'N1'
      end  //nArea = 1
      else if nArea = 2 then begin
         if sLoopSection = 'IT1' then begin
            if sSegmentID = 'IT1' then begin
               sValue := oSegment.DataElementValue[2,0];  //QTY SHIPPED
               sValue := oSegment.DataElementValue[3,0];  //UNIT
               sValue := oSegment.DataElementValue[4,0];  //UNIT PRICE
               sValue := oSegment.DataElementValue[7,0];  //PROUCT ID
            end;
         end  //sLoopSection = 'IT1'
         else if sLoopSection = 'IT1;PID' then begin
            if sSegmentID = 'PID' then begin
               sValue := oSegment.DataElementValue[5,0];  //PRODUCT DESCRIPTION
            end;
         end;  //sLoopSection = 'IT1;PID'
      end
      else if nArea = 3 then begin
         if sLoopSection = '' then begin
            if sSegmentID = 'TDS' then begin
               sValue := oSegment.DataElementValue[1,0];  //TOTAL AMOUNT
            end
            else if sSegmentID = 'CAD' then begin  //ROUTING
            end
            else if sSegmentID = 'CTT' then begin  //TOTAL ITEMS
               sValue := oSegment.DataElementValue[1,0];
            end;
         end
         else if sLoopSection = 'ISS' then begin
            if sSegmentID = 'ISS' then begin
               sValue := oSegment.DataElementValue[1,0];  //TOTAL SHIPPED
            end;
         end;
      end;  //nArea

      //get next data segment
      oSegment := oSegment.Next as IediDataSegment;
   end;

   //get errors
   oWarnings := oEdidoc.GetWarnings as IediWarnings;

   //show errors
   nWarningCount := oWarnings.Count;
   for i := 1 to nWarningCount do begin
        oWarning := oWarnings.Warning[i] as IediWarning;
        ListErrors.Items.Add(oWarning.Description);
   end;

   ShowMessage('Translate done');
   cmdTranslate.Enabled := true;

   //Close oEdiDoc and underlying objects
   oEdiDoc.Close;
end;