private void UpdateProductGuideProducts(string[] selectedProducts, ProductGuide productGuideToUpdate) { if (selectedProducts == null) { productGuideToUpdate.ProductAssignments = new List <ProductAssignment>(); return; } var selectedProductsHS = new HashSet <string>(selectedProducts); var productGuideProducts = new HashSet <int> (productGuideToUpdate.ProductAssignments.Select(c => c.Product.ProductID)); foreach (var product in _context.Products) { if (selectedProductsHS.Contains(product.ProductID.ToString())) { if (!productGuideProducts.Contains(product.ProductID)) { productGuideToUpdate.ProductAssignments.Add(new ProductAssignment { ProductGuideID = productGuideToUpdate.ID, ProductID = product.ProductID }); } } else { if (productGuideProducts.Contains(product.ProductID)) { ProductAssignment productToRemove = productGuideToUpdate.ProductAssignments.FirstOrDefault(i => i.ProductID == product.ProductID); _context.Remove(productToRemove); } } } }
/// <summary> /// To assign List of products into single User /// </summary> /// <param name="lsProduct"></param> /// <param name="CurrentUserId"></param> /// <param name="AssigneeUserId"></param> public void AssignProduct(List <AssProHelper> lsProduct, long CurrentUserId, Decimal AdvAmount, Decimal TotalAmount, long AssigneeUserId, out bool isSuccess) { try { using (UnitOfWork unitOfWork = new UnitOfWork()) { ProductRepository = unitOfWork.GetRepoInstance <Product>(); AssignmentProdRepository = unitOfWork.GetRepoInstance <AssignmentProd>(); //IQueryable<Product> lspro = ProductRepository.GetAllExpressions(x => lsProduct.Select(y => y.ProductId).ToList<long>().Contains(x.ProductPID), null, null); bool isvalidOperation = CheckValidOperation(lsProduct); if (isvalidOperation) { ProductAssignmentRepository = unitOfWork.GetRepoInstance <ProductAssignment>(); ProductAssignment proAssignment = new ProductAssignment(); proAssignment.AssignedUserPid = AssigneeUserId; proAssignment.CreatedOn = DateTime.Now; proAssignment.UpdatedOn = DateTime.Now; proAssignment.CreatedUserPID = CurrentUserId; proAssignment.AdvanceAmount = AdvAmount; proAssignment.TotalAmount = TotalAmount; proAssignment.ProductCode = GenProductCode("Assign");//newly generated code ProductAssignmentRepository.Insert(proAssignment); foreach (var pro in lsProduct) { long Quntity = pro.Qunty; AssignmentProd assignProduct = new AssignmentProd(); Product prdct = ProductRepository.GetByID(pro.ProductId); assignProduct.Product = prdct; assignProduct.Quantity = Quntity; assignProduct.RemQuanity = Quntity; assignProduct.ProductAssignmentPID = proAssignment.ProductAssignmentPID; AssignmentProdRepository.Insert(assignProduct); prdct.UpdatedOn = DateTime.Now; prdct.UpdatedUserPID = CurrentUserId; prdct.RemQuantity = (prdct.RemQuantity == 0) ? prdct.Quanity - Quntity : prdct.RemQuantity - Quntity; ProductRepository.Update(prdct); } unitOfWork.SaveChanges(); //transactFactory.CreateTransac(CurrentUserId, AssigneeUserId,null,0, (long)InlineTranscatType.Assign); isSuccess = true; } else { //send Invalid Operation to User LogHelper.WriteErrorLog("Invalid Operation From User"); isSuccess = false; } } } catch (Exception ex) { LogHelper.WriteErrorLog(ex); isSuccess = false; } }
protected void Page_PreRender(object sender, EventArgs e) { gridFooter.Visible = PG.Visible && PG.Rows.Count > 0; if (!Page.IsPostBack && gridFooter.Visible) { ProductAssignment.ClearSelection(); ListItem item = ProductAssignment.Items.FindByValue(AssignmentStatus); if (item != null) { item.Selected = true; } PG.DataBind(); } }
protected void PG_DataBound(object sender, EventArgs e) { if (!Page.IsPostBack) { if (PG.Rows.Count == 0) { ProductAssignment.ClearSelection(); ListItem item = ProductAssignment.Items.FindByValue("Any"); if (item != null) { item.Selected = true; } PG.DataBind(); } } }
public async Task <IActionResult> Create([Bind("CountryAssignment,LastName,FirstMidName,HireDate")] ProductGuide productGuide, string[] selectedProducts) { if (selectedProducts != null) { productGuide.ProductAssignments = new List <ProductAssignment>(); foreach (var product in selectedProducts) { var productToAdd = new ProductAssignment { ProductGuideID = productGuide.ID, ProductID = int.Parse(product) }; productGuide.ProductAssignments.Add(productToAdd); } } if (ModelState.IsValid) { _context.Add(productGuide); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } PopulateAssignedProductData(productGuide); return(View(productGuide)); }
public static void Initialize(BusinessContext context) { //context.Database.EnsureCreated(); // Look for any customers. if (context.Customers.Any()) { return; // DB has been seeded } var customers = new Customer[] { new Customer { FirstMidName = "Ahmad", LastName = "Jensen", SubscriptionDate = DateTime.Parse("2018-09-01") }, new Customer { FirstMidName = "Meredith", LastName = "Alonso", SubscriptionDate = DateTime.Parse("2017-09-01") }, new Customer { FirstMidName = "Arturo", LastName = "Anand", SubscriptionDate = DateTime.Parse("2018-09-01") }, new Customer { FirstMidName = "Gytis", LastName = "Barzdukas", SubscriptionDate = DateTime.Parse("2017-09-01") }, new Customer { FirstMidName = "Yan", LastName = "Li", SubscriptionDate = DateTime.Parse("2017-09-01") }, new Customer { FirstMidName = "Peggy", LastName = "Justice", SubscriptionDate = DateTime.Parse("2016-09-01") }, new Customer { FirstMidName = "Laura", LastName = "Norman", SubscriptionDate = DateTime.Parse("2018-09-01") }, new Customer { FirstMidName = "Nino", LastName = "Olivetto", SubscriptionDate = DateTime.Parse("2019-09-01") } }; foreach (Customer c in customers) { context.Customers.Add(c); } context.SaveChanges(); var productGuides = new ProductGuide[] { new ProductGuide { FirstMidName = "Faizan", LastName = "Quershi", HireDate = DateTime.Parse("2000-03-11") }, new ProductGuide { FirstMidName = "Fadi", LastName = "Fakhouri", HireDate = DateTime.Parse("2002-07-06") }, new ProductGuide { FirstMidName = "Roger", LastName = "Harui", HireDate = DateTime.Parse("1998-07-01") }, new ProductGuide { FirstMidName = "Candace", LastName = "Kapoor", HireDate = DateTime.Parse("2001-01-15") }, new ProductGuide { FirstMidName = "Roger", LastName = "Youssef", HireDate = DateTime.Parse("2004-02-12") } }; foreach (ProductGuide pg in productGuides) { context.ProductGuides.Add(pg); } context.SaveChanges(); var markets = new Market[] { new Market { Name = "England", Budget = 350000, StartDate = DateTime.Parse("2000-09-01"), ProductGuideID = productGuides.Single(pg => pg.LastName == "Quershi").ID }, new Market { Name = "Danmark", Budget = 100000, StartDate = DateTime.Parse("2007-09-01"), ProductGuideID = productGuides.Single(pg => pg.LastName == "Youssef").ID }, new Market { Name = "Rusland", Budget = 350000, StartDate = DateTime.Parse("2007-09-01"), ProductGuideID = productGuides.Single(pg => pg.LastName == "Harui").ID }, new Market { Name = "Sverige", Budget = 100000, StartDate = DateTime.Parse("2007-09-01"), ProductGuideID = productGuides.Single(pg => pg.LastName == "Kapoor").ID } }; foreach (Market m in markets) { context.Markets.Add(m); } context.SaveChanges(); var products = new Product[] { new Product { ProductID = 1050, ProductName = "CD-ORD", Price = 100, MarketID = markets.Single(s => s.Name == "Danmark").MarketID }, new Product { ProductID = 4022, ProductName = "Intowords", Price = 100, MarketID = markets.Single(s => s.Name == "England").MarketID }, new Product { ProductID = 4041, ProductName = "Reading Pen", Price = 100, MarketID = markets.Single(s => s.Name == "Rusland").MarketID }, new Product { ProductID = 1045, ProductName = "CD-ORD and Intowords", Price = 150, MarketID = markets.Single(s => s.Name == "Sverige").MarketID }, new Product { ProductID = 3141, ProductName = "CD-ORD and Reading Pen", Price = 150, MarketID = markets.Single(s => s.Name == "Danmark").MarketID }, new Product { ProductID = 2021, ProductName = "Intowords and Reading Pen", Price = 150, MarketID = markets.Single(s => s.Name == "Danmark").MarketID }, new Product { ProductID = 2042, ProductName = "Intowords", Price = 100, MarketID = markets.Single(s => s.Name == "Danmark").MarketID }, }; //foreach (Product p in products) //{ // context.Products.Add(p); //} // Istedet for det udkommenterede foreach loop brug følgende linje: context.AddRange(products); context.SaveChanges(); var countryAssignments = new CountryAssignment[] { new CountryAssignment { ProductGuideID = productGuides.Single(pg => pg.LastName == "Quershi").ID, Location = "Odense" }, new CountryAssignment { ProductGuideID = productGuides.Single(pg => pg.LastName == "Harui").ID, Location = "Gowan 27" }, new CountryAssignment { ProductGuideID = productGuides.Single(pg => pg.LastName == "Kapoor").ID, Location = "Odense vej 23 DK" }, }; foreach (CountryAssignment ca in countryAssignments) { context.CountryAssignments.Add(ca); } context.SaveChanges(); var productProductGuides = new ProductAssignment[] { new ProductAssignment { ProductID = products.Single(p => p.ProductName == "CD-ORD").ProductID, ProductGuideID = productGuides.Single(pg => pg.LastName == "Youssef").ID }, new ProductAssignment { ProductID = products.Single(p => p.ProductName == "Intowords").ProductID, ProductGuideID = productGuides.Single(pg => pg.LastName == "Quershi").ID }, new ProductAssignment { ProductID = products.Single(p => p.ProductName == "Reading Pen").ProductID, ProductGuideID = productGuides.Single(pg => pg.LastName == "Harui").ID }, new ProductAssignment { ProductID = products.Single(p => p.ProductName == "CD-ORD and Intowords").ProductID, ProductGuideID = productGuides.Single(pg => pg.LastName == "Kapoor").ID }, new ProductAssignment { ProductID = products.Single(p => p.ProductName == "Intowords and Reading Pen").ProductID, ProductGuideID = productGuides.Single(pg => pg.LastName == "Youssef").ID }, new ProductAssignment { ProductID = products.Single(p => p.ProductName == "CD-ORD").ProductID, ProductGuideID = productGuides.Single(pg => pg.LastName == "Youssef").ID }, new ProductAssignment { ProductID = products.Single(p => p.ProductName == "Intowords").ProductID, ProductGuideID = productGuides.Single(pg => pg.LastName == "Quershi").ID }, new ProductAssignment { ProductID = products.Single(p => p.ProductName == "Intowords").ProductID, ProductGuideID = productGuides.Single(pg => pg.LastName == "Quershi").ID }, }; foreach (ProductAssignment pa in productProductGuides) { context.ProductAssignments.Add(pa); } context.SaveChanges(); var subscriptions = new Subscription[] { new Subscription { CustomerID = customers.Single(c => c.LastName == "Jensen").ID, ProductID = products.Single(p => p.ProductName == "Intowords").ProductID, CustomerLoyalty = CustomerLoyalty.C }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Alexander").ID, ProductID = products.Single(c => c.ProductName == "CD-ORD").ProductID, CustomerLoyalty = CustomerLoyalty.C }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Alexander").ID, ProductID = products.Single(c => c.ProductName == "CD-ORD").ProductID, CustomerLoyalty = CustomerLoyalty.B }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Alonso").ID, ProductID = products.Single(c => c.ProductName == "CD-ORD").ProductID, CustomerLoyalty = CustomerLoyalty.B }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Alonso").ID, ProductID = products.Single(c => c.ProductName == "Intowords").ProductID, CustomerLoyalty = CustomerLoyalty.B }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Alonso").ID, ProductID = products.Single(c => c.ProductName == "Intowords").ProductID, CustomerLoyalty = CustomerLoyalty.B }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Anand").ID, ProductID = products.Single(c => c.ProductName == "Intowords").ProductID }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Anand").ID, ProductID = products.Single(c => c.ProductName == "Intowords").ProductID, CustomerLoyalty = CustomerLoyalty.B }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Barzdukas").ID, ProductID = products.Single(c => c.ProductName == "CD-ORD").ProductID, CustomerLoyalty = CustomerLoyalty.B }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Li").ID, ProductID = products.Single(c => c.ProductName == "CD-ORD").ProductID, CustomerLoyalty = CustomerLoyalty.B }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Justice").ID, ProductID = products.Single(c => c.ProductName == "CD-ORD").ProductID, CustomerLoyalty = CustomerLoyalty.B } }; foreach (Subscription s in subscriptions) { var subscriptionDataBase = context.Subscriptions.Where( c => c.Customer.ID == s.CustomerID && c.Product.ProductID == s.ProductID).SingleOrDefault(); if (subscriptionDataBase == null) { context.Subscriptions.Add(s); } } context.SaveChanges(); }