/// <summary>
 /// This method return true if the LegalEntity exists
 /// </summary>
 /// <param name="cnpj"></param>
 /// <returns></returns>
 public bool LegalEntityExists(string cnpj)
 {
     LegalEntityProfile profile = new ProfileManager(this).GetLegalEntityProfile(cnpj);
     return profile != null && profile.CNPJ != "99.999.999/0001-91";
 }
Exemple #2
0
        /// <summary>
        /// This method return true if the LegalEntity exists
        /// </summary>
        /// <param name="cnpj"></param>
        /// <returns></returns>
        public bool LegalEntityExists(string cnpj)
        {
            LegalEntityProfile profile = new ProfileManager(this).GetLegalEntityProfile(cnpj);

            return(profile != null && profile.CNPJ != "99.999.999/0001-91");
        }
 /// <summary>
 /// This method return true if the profile exists
 /// </summary>
 /// <param name="cnpj"></param>
 /// <returns></returns>
 public bool ProfileExists(string cpf)
 {
     Profile profile = new ProfileManager(this).GetProfile(cpf);
     return profile != null;
 }
        /// <summary>
        /// this method add the administrator of new company as contact of host'customer
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="userId"></param>
        private void AddContactInHostCustomer(Int32 customerId, Int32 userId)
        {
            var contactManager = new ContactManager(this);
            var customerContactManager = new CustomerContactManager(this);
            var profileManager = new ProfileManager(this);
            Profile profile = profileManager.GetProfileByUser(userId);
            var contact = new Contact
                              {
                                  Name = profile.Name,
                                  Phone = profile.Phone,
                                  CellPhone = profile.CellPhone,
                                  Address = profile.Address,
                                  AddressComp = profile.AddressComp,
                                  AddressNumber = profile.AddressNumber,
                                  Email = profile.Email,
                                  PostalCode = profile.PostalCode
                              };

            contactManager.Insert(contact);

            var customerContact = new CustomerContact();
            customerContact.CompanyId = GetHostCompany().CompanyId;
            customerContact.ContactId = contact.ContactId;
            customerContact.CustomerId = customerId;
            customerContactManager.Insert(customerContact);
        }
Exemple #5
0
        /// <summary>
        /// This method return true if the profile exists
        /// </summary>
        /// <param name="cnpj"></param>
        /// <returns></returns>
        public bool ProfileExists(string cpf)
        {
            Profile profile = new ProfileManager(this).GetProfile(cpf);

            return(profile != null);
        }
        /// <summary>
        /// This method updates a user, profile and deposit
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="companyId"></param>
        /// <param name="depositId"></param>
        /// <param name="profile"></param>
        /// <param name="user"></param>
        public void UpdateUser(Int32 companyId, Int32 userId, Int32? depositId, Int32? representantId, Profile profile, User user)
        {
            //
            // Save Profile
            //
            var profileManager = new ProfileManager(this);

            profileManager.SaveProfile(profile);

            if (representantId.HasValue)
            {
                RemoveRepresentantFromUser(userId);
                AddRepresentantToUser(companyId, userId, (int)representantId);
            }
            else
                RemoveRepresentantFromUser(userId);

            AttachProfileEntityToUser(companyId, userId, profile.ProfileId);
            UpdateUser(userId, companyId, depositId, user);
        }
        /// <summary>
        /// Método usado para criar mais companias, referenciando as novas companias, � 1� compania
        /// Ou compania de referência
        /// </summary>
        /// <param name="company"></param>
        /// <param name="userId"></param>
        /// <param name="companyId"></param>
        /// <returns></returns>
        public InsertCompanyStatus InsertCompany(Company company, int userId, int matrixCompanyId)
        {
            if (GetCompany(company.LegalEntityProfile.CNPJ) != null)
                return InsertCompanyStatus.DuplicateCNPJ;

            var profileManager = new ProfileManager(this);
            LegalEntityProfile original_legalEntityProfile;

            //
            // update the legalEntityProfile
            //
            if (company.LegalEntityProfile != null)
            {
                original_legalEntityProfile = profileManager.GetLegalEntityProfile(company.LegalEntityProfile.CNPJ);
                if (original_legalEntityProfile != null)
                {
                    //update the legalEntityProfile
                    if (!String.IsNullOrEmpty(company.LegalEntityProfile.CompanyName))
                        original_legalEntityProfile.CompanyName = company.LegalEntityProfile.CompanyName;

                    if (!String.IsNullOrEmpty(company.LegalEntityProfile.FantasyName))
                        original_legalEntityProfile.FantasyName = company.LegalEntityProfile.FantasyName;

                    original_legalEntityProfile.Email = company.LegalEntityProfile.Email;
                    original_legalEntityProfile.IE = company.LegalEntityProfile.IE;
                    original_legalEntityProfile.Phone = company.LegalEntityProfile.Phone;
                    original_legalEntityProfile.Address = company.LegalEntityProfile.Address;
                    original_legalEntityProfile.AddressComp = company.LegalEntityProfile.AddressComp;
                    original_legalEntityProfile.AddressNumber = company.LegalEntityProfile.AddressNumber;
                    profileManager.DbContext.SubmitChanges();

                    company.LegalEntityProfile = original_legalEntityProfile;
                }
            }



            //
            //Method to insert a new company
            //
            company.CreatorUserId = userId;
            Insert(company);
            company.ReferenceCompanyId = company.CompanyId;
            company.MatrixId = company.CompanyId;
            if (matrixCompanyId != 0)
            {
                company.ReferenceCompanyId = GetCompany(matrixCompanyId).ReferenceCompanyId;
                company.MatrixId = matrixCompanyId;
            }
            DbContext.SubmitChanges();

            //
            //Method to create a Deposit
            //
            Deposit dep = CreateMatrixDeposit(company.CompanyId);

            //
            //method to create a new role to this company
            //
            Role role = CreateAdminRole(company.CompanyId);

            //
            //method to set all permissions writable to the admin
            //
            var pManager = new PlanManager(this);
            Plan plan = pManager.GetCurrentPlan(company.CompanyId);
            if (plan != null)
            {
                CreatePermissions(plan.PlanId, role.RoleId, company.CompanyId);
            }

            //
            //method to associate the user with the company
            //
            AssociateUser(company.CompanyId, userId, dep.DepositId, /*IsMain*/ true);

            //
            //method to associate the user with the role
            //
            AddUserInRoles(userId, company.CompanyId, role.RoleId);

            //
            // method to create a customer under the Host "Vivina", with the company data of the user
            //
            AddContactInHostCustomer(AddCompanyAsCustomer(company), userId);

            //
            // Configurate the company inserting template of the Best Pratices, Report, etc.
            //
            SetCompanyConfiguration(company);

            //
            // Configure home page
            //
            CreateHomePage(company, userId);

            //SetCompanyPaymentMethods(company);

            return InsertCompanyStatus.Success;
        }
        /// <summary>
        /// M�todo usado para criar a primeira companhia, utilizado na tela de registro
        /// </summary>
        /// <param name="newCompany"></param>
        /// <param name="newUser"></param>
        /// <param name="newProfile"></param>
        /// <returns></returns>
        public InsertCompanyStatus InsertMatrixCompany(Company newCompany, User newUser, Profile profile)
        {
            //
            // Insert the profile
            //
            var pManager = new ProfileManager(this);

            // Profile profile = pManager.GetProfile(newProfile.CPF) ?? newProfile;
            //if (profile.ProfileId == 0)
            pManager.Insert(profile);
            //else
            //{
            //    profile.Name = newProfile.Name;
            //    profile.Email = newProfile.Email;
            //    profile.PostalCode = newProfile.PostalCode;
            //    //profile.Address = newProfile.Address;
            //    profile.AddressComp = newProfile.AddressComp;
            //    profile.AddressNumber = newProfile.AddressNumber;
            //    profile.Phone = newProfile.Phone;
            //    DbContext.SubmitChanges();
            //}

            //
            //Insert Admin user
            //
            Int32 UserId;
            //newUser.ProfileId
            DataClasses.User original_User = GetUserByUserName(newUser.Email);
            if (original_User != null)
            {
                UserId = original_User.UserId;
            }
            else
            {
                MembershipCreateStatus status;
                var membershipManager = new MembershipManager(this);
                newUser.ProfileId = profile.ProfileId;
                membershipManager.Insert(
                    newUser,
                    out status,
                    (Membership.Provider as VivinaMembershipProvider).RequiresValidEmail);

                //
                //verify if the status of the inclusion are ok
                //
                if (status != MembershipCreateStatus.Success)
                {
                    DataManager.Rollback();
                    switch (status)
                    {
                        case MembershipCreateStatus.DuplicateUserName:
                            return InsertCompanyStatus.DuplicatedUserName;
                        case MembershipCreateStatus.InvalidPassword:
                            return InsertCompanyStatus.InvalidPassword;
                        case MembershipCreateStatus.DuplicateEmail:
                            return InsertCompanyStatus.DuplicatedAdminEmail;
                        default:
                            return InsertCompanyStatus.InvalidUser;
                    }
                }
                UserId = newUser.UserId;
            }

            if (newCompany.LegalEntityProfile.IsLiberalProfessional)
                newCompany.LegalEntityProfile.CompanyName = newCompany.LegalEntityProfile.FantasyName = profile.Name;

            var insertCompanyStatus = InsertCompany(newCompany, UserId, 0);

            newCompany.ReferenceCompanyId = newCompany.CompanyId;
            newCompany.MatrixId = newCompany.CompanyId;
            DbContext.SubmitChanges();

            return insertCompanyStatus;
        }
        public InsertCompanyStatus InsertUser(int companyId, User user, Profile profile, int? depositId)
        {
            //
            // If ProfileId equal 0 then the profile no exists in bd
            //
            var pManager = new ProfileManager(this);
            if (profile.ProfileId == 0)
                pManager.Insert(profile);

            //
            //method to insert a new user
            //
            var provider = Membership.Provider as VivinaMembershipProvider;
            MembershipCreateStatus status;
            var membershipManager = new MembershipManager(this);
            user.ProfileId = profile.ProfileId;
            membershipManager.Insert(user, out status, provider.RequiresValidEmail);
            //
            //verify if the status of the inclusion is ok
            //
            if (status != MembershipCreateStatus.Success)
            {
                DataManager.Rollback();
                switch (status)
                {
                    case MembershipCreateStatus.InvalidPassword:
                        return InsertCompanyStatus.InvalidPassword;
                    case MembershipCreateStatus.DuplicateEmail:
                        return InsertCompanyStatus.DuplicatedAdminEmail;
                }
            }

            //
            // Associate the User with company
            //
            AssociateUser(companyId, user.UserId, depositId, true);

            //
            // Insert a Employee
            //
            var humanResourcesManager = new HumanResourcesManager(this);
            var employee = new Employee();
            employee.IsActive = true;
            employee.ProfileId = profile.ProfileId;
            employee.CompanyId = companyId;
            humanResourcesManager.InsertEmployee(employee, new List<EmployeeAdditionalInformation>());

            return InsertCompanyStatus.Success;
        }
 /// <summary>
 /// this method return true if the CompanyExists
 /// </summary>
 /// <param name="cnpj"></param>
 /// <returns></returns>
 public bool checkCompanyExists(string cnpj)
 {
     var profileManager = new ProfileManager(this);
     LegalEntityProfile legalEntityProfile;
     legalEntityProfile = profileManager.GetLegalEntityProfile(cnpj);
     if (legalEntityProfile != null && GetCompanyByProfile(legalEntityProfile.LegalEntityProfileId) != null)
         return true;
     else
         return false;
 }
        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;
        }
Exemple #12
0
        /// <summary>
        /// This method retrieves customers data from a excel file and stores in db
        /// </summary>
        /// <param name="companyId"></param>
        /// <param name="userId"></param>
        /// <param name="message"></param>
        public void ImportDataFromExcelFile(Int32 companyId, Int32 userId, string fileName, out string message)
        {
            ProfileManager profileManager;

            profileManager = new ProfileManager(this);

            using (var excelConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + fileName + @"; Extended Properties=Excel 5.0"))
            {
                var excelCommand = new OleDbCommand("Select * from [CLIENTES$]", excelConnection);

                excelConnection.Open();
                OleDbDataReader excelReader;
                try
                {
                    excelReader = excelCommand.ExecuteReader();
                }
                catch (OleDbException)
                {
                    message = "Nome da planilha incorreto! Certifique-se que o nome da planilha no seu arquivo é CLIENTES";
                    return;
                }

                RetrieveExistentColunms(excelReader);

                //
                // Verifies if exist the necessary columms from excel file
                //

                if (!ExistRequiredInformationsToProfile(excelReader) && !ExistRequiredColumnsToLegalEntityProfile(excelReader) || !GetColumm("CEP", excelReader))
                {
                    message = "Coluna obrigatória faltando no arquivo!";
                    return;
                }

                var addressManager = new AddressManager(this);

                while (excelReader.Read())
                {
                    totalRegisters++;

                    if (!String.IsNullOrEmpty(excelReader["CEP"].ToString()))
                    {
                        if (addressManager.GetAddress(excelReader["CEP"].ToString().Replace("-", "")) == null)
                        {
                            if (IsValidAddress(excelReader))
                            {
                                InsertNewAddress(excelReader);
                            }
                            else
                            {
                                errors++;
                                continue;
                            }
                        }
                    }

                    //
                    // Retrieve the cpnj's e cpf's from db for to compare with
                    // the cpnj's e cpf's retrieved from excel file
                    //

                    listCPF.AddRange(profileManager.GetCPFnumbers(companyId));
                    listCNPJ.AddRange(profileManager.GetCNPJnumbers(companyId));

                    var customer           = new Customer();
                    var profile            = new Profile();
                    var legalEntityProfile = new LegalEntityProfile();
                    var contact            = new Contact();

                    if (ExistRequiredInformationsToProfile(excelReader) && !String.IsNullOrEmpty(excelReader["CPF"].ToString()) && !String.IsNullOrEmpty(excelReader["NOME"].ToString()))
                    {
                        //
                        // profile informations
                        //

                        if (ExistCpf(companyId, excelReader))
                        {
                            // verifies if the cpf is related with some customer
                            if (CustomerManager.GetCustomer(companyId, excelReader["CPF"].ToString()) == null)
                            {
                                AttachCustomerToProfile(companyId, userId, excelReader);
                                continue;
                            }
                            else
                            {
                                errors++;
                            }

                            continue;
                        }

                        profile = FillProfileData(excelReader);

                        if (!ValidateProfileData(profile) || !ValidateAddressData(profile.PostalCode ?? String.Empty, profile.AddressNumber ?? String.Empty, profile.AddressComp ?? String.Empty))
                        {
                            errors++;
                            continue;
                        }

                        //save the customer with profile
                        customer.CompanyId    = companyId;
                        customer.ModifiedDate = DateTime.Now;
                        customer.Profile      = profile;

                        CustomerManager.Insert(customer);

                        //
                        // insert a contact and related it with customer
                        //
                        if (ExistsColumm("CONTATO"))
                        {
                            if (!String.IsNullOrEmpty(excelReader["CONTATO"].ToString()))
                            {
                                SaveCustomerContact(customer, userId, excelReader["CONTATO"].ToString());
                            }
                        }

                        listCPF.Add(excelReader["CPF"].ToString());
                        continue;
                    }
                    //
                    // LegalEntityProfile informations
                    //

                    if (ExistRequiredColumnsToLegalEntityProfile(excelReader) && !String.IsNullOrEmpty(excelReader["CNPJ"].ToString()) && !String.IsNullOrEmpty(excelReader["RAZAO_SOCIAL"].ToString()))
                    {
                        if (ExistCnpj(companyId, excelReader))
                        {
                            // verifies if the cnpj is related with some customer
                            if (CustomerManager.GetCustomer(companyId, excelReader["CNPJ"].ToString()) == null)
                            {
                                AttachCustomerToLegalEntityProfile(companyId, userId, excelReader);
                                continue;
                            }
                            else
                            {
                                errors++;
                            }

                            continue;
                        }

                        legalEntityProfile = FillLegalEntityProfileData(excelReader);

                        if (!ValidateLegalEntityProfileData(legalEntityProfile) ||
                            !ValidateAddressData(profile.PostalCode ?? String.Empty, profile.AddressNumber ?? String.Empty, profile.AddressComp ?? String.Empty))
                        {
                            errors++;
                            continue;
                        }

                        //
                        // insert a customer
                        //
                        customer.CompanyId          = companyId;
                        customer.ModifiedDate       = DateTime.Now;
                        customer.LegalEntityProfile = legalEntityProfile;
                        CustomerManager.Insert(customer);

                        //
                        // insert a contact and related it with customer
                        //
                        if (ExistsColumm("CONTATO"))
                        {
                            if (!String.IsNullOrEmpty(excelReader["CONTATO"].ToString()))
                            {
                                SaveCustomerContact(customer, userId, excelReader["CONTATO"].ToString());
                            }
                        }

                        listCNPJ.Add(excelReader["CNPJ"].ToString());
                    }
                }

                message = "Total de Registros: " + totalRegisters + "; Registros importados com sucesso: " + Convert.ToInt32(totalRegisters - errors) +
                          "; Registros não importados por incompatibilidade de dados: " + errors;

                excelConnection.Close();
            }
        }