* This is just an example program to demonstrate how to translate a HIPAA 834_x095 EDI file into a database 
    * in Foxpro using the Framework EDI component
    
    clear all
    
    sEdiFile = "834_Example.X12"
    sSef = "834_X095.SEF"

    oEdiDoc = createobject("fredi.ediDocument")

    * forwardonly cursor type does not load the entire edi file in memory. Improves performance.
    oEdiDoc.CursorType = 1  && forwardonly

    * THIS MAKES CERTAIN THAT FREDI ONLY USES THE SEF FILE PROVIDED, AND THAT IT DOES 
    * NOT USE ITS BUILT-IN STANDARD REFERENCE TABLE TO TRANSLATE THE EDI FILE.
    oSchemas = oEdiDoc.GetSchemas
    oSchemas.EnableStandardReference = .F.

    * loads sef file
    oEdiDoc.LoadSchema(sSef,0)
    
    * loads edi file
    oEdiDoc.LoadEdi(sEdiFile)
    
    select 0
    use interchange alias interchange
    
    select 0
    use funcgroup alias funcgroup
    
    select 0
    use X095Header alias X095Header
    
    select 0
    use x095member alias x095member
    
    select 0
    use x095coverage alias x095coverage 

    
    
    * BEGIN traverse through all segment of the EDI file
    
    * Get first segment of EDI file
    oSegment = oEdiDoc.FirstDataSegment

    * Loop until no more segments
    Do While ! isnull(oSegment)
    
        * segments are identifed by their area, loop section they are in and their segment id
        sSegmentID = alltrim(oSegment.ID)
        nArea = oSegment.Area
        sLoopSection = alltrim(oSegment.LoopSection)

        
        * write your mapping logic here
        do case
        case nArea == 0
            do case
            case sSegmentID == "ISA" 
                sSenderQlfr = oSegment.DataElementValue(5)     && Interchange ID Qualifier
                sSenderID = oSegment.DataElementValue(6)     && Interchange Sender ID
                sReceiverQlfr = oSegment.DataElementValue(7)     && Interchange ID Qualifier
                sReceiverID = oSegment.DataElementValue(8)     && Interchange Receiver ID
                sIsaDate = oSegment.DataElementValue(9)        && date
                sControlNo = oSegment.DataElementValue(13)  && Interchange Control Number
                
                sInterKey = sSenderQlfr + sSenderID + sControlNo
                 select interchange
                append blank
                replace Interkey with sInterKey, senderqlfr with sSenderQlfr, senderid with sSenderID,;
                        receiverql with sReceiverQlfr, receiverid with sReceiverID,;
                        isadate with sIsaDate, controlno with scontrolno
                
                
            case sSegmentID == "GS" 
                sFuncID = oSegment.DataElementValue(1)     && Functional Identifier Code
                sgrpControlNo = oSegment.DataElementValue(6)     && Group Control Number
                
                select funcgroup
                append blank
                replace interkey with sInterKey, funcid with sFuncID, grpcontrol with sgrpControlNo
                replace senderid with oSegment.DataElementValue(2)
                
            endcase    && sSegmentID 
        
        case nArea == 1
            do case
            case len(trim(sLoopSection)) == 0
                do case
                case sSegmentID == "ST"
                    sMessageNo = oSegment.DataElementValue(1)     && Transaction Set Identifier Code
                    sMsgControlNo = oSegment.DataElementValue(2)     && Transaction Set Control Number
                    
                    select X095Header
                    append blank
                    replace interkey with sInterKey, grpcontrol with sGrpControlNo, messageno with sMessageNo, msgcontrol with sMsgControlNo
                
                case sSegmentID == "BGN" 
                    sReferenceID = oSegment.DataElementValue(2) && reference id
                    sRefDate = oSegment.DataElementValue(3)  && Date
                    
                     * select X095Header
                    replace refid with sReferenceID , refdate with sRefDate
                    
                endcase    && sSegmentID 
                
            case sLoopSection == "N1" 
                If sSegmentID == "N1" Then
                    sNm1Entity = oSegment.DataElementValue(1) && qualifier identifies loop instance
                EndIf
                
                do case
                case sNm1Entity == "P5"  && Plan Sponsor
                    If sSegmentID == "N1" Then  
                        sSponsor = oSegment.DataElementValue(2)
                        if oSegment.DataElementValue(3) == "FI" then && Fed Tax ID
                            sSponsorTaxId =  oSegment.DataElementValue(4) 
                            
                            * select X095Header
                            replace sponsor with sSponsor, sponsorid with sSponsorTaxId
                        endif                        
                    EndIf
                    
                case sNm1Entity == "IN"   && Insurer
                    If sSegmentID == "N1" Then  
                        sInsurer = oSegment.DataElementValue(2)
                        sInsurerId = oSegment.DataElementValue(4) 
                        
                        * select X095Header
                        replace insurer with sInsurer, insurerid with sinsurerID
                    EndIf                    
                endcase && sNm1Entity 
                
            endcase && sLoopSection
        
        case nArea == 2 
            do case
            case sLoopSection == "INS"
                do case
                case sSegmentID == "INS" 
                    select X095Member
                    append blank
                    replace interkey with sInterkey, grpcontrol with sgrpcontrolNo, msgcontrol with smsgcontrolno;

                    replace subscriber with oSegment.DataElementValue(1)  && Subscriber Y/N
                    replace relations with oSegment.DataElementValue(2)  && Individual Relationship
                    replace Benefitcod with oSegment.DataElementValue(5)  && Benefit status code
                                        
                case sSegmentID == "REF" 
                    if oSegment.DataElementValue(1) == "0F" then
                        sSubscriberNo = oSegment.DataElementValue(2)  && Subscriber Number
                        
                        * select X095Member
                        replace subscribno with sSubscriberNo
                    endif
                    if oSegment.DataElementValue(1) == "1L" then
                        sGrpPolicyNo = oSegment.DataElementValue(2)  && Policy Number
                        
                        * select X095Member
                        replace grpolcyno with sGrpPolicyNo
                    endif
                case sSegmentID == "DTP" 
                    If oSegment.DataElementValue(1) == "356" Then && Eligibility Begin Date
                        sEligStart = oSegment.DataElementValue(3)  
                        
                        * select X095Member
                        replace elgstrtdat with sEligStart 
                    endif
                endcase    && sSegmentID 
                
            case sLoopSection == "INS;NM1"
                If sSegmentID == "NM1" Then
                    sNm1Entity = oSegment.DataElementValue(1)  && Get entity qualifer to determine loop instances
                EndIf

                do case
                case sNm1Entity == "74" or sNm1Entity == "IL"  && LOOP 2100A
                    do case 
                    case sSegmentID == "NM1"
                        * select X095Member
                        replace Firstname with oSegment.DataElementValue(4)
                        replace Lastname with oSegment.DataElementValue(3)
                        replace SSN with oSegment.DataElementValue(9)
                        
                    case sSegmentID == "PER"
                        * select X095Member
                        replace homephone with oSegment.DataElementValue(4)
                        replace workphone with oSegment.DataElementValue(6)
                        
                    case sSegmentID == "N3"
                        * select X095Member
                        replace Address with oSegment.DataElementValue(1)
                        
                    case sSegmentID == "N4"
                        * select X095Member
                        replace city with oSegment.DataElementValue(1)
                        replace state with oSegment.DataElementValue(2)
                        replace zip with oSegment.DataElementValue(3)
                        
                    case sSegmentID == "DMG"
                        * select X095Member
                        replace birthdate with oSegment.DataElementValue(2)
                        replace gendercode with oSegment.DataElementValue(3)
                    endcase    && sSegmentID 
                
                case sNm1Entity == "70" && Incorrect insured
                    do case 
                    case sSegmentID == "NM1"
                    case sSegmentID == "PER"
                    case sSegmentID == "N3"
                    case sSegmentID == "N4"
                    case sSegmentID == "DMG"
                    endcase
                
                case sNm1Entity == "31" && Post mailing address
                    do case 
                    case sSegmentID == "NM1"
                    case sSegmentID == "PER"
                    case sSegmentID == "N3"
                    case sSegmentID == "N4"
                    case sSegmentID == "DMG"
                    endcase
                
                case sNm1Entity == "ES" && Employer 
                    do case 
                    case sSegmentID == "NM1"
                    case sSegmentID == "PER"
                    case sSegmentID == "N3"
                    case sSegmentID == "N4"
                    case sSegmentID == "DMG"
                    endcase
                
                case sNm1Entity == "M8" && Educational Institution
                    do case 
                    case sSegmentID == "NM1"
                    case sSegmentID == "PER"
                    case sSegmentID == "N3"
                    case sSegmentID == "N4"
                    case sSegmentID == "DMG"
                    endcase
                
                case sNm1Entity == "S3" && Custodial Parent
                    do case 
                    case sSegmentID == "NM1"
                    case sSegmentID == "PER"
                    case sSegmentID == "N3"
                    case sSegmentID == "N4"
                    case sSegmentID == "DMG"
                    endcase
                
                case sNm1Entity == "E1" && Guradian
                    do case 
                    case sSegmentID == "NM1"
                    case sSegmentID == "PER"
                    case sSegmentID == "N3"
                    case sSegmentID == "N4"
                    case sSegmentID == "DMG"
                    endcase
                    
                endcase    && sNm1Entity                 
            case sLoopSection == "INS;HD" 
                do case
                case sSegmentID == "HD" 
                    
                    select X095Coverage
                    append blank
                    replace interkey with sInterkey, grpcontrol with sgrpcontrolNo, msgcontrol with smsgcontrolno, subscribno with sSubscriberNo 
                    replace Insurecode with oSegment.DataElementValue(3) 

                case sSegmentID == "DTP"
                    * select X095Coverage
                    replace benefitbeg with oSegment.DataElementValue(3) 
                endcase    && sSegmentID 
                                             
            endcase    && sLoopSection 
            
        case nArea == 3 
        
        endcase && nArea 
    
        * Get next segment
        oSegment = oSegment.Next
    EndDo

    ? "Done"

    Click here to download a trial version of the Framework EDI