public ClientResponse GetCashFlowByYear(int? accountPlanId, int year)
 {
     return new ClientResponse(() =>
     {
         using (var manager = new AccountManager(null))
             return manager.GetCashFlowByYear(Company.CompanyId, accountPlanId, year);
     });
 }
        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(Request.Form["rbtFinancierCondition"]))
            {
                lblError.Text = "Selecione uma condição de pagamento!";
                return;
            }

            Session["FinacierConditionId"] = Request.Form["rbtFinancierCondition"];

            var financierCondition = new AccountManager(this).GetFinancierCondition(Page.Company.CompanyId, Convert.ToInt32(Request.Form["rbtFinancierCondition"]));

            Session["amount"] = totalWithDeliveryPrice / financierCondition.ParcelCount;
            Response.Redirect("CreateCustomer.aspx");
        }
        /// <summary>
        /// Add all payment methods to the company after configure
        /// </summary>
        /// <param name="company"></param>
        //private void SetCompanyPaymentMethods(Company company)
        //{
        //    PaymentMethodManager manager = new PaymentMethodManager(this);
        //    foreach (PaymentMethod paymentMethod in DbContext.PaymentMethods)
        //    {
        //        CompanyPaymentMethod companyPaymentMethod = new CompanyPaymentMethod();
        //        companyPaymentMethod.PaymentMethodId = paymentMethod.PaymentMethodId;
        //        companyPaymentMethod.CompanyId = company.CompanyId;
        //        manager.InsertCompanyPaymentMethod(companyPaymentMethod);
        //    }
        //}
        /// <summary>
        /// Configurate the company inserting template of the Best Pratices, Report, etc.
        /// </summary>
        /// <param name="company"></param>
        private void SetCompanyConfiguration(Company company)
        {
            //
            //Method to create a Company Configuration
            //
            InsertCompanyConfiguration(new CompanyConfiguration(), company);

            //
            // Accounting Plan
            //
            var accountManager = new AccountManager(this);
            accountManager.RegisterAccountingPlanTemplate(company.CompanyId);

            //
            // Cost Center
            //
            accountManager.RegisterCostCenterTemplate(company.CompanyId);
        }
        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;
        }
        public void GerarArquivoRemessa(Int32 companyId, Int32 accountId, DateTime beginDate, DateTime endDate,
                                        Stream stream)
        {
            var accountManager = new AccountManager(this);
            var companyManager = new CompanyManager(this);

            Company company = companyManager.GetCompany(companyId);
            FinancierOperation operation = accountManager.GetFinancierOperationBoleto(companyId);
            Boletos boletos = ConvertInvoiceParcelsInBoleto(companyId, accountId, beginDate, endDate);
            var exportFile = new ArquivoRemessa(TipoArquivo.CNAB400);
            var cedente = new Cedente(company.LegalEntityProfile.CNPJ, company.LegalEntityProfile.CompanyName,
                                      operation.Account.Agency, Convert.ToString(operation.Account.AgencyDigit), operation.Account.AccountNumber,
                                      Convert.ToString(operation.Account.AccountNumberDigit));


            exportFile.GerarArquivoRemessa(String.Empty,
                                           new Banco(Convert.ToInt32(operation.Account.Bank.BankNumber)), cedente, boletos, stream,
                                           1);
        }
        /// <summary>
        /// this method save the invoice created by Sale
        /// </summary>
        /// <param name="sale"></param>
        private void SaveSaleInvoice(Sale sale, Invoice invoice, List<Parcel> lstParcels)
        {
            //
            // Save Invoice
            //
            var financialManager = new FinancialManager(this);
            sale.InvoiceId = financialManager.InsertRetrievingId(invoice);
            DbContext.SubmitChanges();


            //
            // Save Parcels
            //
            var parcelsManager = new ParcelsManager(this);
            var accountManager = new AccountManager(this);

            var firstParcel = lstParcels.FirstOrDefault();
            var condition = accountManager.GetFinancierConditionByParcelCount(firstParcel.CompanyId, firstParcel.FinancierOperationId.Value, lstParcels.Count());

            foreach (Parcel parcel in lstParcels)
            {
                parcel.InvoiceId = invoice.InvoiceId;
                parcelsManager.Insert(parcel, condition);
            }


        }