Пример #1
0
        private void Submit_Click(object sender, EventArgs _e)
        {
            decimal montant;
            int     everyXDays;

            PeriodiciteRepository.PeriodiciteModel period;

            try {
                PosteRepository.CheckDayRangeOrThrow(int.Parse(this.TxtBoxTousLesXMois.Text));
            }
            catch (ArgumentException e)
            {
                ErrorManager.ShowOperationFailed(this, e.Message);
                return;
            }

            // check if fields not empty
            if (string.IsNullOrWhiteSpace(this.txtBoxLibPoste.Text) ||
                string.IsNullOrWhiteSpace(TxtBoxTousLesXMois.Text) ||
                string.IsNullOrWhiteSpace(this.TxtBoxMontantPosteFixe.Text) ||
                this.ComboxBoxListePeriodicites.SelectedItem == null)
            {
                ErrorManager.ShowMissingFieldsError(this);
                return;
            }

            // check if values are valid
            if (!(LocalizationManager.ConvertFloatingTo <int>(
                      this.TxtBoxTousLesXMois.Text, int.TryParse, out everyXDays) &&
                  LocalizationManager.ConvertFloatingTo <decimal>(
                      this.TxtBoxMontantPosteFixe.Text, decimal.TryParse, out montant)))
            {
                ErrorManager.ShowNotANumberError(this);
                return;
            }

            editedData.libPoste_s = this.txtBoxLibPoste.Text;

            // if the lib poste was modified, check if unique; if not => error
            if (!originalData.libPoste_s.Equals(editedData.libPoste_s) &&
                !PosteRepository.IsUnique(editedData.libPoste_s))
            {
                ErrorManager.ShowDuplicateError(this);
                return;
            }

            period = (PeriodiciteRepository.PeriodiciteModel) this.ComboxBoxListePeriodicites.SelectedItem;

            // retrieve/ set remaining data
            editedData.jourDuMois = everyXDays;
            editedData.montant    = montant;
            editedData.typePer    = period.codePer;
            editedData.libPer_s   = period.libPer;

            // mark form as submitted and close it
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        private void DeleteSelectedPoste(MetroFramework.Controls.MetroGrid controler)
        {
            OleDbConnection  dbConn;
            OleDbTransaction dbTransaction;

            // delete a whole Poste if and only if a row was selected
            DataGridViewCellCollection cells = this.GetSelectedRowData(controler);

            if (cells != null)
            {
                dbConn = DatabaseManager.GetConnection();
                dbConn.Open();
                dbTransaction = dbConn.BeginTransaction();

                try
                {
                    // delete the selected poste in cascade then commit the changes
                    int indice = (int)cells[0].Value;
                    PosteRepository.Delete(dbConn, dbTransaction, indice);
                    dbTransaction.Commit();

                    // update Dataset
                    if (controler == dgvRevenus)
                    {
                        indice = dgvRevenus.Rows.GetFirstRow(DataGridViewElementStates.Selected);
                        ds.Tables["PosteRevenus"].Rows.RemoveAt(indice);
                    }
                    else
                    {
                        indice = dgvPostesFixes.Rows.GetFirstRow(DataGridViewElementStates.Selected);
                        ds.Tables["PostesPerFixes"].Rows.RemoveAt(indice);
                    }

                    SaveTablesEnLocal();
                    // Affichage des postes à périodicité fixe
                    dgvPostesFixes.DataSource = ds.Tables["PostesPerFixes"];
                    dgvPostesFixes.ClearSelection();

                    // Affichage des postes à échéances
                    dgvPostesEcheances.DataSource = ds.Tables["PosteEcheances"];
                    dgvPostesEcheances.ClearSelection();

                    // Affichage des postes revenus
                    dgvRevenus.DataSource = ds.Tables["PosteRevenus"];
                    dgvRevenus.ClearSelection();
                }
                catch (OleDbException ex)
                {
                    dbTransaction.Rollback();
                    ErrorManager.HandleOleDBError(ex);
                }
                finally
                {
                    dbConn.Close();
                }
            }
        }
Пример #3
0
        private void FillPostes()
        {
            listBoxPostes.Items.Clear();
            listBoxPostes.ClearSelected();

            try {
                listBoxPostes.Items.AddRange(
                    PosteRepository.List()
                    );
            } catch (OleDbException e)
            {
                ErrorManager.HandleOleDBError(e);
            }
        }
Пример #4
0
        private void FillPostesComboBox()
        {
            // empty the ComboBox
            this.ComboxBoxListePostes.Items.Clear();

            // Reset the selection
            this.ComboxBoxListePostes.ResetText();
            this.ComboxBoxListePostes.Refresh();

            try
            {
                this.ComboxBoxListePostes.Items.AddRange(PosteRepository.ListAvailableToUse());
            }
            catch (OleDbException ex)
            {
                ErrorManager.HandleOleDBError(ex);
            }
        }
Пример #5
0
        private void DeleteEntries(object s, object e)
        {
            // if the form is waiting for a commit/ rollback from the user:
            // do nothing.
            if (this.RequiresCommitAndShowError())
            {
                return;
            }

            OleDbCommand rmCmd;

            PosteRepository.PosteModel entry;
            int          removePos;
            DialogResult?dialogResult = null;

            // start the transaction mode
            this.BeginTransaction();

            // will there are data to remove and there is no error
            while (this.listBoxPostes.SelectedIndices.Count > 0 && dialogResult == null)
            {
                removePos = this.listBoxPostes.SelectedIndices[0];
                entry     = (PosteRepository.PosteModel) this.listBoxPostes.Items[removePos];

                try
                {
                    // Delete in cascade the given poste
                    PosteRepository.Delete(dbConn, transaction, entry.codePoste);

                    // then remove it from the list
                    this.listBoxPostes.Items.RemoveAt(removePos);
                }
                catch (OleDbException exc)
                {
                    this.RollbackTransaction();
                    ErrorManager.HandleOleDBError(exc);
                }
            }
        }
Пример #6
0
        private void btnValiderBudgetPonctuel_Click(object sender, EventArgs _ev)
        {
            OleDbConnection  dbConn;
            OleDbTransaction dbTransaction;

            // Will store the deadlines
            List <KeyValuePair <DateTime, decimal> > deadLines;

            // Retrieve the budget's title + comments and remove any leading whitespacess
            string budgetTitle    = this.txtBoxIntitulePonctuel.Text.Trim();
            string budgetComments = this.txtBoxCommentairePonctuel.Text.Trim();

            // if the comments are empty, set it to null (to avoid a OleDB Error)
            if (string.IsNullOrEmpty(budgetComments))
            {
                budgetComments = null;
            }

            // TODO: if set, check if the sum of the fields is equal to the input sum
            //		 if not, put a warning & ask for confirmation
            //    string.IsNullOrEmpty(this.txtBoxMontantPonctuel.Text)
            //    && float.TryParse(this.txtBoxMontantPonctuel.Text, out montantTotal)

            // - if:
            //     - the budget's title is null/empty
            //     - or only has spaces:
            //     - or if not every deadline is filled
            //   ---> show missing fields error and stop proceeding
            //
            // - otherwise: proceed and insert the data (if the title does not exist yet)

            if (string.IsNullOrWhiteSpace(budgetTitle) ||
                this.numberOfDeadlines < 1

                // try to retrieve every deadlines, store it to `deadLines` and return false
                // if there was a missing value
                || !CheckAndConvertDeadLinesToList(out deadLines))
            {
                ErrorManager.ShowMissingFieldsError(this);
                return;
            }

            // check if the budgetTitle is unique in the database
            // if not unique: show an error saying that it already exists and stop proceeding
            try {
                if (!PosteRepository.IsUnique(budgetTitle))
                {
                    // show a duplicate value error and specify the field
                    ErrorManager.ShowDuplicateError(this,
                                                    Program.settings.localize.Translate(this.lblIntitulePonctuel.Name));
                    return;
                }
            } catch (OleDbException ex)
            {
                ErrorManager.HandleOleDBError(ex);
                return;
            }

            // otherwise: continue and insert the data
            dbConn = DatabaseManager.GetConnection();

            dbConn.Open();

            dbTransaction = dbConn.BeginTransaction();

            // Insert the data to the data base
            try {
                PostePonctuelRepository.Create(dbConn, dbTransaction,
                                               budgetTitle,
                                               budgetComments,
                                               deadLines.ToArray()
                                               );

                Console.WriteLine("<- Commit");
                dbTransaction.Commit();

                ErrorManager.EntriesSuccessfullyAdded(this);
                ClearPostePonctuelForm();
            } catch (OleDbException e)
            {
                // cancel the changes
                dbTransaction.Rollback();

                // handle the error (log it and report it to the user)
                ErrorManager.HandleOleDBError(e);
            }
            finally
            {
                dbConn.Close();
            }
        }
Пример #7
0
        private void AddEntry(object s, object e)
        {
            // if the form is waiting for a commit/ rollback from the user:
            // do nothing.
            if (this.RequiresCommitAndShowError())
            {
                return;
            }

            InputMessageBox inputBox = new InputMessageBox("Add", null, null);

            inputBox.ShowDialog();

            // if the user didn't cancel
            if (!inputBox.userCancelled)
            {
                if (string.IsNullOrWhiteSpace(inputBox.userInput))
                {
                    MetroMessageBox.Show(this,
                                         Program.settings.localize.Translate("err_invalid_input_empty"),
                                         Program.settings.localize.Translate("err_invalid_input_caption"),
                                         MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (!IsUnique(inputBox.userInput))
                {
                    MetroMessageBox.Show(this,
                                         Program.settings.localize.Translate("err_duplicate_value"),
                                         Program.settings.localize.Translate("err_duplicate_value_caption"),
                                         MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // create a new Poste model,
                // add the user's input to it
                PosteRepository.PosteModel newPoste = new PosteRepository.PosteModel();

                // we retrieve the biggest existing Poste identifier currently in the
                // database and we increment it.
                newPoste.codePoste = PosteRepository.BiggestID() + 1;
                newPoste.libPoste  = inputBox.userInput;

                // append the newly created Poste to the list and to the queue

                //this.BeginTransaction();
                this.dbConn.Open();
                OleDbCommand cmd = new OleDbCommand(
                    "INSERT INTO Poste (codePoste, libPoste) VALUES(@codePoste, @libPoste)", this.dbConn);
                cmd.Parameters.AddWithValue("@codePoste", newPoste.codePoste);
                cmd.Parameters.AddWithValue("@libPoste", newPoste.libPoste);

                try
                {
                    cmd.ExecuteNonQuery();
                    this.listBoxPostes.Items.Add(newPoste);
                }
                catch (OleDbException exc)
                {
                    ErrorManager.HandleOleDBError(exc);
                }
                finally
                {
                    this.dbConn.Close();
                }
            }
        }
Пример #8
0
        private void btnValiderRevenu_Click(object _s, EventArgs _ev)
        {
            OleDbConnection  dbConn;
            OleDbTransaction dbTransaction;

            // Retrieve the budget's title + comments and remove any leading whitespacess
            string  revenuPoste = this.txtBoxPosteRevenu.Text.Trim();
            decimal revenuMontant;
            int     chaqueXDuMois;

            PersonneRepository.PersonneModel personne;

            // try to convert the user amount to a decimal and the day to a int,
            // if it fails, we trigger a "not a number" error and we stop proceeding
            if (!LocalizationManager.ConvertFloatingTo <decimal>(this.txtBoxMontantRevenu.Text, decimal.TryParse, out revenuMontant) ||
                !this.isDayOfTheMonth(this.txtTousLesXDuMoisRevenu.Text, out chaqueXDuMois))
            {
                ErrorManager.ShowNotANumberError(this);
                return;
            }

            // - if:
            //     - the poste is null/empty
            //     - or only has spaces:
            //     - or the beneficiary is not selected
            //   ---> show missing fields error and stop proceeding
            //
            // - otherwise: proceed and insert the data (if the poste does not exist yet)
            if (this.listBeneficiairesComboBox.SelectedItem == null ||
                string.IsNullOrWhiteSpace(revenuPoste))
            {
                ErrorManager.ShowMissingFieldsError(this);
                return;
            }

            // retrieve the selected beneficiary
            personne = (PersonneRepository.PersonneModel)listBeneficiairesComboBox.SelectedItem;

            // check if the budgetTitle is unique in the database
            // if not unique: show an error saying that it already exists and stop proceeding
            try {
                if (!PosteRepository.IsUnique(revenuPoste))
                {
                    // show a duplicate value error and specify the field
                    ErrorManager.ShowDuplicateError(this,
                                                    Program.settings.localize.Translate(this.lblPosteRevenu.Name));
                    return;
                }
            } catch (OleDbException ex)
            {
                ErrorManager.HandleOleDBError(ex);
                return;
            }

            // otherwise: continue and insert the data
            dbConn = DatabaseManager.GetConnection();

            dbConn.Open();

            dbTransaction = dbConn.BeginTransaction();

            // Insert the data to the data base
            try
            {
                PosteRevenuRepository.Create(dbConn, dbTransaction,
                                             revenuPoste,
                                             personne,
                                             revenuMontant,
                                             chaqueXDuMois
                                             );

                Console.WriteLine("<- Commit");
                dbTransaction.Commit();

                ErrorManager.EntriesSuccessfullyAdded(this);
                ClearPosteRevenuForm();
            }
            catch (OleDbException e)
            {
                // cancel the changes
                dbTransaction.Rollback();

                // handle the error (log it and report it to the user)
                ErrorManager.HandleOleDBError(e);
            }
            catch (ArgumentException e)
            {
                ErrorManager.ShowOperationFailed(this, Program.settings.localize.Translate(e.Message));
            }
            finally
            {
                dbConn.Close();
            }
        }
Пример #9
0
        private void BtnValiderBudgetFixe_Click(object _s, EventArgs e)
        {
            MetroButton sender = (MetroButton)_s;

            PosteRepository.PosteModel             SelectedPoste;
            PeriodiciteRepository.PeriodiciteModel SelectedPeriode;
            decimal montant;
            int     TousLesXDuMois;

            // disable submit button
            sender.Enabled = false;

            // Check if every field was filled
            if (
                this.ComboxBoxListePeriodicites.SelectedItem == null ||
                this.ComboxBoxListePostes.SelectedItem == null ||
                this.TxtBoxTousLesXMois.Text.Length < 1 ||
                this.TxtBoxMontantPosteFixe.Text.Length < 1
                )
            {
                MetroMessageBox.Show(this,
                                     Program.settings.localize.Translate("err_missing_fields_msg"),
                                     Program.settings.localize.Translate("err_missing_fields_caption"),
                                     MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            // try to convert the decimals and integers
            else if (!(LocalizationManager.ConvertFloatingTo <decimal>(TxtBoxMontantPosteFixe.Text, decimal.TryParse, out montant) &&
                       int.TryParse(TxtBoxTousLesXMois.Text, out TousLesXDuMois)))
            {
                MetroMessageBox.Show(this,
                                     Program.settings.localize.Translate("err_day_of_month_and_sum_not_number"),
                                     Program.settings.localize.Translate("err_uh_oh_caption"),
                                     MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                // retrieve the selected items
                SelectedPoste   = (PosteRepository.PosteModel) this.ComboxBoxListePostes.SelectedItem;
                SelectedPeriode = (PeriodiciteRepository.PeriodiciteModel) this.ComboxBoxListePeriodicites.SelectedItem;

                OleDbCommand cmd = DatabaseManager.InsertInto("PostePeriodique",
                                                              DatabaseManager.GetConnection(),
                                                              new KeyValuePair <string, object>("codePoste", SelectedPoste.codePoste),
                                                              new KeyValuePair <string, object>("typePer", SelectedPeriode.codePer),
                                                              new KeyValuePair <string, object>("montant", montant),
                                                              new KeyValuePair <string, object>("jourDuMois", TousLesXDuMois)
                                                              );

                try
                {
                    if (PosteRepository.IsAvailable(SelectedPoste.codePoste))
                    {
                        cmd.Connection.Open();
                        cmd.ExecuteNonQuery();  // insert data

                        ErrorManager.EntriesSuccessfullyAdded(this);
                        ClearPosteFixeForm();
                    }
                    else
                    {
                        ErrorManager.ShowAlreadyUsedError(this, this.lblCmbPostes.Text);
                    }
                }
                catch (OleDbException ex)
                {
                    ErrorManager.HandleOleDBError(ex);
                }
                finally
                {
                    cmd.Connection.Close();
                }

                this.FillPostesComboBox();
            }

            // re-enable the submit button
            sender.Enabled = true;
        }