public void CreateOrder(ViewModels.FZ.Home.InspectionOrder model)
        {
            DealerConsultant consultant = model.DealerConsultantId.HasValue ?
                                          _dealerConsultantsRepository.Value.GetQuery().FirstOrDefault(x => x.Id == model.DealerConsultantId.Value) : null;

            if (model.DealerConsultantId.HasValue && consultant == null)
            {
                throw new BusinessException("Данные страницы устарели. Пожалуйста перезагрузите сайт");
            }

            var order = new InspectionOrder
            {
                Gender               = model.Gender,
                LastName             = model.LastName,
                FirstName            = model.FirstName,
                Patronymic           = model.Patronymic,
                Phone                = model.Phone,
                Email                = model.Email,
                Comment              = model.Comment,
                CarId                = model.CarId ?? 0,
                CarManufactureYear   = model.CarManufactureYear.Value,
                CarVin               = model.CarVin,
                DealerConsultantCode = consultant?.Code,
                DealerConsultantName = consultant?.Firstname + " " + consultant?.Surname,
                ToType               = model.ToType,
                DealerId             = model.DealerId.Value,
                OrderDateTime        = model.OrderDate.Value.Date + model.OrderTime.Value,
                CreatedDateTime      = DateTime.Now,
                ModifiedDateTime     = DateTime.Now,
                OrderSource          = OrderSourceType.Desktop
            };

            _inspectionOrdersRepository.Value.Add(order);
            _inspectionOrdersRepository.Value.SaveChanges();

            _inspectionOrdersRepository.Value.ReferenceLoad(order, "Car", "Dealer");

            if (consultant == null)
            {
                _dealerSchedulesRepository.Value.First(s => s.DealerId == model.DealerId.Value && s.IntervalFrom == order.OrderDateTime).IsFree = false;
                _dealerSchedulesRepository.Value.SaveChanges();
            }

            else
            {
                _dealerConsultantSchedulesRepository.Value.First(s => s.DealerConsultant.Code == consultant.Code &&
                                                                 DbFunctions.TruncateTime(s.Date) == model.OrderDate.Value &&
                                                                 s.TimeFrom == model.OrderTime.Value).IsFree = false;
                _dealerConsultantSchedulesRepository.Value.SaveChanges();
            }
        }
        public void CreateOrder(string userGuid, ViewModels.WebApi.ServiceOrder model)
        {
            var carClass = _carClassesRepository.Value.FirstOrDefault(x => x.ContactsDBSysName == model.ClassSysName && !x.IsDeleted);

            if (carClass == null)
            {
                throw new Exception(string.Format("Класс автомобиля с ContactsDBSysName \"{0}\" не найден.", model.ClassSysName));
            }

            var dealer = _dealersRepository.Value.FirstOrDefault(x => x.DealersShowroomId == model.ShowroomId);

            if (dealer == null)
            {
                throw new Exception(string.Format("Дилер с DealersShowroomID \"{0}\" не найден.", model.ShowroomId));
            }

            var serviceType = _serviceTypesRepository.Value.FirstOrDefault(st => st.Id == model.ServiceTypeId);

            if (serviceType == null)
            {
                throw new Exception(string.Format("Вид работ с ID \"{0}\" не найден.", model.ServiceTypeId));
            }

            DealerConsultant consultant = model.DealerConsultantId.HasValue ?
                                          _dealerConsultantsRepository.Value.GetQuery().FirstOrDefault(x => x.Id == model.DealerConsultantId.Value) : null;

            if (model.DealerConsultantId.HasValue && consultant == null)
            {
                throw new Exception("Данные о консультантах устарели");
            }

            var car = _carsRepository.Value.FirstOrDefault(c => c.CarClassId == carClass.Id && c.Modification == model.CarModel);

            if (car == null)
            {
                throw new Exception(string.Format("Данная модификация автомобиля не найдена"));
            }

            if (serviceType.IsInspection)
            {
                var order = new InspectionOrder
                {
                    Gender               = model.Gender,
                    LastName             = model.LastName,
                    FirstName            = model.FirstName,
                    Patronymic           = model.PatronymicName,
                    Phone                = model.Phone,
                    Email                = model.Email,
                    Comment              = model.Comment,
                    CarId                = car.Id,
                    CarManufactureYear   = model.CarYearRelease,
                    CarVin               = model.CarVin,
                    DealerConsultantCode = consultant?.Code,
                    DealerConsultantName = consultant?.Firstname + " " + consultant?.Surname,
                    ToType               = (OrderToTypes)model.ToType.Value,
                    DealerId             = dealer.Id,
                    OrderDateTime        = DateTime.Parse(model.PreferableDate),
                    CreatedDateTime      = DateTime.Now,
                    ModifiedDateTime     = DateTime.Now,
                    OrderSource          = OrderSourceType.Mobile,
                    UserGuid             = userGuid
                };

                _inspectionOrdersRepository.Value.Add(order);
                _inspectionOrdersRepository.Value.SaveChanges();

                if (consultant == null)
                {
                    _dealerSchedulesRepository.Value.First(s => s.DealerId == dealer.Id && s.IntervalFrom == order.OrderDateTime).IsFree = false;
                    _dealerSchedulesRepository.Value.SaveChanges();
                }

                else
                {
                    _dealerConsultantSchedulesRepository.Value.First(s => s.DealerConsultant.Code == consultant.Code &&
                                                                     DbFunctions.TruncateTime(s.Date) == order.OrderDateTime.Date &&
                                                                     s.TimeFrom == order.OrderDateTime.TimeOfDay).IsFree = false;
                    _dealerConsultantSchedulesRepository.Value.SaveChanges();
                }
            }
            else
            {
                var order = new ServiceOrder
                {
                    Gender                = model.Gender,
                    LastName              = model.LastName,
                    FirstName             = model.FirstName,
                    Patronymic            = model.PatronymicName,
                    Phone                 = model.Phone,
                    Email                 = model.Email,
                    Comment               = model.Comment,
                    ServiceTypeId         = model.ServiceTypeId,
                    CarClassId            = carClass.Id,
                    CarModification       = model.CarModel,
                    CarManufactureYear    = model.CarYearRelease,
                    CarVIN                = model.CarVin,
                    CarRegistrationNumber = model.CarRegNumber,
                    DealerId              = dealer.Id,
                    CreatedDateTime       = DateTime.Now,
                    ModifiedDateTime      = DateTime.Now,
                    CDBTransferDateTime   = DateTime.Now,
                    OrderSource           = OrderSourceType.Mobile,
                    UserGuid              = userGuid
                };

                _serviceOrdersRepository.Value.Add(order);
                _serviceOrdersRepository.Value.SaveChanges();
            }
        }