private void CreateLeaseAgeements() { // Lease Agreements int counter = 0; bool createLeaseAgreements = true; var leaseAgreements = _context.LeaseAgreements.ToList(); if (leaseAgreements.Count > 0) { var lastLeaseAgreement = leaseAgreements.Last(); if (leaseAgreementReferences.Contains(lastLeaseAgreement.Reference)) { createLeaseAgreements = false; } ; } if (createLeaseAgreements) { var assetOwner = _context.AssetOwners.FirstOrDefault(); do { int? assetOwnerId = (leaseAgreementAssetOwnerIds[counter].ToUpper() != "NULL") ? (int?)Convert.ToInt32(leaseAgreementAssetOwnerIds[counter]) : null; int? customerId = (leaseAgreementCustomerIds[counter].ToUpper() != "NULL") ? (int?)Convert.ToInt32(leaseAgreementCustomerIds[counter]) : null; DateTime?endDate = (leaseAgreementEndDates[counter].ToUpper() != "NULL") ? (DateTime?)DateTime.ParseExact(leaseAgreementEndDates[counter], "yyyy-mm-dd", null) : null; DateTime?startDate = (leaseAgreementStartDates[counter].ToUpper() != "NULL") ? (DateTime?)DateTime.ParseExact(leaseAgreementStartDates[counter], "yyyy-mm-dd", null) : null; int? contactId = (leaseAgreementContactIds[counter].ToUpper() != "NULL") ? (int?)Convert.ToInt32(leaseAgreementContactIds[counter]) : null; LeaseAgreement leaseAgreement = new LeaseAgreement() { AssetOwnerId = assetOwnerId, CustomerId = customerId, Description = leaseAgreementDescriptions[counter], EndDate = endDate, StartDate = startDate, Reference = leaseAgreementReferences[counter], TenantId = assetOwner.TenantId, Terms = leaseAgreementTerms[counter], Title = leaseAgreementTitles[counter], ContactId = contactId, CreationTime = DateTime.Now, IsDeleted = false }; var createdLeaseAgreement = _context.LeaseAgreements.Add(leaseAgreement).Entity; _context.SaveChanges(); counter = counter + 1; } while (counter < leaseAgreementReferences.Count()); } // Add Assets counter = 0; bool createLeaseItems = true; var leaseItems = _context.LeaseItems.ToList(); if (leaseItems.Count > 0) { var lastLeaseItem = leaseItems.Last(); if (leaseItemItems.Contains(lastLeaseItem.Item)) { createLeaseItems = false; } ; } if (createLeaseItems) { var assetOwner = _context.AssetOwners.FirstOrDefault(); do { int? assetClassId = (leaseItemAssetClassIds[counter].ToUpper() != "NULL") ? (int?)Convert.ToInt32(leaseItemAssetClassIds[counter]) : null; int? assetId = (leaseItemAssetIds[counter].ToUpper() != "NULL") ? (int?)Convert.ToInt32(leaseItemAssetIds[counter]) : null; DateTime?dateAllocated = (leaseItemDateAllocateds[counter].ToUpper() != "NULL") ? (DateTime?)DateTime.ParseExact(leaseItemDateAllocateds[counter], "yyyy-mm-dd", null) : null; int? depositUomRefId = (leaseItemDepositUomRefIds[counter].ToUpper() != "NULL") ? (int?)Convert.ToInt32(leaseItemDepositUomRefIds[counter]) : null; DateTime?endDate = (leaseItemEndDates[counter].ToUpper() != "NULL") ? (DateTime?)DateTime.ParseExact(leaseItemEndDates[counter], "yyyy-mm-dd", null) : null; DateTime?startDate = (leaseItemStartDates[counter].ToUpper() != "NULL") ? (DateTime?)DateTime.ParseExact(leaseItemStartDates[counter], "yyyy-mm-dd", null) : null; int? leaseAgreementId = (leaseItemLeaseAgreementIds[counter].ToUpper() != "NULL") ? (int?)Convert.ToInt32(leaseItemLeaseAgreementIds[counter]) : null; int? rentalUomRefId = (leaseItemRentalUomRefIds[counter].ToUpper() != "NULL") ? (int?)Convert.ToInt32(leaseItemRentalUomRefIds[counter]) : null; decimal? unitDepositRate = (leaseItemUnitDepositRates[counter].ToUpper() != "NULL") ? (decimal?)Convert.ToInt32(leaseItemUnitDepositRates[counter]) : null; decimal? unitRentalRate = (leaseItemUnitRentalRates[counter].ToUpper() != "NULL") ? (decimal?)Convert.ToInt32(leaseItemUnitRentalRates[counter]) : null; LeaseItem leaseItem = new LeaseItem() { AllocationPercentage = 100, AssetClassId = assetClassId, AssetId = assetId, DateAllocated = dateAllocated, DepositUomRefId = depositUomRefId, Description = leaseItemDescriptions[counter], EndDate = endDate, StartDate = startDate, Item = leaseItemItems[counter], LeaseAgreementId = leaseAgreementId, RentalUomRefId = rentalUomRefId, Terms = leaseItemTerms[counter], TenantId = assetOwner.TenantId, UnitDepositRate = unitDepositRate, UnitRentalRate = unitDepositRate, CreationTime = DateTime.Now, IsDeleted = false }; var createdLeaseItem = _context.LeaseItems.Add(leaseItem).Entity; _context.SaveChanges(); counter = counter + 1; } while (counter < leaseItemItems.Count()); } }
public virtual void HandleLeaseItemEvent(LeaseItemDto leaseItemDto, TenantInfo tenantInfo) { // Add Cross-Tenant Permissions between Vendors and Customers based on the new LeaseItem // via SupportItems for the relevant Asset // Permissions are for Incidents, WorkOrders, Assets and Users List <string> entities = new List <string>() { "Incident", "WorkOrder", "Asset", "User" }; bool clearCache = false; LeaseAgreement leaseAgreement = _leaseAgreementRepository.Get((int)leaseItemDto.LeaseAgreementId); Customer customer = _customerRepository.Get((int)leaseAgreement.CustomerId); int customerTenantId = (int)customer.TenantId; List <int> supportContractIds = _supportItemRepository.GetAll().Where(s => s.AssetId == leaseItemDto.AssetId).Select(s => (int)s.SupportContractId).ToList(); List <int> vendorIds = _supportContractRepository.GetAll().Where(l => supportContractIds.Contains(l.Id)).Select(l => (int)l.VendorId).Distinct().ToList(); List <int> vendorTenantIds = _vendorRepository.GetAll().Where(c => vendorIds.Contains(c.Id)).Select(c => (int)c.TenantId).Distinct().ToList(); var allCtps = _crossTenantPermissionRepository.GetAllList(); foreach (var entity in entities) { foreach (var vendorTenantId in vendorTenantIds) { // Add the Vendor Tenant Ids to the Customers' CTPs var existingCustomerPermission = allCtps.Where(p => p.TenantId == customerTenantId && p.EntityType == entity).FirstOrDefault(); if (existingCustomerPermission != null) { var existingIds = existingCustomerPermission.Tenants.Split(',').ToList().Select(int.Parse).ToList(); if (!existingIds.Contains(vendorTenantId)) { existingIds.Add(vendorTenantId); var ctp = _crossTenantPermissionRepository.GetAll().Where(p => p.TenantId == customerTenantId && p.EntityType == entity).FirstOrDefault(); ctp.Tenants = string.Join(",", existingIds); _crossTenantPermissionRepository.Update(ctp); clearCache = true; } } else { CrossTenantPermission ctp = new CrossTenantPermission() { EntityType = entity, TenantId = customerTenantId, Tenants = vendorTenantId.ToString(), TenantType = "C", TenantRefId = customerTenantId }; _crossTenantPermissionRepository.Insert(ctp); clearCache = true; } // Add the Customer Tenant Id to this Vendors' CTPs var existingVendorPermission = allCtps.Where(p => p.TenantId == vendorTenantId && p.EntityType == entity).FirstOrDefault(); if (existingVendorPermission != null) { var existingIds = existingVendorPermission.Tenants.Split(',').ToList().Select(int.Parse).ToList(); if (!existingIds.Contains(customerTenantId)) { existingIds.Add(customerTenantId); var ctp = _crossTenantPermissionRepository.GetAll().Where(p => p.TenantId == vendorTenantId && p.EntityType == entity).FirstOrDefault(); ctp.Tenants = string.Join(",", existingIds); _crossTenantPermissionRepository.Update(ctp); clearCache = true; } } else { CrossTenantPermission ctp = new CrossTenantPermission() { EntityType = entity, TenantId = vendorTenantId, Tenants = customerTenantId.ToString(), TenantType = "V", TenantRefId = vendorTenantId }; _crossTenantPermissionRepository.Insert(ctp); clearCache = true; } } } if (clearCache) { _cacheManager.GetCrossTenantPermissionCache().Clear(); } }
public async Task <PagedResultDto <GetAttachmentForViewDto> > GetSome(GetSomeAttachmentsInput input) { using (_unitOfWorkManager.Current.DisableFilter(AbpDataFilters.MustHaveTenant, AbpDataFilters.MayHaveTenant)) // BYPASS TENANT FILTER to include Users { var tenantInfo = await TenantManager.GetTenantInfo(); var tenantType = tenantInfo.Tenant.TenantType; string relatedType = string.Empty; bool authForAsset = false; bool authForIncident = false; bool authForLeaseAgreement = false; bool authForQuotation = false; bool authForSupportContract = false; bool authForWorkOrder = false; // Check Authorization for Attachments related to Assets Asset relatedAsset = new Asset(); if (input.RelatedEntity == "Asset") { relatedAsset = _assetRepository.Get(input.ReferenceId); SupportItem relatedSupportItem = _supportItemRepository.GetAll().Where(c => c.AssetId == relatedAsset.Id).Include(c => c.SupportContractFk).FirstOrDefault(); switch (tenantType) { case "A": authForAsset = (relatedSupportItem?.SupportContractFk?.AssetOwnerId == tenantInfo.AssetOwner.Id) ? true : false; break; case "V": authForAsset = (relatedSupportItem?.SupportContractFk?.VendorId == tenantInfo.Vendor.Id) ? true : false; break; case "C": LeaseItem relatedLeaseItem = _leaseItemRepository.GetAll().Where(l => l.AssetId == relatedAsset.Id).Include(l => l.LeaseAgreementFk).FirstOrDefault(); authForAsset = (relatedLeaseItem?.LeaseAgreementFk?.CustomerId == tenantInfo.Customer.Id) ? true : false; break; default: authForAsset = true; break; } } SupportContract relatedSupportContract = new SupportContract(); if (input.RelatedEntity == "SupportContract") { relatedSupportContract = _supportContractRepository.Get(input.ReferenceId); switch (tenantType) { case "A": authForSupportContract = (relatedSupportContract?.AssetOwnerId == tenantInfo.AssetOwner.Id) ? true : false; break; case "V": authForSupportContract = (relatedSupportContract?.VendorId == tenantInfo.Vendor.Id) ? true : false; break; case "C": List <int> relatedAssetIds = _supportItemRepository.GetAll().Where(c => c.SupportContractId == input.ReferenceId).Select(c => c.AssetId).ToList(); List <LeaseItem> relatedLeaseItems = _leaseItemRepository.GetAll().Where(l => relatedAssetIds.Contains((int)l.AssetId)).Include(l => l.LeaseAgreementFk).ToList(); List <int?> relatedCustomerIds = relatedLeaseItems.Select(l => l.LeaseAgreementFk).Select(l => l.CustomerId).ToList(); authForSupportContract = (relatedCustomerIds.Contains(tenantInfo.Customer.Id)) ? true : false; break; default: authForSupportContract = true; break; } } WorkOrder relatedWorkOrder = new WorkOrder(); if (input.RelatedEntity == "WorkOrder") { relatedWorkOrder = _workOrderRepository.GetAll().Include(c => c.AssetOwnershipFk).Where(w => w.Id == input.ReferenceId).FirstOrDefault(); switch (tenantType) { case "A": authForWorkOrder = (relatedWorkOrder?.AssetOwnershipFk?.AssetOwnerId == tenantInfo.AssetOwner.Id) ? true : false; break; case "V": authForWorkOrder = (relatedWorkOrder?.VendorId == tenantInfo.Vendor.Id) ? true : false; break; case "C": authForWorkOrder = (relatedWorkOrder?.CustomerId == tenantInfo.Customer.Id) ? true : false; break; default: authForWorkOrder = true; break; } } // TODO: Complete Auth Check LeaseAgreement relatedLeaseAgreement = new LeaseAgreement(); if (input.RelatedEntity == "LeaseAgreement") { relatedLeaseAgreement = _leaseAgreementRepository.Get(input.ReferenceId); if (relatedLeaseAgreement?.Id > 0) { authForLeaseAgreement = true; } } // TODO: Complete Auth Check Incident relatedIncident = new Incident(); if (input.RelatedEntity == "Incident") { relatedIncident = _incidentRepository.Get(input.ReferenceId); if (relatedIncident?.Id > 0) { authForIncident = true; } } // TODO: Complete Auth Check Quotation relatedQuotation = new Quotation(); if (input.RelatedEntity == "Quotation") { relatedQuotation = _quotationRepository.Get(input.ReferenceId); if (relatedQuotation?.Id > 0) { authForQuotation = true; } } var filteredAttachments = _attachmentRepository.GetAll() .Include(e => e.AssetFk) .Include(e => e.IncidentFk) .Include(e => e.LeaseAgreementFk) .Include(e => e.QuotationFk) .Include(e => e.SupportContractFk) .Include(e => e.WorkOrderFk) .Include(e => e.CustomerInvoiceFk) .WhereIf((!authForAsset && !authForIncident && !authForLeaseAgreement && !authForQuotation && !authForSupportContract && !authForWorkOrder), e => e.Id == 0) .WhereIf(authForAsset, e => e.AssetId == input.ReferenceId) .WhereIf(authForIncident, e => e.IncidentId == input.ReferenceId) .WhereIf(authForLeaseAgreement, e => e.LeaseAgreementId == input.ReferenceId) .WhereIf(authForQuotation, e => e.QuotationId == input.ReferenceId) .WhereIf(authForSupportContract, e => e.SupportContractId == input.ReferenceId) .WhereIf(authForWorkOrder, e => e.WorkOrderId == input.ReferenceId); var pagedAndFilteredAttachments = filteredAttachments .OrderBy(input.Sorting ?? "id asc") .PageBy(input); var attachments = from o in pagedAndFilteredAttachments join o1 in _lookup_assetRepository.GetAll() on o.AssetId equals o1.Id into j1 from s1 in j1.DefaultIfEmpty() join o2 in _lookup_incidentRepository.GetAll() on o.IncidentId equals o2.Id into j2 from s2 in j2.DefaultIfEmpty() join o3 in _lookup_leaseAgreementRepository.GetAll() on o.LeaseAgreementId equals o3.Id into j3 from s3 in j3.DefaultIfEmpty() join o4 in _lookup_quotationRepository.GetAll() on o.QuotationId equals o4.Id into j4 from s4 in j4.DefaultIfEmpty() join o5 in _lookup_supportContractRepository.GetAll() on o.SupportContractId equals o5.Id into j5 from s5 in j5.DefaultIfEmpty() join o6 in _lookup_workOrderRepository.GetAll() on o.WorkOrderId equals o6.Id into j6 from s6 in j6.DefaultIfEmpty() join o7 in _lookup_customerInvoiceRepository.GetAll() on o.IncidentId equals o7.Id into j7 from s7 in j7.DefaultIfEmpty() join o8 in _lookup_userRepository.GetAll() on o.UploadedBy equals o8.Id into j8 from s8 in j8.DefaultIfEmpty() select new GetAttachmentForViewDto() { Attachment = new AttachmentDto { Filename = o.Filename, Description = o.Description, UploadedAt = o.UploadedAt, UploadedBy = o.UploadedBy, BlobFolder = o.BlobFolder, BlobId = o.BlobId, Id = o.Id }, AssetReference = s1 == null ? "" : s1.Reference.ToString(), IncidentDescription = s2 == null ? "" : s2.Description.ToString(), LeaseAgreementReference = s3 == null ? "" : s3.Reference.ToString(), QuotationTitle = s4 == null ? "" : s4.Title.ToString(), SupportContractTitle = s5 == null ? "" : s5.Title.ToString(), WorkOrderSubject = s6 == null ? "" : s6.Subject.ToString(), CustomerInvoiceDescription = s7 == null ? "" : s7.Description.ToString(), UploadedByName = s8 == null ? "" : s8.Name + " (" + s8.EmailAddress + ")" //AssetReference = relatedAsset.Reference.ToString(), //IncidentDescription = relatedIncident.Description.ToString(), //LeaseAgreementReference = relatedLeaseAgreement.Reference.ToString(), //QuotationTitle = relatedQuotation.Title.ToString(), //SupportContractTitle = relatedSupportContract.Title.ToString(), //WorkOrderSubject = relatedWorkOrder.Subject.ToString() }; var totalCount = await filteredAttachments.CountAsync(); var pagedResult = new PagedResultDto <GetAttachmentForViewDto>( totalCount, await attachments.ToListAsync()); return(pagedResult); } }