// UPDATE
        //------------------------------------------------------------------------------------------------------------
        public bool UpdateDespesaPeriodica(objDespesaPeriodica desp, object dbTran = null)
        {
            AcessoDados db = dbTran == null ? new AcessoDados() : (AcessoDados)dbTran;

            try
            {
                if (dbTran == null)
                {
                    db.BeginTransaction();
                }

                //--- UPDATE Despesa
                new DespesaBLL().UpdateDespesa(desp, db);

                //--- UPDATE Despesa Periodica
                UpdateDespesaPeriodica(desp, db);

                if (dbTran == null)
                {
                    db.CommitTransaction();
                }
                return(true);
            }
            catch (Exception ex)
            {
                if (dbTran == null)
                {
                    db.RollBackTransaction();
                }
                throw ex;
            }
        }
        // UPDATE DESPESA PERIODICA
        //------------------------------------------------------------------------------------------------------------
        private void UpdateDespesaPeriodica(objDespesaPeriodica desp, AcessoDados dbTran)
        {
            try
            {
                //--- clear Params
                dbTran.LimparParametros();

                //--- define Params
                dbTran.AdicionarParametros("@IDDespesa", desp.IDDespesa);
                dbTran.AdicionarParametros("@IDAPagarForma", desp.IDAPagarForma);
                dbTran.AdicionarParametros("@IniciarData", desp.IniciarData);
                dbTran.AdicionarParametros("@RecorrenciaTipo", desp.RecorrenciaTipo);
                dbTran.AdicionarParametros("@RecorrenciaDia", desp.RecorrenciaDia);
                dbTran.AdicionarParametros("@RecorrenciaMes", desp.RecorrenciaMes);
                dbTran.AdicionarParametros("@RecorrenciaRepeticao", desp.RecorrenciaRepeticao);
                dbTran.AdicionarParametros("@RecorrenciaSemana", desp.RecorrenciaSemana);
                dbTran.AdicionarParametros("@Ativa", desp.Ativa);
                dbTran.AdicionarParametros("@Instalacao", desp.Instalacao);

                //--- convert null parameters
                dbTran.ConvertNullParams();

                string query = dbTran.CreateUpdateSQL("tblDespesaPeriodica", "@IDDespesa");

                //--- update
                dbTran.ExecutarManipulacao(CommandType.Text, query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        // CONVERT ROW IN CLASS
        //------------------------------------------------------------------------------------------------------------
        public objDespesaPeriodica ConvertRowInClass(DataRow row)
        {
            objDespesaPeriodica despesa = new objDespesaPeriodica((long)row["IDDespesa"])
            {
                DespesaOrigem            = 2,
                DespesaDescricao         = (string)row["DespesaDescricao"],
                DespesaData              = (DateTime)row["DespesaData"],
                DespesaValor             = (decimal)row["DespesaValor"],
                IDCredor                 = (int)row["IDCredor"],
                Credor                   = (string)row["Credor"],
                IDDespesaTipo            = (int)row["IDDespesaTipo"],
                DespesaTipo              = (string)row["DespesaTipo"],
                IDSetor                  = (int)row["IDSetor"],
                Setor                    = (string)row["Setor"],
                IDAPagarForma            = (int)row["IDAPagarForma"],
                APagarForma              = (string)row["APagarForma"],
                IniciarData              = (DateTime)row["IniciarData"],
                RecorrenciaTipo          = (byte)row["RecorrenciaTipo"],
                RecorrenciaTipoDescricao = (string)row["RecorrenciaTipoDescricao"],
                RecorrenciaDia           = row["RecorrenciaDia"] == DBNull.Value ? null : (byte?)row["RecorrenciaDia"],
                RecorrenciaMes           = row["RecorrenciaMes"] == DBNull.Value ? null : (byte?)row["RecorrenciaMes"],
                RecorrenciaRepeticao     = row["RecorrenciaRepeticao"] == DBNull.Value ? null : (short?)row["RecorrenciaRepeticao"],
                RecorrenciaSemana        = row["RecorrenciaSemana"] == DBNull.Value ? null : (byte?)row["RecorrenciaSemana"],
                Ativa                    = (bool)row["Ativa"],
                Instalacao               = row["Instalacao"] == DBNull.Value ? string.Empty : (string)row["Instalacao"],
            };

            return(despesa);
        }
        // INSERT
        //------------------------------------------------------------------------------------------------------------
        public long InsertDespesaPeriodica(objDespesaPeriodica desp)
        {
            AcessoDados dbTran = null;

            try
            {
                dbTran = new AcessoDados();
                dbTran.BeginTransaction();

                //--- insert and Get new ID
                long newID = new DespesaBLL().InsertDespesa(desp, dbTran);

                //--- insert Despesa Periodica
                desp.IDDespesa = newID;
                InsertDespesaPeriodica(desp, dbTran);

                dbTran.CommitTransaction();
                return(newID);
            }
            catch (Exception ex)
            {
                dbTran.RollBackTransaction();
                throw ex;
            }
        }
        // EXCLUIR DESPESA PERIODICA
        //------------------------------------------------------------------------------------------------------------
        private void btnExcluir_Click(object sender, EventArgs e)
        {
            var resp = AbrirDialog("Deseja realmente excluir essa Despesa Periódica permanentemente?",
                                   "Excluir Despesa Periódica", DialogType.SIM_NAO,
                                   DialogIcon.Question,
                                   DialogDefaultButton.Second);

            if (resp != DialogResult.Yes)
            {
                return;
            }

            //--- check selected item
            if (dgvListagem.SelectedRows.Count == 0)
            {
                AbrirDialog("Favor selecionar um registro para Excluir...",
                            "Selecionar Registro", DialogType.OK, DialogIcon.Information);
                return;
            }

            //--- get Selected item
            objDespesaPeriodica item = (objDespesaPeriodica)dgvListagem.SelectedRows[0].DataBoundItem;

            try
            {
                // --- Ampulheta ON
                Cursor.Current = Cursors.WaitCursor;

                dBLL.DeleteDespesaPeriodica(item);

                //--- message
                AbrirDialog("Despesa Periódica excluída com sucesso!", "Despesa Excluída",
                            DialogType.OK, DialogIcon.Information);

                //--- obter listagem
                _Alterado = true;
                ObterDados();
            }
            catch (AppException ex)
            {
                AbrirDialog("Não é possível realizar a exclusão do registro de Despesa Periódica\n" +
                            ex.Message, "Bloqueio de Exclusão",
                            DialogType.OK,
                            DialogIcon.Information);
            }
            catch (Exception ex)
            {
                AbrirDialog("Uma exceção ocorreu ao Excluir Despesa Periódica..." + "\n" +
                            ex.Message, "Exceção", DialogType.OK, DialogIcon.Exclamation);
            }
            finally
            {
                // --- Ampulheta OFF
                Cursor.Current = Cursors.Default;
            }
        }
        // CONTROL IMAGES OF LIST DATAGRID
        //------------------------------------------------------------------------------------------------------------
        private void dgvListagem_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == clnAtivo.Index)
            {
                objDespesaPeriodica item = (objDespesaPeriodica)dgvListagem.Rows[e.RowIndex].DataBoundItem;

                if (item.Ativa)
                {
                    e.Value = ImgAtivo;
                }
                else
                {
                    e.Value = ImgInativo;
                }
            }
        }
Exemplo n.º 7
0
        public frmAPagarDespesaPeriodica(objDespesaPeriodica Periodica, Form formOrigem = null)
        {
            InitializeComponent();

            //--- Add any initialization after the InitializeComponent() call.
            _periodica      = Periodica;
            _formOrigem     = formOrigem;
            bind.DataSource = _periodica;
            BindingCreator();

            // obter dados e preenche a listagem
            ObterDados();
            FormataListagem();

            //--- Handlers
            HandlerKeyDownControl(this);
        }
        private void AtivarDesativar_Click(object sender, EventArgs e)
        {
            //--- verifica se existe alguma cell
            if (dgvListagem.SelectedRows.Count == 0)
            {
                return;
            }

            //--- Verifica o item
            objDespesaPeriodica desp = (objDespesaPeriodica)dgvListagem.SelectedRows[0].DataBoundItem;

            //---pergunta ao usuário
            var reponse = AbrirDialog($"Deseja realmente {(desp.Ativa ? "DESATIVAR " : "ATIVAR")} essa Despesa Periódica?\n" +
                                      desp.DespesaTipo.ToUpper(), (desp.Ativa ? "DESATIVAR " : "ATIVAR"),
                                      DialogType.SIM_NAO, DialogIcon.Question);

            if (reponse == DialogResult.No)
            {
                return;
            }

            //--- altera o valor
            desp.Ativa = !desp.Ativa;

            //--- Salvar o Registro
            try
            {
                // --- Ampulheta ON
                Cursor.Current = Cursors.WaitCursor;

                dBLL.UpdateDespesaPeriodicaAtiva((long)desp.IDDespesa, desp.Ativa);

                //--- obter listagem
                ObterDados();
            }
            catch (Exception ex)
            {
                AbrirDialog("Uma exceção ocorreu ao Alterar a Despesa Periódica..." + "\n" +
                            ex.Message, "Exceção", DialogType.OK, DialogIcon.Exclamation);
            }
            finally
            {
                // --- Ampulheta OFF
                Cursor.Current = Cursors.Default;
            }
        }
        // VER LISTA PAGAMENTOS
        //------------------------------------------------------------------------------------------------------------
        private void mnuListaPagamentos_Click(object sender, EventArgs e)
        {
            //--- check selected item
            if (dgvListagem.SelectedRows.Count == 0)
            {
                AbrirDialog("Favor selecionar um registro para Visualizar os Pagamentos...",
                            "Selecionar Registro", DialogType.OK, DialogIcon.Information);
                return;
            }

            //--- get Selected item
            objDespesaPeriodica item = (objDespesaPeriodica)dgvListagem.SelectedRows[0].DataBoundItem;

            var frm = new APagar.frmAPagarDespesaPeriodica(item, this);

            //frm.MdiParent = Application.OpenForms.OfType<frmPrincipal>().FirstOrDefault();
            frm.ShowDialog();
        }
        // EDITAR DESPESA ESCOLHIDA
        //------------------------------------------------------------------------------------------------------------
        private void btnVisualizar_Click(object sender, EventArgs e)
        {
            //--- check selected item
            if (dgvListagem.SelectedRows.Count == 0)
            {
                AbrirDialog("Favor selecionar um registro para Editar...",
                            "Selecionar Registro", DialogType.OK, DialogIcon.Information);
                return;
            }

            //--- get Selected item
            objDespesaPeriodica item = (objDespesaPeriodica)dgvListagem.SelectedRows[0].DataBoundItem;

            frmDespesaPeriodica frm = new frmDespesaPeriodica(item, this);

            frm.MdiParent = Application.OpenForms.OfType <frmPrincipal>().FirstOrDefault();
            DesativaMenuPrincipal();
            Close();
            frm.Show();
        }
Exemplo n.º 11
0
        // CONVERT APAGAR PERIODICO IN REAL
        //------------------------------------------------------------------------------------------------------------
        public objAPagar ConvertPeriodicoInReal(objDespesaPeriodica desp, DateTime Vencimento)
        {
            AcessoDados dbTran = null;

            try
            {
                // init transaction
                dbTran = new AcessoDados();
                dbTran.BeginTransaction();

                //--- check exists DESP PERIODICA before this
                var PeriodicoList = CreateListAPagarPeriodica(desp, desp.IniciarData, desp.IniciarData.AddMonths(1));

                if (PeriodicoList[0].Vencimento != Vencimento)
                {
                    throw new AppException($"Não é possível converter em Despesa Real já que existem vencimentos " +
                                           $"anteriores dessa despesa periódica.\n" +
                                           $"Primeira Data: {PeriodicoList[0].Vencimento:d}");
                }

                // 1. update Despesa Periodica (IniciarData, DespesaValor)
                desp.IniciarData = Vencimento.AddDays(1);
                new DespesaPeriodicaBLL().UpdateDespesaPeriodica(desp, dbTran);

                // 2. create and save APagar
                objAPagar newPag = CreateAPagarByDespesa(desp, Vencimento);
                InsertAPagar(newPag, dbTran);

                // 3. return new APagar
                dbTran.CommitTransaction();
                return(newPag);
            }
            catch (Exception ex)
            {
                dbTran.RollBackTransaction();
                throw ex;
            }
        }
Exemplo n.º 12
0
 // CREATE NEW APAGAR BY DESPESA PERIODICA
 //------------------------------------------------------------------------------------------------------------
 private objAPagar CreateAPagarByDespesa(objDespesaPeriodica desp, DateTime dtAtual)
 {
     return(new objAPagar(null)
     {
         APagarValor = desp.DespesaValor,
         Banco = desp.BancoNome,
         APagarForma = desp.APagarForma,
         Credor = desp.Credor,
         DespesaDescricao = desp.DespesaDescricao,
         DespesaOrigem = 2,
         IDBanco = desp.IDBanco,
         IDAPagarForma = desp.IDAPagarForma,
         IDCredor = desp.IDCredor,
         IDDespesa = (long)desp.IDDespesa,
         IDSituacao = 1,
         PagamentoData = null,
         Identificador = $"PER{desp.IDDespesa:D4} | {dtAtual.Year}{dtAtual.Month:D2}{dtAtual.Day:D2}",
         Parcela = null,
         Situacao = "Em Aberto",
         Vencimento = dtAtual,
         ReferenciaAno = dtAtual.Year,
         ReferenciaMes = dtAtual.Month,
     });
 }
        private void dgvListagem_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Control c = (Control)sender;
                DataGridView.HitTestInfo hit = dgvListagem.HitTest(e.X, e.Y);
                dgvListagem.ClearSelection();

                //---VERIFICAÇÕES NECESSARIAS
                if (hit.Type != DataGridViewHitTestType.Cell)
                {
                    return;
                }

                // seleciona o ROW
                dgvListagem.Rows[hit.RowIndex].Cells[0].Selected = true;
                dgvListagem.Rows[hit.RowIndex].Selected          = true;

                // mostra o MENU ativar e desativar
                objDespesaPeriodica desp = (objDespesaPeriodica)dgvListagem.Rows[hit.RowIndex].DataBoundItem;

                if (desp.Ativa == true)
                {
                    mnuAtivar.Enabled    = false;
                    mnuDesativar.Enabled = true;
                }
                else
                {
                    mnuAtivar.Enabled    = true;
                    mnuDesativar.Enabled = false;
                }

                // revela menu
                MenuListagem.Show(c.PointToScreen(e.Location));
            }
        }
        // DELETE
        //------------------------------------------------------------------------------------------------------------
        public bool DeleteDespesaPeriodica(objDespesaPeriodica periodica)
        {
            AcessoDados dbTran = null;

            //--- get despesa from periodica
            objDespesaComum despesa = new objDespesaComum(periodica.IDDespesa)
            {
                DespesaDescricao = periodica.DespesaDescricao,
                DespesaOrigem    = periodica.DespesaOrigem,
                DespesaValor     = periodica.DespesaValor,
                DespesaData      = periodica.DespesaData,
                IDCredor         = periodica.IDCredor,
                IDSetor          = periodica.IDSetor,
                IDDespesaTipo    = periodica.IDDespesaTipo,
            };

            try
            {
                dbTran = new AcessoDados();
                dbTran.BeginTransaction();

                // 1 - CHECK APagar and Movimentacao Saida
                //------------------------------------------------------------------------------------------------------------
                List <objAPagar>       listAPagar    = new List <objAPagar>();
                List <objMovimentacao> listMovSaidas = new List <objMovimentacao>();

                DespesaComumBLL dBLL = new DespesaComumBLL();

                if (!dBLL.VerifyBeforeDelete(despesa, ref listAPagar, ref listMovSaidas, dbTran))
                {
                    return(false);
                }

                // 2 - delete ALL APAGAR
                //------------------------------------------------------------------------------------------------------------
                if (listAPagar.Count > 0)
                {
                    APagarBLL pBLL = new APagarBLL();

                    foreach (objAPagar pagar in listAPagar)
                    {
                        pBLL.DeleteAPagar(pagar, dbTran);
                    }
                }

                // 3 - delete DESPESA COMUM
                //------------------------------------------------------------------------------------------------------------

                //--- define Params
                dbTran.LimparParametros();
                dbTran.AdicionarParametros("@IDDespesa", despesa.IDDespesa);
                dbTran.ConvertNullParams();

                //--- create query
                string query = "DELETE tblDespesaPeriodica WHERE IDDespesa = @IDDespesa";

                //--- DELETE
                dbTran.ExecutarManipulacao(CommandType.Text, query);

                // 4 - delete DESPESA DATA PERIODO
                //------------------------------------------------------------------------------------------------------------

                //--- define Params
                dbTran.LimparParametros();
                dbTran.AdicionarParametros("@IDDespesa", despesa.IDDespesa);
                dbTran.ConvertNullParams();

                //--- create query
                query = "DELETE tblDespesaDataPeriodo WHERE IDDespesa = @IDDespesa";

                //--- DELETE
                dbTran.ExecutarManipulacao(CommandType.Text, query);

                // 5 - delete DESPESA
                //------------------------------------------------------------------------------------------------------------

                //--- define Params
                dbTran.LimparParametros();
                dbTran.AdicionarParametros("@IDDespesa", despesa.IDDespesa);
                dbTran.ConvertNullParams();

                //--- create query
                query = "DELETE tblDespesa WHERE IDDespesa = @IDDespesa";

                //--- DELETE
                dbTran.ExecutarManipulacao(CommandType.Text, query);

                // 7 - COMMIT AND RETURN
                //------------------------------------------------------------------------------------------------------------
                dbTran.CommitTransaction();
                return(true);
            }
            catch (AppException ex)
            {
                dbTran.RollBackTransaction();
                throw ex;
            }
            catch (Exception ex)
            {
                dbTran.RollBackTransaction();
                throw ex;
            }
        }
