public Product ResolveProduct() { var productAttr = _attrs .ForEntity("new_product") .FirstOrDefault(); // product in Bordereau mapping has priority over the one in Bordereau process if (productAttr != null) { if (!productAttr.HasValue) { throw BordereauException.DataError("Product information is missing."); } var searchProductByField = productAttr.AttributeName; var searchProductByValue = productAttr.Value; var product = _svc.RetrieveMultipleByAttribute("new_product", searchProductByField, searchProductByValue).FirstOrDefault(); if (product == null) { throw BordereauException.DataError("Product not found for value '{0}'.".FormatWith(productAttr.Value)); //allErrors.AddError(new BordereauError(productAttr.TemplateColumn, // BordereauErrorType.BusinessError, // productAttr.Value, // ); } return(new Product(_svc, _trace, product)); } else if (_defaults.Product != null) { return(new Product(_svc, _trace, _svc.Retrieve(_defaults.Product))); } else { throw BordereauException.TemplateError("Product information is missing."); //allErrors.AddError(new BordereauError(BordereauErrorType.BusinessError, "Product information is missing.")); } }
public static CompositeAddress ProcessAddress(this IOrganizationService svc, IEnumerable <MappedAttribute> addressAttrs, EntityReference defaultCountry = null) { var postalCodeAttr = addressAttrs .ForAttribute("new_postalcode") .FirstOrDefault(); // no postal code means no address information if (postalCodeAttr == null || !postalCodeAttr.HasValue) { return(null); } #region resolve country EntityReference countryRef = null; var countryAttr = addressAttrs .ForAttribute("new_country") .FirstOrDefault(); if (defaultCountry != null) { countryRef = defaultCountry; } else if (countryAttr != null) { // TODO search by country code? var countryEntity = countryAttr.AsEntity(); if (countryEntity != null) { countryRef = countryEntity.ToEntityReference(); } } else { // if selecting country fails, it's not possible to create a valid address throw BordereauException.TemplateError("Country information is missing or in incorrect format."); } #endregion #region resolve postal code Entity postalCodeEntity = svc.SearchPostalCode(postalCodeAttr.AsString(), countryRef.Id); if (postalCodeEntity == null) { postalCodeEntity = new Entity("new_postalcode"); postalCodeEntity["new_name"] = postalCodeAttr.AsString(); postalCodeEntity["new_country"] = countryRef; postalCodeEntity.Id = svc.Create(postalCodeEntity); } var postalCodeRef = postalCodeEntity.ToEntityReference(); #endregion Entity address = null; // check if there are any more attributes than just postal code and country if (addressAttrs.ExcludeAttributes("new_country", "new_postalcode").Any()) { var street1Attr = addressAttrs .ForAttribute("new_street1") .FirstOrDefault(); var numberAttr = addressAttrs .ForAttribute("new_addressnumbertext") .FirstOrDefault(); var buildingAttr = addressAttrs .ForAttribute("new_addressname") .FirstOrDefault(); // try to find an existing address based on street, house no. and building information address = svc.MatchAddress(postalCodeRef.Id, (string)street1Attr, (string)numberAttr, (string)buildingAttr); // if address is not found, create a new record with data from Bx row if (address == null) { address = new Entity("new_address"); address.UpdateWithAttributes(addressAttrs.ExcludeAttributes("new_country", "new_postalcode")); address["new_addressorigin"] = AddressOrigin.Bordereaux.ToOptionSet(); address["new_country"] = countryRef; address["new_postalcode"] = postalCodeRef; } } return(new CompositeAddress() { Country = countryRef, PostalCode = postalCodeRef, Address = address }); }
public Entity CreatePolicyVersion(EntityReference policyHolder, EntityReference product) { var transactionAttr = _attrs .ForEntity("new_policy") .ForAttribute("new_transactiontype") .FirstOrDefault(); if (transactionAttr == null) { throw BordereauException.TemplateError("Transaction type of Policy is not mapped in Bordereau template."); } var transactionDateAttr = _attrs .ForEntity("new_policy") .ForAttribute("new_commencementofcover") .FirstOrDefault(); if (transactionDateAttr == null) { throw BordereauException.TemplateError("Transaction date is not mapped in Bordereau template."); } var transactionType = transactionAttr.AsOptionSet().ToEnum <PolicyVersionTransactionType>(); var transactionDate = transactionDateAttr.AsDateTime(); _trace.Trace("Searching for policy folder by policy number: '{0}'", this.PolicyNumber); var policyFolder = _svc.RetrieveMultipleByName("new_policyfolder", this.PolicyNumber) .FirstOrDefault(); if (policyFolder == null) { _trace.Trace("Policy with number '{0}' does not exist.", this.PolicyNumber); } _trace.Trace("Creating risk identifier for current row data."); //var currentRiskIdentifier = CreateRiskIdentifier(riskClass); //_trace.Trace("Risk identifier: {0}", currentRiskIdentifier); // re-upload o same file/rows // - same policy number // - same transaction type // - same transaction effective date // - same risk entity (e.g. vehicle) // result: ignore row, it should not be processed //_trace.Trace("Searching for existing policy version: re-upload of row."); //var previouslyUploaded = FindExistingPolicyVersion(transactionType, transactionDate.Value, currentRiskIdentifier); //if (previouslyUploaded != null) //{ // _trace.Trace("Policy with the same number and risk identifier already exists. Skipping the row."); // return null; //} // 2. adding additional risk object to existing policy version // - same policy number // - same transaction type // - same transaction effective date // - different risk entity (e.g. vehicle) // result: pick only risk entity (and insured risk) attributes from row, create additional insured risk(s) for PV _trace.Trace("Searching for existing policy version: additional risk row."); var previouslyUploaded = FindExistingPolicyVersion(transactionType, transactionDate.Value); if (previouslyUploaded != null) { _trace.Trace("Policy with this number already exists."); //additional risk will be added based on this row. return(null); } Entity policyVersion = null; if (transactionType == PolicyVersionTransactionType.Cancellation) { if (policyFolder != null) { _trace.Trace("Cancellation: Updating policy folder with cancellation information."); // update policy folder, simulate cancellation request, // creation of policy version will do the rest var policyFolderToUpdate = new Entity(policyFolder.LogicalName) { Id = policyFolder.Id }; policyFolderToUpdate["new_cancellationresponsible"] = PolicyCancellationResponsible.Broker.ToOptionSet(); _svc.Update(policyFolderToUpdate); } } if (previouslyUploaded == null) { _trace.Trace("Policy version was not previously uploaded. Creating new policy version."); // new policy version policyVersion = new Entity("new_policy"); policyVersion["new_name"] = this.PolicyNumber; policyVersion["new_inputchannel"] = PolicyInputChannel.Import.ToOptionSet(); policyVersion["new_broker"] = _defaults.Broker; // link policy version with current bordereau process policyVersion["new_bordereauxprocessid"] = _defaults.BordereauProcess; policyVersion["new_monthlybordereau"] = _defaults.MonthlyBordereau; policyVersion.UpdateWithAttributes(_attrs.ForEntity("new_policy")); _trace.Trace("- Setting policy holder."); // set policy holder of policy, based on entity name if (this.PolicyHolderEntityName == "account") { policyVersion["new_insuredid"] = policyHolder; } else if (this.PolicyHolderEntityName == "contact") { policyVersion["new_insured_contact"] = policyHolder; } _trace.Trace("- Setting product."); // set product policyVersion["new_productid"] = product; // set policy folder, if already exists if (policyFolder != null) { policyVersion["new_policy"] = policyFolder.ToEntityReference(); } _trace.Trace("- Creating policy record."); policyVersion.Id = _svc.Create(policyVersion); // retrieve policy version to get all updates done by plugin(s) _trace.Trace("- Retrieving policy with latest data."); policyVersion = _svc.Retrieve(policyVersion.ToEntityReference()); } else { // policy version exists, but we should add insured risk/risk object policyVersion = previouslyUploaded; } // at this point, policy version, insured risks and covers exist in CRM // and can be processed further return(policyVersion); #region TODO fix this!!! //if (transactionType == PolicyVersionTransactionType.NewPolicy) //{ // if (policyFolder != null) // { // } // else // { // //var policyFolderWrapped = new Policy(context.OrganizationService, context.TracingService, policyFolder); // //if (policyFolderWrapped.Versions.Any(version => version.TransactionType == PolicyVersionTransactionType.NewPolicy)) // //{ // // // TODO log error for duplicate NewPolicy transaction? // // //allErrors.AddError(new BordereauError(policyFolderNumberAttr.TemplateColumn, BordereauErrorType.BusinessError, "'New Policy' transaction already exists.")); // // continue; // //} // } //} //else if (policyFolder == null) //{ // //allErrors.AddError(new BordereauError(policyFolderNumberAttr.TemplateColumn, BordereauErrorType.BusinessError, policyFolderNumberAttr.Value, "Policy with number='{0}' does not exist.".FormatWith(policyFolderNumberAttr.Value))); // //continue; //} #endregion }