Defining weight
This section explains how to set the weight of an article.
Earch article should specify its weight, so that the total weight of a delivery can be calculated. The weight should be a numeric field in the article field template.
Once each article has a weight, the total weight of a delivery can be calculated as follows:
Each order row in Litium Studio records the id of the delivery to which it belongs. Thefore, when we have the delivery and the order carrier, we can calculate the weight by simply adding weight of all the order rows together.
/// <summary>
/// Gets the total delivery weight.
/// </summary>
/// <param name="orderCarrier">The order carrier.</param>
/// <param name="deliveryCarrier">The delivery carrier.</param>
/// <param name="token">The token.</param>
/// <returns></returns>
private static decimal GetTotalDeliveryWeight(OrderCarrier orderCarrier, DeliveryCarrier deliveryCarrier, SecurityToken token)
{
var itemsInDelivery = from r in orderCarrier.OrderRows
where r.DeliveryID == deliveryCarrier.ID && !r.CarrierState.IsMarkedForDeleting
select r;
decimal totalWeight = 0;
foreach (var item in itemsInDelivery)
{
try
{
var article = Article.GetArticle(item.ArticleNumber, token);
var weightField = article.Properties[WeightFieldDefinitionName];
if (weightField != null && weightField.Count > 0 && weightField[0].Value != null)
{
totalWeight += (decimal)((NumberPropertyValue)(weightField[0].Value)).Value * (item.Quantity);
}
}
catch (ArticleDoesNotExistException)
{
//article might have got deleted while the calculation is running.
//TODO: log error to error log and throw exception.
}
}
return totalWeight;
}