protected void btnSave_Click(object sender, EventArgs e)
    {
        accountManager = new AccountManager(this);
        account = new Account();

        if (!String.IsNullOrEmpty(Request["AccountId"]))
        {
            originalAccount = accountManager.GetAccount(Convert.ToInt32(Page.ViewState["AccountId"]), Company.CompanyId);
            account.CopyPropertiesFrom(originalAccount);
        }

        account.CompanyId = Company.CompanyId;
        account.BankId = Convert.ToInt32(cboBank.SelectedValue);
        account.AccountNumber = txtAccountNumber.Text;
        account.Agency = txtAgency.Text;
        account.AgencyMail = txtAgencyMail.Text;
        account.AgencyManager = txtAgencyManager.Text;
        account.AgencyPhone = txtAgencyPhone.Text;

        account.PostalCode = ucAddress.PostalCode;
        account.AddressComp = ucAddress.AddressComp;
        account.AddressNumber = ucAddress.AddressNumber;

        if (!String.IsNullOrEmpty(Request["AccountId"]))
            accountManager.Update(originalAccount, account);
        else
            accountManager.Insert(account);

        Response.Redirect("Accounts.aspx");

    }
 public static bool DeleteAccount(int accountId, int companyId)
 {
     bool result = true;
     using (AccountManager accountManager = new AccountManager(null))
     {
         try
         {
             accountManager.Delete(accountManager.GetAccount(accountId, companyId));
         }
         catch (System.Data.SqlClient.SqlException)
         {
             result = false;
         }
     }
     return result;
 }
    private void ShowAccount()
    {
        accountManager = new AccountManager(this);
        originalAccount = accountManager.GetAccount(Convert.ToInt32(Page.ViewState["AccountId"]), Company.CompanyId);
       
        cboBank.SelectedValue = Convert.ToString(originalAccount.BankId);        
        txtAccountNumber.Text = originalAccount.AccountNumber;
        txtAgency.Text = originalAccount.Agency;
        txtAgencyMail.Text = originalAccount.AgencyMail;
        txtAgencyManager.Text = originalAccount.AgencyManager;
        txtAgencyPhone.Text = originalAccount.AgencyPhone;

        ucAddress.PostalCode = originalAccount.PostalCode;
        ucAddress.AddressComp = originalAccount.AddressComp;
        ucAddress.AddressNumber = originalAccount.AddressNumber;

    }
    protected void grdParcel_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //find objects
        //Parcel parcel = DataSource[e.RowIndex];
        var accountManager = new AccountManager(this);

        var txtEffectedDate = (grdParcel.Rows[e.RowIndex].FindControl("txtEffectedDate") as Date);
        var txtDueDate = (grdParcel.Rows[e.RowIndex].FindControl("txtGrdDueDate") as Date);
        var ucCurrFieldEffectedAmount = (grdParcel.Rows[e.RowIndex].FindControl("txtEffectedAmount") as CurrencyField);
        var txtIdentificationNumber = (grdParcel.Rows[e.RowIndex].FindControl("txtIdentificationNumber") as TextBox);
        var ddlCboAccountName = (grdParcel.Rows[e.RowIndex].FindControl("cboAccountName") as DropDownList);
        var ddlcboPaymentMethod = (grdParcel.Rows[e.RowIndex].FindControl("cboPaymentMethod") as DropDownList);
        var lblAmount = (grdParcel.Rows[e.RowIndex].FindControl("lblAmount") as Label);

        if (DataSource[e.RowIndex].ParcelId != 0)
        {
            // Update Mode, there is an object attached

            DataSource[e.RowIndex].Account = null;
            DataSource[e.RowIndex].PaymentMethod = null;
            DataSource[e.RowIndex].FinancierOperation = null;

            if (!String.IsNullOrEmpty(ddlCboAccountName.SelectedValue))
                DataSource[e.RowIndex].Account = accountManager.GetAccount(Convert.ToInt32(ddlCboAccountName.SelectedValue), Page.Company.CompanyId);

            if (!String.IsNullOrEmpty(ddlcboPaymentMethod.SelectedValue))
            {
                DataSource[e.RowIndex].PaymentMethod = accountManager.GetPaymentMethod(Convert.ToInt32(ddlcboPaymentMethod.SelectedValue));

                if (Convert.ToInt32(ddlcboPaymentMethod.SelectedValue) == PaymentMethod.Boleto)
                {
                    var financierOperation = accountManager.GetFinancierOperationBoleto(Page.Company.CompanyId);

                    if (financierOperation != null)
                        DataSource[e.RowIndex].FinancierOperation = financierOperation;
                }
            }
        }
        else
        {
            // Insert mode, so there is no object Attached

            DataSource[e.RowIndex].AccountId = null;
            DataSource[e.RowIndex].PaymentMethodId = null;
            DataSource[e.RowIndex].IdentificationNumber = String.Empty;

            if (!String.IsNullOrEmpty(ddlCboAccountName.SelectedValue))
                DataSource[e.RowIndex].AccountId = accountManager.GetAccount(Convert.ToInt32(ddlCboAccountName.SelectedValue), Page.Company.CompanyId).AccountId;

            if (!String.IsNullOrEmpty(ddlcboPaymentMethod.SelectedValue))
            {
                DataSource[e.RowIndex].PaymentMethodId = Convert.ToInt32(ddlcboPaymentMethod.SelectedValue);

                if (Convert.ToInt32(ddlcboPaymentMethod.SelectedValue) == PaymentMethod.Boleto)
                {
                    var financierOperation = accountManager.GetFinancierOperationBoleto(Page.Company.CompanyId);

                    if (financierOperation != null)
                        DataSource[e.RowIndex].FinancierOperationId = financierOperation.FinancierOperationId;
                }
            }
        }

        if (!String.IsNullOrEmpty(txtIdentificationNumber.Text))
            DataSource[e.RowIndex].IdentificationNumber = txtIdentificationNumber.Text;

        DataSource[e.RowIndex].EffectedAmount = Convert.ToDecimal(ucCurrFieldEffectedAmount.CurrencyValue);
        DataSource[e.RowIndex].Amount = Convert.ToDecimal(lblAmount.Text.Remove(0, 3));
        DataSource[e.RowIndex].DueDate = txtDueDate.DateTime.Value;

        DataSource[e.RowIndex].EffectedDate = txtEffectedDate.DateTime;
        if (txtEffectedDate.DateTime.HasValue)
        {
            if (txtEffectedDate.DateTime.Value < DateTime.MinValue.Sql2005MinValue() || txtEffectedDate.DateTime.Value > DateTime.Now)
            {
                Page.ShowError(Exception.DateBiggerThanCurrentDate);
                e.Cancel = true;
                return;
            }
        }

        if (txtEffectedDate.DateTime.HasValue && ucCurrFieldEffectedAmount.CurrencyValue == 0)
        {
            Page.ShowError("Parcela não pode ser quitada com valor 0(zero)!");
            e.Cancel = true;
            return;
        }

        BindGrid(-1);
    }
        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;
        }