Exemplo n.º 1
0
		public PriceExtra AddPriceExtra(int productId, int priceExtraCategoryId)
		{
			Argument.Require(productId > 0, "Product identifier is required.");
			Argument.Require(priceExtraCategoryId > 0, "Price extra category id is required.");

			var priceExtra = new PriceExtra
			{
				PriceExtraCategoryId = priceExtraCategoryId,
				ProductId = productId,
			};

			using (var storage = new Storage())
			{
				var existingExtra = storage
					.PriceExtras
					.SingleOrDefault(pe => pe.ProductId == productId && pe.PriceExtraCategoryId == priceExtraCategoryId);

				if (null == existingExtra)
				{
					priceExtra.PriceExtraId = Convert.ToInt32(storage.InsertWithIdentity(priceExtra));
				}
				else
				{
					priceExtra.PriceExtraId = existingExtra.PriceExtraId;
				}
			}

			return priceExtra;
		}
		public IEnumerable<PriceExtraCategory> Get()
		{
			using (var storage = new Storage())
			{
				return storage.PriceExtraCategories.ToList();
			}
		}
		public IEnumerable<RawMaterialType> Get()
		{
			using (var storage = new Storage())
			{
				return storage.RawMaterialTypes.ToList();
			}
		}
Exemplo n.º 4
0
		public Manufacturer Get(int id)
		{
			using (var storage = new Storage())
			{
				return storage
					.Manufacturers
					.SingleOrDefault(m => m.ManufacturerId == id);
			}
		}
		public void Delete(int id)
		{
			using (var storage = new Storage())
			{
				storage
					.PriceExtraCategories
					.Delete(pc => pc.PriceExtraCategoryId == id);
			}
		}
Exemplo n.º 6
0
		public IEnumerable<Manufacturer> Get()
		{
			using (var storage = new Storage())
			{
				return storage
					.Manufacturers
					.ToList();
			}
		}
		public PriceExtraCategory Get(int id)
		{
			using (var storage = new Storage())
			{
				return storage
					.PriceExtraCategories
					.SingleOrDefault(pc => pc.PriceExtraCategoryId == id);
			}
		}
Exemplo n.º 8
0
		public PriceExtra Get(int id)
		{
			using (var storage = new Storage())
			{
				return storage
					.PriceExtras
					.SingleOrDefault(pe => pe.PriceExtraId == id);
			}
		}
		public RawMaterialType Get(int id)
		{
			using (var storage = new Storage())
			{
				return storage
					.RawMaterialTypes
					.SingleOrDefault(pc => pc.RawMaterialTypeId == id);
			}
		}
Exemplo n.º 10
0
		public void Delete(int id)
		{
			using (var storage = new Storage())
			{
				storage
					.RawMaterialTypes
					.Delete(pc => pc.RawMaterialTypeId == id);
			}
		}
Exemplo n.º 11
0
		//[Route("~/api/raw-material")]
		//[HttpOptions]
		//[HttpDelete]
		public IHttpActionResult Delete(int id)
		{
			using (var storage = new Storage())
			{
				storage
					.RawMaterials
					.Delete(rm => rm.RawMaterialId == id);
			}

			return Ok();
		}
Exemplo n.º 12
0
		public void ClearStorage()
		{
			using (var storage = new Storage())
			{
				storage.DropTable<Manufacturer>();
				storage.DropTable<RawMaterialType>();
				storage.DropTable<RawMaterial>();
				storage.DropTable<PriceExtraCategory>();
				storage.DropTable<PriceExtra>();
				storage.DropTable<PriceItem>();
				storage.DropTable<Product>();
				storage.DropTable<PriceCache>();
			}
		}
Exemplo n.º 13
0
		public IEnumerable<RawMaterialModel> Get()
		{
			using (var storage = new Storage())
			{
				return storage
					.RawMaterials
					.LoadWith(rm => rm.RawMaterialType)
					.ToList()
					.Select(rm => new RawMaterialModel
					{
						ManufacturerId = rm.ManufacturerId,
						Name = rm.RawMaterialType.Name,
						RawMaterialId = rm.RawMaterialId,
						RawMaterialTypeId = rm.RawMaterialTypeId,
					});
			}
		}
