Unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    ListBox1: TListBox;
    txtInvoiceNo: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
   oEdiDoc: Iedidocument;
   oSchemas: IediSchemas;
   oInterchange: IediInterchange;
   oGroup: IediGroup;
   oTransactionset: IediTransactionset;
   oSegment: IediDataSegment;
   oWarning: IediWarning;
   oWarnings: IediWarnings;

   sPath: string;
   sEdiFile: string;
   sSefFile: string;
   i: integer;
   nWarningCount: integer;


begin
   // This is just an example program to demonstrate how to generate and then
   // validate an 810 EDI file in Delphi using the Framework EDI component.
   
   button1.Enabled := false;
   sPath := Trim(GetCurrentDir) + '\';
   sEdiFile := '810Output.x12';
   sSefFile := '810_X12-4010.sef';

   //CREATE EDI DOCUMENT OBJECT
   oEdidoc := coedidocument.Create ;

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

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

   //LOAD SEF FILE
   oEdidoc.LoadSchema(sPath + sSefFile,0);

   //SET TERMINATORS
   oEdidoc.SegmentTerminator := chr(13) + chr(10);
   oEdidoc.ElementTerminator := '*';
   oedidoc.CompositeTerminator := '>';


   //CREATE ISA SEGMENT
   oInterchange := oEdidoc.CreateInterchange('X','004010') as IediInterchange;
   oSegment := oInterchange.GetDataSegmentHeader as IediDataSegment;
   oSegment.DataElementValue[1,0] := '00';
   oSegment.DataElementValue[2,0] := ' ';
   oSegment.DataElementValue[3,0] := '00';
   oSegment.DataElementValue[4,0] := ' ';
   oSegment.DataElementValue[5,0] := 'ZZ';
   oSegment.DataElementValue[6,0] := 'SENDERID';
   oSegment.DataElementValue[7,0] := 'ZZ';
   oSegment.DataElementValue[8,0] := 'RECEIVERID';
   oSegment.DataElementValue[9,0] := '960702';
   oSegment.DataElementValue[10,0] := '1230';
   oSegment.DataElementValue[11,0] := 'U';
   oSegment.DataElementValue[12,0] := '00401';
   oSegment.DataElementValue[13,0] := '000000020';
   oSegment.DataElementValue[14,0] := '0';
   oSegment.DataElementValue[15,0] := 'T';
   oSegment.DataElementValue[16,0] := '>';

   //CREATE GS SEGMENT
   oGroup := oInterchange.CreateGroup('004010') as IediGroup;
   oSegment := oGroup.GetDataSegmentHeader as IediDataSegment;
   oSegment.DataElementValue[1,0] := 'IN';
   oSegment.DataElementValue[2,0] := 'SENDERDEPT';
   oSegment.DataElementValue[3,0] := 'RECEIVERDEPT';
   oSegment.DataElementValue[4,0] := '19961230';
   oSegment.DataElementValue[5,0] := '1548';
   oSegment.DataElementValue[6,0] := '1';
   oSegment.DataElementValue[7,0] := 'X';
   oSegment.DataElementValue[8,0] := '004010';

   //CREATE ST SEGMENT
   oTransactionset := oGroup.CreateTransactionset('810') as IediTransactionset;
   oSegment := oTransactionset.GetDataSegmentHeader as IediDataSegment;
   oSegment.DataElementValue[2,0] := '12345';

   //BIG
   oSegment := oTransactionset.CreateDataSegment('BIG') as IediDataSegment;
   oSegment.DataElementValue[1,0] := '20020305';  //this is a mandatory data element - omitting it will cause an error
   oSegment.DataElementValue[2,0] := txtInvoiceNo.Text;
   oSegment.DataElementValue[4,0] := '7890';

   //Ship To
   //N1 SEGMENT IN LOOP N1
    oSegment := oTransactionset.CreateDataSegment('N1\N1') as IediDataSegment;
    oSegment.DataElementValue[1,0] := 'ST';
    oSegment.DataElementValue[2,0] := 'BUYSNACKS PORT';
    oSegment.DataElementValue[3,0] := '9';
    oSegment.DataElementValue[4,0] := '1223334445';

    //N3 SEGMENT IN LOOP N1
    oSegment := oTransactionset.CreateDataSegment('N1\N3') as IediDataSegment;
    oSegment.DataElementValue[1,0] := '1000 N. SAMPLE HIGHWAY';

    //N4 SEGMENT IN LOOP N1
    oSegment := oTransactionset.CreateDataSegment('N1\N4') as IediDataSegment;
    oSegment.DataElementValue[1,0] := 'ATHENS';
    oSegment.DataElementValue[2,0] := 'GA';
    oSegment.DataElementValue[3,0] := '30603';

    //Bill To
    //SECOND INSTANCE OF N1 SEGMENT IN LOOP N1
    oSegment := oTransactionset.CreateDataSegment('N1(2)\N1') as IediDataSegment;  //it is not necessary to include the loop instance count e.g. (2) in the syntax when cursor type is forwardwrite
    oSegment.DataElementValue[1,0] := 'BT';
    oSegment.DataElementValue[2,0] := 'BUYSNACKS';
    oSegment.DataElementValue[3,0] := '9';
    oSegment.DataElementValue[4,0] := '1223334444';

    //SECOND INSTANCE OF N3 SEGMENT IN LOOP N1
    oSegment := oTransactionset.CreateDataSegment('N1(2)\N3') as IediDataSegment;
    oSegment.DataElementValue[1,0] := 'P.O. BOX 0000';

    oSegment := oTransactionset.CreateDataSegment('N1(2)\N4') as IediDataSegment;
    oSegment.DataElementValue[1,0] := 'TEMPLE';
    oSegment.DataElementValue[2,0] := 'TX';
    oSegment.DataElementValue[3,0] := '76503';

    //Remit To
    //THIRD INSTANCE OF N1 SEGMENT IN LOOP N1
    oSegment := oTransactionset.CreateDataSegment('N1(3)\N1') as IediDataSegment;
    oSegment.DataElementValue[1,0] := 'RE';
    oSegment.DataElementValue[2,0] := 'FOODSELLER';
    oSegment.DataElementValue[3,0] := '9';
    oSegment.DataElementValue[4,0] := '12345QQQQ';

    //THIRD INSTANCE OF N3 IN LOOP N1
    oSegment := oTransactionset.CreateDataSegment('N1(3)\N3') as IediDataSegment;
    oSegment.DataElementValue[1,0] := 'P.O. BOX 222222';

    oSegment := oTransactionset.CreateDataSegment('N1(3)\N4') as IediDataSegment;
    oSegment.DataElementValue[1,0] := 'DALLAS';
    oSegment.DataElementValue[2,0] := 'TX';
    oSegment.DataElementValue[3,0] := '723224444';

    //ITD
    oSegment := oTransactionset.CreateDataSegment('ITD') as IediDataSegment;
    oSegment.DataElementValue[1,0] := '01';
    oSegment.DataElementValue[2,0] := '3';
    oSegment.DataElementValue[3,0] := '1.000';
    oSegment.DataElementValue[5,0] := '15';
    oSegment.DataElementValue[7,0] := '16';
    oSegment.DataElementValue[12,0] := '1/15 NET 30';

    //FOB
    oSegment := oTransactionset.CreateDataSegment('FOB') as IediDataSegment;
    oSegment.DataElementValue[1,0] := 'PP';

    //IT1
    for i := 1 to 6 do
    begin
        oSegment := oTransactionset.CreateDataSegment('IT1(' + trim(IntToStr(i)) + ')\IT1') as IediDataSegment;
        oSegment.DataElementValue[2,0] := '16';
        oSegment.DataElementValue[3,0] := 'CA';
        oSegment.DataElementValue[4,0] := '12.34';
        oSegment.DataElementValue[6,0] := 'UA';
        oSegment.DataElementValue[7,0] := '002840022222';

        oSegment := oTransactionset.CreateDataSegment('IT1(' + trim(IntToStr(i)) + ')\PID\PID') as IediDataSegment;
        oSegment.DataElementValue[1,0] := 'F';
        oSegment.DataElementValue[5,0] := 'CRUNCHY CHIPS LSS';
    end;

    //TOTAL AMOUNT
    oSegment := oTransactionset.CreateDataSegment('TDS') as IediDataSegment;
    oSegment.DataElementValue[1,0] := '25538';

    //ROUTING DESC
    oSegment := oTransactionset.CreateDataSegment('CAD') as IediDataSegment;
    oSegment.DataElementValue[5,0] := 'FREEFORM';

    //ISS SEGMENT IN LOOP ISS
    oSegment := oTransactionset.CreateDataSegment('ISS\ISS') as IediDataSegment;
    oSegment.DataElementValue[1,0] := '96';
    oSegment.DataElementValue[2,0] := 'CA';

    //ITEM COUNT
    oSegment := oTransactionset.CreateDataSegment('CTT') as IediDataSegment;
    oSegment.DataElementValue[1,0] := '6';

   //write the edi document object to a file
   oEdidoc.Save(sPath + sEdiFile,0);

   //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;
        ListBox1.Items.Add(oWarning.Description);
   end;

   ShowMessage('Done. Output = ' + sPath + sEdiFile);

end;