protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L5ACACU_SCC_1051 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Guid();

            if (Parameter.IsDelete && (Parameter.CMN_BPT_CTM_CustomerID == Guid.Empty))
            {
                return(returnValue);
            }

            ORM_CMN_BPT_CTM_Customer customer;
            if (Parameter.CMN_BPT_CTM_CustomerID != Guid.Empty)
            {
                var customerQ = new ORM_CMN_BPT_CTM_Customer.Query();
                customerQ.Tenant_RefID           = securityTicket.TenantID;
                customerQ.IsDeleted              = false;
                customerQ.CMN_BPT_CTM_CustomerID = Parameter.CMN_BPT_CTM_CustomerID;
                customer = ORM_CMN_BPT_CTM_Customer.Query.Search(Connection, Transaction, customerQ).First();
                customer.InternalCustomerNumber = Parameter.Number;
                customer.IsCustomerOrderAutomaticallyApprovedOnReceipt =
                    Parameter.IsCustomerOrderAutomaticallyApprovedOnReceipt;
            }
            else
            {
                customer = new ORM_CMN_BPT_CTM_Customer();
                customer.Tenant_RefID           = securityTicket.TenantID;
                customer.CMN_BPT_CTM_CustomerID = Guid.NewGuid();
                customer.InternalCustomerNumber = Parameter.Number;
                customer.IsCustomerOrderAutomaticallyApprovedOnReceipt =
                    Parameter.IsCustomerOrderAutomaticallyApprovedOnReceipt;
            }

            ORM_CMN_BPT_CTM_AvailablePaymentType customer2PaymentType;

            var customer2PaymentTypeQ = new ORM_CMN_BPT_CTM_AvailablePaymentType.Query();
            customer2PaymentTypeQ.Tenant_RefID   = securityTicket.TenantID;
            customer2PaymentTypeQ.IsDeleted      = false;
            customer2PaymentTypeQ.Customer_RefID = customer.CMN_BPT_CTM_CustomerID;

            customer2PaymentType = ORM_CMN_BPT_CTM_AvailablePaymentType.Query.Search(Connection, Transaction, customer2PaymentTypeQ).FirstOrDefault();
            if (customer2PaymentType == null)
            {
                customer2PaymentType = new ORM_CMN_BPT_CTM_AvailablePaymentType();
                customer2PaymentType.ACC_PAY_Type_RefID = Guid.NewGuid();
                customer2PaymentType.Tenant_RefID       = securityTicket.TenantID;
                customer2PaymentType.Customer_RefID     = customer.CMN_BPT_CTM_CustomerID;
            }
            customer2PaymentType.ACC_PAY_Type_RefID = Parameter.PaymentTypeID;
            customer2PaymentType.Save(Connection, Transaction);

            #region payment condition

            ORM_CMN_BPT_CTM_AvailablePaymentCondition customer2PaymentCondition;

            var customer2PaymentConditionQ = new ORM_CMN_BPT_CTM_AvailablePaymentCondition.Query();
            customer2PaymentConditionQ.Tenant_RefID   = securityTicket.TenantID;
            customer2PaymentConditionQ.IsDeleted      = false;
            customer2PaymentConditionQ.Customer_RefID = customer.CMN_BPT_CTM_CustomerID;

            customer2PaymentCondition = ORM_CMN_BPT_CTM_AvailablePaymentCondition.Query.Search(Connection, Transaction, customer2PaymentConditionQ).FirstOrDefault();
            if (customer2PaymentCondition == null)
            {
                customer2PaymentCondition = new ORM_CMN_BPT_CTM_AvailablePaymentCondition();
                customer2PaymentCondition.ACC_PAY_Condition_RefID = Guid.NewGuid();
                customer2PaymentCondition.Tenant_RefID            = securityTicket.TenantID;
                customer2PaymentCondition.Customer_RefID          = customer.CMN_BPT_CTM_CustomerID;
            }
            customer2PaymentCondition.ACC_PAY_Condition_RefID = Parameter.PaymentConditionID;
            customer2PaymentCondition.Save(Connection, Transaction);

            #endregion

            ORM_CMN_BPT_BusinessParticipant bParticipant;
            if (customer.Ext_BusinessParticipant_RefID != Guid.Empty)
            {
                var bParticipantQ = new ORM_CMN_BPT_BusinessParticipant.Query();
                bParticipantQ.Tenant_RefID = securityTicket.TenantID;
                bParticipantQ.IsDeleted    = false;
                bParticipantQ.CMN_BPT_BusinessParticipantID = customer.Ext_BusinessParticipant_RefID;
                bParticipant = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, bParticipantQ).First();
            }
            else
            {
                bParticipant = new ORM_CMN_BPT_BusinessParticipant();
                bParticipant.Tenant_RefID = securityTicket.TenantID;
                bParticipant.CMN_BPT_BusinessParticipantID = Guid.NewGuid();
                bParticipant.IsCompany = true;
                customer.Ext_BusinessParticipant_RefID = bParticipant.CMN_BPT_BusinessParticipantID;
            }

            bParticipant.DisplayName = Parameter.FirmName;

            ORM_CMN_COM_CompanyInfo company;
            if (bParticipant.IfCompany_CMN_COM_CompanyInfo_RefID != Guid.Empty)
            {
                var companyQ = new ORM_CMN_COM_CompanyInfo.Query();
                companyQ.Tenant_RefID          = securityTicket.TenantID;
                companyQ.IsDeleted             = false;
                companyQ.CMN_COM_CompanyInfoID = bParticipant.IfCompany_CMN_COM_CompanyInfo_RefID;
                company = ORM_CMN_COM_CompanyInfo.Query.Search(Connection, Transaction, companyQ).First();
            }
            else
            {
                company = new ORM_CMN_COM_CompanyInfo();
                company.Tenant_RefID          = securityTicket.TenantID;
                company.CMN_COM_CompanyInfoID = Guid.NewGuid();
                bParticipant.IfCompany_CMN_COM_CompanyInfo_RefID = company.CMN_COM_CompanyInfoID;
            }

            ORM_CMN_UniversalContactDetail ucd;
            if (company.Contact_UCD_RefID != Guid.Empty)
            {
                var ucdQ = new ORM_CMN_UniversalContactDetail.Query();
                ucdQ.Tenant_RefID = securityTicket.TenantID;
                ucdQ.IsDeleted    = false;
                ucdQ.CMN_UniversalContactDetailID = company.Contact_UCD_RefID;
                ucd = ORM_CMN_UniversalContactDetail.Query.Search(Connection, Transaction, ucdQ).First();
            }
            else
            {
                ucd = new ORM_CMN_UniversalContactDetail();
                ucd.Tenant_RefID = securityTicket.TenantID;
                ucd.CMN_UniversalContactDetailID = Guid.NewGuid();
                ucd.IsCompany             = true;
                company.Contact_UCD_RefID = ucd.CMN_UniversalContactDetailID;
            }

            ucd.CompanyName_Line1  = Parameter.FirmName;
            ucd.CompanyName_Line2  = Parameter.Additional;
            ucd.IsDeleted          = Parameter.IsDelete;
            company.IsDeleted      = Parameter.IsDelete;
            bParticipant.IsDeleted = Parameter.IsDelete;
            customer.IsDeleted     = Parameter.IsDelete;

            ucd.Save(Connection, Transaction);
            company.Save(Connection, Transaction);
            bParticipant.Save(Connection, Transaction);
            customer.Save(Connection, Transaction);

            returnValue.Result = customer.CMN_BPT_CTM_CustomerID;

            return(returnValue);

            #endregion UserCode
        }
