public override InfoControl.Web.Auditing.Event LogErrorInDatabase(Exception ex)
        {

            var eventEntity = base.LogErrorInDatabase(ex);
            HttpContext context = HttpContext.Current;

            if (context != null && context.Session != null)
                using (var customerManager = new CustomerManager(null))
                using (var companyManager = new CompanyManager(null))
                using (var membershipManager = new MembershipManager(null))
                {
                    Company hostCompany = companyManager.GetHostCompany();
                    Company company = companyManager.GetCompanyByContext(context);

                    Customer customer = customerManager.GetCustomerByLegalEntityProfile(hostCompany.CompanyId, company.LegalEntityProfileId);

                    if (customer != null)
                    {
                        var customerCall = new CustomerCall();
                        customerCall.EventId = eventEntity.EventId;

                        customerCall.Subject = (ex.Message.Length > 100 ? ex.Message.Substring(0, 90) + " ..." : ex.Message);

                        customerCall.CallNumber = Util.GenerateUniqueID();
                        customerCall.CompanyId = hostCompany.CompanyId;
                        customerCall.CustomerId = customer.CustomerId;
                        customerCall.OpenedDate = customerCall.ModifiedDate = DateTime.Now.Date;
                        customerCall.Description = String.Empty;

                        customerCall.CustomerCallTypeId = CustomerCallType.ERROR;
                        customerCall.CustomerCallStatusId = CustomerCallStatus.New;
                        customerCall.Rating = 5;

                        customerCall.CallNumberAssociated = context.Request.RawUrl;
                        customerCall.Sector = Convert.ToString(context.Session["_lastPageTitle"]);

                        if (context.User != null)
                            if (context.User.Identity != null)
                                if (context.User.Identity.IsAuthenticated)
                                {
                                    User user = membershipManager.GetUserByEmail(context.User.Identity.Name);
                                    if (user != null)
                                        customerCall.UserId = user.UserId;
                                }

                        customerManager.InsertCustomerCall(customerCall, null, null, null);
                    }
                }
            return eventEntity;
        }
        /// <summary>
        /// Method to add a new COMPANY in a CUSTOMER of the HOST COMPANY
        /// </summary>
        /// <param name="newCompany"></param>
        private Int32 AddCompanyAsCustomer(Company newCompany)
        {
            var customerManager = new CustomerManager(this);

            var customer = new Customer();
            customer.CompanyId = GetHostCompany().CompanyId;
            customer.LegalEntityProfileId = newCompany.LegalEntityProfileId;
            if (customerManager.ExistCustomer(customer))
                customer = customerManager.GetCustomerByLegalEntityProfile(customer.CompanyId,
                                                                           Convert.ToInt32(customer.LegalEntityProfileId));
            else
                customerManager.Insert(customer);
            return customer.CustomerId;
        }
 private Invoice ConvertStatementToInvoice(Company company, Statement statement)
 {
     var customerManager = new CustomerManager(this);
     return new Invoice()
     {
         CompanyId = GetHostCompany().CompanyId,
         CustomerId = customerManager.GetCustomerByLegalEntityProfile(GetHostCompany().CompanyId, company.LegalEntityProfileId).CustomerId,
         EntryDate = DateTime.Now,
         Description = "Manutenção InfoControl"
     };
 }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(Request["w"]))
            btnCancel.OnClientClick = "top.$.LightBoxObject.close();return false;";
        else
        {
            btnCancel.OnClientClick = "location='Customers.aspx';return false;";

            if (!String.IsNullOrEmpty(Request["CustomerId"]))
                btnCancel.OnClientClick = "parent.location='Customers.aspx';return false;";
        }

        customerManager = new CustomerManager(this);
        lblMessage.Text = String.Empty;

        if (!String.IsNullOrEmpty(Request["CustomerId"]))
            Page.ViewState["CustomerId"] = Request["CustomerId"];

        //
        // Update
        //
        if (Page.ViewState["CustomerId"] != null)
        {
            litTitle.Visible = false;
            Page.ViewState["loaded"] = true;
            originalCustomer = customerManager.GetCustomer(Convert.ToInt32(Page.ViewState["CustomerId"]), Company.MatrixId.Value);

            if (!IsPostBack && originalCustomer != null)
            {
                customerComments.SubjectId = originalCustomer.CustomerId;
                customerComments.Visible = true;
                pnlCreateDate.Visible = true;

                if (originalCustomer.User != null)
                {
                    txtUserName.Text = originalCustomer.User.UserName;
                    txtPassword.Text = originalCustomer.User.Password;
                    lblPassword.Visible = false;
                    txtPassword.Visible = false;
                    chkRemoveUser.Visible = true;
                }

                //
                // The code below checks the type of profile(LegalEntityProfile/Profile)
                //
                if (originalCustomer.LegalEntityProfile != null)
                    ucProfile.CompanyProfileEntity = originalCustomer.LegalEntityProfile;
                else
                    ucProfile.ProfileEntity = originalCustomer.Profile;

                pnlOtherData.Visible = true;

                ucCurrFieldCreditLimit.CurrencyValue = originalCustomer.CreditLimit;

                if (originalCustomer.RepresentantId.HasValue)
                    cboRepresentant.SelectedValue = Convert.ToString(originalCustomer.RepresentantId);

                if (originalCustomer.CreatedDate.HasValue)
                    lblCreatedDate.Text = originalCustomer.CreatedDate.Value.Date.ToShortDateString();

                if (originalCustomer.CustomerTypeId.HasValue)
                    cboCustomerType.SelectedValue = originalCustomer.CustomerTypeId.ToString();


                ShowCustomerConfiguration(originalCustomer);
                //
                //load ranking value
                //
                if (originalCustomer.Ranking != null)
                    rtnRanking.CurrentRating = Convert.ToInt32(originalCustomer.Ranking);
            }
        }
        else
        {
            customerComments.Visible = false;

            //
            //  Legal Entity
            //
            if (Page.ViewState["LegalEntityProfileId"] != null)
            {
                originalCustomer = customerManager.GetCustomerByLegalEntityProfile(Company.MatrixId.Value, Convert.ToInt32(Page.ViewState["LegalEntityProfileId"]));
                if (originalCustomer != null)
                {
                    Page.ViewState["ProfileExists"] = "0";

                    /*
                     * if isn't a postback set the values of company in profile_LegalEntity1
                     * else the values are reload in all postback
                     * 
                     */
                    if (!IsPostBack)
                    {
                        //SetVendorValue(originalcustomer);
                        ucProfile.CompanyProfileEntity = originalCustomer.LegalEntityProfile;
                    }
                }
            }

            //
            // Natural Person
            //
            if (Page.ViewState["ProfileId"] != null)
            {
                originalCustomer = customerManager.GetCustomerByProfile(Company.MatrixId.Value, Convert.ToInt32(Page.ViewState["ProfileId"]));
                if (originalCustomer != null)
                {
                    Page.ViewState["ProfileExists"] = "0";
                    /*
                     * if isn't a postback set the values of company in profile
                     * else the values are reload in all postback
                     *
                     */
                    if (!IsPostBack)
                        ucProfile.ProfileEntity = originalCustomer.Profile;

                }
            }
        }
    }