private void SaveRequest(object sender) { if (!(sender is RequestItemViewModel)) { return; } var requestModel = sender as RequestItemViewModel; if (requestModel.SelectedService == null) { MessageBox.Show("Необходимо выбрать причину обращения!"); return; } if (SelectedFlat == null) { MessageBox.Show("Необходимо выбрать верный адрес!"); return; } if (requestModel.RequestId.HasValue) { _requestService.EditRequest(requestModel.RequestId.Value, requestModel.SelectedService.Id, requestModel.Description, requestModel.IsImmediate, requestModel.IsChargeable, requestModel.IsBadWork, requestModel.SelectedGaranty?.Id ?? 0, requestModel.IsRetry, requestModel.AlertTime, requestModel.TermOfExecution); //Делаем назначение в расписании if (requestModel.SelectedExecuter != null && requestModel.SelectedAppointment != null && requestModel.SelectedAppointment.RequestId == null) { if (requestModel.OpenAppointment != null && requestModel.OpenAppointment.RequestId != null) { _requestService.DeleteScheduleTask(requestModel.OpenAppointment.Id); } _requestService.AddScheduleTask(requestModel.SelectedExecuter.Id, requestModel.RequestId.Value, requestModel.SelectedAppointment.StartTime, requestModel.SelectedAppointment.EndTime, null); } MessageBox.Show($"Данные успешно сохранены!", "Заявка", MessageBoxButton.OK); return; } if (string.IsNullOrEmpty(_callUniqueId)) { _callUniqueId = _requestService.GetActiveCallUniqueId(); } var request = _requestService.SaveNewRequest(SelectedFlat.Id, requestModel.SelectedService.Id, ContactList.ToArray(), requestModel.Description, requestModel.IsChargeable, requestModel.IsImmediate, _callUniqueId, Entrance, Floor, requestModel.AlertTime, requestModel.IsRetry, requestModel.IsBadWork, requestModel.SelectedEquipment?.Id); if (!request.HasValue) { MessageBox.Show("Произошла непредвиденная ошибка!"); return; } //Надо УК брать из сохраненной заявки var requestDto = _requestService.GetRequest(request.Value); var smsSettings = _requestService.GetSmsSettingsForServiceCompany(requestDto.ServiceCompanyId); if (smsSettings.SendToClient && ContactList.Any(c => c.IsMain) && requestModel.SelectedParentService.CanSendSms && requestModel.SelectedService.CanSendSms) { var mainClient = ContactList.FirstOrDefault(c => c.IsMain); _requestService.SendSms(request.Value, smsSettings.Sender, mainClient.PhoneNumber, $"Заявка № {request.Value}. {requestModel.SelectedParentService.Name} - {requestModel.SelectedService.Name}", true); } requestModel.RequestId = request; if (requestModel.SelectedMaster != null && requestModel.SelectedMaster.Id > 0) { _requestService.AddNewMaster(request.Value, requestModel.SelectedMaster.Id); } if (requestModel.SelectedExecuter != null && requestModel.SelectedExecuter.Id > 0) { _requestService.AddNewExecuter(request.Value, requestModel.SelectedExecuter.Id); } if (requestModel.SelectedDateTime.HasValue) { _requestService.AddNewExecuteDate(request.Value, requestModel.SelectedDateTime.Value, requestModel.SelectedPeriod, ""); } if (requestModel.TermOfExecution.HasValue) { _requestService.AddNewTermOfExecution(request.Value, requestModel.SelectedDateTime.Value, ""); } //Обновление информации о заявке var newRequest = _requestService.GetRequest(request.Value); requestModel.RequestCreator = newRequest.CreateUser.ShortName; requestModel.RequestDate = newRequest.CreateTime; requestModel.RequestState = newRequest.State.Description; requestModel.Rating = newRequest.Rating; //Делаем назначение в расписании if (requestModel.SelectedExecuter != null && requestModel.SelectedAppointment != null) { _requestService.AddScheduleTask(requestModel.SelectedExecuter.Id, request.Value, requestModel.SelectedAppointment.StartTime, requestModel.SelectedAppointment.EndTime, null); } MessageBox.Show($"Номер заявки №{request}", "Заявка", MessageBoxButton.OK); }