* This is just an example program to show how to generate an EDI 856 file in
* FoxPro using the Framework EDI

clear all

* CREATES EDIDOC OBJECT 
oEdiDoc = createobject("fredi.ediDocument")
 
* THIS MAKES CERTAIN THAT FREDI ONLY USES THE SEF FILE PROVIDED, AND THAT IT DOES 
* NOT USE ITS BUILT-IN STANDARD REFERENCE TABLE TO GENERATE THE EDI FILE.
oSchemas = oEdiDoc.GetSchemas
oSchemas.EnableStandardReference = .F.
 
* ENABLES FORWARD WRITE, AND INCREASES BUFFER I/O TO IMPROVE PERFORMANCE
oEdiDoc.CursorType = 2
oEdiDoc.Property(1002) = 200 
 
* SET TERMINATORS
oEdiDoc.SegmentTerminator = "~{13:10}"
oEdiDoc.ElementTerminator = "*"
oEdiDoc.CompositeTerminator = ">"
 
* LOADS SEF FILE
oSchema = oEdiDoc.LoadSchema("856_X12-4010.SEF", 0)
 
* CREATES THE ISA SEGMENT
oInterchange = oEdiDoc.CreateInterchange("X","004010")
oSegment = oInterchange.GetDataSegmentHeader
oSegment.DataElementValue(1) = "00"     && Authorization Information Qualifier
oSegment.DataElementValue(2) = "          "     && Authorization Information
oSegment.DataElementValue(3) = "00"     && Security Information Qualifier
oSegment.DataElementValue(4) = "          "     && Security Information
oSegment.DataElementValue(5) = "14"     && Interchange ID Qualifier
oSegment.DataElementValue(6) = "0073268795005  "     && Interchange Sender ID
oSegment.DataElementValue(7) = "ZZ"     && Interchange ID Qualifier
oSegment.DataElementValue(8) = "RECEIVERISA    "     && Interchange Receiver ID
oSegment.DataElementValue(9) = "960807"     && Interchange Date
oSegment.DataElementValue(10) = "1548"     && Interchange Time
oSegment.DataElementValue(11) = "U"     && Interchange Control Standards Identifier
oSegment.DataElementValue(12) = "00401"     && Interchange Control Version Number
oSegment.DataElementValue(13) = "000000020"     && Interchange Control Number
oSegment.DataElementValue(14) = "0"     && Acknowledgment Requested
oSegment.DataElementValue(15) = "T"     && Usage Indicator
oSegment.DataElementValue(16) = ">"     && Component Element Separator
 
* CREATES THE GS SEGMENT
oGroup = oInterchange.CreateGroup("004010")
oSegment = oGroup.GetDataSegmentHeader
oSegment.DataElementValue(1) = "SH"     && Functional Identifier Code
oSegment.DataElementValue(2) = "007326879"     && Application Sender's Code
oSegment.DataElementValue(3) = "RECEIVERGS"     && Application Receiver's Code
oSegment.DataElementValue(4) = "19960807"     && Date
oSegment.DataElementValue(5) = "1548"     && Time
oSegment.DataElementValue(6) = "1"     && Group Control Number
oSegment.DataElementValue(7) = "X"     && Responsible Agency Code
oSegment.DataElementValue(8) = "004010"     && Version / Release / Industry Identifier Code
 
* CREATES THE ST SEGMENT
oTransactionset = oGroup.CreateTransactionSet("856")
oSegment = oTransactionset.GetDataSegmentHeader
oSegment.DataElementValue(1) = "856"     && Transaction Set Identifier Code
oSegment.DataElementValue(2) = "00001"     && Transaction Set Control Number
 
