public static decimal ConvertToTarget(ShipmentQuantityUnits source, ShipmentQuantityUnits target, decimal value, bool throwOnLossOfPrecision = true) { if (source == target) { return(value); } if (ShipmentQuantityUnitsMetadata.IsWeightUnit(source) && ShipmentQuantityUnitsMetadata.IsVolumeUnit(target)) { ThrowOnInvalidConversion(source, target); } if (ShipmentQuantityUnitsMetadata.IsVolumeUnit(source) && ShipmentQuantityUnitsMetadata.IsWeightUnit(target)) { ThrowOnInvalidConversion(source, target); } if (source == ShipmentQuantityUnits.Tonnes && target == ShipmentQuantityUnits.Kilograms) { return(TonnesToKilograms(value)); } if (source == ShipmentQuantityUnits.CubicMetres && target == ShipmentQuantityUnits.Litres) { return(CubicMetersToLitres(value)); } if (source == ShipmentQuantityUnits.Kilograms && target == ShipmentQuantityUnits.Tonnes) { var convertedValue = KilogramsToTonnes(value); if (WouldLosePrecision(convertedValue, throwOnLossOfPrecision)) { ThrowOnPrecisionLoss(source, target, value); } return(convertedValue); } if (source == ShipmentQuantityUnits.Litres && target == ShipmentQuantityUnits.CubicMetres) { var convertedValue = LitresToCubicMeters(value); if (WouldLosePrecision(convertedValue, throwOnLossOfPrecision)) { ThrowOnPrecisionLoss(source, target, value); } return(convertedValue); } ThrowOnInvalidConversion(source, target); throw new InvalidOperationException(); }
private decimal?GetIntendedTonnes(Shipment source) { if (!source.TotalQuantityUnitsId.HasValue || !source.TotalQuantity.HasValue || ShipmentQuantityUnitsMetadata.IsVolumeUnit(source.TotalQuantityUnitsId.Value)) { return(null); } return(ShipmentQuantityUnitConverter.ConvertToTarget(source.TotalQuantityUnitsId.Value, ShipmentQuantityUnits.Tonnes, source.TotalQuantity.Value, false)); }
private decimal?GetActualTonnes(Shipment source) { if (!source.Units.HasValue || !source.QuantityReceived.HasValue || ShipmentQuantityUnitsMetadata.IsVolumeUnit(source.Units.Value)) { return(null); } return(ShipmentQuantityUnitConverter.ConvertToTarget(source.Units.Value, ShipmentQuantityUnits.Tonnes, source.QuantityReceived.Value, false)); }