public void DeleteProduct(Detail_Product product) { db.Detail_Product.Attach(product); db.Detail_Product.Remove(product); Input_Form input_ = new Input_Form(); input_ = db.Input_Form.FirstOrDefault(x => x.ID_Product == product.ID_Product); db.Input_Form.Attach(input_); db.Input_Form.Remove(input_); db.SaveChanges(); }
public void DeleteSupplier(Supplier a) { db.Supplier.Attach(a); db.Supplier.Remove(a); var pro = from pros in db.Detail_Product where pros.ID_Supplier == a.ID_sup select pros; foreach (Detail_Product product in pro) { db.Detail_Product.Attach(product); db.Detail_Product.Remove(product); Input_Form input_ = new Input_Form(); input_ = db.Input_Form.FirstOrDefault(x => x.ID_Product == product.ID_Product); db.Input_Form.Attach(input_); db.Input_Form.Remove(input_); } db.SaveChanges(); }
public void AddProduct(bool isEdit, string ID, string typeName, string Name_sup, DateTime dateTime, string Name, int Price, int amount_init, string descr, string img_Path) { Detail_Product product = new Detail_Product(); product.ID_Product = ID; product.NameProduct = Name; product.Original_Price = Price; product.Image_Path = img_Path == null ? null : img_Path; product.Description_Pro = descr.Length == 0 ? null : descr; product.Amount_Current = amount_init; Supplier supp = db.Supplier.FirstOrDefault(x => x.Name_Sup == Name_sup); if (supp != null) { product.ID_Supplier = supp.ID_sup; } //Thêm thông tin vào Input_form cho sản phẩm Type_product type = db.Type_product.FirstOrDefault(x => x.Type_Product1 == typeName); if (type != null) { product.ID_TypeProduct = type.ID; } else { product.ID_Product = "sdf"; } // Nếu sửa if (isEdit) { Input_Form input_ = db.Input_Form.SingleOrDefault(x => x.ID_Product == ID); input_.Input_Date = dateTime; input_.ID_Sup = supp.ID_sup; var oldProduct = db.Detail_Product.FirstOrDefault(x => x.ID_Product == ID); oldProduct.NameProduct = product.NameProduct; oldProduct.Original_Price = product.Original_Price; oldProduct.Description_Pro = product.Description_Pro; oldProduct.Image_Path = product.Image_Path; oldProduct.Amount_Current += product.Amount_Current; // Thêm lượng mới nhập vào cả tồn kho ban đầu và tồn kho hiện tại if (oldProduct.ID_TypeProduct != type.ID) // Nếu có thay đổi mã sản phẩm { type.Num_Of_Product++; // Tăng mã mới Type_product oldType = db.Type_product.Find(oldProduct.ID_TypeProduct); oldType.Num_Of_Product--; // Giảm mã cũ oldProduct.ID_TypeProduct = product.ID_TypeProduct; } } // Nếu thêm else // Nếu thêm { Input_Form input = new Input_Form(); input.ID_Product = ID; input.ID_Sup = supp.ID_sup; input.Input_Date = dateTime; input.Amount = amount_init; //Tao id tự động cho Input ObservableCollection <Input_Form> Input = new ObservableCollection <Input_Form>(db.Input_Form); int count = Input.Count(); string s1 = Input[count - 1].ID_Input; int s2 = Convert.ToInt32(s1.Remove(0, 2)); if (s2 + 1 < 10) { input.ID_Input = "Ip00" + (s2 + 1).ToString(); } else { input.ID_Input = "Ip0" + (s2 + 1).ToString(); } db.Input_Form.Add(input); db.Detail_Product.Add(product); type.Num_Of_Product += product.Amount_Current; } db.SaveChanges(); //db.SaveChanges(); }