Exemplo n.º 1
0
        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();
        }
Exemplo n.º 2
0
        private decimal?GetActualCubicMetres(Shipment source)
        {
            if (!source.Units.HasValue ||
                !source.QuantityReceived.HasValue ||
                ShipmentQuantityUnitsMetadata.IsWeightUnit(source.Units.Value))
            {
                return(null);
            }

            return(ShipmentQuantityUnitConverter.ConvertToTarget(source.Units.Value,
                                                                 ShipmentQuantityUnits.CubicMetres,
                                                                 source.QuantityReceived.Value,
                                                                 false));
        }
Exemplo n.º 3
0
        private decimal?GetIntendedCubicMetres(Shipment source)
        {
            if (!source.TotalQuantityUnitsId.HasValue ||
                !source.TotalQuantity.HasValue ||
                ShipmentQuantityUnitsMetadata.IsWeightUnit(source.TotalQuantityUnitsId.Value))
            {
                return(null);
            }

            return(ShipmentQuantityUnitConverter.ConvertToTarget(source.TotalQuantityUnitsId.Value,
                                                                 ShipmentQuantityUnits.CubicMetres,
                                                                 source.TotalQuantity.Value,
                                                                 false));
        }