public Product getProductDetail(long PRODUCT_ID) { ProductDa dataAccess = new ProductDa(); Product product = dataAccess.getProductDetail(PRODUCT_ID); return(product); }
public ActionResult XoaGioHang(long Id, string strUrl) { ProductDa da = new ProductDa(); // kiểm tra sản phẩm có tồn tại trong csdl ko Product sanpham = da.getProductByID(Id); if (sanpham == null) { // trang đường dẩn ko hợp lệ Response.StatusCode = 404; return(null); } // lấy giỏ hàng List <ItemGioHang> lstGioHang = LayGioHang(); // trường hợp đã tồn tại một sản phẩm trong giỏ hàng ItemGioHang sanphamcheck = lstGioHang.SingleOrDefault(n => n.IdSanPham == Id); if (sanphamcheck != null) { lstGioHang.Remove(sanphamcheck); } Session["GioHang"] = lstGioHang; double TongSoLuong = TinhTongSoLuong(); decimal TotalMoney = TongTien(); return(Redirect(strUrl)); }
public bool DeleteProduct(long PRODUCT_ID) { ProductDa dataAccess = new ProductDa(); long result = 0; using (var transaction = new TransactionScope()) { try { result = dataAccess.DeleteProduct(PRODUCT_ID); if (result > 0) { transaction.Complete(); } } catch (Exception ex) { transaction.Dispose(); result = -1; throw new Exception(ex.Message, ex); } finally { transaction.Dispose(); } } return(result > 0); }
public IEnumerable <Product> SearchProductList(int maxItem = 12) { ProductDa dataAccess = new ProductDa(); IEnumerable <Product> results = dataAccess.GetHotProducts(maxItem); return(results); }
public IEnumerable <ProductModel> SearchProductList(DataTableModel dt, ProductModel model, out int total_row) { ProductDa dataAccess = new ProductDa(); IEnumerable <ProductModel> results = dataAccess.SearchProductList(dt, model, out total_row); return(results); }
public void TestMethod1() { var productDa = new ProductDa(); productDa.Get(1); productDa.GetList(); productDa.Insert(new Product()); }
public ActionResult ThemGioHangAjax(long Id = 0) { ProductDa da = new ProductDa(); // kiểm tra sản phẩm có tồn tại trong csdl ko Product sanpham = da.getProductByID(Id); if (sanpham == null) { // trang đường dẩn ko hợp lệ Response.StatusCode = 404; return(null); } // lấy giỏ hàng List <ItemGioHang> lstGioHang = LayGioHang(); // trường hợp đã tồn tại một sản phẩm trong giỏ hàng ItemGioHang sanphamcheck = lstGioHang.SingleOrDefault(n => n.IdSanPham == Id); int response = 0; if (sanphamcheck != null) { //kiểm tra số lượng sản phẩm tồn if (sanpham.Quantity < sanphamcheck.SoLuong + 1) { response = -1; } else { sanphamcheck.SoLuong++; sanphamcheck.TienHang = sanphamcheck.SoLuong * sanphamcheck.DonGia; response = 1; } } else { ItemGioHang itemGH = new ItemGioHang(Id); if (sanpham.Quantity < itemGH.SoLuong) { response = -1; } else { response = 1; lstGioHang.Add(itemGH); } } Session["GioHang"] = lstGioHang; double TongSoLuong = TinhTongSoLuong(); decimal TotalMoney = TongTien(); JsonResult result = Json(new { TongSoLuong = TongSoLuong, TotalMoney = TotalMoney, response = response }, JsonRequestBehavior.AllowGet); return(result); }
public void CreateOrUpdate(Item item) { ProductDa productDa = new ProductDa(); using (var db = new DatabaseContext()) { productDa.CreateOrUpdate(db, item.Product); item.Product_ID = item.Product.ID; item.Product = null; _itemDa.CreateOrUpdate(db, item); } }
public ItemGioHang(long IdSanPham, int SL) { ProductDa db = new ProductDa(); this.IdSanPham = IdSanPham; Product sanpham = db.getProductByID(IdSanPham); this.TenSanPham = sanpham.Name; this.MetaTitle = sanpham.MetaTitle; this.HinhAnh = sanpham.Image; this.DonGia = sanpham.Price.Value; this.SoLuong = SL; this.TienHang = DonGia * SoLuong; }
public long UpdateProduct(ProductModel model) { long res = 0; // Declare new DataAccess object ProductDa dataAccess = new ProductDa(); using (var transaction = new TransactionScope()) { try { Product product = new Product(); product.ID = model.ID; product.CategoryID = model.CategoryID; product.Name = model.Name; product.MetaTitle = model.Name.Contains("/") ? model.Name.Replace("/", "") : model.Name; product.MetaTitle = UtilityServices.UtilityServices.ConvertToUnsign(product.MetaTitle); product.Detail = model.Detail; product.PriceBefore = model.PriceBefore; product.Price = model.Price; product.Promotion = model.Promotion; product.Quantity = model.Quantity; product.Is_Hot = model.Is_Hot; product.Discount = model.Discount; product.Image = model.Image; product.Unit = model.Unit; product.Status = model.Status; product.del_flg = Constant.DeleteFlag.NON_DELETE; res = dataAccess.UpdateProduct(product); if (res <= 0) { transaction.Dispose(); } transaction.Complete(); } catch (Exception ex) { throw new Exception(ex.Message, ex); } finally { transaction.Dispose(); } } return(res); }
public bool CreateOrUpdate(Item item) { using (var db = new DatabaseContext()) { try { ProductDa productDa = new ProductDa(); productDa.CreateOrUpdate(db, item.Product); item.Product_ID = item.Product.ID; item.Product = null; _itemDa.CreateOrUpdate(db, item); return(true); } catch { return(false); } } }
// thêm giỏ hàng thông thường (Load lại trang) public ActionResult ThemGioHang(long IdSanPham, string strUrl) { ProductDa da = new ProductDa(); // kiểm tra sản phẩm có tồn tại trong csdl ko Product sanpham = da.getProductByID(IdSanPham); if (sanpham == null) { // trang đường dẩn ko hợp lệ Response.StatusCode = 404; return(null); } // lấy giỏ hàng List <ItemGioHang> lstGioHang = LayGioHang(); // trường hợp đã tồn tại một sản phẩm trong giỏ hàng ItemGioHang sanphamcheck = lstGioHang.SingleOrDefault(n => n.IdSanPham == IdSanPham); if (sanphamcheck != null) { // kiểm tra số lượng sản phẩm tồn if (sanpham.Quantity < sanphamcheck.SoLuong + 1) { return(View("ThongBao")); //return Content("<script>alert(\"Sản phẩm đã hết hàng!\")</script>"); } sanphamcheck.SoLuong++; sanphamcheck.TienHang = sanphamcheck.SoLuong * sanphamcheck.DonGia; return(Redirect(strUrl)); } ItemGioHang itemGH = new ItemGioHang(IdSanPham); if (sanpham.Quantity < itemGH.SoLuong) { return(View("ThongBao")); //return Content("<script>alert(\"Sản phẩm đã hết hàng!\")</script>"); } lstGioHang.Add(itemGH); Session["GioHang"] = lstGioHang; //db.SaveChanges(); return(Redirect(strUrl)); }
public ProductModel getProductByID(long PRODUCT_ID) { ProductDa dataAccess = new ProductDa(); ProductModel model = new ProductModel(); var product = dataAccess.getProductByID(PRODUCT_ID); if (product != null) { model.ID = product.ID; model.CategoryID = product.CategoryID; model.Name = product.Name; model.MetaTitle = product.MetaTitle; model.Detail = product.Detail; model.PriceBefore = product.PriceBefore; model.Price = product.Price; model.Promotion = product.Promotion; model.Quantity = product.Quantity; model.Is_Hot = product.Is_Hot; model.Discount = product.Discount; model.Image = product.Image; model.Status = product.Status; model.Unit = product.Unit; model.CreatedDate = product.CreatedDate; model.CreatedBy = product.CreatedBy; model.ModifiedDate = product.ModifiedDate; long userCreateID = product.CreatedBy.HasValue ? product.CreatedBy.Value : 0; var userCreate = dataAccess.getUserByID(userCreateID); model.CreateName = userCreate != null ? userCreate.UserName : ""; long userUpdateID = product.ModifiedBy.HasValue ? product.ModifiedBy.Value : 0; var userUpdate = dataAccess.getUserByID(userUpdateID); model.ModifiedName = userUpdate != null ? userUpdate.UserName : ""; model.del_flg = Constant.DeleteFlag.NON_DELETE; } return(model); }
public bool CreateOrUpdate(Invoice invoice) { CompanyDa companyDa = new CompanyDa(); RepresentativeDa representativeDa = new RepresentativeDa(); ProductDa productDa = new ProductDa(); ItemDa itemDa = new ItemDa(); ElementDa elementDa = new ElementDa(); using (var db = new DatabaseContext()) { using (var dbTransaction = db.Database.BeginTransaction()) { try { #region Invoice companyDa.CreateOrUpdate(db, invoice.Receiver.Company); invoice.Receiver.Company_ID = invoice.Receiver.Company.ID; invoice.Receiver.Company = null; companyDa.CreateOrUpdate(db, invoice.Sender.Company); invoice.Sender.Company_ID = invoice.Sender.Company.ID; invoice.Sender.Company = null; representativeDa.CreateOrUpdate(db, invoice.Receiver); invoice.Receiver_ID = invoice.Receiver.ID; invoice.Receiver = null; representativeDa.CreateOrUpdate(db, invoice.Sender); invoice.Sender_ID = invoice.Sender.ID; invoice.Sender = null; List <Element> elements = invoice.Elements == null ? new List <Element>() : invoice.Elements.ToList(); invoice.Elements = null; #region Transport decimal total; if (invoice.Incoming) //incoming { total = elements.Sum(item => item.Item.IncomingPrice); } else//outgoing { total = elements.Sum(item => item.Item.OutgoingPrice); } decimal transport = invoice.Transport; Invoice dbInvoice = _invoiceDa.GetInvoice(db, invoice.ID); if (dbInvoice != null) { transport = transport - dbInvoice.Transport; } if (total != 0) { transport = transport / total; } #endregion _invoiceDa.CreateOrUpdate(db, invoice); #endregion if (invoice.Incoming)//incoming { foreach (Element element in elements) { Item item = itemDa.GetItem(db, element.Item.ID); List <int> itemIds = new List <int>(); if (item != null) { itemIds = elementDa.GetSameItemIdsInIncomingInvoice(db, item, invoice.ID); } productDa.CreateOrUpdate(db, element.Item.Product); //removes from database if count was reduced and when item wasn't sold yet if (itemIds.Count > element.Item.Quantity) { int removedCount = itemIds.Count - element.Item.Quantity; List <int> removedItemIds = new List <int>(); foreach (int i in itemIds) { item = itemDa.GetItem(db, i); if (removedCount > 0) { if (item.OutgoingPrice == 0 && item.OutgoingTaxGroup_ID == null) { itemDa.Delete(db, item); removedItemIds.Add(i); removedCount = removedCount - 1; } } else { break; } } itemIds = itemIds.Except(removedItemIds).ToList(); } for (int i = 0; i < element.Item.Quantity; i++) { item = itemDa.GetItem(db, itemIds.ElementAtOrDefault(i)); if (element.Item.Delete) { //remove if item hasn't been sold yet if (item != null && item.OutgoingPrice == 0 && item.OutgoingTaxGroup_ID == null) { itemDa.Delete(db, item); } } else { if (item == null) { item = new Item(); } item.Product_ID = element.Item.Product.ID; item.SerNumber = element.Item.SerNumber; item.IncomingPrice = element.Item.IncomingPrice * transport + element.Item.IncomingPrice; item.IncomingTaxGroup_ID = element.Item.IncomingTaxGroup_ID; itemDa.CreateOrUpdate(db, item); Element tempElement = new Element { Item_ID = item.ID, Invoice_ID = invoice.ID }; elementDa.CreateOrUpdate(db, tempElement); } } } } else//outgoing { foreach (Element element in elements) { Item item = itemDa.GetItem(db, element.Item.ID); List <int> itemIds = elementDa.GetSameItemIdsInOutgoingInvoice(db, item, invoice.ID); productDa.CreateOrUpdate(db, element.Item.Product); //removes from invoice if count was reduced if (itemIds.Count > element.Item.Quantity) { int removedCount = itemIds.Count - element.Item.Quantity; List <int> removedItemIds = new List <int>(); foreach (int i in itemIds) { item = itemDa.GetItem(db, i); if (removedCount > 0) { //on delete of outgoing make item not sold and removes from invoice item.OutgoingPrice = 0; item.OutgoingTaxGroup_ID = null; itemDa.CreateOrUpdate(db, item); Element tempElement = new Element { Item_ID = item.ID, Invoice_ID = invoice.ID }; db.Elements.Attach(tempElement); elementDa.Delete(db, tempElement); removedItemIds.Add(i); removedCount = removedCount - 1; } else { break; } } itemIds = itemIds.Except(removedItemIds).ToList(); } for (int i = 0; i < element.Item.Quantity; i++) { item = itemDa.GetItem(db, itemIds.ElementAtOrDefault(i)); if (element.Item.Delete)//on delete of outgoing make item not sold and removes from invoice { item.OutgoingPrice = 0; item.OutgoingTaxGroup_ID = null; itemDa.CreateOrUpdate(db, item); Element tempElement = new Element { Item_ID = item.ID, Invoice_ID = invoice.ID }; db.Elements.Attach(tempElement); elementDa.Delete(db, tempElement); } else { if (item == null) { item = itemDa.GetItem(db, element.Item.ID); item = itemDa.GetItemNotSoldItem(db, item).ElementAtOrDefault(0); } if (item != null) { item.Product_ID = element.Item.Product.ID; item.OutgoingPrice = element.Item.OutgoingPrice * transport + element.Item.OutgoingPrice; item.OutgoingTaxGroup_ID = element.Item.OutgoingTaxGroup_ID; itemDa.CreateOrUpdate(db, item); Element tempElement = new Element { Item_ID = item.ID, Invoice_ID = invoice.ID }; elementDa.CreateOrUpdate(db, tempElement); } } } } } dbTransaction.Commit(); return(true); } catch { dbTransaction.Rollback(); return(false); } } } }
public bool CreateOrUpdate(Invoice invoice) { AddressDa addressDa = new AddressDa(); CompanyDa companyDa = new CompanyDa(); RepresentativeDa representativeDa = new RepresentativeDa(); ProductDa productDa = new ProductDa(); ItemDa itemDa = new ItemDa(); ElementDa elementDa = new ElementDa(); using (var db = new DatabaseContext()) { using (var dbTransaction = db.Database.BeginTransaction()) { try { #region Invoice #region Receiver addressDa.CreateOrUpdate(db, invoice.Receiver.Company.ActualAddress); invoice.Receiver.Company.ActualAddress_ID = invoice.Receiver.Company.ActualAddress.ID; invoice.Receiver.Company.ActualAddress = null; addressDa.CreateOrUpdate(db, invoice.Receiver.Company.LegalAddress); invoice.Receiver.Company.LegalAddress_ID = invoice.Receiver.Company.LegalAddress.ID; invoice.Receiver.Company.LegalAddress = null; companyDa.CreateOrUpdate(db, invoice.Receiver.Company); invoice.Receiver.Company_ID = invoice.Receiver.Company.ID; invoice.Receiver.Company = null; representativeDa.CreateOrUpdate(db, invoice.Receiver); invoice.Receiver_ID = invoice.Receiver.ID; invoice.Receiver = null; #endregion #region Sender addressDa.CreateOrUpdate(db, invoice.Sender.Company.ActualAddress); invoice.Sender.Company.ActualAddress_ID = invoice.Sender.Company.ActualAddress.ID; invoice.Sender.Company.ActualAddress = null; addressDa.CreateOrUpdate(db, invoice.Sender.Company.LegalAddress); invoice.Sender.Company.LegalAddress_ID = invoice.Sender.Company.LegalAddress.ID; invoice.Sender.Company.LegalAddress = null; companyDa.CreateOrUpdate(db, invoice.Sender.Company); invoice.Sender.Company_ID = invoice.Sender.Company.ID; invoice.Sender.Company = null; representativeDa.CreateOrUpdate(db, invoice.Sender); invoice.Sender_ID = invoice.Sender.ID; invoice.Sender = null; #endregion List <Element> elements = invoice.Elements == null ? new List <Element>() : invoice.Elements.ToList(); invoice.Elements = null; //generate the regNumber (cant be null) invoice.RegNumber = "r"; _invoiceDa.CreateOrUpdate(db, invoice); #endregion if (invoice.Incoming)//incoming { foreach (Element element in elements) { Item item = itemDa.GetItem(db, element.Item.ID); List <int> itemIds = new List <int>(); if (item != null) { itemIds = elementDa.GetSameItemIdsFromInvoice(db, item, invoice.ID, true); } productDa.CreateOrUpdate(db, element.Item.Product); //removes from database if count was reduced and when item wasn't sold yet if (itemIds.Count > element.Item.Quantity) { int removedCount = itemIds.Count - element.Item.Quantity; List <int> removedItemIds = new List <int>(); foreach (int i in itemIds) { item = itemDa.GetItem(db, i); if (removedCount > 0) { if (item.OutgoingTaxGroup_ID == null) { itemDa.Delete(db, item); removedItemIds.Add(i); removedCount = removedCount - 1; } } else { break; } } itemIds = itemIds.Except(removedItemIds).ToList(); } for (int i = 0; i < element.Item.Quantity; i++) { item = itemDa.GetItem(db, itemIds.ElementAtOrDefault(i)); if (element.Item.Delete) { //remove if item hasn't been sold yet if (item.OutgoingTaxGroup_ID == null) { itemDa.Delete(db, item); } } else { if (item == null) { item = new Item(); } item.Product_ID = element.Item.Product.ID; item.SerNumber = element.Item.SerNumber; item.Price = element.Item.Price; item.IncomingTaxGroup_ID = element.Item.IncomingTaxGroup.ID; itemDa.CreateOrUpdate(db, item); Element tempElement = new Element { Item_ID = item.ID, Invoice_ID = invoice.ID }; elementDa.CreateOrUpdate(db, tempElement); } } } } else//outgoing { foreach (Element element in elements) { Item item = itemDa.GetItem(db, element.Item.ID); List <int> sameItemIds = elementDa.GetSameItemIdsFromInvoice(db, item, invoice.ID, false);//gets similar items in invoice productDa.CreateOrUpdate(db, element.Item.Product); //removes from invoice if count was reduced if (sameItemIds.Count > element.Item.Quantity) { int removedCount = sameItemIds.Count - element.Item.Quantity; List <int> removedItemIds = new List <int>(); foreach (int i in sameItemIds) { item = itemDa.GetItem(db, i); if (removedCount > 0) { //on delete of outgoing make item not sold and removes from invoice item.OutgoingTaxGroup_ID = null; itemDa.CreateOrUpdate(db, item); Element tempElement = new Element { Item_ID = item.ID, Invoice_ID = invoice.ID }; db.Elements.Attach(tempElement); elementDa.Delete(db, tempElement); removedItemIds.Add(i); removedCount = removedCount - 1; } else { break; } } sameItemIds = sameItemIds.Except(removedItemIds).ToList(); } for (int i = 0; i < element.Item.Quantity; i++) { //tries to get the item item = itemDa.GetItem(db, sameItemIds.ElementAtOrDefault(i)); if (element.Item.Delete)//on delete of outgoing make item not sold and removes from invoice { item.OutgoingTaxGroup_ID = null; itemDa.CreateOrUpdate(db, item); Element tempElement = new Element { Item_ID = item.ID, Invoice_ID = invoice.ID }; db.Elements.Attach(tempElement); elementDa.Delete(db, tempElement); } else { if (item == null)//if item not found take same, but nut sold { item = itemDa.GetItem(db, element.Item.ID); item = new ItemDM().GetSameIncomingPriceItems(db, item).Item1.FirstOrDefault(); } if (item != null) { item.Product_ID = element.Item.Product.ID; item.OutgoingTaxGroup_ID = element.Item.OutgoingTaxGroup.ID; itemDa.CreateOrUpdate(db, item); Element tempElement = new Element { Item_ID = item.ID, Invoice_ID = invoice.ID }; elementDa.CreateOrUpdate(db, tempElement); } } } } } dbTransaction.Commit(); return(true); } catch { dbTransaction.Rollback(); return(false); } } } }
internal ProductDM() { _productDa = new ProductDa(); }
public static Product Get() { return(ProductDa.GetProduct()); }
public static void Add(Product product) { ProductDa.AddProduct(product); }