public async Task <TenancyViewModel> UpdateAsync(Guid portfolioId, TenancyViewModel viewModel, ICollection <TenantViewModel> tenants)
        {
            var entity = await(from tenancy in Context.Tenancies
                               join propertyDetails in Context.PropertyDetails on tenancy.PropertyDetailsId equals propertyDetails.Id
                               join portfolio in Context.Portfolios on propertyDetails.PortfolioId equals portfolio.Id
                               where portfolio.Id == portfolioId && tenancy.Id == viewModel.Id && !tenancy.IsDeleted && !portfolio.IsDeleted && !portfolio.IsDeleted
                               select tenancy)
                         .SingleAsync();

            var propertyDetailsId = entity.PropertyDetailsId;

            entity.MapFrom(viewModel);

            var tenantTenacies = await(from tenantTenancy in Context.TenantTenancies.Include(x => x.Tenancy)
                                       where tenantTenancy.TenancyId == entity.Id && tenantTenancy.Tenancy.PropertyDetailsId == propertyDetailsId && !tenantTenancy.IsDeleted && !tenantTenancy.Tenancy.IsDeleted && !tenantTenancy.Tenant.IsDeleted
                                       select tenantTenancy)
                                 .ToListAsync();

            foreach (var tenantTenancy in tenantTenacies)
            {
                tenantTenancy.Tenancy.PropertyDetailsId = viewModel.PropertyDetailsId;
            }

            Context.Tenancies.Update(entity);
            await Context.SaveChangesAsync();

            return(new TenancyViewModel(entity, viewModel.PropertyDetailsId));
        }
        // GET: Tenancies/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            TenancyViewModel tenancyVM = new TenancyViewModel();
            var _tenancy = await _tenancySvc.GetTenancyAsync(id.Value);

            if (_tenancy == null)
            {
                return(NotFound());
            }

            var user = await _userManager.GetUserAsync(HttpContext.User); //Get logged in user

            tenancyVM = _mapper.Map <TenancyViewModel>(_tenancy);         //Map tenancy object to ViewModel

            //Get created by & update by username
            ApplicationUser createByUser = await _userManager.FindByIdAsync(tenancyVM.createdByUserID);

            tenancyVM.createdByUsername = createByUser.UserName;

            ApplicationUser updatedByUser = await _userManager.FindByIdAsync(tenancyVM.updatedByUserID);

            tenancyVM.updatedByUsername = updatedByUser.UserName;

            tenancyVM.tenancyhousehold = await _tenancySvc.GetTenancyHouseholdAsync(tenancyVM.tenancyId);   //Get household data for tenancy

            tenancyVM.property = await _propertySvc.GetPropertyViewAsync(tenancyVM.propertyId.Value);       //Get property view data for tenancy

            tenancyVM.actions = await _actionSvc.GetActionsForTenancyAsync(tenancyVM.tenancyId);            //Get actions data for tenancy

            tenancyVM.alerts = await _alertSvc.GetAlertsForTenancyAsync(tenancyVM.tenancyId);               //Get all alerts for tenancy

            tenancyVM.rentLedger = await _rentAccountSvc.GetRentLedgerForTenancyAsync(tenancyVM.tenancyId); //Get rent ledger records for tenancy

            ViewBag.ApiLocation    = _config["APILocation"];                                                //Set API location URL
            ViewBag.LoggedInUserId = user.Id;                                                               //Set logged in user Id

            return(View(tenancyVM));
        }
        public async Task <IActionResult> Edit(int id, [Bind("tenancyId,propertyId,leadTenantPersonId,jointTenantPersonId,tenureTypeId,startDate,terminationDate,terminationReasonId,updatedByUserID,updatedDT,createdByUserID,createdDT")] TenancyViewModel tenancyVM)
        {
            if (id != tenancyVM.tenancyId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var tenancy = _mapper.Map <tenancy>(tenancyVM);

                    var user = await _userManager.GetUserAsync(HttpContext.User); //Get logged in user

                    //Set created by & updated by values for record
                    tenancy.updatedByUserID = user.Id;
                    tenancy.updatedDT       = DateTime.Now;

                    await _tenancySvc.UpdateTenancyAsync(tenancy);
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!await _tenancySvc.TenancyExistsAsync(tenancyVM.tenancyId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Details), new { @id = tenancyVM.tenancyId }));
            }

            ViewData["terminationReasonId"] = new SelectList(await _tenancySvc.GetTenancyterminationreasonsAsync(), "tenancyTerminationReasonId", "terminationReason", tenancyVM.terminationReasonId);
            ViewData["tenureTypeId"]        = new SelectList(await _tenancySvc.GetTenuretypesAsync(), "tenureTypeId", "tenureType1", tenancyVM.tenureTypeId);
            return(View(tenancyVM));
        }
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var tenancy = await _tenancySvc.GetTenancyAsync(id.Value);

            if (tenancy == null)
            {
                return(NotFound());
            }

            TenancyViewModel tenancyVM = new TenancyViewModel();

            tenancyVM          = _mapper.Map <TenancyViewModel>(tenancy);
            tenancyVM.property = await _propertySvc.GetPropertyViewAsync(tenancyVM.propertyId.Value);

            ViewData["terminationReasonId"] = new SelectList(await _tenancySvc.GetTenancyterminationreasonsAsync(), "tenancyTerminationReasonId", "terminationReason", tenancyVM.terminationReasonId);
            ViewData["tenureTypeId"]        = new SelectList(await _tenancySvc.GetTenuretypesAsync(), "tenureTypeId", "tenureType1", tenancy.tenureTypeId);
            return(View(tenancyVM));
        }
        public async Task <IActionResult> Create([Bind("tenancyId,propertyId,leadTenantPersonId,jointTenantPersonId,tenureTypeId,startDate,terminationDate,terminationReasonId,updatedByUserID,updatedDT,createdByUserID,createdDT")] TenancyViewModel tenancyVM)
        {
            if (ModelState.IsValid)
            {
                DateTime recordDT = DateTime.Now;
                var      tenancy  = _mapper.Map <tenancy>(tenancyVM);                  //Map viewmodel values to tenancy model object
                var      user     = await _userManager.GetUserAsync(HttpContext.User); //Get logged in user

                //Set created by & updated by values for record
                tenancy.createdByUserID = user.Id;
                tenancy.createdDT       = DateTime.Now;
                tenancy.updatedByUserID = user.Id;
                tenancy.updatedDT       = DateTime.Now;

                int newTenancyId = await _tenancySvc.AddTenancyAsync(tenancy);

                return(RedirectToAction(nameof(Details), new { @id = newTenancyId }));
            }

            ViewData["terminationReasonId"] = new SelectList(await _tenancySvc.GetTenancyterminationreasonsAsync(), "tenancyTerminationReasonId", "terminationReason", tenancyVM.terminationReasonId);
            ViewData["tenureTypeId"]        = new SelectList(await _tenancySvc.GetTenuretypesAsync(), "tenureTypeId", "tenureTypeId", tenancyVM.tenureTypeId);
            return(View(tenancyVM));
        }
        public async Task <TenancyViewModel> CreateAsync(Guid portfolioId, TenancyViewModel viewModel, ICollection <TenantViewModel> tenants)
        {
            //Untested
            var tenancy = new Tenancy
            {
                PropertyDetailsId      = viewModel.PropertyDetailsId,
                Created                = DateTime.Now,
                StartDate              = viewModel.StartDate,
                EndDate                = viewModel.EndDate,
                RentalAmount           = viewModel.RentalAmount,
                RentalFrequency        = viewModel.RentalFrequency,
                RentalPaymentReference = viewModel.RentalPaymentReference,
                TenancyType            = viewModel.TenancyType
            };

            await Context.Tenancies.AddAsync(tenancy);

            await Context.SaveChangesAsync();

            var tenancyViewModel = new TenancyViewModel(tenancy, tenancy.PropertyDetailsId);

            return(tenancyViewModel);
        }
        public async Task CreateTenantTenancyAsync(TenancyViewModel tenancy, ICollection <TenantViewModel> tenants)
        {
            if (tenants == null || tenants.Count == 0)
            {
                return;
            }

            foreach (var tenantViewModel in tenants)
            {
                var entity = await Context.TenantTenancies.FirstOrDefaultAsync(c => c.TenancyId == tenancy.Id && c.TenantId == tenantViewModel.Id && !c.IsDeleted);

                if (entity == null)
                {
                    await Context.TenantTenancies.AddAsync(new TenantTenancy
                    {
                        Created   = DateTime.Now,
                        TenancyId = tenancy.Id,
                        TenantId  = tenantViewModel.Id
                    });
                }
            }

            await Context.SaveChangesAsync();
        }