public static void AddMemberPayment(int memberID, MemberPaymentModel model) { using (var ctx = new DFAppEntities()) { var member = ctx.Members.FirstOrDefault(m => m.MemberID == memberID); if (member != null) { MemberPayments memberPayment = new MemberPayments() { MemberID = memberID, PaymentID = model.ID, DiscountPercentage = model.DiscountPercentage }; ctx.MemberPayments.Add(memberPayment); if (model.Companions != null && model.Companions.Count() > 0) { foreach (var companion in model.Companions) { MemberPaymentsForCompanions companionPayment = new MemberPaymentsForCompanions() { MemberID = memberID, PaymentID = model.ID, CompanionName = companion.Name, CompanionPhone = companion.Phone, CompanionEmail = companion.Email }; ctx.MemberPaymentsForCompanions.Add(companionPayment); } } foreach (var installment in model.Installments) { MemberPaymentInstallments memberInstallment = new MemberPaymentInstallments() { MemberID = memberID, PaymentID = model.ID, InstallmentDate = (DateTime)installment.InstallmentDate, Amount = installment.Amount, IsPaid = false, IsCanceled = false }; ctx.MemberPaymentInstallments.Add(memberInstallment); } ctx.SaveChanges(); } } }
// this is executed by an agent every first day of the month public static void CreateNewMonthlyInstallments() { var latestMonthlyInstallments = Payments.GetLatestMonthlyInstallments(); if (latestMonthlyInstallments != null && latestMonthlyInstallments.Count() > 0) { using (var ctx = new DFAppEntities()) { foreach (var monthlyInstallment in latestMonthlyInstallments) { if (DateTime.Now.Year == monthlyInstallment.InstallmentDate.Year || (monthlyInstallment.InstallmentDate.Month == 12 && monthlyInstallment.InstallmentDate.Year == DateTime.Now.Year - 1)) { int diff = DateTime.Now.Month - monthlyInstallment.InstallmentDate.Month; if (diff == 0) { diff++; } if (diff < 0) { diff = 1; } MemberPaymentInstallments newMonthlyInstallment = new MemberPaymentInstallments() { MemberID = monthlyInstallment.MemberID, PaymentID = monthlyInstallment.PaymentID, InstallmentDate = monthlyInstallment.InstallmentDate.AddMonths(diff), Amount = monthlyInstallment.Amount, IsCanceled = false, IsPaid = false }; ctx.MemberPaymentInstallments.Add(newMonthlyInstallment); } } ctx.SaveChanges(); } } }
public static void UpdateMemberPayments(DFAppEntities ctx, DBModel.Members existingMember) { var memberPayments = existingMember.MemberPayments .Where(x => x.Payments.Type.ToUpper() == "MONTHLY" && x.Payments.Active) .ToList(); if (memberPayments != null && memberPayments.Count() > 0) { foreach (var memberPayment in memberPayments) { var installments = ctx.MemberPaymentInstallments.Where(x => x.MemberID == existingMember.MemberID && x.PaymentID == memberPayment.PaymentID).OrderByDescending(x => x.InstallmentDate).ToList(); var currentInstallment = installments?.ElementAt(0); if (currentInstallment != null) { int installmentDay = currentInstallment.InstallmentDate.Day; DateTime installmentDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, installmentDay); // check if there is already an installment with the given 'installmentDate' var existingInstallment = installments.FirstOrDefault(x => x.InstallmentDate.Date == installmentDate.Date); if (existingInstallment == null) { var newInstallment = new MemberPaymentInstallments { MemberID = existingMember.MemberID, PaymentID = memberPayment.PaymentID, InstallmentDate = installmentDate, Amount = currentInstallment.Amount, IsPaid = false, IsCanceled = false }; ctx.MemberPaymentInstallments.Add(newInstallment); } } } } }