public async Task <OperationResult> Update(ContractInspectionModel contractInspectionUpdateModel)
        {
            try
            {
                ContractInspection contractInspection =
                    await _dbContext.ContractInspection.SingleOrDefaultAsync(x =>
                                                                             x.Id == contractInspectionUpdateModel.Id);

                if (contractInspection != null)
                {
                    contractInspection.ContractId = contractInspectionUpdateModel.ContractId;
                    contractInspection.DoneAt     = contractInspectionUpdateModel.DoneAt;
                    await contractInspection.Update(_dbContext);
                }
                return(new OperationResult(true));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                _logger.LogError(e.Message);
                return(new OperationResult(true, _rentableItemsLocalizationService.GetString("ErrorWhileUpdatingContractInspection")));
            }
        }
        public async Task <OperationResult> Create(ContractInspectionModel contractInspectionCreateModel)
        {
            try
            {
                Core core = await _coreHelper.GetCore();

                await using MicrotingDbContext context = core.DbContextHelper.GetDbContext();

                // finde eform fra settings
                List <ContractRentableItem> contractRentableItem =
                    await _dbContext.ContractRentableItem.Where(x =>
                                                                x.ContractId == contractInspectionCreateModel.ContractId && x.WorkflowState != Constants.WorkflowStates.Removed).ToListAsync();

                foreach (var item in contractRentableItem)
                {
                    int rentableItemId = item.RentableItemId;

                    RentableItem rentableItem =
                        await _dbContext.RentableItem.FirstOrDefaultAsync(x => x.Id == rentableItemId);

                    int eFormId = rentableItem.eFormId;

                    Contract dbContract =
                        await _dbContext.Contract.FirstOrDefaultAsync(x =>
                                                                      x.Id == contractInspectionCreateModel.ContractId);

                    Customer dbCustomer =
                        await _customersPnDbContext.Customers.SingleOrDefaultAsync(x => x.Id == dbContract.CustomerId);

                    Site site = await context.Sites.SingleAsync(x => x.Id == contractInspectionCreateModel.SiteId);

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

                    MainElement mainElement = await core.ReadeForm(eFormId, language);

                    mainElement.Repeated  = 1;
                    mainElement.EndDate   = DateTime.Now.AddDays(14).ToUniversalTime();
                    mainElement.StartDate = DateTime.Now.ToUniversalTime();
                    mainElement.Label     = "";
                    mainElement.Label    += string.IsNullOrEmpty(rentableItem.SerialNumber)
                        ? ""
                        : $"{rentableItem.SerialNumber}";
                    mainElement.Label += string.IsNullOrEmpty(rentableItem.VinNumber)
                        ? ""
                        : $"{rentableItem.VinNumber}";
                    mainElement.Label += string.IsNullOrEmpty(rentableItem.Brand)
                        ? ""
                        : $"<br>{rentableItem.Brand}";
                    mainElement.Label += string.IsNullOrEmpty(rentableItem.ModelName)
                        ? ""
                        : $"<br>{rentableItem.ModelName}";
                    mainElement.Label += string.IsNullOrEmpty(dbCustomer.ContactPerson)
                        ? ""
                        : $"<br>{dbCustomer.ContactPerson}";

                    CDataValue cDataValue = new CDataValue();
                    cDataValue.InderValue  = $"<b>Kontrakt Nr:<b>{dbContract.ContractNr.ToString()}<br>";
                    cDataValue.InderValue += $"<b>Kunde Nr:<b>{dbContract.CustomerId.ToString()}";

                    List <SiteDto> sites = new List <SiteDto>();

                    int?sdkCaseId = await core.CaseCreate(mainElement, "", (int)site.MicrotingUid, null);

                    if (sdkCaseId != null)
                    {
                        ContractInspection contractInspection = new ContractInspection
                        {
                            ContractId = contractInspectionCreateModel.ContractId
                        };
                        await contractInspection.Create(_dbContext);

                        ContractInspectionItem contractInspectionItem = new ContractInspectionItem
                        {
                            ContractInspectionId = contractInspection.Id,
                            RentableItemId       = rentableItemId,
                            SiteId    = site.Id,
                            SDKCaseId = (int)sdkCaseId,
                            Status    = 33
                        };
                        await contractInspectionItem.Create(_dbContext);
                    }
                }

                return(new OperationResult(true, "Inspection Created Successfully"));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                _logger.LogError(e.Message);
                return(new OperationResult(false, _rentableItemsLocalizationService.GetString("ErrorWhileCreatingContractInspection")));
            }
        }
 public Task <OperationResult> Update([FromBody] ContractInspectionModel contractInspectionUpdateModel)
 {
     return(_contractsInspectionService.Update(contractInspectionUpdateModel));
 }
 public ContractInspectionCreate(ContractInspectionModel contractInspectionModel)
 {
     ContractInspectionModel = contractInspectionModel;
 }
 public ContractInspectionDelete(ContractInspectionModel contractInspectionModel)
 {
     ContractInspectionModel = contractInspectionModel;
 }