コード例 #1
0
ファイル: DebitCustomer.cs プロジェクト: EgudaPeter/Ajoor
        private void btn_Debit_Click(object sender, EventArgs e)
        {
            try
            {
                if (cmb_Customers.SelectedValue != null)
                {
                    if (txt_AmountCollected.Text != string.Empty)
                    {
                        var money = Utilities.CurrencyFormat(txt_AmountCollected.Text);
                        txt_AmountCollected.Text = Utilities.RemoveCommasAndDots(txt_AmountCollected.Text);
                        var customer            = _CustomerRepo.GetCustomer(int.Parse(cmb_Customers.SelectedValue.ToString()));
                        var customerTransaction = _TransactionRepo.GetAllTransactions()
                                                  .OrderByDescending(x => x.TransactionId).Where(x => x.CustomerId == customer.CustomerId).FirstOrDefault();
                        DateTime date = new DateTime();
                        if (!_TransactionRepo.HasMonthBeenClosed(DateTime.Now.Month - 1))
                        {
                            date = DateTime.Parse(Utilities.GetLastDateOfPreviousMonth());
                        }
                        else
                        {
                            date = DateTime.Now;
                        }
                        if (customerTransaction != null)
                        {
                            if (decimal.Parse(txt_AmountCollected.Text) <= customerTransaction.AmountPayable)
                            {
                                Transactions transactions = new Transactions()
                                {
                                    CustomerId        = int.Parse(cmb_Customers.SelectedValue.ToString()),
                                    AmountContributed = 0m,
                                    AmountCollected   = decimal.Parse(txt_AmountCollected.Text),
                                    TransactionType   = "Debit",
                                    Date            = date,
                                    Commission      = 0m,
                                    ExtraCommission = 0m,
                                    AmountPayable   = decimal.Parse(txt_AmountCollected.Text),
                                    CreatedBy       = Utilities.USERNAME,
                                    CreatedDate     = date
                                };
                                if (_TransactionRepo.DebitTransaction(transactions))
                                {
                                    MessageBox.Show("Customer debited successfully!", "Superior Investment", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    var totalDebt = _TransactionRepo.GetAllTransactions()
                                                    .OrderByDescending(x => x.TransactionId).Where(x => x.CustomerId == customer.CustomerId).FirstOrDefault().TotalDebt.ToString();
                                    var totalCredit = _TransactionRepo.GetAllTransactions()
                                                      .OrderByDescending(x => x.TransactionId).Where(x => x.CustomerId == customer.CustomerId).FirstOrDefault().AmountPayable.ToString();
                                    txt_TotalDebt.Text = Utilities.CurrencyFormat(totalDebt); txt_TotalCredit.Text = Utilities.CurrencyFormat(totalCredit);
                                    if (!bgwGetRecords.IsBusy)
                                    {
                                        bgwGetRecords.RunWorkerAsync();
                                    }
                                }
                            }
                            else
                            {
                                var debt      = decimal.Parse(txt_AmountCollected.Text) - customerTransaction.AmountPayable;
                                var debtMoney = Utilities.CurrencyFormat(debt.ToString());
                                switch (MessageBox.Show($"You are about to loan {debtMoney} to {customer.FullName}. \n\nDo you wish to continue?", "Superior Investment", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
                                {
                                case DialogResult.Yes:
                                    Transactions transactions = new Transactions()
                                    {
                                        CustomerId        = int.Parse(cmb_Customers.SelectedValue.ToString()),
                                        AmountContributed = 0m,
                                        AmountCollected   = decimal.Parse(txt_AmountCollected.Text),
                                        TransactionType   = "Debit",
                                        Date            = date,
                                        Commission      = 0m,
                                        ExtraCommission = 0m,
                                        AmountPayable   = decimal.Parse(txt_AmountCollected.Text),
                                        Debt            = debt,
                                        TotalDebt       = customerTransaction.TotalDebt.HasValue ? customerTransaction.TotalDebt + debt : debt,
                                        CreatedBy       = Utilities.USERNAME,
                                        CreatedDate     = date
                                    };
                                    if (_TransactionRepo.DebitTransaction(transactions))
                                    {
                                        MessageBox.Show($"Customer has been loaned the sum of {debtMoney} successfully!", "Superior Investment", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                        var totalDebt = _TransactionRepo.GetAllTransactions()
                                                        .OrderByDescending(x => x.TransactionId).Where(x => x.CustomerId == customer.CustomerId).FirstOrDefault().TotalDebt.ToString();
                                        var totalCredit = _TransactionRepo.GetAllTransactions()
                                                          .OrderByDescending(x => x.TransactionId).Where(x => x.CustomerId == customer.CustomerId).FirstOrDefault().AmountPayable.ToString();
                                        txt_TotalDebt.Text = Utilities.CurrencyFormat(totalDebt); txt_TotalCredit.Text = Utilities.CurrencyFormat(totalCredit);
                                        if (!bgwGetRecords.IsBusy)
                                        {
                                            bgwGetRecords.RunWorkerAsync();
                                        }
                                    }
                                    break;

                                case DialogResult.No:
                                    return;
                                }
                            }
                        }
                        else
                        {
                            var debt      = decimal.Parse(txt_AmountCollected.Text);
                            var debtMoney = Utilities.CurrencyFormat(debt.ToString());
                            switch (MessageBox.Show($"You are about to loan {debtMoney} to {customer.FullName}. \n\nDo you wish to continue?", "Superior Investment", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
                            {
                            case DialogResult.Yes:
                                Transactions transactions = new Transactions()
                                {
                                    CustomerId        = int.Parse(cmb_Customers.SelectedValue.ToString()),
                                    AmountContributed = 0m,
                                    AmountCollected   = decimal.Parse(txt_AmountCollected.Text),
                                    TransactionType   = "Debit",
                                    Date            = date,
                                    Commission      = 0m,
                                    ExtraCommission = 0m,
                                    AmountPayable   = decimal.Parse(txt_AmountCollected.Text),
                                    Debt            = debt,
                                    TotalDebt       = customerTransaction != null ? customerTransaction.TotalDebt + debt : debt,
                                    CreatedBy       = Utilities.USERNAME,
                                    CreatedDate     = date
                                };
                                if (_TransactionRepo.DebitTransaction(transactions))
                                {
                                    MessageBox.Show($"Customer has been loaned the sum of {debtMoney} successfully!", "Superior Investment", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    var totalDebt = _TransactionRepo.GetAllTransactions()
                                                    .OrderByDescending(x => x.TransactionId).Where(x => x.CustomerId == customer.CustomerId).FirstOrDefault().TotalDebt.ToString();
                                    var totalCredit = _TransactionRepo.GetAllTransactions()
                                                      .OrderByDescending(x => x.TransactionId).Where(x => x.CustomerId == customer.CustomerId).FirstOrDefault().AmountPayable.ToString();
                                    txt_TotalDebt.Text = Utilities.CurrencyFormat(totalDebt); txt_TotalCredit.Text = Utilities.CurrencyFormat(totalCredit);
                                    if (!bgwGetRecords.IsBusy)
                                    {
                                        bgwGetRecords.RunWorkerAsync();
                                    }
                                }
                                break;

                            case DialogResult.No:
                                return;
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please enter an amount!", "Superior Investment", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txt_AmountCollected.Focus();
                    }
                }
                else
                {
                    MessageBox.Show("Please select atleast one customer to debit!", "Superior Investment", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"{Utilities.ERRORMESSAGE} \n Error details: {ex.Message}", "Superior Investment!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }