Example #1
0
        public async Task <IActionResult> GetDynamicsLegalEntity(string id)
        {
            ViewModels.AdoxioLegalEntity result = null;
            // query the Dynamics system to get the legal entity record.
            if (string.IsNullOrEmpty(id))
            {
                return(new NotFoundResult());
            }
            else
            {
                // get the current user.
                string       temp         = _httpContextAccessor.HttpContext.Session.GetString("UserSettings");
                UserSettings userSettings = JsonConvert.DeserializeObject <UserSettings>(temp);

                Guid adoxio_legalentityid = new Guid(id);
                MicrosoftDynamicsCRMadoxioLegalentity adoxioLegalEntity = await _dynamicsClient.GetLegalEntityById(adoxio_legalentityid);

                //prevent getting legal entity data if the user is not associated with the account
                if (adoxioLegalEntity == null || !DynamicsExtensions.CurrentUserHasAccessToAccount(new Guid(adoxioLegalEntity._adoxioAccountValue), _httpContextAccessor, _dynamicsClient))
                {
                    return(new NotFoundResult());
                }
                result = adoxioLegalEntity.ToViewModel();
            }

            return(Json(result));
        }
Example #2
0
        public async Task <IActionResult> UpdateDynamicsLegalEntity([FromBody] ViewModels.AdoxioLegalEntity item, string id)
        {
            if (id != item.id)
            {
                return(BadRequest());
            }

            // get the legal entity.
            Guid adoxio_legalentityid = new Guid(id);

            MicrosoftDynamicsCRMadoxioLegalentity adoxioLegalEntity = await _dynamicsClient.GetLegalEntityById(adoxio_legalentityid);

            if (adoxioLegalEntity == null)
            {
                return(new NotFoundResult());
            }

            // we are doing a patch, so wipe out the record.
            adoxioLegalEntity = new MicrosoftDynamicsCRMadoxioLegalentity();

            // copy values over from the data provided
            adoxioLegalEntity.CopyValues(item);

            await _dynamicsClient.Legalentities.UpdateAsync(adoxio_legalentityid.ToString(), adoxioLegalEntity);

            adoxioLegalEntity = await _dynamicsClient.GetLegalEntityById(adoxio_legalentityid);

            return(Json(adoxioLegalEntity.ToViewModel()));
        }
Example #3
0
        public async Task <IActionResult> GetApplicantDynamicsLegalEntity()
        {
            ViewModels.AdoxioLegalEntity result = null;

            // get the current user.
            string       temp         = _httpContextAccessor.HttpContext.Session.GetString("UserSettings");
            UserSettings userSettings = JsonConvert.DeserializeObject <UserSettings>(temp);

            // check that the session is setup correctly.
            userSettings.Validate();

            // query the Dynamics system to get the legal entity record.
            MicrosoftDynamicsCRMadoxioLegalentity legalEntity = null;

            _logger.LogError("Find legal entity for applicant = " + userSettings.AccountId.ToString());

            legalEntity = _dynamicsClient.GetAdoxioLegalentityByAccountId(Guid.Parse(userSettings.AccountId));

            if (legalEntity == null)
            {
                return(new NotFoundResult());
            }
            // fix the account.

            result = legalEntity.ToViewModel();

            if (result.account == null)
            {
                MicrosoftDynamicsCRMaccount account = await _dynamicsClient.GetAccountById(Guid.Parse(userSettings.AccountId));

                result.account = account.ToViewModel();
            }

            return(Json(result));
        }
        public async Task <IActionResult> CreateDynamicsShareholderLegalEntity([FromBody] ViewModels.AdoxioLegalEntity item)
        {
            if (item == null)
            {
                return(BadRequest());
            }

            var adoxioLegalEntity = new MicrosoftDynamicsCRMadoxioLegalentity();

            adoxioLegalEntity.CopyValues(item);

            if (item.isShareholder == true && item.isindividual != true)
            {
                var account = new MicrosoftDynamicsCRMaccount();
                account.Name = item.name;
                account.AdoxioAccounttype  = (int)Adoxio_accounttypecodes.Shareholder;
                account.AdoxioBusinesstype = (int)Enum.ToObject(typeof(Gov.Lclb.Cllb.Public.ViewModels.Adoxio_applicanttypecodes), item.legalentitytype);
                account = await _dynamicsClient.Accounts.CreateAsync(account);

                adoxioLegalEntity.AdoxioAccountValueODataBind = _dynamicsClient.GetEntityURI("accounts", account.Accountid);
            }
            else
            {
                adoxioLegalEntity.AdoxioAccountValueODataBind = _dynamicsClient.GetEntityURI("accounts", item.account.id);
            }

            adoxioLegalEntity.AdoxioLegalEntityOwnedODataBind = _dynamicsClient.GetEntityURI("adoxio_legalentities", item.parentLegalEntityId);

            adoxioLegalEntity = await _dynamicsClient.Adoxiolegalentities.CreateAsync(adoxioLegalEntity);

            return(Json(adoxioLegalEntity.ToViewModel()));
        }
Example #5
0
        public async Task <IActionResult> UpdateDynamicsLegalEntity([FromBody] ViewModels.LegalEntity item, string id)
        {
            if (id != item.id)
            {
                return(BadRequest());
            }

            // get the legal entity.
            Guid adoxio_legalentityid = new Guid(id);

            MicrosoftDynamicsCRMadoxioLegalentity adoxioLegalEntity = await _dynamicsClient.GetLegalEntityById(adoxio_legalentityid);

            if (adoxioLegalEntity == null)
            {
                return(new NotFoundResult());
            }

            // we are doing a patch, so wipe out the record.
            adoxioLegalEntity = new MicrosoftDynamicsCRMadoxioLegalentity();

            // copy values over from the data provided
            adoxioLegalEntity.CopyValues(item);
            try
            {
                _dynamicsClient.Legalentities.Update(adoxio_legalentityid.ToString(), adoxioLegalEntity);
            }
            catch (HttpOperationException httpOperationException)
            {
                _logger.LogError(httpOperationException, $"Error updating legal entity: {httpOperationException.Request.Content} Response: {httpOperationException.Response.Content}");
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Unexpected Exception while updating legal entity");
            }


            adoxioLegalEntity = await _dynamicsClient.GetLegalEntityById(adoxio_legalentityid);

            return(new JsonResult(adoxioLegalEntity.ToViewModel()));
        }
Example #6
0
        public async Task <IActionResult> CreateDynamicsLegalEntity([FromBody] ViewModels.AdoxioLegalEntity item)
        {
            // create a new legal entity.
            MicrosoftDynamicsCRMadoxioLegalentity adoxioLegalEntity = new MicrosoftDynamicsCRMadoxioLegalentity();

            // get the current user.
            string       temp         = _httpContextAccessor.HttpContext.Session.GetString("UserSettings");
            UserSettings userSettings = JsonConvert.DeserializeObject <UserSettings>(temp);

            // check that the session is setup correctly.
            userSettings.Validate();
            // copy received values to Dynamics LegalEntity
            adoxioLegalEntity.CopyValues(item);
            try
            {
                adoxioLegalEntity = await _dynamicsClient.Legalentities.CreateAsync(adoxioLegalEntity);
            }
            catch (OdataerrorException odee)
            {
                _logger.LogError("Error creating legal entity");
                _logger.LogError("Request:");
                _logger.LogError(odee.Request.Content);
                _logger.LogError("Response:");
                _logger.LogError(odee.Response.Content);
                throw new Exception("Unable to create legal entity");
            }

            // setup navigation properties.
            MicrosoftDynamicsCRMadoxioLegalentity patchEntity = new MicrosoftDynamicsCRMadoxioLegalentity();
            Guid accountId   = Guid.Parse(userSettings.AccountId);
            var  userAccount = await _dynamicsClient.GetAccountById(accountId);

            patchEntity.AdoxioAccountValueODataBind = _dynamicsClient.GetEntityURI("accounts", accountId.ToString());

            // patch the record.
            try
            {
                await _dynamicsClient.Legalentities.UpdateAsync(adoxioLegalEntity.AdoxioLegalentityid, patchEntity);
            }
            catch (OdataerrorException odee)
            {
                _logger.LogError("Error patching legal entity");
                _logger.LogError(odee.Request.RequestUri.ToString());
                _logger.LogError("Request:");
                _logger.LogError(odee.Request.Content);
                _logger.LogError("Response:");
                _logger.LogError(odee.Response.Content);
            }

            // TODO take the default for now from the parent account's legal entity record
            // TODO likely will have to re-visit for shareholders that are corporations/organizations
            MicrosoftDynamicsCRMadoxioLegalentity tempLegalEntity = _dynamicsClient.GetAdoxioLegalentityByAccountId(Guid.Parse(userSettings.AccountId));

            if (tempLegalEntity != null)
            {
                Guid tempLegalEntityId = Guid.Parse(tempLegalEntity.AdoxioLegalentityid);

                // see https://msdn.microsoft.com/en-us/library/mt607875.aspx
                patchEntity = new MicrosoftDynamicsCRMadoxioLegalentity();
                patchEntity.AdoxioLegalEntityOwnedODataBind = _dynamicsClient.GetEntityURI("adoxio_legalentities", tempLegalEntityId.ToString());

                // patch the record.
                try
                {
                    await _dynamicsClient.Legalentities.UpdateAsync(adoxioLegalEntity.AdoxioLegalentityid, patchEntity);
                }
                catch (OdataerrorException odee)
                {
                    _logger.LogError("Error adding LegalEntityOwned reference to legal entity");
                    _logger.LogError(odee.Request.RequestUri.ToString());
                    _logger.LogError("Request:");
                    _logger.LogError(odee.Request.Content);
                    _logger.LogError("Response:");
                    _logger.LogError(odee.Response.Content);
                }
            }

            return(Json(adoxioLegalEntity.ToViewModel()));
        }
