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

            var item = new CL1_CMN_BPT.ORM_CMN_BPT_BusinessParticipant();
            var associatedBusinessParticipant = new CL1_CMN_BPT.ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant();
            var personInfo            = new CL1_CMN_PER.ORM_CMN_PER_PersonInfo();
            var communicationContacts = new List <CL1_CMN_PER.ORM_CMN_PER_CommunicationContact>();
            var address = new CL1_CMN.ORM_CMN_Address();

            if (Parameter.CMN_BPT_BusinessParticipantID != Guid.Empty)
            {
                var result = item.Load(Connection, Transaction, Parameter.CMN_BPT_BusinessParticipantID);
                associatedBusinessParticipant = CL1_CMN_BPT.ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant.Query.Search(Connection, Transaction,
                                                                                                                                       new CL1_CMN_BPT.ORM_CMN_BPT_BusinessParticipant_AssociatedBusinessParticipant.Query
                {
                    IsDeleted = false,
                    BusinessParticipant_RefID = item.CMN_BPT_BusinessParticipantID
                }).Single();
                // Get person info
                personInfo.Load(Connection, Transaction, item.IfNaturalPerson_CMN_PER_PersonInfo_RefID);
                // Get communication contacts
                communicationContacts = CL1_CMN_PER.ORM_CMN_PER_CommunicationContact.Query.Search(Connection, Transaction,
                                                                                                  new CL1_CMN_PER.ORM_CMN_PER_CommunicationContact.Query {
                    IsDeleted = false, PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID
                });
                // Get address
                if (Parameter.CMN_AddressID != Guid.Empty)
                {
                    address.Load(Connection, Transaction, Parameter.CMN_AddressID);
                }
            }

            if (Parameter.IsDeleted == true)
            {
                associatedBusinessParticipant.IsDeleted = true;
                personInfo.IsDeleted = true;
                communicationContacts.ForEach(x => x.IsDeleted = true);
                communicationContacts.ForEach(x => x.Save(Connection, Transaction));
                if (address.Status_IsAlreadySaved)
                {
                    address.IsDeleted = true;
                    address.Save(Connection, Transaction);
                }
                item.IsDeleted = true;
                return(new FR_Guid(item.Save(Connection, Transaction), item.CMN_BPT_BusinessParticipantID));
            }

            //Creation specific parameters (Tenant, Account ... )
            if (Parameter.CMN_BPT_BusinessParticipantID == Guid.Empty)
            {
                item.Tenant_RefID = securityTicket.TenantID;
                associatedBusinessParticipant.Tenant_RefID = securityTicket.TenantID;
                personInfo.Tenant_RefID = securityTicket.TenantID;
                communicationContacts.ForEach(x => x.Tenant_RefID = securityTicket.TenantID);
                address.Tenant_RefID = securityTicket.TenantID;
            }

            item.IsNaturalPerson = true;
            item.IfNaturalPerson_CMN_PER_PersonInfo_RefID                     = personInfo.CMN_PER_PersonInfoID;
            associatedBusinessParticipant.BusinessParticipant_RefID           = item.CMN_BPT_BusinessParticipantID;
            associatedBusinessParticipant.AssociatedBusinessParticipant_RefID = Parameter.AssociatedBusinessParticipant_RefID;
            associatedBusinessParticipant.AssociatedParticipant_FunctionName  = Parameter.AssociatedParticipant_FunctionName;
            personInfo.FirstName     = Parameter.FirstName;
            personInfo.LastName      = Parameter.LastName;
            personInfo.Address_RefID = address.CMN_AddressID;

            #region Save Address
            if (!Parameter.UseDefaultAddress)
            {
                address.City_Name       = Parameter.City_Name;
                address.Street_Name     = Parameter.Street_Name;
                address.Street_Number   = Parameter.Street_Number;
                address.City_PostalCode = Parameter.City_PostalCode;
                address.Country_ISOCode = Parameter.Country_ISOCode;
                address.Save(Connection, Transaction);
            }
            else
            {
                if (address.Status_IsAlreadySaved)
                {
                    address.IsDeleted = true;
                    address.Save(Connection, Transaction);
                }
                personInfo.Address_RefID = Guid.Empty;
            }
            #endregion
            #region Save Connection Contact
            var tenantCompanyTypes = CL1_CMN_PER.ORM_CMN_PER_CommunicationContact_Type.Query.Search(Connection, Transaction,
                                                                                                    new CL1_CMN_PER.ORM_CMN_PER_CommunicationContact_Type.Query()
            {
                Tenant_RefID = item.Tenant_RefID,
                IsDeleted    = false
            });
            foreach (var ctype in tenantCompanyTypes)
            {
                // Create communication contact save parameter.
                CL2_Contact.Complex.Manipulation.P_L2CN_SCCfPI_1409 commContactParameter = new CL2_Contact.Complex.Manipulation.P_L2CN_SCCfPI_1409();
                // Set person ref id.
                commContactParameter.PersonInfo_RefID = personInfo.CMN_PER_PersonInfoID;
                // Find CommunicationContactID in existig list and set parameter.
                commContactParameter.CMN_PER_CommunicationContactID = communicationContacts
                                                                      .Where(x => x.Contact_Type == ctype.CMN_PER_CommunicationContact_TypeID)
                                                                      .Select(x => x.CMN_PER_CommunicationContactID).SingleOrDefault();
                // Set contact type parameter.
                commContactParameter.ContactType = ctype.Type;
                // Set contact value for specified content type.
                if (ctype.Type == DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(CL2_Contact.DomainManagement.EComunactionContactType.Phone))
                {
                    commContactParameter.ContactValue = Parameter.Phone;
                }
                else if (ctype.Type == DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(CL2_Contact.DomainManagement.EComunactionContactType.Email))
                {
                    commContactParameter.ContactValue = Parameter.Email;
                }
                else if (ctype.Type == DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(CL2_Contact.DomainManagement.EComunactionContactType.Fax))
                {
                    commContactParameter.ContactValue = Parameter.Fax;
                }
                else
                {
                    continue;  // not saving, because tenant don't have this contact type.
                }
                CL2_Contact.Complex.Manipulation.cls_Save_ComunicationContact_for_PersonInfoID.Invoke(Connection, Transaction, commContactParameter, securityTicket);
            }
            #endregion
            associatedBusinessParticipant.Save(Connection, Transaction);
            personInfo.Save(Connection, Transaction);
            return(new FR_Guid(item.Save(Connection, Transaction), item.CMN_BPT_BusinessParticipantID));

            #endregion UserCode
        }
        ///<summary>
        /// Method Invocation of wrapper classes
        ///<summary>
        protected static FR_Guid Invoke(DbConnection Connection, DbTransaction Transaction, string ConnectionString, P_L5AAS_SSPCI_0832 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            bool cleanupConnection  = Connection == null;
            bool cleanupTransaction = Transaction == null;

            FR_Guid functionReturn = new FR_Guid();

            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_Supplier_Contact_Person_Info", ex);
            }
            return(functionReturn);
        }
 ///<summary>
 /// Opens the connection/transaction for the given connectionString, and closes them when complete
 ///<summary>
 public static FR_Guid Invoke(string ConnectionString, P_L5AAS_SSPCI_0832 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
 {
     return(Invoke(null, null, ConnectionString, Parameter, securityTicket));
 }
 ///<summary>
 /// Invokes the method for the given Connection, and Transaction, leaving them open/not commited if no exceptions occured
 ///<summary>
 public static FR_Guid Invoke(DbConnection Connection, DbTransaction Transaction, P_L5AAS_SSPCI_0832 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
 {
     return(Invoke(Connection, Transaction, null, Parameter, securityTicket));
 }
Ejemplo n.º 5
0
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L5AAS_SS_1355 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Guid();

            var item = new CL1_CMN_BPT.ORM_CMN_BPT_Supplier();
            var businessParticipant = new CL1_CMN_BPT.ORM_CMN_BPT_BusinessParticipant();
            var companyInfo         = new CL1_CMN_COM.ORM_CMN_COM_CompanyInfo();
            var universalContact    = new CL1_CMN.ORM_CMN_UniversalContactDetail();
            var discountValue       = new CL1_CMN_BPT.ORM_CMN_BPT_Supplier_DiscountValue();
            var cashDiscountValue   = new CL1_CMN_BPT.ORM_CMN_BPT_Supplier_DiscountValue();


            var discountQuery = new CL1_ORD_PRC.ORM_ORD_PRC_DiscountType.Query();
            discountQuery.Tenant_RefID             = securityTicket.TenantID;
            discountQuery.GlobalPropertyMatchingID = EnumUtils.GetEnumDescription(EDiscountType.MainDiscount);
            var discount = CL1_ORD_PRC.ORM_ORD_PRC_DiscountType.Query.Search(Connection, Transaction, discountQuery).Single();

            var scontoQuery = new CL1_ORD_PRC.ORM_ORD_PRC_DiscountType.Query();
            scontoQuery.Tenant_RefID             = securityTicket.TenantID;
            scontoQuery.GlobalPropertyMatchingID = EnumUtils.GetEnumDescription(EDiscountType.CashDiscount);
            var sconto = CL1_ORD_PRC.ORM_ORD_PRC_DiscountType.Query.Search(Connection, Transaction, scontoQuery).SingleOrDefault();



            if (Parameter.CMN_BPT_SupplierID != Guid.Empty)
            {
                var result = item.Load(Connection, Transaction, Parameter.CMN_BPT_SupplierID);
                if (item.Ext_BusinessParticipant_RefID != Guid.Empty)
                {
                    businessParticipant.Load(Connection, Transaction, item.Ext_BusinessParticipant_RefID);
                }
                if (businessParticipant.IfCompany_CMN_COM_CompanyInfo_RefID != Guid.Empty)
                {
                    companyInfo.Load(Connection, Transaction, businessParticipant.IfCompany_CMN_COM_CompanyInfo_RefID);
                }
                if (companyInfo.Contact_UCD_RefID != Guid.Empty)
                {
                    universalContact.Load(Connection, Transaction, companyInfo.Contact_UCD_RefID);
                }

                var discountValueQuery = new CL1_CMN_BPT.ORM_CMN_BPT_Supplier_DiscountValue.Query();
                discountValueQuery.Supplier_RefID             = Parameter.CMN_BPT_SupplierID;
                discountValueQuery.Tenant_RefID               = securityTicket.TenantID;
                discountValueQuery.ORD_PRC_DiscountType_RefID = discount.ORD_PRC_DiscountTypeID;
                var foundDiscountValue = CL1_CMN_BPT.ORM_CMN_BPT_Supplier_DiscountValue.Query.Search(Connection, Transaction, discountValueQuery);

                if (foundDiscountValue.Count == 1)
                {
                    discountValue = foundDiscountValue.Single();
                }

                var scontoValueQuery = new CL1_CMN_BPT.ORM_CMN_BPT_Supplier_DiscountValue.Query();
                scontoValueQuery.Supplier_RefID             = Parameter.CMN_BPT_SupplierID;
                scontoValueQuery.Tenant_RefID               = securityTicket.TenantID;
                scontoValueQuery.ORD_PRC_DiscountType_RefID = sconto.ORD_PRC_DiscountTypeID;
                var foundCashValue = CL1_CMN_BPT.ORM_CMN_BPT_Supplier_DiscountValue.Query.Search(Connection, Transaction, scontoValueQuery);

                if (foundCashValue.Count == 1)
                {
                    cashDiscountValue = foundCashValue.Single();
                }
            }

            if (Parameter.IsDeleted == true)
            {
                item.IsDeleted = true;
                businessParticipant.IsDeleted = true;
                companyInfo.IsDeleted         = true;
                universalContact.IsDeleted    = true;

                #region Delete company info address
                P_L5AAS_GAfCI_1526 addressParam = new P_L5AAS_GAfCI_1526();
                addressParam.CompanyInfoID = companyInfo.CMN_COM_CompanyInfoID;
                var addresses = cls_Get_Addresses_for_CompanyInfoID.Invoke(Connection, Transaction, addressParam, securityTicket).Result;
                foreach (var a in addresses)
                {
                    P_L5AAS_SSA_1528 addressSaveParameter = new P_L5AAS_SSA_1528();
                    addressSaveParameter.CMN_COM_CompanyInfo_AddressID = a.CMN_COM_CompanyInfo_AddressID;
                    addressSaveParameter.CompanyInfo_RefID             = a.CompanyInfo_RefID;
                    addressSaveParameter.IsDeleted = true;
                    cls_Save_Supplier_Address.Invoke(Connection, Transaction, addressSaveParameter, securityTicket);
                }
                #endregion

                #region Delete Contact Persons
                P_L5AAS_GCPIfBP_1607 contactPersonParam = new P_L5AAS_GCPIfBP_1607();
                contactPersonParam.BusinessParticipantID = businessParticipant.CMN_BPT_BusinessParticipantID;
                var contactPersons = cls_Get_Contact_Person_Info_for_BusinessParticipantID.Invoke(Connection, Transaction, contactPersonParam, securityTicket).Result;
                foreach (var c in contactPersons)
                {
                    P_L5AAS_SSPCI_0832 contactPersonSaveParam = new P_L5AAS_SSPCI_0832();
                    contactPersonSaveParam.AssociatedBusinessParticipant_RefID = c.AssociatedBusinessParticipant_RefID;
                    contactPersonSaveParam.CMN_AddressID = c.CMN_AddressID;
                    contactPersonSaveParam.CMN_BPT_BusinessParticipantID = c.CMN_BPT_BusinessParticipantID;
                    cls_Save_Supplier_Contact_Person_Info.Invoke(Connection, Transaction, contactPersonSaveParam, securityTicket);
                }
                #endregion

                businessParticipant.Save(Connection, Transaction);
                companyInfo.Save(Connection, Transaction);
                universalContact.Save(Connection, Transaction);
                return(new FR_Guid(item.Save(Connection, Transaction), item.CMN_BPT_SupplierID));
            }

            //Creation specific parameters (Tenant, Account ... )
            if (Parameter.CMN_BPT_SupplierID == Guid.Empty)
            {
                item.Tenant_RefID = securityTicket.TenantID;
                businessParticipant.Tenant_RefID = securityTicket.TenantID;
                companyInfo.Tenant_RefID         = securityTicket.TenantID;
                universalContact.Tenant_RefID    = securityTicket.TenantID;
            }

            item.SupplierType_RefID                 = Parameter.CMN_BPT_Supplier_TypeID;
            item.Ext_BusinessParticipant_RefID      = businessParticipant.CMN_BPT_BusinessParticipantID;
            item.ExternalSupplierProvidedIdentifier = Parameter.SupplierProvidedExternalIdentifier;

            businessParticipant.DisplayName           = Parameter.DisplayName;
            businessParticipant.IsCompany             = true;
            businessParticipant.DefaultCurrency_RefID = Parameter.DefaultCurrency_RefID;
            businessParticipant.DefaultLanguage_RefID = Parameter.DefaultLanguage_RefID;
            businessParticipant.IfCompany_CMN_COM_CompanyInfo_RefID = companyInfo.CMN_COM_CompanyInfoID;

            companyInfo.Contact_UCD_RefID = universalContact.CMN_UniversalContactDetailID;
            companyInfo.CompanyInfo_EstablishmentNumber = Parameter.CompanyInfo_EstablishmentNumber;
            companyInfo.AnnualRevenueAmountValue_RefID  = Parameter.AnnualRevenueAmountValue_RefID;
            companyInfo.VATIdentificationNumber         = Parameter.VATIdentificationNumber;

            universalContact.CompanyName_Line1     = Parameter.CompanyName_Line1;
            universalContact.Country_639_1_ISOCode = Parameter.Country_639_1_ISOCode;

            if (businessParticipant.CMN_BPT_BusinessParticipantID != Guid.Empty)
            {
                businessParticipant.Save(Connection, Transaction);
            }

            if (companyInfo.CMN_COM_CompanyInfoID != Guid.Empty)
            {
                companyInfo.Save(Connection, Transaction);
            }

            if (universalContact.CMN_UniversalContactDetailID != Guid.Empty)
            {
                universalContact.Save(Connection, Transaction);
            }

            Guid savedSupplier = new FR_Guid(item.Save(Connection, Transaction), item.CMN_BPT_SupplierID).Result;

            #region DiscountValue


            if (discountValue.Tenant_RefID == Guid.Empty)
            {
                discountValue.CMN_BPT_Supplier_DiscountValueID = Guid.NewGuid();
                discountValue.Supplier_RefID             = savedSupplier;
                discountValue.ORD_PRC_DiscountType_RefID = discount.ORD_PRC_DiscountTypeID;
                discountValue.Tenant_RefID = securityTicket.TenantID;
            }

            discountValue.DiscountValue_in_percent = Parameter.DiscountValue;
            discountValue.Save(Connection, Transaction);


            if (cashDiscountValue.Tenant_RefID == Guid.Empty)
            {
                cashDiscountValue.CMN_BPT_Supplier_DiscountValueID = Guid.NewGuid();
                cashDiscountValue.Supplier_RefID             = savedSupplier;
                cashDiscountValue.ORD_PRC_DiscountType_RefID = sconto.ORD_PRC_DiscountTypeID;
                cashDiscountValue.Tenant_RefID = securityTicket.TenantID;
            }

            cashDiscountValue.DiscountValue_in_percent = Parameter.CashDiscountValue;
            cashDiscountValue.Save(Connection, Transaction);

            #endregion

            returnValue.Result = savedSupplier;
            return(returnValue);

            #endregion UserCode
        }