* This is just an example program to demonstrate how to generate an 834_X095 EDI file in FoxPro using the Framework EDI component
 
 clear
 clear all
 close all
 set talk off
 set echo off
 set confirm off
 set notify off
 
 sSefFile = "834_X095.SEF"
 sEdiFile = "834_X095_Output.X12"
 
* 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(sSefFile, 0)

* OPEN TABLES
select 0
use interchange alias interchange index interchangeId
* index on Interkey to interchangeId



select 0
use funcgroup alias funcgroup index funcgroupid
* index on Interkey + grpcontrol  to funcgroupid

select 0
use x095Header alias X095Header index x095Headerid
* index on interkey + grpcontrol + msgcontrol to x095Headerid

select 0
use x095Member alias x095Member index x095MemberId
* index on interkey + grpcontrol + msgcontrol + subscribno to x095MemberId

select 0
use x095Coverage alias x095Coverage index x095CoverageId
* index on interkey + grpcontrol + msgcontrol + subscribno to x095CoverageId


select interchange
do while not eof()
    ? "interchange"
 
    * CREATES THE ISA SEGMENT
    oInterchange = oEdiDoc.CreateInterchange("X","004010")
    oSegment = oInterchange.GetDataSegmentHeader
    oSegment.DataElementValue(1) = "00"     && Authorization Information Qualifier
    oSegment.DataElementValue(3) = "00"     && Security Information Qualifier
    oSegment.DataElementValue(5) = senderqlfr     && Interchange ID Qualifier
    oSegment.DataElementValue(6) = senderid     && Interchange Sender ID
    oSegment.DataElementValue(7) = "12"  && Interchange ID Qualifier
    oSegment.DataElementValue(8) = "ReceiverID"  && Interchange Receiver ID
    oSegment.DataElementValue(9) = isadate     && 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) = controlno     && Interchange Control Number
    oSegment.DataElementValue(14) = "0"     && Acknowledgment Requested
    oSegment.DataElementValue(15) = "T"     && Usage Indicator
    oSegment.DataElementValue(16) = ":"     && Component Element Separator
    
    sInterkey = Interkey
    
    select funcgroup
    set index to funcgroupid
    go top
    seek sInterkey 
    do while not eof() and trim(Interkey ) == trim(sInterkey )
        ? "func group"
        * CREATES THE GS SEGMENT
        oGroup = oInterchange.CreateGroup("004010X095")
        oSegment = oGroup.GetDataSegmentHeader
        oSegment.DataElementValue(1) = funcid     && Functional Identifier Code
        oSegment.DataElementValue(2) = senderid     && Application Sender's Code
        oSegment.DataElementValue(3) = "ReceiverDept"     && Application Receiver's Code
        oSegment.DataElementValue(4) = "20010821"     && Date
        oSegment.DataElementValue(5) = "1548"     && Time
        oSegment.DataElementValue(6) = grpcontrol     && Group Control Number
        oSegment.DataElementValue(7) = "X"     && Responsible Agency Code
        oSegment.DataElementValue(8) = "004010X095"     && Version / Release / Industry Identifier Code
        
        sGrpControl = grpcontrol     
        
        select X095Header
        set index to x095Headerid
        go top
        seek sInterkey + sGrpControl
        do while not eof() and trim(Interkey ) == trim(sInterkey ) and trim(grpcontrol) == trim(sGrpControl)
            ? "header"
                        
            * CREATES THE ST SEGMENT
            oTransactionset = oGroup.CreateTransactionSet("834")
            oSegment = oTransactionset.GetDataSegmentHeader
            oSegment.DataElementValue(1) = messageno     && Transaction Set Identifier Code
            oSegment.DataElementValue(2) = msgcontrol     && Transaction Set Control Number
             
            * BGN - BEGINNING SEGMENT
            oSegment = oTransactionset.CreateDataSegment("BGN")
            oSegment.DataElementValue(1) = "00"     && Transaction Set Purpose Code
            oSegment.DataElementValue(2) = refid     && Reference Identification
            oSegment.DataElementValue(3) = refdate     && Date
            oSegment.DataElementValue(4) = "1200"     && Time
            oSegment.DataElementValue(8) = "2"     && Action Code
             
            * N1 - NAME
            oSegment = oTransactionset.CreateDataSegment("N1\N1")
            oSegment.DataElementValue(1) = "P5"  && Sponsor    && Entity Identifier Code
            oSegment.DataElementValue(3) = "FI"     && Identification Code Qualifier
            oSegment.DataElementValue(4) = sponsorid     && Identification Code
             
            * N1 - NAME
            oSegment = oTransactionset.CreateDataSegment("N1(2)\N1")
            oSegment.DataElementValue(1) = "IN"  && Insurer   && Entity Identifier Code
            oSegment.DataElementValue(3) = "FI"     && Identification Code Qualifier
            oSegment.DataElementValue(4) = insurerid     && Identification Code

            sMsgcontrol = msgcontrol     
            
            select X095Member
            set index to X095Memberid
            go top
            seek sInterkey + sGrpControl + sMsgcontrol
            
            do while not eof() and trim(Interkey ) == trim(sInterkey ) and trim(grpcontrol) == trim(sGrpControl) and trim(msgcontrol) == trim(sMsgcontrol)
                ? "member"
                            
                * INS - INSURED BENEFIT
                oSegment = oTransactionset.CreateDataSegment("INS\INS")
                oSegment.DataElementValue(1) = subscriber     && Yes/No Condition or Response Code
                oSegment.DataElementValue(2) = relations     && Individual Relationship Code
                oSegment.DataElementValue(3) = "021"     && Maintenance Type Code
                oSegment.DataElementValue(4) = "20"     && Maintenance Reason Code
                oSegment.DataElementValue(5) = benefitcod     && Benefit Status Code
                oSegment.DataElementValue(8) = "FT"     && Employment Status Code
                 
                * REF - REFERENCE IDENTIFICATION
                oSegment = oTransactionset.CreateDataSegment("INS\REF")
                oSegment.DataElementValue(1) = "0F"     && Reference Identification Qualifier
                oSegment.DataElementValue(2) = subscribno   && subscriber number  && Reference Identification
                 
                * REF - REFERENCE IDENTIFICATION
                oSegment = oTransactionset.CreateDataSegment("INS\REF(2)")
                oSegment.DataElementValue(1) = "1L"     && Reference Identification Qualifier
                oSegment.DataElementValue(2) = grpolcyno   && Group Policy number && Reference Identification
                 
                * DTP - DATE OR TIME OR PERIOD
                oSegment = oTransactionset.CreateDataSegment("INS\DTP")
                oSegment.DataElementValue(1) = "356"     && Eligibility    && Date/Time Qualifier
                oSegment.DataElementValue(2) = "D8"     && Date Time Period Format Qualifier
                oSegment.DataElementValue(3) = elgstrtdat     && Eligibility  && Date Time Period
                 
                * NM1 - INDIVIDUAL OR ORGANIZATIONAL NAME
                oSegment = oTransactionset.CreateDataSegment("INS\NM1\NM1")
                oSegment.DataElementValue(1) = "IL"     && Entity Identifier Code
                oSegment.DataElementValue(2) = "1"     && Entity Type Qualifier
                oSegment.DataElementValue(3) = lastname     && Name Last or Organization Name
                oSegment.DataElementValue(4) = firstname   && Name First
                oSegment.DataElementValue(5) = "P"     && Name Middle
                oSegment.DataElementValue(8) = "34"     && Social Security &&  Identification Code Qualifier
                oSegment.DataElementValue(9) = ssn     && ISocial Security  && dentification Code
                 
                * PER - ADMINISTRATIVE COMMUNICATIONS CONTACT
                oSegment = oTransactionset.CreateDataSegment("INS\NM1\PER")
                oSegment.DataElementValue(1) = "IP"     && Contact Function Code
                oSegment.DataElementValue(3) = "HP"     && Communication Number Qualifier
                oSegment.DataElementValue(4) = homephone    && Communication Number
                oSegment.DataElementValue(5) = "WP"     && Communication Number Qualifier
                oSegment.DataElementValue(6) = workphone     && Communication Number
                 
                * N3 - ADDRESS INFORMATION
                oSegment = oTransactionset.CreateDataSegment("INS\NM1\N3")
                oSegment.DataElementValue(1) = address     && Address Information
                oSegment.DataElementValue(2) = "APT 3G"     && Address Information
                 
                * N4 - GEOGRAPHIC LOCATION
                oSegment = oTransactionset.CreateDataSegment("INS\NM1\N4")
                oSegment.DataElementValue(1) = city     && City Name
                oSegment.DataElementValue(2) = state     && State or Province Code
                oSegment.DataElementValue(3) = zip     && Postal Code
                oSegment.DataElementValue(5) = "CY"     && Location Qualifier
                oSegment.DataElementValue(6) = "CUMBERLAND"     && Location Identifier
                 
                * DMG - DEMOGRAPHIC INFORMATION
                oSegment = oTransactionset.CreateDataSegment("INS\NM1\DMG")
                oSegment.DataElementValue(1) = "D8"     && Date Time Period Format Qualifier
                oSegment.DataElementValue(2) = birthdate     && Date Time Period
                oSegment.DataElementValue(3) = gendercode     && Gender Code

                sSubscriberno = subscribno   
                
                select X095Coverage
                set index to X095Coverageid
                go top
                seek sInterkey + sGrpControl + sMsgcontrol
                do while not eof() and trim(Interkey ) == trim(sInterkey ) and trim(grpcontrol) == trim(sGrpControl) and trim(msgcontrol) == trim(smsgcontrol) and trim(sSubscriberno) == trim(subscribno)
                    ? "coverage"
                
                    * HD - HEALTH COVERAGE
                    oSegment = oTransactionset.CreateDataSegment("INS\HD\HD")
                    oSegment.DataElementValue(1) = "021"     && Maintenance Type Code
                    oSegment.DataElementValue(3) = insurecode     && Insurance Line Code
                     
                    * DTP - DATE OR TIME OR PERIOD
                    oSegment = oTransactionset.CreateDataSegment("INS\HD\DTP")
                    oSegment.DataElementValue(1) = "348"     && Date/Time Qualifier
                    oSegment.DataElementValue(2) = "D8"     && Date Time Period Format Qualifier
                    oSegment.DataElementValue(3) = benefitbeg     && Date Time Period
                     
                    * COB - COORDINATION OF BENEFITS
                    oSegment = oTransactionset.CreateDataSegment("INS\HD\COB\COB")
                    oSegment.DataElementValue(1) = "P"     && Payer Responsibility Sequence Number Code
                    oSegment.DataElementValue(2) = "890111"     && Reference Identification
                    oSegment.DataElementValue(3) = "5"     && Coordination of Benefits Code
                     
                    * N1 - NAME
                    oSegment = oTransactionset.CreateDataSegment("INS\HD\COB\N1")
                    oSegment.DataElementValue(1) = "IN"     && Entity Identifier Code
                    oSegment.DataElementValue(2) = "ABC INSURANCE CO"     && Name

                    select x095Coverage
                    skip
                EndDo    && x095Coverage
                
                select x095Member
                skip
            EndDo    && x095Member
            
            select x095header
            skip
        EndDo    && x096header
        
        select funcgroup
        skip
    EndDo    && funcgroup
    
    select interchange
    skip
EndDo && interchange
     
* TRAILING SEGMENTS ARE AUTOMATICALLY CREATED WHEN FREDI COMMITS (SAVES)
* THE EDIDOC OBJECT INTO AN EDI FILE.
oEdiDoc.Save(sEdiFile,0)
     
? "Done"