public void PrepareForSave(LoanApplication loanApplication, PartyRole customerPartyRole, int employeePartyRole, DateTime today) { if (this.IsNew) { var assoc = Context.ChequeApplicationAssocs.Where(e => e.ApplicationId == loanApplication.ApplicationId); if (assoc != null) { foreach (var item in assoc) { var cheque = Context.Cheques.SingleOrDefault(entity => entity.Id == item.ChequeId); var payments = Payment.GetById(cheque.PaymentId); var rpAssoc = Context.ReceiptPaymentAssocs.SingleOrDefault(entity => entity.PaymentId == payments.Id); Receipt receipts = Context.Receipts.SingleOrDefault(entity => entity.Id == rpAssoc.ReceiptId); Context.ChequeApplicationAssocs.DeleteObject(item); Context.Cheques.DeleteObject(cheque); Context.Receipts.DeleteObject(receipts); Context.Payments.DeleteObject(payments); Context.ReceiptPaymentAssocs.DeleteObject(rpAssoc); } } //create Payment var transactionDate = DateTime.Today; Payment payment = Payment.CreatePayment(today, transactionDate, customerPartyRole.Id, employeePartyRole, this.Amount, PaymentType.Receipt, PaymentMethodType.PersonalCheckType, SpecificPaymentType.LoanPaymentType, this.ChequeNumber); Context.Payments.AddObject(payment); //create cheque Cheque newCheck = new Cheque(); newCheck.BankPartyRoleId = this.BankId; newCheck.CheckDate = this.ChequeDate; newCheck.Payment = payment; Context.Cheques.AddObject(newCheck); //create cheque association ChequeApplicationAssoc chequeAssoc = new ChequeApplicationAssoc(); chequeAssoc.Cheque = newCheck; chequeAssoc.Application = loanApplication.Application; //create receipt and receipt payment assoc Receipt receipt = Receipt.CreateReceipt(null, this.Amount); Receipt.CreateReceiptPaymentAssoc(receipt, payment); //Context.SaveChanges(); } else if (this.ToBeDeleted) { var payment = Payment.GetById(this.PaymentId); //TODO:: Changed to new payment var cheque = Context.Cheques.SingleOrDefault(entity => entity.Id == this.ChequeId && entity.PaymentId == payment.Id); var rpAssoc = Context.ReceiptPaymentAssocs.SingleOrDefault(entity => entity.PaymentId == payment.Id); var assoc = Context.ChequeApplicationAssocs.SingleOrDefault(entity => entity.ChequeId == cheque.Id); //TODO:: Changed to new payment Context.ChequeApplicationAssocs.DeleteObject(assoc); Context.Cheques.DeleteObject(cheque); Context.Receipts.DeleteObject(rpAssoc.Receipt); Context.Payments.DeleteObject(rpAssoc.Payment); Context.ReceiptPaymentAssocs.DeleteObject(rpAssoc); } else if (this.IsNew == false && this.ToBeDeleted == false) { var payment = Context.Payments.SingleOrDefault(entity => entity.Id == this.PaymentId); payment.TransactionDate = this.TransactionDate; payment.PaymentReferenceNumber = this.ChequeNumber; var cheque = Context.Cheques.SingleOrDefault(entity => entity.Id == this.ChequeId && entity.PaymentId == payment.Id); cheque.BankPartyRoleId = this.BankId; } }
private void ChequeModelPrepareForSave(FinancialAccount financialAccount, PartyRole customerPartyRole, int employeePartyRoleId, DateTime today, ChequeModel model) { if (model.IsNew) { var application = financialAccount.Agreement.Application; var loanAccount = financialAccount.LoanAccount; //new payment Payment payment = CreatePayment(customerPartyRole, employeePartyRoleId, model, today); //new cheque Cheque newCheck = new Cheque(); newCheck.BankPartyRoleId = model.BankId; newCheck.CheckDate = model.ChequeDate; newCheck.Payment = payment; //new cheque association ChequeApplicationAssoc chequeAssoc = new ChequeApplicationAssoc(); chequeAssoc.Cheque = newCheck; chequeAssoc.Application = application; //new cheque loan association ChequeLoanAssoc chequeLoanAssoc = new ChequeLoanAssoc(); chequeLoanAssoc.Cheque = newCheck; chequeLoanAssoc.LoanAccount = loanAccount; //new receipt Receipt newReceipt = CreateReceipt(customerPartyRole, payment, model); //new receipt payment assoc ReceiptPaymentAssoc newReceiptAssoc = CreateReceiptPaymentAssoc(payment, newReceipt); //new receipt status CreateReceiptStatus(newReceipt, model, today); //new cheque status CreateChequeStatus(newCheck, model, today); Context.Cheques.AddObject(newCheck); } else if (model.ToBeDeleted) { //throw new NotImplementedException(); var payment = Context.Payments.SingleOrDefault(entity => entity.Id == model.PaymentId); var receiptPaymentAssoc = Context.ReceiptPaymentAssocs.SingleOrDefault(entity => entity.PaymentId == payment.Id); var receipts = receiptPaymentAssoc.Receipt; var cheque = Context.Cheques.SingleOrDefault(entity => entity.Id == model.ChequeId && entity.PaymentId == payment.Id); var assoc = Context.ChequeApplicationAssocs.SingleOrDefault(entity => entity.ChequeId == cheque.Id); Context.ReceiptPaymentAssocs.DeleteObject(receiptPaymentAssoc); Context.Receipts.DeleteObject(receipts); Context.ChequeApplicationAssocs.DeleteObject(assoc); Context.Cheques.DeleteObject(cheque); Context.Payments.DeleteObject(payment); } }