///<summary>
 /// Opens the connection/transaction for the given connectionString, and closes them when complete
 ///<summary>
 public static FR_L6HI_SHISwDC_1513 Invoke(string ConnectionString, P_L6HI_SHISwDC_1513 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
 {
     return(Invoke(null, null, ConnectionString, Parameter, securityTicket));
 }
        ///<summary>
        /// Method Invocation of wrapper classes
        ///<summary>
        protected static FR_L6HI_SHISwDC_1513 Invoke(DbConnection Connection, DbTransaction Transaction, string ConnectionString, P_L6HI_SHISwDC_1513 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            bool cleanupConnection  = Connection == null;
            bool cleanupTransaction = Transaction == null;

            FR_L6HI_SHISwDC_1513 functionReturn = new FR_L6HI_SHISwDC_1513();

            try
            {
                if (cleanupConnection == true)
                {
                    Connection = CSV2Core_MySQL.Support.DBSQLSupport.CreateConnection(ConnectionString);
                    Connection.Open();
                }
                if (cleanupTransaction == true)
                {
                    Transaction = Connection.BeginTransaction();
                }

                functionReturn = Execute(Connection, Transaction, Parameter, securityTicket);

                #region Cleanup Connection/Transaction
                //Commit the transaction
                if (cleanupTransaction == true)
                {
                    Transaction.Commit();
                }
                //Close the connection
                if (cleanupConnection == true)
                {
                    Connection.Close();
                }
                #endregion
            }
            catch (Exception ex)
            {
                try
                {
                    if (cleanupTransaction == true && Transaction != null)
                    {
                        Transaction.Rollback();
                    }
                }
                catch { }

                try
                {
                    if (cleanupConnection == true && Connection != null)
                    {
                        Connection.Close();
                    }
                }
                catch { }

                throw new Exception("Exception occured in method cls_Save_HIState_withDeleteCheck", ex);
            }
            return(functionReturn);
        }
        protected static FR_L6HI_SHISwDC_1513 Execute(DbConnection Connection, DbTransaction Transaction, P_L6HI_SHISwDC_1513 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            //Leave UserCode region to enable user code saving
            #region UserCode
            var returnValue = new FR_L6HI_SHISwDC_1513();
            //Put your code here

            returnValue.Result = new L6HI_SHISwDC_1513();

            Guid HealthStateId = Guid.Empty;
            if (!Parameter.BaseData.IsDeleted)
            {
                HealthStateId = cls_Save_HIState.Invoke(Connection, Transaction, Parameter.BaseData, securityTicket).Result;
            }
            else
            {
                List <ORM_HEC_Patient_HealthInsurance> existingPatient = ORM_HEC_Patient_HealthInsurance.Query.Search(Connection, Transaction, new ORM_HEC_Patient_HealthInsurance.Query
                {
                    IsDeleted    = false,
                    Tenant_RefID = securityTicket.TenantID,
                    HealthInsurance_State_RefID = Parameter.BaseData.HEC_Patient_HealthInsurance_StateID
                }).ToList();

                if (existingPatient.Count > 0) //cannot delete
                {
                    List <L6HI_SHISwDC_1513_UsedInPatient> usedPatientList = new List <L6HI_SHISwDC_1513_UsedInPatient>();

                    foreach (var patient in existingPatient)
                    {
                        L5PA_GPNfPID_1500 patientName = cls_Get_Patient_Name_for_PatientID.Invoke(Connection, Transaction, new P_L5PA_GPNfPID_1500 {
                            PatientID = patient.Patient_RefID
                        }, securityTicket).Result;
                        string fullName = patientName.FirstName + " " + patientName.LastName;
                        usedPatientList.Add(new L6HI_SHISwDC_1513_UsedInPatient {
                            PatientName = fullName
                        });
                    }
                    returnValue.Result.UsedInPatient = usedPatientList.ToArray();
                }

                if (existingPatient.Count == 0)
                {
                    HealthStateId = cls_Save_HIState.Invoke(Connection, Transaction, Parameter.BaseData, securityTicket).Result;
                }
            }
            returnValue.Result.ID = HealthStateId;

            return(returnValue);

            #endregion UserCode
        }
 ///<summary>
 /// Invokes the method for the given Connection, and Transaction, leaving them open/not commited if no exceptions occured
 ///<summary>
 public static FR_L6HI_SHISwDC_1513 Invoke(DbConnection Connection, DbTransaction Transaction, P_L6HI_SHISwDC_1513 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
 {
     return(Invoke(Connection, Transaction, null, Parameter, securityTicket));
 }