Exemplo n.º 14
0
		public void RemovePriceExtra(int productId, int priceExtraId, DateTime? date = null)
		{
			Argument.Require(productId > 0, "Product id is required.");
			Argument.Require(priceExtraId > 0, "Price extra id is required.");

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

				if(null == existingPriceExtra)
					return;

				storage.Delete(existingPriceExtra);
				
				UpdatePriceCache(storage, existingPriceExtra.ProductId, date ?? DateTime.Today);
			}
		}
Exemplo n.º 15
0
		public RawMaterialModel Get(int id)
		{
			using (var storage = new Storage())
			{
				return storage
					.RawMaterials
					.LoadWith(rm => rm.RawMaterialType)
					.Where(m => m.RawMaterialId == id)
					.ToList()
					.Select(rm => new RawMaterialModel
					{
						ManufacturerId = rm.ManufacturerId,
						Name = rm.RawMaterialType.Name,
						RawMaterialId = rm.RawMaterialId,
						RawMaterialTypeId = rm.RawMaterialTypeId,
					})
					.SingleOrDefault();
			}
		}
Exemplo n.º 16
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);
				}
			}
		}
Exemplo n.º 17
0
		public ProductModel Get(int id)
		{
			using (var storage = new Storage())
			{
				return storage
					.Products
					.LoadWith(p => p.Manufacturer)
					.LoadWith(p => p.RawMaterial.RawMaterialType)
					.Where(p => p.ProductId == id)
					.Select(p => new ProductModel
					{
						ProductId = p.ProductId,
						ManufacturerId = p.ManufacturerId,
						ManufacturerName = p.Manufacturer.Name,
						RawMaterialTypeId = p.RawMaterial.RawMaterialTypeId,
						RawMaterialTypeName = p.RawMaterial.RawMaterialType.Name,
						Thickness = p.Thickness,
					})
					.SingleOrDefault();
			}
		}
Exemplo n.º 18
0
		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;
			}
		}
Exemplo n.º 19
0
		public Product CreateProduct(int manufacturerId, int rawMaterialId, string size, decimal thickness)
		{
			Argument.Require(manufacturerId > 0, "Manufacturer id is required.");
			Argument.Require(rawMaterialId > 0, "Raw material id is required.");
			Argument.NotNullOrEmpty(size, "Product size is required.");
			Argument.Require(thickness > 0, "Product thickness is required.");

			var product = new Product
			{
				ManufacturerId = manufacturerId,
				RawMaterialId = rawMaterialId,
				Name = size,
				Thickness = thickness,
			};

			using (var storage = new Storage())
			{
				product.ProductId = Convert.ToInt32(storage.InsertWithIdentity(product));
			}

			return product;
		}
Exemplo n.º 20
0
		public void StorageShouldCreateManufacturers()
		{
			using (var storage = new Storage())
			{
				var result = storage.InsertWithIdentity(new Manufacturer
				{
					IsPrimary = true,
					Name = "Severstahl",
				});

				var result2 = storage.InsertWithIdentity(new Manufacturer
				{
					IsPrimary = true,
					Name = "Severstahl2",
				});

				Debug.WriteLine(result);
				Debug.WriteLine(result2);

				Assert.AreEqual(2, storage.Manufacturers.ToList().Count);
			}
		}
Exemplo n.º 21
0
		public void SetupStorage()
		{
			try
			{
				ClearStorage();
			}
			catch
			{
			}

			using (var storage = new Storage())
			{
				storage.CreateTable<Manufacturer>();
				storage.CreateTable<RawMaterialType>();
				storage.CreateTable<RawMaterial>();
				storage.CreateTable<PriceExtraCategory>();
				storage.CreateTable<PriceExtra>();
				storage.CreateTable<PriceItem>();
				storage.CreateTable<Product>();
				storage.CreateTable<PriceCache>();
			}
		}
		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;
			}
		}
Exemplo n.º 23
0
		public IEnumerable<PriceExtraModel> GetPriceExtras(int id)
		{
			var date = DateTime.Today;

			using (var storage = new Storage())
			{
				var product = storage
					.Products
					.LoadWith(p => p.PriceExtras)
					.LoadWith(p => p.PriceExtras[0].PriceExtraCategory)
					.SingleOrDefault(p => p.ProductId == id);

				if (null == product)
					yield break;

				foreach (var priceExtra in product.PriceExtras)
				{
					var lastPrice = storage
						.PriceItems
						.Where(pi => pi.PriceExtraId == priceExtra.PriceExtraId && pi.Date <= date)
						.OrderByDescending(pi => pi.Date)
						.FirstOrDefault();

					yield return new PriceExtraModel
					{
						ProductId = id,
						PriceExtraId = priceExtra.PriceExtraId,
						PriceCategoryId = priceExtra.PriceExtraCategoryId,
						PriceCategoryName = priceExtra.PriceExtraCategory.Name,
						LastDate = lastPrice?.Date ?? date,
						LastPrice = lastPrice?.Price
					};
				}

			}
		}
