public void Update(long id, RequestSaveModel requestModel)
        {
            var request = _dataStore.Get <Request>(id);

            if (request == null)
            {
                throw new EntityNotFoundException(
                          $"Запись типа {typeof(Request).Name} c идентификатором {id} не существует");
            }

            requestModel.ApplyToEntity(request, _dataStore, _positionService, _customerService);

            _dataStore.SaveChanges();

            if (request.ContainerId.HasValue && request.Type == RequestType.Install)
            {
                var container = _dataStore.Get <Container>(request.ContainerId.Value);
                container.Status     = Dictionary.Containers.Enums.ContainerStatus.Installed;
                container.PositionId = request.PositionId;
                _dataStore.SaveChanges();
            }

            if (request.ContainerId.HasValue && request.Type == RequestType.Uninstall)
            {
                var container = _dataStore.Get <Container>(request.ContainerId.Value);
                container.Status     = Dictionary.Containers.Enums.ContainerStatus.Free;
                container.PositionId = null;
                _dataStore.SaveChanges();
            }
        }
        public RequestSaveModel GetRequestModelForCopy(long id)
        {
            var request = _dataStore.Get <Request>(id);

            return(request != null
                ? RequestSaveModel.FromEntity(request)
                : null);
        }
        /// <summary>
        /// Частичное представление - открытие окна создания
        /// </summary>
        public ActionResult Create()
        {
            var model = new RequestSaveModel
            {
                CreateDate = DateTime.Now.ToString("dd.MM.yyyy HH:mm")
            };

            var editWindowModel = new RequestEditWindowModel <RequestSaveModel>(model);

            PrepareEditWindowModel(editWindowModel);
            PrepareCreateWindowModel(editWindowModel);

            return(PartialView("Partial/Create", editWindowModel));
        }
        public void Create(RequestSaveModel requestModel)
        {
            var request = new Request();

            requestModel.ApplyToEntity(request, _dataStore, _positionService, _customerService);

            _dataStore.Save(request);

            if (request.ContainerId.HasValue && request.Type == RequestType.Install)
            {
                var container = _dataStore.Get <Container>(request.ContainerId.Value);
                container.Status = Dictionary.Containers.Enums.ContainerStatus.Installed;
                _dataStore.SaveChanges();
            }
        }
        public async Task CreateAsync(RequestSaveModel requestModel)
        {
            var request = new Request
            {
                CreateDateTime = DateTime.Now
            };

            requestModel.ApplyToEntity(request, _dataStore, _positionService, _customerService);

            await _dataStore.SaveAsync(request);

            if (request.ContainerId.HasValue && request.Type == RequestType.Install)
            {
                var container = _dataStore.Get <Container>(request.ContainerId.Value);
                container.Status = Dictionary.Containers.Enums.ContainerStatus.Installed;
                await _dataStore.SaveChangesAsync();
            }
        }
        public async Task UpdateAsync(long id, RequestSaveModel requestModel)
        {
            var request = _dataStore.Get <Request>(id);

            if (request == null)
            {
                throw new EntityNotFoundException(
                          $"Запись типа {typeof(Request).Name} c идентификатором {id} не существует");
            }

            //если заявка с типом "Постановка" переходит в статус "Выполнена",
            //то создается заявка с типом "Забор" на соответствующее время
            if (request.Type == RequestType.Install &&
                request.Status != RequestStatus.Done &&
                requestModel.Status == RequestStatus.Done)
            {
                //"малые" контейнеры - забор через 3 дня, "большие" - через день
                var daysCountForUninstall = request.Container?.ContainerType.Capacity <= 8 ? 3 : 1;

                var requestForUninstall = new Request
                {
                    Status             = RequestStatus.New,
                    Type               = RequestType.Uninstall,
                    Comment            = $"Автоматически созданная заявка на забор на основании заявки номер '{request.Id}'.",
                    CreateDateTime     = DateTime.Now,
                    IsPaid             = IsPaid.No,
                    Sum                = 0,
                    Address            = request.Address,
                    ContactPersonName  = request.ContactPersonName,
                    ContactPersonPhone = request.ContactPersonPhone,
                    ContainerId        = request.ContainerId,
                    CustomerId         = request.CustomerId,
                    DriverId           = request.DriverId,
                    CarId              = request.CarId,
                    PaymentType        = request.PaymentType,
                    PlannedDateTime    = request.PlannedUninstallDateTime ??
                                         (request.PlannedDateTime.AddDays(daysCountForUninstall) < DateTime.Now
                                          ? DateTime.Now
                                          : request.PlannedDateTime.AddDays(daysCountForUninstall)),
                    PolygonId  = request.PolygonId,
                    PositionId = request.PositionId
                };

                await _dataStore.SaveAsync(requestForUninstall);
            }

            requestModel.ApplyToEntity(request, _dataStore, _positionService, _customerService);

            await _dataStore.SaveChangesAsync();

            //устанавливаем адрес и местоположение контейнера, равного местоположению заявки
            if (request.ContainerId.HasValue && request.Type == RequestType.Install)
            {
                var container = _dataStore.Get <Container>(request.ContainerId.Value);
                container.Status     = Dictionary.Containers.Enums.ContainerStatus.Installed;
                container.PositionId = request.PositionId;
                container.Address    = request.Address;
                await _dataStore.SaveChangesAsync();
            }
            //освобождаем контейнер, убираем адрес и местоположение
            if (request.ContainerId.HasValue && request.Type == RequestType.Uninstall)
            {
                var container = _dataStore.Get <Container>(request.ContainerId.Value);
                container.Status     = Dictionary.Containers.Enums.ContainerStatus.Free;
                container.PositionId = null;
                container.Address    = null;
                await _dataStore.SaveChangesAsync();
            }
        }