private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { var grid = (sender as DataGridView); var rowIndex = grid.CurrentCell.RowIndex; var cus = grid.Rows[grid.CurrentCell.RowIndex].DataBoundItem as Customer; var seqNo = cus.CustomerSeqNumber; // FormGeneral.GetGridCellValue(grid, rowIndex, "CustomerSeqNumber"); var customerId = cus.CustomerId; // FormGeneral.GetGridCellValue(grid, rowIndex, "CustomerId"); var loanAmount = cus.LoanAmount; // FormGeneral.GetGridCellValue(grid, rowIndex, "LoanAmount"); var collectedAmount = FormGeneral.GetGridCellValue(grid, rowIndex, "CollectionAmt"); if (string.IsNullOrEmpty(collectedAmount) == false) { var txn = new Transaction() { AmountReceived = Convert.ToInt32(collectedAmount), CustomerId = customerId, CustomerSequenceNo = seqNo, TransactionId = Transaction.GetNextTransactionId(), Balance = (Transaction.GetBalance(cus) - Convert.ToInt16(collectedAmount)), // TODO: Balance is not updaed correctly eg: 71-104 - 19th july txn. TxnDate = dateTimePicker1.Value }; if (txn.Balance < 0) { MessageBox.Show("Balance is less than 0. Please check your amount. Txn Aborted!"); LogHelper.WriteLog($"Balance is less than 0. Please check your amount. Txn Aborted!", txn.CustomerId, txn.CustomerSequenceNo); return; } if (txn.Balance == 0) { MessageBox.Show("Good News, This txn will be closed!"); LogHelper.WriteLog("Good News, This txn will be closed!", txn.CustomerId, txn.CustomerSequenceNo); Customer.CloseCustomerTxn(cus, false, txn.TxnDate); // new Customer() { CustomerId = txn.CustomerId, CustomerSeqNumber = txn.CustomerSequenceNo, IsActive = false, ClosedDate = txn.TxnDate }); } txn.IsClosed = (txn.Balance <= 0); // Add new Txn. var existingTxn = Transaction.GetTransactionForDate(txn); if (existingTxn == null || existingTxn.Count == 0) { Transaction.AddDailyTransactions(txn); } else { if (existingTxn.First().AmountReceived == 0) // Customer is giving money in the same day fo given date. { Transaction.AddDailyTransactions(txn); } else { txn.TransactionId = existingTxn.First().TransactionId; Transaction.UpdateTransactionDetails(txn); } } // Update txn Closed Date if (txn.IsClosed && txn.Balance == 0) { // Update Closed Date Customer.UpdateCustomerClosedDate( new Customer() { CustomerId = txn.CustomerId, CustomerSeqNumber = txn.CustomerSequenceNo, ClosedDate = txn.TxnDate, }); } return; } //var amountGivenDate = cus.AmountGivenDate; // FormGeneral.GetGridCellValue(grid, rowIndex, "AmountGivenDate"); //var closedDate = cus.ClosedDate; // FormGeneral.GetGridCellValue(grid, rowIndex, "ClosedDate"); //var interest = cus.Interest; // FormGeneral.GetGridCellValue(grid, rowIndex, "Interest"); //var name = cus.Name; // FormGeneral.GetGridCellValue(grid, rowIndex, "Name"); // Update Customer Created Date. Customer.CorrectCustomerData(cus); //Customer.CorrectCustomerData( // new Customer() // { // CustomerId = customerId, // CustomerSeqNumber = Convert.ToInt32(seqNo), // AmountGivenDate = Convert.ToDateTime(amountGivenDate), // ClosedDate = Convert.ToDateTime(closedDate), // Interest = Convert.ToInt32(interest), // LoanAmount = Convert.ToInt32(loanAmount), // Name = name // }); }