Code: Select all
IOrder order = ...
// Get the first HUD.
IList HUDs = (IList)order.GetProperty("HUDs");
IOrderItem hud = (IOrderItem)HUDs[0];
// Get next line for the section 1100.
int sectionNumBase = 1100;
IOrderItem nextLine = GetNextAvailableHudSectionLine(hud, sectionNumBase);
// Make sure we got a line since there is a maximum number the section can hold.
if (nextLine == null)
{
// We can't add the line. Do something else!
return;
}
//Set description on first charge.Line will inherit the description if only one charge.
IList charges = (IList)nextLine.GetProperty("Charges");
IOrderItem firstCharge = (IOrderItem)charges[0];
firstCharge.SetProperty("Description", "Our charge");
...
// Save the order.
os.ApplyChanges(order);
os.CloseOrder(order);
Code: Select all
/// <summary>
/// Search the current section lines and return the first one without a line description
/// and with a line amount of $0. If can't find one, return a new one.
/// </summary>
/// <param name="hud">The HUD object</param>
/// <param name="section">Name of section to get line from</param>
/// <returns></returns>
private IOrderItem GetNextAvailableHudSectionLine(IOrderItem hud, int sectionBaseNumber)
{
// Get the section.
IDictionary lines = (IDictionary)hud.GetProperty($"Section{sectionBaseNumber}");
// Walk the current lines to find an empty one.
foreach (IOrderItem item in lines.Values)
{
if (String.IsNullOrWhiteSpace((string)item.GetProperty("Description"))
&& (decimal)item.GetProperty("Amount") <= 0m)
{
// No description and amount is zero. Pick this one.
return item;
}
}
// No free lines. Make sure we are allowed to create a new one.
// Change the max count to the one needed for your section!
if (lines.Count > 99)
{
// Nope. Return null as flag to caller.
return null;
}
// We can safely add a new line to the section. We need to calculate the object name for HUD.
IOrderItem newLine = (IOrderItem)hud.Order.CreateNew($"HUD{sectionBaseNumber}CommonLine");
IList sectionLines = (IList)hud.GetProperty($"Section{sectionBaseNumber}");
sectionLines.Add(newLine);
return newLine;
}