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 async Task ContractInspectionItem_Update_DoesUpdate() { //Arrange Random rnd = new Random(); DateTime contractEnd = DateTime.Now; DateTime contractStart = DateTime.Now; Contract contract = new Contract { Status = 66, ContractEnd = contractEnd, ContractNr = rnd.Next(1, 255), ContractStart = contractStart, CustomerId = rnd.Next(1, 255) }; await contract.Create(DbContext); Contract contract2 = new Contract { Status = 100, ContractEnd = contractEnd, ContractNr = rnd.Next(1, 255), ContractStart = contractStart, CustomerId = rnd.Next(1, 255) }; await contract2.Create(DbContext); RentableItem rentableItem = new RentableItem { ModelName = Guid.NewGuid().ToString(), Brand = Guid.NewGuid().ToString(), RegistrationDate = DateTime.Now, VinNumber = Guid.NewGuid().ToString(), PlateNumber = Guid.NewGuid().ToString(), SerialNumber = Guid.NewGuid().ToString(), eFormId = rnd.Next(1, 255) }; RentableItem rentableItem2 = new RentableItem { Brand = Guid.NewGuid().ToString(), ModelName = Guid.NewGuid().ToString(), VinNumber = Guid.NewGuid().ToString(), SerialNumber = Guid.NewGuid().ToString(), PlateNumber = Guid.NewGuid().ToString(), RegistrationDate = DateTime.Now.AddDays(1) }; ContractInspection contractInspection = new ContractInspection { ContractId = contract.Id, DoneAt = DateTime.Now, }; await contractInspection.Create(DbContext); ContractInspection contractInspection2 = new ContractInspection { ContractId = contract2.Id, DoneAt = DateTime.Now.AddDays(1) }; ContractInspectionItem inspectionItem = new ContractInspectionItem { ContractInspectionId = contractInspection.Id, RentableItemId = rentableItem.Id, SDKCaseId = rnd.Next(1, 66), SiteId = rnd.Next(1, 999999), Status = 66 }; await inspectionItem.Create(DbContext); inspectionItem.Status = 100; inspectionItem.SDKCaseId = rnd.Next(1, 255); inspectionItem.SiteId = rnd.Next(1, 255); inspectionItem.ContractInspectionId = contractInspection2.Id; inspectionItem.RentableItemId = rentableItem2.Id; //Act await inspectionItem.Update(DbContext); ContractInspectionItem dbInspectionItem = await DbContext.ContractInspectionItem.AsNoTracking().FirstAsync(); List <ContractInspectionItem> inspectionItems = await DbContext.ContractInspectionItem.AsNoTracking().ToListAsync(); List <ContractInspectionItemVersion> versionList = await DbContext.ContractInspectionItemVersion.AsNoTracking().ToListAsync(); //Assert Assert.NotNull(dbInspectionItem); Assert.NotNull(inspectionItems); Assert.NotNull(versionList); Assert.AreEqual(inspectionItem.Status, dbInspectionItem.Status); Assert.AreEqual(inspectionItem.ContractInspectionId, dbInspectionItem.ContractInspectionId); Assert.AreEqual(inspectionItem.RentableItemId, dbInspectionItem.RentableItemId); Assert.AreEqual(inspectionItem.SDKCaseId, dbInspectionItem.SDKCaseId); Assert.AreEqual(inspectionItem.SiteId, dbInspectionItem.SiteId); }