Example #1
0
 private void btn_Credit_Click(object sender, EventArgs e)
 {
     try
     {
         if (cmb_Customers.SelectedValue != null)
         {
             if (txt_AmountContributed.Text != string.Empty)
             {
                 var money = Utilities.CurrencyFormat(txt_AmountContributed.Text);
                 txt_AmountContributed.Text = Utilities.RemoveCommasAndDots(txt_AmountContributed.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;
                 }
                 Transactions transactions = new Transactions()
                 {
                     CustomerId        = int.Parse(cmb_Customers.SelectedValue.ToString()),
                     AmountContributed = decimal.Parse(txt_AmountContributed.Text),
                     AmountCollected   = 0m,
                     TransactionType   = "Credit",
                     TotalDebt         = customerTransaction != null?customerTransaction.TotalDebt.HasValue?decimal.Parse(txt_AmountContributed.Text) < customerTransaction.TotalDebt ? customerTransaction.TotalDebt - decimal.Parse(txt_AmountContributed.Text) : 0m : customerTransaction.TotalDebt : 0m,
                     Date            = date,
                     Commission      = 0m,
                     ExtraCommission = 0m,
                     AmountPayable   = customerTransaction != null?customerTransaction.TotalDebt.HasValue?decimal.Parse(txt_AmountContributed.Text) >= customerTransaction.TotalDebt ? decimal.Parse(txt_AmountContributed.Text) - customerTransaction.TotalDebt : 0m : decimal.Parse(txt_AmountContributed.Text) : decimal.Parse(txt_AmountContributed.Text),
                     CreatedBy       = Utilities.USERNAME,
                     CreatedDate     = date
                 };
                 if (_TransactionRepo.CreditTransaction(transactions))
                 {
                     MessageBox.Show("Customer credited 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
             {
                 MessageBox.Show("Please enter an amount", "Superior Investment", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 txt_AmountContributed.Focus();
             }
         }
         else
         {
             MessageBox.Show("Please select atleast one customer to credit!", "Superior Investment", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show($"{Utilities.ERRORMESSAGE} \n Error details: {ex.Message}", "Superior Investment!", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }