Example #1
0
        public async Task SubmitAdditionalInfoToLicense(Guid appId, string text)
        {
            var application = DataService.GetEntity <ImlApplication>(x => x.Id == appId).FirstOrDefault();

            if (application == null)
            {
                throw new Exception();
            }
            var organization = DataService.GetEntity <OrganizationExt>(x => x.Id == application.OrgUnitId).FirstOrDefault();

            if (organization == null)
            {
                throw new Exception();
            }
            var orgInfo = DataService.GetEntity <OrganizationInfo>(x => x.Id == application.OrganizationInfoId)
                          .FirstOrDefault();

            if (orgInfo == null)
            {
                throw new Exception();
            }
            var limsLicense = (await _limsExchangeService.GetLicenses("Iml",
                                                                      string.IsNullOrEmpty(organization.EDRPOU) ?
                                                                      organization.INN : organization.EDRPOU)).FirstOrDefault();
            var newLic = new ImlLicense
            {
                OldLimsId   = limsLicense.Id,
                OrgUnitId   = application.OrgUnitId,
                ParentId    = application.Id,
                LicType     = "Iml",
                LicState    = "Active",
                IsRelevant  = true,
                LicenseDate = DateTime.Parse(limsLicense.RegistrationDate),
                OrderDate   = limsLicense.OrderDate,
                OrderNumber = limsLicense.OrderNumber
            };

            DataService.Add(newLic);

            application.AppState           = "Reviewed";
            application.BackOfficeAppState = "Reviewed";

            orgInfo.IsActualInfo           = true;
            orgInfo.IsPendingLicenseUpdate = false;

            var decision = new AppDecision {
                Id = Guid.NewGuid(), AppId = appId, DecisionType = "Accepted", DecisionDescription = text
            };

            application.AppDecisionId = decision.Id;
            DataService.Add(decision);

            await DataService.SaveChangesAsync();
        }
        public void CreateLicenseFromApplication(AppDecision decision, AppProtocol protocol)
        {
            var application = _dataService.GetEntity <ImlApplication>(imlApplication => imlApplication.Id == decision.AppId)
                              .FirstOrDefault();
            long id = 0;

            if (application == null)
            {
                Log.Error("[IML]CreateLicenseFromApplication - Заяву не знайдено");
                return;
            }

            var oldLicense = _dataService
                             .GetEntity <ImlLicense>(x => x.OrgUnitId == application.OrgUnitId && x.LicState == "Active" && x.IsRelevant)
                             .FirstOrDefault();

            try
            {
                if (decision == null)
                {
                    Log.Error("[IML]CreateLicenseFromApplication - Рішення не знайдено");
                    application.ErrorProcessingLicense = "Рішення не знайдено";
                    throw new Exception();
                }

                decision.IsClosed = true;
                _dataService.SaveChanges();

                if (protocol == null)
                {
                    Log.Error("[IML]CreateLicenseFromApplication - Протокол не знайдено");
                    application.ErrorProcessingLicense = "Протокол не знайдено";
                    throw new Exception();
                }

                if (application.AppSort == "GetLicenseApplication" || application.AppSort == "IncreaseToIMLApplication")
                {
                    if (oldLicense != null)
                    {
                        Log.Error("[IML]CreateLicenseFromApplication - У даного СГД вже є активна ліцензія");
                        application.ErrorProcessingLicense = "У даного СГД вже є активна ліцензія";
                        throw new Exception();
                    }
                }
                else
                {
                    if (oldLicense == null)
                    {
                        Log.Error("[IML]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 == "IncreaseToIMLApplication" ||
                application.AppSort == "CancelLicenseApplication" ||
                application.AppSort == "DecreaseIMLApplication" ||
                application.AppSort == "ChangeAutPersonApplication" ||
                application.AppSort == "AddBranchApplication" ||
                application.AppSort == "RemBranchApplication" ||
                application.AppSort == "ChangeDrugList" ||
                application.AppSort == "ReplacementDrugList")
            {
                try
                {
                    limsId = _limsExchangeService.InsertLicenseIML(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 ImlLicense
            {
                OldLimsId   = limsId,
                OrgUnitId   = application.OrgUnitId,
                Id          = Guid.NewGuid(),
                ParentId    = application.Id,
                LicType     = "IML",
                LicState    = "Active",
                IsRelevant  = true,
                LicenseDate = decision.DateOfStart,
                OrderNumber = protocol.OrderNumber,
                OrderDate   = protocol.OrderDate.Value
            };

            switch (application.AppSort)
            {
            case "ChangeDrugList":
            {
                _imlMedicineService.UpdateStatus(application.Id);
                break;
            }

            case "ReplacementDrugList":
            {
                Task.WaitAll(_imlMedicineService.DeleteAll(application.Id, true));
                _imlMedicineService.UpdateStatus(application.Id);
                break;
            }

            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 "DecreaseIMLApplication":
                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();
        }