private void MoveProduct(int category, int ManId, int quantMove, int storeFrom, int storeTo, int modelId, string rad, int width, float height, int quantFrom) { try { using (u0324292_tyreshopEntities db = new u0324292_tyreshopEntities()) { var diff = quantFrom - quantMove; if (diff >= 0) { var product = GetProduct(category, ManId, modelId, rad, width, height); if (db.productquantities.Any(s => s.StorehouseId == storeTo && s.ProductId == product.ProductId)) { var storehouseTo = db.productquantities.Single(s => s.StorehouseId == storeTo && s.ProductId == product.ProductId); storehouseTo.Quantity += quantMove; db.Entry(storehouseTo).Property(p => p.Quantity).IsModified = true; var storehouseFrom = db.productquantities.Single(s => s.ProductId == product.ProductId && s.StorehouseId == storeFrom); storehouseFrom.Quantity -= quantMove; db.Entry(storehouseFrom).Property(p => p.Quantity).IsModified = true; db.SaveChanges(); MessageBox.Show("Товар успешно перенесен на указанный склад!", "Информация", MessageBoxButton.OK); } else { productquantity pq = new productquantity() { StorehouseId = storeTo, ProductId = product.ProductId, Quantity = quantMove }; db.productquantities.Add(pq); var storehouseFrom = db.productquantities.Single(s => s.ProductId == product.ProductId && s.StorehouseId == storeFrom); storehouseFrom.Quantity -= quantMove; db.Entry(storehouseFrom).Property(p => p.Quantity).IsModified = true; db.SaveChanges(); // GridItemSource(category, product.ProductId); MessageBox.Show("Товар успешно перенесен на указанный склад!", "Информация", MessageBoxButton.OK); } } else { MessageBox.Show("Невозможно перенести указанный товар со склада, введено неверное количество!", "Информация", MessageBoxButton.OK); } } } catch (Exception ex) { log.Error(ex.Message + " \n" + ex.StackTrace); MessageBox.Show("Кажется, что-то пошло не так...", "Информация", MessageBoxButton.OK); } }
private void InProduct(int category, int ManId, int modelId, string rad, int width, float height, int store, int quant) { try { string model = "", manufacturer = ""; using (u0324292_tyreshopEntities db = new u0324292_tyreshopEntities()) { var product = GetProduct(category, ManId, modelId, rad, width, height); model = db.models.Single(s => s.ModelId == modelId).ModelName; manufacturer = db.manufacturers.Single(s => s.ManufacturerId == ManId).ManufacturerName; if (db.productquantities.Any(s => s.StorehouseId == store && s.ProductId == product.ProductId)) { var storehouse = db.productquantities.Single(s => s.StorehouseId == store && s.ProductId == product.ProductId); storehouse.Quantity += quant; db.Entry(storehouse).Property(p => p.Quantity).IsModified = true; if (storehouse.Quantity > 0) { product.ProdStatus = true; } else { product.ProdStatus = false; } if (db.products.Any(a => a.ProductId == product.ProductId)) { var prod = db.products.Single(s => s.ProductId == product.ProductId); prod.ProdStatus = product.ProdStatus; db.Entry(prod).Property(p => p.ProdStatus).IsModified = true; } db.SaveChanges(); //GridItemSource(category, product.ProductId); MessageBox.Show("Товар успешно оприходован!", "Информация", MessageBoxButton.OK); } else { productquantity pq = new productquantity() { StorehouseId = store, ProductId = product.ProductId, Quantity = quant }; db.productquantities.Add(pq); db.SaveChanges(); //GridItemSource(category, product.ProductId); MessageBox.Show("Товар успешно добавлен на указанный склад!", "Информация", MessageBoxButton.OK); } } using (u0324292_mainEntities db2 = new u0324292_mainEntities()) { var product = GetProduct(category, ManId, modelId, rad, width, height); var id = uint.Parse(product.ProdNumber); if (db2.shop_product.Any(a => a.product_id == id)) { var siteProd = db2.shop_product.Single(a => a.product_id == id); siteProd.quantity += quant; db2.Entry(siteProd).Property(p => p.quantity).IsModified = true; if (siteProd.quantity >= 0) { db2.SaveChanges(); } if (siteProd.quantity >= 0 && siteProd.stock_status_id == 8) { siteProd.stock_status_id = 7; db2.Entry(siteProd).Property(p => p.stock_status_id).IsModified = true; db2.SaveChangesAsync(); } } else { int shopStat = 0; if (quant > 0)//больше нуля - продаем { shopStat = 7; } else//в любом другом случае предзаказ { shopStat = 8; } shop_product sp = new shop_product() { product_id = id, //айдишник продукта в сайтовой базе model = model, //наименование модели quantity = quant, //количество в наличии stock_status_id = shopStat, //статус manufacturer_id = db2.shop_manufacturer.Single(s => s.name == manufacturer).manufacturer_id, //получение и присвоение айдишника производителя price = (decimal)product.Price, //цена status = true, //subtract = true, }; List <shop_product_attribute> lst = new List <shop_product_attribute>()//список атрибутов { new shop_product_attribute() { product_id = int.Parse(product.Articul), attribute_id = 12, text = width.ToString() }, //ширина new shop_product_attribute() { product_id = int.Parse(product.Articul), attribute_id = 13, text = height.ToString() }, //высота new shop_product_attribute() { product_id = int.Parse(product.Articul), attribute_id = 14, text = product.Radius }, //радиус new shop_product_attribute() { product_id = int.Parse(product.Articul), attribute_id = 15, text = product.Season }, //сезон, у меня - зима или лето new shop_product_attribute() { product_id = int.Parse(product.Articul), attribute_id = 16, text = product.InCol }, //ИН new shop_product_attribute() { product_id = int.Parse(product.Articul), attribute_id = 17, text = product.IsCol }, //ИС new shop_product_attribute() { product_id = int.Parse(product.Articul), attribute_id = 18, text = product.RFT }, //РанФлэт new shop_product_attribute() { product_id = int.Parse(product.Articul), attribute_id = 19, text = product.Gruz }, //Грузовой new shop_product_attribute() { product_id = int.Parse(product.Articul), attribute_id = 20, text = product.Spikes }, //Шипы }; shop_product_to_category sptc = new shop_product_to_category() { category_id = 1, product_id = id }; shop_product_to_layout sptl = new shop_product_to_layout() { layout_id = 0, store_id = 0, product_id = id }; shop_product_to_store spts = new shop_product_to_store() { product_id = id, store_id = 0 }; string seasonToMetaTitle = ""; switch (product.Season) { case "Лето": seasonToMetaTitle = "Летние"; break; case "Зима": seasonToMetaTitle = "Зимние"; break; } string productName = manufacturer + " " + model + " " + width + "/" + height + product.Radius + " " + product.InCol + product.IsCol + " " + product.RFT + product.Spikes; shop_product_description spd = new shop_product_description() { product_id = id, language_id = 1, name = productName, description = "", tag = width + "," + height + "," + product.Radius, meta_title = seasonToMetaTitle + " шины " + productName + ". Магазин автошин TireShop", meta_description = seasonToMetaTitle + " шины " + productName + " по " + product.Price * 4 + " руб/шт. Доставка по СПб и в другие регионы", meta_keyword = "" }; shop_url_alias sua = new shop_url_alias() { query = "product_id=" + id, keyword = manufacturer + "_" + id }; db2.shop_product.Add(sp); //добавили сам продукт foreach (var item in lst) { //добавляем все атрибуты шин db2.shop_product_attribute.Add(item); } db2.shop_product_to_category.Add(sptc); db2.shop_product_to_layout.Add(sptl); db2.shop_product_to_store.Add(spts); db2.shop_product_description.Add(spd); db2.shop_url_alias.Add(sua); db2.SaveChanges(); } } } catch (Exception ex) { log.Error(ex.Message + " \n" + ex.StackTrace); MessageBox.Show("Кажется, что-то пошло не так...", "Информация", MessageBoxButton.OK); } }