#pragma warning disable 1998
        public async Task Handle(eFormRetrieved message)
        {
            Console.WriteLine("TrashInspection: We got a message : " + message.caseId);
            TrashInspectionCase trashInspectionCase = _dbContext.TrashInspectionCases.SingleOrDefault(x => x.SdkCaseId == message.caseId.ToString());

            if (trashInspectionCase != null)
            {
                Console.WriteLine("TrashInspection: The incoming case is a trash inspection related case");
                if (trashInspectionCase.Status < 77)
                {
                    trashInspectionCase.Status = 77;
                    await trashInspectionCase.Update(_dbContext);
                }

                TrashInspection trashInspection = _dbContext.TrashInspections.SingleOrDefault(x => x.Id == trashInspectionCase.TrashInspectionId);
                if (trashInspection != null)
                {
                    if (trashInspection.Status < 77)
                    {
                        trashInspection.Status = 77;
                        await trashInspection.Update(_dbContext);
                    }
                }
            }
        }
#pragma warning disable 1998
        public async Task Handle(EformParsingError message)
        {
            TrashInspectionCase trashInspectionCase =
                _dbContext.TrashInspectionCases.SingleOrDefault(x => x.SdkCaseId == message.CaseId.ToString());

            if (trashInspectionCase != null)
            {
                if (trashInspectionCase.Status < 110)
                {
                    trashInspectionCase.Status = 110;
                    await trashInspectionCase.Update(_dbContext);
                }
            }
        }
