public async Task <ActionResult> SoleTraderDetails(SoleTraderDetailsViewModel model)
        {
            SetBreadcrumb(InternalUserActivity.CreateOrganisation);

            if (!ModelState.IsValid)
            {
                var countries = await GetCountries();

                model.Address.Countries = countries;
                return(View(model));
            }

            using (var client = apiClient())
            {
                var request = new CreateOrganisationAdmin()
                {
                    Address          = model.Address,
                    BusinessName     = model.CompanyName,
                    TradingName      = model.BusinessTradingName,
                    OrganisationType = model.OrganisationType.GetValueFromDisplayName <OrganisationType>()
                };

                var id = await client.SendAsync(User.GetAccessToken(), request);

                return(RedirectToAddAction(id, model.EntityType));
            }
        }
        public async Task SoleTraderDetailsPost_Always_SetsInternalBreadcrumb()
        {
            var viewModel = new SoleTraderDetailsViewModel()
            {
                BusinessTradingName = "Company",
                OrganisationType    = "Sole trader or individual",
                Address             = A.Dummy <AddressData>()
            };

            await controller.SoleTraderDetails(viewModel);

            Assert.Equal("Add new organisation", breadcrumbService.InternalActivity);
        }
        public async Task <ActionResult> SoleTraderDetails(string organisationType, EntityType entityType, string searchedText = null)
        {
            SetBreadcrumb(InternalUserActivity.CreateOrganisation);

            var countries = await GetCountries();

            var model = new SoleTraderDetailsViewModel
            {
                CompanyName      = searchedText,
                OrganisationType = organisationType,
                EntityType       = entityType,
                Address          = { Countries = countries }
            };

            return(View(model));
        }
        public async Task SoleTraderDetailsPost_ValidViewModel_RequestWithCorrectParametersCreated()
        {
            var viewModel = new SoleTraderDetailsViewModel()
            {
                CompanyName      = "Company",
                OrganisationType = "Sole trader or individual",
                Address          = A.Dummy <AddressData>()
            };

            viewModel.Address.Countries = countries;

            await controller.SoleTraderDetails(viewModel);

            A.CallTo(() => weeeClient.SendAsync(A <string> ._, A <CreateOrganisationAdmin> .That.Matches(
                                                    p => p.BusinessName == viewModel.CompanyName &&
                                                    p.Address == viewModel.Address &&
                                                    p.OrganisationType == viewModel.OrganisationType.GetValueFromDisplayName <OrganisationType>()))).MustHaveHappened(Repeated.Exactly.Once);
        }
        public async Task SoleTraderDetailsPost_ModelNotValid_ReturnsView()
        {
            var viewModel = new SoleTraderDetailsViewModel
            {
                BusinessTradingName = "Company", OrganisationType = "Sole trader or individual", Address = { Countries = countries }
            };

            controller.ModelState.AddModelError("error", "error");

            var result = await controller.SoleTraderDetails(viewModel) as ViewResult;

            var resultViewModel = result.Model as SoleTraderDetailsViewModel;

            Assert.True(string.IsNullOrEmpty(result.ViewName) || result.ViewName == "SoleTraderOrPartnershipDetails");
            Assert.Equal(viewModel.BusinessTradingName, resultViewModel.BusinessTradingName);
            Assert.Equal(viewModel.OrganisationType, resultViewModel.OrganisationType);
            Assert.Equal(countries, resultViewModel.Address.Countries);
        }
        public async Task SoleTraderDetailsPost_ValidViewModel_ReturnsCorrectRedirect(EntityType type, string expectedController, string expectedAction)
        {
            var viewModel = new SoleTraderDetailsViewModel
            {
                BusinessTradingName = "Company",
                OrganisationType    = "Sole trader or individual",
                EntityType          = type,
                Address             = { Countries = countries }
            };

            var result = await controller.SoleTraderDetails(viewModel) as RedirectToRouteResult;

            result.RouteValues["action"].Should().Be(expectedAction);
            result.RouteValues["controller"].Should().Be(expectedController);

            result.RouteValues["organisationId"].Should().NotBe(null);

            if (type != EntityType.Pcs)
            {
                result.RouteValues["facilityType"].Should().Be(viewModel.EntityType);
            }
        }
        public async Task<ActionResult> SoleTraderDetails(SoleTraderDetailsViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            using (var client = apiClient())
            {
                if (model.OrganisationId != null)
                {
                    // update orgnisation details
                    UpdateOrganisationTypeDetails updateRequest = new UpdateOrganisationTypeDetails(
                        model.OrganisationId.Value,
                        OrganisationType.SoleTraderOrIndividual,
                        String.Empty,
                        model.BusinessTradingName,
                        String.Empty);

                    Guid organisationId = await client.SendAsync(User.GetAccessToken(), updateRequest);
                    return RedirectToAction("MainContactPerson", new { organisationId });
                }

                CreateSoleTraderRequest request = new CreateSoleTraderRequest
                {
                    TradingName = model.BusinessTradingName
                };
                //create the organisation only if does not exist
                Guid orgId = await client.SendAsync(User.GetAccessToken(), request);
                return RedirectToAction("MainContactPerson", new { organisationId = orgId });
            }
        }
        public async Task<ActionResult> SoleTraderDetails(Guid? organisationId = null, string searchedText = null)
        {
            if (organisationId != null)
            {
                using (var client = apiClient())
                {
                    var organisation = await GetOrganisation(organisationId, client);

                    if (organisation.OrganisationType == OrganisationType.SoleTraderOrIndividual)
                    {
                        var model = new SoleTraderDetailsViewModel
                        {
                            BusinessTradingName = organisation.TradingName,
                            OrganisationId = organisationId.Value
                        };

                        return View("SoleTraderDetails", model);
                    }
                    return View(new SoleTraderDetailsViewModel { BusinessTradingName = searchedText });
                }
            }
            return View(new SoleTraderDetailsViewModel { BusinessTradingName = searchedText });
        }