Exemplo n.º 15
0
        // CREATE LIST OF APAGAR FROM DESPESA PERIODICA
        //------------------------------------------------------------------------------------------------------------
        private List <objAPagar> CreateListAPagarPeriodica(
            objDespesaPeriodica desp,
            DateTime?dataInicial,
            DateTime?dataFinal = null)
        {
            // Check dfInicial
            if (dataInicial == null)
            {
                dataInicial = desp.IniciarData;
            }

            // Create dtAtual => 'DataInicial' OR 'IniciarData' o que for MAIOR
            DateTime dtAtual = (DateTime)dataInicial >= desp.IniciarData ? (DateTime)dataInicial : desp.IniciarData;

            // Check dfFinal
            if (dataFinal == null)
            {
                dataFinal = dtAtual.AddYears(1);
            }

            // create list APagar
            List <objAPagar> list = new List <objAPagar>();

            switch (desp.RecorrenciaTipo)
            {
            case 1:                     // DIARIO

                //--- discover the first date
                while (dtAtual <= dataFinal)
                {
                    list.Add(CreateAPagarByDespesa(desp, dtAtual));
                    dtAtual = dtAtual.AddDays((int)desp.RecorrenciaRepeticao);
                }

                break;

            case 2:                     // SEMANAL

                //--- discover the first date
                while ((int)dtAtual.DayOfWeek != desp.RecorrenciaDia)
                {
                    dtAtual = dtAtual.AddDays(1);
                }

                //--- creating Vencimentos
                while (dtAtual <= dataFinal)
                {
                    list.Add(CreateAPagarByDespesa(desp, dtAtual));
                    dtAtual = dtAtual.AddDays(7 * (int)desp.RecorrenciaRepeticao);
                }

                break;

            case 3:                     // MENSAL POR DIA

                //--- discover the first date
                if (dtAtual.Day != desp.RecorrenciaDia)
                {
                    if (dtAtual.Day > desp.RecorrenciaDia)
                    {
                        dtAtual = dtAtual.AddMonths(1);
                    }

                    // create string of date
                    string strDate = $"{(int)desp.RecorrenciaDia}/{dtAtual.Month}/{dtAtual.Year}";

                    // try to convert in date (IF ReferenciaDia > 29 in FEBRUARY)
                    if (!DateTime.TryParse(strDate, out DateTime new_dtAtual))                             // se nao for data
                    {
                        int maxDays = DateTime.DaysInMonth(dtAtual.Year, dtAtual.Month);
                        dtAtual = new DateTime(dtAtual.Year, dtAtual.Month, maxDays);
                    }
                    else                             // se for data possivel
                    {
                        dtAtual = new_dtAtual;
                    }
                }

                //--- creating Vencimentos
                while (dtAtual <= dataFinal)
                {
                    list.Add(CreateAPagarByDespesa(desp, dtAtual));
                    dtAtual = dtAtual.AddMonths((int)desp.RecorrenciaRepeticao);
                }

                break;

            case 4:                     // MENSAL POR SEMANA

                //--- discover the first date
                while ((int)dtAtual.DayOfWeek != desp.RecorrenciaDia)
                {
                    dtAtual = dtAtual.AddDays(1);
                }

                // check week number
                int semana = (int)Math.Ceiling((decimal)dtAtual.Day / 7);

                while (semana != desp.RecorrenciaSemana)
                {
                    dtAtual = dtAtual.AddDays(7);
                    semana  = (int)Math.Ceiling((decimal)dtAtual.Day / 7);
                }

                //--- creating Vencimentos
                while (dtAtual <= dataFinal)
                {
                    list.Add(CreateAPagarByDespesa(desp, dtAtual));
                    dtAtual = dtAtual.AddMonths((int)desp.RecorrenciaRepeticao);
                }

                break;

            case 5:                     // ANUAL POR MES E DIA

                //--- discover the first date
                if (dtAtual.Month != desp.RecorrenciaMes)
                {
                    if (dtAtual.Month > desp.RecorrenciaMes)
                    {
                        dtAtual = dtAtual.AddYears(1);
                    }

                    if (dtAtual.Day > desp.RecorrenciaDia)
                    {
                        dtAtual = dtAtual.AddMonths(1);
                    }
                }
                else
                {
                    if (dtAtual.Day > desp.RecorrenciaDia)
                    {
                        dtAtual = dtAtual.AddMonths(1);
                    }
                }

                // create string of date
                string strDate2 = $"{(int)desp.RecorrenciaDia}/{(int)desp.RecorrenciaMes}/{dtAtual.Year}";

                // try to convert in date
                if (!DateTime.TryParse(strDate2, out DateTime new_dtAtual2))
                {
                    int maxDays = DateTime.DaysInMonth((int)desp.RecorrenciaMes, dtAtual.Year);
                    dtAtual = new DateTime(dtAtual.Year, (int)desp.RecorrenciaMes, maxDays);
                }
                else
                {
                    dtAtual = new_dtAtual2;
                }

                //--- creating Vencimentos
                while (dtAtual <= dataFinal)
                {
                    list.Add(CreateAPagarByDespesa(desp, dtAtual));
                    dtAtual = dtAtual.AddYears((int)desp.RecorrenciaRepeticao);
                }
                break;

            case 6:                     // ANUAL POR MES E SEMANA

                //--- discover the first date
                while ((int)dtAtual.Month != desp.RecorrenciaMes)
                {
                    dtAtual = dtAtual.AddMonths(1);
                }

                while ((int)dtAtual.DayOfWeek != desp.RecorrenciaDia)
                {
                    dtAtual = dtAtual.AddDays(1);
                }

                // check week number
                int semana2 = (int)Math.Ceiling((decimal)dtAtual.Day / 7);

                while (semana2 != desp.RecorrenciaSemana)
                {
                    dtAtual = dtAtual.AddDays(7);
                    semana2 = (int)Math.Ceiling((decimal)dtAtual.Day / 7);
                }

                //--- creating Vencimentos
                while (dtAtual <= dataFinal)
                {
                    list.Add(CreateAPagarByDespesa(desp, dtAtual));
                    dtAtual = dtAtual.AddYears((int)desp.RecorrenciaRepeticao);
                }
                break;

            default:
                break;
            }

            return(list);
        }