예제 #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 Seed(Storage storage, int plantCode)
		{
			storage.Insert(new Manufacturer { Name = "ЧерМК", IsPrimary = true });
			storage.Insert(new Manufacturer { Name = "НЛМК", IsPrimary = true });
			storage.Insert(new Manufacturer { Name = "ММК", IsPrimary = true });
			//storage.Insert(new Manufacturer { Name = "(1) КТЗ", IsPrimary = false });
			//storage.Insert(new Manufacturer { Name = "(1) РязТЗ", IsPrimary = false });
			//storage.Insert(new Manufacturer { Name = "(1) БТЗ", IsPrimary = false });
			//storage.Insert(new Manufacturer { Name = "(1) ОМК", IsPrimary = false });
			//storage.Insert(new Manufacturer { Name = "(1) СЗТЗ", IsPrimary = false });
			//storage.Insert(new Manufacturer { Name = "(1) ТПК Союз", IsPrimary = false });
			//storage.Insert(new Manufacturer { Name = "(1) Машпрофиль", IsPrimary = false });
			//storage.Insert(new Manufacturer { Name = "(1) Профиль А", IsPrimary = false });
			//storage.Insert(new Manufacturer { Name = "(1) БИС", IsPrimary = false });

			storage.Insert(new Manufacturer { Name = "(2) ВестМет", IsPrimary = false });
			//storage.Insert(new Manufacturer { Name = "(2) Промизделия", IsPrimary = false });
			storage.Insert(new Manufacturer { Name = "(2) ТЭМПО", IsPrimary = false });
			//storage.Insert(new Manufacturer { Name = "(2) ОМК", IsPrimary = false });
			//storage.Insert(new Manufacturer { Name = "(2) ТМК", IsPrimary = false });
			//storage.Insert(new Manufacturer { Name = "(2) Уралтрубпром", IsPrimary = false });
			//storage.Insert(new Manufacturer { Name = "(2) Исаевский ТЗ", IsPrimary = false });

			storage.Insert(new PriceExtraCategory { Name = "Себестоимость" });
			storage.Insert(new PriceExtraCategory { Name = "Доставка материала" });
			storage.Insert(new PriceExtraCategory { Name = "Передел" });
			storage.Insert(new PriceExtraCategory { Name = "Доставка до Москвы" });

			var allManufacturers = storage
				.Manufacturers
				.ToList();

			var primaryManufacturers = allManufacturers.Where(m => m.IsPrimary);
			var nonPrimaryManufacturers = allManufacturers.Where(m => !m.IsPrimary);

			var materialThickness = EnumerateThickness(plantCode);

			foreach (var thickness in materialThickness)
			{
				foreach (var rollType in new [] {RollType.Cold, RollType.Hot })
				{
					foreach (var alloyType in new[] {AlloyType.LowAlloy, AlloyType.Regular })
					{
						var materialType = new RawMaterialType
						{
							AlloyType = alloyType,
							RollType = rollType,
							Thickness = thickness,
						};

						SetMaterialTypeName(materialType);

						var materialTypeId = storage.InsertWithIdentity(materialType);

						foreach (var primaryManufacturer in primaryManufacturers)
						{
							var rawMaterialId = storage.InsertWithIdentity(
								new RawMaterial
								{
									ManufacturerId = primaryManufacturer.ManufacturerId,
									RawMaterialTypeId = Convert.ToInt32(materialTypeId),
								});

							foreach (var productName in EnumerateProductNames(plantCode, rollType, alloyType))
							{
								storage.Insert(
									new Product
									{
										ManufacturerId = primaryManufacturer.ManufacturerId,
										RawMaterialId = Convert.ToInt32(rawMaterialId),
										Name = productName,
										Thickness = thickness,
									});


								foreach (var nonPrimaryManufacturer in nonPrimaryManufacturers)
								{
									storage.Insert(
										new Product
										{
											ManufacturerId = nonPrimaryManufacturer.ManufacturerId,
											RawMaterialId = Convert.ToInt32(rawMaterialId),
											Name = productName,
											Thickness = thickness,
										});
								}
								
							}
						}
					}
				}
			}
		}
예제 #6
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);
				}
			}
		}
예제 #7
0
		public void ServiceShouldCreateProducts()
		{
			using (var storage = new Storage())
			{
				// RawMaterialTypeId = 1
				storage.Insert(
					new RawMaterialType
					{
						RawMaterialTypeId = 1,
						AlloyType = AlloyType.Regular,
						RollType = RollType.Undefined,
						Name = "Штрипс",
						Thickness = 10,
					});

				

				// PriceExtraCategoryId = 1
				storage.Insert(
					new PriceExtraCategory
					{
						Name = "Доставка"
					});

				// PriceExtraCategoryId = 2
				storage.Insert(
					new PriceExtraCategory
					{
						Name = "Передел"
					});

				// ManufacturerId = 1
				storage.Insert(
					new Manufacturer
					{
						Name = "Главный Производитель",
						IsPrimary = true,
					});

				// ManufacturerId = 1
				storage.Insert(
					new Manufacturer
					{
						Name = "Субпроизводитель",
						IsPrimary = false,
					});

				// RawMaterialId = 1,
				storage.Insert(
					new RawMaterial
					{
						ManufacturerId = 1,
						RawMaterialTypeId = 1,
					});
			}

			var service = new ProductService();
			var product = service.CreateProduct(2, 1, "10x10", 2);

			var extra1 = service.AddPriceExtra(product.ProductId, 1);
			var extra2 = service.AddPriceExtra(product.ProductId, 2);

			service.SetMaterialPrice(1, 2, new DateTime(2015, 1, 1), 100);
			service.SetExtraPrice(extra1.PriceExtraId, 2, new DateTime(2015, 1, 1), 200);
			service.SetExtraPrice(extra2.PriceExtraId, 2, new DateTime(2015, 1, 2), 300);

			var price1 = service.GetProductWithPrice(product.ProductId).Price;

			service.SetMaterialPrice(1, 2, new DateTime(2015, 4, 1), 150);
			service.SetExtraPrice(extra1.PriceExtraId, 2, new DateTime(2015, 3, 1), 400);

			var price2 = service.GetProductWithPrice(product.ProductId).Price;

			Assert.AreEqual(600, price1);
			Assert.AreEqual(850, price2);

		}
예제 #8
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);
			}
		}
예제 #9
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);
			}
		}
예제 #10
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);
				}
			}
		}