public static void CreateMember(MemberModel model) { using (var ctx = new DFAppEntities()) { var existingJMBG = ctx.Members.FirstOrDefault(x => x.JMBG == model.JMBG); if (existingJMBG != null) { throw new Exception("error_members_jmbg_exists"); } DBModel.Members m = new DBModel.Members(); m.BirthDate = model.BirthDate; m.BirthPlace = model.BirthPlace; m.FirstName = model.FirstName; m.LastName = model.LastName; m.JMBG = model.JMBG; m.AgeCategoryID = model.AgeCategoryID; m.ProfileImage = model.ProfileImage; m.IsActive = true; m.Note = model.Note; if (model.IsCompetitor.HasValue) { m.IsCompetitor = (bool)model.IsCompetitor; } else { m.IsCompetitor = false; } m.ContactData = new ContactData(); m.ContactData.Address = model.ContactData.Address; m.ContactData.Email = model.ContactData.Email; m.ContactData.Phone1 = model.ContactData.Phone1; m.ContactData.Phone2 = model.ContactData.Phone2; m.ContactData.Phone3 = model.ContactData.Phone3; if (model.DanceGroupID.HasValue) { DanceGroupMembers dgMembers = new DanceGroupMembers(); dgMembers.DanceGroupID = (int)model.DanceGroupID; dgMembers.MemberID = m.MemberID; ctx.DanceGroupMembers.Add(dgMembers); } ctx.Members.Add(m); 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); } } } } }