public async Task OnDeleteDocument() { List <Document_Dto> documents = JsonSerializer.Deserialize <List <Document_Dto> >(Request.Form["documents"]); for (int i = 0; i < documents.Count; i++) { Document_Dto document = documents[i]; await documentAppService.DeleteAsync(document.Id); string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "Uploads"); string filePath = Path.Combine(uploadsFolder, document.FileName); if (!string.IsNullOrEmpty(document.FileName) && System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } } }
public async Task <IActionResult> OnPost() { if (ModelState.IsValid) { try { var formData = Request.Form; DocumentViewModel generalInfo = new DocumentViewModel(); generalInfo = JsonSerializer.Deserialize <DocumentViewModel>(formData["info"].ToString()); var files = formData.Files; if (files.Count > 0) { IFormFile document = files[0]; generalInfo.File = document; } if (generalInfo.IsEditing) { Document documentToEdit = await documentAppService.Repository.GetAsync(generalInfo.Document.Id); documentToEdit.ReferenceNo = generalInfo.Document.ReferenceNo; documentToEdit.OwnerTypeId = generalInfo.Document.OwnerTypeId; documentToEdit.DocumentTypeId = generalInfo.Document.DocumentTypeId; documentToEdit.OwnerId = generalInfo.Document.OwnerId; documentToEdit.Name = generalInfo.Document.Name; documentToEdit.NameLocalized = generalInfo.Document.NameLocalized; documentToEdit.Description = generalInfo.Document.Description; documentToEdit.IssueDate = generalInfo.Document.IssueDate; documentToEdit.ExpiryDate = generalInfo.Document.ExpiryDate; if (generalInfo.File != null) { string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "Uploads"); string filePath = Path.Combine(uploadsFolder, documentToEdit.FileName); if (!string.IsNullOrEmpty(documentToEdit.FileName) && System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } string uploadedFileName = UploadedFile(generalInfo.File); documentToEdit.FileName = uploadedFileName; } Document documentEdited = await documentAppService.Repository.UpdateAsync(documentToEdit); Document_Dto documentEditedDto = ObjectMapper.Map <Document, Document_Dto>(documentAppService.Repository.WithDetails().First(x => x.Id == documentEdited.Id)); return(new JsonResult(documentEditedDto)); } else { Document_Dto documentDto = new Document_Dto(guidGenerator.Create()); documentDto.ReferenceNo = generalInfo.Document.ReferenceNo; documentDto.OwnerTypeId = generalInfo.Document.OwnerTypeId; documentDto.DocumentTypeId = generalInfo.Document.DocumentTypeId; documentDto.OwnerId = generalInfo.Document.OwnerId; documentDto.Name = generalInfo.Document.Name; documentDto.NameLocalized = generalInfo.Document.NameLocalized; documentDto.Description = generalInfo.Document.Description; documentDto.IssuedFromId = generalInfo.Document.IssuedFromId; documentDto.IssueDate = generalInfo.Document.IssueDate; documentDto.ExpiryDate = generalInfo.Document.ExpiryDate; string uploadedFileName = UploadedFile(generalInfo.File); documentDto.FileName = uploadedFileName; Document_Dto addedDoc = await documentAppService.CreateAsync(documentDto); addedDoc = ObjectMapper.Map <Document, Document_Dto>(documentAppService.Repository.WithDetails().First(x => x.Id == addedDoc.Id)); return(new JsonResult(addedDoc)); } } catch (Exception ex) { return(StatusCode(500)); } } return(NoContent()); }
public DocumentViewModel() { IsEditing = false; Document = new Document_Dto(); File = null; }
public async Task <IActionResult> OnPostCompany() { if (ModelState.IsValid) { try { var FormData = Request.Form; Company_Dto company = JsonSerializer.Deserialize <Company_Dto>(FormData["info"]); //List<CompanyLocation_Dto> addresses = JsonSerializer.Deserialize<List<CompanyLocation_Dto>>(Request.Form["locations"]); //company.SetProperty("addresses", addresses); bool logoChanged = false; if (FormData.Files.Count > 0 && FormData.Files.Any(x => x.Name == "CompanyPic")) { IFormFile formFile = FormData.Files.First(x => x.Name == "CompanyPic"); string uploadedFileName = UploadedFile(formFile); company.CompanyLogo = uploadedFileName; logoChanged = true; } bool IsEditing = company.Id != Guid.Empty; if (IsEditing) { Company curCompany = await CompanyAppService.Repository.GetAsync(company.Id); if (curCompany.CompanyLogo != null && curCompany.CompanyLogo != "noimage.jpg" && logoChanged) { string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "Uploads"); string filePath = Path.Combine(uploadsFolder, curCompany.CompanyLogo); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } } if (logoChanged) { curCompany.CompanyLogo = company.CompanyLogo; } else { company.CompanyLogo = curCompany.CompanyLogo; } if (AuditingManager.Current != null) { EntityChangeInfo entityChangeInfo = new EntityChangeInfo(); entityChangeInfo.EntityId = company.Id.ToString(); entityChangeInfo.EntityTenantId = company.TenantId; entityChangeInfo.ChangeTime = DateTime.Now; entityChangeInfo.ChangeType = EntityChangeType.Updated; entityChangeInfo.EntityTypeFullName = typeof(Company).FullName; entityChangeInfo.PropertyChanges = new List <EntityPropertyChangeInfo>(); List <EntityPropertyChangeInfo> entityPropertyChanges = new List <EntityPropertyChangeInfo>(); var auditProps = typeof(Company).GetProperties().Where(x => Attribute.IsDefined(x, typeof(CustomAuditedAttribute))).ToList(); Company mappedInput = ObjectMapper.Map <Company_Dto, Company>(company); foreach (var prop in auditProps) { EntityPropertyChangeInfo propertyChange = new EntityPropertyChangeInfo(); object origVal = prop.GetValue(curCompany); propertyChange.OriginalValue = origVal == null ? "" : origVal.ToString(); object newVal = prop.GetValue(mappedInput); propertyChange.NewValue = newVal == null ? "" : newVal.ToString(); if (propertyChange.OriginalValue == propertyChange.NewValue) { continue; } propertyChange.PropertyName = prop.Name; if (prop.Name.EndsWith("Id")) { try { string valuePropName = prop.Name.TrimEnd('d', 'I'); propertyChange.PropertyName = valuePropName; var valueProp = typeof(Company).GetProperty(valuePropName); DictionaryValue _origValObj = (DictionaryValue)valueProp.GetValue(company); if (_origValObj == null) { _origValObj = await DictionaryValuesRepo.GetAsync((Guid)origVal); } string _origVal = _origValObj.Value; propertyChange.OriginalValue = origVal == null ? "" : _origVal; DictionaryValue _newValObj = (DictionaryValue)valueProp.GetValue(mappedInput); if (_newValObj == null) { _newValObj = await DictionaryValuesRepo.GetAsync((Guid)newVal); } string _newVal = _newValObj.Value; propertyChange.NewValue = _newValObj == null ? "" : _newVal; } catch (Exception ex) { } } propertyChange.PropertyTypeFullName = prop.Name.GetType().FullName; entityChangeInfo.PropertyChanges.Add(propertyChange); } #region ExtraProperties List <EmployeeExtraPropertyHistory> allExtraPropertyHistories = new List <EmployeeExtraPropertyHistory>(); if (company.ExtraProperties != curCompany.ExtraProperties) { //GeneralInfo oldGeneralInfo = company.GetProperty<GeneralInfo>("generalInfo"); //List<EmployeeExtraPropertyHistory> physicalIdsHistory = new List<EmployeeExtraPropertyHistory>(); //var generalInfoPhysicalIdAuditProps = typeof(PhysicalID).GetProperties().Where(x => Attribute.IsDefined(x, typeof(CustomAuditedAttribute))).ToList(); //List<PhysicalId<Guid>> NewPhysicalIds = generalInfo.PhysicalIds.Where(x => !oldGeneralInfo.PhysicalIds.Any(y => y.Id == x.Id)).ToList(); //List<PhysicalId<Guid>> UpdatedPhysicalIds = generalInfo.PhysicalIds.Where(x => oldGeneralInfo.PhysicalIds.Any(y => y.Id == x.Id)).ToList(); //List<PhysicalId<Guid>> DeletedPhysicalIds = oldGeneralInfo.PhysicalIds.Where(x => !generalInfo.PhysicalIds.Any(y => y.Id == x.Id)).ToList(); //for (int i = 0; i < NewPhysicalIds.Count; i++) //{ // PhysicalId<Guid> curPhId = generalInfo.PhysicalIds[i]; // EmployeeExtraPropertyHistory newPhIdHistory = new EmployeeExtraPropertyHistory(2, "Physical Id", curPhId.IDNumber, "Created"); // physicalIdsHistory.Add(newPhIdHistory); //} //for (int i = 0; i < UpdatedPhysicalIds.Count; i++) //{ // PhysicalId<Guid> curPhId = generalInfo.PhysicalIds[i]; // PhysicalId<Guid> oldPhId = oldGeneralInfo.PhysicalIds.First(x => x.Id == curPhId.Id); // EmployeeExtraPropertyHistory updatedPhIdHistory = new EmployeeExtraPropertyHistory(2, "Physical Id", curPhId.IDNumber, "Updated"); // foreach (var prop in generalInfoPhysicalIdAuditProps) // { // updatedPhIdHistory.PropertyChanges = new List<EmployeeTypePropertyChange>(); // EmployeeTypePropertyChange propertyChange = new EmployeeTypePropertyChange(); // object origVal = prop.GetValue(oldPhId); // propertyChange.OriginalValue = origVal == null ? "" : origVal.ToString(); // object newVal = prop.GetValue(curPhId); // propertyChange.NewValue = newVal == null ? "" : newVal.ToString(); // if (propertyChange.OriginalValue == propertyChange.NewValue) continue; // propertyChange.PropertyName = prop.Name; // if (prop.Name.EndsWith("Id")) // { // try // { // string valuePropName = prop.Name.TrimEnd('d', 'I'); // propertyChange.PropertyName = valuePropName; // var valueProp = typeof(PhysicalID).GetProperty(valuePropName); // DictionaryValue _origValObj = (DictionaryValue)valueProp.GetValue(oldPhId); // if (_origValObj == null) _origValObj = await DictionaryValuesRepo.GetAsync((Guid)origVal); // string _origVal = _origValObj.Value; // propertyChange.OriginalValue = origVal == null ? "" : _origVal; // DictionaryValue _newValObj = (DictionaryValue)valueProp.GetValue(curPhId); // if (_newValObj == null) _newValObj = await DictionaryValuesRepo.GetAsync((Guid)newVal); // string _newVal = _newValObj.Value; // propertyChange.NewValue = _newValObj == null ? "" : _newVal; // } // catch (Exception ex) // { // } // } // propertyChange.PropertyTypeFullName = prop.Name.GetType().FullName; // updatedPhIdHistory.PropertyChanges.Add(propertyChange); // } // physicalIdsHistory.Add(updatedPhIdHistory); //} //for (int i = 0; i < DeletedPhysicalIds.Count; i++) //{ // PhysicalId<Guid> curPhId = generalInfo.PhysicalIds[i]; // EmployeeExtraPropertyHistory deletedPhIdHistory = new EmployeeExtraPropertyHistory(2, "Physical Id", curPhId.IDNumber, "Deleted"); // physicalIdsHistory.Add(deletedPhIdHistory); //} entityChangeInfo.SetProperty("extraPropertiesHistory", allExtraPropertyHistories); } #endregion AuditingManager.Current.Log.EntityChanges.Add(entityChangeInfo); } curCompany.CompanyName = company.CompanyName; curCompany.CompanyNameLocalized = company.CompanyNameLocalized; curCompany.CompanyLogo = company.CompanyLogo; curCompany.CompanyCode = company.CompanyCode; curCompany.Status = company.Status; curCompany.TaxID = company.TaxID; curCompany.SocialInsuranceID = company.SocialInsuranceID; curCompany.VATID = company.VATID; curCompany.RegistrationID = company.RegistrationID; curCompany.LabourOfficeId = company.LabourOfficeId; curCompany.Language = company.Language; CompanyLocation_Dto[] compLocs = company.CompanyLocations.ToArray(); Guid[] curCompLocsIds = curCompany.CompanyLocations.Select(x => x.Location.Id).ToArray(); List <Guid> toDeleteLocs = new List <Guid>(); for (int i = 0; i < curCompLocsIds.Length; i++) { CompanyLocation curCompanyLocation = curCompany.CompanyLocations.First(x => x.Location.Id == curCompLocsIds[i]); if (!compLocs.Any(x => x.LocationId == curCompLocsIds[i] && x.CreationTime == curCompanyLocation.CreationTime)) { curCompany.CompanyLocations.Remove(curCompany.CompanyLocations.First(x => x.Location.Id == curCompLocsIds[i])); toDeleteLocs.Add(curCompLocsIds[i]); } } for (int i = 0; i < compLocs.Length; i++) { if (!curCompany.CompanyLocations.Any(x => x.LocationId == compLocs[i].Location.Id && x.CreationTime == compLocs[i].CreationTime)) { curCompany.CompanyLocations.Add(new CompanyLocation() { Name = compLocs[i].Name, LocationValidityStart = compLocs[i].LocationValidityStart, LocationValidityEnd = compLocs[i].LocationValidityEnd, LocationId = compLocs[i].Location.Id, LocationType = compLocs[i].LocationType }); } else { var _companyLoc = curCompany.CompanyLocations.First(x => x.LocationId == compLocs[i].Location.Id); _companyLoc.LocationValidityStart = compLocs[i].LocationValidityStart; _companyLoc.LocationValidityEnd = compLocs[i].LocationValidityEnd; _companyLoc.Name = compLocs[i].Name; //curCompany.CompanyLocations.Remove(curCompany.CompanyLocations.First(x => x.LocationId == _companyLoc.LocationId)); await CompanyAppService.LocationsRepository.UpdateAsync(_companyLoc); } } CompanyCurrency_Dto[] compCurrencies = company.CompanyCurrencies.ToArray(); int[] curCompCurrenciesIds = curCompany.CompanyCurrencies.Select(x => x.Currency.Id).ToArray(); for (int i = 0; i < compCurrencies.Length; i++) { if (!curCompCurrenciesIds.Contains(compCurrencies[i].Currency.Id)) { curCompany.CompanyCurrencies.Add(new CompanyCurrency() { ExchangeRate = compCurrencies[i].ExchangeRate, CurrencyId = compCurrencies[i].Currency.Id, Status = compCurrencies[i].Status }); } else { var _companyCurrency = curCompany.CompanyCurrencies.First(x => x.CurrencyId == compCurrencies[i].Currency.Id); _companyCurrency.ExchangeRate = compCurrencies[i].ExchangeRate; _companyCurrency.Status = compCurrencies[i].Status; //curCompany.CompanyLocations.Remove(curCompany.CompanyLocations.First(x => x.LocationId == _companyLoc.LocationId)); await CompanyAppService.CurrenciesRepository.UpdateAsync(_companyCurrency); } } List <int> toDeleteCurrencies = new List <int>(); for (int i = 0; i < curCompCurrenciesIds.Length; i++) { if (!compCurrencies.Any(x => x.Currency.Id == curCompCurrenciesIds[i])) { curCompany.CompanyCurrencies.Remove(curCompany.CompanyCurrencies.First(x => x.CurrencyId == curCompCurrenciesIds[i])); toDeleteCurrencies.Add(curCompCurrenciesIds[i]); } } CompanyPrintSize_Dto[] compPrintSizes = company.CompanyPrintSizes.ToArray(); int[] curCompPrintSizesIds = curCompany.CompanyPrintSizes.Select(x => x.Id).ToArray(); for (int i = 0; i < compPrintSizes.Length; i++) { if (!curCompPrintSizesIds.Contains(compPrintSizes[i].Id)) { curCompany.CompanyPrintSizes.Add(new CompanyPrintSize() { PrintSize = compPrintSizes[i].PrintSize }); } } List <int> toDeletePrintSizes = new List <int>(); for (int i = 0; i < curCompPrintSizesIds.Length; i++) { if (!compPrintSizes.Any(x => x.Id == curCompPrintSizesIds[i])) { curCompany.CompanyPrintSizes.Remove(curCompany.CompanyPrintSizes.First(x => x.Id == curCompPrintSizesIds[i])); toDeletePrintSizes.Add(curCompPrintSizesIds[i]); } } CompanyDocument_Dto[] compDocuments = company.CompanyDocuments.ToArray(); int[] curCompDocumentsIds = curCompany.CompanyDocuments.Select(x => x.Id).ToArray(); for (int i = 0; i < compDocuments.Length; i++) { if (!curCompDocumentsIds.Contains(compDocuments[i].Id)) { string curFileName = compDocuments[i].Document.Name; if (FormData.Files.Any(x => x.Name == curFileName)) { var curDoc = compDocuments[i].Document; if (curDoc.Id == Guid.Empty) { IFormFile document = FormData.Files.First(x => x.Name == curFileName); string uploadedFileName = UploadedFile(document); Document_Dto doc = new Document_Dto(GuidGenerator.Create()); doc.ReferenceNo = (new Random()).Next(10, 90) * (new Random()).Next(10000, 90000); doc.Name = company.CompanyName + "_" + compDocuments[i].DocumentType.Value + "_" + compDocuments[i].Id; doc.NameLocalized = company.CompanyNameLocalized + "_" + compDocuments[i].DocumentType.Value + "_" + compDocuments[i].Id; doc.Description = $"Soft copy of {compDocuments[i].DocumentType.Value}"; doc.OwnerId = company.Id; doc.OwnerTypeId = DictionaryValuesRepo.WithDetails().Where(x => x.Value == "Company").First(x => x.ValueType.ValueTypeFor == ValueTypeModules.OwnerType).Id; doc.DocumentTypeId = compDocuments[i].DocumentType.Id; doc.IssuedFromId = doc.OwnerTypeId; doc.IssueDate = compDocuments[i].IssueDate; doc.ExpiryDate = compDocuments[i].EndDate; doc.FileName = uploadedFileName; Document_Dto created = await documentAppService.CreateAsync(doc); compDocuments[i].DocumentId = created.Id; } } curCompany.CompanyDocuments.Add(new CompanyDocument() { DocumentTitle = compDocuments[i].DocumentTitle, DocumentTitleLocalized = compDocuments[i].DocumentTitleLocalized, DocumentTypeId = compDocuments[i].DocumentType.Id, DocumentId = compDocuments[i].DocumentId, IssueDate = compDocuments[i].IssueDate, EndDate = compDocuments[i].EndDate }); } } List <CompanyDocument> toDeleteDocuments = new List <CompanyDocument>(); for (int i = 0; i < curCompDocumentsIds.Length; i++) { if (!compDocuments.Any(x => x.Id == curCompDocumentsIds[i])) { var doc = curCompany.CompanyDocuments.First(x => x.Id == curCompDocumentsIds[i]); toDeleteDocuments.Add(doc); curCompany.CompanyDocuments.Remove(doc); } } for (int i = 0; i < toDeletePrintSizes.Count; i++) { await CompanyAppService.PrintSizesRepository.DeleteAsync(x => x.Id == toDeletePrintSizes[i]); } for (int i = 0; i < toDeleteLocs.Count; i++) { await CompanyAppService.LocationsRepository.DeleteAsync(x => x.LocationId == toDeleteLocs[i]); } for (int i = 0; i < toDeleteCurrencies.Count; i++) { await CompanyAppService.CurrenciesRepository.DeleteAsync(x => x.CurrencyId == toDeleteCurrencies[i]); } for (int i = 0; i < toDeleteDocuments.Count; i++) { await documentAppService.Repository.DeleteAsync(toDeleteDocuments[i].DocumentId); await CompanyAppService.DocumentsRepository.DeleteAsync(x => x.Id == toDeleteDocuments[i].Id); } Company_Dto updated = ObjectMapper.Map <Company, Company_Dto>(await CompanyAppService.Repository.UpdateAsync(curCompany)); return(StatusCode(200, updated)); } else { List <Document_Dto> documentsToAdd = new List <Document_Dto>(); List <CompanyDocument_Dto> companyDocuments = company.CompanyDocuments; if (FormData.Files.Count > 0) { for (int i = 0; i < companyDocuments.Count; i++) { CompanyDocument_Dto compDoc = companyDocuments[i]; string curFileName = compDoc.Document.Name; if (FormData.Files.Any(x => x.Name == curFileName)) { var curDoc = compDoc.Document; if (curDoc.Id == Guid.Empty) { IFormFile document = FormData.Files.First(x => x.Name == curFileName); string uploadedFileName = UploadedFile(document); Document_Dto doc = new Document_Dto(GuidGenerator.Create()); doc.ReferenceNo = (new Random()).Next(10, 90) * (new Random()).Next(10000, 90000); doc.Name = company.CompanyName + "_" + compDoc.DocumentType.Value + "_" + compDoc.Id; doc.NameLocalized = company.CompanyNameLocalized + "_" + compDoc.DocumentType.Value + "_" + compDoc.Id; doc.Description = $"Soft copy of {compDoc.DocumentType.Value}"; doc.OwnerId = company.Id; doc.OwnerTypeId = DictionaryValuesRepo.WithDetails().Where(x => x.Value == "Company").First(x => x.ValueType.ValueTypeFor == ValueTypeModules.OwnerType).Id; doc.DocumentTypeId = compDoc.DocumentTypeId; doc.IssuedFromId = compDoc.Document.IssuedFromId; doc.IssueDate = compDoc.IssueDate; doc.ExpiryDate = compDoc.EndDate; doc.FileName = uploadedFileName; Document_Dto created = await documentAppService.CreateAsync(doc); compDoc.DocumentId = created.Id; } } } if (company.CompanyLogo == null) { company.CompanyLogo = "noimage.jpg"; } } for (int i = 0; i < documentsToAdd.Count; i++) { Document_Dto curDoc = documentsToAdd[i]; } company.Id = Guid.Empty; company.CompanyCurrencies.ForEach(x => { x.Id = 0; x.CurrencyId = x.Currency.Id; x.Currency = null; }); company.CompanyLocations.ForEach(x => { x.Id = 0; x.LocationId = x.Location.Id; x.Location = null; }); company.CompanyPrintSizes.ForEach(x => { x.Id = 0; }); Company_Dto added = await CompanyAppService.CreateAsync(company); if (AuditingManager.Current != null) { EntityChangeInfo entityChangeInfo = new EntityChangeInfo(); entityChangeInfo.EntityId = added.Id.ToString(); entityChangeInfo.EntityTenantId = added.TenantId; entityChangeInfo.ChangeTime = DateTime.Now; entityChangeInfo.ChangeType = EntityChangeType.Created; entityChangeInfo.EntityTypeFullName = typeof(Company).FullName; AuditingManager.Current.Log.EntityChanges.Add(entityChangeInfo); } return(StatusCode(200, added)); } } catch (Exception ex) { } } return(StatusCode(500)); }