public bool SavePayment(PaymentVM paymentVM, int userID) { using (var dbTransaction = unitOfWork.dbContext.Database.BeginTransaction()) { try { //Save Payment tblPayment payment = new tblPayment(); payment.ClientID = paymentVM.ClientID; payment.PaymentAmount = paymentVM.PaymentAmount; payment.CreatedBy = userID; payment.CreatedDate = DateTime.Now; unitOfWork.TblPaymentRepository.Insert(payment); unitOfWork.Save(); //Save Debit Note foreach (var debitNoteVM in paymentVM.DebitNoteList) { tblDebitNote debitNote = new tblDebitNote(); debitNote.TotalNonCommissionPremium = debitNoteVM.TotalNonCommissionPremium; debitNote.TotalGrossPremium = debitNoteVM.TotalGrossPremium; debitNote.CreatedBy = userID; debitNote.CreatedDate = DateTime.Now; unitOfWork.TblDebitNoteRepository.Insert(debitNote); unitOfWork.Save(); //Save Policy Info Payments foreach (var policyInfoPaymentVM in debitNoteVM.PolicyInfoPaymentLists) { tblBankTransactionDetail Bank = new tblBankTransactionDetail(); Bank.BankID = policyInfoPaymentVM.BankID; Bank.DraftNo = policyInfoPaymentVM.DraftNo; Bank.PaymentID = policyInfoPaymentVM.PaymentMethodID; Bank.Amount = policyInfoPaymentVM.BankAmount; Bank.AgentID = policyInfoPaymentVM.AgentID; Bank.AgentAmount = policyInfoPaymentVM.AgentAmount; Bank.ClientID = paymentVM.ClientID; Bank.PolicyInfoRecID = debitNoteVM.PolicyInfoRecID; Bank.currencyType = policyInfoPaymentVM.currencyType; Bank.ExchangeRate = policyInfoPaymentVM.ExchangeRate; Bank.PaymentDate = !string.IsNullOrEmpty(policyInfoPaymentVM.PaymentDate) ? DateTime.ParseExact(policyInfoPaymentVM.PaymentDate, "dd/MM/yyyy", CultureInfo.InvariantCulture) : (DateTime?)null; // Bank.IBSAmount = policyInfoPaymentVM.SGSAmount; Bank.RequestDate = !string.IsNullOrEmpty(policyInfoPaymentVM.RequestDate) ? DateTime.ParseExact(policyInfoPaymentVM.RequestDate, "dd/MM/yyyy", CultureInfo.InvariantCulture) : (DateTime?)null; unitOfWork.TblBankTransactionDetailRepository.Insert(Bank); unitOfWork.Save(); tblPolicyInfoPayment policyInfoPayment = new tblPolicyInfoPayment(); policyInfoPayment.PolicyInfoRecID = debitNoteVM.PolicyInfoRecID; policyInfoPayment.NonCommissionPremium = debitNoteVM.TotalNonCommissionPremium; policyInfoPayment.GrossPremium = policyInfoPaymentVM.BankAmount; policyInfoPayment.CreatedBy = userID; policyInfoPayment.CreatedDate = DateTime.Now; unitOfWork.TblPolicyInfoPaymentRepository.Insert(policyInfoPayment); unitOfWork.Save(); //foreach (var policyInfoPaymentObj in debitNoteVM.PolicyInfoPaymentList) //{ tblPolicyDebitNote policyDebitNote = new tblPolicyDebitNote(); policyDebitNote.PolicyInfoPaymentID = policyInfoPayment.PolicyInfoPaymentID; policyDebitNote.DebitNoteID = debitNote.DebitNoteID; policyDebitNote.PaymentID = payment.PaymentID; unitOfWork.TblPolicyDebitNoteRepository.Insert(policyDebitNote); unitOfWork.Save(); // } // policyInfoPaymentVM.PolicyInfoPaymentID = policyInfoPayment.PolicyInfoPaymentID; //Save Policy Info Charges //foreach (var policyInfoChargeVM in policyInfoPaymentVM.PolicyInfoChargeList) //{ // tblPolicyInfoCharge policyInfoCharge = new tblPolicyInfoCharge(); // policyInfoCharge.PolicyInfoPaymentID = policyInfoPaymentVM.PolicyInfoRecID; // policyInfoCharge.ChargeTypeID = policyInfoChargeVM.ChargeTypeID; // policyInfoCharge.Amount = policyInfoChargeVM.Amount; // policyInfoCharge.IsCR = policyInfoChargeVM.IsCR; // policyInfoCharge.CreatedBy = userID; // policyInfoCharge.CreatedDate = DateTime.Now; // unitOfWork.TblPolicyInfoChargeRepository.Insert(policyInfoCharge); // unitOfWork.Save(); //} //} //Save Policy Info Payment - Debit Note } } //Complete the Transaction dbTransaction.Commit(); return(true); } catch (Exception ex) { //Roll back the Transaction dbTransaction.Rollback(); return(false); } } }
public bool UpdatePayment(PaymentVM paymentVM, int userID) { using (var dbTransaction = unitOfWork.dbContext.Database.BeginTransaction()) { try { //Update Payment tblPayment payment = unitOfWork.TblPaymentRepository.GetByID(paymentVM.PaymentID); payment.ClientID = paymentVM.ClientID; payment.PaymentAmount = paymentVM.PaymentAmount; payment.ModifiedBy = userID; payment.ModifiedDate = DateTime.Now; unitOfWork.TblPaymentRepository.Update(payment); unitOfWork.Save(); List <tblPolicyDebitNote> policyDebitNoteList = unitOfWork.TblPolicyDebitNoteRepository.Get(x => x.PaymentID == payment.PaymentID).ToList(); List <int> debitNoteList = policyDebitNoteList.GroupBy(x => x.DebitNoteID).Select(x => x.FirstOrDefault()).Select(x => (int)x.DebitNoteID).ToList(); List <int> policyInfoPaymentList = policyDebitNoteList.GroupBy(x => x.PolicyInfoPaymentID).Select(x => x.FirstOrDefault()).Select(x => (int)x.PolicyInfoPaymentID).ToList(); //Delete Existing Policy Debit Note Details foreach (var policyDebitNote in policyDebitNoteList) { unitOfWork.TblPolicyDebitNoteRepository.Delete(policyDebitNote.PolicyDebitNoteID); unitOfWork.Save(); } //Delete Existing Debit Note Details foreach (var debitNote in debitNoteList) { unitOfWork.TblDebitNoteRepository.Delete(debitNote); unitOfWork.Save(); } //Delete Policy Info Payment and Policy Info Charge Details foreach (var policyInfoPayment in policyInfoPaymentList) { List <tblPolicyInfoCharge> policyInfoChargeList = unitOfWork.TblPolicyInfoChargeRepository.Get(x => x.PolicyInfoPaymentID == policyInfoPayment).ToList(); foreach (var policyInfoCharge in policyInfoChargeList) { unitOfWork.TblPolicyInfoChargeRepository.Delete(policyInfoCharge); unitOfWork.Save(); } unitOfWork.TblPolicyInfoPaymentRepository.Delete(policyInfoPayment); unitOfWork.Save(); } //Save Debit Note foreach (var debitNoteVM in paymentVM.DebitNoteList) { tblDebitNote debitNote = new tblDebitNote(); debitNote.TotalNonCommissionPremium = debitNoteVM.TotalNonCommissionPremium; debitNote.TotalGrossPremium = debitNoteVM.TotalGrossPremium; debitNote.CreatedBy = payment.CreatedBy; debitNote.CreatedDate = payment.CreatedDate; debitNote.ModifiedBy = userID; debitNote.ModifiedDate = DateTime.Now; unitOfWork.TblDebitNoteRepository.Insert(debitNote); unitOfWork.Save(); //Save Policy Info Payments //foreach (var policyInfoPaymentVM in debitNoteVM.PolicyInfoPaymentList) //{ // tblPolicyInfoPayment policyInfoPayment = new tblPolicyInfoPayment(); // policyInfoPayment.PolicyInfoRecID = policyInfoPaymentVM.PolicyInfoRecID; // unitOfWork.TblBankTransactionDetailRepository.Delete(policyInfoPayment.PolicyInfoRecID); // unitOfWork.Save(); // policyInfoPayment.NonCommissionPremium = policyInfoPaymentVM.NonCommissionPremium; // policyInfoPayment.GrossPremium = policyInfoPaymentVM.GrossPremium; // policyInfoPayment.CreatedBy = payment.CreatedBy; // policyInfoPayment.CreatedDate = payment.CreatedDate; // policyInfoPayment.ModifiedBy = userID; // policyInfoPayment.ModifiedDate = DateTime.Now; // unitOfWork.TblPolicyInfoPaymentRepository.Insert(policyInfoPayment); // unitOfWork.Save(); // policyInfoPaymentVM.PolicyInfoPaymentID = policyInfoPayment.PolicyInfoPaymentID; List <tblBankTransactionDetail> BankTransactionDetailList = unitOfWork.TblBankTransactionDetailRepository.Get(x => x.PolicyInfoRecID == debitNoteVM.PolicyInfoRecID).ToList(); foreach (var BankTransactionDetail in BankTransactionDetailList) { unitOfWork.TblBankTransactionDetailRepository.Delete(BankTransactionDetail); unitOfWork.Save(); } foreach (var policyInfoPaymentVMs in debitNoteVM.PolicyInfoPaymentLists) { tblBankTransactionDetail Bank = new tblBankTransactionDetail(); Bank.BankID = policyInfoPaymentVMs.BankID; Bank.DraftNo = policyInfoPaymentVMs.DraftNo; Bank.PaymentID = policyInfoPaymentVMs.PaymentMethodID; Bank.Amount = policyInfoPaymentVMs.BankAmount; Bank.AgentID = policyInfoPaymentVMs.AgentID; Bank.AgentAmount = policyInfoPaymentVMs.AgentAmount; Bank.ClientID = paymentVM.ClientID; Bank.PolicyInfoRecID = policyInfoPaymentVMs.PolicyInfoRecID; Bank.PolicyInfoRecID = debitNoteVM.PolicyInfoRecID; // Bank.IBSAmount = policyInfoPaymentVM.SGSAmount; Bank.RequestDate = !string.IsNullOrEmpty(policyInfoPaymentVMs.RequestDate) ? DateTime.ParseExact(policyInfoPaymentVMs.RequestDate, "dd/MM/yyyy", CultureInfo.InvariantCulture) : (DateTime?)null; unitOfWork.TblBankTransactionDetailRepository.Insert(Bank); unitOfWork.Save(); tblPolicyInfoPayment policyInfoPayment = new tblPolicyInfoPayment(); policyInfoPayment.PolicyInfoRecID = policyInfoPaymentVMs.PolicyInfoRecID; policyInfoPayment.NonCommissionPremium = debitNoteVM.TotalNonCommissionPremium; policyInfoPayment.GrossPremium = policyInfoPaymentVMs.BankAmount; policyInfoPayment.CreatedBy = userID; policyInfoPayment.CreatedDate = DateTime.Now; unitOfWork.TblPolicyInfoPaymentRepository.Insert(policyInfoPayment); unitOfWork.Save(); //foreach (var policyInfoPaymentObj in debitNoteVM.PolicyInfoPaymentList) //{ tblPolicyDebitNote policyDebitNote = new tblPolicyDebitNote(); policyDebitNote.PolicyInfoPaymentID = policyInfoPayment.PolicyInfoPaymentID; policyDebitNote.DebitNoteID = debitNote.DebitNoteID; policyDebitNote.PaymentID = payment.PaymentID; unitOfWork.TblPolicyDebitNoteRepository.Insert(policyDebitNote); unitOfWork.Save(); //} } //Save Policy Info Charges // foreach (var policyInfoChargeVM in policyInfoPaymentVM.PolicyInfoChargeList) //{ // tblPolicyInfoCharge policyInfoCharge = new tblPolicyInfoCharge(); // policyInfoCharge.PolicyInfoPaymentID = policyInfoPaymentVM.PolicyInfoRecID; // policyInfoCharge.ChargeTypeID = policyInfoChargeVM.ChargeTypeID; // policyInfoCharge.Amount = policyInfoChargeVM.Amount; // policyInfoCharge.IsCR = policyInfoChargeVM.IsCR; // policyInfoCharge.CreatedBy = payment.CreatedBy; // policyInfoCharge.CreatedDate = payment.CreatedDate; // policyInfoCharge.ModifiedBy = userID; // policyInfoCharge.ModifiedDate = DateTime.Now; // unitOfWork.TblPolicyInfoChargeRepository.Insert(policyInfoCharge); // unitOfWork.Save(); //} // } //Save Policy Info Payment - Debit Note } //Complete the Transaction dbTransaction.Commit(); return(true); } catch (Exception ex) { //Roll back the Transaction dbTransaction.Rollback(); return(false); } } }