public void CreateLicenseFromApplication(AppDecision decision, AppProtocol protocol) { var application = _dataService.GetEntity <TrlApplication>(trlApplication => trlApplication.Id == decision.AppId) .FirstOrDefault(); long id = 0; if (application == null) { Log.Error("[TRL]CreateLicenseFromApplication - Заяву не знайдено"); return; } var oldLicense = _dataService .GetEntity <TrlLicense>(x => x.OrgUnitId == application.OrgUnitId && x.LicState == "Active" && x.IsRelevant) .FirstOrDefault(); try { if (decision == null) { Log.Error("[TRL]CreateLicenseFromApplication - Рішення не знайдено"); application.ErrorProcessingLicense = "Рішення не знайдено"; throw new Exception(); } decision.IsClosed = true; _dataService.SaveChanges(); if (protocol == null) { Log.Error("[TRL]CreateLicenseFromApplication - Протокол не знайдено"); application.ErrorProcessingLicense = "Протокол не знайдено"; throw new Exception(); } if (application.AppSort == "GetLicenseApplication" || application.AppSort == "IncreaseToTRLApplication") { if (oldLicense != null) { Log.Error("[TRL]CreateLicenseFromApplication - У даного СГД вже є активна ліцензія"); application.ErrorProcessingLicense = "У даного СГД вже є активна ліцензія"; throw new Exception(); } } else { if (oldLicense == null) { Log.Error("[TRL]CreateLicenseFromApplication - У даного СГД немає активної ліцензії"); application.ErrorProcessingLicense = "У даного СГД немає активної ліцензії"; throw new Exception(); } oldLicense.IsRelevant = false; id = oldLicense.OldLimsId; } } catch (Exception) { _dataService.SaveChanges(); return; } long limsId = -1; if (application.AppSort == "GetLicenseApplication" || application.AppSort == "IncreaseToTRLApplication" || application.AppSort == "CancelLicenseApplication" || application.AppSort == "DecreaseTRLApplication" //|| application.AppSort == "ChangeAutPersonApplication" || application.AppSort == "AddBranchApplication" || application.AppSort == "RemBranchApplication" //|| application.AppSort == "ChangeDrugList" //|| application.AppSort == "ReplacementDrugList" ) { try { limsId = _limsExchangeService.InsertLicenseTRL(decision.AppId).Result; } catch (Exception e) { Log.Error(e.Message); return; } } else { //lims insert for other sorts of application limsId = id;//id of new limsDoc } var newLicense = new TrlLicense { OldLimsId = limsId, OrgUnitId = application.OrgUnitId, Id = Guid.NewGuid(), ParentId = application.Id, LicType = "TRL", LicState = "Active", IsRelevant = true, LicenseDate = decision.DateOfStart, OrderNumber = protocol.OrderNumber, OrderDate = protocol.OrderDate.Value }; switch (application.AppSort) { case "RemBranchApplication": { var appBranches = _dataService.GetEntity <ApplicationBranch>(x => x.LimsDocumentId == application.Id).Select(x => x.BranchId).ToList(); var branches = _dataService.GetEntity <Branch>(x => appBranches.Contains(x.Id) && x.LicenseDeleteCheck == true).ToList(); branches.ForEach(x => x.RecordState = RecordState.D); branches.ForEach(x => x.BranchActivity = "Closed"); break; } case "CancelLicenseApplication": case "DecreaseTRLApplication": newLicense.LicState = "Canceled"; break; case "AddBranchInfoApplication": { var appBranches = _dataService.GetEntity <ApplicationBranch>(x => x.LimsDocumentId == application.Id) .Select(x => x.BranchId).ToList(); var branches = _dataService .GetEntity <Branch>(x => appBranches.Contains(x.Id)).ToList(); foreach (var branch in branches) { List <Dictionary <string, string> > operationListForm; try { operationListForm = JsonConvert.DeserializeObject <List <Dictionary <string, string> > >( branch.OperationListForm); } catch (Exception) { operationListForm = new List <Dictionary <string, string> >(); } List <Dictionary <string, string> > operationListFormChanging; try { operationListFormChanging = JsonConvert.DeserializeObject <List <Dictionary <string, string> > >( branch.OperationListFormChanging); } catch (Exception) { operationListFormChanging = new List <Dictionary <string, string> >(); } foreach (var itemChanging in operationListFormChanging) { var exists = false; var itemValueChanging = itemChanging.FirstOrDefault().Value; foreach (var item in operationListForm) { var itemValue = item.FirstOrDefault().Value; if (itemValueChanging == itemValue) { exists = true; break; } } if (exists == false) { operationListForm.Add(itemChanging); } } branch.OperationListForm = JsonConvert.SerializeObject(operationListForm.OrderBy(x => x.FirstOrDefault().Value)); branch.OperationListFormChanging = null; } break; } } _dataService.Add(newLicense); application.AppState = "Reviewed"; application.BackOfficeAppState = "Reviewed"; _dataService.SaveChanges(); }