-
-
Save ste-bel/96f6b77a93c14ed8cd70db21b4bfab92 to your computer and use it in GitHub Desktop.
A static Scriban function to convert based on the message, the scheme and other values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string Convert(TemplateContext context, SourceSpan span, string convertFrom, string conversionID, object _row_, string _fieldName_) { | |
if (convertFrom == null) { | |
return null; | |
} | |
if (conversionID == null) { | |
throw new ScriptRuntimeException(span, "You must provide a conversion ID in order to convert data"); | |
} | |
PXTrace.WriteVerbose($"Will convert data for '{conversionID}'"); | |
var messType = ScribanUtils.GetMessageType(context, span); | |
var message = ScribanUtils.GetMessage(context, span); | |
var scheme = messType.ConversionScheme;// What scheme to use | |
var server = ScribanUtils.GetServer(context, span);// What server am I being sent | |
var organizationID = server?.OrganizationID;// What is the organization of this server | |
var bAccountID = message.BAccountID;// Who is the B. Account for this message | |
var parentBAccountID = message.ParentBAccountID;// Who is the Parent B. Account for this message | |
// Find the cached Behaviour on what to do if data is missing | |
string whenNotFound = null; | |
if (ConversionBehaviours.TryGetHelper(conversionID, out var helperBehav)) { | |
helperBehav.TryGetValue(scheme, messType.MessageType, organizationID, bAccountID, out whenNotFound); | |
} | |
if (string.IsNullOrEmpty(whenNotFound)) { | |
whenNotFound = B2WhenNotFound.ThrowError; | |
} | |
// Find the helper based on the conversionID (Branch, Customer. Vendor, ShippingTerms, Currency, etc.) | |
if (ConversionDetails.TryGetHelper(conversionID, out var helperDetails)) { | |
// Found the helper, convert | |
if (helperDetails.TryGetValue(scheme, organizationID, bAccountID, parentBAccountID, messType.MessageType, convertFrom, out string convertTo)) { | |
return convertTo; | |
} | |
} else { | |
// Did not find the helper | |
if (whenNotFound == B2WhenNotFound.ThrowError) { | |
throw new ScriptRuntimeException(span, $"Could not get a Convertor for ID '{conversionID}'"); | |
} | |
} | |
// Did not find the data | |
if (whenNotFound == B2WhenNotFound.ThrowError) { | |
throw new ScriptRuntimeException(span, $"Could not get convert value '{convertFrom}' with Convertor '{conversionID}'"); | |
} | |
// Keep the original data | |
return convertFrom; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment