Esempio n. 1
0
        public static void Add_GPOSes_to_ContractTables(List <GPOS_Model> parameter, DbConnection Connection, DbTransaction Transaction, SessionSecurityTicket securityTicket)
        {
            var accountforTenant = ORM_USR_Account.Query.Search(Connection, Transaction, new ORM_USR_Account.Query()
            {
                IsDeleted     = false,
                USR_AccountID = securityTicket.AccountID,
                Tenant_RefID  = securityTicket.TenantID
            }).Single();

            var businessParticipantForTenant = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, new ORM_CMN_BPT_BusinessParticipant.Query()
            {
                IsDeleted    = false,
                Tenant_RefID = securityTicket.TenantID,
                CMN_BPT_BusinessParticipantID = accountforTenant.BusinessParticipant_RefID
            }).Single();

            var searchContractParties = ORM_CMN_CTR_Contract_Party.Query.Search(Connection, Transaction, new ORM_CMN_CTR_Contract_Party.Query()
            {
                IsDeleted    = false,
                Tenant_RefID = securityTicket.TenantID,
                Undersigning_BusinessParticipant_RefID = businessParticipantForTenant.CMN_BPT_BusinessParticipantID
            }).ToList();
            Guid contractID = Guid.NewGuid();


            var catalog1 = new ORM_HEC_BIL_PotentialCode_Catalog();

            catalog1.IsDeleted    = false;
            catalog1.Tenant_RefID = securityTicket.TenantID;
            catalog1.HEC_BIL_PotentialCode_CatalogID = Guid.NewGuid();
            catalog1.GlobalPropertyMatchingID        = "mm.docconnect.gpos.catalog.operation";
            catalog1.Creation_Timestamp = DateTime.Now;
            catalog1.Save(Connection, Transaction);


            var catalog2 = new ORM_HEC_BIL_PotentialCode_Catalog();

            catalog2.IsDeleted    = false;
            catalog2.Tenant_RefID = securityTicket.TenantID;
            catalog2.HEC_BIL_PotentialCode_CatalogID = Guid.NewGuid();
            catalog2.GlobalPropertyMatchingID        = "mm.docconnect.gpos.catalog.nachsorge";
            catalog2.Creation_Timestamp = DateTime.Now;
            catalog2.Save(Connection, Transaction);

            var catalog3 = new ORM_HEC_BIL_PotentialCode_Catalog();

            catalog3.IsDeleted    = false;
            catalog3.Tenant_RefID = securityTicket.TenantID;
            catalog3.HEC_BIL_PotentialCode_CatalogID = Guid.NewGuid();
            catalog3.GlobalPropertyMatchingID        = "mm.docconnect.gpos.catalog.zusatzposition.bevacizumab";
            catalog3.Creation_Timestamp = DateTime.Now;
            catalog3.Save(Connection, Transaction);

            var catalog4 = new ORM_HEC_BIL_PotentialCode_Catalog();

            catalog4.IsDeleted    = false;
            catalog4.Tenant_RefID = securityTicket.TenantID;
            catalog4.HEC_BIL_PotentialCode_CatalogID = Guid.NewGuid();
            catalog4.GlobalPropertyMatchingID        = "mm.docconnect.gpos.catalog.wartezeitenmanagement";
            catalog4.Creation_Timestamp = DateTime.Now;
            catalog4.Save(Connection, Transaction);

            foreach (var partie in searchContractParties)
            {
                var searchContract = ORM_CMN_CTR_Contract.Query.Search(Connection, Transaction, new ORM_CMN_CTR_Contract.Query()
                {
                    IsDeleted          = false,
                    Tenant_RefID       = securityTicket.TenantID,
                    CMN_CTR_ContractID = partie.Contract_RefID,
                    ContractName       = "IVI-Vertrag",
                }).SingleOrDefault();
                if (searchContract != null)
                {
                    contractID = searchContract.CMN_CTR_ContractID;
                }
            }


            var hecContract = ORM_HEC_CRT_InsuranceToBrokerContract.Query.Search(Connection, Transaction, new ORM_HEC_CRT_InsuranceToBrokerContract.Query()
            {
                IsDeleted    = false,
                Tenant_RefID = securityTicket.TenantID,
                Ext_CMN_CTR_Contract_RefID = contractID
            }).Single();

            foreach (var GPOSData in parameter)
            {
                var contract2BillCodes = new ORM_HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCode();
                contract2BillCodes.IsDeleted = false;
                contract2BillCodes.HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCodeID = Guid.NewGuid();
                contract2BillCodes.Tenant_RefID = securityTicket.TenantID;
                contract2BillCodes.InsuranceToBrokerContract_RefID = hecContract.HEC_CRT_InsuranceToBrokerContractID;
                contract2BillCodes.PotentialBillCode_RefID         = Guid.NewGuid();
                contract2BillCodes.Creation_Timestamp     = DateTime.Now;
                contract2BillCodes.Modification_Timestamp = DateTime.Now;
                contract2BillCodes.Save(Connection, Transaction);

                var  DBLanguages       = cls_Get_All_Languages.Invoke(Connection, Transaction, securityTicket).Result.ToList();
                Dict PotentialCodeName = new Dict(ORM_HEC_BIL_PotentialCode.TableName);
                for (int i = 0; i < DBLanguages.Count; i++)
                {
                    PotentialCodeName.AddEntry(DBLanguages[i].CMN_LanguageID, GPOSData.GPOSTitle);
                }

                Guid billCatalogID = Guid.NewGuid();
                switch (GPOSData.GPOSTitle)
                {
                case "Operation":
                    billCatalogID = catalog1.HEC_BIL_PotentialCode_CatalogID;
                    break;

                case "Nachsorge":
                    billCatalogID = catalog2.HEC_BIL_PotentialCode_CatalogID;
                    break;

                case "Zusatzposition Bevacizumab":
                    billCatalogID = catalog3.HEC_BIL_PotentialCode_CatalogID;
                    break;

                case "Wartezeitenmanagement":
                    billCatalogID = catalog4.HEC_BIL_PotentialCode_CatalogID;
                    break;
                }
                ;


                var potentialBillCode = new ORM_HEC_BIL_PotentialCode();
                potentialBillCode.IsDeleted               = false;
                potentialBillCode.Tenant_RefID            = securityTicket.TenantID;
                potentialBillCode.HEC_BIL_PotentialCodeID = contract2BillCodes.PotentialBillCode_RefID;
                potentialBillCode.Creation_Timestamp      = DateTime.Now;
                potentialBillCode.Modification_Timestamp  = DateTime.Now;
                potentialBillCode.CodeName    = PotentialCodeName;
                potentialBillCode.BillingCode = GPOSData.BillingCode;
                potentialBillCode.Price_RefID = Guid.NewGuid();
                potentialBillCode.PotentialCode_Catalog_RefID = billCatalogID;
                potentialBillCode.Save(Connection, Transaction);


                var hecBillCodes2UniversalProperty = new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_2_UniversalProperty();
                hecBillCodes2UniversalProperty.IsDeleted                      = false;
                hecBillCodes2UniversalProperty.Tenant_RefID                   = securityTicket.TenantID;
                hecBillCodes2UniversalProperty.Creation_Timestamp             = DateTime.Now;
                hecBillCodes2UniversalProperty.Modification_Timestamp         = DateTime.Now;
                hecBillCodes2UniversalProperty.AssignmentID                   = Guid.NewGuid();
                hecBillCodes2UniversalProperty.CoveredPotentialBillCode_RefID = contract2BillCodes.HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCodeID;
                hecBillCodes2UniversalProperty.CoveredPotentialBillCode_UniversalProperty_RefID = Guid.NewGuid();
                hecBillCodes2UniversalProperty.Value_String = GPOSData.Case_Type;
                hecBillCodes2UniversalProperty.Save(Connection, Transaction);

                var billCodesUniversalProperty = new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalProperty();
                billCodesUniversalProperty.IsDeleted              = false;
                billCodesUniversalProperty.Tenant_RefID           = securityTicket.TenantID;
                billCodesUniversalProperty.Creation_Timestamp     = DateTime.Now;
                billCodesUniversalProperty.Modification_Timestamp = DateTime.Now;
                billCodesUniversalProperty.HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalPropertyID = hecBillCodes2UniversalProperty.CoveredPotentialBillCode_RefID;
                billCodesUniversalProperty.PropertyName   = GPOSData.Case_Type_Value;
                billCodesUniversalProperty.IsValue_Number = true;
                billCodesUniversalProperty.Save(Connection, Transaction);


                var hecBillCodes2UniversalProperty2 = new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_2_UniversalProperty();
                hecBillCodes2UniversalProperty2.IsDeleted                      = false;
                hecBillCodes2UniversalProperty2.Tenant_RefID                   = securityTicket.TenantID;
                hecBillCodes2UniversalProperty2.Creation_Timestamp             = DateTime.Now;
                hecBillCodes2UniversalProperty2.CoveredPotentialBillCode_RefID = contract2BillCodes.HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCodeID;
                hecBillCodes2UniversalProperty2.CoveredPotentialBillCode_UniversalProperty_RefID = Guid.NewGuid();
                hecBillCodes2UniversalProperty2.Value_Number = GPOSData.From_inj_No;
                hecBillCodes2UniversalProperty2.Save(Connection, Transaction);

                var billCodesUniversalProperty2 = new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalProperty();
                billCodesUniversalProperty2.IsDeleted              = false;
                billCodesUniversalProperty2.Tenant_RefID           = securityTicket.TenantID;
                billCodesUniversalProperty2.Creation_Timestamp     = DateTime.Now;
                billCodesUniversalProperty2.Modification_Timestamp = DateTime.Now;
                billCodesUniversalProperty2.HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalPropertyID = hecBillCodes2UniversalProperty2.CoveredPotentialBillCode_UniversalProperty_RefID;
                billCodesUniversalProperty2.PropertyName   = GPOSData.From_inj;
                billCodesUniversalProperty2.IsValue_Number = true;
                billCodesUniversalProperty2.Save(Connection, Transaction);

                var hecBillCodes2UniversalProperty3 = new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_2_UniversalProperty();
                hecBillCodes2UniversalProperty3.IsDeleted                      = false;
                hecBillCodes2UniversalProperty3.Tenant_RefID                   = securityTicket.TenantID;
                hecBillCodes2UniversalProperty3.Creation_Timestamp             = DateTime.Now;
                hecBillCodes2UniversalProperty3.CoveredPotentialBillCode_RefID = contract2BillCodes.HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCodeID;
                hecBillCodes2UniversalProperty3.CoveredPotentialBillCode_UniversalProperty_RefID = Guid.NewGuid();
                hecBillCodes2UniversalProperty3.Value_Number = GPOSData.To_inj_No;
                hecBillCodes2UniversalProperty3.Save(Connection, Transaction);

                var billCodesUniversalProperty3 = new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalProperty();
                billCodesUniversalProperty3.IsDeleted              = false;
                billCodesUniversalProperty3.Tenant_RefID           = securityTicket.TenantID;
                billCodesUniversalProperty3.Creation_Timestamp     = DateTime.Now;
                billCodesUniversalProperty3.Modification_Timestamp = DateTime.Now;
                billCodesUniversalProperty3.HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalPropertyID = hecBillCodes2UniversalProperty3.CoveredPotentialBillCode_UniversalProperty_RefID;
                billCodesUniversalProperty3.PropertyName   = GPOSData.To_inj;
                billCodesUniversalProperty3.IsValue_String = true;
                billCodesUniversalProperty3.Save(Connection, Transaction);

                var hecBillCodes2UniversalProperty4 = new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_2_UniversalProperty();
                hecBillCodes2UniversalProperty4.IsDeleted                      = false;
                hecBillCodes2UniversalProperty4.Tenant_RefID                   = securityTicket.TenantID;
                hecBillCodes2UniversalProperty4.Creation_Timestamp             = DateTime.Now;
                hecBillCodes2UniversalProperty4.CoveredPotentialBillCode_RefID = contract2BillCodes.HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCodeID;
                hecBillCodes2UniversalProperty4.CoveredPotentialBillCode_UniversalProperty_RefID = Guid.NewGuid();
                hecBillCodes2UniversalProperty4.Value_Boolean = GPOSData.Waive_with_Order_value;
                hecBillCodes2UniversalProperty4.Save(Connection, Transaction);

                var billCodesUniversalProperty4 = new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalProperty();
                billCodesUniversalProperty4.IsDeleted              = false;
                billCodesUniversalProperty4.Tenant_RefID           = securityTicket.TenantID;
                billCodesUniversalProperty4.Creation_Timestamp     = DateTime.Now;
                billCodesUniversalProperty4.Modification_Timestamp = DateTime.Now;
                billCodesUniversalProperty4.HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalPropertyID = hecBillCodes2UniversalProperty4.CoveredPotentialBillCode_UniversalProperty_RefID;
                billCodesUniversalProperty4.PropertyName    = GPOSData.Waive_with_Order;
                billCodesUniversalProperty4.IsValue_Boolean = true;
                billCodesUniversalProperty4.Save(Connection, Transaction);



                var hecBillCodes2UniversalProperty5 = new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_2_UniversalProperty();
                hecBillCodes2UniversalProperty5.IsDeleted                      = false;
                hecBillCodes2UniversalProperty5.Tenant_RefID                   = securityTicket.TenantID;
                hecBillCodes2UniversalProperty5.Creation_Timestamp             = DateTime.Now;
                hecBillCodes2UniversalProperty5.CoveredPotentialBillCode_RefID = contract2BillCodes.HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCodeID;
                hecBillCodes2UniversalProperty5.CoveredPotentialBillCode_UniversalProperty_RefID = Guid.NewGuid();
                hecBillCodes2UniversalProperty5.Value_String = GPOSData.Service_fee_in_Eur_Value;
                hecBillCodes2UniversalProperty5.Save(Connection, Transaction);

                var billCodesUniversalProperty5 = new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalProperty();
                billCodesUniversalProperty5.IsDeleted              = false;
                billCodesUniversalProperty5.Tenant_RefID           = securityTicket.TenantID;
                billCodesUniversalProperty5.Creation_Timestamp     = DateTime.Now;
                billCodesUniversalProperty5.Modification_Timestamp = DateTime.Now;
                billCodesUniversalProperty5.HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalPropertyID = hecBillCodes2UniversalProperty5.CoveredPotentialBillCode_UniversalProperty_RefID;
                billCodesUniversalProperty5.PropertyName   = GPOSData.Service_fee_in_Eur;
                billCodesUniversalProperty5.IsValue_String = true;
                billCodesUniversalProperty5.Save(Connection, Transaction);



                var currencyforTenant = ORM_CMN_Currency.Query.Search(Connection, Transaction, new ORM_CMN_Currency.Query()
                {
                    IsDeleted    = false,
                    Tenant_RefID = securityTicket.TenantID
                }).First();

                var price = new ORM_CMN_Price();
                price.CMN_PriceID        = potentialBillCode.Price_RefID;
                price.IsDeleted          = false;
                price.Tenant_RefID       = securityTicket.TenantID;
                price.Creation_Timestamp = DateTime.Now;
                price.Save(Connection, Transaction);

                var priceValue = new ORM_CMN_Price_Value();
                priceValue.CMN_Price_ValueID         = Guid.NewGuid();
                priceValue.IsDeleted                 = false;
                priceValue.Tenant_RefID              = securityTicket.TenantID;
                priceValue.Price_RefID               = price.CMN_PriceID;
                priceValue.PriceValue_Amount         = GPOSData.Fee_in_EUR;
                priceValue.PriceValue_Currency_RefID = currencyforTenant.CMN_CurrencyID;
                priceValue.Save(Connection, Transaction);


                var hecContract2Diagnosis = ORM_HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialDiagnosis.Query.Search(Connection, Transaction, new ORM_HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialDiagnosis.Query()
                {
                    IsDeleted    = false,
                    Tenant_RefID = securityTicket.TenantID,
                    InsuranceToBrokerContract_RefID = hecContract.HEC_CRT_InsuranceToBrokerContractID
                }).ToList();
                List <ORM_HEC_DIA_PotentialDiagnosis> potendialDiagnosisList = new List <ORM_HEC_DIA_PotentialDiagnosis>();
                foreach (var ctr2dia in hecContract2Diagnosis)
                {
                    var potentialDiagnosis = ORM_HEC_DIA_PotentialDiagnosis.Query.Search(Connection, Transaction, new ORM_HEC_DIA_PotentialDiagnosis.Query()
                    {
                        IsDeleted    = false,
                        Tenant_RefID = securityTicket.TenantID,
                        HEC_DIA_PotentialDiagnosisID = ctr2dia.PotentialDiagnosis_RefID
                    }).SingleOrDefault();
                    potendialDiagnosisList.Add(potentialDiagnosis);
                }

                foreach (var diagnosispot in potendialDiagnosisList)
                {
                    foreach (var diagnosisData in GPOSData.DiagnosisForGPOS)
                    {
                        var CatalogCodes = ORM_HEC_DIA_PotentialDiagnosis_CatalogCode.Query.Search(Connection, Transaction, new ORM_HEC_DIA_PotentialDiagnosis_CatalogCode.Query()
                        {
                            IsDeleted                = false,
                            Tenant_RefID             = securityTicket.TenantID,
                            PotentialDiagnosis_RefID = diagnosispot.HEC_DIA_PotentialDiagnosisID
                        }).SingleOrDefault();
                        if (CatalogCodes.Code != null)
                        {
                            if (CatalogCodes.Code == diagnosisData.DiagnoseCode || diagnosisData.DiagnoseCode == "all")
                            {
                                var BillCode2PotentialDiagnosis = new ORM_HEC_BIL_PotentialCode_2_PotentialDiagnosis();
                                BillCode2PotentialDiagnosis.HEC_BIL_PotentialCode_RefID      = potentialBillCode.HEC_BIL_PotentialCodeID;
                                BillCode2PotentialDiagnosis.HEC_DIA_PotentialDiagnosis_RefID = diagnosispot.HEC_DIA_PotentialDiagnosisID;
                                BillCode2PotentialDiagnosis.IsDeleted              = false;
                                BillCode2PotentialDiagnosis.Tenant_RefID           = securityTicket.TenantID;
                                BillCode2PotentialDiagnosis.Creation_Timestamp     = DateTime.Now;
                                BillCode2PotentialDiagnosis.Modification_Timestamp = DateTime.Now;
                                BillCode2PotentialDiagnosis.Save(Connection, Transaction);
                            }
                        }
                    }
                }

                var hecContract2Products = ORM_HEC_CTR_InsuranceToBrokerContracts_CoveredHealthcareProduct.Query.Search(Connection, Transaction, new ORM_HEC_CTR_InsuranceToBrokerContracts_CoveredHealthcareProduct.Query()
                {
                    IsDeleted    = false,
                    Tenant_RefID = securityTicket.TenantID,
                    InsuranceToBrokerContract_RefID = hecContract.HEC_CRT_InsuranceToBrokerContractID
                }).ToList();

                List <ORM_HEC_Product> HecProductList = new List <ORM_HEC_Product>();
                foreach (var cont2product in hecContract2Products)
                {
                    var searchProducts = ORM_HEC_Product.Query.Search(Connection, Transaction, new ORM_HEC_Product.Query()
                    {
                        IsDeleted     = false,
                        Tenant_RefID  = securityTicket.TenantID,
                        HEC_ProductID = cont2product.HealthcareProduct_RefID
                    }).SingleOrDefault();
                    if (searchProducts != null)
                    {
                        HecProductList.Add(searchProducts);
                    }
                }

                List <ORM_CMN_PRO_Product> drugList = new List <ORM_CMN_PRO_Product>();
                foreach (var hecProd in HecProductList)
                {
                    var searchProd = ORM_CMN_PRO_Product.Query.Search(Connection, Transaction, new ORM_CMN_PRO_Product.Query()
                    {
                        IsDeleted         = false,
                        Tenant_RefID      = securityTicket.TenantID,
                        CMN_PRO_ProductID = hecProd.Ext_PRO_Product_RefID
                    }).SingleOrDefault();
                    if (searchProd != null)
                    {
                        drugList.Add(searchProd);
                    }
                }



                if (GPOSData.DrugsForGPOS != null)
                {
                    foreach (var drugsData in GPOSData.DrugsForGPOS)
                    {
                        foreach (var drug in HecProductList)
                        {
                            Dict ProductNameDict = new Dict(ORM_CMN_PRO_Product.TableName);
                            for (int i = 0; i < DBLanguages.Count; i++)
                            {
                                ProductNameDict.AddEntry(DBLanguages[i].CMN_LanguageID, drugsData.Drug_name);
                            }

                            var searchProd = ORM_CMN_PRO_Product.Query.Search(Connection, Transaction, new ORM_CMN_PRO_Product.Query()
                            {
                                IsDeleted         = false,
                                Tenant_RefID      = securityTicket.TenantID,
                                CMN_PRO_ProductID = drug.Ext_PRO_Product_RefID
                            }).SingleOrDefault();

                            if (searchProd.Product_Name.Contents[0].Content == ProductNameDict.Contents[0].Content || drugsData.Drug_name == "all")
                            {
                                var billCode2product = new ORM_HEC_BIL_PotentialCode_2_HealthcareProduct();
                                billCode2product.IsDeleted                   = false;
                                billCode2product.Tenant_RefID                = securityTicket.TenantID;
                                billCode2product.Creation_Timestamp          = DateTime.Now;
                                billCode2product.AssignmentID                = Guid.NewGuid();
                                billCode2product.HEC_BIL_PotentialCode_RefID = potentialBillCode.HEC_BIL_PotentialCodeID;
                                billCode2product.HEC_Product_RefID           = drug.HEC_ProductID;
                                billCode2product.Save(Connection, Transaction);
                            }
                        }
                    }
                }

                else
                {
                    Console.WriteLine(GPOSData);
                }
            }
        }
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_MD_SCGPOSD_1306 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Guid();
            //Put your code here

            var insuranceToBrokerContract = ORM_HEC_CRT_InsuranceToBrokerContract.Query.Search(Connection, Transaction, new ORM_HEC_CRT_InsuranceToBrokerContract.Query()
            {
                Ext_CMN_CTR_Contract_RefID = Parameter.ContractID,
                Tenant_RefID = securityTicket.TenantID,
                IsDeleted    = false
            }).SingleOrDefault();

            if (insuranceToBrokerContract == null)
            {
                insuranceToBrokerContract = new ORM_HEC_CRT_InsuranceToBrokerContract();
                insuranceToBrokerContract.Creation_Timestamp                  = DateTime.Now;
                insuranceToBrokerContract.Ext_CMN_CTR_Contract_RefID          = Parameter.ContractID;
                insuranceToBrokerContract.HEC_CRT_InsuranceToBrokerContractID = Guid.NewGuid();
                insuranceToBrokerContract.Modification_Timestamp              = DateTime.Now;
                insuranceToBrokerContract.Tenant_RefID = securityTicket.TenantID;

                insuranceToBrokerContract.Save(Connection, Transaction);
            }

            var allLanguages = ORM_CMN_Language.Query.Search(Connection, Transaction, new ORM_CMN_Language.Query()
            {
                Tenant_RefID = securityTicket.TenantID, IsDeleted = false
            }).ToArray();

            var currency = ORM_CMN_Currency.Query.Search(Connection, Transaction, new ORM_CMN_Currency.Query()
            {
                ISO4127 = "EUR", IsDeleted = false, Symbol = "EUR", Tenant_RefID = securityTicket.TenantID
            }).Single();

            #region DELETE DISCONNECTED GPOS-ES
            var currentGposesConnectedToContract = ORM_HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCode.Query.Search(Connection, Transaction, new ORM_HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCode.Query()
            {
                InsuranceToBrokerContract_RefID = insuranceToBrokerContract.HEC_CRT_InsuranceToBrokerContractID,
                IsDeleted    = false,
                Tenant_RefID = securityTicket.TenantID
            });
            var gposIDs = Parameter.GposData.Select(gpos => gpos.GposID).ToArray();

            foreach (var currGpos in currentGposesConnectedToContract)
            {
                var exists = gposIDs.Any(id => currGpos.PotentialBillCode_RefID == id);
                if (!gposIDs.Any(id => id == currGpos.PotentialBillCode_RefID))
                {
                    cls_Delete_GPOS_Data.Invoke(Connection, Transaction, new P_MD_DGPOSD_1033()
                    {
                        GposID = currGpos.PotentialBillCode_RefID
                    }, securityTicket);
                }
            }
            #endregion DELETE DISCONNECTED GPOS-ES

            foreach (var gpos in Parameter.GposData)
            {
                #region NEW GPOS DATA
                if (gpos.GposID == Guid.Empty)
                {
                    #region GPOS CATALOG
                    var catalog = ORM_HEC_BIL_PotentialCode_Catalog.Query.Search(Connection, Transaction, new ORM_HEC_BIL_PotentialCode_Catalog.Query()
                    {
                        GlobalPropertyMatchingID = gpos.GposType,
                        IsDeleted    = false,
                        Tenant_RefID = securityTicket.TenantID
                    }).SingleOrDefault();

                    if (catalog == null)
                    {
                        catalog = new ORM_HEC_BIL_PotentialCode_Catalog();
                        var catalogNameDict   = new Dict(ORM_HEC_BIL_PotentialCode_Catalog.TableName);
                        var catalogNameString = gpos.GposType.Replace("mm.docconnect.gpos.catalog.", "");
                        catalogNameString = catalogNameString.Substring(0, 1).ToUpper() + catalogNameString.Substring(1);
                        foreach (var lang in allLanguages)
                        {
                            catalogNameDict.AddEntry(lang.CMN_LanguageID, catalogNameString);
                        }
                        catalog.GlobalPropertyMatchingID = gpos.GposType;
                        catalog.Modification_Timestamp   = DateTime.Now;
                        catalog.Tenant_RefID             = securityTicket.TenantID;

                        catalog.Save(Connection, Transaction);
                    }
                    #endregion GPOS CATALOG

                    #region PRICE
                    var price = new ORM_CMN_Price();
                    price.CMN_PriceID        = Guid.NewGuid();
                    price.Creation_Timestamp = DateTime.Now;
                    price.Tenant_RefID       = securityTicket.TenantID;

                    price.Save(Connection, Transaction);

                    var priceValue = new ORM_CMN_Price_Value();
                    priceValue.CMN_Price_ValueID         = Guid.NewGuid();
                    priceValue.Creation_Timestamp        = DateTime.Now;
                    priceValue.Price_RefID               = price.CMN_PriceID;
                    priceValue.PriceValue_Amount         = gpos.FeeValue;
                    priceValue.PriceValue_Currency_RefID = currency.CMN_CurrencyID;
                    priceValue.Tenant_RefID              = securityTicket.TenantID;

                    priceValue.Save(Connection, Transaction);
                    #endregion PRICE

                    #region POTENTIAL CODE
                    var newPotentialCode = new ORM_HEC_BIL_PotentialCode();

                    var potentialCodeName = new Dict(ORM_HEC_BIL_PotentialCode.TableName);
                    foreach (var lang in allLanguages)
                    {
                        potentialCodeName.AddEntry(lang.CMN_LanguageID, gpos.GposName);
                    }

                    newPotentialCode.CodeName                    = potentialCodeName;
                    newPotentialCode.Creation_Timestamp          = DateTime.Now;
                    newPotentialCode.HEC_BIL_PotentialCodeID     = Guid.NewGuid();
                    newPotentialCode.PotentialCode_Catalog_RefID = catalog.HEC_BIL_PotentialCode_CatalogID;
                    newPotentialCode.Modification_Timestamp      = DateTime.Now;
                    newPotentialCode.Price_RefID                 = price.CMN_PriceID;
                    newPotentialCode.BillingCode                 = gpos.GposNumber;
                    newPotentialCode.Tenant_RefID                = securityTicket.TenantID;

                    newPotentialCode.Save(Connection, Transaction);
                    #endregion POTENTIAL CODE

                    #region CONTRACT CONNECTION

                    var coveredPotentialCode = new ORM_HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCode();
                    coveredPotentialCode.Creation_Timestamp = DateTime.Now;
                    coveredPotentialCode.HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCodeID = Guid.NewGuid();
                    coveredPotentialCode.InsuranceToBrokerContract_RefID = insuranceToBrokerContract.HEC_CRT_InsuranceToBrokerContractID;
                    coveredPotentialCode.Modification_Timestamp          = DateTime.Now;
                    coveredPotentialCode.PotentialBillCode_RefID         = newPotentialCode.HEC_BIL_PotentialCodeID;
                    coveredPotentialCode.Tenant_RefID = securityTicket.TenantID;

                    coveredPotentialCode.Save(Connection, Transaction);

                    #endregion CONTRACT CONNECTION

                    #region POTENTIAL CODE PROPERTIES

                    #region FROM INJECTION
                    var fromInjectionProperty = new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalProperty();
                    fromInjectionProperty.Creation_Timestamp = DateTime.Now;
                    fromInjectionProperty.HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalPropertyID = Guid.NewGuid();
                    fromInjectionProperty.IsValue_Number         = true;
                    fromInjectionProperty.Modification_Timestamp = DateTime.Now;
                    fromInjectionProperty.PropertyName           = "From injection no.";
                    fromInjectionProperty.Tenant_RefID           = securityTicket.TenantID;

                    fromInjectionProperty.Save(Connection, Transaction);

                    var fromInjectionPropertyToCode = new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_2_UniversalProperty();
                    fromInjectionPropertyToCode.Tenant_RefID = securityTicket.TenantID;
                    fromInjectionPropertyToCode.AssignmentID = Guid.NewGuid();
                    fromInjectionPropertyToCode.CoveredPotentialBillCode_RefID = coveredPotentialCode.HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCodeID;
                    fromInjectionPropertyToCode.CoveredPotentialBillCode_UniversalProperty_RefID = fromInjectionProperty.HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalPropertyID;
                    fromInjectionPropertyToCode.Creation_Timestamp     = DateTime.Now;
                    fromInjectionPropertyToCode.Modification_Timestamp = DateTime.Now;
                    fromInjectionPropertyToCode.Value_Number           = gpos.FromInjection == 0 || gpos.FromInjection == null ? int.MaxValue : gpos.FromInjection;

                    fromInjectionPropertyToCode.Save(Connection, Transaction);
                    #endregion FROM INJECTION

                    #region SERVICE FEE
                    var serviceFeeProperty = new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalProperty();
                    serviceFeeProperty.Creation_Timestamp = DateTime.Now;
                    serviceFeeProperty.HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalPropertyID = Guid.NewGuid();
                    serviceFeeProperty.IsValue_String         = true;
                    serviceFeeProperty.Modification_Timestamp = DateTime.Now;
                    serviceFeeProperty.PropertyName           = "Service Fee in EUR";
                    serviceFeeProperty.Tenant_RefID           = securityTicket.TenantID;

                    serviceFeeProperty.Save(Connection, Transaction);

                    var serviceFeePropertyToCode = new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_2_UniversalProperty();
                    serviceFeePropertyToCode.Tenant_RefID = securityTicket.TenantID;
                    serviceFeePropertyToCode.AssignmentID = Guid.NewGuid();
                    serviceFeePropertyToCode.CoveredPotentialBillCode_RefID = coveredPotentialCode.HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCodeID;
                    serviceFeePropertyToCode.CoveredPotentialBillCode_UniversalProperty_RefID = serviceFeeProperty.HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalPropertyID;
                    serviceFeePropertyToCode.Creation_Timestamp     = DateTime.Now;
                    serviceFeePropertyToCode.Modification_Timestamp = DateTime.Now;
                    serviceFeePropertyToCode.Value_String           = string.IsNullOrEmpty(gpos.ManagementFeeValue) ? "-" : gpos.ManagementFeeValue;

                    serviceFeePropertyToCode.Save(Connection, Transaction);
                    #endregion SERVICE FEE

                    #region WAIVE WITH ORDER
                    var waiveWithOrderProperty = new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalProperty();
                    waiveWithOrderProperty.Creation_Timestamp = DateTime.Now;
                    waiveWithOrderProperty.HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalPropertyID = Guid.NewGuid();
                    waiveWithOrderProperty.IsValue_Boolean        = true;
                    waiveWithOrderProperty.Modification_Timestamp = DateTime.Now;
                    waiveWithOrderProperty.PropertyName           = "Waive with order";
                    waiveWithOrderProperty.Tenant_RefID           = securityTicket.TenantID;

                    waiveWithOrderProperty.Save(Connection, Transaction);

                    var waiveWithOrderPropertyToCode = new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_2_UniversalProperty();
                    waiveWithOrderPropertyToCode.Tenant_RefID = securityTicket.TenantID;
                    waiveWithOrderPropertyToCode.AssignmentID = Guid.NewGuid();
                    waiveWithOrderPropertyToCode.CoveredPotentialBillCode_RefID = coveredPotentialCode.HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCodeID;
                    waiveWithOrderPropertyToCode.CoveredPotentialBillCode_UniversalProperty_RefID = waiveWithOrderProperty.HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalPropertyID;
                    waiveWithOrderPropertyToCode.Creation_Timestamp     = DateTime.Now;
                    waiveWithOrderPropertyToCode.Modification_Timestamp = DateTime.Now;
                    waiveWithOrderPropertyToCode.Value_Boolean          = gpos.WaiveServiceFeeWithOrder;

                    waiveWithOrderPropertyToCode.Save(Connection, Transaction);
                    #endregion WAIVE WITH ORDER

                    #endregion POTENTIAL CODE PROPERTIES

                    #region CONNECTED DRUGS
                    if (gpos.DrugIDs.Length != 0)
                    {
                        foreach (var drugID in gpos.DrugIDs)
                        {
                            var potentialCodeToDrug = new ORM_HEC_BIL_PotentialCode_2_HealthcareProduct();
                            potentialCodeToDrug.AssignmentID                = Guid.NewGuid();
                            potentialCodeToDrug.Creation_Timestamp          = DateTime.Now;
                            potentialCodeToDrug.HEC_BIL_PotentialCode_RefID = newPotentialCode.HEC_BIL_PotentialCodeID;
                            potentialCodeToDrug.HEC_Product_RefID           = drugID;
                            potentialCodeToDrug.Modification_Timestamp      = DateTime.Now;
                            potentialCodeToDrug.Tenant_RefID                = securityTicket.TenantID;

                            potentialCodeToDrug.Save(Connection, Transaction);
                        }
                    }
                    #endregion CONNECTED DRUGS

                    #region CONNECTED DIAGNOSES
                    if (gpos.DiagnoseIDs.Length != 0)
                    {
                        foreach (var diagnoseID in gpos.DiagnoseIDs)
                        {
                            var potentialCodeToDiagnose = new ORM_HEC_BIL_PotentialCode_2_PotentialDiagnosis();
                            potentialCodeToDiagnose.AssignmentID                     = Guid.NewGuid();
                            potentialCodeToDiagnose.Creation_Timestamp               = DateTime.Now;
                            potentialCodeToDiagnose.HEC_BIL_PotentialCode_RefID      = newPotentialCode.HEC_BIL_PotentialCodeID;
                            potentialCodeToDiagnose.HEC_DIA_PotentialDiagnosis_RefID = diagnoseID;
                            potentialCodeToDiagnose.Modification_Timestamp           = DateTime.Now;
                            potentialCodeToDiagnose.Tenant_RefID                     = securityTicket.TenantID;

                            potentialCodeToDiagnose.Save(Connection, Transaction);
                        }
                    }
                    #endregion CONNECTED DIAGNOSES
                }
                #endregion NEW GPOS DATA

                #region EDIT
                else
                {
                    var currentGpos = ORM_HEC_BIL_PotentialCode.Query.Search(Connection, Transaction, new ORM_HEC_BIL_PotentialCode.Query()
                    {
                        HEC_BIL_PotentialCodeID = gpos.GposID,
                        IsDeleted    = false,
                        Tenant_RefID = securityTicket.TenantID
                    }).SingleOrDefault();
                    if (currentGpos != null)
                    {
                        #region BASE DATA
                        var potentialCodeName = new Dict(ORM_HEC_BIL_PotentialCode.TableName);
                        foreach (var lang in allLanguages)
                        {
                            potentialCodeName.AddEntry(lang.CMN_LanguageID, gpos.GposName);
                        }
                        currentGpos.CodeName    = potentialCodeName;
                        currentGpos.BillingCode = gpos.GposNumber;

                        var catalog = ORM_HEC_BIL_PotentialCode_Catalog.Query.Search(Connection, Transaction, new ORM_HEC_BIL_PotentialCode_Catalog.Query()
                        {
                            GlobalPropertyMatchingID = gpos.GposType,
                            IsDeleted    = false,
                            Tenant_RefID = securityTicket.TenantID
                        }).SingleOrDefault();

                        if (catalog == null)
                        {
                            catalog = new ORM_HEC_BIL_PotentialCode_Catalog();
                            var catalogNameDict   = new Dict(ORM_HEC_BIL_PotentialCode_Catalog.TableName);
                            var catalogNameString = gpos.GposType.Replace("mm.docconnect.gpos.catalog.", "");
                            catalogNameString = catalogNameString.Substring(0, 1).ToUpper() + catalogNameString.Substring(1);

                            foreach (var lang in allLanguages)
                            {
                                catalogNameDict.AddEntry(lang.CMN_LanguageID, catalogNameString);
                            }

                            catalog.GlobalPropertyMatchingID = gpos.GposType;
                            catalog.Modification_Timestamp   = DateTime.Now;
                            catalog.Tenant_RefID             = securityTicket.TenantID;

                            catalog.Save(Connection, Transaction);
                        }

                        currentGpos.PotentialCode_Catalog_RefID = catalog.HEC_BIL_PotentialCode_CatalogID;

                        currentGpos.Save(Connection, Transaction);
                        #endregion

                        #region PRICE
                        var currentPriceValue = ORM_CMN_Price_Value.Query.Search(Connection, Transaction, new ORM_CMN_Price_Value.Query()
                        {
                            Tenant_RefID = securityTicket.TenantID,
                            IsDeleted    = false,
                            Price_RefID  = currentGpos.Price_RefID
                        }).SingleOrDefault();

                        if (currentPriceValue != null)
                        {
                            currentPriceValue.PriceValue_Amount = gpos.FeeValue;
                            currentPriceValue.Save(Connection, Transaction);
                        }
                        #endregion

                        #region CONTRACT CONNECTION
                        var coveredPotentialCode = ORM_HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCode.Query.Search(Connection, Transaction, new ORM_HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCode.Query()
                        {
                            PotentialBillCode_RefID = gpos.GposID,
                            IsDeleted    = false,
                            Tenant_RefID = securityTicket.TenantID
                        }).SingleOrDefault();
                        #endregion

                        #region POTENTIAL CODE PROPERTIES
                        var gposPropertyConnections = ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_2_UniversalProperty.Query.Search(Connection, Transaction, new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_2_UniversalProperty.Query()
                        {
                            CoveredPotentialBillCode_RefID = coveredPotentialCode.HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCodeID,
                            IsDeleted    = false,
                            Tenant_RefID = securityTicket.TenantID
                        });

                        foreach (var conn in gposPropertyConnections)
                        {
                            var gposProperty = ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalProperty.Query.Search(Connection, Transaction, new ORM_HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalProperty.Query()
                            {
                                HEC_CTR_I2BC_CoveredPotentialBillCodes_UniversalPropertyID = conn.CoveredPotentialBillCode_UniversalProperty_RefID,
                                IsDeleted    = false,
                                Tenant_RefID = securityTicket.TenantID
                            }).SingleOrDefault();

                            if (gposProperty != null)
                            {
                                switch (gposProperty.PropertyName)
                                {
                                case "From injection no.":
                                    conn.Value_Number = gpos.FromInjection;
                                    break;

                                case "Waive with order":
                                    conn.Value_Boolean = gpos.WaiveServiceFeeWithOrder;
                                    break;

                                case "Service Fee in EUR":
                                    conn.Value_String = gpos.ManagementFeeValue;
                                    break;
                                }

                                conn.Modification_Timestamp = DateTime.Now;
                                conn.Save(Connection, Transaction);
                            }
                        }

                        #endregion POTENTIAL CODE PROPERTIES

                        #region CONNECTED DRUGS
                        var currentGposDrugConnections = ORM_HEC_BIL_PotentialCode_2_HealthcareProduct.Query.Search(Connection, Transaction, new ORM_HEC_BIL_PotentialCode_2_HealthcareProduct.Query()
                        {
                            HEC_BIL_PotentialCode_RefID = gpos.GposID,
                            IsDeleted    = false,
                            Tenant_RefID = securityTicket.TenantID
                        });

                        foreach (var drugConnection in currentGposDrugConnections)
                        {
                            if (gpos.DrugIDs.Length == 0 || !gpos.DrugIDs.Any(did => did == drugConnection.HEC_Product_RefID))
                            {
                                drugConnection.IsDeleted = true;
                                drugConnection.Modification_Timestamp = DateTime.Now;

                                drugConnection.Save(Connection, Transaction);
                            }
                        }

                        foreach (var drugID in gpos.DrugIDs)
                        {
                            if (!currentGposDrugConnections.Any(cpd => cpd.HEC_Product_RefID == drugID))
                            {
                                var newDrugConnection = new ORM_HEC_BIL_PotentialCode_2_HealthcareProduct();
                                newDrugConnection.AssignmentID                = Guid.NewGuid();
                                newDrugConnection.Creation_Timestamp          = DateTime.Now;
                                newDrugConnection.HEC_BIL_PotentialCode_RefID = gpos.GposID;
                                newDrugConnection.HEC_Product_RefID           = drugID;
                                newDrugConnection.Modification_Timestamp      = DateTime.Now;
                                newDrugConnection.Tenant_RefID                = securityTicket.TenantID;

                                newDrugConnection.Save(Connection, Transaction);
                            }
                        }

                        #endregion CONNECTED DRUGS

                        #region CONNECTED DIAGNOSES
                        var currentGposDiagnoseConnections = ORM_HEC_BIL_PotentialCode_2_PotentialDiagnosis.Query.Search(Connection, Transaction, new ORM_HEC_BIL_PotentialCode_2_PotentialDiagnosis.Query()
                        {
                            HEC_BIL_PotentialCode_RefID = gpos.GposID,
                            IsDeleted    = false,
                            Tenant_RefID = securityTicket.TenantID
                        });

                        foreach (var diagnoseConnection in currentGposDiagnoseConnections)
                        {
                            if (gpos.DiagnoseIDs.Length == 0 || !gpos.DiagnoseIDs.Any(did => did == diagnoseConnection.HEC_DIA_PotentialDiagnosis_RefID))
                            {
                                diagnoseConnection.IsDeleted = true;
                                diagnoseConnection.Modification_Timestamp = DateTime.Now;

                                diagnoseConnection.Save(Connection, Transaction);
                            }
                        }

                        foreach (var diagnoseID in gpos.DiagnoseIDs)
                        {
                            if (!currentGposDiagnoseConnections.Any(cpd => cpd.HEC_DIA_PotentialDiagnosis_RefID == diagnoseID))
                            {
                                var newDiagnoseConnection = new ORM_HEC_BIL_PotentialCode_2_PotentialDiagnosis();
                                newDiagnoseConnection.AssignmentID                     = Guid.NewGuid();
                                newDiagnoseConnection.Creation_Timestamp               = DateTime.Now;
                                newDiagnoseConnection.HEC_BIL_PotentialCode_RefID      = gpos.GposID;
                                newDiagnoseConnection.HEC_DIA_PotentialDiagnosis_RefID = diagnoseID;
                                newDiagnoseConnection.Modification_Timestamp           = DateTime.Now;
                                newDiagnoseConnection.Tenant_RefID                     = securityTicket.TenantID;

                                newDiagnoseConnection.Save(Connection, Transaction);
                            }
                        }
                        #endregion CONNECTED DIAGNOSES
                    }
                }
                #endregion EDIT
            }

            return(returnValue);

            #endregion UserCode
        }