Example #7
0
        public IActionResult CreateDynamicsShareholderLegalEntity([FromBody] ViewModels.LegalEntity item)
        {
            if (item == null)
            {
                return(BadRequest());
            }

            MicrosoftDynamicsCRMadoxioLegalentity adoxioLegalEntity = new MicrosoftDynamicsCRMadoxioLegalentity();

            adoxioLegalEntity.CopyValues(item);

            if (item.isindividual != true)
            {
                var account = new MicrosoftDynamicsCRMaccount();
                account.Name = item.name;
                if (item.isShareholder == true)
                {
                    account.AdoxioAccounttype = (int)AdoxioAccountTypeCodes.Shareholder;
                }
                else if (item.isPartner == true)
                {
                    account.AdoxioAccounttype = (int)AdoxioAccountTypeCodes.Partner;
                }
                if (item.legalentitytype != null)
                {
                    account.AdoxioBusinesstype = (int)Enum.ToObject(typeof(Gov.Lclb.Cllb.Public.ViewModels.AdoxioApplicantTypeCodes), item.legalentitytype);
                }
                try
                {
                    account = _dynamicsClient.Accounts.Create(account);
                }
                catch (HttpOperationException httpOperationException)
                {
                    _logger.LogError(httpOperationException, $"Error creating account: {httpOperationException.Request.Content} Response: {httpOperationException.Response.Content}");
                }
                catch (Exception e)
                {
                    _logger.LogError(e, $"Unexpected Exception while creating tied house connection");
                }

                //create tied house under account
                var tiedHouse = new MicrosoftDynamicsCRMadoxioTiedhouseconnection()
                {
                    AccountODataBind = _dynamicsClient.GetEntityURI("accounts", account.Accountid)
                };

                adoxioLegalEntity.AdoxioShareholderAccountODataBind = _dynamicsClient.GetEntityURI("accounts", account.Accountid);
                try
                {
                    _dynamicsClient.Tiedhouseconnections.Create(tiedHouse);
                }
                catch (HttpOperationException httpOperationException)
                {
                    _logger.LogError(httpOperationException, $"Error creating tied house connection");
                }
                catch (Exception e)
                {
                    _logger.LogError(e, $"Unexpected Exception while creating tied house connection");
                }
            }
            adoxioLegalEntity.AdoxioAccountValueODataBind     = _dynamicsClient.GetEntityURI("accounts", item.account.id);
            adoxioLegalEntity.AdoxioLegalEntityOwnedODataBind = _dynamicsClient.GetEntityURI("adoxio_legalentities", item.parentLegalEntityId);

            try
            {
                adoxioLegalEntity = _dynamicsClient.Legalentities.Create(adoxioLegalEntity);
            }
            catch (HttpOperationException httpOperationException)
            {
                _logger.LogError(httpOperationException, $"Error creating legal entity: {httpOperationException.Request.Content} Response: {httpOperationException.Response.Content}");
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Unexpected Exception while creating legal entity");
            }

            return(new JsonResult(adoxioLegalEntity.ToViewModel()));
        }