public ClientResponse FindCustomers(string q, int limit)
 {
     return new ClientResponse(() =>
     {
         using (var manager = new CustomerManager(null))
             return manager.SearchCustomers(Company.CompanyId, q, limit).ToArray();
     });
 }
        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 txtCallNumber_Changed(object sender, EventArgs e)
        {
            pnlCustomerName.Visible = pnlRepresentantName.Visible = pnlTechnicalEmployeeName.Visible = false;

            if (!String.IsNullOrEmpty(txtCallNumber.Text))
            {
                var customerCall = new CustomerManager(this).GetCustomerCall(txtCallNumber.Text);

                if (customerCall != null)
                    ShowCustomerCallInformations(customerCall);
                else
                    Page.ViewState["customerCallId"] = null;
            }
        }
        private Boletos ConvertInvoiceParcelsInBoleto(Int32 companyId, Int32 accountId, DateTime beginDate,
                                                      DateTime endDate)
        {
            var customerManager = new CustomerManager(this);
            var profileManager = new ProfileManager(this);
            var companyManager = new CompanyManager(this);
            var accountManager = new AccountManager(this);
            Boleto boleto;
            Sacado sacado;
            Endereco endereco;
            var address = new Address();
            Company company = companyManager.GetCompany(companyId);
            Account account = accountManager.GetAccount(accountId, companyId);
            var boletos = new Boletos();

            var cedente = new Cedente(company.LegalEntityProfile.CNPJ, company.LegalEntityProfile.CompanyName,
                                      account.Agency, Convert.ToString(account.AgencyDigit), account.AccountNumber,
                                      Convert.ToString(account.AccountNumberDigit));


            foreach (Parcel parcel in GetOpenInvoiceParcelInPeriodByAccount(companyId, accountId, beginDate, endDate))
            {
                endereco = new Endereco();

                if (parcel.Invoice.Customer.LegalEntityProfileId.HasValue)
                {
                    //Address
                    address = parcel.Invoice.Customer.LegalEntityProfile.Address;

                    endereco.Numero = parcel.Invoice.Customer.LegalEntityProfile.AddressNumber;
                    endereco.Complemento = parcel.Invoice.Customer.LegalEntityProfile.AddressComp;

                    //sacado
                    sacado = new Sacado(parcel.Invoice.Customer.LegalEntityProfile.CNPJ,
                                        parcel.Invoice.Customer.LegalEntityProfile.CompanyName);
                }
                else
                {
                    //Address
                    address = parcel.Invoice.Customer.Profile.Address;

                    endereco.Numero = parcel.Invoice.Customer.Profile.AddressNumber;
                    endereco.Complemento = parcel.Invoice.Customer.Profile.AddressComp;

                    //sacado
                    sacado = new Sacado(parcel.Invoice.Customer.Profile.CPF, parcel.Invoice.Customer.Profile.Name);
                }

                //Address
                endereco.Bairro = address.Neighborhood;
                endereco.CEP = address.PostalCode;
                endereco.Cidade = address.City ?? String.Empty;
                endereco.Logradouro = address.Name;
                endereco.UF = address.State;

                //boleto
                boleto = new Boleto(parcel.DueDate, Convert.ToDouble(parcel.Amount), String.Empty, String.Empty, cedente);

                sacado.Endereco = endereco;

                boleto.Sacado = sacado;

                var instrucao = new Instrucao(Convert.ToInt32(account.Bank.BankNumber));

                var banco = new Banco(Convert.ToInt32(account.Bank.BankNumber));

                instrucao.Banco = banco;
                instrucao.QuantidadeDias = 0;
                instrucao.Descricao = String.Empty;
                instrucao.Codigo = 0;

                boleto.CodigoBarra.LinhaDigitavel = String.Empty;
                boleto.DataDocumento = DateTime.Now;
                boleto.DataVencimento = parcel.DueDate;
                boleto.ValorDesconto = 0;

                boleto.Instrucoes = new List<IInstrucao>();
                boleto.Instrucoes.Add(instrucao);
                boletos.Add(boleto);
            }
            return boletos;
        }
 /// <summary>
 /// this method save a sale item as customer Equipment
 /// </summary>
 /// <param name="saleItem"></param>
 private void SaveSaleItemAsCustomerEquipment(SaleItem saleItem)
 {
     var customerManager = new CustomerManager(this);
     var customerEquipment = new CustomerEquipment();
     if (saleItem.Sale.CustomerId.HasValue && saleItem.ProductId.HasValue &&
         saleItem.Product.AddCustomerEquipmentInSale == true)
     {
         customerEquipment.CustomerId = saleItem.Sale.CustomerId.Value;
         customerEquipment.CompanyId = saleItem.CompanyId;
         customerEquipment.SerialNumber = saleItem.SerialNumber;
         customerEquipment.Name = saleItem.Product.Name;
         if (saleItem.Product.Manufacturer != null)
             customerEquipment.Manufacturer = saleItem.Product.Manufacturer.Name;
         customerManager.InsertCustomerEquipment(customerEquipment);
     }
 }