Beispiel #1
0
        // Constructor takes SEF file name as argument.  Creates ediDocument object.
        public PetSmart852(string sSefPathFile)
        {
            // Create the top-level application object "ediDocument".
            oEdiDoc = new ediDocument();

            // This makes certain that Framework EDI only uses the SEF file provided, and that it does not use its
            // built -in Standard Reference table to translate the EDI document
            oSchemas = (ediSchemas)oEdiDoc.GetSchemas();
            oSchemas.EnableStandardReference = false;

            // The FORWARD-ONLY cursor increases the performance of processing the EDI document
            oEdiDoc.CursorType = DocumentCursorTypeConstants.Cursor_ForwardOnly;

            // Load SEF file
            oEdiDoc.LoadSchema(sSefPathFile, SchemaTypeIDConstants.Schema_Standard_Exchange_Format);

            oConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["EdiDb"].ConnectionString);
            oConnection.Open();

            oDaInterchange = new SqlDataAdapter();
            oDaFuncGroup   = new SqlDataAdapter();
            oDaHeader      = new SqlDataAdapter();
            oDaDetail      = new SqlDataAdapter();
            oProdActivity  = new SqlDataAdapter();
        }
Beispiel #2
0
        protected void AcknowledgeInboundEDIFiles()
        {
            try
            {
                ediDocument oEdiDoc = null;
                ediDataSegment oSegment = null;
                ediAcknowledgment oAck = null;
                ediSchemas oSchemas = null;
                string sSegmentID;
                string sLoopSection;
                int nArea;
                string sPrevEdiFile = "";
                bool bInbound997True = false;
                //  string sPath = AppDomain.CurrentDomain.BaseDirectory;
                SafeAndEdiFilePath objSafeAndEdiFilePath = new SafeAndEdiFilePath();
                EdiFilesPaths EdiFilePath = objSafeAndEdiFilePath.GetFilesAndPaths();

                // string sPath = @"" + ConfigurationManager.AppSettings["AppPath"] + @"\";
                string sPath = EdiFilePath.sPath;
                SqlConnection oConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["EdiDb"].ConnectionString);

                string sInboundPath = EdiFilePath.sInboundPath;//sPath + ConfigurationManager.AppSettings["EDI_Inbound"] + @"\";
                string sOutboundPath = EdiFilePath.sOutboundPath;//sPath + ConfigurationManager.AppSettings["EDI_Outbound"] + @"\";
                string sAcceptPath = EdiFilePath.sAcceptPath;//sPath + ConfigurationManager.AppSettings["EDI_Accepted"] + @"\";
                string sRejectPath = EdiFilePath.sRejectPath;//sPath + ConfigurationManager.AppSettings["EDI_Rejected"] + @"\";
                string s997Path = EdiFilePath.s997Path;//sPath + ConfigurationManager.AppSettings["EDI_997"] + @"\";
                string sSefPath = EdiFilePath.sSefPath;//sPath + ConfigurationManager.AppSettings["Seffolder"] + @"\";
                string sEdiDonePath = EdiFilePath.sEdiDonePath;//sPath + ConfigurationManager.AppSettings["EDI_DONE"] + @"\";

                int nFileCount = 0;

                string[] sEdiPathFiles = Directory.GetFiles(sInboundPath);

                if (sEdiPathFiles.Length == 0)
                {
                    //MessageBox.Show("There are no files in the EDI_Inbound folder.");
                    LogLibrary.WriteErrorLog(" on AcknowledgeInboundEDIFiles" + "There are no files in the EDI_Inbound folder.");
                }
                else
                {
                    System.Windows.Forms.Cursor Cursor = System.Windows.Forms.Cursors.WaitCursor;

                    oConnection.Open();

                    string sSql = "select * from [Config] ";
                    SqlDataAdapter oDaConfig = new SqlDataAdapter(sSql, oConnection);
                    DataSet oConfigDs = new DataSet("dsConfig");
                    oDaConfig.Fill(oConfigDs, "dsConfig");
                    DataRow oConfigRow = oConfigDs.Tables["dsConfig"].Rows[0];

                    string sSenderIdQlfr = oConfigRow["SenderIdQlfr"].ToString();
                    string sSenderId = oConfigRow["SenderId"].ToString();
                    string sReceiverIdQlfr = oConfigRow["ReceiverIdQlfr"].ToString();
                    string sReceiverId = oConfigRow["ReceiverId"].ToString();
                    string sControlNumber = oConfigRow["ControlNumber"].ToString();
                    string sAcknowledgmentRequested = oConfigRow["AcknowledgmentRequested"].ToString();
                    string sUsageIndicator = oConfigRow["UsageIndicator"].ToString();
                    string sComponentElementSeparator = oConfigRow["ComponentElementSeparator"].ToString();

                    Int32 nControlNumber = Convert.ToInt32(sControlNumber);

                    ediDocument.Set(ref oEdiDoc, new ediDocument());

                    //By default, FREDI uses the  universal coordinated time (UTC), however you can change it to local time
                    oEdiDoc.set_Option(DocumentOptionIDConstants.OptDocument_UseLocalTime, 1);

                    // Disabling the internal standard reference library to makes sure that 
                    // FREDI uses only the SEF file provided
                    ediSchemas.Set(ref oSchemas, (ediSchemas)oEdiDoc.GetSchemas());
                    oSchemas.EnableStandardReference = false;

                    // This makes certain that the EDI file must use the same version SEF file, otherwise the process will stop.
                    oSchemas.set_Option(SchemasOptionIDConstants.OptSchemas_VersionRestrict, 1);

                    // By setting the cursor type to ForwardOnly, FREDI does not load the entire file into memory, which
                    // improves performance when processing larger EDI files.
                    oEdiDoc.CursorType = DocumentCursorTypeConstants.Cursor_ForwardOnly;

                    // If an acknowledgment file has to be generated, an acknowledgment object must be created, and its 
                    // property must be enabled before loading the EDI file.
                    oAck = (ediAcknowledgment)oEdiDoc.GetAcknowledgment();
                    oAck.EnableFunctionalAcknowledgment = true;

                    // Load all SEF files from SEF folder.  FREDI will automaticall select the appropriate one.
                    string[] sSefPathFiles = Directory.GetFiles(sSefPath);
                    foreach (string sSefPathFile in sSefPathFiles)
                    {
                        oEdiDoc.LoadSchema(sSefPathFile, 0);

                    } // foreach

                    string sDestTempFile = "dest_temp.txt";

                    using (Stream destStream = File.OpenWrite(sDestTempFile))
                    {
                        foreach (string sEdiPathFile in sEdiPathFiles)
                        {
                            nFileCount = nFileCount + 1;

                            string sEdiFile = Path.GetFileName(sEdiPathFile);
                            DateTime dtEdiFileCreation = File.GetCreationTime(sEdiPathFile);
                            string sEdiFileTranSetNo = "";
                            string sInbound997GroupContolNo = "";
                            string sInbound997TranSetNo = "";
                            string sInbound997Ack501 = "";
                            // Loads EDI file and the corresponding SEF file
                            oEdiDoc.LoadEdi(sEdiPathFile);
                            // This loop will generate a 997 EDI file if the inbound file is itslef not a 997
                            // Gets the first data segment in the EDI files
                            ediDataSegment.Set(ref oSegment, (ediDataSegment)oEdiDoc.FirstDataSegment);  //oSegment = (ediDataSegment) oEdiDoc.FirstDataSegment
                            // This loop iterates though the EDI file (850 or 997) a segment at a time
                            while (oSegment != null)
                            {
                                // A segment is identified by its Area number, Loop section and segment id.
                                sSegmentID = oSegment.ID;
                                sLoopSection = oSegment.LoopSection;
                                nArea = oSegment.Area;

                                if (nArea == 1)
                                {
                                    if (sLoopSection == "")
                                    {
                                        if (sSegmentID == "ST")
                                        {
                                            sEdiFileTranSetNo = oSegment.get_DataElementValue(1);

                                            if (sEdiFileTranSetNo == "997")
                                            {
                                                bInbound997True = true;
                                            }
                                            else
                                            {
                                                bInbound997True = false;
                                            }
                                        }
                                        else if (sSegmentID == "AK1")
                                        {
                                            sInbound997GroupContolNo = oSegment.get_DataElementValue(2);

                                        } // sSegmentID
                                    }
                                    else if (sLoopSection == "AK2")
                                    {
                                        if (sSegmentID == "AK2")
                                        {
                                            sInbound997TranSetNo = oSegment.get_DataElementValue(2);
                                        }
                                        else if (sSegmentID == "AK5")
                                        {
                                            sInbound997Ack501 = oSegment.get_DataElementValue(1);

                                        } // sSegmentID

                                    } // sLoopSection

                                } // nArea

                                //get next data segment
                                ediDataSegment.Set(ref oSegment, (ediDataSegment)oSegment.Next());  //oSegment = (ediDataSegment) oSegment.Next();
                            }

                            // This reads the 997 object that was generated in the above loop to tell us 
                            // if the EDI file received should be accpeted or rejected.  (This does not translate an inbound 997 EDI file.)
                            if (!bInbound997True) // if not an inbound 997 file
                            {
                                // Checks the 997 acknowledgment file just created.
                                // The 997 file is an EDI file, so the logic to read the 997 Functional Acknowledgemnt file is similar
                                // to translating any other EDI file.

                                // Gets the first segment of the 997 acknowledgment file
                                ediDataSegment.Set(ref oSegment, (ediDataSegment)oAck.GetFirst997DataSegment());    //oSegment = (ediDataSegment) oAck.GetFirst997DataSegment();

                                bool bFileAccepted = true;

                                while (oSegment != null)
                                {
                                    nArea = oSegment.Area;
                                    sLoopSection = oSegment.LoopSection;
                                    sSegmentID = oSegment.ID;

                                    if (nArea == 1)
                                    {
                                        if (sLoopSection == "")
                                        {
                                            if (sSegmentID == "AK9")
                                            {
                                                if (oSegment.get_DataElementValue(1, 0) == "R")
                                                {
                                                    bFileAccepted = false;
                                                }
                                            }
                                        }   // sLoopSection == ""
                                    }   //nArea == 1

                                    ediDataSegment.Set(ref oSegment, (ediDataSegment)oSegment.Next());  //oSegment = (ediDataSegment) oSegment.Next();
                                }   //oSegment != null

                                if (bFileAccepted)
                                {
                                    // All accepted EDI files are sent to the ACCEPTED folder and in their corresponding subfolders.
                                    File.Copy(sInboundPath + sEdiFile, sAcceptPath + "\\" + sEdiFileTranSetNo + "\\" + sEdiFileTranSetNo + "_" + dtEdiFileCreation.ToString("yyyyMMddHHmmss") + "_" + sEdiFile, true);

                                }
                                else
                                {
                                    File.Copy(sInboundPath + sEdiFile, sRejectPath + sEdiFileTranSetNo + "_" + dtEdiFileCreation.ToString("yyyyMMddHHmmss") + "_" + sEdiFile, true);
                                    InsertRejectedFile(sEdiFile, sRejectPath, "EDI_Rejected", sEdiFileTranSetNo, dtEdiFileCreation.ToString("yyyyMMddHHmmss"));
                                }

                                // Combine all EDI files.  This file will be read to create one 997 acknowledgment file for all incoming EDI files. 
                                using (Stream ediStream = File.OpenRead(sInboundPath + sEdiFile))
                                {
                                    ediStream.CopyTo(destStream);
                                }

                            } // if !bInbound997True 
                            else
                            {

                                File.Copy(sInboundPath + sEdiFile, s997Path + sEdiFileTranSetNo + "_" + dtEdiFileCreation.ToString("yyyyMMddHHmmss") + "_" + sEdiFile, true);

                            } // if bInbound997True 

                            if (File.Exists(sInboundPath + sPrevEdiFile))
                            {
                                File.Delete(sInboundPath + sPrevEdiFile);
                            }

                            sPrevEdiFile = sEdiFile;

                        } // foreach sEdiPathFile

                    } // using

                    // dispose old ack
                    oAck.Dispose();

                    // instantiate new ack for combined 997
                    oAck = (ediAcknowledgment)oEdiDoc.GetAcknowledgment();
                    oAck.EnableFunctionalAcknowledgment = true;

                    string sIsaControlNoBuff = "000000000" + sControlNumber.Trim();
                    string sIsaControlNo = sIsaControlNoBuff.Substring(sIsaControlNoBuff.Length - 9);

                    // Set the starting point of the control numbers in the acknowledgment
                    oAck.set_Property(AcknowledgmentPropertyIDConstants.PropertyAck_StartInterchangeControlNum, sIsaControlNo);
                    oAck.set_Property(AcknowledgmentPropertyIDConstants.PropertyAck_StartGroupControlNum, sControlNumber);
                    oAck.set_Property(AcknowledgmentPropertyIDConstants.PropertyAck_StartTransactionSetControlNum, 1);

                    oAck.set_Option(AcknowledgmentOptionIDConstants.OptAcknowledgment_ReportToSingleInterchange, 1);
                    oAck.set_Option(AcknowledgmentOptionIDConstants.OptAcknowledgment_ShowReportingLevel, 0);

                    //create combined acknowledgment
                    oEdiDoc.LoadEdi(sDestTempFile);

                    ediDataSegment.Set(ref oSegment, (ediDataSegment)oEdiDoc.FirstDataSegment);

                    while (oSegment != null)
                    {
                        ediDataSegment.Set(ref oSegment, oSegment.Next());

                    }
                    oAck.Save(sOutboundPath + "997_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".X12");

                    //increment Interchange control number
                    sControlNumber = (Convert.ToInt32(sControlNumber) + 1).ToString();
                    sSql = @"UPDATE [Config] SET ControlNumber = @ControlNumber";
                    oDaConfig.UpdateCommand = new SqlCommand(sSql, oConnection);
                    oDaConfig.UpdateCommand.Parameters.AddWithValue("@ControlNumber", sControlNumber);
                    oDaConfig.UpdateCommand.ExecuteNonQuery();

                    oConnection.Close();

                    //Close oEdiDoc to release EDI file
                    oEdiDoc.Close();

                    File.Delete(sDestTempFile);

                    Cursor = System.Windows.Forms.Cursors.Default;

                    if (File.Exists(sInboundPath + sPrevEdiFile))
                    {
                        File.Delete(sInboundPath + sPrevEdiFile);
                    }
                    //tblMessage.Style.Add("display", "block");
                    //lblMessage.Text = "Done. " + nFileCount.ToString() + " file(s) received.";
                } // if sEdiPathFiles.Length
            }
            catch (Exception ex)
            {
                throw ex;
                LogLibrary.WriteErrorLog("Error = " + ex.Message);
            }
        }
        public static int Parse(Edi834DataAccess dbAccess,string content)
        {
            Extras.Logging log = new Extras.Logging();
            //string sEdiFile = ""; ;
            ediDocument oEdiDoc = null;
            //ediSchema oSchema = null;
            ediSchemas oSchemas = null;
            //ediInterchange oInterchange = null;
            //ediGroup oGroup = null;
            //ediTransactionSet oTransactionset = null;
            ediDataSegment oSegment = null;
            //ediAcknowledgment oAck = null;
               // ediWarning oWarning = null;
              //  ediWarnings oWarnings = null;
              //  Int32 nWarningsCount;
            string sPath = AppDomain.CurrentDomain.BaseDirectory;
            string sSefFile = "834_005010X220A1.SemRef.SEF";
            ediAcknowledgment oAck = null;

            //string sValue;
            //string sHlEntity = "";
            //string sNm1Entity = "";
            //string sQlfr;

            //CREATES EDIDOC OBJECT
            oEdiDoc = new ediDocument();
            oEdiDoc.EventNotify += new EventNotifyDelegate(oEdiDoc_EventNotify);
            //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 = (ediSchemas)oEdiDoc.GetSchemas();
            oSchemas.EnableStandardReference = false;

            oAck = (ediAcknowledgment)oEdiDoc.GetAcknowledgment();
            oAck.EnableInterchangeAcknowledgment = true;

            //THIS OPTIONS STOPS FREDI FROM KEEPING ALL THE SEGMENTS IN MEMORY
            oEdiDoc.CursorType = DocumentCursorTypeConstants.Cursor_ForwardOnly;

            //LOADS THE SEF FILE
            oEdiDoc.LoadSchema(sPath + sSefFile, SchemaTypeIDConstants.Schema_Standard_Exchange_Format);

            //LOADS THE EDI FILE
            oEdiDoc.LoadEdiString(content);

            //GETS THE FIRST DATA SEGMENT

            ediDataSegment.Set(ref oSegment, (ediDataSegment)oEdiDoc.FirstDataSegment);

            MsgHeader msg_header=null;
             MsgTrailer msg_trailer =null;
             Int64 msg_HeaderID = 0;
             Int64 msg_transID = 0;
            TransactionHeader trans_Header = null;
            TransactionTrailer trans_Trailer = null;
            Member.HealthCoverage health_coverage=null;
            Member.MemberReport member_report=null;
            Member msg_Member = null;
            while (oSegment != null)
            {
                sSegmentID = oSegment.ID;
                sLoopSection = oSegment.LoopSection;
                nArea = oSegment.Area;
               // Debug.WriteLine("segment id ="+ sSegmentID+"\t section ="+sLoopSection+" \t nArea= "+nArea+"\r\t");

                if (nArea == 0)
                {
                    if (sLoopSection == "")
                    {
                        if (sSegmentID == "ISA")
                        {
                            if (msg_header == null)
                                msg_header = new MsgHeader();
                            msg_header.InterchangeControlHeader=oSegment.get_DataElementValue(1, 0);
                            msg_header.AuthorizationInformation = oSegment.get_DataElementValue(2, 0);
                            msg_header.SecurityInformationQualifier = oSegment.get_DataElementValue(3, 0);
                            msg_header.SecurityInformation = oSegment.get_DataElementValue(4, 0);
                            msg_header.InterchangeIDQualifier_1 = oSegment.get_DataElementValue(5, 0);
                            msg_header.InterchangeSenderID = oSegment.get_DataElementValue(6, 0);
                            msg_header.InterchangeIDQualifier_2 = oSegment.get_DataElementValue(7, 0);
                            msg_header.InterchangeReceiverID = oSegment.get_DataElementValue(8, 0);
                            msg_header.IntegerchangeDate = oSegment.get_DataElementValue(9, 0);
                            msg_header.IntegerchangeTime = oSegment.get_DataElementValue(10, 0);
                            msg_header.RepetitionSeperator = oSegment.get_DataElementValue(11, 0);
                            msg_header.InterchangeControlVersionNumber = oSegment.get_DataElementValue(12, 0);
                            msg_header.InterchangeControlNumber_header = oSegment.get_DataElementValue(13, 0);
                            msg_header.AcknowledgementRequest = oSegment.get_DataElementValue(14, 0);
                            msg_header.UsageIdentifier = oSegment.get_DataElementValue(15, 0);
                            msg_header.ComponentElementSeparator = oSegment.get_DataElementValue(16, 0);

                        }
                        else if (sSegmentID == "GS")
                        {
                            if (msg_header == null)
                                msg_header = new MsgHeader();
                            msg_header.FunctionalGroupHeader_header=oSegment.get_DataElementValue(1,0);
                            msg_header.ApplicationSendersCode=oSegment.get_DataElementValue(2,0);
                            msg_header.ApplicationReceiversCode=oSegment.get_DataElementValue(3,0);
                            msg_header.Date=oSegment.get_DataElementValue(4,0);
                            msg_header.Time=oSegment.get_DataElementValue(5,0);
                            msg_header.GroupControlNumber_header=oSegment.get_DataElementValue(6,0);
                            msg_header.ResponsibleAgencyCode=oSegment.get_DataElementValue(7,0);
                            msg_header.IndustryIndentifierCode=oSegment.get_DataElementValue(8,0);
                            dbAccess.InsertNewMessageHeader(msg_header);        //insert it into the database;
                            msg_header = null;
                            msg_HeaderID = dbAccess.getHeaderID();              //get the header id;

                        }
                        else if (sSegmentID == "GE")
                        {
                            if (msg_trailer == null)
                                msg_trailer = new MsgTrailer();
                            msg_trailer.FunctionalGroupHeader_trailer = oSegment.get_DataElementValue(1, 0);
                            msg_trailer.GroupControlNumber_trailer = oSegment.get_DataElementValue(2, 0);
                        }
                        else if (sSegmentID == "IEA")
                        {
                            msg_trailer.InterchangeControlTrailer = oSegment.get_DataElementValue(1, 0);
                            msg_trailer.InterchangeControlNumber_trailer = oSegment.get_DataElementValue(2, 0);
                            dbAccess.insertNewMessageTrailer(msg_trailer,msg_HeaderID);
                            msg_trailer = null;
                        }
                        else
                        {
                            Debug.WriteLine("segment id ="+ sSegmentID+"\t section ="+sLoopSection+" \t nArea= "+nArea+" is  not  be recorded");
                        }
                    }
                }
                else if (nArea == 1)
                {
                    if (trans_Header == null)
                        trans_Header = new TransactionHeader();
                    if (sLoopSection == "")
                    {
                        if (sSegmentID == "ST")
                        {
                            trans_Header.HeaderID = msg_HeaderID;
                            trans_Header.TransactionSetIdentifier = oSegment.get_DataElementValue(1, 0);
                            trans_Header.TranscationSetControlNumber = oSegment.get_DataElementValue(2, 0);
                            trans_Header.ImplementationConventionReference = oSegment.get_DataElementValue(3, 0);
                        }
                        else if (sSegmentID == "BGN")
                        {
                            if (trans_Header == null)
                        trans_Header = new TransactionHeader();
                            trans_Header.TransactionSetPurposeCode = oSegment.get_DataElementValue(1, 0);
                            trans_Header.ReferenceIdentification_BS = oSegment.get_DataElementValue(2, 0);
                            trans_Header.Date = oSegment.get_DataElementValue(3, 0);
                            trans_Header.Time = oSegment.get_DataElementValue(4, 0);
                            trans_Header.ActionCode = oSegment.get_DataElementValue(5, 0);
                        }
                        else if (sSegmentID == "REF")
                        {if (trans_Header == null)
                        trans_Header = new TransactionHeader();
                            trans_Header.ReferenceIdentificationQualifier = oSegment.get_DataElementValue(1, 0);
                            trans_Header.ReferenceIdentification = oSegment.get_DataElementValue(2, 0);
                        }
                        else if(sSegmentID=="DTP")
                        {
                            //LOOP
                            EDIModel.TransactionHeader.FileDate file_date;
                            file_date.Date_TimeQualifier=oSegment.get_DataElementValue(1,0);
                            file_date.DateTimePeriodFormatQualifier=oSegment.get_DataElementValue(2,0);
                            file_date.DateTimePeriod=oSegment.get_DataElementValue(3,0);
                            trans_Header.FileEffectiveDate.AddLast(file_date);
                        }
                        else if (sSegmentID == "QTY")
                        {
                            EDIModel.TransactionHeader.TransactionSets trans_set;
                            trans_set.QuantityQualifier = oSegment.get_DataElementValue(1, 0);
                            trans_set.Quantity = oSegment.get_DataElementValue(2, 0);
                            trans_Header.TransactionSetControlTotals.AddLast(trans_set);

                        }

                    }
                    else if (sLoopSection == "N1")
                    {
                        if (sSegmentID == "N1" )
                        {
                            string EntityIdentifier = oSegment.get_DataElementValue(1, 0);
                            if (EntityIdentifier == "P5")
                            {
                                trans_Header.EntityIdentifierCode_Sponsor = oSegment.get_DataElementValue(1, 0);
                                trans_Header.Name_Sponsor = oSegment.get_DataElementValue(2, 0);
                                trans_Header.IdentificationCodeQualifier_Sponsor = oSegment.get_DataElementValue(3, 0);
                                //optional
                                trans_Header.IdentificationCode_Sponsor = oSegment.get_DataElementValue(4, 0);

                            }
                            else if (EntityIdentifier =="IN")
                            {
                                trans_Header.EntityIdentifierCode_Payer= oSegment.get_DataElementValue(1, 0);
                                trans_Header.Name_Payer = oSegment.get_DataElementValue(2, 0);
                                trans_Header.IdentificationCodeQualifier_Payer = oSegment.get_DataElementValue(3, 0);
                                trans_Header.IdentificationCode_Payer = oSegment.get_DataElementValue(4, 0);

                            }
                            else
                            {
                                //LOOP
                                EDIModel.TransactionHeader.TPABroker tpa_broker;
                                tpa_broker.EntityIdentityCode = oSegment.get_DataElementValue(1, 0);
                                tpa_broker.Name_TPA_Broker = oSegment.get_DataElementValue(2, 0);
                                tpa_broker.IdentificationCodeQualifier_TPA_Broker = oSegment.get_DataElementValue(3, 0);
                                tpa_broker.IdentificationCode_TPA_Broker = oSegment.get_DataElementValue(4, 0);
                                trans_Header.TPABrokerAccount.AddLast(tpa_broker);
                            }
                        }
                    }
                }

                else if (nArea == 2)
                {
                    if (trans_Header != null)
                    {
                        dbAccess.insertNewTransaction(trans_Header);    //insert the header into database
                        msg_transID = dbAccess.getTransactionID();
                        dbAccess.insertNewTransaction_FileDate(trans_Header.FileEffectiveDate, msg_transID);
                        dbAccess.insertNewTransaction_Sets(trans_Header.TransactionSetControlTotals, msg_transID);
                        dbAccess.insertNewTransaction_TPABroker(trans_Header.TPABrokerAccount, msg_transID);
                        trans_Header = null;
                    }
                    if (sLoopSection == "INS")
                    {
                        if (sSegmentID == "INS")
                        {
                            //Start to import the memeber information;
                            msg_Member = null;
                            msg_Member = new Member();
                            msg_Member.Yes_No_Condition = oSegment.get_DataElementValue(1, 0);
                            msg_Member.IndividualRelationshipCode = oSegment.get_DataElementValue(2, 0);
                            msg_Member.MaintenanceTypeCode_MLD = oSegment.get_DataElementValue(3, 0);
                            //Optional
                            msg_Member.MaintenanceReasonCode = oSegment.get_DataElementValue(4, 0);
                            msg_Member.BenefitStatusCode = oSegment.get_DataElementValue(5, 0);
                            //Optional
                            msg_Member.EmploymentStatusCode = oSegment.get_DataElementValue(8, 0);
                        }
                        else if (sSegmentID == "REF")
                        {
                            string SubscriberNumber = oSegment.get_DataElementValue(1, 0);
                            if (SubscriberNumber == "0F")
                            {
                                msg_Member.SubscriberNumber = oSegment.get_DataElementValue(1, 0);
                                //Optional
                                msg_Member.ReferenceNumber = oSegment.get_DataElementValue(2, 0);
                            }
                            else if (SubscriberNumber == "1L")
                            {
                                msg_Member.MemberPolicyNumber = oSegment.get_DataElementValue(1, 0);
                                msg_Member.ReferenceIdentification_MPM = oSegment.get_DataElementValue(2, 0);
                            }
                            else
                            {
                                //LOOP
                                EDIModel.Member.MemberIdentification member_Identification;
                                member_Identification.MemberIdentificationNumber = oSegment.get_DataElementValue(1, 0);
                                member_Identification.ReferenceIdentification = oSegment.get_DataElementValue(2, 0);
                                msg_Member.MemberIdentificationNumber.AddLast(member_Identification);
                            }
                        }
                        else if (sSegmentID == "DTP")
                        {
                            EDIModel.Member.MemberDate member_level_date;
                            //optional
                            member_level_date.Date_Time_Qualifier = oSegment.get_DataElementValue(1, 0);
                            //optional
                            member_level_date.Date_Time_Format = oSegment.get_DataElementValue(2, 0);
                            //optional
                            member_level_date.Date_Time_Period = oSegment.get_DataElementValue(3, 0);
                            msg_Member.MemberLevelDate.AddLast(member_level_date);
                        }
                        else if (sSegmentID == "LS")
                        {
                            msg_Member.LoopIdentifierCode = oSegment.get_DataElementValue(1, 0);
                        }
                        else if (sSegmentID == "LE")
                        {
                            msg_Member.LoopIdentifierCode = oSegment.get_DataElementValue(1, 0);

                        }

                    }
                    else if (sLoopSection == "")
                    {
                        if (sSegmentID == "SE")
                        {
                            //Insert into database;
                            if (health_coverage != null)
                            {
                                msg_Member.HealthCoverages.AddLast(health_coverage);
                                health_coverage = null;
                            }
                            if (member_report != null)
                            {
                                msg_Member.MemberReportingCategories.AddLast(member_report);
                                member_report = null;
                            }
                            dbAccess.insertNewMember(msg_Member, msg_transID);// insert the member into the database

                            if (trans_Trailer == null)
                                trans_Trailer = new TransactionTrailer();
                            trans_Trailer.NumberOfIncludedSegments = oSegment.get_DataElementValue(1, 0);
                            trans_Trailer.TransactionSetControlNumber = oSegment.get_DataElementValue(2, 0);
                            dbAccess.insertNewTransactionTrailer(trans_Trailer, msg_transID);
                            trans_Trailer = null;

                        }
                    }
                    else if (sLoopSection == "INS;NM1")
                    {
                        if (sSegmentID == "NM1")
                        {
                            msg_Member.EntityIdentifierCode_Member = oSegment.get_DataElementValue(1, 0);
                            msg_Member.EntityTypeQualifier_Member = oSegment.get_DataElementValue(2, 0);
                            msg_Member.NameLast = oSegment.get_DataElementValue(3, 0);
                            msg_Member.NameFirst = oSegment.get_DataElementValue(4, 0);
                            //optional
                            msg_Member.NameMiddle = oSegment.get_DataElementValue(5, 0);
                            msg_Member.IdentificationCodeQualifier = oSegment.get_DataElementValue(8, 0);
                            //optional
                            msg_Member.IdentificationCode = oSegment.get_DataElementValue(9, 0);
                        }
                        else if (sSegmentID == "PER")
                        {
                            msg_Member.ContactFunctionCode = oSegment.get_DataElementValue(1, 0);
                            //keep empty if not given
                            msg_Member.CommunicationNumberQualifier_1 = oSegment.get_DataElementValue(3, 0);
                            msg_Member.CommunicationNumber_1 = oSegment.get_DataElementValue(4, 0);
                            msg_Member.CommunicationNumberQualifier_2 = oSegment.get_DataElementValue(5, 0);
                            msg_Member.CommunicationNumber_2 = oSegment.get_DataElementValue(6, 0);
                            msg_Member.CommunicationNumberQualifier_3 = oSegment.get_DataElementValue(7, 0);
                            msg_Member.CommunicationNumber_3 = oSegment.get_DataElementValue(8, 0);
                        }
                        else if (sSegmentID == "N3")
                        {
                            //Keep Empty if not Given
                            msg_Member.AddressLine_1 = oSegment.get_DataElementValue(1, 0);
                            //optional
                            msg_Member.AddressLine_2 = oSegment.get_DataElementValue(2, 0);
                        }
                        else if (sSegmentID == "N4")
                        {
                            msg_Member.City = oSegment.get_DataElementValue(1, 0);
                            msg_Member.State = oSegment.get_DataElementValue(2, 0);
                            msg_Member.PostalCode = oSegment.get_DataElementValue(3, 0);
                            //Optional
                            msg_Member.LocationQualifier = oSegment.get_DataElementValue(5, 0);
                            //optional
                            msg_Member.LocationIdentifier = oSegment.get_DataElementValue(6, 0);
                        }
                        else if (sSegmentID == "DMG")
                        {
                            msg_Member.Date_Time_FormatQualifier_MD = oSegment.get_DataElementValue(1, 0);
                            msg_Member.Date_Time_Period_MD = oSegment.get_DataElementValue(2, 0);
                            msg_Member.GenderCode = oSegment.get_DataElementValue(3, 0);
                            //optional
                            msg_Member.MaritalStatusCode = oSegment.get_DataElementValue(4, 0);
                            msg_Member.CompositeRaceOrEthnicityInformation = oSegment.get_DataElementValue(5, 0);
                        }
                        else if (sSegmentID == "HLH")
                        {
                            //optional
                            msg_Member.HealthRelatedCode = oSegment.get_DataElementValue(1, 0);
                        }
                    }
                    else if (sLoopSection == "INS;HD")
                    {
                        if (sSegmentID == "HD")
                        {
                            //LOOP Class
                            if (health_coverage == null)
                                health_coverage = new Member.HealthCoverage();
                            else
                            {
                                msg_Member.HealthCoverages.AddLast(health_coverage);
                                health_coverage = new Member.HealthCoverage();
                            }
                            health_coverage.MaintenanceTypeCode = oSegment.get_DataElementValue(1, 0);
                            health_coverage.InsuranceLineCode = oSegment.get_DataElementValue(3, 0);
                            //should set to be empty
                            health_coverage.PlanCoverageDescription = oSegment.get_DataElementValue(4, 0);
                        }
                        else if (sSegmentID == "DTP")
                        {
                            EDIModel.Member.HealthCoverage_Dates health_date;
                            health_date.Date_Time_Qualifier = oSegment.get_DataElementValue(1, 0);
                            health_date.Date_Time_FormatQualifier_HCD = oSegment.get_DataElementValue(2, 0);
                            health_date.Date_Time_Period_HCD = oSegment.get_DataElementValue(3, 0);
                            health_coverage.HealthCoverageDates.AddLast(health_date);
                        }
                        else if (sSegmentID == "REF")
                        {
                            EDIModel.Member.HealthPolicyNum policy_num;
                            policy_num.ReferenceIdentificationQualifier = oSegment.get_DataElementValue(1, 0);
                            policy_num.ReferenceIdentification = oSegment.get_DataElementValue(2, 0);
                            health_coverage.PolicyNumber.AddLast(policy_num);
                        }
                    }
                    else if (sLoopSection == "INS;LX")
                    {
                        if (sSegmentID == "LX")
                        {
                            if (member_report == null)
                                member_report = new Member.MemberReport();
                            else
                            {
                                msg_Member.MemberReportingCategories.AddLast(member_report);
                                member_report = new Member.MemberReport();
                            }
                            member_report.AssignedNumber = oSegment.get_DataElementValue(1, 0);
                        }
                    }
                    else if (sLoopSection == "INS;LX;N1")
                    {
                        if (sSegmentID == "N1")
                        {
                            member_report.EntityIdentifierCode = oSegment.get_DataElementValue(1, 0);
                            member_report.Name = oSegment.get_DataElementValue(2, 0);
                        }
                        else if (sSegmentID == "REF")
                        {
                            member_report.ReferenceIdentificationQualifier = oSegment.get_DataElementValue(1, 0);
                            member_report.ReferenceIdentification = oSegment.get_DataElementValue(2, 0);
                        }
                    }
                }
             ediDataSegment.Set(ref oSegment, (ediDataSegment)oSegment.Next());
            }
            //dbAccess.getHeaderTrailerID();
            //Console.ReadKey();
            string sFileName = "TA1_834_X220A1_" +
                    System.DateTime.Now.Year.ToString() +
                    System.DateTime.Now.Month.ToString().PadLeft(2, '0') +
                    System.DateTime.Now.Day.ToString().PadLeft(2, '0') +
                    System.DateTime.Now.Hour.ToString().PadLeft(2, '0') +
                    System.DateTime.Now.Minute.ToString().PadLeft(2, '0') +
                    System.DateTime.Now.Second.ToString().PadLeft(2, '0') + ".txt";
            oAck.Save(DIR_TA1_OUT+ sFileName);
            oSchemas.Dispose();
            oEdiDoc.Dispose();
            return 0;
        }