public JsonResult CreateProduct(ProductSampleModel model) { var Messaging = new RenderMessaging(); try { if (User == null || User.UserId < 0) { Messaging.isError = true; Messaging.messaging = "Vui lòng đăng nhập lại!"; return(Json(Messaging, JsonRequestBehavior.AllowGet)); } model.masp = model.masp.Replace(" ", ""); var msError = Validate_Product(model); if (msError != null) { return(Json(msError, JsonRequestBehavior.AllowGet)); } var data = new shop_sanpham { tensp = model.tensp, masp = model.masp, Barcode = model.Barcode, PriceCompare = 0, PriceBase = 0, PriceBase_Old = 0, PriceInput = 0, hide = false, Status = model.Status, Note = model.Note, DateCreate = DateTime.Now, FromCreate = (int)TypeFromCreate.Soft }; _crud.Add <shop_sanpham>(data); _crud.SaveChanges(); Messaging.isError = false; Messaging.messaging = "Thêm sản phẩm thành công."; } catch (Exception ex) { Messaging.isError = true; Messaging.messaging = "Tạo sản phẩm không thành công!"; } return(Json(Messaging, JsonRequestBehavior.AllowGet)); }
public void UpdatePriceWholesale(shop_sanpham product, UserCurrent User, bool isCommit = false) { #region Giá sỉ var channelOnline = unitOfWork.ChannelRepository.FindBy(o => o.Type == (int)TypeChannel.IsChannelOnline && o.Id == User.ChannelId).FirstOrDefault(); bool isChange = false; if (channelOnline != null) { var priceOL = product.soft_Channel_Product_Price.FirstOrDefault(o => o.soft_Channel.Type == (int)TypeChannel.IsChannelOnline); int?priceWholesale = 0; if (priceOL != null) { var total = (int)((priceOL.Price - product.PriceBase) / 5.3) + product.PriceBase; double round = Convert.ToDouble(total.Value); priceWholesale = Convert.ToInt32(Helpers.Round_Double(round, -3)); } else { var total = (int)((0 - product.PriceBase) / 5.3) + product.PriceBase; double round = Convert.ToDouble(total.Value); priceWholesale = Convert.ToInt32(Helpers.Round_Double(round, -3)); } var priceSi = unitOfWork.ChanelPriceRepository.Get(o => o.ProductId == product.id && o.soft_Channel.Type == (int)TypeChannel.IsChannelWholesale).FirstOrDefault(); if (priceSi != null) { priceSi.Price = (int)priceWholesale; unitOfWork.ChanelPriceRepository.Update(priceSi, o => o.Price); isChange = true; } else { var ChannelSi = unitOfWork.ChannelRepository.Get(o => o.Type == (int)TypeChannel.IsChannelWholesale).FirstOrDefault(); unitOfWork.ChanelPriceRepository.Add(new soft_Channel_Product_Price { Price = (int)priceWholesale, ChannelId = ChannelSi.Id, DateCreate = DateTime.Now, EmployeeCreate = User.UserId, ProductId = product.id }); isChange = true; } } if (isCommit && isChange) { unitOfWork.SaveChanges(); } #endregion }
public JsonResult UpdateProduct(ProductSampleModel model) { var Messaging = new RenderMessaging(); Messaging.messaging = "Cập nhật thành công."; try { if (User == null || User.UserId < 0) { Messaging.isError = true; Messaging.messaging = "Vui lòng đăng nhập lại!"; return(Json(Messaging, JsonRequestBehavior.AllowGet)); } var productObj = _context.shop_sanpham.Find(model.id); if (productObj != null) { model.masp = model.masp.Replace(" ", ""); var msError = Validate_Product(model); if (msError != null) { return(Json(msError, JsonRequestBehavior.AllowGet)); } var data = new shop_sanpham { id = model.id, tensp = model.tensp, masp = model.masp, Barcode = model.Barcode, PriceBase_Old = model.PriceBase_Old, PriceBase = model.PriceBase, PriceCompare = model.PriceCompare, CatalogId = model.CatalogId <= 0 ? (int?)null : model.CatalogId, SuppliersId = model.SuppliersId <= 0 ? (int?)null : model.SuppliersId, Status = model.Status, Note = model.Note, StatusVAT = model.StatusVAT }; _crud.Update <shop_sanpham>(data, o => o.SuppliersId, o => o.StatusVAT, o => o.tensp, o => o.PriceBase, o => o.PriceBase_Old, o => o.CatalogId, o => o.PriceCompare, o => o.Barcode, o => o.masp, o => o.Status, o => o.Note); _crud.SaveChanges(); if (productObj.PriceBase != model.PriceBase) { var user = Mapper.Map <UserCurrent>(User); productObj = _context.shop_sanpham.Find(model.id); _IOrderBus.UpdatePriceWholesale(productObj, user, true); } Messaging.isError = false; Messaging.messaging = "Cập nhật sản phẩm thành công!"; return(Json(Messaging, JsonRequestBehavior.AllowGet)); } else { Messaging.isError = true; Messaging.messaging = "Sản phẩm không tồn tại!"; return(Json(Messaging, JsonRequestBehavior.AllowGet)); } } catch (Exception ex) { Messaging.isError = true; Messaging.messaging = "Cập nhật sản phẩm không thành công!"; } return(Json(Messaging, JsonRequestBehavior.AllowGet)); }
public async Task <List <shop_sanpham> > ImportPriceDisscount(DataSet data, int UserId) { var masp = string.Empty; var line = 1; var lstError = new List <shop_sanpham>(); await Update_is_import(true, null, 0, true); int percent = 0; try { var supplie_lst = unitOfWork.SuppliersRepository.GetAll().ToList(); var catalog_lst = unitOfWork.CatalogRepository.GetAll().ToList(); var channel_lst = unitOfWork.ChannelRepository.GetAll().ToList(); var braches_lst = unitOfWork.BrachesRepository.GetAll().ToList(); foreach (DataRow row in data.Tables[0].Rows) { line++; bool isError = false; percent = (int)Math.Round(100.0 * line / data.Tables[0].Rows.Count); var product = new shop_sanpham(); try { foreach (DataColumn col in data.Tables[0].Columns) { var columnName = col.ColumnName; var value = row[col.ColumnName].ToString(); if (columnName.Equals("Code")) { masp = value.Trim(); if (string.IsNullOrEmpty(masp)) { isError = true; break; } product = unitOfWork.ProductRepository.FindBy(o => o.masp.Equals(masp)).FirstOrDefault(); if (product == null) { continue; } } if (columnName.StartsWith("Giá KM")) { var channelOL = unitOfWork.ChannelRepository.FindBy(o => o.Code == "OL").FirstOrDefault();// channel_lst.FirstOrDefault(o => o.Code.Equals(codeChannel)); if (channelOL != null) { try { if (product.id > 0) { var channelPrice = unitOfWork.ChanelPriceRepository.FindBy(o => o.ChannelId == channelOL.Id && o.ProductId == product.id).FirstOrDefault(); if (channelPrice != null) { channelPrice.Price_Discount = int.Parse(value); unitOfWork.ChanelPriceRepository.Update(channelPrice, o => o.Price_Discount); } else { } } else { } } catch (Exception ex) { } } } } } catch (Exception ex) { lstError.Add(new shop_sanpham { tensp = product.tensp, masp = product.masp, id = line }); isError = true; } if (isError) { continue; } try { await unitOfWork.SaveChanges(); } catch (Exception ex) { lstError.Add(new shop_sanpham { tensp = product.tensp, masp = product.masp, id = line }); } } var error = string.Empty; if (lstError != null && lstError.Count > 0) { error = new JavaScriptSerializer().Serialize(lstError.Select(o => new { masp = o.masp, tensp = o.tensp, id = o.id })); } await Update_is_import(false, error, percent, true); return(lstError); } catch (Exception ex) { lstError.Add(new shop_sanpham { tensp = ex.Message, masp = masp, id = line }); var error = new JavaScriptSerializer().Serialize(lstError.Select(o => new { masp = o.masp, tensp = o.tensp, id = o.id })); await Update_is_import(false, error, -1, true); return(null); } }
public async Task <List <shop_sanpham> > ImportData(DataSet data, int UserId) { var masp = string.Empty; var line = 1; var perfixBranches = "Kho_"; var perfixChannel = "Kenh_"; var perfixPriceChannel = "KM_Kenh_"; var perfixPriceChannel_StartDate = "KM_NBD_Kenh_"; var perfixPriceChannel_EndDate = "KM_NKT_Kenh_"; var lstError = new List <shop_sanpham>(); await Update_is_import(true, null, 0, true); int percent = 0; try { var supplie_lst = unitOfWork.SuppliersRepository.GetAll().ToList(); var catalog_lst = unitOfWork.CatalogRepository.GetAll().ToList(); var channel_lst = unitOfWork.ChannelRepository.GetAll().ToList(); var braches_lst = unitOfWork.BrachesRepository.GetAll().ToList(); foreach (DataRow row in data.Tables[0].Rows) { line++; bool isError = false; percent = (int)Math.Round(100.0 * line / data.Tables[0].Rows.Count); var product = new shop_sanpham(); var imgage = new shop_image(); bool isUpdatePriceChanle = false; bool isUpdatePriceChanleDisscount = false; bool isUpdateProduct = false; bool isUpdateStock = false; try { foreach (DataColumn col in data.Tables[0].Columns) { var columnName = col.ColumnName; var value = row[col.ColumnName].ToString(); if (columnName.Equals("Code")) { masp = value.Trim(); if (string.IsNullOrEmpty(masp)) { isError = true; break; } product = unitOfWork.ProductRepository.FindBy(o => o.masp.Equals(masp)).FirstOrDefault(); if (product == null || product.id <= 0) { product = new shop_sanpham(); product.masp = value.Trim(); } } if (string.IsNullOrEmpty(value)) { continue; } #region San phẩm switch (columnName) { case "ProductName": product.tensp = value; isUpdateProduct = true; break; case "Img": imgage.url = value; imgage.RefId = product.id; isUpdateProduct = true; break; case "PriceBase": var tmp_PriceBase = float.Parse(value); product.PriceBase = (int)tmp_PriceBase; isUpdateProduct = true; break; case "PriceCompare": var tmp_PriceCompare = float.Parse(value); product.PriceCompare = (int)tmp_PriceCompare; isUpdateProduct = true; break; case "PriceBase_Old": var tmp_PriceBase_Old = float.Parse(value); product.PriceBase_Old = (int)tmp_PriceBase_Old; isUpdateProduct = true; break; case "PriceInput": var tmp_PriceInput = float.Parse(value); product.PriceInput = (int)tmp_PriceInput; isUpdateProduct = true; break; case "PriceWholesale": var tmp_PriceWholesale = float.Parse(value); product.PriceWholesale = (int)tmp_PriceWholesale; isUpdateProduct = true; break; case "Status": var statusE = EnumHelper <StatusProduct> .Parse(value); product.Status = (int)statusE; isUpdateProduct = true; break; case "VAT": var vatE = EnumHelper <StatusVATProduct> .Parse(value); product.Status = (int)vatE; isUpdateProduct = true; break; case "Suppliers": var nameSupp = value.ToLower().Trim(); var supplie = supplie_lst.FirstOrDefault(o => o.Name.ToLower().Equals(nameSupp)); if (supplie != null) { product.SuppliersId = supplie.SuppliersId; } else { supplie = new soft_Suppliers { Name = nameSupp, DateCreate = DateTime.Now, EmployeeCreate = UserId }; product.SuppliersId = supplie.SuppliersId; unitOfWork.SuppliersRepository.Add(supplie); supplie_lst.Add(supplie); } isUpdateProduct = true; break; case "Catalog": var nameCatalog = value.ToLower().Trim(); var catalog = catalog_lst.FirstOrDefault(o => o.Name.ToLower().Equals(nameCatalog)); if (catalog != null) { product.CatalogId = catalog.Id; } else { catalog = new soft_Catalog { Name = nameCatalog, DateCreate = DateTime.Now, EmployeeCreate = UserId }; product.CatalogId = catalog.Id; unitOfWork.CatalogRepository.Add(catalog); catalog_lst.Add(catalog); } isUpdateProduct = true; break; } #endregion #region Gia kenh if (columnName.StartsWith(perfixChannel)) { var codeChannel = columnName.Substring(perfixChannel.Length); var channel = channel_lst.FirstOrDefault(o => o.Code.Equals(codeChannel)); if (channel != null) { if (product.id > 0) { var channelPrice = unitOfWork.ChanelPriceRepository.FindBy(o => o.ChannelId == channel.Id && o.ProductId == product.id).FirstOrDefault(); if (channelPrice == null) { var newobj = new soft_Channel_Product_Price { ProductId = product.id, ChannelId = channel.Id, Price = int.Parse(value), DateCreate = DateTime.Now, EmployeeCreate = UserId }; isUpdatePriceChanle = true; unitOfWork.ChanelPriceRepository.Add(newobj); } else { var ab = double.Parse(value); channelPrice.Price = (int)ab; isUpdatePriceChanle = true; unitOfWork.ChanelPriceRepository.Update(channelPrice, o => o.Price); } } } } #endregion #region Giá khuyến mãi if (columnName.StartsWith(perfixPriceChannel) && !isUpdatePriceChanle) { var codeChannel = columnName.Substring(perfixPriceChannel.Length); var channel = unitOfWork.ChannelRepository.FindBy(o => o.Code == codeChannel).FirstOrDefault();// channel_lst.FirstOrDefault(o => o.Code.Equals(codeChannel)); if (channel != null) { var priceDisscount_Value = 0; DateTime?priceDisscount_StarDate = null; DateTime?priceDisscount_EndDate = null; try { DataRow dr = data.Tables[0].Select("Code='" + masp + "'").FirstOrDefault(); if (dr != null) { priceDisscount_Value = int.Parse(dr[perfixPriceChannel + channel.Code].ToString()); string startDate = perfixPriceChannel_StartDate + channel.Code; string endDate = perfixPriceChannel_EndDate + channel.Code; var StarDate = Convert.ToDateTime(dr[startDate].ToString()); var EndDate = Convert.ToDateTime(dr[endDate].ToString()); priceDisscount_StarDate = StarDate.AddHours(23).AddMinutes(59).AddSeconds(59).AddDays(-1); priceDisscount_EndDate = EndDate.AddHours(23).AddMinutes(59).AddSeconds(59); } if (product.id > 0) { var channelPrice = unitOfWork.ChanelPriceRepository.FindBy(o => o.ChannelId == channel.Id && o.ProductId == product.id).FirstOrDefault(); if (channelPrice != null) { channelPrice.Price_Discount = priceDisscount_Value; channelPrice.StartDate_Discount = priceDisscount_StarDate; channelPrice.Enddate_Discount = priceDisscount_EndDate; unitOfWork.ChanelPriceRepository.Update(channelPrice, o => o.Price_Discount, o => o.StartDate_Discount, o => o.Enddate_Discount); isUpdatePriceChanleDisscount = true; } } } catch (Exception ex) { priceDisscount_Value = 0; priceDisscount_StarDate = null; priceDisscount_EndDate = null; } } } #endregion #region Tồn kho if (columnName.StartsWith(perfixBranches)) { var codeBranche = columnName.Substring(perfixBranches.Length); var barches = braches_lst.FirstOrDefault(o => o.Code.Equals(codeBranche)); if (barches != null) { if (product.id <= 0) { var newobj = new soft_Branches_Product_Stock { ProductId = product.id, BranchesId = barches.BranchesId, Stock_Total = double.Parse(value), DateCreate = DateTime.Now, EmployeeCreate = UserId, }; unitOfWork.BrachesStockRepository.Add(newobj); } else { var stock = unitOfWork.BrachesStockRepository.FindBy(o => o.BranchesId == barches.BranchesId && o.ProductId == product.id).FirstOrDefault(); if (stock == null) { var newobj = new soft_Branches_Product_Stock { ProductId = product.id, BranchesId = barches.BranchesId, Stock_Total = double.Parse(value), DateCreate = DateTime.Now, EmployeeCreate = UserId }; unitOfWork.BrachesStockRepository.Add(newobj); } else { stock.Stock_Total = double.Parse(value); unitOfWork.BrachesStockRepository.Update(stock, o => o.Stock_Total); } } } } #endregion } } catch (Exception ex) { lstError.Add(new shop_sanpham { tensp = product.tensp, masp = product.masp, id = line }); isError = true; } if (isError) { continue; } try { if (isUpdateProduct) { if (product.id <= 0) { product.DateCreate = DateTime.Now; product.FromCreate = (int)TypeFromCreate.Soft; unitOfWork.ProductRepository.Add(product); unitOfWork.ImageRepository.Add(imgage); } else { unitOfWork.ProductRepository.Update(product, o => o.tensp, o => o.PriceBase, o => o.PriceCompare, o => o.PriceBase_Old, o => o.PriceInput, o => o.Status, o => o.StatusVAT, o => o.SuppliersId, o => o.CatalogId, o => o.PriceWholesale); } } await Update_is_import(true, null, percent); if (isUpdateProduct || isUpdatePriceChanle || isUpdateStock || isUpdatePriceChanleDisscount) { await unitOfWork.SaveChanges(); } } catch (Exception ex) { lstError.Add(new shop_sanpham { tensp = product.tensp, masp = product.masp, id = line }); } } var error = string.Empty; if (lstError != null && lstError.Count > 0) { error = new JavaScriptSerializer().Serialize(lstError.Select(o => new { masp = o.masp, tensp = o.tensp, id = o.id })); } await Update_is_import(false, error, percent, true); return(lstError); } catch (Exception ex) { lstError.Add(new shop_sanpham { tensp = ex.Message, masp = masp, id = line }); var error = new JavaScriptSerializer().Serialize(lstError.Select(o => new { masp = o.masp, tensp = o.tensp, id = o.id })); await Update_is_import(false, error, -1, true); return(null); } }
private void UpdateStockByBranches(OrderModel order) { foreach (var item in order.Detail) { var StockProduct = _context.soft_Branches_Product_Stock.Where(o => o.ProductId == item.ProductId).ToList(); if (order.TypeOrder == (int)TypeOrder.Input) { #region Product var product = _context.shop_sanpham.Find(item.ProductId); if (product != null) { var productObj = new shop_sanpham { id = product.id, PriceBase_Old = product.PriceBase, PriceInput = (int)item.Price, PriceCompare = item.PriceCompare }; var stockbySum = StockProduct != null && StockProduct.Count > 0 ? StockProduct.Sum(o => o.Stock_Total) : 0; var pricebase_old = product.PriceBase != null ? product.PriceBase : 0; var chia = (item.Total + stockbySum); productObj.PriceBase = (int)(((stockbySum * pricebase_old) + (item.Total * (int)item.Price)) / (chia != 0 ? chia : 1)); _crud.Update(productObj, o => o.PriceBase, o => o.PriceInput, o => o.PriceBase_Old, o => o.PriceCompare); #endregion #region Stcok var stockTo = StockProduct.FirstOrDefault(o => o.ProductId == item.ProductId && o.BranchesId == order.Id_To); if (stockTo != null) { var newstock = new soft_Branches_Product_Stock { BranchesId = stockTo.BranchesId, ProductId = stockTo.ProductId, Stock_Total = stockTo.Stock_Total + item.Total, DateCreate = stockTo.DateCreate, EmployeeCreate = stockTo.EmployeeCreate, DateUpdate = DateTime.Now, EmployeeUpdate = User.UserId, }; _crud.Update <soft_Branches_Product_Stock>(newstock, o => o.Stock_Total, o => o.EmployeeUpdate, o => o.DateUpdate); } else { var stockNewTo = new soft_Branches_Product_Stock { BranchesId = order.Id_To, ProductId = item.ProductId, Stock_Total = item.Total, DateCreate = DateTime.Now, EmployeeCreate = order.EmployeeCreate }; _crud.Add <soft_Branches_Product_Stock>(stockNewTo); } #endregion } } if (order.TypeOrder == (int)TypeOrder.Output) { var stockTo = StockProduct.FirstOrDefault(o => o.ProductId == item.ProductId && o.BranchesId == order.Id_To); var stockFrom = StockProduct.FirstOrDefault(o => o.ProductId == item.ProductId && o.BranchesId == order.Id_From); if (stockTo != null) { var newstock = new soft_Branches_Product_Stock { BranchesId = stockTo.BranchesId, ProductId = stockTo.ProductId, Stock_Total = stockTo.Stock_Total + item.Total, DateCreate = stockTo.DateCreate, EmployeeCreate = stockTo.EmployeeCreate, DateUpdate = DateTime.Now, EmployeeUpdate = User.UserId, }; _crud.Update <soft_Branches_Product_Stock>(newstock, o => o.Stock_Total, o => o.EmployeeUpdate, o => o.DateUpdate); } else { stockTo = new soft_Branches_Product_Stock { ProductId = item.ProductId, BranchesId = User.BranchesId, DateCreate = DateTime.Now, Stock_Total = item.Total, EmployeeCreate = User.UserId }; _crud.Add(stockTo); } if (stockFrom != null) { var newstock = new soft_Branches_Product_Stock { BranchesId = stockFrom.BranchesId, ProductId = stockFrom.ProductId, Stock_Total = stockFrom.Stock_Total - item.Total, DateCreate = stockFrom.DateCreate, EmployeeCreate = stockFrom.EmployeeCreate, DateUpdate = order.DateCreate, EmployeeUpdate = order.EmployeeCreate, }; _crud.Update(newstock, o => o.Stock_Total, o => o.EmployeeUpdate, o => o.DateUpdate); } else { var stockNewTo = new soft_Branches_Product_Stock { BranchesId = order.Id_From, ProductId = item.ProductId, Stock_Total = 0 - item.Total, DateCreate = order.DateCreate, EmployeeCreate = order.EmployeeCreate, DateUpdate = null, EmployeeUpdate = null }; _crud.Add <soft_Branches_Product_Stock>(stockNewTo); } } } }