Exemplo n.º 24
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);
				}

			}
		}
Exemplo n.º 25
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,
										});
								}
								
							}
						}
					}
				}
			}
		}
Exemplo n.º 26
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);
				}
			}
		}
Exemplo n.º 27
0
		public BulkMaterialParseResult ParseMaterialPricesBulk(
			int manufacturerId,
			int supplierId,
			DateTime date,
			AlloyType alloyType,
			RollType rollType,
			bool remove,
			string raw)
		{
			var materials = new List<RawMaterial>();
			var errors = new List<string>();

			try
			{
				using (var storage = new Storage())
				{
					if (remove)
					{
						var allThickness = storage.RawMaterials
							.Where(
								rm => rm.ManufacturerId == supplierId
									&& (alloyType == AlloyType.Undefined || rm.RawMaterialType.AlloyType == alloyType)
									&& (rollType == RollType.Undefined || rm.RawMaterialType.RollType == rollType)
							)
							.Select(rm => rm.RawMaterialType.Thickness.ToString(CultureInfo.InvariantCulture))
							.ToList();

						raw = string.Join("\t", allThickness)
							+ "\n"
							+ string.Join("\t", allThickness.Select(p => ""));
					}

					var lines = raw.Split('\n');
					var headers = lines[0].Split('\t');
					var prices = lines[1].Split('\t');

					for (int i = 0; i < headers.Length; i++)
					{
						var thickness = Convert.ToDecimal(headers[i].FixDecimalSeparator());
						if (!string.IsNullOrEmpty(prices[i]))
						{
							var rawMaterial = storage
								.RawMaterials
								.Where(
									rm => rm.ManufacturerId == supplierId
										&& rm.RawMaterialType.AlloyType == alloyType
										&& rm.RawMaterialType.RollType == rollType
										&& rm.RawMaterialType.Thickness == thickness)
								.ToList();

							if (!rawMaterial.Any())
							{
								errors.Add($"Выбранный поставщик не производит материала с такими параметрами и толщиной {thickness}");
								continue;
							}
							if (rawMaterial.Count > 1)
							{
								errors.Add(
									$"Выбранный поставщик производит более 1 материала с такими параметрами и толщиной {thickness}, невозможно определить нужный.");
								continue;
							}

							var priceItem = new PriceItem
							{
								RawMaterialId = rawMaterial[0].RawMaterialId,
								OwnerId = manufacturerId,
								Date = date,
								Price = remove 
									? null 
									: (decimal?)Convert.ToDecimal(prices[i].FixDecimalSeparator())
							};

							rawMaterial[0].PriceItems = new[]
							{
								priceItem
							};

							materials.Add(rawMaterial[0]);
						}
					}
				}
			}
			catch (Exception ex)
			{
				_logger.Error(ex);
				errors.Add("Ошибка синтаксического разбора строки.");
			}

			return new BulkMaterialParseResult
			{
				Errors = errors.ToArray(),
				Materials = errors.Any() 
					? new RawMaterial[0]
					: materials.ToArray()
			};
		}
