public static void AddAlertToAllInstances() { DbContextOptionsBuilder <ManufacturingContext> optionsBuilder = new DbContextOptionsBuilder <ManufacturingContext>(); optionsBuilder.UseSqlServer("server=192.168.0.5;database=manufacturing_inventory_dev;user=aelmendorf;password=Drizzle123!;MultipleActiveResultSets=true"); using var context = new ManufacturingContext(optionsBuilder.Options); var user = context.Users .Include(e => e.Sessions) .ThenInclude(e => e.Transactions) .Include(e => e.Permission) .FirstOrDefault(e => e.FirstName == "Andrew"); UserService userService = new UserService(); if (user != null) { Session session = new Session(user); context.Sessions.Add(session); context.SaveChanges(); userService.CurrentUserName = user.UserName; userService.CurrentSessionId = session.Id; userService.UserPermissionName = user.Permission.Name; } var partInstances = context.PartInstances.Include(e => e.IndividualAlert).Include(e => e.StockType).ThenInclude(e => e.CombinedAlert); List <Task> tasks = new List <Task>(); foreach (var instance in partInstances) { if (instance.StockType.IsDefault) { //individual alert if (instance.IndividualAlert == null) { Console.WriteLine("Individual Alert, PartInstance: {0}", instance.Name); IndividualAlert alert = new IndividualAlert(); alert.PartInstance = instance; instance.IndividualAlert = alert; context.Add(alert); } } else { //combined alert if (instance.StockType.CombinedAlert == null) { Console.WriteLine("Combined Alert, StockType: {0}", instance.StockType.Name); CombinedAlert alert = new CombinedAlert(); alert.StockHolder = instance.StockType; context.Add(alert); } } } context.SaveChanges(); }
public static async Task WorkingWithIndividualAlerts() { DbContextOptionsBuilder <ManufacturingContext> optionsBuilder = new DbContextOptionsBuilder <ManufacturingContext>(); optionsBuilder.UseSqlServer("server=172.27.192.1;database=manufacturing_inventory;user=aelmendorf;password=Drizzle123!;MultipleActiveResultSets=true"); using var context = new ManufacturingContext(optionsBuilder.Options); var user = context.Users .Include(e => e.Sessions) .ThenInclude(e => e.Transactions) .Include(e => e.Permission) .FirstOrDefault(e => e.FirstName == "Andrew"); UserService userService = new UserService(); if (user != null) { Session session = new Session(user); await context.Sessions.AddAsync(session); await context.SaveChangesAsync(); userService.CurrentUserName = user.UserName; userService.CurrentSessionId = session.Id; userService.UserPermissionName = user.Permission.Name; } var partInstance = await context.PartInstances.Include(e => e.BubblerParameter).Include(e => e.IndividualAlert).FirstOrDefaultAsync(e => e.Id == 1); if (partInstance != null) { IndividualAlert alert = new IndividualAlert(); alert.PartInstance = partInstance; UserAlert userAlert = new UserAlert(); userAlert.Alert = alert; userAlert.User = user; var added = await context.AddAsync(userAlert); if (added != null) { await context.SaveChangesAsync(); Console.WriteLine("Should be saved"); } else { Console.WriteLine("Failed to add Alert"); } } else { Console.WriteLine("PartInstance Not Found"); } }
public async Task <CheckInOutput> ExecuteNewPrice(CheckInInput input) { var part = await this._partRepository.GetEntityAsync(e => e.Id == input.PartId); if (part != null) { input.PartInstance.PartId = input.PartId; input.PartInstance.Price = input.Price; input.PartInstance.UpdatePrice(); var instanceEntity = await this._partInstanceRepository.AddAsync(input.PartInstance); if (instanceEntity != null) { PartPrice partPrice = new PartPrice(part, instanceEntity.Price); PriceLog priceLog = new PriceLog(instanceEntity, instanceEntity.Price); await this._partPriceRepository.AddAsync(partPrice); await this._priceLogRepository.AddAsync(priceLog); Transaction transaction = new Transaction(); transaction.SetupCheckIn(instanceEntity, InventoryAction.INCOMING, instanceEntity.LocationId, input.TimeStamp); transaction.SessionId = this._userService.CurrentSessionId.Value; var stockType = (StockType)await this._categoryRepository.GetEntityAsync(e => e.Id == instanceEntity.StockTypeId); if (stockType != null) { if (!stockType.IsDefault) { if (stockType != null) { if (instanceEntity.IsBubbler) { stockType.Quantity += (int)instanceEntity.BubblerParameter.Weight; } else { stockType.Quantity += instanceEntity.Quantity; } await this._categoryRepository.UpdateAsync(stockType); } } else { IndividualAlert alert = new IndividualAlert(); alert.PartInstance = instanceEntity; instanceEntity.IndividualAlert = alert; this._context.Add(alert); } } else { await this._unitOfWork.Undo(); return(new CheckInOutput(null, false, "Error: Could not adjust stock, Please contact administrator")); } var tranEntity = await this._transactionRepository.AddAsync(transaction); var count = await this._unitOfWork.Save(); if (count > 0) { return(new CheckInOutput(instanceEntity, true, "Part Checked In!")); } else { await this._unitOfWork.Undo(); return(new CheckInOutput(null, false, "Error: Check in Failed")); } } else { await this._unitOfWork.Undo(); return(new CheckInOutput(null, false, "Error: Could Not Create Part Instance")); } } else { await this._unitOfWork.Undo(); return(new CheckInOutput(null, false, "Error: Part Not Found")); } }
private async Task <CheckInOutput> ExecuteAddToExisiting(CheckInInput input) { var partInstance = await this._partInstanceRepository.GetEntityAsync(e => e.Id == input.PartInstance.Id); if (partInstance != null) { partInstance.UpdateQuantity(input.Quantity.Value); Transaction transaction = new Transaction(partInstance, InventoryAction.INCOMING, 0, 0, partInstance.CurrentLocation, input.TimeStamp); transaction.Quantity = input.Quantity.Value; transaction.UnitCost = partInstance.UnitCost; transaction.TotalCost = transaction.UnitCost * transaction.Quantity; transaction.SessionId = this._userService.CurrentSessionId.Value; var stockType = (StockType)await this._categoryRepository.GetEntityAsync(e => e.Id == partInstance.StockType.Id); if (stockType != null) { if (!stockType.IsDefault) { if (stockType != null) { if (partInstance.IsBubbler) { stockType.Quantity += (int)partInstance.BubblerParameter.Weight; } else { stockType.Quantity += input.Quantity.Value; } await this._categoryRepository.UpdateAsync(stockType); } } else { IndividualAlert alert = new IndividualAlert(); alert.PartInstance = partInstance; partInstance.IndividualAlert = alert; this._context.Add(alert); } } else { await this._unitOfWork.Undo(); return(new CheckInOutput(null, false, "Error: Could not adjust stock, Please contact administrator")); } var instance = await this._partInstanceRepository.UpdateAsync(partInstance); var trans = await this._transactionRepository.AddAsync(transaction); StringBuilder builder = new StringBuilder(); if (instance != null && trans != null) { var val = await this._unitOfWork.Save(); builder.AppendFormat("Success: {0} added to {1}", instance.Name, trans.Quantity); return(new CheckInOutput(instance, true, builder.ToString())); } else { await this._unitOfWork.Undo(); builder.AppendFormat("Error: Check In Failed, Please Contact Admin").AppendLine(); return(new CheckInOutput(instance, false, builder.ToString())); } } else { return(new CheckInOutput(null, false, "Error: Part Instance not found")); } }
public async Task <CategoryBoundaryOutput> RemovePartFrom(int entityId, CategoryDTO category) { if (category.Type == CategoryTypes.Organization) { var part = await this._partRepository.GetEntityAsync(e => e.Id == entityId); if (part != null) { part.OrganizationId = null; part.Organization = null; var updated = await this._partRepository.UpdateAsync(part); if (updated != null) { await this._unitOfWork.Save(); return(new CategoryBoundaryOutput(null, true, "Part " + part.Name + " Removed Successfully")); } else { await this._unitOfWork.Undo(); return(new CategoryBoundaryOutput(null, false, "PartInstance " + part.Name + " Failed to Add")); } } else { await this._unitOfWork.Undo(); return(new CategoryBoundaryOutput(null, false, "Error: Part Not Found!")); } } else { var instance = await this._instanceRepository.GetEntityAsync(e => e.Id == entityId); var exisitingCategory = await this._categoryRepository.GetEntityAsync(e => e.Id == category.Id); if (instance == null || exisitingCategory == null) { var msg = (instance == null) ? "PartInstance Id " + entityId + "Not Found" : "Category Not Found"; return(new CategoryBoundaryOutput(null, false, "Error: " + msg)); } switch (category.Type) { case CategoryTypes.Condition: instance.Condition = null; instance.ConditionId = null; break; case CategoryTypes.StockType: if (!exisitingCategory.IsDefault) { var newCategory = await this._context.Categories.OfType <StockType>().Include(e => e.PartInstances).FirstOrDefaultAsync(e => e.IsDefault); if (newCategory != null) { if (instance.IsBubbler) { ((StockType)exisitingCategory).Quantity -= (int)instance.BubblerParameter.Weight; } else { ((StockType)exisitingCategory).Quantity -= (int)instance.Quantity; } IndividualAlert alert = new IndividualAlert(); alert.PartInstance = instance; instance.IndividualAlert = alert; var added = this._context.Alerts.Add(alert); instance.StockType = newCategory; instance.StockTypeId = newCategory.Id; var oldUpdated = await this._categoryRepository.UpdateAsync(exisitingCategory); if (added == null || oldUpdated == null) { await this._unitOfWork.Undo(); return(new CategoryBoundaryOutput(null, false, "Error: Could not update alerts and old category,Please contact administrator")); } } else { await this._unitOfWork.Undo(); return(new CategoryBoundaryOutput(null, false, "Error: Could not remove from category, please contatc administrator")); } } else { return(new CategoryBoundaryOutput(null, false, "Error: Cannot remove from default category" + Environment.NewLine + "To change category select desired category then add part instance")); } break; case CategoryTypes.Usage: instance.UsageId = null; instance.Usage = null; break; } var instanceUpdated = await this._instanceRepository.UpdateAsync(instance); if (instanceUpdated != null) { await this._unitOfWork.Save(); return(new CategoryBoundaryOutput(exisitingCategory, true, "PartInstance " + instance.Name + " Removed Successfully")); } else { await this._unitOfWork.Undo(); return(new CategoryBoundaryOutput(null, false, "PartInstance " + instance.Name + " Failed to Remove")); } } }
public async Task <CategoryBoundaryOutput> AddPartTo(int entityId, CategoryDTO category) { if (category.Type == CategoryTypes.Organization) { var part = await this._partRepository.GetEntityAsync(e => e.Id == entityId); var newCategory = await this._categoryRepository.GetEntityAsync(e => e.Id == category.Id); if (part == null || newCategory == null) { var msg = (part == null) ? "Part Not Found" : "Category Not Found"; return(new CategoryBoundaryOutput(null, false, "Error: " + msg)); } part.OrganizationId = newCategory.Id; var updated = await this._partRepository.UpdateAsync(part); if (updated != null) { await this._unitOfWork.Save(); return(new CategoryBoundaryOutput(newCategory, true, "PartInstance " + part.Name + " Added Successfully")); } else { await this._unitOfWork.Undo(); return(new CategoryBoundaryOutput(null, false, "PartInstance " + part.Name + " Failed to Add")); } } else { var instance = await this._instanceRepository.GetEntityAsync(e => e.Id == entityId); var newCategory = await this._categoryRepository.GetEntityAsync(e => e.Id == category.Id); if (instance == null || newCategory == null) { var msg = (instance == null) ? "PartInstance Id " + entityId + "Not Found" : "Category Not Found"; return(new CategoryBoundaryOutput(null, false, "Error: " + msg)); } bool error = false; switch (category.Type) { case CategoryTypes.Condition: instance.ConditionId = newCategory.Id; break; case CategoryTypes.StockType: var oldCategory = await this._categoryRepository.GetEntityAsync(e => e.Id == instance.StockTypeId); if (oldCategory != null) { instance.StockTypeId = newCategory.Id; if (oldCategory.IsDefault) { if (instance.IsBubbler) { ((StockType)newCategory).Quantity += (int)instance.BubblerParameter.Weight; } else { ((StockType)newCategory).Quantity += (int)instance.Quantity; } var userAlerts = this._context.UserAlerts.Where(e => e.AlertId == instance.IndividualAlertId); if (userAlerts.Count() > 0) { this._context.RemoveRange(userAlerts); } this._context.Alerts.Remove(instance.IndividualAlert); instance.IndividualAlert = null; } else { if (instance.IsBubbler) { ((StockType)oldCategory).Quantity -= (int)instance.BubblerParameter.Weight; } else { ((StockType)oldCategory).Quantity -= (int)instance.Quantity; } if (newCategory.IsDefault) { IndividualAlert alert = new IndividualAlert(); alert.PartInstance = instance; instance.IndividualAlert = alert; var added = this._context.Alerts.Add(alert); if (added == null) { return(new CategoryBoundaryOutput(null, false, "Error: Could not creat new IndividualAlert,Please contact administrator")); } } else { if (instance.IsBubbler) { //((StockType)oldCategory).Quantity -= (int)instance.BubblerParameter.Weight; ((StockType)newCategory).Quantity += (int)instance.BubblerParameter.Weight; } else { //((StockType)oldCategory).Quantity -= (int)instance.Quantity; ((StockType)newCategory).Quantity += (int)instance.Quantity; } } } var oldUpdated = await this._categoryRepository.UpdateAsync(oldCategory); error = !(oldUpdated != null); } else { error = true; } break; case CategoryTypes.Usage: instance.UsageId = newCategory.Id; break; } if (!error) { var catUpdated = await this._categoryRepository.UpdateAsync(newCategory); var instanceUpdated = await this._instanceRepository.UpdateAsync(instance); if (instanceUpdated != null && catUpdated != null) { await this._unitOfWork.Save(); return(new CategoryBoundaryOutput(newCategory, true, "PartInstance " + instance.Name + " Added Successfully")); } else { await this._unitOfWork.Undo(); return(new CategoryBoundaryOutput(null, false, "PartInstance " + instance.Name + " Failed to Add")); } } else { await this._unitOfWork.Undo(); return(new CategoryBoundaryOutput(null, false, "PartInstance " + instance.Name + " Failed to Add")); } } }
public static async Task ChangeStockTypeThenAlert() { DbContextOptionsBuilder <ManufacturingContext> optionsBuilder = new DbContextOptionsBuilder <ManufacturingContext>(); optionsBuilder.UseSqlServer("server=172.27.192.1;database=manufacturing_inventory;user=aelmendorf;password=Drizzle123!;MultipleActiveResultSets=true"); using var context = new ManufacturingContext(optionsBuilder.Options); var user = context.Users .Include(e => e.Sessions) .ThenInclude(e => e.Transactions) .Include(e => e.Permission) .FirstOrDefault(e => e.FirstName == "Andrew"); UserService userService = new UserService(); if (user != null) { Session session = new Session(user); context.Sessions.Add(session); context.SaveChanges(); userService.CurrentUserName = user.UserName; userService.CurrentSessionId = session.Id; userService.UserPermissionName = user.Permission.Name; } var partInstance = await context.PartInstances.Include(e => e.IndividualAlert).Include(e => e.StockType).ThenInclude(e => e.CombinedAlert).FirstOrDefaultAsync(e => e.Id == 66); var newStockType = await context.Categories.OfType <StockType>().Include(e => e.CombinedAlert).ThenInclude(e => e.UserAlerts).FirstOrDefaultAsync(e => e.Id == 14); if (partInstance != null && newStockType != null) { if (newStockType.IsDefault) { if (!partInstance.StockType.IsDefault) { //from combinded to individual Console.WriteLine("Combined to Individual"); IndividualAlert alert = new IndividualAlert(); alert.PartInstance = partInstance; partInstance.IndividualAlert = alert; context.Add(alert); partInstance.StockType = newStockType; context.Update(partInstance); await context.SaveChangesAsync(); Console.WriteLine("Case one should be done"); } else { //from individual to individual. Should never be here Console.WriteLine("You should not be here"); } } else { if (partInstance.StockType.IsDefault) { if (partInstance.IndividualAlert != null) { //from individual to combined var userAlerts = context.UserAlerts.Where(e => e.AlertId == partInstance.IndividualAlertId); context.RemoveRange(userAlerts); var deleted = context.Alerts.Remove(partInstance.IndividualAlert); partInstance.IndividualAlert = null; partInstance.StockType = newStockType; newStockType.Quantity += partInstance.Quantity; context.Update(newStockType); await context.SaveChangesAsync(); Console.WriteLine("Should be done"); } else { Console.WriteLine("You should not be here"); } } else { //from combined to another combined Console.WriteLine("Combined to Combined"); var oldStock = context.Entry <StockType>(partInstance.StockType).Entity; oldStock.Quantity -= partInstance.Quantity; var okay = oldStock.PartInstances.Remove(partInstance); partInstance.StockType = newStockType; newStockType.PartInstances.Add(partInstance); newStockType.Quantity += partInstance.Quantity; context.Update(newStockType); context.Update(oldStock); context.Update(partInstance); await context.SaveChangesAsync(); Console.WriteLine("Should be finished"); } } Console.WriteLine("Done, Press any key to exit"); } else { Console.WriteLine("PartInstance not found"); } }