Exemple #1
0
        private string SaveDailyInfo()
        {
            if (!_hasUnsavedChanges)
            {
                return("No changes to save.");
            }

            DailyInfo daily = new DailyInfo();

            daily.Note         = NoteTextBox.ForeColor == Color.Black ? NoteTextBox.Text : "No note";
            daily.Day          = _selectedDay;
            daily.Month        = _selectedMonth;
            daily.Year         = _selectedYear;
            daily.TotalEarning = Convert.ToDouble(TotalEarningLabel.Text);
            daily.TotalExpense = Convert.ToDouble(TotalExpenseLabel.Text);

            string source;
            string reason;
            string category;
            string comment;
            double amount;

            foreach (DataGridViewRow row in ExpenseDataGridView.Rows)
            {
                if (IsLastEmptyRow(ExpenseDataGridView, row.Index))
                {
                    break;
                }

                try
                {
                    reason = ExpenseDataGridView.Rows[row.Index].Cells[0].Value.ToString();
                    reason = FilterString(reason);
                }
                catch
                {
                    reason = "";
                }

                try
                {
                    if (!double.TryParse(ExpenseDataGridView.Rows[row.Index].Cells[1].Value.ToString(), out amount))
                    {
                        string message = "The value for amount is not correct on row";
                        message += (row.Index + 1) + " in the expense table. Please correct" +
                                   " the amount in order to save.";

                        MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                        return("Invalid value entered");
                    }
                }
                catch (NullReferenceException)
                {
                    string message;
                    message  = "Looks like you forgot to enter amount on row ";
                    message += (row.Index + 1) + " in the expense table. ";
                    message += "Do you want to save without changing the amount?";

                    DialogResult dlgResult = MessageBox.Show(message, "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                    if (dlgResult == DialogResult.Yes)
                    {
                        amount = 0;
                    }
                    else
                    {
                        return("Terminated by user");
                    }
                }

                try
                {
                    category = ExpenseDataGridView.Rows[row.Index].Cells[2].Value.ToString();
                    category = FilterString(category);
                }
                catch
                {
                    category = "Other";
                }

                try
                {
                    comment = ExpenseDataGridView.Rows[row.Index].Cells[3].Value.ToString();
                    comment = FilterString(comment);
                }
                catch
                {
                    comment = "";
                }

                ExpenseInfo expense = new ExpenseInfo
                {
                    Reason   = reason,
                    Amount   = amount,
                    Category = category,
                    Comment  = comment
                };
                daily.ExpenseList.Add(expense);
            }

            foreach (DataGridViewRow row in EarningDataGridView.Rows)
            {
                if (IsLastEmptyRow(EarningDataGridView, row.Index))
                {
                    break;
                }

                try
                {
                    source = EarningDataGridView.Rows[row.Index].Cells[0].Value.ToString();
                    source = FilterString(source);
                }
                catch
                {
                    source = "";
                }

                try
                {
                    if (!double.TryParse(EarningDataGridView.Rows[row.Index].Cells[1].Value.ToString(), out amount))
                    {
                        string message = "The value for amount is not correct on row" + (row.Index + 1) +
                                         " in the earning table. Please correct the amount in order to save.";

                        MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                        return("Invalid value entered");
                    }
                }
                catch (NullReferenceException)
                {
                    string message = "Looks like you forgot to enter amount on row " + (row.Index + 1) +
                                     " in the earning table. Do you want to save without changing the amount?";

                    DialogResult dlgResult = MessageBox.Show(message, "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                    if (dlgResult == DialogResult.Yes)
                    {
                        amount = 0;
                    }
                    else
                    {
                        return("Terminated by user");
                    }
                }

                try
                {
                    category = EarningDataGridView.Rows[row.Index].Cells[2].Value.ToString();
                    category = FilterString(category);
                }
                catch
                {
                    category = "Other";
                }

                try
                {
                    comment = EarningDataGridView.Rows[row.Index].Cells[3].Value.ToString();
                    comment = FilterString(comment);
                }
                catch
                {
                    comment = "";
                }

                EarningInfo earning = new EarningInfo
                {
                    Source   = source,
                    Amount   = amount,
                    Category = category,
                    Comment  = comment,
                };
                daily.EarningList.Add(earning);
            }

            string result = WebHandler.SaveDailyInfo(daily);

            if (result == "SUCCESS")
            {
                //if any info on the same date already exists, overwrite that info
                //otherwise, add this info as new info
                int index = GlobalSpace.DailyInfoList.FindIndex(
                    d => d.Day == daily.Day && d.Month == daily.Month && d.Year == daily.Year);

                if (index != -1)
                {
                    GlobalSpace.DailyInfoList[index] = daily;
                }
                else
                {
                    GlobalSpace.DailyInfoList.Add(daily);
                }

                //monthly info should change accordingly since daily info has been modified
                MonthlyInfo.Fetch();

                //MessageBox.Show("The data has been successfully saved!", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);

                _hasUnsavedChanges = false;
            }

            return(result);
        }
        private void ActionUponFetchDailyInfoSuccess()
        {
            string result = _webHandlerObject.Response;

            string[] rows = result.Split('^');

            if (rows[0] == "Server connection error")
            {
                //some server error occurred
                _progressViewerObject.StopProgress();
                ShowErrorMessage("Server connection error. Please check your internet connection.");
                return;
            }

            //start fetching categories from database on a seperate thread
            //before starting to process the fetched daily info
            FetchCategories();

            if (rows[0] == "")
            {
                //no daily info found for this user in database
                //so no need to process the info
                return;
            }

            //process the fetched daily info and store them in StaticStorage class
            foreach (string row in rows)
            {
                string[] cols = row.Split('|');

                int day   = Convert.ToInt16(cols[0]);
                int month = Convert.ToInt16(cols[1]);
                int year  = Convert.ToInt16(cols[2]);

                string   note              = StringCipher.Decrypt(cols[3], GlobalSpace.CypherKey);
                string[] expenseReasons    = StringCipher.Decrypt(cols[4], GlobalSpace.CypherKey).Split('~');
                string[] expenseAmounts    = StringCipher.Decrypt(cols[5], GlobalSpace.CypherKey).Split('~');
                string[] expenseCategories = StringCipher.Decrypt(cols[6], GlobalSpace.CypherKey).Split('~');
                string[] expenseComments   = StringCipher.Decrypt(cols[7], GlobalSpace.CypherKey).Split('~');
                string[] earningSources    = StringCipher.Decrypt(cols[8], GlobalSpace.CypherKey).Split('~');
                string[] earningAmounts    = StringCipher.Decrypt(cols[9], GlobalSpace.CypherKey).Split('~');
                string[] earningCategories = StringCipher.Decrypt(cols[10], GlobalSpace.CypherKey).Split('~');
                string[] earningComments   = StringCipher.Decrypt(cols[11], GlobalSpace.CypherKey).Split('~');
                string   totalExpense      = StringCipher.Decrypt(cols[12], GlobalSpace.CypherKey);
                string   totalEarning      = StringCipher.Decrypt(cols[13], GlobalSpace.CypherKey);

                DailyInfo daily = new DailyInfo
                {
                    Day          = day,
                    Month        = month,
                    Year         = year,
                    Note         = note,
                    TotalEarning = Convert.ToDouble(totalEarning),
                    TotalExpense = Convert.ToDouble(totalExpense)
                };

                if (expenseAmounts[0] != "")
                {
                    for (int i = 0; i < expenseReasons.Length; i++)
                    {
                        double amount   = Convert.ToDouble(expenseAmounts[i]);
                        string reason   = expenseReasons[i];
                        string category = expenseCategories[i];
                        string comment  = expenseComments[i];

                        ExpenseInfo expense = new ExpenseInfo
                        {
                            Reason   = reason,
                            Amount   = amount,
                            Category = category,
                            Comment  = comment
                        };
                        daily.ExpenseList.Add(expense);
                    }
                }

                if (earningAmounts[0] != "")
                {
                    for (int i = 0; i < earningSources.Length; i++)
                    {
                        double amount   = Convert.ToDouble(earningAmounts[i]);
                        string source   = earningSources[i];
                        string category = earningCategories[i];
                        string comment  = earningComments[i];

                        EarningInfo earning = new EarningInfo
                        {
                            Source   = source,
                            Amount   = amount,
                            Category = category,
                            Comment  = comment
                        };
                        daily.EarningList.Add(earning);
                    }
                }
                GlobalSpace.DailyInfoList.Add(daily);
            }
        }