Exemplo n.º 3
0
        public async Task Handle(EformParsedByServer message)
        {
            await using MicrotingDbContext sdkDbContext = _sdkCore.DbContextHelper.GetDbContext();
            Case theCase = await sdkDbContext.Cases.SingleOrDefaultAsync(x => x.MicrotingUid == message.CaseId);

            if (theCase != null)
            {
                TrashInspectionCase trashInspectionCase =
                    _dbContext.TrashInspectionCases.SingleOrDefault(x => x.SdkCaseId == message.CaseId.ToString());
                if (trashInspectionCase != null)
                {
                    if (trashInspectionCase.Status < 70)
                    {
                        trashInspectionCase.Status = 70;
                        await trashInspectionCase.Update(_dbContext);
                    }
                }
            }
        }
        #pragma warning disable 1998
        public async Task Handle(TrashInspectionCaseCreated message)
        {
            TrashInspectionCase trashInspectionCase =
                await _dbContext.TrashInspectionCases.SingleOrDefaultAsync(x => x.Id == message.TrashInspectionCaseId);

            LogEvent($"TrashInspectionCaseCreatedHandler.Handle: called for message.TrashInspectionModel.WeighingNumber / message.TrashInspectionCase.Id : {message.TrashInspectionModel.WeighingNumber} / {message.TrashInspectionCaseId}");
            CultureInfo cultureInfo = new CultureInfo("de-DE");
            int         sdkSiteId   = trashInspectionCase.SdkSiteId;

            await using MicrotingDbContext microtingDbContext = _core.DbContextHelper.GetDbContext();
            Site site = await microtingDbContext.Sites.SingleAsync(x => x.MicrotingUid == sdkSiteId);

            Language language = await microtingDbContext.Languages.SingleAsync(x => x.Id == site.LanguageId);

            LogEvent($"TrashInspectionCaseCreatedHandler: sdkSiteId: {sdkSiteId}, message.TemplateId: {message.TemplateId} ");
            MainElement mainElement = await _core.ReadeForm(message.TemplateId, language);

            TrashInspectionModel createModel = message.TrashInspectionModel;
            Segment segment = await _dbContext.Segments.SingleOrDefaultAsync(x => x.Id == message.SegmentId);

            Fraction fraction = await _dbContext.Fractions.SingleOrDefaultAsync(x => x.Id == message.FractionId);

            LogEvent($"TrashInspectionCaseCreatedHandler: Segment: {segment.Name}, TrashFraction: {fraction.Name} ");

            mainElement.Repeated  = 1;
            mainElement.EndDate   = DateTime.Now.AddDays(2).ToUniversalTime();
            mainElement.StartDate = DateTime.Now.ToUniversalTime();
            await using (var dbContext = _core.DbContextHelper.GetDbContext())
            {
                mainElement.CheckListFolderName = dbContext.Folders.Single(x => x.Id == segment.SdkFolderId).MicrotingUid.ToString();
            }
            mainElement.Label           = createModel.RegistrationNumber.ToUpper() + ", " + createModel.Transporter;
            mainElement.EnableQuickSync = true;
            mainElement.DisplayOrder    = (int)Math.Round(DateTime.Now.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds) * -1;

            TimeZoneInfo timeZoneInfo;

            try
            {
                timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Europe/Copenhagen");
            }
            catch
            {
                timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("E. Europe Standard Time");
            }
            var        localTime  = TimeZoneInfo.ConvertTimeFromUtc(createModel.Time, timeZoneInfo);
            CDataValue cDataValue = new CDataValue {
                InderValue = $"<b>Vejenr:</b> {createModel.WeighingNumber}<br>"
            };

            cDataValue.InderValue += $"<b>Dato:</b> {createModel.Date.ToString("dd-MM-yyyy") + " " + localTime.ToString("T", cultureInfo)}<br>";
            cDataValue.InderValue += $"<b>Område:</b> {segment.Name}<br>";
            cDataValue.InderValue += $"<b>Producent:</b> {createModel.Producer}<br>";
            cDataValue.InderValue += $"<b>Varenummer:</b> {fraction.ItemNumber} {fraction.Name}";
            if (createModel.EakCode != null)
            {
                cDataValue.InderValue += $"<br><b>EAK Kode:</b> {createModel.EakCode}";
            }

            mainElement.PushMessageTitle = mainElement.Label;
            mainElement.PushMessageBody  = "";

            if (createModel.MustBeInspected && !createModel.ExtendedInspection)
            {
                mainElement.PushMessageBody += "*** SKAL INSPICERES ***\n";
                cDataValue.InderValue       += "<br><br><b>*** SKAL INSPICERES ***</b>";
            }

            if (createModel.ExtendedInspection)
            {
                mainElement.PushMessageBody += "*** LOVPLIGTIG KONTROL ***\n";
                cDataValue.InderValue       += "<br><br><b>*** LOVPLIGTIG KONTROL ***</b>";
                mainElement.Color            = Constants.CheckListColors.Red;
            }

            mainElement.PushMessageBody += $"Vare: {fraction.Name}\n";

            if (createModel.Producer.Length > 17)
            {
                mainElement.PushMessageBody += $"Producent: {createModel.Producer.Substring(0,17)}...";
            }
            else
            {
                mainElement.PushMessageBody += $"Producent: {createModel.Producer}";
            }

            mainElement.ElementList[0].Description = cDataValue;
            mainElement.ElementList[0].Label       = mainElement.Label;
            DataElement dataElement = (DataElement)mainElement.ElementList[0];

            dataElement.DataItemList[0].Label       = mainElement.Label;
            dataElement.DataItemList[0].Description = cDataValue;

            if (createModel.MustBeInspected || createModel.ExtendedInspection)
            {
                dataElement.DataItemList[0].Color = Constants.FieldColors.Red;
            }

            LogEvent("CreateTrashInspection: Trying to create SDK case");
            int?sdkCaseId = await _core.CaseCreate(mainElement, "", sdkSiteId, segment.SdkFolderId);

            LogEvent($"CreateTrashInspection: SDK case created and got id {sdkCaseId}");

            trashInspectionCase.SdkCaseId = sdkCaseId.ToString();
            trashInspectionCase.Status    = 66;
            await trashInspectionCase.Update(_dbContext);

            var trashInspectionCases =
                _dbContext.TrashInspectionCases.AsNoTracking().Where(x =>
                                                                     x.TrashInspectionId == trashInspectionCase.TrashInspectionId);
            bool allDone = true;

            foreach (TrashInspectionCase inspectionCase in trashInspectionCases)
            {
                if (inspectionCase.Status < 66)
                {
                    allDone = false;
                }
            }

            if (allDone)
            {
                var trashInspection = await _dbContext.TrashInspections.SingleOrDefaultAsync(x =>
                                                                                             x.Id == trashInspectionCase.TrashInspectionId);

                if (trashInspection.Status < 66)
                {
                    trashInspection.Status = 66;
                    await trashInspection.Update(_dbContext);
                }
            }
        }
        #pragma warning disable 1998
        public async Task Handle(eFormCompleted message)
        {
            Console.WriteLine("[DBG] TrashInspection: We got a message : " + message.caseId);
            TrashInspectionCase trashInspectionCase =
                _dbContext.TrashInspectionCases.SingleOrDefault(x => x.SdkCaseId == message.caseId.ToString());

            if (trashInspectionCase != null)
            {
                #region get case information

                CaseDto caseDto = await _sdkCore.CaseLookupMUId(message.caseId);

                await using MicrotingDbContext microtingDbContext = _sdkCore.DbContextHelper.GetDbContext();
                var      microtingUId      = caseDto.MicrotingUId;
                var      microtingCheckUId = caseDto.CheckUId;
                Language language          = await microtingDbContext.Languages.SingleAsync(x => x.Name == "Danish");

                ReplyElement theCase = await _sdkCore.CaseRead((int)microtingUId, (int)microtingCheckUId, language);

                CheckListValue dataElement        = (CheckListValue)theCase.ElementList[0];
                bool           inspectionApproved = false;
                string         approvedValue      = "";
                string         comment            = "";
                Console.WriteLine("[DBG] Trying to find the field with the approval value");
                foreach (var field in dataElement.DataItemList)
                {
                    Field f = (Field)field;
                    if (f.Label.Contains("Angiv om læs er Godkendt"))
                    {
                        Console.WriteLine($"The field is {f.Label}");
                        FieldValue fv         = f.FieldValues[0];
                        String     fieldValue = fv.Value;
                        inspectionApproved = (fieldValue == "1");
                        approvedValue      = fieldValue;
                        Console.WriteLine($"[DBG] We are setting the approved state to {inspectionApproved.ToString()}");
                    }

                    if (f.Label.Equals("Kommentar"))
                    {
                        Console.WriteLine($"[DBG] The field is {f.Label}");
                        FieldValue fv         = f.FieldValues[0];
                        String     fieldValue = fv.Value;
                        comment = fieldValue;
                        Console.WriteLine($"[DBG] We are setting the comment to {comment.ToString()}");
                    }
                }
                #endregion

                Console.WriteLine("TrashInspection: The incoming case is a trash inspection related case");
                trashInspectionCase.Status = 100;
                await trashInspectionCase.Update(_dbContext);

                TrashInspection trashInspection =
                    _dbContext.TrashInspections.SingleOrDefault(x => x.Id == trashInspectionCase.TrashInspectionId);
                if (trashInspection != null)
                {
                    trashInspection.Status         = 100;
                    trashInspection.IsApproved     = inspectionApproved;
                    trashInspection.Comment        = comment;
                    trashInspection.ApprovedValue  = approvedValue;
                    trashInspection.InspectionDone = true;
                    await trashInspection.Update(_dbContext);

                    List <TrashInspectionCase> trashInspectionCases = _dbContext.TrashInspectionCases
                                                                      .Where(x => x.TrashInspectionId == trashInspection.Id).ToList();
                    foreach (TrashInspectionCase inspectionCase in trashInspectionCases)
                    {
                        if (await _sdkCore.CaseDelete(int.Parse(inspectionCase.SdkCaseId)))
                        {
                            inspectionCase.WorkflowState = Constants.WorkflowStates.Retracted;
                            await inspectionCase.Update(_dbContext);
                        }
                    }

                    #region get settings

                    string callBackUrl = _dbContext.PluginConfigurationValues
                                         .SingleOrDefault(x => x.Name == "TrashInspectionBaseSettings:callBackUrl")?.Value;
                    Console.WriteLine("[DBG] callBackUrl is : " + callBackUrl);

                    string callBackCredentialDomain = _dbContext.PluginConfigurationValues.SingleOrDefault(x =>
                                                                                                           x.Name == "TrashInspectionBaseSettings:CallBackCredentialDomain")?.Value;
                    Console.WriteLine("[DBG] callBackCredentialDomain is : " + callBackCredentialDomain);

                    string callbackCredentialUserName = _dbContext.PluginConfigurationValues.SingleOrDefault(x =>
                                                                                                             x.Name == "TrashInspectionBaseSettings:callbackCredentialUserName")?.Value;
                    Console.WriteLine("[DBG] callbackCredentialUserName is : " + callbackCredentialUserName);

                    string callbackCredentialPassword = _dbContext.PluginConfigurationValues.SingleOrDefault(x =>
                                                                                                             x.Name == "TrashInspectionBaseSettings:CallbackCredentialPassword")?.Value;
                    Console.WriteLine("[DBG] callbackCredentialPassword is : " + callbackCredentialPassword);

                    string callbackCredentialAuthType = _dbContext.PluginConfigurationValues.SingleOrDefault(x =>
                                                                                                             x.Name == "TrashInspectionBaseSettings:CallbackCredentialAuthType")?.Value;
                    Console.WriteLine("[DBG] callbackCredentialAuthType is : " + callbackCredentialAuthType);

                    Console.WriteLine($"[DBG] trashInspection.WeighingNumber is {trashInspection.WeighingNumber}");
                    #endregion

                    switch (callbackCredentialAuthType)
                    {
                    case "NTLM":
                        await CallUrlNtlmAuth(callBackUrl, callBackCredentialDomain, callbackCredentialUserName,
                                              callbackCredentialPassword, trashInspection, inspectionApproved);

                        break;

                    case "basic":
                    default:
                        await CallUrlBaiscAuth(callBackUrl, callBackCredentialDomain, callbackCredentialUserName,
                                               callbackCredentialPassword, trashInspection, inspectionApproved);

                        break;
                    }
                }
            }
        }