* BSN - BEGINNING SEGMENT FOR SHIP NOTICE
oSegment = oTransactionset.CreateDataSegment("BSN")
oSegment.DataElementValue(1) = "00"     && Transaction Set Purpose Code
oSegment.DataElementValue(2) = "3333"     && Shipment Identification
oSegment.DataElementValue(3) = "20020301"     && Date
oSegment.DataElementValue(4) = "1340"     && Time
oSegment.DataElementValue(5) = "0002"     && Hierarchical Structure Code
 
    * hypothetical numbers of shipments, orders and items
    nShipmentCounter = 1
    nShipments = 1
    nOrderCounter = 1
    nOrders = 1
    nItemCounter = 1
    nItems = 6
    nHlCounter = 0
    
    *********************************************************************************************
    * HL - HIERARCHICAL LEVEL - SHIPMENTS ********************************************************
    Do While nShipmentCounter <= nShipments
    
        nHlCounter = nHlCounter + 1
        nHlOrderParent = nHlCounter

 
        * HL - HIERARCHICAL LEVEL
        oSegment = oTransactionset.CreateDataSegment("HL\HL")
        oSegment.DataElementValue(1) =nHlCounter     && Hierarchical ID Number
        oSegment.DataElementValue(3) = "S"     && Hierarchical Level Code
        oSegment.DataElementValue(4) = "1"     && Hierarchical Child Code
 
        * TD1 - CARRIER DETAILS (QUANTITY AND WEIGHT)
        oSegment = oTransactionset.CreateDataSegment("HL\TD1")
        oSegment.DataElementValue(1) = "TKT"     && Packaging Code
        oSegment.DataElementValue(2) = "207"     && Lading Quantity
        oSegment.DataElementValue(6) = "A3"     && Weight Qualifier
        oSegment.DataElementValue(7) = "46.09"     && Weight
        oSegment.DataElementValue(8) = "01"     && Unit or Basis for Measurement Code
         
        * TD5 - CARRIER DETAILS (ROUTING SEQUENCE/TRANSIT TIME)
        oSegment = oTransactionset.CreateDataSegment("HL\TD5")
        oSegment.DataElementValue(2) = "2"     && Identification Code Qualifier
        oSegment.DataElementValue(3) = "IDCODE"     && Identification Code
        oSegment.DataElementValue(4) = "M"     && Transportation Method/Type Code
        oSegment.DataElementValue(5) = "FREEFORM"     && Routing
         
        * TD3 - CARRIER DETAILS (EQUIPMENT)
        oSegment = oTransactionset.CreateDataSegment("HL\TD3")
        oSegment.DataElementValue(1) = "TN"     && Equipment Description Code
        oSegment.DataElementValue(2) = "BPL3"     && Equipment Initial
        oSegment.DataElementValue(3) = "777777"     && Equipment Number
         
        * REF - REFERENCE IDENTIFICATION
        oSegment = oTransactionset.CreateDataSegment("HL\REF")
        oSegment.DataElementValue(1) = "BM"     && Reference Identification Qualifier
        oSegment.DataElementValue(2) = "33333"     && Reference Identification
         
        * DTM - DATE/TIME REFERENCE
        oSegment = oTransactionset.CreateDataSegment("HL\DTM")
        oSegment.DataElementValue(1) = "011"     && Date/Time Qualifier
        oSegment.DataElementValue(2) = "19971211"     && Date
         
        * DTM - DATE/TIME REFERENCE
        oSegment = oTransactionset.CreateDataSegment("HL\DTM(2)")
        oSegment.DataElementValue(1) = "017"     && Date/Time Qualifier
        oSegment.DataElementValue(2) = "19971211"     && Date
         
        * N1 - NAME
        oSegment = oTransactionset.CreateDataSegment("HL\N1\N1")
        oSegment.DataElementValue(1) = "BT"     && Entity Identifier Code
        oSegment.DataElementValue(2) = "BUYSNACKS"     && Name
        oSegment.DataElementValue(3) = "1"     && Identification Code Qualifier
        oSegment.DataElementValue(4) = "1223334444"     && Identification Code
         
        * N3 - ADDRESS INFORMATION
        oSegment = oTransactionset.CreateDataSegment("HL\N1\N3")
        oSegment.DataElementValue(1) = "P.O. BOX 0000"     && Address Information
         
        * N4 - GEOGRAPHIC LOCATION
        oSegment = oTransactionset.CreateDataSegment("HL\N1\N4")
        oSegment.DataElementValue(1) = "TEMPLE"     && City Name
        oSegment.DataElementValue(2) = "TX"     && State or Province Code
        oSegment.DataElementValue(3) = "76503"     && Postal Code
         
        * N1 - NAME
        oSegment = oTransactionset.CreateDataSegment("HL\N1(2)\N1")
        oSegment.DataElementValue(1) = "ST"     && Entity Identifier Code
        oSegment.DataElementValue(2) = "BUYSNACKS PORT"     && Name
        oSegment.DataElementValue(3) = "1"     && Identification Code Qualifier
        oSegment.DataElementValue(4) = "1223334445"     && Identification Code
         
        * N3 - ADDRESS INFORMATION
        oSegment = oTransactionset.CreateDataSegment("HL\N1(2)\N3")
        oSegment.DataElementValue(1) = "1000 N. SAMPLE HIGHWAY"     && Address Information
         
        * N4 - GEOGRAPHIC LOCATION
        oSegment = oTransactionset.CreateDataSegment("HL\N1(2)\N4")
        oSegment.DataElementValue(1) = "ATHENS"     && City Name
        oSegment.DataElementValue(2) = "GA"     && State or Province Code
        oSegment.DataElementValue(3) = "30603"     && Postal Code
         
        **********************************************************************************************
        * HL - HIERARCHICAL LEVEL - ORDER ************************************************************
        Do While nOrderCounter <= nOrders

            nHlCounter = nHlCounter + 1
            nHlItemParent = nHlCounter
         
            * HL - HIERARCHICAL LEVEL
            oSegment = oTransactionset.CreateDataSegment("HL\HL")
            oSegment.DataElementValue(1) = nHlCounter     && Hierarchical ID Number
            oSegment.DataElementValue(2) = nHlOrderParent     && Hierarchical Parent ID Number
            oSegment.DataElementValue(3) = "O"     && Hierarchical Level Code
            oSegment.DataElementValue(4) = "1"     && Hierarchical Child Code
             
            * PRF - PURCHASE ORDER REFERENCE
            oSegment = oTransactionset.CreateDataSegment("HL\PRF")
            oSegment.DataElementValue(1) = "A99999-01"     && Purchase Order Number
            oSegment.DataElementValue(2) = "016900"     && Release Number
            oSegment.DataElementValue(4) = "19971207"     && Date
             
            * REF - REFERENCE IDENTIFICATION
            oSegment = oTransactionset.CreateDataSegment("HL\REF")
            oSegment.DataElementValue(1) = "IV"     && Reference Identification Qualifier
            oSegment.DataElementValue(2) = "00001"     && Reference Identification
             
            * FOB - F.O.B. RELATED INSTRUCTIONS
            oSegment = oTransactionset.CreateDataSegment("HL\FOB")
            oSegment.DataElementValue(1) = "PS"     && Shipment Method of Payment
            oSegment.DataElementValue(2) = "DE"     && Location Qualifier
             
            *********************************************************************************************
            * HL - HIERARCHICAL LEVEL - ITEMS ************************************************************
            Do While nItemCounter <= nItems
                nHlCounter = nHlCounter + 1
             
                * HL - HIERARCHICAL LEVEL
                oSegment = oTransactionset.CreateDataSegment("HL\HL")
                oSegment.DataElementValue(1) = nHlCounter     && Hierarchical ID Number
                oSegment.DataElementValue(2) = nHlItemParent     && Hierarchical Parent ID Number
                oSegment.DataElementValue(3) = "I"     && Hierarchical Level Code
                oSegment.DataElementValue(4) = "0"     && Hierarchical Child Code
                 
                * LIN - ITEM IDENTIFICATION
                oSegment = oTransactionset.CreateDataSegment("HL\LIN")
                oSegment.DataElementValue(1) = "1"     && Assigned Identification
                oSegment.DataElementValue(2) = "UA"     && Product/Service ID Qualifier
                oSegment.DataElementValue(3) = "Product ID of item 1"     && Product/Service ID
                 
                * SN1 - ITEM DETAIL (SHIPMENT)
                oSegment = oTransactionset.CreateDataSegment("HL\SN1")
                oSegment.DataElementValue(2) = "13"     && Number of Units Shipped
                oSegment.DataElementValue(3) = "CA"     && Unit or Basis for Measurement Code
                oSegment.DataElementValue(5) = "13"     && Quantity Ordered
                oSegment.DataElementValue(6) = "CA"     && Unit or Basis for Measurement Code
                oSegment.DataElementValue(8) = "AC"     && Line Item Status Code
                 
                * PRF - PURCHASE ORDER REFERENCE
                oSegment = oTransactionset.CreateDataSegment("HL\PRF")
                oSegment.DataElementValue(1) = "A99999-01"     && Purchase Order Number
                oSegment.DataElementValue(2) = "016900"     && Release Number
                oSegment.DataElementValue(4) = "19971207"     && Date
                 
                * PID - PRODUCT/ITEM DESCRIPTION
                oSegment = oTransactionset.CreateDataSegment("HL\PID")
                oSegment.DataElementValue(1) = "F"     && Item Description Type
                oSegment.DataElementValue(5) = "Description of item 1"     && Description
                
                nItemCounter = nItemCounter  + 1
            EndDo    && ITEMS
            
            nOrderCounter = nOrderCounter  + 1
        EndDo    && ORDERS
        
        nShipmentCounter = nShipmentCounter  + 1
    EndDo    && SHIPMENT
 
* CTT - TRANSACTION TOTALS
oSegment = oTransactionset.CreateDataSegment("CTT")
oSegment.DataElementValue(1) = "6"     && Number of Line Items
 
* TRAILING SEGMENTS ARE AUTOMATICALLY CREATED WHEN FREDI COMMITS (SAVES)
* THE EDIDOC OBJECT INTO AN EDI FILE.
oEdiDoc.Save("856OUTPUT.X12",0)
 
? "Done"