Ejemplo n.º 1
0
        protected void AccountContactsList_Sorting(object sender, GridViewSortEventArgs e)
        {
            SortDirection = e.SortExpression == SortExpression
                                ? (SortDirection == "ASC" ? "DESC" : "ASC")
                                : "ASC";

            SortExpression = e.SortExpression;

            _contacts.DefaultView.Sort = string.Format("{0} {1}", SortExpression, SortDirection);

            AccountContactsList.DataSource = _contacts;
            AccountContactsList.DataBind();
        }
Ejemplo n.º 2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            RedirectToLoginIfAnonymous();

            if (AccountToEdit == null)
            {
                RecordNotFoundError.Visible = true;

                EditAccountForm.Visible = false;

                return;
            }

            var formViewDataSource = new CrmDataSource {
                ID = "WebFormDataSource", CrmDataContextName = FormView.ContextName
            };
            var managingPartnerAccount = ServiceContext.CreateQuery("account").FirstOrDefault(a => a.GetAttributeValue <Guid>("accountid") == (AccountToEdit.GetAttributeValue <EntityReference>("msa_managingpartnerid") == null ? Guid.Empty : AccountToEdit.GetAttributeValue <EntityReference>("msa_managingpartnerid").Id));
            var channelPermission      = ServiceContext.GetChannelAccessByContact(Contact);
            var channelCreateAccess    = (channelPermission != null && channelPermission.GetAttributeValue <bool?>("adx_create").GetValueOrDefault(false));
            var channelWriteAccess     = (channelPermission != null && channelPermission.GetAttributeValue <bool?>("adx_write").GetValueOrDefault(false));
            var channelPermissionForParentAccountExists = managingPartnerAccount != null && channelPermission != null && channelPermission.GetAttributeValue <EntityReference>("adx_accountid") != null && channelPermission.GetAttributeValue <EntityReference>("adx_accountid").Equals(managingPartnerAccount.ToEntityReference());
            var validAcccountClassificationCode         = managingPartnerAccount != null && managingPartnerAccount.GetAttributeValue <OptionSetValue>("accountclassificationcode") != null && managingPartnerAccount.GetAttributeValue <OptionSetValue>("accountclassificationcode").Value == (int)Enums.AccountClassificationCode.Partner;

            if (channelPermission == null)
            {
                NoChannelPermissionsRecordError.Visible = true;

                EditAccountForm.Visible = false;

                ContactsList.Visible = false;

                return;
            }

            if (!channelWriteAccess)
            {
                ChannelPermissionsError.Visible = true;
            }
            else
            {
                if (managingPartnerAccount == null)
                {
                    NoParentAccountError.Visible = true;
                }
                else if (!validAcccountClassificationCode)
                {
                    ParentAccountClassificationCodeError.Visible = true;
                }
                else if (!channelPermissionForParentAccountExists)
                {
                    NoChannelPermissionsForParentAccountError.Visible = true;
                }
            }

            if (!channelWriteAccess || managingPartnerAccount == null || !channelPermissionForParentAccountExists || !validAcccountClassificationCode)
            {
                EditAccountForm.Visible = false;

                ContactsList.Visible = false;

                return;
            }

            CreateContactButton.Visible = channelCreateAccess;

            CreateContactButton.QueryStringCollection = CreateCustomerContactQueryStringCollection();

            var fetchXml = string.Format("<fetch mapping='logical'><entity name='{0}'><all-attributes /><filter type='and'><condition attribute = '{1}' operator='eq' value='{{{2}}}'/></filter></entity></fetch>", "account", "accountid", AccountToEdit.GetAttributeValue <Guid>("accountid"));

            formViewDataSource.FetchXml = fetchXml;

            AccountForm.Controls.Add(formViewDataSource);

            FormView.DataSourceID = "WebFormDataSource";

            if (!IsPostBack)
            {
                BindPrimaryContact();

                var contacts = ServiceContext.CreateQuery("contact").Where(c => c.GetAttributeValue <EntityReference>("parentcustomerid") != null && c.GetAttributeValue <EntityReference>("parentcustomerid").Equals(AccountToEdit.ToEntityReference()));

                _contacts = EnumerableExtensions.CopyToDataTable(contacts.Select(c => new
                {
                    contactid   = c.GetAttributeValue <Guid>("contactid"),
                    ID          = c.GetAttributeValue <string>("fullname"),
                    CompanyName = c.GetRelatedEntity(ServiceContext, new Relationship("contact_customer_accounts")) == null ? string.Empty : c.GetRelatedEntity(ServiceContext, new Relationship("contact_customer_accounts")).GetAttributeValue <string>("name"),
                    City        = c.GetAttributeValue <string>("address1_city"),
                    State       = c.GetAttributeValue <string>("address1_stateorprovince"),
                    Phone       = c.GetAttributeValue <string>("address1_telephone1"),
                    Email       = c.GetAttributeValue <string>("emailaddress1"),
                }).ToList().OrderBy(c => c.CompanyName));

                _contacts.Columns["City"].ColumnName  = "City";
                _contacts.Columns["State"].ColumnName = "State";
                _contacts.Columns["Phone"].ColumnName = "Phone";
                _contacts.Columns["Email"].ColumnName = "E-mail Address";

                AccountContactsList.DataKeyNames = new[] { "contactid" };
                AccountContactsList.DataSource   = _contacts;
                AccountContactsList.DataBind();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            RedirectToLoginIfAnonymous();

            var parentAccount            = Contact.GetAttributeValue <EntityReference>("parentcustomerid") == null ? null : ServiceContext.CreateQuery("account").FirstOrDefault(a => a.GetAttributeValue <Guid>("accountid") == Contact.GetAttributeValue <EntityReference>("parentcustomerid").Id);
            var accountAccessPermissions = parentAccount == null ? new List <Entity>() : ServiceContext.GetAccountAccessByContact(Contact).ToList();
            var accountAccessPermissionsForParentAccount = parentAccount == null ? new List <Entity>() : accountAccessPermissions.Where(a => a.GetAttributeValue <EntityReference>("adx_accountid") != null && a.GetAttributeValue <EntityReference>("adx_accountid").Equals(parentAccount.ToEntityReference())).ToList();
            var contactAccessPermissions = parentAccount == null ? new List <Entity>() : ServiceContext.GetContactAccessByContact(Contact).ToList();
            var contactAccessPermissionsForParentAccount = parentAccount == null ? new List <Entity>() : contactAccessPermissions.Where(c => c.GetAttributeValue <EntityReference>("adx_accountid") != null && c.GetAttributeValue <EntityReference>("adx_accountid").Equals(parentAccount.ToEntityReference()) && c.GetAttributeValue <OptionSetValue>("adx_scope").Value == (int)Enums.ContactAccessScope.Account).ToList();
            var canReadAccount    = false;
            var canEditAccount    = false;
            var canCreateContacts = false;
            var canReadContacts   = false;

            if (parentAccount == null)
            {
                AccountInformation.Visible = false;

                ContactsList.Visible = false;

                NoParentAccountError.Visible = true;

                return;
            }

            if (!accountAccessPermissions.Any())
            {
                AccountInformation.Visible = false;

                ContactsList.Visible = false;

                NoAccountAccessPermissionsRecordError.Visible = true;

                return;
            }

            if (!accountAccessPermissionsForParentAccount.Any())
            {
                AccountInformation.Visible = false;

                ContactsList.Visible = false;

                NoAccountAccessPermissionsForParentAccountError.Visible = true;

                return;
            }

            foreach (var access in accountAccessPermissionsForParentAccount)
            {
                if (access.GetAttributeValue <bool?>("adx_write").GetValueOrDefault(false))
                {
                    canEditAccount = true;
                    canReadAccount = true;
                }
                if (access.GetAttributeValue <bool?>("adx_read").GetValueOrDefault(false))
                {
                    canReadAccount = true;
                }
            }

            foreach (var access in contactAccessPermissionsForParentAccount)
            {
                if (access.GetAttributeValue <bool?>("adx_create").GetValueOrDefault(false))
                {
                    canCreateContacts = true;
                    canReadContacts   = true;
                }
                if (access.GetAttributeValue <bool?>("adx_read").GetValueOrDefault(false))
                {
                    canReadContacts = true;
                }
            }

            if (!contactAccessPermissions.Any())
            {
                NoContactAccessPermissionsRecordMessage.Visible = true;
            }
            else
            {
                if (!contactAccessPermissionsForParentAccount.Any())
                {
                    NoContactAccessPermissionsForParentAccountMessage.Visible = true;
                }
            }

            if (!canReadAccount)
            {
                AccountInformation.Visible = false;

                ContactsList.Visible = false;

                return;
            }

            if (!canEditAccount)
            {
                AccountEditForm.Visible = false;

                AccountReadOnlyForm.Visible = true;

                AccountAccessWritePermissionDeniedMessage.Visible = true;
            }

            if (!canReadContacts)
            {
                AccountContactsList.Visible = false;

                if (contactAccessPermissions.Any() && contactAccessPermissionsForParentAccount.Any())
                {
                    ContactAccessPermissionsMessage.Visible = true;
                }
            }

            CreateContactButton.Visible = canCreateContacts;

            var formViewDataSource = new CrmDataSource {
                ID = "WebFormDataSource", CrmDataContextName = AccountEditFormView.ContextName
            };

            var fetchXml = string.Format("<fetch mapping='logical'><entity name='{0}'><all-attributes /><filter type='and'><condition attribute = '{1}' operator='eq' value='{{{2}}}'/></filter></entity></fetch>", "account", "accountid", parentAccount.GetAttributeValue <Guid>("accountid"));

            formViewDataSource.FetchXml = fetchXml;

            AccountInformation.Controls.Add(formViewDataSource);

            AccountEditFormView.DataSourceID = "WebFormDataSource";

            AccountReadOnlyFormView.DataSourceID = "WebFormDataSource";

            var contacts = ServiceContext.GetContactsForContact(Contact)
                           .Where(c => c.GetAttributeValue <EntityReference>("parentcustomerid") != null && c.GetAttributeValue <EntityReference>("parentcustomerid").Equals(parentAccount.ToEntityReference()));

            _contacts = EnumerableExtensions.CopyToDataTable(contacts.Select(c => new
            {
                contactid   = c.GetAttributeValue <Guid>("contactid"),
                ID          = c.GetAttributeValue <string>("fullname"),
                CompanyName = c.GetRelatedEntity(ServiceContext, new Relationship("contact_customer_accounts")) == null ? string.Empty : c.GetRelatedEntity(ServiceContext, new Relationship("contact_customer_accounts")).GetAttributeValue <string>("name"),
                City        = c.GetAttributeValue <string>("address1_city"),
                State       = c.GetAttributeValue <string>("address1_stateorprovince"),
                Phone       = c.GetAttributeValue <string>("address1_telephone1"),
                Email       = c.GetAttributeValue <string>("emailaddress1"),
            }).OrderBy(opp => opp.CompanyName));

            _contacts.Columns["City"].ColumnName  = "City";
            _contacts.Columns["State"].ColumnName = "State";
            _contacts.Columns["Phone"].ColumnName = "Phone";
            _contacts.Columns["Email"].ColumnName = "E-mail Address";

            AccountContactsList.DataKeyNames = new[] { "contactid" };
            AccountContactsList.DataSource   = _contacts;
            AccountContactsList.DataBind();
        }