public void CreateOrder(ViewModels.FZ.Home.ServiceOrder model)
        {
            var order = new ServiceOrder
            {
                Gender                = model.Gender,
                LastName              = model.LastName,
                FirstName             = model.FirstName,
                Patronymic            = model.Patronymic,
                Phone                 = model.Phone,
                Email                 = model.Email,
                Comment               = model.Comment,
                ServiceTypeId         = model.ServiceTypeId.Value,
                CarClassId            = model.CarClassId.Value,
                CarModification       = model.CarModification,
                CarManufactureYear    = model.CarManufactureYear.Value,
                CarVIN                = model.CarVIN,
                CarRegistrationNumber = model.CarRegistrationNumber,
                DealerId              = model.DealerId.Value,
                CreatedDateTime       = DateTime.Now,
                ModifiedDateTime      = DateTime.Now,
                OrderSource           = OrderSourceType.Desktop
            };

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

            _serviceOrdersRepository.Value.ReferenceLoad(order, "ServiceType", "CarClass", "Dealer");
        }
        public void CreateOrder(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 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
            };

            _serviceOrdersRepository.Value.Add(order);
            _serviceOrdersRepository.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();
            }
        }