public void CreateOrUpdate(ReceptionBindingModel model)
        {
            using (var context = new SchoolAgainDatabase())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        Reception element = model.Id.HasValue ? null : new Reception();
                        if (model.Id.HasValue)
                        {
                            element = context.Receptions.FirstOrDefault(rec => rec.Id ==
                                                                        model.Id);
                            if (element == null)
                            {
                                throw new Exception("Элемент не найден");
                            }
                            element.ClientId        = model.ClientId;
                            element.DateCreate      = model.DateCreate;
                            element.TotalSum        = model.TotalSum;
                            element.ReceptionStatus = model.ReceptionStatus;
                            context.SaveChanges();
                        }
                        else
                        {
                            element.ClientId        = model.ClientId;
                            element.DateCreate      = model.DateCreate;
                            element.TotalSum        = model.TotalSum;
                            element.ReceptionStatus = model.ReceptionStatus;
                            context.Receptions.Add(element);
                            context.SaveChanges();
                            var groupServices = model.ReceptionServices
                                                .GroupBy(rec => rec.ServiceId)
                                                .Select(rec => new
                            {
                                ServiceId = rec.Key,
                                Count     = rec.Sum(r => r.Count)
                            });

                            foreach (var groupService in groupServices)
                            {
                                context.ReceptionServices.Add(new ReceptionService
                                {
                                    ReceptionId = element.Id,
                                    ServiceId   = groupService.ServiceId,
                                    Count       = groupService.Count
                                });
                                context.SaveChanges();
                            }
                        }
                        transaction.Commit();
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
        public void SaveReceptionPaymentsToPdfFile(string fileName, ReceptionBindingModel reception, string email)
        {
            string title = "Список записей в период с " + reception.Date.ToString() + " по " + reception.DateTo.ToString();

            SaveToPdf.CreateDoc(new PdfInfo
            {
                FileName   = fileName,
                Title      = title,
                Receptions = receptionLogic.Read(reception).ToList(),
                Payments   = GetReceptionPayments(reception)
            });
            SendMail(email, fileName, title);
        }
        public Dictionary <int, List <PaymentViewModel> > GetReceptionPayments(ReceptionBindingModel model)
        {
            var receptions = receptionLogic.Read(model).ToList();
            Dictionary <int, List <PaymentViewModel> > payments = new Dictionary <int, List <PaymentViewModel> >();

            foreach (var reception in receptions)
            {
                var receptionPayments = paymentLogic.Read(new PaymentBindingModel {
                    ReceptionId = reception.Id
                }).ToList();
                payments.Add(reception.Id, receptionPayments);
            }
            return(payments);
        }
        public void Delete(ReceptionBindingModel model)
        {
            using (var context = new SchoolAgainDatabase())
            {
                Reception element = context.Receptions.FirstOrDefault(rec => rec.Id == model.Id.Value);

                if (element != null)
                {
                    context.Receptions.Remove(element);
                    context.SaveChanges();
                }
                else
                {
                    throw new Exception("Элемент не найден");
                }
            }
        }
 public List <ReceptionViewModel> Read(ReceptionBindingModel model)
 {
     using (var context = new SchoolAgainDatabase())
     {
         return(context.Receptions.Where(rec => rec.Id == model.Id ||
                                         (rec.ClientId == model.ClientId) &&
                                         (model.Date == null &&
                                          model.DateTo == null ||
                                          rec.DateCreate >= model.Date &&
                                          rec.DateCreate <= model.DateTo))
                .Select(rec => new ReceptionViewModel
         {
             Id = rec.Id,
             ClientId = rec.ClientId,
             ClientFIO = rec.Client.ClientFIO,
             TotalSum = rec.TotalSum,
             DateCreate = rec.DateCreate,
             LeftSum = rec.TotalSum - context.Payments.Where(recP => recP.ReceptionId == rec.Id).Select(recP => recP.Sum).Sum(),
             ReceptionStatus = rec.ReceptionStatus,
             ReceptionServices = GetReceptionServiceViewModel(rec)
         })
                .ToList());
     }
 }