public void DeleteUser(int userId) { using (var context = new WareMasterContext()) { var userToDelete = context.Users.FirstOrDefault(user => user.Id == userId); if (userToDelete == null) { return; } var userToDeleteEmployeeOrders = context.Orders.Where(order => order.AssignedEmployeeId == userToDelete.Id); foreach (var order in userToDeleteEmployeeOrders) { order.AssignedEmployeeId = null; } var userToDeleteManagerOrders = context.Orders.Where(order => order.AssignedManagerId == userToDelete.Id); foreach (var order in userToDeleteManagerOrders) { order.AssignedManagerId = null; } var userToDeleteLogs = context.ActivityLogs.Where(log => log.UserId == userToDelete.Id); foreach (var log in userToDeleteLogs) { log.UserId = null; } context.Users.Remove(userToDelete); context.SaveChanges(); } }
public List <ActivityLog> GetActivityLogsForEmployee(int employeeId) { using (var context = new WareMasterContext()) return(context.ActivityLogs.Where(log => log.UserId == employeeId) .OrderByDescending(order => order.TimeOfActivity) .ToList()); }
public List <User> GetAllManagers(int companyId) { using (var context = new WareMasterContext()) return(context.Users .Where(user => user.Role == Role.Manager && user.CompanyId == companyId).ToList()); }
public List <Supplier> SearchSuppliers(int companyId, string searchText) { using (var context = new WareMasterContext()) return(context.Suppliers.Where(supplier => supplier.CompanyId == companyId && supplier.Name.ToLower().StartsWith(searchText.ToLower())) .ToList()); }
public bool DoesBarcodeExist(Product productToTest) { using (var context = new WareMasterContext()) return(context.Products.Where(p => p.Id != productToTest.Id) .Any(p => p.Barcode == productToTest.Barcode && p.CompanyId == productToTest.CompanyId)); }
public Product GetProduct(int productId) { using (var context = new WareMasterContext()) return(context.Products .Include(product => product.Suppliers) .SingleOrDefault(product => product.Id == productId)); }
public List <Supplier> GetAllSuppliers(int companyId) { using (var context = new WareMasterContext()) return(context.Suppliers.Where(supplier => supplier.CompanyId == companyId) .Include(supplier => supplier.Products) .ToList()); }
public List <Product> SearchProducts(int companyId, string searchText) { using (var context = new WareMasterContext()) return(context.Products.Where(product => product.CompanyId == companyId && product.Name.ToLower().StartsWith(searchText.ToLower())) .ToList()); }
public List <User> SearchEmployees(int companyId, string searchText) { using (var context = new WareMasterContext()) return(context.Users.Where(user => user.CompanyId == companyId && user.Role == Role.Employee && (user.FirstName.ToLower().StartsWith(searchText.ToLower()) || user.LastName.ToLower().StartsWith(searchText.ToLower()))) .ToList()); }
public List <Order> GetAllOrders(int companyId) { using (var context = new WareMasterContext()) return(context.Orders.Include(order => order.AssignedEmployee) .Include(order => order.Supplier) .OrderByDescending(order => order.TimeOfCreation) .Where(order => order.CompanyId == companyId) .ToList()); }
public List <ActivityLog> GetActivityLogs(int companyId, int currentPosition) { using (var context = new WareMasterContext()) return(context.ActivityLogs.Where(log => log.CompanyId == companyId) .OrderByDescending(order => order.TimeOfActivity) .Skip(currentPosition) .Take(20) .ToList()); }
public bool DoesUsernameExist(string username) { if (username == null) { return(false); } using (var context = new WareMasterContext()) return(context.Users.Any(user => user.Username == username)); }
public User GetUser(int userId) { using (var context = new WareMasterContext()) return(context.Users .Include(u => u.EmployeeOrders) .Include(u => u.ManagerOrders) .Include(u => u.ActivityLogs) .FirstOrDefault(u => u.Id == userId)); }
public List <User> GetEmployees(int companyId, int currentPosition) { using (var context = new WareMasterContext()) return(context.Users.Where(user => user.Role == Role.Employee && user.CompanyId == companyId) .OrderBy(user => user.Id) .Skip(currentPosition) .Take(10) .ToList()); }
public List <Product> GetProducts(int companyId, int currentPosition) { using (var context = new WareMasterContext()) return(context.Products.Where(product => product.CompanyId == companyId) .OrderBy(product => product.Id) .Skip(currentPosition) .Take(10) .ToList()); }
public Company GetCompanyById(int companyId) { using (var context = new WareMasterContext()) return(context.Companies .Include(company => company.EmployeesManagers) .Include(company => company.Products) .Include(company => company.Orders) .Include(company => company.Suppliers) .FirstOrDefault(company => company.Id == companyId)); }
public List <Supplier> GetSuppliers(int companyId, int currentPosition) { using (var context = new WareMasterContext()) { return(context.Suppliers.Where(supplier => supplier.CompanyId == companyId) .OrderBy(supplier => supplier.Id) .Skip(currentPosition) .Take(10) .ToList()); } }
public List <Order> SearchOrders(int companyId, string searchText) { using (var context = new WareMasterContext()) return(context.Orders.Include(order => order.Supplier) .Include(order => order.AssignedEmployee) .Where(order => order.CompanyId == companyId && (order.Supplier.Name.ToLower().StartsWith(searchText.ToLower()) || order.AssignedEmployee.FirstName.ToLower().StartsWith(searchText.ToLower()) || order.AssignedEmployee.LastName.ToLower().StartsWith(searchText.ToLower()))) .ToList()); }
public int AddUser(User userToAdd) { using (var context = new WareMasterContext()) { userToAdd.Company = context.Companies.Find(userToAdd.CompanyId); context.Companies.Attach(userToAdd.Company); context.Users.Add(userToAdd); context.SaveChanges(); return(userToAdd.Id); } }
public int AddProduct(Product productToAdd) { using (var context = new WareMasterContext()) { productToAdd.Company = context.Companies.Find(productToAdd.CompanyId); context.Companies.Attach(productToAdd.Company); context.Products.Add(productToAdd); context.SaveChanges(); return(productToAdd.Id); } }
public Supplier GetSupplierDetails(int supplierId, int companyId) { using (var context = new WareMasterContext()) { var supplier = context.Suppliers .Include(s => s.Products) .SingleOrDefault(s => s.Id == supplierId); if (supplier != null && supplier.CompanyId == companyId) { return(supplier); } return(null); } }
public List <Product> GetProductsUncontainedInOrder(int orderId, int companyId) { using (var context = new WareMasterContext()) { var orderToExcludeFrom = context.Orders.Include(order => order.ProductOrders).SingleOrDefault(order => order.Id == orderId); if (orderToExcludeFrom == null) { return(null); } var orderProductIdsToLeaveOut = orderToExcludeFrom.ProductOrders.Select(product => product.ProductId); return(context.Products.Where(product => product.CompanyId == companyId && orderProductIdsToLeaveOut.All(pr => pr != product.Id)) .ToList()); } }
public Product GetProductDetails(int productId, int companyId) { using (var context = new WareMasterContext()) { var product = context.Products .Include(p => p.Suppliers) .SingleOrDefault(p => p.Id == productId); if (product != null && product.CompanyId == companyId) { return(product); } return(null); } }
public int AddNewCompany(string companyToAddName) { using (var context = new WareMasterContext()) { var companyToAdd = new Company { Name = companyToAddName }; context.Companies.Add(companyToAdd); context.SaveChanges(); return(companyToAdd.Id); } }
public void AddNewSupplier(Supplier supplier) { using (var context = new WareMasterContext()) { supplier.Company = context.Companies.Find(supplier.CompanyId); context.Companies.Attach(supplier.Company); foreach (var product in supplier.Products) { context.Products.Attach(product); } context.Suppliers.Add(supplier); context.SaveChanges(); } }
public User GetUserDetails(int userId, int companyId) { using (var context = new WareMasterContext()) { var user = context.Users .Include(u => u.EmployeeOrders) .Include(u => u.ManagerOrders) .Include(u => u.ActivityLogs) .FirstOrDefault(u => u.Id == userId); if (user != null && user.CompanyId == companyId && user.Role != Role.Manager) { return(user); } return(null); } }
public void DeleteProduct(int productId) { using (var context = new WareMasterContext()) { var productToDelete = context.Products.Include(product => product.ProductOrders) .FirstOrDefault(product => product.Id == productId); if (productToDelete == null) { return; } context.Products.Remove(productToDelete); context.SaveChanges(); } }
public bool EditOrder(Order editedOrder) { using (var context = new WareMasterContext()) { var orderToEdit = context.Orders .Include(order => order.ProductOrders) .Include(o => o.ProductOrders.Select(x => x.Product)) .Include(order => order.AssignedEmployee) .SingleOrDefault(order => order.Id == editedOrder.Id); if (orderToEdit == null || orderToEdit.Status == Status.Finished) { return(false); } orderToEdit.Status = editedOrder.Status; if (orderToEdit.AssignedEmployee != null && editedOrder.AssignedEmployee != null && orderToEdit.AssignedEmployee.Id != editedOrder.AssignedEmployee.Id || orderToEdit.AssignedEmployee == null && editedOrder.AssignedEmployee != null) { context.Users.Attach(editedOrder.AssignedEmployee); orderToEdit.AssignedEmployee = editedOrder.AssignedEmployee; } else if (editedOrder.AssignedEmployee == null) { orderToEdit.AssignedEmployee = null; } if (editedOrder.Status == Status.Finished) { foreach (var productOrder in orderToEdit.ProductOrders) { productOrder.Product.Counter += productOrder.ProductQuantity; } } orderToEdit.Status = editedOrder.Status; orderToEdit.ProductOrders = editedOrder.ProductOrders; orderToEdit.Note = editedOrder.Note; context.SaveChanges(); return(true); } }
public bool FinishOrder(int orderId, JObject takenProducts) { using (var context = new WareMasterContext()) { var orderToFinish = context.Orders.Include(order => order.ProductOrders) .Include(o => o.ProductOrders.Select(x => x.Product)) .Include(order => order.AssignedEmployee) .SingleOrDefault(order => order.Id == orderId); if (orderToFinish == null) { return(false); } orderToFinish.Note = "Narudžbu obradio: " + orderToFinish.AssignedEmployee.FirstName + " " + orderToFinish.AssignedEmployee.LastName + "\n"; var productsToCheck = new List <ProductOrders>(orderToFinish.ProductOrders); foreach (var takenProduct in takenProducts) { var productOrder = orderToFinish.ProductOrders.SingleOrDefault(pOrder => pOrder.ProductId == int.Parse(takenProduct.Key)); productsToCheck.Remove(productOrder); var numberOfTaken = takenProduct.Value.ToObject <int>(); if (productOrder == null || numberOfTaken > productOrder.ProductQuantity || numberOfTaken < 0 || numberOfTaken > productOrder.Product.Counter) { return(false); } productOrder.Product.Counter -= numberOfTaken; if (numberOfTaken < productOrder.ProductQuantity) { orderToFinish.Note += "Uzeto je " + numberOfTaken + "/" + productOrder.ProductQuantity + "proizvoda" + productOrder.Product.Name + "\n"; } } foreach (var unsentProductOrder in productsToCheck) { orderToFinish.Note += "Uzeto je 0" + "/" + unsentProductOrder.ProductQuantity + " proizvoda" + unsentProductOrder.Product.Name + "\n"; } orderToFinish.Note += "Svi ostali proizvodi su dobro prikupljeni."; orderToFinish.Status = Status.Finished; context.SaveChanges(); return(true); } }
public void AddActivityLog(ActivityLog activityLogToAdd) { using (var context = new WareMasterContext()) { activityLogToAdd.User = context.Users.Find(activityLogToAdd.UserId); activityLogToAdd.Company = context.Companies.Find(activityLogToAdd.CompanyId); if (activityLogToAdd.User != null) { context.Users.Attach(activityLogToAdd.User); } context.Companies.Attach(activityLogToAdd.Company); activityLogToAdd.TimeOfActivity = DateTime.Now; context.ActivityLogs.Add(activityLogToAdd); context.SaveChanges(); } }