Exemplo n.º 28
0
		public BulkProductParseResult ParseExtraPricesBulk(
			int manufacturerId,
			int supplierId,
			DateTime date,
			AlloyType alloyType,
			RollType rollType,
			int priceExtraCategoryId,
			bool remove,
			string raw)
		{
			var products = new List<Product>();
			var errors = new List<string>();

			if (remove)
			{
				raw = "0";
			}

			try
			{
				var lines = raw.Split(new [] {'\n'}, StringSplitOptions.RemoveEmptyEntries);
				var headers = lines[0].Split('\t');

				
				using (var storage = new Storage())
				{
					if (lines.Length == 1 && headers.Length == 1)
					{
						var price = Convert.ToDecimal(raw.Trim().FixDecimalSeparator());

						return new BulkProductParseResult
						{
							Errors = new string[0],
							Products = storage
								.Products
								.Where(
									p =>
										p.ManufacturerId == manufacturerId
											&& p.RawMaterial.ManufacturerId == supplierId
											&& (alloyType == AlloyType.Undefined || p.RawMaterial.RawMaterialType.AlloyType == alloyType)
											&& (rollType == RollType.Undefined || p.RawMaterial.RawMaterialType.RollType == rollType)
								)
								.ToList()
								.Select(
									p =>
									{
										p.PriceExtras = new[]
										{
											new PriceExtra
											{
												PriceExtraCategoryId = priceExtraCategoryId,
												PriceItems = new[]
												{
													new PriceItem
													{
														Date = date,
														OwnerId = manufacturerId,
														Price = price,
													}
												}
											}
										};
										return p;
									})
								.ToArray()
						};
					}


					for (int lineId = 1; lineId < lines.Length; lineId++)
					{
						var prices = lines[lineId].Split('\t');
						var productName = prices[0].Trim();

						for (int colId = 1; colId < headers.Length; colId++)
						{
							var thickness = Convert.ToDecimal(headers[colId].FixDecimalSeparator());

							var query = storage
									.RawMaterials
									.LoadWith(rm => rm.RawMaterialType)
									.Where(
										rm => rm.ManufacturerId == supplierId
											&& rm.RawMaterialType.Thickness == thickness);

							if (alloyType != AlloyType.Undefined)
							{
								query = query.Where(rm => rm.RawMaterialType.AlloyType == alloyType);
							}

							if (rollType != RollType.Undefined)
							{
								query = query.Where(rm => rm.RawMaterialType.RollType == rollType);
							}

							var rawMaterials = query.ToList();

							if (!rawMaterials.Any())
							{
								errors.Add($"Выбранный поставщик не производит материала с такими параметрами и толщиной {thickness}");
								continue;
							}

							foreach (var rawMaterial in rawMaterials)
							{
								var product = storage
									.Products
									.SingleOrDefault(
										p => p.ManufacturerId == manufacturerId
											&& p.RawMaterialId == rawMaterial.RawMaterialId
											&& p.Name == productName);

								if (null == product)
								{
									product = new Product
									{
										ManufacturerId = manufacturerId,
										RawMaterialId = rawMaterial.RawMaterialId,
										Thickness = rawMaterial.RawMaterialType.Thickness,
										Name = productName,
									};
								}

								var priceExtra = new PriceExtra
								{
									PriceExtraCategoryId = priceExtraCategoryId,
									ProductId = product.ProductId,
								};

								var price = string.IsNullOrEmpty(prices[colId])
									? null
									: (decimal?)Convert.ToDecimal(prices[colId].FixDecimalSeparator());

								var priceItem = new PriceItem
								{
									OwnerId = manufacturerId,
									Date = date,
									Price = remove ? null : price,
								};

								priceExtra.PriceItems = new[]
								{
									priceItem
								};

								product.PriceExtras = new[]
								{
									priceExtra
								};

								products.Add(product);


								if (!string.IsNullOrEmpty(prices[colId]))
							{
								
								}

							}
						}
					}
					
				}
			}
			catch (Exception ex)
			{
				_logger.Error(ex);
				errors.Add("Ошибка синтаксического разбора строки.");
			}

			return new BulkProductParseResult
			{
				Errors = errors.ToArray(),
				Products = errors.Any()
					? new Product[0]
					: products.ToArray()
			};
		}
Exemplo n.º 29
0
		public IEnumerable<ProductModel> Get()
		{
			using (var storage = new Storage())
			{
				return storage
					.Products
					.LoadWith(p => p.Manufacturer)
					.LoadWith(p => p.RawMaterial.RawMaterialType)
					.Select(p => new ProductModel
					{
						ProductId = p.ProductId,
						ManufacturerId = p.ManufacturerId,
						ManufacturerName = p.Manufacturer.Name,
						RawMaterialTypeId = p.RawMaterial.RawMaterialTypeId,
						RawMaterialTypeName = p.RawMaterial.RawMaterialType.Name,
						Thickness = p.Thickness,
					})
					.ToList();
			}
		}
Exemplo n.º 30
0
		public IHttpActionResult Delete(int id)
		{
			Argument.Require(id > 0, "Product id is required.");

			using (var storage = new Storage())
			{
				storage
					.Products
					.Delete(p => p.ProductId == id);
			}

			return Ok();
		}