public async Task <OperationResult> Create(TrashInspectionModel createModel)
        {
            var dateTime            = DateTime.UtcNow;
            var pluginConfiguration = await _dbContext.PluginConfigurationValues.SingleOrDefaultAsync(x => x.Name == "TrashInspectionBaseSettings:Token");

            if (pluginConfiguration == null)
            {
                return(new OperationResult(false));
            }

            if (createModel.Token == pluginConfiguration.Value && createModel.WeighingNumber != null)
            {
                try
                {
// Handling the situation, where incoming timestamp is not in UTC.
                    var utcAdjustment = await _dbContext.PluginConfigurationValues.SingleOrDefaultAsync(x => x.Name == "TrashInspectionBaseSettings:UtcAdjustment");

                    if (utcAdjustment.Value == "true")
                    {
                        if (createModel.Time.Hour > dateTime.Hour)
                        {
                            var timeSpan  = createModel.Time.Subtract(dateTime);
                            var minutes   = timeSpan.Hours * 60.0 + timeSpan.Minutes;
                            var hours     = minutes / 60.0;
                            var fullHours = Math.Round(hours);
                            createModel.Time = createModel.Time.AddHours(-fullHours);
                        }
                    }

                    TrashInspection trashInspection = await _dbContext.TrashInspections.FirstOrDefaultAsync(x =>
                                                                                                            x.WeighingNumber == createModel.WeighingNumber);

                    if (trashInspection != null)
                    {
                        return(new OperationResult(true, trashInspection.Id.ToString()));
                    }

                    trashInspection =
                        new TrashInspection
                    {
                        WeighingNumber     = createModel.WeighingNumber,
                        Date               = createModel.Date,
                        Time               = createModel.Time,
                        EakCode            = createModel.EakCode,
                        ExtendedInspection = createModel.ExtendedInspection,
                        RegistrationNumber = createModel.RegistrationNumber,
                        TrashFraction      = createModel.TrashFraction,
                        Producer           = createModel.Producer,
                        Transporter        = createModel.Transporter,
                        MustBeInspected    = createModel.MustBeInspected,
                        InspectionDone     = false,
                        Status             = 0,
                        UpdatedByUserId    = _userService.UserId,
                        CreatedByUserId    = _userService.UserId,
                    };

                    await trashInspection.Create(_dbContext);

                    var segment      = _dbContext.Segments.FirstOrDefault(x => x.Name == createModel.Segment);
                    var installation =
                        _dbContext.Installations.FirstOrDefault(x => x.Name == createModel.InstallationName);
                    var fraction =
                        _dbContext.Fractions.FirstOrDefault(x => x.ItemNumber == createModel.TrashFraction);

                    _coreHelper.LogEvent($"CreateTrashInspection: Segment: {createModel.Segment}, InstallationName: {createModel.InstallationName}, TrashFraction: {createModel.TrashFraction} ");
                    if (segment != null && installation != null && fraction != null)
                    {
                        trashInspection.SegmentId      = segment.Id;
                        trashInspection.FractionId     = fraction.Id;
                        trashInspection.InstallationId = installation.Id;
                        await trashInspection.Update(_dbContext);

                        createModel.SegmentId      = segment.Id;
                        createModel.FractionId     = fraction.Id;
                        createModel.InstallationId = installation.Id;
                        createModel.Id             = trashInspection.Id;

                        await UpdateProducerAndTransporter(trashInspection, createModel);

                        _coreHelper.LogEvent($"CreateTrashInspection: Segment: {segment.Name}, InstallationName: {installation.Name}, TrashFraction: {fraction.Name} ");
                        await _bus.SendLocal(new TrashInspectionReceived(createModel, fraction.Id, segment.Id, installation.Id));
                    }

                    return(new OperationResult(true, createModel.Id.ToString()));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return(new OperationResult(false));
                }
            }

            return(new OperationResult(false));
        }
        public async Task <string> DownloadEFormPdf(int id, string token, string fileType)
        {
            Core core = await _coreHelper.GetCore();

            var locale = await _userService.GetCurrentUserLocale();

            var    language = core.DbContextHelper.GetDbContext().Languages.Single(x => x.LanguageCode.ToLower() == locale.ToLower());
            string microtingUId;
            string microtingCheckUId;
            int    caseId  = 0;
            int    eFormId = 0;

            ContractInspection contractInspection =
                await _dbContext.ContractInspection.SingleOrDefaultAsync(x => x.Id == id);

            Contract contract = _dbContext.Contract.Single(x => x.Id == contractInspection.ContractId);

            int      i          = 0;
            XElement xmlContent = new XElement("ContractInspection");

            foreach (ContractRentableItem contractRentableItem in _dbContext.ContractRentableItem
                     .Where(x => x.ContractId == contract.Id &&
                            x.WorkflowState == Constants.WorkflowStates.Created)
                     .ToList())
            {
                RentableItem rentableItem = _dbContext.RentableItem.Single(x => x.Id == contractRentableItem.RentableItemId);
                xmlContent.Add(new XElement($"Brand_{i}", rentableItem.Brand));
                xmlContent.Add(new XElement($"ModelName_{i}", rentableItem.ModelName));
                xmlContent.Add(new XElement($"Serial_{i}", rentableItem.SerialNumber));
                xmlContent.Add(new XElement($"RegistrationDate_{i}", rentableItem.RegistrationDate));
                xmlContent.Add(new XElement($"vinNumber_{i}", rentableItem.VinNumber));
                xmlContent.Add(new XElement($"PlateNumber_{i}", rentableItem.PlateNumber));
                i += 1;
            }

            Customer customer =
                _customerDbContext.Customers.Single(x => x.Id == contract.CustomerId);

            xmlContent.Add(new XElement("CustomerCustomerNo", customer.CustomerNo));
            xmlContent.Add(new XElement("CustomerCompanyName", customer.CompanyName));
            xmlContent.Add(new XElement("CustomerContactPerson", customer.ContactPerson));
            xmlContent.Add(new XElement("CustomerCompanyAddress", customer.CompanyAddress));
            xmlContent.Add(new XElement("CustomerCompanyAddress2", customer.CompanyAddress2));
            xmlContent.Add(new XElement("CustomerCityName", customer.CityName));
            xmlContent.Add(new XElement("CustomerZipCode", customer.ZipCode));
            xmlContent.Add(new XElement("CustomerCountryCode", customer.CountryCode));
            xmlContent.Add(new XElement("CustomerEanCode", customer.EanCode));
            xmlContent.Add(new XElement("CustomerVatNumber", customer.VatNumber));
            xmlContent.Add(new XElement("CustomerEmail", customer.Email));
            xmlContent.Add(new XElement("CustomerPhone", customer.Phone));
            xmlContent.Add(new XElement("CustomerDescription", customer.Description));

            _coreHelper.LogEvent($"DownloadEFormPdf: xmlContent is {xmlContent}");
            ContractInspectionItem contractInspectionItem =
                _dbContext.ContractInspectionItem.FirstOrDefault(x =>
                                                                 x.ContractInspectionId == contractInspection.Id);

            CaseDto caseDto = await core.CaseLookupMUId(contractInspectionItem.SDKCaseId);

            caseId  = (int)caseDto.CaseId;
            eFormId = caseDto.CheckListId;

            if (caseId != 0 && eFormId != 0)
            {
                _coreHelper.LogEvent($"DownloadEFormPdf: caseId is {caseId}, eFormId is {eFormId}");
                var filePath = await core.CaseToPdf(caseId, eFormId.ToString(),
                                                    DateTime.Now.ToString("yyyyMMddHHmmssffff"),
                                                    $"{await core.GetSdkSetting(Settings.httpServerAddress)}/" + "api/template-files/get-image/", xmlContent.ToString(), language);

                if (!File.Exists(filePath))
                {
                    throw new FileNotFoundException();
                }

                return(filePath);
            }

            throw new Exception("could not find case of eform!");
        }