public ActionResult CreateUpdateUnitConfiguration(int id)
        {
            var configurationTypes = this.UnitConfigurationTypeService.GetAll();
            var configurations     = this.UnitConfigurationService.GetByUnitId(id, DateTime.Now.AddYears(-1), DateTime.Now);
            var configDictionary   = new Dictionary <int, UnitConfiguration>();

            foreach (var conft in configurationTypes)
            {
                var lastConfig = configurations.Where(x => x.Type.Id == conft.Id)
                                 .OrderByDescending(x => x.ConfigurationDate).FirstOrDefault();
                if (lastConfig != null)
                {
                    configDictionary.Add(conft.Id, lastConfig);
                }
            }

            var ConfigurationVm = new UnitConfigurationViewModel()
            {
                Configurations     = configDictionary,
                ConfigurationTypes = configurationTypes,
                UnitId             = id
            };

            return(View(ConfigurationVm));
        }
        public ActionResult CreateUpdateUnitConfiguration(UnitConfigurationViewModel configurationModel)
        {
            var configurationTypes = this.UnitConfigurationTypeService.GetAll();
            var configurations     = this.UnitConfigurationService.GetByUnitId(configurationModel.UnitId, DateTime.Now.AddYears(-1), DateTime.Now);
            var configDictionary   = new Dictionary <int, UnitConfiguration>();

            foreach (var conft in configurationTypes)
            {
                var lastConfig = configurations.Where(x => x.Type.Id == conft.Id)
                                 .OrderByDescending(x => x.ConfigurationDate).FirstOrDefault();
                if (lastConfig != null)
                {
                    configDictionary.Add(conft.Id, lastConfig);
                }
            }

            foreach (var configuration in configurationModel.UnitConfigurations)
            {
                var actualConfig = configDictionary.ContainsKey(configuration.UnitConfigurationTypeId) ?
                                   configDictionary[configuration.UnitConfigurationTypeId] : null;

                if (configuration.Value != 0 && (actualConfig == null || actualConfig.Value != configuration.Value))
                {
                    var confRequest = new UnitConfigurationRequest()
                    {
                        UnitConfigurationTypeId = configuration.UnitConfigurationTypeId,
                        UnitId = configurationModel.UnitId,
                        Value  = configuration.Value,
                    };

                    this.UnitConfigurationService.CreateUnitConfiguration(confRequest);
                }
            }

            return(View("CreateUpdateUnitConfigurationSuccess", configurationModel.UnitId));
        }