private static void FillListOfCashReceiptsUsed(Payment loanPayment, PrintLoanPayment loanPaymentModel) { var cashPayments = from p in Context.Payments join rpa in Context.ReceiptPaymentAssocs on p.Id equals rpa.PaymentId where p.ParentPaymentId == loanPayment.Id && p.PaymentMethodTypeId == PaymentMethodType.CashType.Id && p.PaymentTypeId == PaymentType.LoanPayment.Id select rpa; var customerCashPayments = from crp in cashPayments join rpa in Context.ReceiptPaymentAssocs on crp.ReceiptId equals rpa.ReceiptId join p in Context.Payments on rpa.PaymentId equals p.Id where p.PaymentTypeId == PaymentType.Receipt.Id && p.TransactionDate < loanPayment.TransactionDate select new { ActualPayment = crp.Payment, ReceivePayment = p}; var customerCashPaymentsToday = from crp in cashPayments join rpa in Context.ReceiptPaymentAssocs on crp.ReceiptId equals rpa.ReceiptId join p in Context.Payments on rpa.PaymentId equals p.Id where p.PaymentTypeId == PaymentType.Receipt.Id && p.TransactionDate == loanPayment.TransactionDate select new { ActualPayment = crp.Payment, ReceivePayment = p }; var modelsFromPreviousReceipts = new List<PaymentModel>(); foreach (var cashPayment in customerCashPayments) { var amountTendered = cashPayments.FirstOrDefault(entity => entity.PaymentId == cashPayment.ActualPayment.Id); PaymentModel paymentCash = new PaymentModel(); paymentCash.PaymentMethod = "Cash Previous Receipt"; paymentCash.BankName = "NA"; paymentCash.BankBranch = "NA"; paymentCash.CheckNumber = "NA"; paymentCash.TransactionDate = cashPayment.ReceivePayment.TransactionDate; paymentCash.AmountTendered = amountTendered.Amount; paymentCash.AmountApplied = cashPayment.ActualPayment.TotalAmount; modelsFromPreviousReceipts.Add(paymentCash); } loanPaymentModel.CashReceiptsFromPreviousTransaction = modelsFromPreviousReceipts; var models = new List<PaymentModel>(); foreach (var cashPayment in customerCashPaymentsToday) { var amountTendered = cashPayments.FirstOrDefault(entity => entity.PaymentId == cashPayment.ActualPayment.Id); PaymentModel paymentCash = new PaymentModel(); paymentCash.PaymentMethod = "Cash"; paymentCash.BankName = "NA"; paymentCash.BankBranch = "NA"; paymentCash.CheckNumber = "NA"; paymentCash.TransactionDate = cashPayment.ReceivePayment.TransactionDate; paymentCash.AmountTendered = amountTendered.Amount; paymentCash.AmountApplied = cashPayment.ActualPayment.TotalAmount; models.Add(paymentCash); } loanPaymentModel.CashPayments = models; }
public static PrintLoanPayment Create(Payment loanPayment) { if (loanPayment.LoanPayment == null) throw new ArgumentException("Please supply the parent of the payments."); var customer = PartyRole.GetById(loanPayment.ProcessedToPartyRoleId.Value); var teller = PartyRole.GetById(loanPayment.ProcessedByPartyRoleId); var district = Context.CustomerClassifications.SingleOrDefault(entity => entity.PartyRoleId == customer.Id); decimal ownerOutstandingLoan = loanPayment.LoanPayment.OwnerOutstandingLoan + loanPayment.LoanPayment.OwnerOutstandingInterest; decimal coOwnerOutsandingLoan = loanPayment.LoanPayment.CoOwnerOutstandingInterest + loanPayment.LoanPayment.CoOwnerOutstandingLoan; PrintLoanPayment model = new PrintLoanPayment(); model.ReceiveFrom = customer.Party.Name; model.Teller = teller.Party.Name; model.StationNumber = district != null ? district.ClassificationType.StationNumber : ""; model.TransactionDate = loanPayment.TransactionDate; model.Total = loanPayment.TotalAmount; model.TotalPrincipalDue = GetTotalPrincipalDue(loanPayment); model.TotalInterestDue = GetTotalInterestDue(loanPayment); model.Total = model.TotalPrincipalDue + model.TotalInterestDue; model.CustomerLoanBalance = ownerOutstandingLoan; model.CustomerAsCoOwnerLoanBalance = coOwnerOutsandingLoan; model.TotalLoanBalance = model.CustomerLoanBalance + model.CustomerAsCoOwnerLoanBalance; model.Cheques = GetListOfCheques(loanPayment); model.ATMPayments = GetATMPayments(loanPayment); FillListOfCashReceiptsUsed(loanPayment, model); model.CalculateAmountTendered(); model.Change = model.AmountTendered - model.Total; var controlNumber = ControlNumberFacade.GetByPaymentId(loanPayment.Id, FormType.PaymentFormType); model.ControlNumber = controlNumber.LastControlNumber; return model; }