async private Task InitializeMunicipality(Repository repo, form summaryForm, int year)
        {
            var munit = await repo.GetMunicipality(Authentication.Credentials.MunitId);
            _formula = await repo.GetFormulasBySummaryForm(summaryForm.form_id);
            this.IsAvailable = (_formula != null);
            if (this.IsAvailable)
            {
                _municipality = munit;
                _summaryForm = summaryForm;
                _regularForm = _formula.regular_form;
                _year = year;

                var eduList = await repo.GetEdus(munit.municipality_id);
                var eduHasFormIdList = (await repo.GetEdusHaveFormData(_regularForm.form_id, _year))
                    .Select(t => t.edu_id);
                foreach (var edu in eduList)
                {
                    var hasForm = eduHasFormIdList.Contains(edu.edu_id);
                    edu.SetAttachedProperty("bHasForm", hasForm);
                }
                this.eduBindingSource.DataSource = eduList;
            }
        }
        private async void windowsUIViewMain_QueryControl(object sender, QueryControlEventArgs e)
        {
            try
            {
                var queryReportRegex = new Regex(@"QueryReport(?<idx>\d+)");
                var queryReportRegex2 = new Regex(@"QueryReport-2(?<idx>\d+)");
                var queryReportMatch = queryReportRegex.Match(e.Document.ControlName);
                var queryReportMatch2 = queryReportRegex2.Match(e.Document.ControlName);
                if (queryReportMatch.Success)
                {
                    try
                    {
                        var idx = int.Parse(queryReportMatch.Groups["idx"].Value);
                        e.Control = new XtraReportViewControl(windowsUIViewMain) {Report = ReportFactory.Reports[idx]};
                        return;
                    }
                    catch
                    {
                        throw new KeyNotFoundException("Отчет не найден");
                    }
                }
                if (queryReportMatch2.Success)
                {
                    try
                    {
                        var idx = int.Parse(queryReportMatch2.Groups["idx"].Value);
                        e.Control = new XtraReportViewControl2(windowsUIViewMain) { ReportData = ReportFactory2.Reports[idx] };
                        return;
                    }
                    catch
                    {
                        throw new KeyNotFoundException("Отчет не найден");
                    }
                }
                switch (e.Document.ControlName)
                {
                    case "Questionary":
                        {
                            //XtraDictionaryQuestionaryControl questionaryControl = new XtraDictionaryQuestionaryControl(this.windowsUIViewMain);
                            //int? questionaryId = TagHelper.GetQuestionaryId(e.Document.Tag.ToString());
                            //if (questionaryId.HasValue)
                            //    questionaryControl.CurrentQuestionary = RepoHelper.Repo.GetById<Questionary>(questionaryId.Value);
                            //e.Control = questionaryControl;
                            break;
                        }
                    case "QueriesEdu":
                        {
                            var repo = new Repository();
                            var queryControl = new XtraQueryEduControl(windowsUIViewMain, repo)
                            {
                                FormDataSource = await repo.GetFormsHaveQueries(FormType.Edu, FormType.OtherEdu),
                                YearDataSource = await repo.GetAvailableYears(),
                                QueryPartHeadDataSource = await repo.GetQueriesHeadParts()
                            };
                            e.Control = queryControl;
                            break;
                        }
                    case "QueriesMunicipality":
                        {
                            var repo = new Repository();
                            var queryControl = new XtraQueryMunicipalityControl(windowsUIViewMain, repo)
                            {
                                FormDataSource = await repo.GetFormsHaveQueries(FormType.Municipality, FormType.OtherMunicipality),
                                YearDataSource = await repo.GetAvailableYears(),
                                QueryPartHeadDataSource = await repo.GetQueriesHeadParts()
                            };
                            e.Control = queryControl;
                            break;
                        }
                    case "QueriesRegion":
                        {
                            var repo = new Repository();
                            var queryControl = new XtraQueryRegionControl(windowsUIViewMain, repo)
                            {
                                FormDataSource = await repo.GetFormsHaveQueries(FormType.Region, FormType.OtherRegion),
                                YearDataSource = await repo.GetAvailableYears(),
                                QueryPartHeadDataSource = await repo.GetQueriesHeadParts()
                            };
                            e.Control = queryControl;
                            break;
                        }
                    case "AlignData":
                        {
                            var repo = new Repository();
                            var formDataId = (int) RuntimeSettings.Current["FormDataId"];
                            var formType = (FormType) RuntimeSettings.Current["FormTypeId"];
                            var currentFormData = await repo.GetFormDataById(formDataId, formType);
                            var currentForm = currentFormData.form;
                            var previousData = await repo.FindYesteryearFormData(currentFormData);

                            var alignDataControl = new XtraAlignDataControl(windowsUIViewMain, repo)
                            {
                                PreviousFormData = previousData,
                                CurrentFormData = currentFormData
                            };
                            e.Control = alignDataControl;
                            await alignDataControl.Process(currentForm);
                            break;
                        }
                    case "DictAlignData":
                        {
                            var repo = new Repository();
                            var alignDataControl = new XtraDictionaryAlignDataFormulaControl(windowsUIViewMain, repo)
                            {
                                FormDataSource = await repo.GetForms()
                            };
                            e.Control = alignDataControl;
                            break;
                        }
                    case "DictCreateQuery":
                        {
                            var repo = new Repository();
                            var createQueryControl = new XtraCreateQueryControl(windowsUIViewMain, repo)
                            {
                                QueryAutocomletePartSource = await repo.GetQueriesAutocompleteParts(),
                                QueryHeadPartSource = await repo.GetQueriesHeadParts()
                            };
                            if (Authentication.Credentials.IsRegion)
                            {
                                createQueryControl.FormsWithTemplatesDataSource = 
                                    await repo.GetFormsWithTemplates();
                            }
                            else if (Authentication.Credentials.IsMunicipality)
                            {
                                createQueryControl.FormsWithTemplatesDataSource =
                                    await repo.GetFormsWithTemplatesForMunicipality(Authentication.Credentials.MunitId);
                            }
                            else if (Authentication.Credentials.IsEdu)
                            {
                                createQueryControl.FormsWithTemplatesDataSource =
                                    await repo.GetFormsWithTemplatesForEdu(Authentication.Credentials.EduId);
                            }
                            e.Control = createQueryControl;
                            break;
                        }
                    case "DictCredentials":
                    {
                        var repo = new Repository();
                        var dictCredControl = new XtraDictCredentials(windowsUIViewMain, repo)
                        {
                            CredentialsDataSource = await repo.GetCredential(),
                            EduDataSource = await repo.GetEdus(),
                            MunicipalityDataSource = await repo.GetMunicipalities(),
                            RegionDataSource = await repo.GetRegions()
                        };
                        e.Control = dictCredControl;
                        break;
                    }
                    case "DictEdu":
                        {
                            var repo = new Repository();
                            var dictionaryEduControl =
                                new XtraDictionaryEduControl(windowsUIViewMain, repo)
                                {
                                    EduDataSource = await repo.GetEdus(),
                                    MunicipalityDataSource = await repo.GetMunicipalities(),
                                    DataSourceEduKind = await repo.GetEduKinds()
                                };

                            e.Control = dictionaryEduControl;
                            break;
                        }
                    case "DictMunicipality":
                        {
                            var repo = new Repository();
                            var dictionaryMunitControl =
                                new XtraDictionaryMunitControl(windowsUIViewMain, repo)
                                {
                                    MunicipalityDataSource = await repo.GetMunicipalities()
                                };
                            e.Control = dictionaryMunitControl;
                            break;
                        }
                    case "DictTemplate":
                        {
                            var repo = new Repository();
                            var templateControl = new XtraDictionaryTemplateControl(windowsUIViewMain, repo)
                            {
                                FormsWithTemplatesDataSource = await repo.GetFormsWithTemplates(),
                                FormsWithoutTemplatesDataSource = await repo.GetFormsWithoutTemplates()
                            };
                            e.Control = templateControl;
                            break;
                        }
                    case "DictForms":
                        {
                            var repo = new Repository();
                            var dictionaryFormControl = new XtraDictionaryFormsControl(windowsUIViewMain, repo)
                            {
                                FormDataSource = await repo.GetForms(),
                                DataSourceEduKind = await repo.GetEduKinds(),
                                FormTypeDataSource = await repo.GetFormTypes(),
                                MunicipalityDataSource = await repo.GetMunicipalities()
                            };

                            e.Control = dictionaryFormControl;
                            break;
                        }
                    case "DictMunicipalityFormula":
                        {
                            var repo = new Repository();
                            var dictionaryMunicipalityFormulaControl = new XtraDictionarySummaryFormulaControl(windowsUIViewMain, repo);
                            e.Control = dictionaryMunicipalityFormulaControl;
                            dictionaryMunicipalityFormulaControl.RegularFormDataSource = await repo.GetForms();
                            dictionaryMunicipalityFormulaControl.SummaryFormDataSource = await repo.GetForms();
                            break;
                        }
                    case "ArchiveFormData6":
                    case "FormData6": //допформа
                    case "ArchiveFormData5":
                    case "FormData5": //форма региона
                        {
                            var dataId = TagHelper.GetFormDataId(e.Document.Tag.ToString());
                            if (dataId.HasValue)
                            {
                                var repo = new Repository();
                                var fd = await repo.GetRegionFormDataById(dataId.Value);
                                var sheetControl = new XtraSpreadsheet(windowsUIViewMain, repo)
                                {
                                    FormDataSource = await repo.GetRegionForms(),
                                    FormData = fd,
                                    FormStatus = (Status)fd.status,
                                    Source = XtraSpreadsheet.FormSource.File,
                                    Mode = XtraSpreadsheet.ControlMode.Edit,
                                    ActiveForm = fd.form
                                };
                                sheetControl.LoadDocument();

                                e.Control = sheetControl;
                            }
                            else
                            {
                                e.Control = new Control();
                            }
                            break;
                        }
                    case "ArchiveFormData4":
                    case "FormData4": //допформа
                    case "ArchiveFormData3":
                    case "FormData3": //форма муниципалитета
                        {
                            var dataId = TagHelper.GetFormDataId(e.Document.Tag.ToString());
                            if (dataId.HasValue)
                            {
                                var repo = new Repository();
                                var fd = await repo.GetMunitFormDataById(dataId.Value);
                                var sheetControl = new XtraSpreadsheet(windowsUIViewMain, repo)
                                {
                                    FormDataSource = await repo.GetMunicipalityForms(),
                                    FormData = fd,
                                    FormStatus = (Status)fd.status,
                                    Source = XtraSpreadsheet.FormSource.File,
                                    Mode = await fd.form.IsBlockedAsync() ? XtraSpreadsheet.ControlMode.Disabled : XtraSpreadsheet.ControlMode.Edit,
                                    ActiveForm = fd.form
                                };
                                sheetControl.LoadDocument();

                                e.Control = sheetControl;
                            }
                            else
                            {
                                e.Control = new Control();
                            }
                            break;
                        }
                    case "ArchiveFormData1":
                    case "ArchiveFormData2":
                    case "FormData2": //допформа
                    case "FormData1": //форма организации
                        {
                            var dataId = TagHelper.GetFormDataId(e.Document.Tag.ToString());
                            if (dataId.HasValue)
                            {
                                var repo = new Repository();
                                var fd = await repo.GetEduFormDataById(dataId.Value);
                                var sheetControl = new XtraSpreadsheet(windowsUIViewMain, repo)
                                {
                                    FormData = fd,
                                    FormStatus = (Status)fd.status,
                                    Source = XtraSpreadsheet.FormSource.File,
                                    Mode = await fd.form.IsBlockedAsync() ? XtraSpreadsheet.ControlMode.Disabled : XtraSpreadsheet.ControlMode.Edit
                                };
                                if (Authentication.Credentials.IsEdu)
                                {
                                    var edu = await repo.GetEdu(Authentication.Credentials.EduId);
                                    if (edu.edu_kind_id.HasValue)
                                        sheetControl.FormDataSource = await repo.GetFormsByEduKind(edu.edu_kind_id.Value);
                                }
                                else
                                {
                                    sheetControl.FormDataSource = await repo.GetForms();
                                }
                                sheetControl.ActiveForm = fd.form;
                                sheetControl.LoadDocument();

                                e.Control = sheetControl;
                            }
                            else
                            {
                                e.Control = new Control();
                            }
                            break;
                        }
                    case "XtraProgress":
                        e.Control = new XtraProgress();
                        break;
                    case "Authentication":
                        e.Control = _authForm;
                        break;
                    case "Passport":
                        {
                            var repo = new Repository();
                            if (Authentication.Credentials.IsEdu)
                            {
                                var passportControl = new XtraDictionaryEduPassportControl(windowsUIViewMain, repo)
                                {
                                    MunicipalityDataSource = await repo.GetMunicipalities(),
                                    ActivityTypeDataSource = await repo.GetActivityTypes(),
                                    EduStatusDataSource = await repo.GetEduStatuses(),
                                    EduTypeDataSource = await repo.GetEduTypes(),
                                    DataSourceEduKind = await repo.GetEduKinds(),
                                    ManagementAgencyDataSource = await repo.GetManagementAgencies(),
                                    ManagementAgencyActivityDataSource = await repo.GetManagementAgencyActivities(),
                                    OwnershipTypeDataSource = await repo.GetOwnershipTypes(),
                                    Edu = await repo.GetEdu(Authentication.Credentials.EduId)
                                };
                                e.Control = passportControl;
                            }
                            else if(Authentication.Credentials.IsMunicipality)
                            {
                                var passportControl = new XtraDictionaryMunicipalityPassportControl(windowsUIViewMain, repo)
                                {
                                    Municipality = await repo.GetMunicipality(Authentication.Credentials.MunitId)
                                };
                                e.Control = passportControl;
                            }
                            break;
                        }
                    case "UploadSheetFromFile":
                        {
                            var repo = new Repository();
                            var sheetControl = new XtraSpreadsheet(windowsUIViewMain, repo)
                            {
                                Source = XtraSpreadsheet.FormSource.File,
                                Mode = XtraSpreadsheet.ControlMode.New
                            };
                            var forms = new List<form>();
                            if (Authentication.Credentials.IsEdu)
                            {
                                var edu = await repo.GetEdu(Authentication.Credentials.EduId);
                                forms = await repo.GetFormsByEduKind(edu.edu_kind.edu_kind_id);
                            }
                            else if (Authentication.Credentials.IsMunicipality)
                            {
                                forms = await repo.GetMunicipalityForms();
                            }
                            else if (Authentication.Credentials.IsRegion)
                            {
                                forms = await repo.GetForms();
                            }
                            sheetControl.FormDataSource = forms;
                            sheetControl.ActiveForm = forms.FirstOrDefault();
                            e.Control = sheetControl;
                            break;
                        }
                    case "UploadSheetFromTemplate":
                        {
                            var repo = new Repository();
                            var sheetControl = new XtraSpreadsheet(windowsUIViewMain, repo)
                            {
                                Source = XtraSpreadsheet.FormSource.Template,
                                Mode = XtraSpreadsheet.ControlMode.New
                            };
                            var forms = new List<form>();
                            if (Authentication.Credentials.IsEdu)
                            {
                                var edu = await repo.GetEdu(Authentication.Credentials.EduId);
                                forms = await repo.GetFormsByEduKind(edu.edu_kind.edu_kind_id);
                            }
                            else if (Authentication.Credentials.IsMunicipality)
                            {
                                forms = await repo.GetFormsByType(false, FormType.Municipality, FormType.OtherMunicipality, FormType.Edu, FormType.OtherEdu);
                            }
                            else if (Authentication.Credentials.IsRegion)
                            {
                                forms = await repo.GetForms();
                            }
                            sheetControl.FormDataSource = forms;
                            sheetControl.ActiveForm = forms.FirstOrDefault();
                            e.Control = sheetControl;
                            break;
                        }
                    case "RequestQuery":
                        {
                            var repo = new Repository();
                            var messageControl = new XtraMessageControl(windowsUIViewMain, repo)
                            {
                                MessageType = XtraMessageControl.MessageTypeEnum.RequestForQuery,
                                FormDataSource = await repo.GetForms()
                            };

                            e.Control = messageControl;
                            break;
                        }
                    case "BugReport":
                        {
                            var repo = new Repository();
                            var messageControl = new XtraMessageControl(windowsUIViewMain, repo)
                            {
                                MessageType = XtraMessageControl.MessageTypeEnum.BugReport,
                                FormDataSource = await repo.GetForms()
                            }; e.Control = messageControl;
                            break;
                        }
                    case "MessageList":
                        {
                            var repo = new Repository();
                            var messageListControl = new XtraMessageListControl(windowsUIViewMain, repo)
                            {
                                DataSourceMessage = await repo.GetMessages()
                            };
                            e.Control = messageListControl;
                            break;
                        }
                    case "FormDetailedStatisticsReport":
                        {
                            var doc = windowsUIViewMain.Documents.FindFirst(t => t.ControlName == "FormDetailedStatistics");
                            var formStatData = new List<t_detailed_form_statistics>();
                            if (doc != null)
                            {
                                formStatData = (List<t_detailed_form_statistics>)doc.Tag;
                            }
                            var formStatisticReport = new XtraDetailedFormStatisticReport();
                            var formStatisticReportControl = new XtraReportViewControl(windowsUIViewMain);
                            formStatisticReport.FormStatisticDataSource = formStatData;
                            formStatisticReportControl.Report = formStatisticReport;
                            formStatisticReport.CreateDocument();

                            e.Control = formStatisticReportControl;
                            break;
                        }
                    case "FormDetailedStatistics":
                        {
                            var repo = new Repository();
                            var detailedFormStatisticsReport = new XtraFormDetailedStatisticControl(windowsUIViewMain, repo);
                            e.Control = detailedFormStatisticsReport;
                            detailedFormStatisticsReport.FormStatisticDataSource =
                                Authentication.Credentials.IsMunicipality
                                    ? await repo.GetDetailedFormStatistics(Authentication.Credentials.MunitId, DateTime.Now.Year)
                                    : await repo.GetDetailedFormStatistics(DateTime.Now.Year);
                            detailedFormStatisticsReport.YearsDataSource = await repo.GetAvailableYears();
                            break;
                        }
                    case "FormStatistics":
                        using (var repo = new Repository())
                        {
                            var formStatisticReport = new XtraFormStatisticReport
                            {
                                FormStatisticDataSource = Authentication.Credentials.IsMunicipality
                                    ? repo.GetFormStatistics(Authentication.Credentials.MunitId).Result
                                    : repo.GetFormStatistics().Result
                            };
                            var formStatisticReportControl = new XtraReportViewControl(windowsUIViewMain)
                            {
                                Report = formStatisticReport
                            };
                            formStatisticReport.CreateDocument();

                            e.Control = formStatisticReportControl;
                            break;
                        }
                    case "MunicipalityFormStatistics":
                        using (var repo = new Repository())
                        {
                            var formStatisticReport = new XtraMunicipalityFormStatisticReport //не ясно почему вылетает deferredloadexception, если использовать await
                            {
                                FormStatisticDataSource = Authentication.Credentials.IsMunicipality
                                    ? repo.GetMunicipalityFormStatistics(Authentication.Credentials.MunitId).Result
                                    : repo.GetMunicipalityFormStatistics().Result
                            };
                            var formStatisticReportControl = new XtraReportViewControl(windowsUIViewMain)
                            {
                                Report = formStatisticReport
                            };
                            formStatisticReport.CreateDocument();

                            e.Control = formStatisticReportControl;
                            break;
                        }
                    case "MunicipalityFormDetailedStatisticsReport":
                        {
                            var doc = windowsUIViewMain.Documents.FindFirst(t => t.ControlName == "MunicipalityFormDetailedStatistics");
                            var formStatData = new List<t_detailed_municipality_form_statistics>();
                            if (doc != null)
                            {
                                formStatData = (List<t_detailed_municipality_form_statistics>)doc.Tag;
                            }
                            var formStatisticReport = new XtraDetailedMunicipalityFormStatisticReport();
                            var formStatisticReportControl = new XtraReportViewControl(windowsUIViewMain);

                            formStatisticReport.FormStatisticDataSource = formStatData;
                            formStatisticReportControl.Report = formStatisticReport;
                            formStatisticReport.CreateDocument();

                            e.Control = formStatisticReportControl;
                            break;
                        }
                    case "MunicipalityFormDetailedStatistics":
                        {
                            var repo = new Repository();
                            var detailedFormStatisticsReport = new XtraMunicipalityFormDetailedStatisticControl(windowsUIViewMain, repo)
                            {
                                FormStatisticDataSource = Authentication.Credentials.IsMunicipality
                                    ? repo.GetDetailedMunicipalityFormStatistics(Authentication.Credentials.MunitId).Result
                                    : repo.GetDetailedMunicipalityFormStatistics().Result,
                                YearsDataSource = await repo.GetAvailableYears()
                            };

                            e.Control = detailedFormStatisticsReport;
                            break;
                        }
                    case "FormStatisticsSummary":
                        using (var repo = new Repository())
                        {
                            var formStatisticReport = new XtraFormStatisticSummaryReport
                            {
                                FormStatisticSummaryDataSource = Authentication.Credentials.IsMunicipality
                                    ? repo.GetSummaryFormStatistics(Authentication.Credentials.MunitId).Result
                                    : repo.GetSummaryFormStatistics().Result
                            };
                            var formStatisticReportControl = new XtraReportViewControl(windowsUIViewMain)
                            {
                                Report = formStatisticReport
                            };
                            formStatisticReport.CreateDocument();

                            e.Control = formStatisticReportControl;
                            break;
                        }
                    case "SignInOutLog":
                        {
                            var repo = new Repository();
                            var signInOutReport = new XtraSignInOutReport {Repo = repo};
                            var signInOutReportControl = new XtraReportViewControl(windowsUIViewMain);

                            var startDate = DateTime.Now.AddDays(-30);
                            var endDate = DateTime.Now;

                            var logs = await repo.GetLoginLogs(startDate, endDate);
                            signInOutReport.SignInOutLogDataSource = logs;
                            signInOutReportControl.Report = signInOutReport;
                            signInOutReport.CreateDocument();

                            e.Control = signInOutReportControl;
                            break;
                        }
                    case "ExportReport":
                        {
                            var exportControl = new XtraExportReportControl();
                            e.Control = exportControl;
                            break;
                        }
                    default:
                        e.Control = new Control();
                        break;
                }
            }
            catch (Exception ex)
            {
                GuiUtility.ShowFlyoutMessage(windowsUIViewMain, flyoutMessage, "Ошибка", ex.Message + "\n" + ex.InnerException ?? ex.InnerException.Message, FlyoutCommand.OK);
                Logger.Error(ex.Message);
                Logger.Error(ex.StackTrace);
                e.Control = new Control();
            }
        }