Esempio n. 1
0
		public TariffDetailsViewModel(Tariff tariff = null)
		{
			EditDevicesCommand = new RelayCommand(OnEditDevicesCommand);
			TariffTypes = new ObservableCollection<TariffType>(Enum.GetValues(typeof(TariffType)).Cast<TariffType>());
			TariffParts = new ObservableCollection<TariffPartViewModel>();
			if (tariff == null)
			{
				IsNew = true;
				Tariff = new Tariff
					{
						Description = "",
						Devices = new List<Device>(),
						Name = "Новый тариф",
						TariffParts = new List<TariffPart>(),
					};
				Title = "Создание нового тарифа";
				SelectedTariffPartsNumber = 1;
			}
			else
			{
				IsNew = false;
				Tariff = tariff;
				Title = "Редактирование тарифа: " + tariff.TariffType.ToDescription();
				SelectedTariffPartsNumber = tariff.TariffParts.Count;
				SelectedTariffType = tariff.TariffType;
			}
			GetMaxTariffParts();
		}
		public TariffSelectionViewModel(DeviceDetailsViewModel parentViewModel)
		{
			Tariffs = new ObservableCollection<Tariff>(DbCache.Tariffs.Where(x => 
				x.TariffType == parentViewModel.TariffType && 
				x.TariffParts.Count <= parentViewModel.Device.MaxTatiffParts));
			_emptyTariff = new Tariff { Name = "Открепить от тарифа" };
			Tariffs.Insert(0, _emptyTariff);
			if(parentViewModel.Tariff != null)
				SelectedTariff = Tariffs.FirstOrDefault(x => x.UID == parentViewModel.Tariff.UID);
		}
Esempio n. 3
0
		public static void CreateTariff(Tariff tariff)
		{
			using (var context = DatabaseContext.Initialize())
			{
				var devicesToUpdate = tariff.Devices;
				tariff.Devices = new List<Device>();
				var tariffEntity = context.Tariffs.Add(tariff);

				foreach (var device in devicesToUpdate)
				{
					var dbDevice = context.Devices.FirstOrDefault(x => x.UID == device.UID);
					if (dbDevice != null)
						dbDevice.TariffUID = tariff.UID;
				}
				context.SaveChanges();
			}
			Tariffs.Add(tariff);
		}
Esempio n. 4
0
		public static void UpdateTariff(Tariff tariff)
		{
			using (var context = DatabaseContext.Initialize())
			{
				var tariffEntity = context.Tariffs.Include(x => x.Devices).Include(x => x.TariffParts).FirstOrDefault(x => x.UID == tariff.UID);
				if (tariffEntity != null)
				{
					context.TariffParts.RemoveRange(tariffEntity.TariffParts);
					tariffEntity.UID = tariff.UID;
					tariffEntity.Name = tariff.Name;
					tariffEntity.Description = tariff.Description;
					tariffEntity.TariffType = tariff.TariffType;
					tariffEntity.IsDiscount = tariff.IsDiscount;
					tariffEntity.Devices = new List<Device>();
					var devicesToUpdate = tariff.Devices;
					foreach (var device in devicesToUpdate)
					{
						var dbDevice = context.Devices.FirstOrDefault(x => x.UID == device.UID);
						if (dbDevice != null)
							dbDevice.TariffUID = tariff.UID;
						tariffEntity.Devices.Add(dbDevice);
					}
					tariffEntity.TariffParts = new List<TariffPart>();
					tariffEntity.TariffParts.AddRange(tariff.TariffParts
						.Select(x => new TariffPart
						{
							UID = x.UID,
							Price = x.Price,
							Threshold = x.Threshold,
							Discount = x.Discount,
							StartTime = x.StartTime,
							Tariff = tariffEntity,
						}));
					context.SaveChanges();
				}
			}
			Tariffs.RemoveAll(x => x.UID == tariff.UID);
			Tariffs.Add(tariff);
		}
Esempio n. 5
0
		public TariffDevicesViewModel(Tariff tariff)
		{
			_tariff = tariff;
			Title = "Выбор счётчиков для привязки";
			Devices = new ObservableCollection<TariffDeviceViewModel>();
			GetAllDevices();
			SelectedDevices = new List<TariffDeviceViewModel>();

			foreach (var device in Devices)
			{
				if (device.Device.TariffUID != null)
				{
					if (device.Device.TariffUID == _tariff.UID)
					{
						device.IsChecked = true;
					}
					else
					{
						device.HasTariff = true;
					}
				}
			}
		}
Esempio n. 6
0
		public FilterTariffViewModel(Tariff tariff)
		{
			Tariff = tariff;
		}
Esempio n. 7
0
		public TariffViewModel (Tariff tariff)
		{
			Tariff = tariff;
		}
Esempio n. 8
0
		//device is not deleted from dbcashe
		public static void DeleteTariff(Tariff tariff)
		{
			using (var context = DatabaseContext.Initialize())
			{
				var tmp = context.Tariffs.FirstOrDefault(x => x.UID == tariff.UID);
				foreach (var device in context.Devices)
				{
					device.Tariff = null;
				}
				context.Tariffs.Remove(tmp);
				context.SaveChanges();
			}
			Tariffs.Remove(tariff);
		}
Esempio n. 9
0
		public TariffPart(Tariff tariff)
		{
			UID = Guid.NewGuid();
			Tariff = tariff;
		}