protected static FR_Guids Execute(DbConnection Connection, DbTransaction Transaction, P_CAS_CCGPOS_1000 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var  returnValue = new FR_Guids();
            Guid treatment_performed_action_type_id = Guid.Empty;
            var  resultList = new List <Guid>();

            var treatment_performed_action_type = ORM_HEC_ACT_ActionType.Query.Search(Connection, Transaction, new ORM_HEC_ACT_ActionType.Query()
            {
                Tenant_RefID             = securityTicket.TenantID,
                IsDeleted                = false,
                GlobalPropertyMatchingID = "mm.docconect.doc.app.performed.action.treatment"
            }).SingleOrDefault();

            if (treatment_performed_action_type == null)
            {
                treatment_performed_action_type = new ORM_HEC_ACT_ActionType();
                treatment_performed_action_type.GlobalPropertyMatchingID = "mm.docconect.doc.app.performed.action.treatment";
                treatment_performed_action_type.Creation_Timestamp       = DateTime.Now;
                treatment_performed_action_type.Modification_Timestamp   = DateTime.Now;
                treatment_performed_action_type.Tenant_RefID             = securityTicket.TenantID;

                treatment_performed_action_type.Save(Connection, Transaction);

                treatment_performed_action_type_id = treatment_performed_action_type.HEC_ACT_ActionTypeID;
            }
            else
            {
                treatment_performed_action_type_id = treatment_performed_action_type.HEC_ACT_ActionTypeID;
            }

            var treatment_count = cls_Get_Treatment_Count_for_PatientID_And_DiagnoseID_and_LocalizationCode.Invoke(Connection, Transaction, new P_CAS_GTCfPIDaDIDaLC_1008()
            {
                ActionTypeID     = treatment_performed_action_type_id,
                DiagnoseID       = Parameter.diagnose_id,
                PatientID        = Parameter.patient_id,
                LocalizationCode = Parameter.localization,
                PerformedDate    = Parameter.treatment_date == DateTime.MinValue ? DateTime.Now : Parameter.treatment_date
            }, securityTicket).Result;

            if (treatment_count != null)
            {
                treatment_count.treatment_count++;

                var contracts = cls_Get_InsuranceToBrokerContractID_for_DrugID_and_DiagnoseID.Invoke(Connection, Transaction, new P_CAS_GItBCIDfDIDaDID_1541()
                {
                    DiagnoseID    = Parameter.diagnose_id,
                    DrugID        = Parameter.drug_id,
                    PatientID     = Parameter.patient_id,
                    TreatmentDate = DateTime.Now
                }, securityTicket).Result.OrderBy(ctr => ctr.patient_consent_valid_from);

                var contract    = contracts.LastOrDefault(ctr => ctr.patient_consent_valid_from.Date <= Parameter.treatment_date);
                var contract_id = Guid.Empty;
                if (contract == null)
                {
                    if (contracts.Any())
                    {
                        contract = contracts.Last();
                    }
                    else
                    {
                        var hip = cls_Get_Patient_Insurance_Data_for_PatientIDs.Invoke(Connection, Transaction, new P_PA_GPIDfPIDs_1002()
                        {
                            PatientIDs = new Guid[] { Parameter.patient_id }
                        }, securityTicket).Result.First();
                        contract_id = cls_Get_Contracts_Where_Hip_Participating_for_HipID.Invoke(Connection, Transaction, new P_PA_GCwHipPfHipID_0954()
                        {
                            HipIkNumber = hip.HipIkNumber
                        }, securityTicket).Result.Last().ContractID;
                    }
                }
                else
                {
                    contract_id = contract.contract_id;
                }

                if (!Parameter.should_update)
                {
                    var gpos_details = cls_Get_All_GPOS_Details_for_ContractID.Invoke(Connection, Transaction, new P_CAS_GAGPOSDfCID_1754()
                    {
                        ContractID = contract_id
                    }, securityTicket).Result;

                    if (gpos_details.Length != 0)
                    {
                        var gpos_types = new List <string>();

                        if (Parameter.oct_doctor_id != Guid.Empty)
                        {
                            gpos_types.Add(EGposType.Oct.Value());
                            if (Parameter.treatment_doctor_id == Guid.Empty && Parameter.ac_doctor_id != Guid.Empty)
                            {
                                gpos_types.Add(EGposType.Aftercare.Value());
                            }
                        }
                        else
                        {
                            if (Parameter.treatment_doctor_id == Guid.Empty && Parameter.ac_doctor_id != Guid.Empty)
                            {
                                gpos_types.Add(EGposType.Aftercare.Value());
                            }
                            else if (Parameter.diagnose_id != Guid.Empty)
                            {
                                gpos_types.Add(EGposType.Operation.Value());
                                gpos_types.Add(EGposType.Aftercare.Value());
                            }
                        }

                        foreach (var gpos_type in gpos_types)
                        {
                            foreach (var gpos_detail in gpos_details.Where(t => t.GposType == gpos_type))
                            {
                                var covered_drugs = ORM_HEC_BIL_PotentialCode_2_HealthcareProduct.Query.Search(Connection, Transaction, new ORM_HEC_BIL_PotentialCode_2_HealthcareProduct.Query()
                                {
                                    HEC_BIL_PotentialCode_RefID = gpos_detail.GposID,
                                    Tenant_RefID = securityTicket.TenantID,
                                    IsDeleted    = false
                                }).Select(covered_drug => covered_drug.HEC_Product_RefID);

                                var covered_diagnoses = ORM_HEC_BIL_PotentialCode_2_PotentialDiagnosis.Query.Search(Connection, Transaction, new ORM_HEC_BIL_PotentialCode_2_PotentialDiagnosis.Query()
                                {
                                    HEC_BIL_PotentialCode_RefID = gpos_detail.GposID,
                                    Tenant_RefID = securityTicket.TenantID,
                                    IsDeleted    = false
                                }).Select(covered_diagnose => covered_diagnose.HEC_DIA_PotentialDiagnosis_RefID);

                                var drug_condition     = covered_drugs.Any() ? covered_drugs.Any(t => t == Parameter.drug_id) : true;
                                var diagnose_condition = covered_diagnoses.Any() ? covered_diagnoses.Any(t => t == Parameter.diagnose_id) : true;

                                bool treatment_count_condition = gpos_detail.FromInjection > 1000000;

                                if (covered_drugs.Any() && covered_diagnoses.Any())
                                {
                                    var gpos_details_for_drug_diagnose_combo = cls_Get_GPOS_Details_for_DiagnoseID_and_DrugID.Invoke(Connection, Transaction, new P_CAS_GGPOSDfDIDaDID_1033()
                                    {
                                        ContractID = contract_id,
                                        DiagnoseID = Parameter.diagnose_id,
                                        DrugID     = Parameter.drug_id
                                    }, securityTicket).Result;

                                    treatment_count_condition = treatment_count.treatment_count >= gpos_detail.FromInjection &&
                                                                !gpos_details_for_drug_diagnose_combo.Any(gpos => treatment_count.treatment_count >= gpos.injection_from && gpos.injection_from > gpos_detail.FromInjection && gpos.gpos_type == gpos_type);
                                }

                                var gpos_condition = drug_condition && diagnose_condition && treatment_count_condition;

                                if (gpos_condition)
                                {
                                    ORM_BIL_BillPosition gpos_position = new ORM_BIL_BillPosition();
                                    gpos_position.BIL_BilHeader_RefID        = Guid.Empty;
                                    gpos_position.BIL_BillPositionID         = Guid.NewGuid();
                                    gpos_position.Creation_Timestamp         = DateTime.Now;
                                    gpos_position.Modification_Timestamp     = DateTime.Now;
                                    gpos_position.Tenant_RefID               = securityTicket.TenantID;
                                    gpos_position.PositionValue_IncludingTax = Convert.ToDecimal(gpos_detail.GposPrice);

                                    gpos_position.Save(Connection, Transaction);

                                    resultList.Add(gpos_position.BIL_BillPositionID);

                                    ORM_HEC_BIL_BillPosition hec_gpos_position = new ORM_HEC_BIL_BillPosition();
                                    hec_gpos_position.Creation_Timestamp         = DateTime.Now;
                                    hec_gpos_position.Ext_BIL_BillPosition_RefID = gpos_position.BIL_BillPositionID;
                                    hec_gpos_position.HEC_BIL_BillPositionID     = Guid.NewGuid();
                                    hec_gpos_position.Modification_Timestamp     = DateTime.Now;
                                    hec_gpos_position.Tenant_RefID = securityTicket.TenantID;
                                    hec_gpos_position.PositionFor_Patient_RefID = Parameter.patient_id;

                                    hec_gpos_position.Save(Connection, Transaction);

                                    ORM_HEC_BIL_BillPosition_BillCode hec_gpos_position_code = new ORM_HEC_BIL_BillPosition_BillCode();
                                    hec_gpos_position_code.BillPosition_RefID = hec_gpos_position.HEC_BIL_BillPositionID;
                                    hec_gpos_position_code.Creation_Timestamp = DateTime.Now;
                                    hec_gpos_position_code.HEC_BIL_BillPosition_BillCodeID = Guid.NewGuid();
                                    hec_gpos_position_code.IM_BillingCode         = gpos_detail.GposNumber;
                                    hec_gpos_position_code.Modification_Timestamp = DateTime.Now;
                                    hec_gpos_position_code.PotentialCode_RefID    = gpos_detail.GposID;
                                    hec_gpos_position_code.Tenant_RefID           = securityTicket.TenantID;

                                    hec_gpos_position_code.Save(Connection, Transaction);

                                    ORM_HEC_CAS_Case_BillCode hec_gpos_case_code = new ORM_HEC_CAS_Case_BillCode();
                                    hec_gpos_case_code.Creation_Timestamp = DateTime.Now;
                                    hec_gpos_case_code.HEC_BIL_BillPosition_BillCode_RefID = hec_gpos_position_code.HEC_BIL_BillPosition_BillCodeID;
                                    hec_gpos_case_code.HEC_CAS_Case_BillCodeID             = Guid.NewGuid();
                                    hec_gpos_case_code.HEC_CAS_Case_RefID     = Parameter.case_id;
                                    hec_gpos_case_code.Modification_Timestamp = DateTime.Now;
                                    hec_gpos_case_code.Tenant_RefID           = securityTicket.TenantID;

                                    hec_gpos_case_code.Save(Connection, Transaction);
                                }
                            }
                        }
                    }
                }
                else
                {
                    // TODO: fix update - beva -> no beva and such
                    var bill_position_ids = cls_Get_BillPositionIDs_for_CaseID.Invoke(Connection, Transaction, new P_CAS_GBPIDsfCID_0928()
                    {
                        CaseID = Parameter.case_id
                    }, securityTicket).Result;
                    var gpos_details = cls_Get_GPOS_Details_for_DiagnoseID_and_DrugID.Invoke(Connection, Transaction, new P_CAS_GGPOSDfDIDaDID_1033()
                    {
                        ContractID = contract_id,
                        DiagnoseID = Parameter.diagnose_id,
                        DrugID     = Parameter.drug_id
                    }, securityTicket).Result;

                    if (gpos_details.Length != 0 && bill_position_ids.Length != 0)
                    {
                        bill_position_ids = bill_position_ids.Where(bp => ORM_HEC_BIL_PotentialCode_2_HealthcareProduct.Query.Search(Connection, Transaction, new ORM_HEC_BIL_PotentialCode_2_HealthcareProduct.Query()
                        {
                            HEC_BIL_PotentialCode_RefID = bp.gpos_id,
                            Tenant_RefID = securityTicket.TenantID,
                            IsDeleted    = false
                        }).Count != 0 &&
                                                                    ORM_HEC_BIL_PotentialCode_2_PotentialDiagnosis.Query.Search(Connection, Transaction, new ORM_HEC_BIL_PotentialCode_2_PotentialDiagnosis.Query()
                        {
                            HEC_BIL_PotentialCode_RefID = bp.gpos_id,
                            Tenant_RefID = securityTicket.TenantID,
                            IsDeleted    = false
                        }).Count != 0).ToArray();

                        foreach (var id in bill_position_ids)
                        {
                            foreach (var gpos_detail in gpos_details)
                            {
                                if (id.gpos_type == gpos_detail.gpos_type)
                                {
                                    if (treatment_count.treatment_count >= gpos_detail.injection_from &&
                                        !gpos_details.Any(gpos => treatment_count.treatment_count >= gpos.injection_from && gpos.injection_from > gpos_detail.injection_from && gpos.gpos_type == id.gpos_type))
                                    {
                                        ORM_BIL_BillPosition.Query old_gpos_positionQ = new ORM_BIL_BillPosition.Query();
                                        old_gpos_positionQ.BIL_BillPositionID = id.bill_position_id;
                                        old_gpos_positionQ.Tenant_RefID       = securityTicket.TenantID;
                                        old_gpos_positionQ.IsDeleted          = false;

                                        var old_gpos_position = ORM_BIL_BillPosition.Query.Search(Connection, Transaction, old_gpos_positionQ).SingleOrDefault();
                                        if (old_gpos_position != null)
                                        {
                                            old_gpos_position.Modification_Timestamp     = DateTime.Now;
                                            old_gpos_position.PositionValue_IncludingTax = Convert.ToDecimal(gpos_detail.gpos_price);

                                            old_gpos_position.Save(Connection, Transaction);

                                            ORM_HEC_BIL_BillPosition.Query old_hec_gpos_positionQ = new ORM_HEC_BIL_BillPosition.Query();
                                            old_hec_gpos_positionQ.Ext_BIL_BillPosition_RefID = old_gpos_position.BIL_BillPositionID;
                                            old_hec_gpos_positionQ.Tenant_RefID = securityTicket.TenantID;
                                            old_hec_gpos_positionQ.PositionFor_Patient_RefID = Parameter.patient_id;
                                            old_hec_gpos_positionQ.IsDeleted = false;

                                            var old_hec_gpos_position = ORM_HEC_BIL_BillPosition.Query.Search(Connection, Transaction, old_hec_gpos_positionQ).SingleOrDefault();
                                            if (old_hec_gpos_position != null)
                                            {
                                                ORM_HEC_BIL_BillPosition_BillCode.Query old_hec_gpos_position_codeQ = new ORM_HEC_BIL_BillPosition_BillCode.Query();
                                                old_hec_gpos_position_codeQ.Tenant_RefID       = securityTicket.TenantID;
                                                old_hec_gpos_position_codeQ.BillPosition_RefID = old_hec_gpos_position.HEC_BIL_BillPositionID;
                                                old_hec_gpos_position_codeQ.IsDeleted          = false;

                                                var old_hec_gpos_position_code = ORM_HEC_BIL_BillPosition_BillCode.Query.Search(Connection, Transaction, old_hec_gpos_position_codeQ).SingleOrDefault();
                                                if (old_hec_gpos_position_code != null)
                                                {
                                                    old_hec_gpos_position_code.IM_BillingCode         = gpos_detail.gpos_code;
                                                    old_hec_gpos_position_code.Modification_Timestamp = DateTime.Now;
                                                    old_hec_gpos_position_code.PotentialCode_RefID    = gpos_detail.gpos_id;

                                                    old_hec_gpos_position_code.Save(Connection, Transaction);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            returnValue.Result = resultList.ToArray();
            return(returnValue);

            #endregion UserCode
        }
示例#2
0
        protected static FR_Guids Execute(DbConnection Connection, DbTransaction Transaction, P_CAS_CCGPOS_1000 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var  returnValue = new FR_Guids();
            Guid treatment_performed_action_type_id = Guid.Empty;
            var  resultList = new List <Guid>();

            var treatment_performed_action_type = ORM_HEC_ACT_ActionType.Query.Search(Connection, Transaction, new ORM_HEC_ACT_ActionType.Query()
            {
                Tenant_RefID             = securityTicket.TenantID,
                IsDeleted                = false,
                GlobalPropertyMatchingID = "mm.docconect.doc.app.performed.action.treatment"
            }).SingleOrDefault();

            if (treatment_performed_action_type == null)
            {
                treatment_performed_action_type = new ORM_HEC_ACT_ActionType();
                treatment_performed_action_type.GlobalPropertyMatchingID = "mm.docconect.doc.app.performed.action.treatment";
                treatment_performed_action_type.Creation_Timestamp       = DateTime.Now;
                treatment_performed_action_type.Modification_Timestamp   = DateTime.Now;
                treatment_performed_action_type.Tenant_RefID             = securityTicket.TenantID;

                treatment_performed_action_type.Save(Connection, Transaction);

                treatment_performed_action_type_id = treatment_performed_action_type.HEC_ACT_ActionTypeID;
            }
            else
            {
                treatment_performed_action_type_id = treatment_performed_action_type.HEC_ACT_ActionTypeID;
            }

            var treatment_count = cls_Get_Treatment_Count_for_PatientID_And_DiagnoseID_and_LocalizationCode.Invoke(Connection, Transaction, new P_CAS_GTCfPIDaDIDaLC_1008()
            {
                ActionTypeID     = treatment_performed_action_type_id,
                DiagnoseID       = Parameter.diagnose_id,
                PatientID        = Parameter.patient_id,
                LocalizationCode = Parameter.localization,
                PerformedDate    = Parameter.treatment_date == DateTime.MinValue ? DateTime.Now : Parameter.treatment_date
            }, securityTicket).Result;

            if (treatment_count != null)
            {
                treatment_count.treatment_count++;

                var contracts = cls_Get_InsuranceToBrokerContractID_for_DrugID_and_DiagnoseID.Invoke(Connection, Transaction, new P_CAS_GItBCIDfDIDaDID_1541()
                {
                    DiagnoseID    = Parameter.diagnose_id,
                    DrugID        = Parameter.drug_id,
                    PatientID     = Parameter.patient_id,
                    TreatmentDate = DateTime.Now
                }, securityTicket).Result.OrderBy(ctr => ctr.patient_consent_valid_from);

                var contract_id = contracts.LastOrDefault(ctr => ctr.patient_consent_valid_from.Date <= Parameter.treatment_date);

                if (contract_id == null)
                {
                    contract_id = contracts.LastOrDefault();
                }

                if (contract_id == null)
                {
                    throw new Exception("Contract not found during GPOS calculation");
                }

                var gpos_details = cls_Get_All_GPOS_Details_for_ContractID.Invoke(Connection, Transaction, new P_CAS_GAGPOSDfCID_1754()
                {
                    ContractID = contract_id.contract_id
                }, securityTicket).Result;

                if (gpos_details.Length != 0)
                {
                    List <string> gpos_types = new List <string>();

                    if (Parameter.oct_doctor_id != Guid.Empty)
                    {
                        gpos_types.Add("mm.docconnect.gpos.catalog.voruntersuchung");
                    }
                    else
                    {
                        if (Parameter.treatment_doctor_id == Guid.Empty && Parameter.ac_doctor_id != Guid.Empty)
                        {
                            gpos_types.Add("mm.docconnect.gpos.catalog.nachsorge");
                        }
                        else if (Parameter.diagnose_id != Guid.Empty)
                        {
                            gpos_types.Add("mm.docconnect.gpos.catalog.operation");
                            gpos_types.Add("mm.docconnect.gpos.catalog.nachsorge");
                        }
                    }

                    foreach (var gpos_type in gpos_types)
                    {
                        foreach (var gpos_detail in gpos_details.Where(t => t.GposType == gpos_type))
                        {
                            var covered_drugs = ORM_HEC_BIL_PotentialCode_2_HealthcareProduct.Query.Search(Connection, Transaction, new ORM_HEC_BIL_PotentialCode_2_HealthcareProduct.Query()
                            {
                                HEC_BIL_PotentialCode_RefID = gpos_detail.GposID,
                                Tenant_RefID = securityTicket.TenantID,
                                IsDeleted    = false
                            }).Select(covered_drug => covered_drug.HEC_Product_RefID);

                            var covered_diagnoses = ORM_HEC_BIL_PotentialCode_2_PotentialDiagnosis.Query.Search(Connection, Transaction, new ORM_HEC_BIL_PotentialCode_2_PotentialDiagnosis.Query()
                            {
                                HEC_BIL_PotentialCode_RefID = gpos_detail.GposID,
                                Tenant_RefID = securityTicket.TenantID,
                                IsDeleted    = false
                            }).Select(covered_diagnose => covered_diagnose.HEC_DIA_PotentialDiagnosis_RefID);

                            var drug_condition     = covered_drugs.Any() ? covered_drugs.Any(t => t == Parameter.drug_id) : true;
                            var diagnose_condition = covered_diagnoses.Any() ? covered_diagnoses.Any(t => t == Parameter.diagnose_id) : true;

                            bool treatment_count_condition = gpos_detail.FromInjection > 1000000;

                            if (covered_drugs.Any() && covered_diagnoses.Any())
                            {
                                var gpos_details_for_drug_diagnose_combo = cls_Get_GPOS_Details_for_DiagnoseID_and_DrugID.Invoke(Connection, Transaction, new P_CAS_GGPOSDfDIDaDID_1033()
                                {
                                    ContractID = contract_id.contract_id,
                                    DiagnoseID = Parameter.diagnose_id,
                                    DrugID     = Parameter.drug_id
                                }, securityTicket).Result;

                                treatment_count_condition = treatment_count.treatment_count >= gpos_detail.FromInjection &&
                                                            !gpos_details_for_drug_diagnose_combo.Any(gpos => treatment_count.treatment_count >= gpos.injection_from && gpos.injection_from > gpos_detail.FromInjection && gpos.gpos_type == gpos_type);
                            }

                            var gpos_condition = drug_condition && diagnose_condition && treatment_count_condition;

                            if (gpos_condition)
                            {
                                ORM_BIL_BillPosition gpos_position = new ORM_BIL_BillPosition();
                                gpos_position.BIL_BilHeader_RefID        = Guid.Empty;
                                gpos_position.Creation_Timestamp         = Parameter.creation_timestamp != DateTime.MinValue ? Parameter.creation_timestamp : DateTime.Now;
                                gpos_position.Modification_Timestamp     = DateTime.Now;
                                gpos_position.Tenant_RefID               = securityTicket.TenantID;
                                gpos_position.PositionValue_IncludingTax = Convert.ToDecimal(gpos_detail.GposPrice);

                                gpos_position.Save(Connection, Transaction);

                                resultList.Add(gpos_position.BIL_BillPositionID);

                                ORM_HEC_BIL_BillPosition hec_gpos_position = new ORM_HEC_BIL_BillPosition();
                                hec_gpos_position.Creation_Timestamp         = Parameter.creation_timestamp != DateTime.MinValue ? Parameter.creation_timestamp : DateTime.Now;
                                hec_gpos_position.Ext_BIL_BillPosition_RefID = gpos_position.BIL_BillPositionID;
                                hec_gpos_position.HEC_BIL_BillPositionID     = Guid.NewGuid();
                                hec_gpos_position.Modification_Timestamp     = DateTime.Now;
                                hec_gpos_position.Tenant_RefID = securityTicket.TenantID;
                                hec_gpos_position.PositionFor_Patient_RefID = Parameter.patient_id;

                                hec_gpos_position.Save(Connection, Transaction);

                                ORM_HEC_BIL_BillPosition_BillCode hec_gpos_position_code = new ORM_HEC_BIL_BillPosition_BillCode();
                                hec_gpos_position_code.BillPosition_RefID = hec_gpos_position.HEC_BIL_BillPositionID;
                                hec_gpos_position_code.Creation_Timestamp = Parameter.creation_timestamp != DateTime.MinValue ? Parameter.creation_timestamp : DateTime.Now;
                                hec_gpos_position_code.HEC_BIL_BillPosition_BillCodeID = Guid.NewGuid();
                                hec_gpos_position_code.IM_BillingCode         = gpos_detail.GposNumber;
                                hec_gpos_position_code.Modification_Timestamp = DateTime.Now;
                                hec_gpos_position_code.PotentialCode_RefID    = gpos_detail.GposID;
                                hec_gpos_position_code.Tenant_RefID           = securityTicket.TenantID;

                                hec_gpos_position_code.Save(Connection, Transaction);

                                ORM_HEC_CAS_Case_BillCode hec_gpos_case_code = new ORM_HEC_CAS_Case_BillCode();
                                hec_gpos_case_code.Creation_Timestamp = Parameter.creation_timestamp != DateTime.MinValue ? Parameter.creation_timestamp : DateTime.Now;
                                hec_gpos_case_code.HEC_BIL_BillPosition_BillCode_RefID = hec_gpos_position_code.HEC_BIL_BillPosition_BillCodeID;
                                hec_gpos_case_code.HEC_CAS_Case_BillCodeID             = Guid.NewGuid();
                                hec_gpos_case_code.HEC_CAS_Case_RefID     = Parameter.case_id;
                                hec_gpos_case_code.Modification_Timestamp = DateTime.Now;
                                hec_gpos_case_code.Tenant_RefID           = securityTicket.TenantID;

                                hec_gpos_case_code.Save(Connection, Transaction);
                            }
                        }
                    }
                }
            }

            returnValue.Result = resultList.ToArray();
            return(returnValue);

            #endregion UserCode
        }
示例#3
0
        protected static FR_CAS_SPE_1805 Execute(DbConnection Connection, DbTransaction Transaction, P_CAS_SPE_1805 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_CAS_SPE_1805();
            returnValue.Result = new CAS_SPE_1805();
            //Put your code here
            var case_id = Parameter.case_id;

            #region DATA
            var patient_details = cls_Get_Patient_Details_for_PatientID.Invoke(Connection, Transaction, new P_P_PA_GPDfPID_1124()
            {
                PatientID = Parameter.patient_id
            }, securityTicket).Result;
            if (patient_details == null)
            {
                throw new Exception("Patient details not found for ID: " + Parameter.patient_id);
            }

            var doctor = ORM_HEC_Doctor.Query.Search(Connection, Transaction, new ORM_HEC_Doctor.Query()
            {
                Tenant_RefID = securityTicket.TenantID, HEC_DoctorID = Parameter.doctor_id, IsDeleted = false
            }).SingleOrDefault();
            if (doctor == null)
            {
                throw new Exception("Doctor not found for ID: " + Parameter.doctor_id);
            }


            var doctor_details = cls_Get_Doctor_Details_for_DoctorID.Invoke(Connection, Transaction, new P_DO_GDDfDID_0823()
            {
                DoctorID = Parameter.doctor_id
            }, securityTicket).Result.FirstOrDefault();

            if (doctor_details == null)
            {
                throw new Exception("Doctor details not found for ID: " + Parameter.doctor_id);
            }

            var practice_details = cls_Get_Practice_Details_for_PracticeID.Invoke(Connection, Transaction, new P_DO_GPDfPID_1432()
            {
                PracticeID = doctor_details.practice_id
            }, securityTicket).Result.FirstOrDefault();
            if (practice_details == null)
            {
                throw new Exception("Practice details not found for ID: " + doctor_details.practice_id);
            }

            var shouldDownloadReportProperty = cls_Get_Practice_PropertyValue_for_PropertyName_and_PracticeID.Invoke(Connection, Transaction, new P_DO_GPPVfPNaPID_0916()
            {
                PracticeID = doctor_details.practice_id, PropertyName = "Download Report Upon Submission"
            }, securityTicket).Result;
            var shouldDownloadReport = shouldDownloadReportProperty == null ? false : shouldDownloadReportProperty.BooleanValue;

            ORM_USR_Account.Query trigger_accQ = new ORM_USR_Account.Query();
            trigger_accQ.Tenant_RefID  = securityTicket.TenantID;
            trigger_accQ.USR_AccountID = securityTicket.AccountID;
            trigger_accQ.IsDeleted     = false;

            ORM_USR_Account trigger_acc = ORM_USR_Account.Query.Search(Connection, Transaction, trigger_accQ).Single();

            ORM_CMN_Language.Query all_languagesQ = new ORM_CMN_Language.Query();
            all_languagesQ.Tenant_RefID = securityTicket.TenantID;
            all_languagesQ.IsDeleted    = false;

            var all_languagesL           = ORM_CMN_Language.Query.Search(Connection, Transaction, all_languagesQ).ToArray();
            var intraocular_procedure_id = Guid.Empty;
            #endregion

            if (!Parameter.isResubmit)
            {
                #region NEW CASE
                ORM_HEC_CAS_Case new_case = new ORM_HEC_CAS_Case();
                new_case.HEC_CAS_CaseID     = Guid.NewGuid();
                new_case.Creation_Timestamp = DateTime.Now;
                new_case.CreatedBy_BusinessParticipant_RefID = trigger_acc.BusinessParticipant_RefID;
                new_case.Patient_RefID          = Parameter.patient_id;
                new_case.Patient_FirstName      = patient_details.patient_first_name;
                new_case.Patient_LastName       = patient_details.patient_last_name;
                new_case.Patient_Gender         = patient_details.gender;
                new_case.Patient_BirthDate      = patient_details.birthday;
                new_case.CaseNumber             = cls_Get_Next_Case_Number.Invoke(Connection, Transaction, securityTicket).Result.case_number;
                new_case.Modification_Timestamp = DateTime.Now;

                DateTime today = DateTime.Today;
                int      age   = today.Year - patient_details.birthday.Year;
                if (patient_details.birthday > today.AddYears(-age))
                {
                    age--;
                }

                new_case.Patient_Age  = age;
                new_case.Tenant_RefID = securityTicket.TenantID;

                new_case.Save(Connection, Transaction);
                case_id = new_case.HEC_CAS_CaseID;
                #endregion NEW CASE

                #region INITIAL PERFORMED ACTION
                var initial_performed_action_id = cls_Create_Initial_Performed_Action.Invoke(Connection, Transaction, new P_CAS_CIPA_1140()
                {
                    all_languagesL    = all_languagesL,
                    case_id           = new_case.HEC_CAS_CaseID,
                    created_by_bpt    = trigger_acc.BusinessParticipant_RefID,
                    patient_id        = Parameter.patient_id,
                    practice_id       = doctor_details.practice_id,
                    action_type_gpmid = "mm.docconect.doc.app.performed.action.preexamination"
                }, securityTicket).Result;
                #endregion INITIAL PERFORMED ACTION

                #region LOCALIZATION
                ORM_HEC_DIA_Diagnosis_Localization diagnosis_localization = new ORM_HEC_DIA_Diagnosis_Localization();
                diagnosis_localization.Modification_Timestamp = DateTime.Now;
                diagnosis_localization.Tenant_RefID           = securityTicket.TenantID;
                diagnosis_localization.LocalizationCode       = Parameter.localization;

                diagnosis_localization.Save(Connection, Transaction);

                ORM_HEC_ACT_PerformedAction_DiagnosisUpdate initial_performed_action_diagnose = new ORM_HEC_ACT_PerformedAction_DiagnosisUpdate();
                initial_performed_action_diagnose.HEC_ACT_PerformedAction_RefID = initial_performed_action_id;
                initial_performed_action_diagnose.Modification_Timestamp        = DateTime.Now;
                initial_performed_action_diagnose.Tenant_RefID = securityTicket.TenantID;

                initial_performed_action_diagnose.Save(Connection, Transaction);

                ORM_HEC_ACT_PerformedAction_DiagnosisUpdate_Localization initial_performed_action_diagnose_localization = new ORM_HEC_ACT_PerformedAction_DiagnosisUpdate_Localization();
                initial_performed_action_diagnose_localization.HEX_EXC_Action_DiagnosisUpdate_RefID = initial_performed_action_diagnose.HEC_ACT_PerformedAction_DiagnosisUpdateID;
                initial_performed_action_diagnose_localization.HEC_DIA_Diagnosis_Localization_RefID = diagnosis_localization.HEC_DIA_Diagnosis_LocalizationID;
                initial_performed_action_diagnose_localization.Modification_Timestamp = DateTime.Now;
                initial_performed_action_diagnose_localization.Tenant_RefID           = securityTicket.TenantID;
                initial_performed_action_diagnose_localization.IM_PotentialDiagnosisLocalization_Code = Parameter.localization;

                initial_performed_action_diagnose_localization.Save(Connection, Transaction);
                #endregion

                #region PLANNED ACTION
                var action_gpmid = "mm.docconect.doc.app.planned.action.preexamination";

                var planned_action_type = ORM_HEC_ACT_ActionType.Query.Search(Connection, Transaction, new ORM_HEC_ACT_ActionType.Query()
                {
                    GlobalPropertyMatchingID = action_gpmid,
                    Tenant_RefID             = securityTicket.TenantID,
                    IsDeleted = false
                }).SingleOrDefault();

                if (planned_action_type == null)
                {
                    planned_action_type = new ORM_HEC_ACT_ActionType();
                    planned_action_type.GlobalPropertyMatchingID = action_gpmid;
                    planned_action_type.Modification_Timestamp   = DateTime.Now;
                    planned_action_type.Tenant_RefID             = securityTicket.TenantID;

                    planned_action_type.Save(Connection, Transaction);
                }

                // circular reference to same performed action in order to be compatible with treatment planned actions,
                // so that it can be accessed in the same way
                var preexamination_planned_action = new ORM_HEC_ACT_PlannedAction();
                preexamination_planned_action.IfPlannedFollowup_PreviousAction_RefID = initial_performed_action_id;
                preexamination_planned_action.IsPerformed = true;
                preexamination_planned_action.IfPerformed_PerformedAction_RefID = initial_performed_action_id;
                preexamination_planned_action.MedicalPractice_RefID             = doctor_details.practice_id;
                preexamination_planned_action.Modification_Timestamp            = DateTime.Now;
                preexamination_planned_action.Patient_RefID   = Parameter.patient_id;
                preexamination_planned_action.PlannedFor_Date = Parameter.date;
                preexamination_planned_action.ToBePerformedBy_BusinessParticipant_RefID = doctor.BusinessParticipant_RefID;
                preexamination_planned_action.Tenant_RefID = securityTicket.TenantID;

                preexamination_planned_action.Save(Connection, Transaction);

                var action_to_type = new ORM_HEC_ACT_PlannedAction_2_ActionType();
                action_to_type.HEC_ACT_ActionType_RefID    = planned_action_type.HEC_ACT_ActionTypeID;
                action_to_type.HEC_ACT_PlannedAction_RefID = preexamination_planned_action.HEC_ACT_PlannedActionID;
                action_to_type.Modification_Timestamp      = DateTime.Now;
                action_to_type.Tenant_RefID = securityTicket.TenantID;

                action_to_type.Save(Connection, Transaction);
                #endregion

                #region GPOS
                var patient_consent = ORM_HEC_CRT_InsuranceToBrokerContract_ParticipatingPatient.Query.Search(Connection, Transaction, new ORM_HEC_CRT_InsuranceToBrokerContract_ParticipatingPatient.Query()
                {
                    Patient_RefID = Parameter.patient_id,
                    IsDeleted     = false,
                    Tenant_RefID  = securityTicket.TenantID
                }).Where(t =>
                {
                    var ctrParameter = cls_Get_Contract_Parameter_Value_for_InsuranceToBrokerContractID.Invoke(Connection, Transaction, new P_MD_GCPVfITBCID_1647()
                    {
                        ParameterName = "Duration of participation consent – Month",
                        InsuranceToBrokerContractID = t.InsuranceToBrokerContract_RefID
                    }, securityTicket).Result;

                    var validThrough = ctrParameter == null || ctrParameter.ConsentValidForMonths == double.MaxValue ? DateTime.MaxValue : t.ValidFrom.AddMonths(Convert.ToInt32(ctrParameter.ConsentValidForMonths));
                    return(t.ValidFrom <= Parameter.date && validThrough >= Parameter.date);
                }).OrderBy(t => t.ValidFrom).FirstOrDefault();

                if (patient_consent == null)
                {
                    throw new Exception("No patients consents found for selected date: " + Parameter.date.ToString("dd.MM.yyyy") + " and  patient id: " + Parameter.patient_id);
                }


                var gpos_gpmid = "mm.docconnect.gpos.catalog.voruntersuchung";
                var preexamination_gpos_catalog = ORM_HEC_BIL_PotentialCode_Catalog.Query.Search(Connection, Transaction, new ORM_HEC_BIL_PotentialCode_Catalog.Query()
                {
                    Tenant_RefID             = securityTicket.TenantID,
                    IsDeleted                = false,
                    GlobalPropertyMatchingID = gpos_gpmid
                }).SingleOrDefault();

                if (preexamination_gpos_catalog == null)
                {
                    throw new Exception("Preexamination catalog not found.");
                }

                var gpos_connections_to_drugs = ORM_HEC_BIL_PotentialCode_2_HealthcareProduct.Query.Search(Connection, Transaction, new ORM_HEC_BIL_PotentialCode_2_HealthcareProduct.Query()
                {
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false
                }).GroupBy(t => t.HEC_BIL_PotentialCode_RefID);

                var gpos_connections_to_diagnoses = ORM_HEC_BIL_PotentialCode_2_PotentialDiagnosis.Query.Search(Connection, Transaction, new ORM_HEC_BIL_PotentialCode_2_PotentialDiagnosis.Query()
                {
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false
                }).GroupBy(t => t.HEC_BIL_PotentialCode_RefID);

                var preexamination_gposes = ORM_HEC_BIL_PotentialCode.Query.Search(Connection, Transaction, new ORM_HEC_BIL_PotentialCode.Query()
                {
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false,
                    PotentialCode_Catalog_RefID = preexamination_gpos_catalog.HEC_BIL_PotentialCode_CatalogID
                });

                var covered_gposes = ORM_HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCode.Query.Search(Connection, Transaction, new ORM_HEC_CTR_InsuranceToBrokerContracts_CoveredPotentialBillCode.Query()
                {
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false,
                    InsuranceToBrokerContract_RefID = patient_consent.InsuranceToBrokerContract_RefID
                });

                preexamination_gposes = preexamination_gposes.Where(t => covered_gposes.Any(c => c.PotentialBillCode_RefID == t.HEC_BIL_PotentialCodeID)).ToList();

                var bill_header = new ORM_BIL_BillHeader();
                bill_header.Modification_Timestamp = DateTime.Now;
                bill_header.Tenant_RefID           = securityTicket.TenantID;

                foreach (var gpos in preexamination_gposes)
                {
                    if (!gpos_connections_to_diagnoses.Any(t => t.Key == gpos.HEC_BIL_PotentialCodeID) && !gpos_connections_to_drugs.Any(t => t.Key == gpos.HEC_BIL_PotentialCodeID))
                    {
                        var price_value = ORM_CMN_Price_Value.Query.Search(Connection, Transaction, new ORM_CMN_Price_Value.Query()
                        {
                            Price_RefID = gpos.Price_RefID, IsDeleted = false, Tenant_RefID = securityTicket.TenantID
                        }).SingleOrDefault();
                        if (price_value == null)
                        {
                            throw new Exception("Price value not found for GPOS: " + gpos.BillingCode);
                        }

                        ORM_BIL_BillPosition gpos_position = new ORM_BIL_BillPosition();
                        gpos_position.BIL_BilHeader_RefID        = bill_header.BIL_BillHeaderID;
                        gpos_position.Modification_Timestamp     = DateTime.Now;
                        gpos_position.Tenant_RefID               = securityTicket.TenantID;
                        gpos_position.PositionValue_IncludingTax = Convert.ToDecimal(price_value.PriceValue_Amount);
                        gpos_position.PositionNumber             = cls_Get_Next_Bill_Position_Number.Invoke(Connection, Transaction, securityTicket).Result.bill_position_number;

                        gpos_position.Save(Connection, Transaction);

                        ORM_BIL_BillPosition_TransmitionStatus fs_status = new ORM_BIL_BillPosition_TransmitionStatus();
                        fs_status.BillPosition_RefID     = gpos_position.BIL_BillPositionID;
                        fs_status.IsActive               = true;
                        fs_status.Modification_Timestamp = DateTime.Now;
                        fs_status.Tenant_RefID           = securityTicket.TenantID;
                        fs_status.TransmitionCode        = 1;
                        fs_status.TransmitionStatusKey   = "preexamination";
                        fs_status.TransmittedOnDate      = DateTime.Now;

                        fs_status.Save(Connection, Transaction);

                        ORM_HEC_BIL_BillPosition hec_gpos_position = new ORM_HEC_BIL_BillPosition();
                        hec_gpos_position.Ext_BIL_BillPosition_RefID = gpos_position.BIL_BillPositionID;
                        hec_gpos_position.Modification_Timestamp     = DateTime.Now;
                        hec_gpos_position.Tenant_RefID = securityTicket.TenantID;
                        hec_gpos_position.PositionFor_Patient_RefID = Parameter.patient_id;

                        hec_gpos_position.Save(Connection, Transaction);

                        ORM_HEC_BIL_BillPosition_BillCode hec_gpos_position_code = new ORM_HEC_BIL_BillPosition_BillCode();
                        hec_gpos_position_code.BillPosition_RefID     = hec_gpos_position.HEC_BIL_BillPositionID;
                        hec_gpos_position_code.IM_BillingCode         = gpos.BillingCode;
                        hec_gpos_position_code.Modification_Timestamp = DateTime.Now;
                        hec_gpos_position_code.PotentialCode_RefID    = gpos.HEC_BIL_PotentialCodeID;
                        hec_gpos_position_code.Tenant_RefID           = securityTicket.TenantID;

                        hec_gpos_position_code.Save(Connection, Transaction);

                        ORM_HEC_CAS_Case_BillCode hec_gpos_case_code = new ORM_HEC_CAS_Case_BillCode();
                        hec_gpos_case_code.HEC_BIL_BillPosition_BillCode_RefID = hec_gpos_position_code.HEC_BIL_BillPosition_BillCodeID;
                        hec_gpos_case_code.HEC_CAS_Case_RefID     = new_case.HEC_CAS_CaseID;
                        hec_gpos_case_code.Modification_Timestamp = DateTime.Now;
                        hec_gpos_case_code.Tenant_RefID           = securityTicket.TenantID;

                        hec_gpos_case_code.Save(Connection, Transaction);

                        ORM_BIL_BillPosition_PropertyValue gpos_management_fee_property_value = new ORM_BIL_BillPosition_PropertyValue();
                        gpos_management_fee_property_value.BIL_BillPosition_RefID           = gpos_position.BIL_BillPositionID;
                        gpos_management_fee_property_value.BIL_BillPosition_PropertyValueID = Guid.NewGuid();
                        gpos_management_fee_property_value.Creation_Timestamp     = DateTime.Now;
                        gpos_management_fee_property_value.Modification_Timestamp = DateTime.Now;
                        gpos_management_fee_property_value.PropertyKey            = "mm.doc.connect.management.fee";
                        gpos_management_fee_property_value.PropertyValue          = "waived";
                        gpos_management_fee_property_value.Tenant_RefID           = securityTicket.TenantID;

                        gpos_management_fee_property_value.Save(Connection, Transaction);

                        bill_header.TotalValue_IncludingTax += Convert.ToDecimal(price_value.PriceValue_Amount);
                    }
                }

                bill_header.Save(Connection, Transaction);
                #endregion
            }

            return(returnValue);

            #endregion UserCode
        }
        protected static FR_Base Execute(DbConnection Connection, DbTransaction Transaction, P_CAS_CCGPOS_1000 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var  returnValue = new FR_Base();
            Guid treatment_performed_action_type_id = Guid.Empty;

            var treatment_performed_action_type = ORM_HEC_ACT_ActionType.Query.Search(Connection, Transaction, new ORM_HEC_ACT_ActionType.Query()
            {
                Tenant_RefID             = securityTicket.TenantID,
                IsDeleted                = false,
                GlobalPropertyMatchingID = "mm.docconect.doc.app.performed.action.treatment"
            }).SingleOrDefault();

            if (treatment_performed_action_type == null)
            {
                treatment_performed_action_type = new ORM_HEC_ACT_ActionType();
                treatment_performed_action_type.GlobalPropertyMatchingID = "mm.docconect.doc.app.performed.action.treatment";
                treatment_performed_action_type.Creation_Timestamp       = DateTime.Now;
                treatment_performed_action_type.Modification_Timestamp   = DateTime.Now;
                treatment_performed_action_type.Tenant_RefID             = securityTicket.TenantID;

                treatment_performed_action_type.Save(Connection, Transaction);

                treatment_performed_action_type_id = treatment_performed_action_type.HEC_ACT_ActionTypeID;
            }
            else
            {
                treatment_performed_action_type_id = treatment_performed_action_type.HEC_ACT_ActionTypeID;
            }

            ORM_HEC_Product.Query hec_drug_detailsQ = new ORM_HEC_Product.Query();
            hec_drug_detailsQ.HEC_ProductID = Parameter.drug_id;
            hec_drug_detailsQ.Tenant_RefID  = securityTicket.TenantID;
            hec_drug_detailsQ.IsDeleted     = false;

            var hec_drug_details = ORM_HEC_Product.Query.Search(Connection, Transaction, hec_drug_detailsQ).SingleOrDefault();
            if (hec_drug_details != null)
            {
                ORM_CMN_PRO_Product.Query cmn_drug_detailsQ = new ORM_CMN_PRO_Product.Query();
                cmn_drug_detailsQ.Tenant_RefID      = securityTicket.TenantID;
                cmn_drug_detailsQ.CMN_PRO_ProductID = hec_drug_details.Ext_PRO_Product_RefID;
                cmn_drug_detailsQ.IsDeleted         = false;

                var cmn_drug_details = ORM_CMN_PRO_Product.Query.Search(Connection, Transaction, cmn_drug_detailsQ).SingleOrDefault();
                if (cmn_drug_details != null)
                {
                    var drug_name = cmn_drug_details.Product_Name.GetContent(Parameter.all_languagesL.FirstOrDefault().CMN_LanguageID);

                    var treatment_count = cls_Get_Treatment_Count_for_PatientID_And_DiagnoseID_and_LocalizationCode.Invoke(Connection, Transaction, new P_CAS_GTCfPIDaDIDaLC_1008()
                    {
                        ActionTypeID     = treatment_performed_action_type_id,
                        DiagnoseID       = Parameter.diagnose_id,
                        PatientID        = Parameter.patient_id,
                        LocalizationCode = Parameter.localization,
                        PerformedDate    = Parameter.treatment_date == DateTime.MinValue ? DateTime.Now : Parameter.treatment_date
                    }, securityTicket).Result;

                    if (treatment_count != null)
                    {
                        treatment_count.treatment_count++;

                        var contracts = cls_Get_InsuranceToBrokerContractID_for_DrugID_and_DiagnoseID.Invoke(Connection, Transaction, new P_CAS_GItBCIDfDIDaDID_1541()
                        {
                            DiagnoseID    = Parameter.diagnose_id,
                            DrugID        = Parameter.drug_id,
                            PatientID     = Parameter.patient_id,
                            TreatmentDate = Parameter.treatment_date
                        }, securityTicket).Result;

                        var contract_id = contracts.Where(ctr => ctr.patient_consent_valid_from.AddMonths(ctr.contract_consent_valid_for_months) >= Parameter.treatment_date).OrderBy(ctr => ctr.patient_consent_valid_from).FirstOrDefault();

                        if (contract_id != null)
                        {
                            var gpos_details = cls_Get_GPOS_Details_for_DiagnoseID_and_DrugID.Invoke(Connection, Transaction, new P_CAS_GGPOSDfDIDaDID_1033()
                            {
                                DiagnoseID = Parameter.diagnose_id,
                                DrugID     = Parameter.drug_id,
                                ContractID = contract_id.contract_id
                            }, securityTicket).Result;

                            if (gpos_details.Length != 0)
                            {
                                if (!Parameter.should_update)
                                {
                                    List <string> gpos_types = new List <string>();

                                    if (Parameter.treatment_doctor_id != Guid.Empty)
                                    {
                                        gpos_types.Add("mm.docconnect.gpos.catalog.operation");
                                    }

                                    if (Parameter.ac_doctor_id != Guid.Empty)
                                    {
                                        gpos_types.Add("mm.docconnect.gpos.catalog.nachsorge");
                                    }

                                    if (drug_name.Contains("Bevacizumab") && Parameter.treatment_doctor_id != Guid.Empty)
                                    {
                                        gpos_types.Add("mm.docconnect.gpos.catalog.zusatzposition.bevacizumab");
                                    }

                                    if (Parameter.treatment_doctor_id != Guid.Empty)
                                    {
                                        gpos_types.Add("mm.docconnect.gpos.catalog.wartezeitenmanagement");
                                    }

                                    foreach (var gpos_type in gpos_types)
                                    {
                                        foreach (var gpos_detail in gpos_details)
                                        {
                                            if (gpos_detail.gpos_type == gpos_type)
                                            {
                                                bool gpos_condition = gpos_type == "mm.docconnect.gpos.catalog.operation" || gpos_type == "mm.docconnect.gpos.catalog.nachsorge" ?
                                                                      treatment_count.treatment_count >= gpos_detail.injection_from &&
                                                                      !gpos_details.Any(gpos => treatment_count.treatment_count >= gpos.injection_from && gpos.injection_from > gpos_detail.injection_from && gpos.gpos_type == gpos_type) : true;

                                                if (gpos_condition)
                                                {
                                                    ORM_BIL_BillPosition gpos_position = new ORM_BIL_BillPosition();
                                                    gpos_position.BIL_BilHeader_RefID        = Guid.Empty;
                                                    gpos_position.BIL_BillPositionID         = Guid.NewGuid();
                                                    gpos_position.Creation_Timestamp         = DateTime.Now;
                                                    gpos_position.Modification_Timestamp     = DateTime.Now;
                                                    gpos_position.Tenant_RefID               = securityTicket.TenantID;
                                                    gpos_position.PositionValue_IncludingTax = Convert.ToDecimal(gpos_detail.gpos_price);

                                                    gpos_position.Save(Connection, Transaction);

                                                    ORM_HEC_BIL_BillPosition hec_gpos_position = new ORM_HEC_BIL_BillPosition();
                                                    hec_gpos_position.Creation_Timestamp         = DateTime.Now;
                                                    hec_gpos_position.Ext_BIL_BillPosition_RefID = gpos_position.BIL_BillPositionID;
                                                    hec_gpos_position.HEC_BIL_BillPositionID     = Guid.NewGuid();
                                                    hec_gpos_position.Modification_Timestamp     = DateTime.Now;
                                                    hec_gpos_position.Tenant_RefID = securityTicket.TenantID;
                                                    hec_gpos_position.PositionFor_Patient_RefID = Parameter.patient_id;

                                                    hec_gpos_position.Save(Connection, Transaction);

                                                    ORM_HEC_BIL_BillPosition_BillCode hec_gpos_position_code = new ORM_HEC_BIL_BillPosition_BillCode();
                                                    hec_gpos_position_code.BillPosition_RefID = hec_gpos_position.HEC_BIL_BillPositionID;
                                                    hec_gpos_position_code.Creation_Timestamp = DateTime.Now;
                                                    hec_gpos_position_code.HEC_BIL_BillPosition_BillCodeID = Guid.NewGuid();
                                                    hec_gpos_position_code.IM_BillingCode         = gpos_detail.gpos_code;
                                                    hec_gpos_position_code.Modification_Timestamp = DateTime.Now;
                                                    hec_gpos_position_code.PotentialCode_RefID    = gpos_detail.gpos_id;
                                                    hec_gpos_position_code.Tenant_RefID           = securityTicket.TenantID;

                                                    hec_gpos_position_code.Save(Connection, Transaction);

                                                    ORM_HEC_CAS_Case_BillCode hec_gpos_case_code = new ORM_HEC_CAS_Case_BillCode();
                                                    hec_gpos_case_code.Creation_Timestamp = DateTime.Now;
                                                    hec_gpos_case_code.HEC_BIL_BillPosition_BillCode_RefID = hec_gpos_position_code.HEC_BIL_BillPosition_BillCodeID;
                                                    hec_gpos_case_code.HEC_CAS_Case_BillCodeID             = Guid.NewGuid();
                                                    hec_gpos_case_code.HEC_CAS_Case_RefID     = Parameter.case_id;
                                                    hec_gpos_case_code.Modification_Timestamp = DateTime.Now;
                                                    hec_gpos_case_code.Tenant_RefID           = securityTicket.TenantID;

                                                    hec_gpos_case_code.Save(Connection, Transaction);
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    var bill_position_ids = cls_Get_BillPositionIDs_for_CaseID.Invoke(Connection, Transaction, new P_CAS_GBPIDsfCID_0928()
                                    {
                                        CaseID = Parameter.case_id
                                    }, securityTicket).Result;

                                    foreach (var id in bill_position_ids)
                                    {
                                        foreach (var gpos_detail in gpos_details)
                                        {
                                            if (id.gpos_type == "mm.docconnect.gpos.catalog.operation" || id.gpos_type == "mm.docconnect.gpos.catalog.nachsorge")
                                            {
                                                if (id.gpos_type == gpos_detail.gpos_type)
                                                {
                                                    if (treatment_count.treatment_count >= gpos_detail.injection_from &&
                                                        !gpos_details.Any(gpos => treatment_count.treatment_count >= gpos.injection_from && gpos.injection_from > gpos_detail.injection_from))
                                                    {
                                                        ORM_BIL_BillPosition.Query old_gpos_positionQ = new ORM_BIL_BillPosition.Query();
                                                        old_gpos_positionQ.BIL_BillPositionID = id.bill_position_id;
                                                        old_gpos_positionQ.Tenant_RefID       = securityTicket.TenantID;
                                                        old_gpos_positionQ.IsDeleted          = false;

                                                        var old_gpos_position = ORM_BIL_BillPosition.Query.Search(Connection, Transaction, old_gpos_positionQ).SingleOrDefault();
                                                        if (old_gpos_position != null)
                                                        {
                                                            old_gpos_position.Modification_Timestamp     = DateTime.Now;
                                                            old_gpos_position.PositionValue_IncludingTax = Convert.ToDecimal(gpos_detail.gpos_price);

                                                            old_gpos_position.Save(Connection, Transaction);

                                                            ORM_HEC_BIL_BillPosition.Query old_hec_gpos_positionQ = new ORM_HEC_BIL_BillPosition.Query();
                                                            old_hec_gpos_positionQ.Ext_BIL_BillPosition_RefID = old_gpos_position.BIL_BillPositionID;
                                                            old_hec_gpos_positionQ.Tenant_RefID = securityTicket.TenantID;
                                                            old_hec_gpos_positionQ.PositionFor_Patient_RefID = Parameter.patient_id;
                                                            old_hec_gpos_positionQ.IsDeleted = false;

                                                            var old_hec_gpos_position = ORM_HEC_BIL_BillPosition.Query.Search(Connection, Transaction, old_hec_gpos_positionQ).SingleOrDefault();
                                                            if (old_hec_gpos_position != null)
                                                            {
                                                                ORM_HEC_BIL_BillPosition_BillCode.Query old_hec_gpos_position_codeQ = new ORM_HEC_BIL_BillPosition_BillCode.Query();
                                                                old_hec_gpos_position_codeQ.Tenant_RefID       = securityTicket.TenantID;
                                                                old_hec_gpos_position_codeQ.BillPosition_RefID = old_hec_gpos_position.HEC_BIL_BillPositionID;
                                                                old_hec_gpos_position_codeQ.IsDeleted          = false;

                                                                var old_hec_gpos_position_code = ORM_HEC_BIL_BillPosition_BillCode.Query.Search(Connection, Transaction, old_hec_gpos_position_codeQ).SingleOrDefault();
                                                                if (old_hec_gpos_position_code != null)
                                                                {
                                                                    old_hec_gpos_position_code.IM_BillingCode         = gpos_detail.gpos_code;
                                                                    old_hec_gpos_position_code.Modification_Timestamp = DateTime.Now;
                                                                    old_hec_gpos_position_code.PotentialCode_RefID    = gpos_detail.gpos_id;

                                                                    old_hec_gpos_position_code.Save(Connection, Transaction);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(returnValue);

            #endregion UserCode
        }