Beispiel #2
0
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L5ACACU_SPC_1047 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Guid();

            if (Parameter.IsDelete && (Parameter.CMN_BPT_CTM_CustomerID == Guid.Empty))
            {
                return(returnValue);
            }

            ORM_CMN_BPT_CTM_Customer customer;
            if (Parameter.CMN_BPT_CTM_CustomerID != Guid.Empty)
            {
                var customerQ = new ORM_CMN_BPT_CTM_Customer.Query();
                customerQ.Tenant_RefID           = securityTicket.TenantID;
                customerQ.IsDeleted              = false;
                customerQ.CMN_BPT_CTM_CustomerID = Parameter.CMN_BPT_CTM_CustomerID;
                customer = ORM_CMN_BPT_CTM_Customer.Query.Search(Connection, Transaction, customerQ).First();
                customer.IsCustomerOrderAutomaticallyApprovedOnReceipt =
                    Parameter.IsCustomerOrderAutomaticallyApprovedOnReceipt;
            }
            else
            {
                customer = new ORM_CMN_BPT_CTM_Customer();
                customer.Tenant_RefID           = securityTicket.TenantID;
                customer.CMN_BPT_CTM_CustomerID = Guid.NewGuid();
                customer.InternalCustomerNumber = Parameter.Number;
                customer.IsCustomerOrderAutomaticallyApprovedOnReceipt =
                    Parameter.IsCustomerOrderAutomaticallyApprovedOnReceipt;
            }

            ORM_CMN_BPT_CTM_AvailablePaymentType customer2PaymentType;

            var customer2PaymentTypeQ = new ORM_CMN_BPT_CTM_AvailablePaymentType.Query();
            customer2PaymentTypeQ.Tenant_RefID   = securityTicket.TenantID;
            customer2PaymentTypeQ.IsDeleted      = false;
            customer2PaymentTypeQ.Customer_RefID = customer.CMN_BPT_CTM_CustomerID;

            customer2PaymentType = ORM_CMN_BPT_CTM_AvailablePaymentType.Query.Search(Connection, Transaction, customer2PaymentTypeQ).FirstOrDefault();
            if (customer2PaymentType == null)
            {
                customer2PaymentType = new ORM_CMN_BPT_CTM_AvailablePaymentType();
                customer2PaymentType.ACC_PAY_Type_RefID = Guid.NewGuid();
                customer2PaymentType.Tenant_RefID       = securityTicket.TenantID;
                customer2PaymentType.Customer_RefID     = customer.CMN_BPT_CTM_CustomerID;
            }
            customer2PaymentType.ACC_PAY_Type_RefID = Parameter.PaymentTypeID;
            customer2PaymentType.Save(Connection, Transaction);

            #region payment condition

            ORM_CMN_BPT_CTM_AvailablePaymentCondition customer2PaymentCondition;

            var customer2PaymentConditionQ = new ORM_CMN_BPT_CTM_AvailablePaymentCondition.Query();
            customer2PaymentConditionQ.Tenant_RefID   = securityTicket.TenantID;
            customer2PaymentConditionQ.IsDeleted      = false;
            customer2PaymentConditionQ.Customer_RefID = customer.CMN_BPT_CTM_CustomerID;

            customer2PaymentCondition = ORM_CMN_BPT_CTM_AvailablePaymentCondition.Query.Search(Connection, Transaction, customer2PaymentConditionQ).FirstOrDefault();
            if (customer2PaymentCondition == null)
            {
                customer2PaymentCondition = new ORM_CMN_BPT_CTM_AvailablePaymentCondition();
                customer2PaymentCondition.ACC_PAY_Condition_RefID = Guid.NewGuid();
                customer2PaymentCondition.Tenant_RefID            = securityTicket.TenantID;
                customer2PaymentCondition.Customer_RefID          = customer.CMN_BPT_CTM_CustomerID;
            }
            customer2PaymentCondition.ACC_PAY_Condition_RefID = Parameter.PaymentConditionID;
            customer2PaymentCondition.Save(Connection, Transaction);

            #endregion

            ORM_CMN_BPT_BusinessParticipant bParticipant;
            if (customer.Ext_BusinessParticipant_RefID != Guid.Empty)
            {
                var bParticipantQ = new ORM_CMN_BPT_BusinessParticipant.Query();
                bParticipantQ.Tenant_RefID = securityTicket.TenantID;
                bParticipantQ.IsDeleted    = false;
                bParticipantQ.CMN_BPT_BusinessParticipantID = customer.Ext_BusinessParticipant_RefID;
                bParticipant = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, bParticipantQ).First();
            }
            else
            {
                bParticipant = new ORM_CMN_BPT_BusinessParticipant();
                bParticipant.Tenant_RefID = securityTicket.TenantID;
                bParticipant.CMN_BPT_BusinessParticipantID = Guid.NewGuid();
                bParticipant.IsNaturalPerson           = true;
                customer.Ext_BusinessParticipant_RefID = bParticipant.CMN_BPT_BusinessParticipantID;
            }

            bParticipant.DisplayName = Parameter.FirstName + " " + Parameter.LastName;

            ORM_CMN_PER_PersonInfo personInfo;
            if (bParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID != Guid.Empty)
            {
                var personInfoQ = new ORM_CMN_PER_PersonInfo.Query();
                personInfoQ.Tenant_RefID         = securityTicket.TenantID;
                personInfoQ.IsDeleted            = false;
                personInfoQ.CMN_PER_PersonInfoID = bParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID;
                personInfo = ORM_CMN_PER_PersonInfo.Query.Search(Connection, Transaction, personInfoQ).First();
            }
            else
            {
                personInfo = new ORM_CMN_PER_PersonInfo();
                personInfo.Tenant_RefID         = securityTicket.TenantID;
                personInfo.CMN_PER_PersonInfoID = Guid.NewGuid();
                bParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;
            }

            #region connection with legal guardian
            //checking is there allready legal guardian associated with this business participant

            ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant.Query LGCheckQuery = new ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant.Query();
            LGCheckQuery.BusinessParticipant_RefID = bParticipant.CMN_BPT_BusinessParticipantID;
            LGCheckQuery.IsDeleted = false;

            Boolean oldLegalGuardianConnectionExist = ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant.Query.Exists(Connection, Transaction, LGCheckQuery);

            if (Parameter.IsRepresentedByLegalGuardian)
            {
                if (oldLegalGuardianConnectionExist)
                {
                    ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant oldLegalGuardianConnection = ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant.Query.Search(Connection, Transaction, LGCheckQuery).First();
                    ORM_CMN_BPT_BusinessParticipant.Query oldLegalGuardianQuery = new ORM_CMN_BPT_BusinessParticipant.Query();
                    oldLegalGuardianQuery.CMN_BPT_BusinessParticipantID = oldLegalGuardianConnection.AssociatedBusinessParticipant_RefID;
                    oldLegalGuardianQuery.IsDeleted = false;

                    ORM_CMN_BPT_BusinessParticipant oldLegalGuardian = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, oldLegalGuardianQuery).First();
                    oldLegalGuardian.DisplayName = Parameter.LegalGuardianName;
                    oldLegalGuardian.Save(Connection, Transaction);
                }
                else
                {
                    // creating a new legal guardian
                    ORM_CMN_BPT_BusinessParticipant newLegalGuardian = new ORM_CMN_BPT_BusinessParticipant();
                    ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant legalGuardianLink = new ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant();

                    newLegalGuardian.DisplayName = Parameter.LegalGuardianName;
                    legalGuardianLink.AssociatedBusinessParticipant_RefID = newLegalGuardian.CMN_BPT_BusinessParticipantID;
                    legalGuardianLink.BusinessParticipant_RefID           = bParticipant.CMN_BPT_BusinessParticipantID;
                    newLegalGuardian.Save(Connection, Transaction);
                    legalGuardianLink.Save(Connection, Transaction);
                }
            }
            else
            {
                //delete old legal guardian if there is an old legal guardian
                if (oldLegalGuardianConnectionExist)
                {
                    ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant oldLegalGuardianConnection = ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant.Query.Search(Connection, Transaction, LGCheckQuery).First();
                    ORM_CMN_BPT_BusinessParticipant.Query oldLegalGuardianQuery = new ORM_CMN_BPT_BusinessParticipant.Query();
                    oldLegalGuardianQuery.CMN_BPT_BusinessParticipantID = oldLegalGuardianConnection.AssociatedBusinessParticipant_RefID;
                    oldLegalGuardianQuery.IsDeleted = false;

                    //deleting old legal guardian from business participant table
                    if (ORM_CMN_BPT_BusinessParticipant.Query.Exists(Connection, Transaction, oldLegalGuardianQuery))
                    {
                        ORM_CMN_BPT_BusinessParticipant oldLegalGuardian = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, oldLegalGuardianQuery).First();
                        oldLegalGuardian.IsDeleted = true;
                        oldLegalGuardian.Save(Connection, Transaction);
                    }
                    // deleting connenction beetwen customer and legal guardian
                    oldLegalGuardianConnection.IsDeleted = true;
                    oldLegalGuardianConnection.Save(Connection, Transaction);
                }
            }

            #endregion

            personInfo.FirstName = Parameter.FirstName;
            personInfo.LastName  = Parameter.LastName;
            personInfo.BirthDate = Parameter.BirthDate;
            personInfo.IsRepresentedByLegalGuardian = Parameter.IsRepresentedByLegalGuardian;

            personInfo.IsDeleted   = Parameter.IsDelete;
            bParticipant.IsDeleted = Parameter.IsDelete;
            customer.IsDeleted     = Parameter.IsDelete;

            personInfo.Save(Connection, Transaction);
            bParticipant.Save(Connection, Transaction);
            customer.Save(Connection, Transaction);

            returnValue.Result = customer.CMN_BPT_CTM_CustomerID;

            return(returnValue);

            #endregion UserCode
        }