예제 #1
0
		public void Post([FromBody] RawMaterial material)
		{
			Argument.NotNull(material, "Material is required.");

			using (var storage = new Storage())
			{
				var existingMaterial = storage
					.RawMaterials
					.SingleOrDefault(rm => rm.RawMaterialId == material.RawMaterialId);

				if (null != existingMaterial)
				{
					storage.Update(material);
				}
				else
				{
					storage.Insert(material);
				}
			}
		}
		public RawMaterialType Post([FromBody] RawMaterialType rawMaterialType)
		{
			Argument.NotNull(rawMaterialType, "Price extra category is required.");

			using (var storage = new Storage())
			{
				var existingMaterial = storage
					.RawMaterialTypes
					.SingleOrDefault(rmt => rmt.RawMaterialTypeId == rawMaterialType.RawMaterialTypeId);

				if (null != existingMaterial)
				{
					storage.Update(rawMaterialType);
				}
				else
				{
					storage.Insert(rawMaterialType);
				}

				return rawMaterialType;
			}
		}
		public PriceExtraCategory Post([FromBody] PriceExtraCategory priceExtraCategory)
		{
			Argument.NotNull(priceExtraCategory, "Price extra category is required.");

			using (var storage = new Storage())
			{
				var existingCategory = storage
					.PriceExtraCategories
					.SingleOrDefault(pc => pc.PriceExtraCategoryId == priceExtraCategory.PriceExtraCategoryId);

				if (null != existingCategory)
				{
					storage.Update(priceExtraCategory);
				}
				else
				{
					storage.Insert(priceExtraCategory);
				}

				return priceExtraCategory;
			}
		}
예제 #4
0
		public void Post([FromBody] Manufacturer manufacturer)
		{
			Argument.NotNull(manufacturer, "Manufacturer is required.");

			if (manufacturer.IsPrimary)
				return;

			using (var storage = new Storage())
			{
				var existingManufacturer = storage
					.Manufacturers
					.SingleOrDefault(m => m.ManufacturerId == manufacturer.ManufacturerId);

				if (null != existingManufacturer)
				{
					storage.Update(manufacturer);
				}
				else
				{
					storage.Insert(manufacturer);
				}

			}
		}
예제 #5
0
		public void Post([FromBody] Product product)
		{
			Argument.NotNull(product, "Product is required.");

			using (var storage = new Storage())
			{
				var existingProduct = storage
					.Products
					.SingleOrDefault(p => p.ProductId == product.ProductId);

				if (null != existingProduct)
				{
					storage.Update(product);
				}
				else
				{
					storage.Insert(product);
				}
			}
		}
예제 #6
0
		public void SetExtraPrice(int priceExtraId, int ownerId, DateTime date, decimal? price)
		{
			Argument.Require(priceExtraId > 0, "Price extra id is required.");
			Argument.Require(ownerId > 0, "Owner id is required.");
			Argument.Require(date > DateTime.MinValue, "Price date is required.");
			Argument.Require(null == price || price.Value >= 0, "Price should be >= 0");

			using (var storage = new Storage())
			{
				var priceExtra = storage
					.PriceExtras
					.SingleOrDefault(pe => pe.PriceExtraId == priceExtraId);

				if(null == priceExtra)
					throw new InvalidOperationException($"Price extra with id {priceExtraId} not found.");

				var existingPriceItem = storage
					.PriceItems
					.SingleOrDefault(pi => pi.PriceExtraId == priceExtraId && pi.OwnerId == ownerId && pi.Date == date);

				if (null != existingPriceItem)
				{
					existingPriceItem.Price = price;
					storage.Update(existingPriceItem);
				}
				else
				{
					var priceItem = new PriceItem
					{
						ProductId = priceExtra.ProductId,
						PriceType = PriceType.PriceExtra,
						PriceExtraId = priceExtraId,
						Date = date,
						OwnerId = ownerId,
						Price = price,
					};

					storage.Insert(priceItem);
				}

				UpdatePriceCache(storage, priceExtra.ProductId, date);
			}
		}
예제 #7
0
		public void SetRetailPrice(int productId, DateTime date, decimal? price)
		{
			Argument.Require(productId > 0, "Price extra id is required.");
			Argument.Require(date > DateTime.MinValue, "Price date is required.");
			Argument.Require(null == price || price.Value >= 0, "Price should be >= 0");

			using (var storage = new Storage())
			{
				var product = storage
					.Products
					.LoadWith(p => p.RawMaterial.RawMaterialType)
					.SingleOrDefault(pe => pe.ProductId == productId);

				if (null == product)
					throw new InvalidOperationException($"Product with id {productId} not found.");

				var alloyType = product.RawMaterial.RawMaterialType.AlloyType;
				var rollType = product.RawMaterial.RawMaterialType.RollType;

				if (alloyType == AlloyType.Undefined
					|| rollType == RollType.Undefined)
				{
					throw new InvalidOperationException("Для сохранения цен поля Тип проката и Тип продукта обязательны.");
				}


				var existingPriceItem = storage
					.PriceItems
					.SingleOrDefault(
						pi =>
							pi.PriceType == PriceType.RetailPrice
								&& pi.Product.RawMaterial.RawMaterialType.AlloyType == alloyType
								&& pi.Product.RawMaterial.RawMaterialType.RollType == rollType
								&& pi.Product.Thickness == product.Thickness
								&& pi.Product.Name == product.Name
					);
					

				if (null != existingPriceItem)
				{
					existingPriceItem.Price = price;
					storage.Update(existingPriceItem);
				}
				else
				{
					var priceItem = new PriceItem
					{
						PriceType = PriceType.RetailPrice,
						ProductId = productId,
						Date = date,
						Price = price,
					};

					storage.Insert(priceItem);
				}

				UpdatePriceCache(storage, productId, date);
			}
		}
예제 #8
0
		public void SetMaterialPrice(int rawMaterialId, int ownerId, DateTime date, decimal? price)
		{
			Argument.Require(rawMaterialId > 0, "Raw material identifier is required.");
			Argument.Require(ownerId > 0, "Owner identifier is required.");
			Argument.Require(date > DateTime.MinValue, "Date is required.");
			Argument.Require(null == price || price.Value >= 0, "Price should be >= 0.");

			using (var storage = new Storage())
			{
				var priceExtra = storage
					.RawMaterials
					.SingleOrDefault(pe => pe.RawMaterialId == rawMaterialId);

				if (null == priceExtra)
					throw new InvalidOperationException($"Raw material with id {rawMaterialId} not found.");

				var existingPriceItem = storage
					.PriceItems
					.SingleOrDefault(pi => pi.RawMaterialId == rawMaterialId && pi.OwnerId == ownerId && pi.Date == date);

				if (null != existingPriceItem)
				{
					existingPriceItem.Price = price;
					storage.Update(existingPriceItem);
				}
				else
				{
					var priceItem = new PriceItem
					{
						PriceType = PriceType.RawMaterial,
						RawMaterialId = rawMaterialId,
						Date = date,
						OwnerId = ownerId,
						Price = price,
					};

					storage.Insert(priceItem);
				}

				var products = storage
					.Products
					.Where(product => product.ManufacturerId == ownerId && product.RawMaterialId == rawMaterialId)
					.Select(p => p.ProductId)
					.ToList();

				foreach (var productId in products)
				{
					UpdatePriceCache(storage, productId, date);
				}
			}
		}