Example #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;
		}
Example #2
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()
			};
		}