예제 #1
0
        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();
            }
        }
예제 #2
0
        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);
            }
        }
예제 #3
0
        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);
            }
        }
예제 #4
0
        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();
            }
        }
예제 #5
0
        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);
            }
        }
예제 #6
0
        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();
            }
        }
예제 #7
0
        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);
            }
        }
예제 #8
0
        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);
            }
        }
예제 #9
0
        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();
            }
        }
예제 #10
0
        public void DeleteSupplier(int supplierId)
        {
            using (var context = new WareMasterContext())
            {
                var supplierToDelete = context.Suppliers.SingleOrDefault(supplier => supplier.Id == supplierId);

                if (supplierToDelete == null)
                {
                    return;
                }

                var supplierToDeleteOrders = context.Orders.Include(order => order.ProductOrders)
                                             .Where(order => order.SupplierId == supplierToDelete.Id);
                context.Orders.RemoveRange(supplierToDeleteOrders);

                context.Suppliers.Remove(supplierToDelete);
                context.SaveChanges();
            }
        }
예제 #11
0
        public void EditProduct(Product editedProduct)
        {
            using (var context = new WareMasterContext())
            {
                var productToEdit = context.Products
                                    .SingleOrDefault(product => product.Id == editedProduct.Id);

                if (productToEdit == null)
                {
                    return;
                }

                productToEdit.Name     = editedProduct.Name;
                productToEdit.Counter  = editedProduct.Counter;
                productToEdit.Barcode  = editedProduct.Barcode;
                productToEdit.ImageUrl = editedProduct.ImageUrl;

                context.SaveChanges();
            }
        }
예제 #12
0
        public bool DeleteOrder(int orderId)
        {
            using (var context = new WareMasterContext())
            {
                var orderToDelete = context.Orders
                                    .Include(order => order.ProductOrders)
                                    .FirstOrDefault(order => order.Id == orderId);

                if (orderToDelete == null ||
                    orderToDelete.Status == Status.InProgress ||
                    orderToDelete.Status == Status.Finished)
                {
                    return(false);
                }

                context.Orders.Remove(orderToDelete);
                context.SaveChanges();

                return(true);
            }
        }
예제 #13
0
        public void EditUser(User editedUser)
        {
            using (var context = new WareMasterContext())
            {
                var userToEdit = context.Users
                                 .SingleOrDefault(user => user.Id == editedUser.Id);

                if (userToEdit == null)
                {
                    return;
                }

                userToEdit.FirstName = editedUser.FirstName;
                userToEdit.LastName  = editedUser.LastName;
                userToEdit.Username  = editedUser.Username;
                userToEdit.Password  = editedUser.Password;
                userToEdit.ImageUrl  = editedUser.ImageUrl;
                userToEdit.Role      = editedUser.Role;

                context.SaveChanges();
            }
        }
예제 #14
0
        public int AddNewOrder(Order order)
        {
            using (var context = new WareMasterContext())
            {
                var newOrder = new Order()
                {
                    AssignedEmployeeId = order.AssignedEmployeeId,
                    AssignedManagerId  = order.AssignedManagerId,
                    TimeOfCreation     = DateTime.Now,
                    Status             = order.Status,
                    Type          = order.Type,
                    CompanyId     = order.CompanyId,
                    SupplierId    = order.SupplierId,
                    ProductOrders = order.ProductOrders
                };

                context.Orders.Add(newOrder);
                context.SaveChanges();

                return(newOrder.Id);
            }
        }
예제 #15
0
        public bool ConfirmIncomingOrder(int orderId)
        {
            using (var context = new WareMasterContext())
            {
                var orderToConfirm = context.Orders.Include(order => order.Supplier).SingleOrDefault(order => order.Id == orderId);
                if (orderToConfirm == null || orderToConfirm.Status != Status.Created)
                {
                    return(false);
                }
                orderToConfirm.Status = Status.InProgress;
                context.SaveChanges();
                var activityLog = new ActivityLog()
                {
                    CompanyId = orderToConfirm.CompanyId,
                    Text      = "Dobavljač " + orderToConfirm.Supplier.Name + " je potvrdio ulaznu narudžbu."
                };
                var activityLogRepository = new ActivityLogRepository();
                activityLogRepository.AddActivityLog(activityLog);

                return(true);
            }
        }
예제 #16
0
        public void EditSupplier(Supplier editedSupplier)
        {
            using (var context = new WareMasterContext())
            {
                foreach (var product in editedSupplier.Products)
                {
                    context.Products.Attach(product);
                }

                var supplierToEdit = context.Suppliers
                                     .Include(supplier => supplier.Products)
                                     .SingleOrDefault(supplier => supplier.Id == editedSupplier.Id);

                if (supplierToEdit == null)
                {
                    return;
                }

                var deletedProducts       = supplierToEdit.Products.Except(editedSupplier.Products).ToArray();
                var productOrdersToDelete = new List <ProductOrders>();
                foreach (var product in deletedProducts)
                {
                    productOrdersToDelete.AddRange(context.ProductOrders.Where(productOrder =>
                                                                               productOrder.Order.SupplierId == editedSupplier.Id &&
                                                                               productOrder.ProductId == product.Id &&
                                                                               productOrder.Order.Status == Status.Created));
                }
                context.ProductOrders.RemoveRange(productOrdersToDelete);

                supplierToEdit.Name     = editedSupplier.Name;
                supplierToEdit.Email    = editedSupplier.Email;
                supplierToEdit.Products = editedSupplier.Products;

                context.SaveChanges();
            }
        }