/// <summary>
        /// Deletes measure dimension
        /// </summary>
        /// <param name="measureDimension">Measure dimension</param>
        public virtual void DeleteMeasureDimension(MeasureDimension measureDimension)
        {
            if (measureDimension == null)
                throw new ArgumentNullException("measureDimension");

            _measureDimensionRepository.Delete(measureDimension);

            _cacheManager.RemoveByPattern(MEASUREDIMENSIONS_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityDeleted(measureDimension);
        }
        public void Can_save_and_load_measureDimension()
        {
            var measureDimension = new MeasureDimension
            {
                Name = "inch(es)",
                SystemKeyword = "inch",
                Ratio = 1.12345678M,
                DisplayOrder = 2,
            };

            var fromDb = SaveAndLoadEntity(measureDimension);
            fromDb.ShouldNotBeNull();
            fromDb.Name.ShouldEqual("inch(es)");
            fromDb.SystemKeyword.ShouldEqual("inch");
            fromDb.Ratio.ShouldEqual(1.12345678M);
            fromDb.DisplayOrder.ShouldEqual(2);
        }
        public ActionResult DimensionAdd([Bind(Exclude="Id")] MeasureDimensionModel model, GridCommand command)
        {
            if (_permissionService.Authorize(StandardPermissionProvider.ManageMeasures))
            {
                if (!ModelState.IsValid)
                {
                    var modelStateErrors = this.ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage);
                    return Content(modelStateErrors.FirstOrDefault());
                }

                var dimension = new MeasureDimension();
                dimension = model.ToEntity(dimension);

                _measureService.InsertMeasureDimension(dimension);
            }

            return Dimensions(command);
        }
        public new void SetUp()
        {
            measureDimension1 = new MeasureDimension()
            {
                Id = 1,
                Name = "inch(es)",
                SystemKeyword = "inch",
                Ratio = 1M,
                DisplayOrder = 1,
            };
            measureDimension2 = new MeasureDimension()
            {
                Id = 2,
                Name = "feet",
                SystemKeyword = "ft",
                Ratio = 0.08333333M,
                DisplayOrder = 2,
            };
            measureDimension3 = new MeasureDimension()
            {
                Id = 3,
                Name = "meter(s)",
                SystemKeyword = "m",
                Ratio = 0.0254M,
                DisplayOrder = 3,
            };
            measureDimension4 = new MeasureDimension()
            {
                Id = 4,
                Name = "millimetre(s)",
                SystemKeyword = "mm",
                Ratio = 25.4M,
                DisplayOrder = 4,
            };



            measureWeight1 = new MeasureWeight()
            {
                Id = 1,
                Name = "ounce(s)",
                SystemKeyword = "oz",
                Ratio = 16M,
                DisplayOrder = 1,
            };
            measureWeight2 = new MeasureWeight()
            {
                Id = 2,
                Name = "lb(s)",
                SystemKeyword = "lb",
                Ratio = 1M,
                DisplayOrder = 2,
            };
            measureWeight3 = new MeasureWeight()
            {
                Id = 3,
                Name = "kg(s)",
                SystemKeyword = "kg",
                Ratio = 0.45359237M,
                DisplayOrder = 3,
            };
            measureWeight4 = new MeasureWeight()
            {
                Id = 4,
                Name = "gram(s)",
                SystemKeyword = "g",
                Ratio = 453.59237M,
                DisplayOrder = 4,
            };

            _measureDimensionRepository = MockRepository.GenerateMock<IRepository<MeasureDimension>>();
            _measureDimensionRepository.Expect(x => x.Table).Return(new List<MeasureDimension>() { measureDimension1, measureDimension2, measureDimension3, measureDimension4 }.AsQueryable());
            _measureDimensionRepository.Expect(x => x.GetById(measureDimension1.Id)).Return(measureDimension1);
            _measureDimensionRepository.Expect(x => x.GetById(measureDimension2.Id)).Return(measureDimension2);
            _measureDimensionRepository.Expect(x => x.GetById(measureDimension3.Id)).Return(measureDimension3);
            _measureDimensionRepository.Expect(x => x.GetById(measureDimension4.Id)).Return(measureDimension4);

            _measureWeightRepository = MockRepository.GenerateMock<IRepository<MeasureWeight>>();
            _measureWeightRepository.Expect(x => x.Table).Return(new List<MeasureWeight>() { measureWeight1, measureWeight2, measureWeight3, measureWeight4 }.AsQueryable());
            _measureWeightRepository.Expect(x => x.GetById(measureWeight1.Id)).Return(measureWeight1);
            _measureWeightRepository.Expect(x => x.GetById(measureWeight2.Id)).Return(measureWeight2);
            _measureWeightRepository.Expect(x => x.GetById(measureWeight3.Id)).Return(measureWeight3);
            _measureWeightRepository.Expect(x => x.GetById(measureWeight4.Id)).Return(measureWeight4);


            var cacheManager = new NullCache();

            _measureSettings = new MeasureSettings();
            _measureSettings.BaseDimensionId = measureDimension1.Id; //inch(es)
            _measureSettings.BaseWeightId = measureWeight2.Id; //lb(s)

            _eventPublisher = MockRepository.GenerateMock<IEventPublisher>();
            _eventPublisher.Expect(x => x.Publish(Arg<object>.Is.Anything));

            _measureService = new MeasureService(cacheManager,
                _measureDimensionRepository,
                _measureWeightRepository,
                _measureSettings, _eventPublisher);
        }
 public static MeasureDimension ToEntity(this MeasureDimensionModel model, MeasureDimension destination)
 {
     return Mapper.Map(model, destination);
 }
 /// <summary>
 /// Converts from primary dimension
 /// </summary>
 /// <param name="quantity">Quantity</param>
 /// <param name="targetMeasureDimension">Target dimension</param>
 /// <returns>Converted value</returns>
 public virtual decimal ConvertFromPrimaryMeasureDimension(decimal quantity,
     MeasureDimension targetMeasureDimension)
 {
     decimal result = quantity;
     var baseDimensionIn = GetMeasureDimensionById(_measureSettings.BaseDimensionId);
     if (result != decimal.Zero && targetMeasureDimension.Id != baseDimensionIn.Id)
     {
         decimal exchangeRatio = targetMeasureDimension.Ratio;
         if (exchangeRatio == decimal.Zero)
             throw new SmartException(string.Format("Exchange ratio not set for dimension [{0}]", targetMeasureDimension.Name));
         result = result * exchangeRatio;
     }
     return result;
 }
 /// <summary>
 /// Converts dimension
 /// </summary>
 /// <param name="quantity">Quantity</param>
 /// <param name="sourceMeasureDimension">Source dimension</param>
 /// <param name="targetMeasureDimension">Target dimension</param>
 /// <param name="round">A value indicating whether a result should be rounded</param>
 /// <returns>Converted value</returns>
 public virtual decimal ConvertDimension(decimal quantity, 
     MeasureDimension sourceMeasureDimension, MeasureDimension targetMeasureDimension, bool round = true)
 {
     decimal result = quantity;
     if (result != decimal.Zero && sourceMeasureDimension.Id != targetMeasureDimension.Id)
     {
         result = ConvertToPrimaryMeasureDimension(result, sourceMeasureDimension);
         result = ConvertFromPrimaryMeasureDimension(result, targetMeasureDimension);
     }
     if (round)
         result = Math.Round(result, 2);
     return result;
 }