public bool CreateSale(SalesDto sale) { using (var dbContext = new SalesBogEntities()) { var dbSale = new Sales { ConsultantID = sale.ConsultantID, SaleDescription = sale.SaleDescription, SaleDate = DateTime.Now }; var prodSales = new List <ProductSales>(); var products = new List <Products>(); foreach (var item in sale.Products) { prodSales.Add(new ProductSales { ProductID = item.ID, Sales = dbSale, ProductCount = item.ProductCount }); } dbContext.ProductSales.AddRange(prodSales); return(dbContext.SaveChanges() > 0 ? true : false); } }
public bool EditSales(SalesDto sale) { using (var dbContext = new SalesBogEntities()) { var dbSale = dbContext.Sales.Where(w => w.ID == sale.ID).FirstOrDefault(); dbSale.SaleDescription = sale.SaleDescription; dbSale.ConsultantID = sale.ConsultantID; //Existing Products Edit var existingDBProducts = dbContext.ProductSales.Where(w => w.SaleID == sale.ID).ToList(); var modelProducts = sale.Products.Where(w => w.IsDeleted == false && w.ID != 0).ToList(); var equalProducts = (from e in existingDBProducts join m in modelProducts on e.ProductID equals m.ID where e.SaleID == sale.ID select e).ToList(); if (equalProducts.Count > 0) { foreach (var item in equalProducts) { var correspondingProduct = modelProducts.Where(w => w.ID == item.ProductID).FirstOrDefault(); item.ProductID = correspondingProduct.ID; item.ProductCount = correspondingProduct.ProductCount; } } //New Products Add var newProducts = modelProducts.Where(s => !existingDBProducts.Where(w => w.SaleID == sale.ID).Select(a => a.ProductID).Contains(s.ID)).ToList(); if (newProducts.Count() > 0) { var prodSales = new List <ProductSales>(); foreach (var item in newProducts) { prodSales.Add(new ProductSales { ProductID = item.ID, Sales = dbSale, ProductCount = item.ProductCount }); } dbContext.ProductSales.AddRange(prodSales); } // Deleted Product var deletedProducts = sale.Products.Where(w => w.IsDeleted == true).ToList(); if (deletedProducts.Count() > 0) { foreach (var item in deletedProducts) { var deletedItem = dbContext.ProductSales.Where(w => w.ProductID == item.ID && w.SaleID == dbSale.ID).FirstOrDefault(); dbContext.ProductSales.Remove(deletedItem); } } return(dbContext.SaveChanges() > 0 ? true : false); } }
public bool DeleteConsultant(int id) { using (var dbContext = new SalesBogEntities()) { Consultants consultant = dbContext.Consultants.Find(id); dbContext.Consultants.Remove(consultant); return(dbContext.SaveChanges() > 0 ? true : false); } }
public bool DeleteProduct(int id) { using (var dbContext = new SalesBogEntities()) { Products prod = dbContext.Products.Find(id); dbContext.Products.Remove(prod); return(dbContext.SaveChanges() > 0 ? true : false); } }
public bool DeleteSale(int id) { using (var dbContext = new SalesBogEntities()) { Sales sale = dbContext.Sales.Find(id); dbContext.Sales.Remove(sale); return(dbContext.SaveChanges() > 0 ? true : false); } }
public bool EditProduct(ProductDto product) { using (var dbContext = new SalesBogEntities()) { var dbProduct = dbContext.Products.Where(s => s.ID == product.ID).FirstOrDefault(); dbProduct.Price = product.Price; dbProduct.ProductCode = product.ProductCode; dbProduct.ProductName = product.ProductName; return(dbContext.SaveChanges() > 0 ? true : false); } }
public bool CreateProduct(ProductDto product) { using (var dbContext = new SalesBogEntities()) { dbContext.Products.Add(new Products { Price = product.Price, ProductCode = product.ProductCode, ProductName = product.ProductName }); return(dbContext.SaveChanges() > 0 ? true : false); } }
public bool EditConultant(ConsultantDto consultant) { using (var dbContext = new SalesBogEntities()) { var dbConsultant = dbContext.Consultants.Where(s => s.ID == consultant.ID).FirstOrDefault(); dbConsultant.FirstName = consultant.FirstName; dbConsultant.LastName = consultant.LastName; dbConsultant.Gender = consultant.Gender; dbConsultant.BirthDate = consultant.BirthDate; dbConsultant.RecommenderConsultantID = consultant.RecommenderConsultantID; dbConsultant.PersonalNumber = consultant.PersonalNumber; return(dbContext.SaveChanges() > 0 ? true : false); } }
public bool CreateConsultant(ConsultantDto consultant) { using (var dbContext = new SalesBogEntities()) { dbContext.Consultants.Add(new Consultants { FirstName = consultant.FirstName, LastName = consultant.LastName, BirthDate = consultant.BirthDate, PersonalNumber = consultant.PersonalNumber, Gender = consultant.Gender, RecommenderConsultantID = consultant.RecommenderConsultantID }); return(dbContext.SaveChanges() > 0 ? true : false); } }