public void CreateLicenseFromApplication(AppDecision decision, AppProtocol protocol) { var application = _dataService.GetEntity <PrlApplication>(prlApplication => prlApplication.Id == decision.AppId) .FirstOrDefault(); long id = 0; if (application == null) { Log.Error("CreateLicenseFromApplication - Заяву не знайдено"); return; } var oldLicense = _dataService .GetEntity <PrlLicense>(x => x.OrgUnitId == application.OrgUnitId && x.LicState == "Active" && x.IsRelevant) .FirstOrDefault(); try { if (decision == null) { Log.Error("CreateLicenseFromApplication - Рішення не знайдено"); application.ErrorProcessingLicense = "Рішення не знайдено"; throw new Exception(); } decision.IsClosed = true; _dataService.SaveChanges(); if (protocol == null) { Log.Error("CreateLicenseFromApplication - Протокол не знайдено"); application.ErrorProcessingLicense = "Протокол не знайдено"; throw new Exception(); } if (application.AppSort == "GetLicenseApplication" || application.AppSort == "IncreaseToPRLApplication") { if (oldLicense != null) { Log.Error("CreateLicenseFromApplication - У даного СГД вже є активна ліцензія"); application.ErrorProcessingLicense = "У даного СГД вже є активна ліцензія"; throw new Exception(); } } else { if (oldLicense == null) { Log.Error("CreateLicenseFromApplication - У даного СГД немає активної ліцензії"); application.ErrorProcessingLicense = "У даного СГД немає активної ліцензії"; throw new Exception(); } oldLicense.IsRelevant = false; id = oldLicense.OldLimsId; } } catch (Exception) { _dataService.SaveChanges(); return; } long limsId; if (application.AppSort == "GetLicenseApplication" || application.AppSort == "IncreaseToPRLApplication" || application.AppSort == "CancelLicenseApplication" || application.AppSort == "DecreasePRLApplication" || application.AppSort == "AddBranchApplication" || application.AppSort == "RemBranchApplication" || application.AppSort == "ChangeAutPersonApplication" || application.AppSort == "AddBranchInfoApplication" || application.AppSort == "RemBranchInfoApplication" || application.AppSort == "ChangeContrApplication" ) { try { limsId = _limsExchangeService.InsertLicense(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 PrlLicense { OldLimsId = limsId, OrgUnitId = application.OrgUnitId, Id = Guid.NewGuid(), ParentId = application.Id, LicType = "PRL", 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 "DecreasePRLApplication": 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; } case "RemBranchInfoApplication": { 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 itemValueChanging = itemChanging.FirstOrDefault().Value; for (int i = operationListForm.Count - 1; i >= 0; i--) { var itemValue = operationListForm[i].FirstOrDefault().Value; if (itemValue == itemValueChanging) { operationListForm.RemoveAt(i); } } } branch.OperationListForm = JsonConvert.SerializeObject(operationListForm); branch.OperationListFormChanging = null; } break; } } _dataService.Add(newLicense); application.AppState = "Reviewed"; application.BackOfficeAppState = "Reviewed"; _dataService.SaveChanges(); }